Skip to content

Commit

Permalink
boots: Add Assoc and GenericAssoc to BytecodeType
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Feb 1, 2025
1 parent 742aaba commit ac97751
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 14 deletions.
27 changes: 23 additions & 4 deletions dora-runtime/src/boots/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use dora_bytecode::{
BytecodeFunction, BytecodeTypeArray, ConstPoolEntry, ConstPoolOpcode, EnumData, FunctionData,
Location, StructData,
};
use dora_bytecode::{BytecodeType, BytecodeTypeKind};
use dora_bytecode::{BytecodeTraitType, BytecodeType, BytecodeTypeKind};

pub fn allocate_encoded_system_config(vm: &VM) -> Ref<UInt8Array> {
let mut buffer = ByteBuffer::new();
Expand Down Expand Up @@ -276,14 +276,33 @@ fn encode_bytecode_type(vm: &VM, ty: &BytecodeType, buffer: &mut ByteBuffer) {
encode_bytecode_type_array(vm, params, buffer);
encode_bytecode_type(vm, ret.as_ref(), buffer);
}
BytecodeType::TypeAlias(..)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc { .. } => {
BytecodeType::GenericAssoc {
type_param_id,
trait_ty,
assoc_id,
} => {
buffer.emit_u8(BytecodeTypeKind::GenericAssoc as u8);
buffer.emit_u32(*type_param_id);
encode_bytecode_trait_type(vm, trait_ty, buffer);
buffer.emit_id(assoc_id.0 as usize);
}
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) => {
unreachable!()
}
}
}

fn encode_bytecode_trait_type(vm: &VM, trait_ty: &BytecodeTraitType, buffer: &mut ByteBuffer) {
buffer.emit_u32(trait_ty.trait_id.0);
encode_bytecode_type_array(vm, &trait_ty.type_params, buffer);
buffer.emit_u32(trait_ty.bindings.len() as u32);

for (alias_id, ty) in &trait_ty.bindings {
buffer.emit_u32(alias_id.0);
encode_bytecode_type(vm, ty, buffer);
}
}

fn encode_constpool_array(vm: &VM, fct: &BytecodeFunction, buffer: &mut ByteBuffer) {
buffer.emit_u32(fct.const_pool_entries().len() as u32);

Expand Down
2 changes: 1 addition & 1 deletion pkgs/boots/bytecode.dora
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use package::bytecode::data::{BytecodeFunction, BytecodeRegister, BytecodeType, ConstPoolId, ConstPoolEntry, Location};
pub use package::bytecode::data::{BytecodeFunction, BytecodeRegister, BytecodeTraitType, BytecodeType, ConstPoolId, ConstPoolEntry, Location};
pub use package::bytecode::dump::{BytecodeDumper, dumpInstruction};
pub use package::bytecode::program::{AliasId, AliasData, ClassId, ClassData, ClassFieldId,
ClassField, EnumId, EnumData, EnumVariant,
Expand Down
23 changes: 21 additions & 2 deletions pkgs/boots/bytecode/data.dora
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ pub enum BytecodeType {
TypeParam(Int32),
Lambda(Array[BytecodeType], BytecodeType),
TypeAlias(AliasId),
Assoc(AliasId, Array[BytecodeType]),
GenericAssoc { typeParamId: Int32, traitTy: BytecodeTraitType, assocId: AliasId },
}

impl BytecodeType {
Expand Down Expand Up @@ -256,7 +258,8 @@ impl BytecodeType {
BytecodeType::Tuple(subtypes) => subtypes.isGeneric(),
BytecodeType::TypeParam(idx) => true,
BytecodeType::Lambda(params, ret) => params.isGeneric() || ret.isGeneric(),
BytecodeType::TypeAlias(alias_id) => unreachable[Bool](),
BytecodeType::GenericAssoc(..) => true,
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) => unreachable[Bool](),
}
}

Expand Down Expand Up @@ -323,7 +326,13 @@ impl BytecodeType {
BytecodeType::Lambda(params, ret) => {
BytecodeType::Lambda(params.specialize(type_params), ret.specialize(type_params))
},
BytecodeType::TypeAlias(alias_id) => unreachable[BytecodeType](),
BytecodeType::GenericAssoc(typeParamId, ..) => {
let typeParamTy = type_params(typeParamId.toInt64());
assert(!typeParamTy.isGeneric());

unimplemented[BytecodeType]()
}
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) => unreachable[BytecodeType](),
}
}
}
Expand Down Expand Up @@ -456,6 +465,8 @@ impl Equals for BytecodeType {
_ => false,
}
}

