Skip to content

Commit

Permalink
cannon: make --generic-bytecode the default mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Sep 25, 2020
1 parent aa89a11 commit a471b05
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 110 deletions.
83 changes: 7 additions & 76 deletions dora/src/bytecode/generator.rs
Expand Up @@ -53,7 +53,6 @@ pub fn generate<'ast>(
gen: BytecodeBuilder::new(&vm.args),
loops: Vec::new(),
var_registers: HashMap::new(),
generic_mode: false,
};
ast_bytecode_generator.generate()
}
Expand All @@ -79,7 +78,6 @@ pub fn generate_generic<'ast>(vm: &VM<'ast>, fct: &Fct<'ast>, src: &FctSrc) -> B
gen: BytecodeBuilder::new(&vm.args),
loops: Vec::new(),
var_registers: HashMap::new(),
generic_mode: true,
};
ast_bytecode_generator.generate()
}
Expand All @@ -95,9 +93,6 @@ struct AstBytecodeGen<'a, 'ast: 'a> {
gen: BytecodeBuilder,
loops: Vec<LoopLabels>,
var_registers: HashMap<VarId, Register>,

// true when generic bytecode should be generated
generic_mode: bool,
}

impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {
Expand Down Expand Up @@ -619,7 +614,7 @@ impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {

if ty.cls_id(self.vm) == Some(self.vm.vips.string_class) {
self.visit_expr(part, DataDest::Reg(part_register));
} else if ty.is_type_param() && self.generic_mode {
} else if ty.is_type_param() {
let type_list_id = match ty {
BuiltinType::TypeParam(id) => id,
_ => unreachable!(),
Expand Down Expand Up @@ -944,52 +939,7 @@ impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {
}

fn determine_callee(&mut self, call_type: &CallType) -> FctId {
match *call_type {
CallType::GenericMethod(id, trait_id, trait_fct_id) => {
if self.generic_mode {
trait_fct_id
} else {
// This happens for calls like (T: SomeTrait).method()
// Find the exact method that is called
let object_type = self.specialize_type(BuiltinType::TypeParam(id));
self.find_trait_impl(trait_fct_id, trait_id, object_type)
}
}
CallType::GenericStaticMethod(list_id, trait_id, trait_fct_id) => {
if self.generic_mode {
trait_fct_id
} else {
let ty = self.type_params[list_id.to_usize()];
let cls_id = ty.cls_id(self.vm).expect("no cls_id for type");

let cls = self.vm.classes.idx(cls_id);
let cls = cls.read();

let mut impl_fct_id: Option<FctId> = None;

for &impl_id in &cls.impls {
let ximpl = self.vm.impls[impl_id].read();

if ximpl.trait_id != Some(trait_id) {
continue;
}

for &fid in &ximpl.methods {
let method = self.vm.fcts.idx(fid);
let method = method.read();

if method.impl_for == Some(trait_fct_id) {
impl_fct_id = Some(fid);
break;
}
}
}

impl_fct_id.expect("no impl_fct_id found")
}
}
_ => call_type.fct_id().unwrap(),
}
call_type.fct_id().expect("FctId missing")
}

fn determine_callee_types(
Expand Down Expand Up @@ -1200,18 +1150,10 @@ impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {
}
CallType::TraitObjectMethod(_, _) => unimplemented!(),
CallType::GenericMethod(_, _, _) => {
if self.generic_mode {
self.emit_invoke_generic_direct(return_type, return_reg, callee_idx, pos);
} else {
self.emit_invoke_direct(return_type, return_reg, callee_idx, pos);
}
self.emit_invoke_generic_direct(return_type, return_reg, callee_idx, pos);
}
CallType::GenericStaticMethod(_, _, _) => {
if self.generic_mode {
self.emit_invoke_generic_static(return_type, return_reg, callee_idx, pos);
} else {
self.emit_invoke_static(return_type, return_reg, callee_idx, pos);
}
self.emit_invoke_generic_static(return_type, return_reg, callee_idx, pos);
}
CallType::Intrinsic(_) => unreachable!(),
}
Expand Down Expand Up @@ -1258,10 +1200,7 @@ impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {
BytecodeType::Int64 => self.gen.emit_mov_int64(dest, src),
BytecodeType::Ptr => self.gen.emit_mov_ptr(dest, src),
BytecodeType::Tuple(tuple_id) => self.gen.emit_mov_tuple(dest, src, tuple_id),
BytecodeType::TypeParam(_) => {
assert!(self.generic_mode);
self.gen.emit_mov_generic(dest, src)
}
BytecodeType::TypeParam(_) => self.gen.emit_mov_generic(dest, src),
}
}

Expand Down Expand Up @@ -2736,11 +2675,7 @@ impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {

match *call_type {
CallType::GenericStaticMethod(id, _, _) | CallType::GenericMethod(id, _, _) => {
if self.generic_mode {
self.gen.add_const_generic(id, fct.id, type_params)
} else {
self.gen.add_const_fct_types(fct.id, type_params)
}
self.gen.add_const_generic(id, fct.id, type_params)
}
_ => self.gen.add_const_fct_types(fct.id, type_params),
}
Expand Down Expand Up @@ -2791,11 +2726,7 @@ impl<'a, 'ast> AstBytecodeGen<'a, 'ast> {
}

fn specialize_type(&self, ty: BuiltinType) -> BuiltinType {
if self.generic_mode {
ty
} else {
specialize_type(self.vm, ty, self.type_params)
}
ty
}

fn ty(&self, id: NodeId) -> BuiltinType {
Expand Down
18 changes: 2 additions & 16 deletions dora/src/cannon.rs
@@ -1,6 +1,6 @@
use self::codegen::CannonCodeGen;

use crate::bytecode::{self, BytecodeFunction};
use crate::bytecode;
use crate::compiler::codegen::should_emit_bytecode;
use crate::compiler::Code;
use crate::ty::TypeList;
Expand All @@ -14,22 +14,8 @@ pub(super) fn compile<'a, 'ast: 'a>(
src: &'a FctSrc,
type_params: &TypeList,
) -> Code {
if vm.args.flag_generic_bytecode {
let bytecode_fct = fct.bytecode.as_ref().expect("bytecode missing");
compile_from_bytecode(vm, fct, src, type_params, &bytecode_fct)
} else {
let bytecode_fct = bytecode::generate(vm, fct, src, type_params);
compile_from_bytecode(vm, fct, src, type_params, &bytecode_fct)
}
}
let bytecode_fct = fct.bytecode.as_ref().expect("bytecode missing");

fn compile_from_bytecode<'a, 'ast: 'a>(
vm: &'a VM<'ast>,
fct: &Fct<'ast>,
src: &'a FctSrc,
type_params: &TypeList,
bytecode_fct: &BytecodeFunction,
) -> Code {
if should_emit_bytecode(vm, fct) {
bytecode::dump(vm, Some(fct), bytecode_fct);
}
Expand Down
6 changes: 1 addition & 5 deletions dora/src/cannon/codegen.rs
Expand Up @@ -2581,18 +2581,14 @@ where

fn specialize_bytecode_type(&self, ty: BytecodeType) -> BytecodeType {
match ty {
BytecodeType::TypeParam(id) => {
assert!(self.vm.args.flag_generic_bytecode);
self.type_params[id as usize].into()
}
BytecodeType::TypeParam(id) => self.type_params[id as usize].into(),
_ => ty,
}
}

fn specialize_bytecode_type_unit(&self, ty: BytecodeType) -> Option<BytecodeType> {
match ty {
BytecodeType::TypeParam(id) => {
assert!(self.vm.args.flag_generic_bytecode);
let ty = self.type_params[id as usize];

if ty.is_unit() {
Expand Down
3 changes: 0 additions & 3 deletions dora/src/driver/cmd.rs
Expand Up @@ -60,7 +60,6 @@ Options:
--compiler=<name> Switch default compiler. Possible values: cannon [default: cannon].
--test-filter=<name> Filter tests.
--clear-regs Clear register when freeing.
--generic-bytecode Generate generic bytecode.
--disable-tlab Disable tlab allocation.
--disable-barrier Disable barriers.
Expand Down Expand Up @@ -119,7 +118,6 @@ pub struct Args {
pub flag_boots: Option<String>,
pub flag_test_filter: Option<String>,
pub flag_clear_regs: bool,
pub flag_generic_bytecode: bool,

pub cmd_test: bool,
}
Expand Down Expand Up @@ -225,7 +223,6 @@ impl Default for Args {
flag_boots: None,
flag_test_filter: None,
flag_clear_regs: false,
flag_generic_bytecode: false,

cmd_test: false,
}
Expand Down
4 changes: 1 addition & 3 deletions dora/src/driver/start.rs
Expand Up @@ -67,9 +67,7 @@ pub fn start(content: Option<&str>) -> i32 {

semck::prelude::install_conditional_intrinsics(&mut vm);

if vm.args.flag_generic_bytecode {
semck::bytecode(&vm);
}
semck::bytecode(&vm);

if !vm.args.cmd_test && main.is_none() {
println!("error: no `main` function found in the program");
Expand Down
2 changes: 0 additions & 2 deletions tests/generic/generic-identity.dora
@@ -1,5 +1,3 @@
//= vm-args --generic-bytecode

fun main() {
assert(1 == identity[Int32](1));
assert(42.5F == identity[Float32](42.5F));
Expand Down
2 changes: 0 additions & 2 deletions tests/generic/generic-new-array1.dora
@@ -1,5 +1,3 @@
//= vm-args --generic-bytecode

fun main() {
assert(0L == newArray[Int32]().size());
assert(0L == newArray[()]().size());
Expand Down
2 changes: 1 addition & 1 deletion tests/generic/generic-trait-method1.dora
@@ -1,4 +1,4 @@
//= vm-args --generic-bytecode --gc=copy --gc-stress --gc-verify
//= vm-args --gc=copy --gc-stress --gc-verify

fun main() {
assert(17 * 17 == bar[SomeClass](SomeClass(17)));
Expand Down
2 changes: 0 additions & 2 deletions tests/generic/generic-trait-static1.dora
@@ -1,5 +1,3 @@
//= vm-args --generic-bytecode

fun main() {
assert(17 == bar[SomeClass]());
assert(42 == bar[OtherClass]());
Expand Down

0 comments on commit a471b05

Please sign in to comment.