Esempio n. 1
0
struct InterCodes* translate_ExtDef(struct TreeNode* ExtDef){
	if(strcmp(ExtDef->children->name, "CompSt") == 0){
		struct InterCodes* code1 = translate_FunDec(ExtDef->children->neighbours);
		struct InterCodes* code2 = translate_CompSt(ExtDef->children);

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code2;
		code2->prev = p;

		return code1;
	}
}
Esempio n. 2
0
InterCodeNode translate_ExtDef(treenode *ExtDef_n){
	if(ExtDef_n == NULL){
		//deprintf("ExtDef is NULL\n");
		return NULL;
	}
	if(ExtDef_n->child_num == 3 && strcmp(ExtDef_n->node[1]->name,"FunDec") == 0){
		//extdef -> specifier fundec compst
		InterCodeNode node1 = translate_FunDec(ExtDef_n->node[1]);
		InterCodeNode node2 = translate_CompSt(ExtDef_n->node[2]);
		return produceNodeByNode(node1,node2);
	}else if(ExtDef_n->child_num == 3 && strcmp(ExtDef_n->node[1]->name,"ExtDecList") == 0){
		//extdef -> specifier extdeclist ;
		return translate_ExtDecList(ExtDef_n->node[1]);
	}
	return NULL;	
}
Esempio n. 3
0
intercodes translate_ExtDef(struct Node *node) {
	intercodes code1,code2;
	struct Node *child = node->child;
	assert(child != NULL);
	//Specifier ExtDecList SEMI
	if(child -> sibling!= NULL)	{
		if (strcmp(child->sibling->type,"ExtDecList") == 0)  {
			return translate_ExtDecList(child->sibling);
		}
		//Specifier FunDec CompSt
		if (strcmp(child->sibling->type,"FunDec") == 0) {
			code1 = translate_FunDec(child->sibling);
			code2 = translate_CompSt(child->sibling->sibling);
			code1 = link(code1,code2);
			return code1;
		}
	}
	//Specifier SIMI
	return NULL;
}
Esempio n. 4
0
MCAHead translate_Program(Node *n, SymbolTable table, MCA mca)
{
    assert(n->nodetype == Program);
    Var v1, v2, v3;
    Node* extdl = NT_getChild(n, 0);
    Node *ext, *comp, *funcdecid;
    MCAHead h = NULL;
    MCAHead h1, h2, h3;
    
    while(extdl->seq != 1)
    {
        ext = NT_getChild(extdl, 0);
        if(ext->seq == 2)
        {
            h1 = translate_FunDec(NT_getChild(ext, 1), table, mca);
            #ifdef debug
            printf("fundec\n");
            printMCA(mca, h1);
            printf("fundec\n");
            #endif

            comp = NT_getChild(ext, 2);
            h2 = translate_CompSt(comp, table, mca);
            if(h == NULL)
                h = LinkMulMCAHead(mca, 2, h1, h2);
            else
                h = LinkMulMCAHead(mca, 3, h, h1, h2);
        }



        extdl = NT_getChild(extdl, 1);
    }


    return h;
}
Esempio n. 5
0
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;
	}
}
Esempio n. 6
0
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;
}
Esempio n. 7
0
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;
		}
	}
}
Esempio n. 8
0
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;
}