BytecodeType::Assoc(..) | BytecodeType::GenericAssoc(..) => unreachable[Bool](),
}
}
}
Expand All @@ -481,6 +492,7 @@ impl Hash for BytecodeType {
BytecodeType::TypeParam(id) => opc::BC_TYPE_TYPE_PARAM ^ id,
BytecodeType::Lambda(params, ret) => params.hash() ^ ret.hash(),
BytecodeType::TypeAlias(id) => opc::BC_TYPE_TYPE_ALIAS ^ id.hash(),
BytecodeType::Assoc(..) | BytecodeType::GenericAssoc(..) => unreachable[Int32](),
}
}
}
Expand All @@ -506,6 +518,7 @@ impl Stringable for BytecodeType {
BytecodeType::TypeParam(idx) => "TypeParam(${idx})",
BytecodeType::Lambda(params, ty) => "Lambda(${params}, ${ty})",
BytecodeType::TypeAlias(id) => "TypeAlias(${id})",
BytecodeType::Assoc(..) | BytecodeType::GenericAssoc(..) => unreachable[String](),
}
}
}
Expand Down Expand Up @@ -545,3 +558,9 @@ pub struct Location {
pub line: Int32,
pub column: Int32,
}

pub class BytecodeTraitType {
pub trait_id: TraitId,
pub typeParams: Array[BytecodeType],
pub bindings: Array[(AliasId, BytecodeType)],
}
4 changes: 3 additions & 1 deletion pkgs/boots/bytecode/opcode.dora
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ pub const BC_TYPE_TRAIT_OBJECT: Int32 = 14;
pub const BC_TYPE_LAMBDA: Int32 = 15;
pub const BC_TYPE_TYPE_ALIAS: Int32 = 16;
pub const BC_TYPE_ASSOC: Int32 = 17;
pub const BC_TYPE_THIS: Int32 = 18;
pub const BC_TYPE_GENERIC_ASSOC: Int32 = 18;
pub const BC_TYPE_THIS: Int32 = 19;

pub const CONSTPOOL_OPCODE_STRING: Int32 = 0;
pub const CONSTPOOL_OPCODE_FLOAT32: Int32 = 1;
Expand Down Expand Up @@ -342,6 +343,7 @@ pub fn bytecodeTypeName(code: Int32): String {
if code == BC_TYPE_LAMBDA { return "Lambda"; }
if code == BC_TYPE_TYPE_ALIAS { return "TypeAlias"; }
if code == BC_TYPE_ASSOC { return "Assoc"; }
if code == BC_TYPE_GENERIC_ASSOC { return "GenericAssoc"; }
if code == BC_TYPE_THIS { return "This"; }
unreachable[String]()
}
Expand Down
22 changes: 21 additions & 1 deletion pkgs/boots/deserializer.dora
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use package::interface::{Address, Config, StructData, EnumData, EnumVariantData, CompilationMode};
use package::bytecode::{AliasId, BytecodeFunction, BytecodeType, ClassId, ConstPoolEntry, EnumId, FunctionId, ClassFieldId, Location, StructId, StructFieldId, TraitId};
use package::bytecode::{AliasId, BytecodeFunction, BytecodeTraitType, BytecodeType, ClassId, ConstPoolEntry, EnumId, FunctionId, ClassFieldId, Location, StructId, StructFieldId, TraitId};
use package::bytecode::opcode as opc;
use package::compilation::CompilationInfo;
use package::interface::{FunctionInliningData, FunctionInliningInfo, Architecture};
Expand Down Expand Up @@ -183,12 +183,32 @@ fn decodeBytecodeType(reader: ByteReader): BytecodeType {
} else if opcode == opc::BC_TYPE_TYPE_ALIAS {
let alias_id = AliasId(reader.readId());
BytecodeType::TypeAlias(alias_id)
} else if opcode == opc::BC_TYPE_GENERIC_ASSOC {
let typeParamId = reader.readId();
let traitTy = decodeBytecodeTraitType(reader);
let assocId = AliasId(reader.readId());
BytecodeType::GenericAssoc(typeParamId, traitTy, assocId)
} else {
println("unknown bytecode type opcode = ${opcode}");
unreachable[BytecodeType]()
}
}

