int code_stmt(ASTNode stmt,int BASEREG){ switch(stmt->type){ case Block: code_block(stmt,BASEREG); break; case IfClause: code_ifclause(stmt,BASEREG); break; case AssignExp: code_assign(stmt,BASEREG); break; case NilStmt: stmt->codes.head=stmt->codes.tail=NULL; break; case WhileClause: code_whileclause(stmt,BASEREG); break; case FunctionCall: code_call(stmt,BASEREG); break; default: fprintf(stderr,"unsupported stmt\n"); } return 0; }
void c_typecheck_baset::typecheck_decl(codet &code) { // this comes with 1 operand, which is a declaration if(code.operands().size()!=1) { err_location(code); error() << "decl expected to have 1 operand" << eom; throw 0; } // op0 must be declaration if(code.op0().id()!=ID_declaration) { err_location(code); error() << "decl statement expected to have declaration as operand" << eom; throw 0; } ansi_c_declarationt declaration; declaration.swap(code.op0()); if(declaration.get_is_static_assert()) { assert(declaration.operands().size()==2); codet new_code(ID_static_assert); new_code.add_source_location()=code.source_location(); new_code.operands().swap(declaration.operands()); code.swap(new_code); typecheck_code(code); return; // done } typecheck_declaration(declaration); std::list<codet> new_code; // iterate over declarators for(ansi_c_declarationt::declaratorst::const_iterator d_it=declaration.declarators().begin(); d_it!=declaration.declarators().end(); d_it++) { irep_idt identifier=d_it->get_name(); // look it up symbol_tablet::symbolst::iterator s_it= symbol_table.symbols.find(identifier); if(s_it==symbol_table.symbols.end()) { err_location(code); error() << "failed to find decl symbol `" << identifier << "' in symbol table" << eom; throw 0; } symbolt &symbol=s_it->second; // This must not be an incomplete type, unless it's 'extern' // or a typedef. if(!symbol.is_type && !symbol.is_extern && !is_complete_type(symbol.type)) { error().source_location=symbol.location; error() << "incomplete type not permitted here" << eom; throw 0; } // see if it's a typedef // or a function // or static if(symbol.is_type || symbol.type.id()==ID_code || symbol.is_static_lifetime) { // we ignore } else { code_declt code; code.add_source_location()=symbol.location; code.symbol()=symbol.symbol_expr(); code.symbol().add_source_location()=symbol.location; // add initializer, if any if(symbol.value.is_not_nil()) { code.operands().resize(2); code.op1()=symbol.value; } new_code.push_back(code); } } // stash away any side-effects in the declaration new_code.splice(new_code.begin(), clean_code); if(new_code.empty()) { source_locationt source_location=code.source_location(); code=code_skipt(); code.add_source_location()=source_location; } else if(new_code.size()==1) { code.swap(new_code.front()); } else { // build a decl-block code_blockt code_block(new_code); code_block.set_statement(ID_decl_block); code.swap(code_block); } }
int code_function(ASTNode function){ CODELIST *list; CODELIST envstore,envrestore,stackalloc,stackrestore,statements; CODE86 *code; ASTNode func_block=function->firstchild->nextsibling; ASTNode p=function; SYMTAB symtab=func_block->symtab; SYM sym=function->firstchild->sym; int maxsize; char *symlabel; char *lfblabel; char *lfelabel; /*omp env init */ omp_flag=0; omp_count=0; func_sym=sym; omp_functions.head=omp_functions.tail=NULL; // printf("enter code_function\n"); stack_allocate(symtab,0); maxsize=symtab->size_max; function->codes.head=function->codes.tail=NULL; /* .text */ code=newcode(PS_TEXT,NULL,NULL,NULL); code_link_one(&p->codes,code); /* .globl */ code=newcode(PS_GLOBL,NULL,NULL,(void *)sym); code_link_one(&p->codes,code); /* .type */ code=newcode(PS_TYPE,NULL,NULL,(void *)sym); code_link_one(&p->codes,code); /* .label */ symlabel=new_label(LABEL_SYM,(void *)sym,0); code=newcode(PS_LABEL,NULL,NULL,(void *)symlabel); code_link_one(&p->codes,code); /* .LFB */ lfblabel=new_label(LABEL_LFB,NULL,0); lfelabel=new_label(LABEL_LFE,NULL,0); code=newcode(PS_LABEL,NULL,NULL,(void *)lfblabel); code_link_one(&p->codes,code); /* pushl %ebp */ code=newcode(OP_PUSHL,newdessrc(REG,0,NULL,EBP,0,0),NULL,NULL);; code_link_one(&p->codes,code); /* movl %esp, %ebp */ code=newcode(OP_MOVL,newdessrc(REG,0,NULL,ESP,0,0),newdessrc(REG,0,NULL,EBP,0,0),NULL); code_link_one(&p->codes,code); /* stackalloc */ stackalloc.head=stackalloc.tail=NULL; /* subl maxsize, %esp */ code=newcode(OP_SUBL,newdessrc(IMM,maxsize,NULL,NONE,0,0),newdessrc(REG,0,NULL,ESP,0,0),NULL); code_link_one(&stackalloc,code); /* envstore */ envstore.head=envstore.tail=NULL; /* block */ statements.head=statements.tail=NULL; code_block(func_block,EBP); code_link(&statements,&func_block->codes); /* evnrestore */ envrestore.head=envrestore.tail=NULL; /* stackrestore */ stackrestore.head=stackrestore.tail=NULL; /* addl maxsize, %esp */ code=newcode(OP_ADDL,newdessrc(IMM,maxsize,NULL,NONE,0,0),newdessrc(REG,0,NULL,ESP,0,0),NULL); code_link_one(&stackrestore,code); /******************* link codes ************/ code_link(&p->codes,&stackalloc); code_link(&p->codes,&envstore); code_link(&p->codes,&statements); code_link(&p->codes,&envrestore); code_link(&p->codes,&stackrestore); /* leave */ code=newcode(OP_LEAVE,NULL,NULL,NULL);; code_link_one(&p->codes,code); /* ret */ code=newcode(OP_RET,NULL,NULL,NULL); code_link_one(&p->codes,code); /* .LFE */ code=newcode(PS_LABEL,NULL,NULL,(void *)lfelabel); code_link_one(&p->codes,code); /* .size */ code=newcode(PS_SIZE,NULL,newdessrc(IMM,1,NULL,NONE,0,0),(void *)sym); code_link_one(&p->codes,code); code_link(&p->codes,&omp_functions); return 0; }
bool eval(string express) { filter_useless_char(express); trim(express); if (check_string("{",express.c_str()) && INVALID_VALUE!=express.find('}')) { // inside code block .. for (unsigned long code_block_end_index=get_matching_outside_right_brace(express.substr(1),0), code_block_start_index=express.find('{'); INVALID_VALUE!=code_block_start_index && INVALID_VALUE!=code_block_end_index; code_block_end_index=get_matching_outside_right_brace(express.substr(1),0), code_block_start_index=express.find('{')) { string code_block(express.substr(code_block_start_index+1,code_block_end_index-code_block_start_index)); while (INVALID_VALUE!=code_block.find(';')) { if (!eval(code_block.substr(0,code_block.find(';')))) return false; code_block=code_block.substr(code_block.find(';')+1); } if (!code_block_start_index) break; express=express.substr(0,code_block_start_index-1)+express.substr(code_block_end_index+1); } return true; } bool base_javascript_syntax_execute_result=true; if (check_string("for",express.c_str())) { // base JavaScript syntax .. base_javascript_syntax_execute_result=eval_for(express); } else if (check_string("if",express.c_str())) { base_javascript_syntax_execute_result=eval_if(express); } if (check_string("function",express.c_str())) { base_javascript_syntax_execute_result=eval_decleare_function(express); if (base_javascript_syntax_execute_result) return eval(express); } else if (check_string("return",express.c_str())) { return eval_function_return(express); } string next_express; if (INVALID_VALUE!=express.find(';')) { // put data .. next_express=express.substr(express.find(';')+1); express=express.substr(0,express.find(';')); } if (eval_function(express)) { } else if (INVALID_VALUE!=express.find('=')) { // var asd=123 or var4='asd' char calcu_flag=express[express.find('=')-1]; if ('+'==calcu_flag) { if (INVALID_VALUE!=express.find("+=")) { // asd+=123 -> asd=asd+123 string variant_name(express.substr(0,express.find("+="))); trim(variant_name); if (!eval(variant_name+"="+variant_name+"+"+express.substr(express.find("+=")+2))) return false; } } else if ('-'==calcu_flag) { if (INVALID_VALUE!=express.find("-=")) { // asd+=123 -> asd=asd+123 string variant_name(express.substr(0,express.find("-="))); trim(variant_name); if (!eval(variant_name+"="+variant_name+"-"+express.substr(express.find("-=")+2))) return false; } } else if ('*'==calcu_flag) { if (INVALID_VALUE!=express.find("*=")) { // asd+=123 -> asd=asd+123 string variant_name(express.substr(0,express.find("*="))); trim(variant_name); if (!eval(variant_name+"="+variant_name+"*"+express.substr(express.find("*=")+2))) return false; } } else if ('/'==calcu_flag) { if (INVALID_VALUE!=express.find("/=")) { // asd+=123 -> asd=asd+123 string variant_name(express.substr(0,express.find("/="))); trim(variant_name); if (!eval(variant_name+"="+variant_name+"/"+express.substr(express.find("/=")+2))) return false; } } else { string variant_name; // asd string calcu_express; if (check_string("var",express.c_str())) { if (INVALID_VALUE!=express.find('=')) { variant_name=express.substr(3,express.find('=')-3); calcu_express=express.substr(express.find('=')+1); } else { variant_name=express.substr(3,express.find(';')-3); } } else { if (INVALID_VALUE!=express.find('=')) { variant_name=express.substr(0,express.find('=')); calcu_express=express.substr(express.find('=')+1); } else { variant_name=express.substr(3,express.find(';')-3); } } trim(variant_name); if (!calcu_express.empty()) if (!express_calcu(calcu_express)) return false; if (EXPRESSION_ARRAY==get_express_type(variant_name)) { unsigned long calcu_result=0; support_javascript_variant_type calcu_result_type=NONE; get_variant(JAVASCRIPT_VARIANT_KEYNAME_CALCULATION_RESULT,(void*)&calcu_result,&calcu_result_type); string array_index_express(variant_name.substr(variant_name.find('[')+1,variant_name.find(']')-variant_name.find('[')-1)); if (!express_calcu(array_index_express)) return false; unsigned long array_index=0; support_javascript_variant_type array_index_type=NONE; get_variant(JAVASCRIPT_VARIANT_KEYNAME_CALCULATION_RESULT,(void*)&array_index,&array_index_type); variant_name=variant_name.substr(0,variant_name.find('[')); trim(variant_name); if (INT_ARRAY==get_variant_type(variant_name) && NUMBER!=calcu_result_type) return false; set_variant_array(variant_name,array_index,(void*)calcu_result,calcu_result_type); } else { copy_variant(variant_name,JAVASCRIPT_VARIANT_KEYNAME_CALCULATION_RESULT); } } } if (!next_express.empty()) return eval(next_express); return base_javascript_syntax_execute_result; }