This commit is contained in:
Li Haokun 2021-07-24 19:59:56 +08:00 committed by GitHub
parent 9ebabfe737
commit 816be43a98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 20 deletions

View File

@ -19,7 +19,9 @@ private:
std::vector<opcode> bytecode; // bytecode std::vector<opcode> bytecode; // bytecode
std::vector<std::string> files; // files std::vector<std::string> files; // files
void stackinfo(int);
void die(std::string); void die(std::string);
void stackoverflow();
bool condition(nasal_val*); bool condition(nasal_val*);
void opr_intg(); void opr_intg();
void opr_intl(); void opr_intl();
@ -130,6 +132,27 @@ void nasal_vm::clear()
imm.clear(); imm.clear();
return; return;
} }
void nasal_vm::stackinfo(int limit)
{
printf("vm stack(limit %d):\n",limit);
for(int i=0;i<limit && stack_top-i>=gc.val_stack;++i)
{
if(!stack_top[-i])
printf("\t%p nullptr\n",stack_top[-i]);
else
switch(stack_top[-i]->type)
{
case vm_nil: printf("\t%p nil\n",stack_top[-i]);break;
case vm_num: printf("\t%p num :%lf\n",stack_top[-i],stack_top[-i]->ptr.num);break;
case vm_str: printf("\t%p str :",stack_top[-i]->ptr.str);raw_string(*stack_top[-i]->ptr.str);putchar('\n');break;
case vm_func: printf("\t%p func\n",stack_top[-i]->ptr.func);break;
case vm_vec: printf("\t%p vec \n",stack_top[-i]->ptr.vec); break;
case vm_hash: printf("\t%p hash\n",stack_top[-i]->ptr.hash);break;
default: printf("\t%p unknown\n",stack_top[-i]);break;
}
}
return;
}
void nasal_vm::die(std::string str) void nasal_vm::die(std::string str)
{ {
printf(">> [vm] error at 0x%.8x: %s\ntrace back:\n",pc,str.c_str()); printf(">> [vm] error at 0x%.8x: %s\ntrace back:\n",pc,str.c_str());
@ -148,26 +171,27 @@ void nasal_vm::die(std::string str)
files[bytecode[point].fidx].c_str(), files[bytecode[point].fidx].c_str(),
bytecode[point].line); bytecode[point].line);
} }
printf("vm stack(limit 10):\n"); stackinfo(10);
for(int i=0;i<10 && stack_top-i>=gc.val_stack;++i)
{
if(!stack_top[-i])
printf("\t%p nullptr\n",stack_top[-i]);
else
switch(stack_top[-i]->type)
{
case vm_nil: printf("\t%p nil\n",stack_top[-i]);break;
case vm_num: printf("\t%p num :%lf\n",stack_top[-i],stack_top[-i]->ptr.num);break;
case vm_str: printf("\t%p str :",stack_top[-i]->ptr.str);raw_string(*stack_top[-i]->ptr.str);putchar('\n');break;
case vm_func: printf("\t%p func\n",stack_top[-i]->ptr.func);break;
case vm_vec: printf("\t%p vec \n",stack_top[-i]->ptr.vec); break;
case vm_hash: printf("\t%p hash\n",stack_top[-i]->ptr.hash);break;
default: printf("\t%p unknown\n",stack_top[-i]);break;
}
}
gc.val_stack[STACK_MAX_DEPTH-1]=(nasal_val*)0xffff; gc.val_stack[STACK_MAX_DEPTH-1]=(nasal_val*)0xffff;
return; return;
} }
void nasal_vm::stackoverflow()
{
printf(">> [vm] stack overflow\nlast called(limit 10):\n");
for(int i=0;i<10 && !ret.empty();++i,ret.pop())
{
uint32_t point=ret.top();
printf(
"\t0x%.8x: %s 0x%.8x (%s line %d)\n",
point,
code_table[bytecode[point].op].name,
bytecode[point].num,
files[bytecode[point].fidx].c_str(),
bytecode[point].line);
}
stackinfo(10);
return;
}
bool nasal_vm::condition(nasal_val* val_addr) bool nasal_vm::condition(nasal_val* val_addr)
{ {
if(val_addr->type==vm_num) if(val_addr->type==vm_num)
@ -947,14 +971,14 @@ void nasal_vm::run(std::vector<opcode>& exec)
} }
auto& canary=gc.val_stack[STACK_MAX_DEPTH-1]; auto& canary=gc.val_stack[STACK_MAX_DEPTH-1];
clock_t begin=clock(); // clock_t begin=clock();
pc=0; pc=0;
goto *code[pc]; goto *code[pc];
nop: nop:
if(canary && canary!=(nasal_val*)0xffff) if(canary && canary!=(nasal_val*)0xffff)
die("stack overflow"); stackoverflow();
std::cout<<">> [vm] process exited after "<<((double)(clock()-begin))/CLOCKS_PER_SEC<<"s.\n"; // std::cout<<">> [vm] process exited after "<<((double)(clock()-begin))/CLOCKS_PER_SEC<<"s.\n";
// debug // debug
// for(int i=0;i<15;++i) // for(int i=0;i<15;++i)
// { // {