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

@ -964,11 +964,13 @@ impl<
}
}
fn build_refcount_getptr(&mut self, dst: &Symbol, src: &Symbol) {
fn build_ptr_cast(&mut self, dst: &Symbol, src: &Symbol) {
// We may not strictly need an instruction here.
// What's important is to load the value, and for src and dest to have different Layouts.
// This is used for pointer math in refcounting and for pointer equality
let dst_reg = self.claim_general_reg(dst);
let src_reg = self.load_to_general_reg(src);
// The refcount pointer is the value before the pointer.
ASM::sub_reg64_reg64_imm32(&mut self.buf, dst_reg, src_reg, PTR_SIZE as i32);
ASM::mov_reg64_reg64(&mut self.buf, dst_reg, src_reg);
}
fn create_struct(&mut self, sym: &Symbol, layout: &Layout<'a>, fields: &'a [Symbol]) {

View file

@ -531,13 +531,13 @@ trait Backend<'a> {
arg_layouts,
ret_layout,
),
LowLevel::RefCountGetPtr => {
LowLevel::PtrCast => {
debug_assert_eq!(
1,
args.len(),
"RefCountGetPtr: expected to have exactly one argument"
);
self.build_refcount_getptr(sym, &args[0])
self.build_ptr_cast(sym, &args[0])
}
LowLevel::RefCountDec => self.build_fn_call(
sym,
@ -642,7 +642,7 @@ trait Backend<'a> {
);
/// build_refcount_getptr loads the pointer to the reference count of src into dst.
fn build_refcount_getptr(&mut self, dst: &Symbol, src: &Symbol);
fn build_ptr_cast(&mut self, dst: &Symbol, src: &Symbol);
/// literal_map gets the map from symbol to literal and layout, used for lazy loading and literal folding.
fn literal_map(&mut self) -> &mut MutMap<Symbol, (*const Literal<'a>, *const Layout<'a>)>;

View file

@ -6066,8 +6066,8 @@ fn run_low_level<'a, 'ctx, 'env>(
unreachable!("these are higher order, and are handled elsewhere")
}
RefCountGetPtr | RefCountInc | RefCountDec => {
unreachable!("LLVM backend does not use lowlevels for refcounting");
PtrCast | RefCountInc | RefCountDec => {
unreachable!("Not used in LLVM backend: {:?}", op);
}
}
}

View file

@ -605,9 +605,10 @@ pub fn decode_low_level<'a>(
Not => code_builder.i32_eqz(),
Hash => return SpecializedHash,
ExpectTrue => return NotImplemented,
RefCountGetPtr => {
code_builder.i32_const(4);
code_builder.i32_sub();
PtrCast => {
// We don't need any instructions here, since we've already loaded the value.
// PtrCast just creates separate Symbols and Layouts for the argument and return value.
// This is used for pointer math in refcounting and for pointer equality
}
RefCountInc => return BuiltinCall(bitcode::UTILS_INCREF),
RefCountDec => return BuiltinCall(bitcode::UTILS_DECREF),

View file

@ -117,7 +117,7 @@ pub enum LowLevel {
Not,
Hash,
ExpectTrue,
RefCountGetPtr,
PtrCast,
RefCountInc,
RefCountDec,
}

View file

@ -1007,8 +1007,8 @@ pub fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[bool] {
ExpectTrue => arena.alloc_slice_copy(&[irrelevant]),
RefCountGetPtr | RefCountInc | RefCountDec => {
unreachable!("Refcounting lowlevel calls are inserted *after* borrow checking");
PtrCast | RefCountInc | RefCountDec => {
unreachable!("Only inserted *after* borrow checking: {:?}", op);
}
}
}

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,7 +633,11 @@ 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(
self.rc_ptr_from_struct(
ident_ids,
elements,
rc_ptr,
self.arena.alloc(
//
alignment_stmt(self.arena.alloc(
//
@ -590,7 +646,8 @@ impl<'a> CodeGenHelp<'a> {
Stmt::Ret(zig_call_result),
)),
)),
)),
),
),
));
let if_stmt = Stmt::Switch {
cond_symbol: is_big_str,