refactor and cleanup the following code: void generate_OR(quad *quad) { quad->address =…
- User
refactor and cleanup the following code:
void generate_OR(quad *quad) { quad->address = instruction_line; instruction *t,*t1,*t2,*t3,*t4;
t = (instruction*)malloc(sizeof(instruction)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->address = instruction_line; t->opcode = jeq_v; t->srcLine = quad->srcLine; make_operand(quad->arg1, t->arg1); make_booloperand(t->arg2, 1); t->result->type = label_a; t->result->val = nextinstructionlabel()+4; emit_vmarg(t); t1 = (instruction*)malloc(sizeof(instruction)); t1->srcLine = quad->srcLine; t1->arg1 = (vmarg*)malloc(sizeof(vmarg)); t1->arg2 = (vmarg*)malloc(sizeof(vmarg)); t1->result = (vmarg*)malloc(sizeof(vmarg)); t1->address = instruction_line; t1->opcode = jeq_v; make_operand(quad->arg2, t1->arg1); make_booloperand(t1->arg2, 1); t1->result->type = label_a; t1->result->val = nextinstructionlabel()+3; emit_vmarg(t1); t2 = (instruction*)malloc(sizeof(instruction)); t2->srcLine = quad->srcLine; t2->arg1 = (vmarg*)malloc(sizeof(vmarg)); t2->arg2 = (vmarg*)malloc(sizeof(vmarg)); t2->result = (vmarg*)malloc(sizeof(vmarg)); t2->address = nextinstructionlabel(); t2->opcode = assign_v; make_booloperand(t2->arg1,0); t2->arg2=NULL; make_operand(quad->result, t2->result); emit_vmarg(t2); t3 = (instruction*)malloc(sizeof(instruction)); t3->srcLine = quad->srcLine; t3->arg1 = (vmarg*)malloc(sizeof(vmarg)); t3->arg2 = (vmarg*)malloc(sizeof(vmarg)); t3->result = (vmarg*)malloc(sizeof(vmarg)); t3->address = instruction_line; t3->opcode = jump_v; t3->arg1=NULL; t3->arg2=NULL; t3->result->type = label_a; t3->result->val = nextinstructionlabel()+2; emit_vmarg(t3); t4 = (instruction*)malloc(sizeof(instruction)); t4->srcLine = quad->srcLine; t4->arg1 = (vmarg*)malloc(sizeof(vmarg)); t4->arg2 = (vmarg*)malloc(sizeof(vmarg)); t4->result = (vmarg*)malloc(sizeof(vmarg)); t4->address = nextinstructionlabel(); t4->opcode = assign_v; make_booloperand(t4->arg1,1); t4->arg2=NULL; make_operand(quad->result, t4->result); emit_vmarg(t4);}
void generate_AND(quad *quad) { quad->address = instruction_line; instruction *t,*t1,*t2,*t3,*t4;
t = (instruction*)malloc(sizeof(instruction)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->srcLine = quad->srcLine; t->address = instruction_line; t->opcode = jeq_v; make_operand(quad->arg1, t->arg1); make_booloperand(t->arg2, 0); t->result->type = label_a; t->result->val = nextinstructionlabel()+4; emit_vmarg(t); t1 = (instruction*)malloc(sizeof(instruction)); t1->arg1 = (vmarg*)malloc(sizeof(vmarg)); t1->arg2 = (vmarg*)malloc(sizeof(vmarg)); t1->result = (vmarg*)malloc(sizeof(vmarg)); t1->srcLine = quad->srcLine; t1->address = instruction_line; t1->opcode = jeq_v; make_operand(quad->arg2, t1->arg1); make_booloperand(t1->arg2, 0); t1->result->type = label_a; t1->result->val = nextinstructionlabel()+3; emit_vmarg(t1); t2 = (instruction*)malloc(sizeof(instruction)); t2->arg1 = (vmarg*)malloc(sizeof(vmarg)); t2->arg2 = (vmarg*)malloc(sizeof(vmarg)); t2->result = (vmarg*)malloc(sizeof(vmarg)); t2->srcLine = quad->srcLine; t2->address = nextinstructionlabel(); t2->opcode = assign_v; make_booloperand(t2->arg1,1); t2->arg2=NULL; make_operand(quad->result, t2->result); emit_vmarg(t2); t3 = (instruction*)malloc(sizeof(instruction)); t3->arg1 = (vmarg*)malloc(sizeof(vmarg)); t3->arg2 = (vmarg*)malloc(sizeof(vmarg)); t3->result = (vmarg*)malloc(sizeof(vmarg)); t3->srcLine = quad->srcLine; t3->address = instruction_line; t3->opcode = jump_v; t3->arg1=NULL; t3->arg2=NULL; t3->result->type = label_a; t3->result->val = nextinstructionlabel()+2; emit_vmarg(t3); t4 = (instruction*)malloc(sizeof(instruction)); t4->arg1 = (vmarg*)malloc(sizeof(vmarg)); t4->arg2 = (vmarg*)malloc(sizeof(vmarg)); t4->result = (vmarg*)malloc(sizeof(vmarg)); t4->srcLine = quad->srcLine; t4->address = nextinstructionlabel(); t4->opcode = assign_v; make_booloperand(t4->arg1,0); t4->arg2=NULL; make_operand(quad->result, t4->result); emit_vmarg(t4);}
void generate_PARAM(quad* quad) { quad->address = instruction_line; instruction t; t = (instruction)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = NULL; t->arg2 = NULL; t->srcLine = quad->srcLine; t->address = nextinstructionlabel(); t->opcode = pusharg_v; make_operand(quad->result, t->result); emit_vmarg(t); }
void generate_CALL(quad* quad) { quad->address = instruction_line; instruction t; t = (instruction)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->address = nextinstructionlabel(); t->opcode = call_v; t->arg1 = NULL; t->arg2 = NULL; t->srcLine = quad->srcLine; if(quad->result->type == programfunc_e){
userfuncTable *tmp = userFuncs; while(tmp!=NULL){ if(strcmp(tmp->value->id,quad->result->sym->name)==0){ t->result->val = tmp->key; t->result->type = userfunc_a; break; } tmp=tmp->next; } } else{ make_operand(quad->result, t->result); } emit_vmarg(t);}
void generate_GETRETVAL(quad* quad) { quad->address = instruction_line; instruction t; t = (instruction)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg));
t->srcLine = quad->srcLine; t->address = nextinstructionlabel(); t->opcode = assign_v; make_operand(quad->result,t->result); make_retvaloperand(t->arg1); emit_vmarg(t);}
void generate_FUNCSTART(quad* quad){
instruction* t,*t1; t1 = (instruction*)malloc(sizeof(instruction)); t1->result = (vmarg*)malloc(sizeof(vmarg)); t1->arg1 = (vmarg*)malloc(sizeof(vmarg)); t1->arg2 = (vmarg*)malloc(sizeof(vmarg)); t1->arg1 = NULL; t1->arg2 = NULL; t1->srcLine = quad->srcLine; t1->address = nextinstructionlabel(); t1->opcode = jump_v; emit_vmarg(t1); t = (instruction*)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->srcLine = quad->srcLine; t->address = instruction_line; quad->result->sym->address = instruction_line; quad->address = nextinstructionlabel(); t->arg1 = NULL; t->arg2 = NULL; t->opcode = funcenter_v; make_operand(quad->result, t->result); emit_vmarg(t); functStackHead = push_function_list(totalUserFuncs); functStackHead->return_l = (return_list*)malloc(sizeof(return_list)); functStackHead->return_l->taddress = instruction_line-2; functStackHead->return_l->next = NULL; totalUserFuncs++;}
void generate_RETURN(quad* quad){ instruction* t,t1; t = (instruction)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = NULL;
quad->address = instruction_line; t->address = nextinstructionlabel(); t->srcLine = quad->srcLine; t->opcode = assign_v; make_retvaloperand(t->result); if(quad->result->type != programfunc_e){ make_operand(quad->result,t->arg1); } else{ userfuncTable* tmp = userFuncs; while(tmp!=NULL){ printf("mphka edw!!!\n"); printf("name func = %s\t",tmp->value->id); printf("scope func = %d\t",tmp->value->scope); printf("name search = %s\t",quad->result->sym->name); printf("scope func = %d\n",quad->result->sym->scope); if(strcmp(tmp->value->id,quad->result->sym->name)==0 && quad->result->sym->scope == tmp->value->scope){ t->arg1->type = userfunc_a; t->arg1->val = tmp->key; break; } tmp = tmp->next; } } emit_vmarg(t); if(functStackHead->return_l == NULL){ functStackHead->return_l = (return_list*)malloc(sizeof(return_list)); functStackHead->return_l->taddress = instruction_line; functStackHead->return_l->next = NULL; } else{ return_list* tmp = (return_list*)malloc(sizeof(return_list)); tmp->taddress = instruction_line; tmp->next = functStackHead->return_l; functStackHead->return_l = tmp; } //printreturnstack(); t1 = (instruction*)malloc(sizeof(instruction)); t1->result = (vmarg*)malloc(sizeof(vmarg)); t1->arg1 = (vmarg*)malloc(sizeof(vmarg)); t1->arg2 = (vmarg*)malloc(sizeof(vmarg)); t1->arg1 = NULL; t1->arg2 = NULL; t1->opcode = jump_v; t->srcLine = quad->srcLine; t1->address = nextinstructionlabel(); t1->result->type = label_a; emit_vmarg(t1);}
// thelei ftiaksimo giati exei mesa merikh apotimish void generate_FUNCEND(quad* quad){ instruction* in = instructions; printreturnstack(); func_stack* tmp = pop_function_list();
instruction* t; t = (instruction*)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = NULL; t->arg2 = NULL; t->opcode = funcexit_v; t->result->type = userfunc_a; t->result->val = tmp->index; //printreturnstack(); while(tmp->return_l->next != NULL){ in = in+tmp->return_l->taddress; in->result->val = instruction_line; tmp->return_l = tmp->return_l->next; in = instructions; } in = in+tmp->return_l->taddress; in->result->val = instruction_line+1; t->srcLine = quad->srcLine; t->address = instruction_line; quad->address = nextinstructionlabel(); emit_vmarg(t);}
void generate_UMINUS(quad* quad){ instruction* t; t = (instruction*)malloc(sizeof(instruction)); t->result = (vmarg*)malloc(sizeof(vmarg)); t->arg1 = (vmarg*)malloc(sizeof(vmarg)); t->arg2 = (vmarg*)malloc(sizeof(vmarg));
t->opcode = mul_v; make_operand(quad->result,t->result); consts_newnumber(-1); t->arg1->type = number_a; t->arg1->val = totalNumConsts++; make_operand(quad->arg1,t->arg2); t->srcLine = quad->srcLine; t->address = instruction_line; quad->address = nextinstructionlabel(); emit_vmarg(t);}
- Assistant
void generate_OR(quad* quad) { quad->address = instruction_line; instruction t = (instruction)malloc(sizeof(instruction)), t1 = (instruction)malloc(sizeof(instruction)), t2 = (instruction)malloc(sizeof(instruction)), t3 = (instruction)malloc(sizeof(instruction)), t4 = (instruction)malloc(sizeof(instruction));
make_operand(quad->arg1, t->arg1); make_booloperand(t->arg2, 1); t->result = (vmarg*)malloc(sizeof(vmarg)); t->result->type = label_a; t->result->val = nextinstructionlabel() + 4; t->address = instruction_line; t->opcode = jeq_v; t->srcLine = quad->srcLine; emit_vmarg(t); make_operand(quad->arg2, t1->arg1); make_booloperand(t1->arg2, 1); t1->result = (vmarg*)malloc(sizeof(vmarg)); t1->result->type = label_a; t1->result->val = nextinstructionlabel() + 3; t1->address = instruction_line; t1->opcode = jeq_v; t1->srcLine = quad->srcLine; emit_vmarg(t1); t2->result = (vmarg*)malloc(sizeof(vmarg)); t2->result->type = label_a; t2->result->val = nextinstructionlabel() + 2; t2->address = nextinstructionlabel(); t2->opcode = assign_v; make_booloperand(t2->arg1, 0); make_operand(quad->result, t2->result); t3->result = (vmarg*)malloc(sizeof(vmarg)); t3->result->type = label_a; t3->result->val = nextinstructionlabel() + 1; t3->address = instruction_line; t3->opcode = jump_v; t4->result = (vmarg*)malloc(sizeof(vmarg)); t4->result->type = label_a; t4->result->val = nextinstructionlabel(); t4->address = nextinstructionlabel(); t4->opcode = assign_v; make_booloperand(t4->arg1, 1); make_operand(quad->result, t4->result); emit_vmarg(t2); emit_vmarg(t3); emit_vmarg(t4);}
void generate_AND(quad* quad) { quad->address = instruction_line; instruction t = (instruction)malloc(sizeof(instruction)), t1 = (instruction)malloc(sizeof(instruction)), t2 = (instruction)malloc(sizeof(instruction)), t3 = (instruction)malloc(sizeof(instruction)), t4 = (instruction)malloc(sizeof(instruction));
make_operand(quad->arg1, t->arg1); make_booloperand(t->arg2, 0); t->result = (vmarg*)malloc(sizeof(vmarg)); t->result->type = label_a; t->result->val = nextinstructionlabel() + 4; t->address = instruction_line; t->opcode = jeq_v; t->srcLine = quad->srcLine; emit_vmarg(t); make_operand(quad->arg2, t1->arg1); make_booloperand(t1->arg2, 0); t1->result = (vmarg*)malloc(sizeof(vmarg)); t1->result->type = label_a; t1->result->val = nextinstructionlabel() + 3; t1->address = instruction_line; t1->opcode = jeq_v; t1->srcLine = quad->srcLine; emit_vmarg(t1); t2->result = (vmarg*)malloc(sizeof(vmarg)); t2->result->type = label_a; t2->result->val = nextinstructionlabel() + 1; t2->address = nextinstructionlabel(); t2->opcode = assign_v; make_booloperand(t2->arg1, 1); make_operand(quad->result, t2->result); t3->result = (vmarg*)malloc(sizeof(vmarg)); t3->result->type = label_a; t3->result->val = nextinstructionlabel(); t3->address = instruction_line; t3->opcode = jump_v; t4->result = (vmarg*)malloc(sizeof(vmarg)); t4->result->type = label_a; t4->result->val = nextinstructionlabel(); t4->address = nextinstructionlabel(); t4->opcode = assign_v; make_booloperand(t4->arg1, 0); make_operand(quad->result, t4->result); emit