InterCodes translate_Dec(node* dec){ /* * Dec * | VarDec * | VarDec ASSIGNOP Exp */ if(dec->child->brother != NULL){ Operand t1 = new_temp(); InterCodes codes1 = translate_Exp(dec->child->brother->brother, t1); InterCodes codes2 = translate_VarDec(dec->child); InterCodes codes3 = InterCodes_init(); codes3->code = new_interCode(0); codes3->code->assign.left = new_operand_name(dec->child->node_value); codes3->code->assign.right = t1; if(codes1 == NULL){ codes1 = codes2; } else{ InterCodes_link(codes1,codes2); } InterCodes_link(codes1,codes3); return codes1; } else if(dec->child->brother == NULL){ return translate_VarDec(dec->child); } return NULL; }
intercodes translate_ExtDecList(struct Node *node) { intercodes code1,code2; struct Node *child = node->child; assert(child != NULL); //VarDec if (child->sibling == NULL) return translate_VarDec(child,NULL); if (child->sibling != NULL && strcmp(child->sibling->type,"COMMA") == 0) { code1 = translate_VarDec(child,NULL); code2 = translate_ExtDecList(child->sibling->sibling); code1 = link(code1,code2); return code1; } return NULL; }
intercodes translate_VarDec(struct Node *node,operand *place) { intercodes code1,code2,code3,code4; struct Node *child = node->child; assert(child != NULL); //ID if (strcmp(child->type,"ID") == 0) { int id = getVarID(child); enum varType type; union varp vp = getVar(child,&type); operand v1 = new_var(id); if (place != NULL) { *place = v1; } if (type == varInt || type == varFloat) { return NULL; } else if (type == varArray || type == varStruct) { int size = get_structure_size(vp,type); operand c1 = new_constant(size); code1 = gen_assign(DEC_K,v1,c1); return code1; } else { printf("type is varError%d\n",type == varError); exit(-1); } } //VarDec INT LB INT RB return translate_VarDec(child,NULL); }
InterCodes translate_Extdeclist(node* Extdeclist) { InterCodes code1, code2; if(Extdeclist->child->brother == NULL){ //VarDec return translate_VarDec(Extdeclist->child); } else if(Extdeclist->child->brother != NULL && strcmp(Extdeclist->child->brother->node_value, "COMMA") == 0){ //VarDec COMMA ExtDecList code1 = translate_VarDec(Extdeclist->child); code2 = translate_Extdeclist(Extdeclist->child->brother->brother); if(code1 == NULL){ code1 = code2; } else InterCodes_link(code1, code2); return code1; } else return NULL; }
InterCodeNode translate_ExtDecList(treenode *ExtDecList_n){ if(ExtDecList_n == NULL){ //deprintf("ExtDecList is NULL"); return NULL; } if(ExtDecList_n->child_num == 1){ //extdeclist -> vardec Operand temp = new_Operand(); return translate_VarDec(ExtDecList_n->node[0],temp); } if(ExtDecList_n->child_num == 3){ Operand temp = new_Operand(); treenode *vardec_node = ExtDecList_n->node[0]; treenode *extdeclist_node = ExtDecList_n->node[2]; InterCodeNode node1 = translate_VarDec(vardec_node,temp); InterCodeNode node2 = translate_ExtDecList(extdeclist_node); return produceNodeByNode(node1,node2); } }
intercodes translate_ParamDec(struct Node *node) { intercodes code1; struct Node *child = node->child; assert(child != NULL); if (strcmp(child->sibling->type,"VarDec") == 0) { code1 = translate_VarDec(child->sibling,NULL); return code1; } return NULL; }
struct InterCodes* translate_ExtDecList(struct TreeNode* ExtDecList){ if(strcmp(ExtDecList->children->name, "VarDec") == 0){ printf("ExtDecList - VarDec\n"); struct InterCodes* code1 = translate_VarDec(ExtDecList->children); return code1; } if(strcmp(ExtDecList->children->name, "ExtDecList") == 0){ printf("ExtDecList - VarDec COMMA ExtDecList\n"); struct InterCodes* code1 = translate_VarDec(ExtDecList->children->neighbours->neighbours); struct InterCodes* code2 = translate_ExtDecList(ExtDecList->children); struct InterCodes* p = code1; while(p->next != NULL) p = p->next; p->next = code2; code2->prev = p; return code1; } }
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; } }
InterCodeNode translate_Dec(treenode * Dec_n){ if(Dec_n == NULL){ //deprintf("Dec is NULL\n"); return NULL; } if(Dec_n->child_num == 1){ //dec -> vardec Operand temp = new_Operand(); return translate_VarDec(Dec_n->node[0],temp); } //dec -> vardec = exp assert(Dec_n->child_num == 3); Operand left = new_Operand(); Operand right = new_Temp(); InterCodeNode node1 = translate_VarDec(Dec_n->node[0],left); InterCodeNode node2 = translate_Exp(Dec_n->node[2],right); InterCode assign_code = new_InterCode(); assign_code->kind = ASSIGN; assign_code->u.assign.left = left; assign_code->u.assign.right = right; InterCodeNode node3 = produceNodeByCode(assign_code); InterCodeNode temp = produceNodeByNode(node1,node2); return produceNodeByNode(temp,node3); }
InterCodes translate_VarDec(node* VarDec){ InterCodes code1 = InterCodes_init(); if(strcmp(VarDec->child->name, "ID") == 0){ //ID FieldList p = Findname(VarDec->child->node_value); if(p->type->kind == Int || p->type->kind == Float){ return NULL; } else if(p->type->kind == ARRAY || p->type->kind == STRUCTVAR ){ int size = getSize(p); code1->code = new_interCode(DEC); code1->code->assign.left = new_operand_name(VarDec->child->node_value); code1->code->assign.right = new_operand(1,size); } return code1; } else //ID LB INT RB return translate_VarDec(VarDec->child); }