Skip to content

Commit

Permalink
preparing for var load and store
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Mar 1, 2024
1 parent 88ef2e7 commit 7e22a1a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 20 deletions.
11 changes: 11 additions & 0 deletions rust/src/base/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,24 @@ pub enum Opcode {
LoadChar,
LoadBool,
LoadLocal,
LoadVarBool,
LoadVarInt,
LoadVarFloat,
LoadVarStr,
LoadVarChar,
// Move a value into the stack
MoveInt,
MoveFloat,
MoveChar,
MoveBool,
MoveStr,
// Store a local var
StoreLocal,
StoreInt,
StoreFloat,
StoreChar,
StoreBool,
StoreStr,
// Do Nothing
Empty,
// a = -a
Expand Down
6 changes: 4 additions & 2 deletions rust/src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
75 changes: 64 additions & 11 deletions rust/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ pub struct DynaData<'a> {
gc: GcMgr,
obj_stack: Vec<*mut dyn TrcObj>,
int_stack: Vec<i64>,
str_stack: Vec<String>,
str_stack: Vec<*mut String>,
float_stack: Vec<f64>,
bool_stack: Vec<bool>,
char_stack: Vec<char>,
frames_stack: Vec<function::Frame<'a>>,
var_store: Vec<*mut dyn TrcObj>,
int_store: Vec<i64>,
float_store: Vec<f64>,
str_store: Vec<*mut String>,
bool_store: Vec<bool>,
char_store: Vec<char>,
}

impl<'a> DynaData<'a> {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/tvm/stdlib/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions rust/src/tvm/types/trcstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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::<TrcStr>() {
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(
Expand All @@ -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 }
}

Expand Down

0 comments on commit 7e22a1a

Please sign in to comment.