intercodes translate_Cond(struct Node *node,operand label_true,operand label_false) { struct Node *child = node->child; assert(child != NULL); intercodes code1,code2,code3,code4; //Exp1 RELOP Exp2 if (child->sibling != NULL && strcmp(child->sibling->type,"RELOP") == 0) { operand t1 = new_tmp(); operand t2 = new_tmp(); code1 = translate_Exp(child,t1); code2 = translate_Exp(child->sibling->sibling,t2); relop_type type = get_relop(child->sibling); code3 = gen_triop(type,t1,t2,label_true); code4 = gen_one(GOTO_K,label_false); code1 = link(code1,code2); code1 = link(code1,code3); code1 = link(code1,code4); return code1; } //NOT Exp1 if (strcmp(child->type,"NOT") == 0) return translate_Cond(child->sibling,label_true,label_false); //Exp1 AND Exp2 if (child->sibling != NULL && strcmp(child->sibling->type,"AND") == 0) { operand label1 = new_label(); code1 = translate_Cond(child,label1,label_false); code2 = translate_Cond(child->sibling->sibling,label_true,label_false); code3 = gen_one(LABEL_K,label1); code1 = link(code1,code3); code1 = link(code1,code2); return code1; } //Exp1 OR Exp2 if (child->sibling != NULL && strcmp(child->sibling->type,"OR") == 0) { operand label1 = new_label(); code1 = translate_Cond(child,label_true,label1); code2 = translate_Cond(child->sibling->sibling,label_true,label_false); code3 = gen_one(LABEL_K,label1); code1 = link(code1,code3); code1 = link(code1,code2); return code1; } //other cases operand t1 = new_tmp(); code1 = translate_Exp(node,t1); operand c1 = new_constant(0); code2 = gen_triop(NE,t1,c1,label_true); code3 = gen_one(LABEL_K,label_false); code1 = link(code1,code2); code1 = link(code1,code3); return code1; }
intercodes translate_array(struct Node *node,operand place,union varp *list,enum varType *listtype) { struct Node *child = node->child; assert(child != NULL); intercodes code1 = NULL; intercodes code2,code3,code4; int size = 0; operand t1; //ID[Exp] if (strcmp(child->sibling->type,"ID") == 0) { int id = getVarID(child->child); int flag = getFlag(child->child->text); if (flag == 1) t1 = new_var(id); else t1 = new_reference(id); enum varType type; union varp vp = getVar(child->child,&type); if (vp.ap->basetype == varStruct) size = get_structure_size(vp.ap->base,vp.ap->basetype); if (vp.ap->basetype == varInt || vp.ap->basetype == varFloat) size = 4; if (list != NULL) { *list = vp.ap->base; *listtype = vp.ap->basetype; } } //Exp[Exp][Exp] // if (child->child->sibling != NULL && strcmp(child->child->sibling->type,"LB") == 0) { // } //handle Exp2 operand t2 = new_tmp(); code2 = translate_Exp(child->sibling->sibling,t2); code1 = link(code1,code2); //calculate offset operand c1 = new_constant(size); operand t3 = new_tmp(); code3 = gen_binop(MUL_K,t3,t2,c1); //Add array address operand t4 = new_tmp_id(place->u.tmp_no); code4 = gen_binop(ADD_K,t4,t1,t3); code1 = link(code1,code4); place->kind = ADDRESS; return code1; }
intercodes translate_Args(struct Node *node,operand *arg_list,int *arg_num) { intercodes code1,code2,code3,code4; struct Node *child = node->child; assert(child != NULL); //Exp if (child->sibling == NULL) { operand t1 = new_tmp(); code1 = translate_Exp(child,t1); arg_list[*arg_num++] = t1; return code1; } //Exp COMMA Args if (strcmp(child->sibling->type,"COMMA") == 0) { operand t1 = new_tmp(); code1 = translate_Exp(child,t1); arg_list[*arg_num++] = t1; code2 = translate_Args(child->sibling->sibling,arg_list,arg_num); code1 = link(code1,code2); return code1; } }
t_obj *my_spot(t_obj *obj, int fd, int *cmp) { t_tmp *tmp; char *s; tmp = new_tmp(); tmp->name = "spot"; while ((s = get_next_line(fd)) && my_strcmp(s, "</SPOT>")) { if (s[0] && s[0] != COMMENT_CHAR) tmp = parse_str(tmp, s, cmp); (*cmp)++; free(s); } obj = append_obj(obj, tmp); free(s); free(tmp); return (obj); }
intercodes translate_Dec(struct Node *node) { struct Node *child = node->child; assert(child != NULL); intercodes code1,code2,code3; //VarDec if (child->sibling == NULL) { return translate_VarDec(child,NULL); } //VarDec ASSIGNOP Exp if (child->sibling != NULL && strcmp(child->sibling->type,"ASSIGNOP") == 0) { operand t1 = NULL; code1 = translate_VarDec(child,&t1); operand t2 = new_tmp(); code2 = translate_Exp(child->sibling->sibling,t2); code3 = gen_assign(ASSIGN_K,t1,t2); code1 = link(code1,code2); code1 = link(code1,code3); return code1; } }
hdl::detail::named_obj::named_obj(std::string name) : myname(name == "" ? new_tmp() : name) { }
/* Create a new temporary and set it to the value of a CPU register. */ static inline TCGv load_reg(DisasContext *s, int reg) { TCGv tmp = new_tmp(); tcg_gen_mov_i32(tmp, cpu_R[reg >> 2]); return tmp; }
static void gen_exception(int excp) { TCGv tmp = new_tmp(); tcg_gen_movi_i32(tmp, excp); gen_helper_exception(tmp); dead_tmp(tmp); }
intercodes translate_Stmt(struct Node *node) { struct Node *child = node->child; assert(child != NULL); intercodes code1,code2,code3,code4,code5,code6,code7; //Exp SEMI if (strcmp(child->type,"Exp") == 0) { return translate_Exp(child,NULL); } //CompSt if (strcmp(child->type,"CompSt") == 0) return translate_CompSt(child); //RETURN Exp SEMI if (strcmp(child->type,"RETURN") == 0) { operand t1 = new_tmp(); code1 = translate_Exp(child->sibling,t1); code2 = gen_one(RETURN_K,t1); // code1 = link(code1,code2); return code1; } if (strcmp(child->type,"IF") == 0) { struct Node *brother = child->sibling->sibling->sibling->sibling->sibling; //IF LP Exp RP Stmt1 if (brother == NULL) { operand label1 = new_label(); operand label2 = new_label(); code1 = translate_Cond(child->sibling->sibling,label1,label2); code2 = translate_Stmt(child->sibling->sibling->sibling->sibling); code3 = gen_one(LABEL_K,label1); code4 = gen_one(LABEL_K,label2); code1 = link(code1,code3); code1 = link(code1,code2); code1 = link(code1,code4); return code1; } //IF LP Exp RP Stmt1 ELSE Stmt2 else { operand label1 = new_label(); operand label2 = new_label(); operand label3 = new_label(); code1 = translate_Cond(child->sibling->sibling,label1,label2); code2 = translate_Stmt(child->sibling->sibling->sibling->sibling); code3 = translate_Stmt(brother->sibling); code4 = gen_one(LABEL_K,label1); code5 = gen_one(LABEL_K,label2); code6 = gen_one(LABEL_K,label3); code7 = gen_one(GOTO_K,label3); code1 = link(code1,code4); code1 = link(code1,code2); code1 = link(code1,code7); code1 = link(code1,code5); code1 = link(code1,code3); code1 = link(code1,code6); return code1; } } //WHILE LP Exp RP Stmt1 if (strcmp(child->type,"WHILE") == 0) { operand label1 = new_label(); operand label2 = new_label(); operand label3 = new_label(); code1 = translate_Cond(child->sibling->sibling,label2,label3); code2 = translate_Stmt(child->sibling->sibling->sibling->sibling); code3 = gen_one(LABEL_K,label1); code4 = gen_one(LABEL_K,label2); code5 = gen_one(LABEL_K,label3); code6 = gen_one(GOTO_K,label1); code1 = link(code3,code1); code1 = link(code1,code4); code1 = link(code1,code2); code1 = link(code1,code6); code1 = link(code1,code5); return code1; } return NULL; }
//translate intercodes translate_Exp(struct Node *node, operand place) { struct Node *child = node->child; assert(child != NULL); intercodes code1 = NULL; intercodes code2 = NULL; intercodes code3 = NULL; intercodes code4 = NULL; intercodes code5 = NULL; //INT if (strcmp(child->type,"INT") == 0) { operand c1 = new_constant(atoi(child->text)); code1 = gen_assign(ASSIGN_K,place,c1); return code1; } //ID if (strcmp(child->type,"ID") == 0 && child->sibling == NULL) { int id = getVarID(child); operand v1 = new_var(id); code1 = gen_assign(ASSIGN_K,place,v1); return code1; } //Exp1 ASSINGOP Exp2 if (strcmp(child->type,"Exp") == 0 && strcmp(child->sibling->type,"ASSIGNOP") == 0) { //Exp1 is ID (ID = Exp2) // printf("%s\n",child->child->type); if (strcmp(child->child->type,"ID") == 0) { operand t1 = new_tmp(); int id = getVarID(child->child); operand v1 = new_var(id); code1 = translate_Exp(child->sibling->sibling,t1); code2 = gen_assign(ASSIGN_K,v1,t1); // code1 = link(code1,code2); if (place != NULL) { code3 = gen_assign(ASSIGN_K,place,v1); code1 = link(code1,code3); } return code1; } //Exp[Exp] = Exp2 } //Exp1 PLUS/MINUS/STAR/DIV Exp2 if (strcmp(child->type,"Exp") == 0 && (strcmp(child->sibling->type,"PLUS") == 0 || strcmp(child->sibling->type,"MINUS") == 0 || strcmp(child->sibling->type,"STAR") == 0 || strcmp(child->sibling->type,"DIV") == 0)) { operand t1 = new_tmp(); operand t2 = new_tmp(); code1 = translate_Exp(child,t1); code2 = translate_Exp(child->sibling->sibling,t2); //printf("%d %d\n", t1->u.tmp_no, t2 -> u.tmp_no); if (strcmp(child->sibling->type,"PLUS") == 0) { code3 = gen_binop(ADD_K,place,t1,t2); } else if (strcmp(child->sibling->type,"MINUS") == 0) code3 = gen_binop(SUB_K,place,t1,t2); else if (strcmp(child->sibling->type,"STAR") == 0) code3 = gen_binop(MUL_K,place,t1,t2); else if (strcmp(child->sibling->type,"DIV") == 0) code3 = gen_binop(DIV_K,place,t1,t2); //code1 = link(code1,code2); //code1 = link(code1,code3); return code1; } //MINUS Exp1 if (strcmp(child->type,"MINUS") == 0) { operand t1 = new_tmp(); code1 = translate_Exp(child,t1); operand c1 = new_constant(0); code2 = gen_binop(SUB_K,place,c1,t1); code1 = link(code1,code2); return code1; } //Exp1 RELOP Exp2 //NOT Exp1 //Exp1 AND Exp2 //Exp1 OR Exp2 if (strcmp(child->type,"NOT") == 0 || strcmp(child->sibling->type,"RELOP") == 0 || strcmp(child->sibling->type,"AND") == 0 || strcmp(child->sibling->type,"OR") == 0) { operand label1 = new_label(); operand label2 = new_label(); operand c1 = new_constant(0); operand c2 = new_constant(1); code1 = gen_assign(ASSIGN_K,place,c1); code2 = translate_Cond(node,label1,label2); code3 = gen_one(LABEL_K,label1); code4 = gen_assign(ASSIGN_K,place,c2); code5 = gen_one(LABEL_K,label2); code1 = link(code1,code2); code1 = link(code1,code3); code1 = link(code1,code4); code1 = link(code1,code5); return code1; } //ID LP RP if (strcmp(child->sibling->sibling->type,"LP") == 0 && strcmp(child->sibling->sibling->type,"RP") == 0) { char *func_name = child->text; if (strcmp(func_name,"read") == 0) { if (place == NULL) { operand t1 = new_tmp(); code1 = gen_one(READ_K,t1); } else code1 = gen_one(READ_K,place); return code1; } struct funMes *func_node = getFunMes(child); assert(func_node != NULL); operand f1 = new_function(func_name); if (place != NULL && place->kind != ADDRESS) code1 = gen_assign(CALL_K,place,f1); else if (place != NULL && place->kind == ADDRESS) { operand t2 = new_tmp(); code1 = gen_assign(CALL_K,t2,f1); code2 = gen_assign(ASSIGN_K,place,t2); code1 = link(code1,code2); } else { operand t2 = new_tmp(); code1 = gen_assign(CALL_K,t2,f1); } return code1; } //ID LP Args RP if (strcmp(child->sibling->type,"LP") == 0 && strcmp(child->sibling->sibling->type,"Args") == 0) { char *func_name = child->text; operand *arg_list = (operand *)malloc(sizeof(operand) * 10); int arg_num = 0; code1 = translate_Args(child->sibling->sibling,arg_list,&arg_num); if (strcmp(func_name,"write") == 0) { assert(arg_num == 1); operand t1; if (arg_list[0]->kind == ADDRESS) { t1 = new_tmp(); code2 = gen_assign(ASSIGN_K,t1,arg_list[0]); code1 = link(code1,code2); } else t1 = arg_list[0]; code3 = gen_one(WRITE_K,t1); code1 = link(code1,code3); return code1; } int i; for (i = 0;i < arg_num;i++) { code2 = gen_one(ARG_K,arg_list[i]); code1 = link(code1,code2); } operand f1 = new_function(func_name); if (place != NULL && place->kind != ADDRESS) code3 = gen_assign(CALL_K,place,f1); else if (place != NULL && place->kind == ADDRESS) { operand t2 = new_tmp(); code1 = gen_assign(CALL_K,t2,f1); code2 = gen_assign(ASSIGN_K,place,t2); code1 = link(code1,code2); } else { operand t2 = new_tmp(); code3 = gen_assign(CALL_K,t2,f1); } code1 = link(code1,code3); return code1; } //LP Exp RP if (strcmp(child->type,"LP") == 0) { return translate_Exp(child->sibling,place); } //Exp1 LB Exp2 RB if (strcmp(child->type,"LB") == 0) { return translate_array(node,place,NULL,NULL); } //Exp DOT ID return NULL; }