Skip to content

Commit

Permalink
fix(tvm):fix stack error
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Apr 12, 2024
1 parent 43d65e3 commit ea6a802
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 50 deletions.
58 changes: 34 additions & 24 deletions libcore/src/dynadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct DynaData {
// 变量已经使用的内存空间大小
var_used: usize,
#[cfg(debug_assertions)]
type_used: Vec<&'static str>,
size_used: Vec<usize>,
}

impl DynaData {
Expand Down Expand Up @@ -52,7 +52,7 @@ impl DynaData {
self.stack_ptr += size_of::<T>();
#[cfg(debug_assertions)]
{
self.type_used.push(std::any::type_name::<T>());
self.size_used.push(size_of::<T>());
}
}

Expand All @@ -65,15 +65,15 @@ impl DynaData {
let sz = size_of::<T>();
#[cfg(debug_assertions)]
{
let info = std::any::type_name::<T>();
let info_stack = self.type_used.pop().unwrap();
if info_stack != info {
panic!(
"pop data type error.Expected get {}.Actually has {}",
std::any::type_name::<T>(),
info_stack
);
}
let info = size_of::<T>();
let info_stack = self.size_used.pop().unwrap();
debug_assert_eq!(
info_stack,
info,
"pop data type error.Expected get {}.Actually has {}",
std::any::type_name::<T>(),
info_stack
);
debug_assert!(self.stack_ptr >= sz);
}
self.stack_ptr -= sz;
Expand All @@ -87,7 +87,8 @@ impl DynaData {
let ret = self.get_stack_addr_mut(self.stack_ptr);
#[cfg(debug_assertions)]
{
self.type_used.pop().unwrap();
let sz = self.size_used.pop().unwrap();
debug_assert_eq!(sz, n)
}
ret
}
Expand All @@ -101,15 +102,15 @@ impl DynaData {
let sz = size_of::<T>();
#[cfg(debug_assertions)]
{
let info = std::any::type_name::<T>();
let info_stack = self.type_used.last().unwrap();
if *info_stack != info {
panic!(
"pop data type error.Expected get {}.Actually has {}",
std::any::type_name::<T>(),
info_stack
);
}
let info = size_of::<T>();
let info_stack = self.size_used.last().unwrap();
debug_assert_eq!(
*info_stack,
info,
"pop data type error.Expected get {}.Actually has {}",
std::any::type_name::<T>(),
info_stack
);
debug_assert!(self.stack_ptr >= sz);
}
unsafe { *(self.get_stack_addr(self.stack_ptr - sz) as *const T) }
Expand All @@ -126,23 +127,32 @@ impl DynaData {
/// Sets the var of this [`DynaData`].
///
/// # Safety
/// make sure your addr is valid, or it will panic
/// make sure your addr is valid, or it will crash
/// .
pub unsafe fn set_var<T: 'static>(&mut self, addr: usize, data: T) {
unsafe {
(self.get_var_addr_mut(addr) as *mut T).write(data);
}
}

/// write n byte from src to data stack
///
/// # Safety
/// make sure your addr is valid, or it will crash
/// .
pub unsafe fn write_to_stack(&mut self, src: *mut Byte, n: usize) {
unsafe { self.get_stack_addr_mut(self.stack_ptr).copy_from(src, n) }
self.stack_ptr += n;
#[cfg(debug_assertions)]
{
self.size_used.push(n);
}
}

/// Sets the var of this [`DynaData`].
///
/// # Safety
/// make sure your addr is valid, or it will panic
/// make sure your addr is valid, or it will crash
/// .
pub unsafe fn get_var<T: Copy + 'static>(&self, addr: usize) -> T {
debug_assert!(addr < self.var_used);
Expand All @@ -152,7 +162,7 @@ impl DynaData {
/// Sets the var of this [`DynaData`].
///
/// # Safety
/// make sure your addr and src are valid, or it will panic
/// make sure your addr and src are valid, or it will crash
/// .
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) }
Expand Down
52 changes: 26 additions & 26 deletions tests/test_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ fn test_assign() {
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_double(Opcode::LoadGlobalVar, get_offset(0), intsz!()),
Inst::new_single(Opcode::MoveInt, NO_ARG),
Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(1)),
Inst::new_double(Opcode::LoadGlobalVar, get_offset(1), intsz!()),
Inst::new_single(Opcode::MoveInt, NO_ARG),
Inst::new_single(Opcode::LoadInt, 4),
Inst::new_single(Opcode::CallNative, fid),
Expand Down Expand Up @@ -217,7 +217,7 @@ print("{}", a+90)"#,
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_double(Opcode::LoadGlobalVar, get_offset(0), intsz!()),
Inst::new_single(Opcode::LoadInt, 2),
Inst::new_single(Opcode::AddInt, NO_ARG),
Inst::new_single(Opcode::MoveInt, NO_ARG),
Expand Down Expand Up @@ -325,12 +325,12 @@ if a<8{
vec![
Inst::new_single(Opcode::LoadInt, 2),
Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()),
Inst::new_single(Opcode::LoadGlobalVarInt, 0),
Inst::new_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
Inst::new_single(Opcode::LoadInt, 4),
Inst::new_single(Opcode::GtInt, 0),
Inst::new_single(Opcode::JumpIfFalse, 11),
Expand Down Expand Up @@ -395,14 +395,14 @@ fn test_for_1() {
vec![
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_double(Opcode::LoadGlobalVar, get_offset(0), intsz!()),
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_double(Opcode::LoadGlobalVar, get_offset(0), intsz!()),
Inst::new_single(Opcode::LoadInt, 1),
Inst::new_single(Opcode::AddInt, NO_ARG),
Inst::new_double(Opcode::StoreGlobal, get_offset(0), intsz!()),
Expand Down Expand Up @@ -495,19 +495,19 @@ print("{}{}", a, b)
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_double(Opcode::LoadGlobalVar, get_offset(0), intsz!()),
Inst::new_single(Opcode::MoveInt, NO_ARG),
Inst::new_single(Opcode::LoadGlobalVarInt, get_offset(1)),
Inst::new_double(Opcode::LoadGlobalVar, get_offset(1), intsz!()),
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_double(Opcode::LoadLocalVar, get_offset(1), intsz!()),
Inst::new_single(Opcode::MoveInt, NO_ARG),
Inst::new_single(Opcode::LoadLocalVarInt, get_offset(0)),
Inst::new_double(Opcode::LoadLocalVar, get_offset(0), intsz!()),
Inst::new_single(Opcode::MoveInt, NO_ARG),
Inst::new_single(Opcode::LoadInt, 2),
Inst::new_single(Opcode::CallNative, fid),
Expand Down Expand Up @@ -549,51 +549,51 @@ while a<10{
vec![
Inst::new_single(Opcode::LoadInt, 0),
Inst::new_double(Opcode::StoreGlobal, 0, intsz!()),
Inst::new_single(Opcode::LoadGlobalVarInt, 0),
Inst::new_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 8, intsz!()),
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_double(Opcode::LoadGlobalVar, 8, intsz!()),
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_double(Opcode::LoadGlobalVar, 8, intsz!()),
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_double(Opcode::LoadGlobalVar, 8, intsz!()),
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_double(Opcode::LoadGlobalVar, 8, intsz!()),
Inst::new_single(Opcode::LoadInt, 4),
Inst::new_single(Opcode::EqInt, 0),
Inst::new_single(Opcode::JumpIfFalse, 51),
Expand Down Expand Up @@ -657,13 +657,13 @@ println("out of range")
vec![
Inst::new_single(Opcode::LoadInt, 2),
Inst::new_double(Opcode::StoreGlobal, 0, intsz!()),
Inst::new_single(Opcode::LoadGlobalVarInt, 0),
Inst::new_double(Opcode::LoadGlobalVar, 0, intsz!()),
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
Inst::new_single(Opcode::MoveInt, 0),
Inst::new_single(Opcode::LoadInt, 1),
Inst::new_single(Opcode::CallNative, fid),
Expand All @@ -676,7 +676,7 @@ println("out of range")
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_double(Opcode::LoadGlobalVar, 0, intsz!()),
Inst::new_single(Opcode::MoveInt, 0),
Inst::new_single(Opcode::LoadInt, 1),
Inst::new_single(Opcode::CallNative, fid),
Expand Down Expand Up @@ -717,7 +717,7 @@ println("run final!")
vec![
Inst::new_single(Opcode::LoadString, 0),
Inst::new_double(Opcode::StoreGlobal, 0, strsz!()),
Inst::new_single(Opcode::LoadGlobalVarStr, 0),
Inst::new_double(Opcode::LoadGlobalVar, 0, strsz!()),
Inst::new_single(Opcode::LoadString, 0),
Inst::new_single(Opcode::EqStrWithoutPop, 0),
Inst::new_single(Opcode::JumpIfTrue, 10),
Expand All @@ -726,7 +726,7 @@ println("run final!")
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_double(Opcode::LoadGlobalVar, 0, strsz!()),
Inst::new_single(Opcode::MoveStr, 0),
Inst::new_single(Opcode::LoadInt, 1),
Inst::new_single(Opcode::CallNative, fid),
Expand Down

0 comments on commit ea6a802

Please sign in to comment.