Skip to content

Commit

Permalink
rt: Remove most constpool usages on x64
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Jan 4, 2025
1 parent 64ed397 commit 1df4b3f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 51 deletions.
34 changes: 20 additions & 14 deletions dora-runtime/src/cannon/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ impl<'a> BaselineAssembler<'a> {
self.masm.lea(dest, mem);
}

pub fn lea_label(&mut self, dest: Reg, label: Label) {
self.masm.lea_label(dest, label);
}

pub fn load_mem(&mut self, mode: MachineMode, dest: AnyReg, mem: Mem) {
self.masm.load_mem(mode, dest, mem);
}
Expand Down Expand Up @@ -586,7 +590,7 @@ impl<'a> BaselineAssembler<'a> {
});
}

pub fn emit_jump_table(&mut self, targets: &[Label]) -> i32 {
pub fn emit_jump_table(&mut self, targets: Vec<Label>) -> Label {
self.masm.emit_jump_table(targets)
}

Expand Down Expand Up @@ -618,10 +622,6 @@ impl<'a> BaselineAssembler<'a> {
self.masm.cmp_mem(mode, mem, rhs);
}

pub fn add_addr(&mut self, ptr: Address) -> i32 {
self.masm.add_const_addr(ptr)
}

pub fn set(&mut self, dest: Reg, op: CondCode) {
self.masm.set(dest, op);
}
Expand Down Expand Up @@ -1219,9 +1219,11 @@ impl<'a> BaselineAssembler<'a> {
.unwrap()
.address_init(global_id);

let disp = self.masm.add_const_addr(address_init);
let pos = self.masm.pos() as i32;
self.masm.load_constpool(REG_RESULT, disp + pos);
self.masm.load_int_const(
MachineMode::IntPtr,
REG_RESULT,
address_init.to_usize() as i64,
);
self.masm.load_int8_synchronized(REG_RESULT, REG_RESULT);
self.masm
.cmp_reg_imm(MachineMode::Ptr, REG_RESULT, INITIALIZED as i32);
Expand Down Expand Up @@ -1489,9 +1491,11 @@ impl<'a> BaselineAssembler<'a> {
.as_ref()
.unwrap()
.address_value(global_id);
let disp = self.masm.add_const_addr(address_value);
let pos = self.masm.pos() as i32;
self.masm.load_constpool(REG_TMP1, disp + pos);
self.masm.load_int_const(
MachineMode::IntPtr,
REG_TMP1,
address_value.to_usize() as i64,
);

if store_result_on_stack {
self.copy_bytecode_ty(
Expand All @@ -1513,9 +1517,11 @@ impl<'a> BaselineAssembler<'a> {
.unwrap()
.address_init(global_id);

let disp = self.masm.add_const_addr(address_init);
let pos = self.masm.pos() as i32;
self.masm.load_constpool(REG_RESULT, disp + pos);
self.masm.load_int_const(
MachineMode::IntPtr,
REG_RESULT,
address_init.to_usize() as i64,
);
self.masm
.load_int_const(MachineMode::Int32, REG_TMP1, INITIALIZED as i64);
self.masm.store_int8_synchronized(REG_TMP1, REG_RESULT);
Expand Down
36 changes: 19 additions & 17 deletions dora-runtime/src/cannon/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,9 +1397,11 @@ impl<'a> CannonCodeGen<'a> {
.unwrap()
.address_value(global_id);

let disp = self.asm.add_addr(address_value);
let pos = self.asm.pos() as i32;
self.asm.load_constpool(REG_TMP1, disp + pos);
self.asm.load_int_const(
MachineMode::IntPtr,
REG_TMP1,
address_value.to_usize() as i64,
);

let bytecode_type = self.bytecode.register_type(dest);

Expand All @@ -1423,10 +1425,11 @@ impl<'a> CannonCodeGen<'a> {
.unwrap()
.address_value(global_id);

let disp = self.asm.add_addr(address_value);
let pos = self.asm.pos() as i32;

self.asm.load_constpool(REG_TMP1, disp + pos);
self.asm.load_int_const(
MachineMode::IntPtr,
REG_TMP1,
address_value.to_usize() as i64,
);

let bytecode_type = self.bytecode.register_type(src);

Expand All @@ -1442,9 +1445,11 @@ impl<'a> CannonCodeGen<'a> {
.unwrap()
.address_init(global_id);

let disp = self.asm.add_addr(address_init);
let pos = self.asm.pos() as i32;
self.asm.load_constpool(REG_RESULT, disp + pos);
self.asm.load_int_const(
MachineMode::IntPtr,
REG_RESULT,
address_init.to_usize() as i64,
);
self.asm
.load_int_const(MachineMode::Int8, REG_TMP1, INITIALIZED as i64);
self.asm
Expand Down Expand Up @@ -1494,10 +1499,8 @@ impl<'a> CannonCodeGen<'a> {
assert_eq!(bytecode_type, BytecodeType::Ptr);

let address = self.vm.internalize_string_constant(lit_value);
let disp = self.asm.add_addr(address);
let pos = self.asm.pos() as i32;

self.asm.load_constpool(REG_RESULT, disp + pos);
self.asm
.load_int_const(MachineMode::IntPtr, REG_RESULT, address.to_usize() as i64);

self.emit_store_register(REG_RESULT.into(), dest);
}
Expand Down Expand Up @@ -1662,9 +1665,8 @@ impl<'a> CannonCodeGen<'a> {
.cmp_reg_imm(MachineMode::Int32, REG_TMP1, number_cases as i32);
self.asm.jump_if(CondCode::UnsignedGreaterEq, default_label);

let disp = self.asm.emit_jump_table(&target_labels);
let pos = self.asm.pos() as i32;
self.asm.load_constpool(REG_TMP2, pos + disp);
let label = self.asm.emit_jump_table(target_labels);
self.asm.lea_label(REG_TMP2, label);

// Load target address out of jump table.
self.asm.load_mem(
Expand Down
43 changes: 27 additions & 16 deletions dora-runtime/src/masm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::mem;
use crate::mirror::Header;
use crate::mode::MachineMode;
use crate::vm::{
CodeDescriptor, CommentTable, ConstPool, ConstPoolValue, GcPoint, GcPointTable,
InlinedLocation, LazyCompilationData, LazyCompilationSite, LocationTable, RelocationKind,
RelocationTable, Trap, CODE_ALIGNMENT,
CodeDescriptor, CommentTable, ConstPool, GcPoint, GcPointTable, InlinedLocation,
LazyCompilationData, LazyCompilationSite, LocationTable, RelocationKind, RelocationTable, Trap,
CODE_ALIGNMENT,
};
pub use dora_asm::Label;
use dora_bytecode::Location;
Expand Down Expand Up @@ -90,13 +90,20 @@ impl NewConstPool {
}
}

pub enum EpilogConstant {
Float32(f32),
Float64(f64),
Address(Address),
JumpTable(Vec<Label>),
}

pub struct MacroAssembler {
asm: Assembler,
bailouts: Vec<(Label, Trap, Location)>,
lazy_compilation: LazyCompilationData,
constpool: ConstPool,
new_constpool: NewConstPool,
epilog_constants: Vec<(Label, ConstPoolValue)>,
epilog_constants: Vec<(Label, EpilogConstant)>,
gcpoints: GcPointTable,
comments: CommentTable,
positions: LocationTable,
Expand Down Expand Up @@ -186,19 +193,26 @@ impl MacroAssembler {
for (label, value) in &self.epilog_constants {
self.asm.bind_label(*label);
match value {
ConstPoolValue::Ptr(value) => {
EpilogConstant::Address(value) => {
self.asm.emit_u64(value.to_usize() as u64);
}

ConstPoolValue::Float32(value) => {
EpilogConstant::Float32(value) => {
self.asm.emit_u32(unsafe { std::mem::transmute(*value) });
}

ConstPoolValue::Float64(value) => {
EpilogConstant::Float64(value) => {
self.asm.emit_u64(unsafe { std::mem::transmute(*value) });
}

_ => unreachable!(),
EpilogConstant::JumpTable(targets) => {
for target in targets {
let offset = self.asm.position();
self.asm.emit_u64(0);
self.relocations
.push((offset.try_into().expect("overflow"), *target));
}
}
}
}
}
Expand Down Expand Up @@ -382,15 +396,12 @@ impl MacroAssembler {
self.bind_label(done);
}

pub fn emit_jump_table(&mut self, targets: &[Label]) -> i32 {
pub fn emit_jump_table(&mut self, targets: Vec<Label>) -> Label {
assert!(!targets.is_empty());
let start = self.add_const_addr(Address::null());
self.relocations.push((start, targets[0]));
for &target in &targets[1..] {
let offset = self.add_const_addr(Address::null());
self.relocations.push((offset, target));
}
start
let label = self.create_label();
self.epilog_constants
.push((label, EpilogConstant::JumpTable(targets)));
label
}
}

Expand Down
4 changes: 4 additions & 0 deletions dora-runtime/src/masm/arm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,10 @@ impl MacroAssembler {
}
}

pub fn lea_label(&mut self, _dest: Reg, _label: Label) {
unimplemented!();
}

pub fn emit_object_write_barrier_fast_path(&mut self, host: Reg) -> Label {
let scratch = self.get_scratch();
self.asm.ldrb_imm(
Expand Down
12 changes: 8 additions & 4 deletions dora-runtime/src/masm/x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate::compiler::codegen::AnyReg;
use crate::cpu::*;
use crate::gc::swiper::LARGE_OBJECT_SIZE;
use crate::gc::Address;
use crate::masm::{CondCode, Label, MacroAssembler, Mem};
use crate::masm::{CondCode, EpilogConstant, Label, MacroAssembler, Mem};
use crate::mem::{fits_i32, ptr_width};
use crate::mirror::{offset_of_array_data, offset_of_array_length, Header, REMEMBERED_BIT_SHIFT};
use crate::mode::MachineMode;
use crate::shape::Shape;
use crate::threads::ThreadLocalData;
use crate::vm::{get_vm, ConstPoolValue, LazyCompilationSite, Trap};
use crate::vm::{get_vm, LazyCompilationSite, Trap};
pub use dora_asm::x64::AssemblerX64 as Assembler;
use dora_asm::x64::Register as AsmRegister;
use dora_asm::x64::{Address as AsmAddress, Condition, Immediate, ScaleFactor, XmmRegister};
Expand Down Expand Up @@ -1121,6 +1121,10 @@ impl MacroAssembler {
self.asm.lea(dest.into(), address_from_mem(mem));
}

pub fn lea_label(&mut self, dest: Reg, label: Label) {
self.asm.movq_rl(dest.into(), label);
}

pub fn emit_object_write_barrier_fast_path(&mut self, host: Reg) -> Label {
let remembered_bit_index =
(REMEMBERED_BIT_SHIFT - Header::offset_metadata_word() * 8) as u32;
Expand Down Expand Up @@ -1271,8 +1275,8 @@ impl MacroAssembler {
}

let const_value = match mode {
MachineMode::Float32 => ConstPoolValue::Float32(imm as f32),
MachineMode::Float64 => ConstPoolValue::Float64(imm),
MachineMode::Float32 => EpilogConstant::Float32(imm as f32),
MachineMode::Float64 => EpilogConstant::Float64(imm),
_ => unreachable!(),
};
let label = self.asm.create_label();
Expand Down

0 comments on commit 1df4b3f

Please sign in to comment.