This commit is contained in:
Li Haokun 2021-08-05 19:02:41 +08:00 committed by GitHub
parent 2b17f3d702
commit 40b690b67b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 37 deletions

View File

@ -20,6 +20,7 @@ void help_cmd()
<<" --code | view bytecode.\n"
<<" --exec | execute script file.\n"
<<" --time | execute and get the running time.\n"
<<" --opcnt | count operands while running.\n"
<<"file:\n"
<<" input file name to execute script file.\n";
return;
@ -101,12 +102,12 @@ void execute(std::string& file,std::string& command)
codegen.get_num_table(),
import.get_file()
);
if(command=="--exec")
vm.run(codegen.get_exec_code());
if(command=="--exec" || command=="--opcnt")
vm.run(codegen.get_exec_code(),command=="--opcnt");
else if(command=="--time")
{
clock_t begin=clock();
vm.run(codegen.get_exec_code());
vm.run(codegen.get_exec_code(),false);
std::cout<<"process exited after "<<((double)(clock()-begin))/CLOCKS_PER_SEC<<"s.\n";
}
vm.clear();
@ -127,9 +128,10 @@ int main(int argc,const char* argv[])
}
else if(argc==3 &&
(!strcmp(argv[1],"--lex") ||
!strcmp(argv[1],"--ast") ||
!strcmp(argv[1],"--ast") ||
!strcmp(argv[1],"--code") ||
!strcmp(argv[1],"--exec") ||
!strcmp(argv[1],"--opcnt")||
!strcmp(argv[1],"--time")))
{
file=argv[2];

View File

@ -1276,13 +1276,17 @@ void nasal_codegen::print_op(int index)
void nasal_codegen::print_byte_code()
{
if(num_res_table.size())
std::cout<<".number"<<std::endl;
for(auto& num:num_res_table)
std::cout<<".number "<<num<<'\n';
std::cout<<'\t'<<num<<'\n';
if(str_res_table.size())
std::cout<<".symbol"<<std::endl;
for(auto& str:str_res_table)
{
std::cout<<".symbol ";
std::cout<<'\t';
raw_string(str);
std::cout<<'\n';
std::cout<<std::endl;
}
for(int i=0;i<exec_code.size();++i)
print_op(i);

View File

@ -101,6 +101,7 @@ void nasal_vec::print()
std::cout<<"[]";
return;
}
ssize_t iter=0;
std::cout<<'[';
for(auto i:elems)
{
@ -113,7 +114,7 @@ void nasal_vec::print()
case vm_hash: i->ptr.hash->print(); break;
case vm_func: std::cout<<"func(..){..}"; break;
}
std::cout<<",]"[i==elems.back()];
std::cout<<",]"[(++iter)==elems.size()];
}
return;
}

View File

@ -108,7 +108,7 @@ public:
std::vector<double>&,
std::vector<std::string>&);
void clear();
void run(std::vector<opcode>&);
void run(std::vector<opcode>&,bool);
};
void nasal_vm::init(
@ -180,7 +180,7 @@ void nasal_vm::stackinfo(int limit)
}
if(same_cnt)
{
printf("\t%p %d same value(s) ...\n",last_ptr,same_cnt);
printf("\t%p ... | %d same value(s)\n",last_ptr,same_cnt);
same_cnt=0;
}
last_ptr=stack_top[-i];
@ -190,18 +190,18 @@ void nasal_vm::stackinfo(int limit)
else
switch(stack_top[-i]->type)
{
case vm_nil: printf("nil");break;
case vm_nil: printf("nil | gc.nil_addr");break;
case vm_num: printf("num | %lf",stack_top[-i]->ptr.num);break;
case vm_str: printf("str | ");raw_string(*stack_top[-i]->ptr.str);break;
case vm_func: printf("func | func(..){..}");break;
case vm_vec: printf("vec | ");stack_top[-i]->ptr.vec->print();break;
case vm_hash: printf("hash | ");stack_top[-i]->ptr.hash->print();break;
case vm_func: printf("func | func(%lu para){..}",stack_top[-i]->ptr.func->key_table.size());break;
case vm_vec: printf("vec | [%lu val]",stack_top[-i]->ptr.vec->elems.size());break;
case vm_hash: printf("hash | {%lu member}",stack_top[-i]->ptr.hash->elems.size());break;
default: printf("unknown");break;
}
putchar('\n');
}
if(same_cnt)
printf("\t%p %d same value(s) ...\n",last_ptr,same_cnt);
printf("\t%p ... | %d same value(s)\n",last_ptr,same_cnt);
return;
}
void nasal_vm::die(std::string str)
@ -221,7 +221,7 @@ void nasal_vm::stackoverflow()
stackinfo(10);
return;
}
bool nasal_vm::condition(nasal_val* val_addr)
inline bool nasal_vm::condition(nasal_val* val_addr)
{
if(val_addr->type==vm_num)
return val_addr->ptr.num;
@ -965,9 +965,9 @@ inline void nasal_vm::opr_ret()
stack_top[0]=stack_top[1];// rewrite nasal_func with returned value
return;
}
void nasal_vm::run(std::vector<opcode>& exec)
void nasal_vm::run(std::vector<opcode>& exec,bool op_cnt)
{
//uint64_t count[op_ret+1]={0};
uint64_t count[op_ret+1]={0};
void* opr_table[]=
{
&&nop, &&intg, &&intl, &&offset,
@ -1009,23 +1009,25 @@ nop:
if(canary && canary!=(nasal_val*)0xffff)
stackoverflow();
// debug
// for(int i=0;i<15;++i)
// {
// uint64_t maxnum=0,index=0;
// for(int j=0;j<op_ret+1;++j)
// if(count[j]>maxnum)
// {
// index=j;
// maxnum=count[j];
// }
// std::cout<<code_table[index].name<<": "<<maxnum<<"\n";
// count[index]=0;
// }
if(op_cnt)
{
std::cout<<std::endl;
for(int i=0;i<15;++i)
{
uint64_t maxnum=0,index=0;
for(int j=0;j<op_ret+1;++j)
if(count[j]>maxnum)
{
index=j;
maxnum=count[j];
}
std::cout<<code_table[index].name<<": "<<maxnum<<"\n";
count[index]=0;
}
}
return;
//#define exec_operand(op,num) {op();++count[num];if(!canary[0])goto *code[++pc];goto nop;}
#define exec_operand(op,num) {op();if(!canary)goto *code[++pc];goto nop;}
//#define exec_opnodie(op,num) {op();++count[num];goto *code[++pc];}
#define exec_opnodie(op,num) {op();goto *code[++pc];}
#define exec_operand(op,num) {op();++count[num];if(!canary)goto *code[++pc];goto nop;}
#define exec_opnodie(op,num) {op();++count[num];goto *code[++pc];}
intg: exec_opnodie(opr_intg ,op_intg );
intl: exec_opnodie(opr_intl ,op_intl );

View File

@ -63,14 +63,14 @@ props.Node=
foreach(var label;path)
tmp=tmp.val[label];
tmp.val=val;
if(typeof(val)=='string')
if(typeof(val)=='str')
{
if(val=='true' or val=='false')
tmp.type='BOOL';
else
tmp.type='STRING';
}
elsif(typeof(val)=='number')
elsif(typeof(val)=='num')
tmp.type='DOUBLE';
return;
},
@ -117,6 +117,11 @@ props.Node=
if(typeof(me.val)=='hash')
{
var key=keys(me.val);
if(!size(key))
{
println("{}");
return;
}
println('{');
foreach(var k;key)
{