fn decodeBytecodeTraitType(reader: ByteReader): BytecodeTraitType {
let trait_id = TraitId(reader.readId());
let typeParams = decodeBytecodeTypeArray(reader);
let length = reader.readInt32().toInt64();
let bindings = Vec[(AliasId, BytecodeType)]::newWithCapacity(length);

for _ in std::range(0, length) {
let alias_id = AliasId(reader.readId());
let ty = decodeBytecodeType(reader);
bindings.push((alias_id, ty));
}

BytecodeTraitType(trait_id, typeParams, bindings = bindings.toArray())
}

fn decodeConstPool(reader: ByteReader): Array[ConstPoolEntry] {
let size = reader.readInt32().toInt64();

Expand Down
6 changes: 4 additions & 2 deletions pkgs/boots/graph_builder.dora
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,9 @@ impl SsaGen {
}
BytecodeType::This
| BytecodeType::TypeParam(_)
| BytecodeType::TypeAlias(_) => unreachable[()](),
| BytecodeType::TypeAlias(_)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc(..) => unreachable[()](),
}
}

Expand Down Expand Up @@ -2716,7 +2718,7 @@ impl SsaGen {
| BytecodeType::Tuple(_) => Type::Address,
BytecodeType::This
| BytecodeType::TypeParam(_) => unreachable[Type](),
BytecodeType::TypeAlias(_) => unreachable[Type](),
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) | BytecodeType::GenericAssoc(..) => unreachable[Type](),
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkgs/boots/location.dora
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,9 @@ fn computeArgumentLocations(codegen: CodeGen, ci: CompilationInfo): ArgumentLoca
BytecodeType::Unit
| BytecodeType::This
| BytecodeType::TypeParam(_)
| BytecodeType::TypeAlias(_) => unreachable[Bool](),
| BytecodeType::TypeAlias(_)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc(..) => unreachable[Bool](),
};

if uses_fp_reg {
Expand Down
4 changes: 3 additions & 1 deletion pkgs/boots/regalloc.dora
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,8 @@ pub fn isReference(ty: BytecodeType): Bool {
EnumLayout::PtrOrNull(_) | EnumLayout::Tagged => true,
}
}
BytecodeType::TypeAlias(_) => unreachable[Bool](),
BytecodeType::TypeAlias(_)
| BytecodeType::Assoc(..)
| BytecodeType::GenericAssoc(..) => unreachable[Bool](),
}
}
2 changes: 1 addition & 1 deletion pkgs/boots/serializer.dora
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ pub fn encodeBytecodeType(writer: ByteWriter, ty: BytecodeType) {
encodeBytecodeTypeArray(writer, params);
encodeBytecodeType(writer, ret);
},
BytecodeType::TypeAlias(id) => {
BytecodeType::TypeAlias(..) | BytecodeType::Assoc(..) | BytecodeType::GenericAssoc(..) => {
unimplemented[()]();
}
}
Expand Down

0 comments on commit ac97751

Please sign in to comment.