diff --git a/libcore/src/lib.rs b/libcore/src/lib.rs index f9f222d7..7747d418 100644 --- a/libcore/src/lib.rs +++ b/libcore/src/lib.rs @@ -16,8 +16,8 @@ pub use types::*; pub use mimalloc::MiMalloc; -#[global_allocator] -static GLOBAL: MiMalloc = MiMalloc; +// #[global_allocator] +// static GLOBAL: MiMalloc = MiMalloc; pub const GET_LIB_FUNC_NAME: &str = "get_lib"; pub const GET_STORAGE_FUNC_NAME: &str = "get_storage"; diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index aa452ba3..aeac125a 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -16,7 +16,6 @@ use rust_i18n::t; use std::{ cell::RefCell, collections::{HashMap, HashSet}, - iter::Cloned, mem::swap, path::PathBuf, rc::Rc, @@ -49,6 +48,7 @@ pub struct ModuleUnit<'a> { first_func: bool, /// 对哈希表去重,并且记录每个dll的函数起始索引 modules_dll_dup: HashSet, + /// 每个模块的函数起始点 modules_info: HashMap, /// 引用的dll modules_dll: Vec, @@ -1019,8 +1019,9 @@ impl<'a> ModuleUnit<'a> { .token_lexer .borrow_mut() .add_id(func_item.get_name().to_owned()); - let func_extern_id = - func_item.buildin_id + self.modules_info[&import_file_path]; + let func_extern_id = FuncIdxTy( + *func_item.buildin_id + self.modules_info[&import_file_path], + ); // println!("{}", func_extern_id); let name = func_item.get_name().to_owned(); if let Err(e) = self.self_scope.borrow_mut().import_extern_func( @@ -1063,7 +1064,7 @@ impl<'a> ModuleUnit<'a> { } /// 生成修改变量的指令 - fn modify_var(&mut self, varty: TyIdxTy, var_addr: usize, is_global: bool) { + fn modify_var(&mut self, varty: ClassIdxId, var_addr: usize, is_global: bool) { let objsz = self.get_ty_sz(varty); if !is_global { self.add_double_bycode(Opcode::StoreLocal, var_addr as Opidx, objsz as Opidx); @@ -1236,8 +1237,8 @@ impl<'a> ModuleUnit<'a> { self.expr(true)?; let actual_ty = self.process_info.pop_last_ty().unwrap(); if ty != actual_ty { - let s1 = self.get_ty_name(ty); - let s2 = self.get_ty_name(actual_ty); + let s1 = self.get_ty_name_by_class_id(ty); + let s2 = self.get_ty_name_by_class_id(actual_ty); return self.gen_error(ErrorInfo::new( t!(RETURN_TYPE_ERROR, "0" = s1, "1" = s2), t!(TYPE_ERROR), @@ -1247,7 +1248,7 @@ impl<'a> ModuleUnit<'a> { None => { if self.expr(true).is_ok() { let actual_ty = self.process_info.pop_last_ty().unwrap(); - let name = self.get_ty_name(actual_ty); + let name = self.get_ty_name_by_class_id(actual_ty); return self.gen_error(ErrorInfo::new( t!(RETURN_TYPE_ERROR, "0" = "void", "1" = name), t!(TYPE_ERROR), diff --git a/src/compiler/ast/ast_base.rs b/src/compiler/ast/ast_base.rs index e835e00f..9a38015e 100644 --- a/src/compiler/ast/ast_base.rs +++ b/src/compiler/ast/ast_base.rs @@ -195,7 +195,7 @@ impl<'a> ModuleUnit<'a> { } /// 通过Scope中的ID获取类型名 - pub fn get_ty_name(&mut self, type_name: TyIdxTy) -> String { + pub fn get_ty_name(&mut self, type_name: ScopeAllocIdTy) -> String { self.self_scope .borrow() .get_class(type_name) @@ -204,6 +204,15 @@ impl<'a> ModuleUnit<'a> { .to_string() } + pub fn get_ty_name_by_class_id(&mut self, type_name: ClassIdxId) -> String { + self.self_scope + .borrow() + .get_class_by_class_id(type_name) + .unwrap() + .get_name() + .to_string() + } + /// 导入模块中的符号 pub fn import_module_sym(&mut self, lib: &Module) { for i in lib.functions() { diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index ba2212e6..6b4a51cf 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -3,7 +3,6 @@ use super::{ ValuePool, }; use libcore::*; -use num_enum::Default; use std::{ cell::RefCell, collections::HashMap, @@ -127,7 +126,7 @@ pub struct VarInfo { } impl VarInfo { - pub fn new(ty: TyIdxTy, var_idx: usize, addr: usize) -> Self { + pub fn new(ty: ClassIdxId, var_idx: usize, addr: usize) -> Self { Self { ty, var_idx, addr } } } @@ -440,14 +439,19 @@ impl SymScope { } /// 返回变量的索引和内存地址 - pub fn add_var(&mut self, id: ScopeAllocIdTy, ty: TyIdxTy, var_sz: usize) -> (VarIdxTy, usize) { + pub fn add_var( + &mut self, + id: ScopeAllocIdTy, + ty: ClassIdxId, + var_sz: usize, + ) -> (VarIdxTy, usize) { let ret_addr = self.var_sz; self.vars .insert(id, VarInfo::new(ty, self.vars_id, ret_addr)); let ret = self.vars_id; self.vars_id += 1; self.var_sz += var_sz; - (ret, ret_addr) + (VarIdxTy(ret), ret_addr) } pub fn get_var_table_sz(&self) -> usize { diff --git a/tests/test_all_examples.rs b/tests/test_all_examples.rs index 59b632bc..55210a6e 100644 --- a/tests/test_all_examples.rs +++ b/tests/test_all_examples.rs @@ -1,8 +1,8 @@ //! this test all examples files and check them output use assert_cmd::Command; +use libcore::utils::get_next_check_char; use std::fs::read_to_string; -use trc::base::utils::get_next_check_char; /// 检查迭代器是否剩下的所有字符都满足某个条件 fn check_whether_end(iter: &mut impl Iterator, condit: impl Fn(char) -> bool) -> bool { diff --git a/tests/test_compiler.rs b/tests/test_compiler.rs index 61783ca1..2e17a27b 100644 --- a/tests/test_compiler.rs +++ b/tests/test_compiler.rs @@ -41,7 +41,7 @@ fn get_func_id(scope: &mut ModuleUnit, name: &str) -> Opidx { .borrow() .get_sym(idstr) .expect("sym not found"); - scope + *scope .get_scope() .borrow() .get_function(symid)