From 122cedba56a47a54a41a071c47faff9344b98d29 Mon Sep 17 00:00:00 2001 From: limuy Date: Thu, 11 Apr 2024 17:47:26 +0800 Subject: [PATCH] feat(tvm):extend the opcode args num feat(tvm):reduce repeated opcode --- Cargo.lock | 4 +- libcore/src/codegen.rs | 35 ++- libcore/src/dynadata.rs | 57 +++- libcore/src/lib.rs | 36 +++ src/compiler/ast.rs | 50 ++-- src/compiler/ast/ast_base.rs | 29 +- src/tools/dis.rs | 20 +- src/tvm.rs | 162 +++++----- src/tvm/function.rs | 18 +- stdlib/src/ds/sam.rs | 7 +- tests/test_compiler.rs | 562 +++++++++++++++++------------------ 11 files changed, 524 insertions(+), 456 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff6732a7..91f56eb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,9 +504,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "jobserver" -version = "0.1.28" +version = "0.1.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab46a6e9526ddef3ae7f787c06f0f2600639ba80ea3eade3d8e670a2230f51d6" +checksum = "f08474e32172238f2827bd160c67871cdb2801430f65c3979184dc362e3ca118" dependencies = [ "libc", ] diff --git a/libcore/src/codegen.rs b/libcore/src/codegen.rs index b27a800a..3e45d2f6 100644 --- a/libcore/src/codegen.rs +++ b/libcore/src/codegen.rs @@ -1,5 +1,5 @@ use core::{cmp::max, fmt}; -use std::{fmt::Display, usize}; +use std::fmt::Display; #[derive(Clone)] pub struct FuncStorage { @@ -112,18 +112,8 @@ pub enum Opcode { MoveBool, MoveStr, // Store a local var - StoreLocalObj, - StoreLocalInt, - StoreLocalFloat, - StoreLocalChar, - StoreLocalBool, - StoreLocalStr, - StoreGlobalObj, - StoreGlobalInt, - StoreGlobalFloat, - StoreGlobalChar, - StoreGlobalBool, - StoreGlobalStr, + StoreLocal, + StoreGlobal, // Do Nothing Empty, // a = -a @@ -149,7 +139,6 @@ pub enum Opcode { PopDataStr, PopDataChar, PopDataBool, - ImportNativeModule, } impl Display for Opcode { @@ -180,19 +169,29 @@ pub const ARG_WRONG: usize = usize::MAX; #[derive(Debug, PartialEq, Clone)] pub struct Inst { pub opcode: Opcode, - pub operand: usize, + pub operand: (usize, usize), } impl Inst { - pub fn new(opcode: Opcode, operand: usize) -> Self { - Self { opcode, operand } + pub fn new_single(opcode: Opcode, operand: usize) -> Self { + Self { + opcode, + operand: (operand, ARG_WRONG), + } + } + + pub fn new_double(opcode: Opcode, operand1: usize, operand2: usize) -> Self { + Self { + opcode, + operand: (operand1, operand2), + } } } impl Display for Inst { #[cfg(not(tarpaulin_include))] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{} {}", self.opcode, self.operand) + write!(f, "{} {} {}", self.opcode, self.operand.0, self.operand.1) } } diff --git a/libcore/src/dynadata.rs b/libcore/src/dynadata.rs index 3d4dff3e..8ae0e03a 100644 --- a/libcore/src/dynadata.rs +++ b/libcore/src/dynadata.rs @@ -9,11 +9,6 @@ pub fn get_max_stack_sz() -> usize { *T.get_or_init(|| 1024 * 1024 * 2 / size_of::()) } -pub fn get_trcobj_sz() -> usize { - static T: OnceLock = OnceLock::new(); - *T.get_or_init(size_of::<*mut dyn crate::types::TrcObj>) -} - #[derive(Default)] pub struct DynaData { pub gc: GcMgr, @@ -89,6 +84,23 @@ impl DynaData { unsafe { *(self.run_stack.as_ptr().byte_offset(self.stack_ptr as isize) as *const T) } } + /// Pop n bytes of stack + pub fn pop_n_bytes_data(&mut self, n: usize) -> *mut Byte { + debug_assert!(self.stack_ptr >= n); + self.stack_ptr -= n; + unsafe { + let ret = self + .run_stack + .as_mut_ptr() + .byte_offset(self.stack_ptr as isize); + #[cfg(debug_assertions)] + { + self.type_used.pop().unwrap(); + } + ret + } + } + /// Returns the top data of the data stack. /// /// # Panics @@ -117,15 +129,42 @@ impl DynaData { } } - pub fn set_var(&mut self, addr: usize, data: T) { + /// Sets the var of this [`DynaData`]. + /// + /// # Safety + /// make sure your addr is valid, or it will panic + /// . + pub unsafe fn set_var(&mut self, addr: usize, data: T) { unsafe { - *(self.var_store.as_mut_ptr().byte_offset(addr as isize) as *mut T) = data; + (self.get_var_addr_mut(addr) as *mut T).write(data); } } - pub fn get_var(&self, addr: usize) -> T { + /// Sets the var of this [`DynaData`]. + /// + /// # Safety + /// make sure your addr is valid, or it will panic + /// . + pub unsafe fn get_var(&self, addr: usize) -> T { debug_assert!(addr < self.var_used); - unsafe { *(self.var_store.as_ptr().byte_offset(addr as isize) as *const T) } + unsafe { *(self.get_var_addr(addr) as *const T) } + } + + /// Sets the var of this [`DynaData`]. + /// + /// # Safety + /// make sure your addr and src are valid, or it will panic + /// . + pub unsafe fn write_to_val(&mut self, addr: usize, src: *mut Byte, n: usize) { + unsafe { self.get_var_addr_mut(addr).copy_from(src, n) } + } + + fn get_var_addr(&self, addr: usize) -> *const Byte { + unsafe { self.var_store.as_ptr().byte_offset(addr as isize) } + } + + fn get_var_addr_mut(&mut self, addr: usize) -> *mut Byte { + unsafe { self.var_store.as_mut_ptr().byte_offset(addr as isize) } } pub fn alloc_var_space(&mut self, need_sz: usize) -> *mut Byte { diff --git a/libcore/src/lib.rs b/libcore/src/lib.rs index 4a499e5c..9adae8f1 100644 --- a/libcore/src/lib.rs +++ b/libcore/src/lib.rs @@ -14,4 +14,40 @@ pub use types::*; pub const GET_LIB_FUNC_NAME: &str = "get_lib"; pub const GET_STORAGE_FUNC_NAME: &str = "get_storage"; +#[macro_export] +macro_rules! intsz { + () => { + std::mem::size_of::<$crate::TrcIntInternal>() + }; +} +#[macro_export] +macro_rules! floatsz { + () => { + std::mem::size_of::<$crate::TrcFloatInternal>() + }; +} +#[macro_export] +macro_rules! charsz { + () => { + std::mem::size_of::<$crate::TrcCharInternal>() + }; +} +#[macro_export] +macro_rules! strsz { + () => { + std::mem::size_of::<$crate::TrcStrInternal>() + }; +} +#[macro_export] +macro_rules! boolsz { + () => { + std::mem::size_of::() + }; +} +#[macro_export] +macro_rules! objsz { + () => { + std::mem::size_of::<*mut dyn $crate::TrcObj>() + }; +} rust_i18n::i18n!("locales"); diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index 1fc88447..7f925c0d 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -238,14 +238,14 @@ impl<'a> AstBuilder<'a> { self.lex_until(RightBigBrace)?; self.add_bycode(Opcode::Jump, condit_id); let opcode_after_while = self.staticdata.get_next_opcode_id(); - self.staticdata.inst[jump_false_id].operand = opcode_after_while; + self.staticdata.inst[jump_false_id].operand.0 = opcode_after_while; let mut break_record = vec![]; swap( &mut break_record, &mut self.self_scope.borrow_mut().for_break, ); for i in break_record { - self.staticdata.inst[i].operand = opcode_after_while; + self.staticdata.inst[i].operand.0 = opcode_after_while; } let mut continue_record = vec![]; swap( @@ -253,7 +253,7 @@ impl<'a> AstBuilder<'a> { &mut self.self_scope.borrow_mut().for_continue, ); for i in continue_record { - self.staticdata.inst[i].operand = condit_id; + self.staticdata.inst[i].operand.0 = condit_id; } swap( &mut prev_loop_state, @@ -310,7 +310,7 @@ impl<'a> AstBuilder<'a> { // 跳转到条件判断语句 self.add_bycode(Opcode::Jump, conid_id); let next_opcode_after_for = self.staticdata.get_next_opcode_id(); - self.staticdata.inst[jump_false_id].operand = next_opcode_after_for; + self.staticdata.inst[jump_false_id].operand.0 = next_opcode_after_for; // 开始处理所有的break let mut break_record = vec![]; swap( @@ -318,7 +318,7 @@ impl<'a> AstBuilder<'a> { &mut self.self_scope.borrow_mut().for_break, ); for i in break_record { - self.staticdata.inst[i].operand = next_opcode_after_for; + self.staticdata.inst[i].operand.0 = next_opcode_after_for; } let mut continue_record = vec![]; swap( @@ -326,7 +326,7 @@ impl<'a> AstBuilder<'a> { &mut self.self_scope.borrow_mut().for_continue, ); for i in continue_record { - self.staticdata.inst[i].operand = opcode_goto; + self.staticdata.inst[i].operand.0 = opcode_goto; } // 重置循环状态 swap( @@ -684,7 +684,7 @@ impl<'a> AstBuilder<'a> { self.add_bycode(Opcode::Jump, ARG_WRONG); add_expr_addr(self.staticdata.get_last_opcode_id()); for i in &jump_into_case { - self.staticdata.inst[*i].operand = self.staticdata.get_next_opcode_id(); + self.staticdata.inst[*i].operand.0 = self.staticdata.get_next_opcode_id(); } self.get_token_checked(TokenType::LeftBigBrace)?; self.lex_until(RightBigBrace)?; @@ -708,7 +708,7 @@ impl<'a> AstBuilder<'a> { loop { for i in &jump_condit_save { let t = self.staticdata.get_next_opcode_id(); - self.staticdata.inst[*i].operand = t; + self.staticdata.inst[*i].operand.0 = t; } jump_condit_save.clear(); let t = self.token_lexer.next_token()?; @@ -726,7 +726,7 @@ impl<'a> AstBuilder<'a> { )?; } for i in jump_case_final { - self.staticdata.inst[i].operand = self.staticdata.get_next_opcode_id(); + self.staticdata.inst[i].operand.0 = self.staticdata.get_next_opcode_id(); } } } @@ -994,28 +994,12 @@ impl<'a> AstBuilder<'a> { /// 生成修改变量的指令 fn modify_var(&mut self, varty: TyIdxTy, var_addr: usize, is_global: bool) { - self.add_bycode( - if !is_global { - match self.convert_id_to_vm_ty(varty) { - VmStackType::Int => Opcode::StoreLocalInt, - VmStackType::Float => Opcode::StoreLocalFloat, - VmStackType::Str => Opcode::StoreLocalStr, - VmStackType::Char => Opcode::StoreLocalChar, - VmStackType::Bool => Opcode::StoreLocalBool, - VmStackType::Object => Opcode::StoreLocalObj, - } - } else { - match self.convert_id_to_vm_ty(varty) { - VmStackType::Int => Opcode::StoreGlobalInt, - VmStackType::Float => Opcode::StoreGlobalFloat, - VmStackType::Str => Opcode::StoreGlobalStr, - VmStackType::Char => Opcode::StoreGlobalChar, - VmStackType::Bool => Opcode::StoreGlobalBool, - VmStackType::Object => Opcode::StoreGlobalObj, - } - }, - var_addr, - ); + let objsz = self.get_ty_sz(varty); + if !is_global { + self.add_double_bycode(Opcode::StoreLocal, var_addr, objsz); + } else { + self.add_double_bycode(Opcode::StoreGlobal, var_addr, objsz); + } } /// 生成新建变量的指令 @@ -1107,7 +1091,7 @@ impl<'a> AstBuilder<'a> { // 本行是为了跳转到下一个分支 self.add_bycode(Opcode::JumpIfFalse, ARG_WRONG); self.lex_until(RightBigBrace)?; - self.staticdata.inst[op_idx].operand = self.staticdata.get_next_opcode_id(); + self.staticdata.inst[op_idx].operand.0 = self.staticdata.get_next_opcode_id(); self.add_bycode(Opcode::Jump, ARG_WRONG); save_jump_opcode_idx.push(self.staticdata.get_last_opcode_id()); let t = self.token_lexer.next_token()?; @@ -1129,7 +1113,7 @@ impl<'a> AstBuilder<'a> { break; } for i in save_jump_opcode_idx { - self.staticdata.inst[i].operand = self.staticdata.get_next_opcode_id(); + self.staticdata.inst[i].operand.0 = self.staticdata.get_next_opcode_id(); } Ok(()) } diff --git a/src/compiler/ast/ast_base.rs b/src/compiler/ast/ast_base.rs index ff67985b..a8c8ea30 100644 --- a/src/compiler/ast/ast_base.rs +++ b/src/compiler/ast/ast_base.rs @@ -5,7 +5,7 @@ use crate::compiler::token::TokenType; use crate::compiler::{scope::SymScope, token::ConstPoolIndexTy}; use libcore::*; use rust_i18n::t; -use std::{cell::RefCell, mem::size_of, rc::Rc}; +use std::{cell::RefCell, rc::Rc}; impl<'a> AstBuilder<'a> { pub fn clear_inst(&mut self) { @@ -26,12 +26,12 @@ impl<'a> AstBuilder<'a> { /// 获取对应类型的真实大小(内存对齐后) pub fn get_ty_sz(&self, id: TyIdxTy) -> usize { match self.convert_id_to_vm_ty(id) { - VmStackType::Int => size_of::(), - VmStackType::Float => size_of::(), - VmStackType::Str => size_of::<*mut String>(), - VmStackType::Char => size_of::(), - VmStackType::Bool => size_of::(), - VmStackType::Object => get_trcobj_sz(), + VmStackType::Int => intsz!(), + VmStackType::Float => floatsz!(), + VmStackType::Str => strsz!(), + VmStackType::Char => charsz!(), + VmStackType::Bool => boolsz!(), + VmStackType::Object => objsz!(), } } @@ -121,8 +121,7 @@ impl<'a> AstBuilder<'a> { } } - pub fn add_bycode(&mut self, opty: Opcode, opnum: usize) { - self.staticdata.inst.push(Inst::new(opty, opnum)); + fn gen_line_table(&mut self) { if !self.token_lexer.compiler_data.option.optimize { // 不生成行号表了 self.staticdata @@ -131,6 +130,18 @@ impl<'a> AstBuilder<'a> { } } + pub fn add_bycode(&mut self, opty: Opcode, opnum: usize) { + self.staticdata.inst.push(Inst::new_single(opty, opnum)); + self.gen_line_table(); + } + + pub fn add_double_bycode(&mut self, opty: Opcode, opnum1: usize, opnum2: usize) { + self.staticdata + .inst + .push(Inst::new_double(opty, opnum1, opnum2)); + self.gen_line_table(); + } + pub fn gen_args_error(&mut self, info: ArguError) -> ErrorInfo { match info { ArguError::TypeNotMatch(ArgumentError { expected, actual }) => ErrorInfo::new( diff --git a/src/tools/dis.rs b/src/tools/dis.rs index 7f892159..69c146ea 100644 --- a/src/tools/dis.rs +++ b/src/tools/dis.rs @@ -11,13 +11,25 @@ pub fn dis(opt: crate::compiler::CompileOption, rustcode: bool) -> RuntimeResult println!(); for i in static_data.inst.iter().enumerate() { if rustcode { - println!("Inst::new(Opcode::{}, {}),", i.1.opcode, i.1.operand); + if i.1.operand.1 == ARG_WRONG { + println!( + "Inst::new_single(Opcode::{}, {}),", + i.1.opcode, i.1.operand.0 + ); + } else { + println!( + "Inst::new_double(Opcode::{}, {}, {}),", + i.1.opcode, i.1.operand.0, i.1.operand.1 + ); + } } else { print!("{} {}", i.0, i.1); match i.1.opcode { - Opcode::LoadInt => print!("({})", static_data.constpool.intpool[i.1.operand]), - Opcode::LoadString => print!("({})", static_data.constpool.stringpool[i.1.operand]), - Opcode::LoadFloat => print!("({})", static_data.constpool.floatpool[i.1.operand]), + Opcode::LoadInt => print!("({})", static_data.constpool.intpool[i.1.operand.0]), + Opcode::LoadString => { + print!("({})", static_data.constpool.stringpool[i.1.operand.0]) + } + Opcode::LoadFloat => print!("({})", static_data.constpool.floatpool[i.1.operand.0]), _ => {} } println!(); diff --git a/src/tvm.rs b/src/tvm.rs index cd794dac..1cffee3c 100644 --- a/src/tvm.rs +++ b/src/tvm.rs @@ -95,27 +95,29 @@ macro_rules! impl_opcode_without_pop_first_data { }}; } -macro_rules! impl_store_local_var { - ($ty:path, $pc:expr, $sself:expr) => {{ - let data = $sself.dynadata.dydata.pop_data::<$ty>(); - let addr = $sself.static_data.inst[$pc].operand; - $sself - .dynadata - .frames_stack - .last_mut() - .unwrap() - .set_var(addr, data) - }}; -} +// macro_rules! impl_store_local_var { +// ($ty:path, $pc:expr, $sself:expr) => {{ +// let data = $sself.dynadata.dydata.pop_data::<$ty>(); +// let addr = $sself.static_data.inst[$pc].operand.0; +// $sself +// .dynadata +// .frames_stack +// .last_mut() +// .unwrap() +// .set_var(addr, data) +// }}; +// } macro_rules! impl_load_local_var { ($ty:path, $pc:expr, $sself:expr) => { - let data = $sself - .dynadata - .frames_stack - .last() - .unwrap() - .get_var::<$ty>($sself.static_data.inst[$pc].operand); + let data = unsafe { + $sself + .dynadata + .frames_stack + .last() + .unwrap() + .get_var::<$ty>($sself.static_data.inst[$pc].operand.0) + }; $sself.dynadata.dydata.push_data(data); }; } @@ -216,7 +218,7 @@ impl<'a> Vm<'a> { } Opcode::LoadInt => { self.dynadata.dydata.push_data( - self.static_data.constpool.intpool[self.static_data.inst[*pc].operand], + self.static_data.constpool.intpool[self.static_data.inst[*pc].operand.0], ); } Opcode::BitAnd => operator_opcode!(bit_and, self), @@ -225,23 +227,19 @@ impl<'a> Vm<'a> { Opcode::BitLeftShift => operator_opcode!(bit_left_shift, self), Opcode::BitRightShift => operator_opcode!(bit_right_shift, self), Opcode::LoadLocalVarObj => { - self.dynadata - .dydata - .push_data(self.dynadata.dydata.var_store[self.static_data.inst[*pc].operand]); - } - Opcode::StoreLocalObj => { - self.dynadata.dydata.var_store[self.static_data.inst[*pc].operand] = - self.dynadata.dydata.pop_data(); + self.dynadata.dydata.push_data( + self.dynadata.dydata.var_store[self.static_data.inst[*pc].operand.0], + ); } Opcode::LoadString => { - let tmp = self.static_data.inst[*pc].operand; + let tmp = self.static_data.inst[*pc].operand.0; let tmp = self.static_data.constpool.stringpool[tmp].clone(); let tmp = self.dynadata.dydata.gc.alloc(tmp); self.dynadata.dydata.push_data(tmp); } Opcode::LoadFloat => { self.dynadata.dydata.push_data( - self.static_data.constpool.floatpool[self.static_data.inst[*pc].operand], + self.static_data.constpool.floatpool[self.static_data.inst[*pc].operand.0], ); } Opcode::LoadBigInt => {} @@ -250,7 +248,7 @@ impl<'a> Vm<'a> { operator_opcode!(self_negative, self); } Opcode::CallNative => { - let tmp = self.dynadata.imported_func[self.static_data.inst[*pc].operand]( + let tmp = self.dynadata.imported_func[self.static_data.inst[*pc].operand.0]( &mut self.dynadata.dydata, ); self.convert_err_info(tmp)?; @@ -441,23 +439,23 @@ impl<'a> Vm<'a> { Opcode::JumpIfFalse => { let condit = impl_opcode!(bool, self, 1); if !condit { - *pc = self.static_data.inst[*pc].operand; + *pc = self.static_data.inst[*pc].operand.0; return Ok(()); } } Opcode::Jump => { - *pc = self.static_data.inst[*pc].operand; + *pc = self.static_data.inst[*pc].operand.0; return Ok(()); } Opcode::LoadChar => unsafe { self.dynadata.dydata.push_data(char::from_u32_unchecked( - self.static_data.inst[*pc].operand as u32, + self.static_data.inst[*pc].operand.0 as u32, )); }, Opcode::LoadBool => { self.dynadata .dydata - .push_data(self.static_data.inst[*pc].operand != 0); + .push_data(self.static_data.inst[*pc].operand.0 != 0); } Opcode::MoveInt => { let tmp = TrcInt::new(self.dynadata.dydata.pop_data()); @@ -485,21 +483,6 @@ impl<'a> Vm<'a> { let ptr = self.dynadata.dydata.gc.alloc(tmp); self.dynadata.dydata.push_data(ptr as *mut dyn TrcObj); } - Opcode::StoreLocalInt => { - impl_store_local_var!(TrcIntInternal, *pc, self); - } - Opcode::StoreLocalFloat => { - impl_store_local_var!(TrcFloatInternal, *pc, self); - } - Opcode::StoreLocalChar => { - impl_store_local_var!(TrcCharInternal, *pc, self); - } - Opcode::StoreLocalBool => { - impl_store_local_var!(bool, *pc, self); - } - Opcode::StoreLocalStr => { - impl_store_local_var!(TrcStrInternal, *pc, self); - } Opcode::LoadLocalVarBool => { impl_load_local_var!(bool, *pc, self); } @@ -521,70 +504,40 @@ impl<'a> Vm<'a> { } Opcode::CallCustom => { let var_table_mem_sz = - self.static_data.funcs[self.static_data.inst[*pc].operand].var_table_sz; + self.static_data.funcs[self.static_data.inst[*pc].operand.0].var_table_sz; let space = self.dynadata.dydata.alloc_var_space(var_table_mem_sz); self.dynadata.frames_stack.push(Frame::new(*pc, space)); - *pc = self.static_data.funcs[self.static_data.inst[*pc].operand].func_addr; + *pc = self.static_data.funcs[self.static_data.inst[*pc].operand.0].func_addr; return Ok(()); } - Opcode::StoreGlobalObj => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.pop_data::<*mut dyn TrcObj>(); - self.dynadata.dydata.set_var(addr, data); - } - Opcode::StoreGlobalInt => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.pop_data::(); - self.dynadata.dydata.set_var(addr, data); - } - Opcode::StoreGlobalFloat => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.pop_data::(); - self.dynadata.dydata.set_var(addr, data); - } - Opcode::StoreGlobalChar => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.pop_data::(); - self.dynadata.dydata.set_var(addr, data); - } - Opcode::StoreGlobalBool => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.pop_data::(); - self.dynadata.dydata.set_var(addr, data); - } - Opcode::StoreGlobalStr => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.pop_data::(); - self.dynadata.dydata.set_var(addr, data); - } Opcode::LoadGlobalVarObj => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.get_var::<*mut dyn TrcObj>(addr); + let addr = self.static_data.inst[*pc].operand.0; + let data = unsafe { self.dynadata.dydata.get_var::<*mut dyn TrcObj>(addr) }; self.dynadata.dydata.push_data(data); } Opcode::LoadGlobalVarBool => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.get_var::(addr); + let addr = self.static_data.inst[*pc].operand.0; + let data = unsafe { self.dynadata.dydata.get_var::(addr) }; self.dynadata.dydata.push_data(data); } Opcode::LoadGlobalVarInt => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.get_var::(addr); + let addr = self.static_data.inst[*pc].operand.0; + let data = unsafe { self.dynadata.dydata.get_var::(addr) }; self.dynadata.dydata.push_data(data); } Opcode::LoadGlobalVarFloat => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.get_var::(addr); + let addr = self.static_data.inst[*pc].operand.0; + let data = unsafe { self.dynadata.dydata.get_var::(addr) }; self.dynadata.dydata.push_data(data); } Opcode::LoadGlobalVarStr => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.get_var::(addr); + let addr = self.static_data.inst[*pc].operand.0; + let data = unsafe { self.dynadata.dydata.get_var::(addr) }; self.dynadata.dydata.push_data(data); } Opcode::LoadGlobalVarChar => { - let addr = self.static_data.inst[*pc].operand; - let data = self.dynadata.dydata.get_var::(addr); + let addr = self.static_data.inst[*pc].operand.0; + let data = unsafe { self.dynadata.dydata.get_var::(addr) }; self.dynadata.dydata.push_data(data); } Opcode::EqWithoutPop => { @@ -632,11 +585,32 @@ impl<'a> Vm<'a> { Opcode::JumpIfTrue => { let condit = impl_opcode!(bool, self, 1); if condit { - *pc = self.static_data.inst[*pc].operand; + *pc = self.static_data.inst[*pc].operand.0; return Ok(()); } } - Opcode::ImportNativeModule => todo!(), + Opcode::StoreLocal => { + let byte_num = self.static_data.inst[*pc].operand.1; + let var_addr = self.static_data.inst[*pc].operand.0; + let stack_ptr = self.dynadata.dydata.pop_n_bytes_data(byte_num); + unsafe { + self.dynadata + .frames_stack + .last_mut() + .unwrap() + .write_to(var_addr, stack_ptr, byte_num) + }; + } + Opcode::StoreGlobal => { + let var_addr = self.static_data.inst[*pc].operand.0; + let bytes_num = self.static_data.inst[*pc].operand.1; + let stack_data_ptr = self.dynadata.dydata.pop_n_bytes_data(bytes_num); + unsafe { + self.dynadata + .dydata + .write_to_val(var_addr, stack_data_ptr, bytes_num) + }; + } }; *pc += 1; Ok(()) diff --git a/src/tvm/function.rs b/src/tvm/function.rs index 70bd7edc..00de38fe 100644 --- a/src/tvm/function.rs +++ b/src/tvm/function.rs @@ -14,13 +14,21 @@ impl Frame { } } - pub fn set_var(&mut self, addr: usize, data: T) { - unsafe { - *(self.var_table_addr.byte_offset(addr as isize) as *mut Byte as *mut T) = data; - } + // pub unsafe fn set_var(&mut self, addr: usize, data: T) { + // unsafe { + // (self.get_addr(addr) as *mut T).write(data); + // } + // } + + pub unsafe fn write_to(&mut self, addr: usize, src: *mut Byte, n: usize) { + unsafe { self.get_addr(addr).copy_from(src, n) } } - pub fn get_var(&self, addr: usize) -> T { + pub unsafe fn get_var(&self, addr: usize) -> T { unsafe { *(self.var_table_addr.byte_offset(addr as isize) as *const Byte as *const T) } } + + fn get_addr(&self, addr: usize) -> *mut Byte { + unsafe { self.var_table_addr.byte_offset(addr as isize) } + } } diff --git a/stdlib/src/ds/sam.rs b/stdlib/src/ds/sam.rs index 1d189bd0..6502e171 100644 --- a/stdlib/src/ds/sam.rs +++ b/stdlib/src/ds/sam.rs @@ -1,6 +1,5 @@ use collection_literals::collection; use derive::{trc_class, trc_function, trc_method}; -use libcore::error::*; use libcore::libbasic::*; use libcore::types::trcchar::TrcChar; use libcore::types::TrcObj; @@ -90,6 +89,12 @@ impl Sam { } } +impl Default for Sam { + fn default() -> Self { + Self::new() + } +} + #[trc_method] impl Sam { #[trc_function(method = true)] diff --git a/tests/test_compiler.rs b/tests/test_compiler.rs index 7cd123ac..f503388e 100644 --- a/tests/test_compiler.rs +++ b/tests/test_compiler.rs @@ -51,7 +51,7 @@ fn opcode_assert_eq(expect: Vec, acual: Vec) { "\nexpected:{:?}\nactual:{:?}", expect, acual ); - if i.opcode == Opcode::CallNative && j.operand == SEPCIAL_FUNC_ID { + if i.opcode == Opcode::CallNative && j.operand.0 == SEPCIAL_FUNC_ID { continue; } assert_eq!( @@ -76,19 +76,19 @@ fn test_assign() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::StoreGlobalInt, get_offset(0)), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::StoreGlobalInt, get_offset(0)), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::StoreGlobalInt, get_offset(1)), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(0)), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(1)), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_double(Opcode::StoreGlobal, get_offset(1), intsz!()), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(0)), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(1)), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::CallNative, fid), ], ) } @@ -99,7 +99,7 @@ fn test_expr_easy1() { t.generate_code().unwrap(); opcode_assert_eq( t.staticdata.inst, - vec![Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE)], + vec![Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ONE)], ); } @@ -110,10 +110,10 @@ fn test_expr_easy2() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::BitNotInt, NO_ARG), - Inst::new(Opcode::AddInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::BitNotInt, NO_ARG), + Inst::new_single(Opcode::AddInt, NO_ARG), ], ); } @@ -125,11 +125,11 @@ fn text_expr_easy3() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::SubInt, NO_ARG), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::SubInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::SubInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::SubInt, NO_ARG), ], ) } @@ -141,11 +141,11 @@ fn test_expr_easy4() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::SubInt, NO_ARG), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::MulInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::SubInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::MulInt, NO_ARG), ], ) } @@ -157,15 +157,15 @@ fn test_expr() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::AddInt, NO_ARG), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::LoadInt, 5), - Inst::new(Opcode::PowerInt, NO_ARG), - Inst::new(Opcode::MulInt, NO_ARG), - Inst::new(Opcode::SubInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ONE), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::AddInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::LoadInt, 5), + Inst::new_single(Opcode::PowerInt, NO_ARG), + Inst::new_single(Opcode::MulInt, NO_ARG), + Inst::new_single(Opcode::SubInt, NO_ARG), ], ); } @@ -177,26 +177,26 @@ fn test_expr_final() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::SelfNegativeInt, NO_ARG), - Inst::new(Opcode::AddInt, NO_ARG), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::MulInt, NO_ARG), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::LoadInt, 5), - Inst::new(Opcode::LoadInt, 6), - Inst::new(Opcode::PowerInt, NO_ARG), - Inst::new(Opcode::PowerInt, NO_ARG), - Inst::new(Opcode::ExactDivInt, NO_ARG), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::EqInt, NO_ARG), - Inst::new(Opcode::LoadInt, 7), - Inst::new(Opcode::LoadInt, 8), - Inst::new(Opcode::LoadInt, 9), - Inst::new(Opcode::BitAndInt, NO_ARG), - Inst::new(Opcode::EqInt, NO_ARG), - Inst::new(Opcode::OrBool, NO_ARG), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ONE), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::SelfNegativeInt, NO_ARG), + Inst::new_single(Opcode::AddInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::MulInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::LoadInt, 5), + Inst::new_single(Opcode::LoadInt, 6), + Inst::new_single(Opcode::PowerInt, NO_ARG), + Inst::new_single(Opcode::PowerInt, NO_ARG), + Inst::new_single(Opcode::ExactDivInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::EqInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 7), + Inst::new_single(Opcode::LoadInt, 8), + Inst::new_single(Opcode::LoadInt, 9), + Inst::new_single(Opcode::BitAndInt, NO_ARG), + Inst::new_single(Opcode::EqInt, NO_ARG), + Inst::new_single(Opcode::OrBool, NO_ARG), ], ); } @@ -214,15 +214,15 @@ print("{}", a+90)"#, opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::StoreGlobalInt, get_offset(0)), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(0)), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::AddInt, NO_ARG), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(0)), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::AddInt, NO_ARG), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ONE), + Inst::new_single(Opcode::CallNative, fid), ], ) } @@ -235,9 +235,9 @@ fn test_call_builtin_function() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), ], ) } @@ -274,13 +274,13 @@ fn test_if_easy() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::EqInt, NO_ARG), - Inst::new(Opcode::JumpIfFalse, 7), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::EqInt, NO_ARG), + Inst::new_single(Opcode::JumpIfFalse, 7), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), ], ) } @@ -323,23 +323,23 @@ if a<8{ opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::StoreGlobalInt, 0), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::LtInt, 0), - Inst::new(Opcode::JumpIfFalse, 6), - Inst::new(Opcode::Jump, 17), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::GtInt, 0), - Inst::new(Opcode::JumpIfFalse, 11), - Inst::new(Opcode::Jump, 17), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::LoadInt, 5), - Inst::new(Opcode::EqInt, 0), - Inst::new(Opcode::JumpIfFalse, 16), - Inst::new(Opcode::Jump, 17), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::LtInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 6), + Inst::new_single(Opcode::Jump, 17), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::GtInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 11), + Inst::new_single(Opcode::Jump, 17), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::LoadInt, 5), + Inst::new_single(Opcode::EqInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 16), + Inst::new_single(Opcode::Jump, 17), ], ) } @@ -352,15 +352,15 @@ fn test_var_params() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ONE), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::CallNative, fid), ], ) } @@ -373,14 +373,14 @@ fn test_while_1() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::EqInt, NO_ARG), - Inst::new(Opcode::JumpIfFalse, 8), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Jump, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::EqInt, NO_ARG), + Inst::new_single(Opcode::JumpIfFalse, 8), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Jump, 0), ], ) } @@ -393,20 +393,20 @@ fn test_for_1() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::StoreGlobalInt, get_offset(0)), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(0)), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::LtInt, NO_ARG), - Inst::new(Opcode::JumpIfFalse, 14), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(0)), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::AddInt, NO_ARG), - Inst::new(Opcode::StoreGlobalInt, get_offset(0)), - Inst::new(Opcode::Jump, 2), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(0)), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::LtInt, NO_ARG), + Inst::new_single(Opcode::JumpIfFalse, 14), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(0)), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::AddInt, NO_ARG), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::Jump, 2), ], ) } @@ -429,16 +429,16 @@ func f() { opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::Stop, NO_ARG), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::PopFrame, NO_ARG), - Inst::new(Opcode::LoadString, 1), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::CallCustom, 0), - Inst::new(Opcode::PopFrame, NO_ARG), + Inst::new_single(Opcode::Stop, NO_ARG), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::PopFrame, NO_ARG), + Inst::new_single(Opcode::LoadString, 1), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::CallCustom, 0), + Inst::new_single(Opcode::PopFrame, NO_ARG), ], ) } @@ -458,12 +458,12 @@ f()"#, opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::CallCustom, 0), - Inst::new(Opcode::Stop, NO_ARG), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::PopFrame, NO_ARG), + Inst::new_single(Opcode::CallCustom, 0), + Inst::new_single(Opcode::Stop, NO_ARG), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::PopFrame, NO_ARG), ], ) } @@ -487,31 +487,31 @@ print("{}{}", a, b) opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallCustom, 0), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ONE), - Inst::new(Opcode::StoreGlobalInt, get_offset(0)), - Inst::new(Opcode::LoadInt, INT_VAL_POOL_ZERO), - Inst::new(Opcode::StoreGlobalInt, get_offset(1)), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(0)), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadGlobalVarInt, get_offset(1)), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Stop, NO_ARG), - Inst::new(Opcode::StoreLocalInt, get_offset(0)), - Inst::new(Opcode::StoreLocalInt, get_offset(1)), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadLocalVarInt, get_offset(1)), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadLocalVarInt, get_offset(0)), - Inst::new(Opcode::MoveInt, NO_ARG), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::PopFrame, NO_ARG), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallCustom, 0), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ONE), + Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()), + Inst::new_single(Opcode::LoadInt, INT_VAL_POOL_ZERO), + Inst::new_double(Opcode::StoreGlobal, get_offset(1), intsz!()), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(0)), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(1)), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Stop, NO_ARG), + Inst::new_double(Opcode::StoreLocal, get_offset(0), intsz!()), + Inst::new_double(Opcode::StoreLocal, get_offset(1), intsz!()), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadLocalVarInt, get_offset(1)), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadLocalVarInt, get_offset(0)), + Inst::new_single(Opcode::MoveInt, NO_ARG), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::PopFrame, NO_ARG), ], ) } @@ -547,58 +547,58 @@ while a<10{ opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::StoreGlobalInt, 0), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::LeInt, 0), - Inst::new(Opcode::JumpIfFalse, 26), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::EqInt, 0), - Inst::new(Opcode::JumpIfFalse, 11), - Inst::new(Opcode::Jump, 21), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::EqInt, 0), - Inst::new(Opcode::JumpIfFalse, 16), - Inst::new(Opcode::Jump, 26), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::MoveInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::AddInt, 0), - Inst::new(Opcode::StoreGlobalInt, 0), - Inst::new(Opcode::Jump, 2), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::StoreGlobalInt, 8), - Inst::new(Opcode::LoadGlobalVarInt, 8), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::LtInt, 0), - Inst::new(Opcode::JumpIfFalse, 52), - Inst::new(Opcode::LoadGlobalVarInt, 8), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::AddInt, 0), - Inst::new(Opcode::StoreGlobalInt, 8), - Inst::new(Opcode::LoadGlobalVarInt, 8), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::EqInt, 0), - Inst::new(Opcode::JumpIfFalse, 41), - Inst::new(Opcode::Jump, 28), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, 8), - Inst::new(Opcode::MoveInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::LoadGlobalVarInt, 8), - Inst::new(Opcode::LoadInt, 4), - Inst::new(Opcode::EqInt, 0), - Inst::new(Opcode::JumpIfFalse, 51), - Inst::new(Opcode::Jump, 52), - Inst::new(Opcode::Jump, 28), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_double(Opcode::StoreGlobal, 0, intsz!()), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::LeInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 26), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::EqInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 11), + Inst::new_single(Opcode::Jump, 21), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::EqInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 16), + Inst::new_single(Opcode::Jump, 26), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::MoveInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::AddInt, 0), + Inst::new_double(Opcode::StoreGlobal, 0, intsz!()), + Inst::new_single(Opcode::Jump, 2), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_double(Opcode::StoreGlobal, 8, intsz!()), + Inst::new_single(Opcode::LoadGlobalVarInt, 8), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::LtInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 52), + Inst::new_single(Opcode::LoadGlobalVarInt, 8), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::AddInt, 0), + Inst::new_double(Opcode::StoreGlobal, 8, intsz!()), + Inst::new_single(Opcode::LoadGlobalVarInt, 8), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::EqInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 41), + Inst::new_single(Opcode::Jump, 28), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, 8), + Inst::new_single(Opcode::MoveInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadGlobalVarInt, 8), + Inst::new_single(Opcode::LoadInt, 4), + Inst::new_single(Opcode::EqInt, 0), + Inst::new_single(Opcode::JumpIfFalse, 51), + Inst::new_single(Opcode::Jump, 52), + Inst::new_single(Opcode::Jump, 28), ], ); } @@ -620,12 +620,12 @@ print("{}", math::sin(9.8)) opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadString, 1), - Inst::new(Opcode::LoadFloat, 0), - Inst::new(Opcode::CallNative, SEPCIAL_FUNC_ID), - Inst::new(Opcode::MoveFloat, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadString, 1), + Inst::new_single(Opcode::LoadFloat, 0), + Inst::new_single(Opcode::CallNative, SEPCIAL_FUNC_ID), + Inst::new_single(Opcode::MoveFloat, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallNative, fid), ], ); } @@ -655,41 +655,41 @@ println("out of range") opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::StoreGlobalInt, 0), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::EqIntWithoutPop, 0), - Inst::new(Opcode::JumpIfTrue, 7), - Inst::new(Opcode::Jump, 13), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::MoveInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Jump, 32), - Inst::new(Opcode::LoadInt, 3), - Inst::new(Opcode::EqIntWithoutPop, 0), - Inst::new(Opcode::JumpIfTrue, 20), - Inst::new(Opcode::LoadInt, 2), - Inst::new(Opcode::EqIntWithoutPop, 0), - Inst::new(Opcode::JumpIfTrue, 20), - Inst::new(Opcode::Jump, 26), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::LoadGlobalVarInt, 0), - Inst::new(Opcode::MoveInt, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Jump, 32), - Inst::new(Opcode::Jump, 28), - Inst::new(Opcode::Jump, 32), - Inst::new(Opcode::LoadString, 1), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Jump, 32), - Inst::new(Opcode::LoadString, 2), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::CallNative, fid), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_double(Opcode::StoreGlobal, 0, intsz!()), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::EqIntWithoutPop, 0), + Inst::new_single(Opcode::JumpIfTrue, 7), + Inst::new_single(Opcode::Jump, 13), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::MoveInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Jump, 32), + Inst::new_single(Opcode::LoadInt, 3), + Inst::new_single(Opcode::EqIntWithoutPop, 0), + Inst::new_single(Opcode::JumpIfTrue, 20), + Inst::new_single(Opcode::LoadInt, 2), + Inst::new_single(Opcode::EqIntWithoutPop, 0), + Inst::new_single(Opcode::JumpIfTrue, 20), + Inst::new_single(Opcode::Jump, 26), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::LoadGlobalVarInt, 0), + Inst::new_single(Opcode::MoveInt, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Jump, 32), + Inst::new_single(Opcode::Jump, 28), + Inst::new_single(Opcode::Jump, 32), + Inst::new_single(Opcode::LoadString, 1), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Jump, 32), + Inst::new_single(Opcode::LoadString, 2), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_single(Opcode::CallNative, fid), ], ) } @@ -715,28 +715,28 @@ println("run final!") opcode_assert_eq( t.staticdata.inst, vec![ - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::StoreGlobalStr, 0), - Inst::new(Opcode::LoadGlobalVarStr, 0), - Inst::new(Opcode::LoadString, 0), - Inst::new(Opcode::EqStrWithoutPop, 0), - Inst::new(Opcode::JumpIfTrue, 10), - Inst::new(Opcode::LoadString, 1), - Inst::new(Opcode::EqStrWithoutPop, 0), - Inst::new(Opcode::JumpIfTrue, 10), - Inst::new(Opcode::Jump, 16), - Inst::new(Opcode::LoadString, 2), - Inst::new(Opcode::LoadGlobalVarStr, 0), - Inst::new(Opcode::MoveStr, 0), - Inst::new(Opcode::LoadInt, 1), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Jump, 22), - Inst::new(Opcode::Jump, 18), - Inst::new(Opcode::Jump, 22), - Inst::new(Opcode::LoadString, 3), - Inst::new(Opcode::LoadInt, 0), - Inst::new(Opcode::CallNative, fid), - Inst::new(Opcode::Jump, 22), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_double(Opcode::StoreGlobal, 0, strsz!()), + Inst::new_single(Opcode::LoadGlobalVarStr, 0), + Inst::new_single(Opcode::LoadString, 0), + Inst::new_single(Opcode::EqStrWithoutPop, 0), + Inst::new_single(Opcode::JumpIfTrue, 10), + Inst::new_single(Opcode::LoadString, 1), + Inst::new_single(Opcode::EqStrWithoutPop, 0), + Inst::new_single(Opcode::JumpIfTrue, 10), + Inst::new_single(Opcode::Jump, 16), + Inst::new_single(Opcode::LoadString, 2), + Inst::new_single(Opcode::LoadGlobalVarStr, 0), + Inst::new_single(Opcode::MoveStr, 0), + Inst::new_single(Opcode::LoadInt, 1), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Jump, 22), + Inst::new_single(Opcode::Jump, 18), + Inst::new_single(Opcode::Jump, 22), + Inst::new_single(Opcode::LoadString, 3), + Inst::new_single(Opcode::LoadInt, 0), + Inst::new_single(Opcode::CallNative, fid), + Inst::new_single(Opcode::Jump, 22), ], ) }