void generate_RETURN(quad *quad){ quad->taddress = nextInstructionLabel(); instruction *t = (instruction *)malloc(sizeof(instruction)); instruction *t1 = (instruction *)malloc(sizeof(instruction)); t->arg1 = malloc(sizeof(vmarg)); t->result = malloc(sizeof(vmarg)); t1->result = malloc(sizeof(vmarg)); funcs *fun; if(quad->result!=NULL){ t->opcode = assign_v; make_retvaloperand(t->result); make_operand(quad->result, t->arg1); reset_operand(t->arg2); t->srcLine = quad->line; emitVmarg(t); } fun = topSym(); fun->nodeR = appendR(fun->nodeR,nextInstructionLabel()); t1->opcode = jump_v; reset_operand(t1->arg1); reset_operand(t1->arg2); t->arg1 = malloc(sizeof(vmarg)); make_retvaloperand(t1->result); (t1->result)->type = label_a; t1->srcLine = quad->line; emitVmarg(t1); }
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_FUNCSTART( quad* quad ){ Symbol *funSymbol = malloc( sizeof( Symbol ) ); funSymbol = createS( quad->result->sym->name, 0, 0, quad->result->sym->name, 0, 0, "func", 0, 0, 0); instruction *t = malloc( sizeof( instruction ) ); t->opcode = jump_v; reset_operand(t->arg1); reset_operand(t->arg2); t->result = make_vmarg(); (t->result)->type = label_a; (t->result)->val = nextInstructionLabel(); t->srcLine = quad->line; emitVmarg( t ); funSymbol = (quad->result)->sym; funSymbol->taddress = nextInstructionLabel(); quad->taddress = nextInstructionLabel(); funSymbol->line = 0; AddFunctionUserElem(funSymbol); pushSym(funSymbol); instruction *t1 = (instruction *)malloc(sizeof(instruction)); t1->result = (vmarg *)malloc(sizeof(vmarg)); t1->opcode = funcenter_v; make_operand(quad->result, t1->result); reset_operand(t1->arg1); reset_operand(t1->arg2); t1->srcLine = quad->line; emitVmarg(t1); }
void generate_PARAM(quad *quad){ quad->taddress = nextInstructionLabel(); instruction *t = (instruction *)malloc(sizeof(instruction)); t->result = malloc(sizeof(vmarg)); t->opcode = pusharg_v; make_operand(quad->result, t->result); reset_operand(t->arg1); reset_operand(t->arg2); t->srcLine = quad->line; emitVmarg(t); }
void generate_AND( quad *quad ){ instruction *t = InitInstruction(); instruction *t1 = InitInstruction(); instruction *t2 = InitInstruction(); instruction *t3 = InitInstruction(); instruction *t4 = InitInstruction(); quad->taddress = nextInstructionLabel(); 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; emitVmarg( t ); t1->opcode = jeq_v; make_operand(quad->arg2,t1->arg1); make_booloperand(t1->arg2,0); t1->result = make_vmarg(); (t1->result)->val = nextInstructionLabel()+3; (t1->result)->type = label_a; emitVmarg( t1 ); t2->opcode = assign_v; make_booloperand(t2->arg1,1); t2->arg2 = reset_operand(); make_operand(quad->result,t2->result); t2->srcLine = quad->line; emitVmarg(t2); t3->opcode = jump_v; t3->arg1 = reset_operand(); t3->arg2 = reset_operand(); t3->result = make_vmarg(); (t3->result)->type = label_a; (t3->result)->val = nextInstructionLabel()+2; t3->srcLine = quad->line; emitVmarg(t3); t4->opcode = assign_v; make_booloperand(t4->arg1,0); t4->arg2 = reset_operand(); make_operand(quad->result,t4->result); t4->srcLine = quad->line; emitVmarg(t4); return; }
void generate_GETRETVAL( quad* quad ){ quad->taddress = nextInstructionLabel(); instruction *t = malloc( sizeof( instruction ) ); t->result = malloc(sizeof(vmarg)); t->opcode = getretval_v; make_operand( quad->result ,t->result); reset_operand(t->arg1);/////////Vervelak XXX reset_operand(t->arg2); t->srcLine = quad->line; emitVmarg( t ); return; }
void generate_NOT( quad *quad ){ quad->taddress = nextInstructionLabel(); instruction *t = InitInstruction(); instruction *t1 = InitInstruction(); instruction *t2 = InitInstruction(); instruction *t3 = InitInstruction(); t->opcode = jeq_v; make_operand( quad->arg1, t->arg1 ); make_booloperand( t->arg2, 1 ); t->result->type = label_a; t->result->val = nextInstructionLabel()+3; emitVmarg( t ); t1->opcode = assign_v; make_booloperand( t1->arg1, 1 ); //reset_operand( t->arg2 ); make_operand( quad->result, t1->result ); emitVmarg( t1 ); t2->opcode = jump_v; //reset_operand( t1->arg1 ); //reset_operand( t1->arg2 ); t2->result->type = label_a; t2->result->val = nextInstructionLabel()+2; emitVmarg( t2 ); t2->opcode = assign_v; make_booloperand( t2->arg1, 0 ); reset_operand( t2->arg2 ); make_operand( quad->result, t2->result ); emitVmarg( t2 ); }
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_FUNCEND(quad *quad){ funcs *fun; instruction *t = (instruction *)malloc(sizeof(instruction)); t->result = (vmarg *)malloc(sizeof(vmarg)); fun = popSym(); patchReturnList(fun->nodeR,nextInstructionLabel()); (VmargsTable[fun->line].result)->val = nextInstructionLabel()+1; quad->taddress = nextInstructionLabel(); t->opcode = funcexit_v; make_operand(quad->result, t->result); reset_operand(t->arg1); reset_operand(t->arg2); t->srcLine = quad->line; t->result->val = quad->line; //fun->sym->line = quad->result->lvar; -- //quad->faddress = fun->sym->taddress; t->result->val = fun->sym->taddress; // -- patchLocals(fun->sym->taddress, quad->result->lvar); //AddFunctionUserElem(fun->sym); emitVmarg(t); }
void generate_CALL( quad* quad ){ quad->taddress = nextInstructionLabel(); ///print///printf("QUAD FUNC NUM: %d",quad->line ); instruction *t = malloc( sizeof( instruction ) ); t->result = malloc(sizeof(vmarg)); t->opcode = call_v; make_operand( quad->result, t->result ); reset_operand(t->arg1); reset_operand(t->arg2); t->srcLine = quad->line; t->result->val = FindFunctionUser((quad->result->sym)->name,deepestFunctionScope); if (t->result->val == -1){ if(isLibFunction((quad->result->sym)->name)){ int check = FindLibFunction((quad->result->sym)->name); if (check == -1){ instruction *t1 =(instruction *) InitInstruction(); expr * e = malloc(sizeof(expr)); e->type = libraryfunc_e; e->sym =createS( (quad->result->sym)->name, 0, 0, (quad->result->sym)->name, 0, 0, "lib_func", 1,currscopespace(),currscopeoffset()); make_operand(e,t1->arg1); } t->result->val = FindLibFunction((quad->result->sym)->name); t->result->type = libfunc_a; emitVmarg( t ); } } else { emitVmarg( t ); } return; }