intercodes translate_StmtList(struct Node *node) { intercodes code1 = NULL; intercodes code2 = NULL; //NULL if (node == NULL) return NULL; struct Node *child = node->child; assert(child != NULL); //Stmt StmtList code1 = translate_Stmt(child); code2 = translate_StmtList(child->sibling); //code1 = link(code1,code2); return code1; }
InterCodes translate_Stmtlist(node* Stmtlist){ if(Stmtlist != NULL && Stmtlist->child != NULL){ InterCodes codes1 = InterCodes_init(); InterCodes codes2 = InterCodes_init(); codes1 = translate_Stmt(Stmtlist->child); codes2 = translate_Stmtlist(Stmtlist->child->brother); if(codes1 == NULL){ codes1 = codes2; } else InterCodes_link(codes1,codes2); return codes1; } else return NULL; }
InterCodeNode translate_StmtList(treenode *StmtList_n){ if(StmtList_n == NULL){ //deprintf("StmtList is NULL\n"); return NULL; } if(StmtList_n->child_num != 2){ //stmtlist -> empty assert(StmtList_n->child_num == 0); //deprintf("StmtList is empty\n"); return NULL; } //stmtlist -> stmt stmtlist InterCodeNode node1 = translate_Stmt(StmtList_n->node[0]); //show(node1); InterCodeNode node2 = translate_StmtList(StmtList_n->node[1]); return produceNodeByNode(node1,node2); }
void intercode_aly(node *p){ char name[20]; strcpy(name,p->name); if (p == NULL){ return ; } if(strcmp(name,"ExtDef")==0){ InterCodes codes = translate_Extdef(p); add_to_head(codes); if(p->brother != NULL) intercode_aly(p->brother); return; } else if(strcmp(name,"Exp")==0){ Operand t1 = new_temp(); InterCodes expe = translate_Exp(p,t1); add_to_head(expe); if(p->brother != NULL) intercode_aly(p->brother); return; } else if(strcmp(name,"CompSt")==0){/* InterCodes codes = translate_Compst(p); add_to_head(codes); if(p->brother != NULL) intercode_aly(p->brother); return;*/ } else if(strcmp(name,"Stmt")==0){ InterCodes codes = translate_Stmt(p); add_to_head(codes); if(p->brother != NULL) intercode_aly(p->brother); return; } if(p->child != NULL) intercode_aly(p->child); if(p->brother != NULL) intercode_aly(p->brother); return; }
struct InterCodes* translate_StmtList(struct TreeNode* StmtList){ if(StmtList->children == NULL){ struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code1->code.kind = NONE_; return code1; } else{ struct InterCodes* code1 = translate_Stmt(StmtList->children->neighbours); struct InterCodes* code2 = translate_StmtList(StmtList->children); struct InterCodes* p = code1; while(p->next != NULL) p = p->next; p->next = code2; code2->prev = p; return code1; } }
MCAHead translate_CompSt(Node *n, SymbolTable table, MCA mca) { assert(n->nodetype == CompSt); Var v1, v2, v3; Node* stmtlist = NT_getChild(n, 2); Node* deflist = NT_getChild(n, 1); Node *stmt; MCAHead h = NULL; MCAHead h1, h2, h3; h = translate_DefList(deflist, table, mca); while(stmtlist->seq != 1) { stmt = NT_getChild(stmtlist, 0); h1 = translate_Stmt(stmt, table, mca); h = LinkMulMCAHead(mca, 2, h, h1); stmtlist = NT_getChild(stmtlist, 1); } return h; }
InterCodeNode translate_Stmt(treenode *Stmt_n){ if(Stmt_n == NULL){ //deprintf("Stmt is NULL\n"); return NULL; } if(Stmt_n->child_num == 1){ //compst return translate_CompSt(Stmt_n->node[0]); }else if(Stmt_n->child_num == 2){ //exp ; Operand temp = new_Operand(); return translate_Exp(Stmt_n->node[0],temp); }else if(Stmt_n->child_num == 3){ //return exp ; Operand temp = new_Temp(); InterCodeNode node1 = translate_Exp(Stmt_n->node[1],temp); InterCode ret_code = new_InterCode(); ret_code->kind = RETURN; ret_code->u.retn.return_op = temp; InterCodeNode node2 = produceNodeByCode(ret_code); return produceNodeByNode(node1,node2); }else if(Stmt_n->child_num == 5){ treenode *head = Stmt_n->node[0]; if(strcmp(head->operval,"IF") == 0){ //if (exp) stmt Operand label1 = new_Label(); Operand label2 = new_Label(); InterCodeNode node1 = translate_Cond(Stmt_n->node[2],label1,label2); InterCodeNode node3 = translate_Stmt(Stmt_n->node[4]); InterCode code2 = new_InterCode(); code2->kind = LABEL; code2->u.label.label_op = label1; InterCodeNode node2 = NULL;//produceNodeByCode(code2); InterCode code4 = new_InterCode(); code4->kind = LABEL; code4->u.label.label_op = label2; InterCodeNode node4 = produceNodeByCode(code4); InterCodeNode temp1 = produceNodeByNode(node1,node2); InterCodeNode temp2 = produceNodeByNode(temp1,node3); return produceNodeByNode(temp2,node4); } if(strcmp(head->operval,"WHILE") == 0){ //while (exp) stmt Operand label1 = new_Label(); Operand label2 = new_Label(); Operand label3 = new_Label(); InterCode code1 = new_InterCode(); code1->kind = LABEL; code1->u.label.label_op = label1; InterCodeNode node1 = produceNodeByCode(code1); InterCodeNode node2 = translate_Cond(Stmt_n->node[2],label2,label3); InterCode code3 = new_InterCode(); code3->kind = LABEL; code3->u.label.label_op = label2; InterCodeNode node3 = NULL;//produceNodeByCode(code3); InterCodeNode node4 = translate_Stmt(Stmt_n->node[4]); InterCode code5 = new_InterCode(); code5->kind = GOTO; code5->u.label.label_op = label1; InterCodeNode node5 = produceNodeByCode(code5); InterCode code6 = new_InterCode(); code6->kind = LABEL; code6->u.label.label_op = label3; InterCodeNode node6 = produceNodeByCode(code6); InterCodeNode temp1 = produceNodeByNode(node1,node2); InterCodeNode temp2 = produceNodeByNode(temp1,node3); InterCodeNode temp3 = produceNodeByNode(temp2,node4); InterCodeNode temp4 = produceNodeByNode(temp3,node5); return produceNodeByNode(temp4,node6); } }else if(Stmt_n->child_num == 7){ //if (exp) stmt else stmt Operand label1 = new_Label(); Operand label2 = new_Label(); Operand label3 = new_Label(); InterCodeNode node1 = translate_Cond(Stmt_n->node[2],label1,label2); InterCode code2 = new_InterCode(); code2->kind = LABEL; code2->u.label.label_op = label1; InterCodeNode node2 = NULL;//produceNodeByCode(code2); InterCodeNode node3 = translate_Stmt(Stmt_n->node[4]); InterCode code4 = new_InterCode(); code4->kind = GOTO; code4->u.label.label_op = label3; InterCodeNode node4 = produceNodeByCode(code4); InterCode code5 = new_InterCode(); code5->kind = LABEL; code5->u.label.label_op = label2; InterCodeNode node5 = produceNodeByCode(code5); InterCodeNode node6 = translate_Stmt(Stmt_n->node[6]); InterCode code7 = new_InterCode(); code7->kind = LABEL; code7->u.label.label_op = label3; InterCodeNode node7 = produceNodeByCode(code7); InterCodeNode temp1 = produceNodeByNode(node1,node2); InterCodeNode temp2 = produceNodeByNode(temp1,node3); InterCodeNode temp3 = produceNodeByNode(temp2,node4); InterCodeNode temp4 = produceNodeByNode(temp3,node5); InterCodeNode temp5 = produceNodeByNode(temp4,node6); return produceNodeByNode(temp5,node7); }else{ //deprintf("**** Illegal Stmt Error ****\n"); return NULL; } }
MCAHead translate_Stmt(Node* n, SymbolTable table, MCA mca) { assert(n->nodetype == Stmt); Var v1, v2, v3; Node* child = n->children->head->next; MCAHead h; MCAHead h1, h2, h3, h4, h5, h6, h7; switch(n->seq) { case 0: //Exp h = translate_Exp(child, table, NULL, mca); break; case 1: //Compst return translate_CompSt(child, table, mca); break; case 2: //RETURN v1 = NewVar(PLACE); v1->ttype = TEMP; h1 = translate_Exp(child->next, table, v1, mca); h2 = NewMCAHead(mca, NewMidCode(C_RETURN, v1, NULL, NULL)); h = LinkMulMCAHead(mca, 2, h1, h2); break; case 3: //IF v1 = NewVar(PLACE); v1->ttype = LABEL; v2 = NewVar(PLACE); v2->ttype = LABEL; h1 = translate_Cond(child->next->next, v1, v2, table,mca); h2 = NewMCAHead(mca, NewMidCode(C_LAB, v1, NULL, NULL)); h3 = translate_Stmt(NT_getChild(n, 4), table, mca); h4 = NewMCAHead(mca, NewMidCode(C_LAB, v2, NULL, NULL)); h = LinkMulMCAHead(mca, 4, h1, h2, h3, h4); break; case 4: //IF ELSE v1 = NewVar(PLACE); v1->ttype = LABEL; v2 = NewVar(PLACE); v2->ttype = LABEL; v3 = NewVar(PLACE); v3->ttype = LABEL; h1 = translate_Cond(child->next->next, v1, v2, table,mca); h2 = NewMCAHead(mca, NewMidCode(C_LAB, v1, NULL, NULL)); h3 = translate_Stmt(NT_getChild(n, 4), table, mca); h4 = NewMCAHead(mca, NewMidCode(C_GOTO, v3, NULL, NULL)); h5 = NewMCAHead(mca, NewMidCode(C_LAB, v2, NULL, NULL)); h6 = translate_Stmt(NT_getChild(n, 6), table, mca); h7 = NewMCAHead(mca, NewMidCode(C_LAB, v3, NULL, NULL)); assert(h6 != NULL && h2 != NULL); h = LinkMulMCAHead(mca, 7, h1, h2, h3, h4, h5, h6, h7); break; case 5: //WHILE v1 = NewVar(PLACE); v1->ttype = LABEL; v2 = NewVar(PLACE); v2->ttype = LABEL; v3 = NewVar(PLACE); v3->ttype = LABEL; h1 = NewMCAHead(mca, NewMidCode(C_LAB, v1, NULL, NULL)); h2 = translate_Cond(NT_getChild(n, 2), v2, v3, table,mca); h3 = NewMCAHead(mca, NewMidCode(C_LAB, v2, NULL, NULL)); h4 = translate_Stmt(NT_getChild(n, 4), table, mca); h5 = NewMCAHead(mca, NewMidCode(C_GOTO, v1, NULL, NULL)); h6 = NewMCAHead(mca, NewMidCode(C_LAB, v3, NULL, NULL)); h = LinkMulMCAHead(mca, 6, h1, h2, h3, h4, h5, h6); break; } return h; }
InterCodes translate_Stmt(node* Stmt){ if(strcmp(Stmt->child->name,"Exp") == 0){ Operand t = new_temp(); return translate_Exp(Stmt->child,t); } else if(strcmp(Stmt->child->name,"CompSt") == 0){ return translate_Compst(Stmt->child); } else if(strcmp(Stmt->child->name,"RETURN") == 0){ Operand t1 = new_temp(); InterCodes codes1 = translate_Exp(Stmt->child->brother,t1); InterCodes codes2 = InterCodes_init(); codes2->code = new_interCode(RET); codes2->code->onlyop.op = t1; InterCodes_link(codes1,codes2); return codes1; } else if(strcmp(Stmt->child->name,"WHILE") == 0){ Operand label1 = new_label(); Operand label2 = new_label(); Operand label3 = new_label(); InterCodes codes1 = InterCodes_init(); codes1->code = new_interCode(LAB); codes1->code->onlyop.op = label1; InterCodes codes2 = translate_Cond(Stmt->child->brother->brother, label2, label3); InterCodes codes3 = InterCodes_init(); codes3->code = new_interCode(LAB); codes3->code->onlyop.op = label2; InterCodes codes4 = translate_Stmt(Stmt->child->brother->brother->brother->brother); InterCodes codes5 = InterCodes_init(); codes5->code = new_interCode(GOTO); codes5->code->onlyop.op = label1; InterCodes codes6 = InterCodes_init(); codes6->code = new_interCode(LAB); codes6->code->onlyop.op = label3; InterCodes_link(codes1,codes2); InterCodes_link(codes1,codes3); InterCodes_link(codes1,codes4); InterCodes_link(codes1,codes5); InterCodes_link(codes1,codes6); return codes1; } //IF LP Exp RP Stmt1 else if(Stmt->child->brother->brother->brother->brother->brother == NULL){ Operand label1 = new_label(); Operand label2 = new_label(); InterCodes codes1 = translate_Cond(Stmt->child->brother->brother, label1, label2); InterCodes codes2 = InterCodes_init(); codes2->code = new_interCode(LAB); codes2->code->onlyop.op = label1; InterCodes codes3 = translate_Stmt(Stmt->child->brother->brother->brother->brother); InterCodes codes4 = InterCodes_init(); codes4->code = new_interCode(LAB); codes4->code->onlyop.op = label2; InterCodes_link(codes1,codes2); InterCodes_link(codes1,codes3); InterCodes_link(codes1,codes4); return codes1; } //IF LP Exp RP Stmt1 ELSE Stmt2 else{ Operand label1 = new_label(); Operand label2 = new_label(); Operand label3 = new_label(); InterCodes codes1 = translate_Cond(Stmt->child->brother->brother, label1, label2); InterCodes codes2 = InterCodes_init(); codes2->code = new_interCode(LAB); codes2->code->onlyop.op = label1; InterCodes codes3 = translate_Stmt(Stmt->child->brother->brother->brother->brother); InterCodes codes4 = InterCodes_init(); codes4->code = new_interCode(GOTO); codes4->code->onlyop.op = label3; InterCodes codes5 = InterCodes_init(); codes5->code = new_interCode(LAB); codes5->code->onlyop.op = label2; InterCodes codes6 = translate_Stmt(Stmt->child->brother->brother->brother->brother->brother->brother); InterCodes codes7 = InterCodes_init(); codes7->code = new_interCode(LAB); codes7->code->onlyop.op = label3; InterCodes_link(codes1,codes2); InterCodes_link(codes1,codes3); InterCodes_link(codes1,codes4); InterCodes_link(codes1,codes5); InterCodes_link(codes1,codes6); InterCodes_link(codes1,codes7); return codes1; } //else if(Stmt->child->brother-) }
struct InterCodes* translate_Stmt(struct TreeNode* Stmt){ if (strcmp(Stmt->children->name, "SEMI") == 0){ if(Stmt->children->neighbours->neighbours == NULL){ printf("Stmt - Stmt SEMI\n"); return translate_Exp(Stmt->children->neighbours, NULL); } else{ printf("Stmt - RETURN Stmt SEMI\n"); Operand t1 = new_temp(); struct InterCodes* code1 = translate_Exp(Stmt->children->neighbours, t1); struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code2->code.kind = RETURN_; code2->code.u.returncode.r = t1; struct InterCodes* p = code1; while(p->next != NULL) p = p->next; p->next = code2; code2->prev = p; code2->next = NULL; return code1; } } if(strcmp(Stmt->children->name, "CompSt") == 0){ printf("Stmt - CompSt\n"); return translate_CompSt(Stmt->children); } if(strcmp(Stmt->children->name, "Stmt") == 0){ if(strcmp(Stmt->children->neighbours->neighbours->neighbours->neighbours->name, "IF") == 0){ printf("Stmt - IF LP Exp RP Stmt\n"); Operand label1 = new_label(); Operand label2 = new_label(); struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label1, label2); struct InterCodes* code2 = translate_Stmt(Stmt->children); struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code3->code.kind = LABEL_; code3->code.u.labelcode.label = label1; struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code4->code.kind = LABEL_; code4->code.u.labelcode.label = label2; struct InterCodes* p = code1; while(p->next != NULL) p = p->next; p->next = code3; code3->prev = p; code3->next = code2; p = code2; while(p->next != NULL) p = p->next; p->next = code4; code4->prev = p; code4->next = NULL; return code1; } if(strcmp(Stmt->children->neighbours->neighbours->neighbours->neighbours->name, "WHILE") == 0){ printf("Stmt - WHILE LP Exp RP Stmt\n"); Operand label1 = new_label(); Operand label2 = new_label(); Operand label3 = new_label(); struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label2, label3); struct InterCodes* code2 = translate_Stmt(Stmt->children->neighbours->neighbours); struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code3->code.kind = LABEL_; code3->code.u.labelcode.label = label1; struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code4->code.kind = LABEL_; code4->code.u.labelcode.label = label2; struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code5->code.kind = LABEL_; code5->code.u.labelcode.label = label3; struct InterCodes* code6 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code6->code.kind = GOTO_; code6->code.u.gotocode.label = label3; code3->next = code1; code1->prev = code3; struct InterCodes* p = code1; while(p->next != NULL) p = p->next; p->next = code4; code4->prev = p; code4->next = code2; p = code2; while(p->next != NULL) p = p->next; p->next = code6; code6->prev = p; code6->next = code5; code5->prev = code6; code5->next = NULL; return code3; } if(strcmp(Stmt->children->neighbours->name, "ELSE") == 0){ printf("Stmt - IF LP Exp RP Stmt ELSE Stmt\n"); Operand label1 = new_label(); Operand label2 = new_label(); Operand label3 = new_label(); struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label1, label2); struct InterCodes* code2 = translate_Stmt(Stmt->children->neighbours->neighbours); struct InterCodes* code3 = translate_Stmt(Stmt->children); struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code4->code.kind = LABEL_; code4->code.u.labelcode.label = label1; struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code5->code.kind = LABEL_; code5->code.u.labelcode.label = label2; struct InterCodes* code6 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code6->code.kind = LABEL_; code6->code.u.labelcode.label = label3; struct InterCodes* code7 = (struct InterCodes*)malloc(sizeof(struct InterCodes)); code7->code.kind = GOTO_; code7->code.u.gotocode.label = label3; struct InterCodes* p = code1; while(p->next != NULL) p = p->next; p->next = code4; code4->prev = p; code4->next = code2; p = code2; while(p->next != NULL) p = p->next; p->next = code7; code7->prev = p; code7->next = code5; code5->prev = code7; code5->next = code3; p = code3; while(p->next != NULL) p = p->next; p->next = code6; code6->prev = p; code6->next = NULL; return code1; } } }
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; }