Route IdentIds to refcount proc generator in dev backend

This commit is contained in:
Brian Carroll 2021-12-02 09:58:34 +00:00
parent b193483756
commit d0ef1e5b36
5 changed files with 77 additions and 28 deletions

View file

@ -2,7 +2,8 @@ use crate::{Backend, Env, Relocation};
use bumpalo::collections::Vec;
use roc_builtins::bitcode::{FloatWidth, IntWidth};
use roc_collections::all::{MutMap, MutSet};
use roc_module::symbol::Symbol;
use roc_module::symbol::{IdentIds, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::ir::{BranchInfo, JoinPointId, Literal, Param, SelfRecursive, Stmt};
use roc_mono::layout::{Builtin, Layout};
use roc_reporting::internal_error;
@ -226,6 +227,7 @@ pub struct Backend64Bit<
phantom_asm: PhantomData<ASM>,
phantom_cc: PhantomData<CC>,
env: &'a Env<'a>,
refcount_proc_gen: RefcountProcGenerator<'a>,
buf: Vec<'a, u8>,
relocs: Vec<'a, Relocation>,
proc_name: Option<String>,
@ -273,6 +275,7 @@ impl<
phantom_asm: PhantomData,
phantom_cc: PhantomData,
env,
refcount_proc_gen: RefcountProcGenerator::new(env.arena, IntWidth::I64, env.module_id),
proc_name: None,
is_self_recursive: None,
buf: bumpalo::vec![in env.arena],
@ -298,6 +301,9 @@ impl<
fn env(&self) -> &'a Env<'a> {
self.env
}
fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a> {
&mut self.refcount_proc_gen
}
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive) {
self.proc_name = Some(name);
@ -532,6 +538,7 @@ impl<
fn build_switch(
&mut self,
ident_ids: &mut IdentIds,
cond_symbol: &Symbol,
_cond_layout: &Layout<'a>, // cond_layout must be a integer due to potential jump table optimizations.
branches: &'a [(u64, BranchInfo<'a>, Stmt<'a>)],
@ -554,7 +561,7 @@ impl<
let start_offset = ASM::jne_reg64_imm64_imm32(&mut self.buf, cond_reg, *val, 0);
// Build all statements in this branch.
self.build_stmt(stmt, ret_layout);
self.build_stmt(ident_ids, stmt, ret_layout);
// Build unconditional jump to the end of this switch.
// Since we don't know the offset yet, set it to 0 and overwrite later.
@ -578,7 +585,7 @@ impl<
}
let (branch_info, stmt) = default_branch;
if let BranchInfo::None = branch_info {
self.build_stmt(stmt, ret_layout);
self.build_stmt(ident_ids, stmt, ret_layout);
// Update all return jumps to jump past the default case.
let ret_offset = self.buf.len();
@ -600,6 +607,7 @@ impl<
fn build_join(
&mut self,
ident_ids: &mut IdentIds,
id: &JoinPointId,
parameters: &'a [Param<'a>],
body: &'a Stmt<'a>,
@ -638,7 +646,7 @@ impl<
sub_backend.load_args(args.into_bump_slice(), ret_layout);
// Build all statements in body.
sub_backend.build_stmt(body, ret_layout);
sub_backend.build_stmt(ident_ids, body, ret_layout);
// Merge the "sub function" into the main function.
let sub_func_offset = self.buf.len() as u64;
@ -682,7 +690,7 @@ impl<
);
// Build remainder of function.
self.build_stmt(remainder, ret_layout)
self.build_stmt(ident_ids, remainder, ret_layout)
}
fn build_jump(

View file

@ -7,7 +7,8 @@ use roc_builtins::bitcode::{self, FloatWidth, IntWidth};
use roc_collections::all::{MutMap, MutSet};
use roc_module::ident::{ModuleName, TagName};
use roc_module::low_level::LowLevel;
use roc_module::symbol::{Interns, Symbol};
use roc_module::symbol::{IdentIds, Interns, ModuleId, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::ir::{
BranchInfo, CallType, Expr, JoinPointId, ListLiteralElement, Literal, Param, Proc,
SelfRecursive, Stmt,
@ -22,6 +23,7 @@ mod run_roc;
pub struct Env<'a> {
pub arena: &'a Bump,
pub module_id: ModuleId,
pub interns: Interns,
pub exposed_to_host: MutSet<Symbol>,
pub lazy_literals: bool,
@ -63,6 +65,8 @@ where
fn env(&self) -> &'a Env<'a>;
fn refcount_proc_gen_mut(&mut self) -> &mut RefcountProcGenerator<'a>;
/// 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.
fn reset(&mut self, name: String, is_self_recursive: SelfRecursive);
@ -81,7 +85,11 @@ where
fn build_wrapped_jmp(&mut self) -> (&'a [u8], u64);
/// build_proc creates a procedure and outputs it to the wrapped object writer.
fn build_proc(&mut self, proc: Proc<'a>) -> (&'a [u8], &[Relocation]) {
fn build_proc(
&mut self,
ident_ids: &mut IdentIds,
proc: Proc<'a>,
) -> (&'a [u8], &[Relocation]) {
let proc_name = LayoutIds::default()
.get(proc.name, &proc.ret_layout)
.to_symbol_string(proc.name, &self.env().interns);
@ -92,27 +100,37 @@ where
}
self.scan_ast(&proc.body);
self.create_free_map();
self.build_stmt(&proc.body, &proc.ret_layout);
self.build_stmt(ident_ids, &proc.body, &proc.ret_layout);
self.finalize()
}
/// build_stmt builds a statement and outputs at the end of the buffer.
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) {
fn build_stmt(&mut self, ident_ids: &mut IdentIds, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) {
match stmt {
Stmt::Let(sym, expr, layout, following) => {
self.build_expr(sym, expr, layout);
self.set_layout_map(*sym, layout);
self.free_symbols(stmt);
self.build_stmt(following, ret_layout);
self.build_stmt(ident_ids, following, ret_layout);
}
Stmt::Ret(sym) => {
self.load_literal_symbols(&[*sym]);
self.return_symbol(sym, ret_layout);
self.free_symbols(stmt);
}
Stmt::Refcounting(_modify, following) => {
// TODO: actually deal with refcounting. For hello world, we just skipped it.
self.build_stmt(following, ret_layout);
Stmt::Refcounting(modify, following) => {
let sym = modify.get_symbol();
let layout = self.layout_map().get(&sym).unwrap().clone();
// 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
// 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
.refcount_proc_gen_mut()
.expand_refcount_stmt(ident_ids, layout, modify, *following);
// TODO: actually use the rc_stmt!! For now just trying to satisfy the borrow checker
self.build_stmt(ident_ids, *following, ret_layout)
}
Stmt::Switch {
cond_symbol,
@ -123,6 +141,7 @@ where
} => {
self.load_literal_symbols(&[*cond_symbol]);
self.build_switch(
ident_ids,
cond_symbol,
cond_layout,
branches,
@ -140,7 +159,7 @@ where
for param in parameters.iter() {
self.set_layout_map(param.symbol, &param.layout);
}
self.build_join(id, parameters, body, remainder, ret_layout);
self.build_join(ident_ids, id, parameters, body, remainder, ret_layout);
self.free_symbols(stmt);
}
Stmt::Jump(id, args) => {
@ -164,6 +183,7 @@ where
// build_switch generates a instructions for a switch statement.
fn build_switch(
&mut self,
ident_ids: &mut IdentIds,
cond_symbol: &Symbol,
cond_layout: &Layout<'a>,
branches: &'a [(u64, BranchInfo<'a>, Stmt<'a>)],
@ -174,6 +194,7 @@ where
// build_join generates a instructions for a join statement.
fn build_join(
&mut self,
ident_ids: &mut IdentIds,
id: &JoinPointId,
parameters: &'a [Param<'a>],
body: &'a Stmt<'a>,

View file

@ -8,7 +8,7 @@ use object::{
SymbolFlags, SymbolKind, SymbolScope,
};
use roc_collections::all::MutMap;
use roc_module::symbol;
use roc_module::symbol::{self, IdentIds};
use roc_mono::ir::{Proc, ProcLayout};
use roc_mono::layout::LayoutIds;
use roc_reporting::internal_error;
@ -22,6 +22,7 @@ use target_lexicon::{Architecture as TargetArch, BinaryFormat as TargetBF, Tripl
/// It takes the request to build a module and output the object file for the module.
pub fn build_module<'a>(
env: &'a Env,
ident_ids: &'a mut IdentIds,
target: &Triple,
procedures: MutMap<(symbol::Symbol, ProcLayout<'a>), Proc<'a>>,
) -> Object {
@ -39,6 +40,7 @@ pub fn build_module<'a>(
> = Backend::new(env);
build_object(
env,
ident_ids,
procedures,
backend,
Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little),
@ -57,6 +59,7 @@ pub fn build_module<'a>(
> = Backend::new(env);
build_object(
env,
ident_ids,
procedures,
backend,
Object::new(
@ -79,6 +82,7 @@ pub fn build_module<'a>(
> = Backend::new(env);
build_object(
env,
ident_ids,
procedures,
backend,
Object::new(BinaryFormat::Elf, Architecture::Aarch64, Endianness::Little),
@ -97,6 +101,7 @@ pub fn build_module<'a>(
> = Backend::new(env);
build_object(
env,
ident_ids,
procedures,
backend,
Object::new(
@ -165,6 +170,7 @@ fn generate_wrapper<'a, B: Backend<'a>>(
fn build_object<'a, B: Backend<'a>>(
env: &'a Env,
ident_ids: &'a mut IdentIds,
procedures: MutMap<(symbol::Symbol, ProcLayout<'a>), Proc<'a>>,
mut backend: B,
mut output: Object,
@ -231,6 +237,7 @@ fn build_object<'a, B: Backend<'a>>(
&mut output,
&mut backend,
&mut relocations,
ident_ids,
data_section,
fn_name,
section_id,
@ -296,6 +303,7 @@ fn build_proc<'a, B: Backend<'a>>(
output: &mut Object,
backend: &mut B,
relocations: &mut Vec<'a, (SectionId, object::write::Relocation)>,
ident_ids: &mut IdentIds,
data_section: SectionId,
fn_name: String,
section_id: SectionId,
@ -303,7 +311,7 @@ fn build_proc<'a, B: Backend<'a>>(
proc: Proc<'a>,
) {
let mut local_data_index = 0;
let (proc_data, relocs) = backend.build_proc(proc);
let (proc_data, relocs) = backend.build_proc(ident_ids, proc);
let proc_offset = output.add_symbol_data(proc_id, section_id, proc_data, 16);
for reloc in relocs {
let elfreloc = match reloc {