Generate refcounting procs in gen_dev

This commit is contained in:
Brian Carroll 2021-12-02 15:41:09 +00:00
parent d0ef1e5b36
commit e088eceb29
4 changed files with 57 additions and 7 deletions

View file

@ -534,7 +534,9 @@ fn gen_from_mono_module_dev_assembly(
.. ..
} = loaded; } = loaded;
let mut ident_ids = get_module_ident_ids(&interns.all_ident_ids, &module_id).unwrap().clone(); let mut ident_ids = get_module_ident_ids(&interns.all_ident_ids, &module_id)
.unwrap()
.clone();
let env = roc_gen_dev::Env { let env = roc_gen_dev::Env {
arena, arena,

View file

@ -4,7 +4,7 @@ use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_collections::all::{MutMap, MutSet}; use roc_collections::all::{MutMap, MutSet};
use roc_module::symbol::{IdentIds, Symbol}; use roc_module::symbol::{IdentIds, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator; use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::ir::{BranchInfo, JoinPointId, Literal, Param, SelfRecursive, Stmt}; use roc_mono::ir::{BranchInfo, JoinPointId, Literal, Param, ProcLayout, SelfRecursive, Stmt};
use roc_mono::layout::{Builtin, Layout}; use roc_mono::layout::{Builtin, Layout};
use roc_reporting::internal_error; use roc_reporting::internal_error;
use std::marker::PhantomData; use std::marker::PhantomData;
@ -228,6 +228,7 @@ pub struct Backend64Bit<
phantom_cc: PhantomData<CC>, phantom_cc: PhantomData<CC>,
env: &'a Env<'a>, env: &'a Env<'a>,
refcount_proc_gen: RefcountProcGenerator<'a>, refcount_proc_gen: RefcountProcGenerator<'a>,
refcount_proc_symbols: Vec<'a, (Symbol, ProcLayout<'a>)>,
buf: Vec<'a, u8>, buf: Vec<'a, u8>,
relocs: Vec<'a, Relocation>, relocs: Vec<'a, Relocation>,
proc_name: Option<String>, proc_name: Option<String>,
@ -276,6 +277,7 @@ impl<
phantom_cc: PhantomData, phantom_cc: PhantomData,
env, env,
refcount_proc_gen: RefcountProcGenerator::new(env.arena, IntWidth::I64, env.module_id), refcount_proc_gen: RefcountProcGenerator::new(env.arena, IntWidth::I64, env.module_id),
refcount_proc_symbols: bumpalo::vec![in env.arena],
proc_name: None, proc_name: None,
is_self_recursive: None, is_self_recursive: None,
buf: bumpalo::vec![in env.arena], buf: bumpalo::vec![in env.arena],
@ -304,6 +306,9 @@ impl<
fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a> { fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a> {
&mut self.refcount_proc_gen &mut self.refcount_proc_gen
} }
fn refcount_proc_symbols_mut(&mut self) -> &mut Vec<'a, (Symbol, ProcLayout<'a>)> {
&mut self.refcount_proc_symbols
}
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive) { fn reset(&mut self, name: String, is_self_recursive: SelfRecursive) {
self.proc_name = Some(name); self.proc_name = Some(name);

View file

@ -11,7 +11,7 @@ use roc_module::symbol::{IdentIds, Interns, ModuleId, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator; use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::ir::{ use roc_mono::ir::{
BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Param, Proc, BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Param, Proc,
SelfRecursive, Stmt, SelfRecursive, Stmt, ProcLayout,
}; };
use roc_mono::layout::{Builtin, Layout, LayoutIds}; use roc_mono::layout::{Builtin, Layout, LayoutIds};
use roc_reporting::internal_error; use roc_reporting::internal_error;
@ -67,6 +67,8 @@ where
fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a>; fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a>;
fn refcount_proc_symbols_mut(&mut self) -> &mut Vec<'a, (Symbol, ProcLayout<'a>)>;
/// reset resets any registers or other values that may be occupied at the end of a procedure. /// reset resets any registers or other values that may be occupied at the end of a procedure.
/// It also passes basic procedure information to the builder for setup of the next function. /// It also passes basic procedure information to the builder for setup of the next function.
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive); fn reset(&mut self, name: String, is_self_recursive: SelfRecursive);
@ -125,12 +127,15 @@ where
// Expand the Refcounting statement into more detailed IR with a function call // Expand the Refcounting statement into more detailed IR with a function call
// If this layout requires a new RC proc, we get enough info to create a linker symbol // If this layout requires a new RC proc, we get enough info to create a linker symbol
// for it. Here we don't create linker symbols at this time, but in Wasm backend, we do. // for it. Here we don't create linker symbols at this time, but in Wasm backend, we do.
let (_rc_stmt, _new_proc_info) = self let (rc_stmt, new_proc_info) = self
.refcount_proc_gen_mut() .refcount_proc_gen_mut()
.expand_refcount_stmt(ident_ids, layout, modify, *following); .expand_refcount_stmt(ident_ids, layout, modify, *following);
// TODO: actually use the rc_stmt!! For now just trying to satisfy the borrow checker if let Some((rc_proc_symbol, rc_proc_layout)) = new_proc_info {
self.build_stmt(ident_ids, *following, ret_layout) self.refcount_proc_symbols_mut().push((rc_proc_symbol, rc_proc_layout));
}
self.build_stmt(ident_ids, &rc_stmt, ret_layout)
} }
Stmt::Switch { Stmt::Switch {
cond_symbol, cond_symbol,

View file

@ -218,6 +218,7 @@ fn build_object<'a, B: Backend<'a>>(
let mut layout_ids = LayoutIds::default(); let mut layout_ids = LayoutIds::default();
let mut procs = Vec::with_capacity_in(procedures.len(), env.arena); let mut procs = Vec::with_capacity_in(procedures.len(), env.arena);
// Names and linker data for user procedures
for ((sym, layout), proc) in procedures { for ((sym, layout), proc) in procedures {
build_proc_symbol( build_proc_symbol(
&mut output, &mut output,
@ -230,7 +231,7 @@ fn build_object<'a, B: Backend<'a>>(
) )
} }
// Build procedures. // Build procedures from user code
let mut relocations = bumpalo::vec![in env.arena]; let mut relocations = bumpalo::vec![in env.arena];
for (fn_name, section_id, proc_id, proc) in procs { for (fn_name, section_id, proc_id, proc) in procs {
build_proc( build_proc(
@ -245,6 +246,43 @@ fn build_object<'a, B: Backend<'a>>(
proc, proc,
) )
} }
// Generate IR for refcounting procedures
let rc_proc_gen = backend.refcount_proc_gen_mut();
let rc_procs = rc_proc_gen.generate_refcount_procs(env.arena, ident_ids);
let rc_symbols_and_layouts = backend.refcount_proc_symbols_mut();
let mut rc_names_symbols_procs = Vec::with_capacity_in(rc_procs.len(), env.arena);
env.module_id.register_debug_idents(ident_ids);
// Names and linker data for refcounting procedures
for ((sym, layout), proc) in rc_symbols_and_layouts.into_iter().zip(rc_procs) {
build_proc_symbol(
&mut output,
&mut layout_ids,
&mut rc_names_symbols_procs,
env,
*sym,
*layout,
proc,
)
}
// Build refcounting procedures
for (fn_name, section_id, proc_id, proc) in rc_names_symbols_procs {
build_proc(
&mut output,
&mut backend,
&mut relocations,
ident_ids,
data_section,
fn_name,
section_id,
proc_id,
proc,
)
}
// Relocations for all procedures (user code & refcounting)
for (section_id, reloc) in relocations { for (section_id, reloc) in relocations {
match output.add_relocation(section_id, reloc) { match output.add_relocation(section_id, reloc) {
Ok(obj) => obj, Ok(obj) => obj,