Prefix string constants with REFCOUNT_MAX

This commit is contained in:
Brian Carroll 2021-11-30 09:43:16 +00:00
parent c2a2ff2957
commit 22ccb6eca4
2 changed files with 14 additions and 7 deletions

View file

@ -4,7 +4,7 @@ use code_builder::Align;
use roc_collections::all::MutMap;
use roc_module::low_level::LowLevel;
use roc_module::symbol::{Interns, Symbol};
use roc_mono::gen_refcount::RefcountProcGenerator;
use roc_mono::gen_refcount::{RefcountProcGenerator, REFCOUNT_MAX};
use roc_mono::ir::{CallType, Expr, JoinPointId, Literal, Proc, Stmt};
use roc_mono::layout::{Builtin, Layout, LayoutIds};
@ -799,10 +799,13 @@ impl<'a> WasmBackend<'a> {
None => {
let const_segment_bytes = &mut self.module.data.segments[CONST_SEGMENT_INDEX].init;
// Store the string in the data section, to be loaded on module instantiation
// RocStr `elements` field will point to that constant data, not the heap
let segment_offset = const_segment_bytes.len() as u32;
let elements_addr = segment_offset + CONST_SEGMENT_BASE_ADDR;
// Store the string in the data section
// Prefix it with a special refcount value (treated as "infinity")
// The string's `elements` field points at the data after the refcount
let refcount_max_bytes: [u8; 4] = (REFCOUNT_MAX as i32).to_le_bytes();
const_segment_bytes.extend_from_slice(&refcount_max_bytes);
let elements_offset = const_segment_bytes.len() as u32;
let elements_addr = elements_offset + CONST_SEGMENT_BASE_ADDR;
const_segment_bytes.extend_from_slice(string.as_bytes());
// Generate linker info
@ -815,7 +818,7 @@ impl<'a> WasmBackend<'a> {
flags: 0,
name,
segment_index: CONST_SEGMENT_INDEX as u32,
segment_offset,
segment_offset: elements_offset,
size: string.len() as u32,
});

View file

@ -21,6 +21,10 @@ const LAYOUT_UNIT: Layout = Layout::Struct(&[]);
const LAYOUT_PTR: Layout = Layout::RecursivePointer;
const LAYOUT_U32: Layout = Layout::Builtin(Builtin::Int(IntWidth::U32));
/// "Infinite" reference count, for static values
/// Ref counts are encoded as negative numbers where isize::MIN represents 1
pub const REFCOUNT_MAX: usize = 0;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RefcountOp {
Inc,
@ -288,7 +292,7 @@ impl<'a> RefcountProcGenerator<'a> {
op: LowLevel::RefCountGetPtr,
update_mode: UpdateModeId::BACKEND_DUMMY,
},
arguments: self.arena.alloc([string]),
arguments: self.arena.alloc([elements]),
});
let rc_ptr_stmt = |next| Stmt::Let(rc_ptr, rc_ptr_expr, LAYOUT_PTR, next);