diff --git a/main.cpp b/main.cpp index e442544..0e7a508 100644 --- a/main.cpp +++ b/main.cpp @@ -8,6 +8,7 @@ constexpr uint32_t VM_OPCALLNUM=16; constexpr uint32_t VM_EXEC =32; constexpr uint32_t VM_DBGINFO =64; constexpr uint32_t VM_DEBUG =128; +constexpr uint32_t VM_OPTIMIZE =256; void help() { @@ -24,13 +25,14 @@ void help() <<" input file name to execute script file.\n\n" <<"nasal [options...] \n" <<"option:\n" - <<" -l, --lex | view token info.\n" - <<" -a, --ast | view abstract syntax tree.\n" - <<" -c, --code | view bytecode.\n" - <<" -t, --time | execute and get the running time.\n" - <<" -o, --opcnt | execute and count used operands.\n" - <<" -d, --detail| execute and get detail crash info.\n" - <<" -dbg, --debug | debug mode (this will ignore -t -o -d).\n" + <<" -l, --lex | view token info.\n" + <<" -a, --ast | view abstract syntax tree.\n" + <<" -c, --code | view bytecode.\n" + <<" -t, --time | execute and get the running time.\n" + <<" -o, --opcnt | execute and count used operands.\n" + <<" -d, --detail | execute and get detail crash info.\n" + <<" -op, --optimize| use optimizer(beta).\n" + <<" -dbg, --debug | debug mode (this will ignore -t -o -d).\n" <<"file:\n" <<" input file name to execute script file.\n"; } @@ -82,8 +84,11 @@ void execute(const std::string& file,const uint32_t cmd) if(cmd&VM_ASTINFO) parse.print(); + // optimizer does simple optimization on ast + if(cmd&VM_OPTIMIZE) + optimize(parse.ast()); + // code generator gets parser's ast and linker's import file list to generate code - optimize(parse.ast()); gen.compile(parse,linker); if(cmd&VM_CODEINFO) gen.print(); @@ -140,6 +145,8 @@ int main(int argc,const char* argv[]) cmd|=VM_EXECTIME; else if(s=="--detail" || s=="-d") cmd|=VM_DBGINFO|VM_EXEC; + else if(s=="--optimize" || s=="-op") + cmd|=VM_OPTIMIZE; else if(s=="--debug" || s=="-dbg") cmd|=VM_DEBUG; else diff --git a/nasal_codegen.h b/nasal_codegen.h index af59796..4e55786 100644 --- a/nasal_codegen.h +++ b/nasal_codegen.h @@ -10,8 +10,6 @@ enum op_code op_loadl, // load local value op_loadu, // load upvalue op_pnum, // push constant number to the stack - op_pone, // push 1 to the stack - op_pzero, // push 0 to the stack op_pnil, // push constant nil to the stack op_pstr, // push constant string to the stack op_newv, // push new vector with initial values from stack @@ -96,8 +94,6 @@ struct {op_loadl, "loadl "}, {op_loadu, "loadu "}, {op_pnum, "pnum "}, - {op_pone, "pone "}, - {op_pzero, "pzero "}, {op_pnil, "pnil "}, {op_pstr, "pstr "}, {op_newv, "newv "}, @@ -363,13 +359,8 @@ void nasal_codegen::gen(uint8_t op,uint32_t num,uint32_t line) void nasal_codegen::num_gen(const nasal_ast& ast) { double num=ast.num(); - if(num==0)gen(op_pzero,0,ast.line()); - else if(num==1)gen(op_pone,0,ast.line()); - else - { - regist_number(num); - gen(op_pnum,num_table[num],ast.line()); - } + regist_number(num); + gen(op_pnum,num_table[num],ast.line()); } void nasal_codegen::str_gen(const nasal_ast& ast) @@ -816,7 +807,7 @@ void nasal_codegen::for_gen(const nasal_ast& ast) } int jmp_place=code.size(); if(ast[1].type()==ast_null) - gen(op_pone,0,ast[1].line()); + gen(op_pnum,num_table[1],ast[1].line()); else calc_gen(ast[1]); int label_exit=code.size(); @@ -1150,6 +1141,9 @@ void nasal_codegen::compile(const nasal_parse& parse,const nasal_import& import) file=import.get_file().data(); in_iterloop.push(0); + regist_number(0); + regist_number(1); + find_symbol(parse.ast()); // search symbols first gen(op_intg,global.size(),0); block_gen(parse.ast()); // generate main block diff --git a/nasal_dbg.h b/nasal_dbg.h index 031121a..2b72da3 100644 --- a/nasal_dbg.h +++ b/nasal_dbg.h @@ -184,25 +184,24 @@ void nasal_dbg::run( const void* opr_table[]= { &&nop, &&intg, &&intl, &&loadg, - &&loadl, &&loadu, &&pnum, &&pone, - &&pzero, &&pnil, &&pstr, &&newv, - &&newh, &&newf, &&happ, &¶, - &&defpara, &&dynpara, &&unot, &&usub, - &&add, &&sub, &&mul, &&div, - &&lnk, &&addc, &&subc, &&mulc, - &&divc, &&lnkc, &&addeq, &&subeq, - &&muleq, &&diveq, &&lnkeq, &&addeqc, - &&subeqc, &&muleqc, &&diveqc, &&lnkeqc, - &&meq, &&eq, &&neq, &&less, - &&leq, &&grt, &&geq, &&lessc, - &&leqc, &&grtc, &&geqc, &&pop, - &&jmp, &&jt, &&jf, &&counter, - &&findex, &&feach, &&callg, &&calll, - &&upval, &&callv, &&callvi, &&callh, - &&callfv, &&callfh, &&callb, &&slcbegin, - &&slcend, &&slc, &&slc2, &&mcallg, - &&mcalll, &&mupval, &&mcallv, &&mcallh, - &&ret, &&vmexit + &&loadl, &&loadu, &&pnum, &&pnil, + &&pstr, &&newv, &&newh, &&newf, + &&happ, &¶, &&defpara,&&dynpara, + &&unot, &&usub, &&add, &&sub, + &&mul, &&div, &&lnk, &&addc, + &&subc, &&mulc, &&divc, &&lnkc, + &&addeq, &&subeq, &&muleq, &&diveq, + &&lnkeq, &&addeqc, &&subeqc, &&muleqc, + &&diveqc, &&lnkeqc, &&meq, &&eq, + &&neq, &&less, &&leq, &&grt, + &&geq, &&lessc, &&leqc, &&grtc, + &&geqc, &&pop, &&jmp, &&jt, + &&jf, &&counter, &&findex, &&feach, + &&callg, &&calll, &&upval, &&callv, + &&callvi, &&callh, &&callfv, &&callfh, + &&callb, &&slcbegin, &&slcend, &&slc, + &&slc2, &&mcallg, &&mcalll, &&mupval, + &&mcallv, &&mcallh, &&ret, &&vmexit }; bytecode=gen.get_code().data(); std::vector code; @@ -234,8 +233,6 @@ loadg: dbg(opr_loadg ); loadl: dbg(opr_loadl ); loadu: dbg(opr_loadu ); pnum: dbg(opr_pnum ); -pone: dbg(opr_pone ); -pzero: dbg(opr_pzero ); pnil: dbg(opr_pnil ); pstr: dbg(opr_pstr ); newv: dbg(opr_newv ); diff --git a/nasal_vm.h b/nasal_vm.h index 3d9bff9..e676f54 100644 --- a/nasal_vm.h +++ b/nasal_vm.h @@ -44,8 +44,6 @@ protected: void opr_loadl(); void opr_loadu(); void opr_pnum(); - void opr_pone(); - void opr_pzero(); void opr_pnil(); void opr_pstr(); void opr_newv(); @@ -343,14 +341,6 @@ inline void nasal_vm::opr_pnum() { (++gc.top)[0]={vm_num,num_table[imm[pc]]}; } -inline void nasal_vm::opr_pone() -{ - (++gc.top)[0]={vm_num,(double)1}; -} -inline void nasal_vm::opr_pzero() -{ - (++gc.top)[0]={vm_num,(double)0}; -} inline void nasal_vm::opr_pnil() { (++gc.top)[0]={vm_nil,(double)0}; @@ -858,25 +848,24 @@ void nasal_vm::run( const void* opr_table[]= { &&nop, &&intg, &&intl, &&loadg, - &&loadl, &&loadu, &&pnum, &&pone, - &&pzero, &&pnil, &&pstr, &&newv, - &&newh, &&newf, &&happ, &¶, - &&defpara, &&dynpara, &&unot, &&usub, - &&add, &&sub, &&mul, &&div, - &&lnk, &&addc, &&subc, &&mulc, - &&divc, &&lnkc, &&addeq, &&subeq, - &&muleq, &&diveq, &&lnkeq, &&addeqc, - &&subeqc, &&muleqc, &&diveqc, &&lnkeqc, - &&meq, &&eq, &&neq, &&less, - &&leq, &&grt, &&geq, &&lessc, - &&leqc, &&grtc, &&geqc, &&pop, - &&jmp, &&jt, &&jf, &&counter, - &&findex, &&feach, &&callg, &&calll, - &&upval, &&callv, &&callvi, &&callh, - &&callfv, &&callfh, &&callb, &&slcbegin, - &&slcend, &&slc, &&slc2, &&mcallg, - &&mcalll, &&mupval, &&mcallv, &&mcallh, - &&ret, &&vmexit + &&loadl, &&loadu, &&pnum, &&pnil, + &&pstr, &&newv, &&newh, &&newf, + &&happ, &¶, &&defpara,&&dynpara, + &&unot, &&usub, &&add, &&sub, + &&mul, &&div, &&lnk, &&addc, + &&subc, &&mulc, &&divc, &&lnkc, + &&addeq, &&subeq, &&muleq, &&diveq, + &&lnkeq, &&addeqc, &&subeqc, &&muleqc, + &&diveqc, &&lnkeqc, &&meq, &&eq, + &&neq, &&less, &&leq, &&grt, + &&geq, &&lessc, &&leqc, &&grtc, + &&geqc, &&pop, &&jmp, &&jt, + &&jf, &&counter, &&findex, &&feach, + &&callg, &&calll, &&upval, &&callv, + &&callvi, &&callh, &&callfv, &&callfh, + &&callb, &&slcbegin, &&slcend, &&slc, + &&slc2, &&mcallg, &&mcalll, &&mupval, + &&mcallv, &&mcallh, &&ret, &&vmexit }; bytecode=gen.get_code().data(); std::vector code; @@ -912,8 +901,6 @@ loadg: exec_opnodie(opr_loadg ,op_loadg ); // -1 loadl: exec_opnodie(opr_loadl ,op_loadl ); // -1 loadu: exec_opnodie(opr_loadu ,op_loadu ); // -1 pnum: exec_operand(opr_pnum ,op_pnum ); // +1 -pone: exec_operand(opr_pone ,op_pone ); // +1 -pzero: exec_operand(opr_pzero ,op_pzero ); // +1 pnil: exec_operand(opr_pnil ,op_pnil ); // +1 pstr: exec_operand(opr_pstr ,op_pstr ); // +1 newv: exec_operand(opr_newv ,op_newv ); // +1-imm[pc]