update
This commit is contained in:
parent
d0206abb8d
commit
14852bfc2e
130
nasal_codegen.h
130
nasal_codegen.h
|
@ -18,16 +18,12 @@ enum op_code
|
|||
op_addeq,op_subeq,op_muleq,op_diveq,op_lnkeq,op_meq,
|
||||
op_eq,op_neq,op_less,op_leq,op_grt,op_geq,
|
||||
op_pop,
|
||||
op_newscope,
|
||||
op_delscope,
|
||||
op_jmp,
|
||||
op_jmptrue,
|
||||
op_jmpfalse,
|
||||
op_jp,
|
||||
op_jtp,
|
||||
op_jfp,
|
||||
op_continue,
|
||||
op_break,
|
||||
op_forindex, // index counter on the top of forindex_stack plus 1
|
||||
op_foreach, // index counter on the top of forindex_stack plus 1 and get the value in vector
|
||||
op_call, // call identifier
|
||||
|
@ -88,16 +84,12 @@ struct
|
|||
{op_grt, "g "},
|
||||
{op_geq, "geq "},
|
||||
{op_pop, "pop "},
|
||||
{op_newscope, "nscp "},
|
||||
{op_delscope, "dscp "},
|
||||
{op_jmp, "jmp "},
|
||||
{op_jmptrue, "jt "},
|
||||
{op_jmpfalse, "jf "},
|
||||
{op_jp, "jp "},
|
||||
{op_jtp, "jtp "},
|
||||
{op_jfp, "jfp "},
|
||||
{op_continue, "contn "},
|
||||
{op_break, "break "},
|
||||
{op_forindex, "findx "},
|
||||
{op_foreach, "feach "},
|
||||
{op_call, "call "},
|
||||
|
@ -120,8 +112,15 @@ struct
|
|||
struct opcode
|
||||
{
|
||||
unsigned char op;
|
||||
unsigned int scope;
|
||||
unsigned int index;
|
||||
opcode(){op=op_nop;index=0;}
|
||||
opcode()
|
||||
{
|
||||
op=op_nop;
|
||||
scope=0;
|
||||
index=0;
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// unfinished
|
||||
|
@ -137,6 +136,7 @@ private:
|
|||
std::vector<opcode> exec_code;
|
||||
std::vector<int> continue_ptr;
|
||||
std::vector<int> break_ptr;
|
||||
int scope_depth;
|
||||
int error;
|
||||
void regist_number(double);
|
||||
void regist_string(std::string);
|
||||
|
@ -206,6 +206,7 @@ void nasal_codegen::pop_gen()
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_pop;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -215,6 +216,7 @@ void nasal_codegen::nil_gen()
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_pushnil;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
|
@ -233,6 +235,7 @@ void nasal_codegen::number_gen(nasal_ast& ast)
|
|||
op.op=op_pushnum;
|
||||
op.index=number_table[num];
|
||||
}
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
|
@ -243,6 +246,7 @@ void nasal_codegen::string_gen(nasal_ast& ast)
|
|||
regist_string(str);
|
||||
opcode op;
|
||||
op.op=op_pushstr;
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -253,6 +257,7 @@ void nasal_codegen::vector_gen(nasal_ast& ast)
|
|||
int size=ast.get_children().size();
|
||||
opcode op;
|
||||
op.op=op_newvec;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
for(int i=0;i<size;++i)
|
||||
|
@ -271,6 +276,7 @@ void nasal_codegen::hash_gen(nasal_ast& ast)
|
|||
int size=ast.get_children().size();
|
||||
opcode op;
|
||||
op.op=op_newhash;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
for(int i=0;i<size;++i)
|
||||
|
@ -278,6 +284,7 @@ void nasal_codegen::hash_gen(nasal_ast& ast)
|
|||
string_gen(ast.get_children()[i].get_children()[0]);
|
||||
calculation_gen(ast.get_children()[i].get_children()[1]);
|
||||
op.op=op_hashapp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -288,6 +295,7 @@ void nasal_codegen::function_gen(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newfunc;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
|
||||
|
@ -327,9 +335,11 @@ void nasal_codegen::function_gen(nasal_ast& ast)
|
|||
}
|
||||
|
||||
op.op=op_entry;
|
||||
op.scope=scope_depth;
|
||||
op.index=exec_code.size()+2;
|
||||
exec_code.push_back(op);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -339,9 +349,11 @@ void nasal_codegen::function_gen(nasal_ast& ast)
|
|||
if(!block.get_children().size() || block.get_children().back().get_type()!=ast_return)
|
||||
{
|
||||
op.op=op_pushnil;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
op.op=op_return;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -381,6 +393,7 @@ void nasal_codegen::call_id(nasal_ast& ast)
|
|||
break;
|
||||
}
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -392,6 +405,7 @@ void nasal_codegen::call_hash(nasal_ast& ast)
|
|||
op.op=op_callh;
|
||||
std::string str=ast.get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -404,12 +418,14 @@ void nasal_codegen::call_vec(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[0]);
|
||||
opcode op;
|
||||
op.op=op_callv;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
opcode op;
|
||||
op.op=op_slicebegin;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
int size=ast.get_children().size();
|
||||
|
@ -420,6 +436,7 @@ void nasal_codegen::call_vec(nasal_ast& ast)
|
|||
{
|
||||
calculation_gen(tmp);
|
||||
op.op=op_slice;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -428,11 +445,13 @@ void nasal_codegen::call_vec(nasal_ast& ast)
|
|||
calculation_gen(tmp.get_children()[0]);
|
||||
calculation_gen(tmp.get_children()[1]);
|
||||
op.op=op_slice2;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
}
|
||||
op.op=op_sliceend;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -444,10 +463,12 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newvec;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
|
||||
op.op=op_callf;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -456,6 +477,7 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newhash;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
int size=ast.get_children().size();
|
||||
|
@ -465,10 +487,12 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
string_gen(tmp.get_children()[0]);
|
||||
calculation_gen(tmp.get_children()[1]);
|
||||
op.op=op_hashapp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
op.op=op_callf;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -476,6 +500,7 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_newvec;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
int size=ast.get_children().size();
|
||||
|
@ -484,10 +509,12 @@ void nasal_codegen::call_func(nasal_ast& ast)
|
|||
nasal_ast& tmp=ast.get_children()[i];
|
||||
calculation_gen(tmp);
|
||||
op.op=op_vecapp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
op.op=op_callf;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -518,6 +545,7 @@ void nasal_codegen::mem_call_id(nasal_ast& ast)
|
|||
regist_string(str);
|
||||
opcode op;
|
||||
op.op=op_mcall;
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -528,6 +556,7 @@ void nasal_codegen::mem_call_vec(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[0]);
|
||||
opcode op;
|
||||
op.op=op_mcallv;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -539,6 +568,7 @@ void nasal_codegen::mem_call_hash(nasal_ast& ast)
|
|||
regist_string(str);
|
||||
opcode op;
|
||||
op.op=op_mcallh;
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -551,6 +581,7 @@ void nasal_codegen::single_def(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
|
@ -567,6 +598,7 @@ void nasal_codegen::multi_def(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[i].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -578,11 +610,13 @@ void nasal_codegen::multi_def(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_callvi;
|
||||
op.scope=scope_depth;
|
||||
op.index=i;
|
||||
exec_code.push_back(op);
|
||||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[i].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -611,6 +645,7 @@ void nasal_codegen::multi_assignment_gen(nasal_ast& ast)
|
|||
mem_call(ast.get_children()[0].get_children()[i]);
|
||||
opcode op;
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
}
|
||||
|
@ -622,10 +657,12 @@ void nasal_codegen::multi_assignment_gen(nasal_ast& ast)
|
|||
{
|
||||
opcode op;
|
||||
op.op=op_callvi;
|
||||
op.scope=scope_depth;
|
||||
op.index=i;
|
||||
exec_code.push_back(op);
|
||||
mem_call(ast.get_children()[0].get_children()[i]);
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
}
|
||||
|
@ -645,11 +682,13 @@ void nasal_codegen::conditional_gen(nasal_ast& ast)
|
|||
{
|
||||
calculation_gen(tmp.get_children()[0]);
|
||||
op.op=op_jfp;
|
||||
op.scope=scope_depth;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
block_gen(tmp.get_children()[1]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
jmp_label.push_back(exec_code.size());
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -694,11 +733,13 @@ void nasal_codegen::while_gen(nasal_ast& ast)
|
|||
int loop_ptr=exec_code.size();
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jfp;
|
||||
op.scope=scope_depth;
|
||||
int condition_ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
block_gen(ast.get_children()[1]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=loop_ptr;
|
||||
exec_code.push_back(op);
|
||||
exec_code[condition_ptr].index=exec_code.size();
|
||||
|
@ -709,6 +750,7 @@ void nasal_codegen::while_gen(nasal_ast& ast)
|
|||
void nasal_codegen::for_gen(nasal_ast& ast)
|
||||
{
|
||||
opcode op;
|
||||
++scope_depth;
|
||||
switch(ast.get_children()[0].get_type())
|
||||
{
|
||||
case ast_null:break;
|
||||
|
@ -727,12 +769,14 @@ void nasal_codegen::for_gen(nasal_ast& ast)
|
|||
if(ast.get_children()[1].get_type()==ast_null)
|
||||
{
|
||||
op.op=op_pushone;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
else
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jfp;
|
||||
op.scope=scope_depth;
|
||||
int label_exit=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
|
@ -752,10 +796,12 @@ void nasal_codegen::for_gen(nasal_ast& ast)
|
|||
case ast_trinocular:calculation_gen(ast.get_children()[2]);pop_gen();break;
|
||||
}
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=jmp_place;
|
||||
exec_code.push_back(op);
|
||||
exec_code[label_exit].index=exec_code.size();
|
||||
load_continue_break();
|
||||
--scope_depth;
|
||||
return;
|
||||
}
|
||||
void nasal_codegen::forindex_gen(nasal_ast& ast)
|
||||
|
@ -764,6 +810,7 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[1]);
|
||||
|
||||
op.op=op_forindex;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -772,6 +819,7 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[0].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -779,11 +827,13 @@ void nasal_codegen::forindex_gen(nasal_ast& ast)
|
|||
{
|
||||
mem_call(ast.get_children()[0]);
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
block_gen(ast.get_children()[2]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=ptr;
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -796,6 +846,7 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[1]);
|
||||
|
||||
op.op=op_foreach;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -804,6 +855,7 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
|
|||
op.op=op_load;
|
||||
std::string str=ast.get_children()[0].get_children()[0].get_str();
|
||||
regist_string(str);
|
||||
op.scope=scope_depth;
|
||||
op.index=string_table[str];
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
|
@ -811,11 +863,13 @@ void nasal_codegen::foreach_gen(nasal_ast& ast)
|
|||
{
|
||||
mem_call(ast.get_children()[0]);
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
}
|
||||
block_gen(ast.get_children()[2]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=ptr;
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -836,6 +890,7 @@ void nasal_codegen::or_gen(nasal_ast& ast)
|
|||
int l1,l2;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
l1=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -843,6 +898,7 @@ void nasal_codegen::or_gen(nasal_ast& ast)
|
|||
pop_gen();
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
l2=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
@ -868,16 +924,19 @@ void nasal_codegen::and_gen(nasal_ast& ast)
|
|||
opcode op;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=exec_code.size()+2;
|
||||
exec_code.push_back(op);
|
||||
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
|
||||
pop_gen();
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jmptrue;
|
||||
op.scope=scope_depth;
|
||||
op.index=exec_code.size()+3;
|
||||
exec_code.push_back(op);
|
||||
|
||||
|
@ -893,11 +952,13 @@ void nasal_codegen::trino_gen(nasal_ast& ast)
|
|||
opcode op;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
op.op=op_jmpfalse;
|
||||
op.scope=scope_depth;
|
||||
int ptr=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
pop_gen();
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
int ptr_exit=exec_code.size();
|
||||
exec_code.push_back(op);
|
||||
exec_code[ptr].index=exec_code.size();
|
||||
|
@ -923,36 +984,42 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
case ast_call:call_gen(ast);break;
|
||||
case ast_equal:
|
||||
op.op=op_meq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_add_equal:
|
||||
op.op=op_addeq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_sub_equal:
|
||||
op.op=op_subeq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_mult_equal:
|
||||
op.op=op_muleq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_div_equal:
|
||||
op.op=op_diveq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_link_equal:
|
||||
op.op=op_lnkeq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
mem_call(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
|
@ -961,66 +1028,77 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
case ast_and:and_gen(ast);break;
|
||||
case ast_add:
|
||||
op.op=op_add;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_sub:
|
||||
op.op=op_sub;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_mult:
|
||||
op.op=op_mul;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_div:
|
||||
op.op=op_div;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_link:
|
||||
op.op=op_lnk;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_cmp_equal:
|
||||
op.op=op_eq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_cmp_not_equal:
|
||||
op.op=op_neq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_less_equal:
|
||||
op.op=op_leq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_less_than:
|
||||
op.op=op_less;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_greater_equal:
|
||||
op.op=op_geq;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_greater_than:
|
||||
op.op=op_grt;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
calculation_gen(ast.get_children()[1]);
|
||||
exec_code.push_back(op);
|
||||
|
@ -1028,11 +1106,13 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
case ast_trinocular:trino_gen(ast);break;
|
||||
case ast_unary_sub:
|
||||
op.op=op_usub;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_unary_not:
|
||||
op.op=op_unot;
|
||||
op.scope=scope_depth;
|
||||
calculation_gen(ast.get_children()[0]);
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
|
@ -1043,9 +1123,7 @@ void nasal_codegen::calculation_gen(nasal_ast& ast)
|
|||
void nasal_codegen::block_gen(nasal_ast& ast)
|
||||
{
|
||||
opcode op;
|
||||
op.op=op_newscope;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
++scope_depth;
|
||||
|
||||
int size=ast.get_children().size();
|
||||
for(int i=0;i<size;++i)
|
||||
|
@ -1062,13 +1140,15 @@ void nasal_codegen::block_gen(nasal_ast& ast)
|
|||
case ast_multi_assign:multi_assignment_gen(tmp);break;
|
||||
case ast_conditional:conditional_gen(tmp);break;
|
||||
case ast_continue:
|
||||
op.op=op_continue;
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
continue_ptr.push_back(exec_code.size());
|
||||
exec_code.push_back(op);
|
||||
break;
|
||||
case ast_break:
|
||||
op.op=op_break;
|
||||
op.op=op_jmp;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
break_ptr.push_back(exec_code.size());
|
||||
exec_code.push_back(op);
|
||||
|
@ -1107,9 +1187,7 @@ void nasal_codegen::block_gen(nasal_ast& ast)
|
|||
}
|
||||
}
|
||||
|
||||
op.op=op_delscope;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
--scope_depth;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -1119,6 +1197,7 @@ void nasal_codegen::return_gen(nasal_ast& ast)
|
|||
calculation_gen(ast.get_children()[0]);
|
||||
opcode op;
|
||||
op.op=op_return;
|
||||
op.scope=scope_depth;
|
||||
exec_code.push_back(op);
|
||||
return;
|
||||
}
|
||||
|
@ -1126,6 +1205,7 @@ void nasal_codegen::return_gen(nasal_ast& ast)
|
|||
void nasal_codegen::main_progress(nasal_ast& ast)
|
||||
{
|
||||
error=0;
|
||||
scope_depth=0;
|
||||
number_table.clear();
|
||||
string_table.clear();
|
||||
exec_code.clear();
|
||||
|
@ -1178,6 +1258,7 @@ void nasal_codegen::main_progress(nasal_ast& ast)
|
|||
}
|
||||
opcode op;
|
||||
op.op=op_nop;
|
||||
op.scope=scope_depth;
|
||||
op.index=0;
|
||||
exec_code.push_back(op);
|
||||
|
||||
|
@ -1201,6 +1282,15 @@ void nasal_codegen::print_op(int index)
|
|||
num>>=4;
|
||||
}
|
||||
std::cout<<"0x"<<numinfo<<": ";
|
||||
numinfo="";
|
||||
num=exec_code[index].scope;
|
||||
for(int i=0;i<8;++i)
|
||||
{
|
||||
int tmp=num&0x0f;
|
||||
numinfo=(char)(tmp>9? 'a'+tmp-10:'0'+tmp)+numinfo;
|
||||
num>>=4;
|
||||
}
|
||||
std::cout<<"[0x"<<numinfo<<"]";
|
||||
for(int i=0;code_table[i].name;++i)
|
||||
if(exec_code[index].op==code_table[i].type)
|
||||
{
|
||||
|
@ -1215,7 +1305,7 @@ void nasal_codegen::print_op(int index)
|
|||
numinfo=(char)(tmp>9? 'a'+tmp-10:'0'+tmp)+numinfo;
|
||||
num>>=4;
|
||||
}
|
||||
std::cout<<"0x"<<numinfo;
|
||||
std::cout<<"0x"<<numinfo<<" ";
|
||||
switch(exec_code[index].op)
|
||||
{
|
||||
case op_pushnum:std::cout<<'('<<number_result_table[exec_code[index].index]<<')';break;
|
||||
|
@ -1236,6 +1326,10 @@ void nasal_codegen::print_op(int index)
|
|||
|
||||
void nasal_codegen::print_byte_code()
|
||||
{
|
||||
for(int i=0;i<number_result_table.size();++i)
|
||||
std::cout<<".number "<<number_result_table[i]<<'\n';
|
||||
for(int i=0;i<string_result_table.size();++i)
|
||||
std::cout<<".symbol "<<string_result_table[i]<<'\n';
|
||||
int size=exec_code.size();
|
||||
for(int i=0;i<size;++i)
|
||||
print_op(i);
|
||||
|
|
63
nasal_gc.h
63
nasal_gc.h
|
@ -87,13 +87,11 @@ private:
|
|||
// and this memory_manager_memory space stores an address to garbage_collector_memory
|
||||
// and this address points to an nasal_hash
|
||||
nasal_virtual_machine& vm;
|
||||
std::list<std::map<std::string,int> > elems;
|
||||
std::map<std::string,int> elems;
|
||||
public:
|
||||
nasal_closure(nasal_virtual_machine&);
|
||||
~nasal_closure();
|
||||
void set_vm(nasal_virtual_machine&);
|
||||
void add_scope();
|
||||
void del_scope();
|
||||
void add_new_value(std::string,int);
|
||||
int get_value_address(std::string);
|
||||
int get_mem_address(std::string);
|
||||
|
@ -448,75 +446,48 @@ nasal_closure::nasal_closure(nasal_virtual_machine& nvm):vm(nvm)
|
|||
}
|
||||
nasal_closure::~nasal_closure()
|
||||
{
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
vm.mem_free(j->second);
|
||||
elems.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_closure::add_scope()
|
||||
{
|
||||
std::map<std::string,int> new_scope;
|
||||
elems.push_back(new_scope);
|
||||
return;
|
||||
}
|
||||
void nasal_closure::del_scope()
|
||||
{
|
||||
if(this->elems.empty())
|
||||
return;
|
||||
for(std::map<std::string,int>::iterator i=elems.back().begin();i!=elems.back().end();++i)
|
||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
this->elems.pop_back();
|
||||
elems.clear();
|
||||
return;
|
||||
}
|
||||
void nasal_closure::add_new_value(std::string key,int value_address)
|
||||
{
|
||||
int new_mem_address=vm.mem_alloc(value_address);
|
||||
if(elems.back().find(key)!=elems.back().end())
|
||||
if(elems.find(key)!=elems.end())
|
||||
{
|
||||
// if this value already exists,delete the old value and update a new value
|
||||
int old_mem_address=elems.back()[key];
|
||||
int old_mem_address=elems[key];
|
||||
vm.mem_free(old_mem_address);
|
||||
}
|
||||
elems.back()[key]=new_mem_address;
|
||||
elems[key]=new_mem_address;
|
||||
return;
|
||||
}
|
||||
int nasal_closure::get_value_address(std::string key)
|
||||
{
|
||||
int ret_address=-1;
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
{
|
||||
if(i->find(key)!=i->end())
|
||||
ret_address=vm.mem_get((*i)[key]);
|
||||
}
|
||||
if(elems.find(key)!=elems.end())
|
||||
ret_address=vm.mem_get(elems[key]);
|
||||
return ret_address;
|
||||
}
|
||||
int nasal_closure::get_mem_address(std::string key)
|
||||
{
|
||||
int ret_address=-1;
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
{
|
||||
if(i->find(key)!=i->end())
|
||||
ret_address=(*i)[key];
|
||||
}
|
||||
if(elems.find(key)!=elems.end())
|
||||
ret_address=elems[key];
|
||||
return ret_address;
|
||||
}
|
||||
void nasal_closure::set_closure(nasal_closure& tmp)
|
||||
{
|
||||
for(std::list<std::map<std::string,int> >::iterator i=elems.begin();i!=elems.end();++i)
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
vm.mem_free(j->second);
|
||||
for(std::map<std::string,int>::iterator i=elems.begin();i!=elems.end();++i)
|
||||
vm.mem_free(i->second);
|
||||
elems.clear();
|
||||
for(std::list<std::map<std::string,int> >::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i)
|
||||
for(std::map<std::string,int>::iterator i=tmp.elems.begin();i!=tmp.elems.end();++i)
|
||||
{
|
||||
this->add_scope();
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
{
|
||||
int value_addr=vm.mem_get(j->second);
|
||||
int new_mem_addr=vm.mem_alloc(value_addr);
|
||||
vm.add_reference(value_addr);
|
||||
elems.back()[j->first]=new_mem_addr;
|
||||
}
|
||||
int value_addr=vm.mem_get(i->second);
|
||||
int new_mem_addr=vm.mem_alloc(value_addr);
|
||||
vm.add_reference(value_addr);
|
||||
elems[i->first]=new_mem_addr;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -128,13 +128,11 @@ void nasal_runtime::run()
|
|||
this->function_returned_address=-1;
|
||||
|
||||
this->global_scope_address=nasal_vm.gc_alloc(vm_closure);
|
||||
nasal_vm.gc_get(global_scope_address).get_closure().add_scope();
|
||||
|
||||
time_t begin_time=std::time(NULL);
|
||||
main_progress();
|
||||
time_t end_time=std::time(NULL);
|
||||
|
||||
nasal_vm.gc_get(global_scope_address).get_closure().del_scope();
|
||||
nasal_vm.del_reference(global_scope_address);
|
||||
nasal_vm.clear();
|
||||
|
||||
|
@ -224,17 +222,6 @@ void nasal_runtime::main_progress()
|
|||
int nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr)
|
||||
{
|
||||
int ret_state=rt_exit_without_error;
|
||||
// if local_scope does not exist,create a new one.
|
||||
if(local_scope_addr<0)
|
||||
{
|
||||
local_scope_addr=nasal_vm.gc_alloc(vm_closure);
|
||||
nasal_vm.gc_get(local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
else
|
||||
{
|
||||
nasal_vm.add_reference(local_scope_addr);
|
||||
nasal_vm.gc_get(local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
int expr_number=node.get_children().size();
|
||||
int process_returned_value_addr=-1;
|
||||
for(int i=0;i<expr_number;++i)
|
||||
|
@ -275,8 +262,6 @@ int nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr)
|
|||
if(error || ret_state==rt_break || ret_state==rt_continue || ret_state==rt_return)
|
||||
break;
|
||||
}
|
||||
nasal_vm.gc_get(local_scope_addr).get_closure().del_scope();
|
||||
nasal_vm.del_reference(local_scope_addr);
|
||||
return ret_state;
|
||||
}
|
||||
void nasal_runtime::before_for_loop(nasal_ast& node,int local_scope_addr)
|
||||
|
@ -324,20 +309,8 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
|
|||
{
|
||||
nasal_ast& condition_node=node.get_children()[0];
|
||||
nasal_ast& run_block_node=node.get_children()[1];
|
||||
// create a new local scope to store iterator if local_scope_addr=-1
|
||||
int while_local_scope_addr=local_scope_addr;
|
||||
if(while_local_scope_addr<0)
|
||||
{
|
||||
while_local_scope_addr=nasal_vm.gc_alloc(vm_closure);
|
||||
nasal_vm.gc_get(while_local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
else
|
||||
{
|
||||
nasal_vm.add_reference(local_scope_addr);
|
||||
nasal_vm.gc_get(while_local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
// check condition and begin loop
|
||||
int condition_value_addr=calculation(condition_node,while_local_scope_addr);
|
||||
int condition_value_addr=calculation(condition_node,local_scope_addr);
|
||||
bool result=check_condition(condition_value_addr);
|
||||
nasal_vm.del_reference(condition_value_addr);
|
||||
while(result)
|
||||
|
@ -346,12 +319,10 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
|
|||
ret_state=block_progress(run_block_node,local_scope_addr);
|
||||
if(ret_state==rt_break || ret_state==rt_return || error)
|
||||
break;
|
||||
condition_value_addr=calculation(condition_node,while_local_scope_addr);
|
||||
condition_value_addr=calculation(condition_node,local_scope_addr);
|
||||
result=check_condition(condition_value_addr);
|
||||
nasal_vm.del_reference(condition_value_addr);
|
||||
}
|
||||
nasal_vm.gc_get(while_local_scope_addr).get_closure().del_scope();
|
||||
nasal_vm.del_reference(while_local_scope_addr);
|
||||
}
|
||||
else if(loop_type==ast_forindex || loop_type==ast_foreach)
|
||||
{
|
||||
|
@ -365,26 +336,14 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
|
|||
die(vector_node.get_line(),"must use vector in forindex/foreach");
|
||||
return rt_error;
|
||||
}
|
||||
// create a new local scope to store iterator if local_scope_addr=-1
|
||||
int forei_local_scope_addr=local_scope_addr;
|
||||
if(forei_local_scope_addr<0)
|
||||
{
|
||||
forei_local_scope_addr=nasal_vm.gc_alloc(vm_closure);
|
||||
nasal_vm.gc_get(forei_local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
else
|
||||
{
|
||||
nasal_vm.add_reference(local_scope_addr);
|
||||
nasal_vm.gc_get(forei_local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
// begin loop progress
|
||||
int mem_addr=-1;
|
||||
if(iter_node.get_type()==ast_new_iter)
|
||||
{
|
||||
int new_value_addr=nasal_vm.gc_alloc(vm_nil);
|
||||
std::string val_name=iter_node.get_children()[0].get_str();
|
||||
nasal_vm.gc_get(forei_local_scope_addr).get_closure().add_new_value(val_name,new_value_addr);
|
||||
mem_addr=nasal_vm.gc_get(forei_local_scope_addr).get_closure().get_mem_address(val_name);
|
||||
nasal_vm.gc_get(local_scope_addr<0? global_scope_address:local_scope_addr).get_closure().add_new_value(val_name,new_value_addr);
|
||||
mem_addr=nasal_vm.gc_get(local_scope_addr<0? global_scope_address:local_scope_addr).get_closure().get_mem_address(val_name);
|
||||
}
|
||||
else
|
||||
mem_addr=call_scalar_mem(iter_node,local_scope_addr);
|
||||
|
@ -410,13 +369,11 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
|
|||
nasal_vm.add_reference(value_addr);
|
||||
nasal_vm.mem_change(mem_addr,value_addr);
|
||||
}
|
||||
ret_state=block_progress(run_block_node,forei_local_scope_addr);
|
||||
ret_state=block_progress(run_block_node,local_scope_addr);
|
||||
if(ret_state==rt_break || ret_state==rt_return || error)
|
||||
break;
|
||||
}
|
||||
nasal_vm.del_reference(vector_value_addr);
|
||||
nasal_vm.gc_get(forei_local_scope_addr).get_closure().del_scope();
|
||||
nasal_vm.del_reference(forei_local_scope_addr);
|
||||
}
|
||||
else if(loop_type==ast_for)
|
||||
{
|
||||
|
@ -425,37 +382,23 @@ int nasal_runtime::loop_progress(nasal_ast& node,int local_scope_addr)
|
|||
nasal_ast& each_loop_do_node=node.get_children()[2];
|
||||
nasal_ast& run_block_node=node.get_children()[3];
|
||||
|
||||
// set local scope if local_scope_addr=-1
|
||||
int for_local_scope_addr=local_scope_addr;
|
||||
if(for_local_scope_addr<0)
|
||||
{
|
||||
for_local_scope_addr=nasal_vm.gc_alloc(vm_closure);
|
||||
nasal_vm.gc_get(for_local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
else
|
||||
{
|
||||
nasal_vm.add_reference(local_scope_addr);
|
||||
nasal_vm.gc_get(for_local_scope_addr).get_closure().add_scope();
|
||||
}
|
||||
// for progress
|
||||
before_for_loop(before_loop_node,for_local_scope_addr);
|
||||
int condition_value_addr=calculation(condition_node,for_local_scope_addr);
|
||||
before_for_loop(before_loop_node,local_scope_addr);
|
||||
int condition_value_addr=calculation(condition_node,local_scope_addr);
|
||||
bool result=check_condition(condition_value_addr);
|
||||
nasal_vm.del_reference(condition_value_addr);
|
||||
while(result)
|
||||
{
|
||||
if(error)
|
||||
break;
|
||||
ret_state=block_progress(run_block_node,for_local_scope_addr);
|
||||
ret_state=block_progress(run_block_node,local_scope_addr);
|
||||
if(ret_state==rt_return || ret_state==rt_break || error)
|
||||
break;
|
||||
after_each_for_loop(each_loop_do_node,for_local_scope_addr);
|
||||
condition_value_addr=calculation(condition_node,for_local_scope_addr);
|
||||
after_each_for_loop(each_loop_do_node,local_scope_addr);
|
||||
condition_value_addr=calculation(condition_node,local_scope_addr);
|
||||
result=check_condition(condition_value_addr);
|
||||
nasal_vm.del_reference(condition_value_addr);
|
||||
}
|
||||
nasal_vm.gc_get(for_local_scope_addr).get_closure().del_scope();
|
||||
nasal_vm.del_reference(for_local_scope_addr);
|
||||
}
|
||||
if(ret_state==rt_break || ret_state==rt_continue)
|
||||
ret_state=rt_exit_without_error;
|
||||
|
@ -803,7 +746,6 @@ int nasal_runtime::call_function(nasal_ast& node,std::string func_name,int base_
|
|||
nasal_function& reference_of_func=nasal_vm.gc_get(base_value_addr).get_func();
|
||||
int run_closure_addr=reference_of_func.get_closure_addr();
|
||||
nasal_closure& run_closure=nasal_vm.gc_get(run_closure_addr).get_closure();
|
||||
run_closure.add_scope();
|
||||
if(last_call_hash_addr>=0)
|
||||
{
|
||||
// set hash.me
|
||||
|
@ -966,7 +908,6 @@ int nasal_runtime::call_function(nasal_ast& node,std::string func_name,int base_
|
|||
}
|
||||
}
|
||||
block_progress(reference_of_func.get_run_block(),run_closure_addr);
|
||||
run_closure.del_scope();
|
||||
|
||||
if(function_returned_address>=0)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue