static void param_list() { int seq = 0; do { insert_param(param(),seq,¤t_function->u.f.param_head); seq++; if(token == TOK_COMMA) match(TOK_COMMA); else break; }while(true); }
custom_wave_t * new_custom_wave(int id) { custom_wave_t * custom_wave; param_t * param; if ((custom_wave = (custom_wave_t*)malloc(sizeof(custom_wave_t))) == NULL) return NULL; custom_wave->id = id; custom_wave->per_frame_count = 0; custom_wave->samples = 512; custom_wave->bSpectrum = 0; custom_wave->enabled = 1; custom_wave->sep = 1; custom_wave->smoothing = 0.0; custom_wave->bUseDots = 0; custom_wave->bAdditive = 0; custom_wave->r = custom_wave->g = custom_wave->b = custom_wave->a = 0.0; custom_wave->scaling = 1.0; custom_wave->per_frame_eqn_string_index = 0; custom_wave->per_frame_init_eqn_string_index = 0; custom_wave->per_point_eqn_string_index = 0; custom_wave->r_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->g_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->b_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->a_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->x_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->y_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->value1 = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->value2 = malloc(MAX_SAMPLE_SIZE*sizeof(double)); custom_wave->sample_mesh = malloc(MAX_SAMPLE_SIZE*sizeof(double)); /* Initialize tree data structures */ if ((custom_wave->param_tree = create_splaytree(compare_string, copy_string, free_string)) == NULL) { free_custom_wave(custom_wave); return NULL; } if ((custom_wave->per_point_eqn_tree = create_splaytree(compare_int, copy_int, free_int)) == NULL) { free_custom_wave(custom_wave); return NULL; } if ((custom_wave->per_frame_eqn_tree = create_splaytree(compare_int, copy_int, free_int)) == NULL) { free_custom_wave(custom_wave); return NULL; } if ((custom_wave->init_cond_tree = create_splaytree(compare_string, copy_string, free_string)) == NULL) { free_custom_wave(custom_wave); return NULL; } if ((custom_wave->per_frame_init_eqn_tree = create_splaytree(compare_string, copy_string, free_string)) == NULL) { free_custom_wave(custom_wave); return NULL; } /* Start: Load custom wave parameters */ if ((param = new_param_double("r", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->r, custom_wave->r_mesh, 1.0, 0.0, .5)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("g", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->g, custom_wave->g_mesh, 1.0, 0.0, .5)) == NULL){ free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("b", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->b, custom_wave->b_mesh, 1.0, 0.0, .5)) == NULL){ free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("a", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->a, custom_wave->a_mesh, 1.0, 0.0, .5)) == NULL){ free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("x", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->x, custom_wave->x_mesh, 1.0, 0.0, .5)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("y", P_FLAG_DONT_FREE_MATRIX | P_FLAG_PER_POINT, &custom_wave->y, custom_wave->y_mesh, 1.0, 0.0, .5)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_bool("enabled", P_FLAG_NONE, &custom_wave->enabled, 1, 0, 0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_int("sep", P_FLAG_NONE, &custom_wave->sep, 100, -100, 0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_bool("bSpectrum", P_FLAG_NONE, &custom_wave->bSpectrum, 1, 0, 0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_bool("bDrawThick", P_FLAG_NONE, &custom_wave->bDrawThick, 1, 0, 0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_bool("bUseDots", P_FLAG_NONE, &custom_wave->bUseDots, 1, 0, 0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_bool("bAdditive", P_FLAG_NONE, &custom_wave->bAdditive, 1, 0, 0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_int("samples", P_FLAG_NONE, &custom_wave->samples, 2048, 1, 512)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("sample", P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX | P_FLAG_ALWAYS_MATRIX | P_FLAG_PER_POINT, &custom_wave->sample, custom_wave->sample_mesh, 1.0, 0.0, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { printf("failed to insert sample\n"); free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("value1", P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX | P_FLAG_ALWAYS_MATRIX | P_FLAG_PER_POINT, &custom_wave->v1, custom_wave->value1, 1.0, -1.0, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("value2", P_FLAG_READONLY | P_FLAG_DONT_FREE_MATRIX | P_FLAG_ALWAYS_MATRIX | P_FLAG_PER_POINT, &custom_wave->v2, custom_wave->value2, 1.0, -1.0, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("smoothing", P_FLAG_NONE, &custom_wave->smoothing, NULL, 1.0, 0.0, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("scaling", P_FLAG_NONE, &custom_wave->scaling, NULL, MAX_DOUBLE_SIZE, 0.0, 1.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t1", P_FLAG_PER_POINT | P_FLAG_TVAR, &custom_wave->t1, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t2", P_FLAG_PER_POINT |P_FLAG_TVAR, &custom_wave->t2, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t3", P_FLAG_PER_POINT |P_FLAG_TVAR, &custom_wave->t3, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t4", P_FLAG_PER_POINT |P_FLAG_TVAR, &custom_wave->t4, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t5", P_FLAG_TVAR, &custom_wave->t5, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t6", P_FLAG_TVAR | P_FLAG_PER_POINT, &custom_wave->t6, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t7", P_FLAG_TVAR | P_FLAG_PER_POINT, &custom_wave->t7, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } if ((param = new_param_double("t8", P_FLAG_TVAR | P_FLAG_PER_POINT, &custom_wave->t8, NULL, MAX_DOUBLE_SIZE, -MAX_DOUBLE_SIZE, 0.0)) == NULL) { free_custom_wave(custom_wave); return NULL; } if (insert_param(param, custom_wave->param_tree) < 0) { free_custom_wave(custom_wave); return NULL; } /* End of parameter loading. Note that the read only parameters associated with custom waves (ie, sample) are global variables, and not specific to the custom wave datastructure. */ return custom_wave; }
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; } } }