Skip to content

Commit

Permalink
frontend: Enable --new-default-impl by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Feb 5, 2025
1 parent 4e39d56 commit 4d1499b
Show file tree
Hide file tree
Showing 20 changed files with 190 additions and 63 deletions.
7 changes: 7 additions & 0 deletions dora-bytecode/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,13 @@ impl BytecodeType {
}
}

pub fn is_assoc(&self) -> bool {
match self {
BytecodeType::Assoc { .. } => true,
_ => false,
}
}

pub fn is_concrete_type(&self) -> bool {
match self {
BytecodeType::Unit
Expand Down
4 changes: 2 additions & 2 deletions dora-frontend/src/sema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl SemaFlags {
packages,
program_file: Some(FileContent::Content(input.to_string())),
boots: false,
new_default_impl: false,
new_default_impl: true,
}
}

Expand All @@ -118,7 +118,7 @@ impl SemaFlags {
packages,
program_file: Some(FileContent::Content(input.to_string())),
boots: false,
new_default_impl: false,
new_default_impl: true,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion dora-language-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ fn compile_project(project: ProjectConfig, sender: Sender<MainLoopTask>) {
program_file: Some(FileContent::Path(project.main.clone())),
packages: Vec::new(),
boots: false,
new_default_impl: false,
new_default_impl: true,
};

let mut sa = Sema::new(sem_args);
Expand Down
14 changes: 8 additions & 6 deletions dora-runtime/src/boots.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::mem;
use std::ptr;

use deserializer::decode_specialize_self;
use dora_bytecode::{
display_fct, BytecodeTraitType, BytecodeTypeArray, ClassId, EnumId, FunctionId, FunctionKind,
GlobalId, StructId, TraitId,
Expand Down Expand Up @@ -88,8 +89,8 @@ pub const BOOTS_FUNCTIONS: &[(&'static str, FctImplementation)] = &[
N(find_trait_impl_raw as *const u8),
),
(
"boots::interface::specializeTyRaw",
N(specialize_ty_raw as *const u8),
"boots::interface::specializeAssocTyRaw",
N(specialize_assoc_ty_raw as *const u8),
),
(
"boots::interface::getIntrinsicForFunctionRaw",
Expand Down Expand Up @@ -441,7 +442,7 @@ extern "C" fn find_trait_impl_raw(data: Handle<UInt8Array>) -> Ref<UInt8Array> {
byte_array_from_buffer(vm, buffer.data()).cast()
}

extern "C" fn specialize_ty_raw(data: Handle<UInt8Array>) -> Ref<UInt8Array> {
extern "C" fn specialize_assoc_ty_raw(data: Handle<UInt8Array>) -> Ref<UInt8Array> {
let vm = get_vm();

let mut serialized_data = vec![0; data.len()];
Expand All @@ -455,12 +456,13 @@ extern "C" fn specialize_ty_raw(data: Handle<UInt8Array>) -> Ref<UInt8Array> {
}

let mut reader = ByteReader::new(serialized_data);
let generic_assoc_ty = decode_bytecode_type(&mut reader);
assert!(generic_assoc_ty.is_generic_assoc());
let specialize_self = decode_specialize_self(&mut reader);
let ty = decode_bytecode_type(&mut reader);
assert!(ty.is_generic_assoc() || ty.is_assoc());
let type_params = decode_bytecode_type_array(&mut reader);
assert!(!reader.has_more());

let ty = specialize_ty(vm, None, generic_assoc_ty, &type_params);
let ty = specialize_ty(vm, specialize_self.as_ref(), ty, &type_params);

let mut buffer = ByteBuffer::new();
serializer::encode_bytecode_type(vm, &ty, &mut buffer);
Expand Down
26 changes: 24 additions & 2 deletions dora-runtime/src/boots/deserializer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::boots::data::{LazyCompilationSiteKind, RelocationKindKind};
use crate::compiler::codegen::SpecializeSelf;
use crate::gc::Address;
use crate::vm::{
CodeDescriptor, CommentTable, GcPoint, GcPointTable, InlinedFunction, InlinedFunctionId,
Expand All @@ -7,7 +8,7 @@ use crate::vm::{
};
use dora_bytecode::{
AliasId, BytecodeTraitType, BytecodeType, BytecodeTypeArray, BytecodeTypeKind, ClassId, EnumId,
FunctionId, Location, StructId, TraitId,
FunctionId, ImplId, Location, StructId, TraitId,
};

pub fn decode_code_descriptor(reader: &mut ByteReader) -> CodeDescriptor {
Expand Down Expand Up @@ -246,7 +247,13 @@ pub fn decode_bytecode_type(reader: &mut ByteReader) -> BytecodeType {
}
}

BytecodeTypeKind::TypeAlias | BytecodeTypeKind::Assoc => unreachable!(),
BytecodeTypeKind::Assoc => {
let assoc_id = AliasId(reader.read_u32());
let type_params = decode_bytecode_type_array(reader);
BytecodeType::Assoc(assoc_id, type_params)
}

BytecodeTypeKind::TypeAlias => unreachable!(),
}
}

Expand Down Expand Up @@ -281,6 +288,21 @@ pub fn decode_bytecode_type_array(reader: &mut ByteReader) -> BytecodeTypeArray
BytecodeTypeArray::new(types)
}

pub fn decode_specialize_self(reader: &mut ByteReader) -> Option<SpecializeSelf> {
if reader.read_bool() {
let impl_id = ImplId(reader.read_u32());
let trait_ty = decode_bytecode_trait_ty(reader);
let extended_ty = decode_bytecode_type(reader);
Some(SpecializeSelf {
impl_id,
trait_ty,
extended_ty,
})
} else {
None
}
}

fn decode_gcpoint_table(reader: &mut ByteReader) -> GcPointTable {
let length = reader.read_u32() as usize;
let mut result = GcPointTable::new();
Expand Down
11 changes: 9 additions & 2 deletions dora-runtime/src/boots/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ fn encode_compilation_info(
encode_bytecode_type(vm, &compilation_data.return_type, buffer);
if let Some(ref specialize_self) = compilation_data.specialize_self {
buffer.emit_bool(true);
encode_bytecode_type(vm, specialize_self, buffer);
buffer.emit_id(specialize_self.impl_id.0 as usize);
encode_bytecode_trait_type(vm, &specialize_self.trait_ty, buffer);
encode_bytecode_type(vm, &specialize_self.extended_ty, buffer);
} else {
buffer.emit_bool(false);
}
Expand Down Expand Up @@ -292,7 +294,12 @@ pub fn encode_bytecode_type(vm: &VM, ty: &BytecodeType, buffer: &mut ByteBuffer)
encode_bytecode_trait_type(vm, trait_ty, buffer);
buffer.emit_id(assoc_id.0 as usize);
}
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) => {
BytecodeType::Assoc(assoc_id, assoc_type_params) => {
buffer.emit_u8(BytecodeTypeKind::Assoc as u8);
buffer.emit_u32(assoc_id.0);
encode_bytecode_type_array(vm, assoc_type_params, buffer);
}
BytecodeType::TypeAlias(..) => {
unreachable!()
}
}
Expand Down
9 changes: 7 additions & 2 deletions dora-runtime/src/cannon/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::vm::{
get_concrete_tuple_bty, get_concrete_tuple_bty_array, specialize_ty, specialize_ty_array,
CodeDescriptor, EnumLayout, GcPoint, Intrinsic, LazyCompilationSite, Trap, INITIALIZED, VM,
};
use crate::SpecializeSelf;
use dora_bytecode::{
display_fct, display_ty, read, BytecodeFunction, BytecodeOffset, BytecodeTraitType,
BytecodeType, BytecodeTypeArray, BytecodeVisitor, ConstPoolEntry, ConstPoolIdx, FunctionId,
Expand Down Expand Up @@ -55,7 +56,7 @@ pub struct CannonCodeGen<'a> {
emit_code_comments: bool,

type_params: BytecodeTypeArray,
specialize_self: Option<BytecodeType>,
specialize_self: Option<SpecializeSelf>,

offset_to_address: HashMap<BytecodeOffset, usize>,
offset_to_label: HashMap<BytecodeOffset, Label>,
Expand Down Expand Up @@ -2641,7 +2642,11 @@ impl<'a> CannonCodeGen<'a> {
type_params.clone(),
),
ConstPoolEntry::GenericSelf(fct_id, type_params) => (
self.specialize_self.clone().expect("missing Self type"),
self.specialize_self
.as_ref()
.expect("missing Self type")
.extended_ty
.clone(),
*fct_id,
type_params.clone(),
),
Expand Down
10 changes: 7 additions & 3 deletions dora-runtime/src/compiler/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::vm::{
specialize_bty, specialize_bty_array, BytecodeTypeExt, Code, LazyCompilationSite, ShapeKind,
VM,
};
use crate::{get_bytecode, Shape};
use crate::{get_bytecode, Shape, SpecializeSelf};

pub fn compile_boots_aot(vm: &VM) {
if vm.has_boots() {
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<'a> TransitiveClosureComputation<'a> {
&mut self,
bytecode_function: &BytecodeFunction,
type_params: BytecodeTypeArray,
specialize_self: Option<BytecodeType>,
specialize_self: Option<SpecializeSelf>,
) {
let reader = BytecodeReader::new(bytecode_function.code());

Expand Down Expand Up @@ -290,7 +290,11 @@ impl<'a> TransitiveClosureComputation<'a> {
}

ConstPoolEntry::GenericSelf(fct_id, fct_type_params) => {
generic_ty = specialize_self.clone().expect("missing Self type");
generic_ty = specialize_self
.as_ref()
.expect("missing Self type")
.extended_ty
.clone();
callee_trait_fct_id = *fct_id;
callee_type_params = fct_type_params.clone();
}
Expand Down
24 changes: 18 additions & 6 deletions dora-runtime/src/compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::os;
use crate::vm::{install_code, Code, CodeDescriptor, CodeId, CodeKind, Compiler, VM};
use dora_bytecode::{
display_fct, display_ty_array, display_ty_without_type_params, dump_stdout, BytecodeFunction,
BytecodeType, BytecodeTypeArray, FunctionData, FunctionId, FunctionKind, Location,
BytecodeTraitType, BytecodeType, BytecodeTypeArray, FunctionData, FunctionId, FunctionKind,
ImplId, Location,
};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -100,10 +101,16 @@ pub fn compile_fct_aot(
(code_id, code)
}

pub struct SpecializeSelf {
pub impl_id: ImplId,
pub trait_ty: BytecodeTraitType,
pub extended_ty: BytecodeType,
}

pub fn get_bytecode<'a>(
vm: &'a VM,
program_fct: &'a FunctionData,
) -> Option<(&'a BytecodeFunction, Option<BytecodeType>)> {
) -> Option<(&'a BytecodeFunction, Option<SpecializeSelf>)> {
match program_fct.bytecode.as_ref() {
Some(bytecode_fct) => Some((bytecode_fct, None)),
None => {
Expand All @@ -118,7 +125,12 @@ pub fn get_bytecode<'a>(
let bytecode_fct = trait_method.bytecode.as_ref()?;

let program_fct_impl = vm.impl_(program_fct_impl_id);
let specialize_self = program_fct_impl.extended_ty.clone();

let specialize_self = SpecializeSelf {
impl_id: program_fct_impl_id,
trait_ty: program_fct_impl.trait_ty.clone(),
extended_ty: program_fct_impl.extended_ty.clone(),
};

Some((bytecode_fct, Some(specialize_self)))
}
Expand All @@ -133,7 +145,7 @@ pub(super) fn compile_fct_to_code(
return_type: BytecodeType,
bytecode_fct: &BytecodeFunction,
type_params: &BytecodeTypeArray,
specialize_self: Option<BytecodeType>,
specialize_self: Option<SpecializeSelf>,
compiler: CompilerInvocation,
emit_compiler: bool,
mode: CompilationMode,
Expand Down Expand Up @@ -178,7 +190,7 @@ fn compile_fct_to_descriptor(
return_type: BytecodeType,
bytecode_fct: &BytecodeFunction,
type_params: &BytecodeTypeArray,
specialize_self: Option<BytecodeType>,
specialize_self: Option<SpecializeSelf>,
compiler: CompilerInvocation,
emit_compiler: bool,
mode: CompilationMode,
Expand Down Expand Up @@ -469,7 +481,7 @@ pub struct CompilationData<'a> {
pub return_type: BytecodeType,
pub fct_id: FunctionId,
pub type_params: BytecodeTypeArray,
pub specialize_self: Option<BytecodeType>,
pub specialize_self: Option<SpecializeSelf>,
pub loc: Location,

pub emit_debug: bool,
Expand Down
2 changes: 1 addition & 1 deletion dora-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod timer;
mod utils;
pub mod vm;

use compiler::codegen::get_bytecode;
use compiler::codegen::{get_bytecode, SpecializeSelf};
use gc::Address;
use shape::{Shape, ShapeVisitor};
pub use vm::VM;
Expand Down
Loading

0 comments on commit 4d1499b

Please sign in to comment.