diff --git a/rust/src/base/codegen.rs b/rust/src/base/codegen.rs index a3d69d18..f9b6de4d 100644 --- a/rust/src/base/codegen.rs +++ b/rust/src/base/codegen.rs @@ -82,6 +82,12 @@ pub enum Opcode { LoadChar, LoadBool, LoadLocal, + LoadVarBool, + LoadVarInt, + LoadVarFloat, + LoadVarStr, + LoadVarChar, + // Move a value into the stack MoveInt, MoveFloat, MoveChar, @@ -89,6 +95,11 @@ pub enum Opcode { MoveStr, // Store a local var StoreLocal, + StoreInt, + StoreFloat, + StoreChar, + StoreBool, + StoreStr, // Do Nothing Empty, // a = -a diff --git a/rust/src/compiler/ast.rs b/rust/src/compiler/ast.rs index 42c76311..eef6b14a 100644 --- a/rust/src/compiler/ast.rs +++ b/rust/src/compiler/ast.rs @@ -560,7 +560,8 @@ impl<'a> AstBuilder<'a> { } self.expr(false)?; let var = var.unwrap(); - self.add_bycode(Opcode::StoreLocal, var) + self.add_bycode(Opcode::StoreLocal, var); + return Ok(()); } TokenType::Store => { if self.self_scope.as_ref().borrow().has_sym(name) { @@ -581,7 +582,8 @@ impl<'a> AstBuilder<'a> { self.staticdata.update_sym_table_sz( self.self_scope.as_ref().borrow().get_scope_last_idx(), ); - self.add_bycode(Opcode::StoreLocal, var_idx) + self.add_bycode(Opcode::StoreLocal, var_idx); + return Ok(()); } _ => { self.token_lexer.next_back(tt); diff --git a/rust/src/tvm.rs b/rust/src/tvm.rs index b7076b15..df029b0a 100644 --- a/rust/src/tvm.rs +++ b/rust/src/tvm.rs @@ -30,12 +30,17 @@ pub struct DynaData<'a> { gc: GcMgr, obj_stack: Vec<*mut dyn TrcObj>, int_stack: Vec, - str_stack: Vec, + str_stack: Vec<*mut String>, float_stack: Vec, bool_stack: Vec, char_stack: Vec, frames_stack: Vec>, var_store: Vec<*mut dyn TrcObj>, + int_store: Vec, + float_store: Vec, + str_store: Vec<*mut String>, + bool_store: Vec, + char_store: Vec, } impl<'a> DynaData<'a> { @@ -265,11 +270,10 @@ impl<'a> Vm<'a> { self.dynadata.obj_stack.pop().unwrap(); } Opcode::LoadString => { - self.dynadata.str_stack.push( - self.static_data.constpool.stringpool - [self.static_data.inst[self.pc].operand] - .clone(), - ); + let tmp = self.static_data.inst[self.pc].operand; + let tmp = self.static_data.constpool.stringpool[tmp].clone(); + let tmp = self.dynadata.gc.alloc(tmp); + self.dynadata.str_stack.push(tmp); } Opcode::LoadFloat => { self.dynadata.float_stack.push( @@ -297,7 +301,11 @@ impl<'a> Vm<'a> { } Opcode::AddStr => { let (first, second) = impl_opcode!(self.dynadata.str_stack, self, 2); - self.dynadata.str_stack.push(format!("{}{}", first, second)); + self.dynadata.str_stack.push( + self.dynadata + .gc + .alloc(unsafe { format!("{}{}", *first, *second) }), + ); } Opcode::SubInt => { let (first, second) = impl_opcode!(self.dynadata.int_stack, self, 2); @@ -513,12 +521,57 @@ impl<'a> Vm<'a> { self.dynadata.obj_stack.push(ptr); } Opcode::MoveStr => { - let ptr = self - .dynadata - .gc - .alloc(TrcStr::new(self.dynadata.str_stack.pop().unwrap())); + // todo:inmprove performance + let ptr = self.dynadata.gc.alloc(TrcStr::new(unsafe { + self.dynadata.str_stack.pop().unwrap() + })); self.dynadata.obj_stack.push(ptr); } + Opcode::StoreInt => { + self.dynadata.int_store[self.static_data.inst[self.pc].operand] = + self.dynadata.int_stack.pop().unwrap(); + } + Opcode::StoreFloat => { + self.dynadata.float_store[self.static_data.inst[self.pc].operand] = + self.dynadata.float_stack.pop().unwrap(); + } + Opcode::StoreChar => { + self.dynadata.char_store[self.static_data.inst[self.pc].operand] = + self.dynadata.char_stack.pop().unwrap(); + } + Opcode::StoreBool => { + self.dynadata.bool_store[self.static_data.inst[self.pc].operand] = + self.dynadata.bool_stack.pop().unwrap(); + } + Opcode::StoreStr => { + self.dynadata.str_store[self.static_data.inst[self.pc].operand] = + self.dynadata.str_stack.pop().unwrap(); + } + Opcode::LoadVarBool => { + self.dynadata + .bool_stack + .push(self.dynadata.bool_store[self.static_data.inst[self.pc].operand]); + } + Opcode::LoadVarInt => { + self.dynadata + .int_stack + .push(self.dynadata.int_store[self.static_data.inst[self.pc].operand]); + } + Opcode::LoadVarFloat => { + self.dynadata + .float_stack + .push(self.dynadata.float_store[self.static_data.inst[self.pc].operand]); + } + Opcode::LoadVarStr => { + self.dynadata + .str_stack + .push(self.dynadata.str_store[self.static_data.inst[self.pc].operand]); + } + Opcode::LoadVarChar => { + self.dynadata + .char_stack + .push(self.dynadata.char_store[self.static_data.inst[self.pc].operand]); + } } self.pc += 1; } diff --git a/rust/src/tvm/stdlib/prelude.rs b/rust/src/tvm/stdlib/prelude.rs index 7a663b49..fb907801 100644 --- a/rust/src/tvm/stdlib/prelude.rs +++ b/rust/src/tvm/stdlib/prelude.rs @@ -8,7 +8,7 @@ use std::io::{self, Write}; #[trc_function(var_params = true)] pub fn print(fmt_string: str) -> void { let mut iter = va_list.iter(); - let mut output_iter = fmt_string.chars(); + let mut output_iter = unsafe { (*fmt_string).chars() }; while let Some(i) = output_iter.next() { if i == '{' { unsafe { @@ -30,7 +30,7 @@ pub fn print(fmt_string: str) -> void { #[trc_function(var_params = true)] pub fn println(fmt_string: str) -> void { let mut iter = va_list.iter(); - let mut output_iter = fmt_string.chars(); + let mut output_iter = unsafe { (*fmt_string).chars() }; while let Some(i) = output_iter.next() { if i == '{' { unsafe { diff --git a/rust/src/tvm/types/trcstr.rs b/rust/src/tvm/types/trcstr.rs index 12aea76c..59704001 100644 --- a/rust/src/tvm/types/trcstr.rs +++ b/rust/src/tvm/types/trcstr.rs @@ -13,7 +13,7 @@ use std::fmt::Display; #[trc_class] #[derive(Debug, Clone)] pub struct TrcStr { - pub _value: String, + pub _value: *mut String, } fn cat_string(a: &String, b: &String) -> String { @@ -26,12 +26,12 @@ impl TrcObj for TrcStr { "str" } - batch_impl_opers! {} fn add(&self, other: *mut dyn TrcObj, gc: &mut GcMgr) -> RuntimeResult<*mut dyn TrcObj> { unsafe { match (*other).downcast_ref::() { Some(v) => { - return Ok(gc.alloc(TrcStr::new(cat_string(&self._value, &v._value)))); + let val = gc.alloc(cat_string(&*self._value, &*((*v)._value))); + return Ok(gc.alloc(TrcStr::new(val))); } None => { return Err(ErrorInfo::new( @@ -50,12 +50,12 @@ impl TrcObj for TrcStr { impl Display for TrcStr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self._value) + write!(f, "{}", unsafe { &*self._value }) } } impl TrcStr { - pub fn new(value: String) -> Self { + pub fn new(value: *mut String) -> Self { Self { _value: value } }