Skip to content

Commit

Permalink
introduce value_instance to de-duplicate repeating code patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
soc committed Sep 1, 2023
1 parent 3152b55 commit f46c4d5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 55 deletions.
39 changes: 10 additions & 29 deletions dora/src/cannon/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use crate::vm::{
get_concrete_tuple_array, get_concrete_tuple_bytecode_ty, get_concrete_tuple_ty,
specialize_class_id_params, specialize_enum_class, specialize_enum_id_params,
specialize_lambda, specialize_trait_object, specialize_tuple_array, specialize_tuple_bty,
specialize_tuple_ty, specialize_type, specialize_type_list, specialize_value_id_params,
EnumLayout, GcPoint, LazyCompilationSite, Trap, VM,
specialize_tuple_ty, specialize_type, specialize_type_list, value_instance, EnumLayout,
GcPoint, LazyCompilationSite, Trap, VM,
};
use crate::vtable::VTable;

Expand Down Expand Up @@ -230,10 +230,7 @@ impl<'a> CannonCodeGen<'a> {

BytecodeType::Value(value_id, type_params) => {
let offset = self.register_offset(Register(idx));
let value_instance_id =
specialize_value_id_params(self.vm, value_id, type_params);
let value_instance = self.vm.value_instances.idx(value_instance_id);

let value_instance = value_instance(self.vm, value_id, type_params);
for &ref_offset in &value_instance.ref_fields {
self.references.push(offset + ref_offset);
}
Expand Down Expand Up @@ -1311,9 +1308,7 @@ impl<'a> CannonCodeGen<'a> {
dest: RegOrOffset,
src: RegOrOffset,
) {
let value_instance_id = specialize_value_id_params(self.vm, value_id, type_params);
let value_instance = self.vm.value_instances.idx(value_instance_id);

let value_instance = value_instance(self.vm, value_id, type_params);
for field in &value_instance.fields {
let src = src.offset(field.offset);
let dest = dest.offset(field.offset);
Expand Down Expand Up @@ -1549,9 +1544,7 @@ impl<'a> CannonCodeGen<'a> {
let type_params = specialize_type_list(self.vm, &type_params, self.type_params);
debug_assert!(type_params.iter().all(|ty| ty.is_concrete_type(self.vm)));

let value_instance_id = specialize_value_id_params(self.vm, value_id, type_params.clone());
let value_instance = self.vm.value_instances.idx(value_instance_id);

let value_instance = value_instance(self.vm, value_id, type_params);
let field = &value_instance.fields[field_id.to_usize()];

let bytecode_type = self.specialize_register_type(dest);
Expand Down Expand Up @@ -1661,9 +1654,7 @@ impl<'a> CannonCodeGen<'a> {
RegOrOffset::Offset(src_offset),
);

let value_instance_id =
specialize_value_id_params(self.vm, *value_id, type_params.clone());
let value_instance = self.vm.value_instances.idx(value_instance_id);
let value_instance = value_instance(self.vm, *value_id, type_params.clone());
needs_write_barrier = value_instance.contains_references();
}

Expand Down Expand Up @@ -2468,9 +2459,7 @@ impl<'a> CannonCodeGen<'a> {
let type_params = specialize_type_list(self.vm, &type_params, self.type_params);
debug_assert!(type_params.iter().all(|ty| ty.is_concrete_type(self.vm)));

let value_instance_id = specialize_value_id_params(self.vm, value_id, type_params);
let value_instance = self.vm.value_instances.idx(value_instance_id);

let value_instance = value_instance(self.vm, value_id, type_params);
let arguments = self.argument_stack.drain(..).collect::<Vec<_>>();

let dest_offset = self.register_offset(dest);
Expand Down Expand Up @@ -2716,9 +2705,7 @@ impl<'a> CannonCodeGen<'a> {
}

BytecodeType::Value(value_id, type_params) => {
let value_instance_id =
specialize_value_id_params(self.vm, value_id, type_params.clone());
let value_instance = self.vm.value_instances.idx(value_instance_id);
let value_instance = value_instance(self.vm, value_id, type_params.clone());

self.asm
.array_address(REG_TMP1, REG_RESULT, REG_TMP1, value_instance.size);
Expand Down Expand Up @@ -2860,10 +2847,7 @@ impl<'a> CannonCodeGen<'a> {
}

BytecodeType::Value(value_id, type_params) => {
let value_instance_id =
specialize_value_id_params(self.vm, value_id, type_params.clone());
let value_instance = self.vm.value_instances.idx(value_instance_id);

let value_instance = value_instance(self.vm, value_id, type_params.clone());
let element_size = value_instance.size;
self.asm
.array_address(REG_TMP1, REG_RESULT, REG_TMP1, element_size);
Expand Down Expand Up @@ -5403,10 +5387,7 @@ pub fn size(vm: &VM, ty: BytecodeType) -> i32 {
}
}
BytecodeType::Value(value_id, type_params) => {
let vdef_id = specialize_value_id_params(vm, value_id, type_params);
let vdef = vm.value_instances.idx(vdef_id);

vdef.size
value_instance(vm, value_id, type_params).size
}
BytecodeType::Class(_, _) | BytecodeType::Lambda(_, _) => {
unreachable!()
Expand Down
9 changes: 3 additions & 6 deletions dora/src/gc/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::language::ty::SourceType;
use crate::stack::DoraToNativeInfo;
use crate::threads::DoraThread;
use crate::vm::{
get_concrete_tuple_ty, specialize_enum_id_params, specialize_value_id_params, CodeKind,
EnumLayout, VM,
get_concrete_tuple_ty, specialize_enum_id_params, value_instance, CodeKind, EnumLayout, VM,
};

pub fn determine_strong_roots(vm: &VM, threads: &[Arc<DoraThread>]) -> Vec<Slot> {
Expand Down Expand Up @@ -62,10 +61,8 @@ fn iterate_roots_from_globals<F: FnMut(Slot)>(vm: &VM, callback: &mut F) {

match global_var.ty {
SourceType::Value(value_id, ref type_params) => {
let sdef_id = specialize_value_id_params(vm, value_id, type_params.clone());
let sdef = vm.value_instances.idx(sdef_id);

for &offset in &sdef.ref_fields {
let value_instance = value_instance(vm, value_id, type_params.clone());
for &offset in &value_instance.ref_fields {
let slot_address = global_var.address_value.offset(offset as usize);
let slot = Slot::at(slot_address);
callback(slot);
Expand Down
2 changes: 1 addition & 1 deletion dora/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub use self::specialize::{
add_ref_fields, replace_type_param, specialize_class_id, specialize_class_id_params,
specialize_enum_class, specialize_enum_id_params, specialize_lambda, specialize_trait_object,
specialize_tuple_array, specialize_tuple_bty, specialize_tuple_ty, specialize_type,
specialize_type_list, specialize_value_id_params,
specialize_type_list, specialize_value_id_params, value_instance,
};
pub use self::stubs::{setup_stubs, Stubs};
pub use self::tuples::{
Expand Down
21 changes: 13 additions & 8 deletions dora/src/vm/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ pub fn specialize_type_list(
SourceTypeArray::with(specialized_types)
}

pub fn value_instance(
vm: &VM,
value_id: ValueDefinitionId,
type_params: SourceTypeArray,
) -> Arc<ValueInstance> {
let value_instance_id = specialize_value_id_params(vm, value_id, type_params);
vm.value_instances.idx(value_instance_id)
}

pub fn specialize_value_id_params(
vm: &VM,
value_id: ValueDefinitionId,
Expand Down Expand Up @@ -288,10 +297,8 @@ pub fn add_ref_fields(vm: &VM, ref_fields: &mut Vec<i32>, offset: i32, ty: Sourc
}
}
} else if let SourceType::Value(value_id, type_params) = ty.clone() {
let sdef_id = specialize_value_id_params(vm, value_id, type_params);
let sdef = vm.value_instances.idx(sdef_id);

for &ref_offset in &sdef.ref_fields {
let value_instance = value_instance(vm, value_id, type_params);
for &ref_offset in &value_instance.ref_fields {
ref_fields.push(offset + ref_offset);
}
} else if ty.reference_type() {
Expand Down Expand Up @@ -419,10 +426,8 @@ fn create_specialized_class_array(
}

SourceType::Value(value_id, type_params) => {
let vdef_id = specialize_value_id_params(vm, value_id, type_params);
let vdef = vm.value_instances.idx(vdef_id);

InstanceSize::ValueArray(vdef.size)
let value_instance = value_instance(vm, value_id, type_params);
InstanceSize::ValueArray(value_instance.size)
}

SourceType::Enum(enum_id, type_params) => {
Expand Down
17 changes: 6 additions & 11 deletions dora/src/vm/ty.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::language::ty::SourceType;
use crate::mem;
use crate::mode::MachineMode;
use crate::vm::{get_concrete_tuple_ty, VM};
use crate::vm::{specialize_enum_id_params, specialize_value_id_params, EnumLayout};
use crate::vm::{get_concrete_tuple_ty, specialize_enum_id_params, value_instance, EnumLayout, VM};

impl SourceType {
pub fn size(&self, vm: &VM) -> i32 {
Expand Down Expand Up @@ -30,10 +29,8 @@ impl SourceType {
SourceType::Class(_, _) | SourceType::Lambda(_, _) | SourceType::Ptr => {
mem::ptr_width()
}
SourceType::Value(value_id, params) => {
let value_instance_id = specialize_value_id_params(vm, *value_id, params.clone());
let value_instance = vm.value_instances.idx(value_instance_id);

SourceType::Value(value_id, type_params) => {
let value_instance = value_instance(vm, *value_id, type_params.clone());
value_instance.size
}
SourceType::Trait(_, _) => mem::ptr_width(),
Expand Down Expand Up @@ -67,11 +64,9 @@ impl SourceType {
SourceType::Class(_, _) | SourceType::Lambda(_, _) | SourceType::Ptr => {
mem::ptr_width()
}
SourceType::Value(value_id, params) => {
let value_instance_id = specialize_value_id_params(vm, *value_id, params.clone());
let value = vm.value_instances.idx(value_instance_id);

value.align
SourceType::Value(value_id, type_params) => {
let value_instance = value_instance(vm, *value_id, type_params.clone());
value_instance.align
}
SourceType::Trait(_, _) => mem::ptr_width(),
SourceType::TypeParam(_) => panic!("no alignment for type variable."),
Expand Down

0 comments on commit f46c4d5

Please sign in to comment.