/* might as well make this public since the assertion stuff seemed be using it anyways */ char *dispmon_parse(const char *fmt, int nump, int start, const char *unit) { FmtArray *f=parse_fmt(fmt); size_t i=0, pno=start; RStr *output=RStr_new(256); char *s; for(i=0; i<f->used && output; i++) { if(f->f[i]->text) expanding_strcat(output,f->f[i]->text); else { /* should not need this check if checktf errors */ if(pno>nump) { //cDispmon(modulename, MON_ERR, // "insufficient parameters for format string \"%s\" in " // "$dispmon/$writemon", fmt); RStr_delete(output); output = NULL; } else pno=insert_var(output,f->f[i],pno); } } FmtArray_delete(f); s = output ? output->str : 0; free(output); return s; }
void send_temp(int num,Operand a,FILE *wf) { int result = param_num(a); if(result < 0) { result = var_num(a); if(result < 0 ) { //var_add(4); var_total_num = var_total_num + 4; insert_var(a,var_total_num); fprintf(wf,"\tsw $t%d, %d($fp)\n",num, -var_total_num); } else fprintf(wf,"\tsw $t%d, %d($fp)\n",num, -result); } else { fprintf(wf,"\tlw $t%d, %d($sp)\n",num , result * 4); } }
void create_code(FILE *wf) { fprintf(wf, ".data\n"); fprintf(wf, "_prompt: .asciiz \"Enter an integer:\"\n"); fprintf(wf, "_ret: .asciiz \"\\n\"\n"); fprintf(wf, ".globl main\n"); fprintf(wf, ".text\n"); fprintf(wf, "read:\n"); fprintf(wf, "\tli $v0, 4\n"); fprintf(wf, "\tla $a0, _prompt\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tli $v0, 5\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tjr $ra\n"); fprintf(wf, "\n"); fprintf(wf, "write:\n"); fprintf(wf, "\tli $v0, 1\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tli $v0, 4\n"); fprintf(wf, "\tla $a0, _ret\n"); fprintf(wf, "\tsyscall\n"); fprintf(wf, "\tmove $v0, $0\n"); fprintf(wf, "\tjr $ra\n"); InterCode * temp = InterCodeHead; int num = 1; int result = 0; int op1 = 0; int op2 = 0; int arg_num = 0; InterCode * argpos = NULL; int i = 0; for(; temp != NULL ; temp = temp->next) { switch(temp->kind) { case ASSIGN: get_temp(0, temp->u.assign.right, wf); //fprintf(wf, "\tmove $t1, $t0"); send_temp(0, temp->u.assign.left, wf); break; case ADDR: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf,"\tsub $t2, $t0, $t1\n"); fprintf(wf,"\taddi $t2, $t2, -4\n"); send_temp(2,temp->u.addr.result,wf); break; case MEMREAD: get_temp(0, temp->u.assign.right, wf); fprintf(wf,"\tlw $t1, 0($t0)\n"); send_temp(1,temp->u.assign.left,wf); break; case MEMWRITE: get_temp(0, temp->u.assign.right, wf); get_temp(1, temp->u.assign.left, wf); fprintf(wf,"\tsw $t0, 0($t1)\n"); break; case ADD: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tadd $t2, $t1, $t0\n"); send_temp(2, temp->u.addr.result, wf); break; case SUB: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tsub $t2, $t0, $t1\n"); send_temp(2, temp->u.addr.result, wf); break; case MUL: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tmul $t2, $t1, $t0\n"); send_temp(2, temp->u.addr.result, wf); break; case DIV: get_temp(0, temp->u.addr.op1, wf); get_temp(1, temp->u.addr.op2, wf); fprintf(wf, "\tdiv $t0, $t1\n"); fprintf(wf, "\tmflo $t2\n"); send_temp(2, temp->u.addr.result, wf); break; case LABEL: fprintf(wf,"label%d :\n",temp->u.label.label); break; case FUNCTION: fprintf(wf,"%s:\n",temp->u.func.name); if(strcmp(temp->u.func.name,"main") == 0) { fprintf(wf,"\tmove $fp, $sp\n"); fprintf(wf,"\taddi $sp, $sp, -2048\n"); fprintf(wf, "\tli $t0, 8\n"); fprintf(wf,"\tsw $t0, 4($sp)\n"); } param_total_num = 0; break; case GOTO: fprintf(wf,"\tj label%d\n",temp->u.label.label); break; case IF: get_temp(0, temp->u.ifstruct.left, wf); get_temp(1, temp->u.ifstruct.right, wf); switch(temp->u.ifstruct.relop) { case 3: fprintf(wf,"\tbne "); break; case 4: fprintf(wf,"\tbeq "); break; case 5: fprintf(wf,"\tblt "); break; case 6: fprintf(wf,"\tbgt "); break; case 7: fprintf(wf,"\tbge "); break; case 8: fprintf(wf,"\tble "); break; default: fprintf(stdout,"error\n"); break; } fprintf(wf,"$t0, $t1, label%d\n", temp->u.ifstruct.label); break; case RETURN: get_temp(0, temp->u.arg.result, wf); fprintf(wf, "\tmove $v0, $t0\n"); fprintf(wf, "\tjr $ra\n"); break; case DEC: var_total_num = var_total_num + 4; insert_var(temp->u.dec.dec,var_total_num); fprintf(wf,"\tmove $t0, $fp\n"); fprintf(wf,"\taddi $t0, $t0, %d\n",-var_total_num); fprintf(wf,"\tsw $t0, %d($fp)\n",-var_total_num); var_total_num = var_total_num + temp->u.dec.size; break; case ARG: fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf,"\taddi $sp, $sp, %d\n",(param_total_num + 2) * 4); //printf("param_total_num----%d\n",param_total_num); argpos = temp; arg_num = 1; for( ; argpos->next->kind == ARG; argpos = argpos->next) { arg_num ++; } i = arg_num; for( ; temp->next->kind == ARG; temp = temp->next) { get_temp(0, temp->u.arg.result, wf); printf("arg: %d\n",i); fprintf(wf,"\tsw $t0, %d($sp)\n", i * 4); i --; } get_temp(0, temp->u.arg.result, wf); //printf("arg: %d\n",i); fprintf(wf,"\tsw $t0, %d($sp)\n", i * 4); //printf("arg_num: %d\n",arg_num); fprintf(wf,"\tli $t0, %d\n", (arg_num + 2) * 4); fprintf(wf,"\tsw $t0, %d($sp)\n", (arg_num + 1) * 4); temp = temp->next; //printf("arg_num: %d----%s\n",arg_num,temp->u.callFunc.func); fprintf(wf, "\tjal %s\n",temp->u.callFunc.func); fprintf(wf, "\tlw $t0, -4($sp)\n"); fprintf(wf,"\tsub $sp, $sp, $t0\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); fprintf(wf, "\tmove $t0, $v0\n"); send_temp(0, temp->u.callFunc.result,wf); break; case CALL: //printf("%d",num); fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf, "\tli $t0, 8\n"); fprintf(wf,"\tsw $t0, 4($sp)\n"); fprintf(wf,"\tadd $sp, $sp, $t0\n"); fprintf(wf, "\tjal %s\n",temp->u.callFunc.func); fprintf(wf, "\tlw $t0, -4($sp)\n"); fprintf(wf,"\tsub $sp, $sp, $t0\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); fprintf(wf, "\tmove $t0, $v0\n"); send_temp(0, temp->u.callFunc.result,wf); break; case PARAM: num = 1; param_total_num = 1; for( ; temp->next->kind == PARAM; temp = temp->next) { param_total_num ++; insert_param(temp->u.arg.result, num); num ++; } insert_param(temp->u.arg.result, num); num ++; break; case READ: fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf,"\taddi $sp, $sp, 4\n"); fprintf(wf, "\tjal read\n"); fprintf(wf,"\tsub $sp, $sp, 4\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); fprintf(wf, "\tmove $t0, $v0\n"); send_temp(0, temp->u.arg.result,wf); break; case WRITE: fprintf(wf,"\tsw $ra, 0($sp)\n"); fprintf(wf,"\taddi $sp, $sp, 4\n"); get_temp(0, temp->u.arg.result, wf); fprintf(wf,"\tmove $a0, $t0\n"); fprintf(wf, "\tjal write\n"); fprintf(wf,"\taddi $sp, $sp, -4\n"); fprintf(wf, "\tlw $ra, 0($sp)\n"); break; default: break; } } }
void and_var(bfun* b, int var) { for (cube* c = b->begin; c != NULL; c = c->next) { insert_var (c, var); } }