diff --git a/dora/src/cannon/codegen.rs b/dora/src/cannon/codegen.rs index a5404e2c..7fd6e41a 100644 --- a/dora/src/cannon/codegen.rs +++ b/dora/src/cannon/codegen.rs @@ -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; @@ -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); } @@ -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); @@ -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); @@ -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(); } @@ -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::>(); let dest_offset = self.register_offset(dest); @@ -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); @@ -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); @@ -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!() diff --git a/dora/src/gc/root.rs b/dora/src/gc/root.rs index 1c751e92..e1aa146d 100644 --- a/dora/src/gc/root.rs +++ b/dora/src/gc/root.rs @@ -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]) -> Vec { @@ -62,10 +61,8 @@ fn iterate_roots_from_globals(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); diff --git a/dora/src/vm.rs b/dora/src/vm.rs index 747918e8..4703ce54 100644 --- a/dora/src/vm.rs +++ b/dora/src/vm.rs @@ -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::{ diff --git a/dora/src/vm/specialize.rs b/dora/src/vm/specialize.rs index 6ad898f6..d9f8eae7 100644 --- a/dora/src/vm/specialize.rs +++ b/dora/src/vm/specialize.rs @@ -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 { + 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, @@ -288,10 +297,8 @@ pub fn add_ref_fields(vm: &VM, ref_fields: &mut Vec, 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() { @@ -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) => { diff --git a/dora/src/vm/ty.rs b/dora/src/vm/ty.rs index be51f9b1..6b19a1ae 100644 --- a/dora/src/vm/ty.rs +++ b/dora/src/vm/ty.rs @@ -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 { @@ -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(), @@ -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."),