void generate_FUNCSTART(quad* quad){ struct instruction t0; t0.arg1.type = nil_a; t0.arg2.type = nil_a; t0.result.type= label_a; t0.result.val = 0; t0.opcode = jump_v; add_func_jump(nextinstructionlabel()); emit_instruction(&t0); struct userfunc* f; f = (struct userfunc*)malloc(sizeof(struct userfunc)); f->id=quad->result->sym->name; f->address = nextinstructionlabel(); quad->taddress = nextinstructionlabel(); userfuncs_newfunc(quad->result->sym); //added 6/5 ore (userFuncs+totalUserFuncs)->address = f->address; (userFuncs+totalUserFuncs)->id = f->id; (userFuncs+totalUserFuncs)->localSize = quad->result->sym->totallocals; f->index = totalUserFuncs-1; funcpush(f); struct instruction t; totalUserFuncs++; t.arg1.type = nil_a; t.arg2.type = nil_a; t.opcode = funcenter_v; make_operand(quad->result, &t.result); t.result.val = totalUserFuncs-1; //EDW emit_instruction(&t); }
void generate_relational(iopcode op, quad *q) { instruction t; init_instruction(&t); t.opcode = op; // # WARNING t.srcLine = q->line; make_operand(q->arg1, t.arg1); make_operand(q->arg2, t.arg2); t.result->type = label_a; if(q->label < currproccessedquad()) { t.result->val = quads[q->label].taddress; } else { add_incomplete_jump(nextinstructionlabel(), q->label); } q->taddress = nextinstructionlabel(); emit_icode(t); }
void generate_OR (quad* quad) { quad->taddress = nextinstructionlabel(); struct instruction t; t.opcode = jeq_v; make_operand(quad->arg1, &t.arg1); make_booloperand(&t.arg2, '1'); t.result.type = label_a; t.result.val = nextinstructionlabel()+4; emit_instruction(&t); make_operand(quad->arg2, &t.arg1); t.result.val = nextinstructionlabel()+3; emit_instruction(&t); t.opcode = assign_v; make_booloperand(&t.arg1, '0'); reset_operand(&t.arg2); make_operand(quad->result, &t.result); emit_instruction(&t); t.opcode = jump_v; reset_operand (&t.arg1); reset_operand(&t.arg2); t.result.type = label_a; t.result.val = nextinstructionlabel()+2; emit_instruction(&t); t.opcode = assign_v; make_booloperand(&t.arg1, '1'); reset_operand(&t.arg2); make_operand(quad->result, &t.result); emit_instruction(&t); }
void generate_AND(quad *q) { q->taddress = nextinstructionlabel(); instruction t1, t2, t3, t4, t5; init_instruction(&t1); init_instruction(&t2); init_instruction(&t3); init_instruction(&t4); init_instruction(&t5); t1.srcLine = q->line; t1.opcode = ieq_v; make_operand(q->arg1, t1.arg1); make_booloperand(t1.arg2, 0); t1.result->type = label_a; t1.result->val = nextinstructionlabel()+4; emit_icode(t1); t2.opcode = ieq_v; t2.srcLine = q->line; make_booloperand(t2.arg2, 0); make_operand(q->arg2, t2.arg1); t2.result->type = label_a; t2.result->val = nextinstructionlabel()+3; emit_icode(t2); t3.opcode = assign_v; t3.srcLine = q->line; make_booloperand(t3.arg1, 1); make_operand(q->result, t3.result); emit_icode(t3); t4.opcode = jump_v; t4.srcLine = q->line; t4.result->type = label_a; t4.result->val = nextinstructionlabel()+2; emit_icode(t4); t5.opcode = assign_v; t5.srcLine = q->line; make_booloperand(t5.arg1, 0); make_operand(q->result, t5.result); emit_icode(t5); }
void generate_FUNCEND(quad* quad){ struct userfunc* f; f=funcpop(funcstack); patch_returns(f->returnList, nextinstructionlabel()); quad->taddress = nextinstructionlabel(); struct instruction t; t.opcode = funcexit_v; t.arg1.type = nil_a; t.arg2.type = nil_a; make_operand(quad->result, &t.result); t.result.val = f->index; patch_func_jump(quad->taddress); emit_instruction(&t); }
void generate_relational (int op,quad* quad) { struct instruction t; t.opcode = op; // printf("arg1:%d,arg2:%d\n", quad->arg1->boolConst,quad->arg2->boolConst); make_operand(quad->arg1, &t.arg1); make_operand(quad->arg2, &t.arg2); t.result.type = label_a; if (quad->label < currprocessedquad()) t.result.val = (quads+quad->label)->taddress; else add_incomplete_jump(nextinstructionlabel(), quad->label); quad->taddress = nextinstructionlabel(); emit_instruction(&t); }
void generate_FUNCEND(quad *q) { SymbolTableEntry *f = pop_func(funcstack); backpatch_returnList(f->returnList, nextinstructionlabel()); q->taddress = nextinstructionlabel(); instruction t; init_instruction(&t); t.srcLine = q->line; t.opcode = funcexit_v; make_operand(q->arg1, t.arg1); emit_icode(t); }
void generate_GETRETVAL(quad* quad) { quad->taddress = nextinstructionlabel(); struct instruction t; t.opcode = assign_v; make_operand(quad->result, &t.result); make_retvaloperand(&t.arg1); emit_instruction(&t); }
void generate_CALL(quad* quad) { quad->taddress = nextinstructionlabel(); struct instruction t; t.opcode = call_v; t.arg1.type = nil_a; t.arg2.type = nil_a; make_operand(quad->result, &t.result); emit_instruction(&t); }
void generate_FUNCSTART(quad *q) { SymbolTableEntry *f = q->arg1->sym; f->iaddress = nextinstructionlabel(); q->taddress = nextinstructionlabel(); userfuncs_newfunc(f); push_func(funcstack, f); instruction t; init_instruction(&t); t.srcLine = q->line; t.opcode = funcenter_v; make_operand(q->arg1, t.arg1); emit_icode(t); }
void generate_instr(enum vmopcode op,quad* quad){ struct instruction t; t.opcode = op; if(op==23){ printf("element:%lf,type:%d\n",quad->arg1->numConst,quad->arg1->type ); } make_operand(quad->arg1, &(t.arg1)); make_operand(quad->arg2, &(t.arg2)); make_operand(quad->result, &(t.result)); quad->taddress = nextinstructionlabel(); emit_instruction(&t); }
void generate_GETRETVAL(quad *q) { q->taddress = nextinstructionlabel(); instruction t; init_instruction(&t); t.srcLine = q->line; t.opcode = assign_v; make_operand(q->arg1, t.arg1); make_retvaloperand(t.result); emit_icode(t); }
void generate_CALL(quad *q) { q->taddress = nextinstructionlabel(); instruction t; init_instruction(&t); t.srcLine = q->line; t.opcode = call_v; make_operand(q->arg1, t.arg1); emit_icode(t); }
void generate_RETURN(quad* quad){ quad->taddress = nextinstructionlabel(); struct instruction t; t.opcode = assign_v; make_retvaloperand(&t.result); t.arg2.type=nil_a; make_operand(quad->arg1, &t.arg1); emit_instruction(&t); struct userfunc* f; f = functop(funcstack); f->returnList = (struct userfunc*)malloc(sizeof(struct userfunc)); append(f->returnList, nextinstructionlabel()); t.opcode = jump_v; reset_operand(&t.arg1); t.arg1.type = nil_a; reset_operand(&t.arg2); t.result.type = label_a; //printf("%d LABEL_A\n",f->returnList->address); emit_instruction(&t); }
void generate_RETURN(quad *q) { q->taddress = nextinstructionlabel(); instruction t1, t2; init_instruction(&t1); init_instruction(&t2); t1.srcLine = q->line; t1.opcode = assign_v; make_retvaloperand(t1.result); make_operand(q->arg1, t1.arg1); emit_icode(t1); SymbolTableEntry *f = top_func(funcstack); append(f->returnList, nextinstructionlabel()); t2.opcode = jump_v; t2.srcLine = q->line; t2.result->type = label_a; emit_icode(t2); }
void generate(iopcode op, quad *q) { instruction t; init_instruction(&t); t.opcode = op; // # WARNING t.srcLine = q->line; make_operand(q->arg1, t.arg1); make_operand(q->arg2, t.arg2); make_operand(q->result, t.result); q->taddress = nextinstructionlabel(); emit_icode(t); }
void printInstructions(){ struct instruction* temp = NULL; temp = instructions; unsigned int i=0; printf("\n^^^^^^^^^^^^^ Start printing Instruction Table ^^^^^^^^^^^^^\n"); while(temp <= (instructions+nextinstructionlabel()-1)){ printf("%d:",i); printInstrOp(temp->opcode); if(&temp->arg1){ printVMarg(&temp->arg1); } if(&temp->arg2){ printVMarg(&temp->arg2); } if(&temp->result){ printVMarg(&temp->result); } i++; temp = instructions+i; printf("\n"); } }
void printInstructions_tofile(FILE* fp){ struct instruction* temp = NULL; temp = instructions; unsigned int i=0; fprintf(fp,"total_instructions %d\n",currInstruction+1); printf("total instr %d\n",currInstruction); while(temp <= (instructions+nextinstructionlabel()-1)){ printInstrOp_tofile(temp->opcode,fp); if(&temp->arg1){ printVMarg_tofile(&temp->arg1,fp); } if(&temp->arg2){ printVMarg_tofile(&temp->arg2,fp); } if(&temp->result){ printVMarg_tofile(&temp->result,fp); } i++; temp = instructions+i; fprintf(fp,"\n"); } }