Lowlevels: replace RefCountGetPtr with the more general GetPtrAsInt.

This commit is contained in:
Brian Carroll 2021-12-18 20:05:05 +00:00
parent a1d883600c
commit e847c924dd
7 changed files with 104 additions and 44 deletions

View file

@ -161,18 +161,8 @@ impl<'a> CodeGenHelp<'a> {
}
ModifyRc::DecRef(structure) => {
// No generated procs for DecRef, just lowlevel calls
// Get a pointer to the refcount itself
// No generated procs for DecRef, just lowlevel ops
let rc_ptr_sym = self.create_symbol(ident_ids, "rc_ptr");
let rc_ptr_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::RefCountGetPtr,
update_mode: UpdateModeId::BACKEND_DUMMY,
},
arguments: arena.alloc([*structure]),
});
let rc_ptr_stmt = |next| Stmt::Let(rc_ptr_sym, rc_ptr_expr, LAYOUT_PTR, next);
// Pass the refcount pointer to the lowlevel call (see utils.zig)
let call_result_empty = self.create_symbol(ident_ids, "call_result_empty");
@ -184,7 +174,13 @@ impl<'a> CodeGenHelp<'a> {
arguments: arena.alloc([rc_ptr_sym]),
});
let call_stmt = Stmt::Let(call_result_empty, call_expr, LAYOUT_UNIT, following);
let rc_stmt = arena.alloc(rc_ptr_stmt(arena.alloc(call_stmt)));
let rc_stmt = arena.alloc(self.rc_ptr_from_struct(
ident_ids,
*structure,
rc_ptr_sym,
arena.alloc(call_stmt),
));
(rc_stmt, Vec::new_in(self.arena))
}
@ -501,6 +497,70 @@ impl<'a> CodeGenHelp<'a> {
Stmt::Let(unit, Expr::Struct(&[]), LAYOUT_UNIT, ret_stmt)
}
// Subtract a constant from a pointer to find the refcount
// Also does some type casting, so that we have different Symbols and Layouts
// for the 'pointer' and 'integer' versions of the address.
// This helps to avoid issues with the backends Symbol->Layout mapping.
fn rc_ptr_from_struct(
&mut self,
ident_ids: &mut IdentIds,
structure: Symbol,
rc_ptr_sym: Symbol,
following: &'a Stmt<'a>,
) -> Stmt<'a> {
// Typecast the structure pointer to an integer
// Backends expect a number Layout to choose the right "subtract" instruction
let addr_sym = self.create_symbol(ident_ids, "addr");
let addr_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::PtrCast,
update_mode: UpdateModeId::BACKEND_DUMMY,
},
arguments: self.arena.alloc([structure]),
});
let addr_stmt = |next| Stmt::Let(addr_sym, addr_expr, self.layout_isize, next);
// Pointer size constant
let ptr_size_sym = self.create_symbol(ident_ids, "ptr_size");
let ptr_size_expr = Expr::Literal(Literal::Int(self.ptr_size as i128));
let ptr_size_stmt = |next| Stmt::Let(ptr_size_sym, ptr_size_expr, self.layout_isize, next);
// Refcount address
let rc_addr_sym = self.create_symbol(ident_ids, "rc_addr");
let rc_addr_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::NumSub,
update_mode: UpdateModeId::BACKEND_DUMMY,
},
arguments: self.arena.alloc([structure, ptr_size_sym]),
});
let rc_addr_stmt = |next| Stmt::Let(rc_addr_sym, rc_addr_expr, self.layout_isize, next);
// Typecast the refcount address from integer to pointer
let rc_ptr_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::PtrCast,
update_mode: UpdateModeId::BACKEND_DUMMY,
},
arguments: self.arena.alloc([rc_addr_sym]),
});
let rc_ptr_stmt = |next| Stmt::Let(rc_ptr_sym, rc_ptr_expr, LAYOUT_PTR, next);
addr_stmt(self.arena.alloc(
//
ptr_size_stmt(self.arena.alloc(
//
rc_addr_stmt(self.arena.alloc(
//
rc_ptr_stmt(self.arena.alloc(
//
following,
)),
)),
)),
))
}
/// Generate a procedure to modify the reference count of a Str
fn refcount_str(&mut self, ident_ids: &mut IdentIds, op: HelperOp) -> Stmt<'a> {
let string = Symbol::ARG_1;
@ -539,20 +599,12 @@ impl<'a> CodeGenHelp<'a> {
field_layouts: self.arena.alloc([LAYOUT_PTR, layout_isize]),
structure: string,
};
let elements_stmt = |next| Stmt::Let(elements, elements_expr, LAYOUT_PTR, next);
let elements_stmt = |next| Stmt::Let(elements, elements_expr, layout_isize, next);
// Get a pointer to the refcount value, just below the elements pointer
// A pointer to the refcount value itself
let rc_ptr = self.create_symbol(ident_ids, "rc_ptr");
let rc_ptr_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::RefCountGetPtr,
update_mode: UpdateModeId::BACKEND_DUMMY,
},
arguments: self.arena.alloc([elements]),
});
let rc_ptr_stmt = |next| Stmt::Let(rc_ptr, rc_ptr_expr, LAYOUT_PTR, next);
// Alignment constant
// Alignment constant (same value as ptr_size but different layout)
let alignment = self.create_symbol(ident_ids, "alignment");
let alignment_expr = Expr::Literal(Literal::Int(self.ptr_size as i128));
let alignment_stmt = |next| Stmt::Let(alignment, alignment_expr, LAYOUT_U32, next);
@ -581,16 +633,21 @@ impl<'a> CodeGenHelp<'a> {
// Generate an `if` to skip small strings but modify big strings
let then_branch = elements_stmt(self.arena.alloc(
//
rc_ptr_stmt(self.arena.alloc(
//
alignment_stmt(self.arena.alloc(
self.rc_ptr_from_struct(
ident_ids,
elements,
rc_ptr,
self.arena.alloc(
//
zig_call_stmt(self.arena.alloc(
alignment_stmt(self.arena.alloc(
//
Stmt::Ret(zig_call_result),
zig_call_stmt(self.arena.alloc(
//
Stmt::Ret(zig_call_result),
)),
)),
)),
)),
),
),
));
let if_stmt = Stmt::Switch {
cond_symbol: is_big_str,