Skip to content

Commit

Permalink
runtime: Move display-methods into dora-bytecode-crate
Browse files Browse the repository at this point in the history
  • Loading branch information
dinfuehr committed Oct 24, 2024
1 parent 41b3728 commit 338bb28
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 96 deletions.
47 changes: 44 additions & 3 deletions dora-runtime/src/vm/display.rs → dora-bytecode/src/display.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::vm::{module_path, module_path_name};
use dora_bytecode::{
BytecodeType, BytecodeTypeArray, FunctionId, FunctionKind, Program, TypeParamData,
use crate::{
BytecodeType, BytecodeTypeArray, FunctionId, FunctionKind, ModuleId, Program, TypeParamData,
};

pub fn display_fct(prog: &Program, fct_id: FunctionId) -> String {
Expand Down Expand Up @@ -266,3 +265,45 @@ impl<'a> std::fmt::Display for BytecodeTypePrinter<'a> {
self.name(&self.ty, f)
}
}

pub fn module_path_name(prog: &Program, module_id: ModuleId, name: &str) -> String {
let mut result = module_path(prog, module_id);

if !result.is_empty() {
result.push_str("::");
}

result.push_str(name);
result
}

pub fn module_path(prog: &Program, module_id: ModuleId) -> String {
let mut path = String::new();

let current_id = module_id;

// Do not print name for the top program module.
if current_id == prog.program_module_id() {
return "".into();
}

let module = prog.module(current_id);
path.push_str(&module.name);

let mut module_id = module.parent_id;

while let Some(current_id) = module_id {
// Do not print name for the top program module.
if current_id == prog.program_module_id() {
break;
}

let module = prog.module(current_id);
assert_ne!("<root>", module.name);
path.insert_str(0, "::");
path.insert_str(0, &module.name);
module_id = module.parent_id;
}

path
}
5 changes: 5 additions & 0 deletions dora-bytecode/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod data;
pub mod display;
pub mod dumper;
pub mod program;
pub mod reader;
Expand All @@ -9,6 +10,10 @@ pub mod writer;
mod tests;

pub use data::*;
pub use display::{
display_fct, display_ty, display_ty_array, display_ty_with_type_params,
display_ty_without_type_params, module_path, module_path_name,
};
pub use dumper::{dump, dump_stdout};
pub use program::{
AliasData, AliasId, ClassData, ClassField, ClassId, EnumData, EnumId, EnumVariant,
Expand Down
10 changes: 6 additions & 4 deletions dora-frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::sema::{Sema, SourceFileId};
use crate::sym::{ModuleSymTable, SymTable, Symbol, SymbolKind};
#[allow(unused)]
use crate::ty::{contains_self, empty_sta, SourceType, SourceTypeArray, TraitType, TyKind};
use dora_bytecode::{dump_stdout, Program};
use dora_bytecode::{display_fct, dump_stdout, FunctionId, Program};
use dora_parser::ast;
use dora_parser::Span;

Expand Down Expand Up @@ -140,10 +140,12 @@ pub fn generate_bytecode(sa: &Sema) {
}

pub fn emit_bytecode(prog: &Program, filter: &str) {
for fct in prog.functions.iter() {
for (id, fct) in prog.functions.iter().enumerate() {
let id = FunctionId(id.try_into().expect("overflow"));
if let Some(ref bc) = fct.bytecode {
if fct_pattern_match(&fct.name, filter) {
println!("Bytecode for {}:", fct.name);
let name = display_fct(prog, id);
if fct_pattern_match(&name, filter) {
println!("Bytecode for {}:", name);
dump_stdout(prog, bc);
}
}
Expand Down
8 changes: 4 additions & 4 deletions dora-runtime/src/boots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::mem;
use std::ptr;

use dora_bytecode::{
BytecodeType, BytecodeTypeArray, ClassId, EnumId, FunctionId, FunctionKind, GlobalId, StructId,
TraitId,
display_fct, BytecodeType, BytecodeTypeArray, ClassId, EnumId, FunctionId, FunctionKind,
GlobalId, StructId, TraitId,
};

use crate::boots::deserializer::{decode_code_descriptor, ByteReader};
Expand All @@ -17,8 +17,8 @@ use crate::size::InstanceSize;
use crate::threads::current_thread;
use crate::vm::compute_vtable_index;
use crate::vm::{
create_class_instance, create_enum_instance, display_fct,
ensure_class_instance_for_enum_variant, get_vm, impls, CodeDescriptor, FctImplementation, VM,
create_class_instance, create_enum_instance, ensure_class_instance_for_enum_variant, get_vm,
impls, CodeDescriptor, FctImplementation, VM,
};

use self::deserializer::{decode_bytecode_type, decode_bytecode_type_array};
Expand Down
14 changes: 7 additions & 7 deletions dora-runtime/src/cannon/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use crate::object::{Header, Str};
use crate::size::InstanceSize;
use crate::vm::{
compute_vtable_index, create_class_instance, create_enum_instance, create_struct_instance,
display_fct, display_ty, ensure_class_instance_for_enum_variant,
ensure_class_instance_for_lambda, ensure_class_instance_for_trait_object, find_trait_impl,
get_concrete_tuple_bty, get_concrete_tuple_bty_array, specialize_bty, specialize_bty_array,
BytecodeTypeExt, CodeDescriptor, EnumLayout, GcPoint, Intrinsic, LazyCompilationSite, Trap,
INITIALIZED, VM,
ensure_class_instance_for_enum_variant, ensure_class_instance_for_lambda,
ensure_class_instance_for_trait_object, find_trait_impl, get_concrete_tuple_bty,
get_concrete_tuple_bty_array, specialize_bty, specialize_bty_array, BytecodeTypeExt,
CodeDescriptor, EnumLayout, GcPoint, Intrinsic, LazyCompilationSite, Trap, INITIALIZED, VM,
};
use dora_bytecode::{
read, BytecodeFunction, BytecodeOffset, BytecodeType, BytecodeTypeArray, BytecodeVisitor,
ConstPoolEntry, ConstPoolIdx, FunctionId, FunctionKind, GlobalId, Location, Register,
display_fct, display_ty, read, BytecodeFunction, BytecodeOffset, BytecodeType,
BytecodeTypeArray, BytecodeVisitor, ConstPoolEntry, ConstPoolIdx, FunctionId, FunctionKind,
GlobalId, Location, Register,
};

macro_rules! comment {
Expand Down
10 changes: 5 additions & 5 deletions dora-runtime/src/compiler/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ use std::sync::Arc;
use std::time::Instant;

use dora_bytecode::{
BytecodeFunction, BytecodeInstruction, BytecodeReader, BytecodeType, BytecodeTypeArray,
ConstPoolEntry, FunctionId, FunctionKind, PackageId,
display_fct, BytecodeFunction, BytecodeInstruction, BytecodeReader, BytecodeType,
BytecodeTypeArray, ConstPoolEntry, FunctionId, FunctionKind, PackageId,
};

use crate::compiler::codegen::{ensure_runtime_entry_trampoline, CompilerInvocation};
use crate::compiler::{compile_fct_aot, trait_object_thunk, NativeFct, NativeFctKind};
use crate::gc::{formatted_size, Address};
use crate::os;
use crate::vm::{
display_fct, ensure_class_instance_for_lambda, ensure_class_instance_for_trait_object,
execute_on_main, find_trait_impl, specialize_bty, specialize_bty_array, BytecodeTypeExt,
ClassInstanceId, Code, LazyCompilationSite, ShapeKind, VM,
ensure_class_instance_for_lambda, ensure_class_instance_for_trait_object, execute_on_main,
find_trait_impl, specialize_bty, specialize_bty_array, BytecodeTypeExt, ClassInstanceId, Code,
LazyCompilationSite, ShapeKind, VM,
};

pub fn compile_boots_aot(vm: &VM) {
Expand Down
9 changes: 3 additions & 6 deletions dora-runtime/src/compiler/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ use crate::cpu::{FReg, Reg};
use crate::disassembler;
use crate::gc::Address;
use crate::os;
use crate::vm::{
display_fct, display_ty_without_type_params, install_code, Code, CodeDescriptor, CodeId,
CodeKind, Compiler, VM,
};
use crate::vm::{install_code, Code, CodeDescriptor, CodeId, CodeKind, Compiler, VM};
use dora_bytecode::{
dump_stdout, BytecodeFunction, BytecodeType, BytecodeTypeArray, FunctionData, FunctionId,
Location,
display_fct, display_ty_without_type_params, dump_stdout, BytecodeFunction, BytecodeType,
BytecodeTypeArray, FunctionData, FunctionId, Location,
};

#[derive(Clone, Copy)]
Expand Down
4 changes: 2 additions & 2 deletions dora-runtime/src/disassembler/capstone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::slice;

use capstone::prelude::*;

use crate::vm::{display_fct, display_ty, Code, VM};
use dora_bytecode::{BytecodeTypeArray, FunctionId};
use crate::vm::{Code, VM};
use dora_bytecode::{display_fct, display_ty, BytecodeTypeArray, FunctionId};

pub fn supported() -> bool {
true
Expand Down
4 changes: 1 addition & 3 deletions dora-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ pub mod vm;
mod vtable;

pub use vm::VM;
pub use vm::{
clear_vm, display_fct, execute_on_main, set_vm, CollectorName, Compiler, Flags, MemSize,
};
pub use vm::{clear_vm, execute_on_main, set_vm, CollectorName, Compiler, Flags, MemSize};
6 changes: 2 additions & 4 deletions dora-runtime/src/stack.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use dora_bytecode::{FunctionId, Location};
use dora_bytecode::{display_fct, FunctionId, Location};

use crate::handle::{create_handle, Handle};
use crate::object::{Array, Int32Array, Ref, Stacktrace, StacktraceIterator, Str};
use crate::threads::current_thread;
use crate::vm::{
display_fct, get_vm, Code, CodeId, CodeKind, InlinedFunctionId, InlinedLocation, VM,
};
use crate::vm::{get_vm, Code, CodeId, CodeKind, InlinedFunctionId, InlinedLocation, VM};

pub struct NativeStacktrace {
elems: Vec<StackElem>,
Expand Down
7 changes: 0 additions & 7 deletions dora-runtime/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ pub use self::code::{
};
pub use self::code_map::CodeMap;
pub use self::compilation::CompilationDatabase;
pub use self::display::{
display_fct, display_ty, display_ty_array, display_ty_with_type_params,
display_ty_without_type_params,
};
pub use self::enums::{enum_definition_name, EnumInstance, EnumInstanceId, EnumLayout};
pub use self::extensions::block_matches_ty;
pub use self::flags::{CollectorName, Compiler, Flags, MemSize};
Expand All @@ -48,7 +44,6 @@ pub use self::globals::{INITIALIZED, RUNNING, UNINITIALIZED};
pub use self::impls::{bounds_for_tp, find_trait_impl, tp_implements_trait, ty_implements_trait};
pub use self::known::Intrinsic;
use self::known::KnownElements;
pub use self::modules::{module_path, module_path_name};
pub use self::natives::{setup_builtin_natives, NativeMethods};
pub use self::specialize::{
add_ref_fields, compute_vtable_index, create_class_instance, create_enum_instance,
Expand All @@ -66,15 +61,13 @@ mod classes;
mod code;
mod code_map;
mod compilation;
mod display;
mod enums;
mod extensions;
mod flags;
mod globals;
pub mod impls;
mod initialize;
mod known;
mod modules;
mod natives;
mod specialize;
mod stdlib_lookup;
Expand Down
3 changes: 2 additions & 1 deletion dora-runtime/src/vm/code_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::cmp::Ordering;
use std::collections::BTreeMap;

use crate::gc::Address;
use crate::vm::display_fct;
use crate::vm::{CodeId, CodeKind, VM};

use dora_bytecode::display_fct;

pub struct CodeMap {
tree: RwLock<BTreeMap<CodeSpan, CodeId>>,
}
Expand Down
4 changes: 2 additions & 2 deletions dora-runtime/src/vm/enums.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use parking_lot::RwLock;

use crate::utils::Id;
use crate::vm::{module_path_name, ClassInstanceId, VM};
use crate::vm::{ClassInstanceId, VM};
use dora_bytecode::ty::BytecodeTypeArray;
use dora_bytecode::{EnumData, EnumId};
use dora_bytecode::{module_path_name, EnumData, EnumId};

pub fn enum_definition_name(enum_: &EnumData, vm: &VM) -> String {
module_path_name(&vm.program, enum_.module_id, &enum_.name)
Expand Down
43 changes: 0 additions & 43 deletions dora-runtime/src/vm/modules.rs

This file was deleted.

6 changes: 3 additions & 3 deletions dora-runtime/src/vm/stdlib_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::boots::BOOTS_FUNCTIONS;
use crate::gc::Address;
use crate::stdlib::io::IO_FUNCTIONS;
use crate::stdlib::STDLIB_FUNCTIONS;
use crate::vm::{display_fct, BytecodeType, Intrinsic, VM};
use crate::vm::{BytecodeType, Intrinsic, VM};
use dora_bytecode::{
ClassId, EnumId, ExtensionId, FunctionId, FunctionKind, ImplId, ModuleId, PackageId, Program,
StructId, TraitId,
display_fct, ClassId, EnumId, ExtensionId, FunctionId, FunctionKind, ImplId, ModuleId,
PackageId, Program, StructId, TraitId,
};

#[derive(Clone)]
Expand Down
4 changes: 2 additions & 2 deletions dora/src/driver/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::io::{self, Read, Write};
use std::path::PathBuf;

use crate::driver::cmd::{self, Args};
use dora_bytecode::{FunctionId, PackageId, Program};
use dora_bytecode::{display_fct, FunctionId, PackageId, Program};
use dora_frontend as language;
use dora_frontend::sema::{Sema, SemaArgs};
use dora_runtime::{clear_vm, display_fct, execute_on_main, set_vm, VM};
use dora_runtime::{clear_vm, execute_on_main, set_vm, VM};

pub fn start() -> i32 {
let args = cmd::parse_arguments();
Expand Down

0 comments on commit 338bb28

Please sign in to comment.