diff --git a/Lab/Code/Makefile b/Lab/Code/Makefile index 90be048..dadb353 100644 --- a/Lab/Code/Makefile +++ b/Lab/Code/Makefile @@ -1,6 +1,12 @@ -.PHONY:run, git +filename := ../Test/test.cmm +.PHONY:run, git, com run: parser - ./parser ../Test/test.cmm out.ir + ./parser $(filename) out.s +com: run + spim -stext 4000000 -file out.s > ans.out + vimdiff ans.out $(ansname) +haha: + ../parser $(filename) out.ir # GNU make手册:http://www.gnu.org/software/make/manual/make.html # ************ 遇到不明白的地方请google以及阅读手册 ************* diff --git a/Lab/Code/intercode.c b/Lab/Code/intercode.c index 6443f87..e22531a 100644 --- a/Lab/Code/intercode.c +++ b/Lab/Code/intercode.c @@ -22,6 +22,10 @@ void code_delete(struct intercode_t *code) assert(code->prev != code); code->prev->next = code->next; code->next->prev = code->prev; + free(code->result); + free(code->op1); + free(code->op2); + free(code); } void generate_code(int kind, int result, int op1, int op2) @@ -40,56 +44,36 @@ void generate_code(int kind, int result, int op1, int op2) temp->op2 = temp3; code_insert(temp); } -void code_optimize() -{ -} - -void print_code() +void clear_label() { struct intercode_t *temp = code_head->next; - while(temp != code_head) + int *label_change = (int *)malloc((Label + 2) * sizeof(int)); + for(int i = 0; i <= Label; i++) + label_change[i] = i; + while(temp != code_head) { - if(temp->kind == codeLABEL)fprintf(fp, "LABEL L%d :\n", temp->result->value); - else if(temp->kind == codeFUNCTION) + while(temp->kind == codeLABEL && temp->next != code_head && temp->next->kind == codeLABEL) { - if(temp->result->value == 1)fprintf(fp, "FUNCTION main :\n"); - else fprintf(fp, "FUNCTION F%d :\n", temp->result->value); + label_change[temp->next->result->value] = temp->result->value; + code_delete(temp->next); } - else if(temp->kind == codeASSIGN) - { - if(temp->op2->value != 0)fprintf(fp, "t%d := #%d\n", temp->result->value, temp->op1->value); - else fprintf(fp, "t%d := t%d\n", temp->result->value, temp->op1->value); - } - else if(temp->kind == codeADD)fprintf(fp, "t%d := t%d + t%d\n", temp->result->value, temp->op1->value, temp->op2->value);//only t1 = t2 + t3 is allowed: variable = variable + variable, no constant is involved - else if(temp->kind == codeSUB)fprintf(fp, "t%d := t%d - t%d\n", temp->result->value, temp->op1->value, temp->op2->value); - else if(temp->kind == codeMUL)fprintf(fp, "t%d := t%d * t%d\n", temp->result->value, temp->op1->value, temp->op2->value); - else if(temp->kind == codeDIV)fprintf(fp, "t%d := t%d / t%d\n", temp->result->value, temp->op1->value, temp->op2->value); - else if(temp->kind == codeAND)fprintf(fp, "t%d := &t%d\n", temp->result->value, temp->op1->value); - else if(temp->kind == codeRSTAR)fprintf(fp, "t%d := *t%d\n", temp->result->value, temp->op1->value); - else if(temp->kind == codeLSTAR)fprintf(fp, "*t%d := t%d\n", temp->result->value, temp->op1->value); - else if(temp->kind == codeGOTO)fprintf(fp, "GOTO L%d\n", temp->result->value); - else if(temp->kind == codeE)fprintf(fp, "IF t%d == t%d GOTO L%d\n", temp->op1->value, temp->op2->value, temp->result->value); - else if(temp->kind == codeNE)fprintf(fp, "IF t%d != t%d GOTO L%d\n", temp->op1->value, temp->op2->value, temp->result->value); - else if(temp->kind == codeG)fprintf(fp, "IF t%d > t%d GOTO L%d\n", temp->op1->value, temp->op2->value, temp->result->value); - else if(temp->kind == codeGE)fprintf(fp, "IF t%d >= t%d GOTO L%d\n", temp->op1->value, temp->op2->value, temp->result->value); - else if(temp->kind == codeL)fprintf(fp, "IF t%d < t%d GOTO L%d\n", temp->op1->value, temp->op2->value, temp->result->value); - else if(temp->kind == codeLE)fprintf(fp, "IF t%d <= t%d GOTO L%d\n", temp->op1->value, temp->op2->value, temp->result->value); - else if(temp->kind == codeRETURN)fprintf(fp, "RETURN t%d\n", temp->result->value); - else if(temp->kind == codeDEC)fprintf(fp, "DEC t%d %d\n", temp->result->value, temp->op1->value); - else if(temp->kind == codeARG)fprintf(fp, "ARG t%d\n", temp->result->value); - else if(temp->kind == codeCALL) - { - if(temp->op1->value == 1)fprintf(fp, "t%d := CALL main\n", temp->result->value); - else fprintf(fp, "t%d := CALL F%d\n", temp->result->value, temp->op1->value); - } - else if(temp->kind == codePARAM)fprintf(fp, "PARAM t%d\n", temp->result->value); - else if(temp->kind == codeREAD)fprintf(fp, "READ t%d\n", temp->result->value); - else if(temp->kind == codeWRITE)fprintf(fp, "WRITE t%d\n", temp->result->value); temp = temp->next; } + temp = code_head->next; + while(temp != code_head) + { + if(codeGOTO <= temp->kind && temp->kind <= codeLE) + temp->result->value = label_change[temp->result->value]; + temp = temp->next; + } + free(label_change); +} +void code_optimize() +{ + clear_label(); } -void print_mips() +void print_code() { struct intercode_t *temp = code_head->next; while(temp != code_head) @@ -133,4 +117,3 @@ void print_mips() temp = temp->next; } } - diff --git a/Lab/Code/intercode.o b/Lab/Code/intercode.o index 72c364e..86d9fdd 100644 Binary files a/Lab/Code/intercode.o and b/Lab/Code/intercode.o differ diff --git a/Lab/Code/main.c b/Lab/Code/main.c index e42ad27..4f10c23 100644 --- a/Lab/Code/main.c +++ b/Lab/Code/main.c @@ -8,6 +8,7 @@ void Parse_Tree(struct _node *cur); FILE *fp = NULL; void code_optimize(); void print_mips(); +void print_code(); int optimize_flag = 1; int main(int argc, char** argv) @@ -35,11 +36,5 @@ int main(int argc, char** argv) fprintf(stderr, "Argument error!\n"); return 1; } - - return 0; } - - - - diff --git a/Lab/Code/mips.c b/Lab/Code/mips.c new file mode 100644 index 0000000..1957772 --- /dev/null +++ b/Lab/Code/mips.c @@ -0,0 +1,199 @@ +#include +#include "common.h" +#include +#include +#include +extern struct intercode_t *code_head; +extern FILE *fp; +extern int Variable, Function; +int *param_begin = NULL; +int *param_size = NULL; +int *var_begin = NULL; +int *var_end = NULL; +int in_which_func = 0; +void before_funcall(int funNo) +{ + fprintf(fp, "move $t0, $sp\n"); + fprintf(fp, "addi $sp, $sp, -8\n"); + fprintf(fp, "sw $fp, 4($sp)\n"); + fprintf(fp, "sw $ra, 0($sp)\n"); + fprintf(fp, "addi $sp, $sp, -%d\n", (var_end[funNo] - var_begin[funNo]) * 4); + fprintf(fp, "move $fp, $sp\n"); + for(int i = 0; i < param_size[funNo]; i++) + { + fprintf(fp, "lw $t1, %d($t0)\n", i * 4); + fprintf(fp, "sw $t1, %d($fp)\n", i * 4); + } +} + +void after_funcall(int funNo) +{ + fprintf(fp, "move $sp, $fp\n"); + fprintf(fp, "addi $sp, $sp, %d\n", (var_end[funNo] - var_begin[funNo]) * 4); + fprintf(fp, "lw $fp, 4($sp)\n"); + fprintf(fp, "lw $ra, 0($sp)\n"); + fprintf(fp, "addi $sp, $sp, 8\n"); +} +void value_load(int reg_no, int var_no) +{ + var_no -= var_begin[in_which_func]; + if(reg_no != 0)fprintf(fp, "lw $t%d, %d($fp)\n", reg_no, var_no * 4); + else fprintf(fp, "lw, $v%d, %d($fp)\n", reg_no, var_no * 4); +} + +void value_store(int reg_no, int var_no) +{ + var_no -= var_begin[in_which_func]; + if(reg_no != 0)fprintf(fp, "sw $t%d, %d($fp)\n", reg_no, var_no * 4); + else fprintf(fp, "sw $v%d, %d($fp)\n", reg_no, var_no * 4); +} +//we only use t1, t2, t3 for ourselves and use t0 for special purpose. when reg_no is 0,it means v0 +void print_mips() +{ + param_begin = malloc(sizeof(int) * (Function + 1)); + param_size = malloc(sizeof(int) * (Function + 1)); + memset(param_begin, 0, sizeof(int) * (Function + 1)); + memset(param_size, 0, sizeof(int) * (Function + 1)); + + var_begin = malloc(sizeof(int) * (Function + 1)); + var_end = malloc(sizeof(int) * (Function + 1)); + memset(var_begin, 0, sizeof(int) * (Function + 1)); + memset(var_end, 0, sizeof(int) * (Function + 1)); + + fprintf(fp, ".data\n_prompt: .asciiz \"Enter an integer:\"\n_ret: .asciiz \"\\n\"\n.globl main\n.text\nread:\nli $v0, 4\nla $a0, _prompt\nsyscall\nli $v0, 5\nsyscall\njr $ra\n\nwrite:\nli $v0, 1\nsyscall\nli $v0, 4\nla $a0, _ret\nsyscall\nmove $v0, $0\njr $ra\n\n"); + struct intercode_t *temp = code_head->next; + in_which_func = 0; + while(temp != code_head) + { + if(temp->kind == codeFUNCTION) + { + if(in_which_func != 0)var_end[in_which_func] = temp->op1->value; + in_which_func = temp->result->value; + var_begin[in_which_func] = temp->op1->value; + while(temp->next != code_head && temp->next->kind == codePARAM) + { + if(param_size[in_which_func] == 0)param_begin[in_which_func] = temp->next->result->value; + param_size[in_which_func]++; + temp = temp->next; + } + } + temp = temp->next; + } + var_end[in_which_func] = Variable + 1; + + in_which_func = 0; + temp = code_head->next; + while(temp != code_head) + { + if(temp->kind == codeLABEL)fprintf(fp, "L%d:\n", temp->result->value); + else if(temp->kind == codeFUNCTION) + { + in_which_func = temp->result->value; + if(temp->result->value == 1)fprintf(fp, "main:\n"); + else fprintf(fp, "F%d:\n", temp->result->value); + if(temp->result->value == 1)before_funcall(1); + } + else if(temp->kind == codeASSIGN) + { + if(temp->op2->value != 0)fprintf(fp, "li $t1, %d\n", temp->op1->value); + else value_load(1, temp->op1->value); + value_store(1, temp->result->value); + } + else if(temp->kind >= codeADD && temp->kind <= codeDIV) + { + value_load(1, temp->op1->value); + value_load(2, temp->op2->value); + if(temp->kind == codeADD)fprintf(fp, "add $t3, $t1, $t2\n"); + else if(temp->kind == codeSUB)fprintf(fp, "sub $t3, $t1, $t2\n"); + else if(temp->kind == codeMUL)fprintf(fp, "mul $t3, $t1, $t2\n"); + else if(temp->kind == codeDIV) + { + fprintf(fp, "div $t1, $t2\n"); + fprintf(fp, "mflo $t3\n"); + } + value_store(3, temp->result->value); + } + else if(temp->kind == codeAND) + { + value_load(1, temp->op1->value); + value_store(1, temp->result->value); + } + else if(temp->kind == codeRSTAR) + { + value_load(1, temp->op1->value); + fprintf(fp, "lw $t2, 0($t1)\n"); + value_store(2, temp->result->value); + } + else if(temp->kind == codeLSTAR) + { + value_load(1, temp->op1->value); + value_load(2, temp->result->value); + fprintf(fp, "sw $t1, 0($t2)\n"); + } + else if(temp->kind == codeGOTO)fprintf(fp, "j L%d\n", temp->result->value); + else if(temp->kind >= codeE && temp->kind <= codeLE) + { + value_load(1, temp->op1->value); + value_load(2, temp->op2->value); + if(temp->kind == codeE)fprintf(fp, "beq $t1, $t2, L%d\n", temp->result->value); + else if(temp->kind == codeNE)fprintf(fp, "bne $t1, $t2, L%d\n", temp->result->value); + else if(temp->kind == codeG)fprintf(fp, "bgt $t1, $t2, L%d\n", temp->result->value); + else if(temp->kind == codeGE)fprintf(fp, "bge $t1, $t2, L%d\n", temp->result->value); + else if(temp->kind == codeL)fprintf(fp, "blt $t1, $t2, L%d\n", temp->result->value); + else if(temp->kind == codeLE)fprintf(fp, "ble $t1, $t2, L%d\n", temp->result->value); + } + else if(temp->kind == codeRETURN) + { + value_load(0, temp->result->value); + if(in_which_func == 1)after_funcall(1); + fprintf(fp, "jr $ra\n"); + } + else if(temp->kind == codeDEC) + { + fprintf(fp, "addi $sp, $sp, -%d\n", temp->op1->value); + fprintf(fp, "move $t1, $sp\n"); + value_store(1, temp->result->value); + } + else if(temp->kind == codeARG) + { + value_load(1, temp->result->value); + fprintf(fp, "addi $sp, $sp, -4\n"); + fprintf(fp, "sw $t1, 0($sp)\n"); + while(temp->next != code_head && temp->next->kind == codeARG) + { + value_load(1, temp->next->result->value); + fprintf(fp, "addi $sp, $sp, -4\n"); + fprintf(fp, "sw $t1, 0($sp)\n"); + temp = temp->next; + } + } + else if(temp->kind == codeCALL) + { + before_funcall(temp->op1->value); + if(temp->op1->value == 1)fprintf(fp, "jal main\n"); + else fprintf(fp, "jal F%d\n", temp->op1->value); + after_funcall(temp->op1->value); + value_store(0, temp->result->value); + } + else if(temp->kind == codeREAD) + { + before_funcall(2); + fprintf(fp, "jal read\n"); + after_funcall(2); + value_store(0, temp->result->value); + } + else if(temp->kind == codeWRITE) + { + value_load(1, temp->result->value); + fprintf(fp, "move $a0, $t1\n"); + before_funcall(3); + fprintf(fp, "jal write\n"); + after_funcall(3); + } + temp = temp->next; + } + free(param_begin); + free(param_size); + free(var_begin); + free(var_end); +} diff --git a/Lab/Code/mips.o b/Lab/Code/mips.o new file mode 100644 index 0000000..7e6c932 Binary files /dev/null and b/Lab/Code/mips.o differ diff --git a/Lab/Code/out.ir b/Lab/Code/out.ir index 21b6c90..8c54ac8 100644 --- a/Lab/Code/out.ir +++ b/Lab/Code/out.ir @@ -1,3 +1,101 @@ FUNCTION F4 : PARAM t1 -RETURN t1 +PARAM t2 +t4 := #60483 +t3 := t4 +t6 := #63528 +t5 := t6 +t8 := #62159 +t7 := t8 +t10 := #23290 +t9 := t10 +t12 := #23183 +t11 := t12 +t14 := #63380 +t13 := t14 +t16 := #28578 +t15 := t16 +t18 := #0 +t19 := #1 +t21 := #0 +t22 := #1 +t24 := #0 +t25 := #1 +t27 := #0 +t28 := #1 +t29 := #0 +t30 := #1 +t31 := t30 +IF t7 == t29 GOTO L5 +t31 := t29 +LABEL L5 : +t32 := t31 + t9 +t33 := #6632 +t26 := t28 +IF t32 != t33 GOTO L4 +t26 := t27 +LABEL L4 : +t35 := #0 +t34 := t35 - t15 +t23 := t25 +IF t26 != t34 GOTO L3 +t23 := t24 +LABEL L3 : +t20 := t22 +IF t23 == t5 GOTO L2 +t20 := t21 +LABEL L2 : +t17 := t19 +IF t20 >= t3 GOTO L1 +t17 := t18 +LABEL L1 : +t36 := #0 +IF t17 == t36 GOTO L6 +t37 := #123 +WRITE t37 +LABEL L6 : +t40 := #0 +t41 := #1 +t42 := #0 +t43 := #1 +t44 := t43 +IF t7 == t42 GOTO L8 +t44 := t42 +LABEL L8 : +t45 := t44 + t9 +t46 := #6332 +t39 := t41 +IF t45 != t46 GOTO L7 +t39 := t40 +LABEL L7 : +WRITE t39 +RETURN t11 +FUNCTION F5 : +t49 := #1991 +t48 := t49 +t50 := #1230 +t52 := #0 +t53 := #1 +t51 := t53 +t55 := #0 +t56 := #1 +t54 := t56 +IF t48 != t48 GOTO L10 +t54 := t55 +LABEL L10 : +IF t54 != t52 GOTO L9 +t57 := #47129 +IF t57 != t52 GOTO L9 +t51 := t52 +LABEL L9 : +ARG t51 +ARG t50 +t58 := CALL F4 +WRITE t48 +t60 := #64344 +RETURN t60 +FUNCTION main : +t61 := CALL F5 +WRITE t61 +t63 := #0 +RETURN t63 diff --git a/Lab/Code/out.s b/Lab/Code/out.s new file mode 100644 index 0000000..bade7b2 --- /dev/null +++ b/Lab/Code/out.s @@ -0,0 +1,311 @@ +.data +_prompt: .asciiz "Enter an integer:" +_ret: .asciiz "\n" +.globl main +.text +read: +li $v0, 4 +la $a0, _prompt +syscall +li $v0, 5 +syscall +jr $ra + +write: +li $v0, 1 +syscall +li $v0, 4 +la $a0, _ret +syscall +move $v0, $0 +jr $ra + +F4: +li $t1, 60483 +sw $t1, 12($fp) +lw $t1, 12($fp) +sw $t1, 8($fp) +li $t1, 63528 +sw $t1, 20($fp) +lw $t1, 20($fp) +sw $t1, 16($fp) +li $t1, 62159 +sw $t1, 28($fp) +lw $t1, 28($fp) +sw $t1, 24($fp) +li $t1, 23290 +sw $t1, 36($fp) +lw $t1, 36($fp) +sw $t1, 32($fp) +li $t1, 23183 +sw $t1, 44($fp) +lw $t1, 44($fp) +sw $t1, 40($fp) +li $t1, 63380 +sw $t1, 52($fp) +lw $t1, 52($fp) +sw $t1, 48($fp) +li $t1, 28578 +sw $t1, 60($fp) +lw $t1, 60($fp) +sw $t1, 56($fp) +li $t1, 0 +sw $t1, 68($fp) +li $t1, 1 +sw $t1, 72($fp) +li $t1, 0 +sw $t1, 80($fp) +li $t1, 1 +sw $t1, 84($fp) +li $t1, 0 +sw $t1, 92($fp) +li $t1, 1 +sw $t1, 96($fp) +li $t1, 0 +sw $t1, 104($fp) +li $t1, 1 +sw $t1, 108($fp) +li $t1, 0 +sw $t1, 112($fp) +li $t1, 1 +sw $t1, 116($fp) +lw $t1, 116($fp) +sw $t1, 120($fp) +lw $t1, 24($fp) +lw $t2, 112($fp) +beq $t1, $t2, L5 +lw $t1, 112($fp) +sw $t1, 120($fp) +L5: +lw $t1, 120($fp) +lw $t2, 32($fp) +add $t3, $t1, $t2 +sw $t3, 124($fp) +li $t1, 6632 +sw $t1, 128($fp) +lw $t1, 108($fp) +sw $t1, 100($fp) +lw $t1, 124($fp) +lw $t2, 128($fp) +bne $t1, $t2, L4 +lw $t1, 104($fp) +sw $t1, 100($fp) +L4: +li $t1, 0 +sw $t1, 136($fp) +lw $t1, 136($fp) +lw $t2, 56($fp) +sub $t3, $t1, $t2 +sw $t3, 132($fp) +lw $t1, 96($fp) +sw $t1, 88($fp) +lw $t1, 100($fp) +lw $t2, 132($fp) +bne $t1, $t2, L3 +lw $t1, 92($fp) +sw $t1, 88($fp) +L3: +lw $t1, 84($fp) +sw $t1, 76($fp) +lw $t1, 88($fp) +lw $t2, 16($fp) +beq $t1, $t2, L2 +lw $t1, 80($fp) +sw $t1, 76($fp) +L2: +lw $t1, 72($fp) +sw $t1, 64($fp) +lw $t1, 76($fp) +lw $t2, 8($fp) +bge $t1, $t2, L1 +lw $t1, 68($fp) +sw $t1, 64($fp) +L1: +li $t1, 0 +sw $t1, 140($fp) +lw $t1, 64($fp) +lw $t2, 140($fp) +beq $t1, $t2, L6 +li $t1, 123 +sw $t1, 144($fp) +lw $t1, 144($fp) +move $a0, $t1 +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -0 +move $fp, $sp +jal write +move $sp, $fp +addi $sp, $sp, 0 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +L6: +li $t1, 0 +sw $t1, 156($fp) +li $t1, 1 +sw $t1, 160($fp) +li $t1, 0 +sw $t1, 164($fp) +li $t1, 1 +sw $t1, 168($fp) +lw $t1, 168($fp) +sw $t1, 172($fp) +lw $t1, 24($fp) +lw $t2, 164($fp) +beq $t1, $t2, L8 +lw $t1, 164($fp) +sw $t1, 172($fp) +L8: +lw $t1, 172($fp) +lw $t2, 32($fp) +add $t3, $t1, $t2 +sw $t3, 176($fp) +li $t1, 6332 +sw $t1, 180($fp) +lw $t1, 160($fp) +sw $t1, 152($fp) +lw $t1, 176($fp) +lw $t2, 180($fp) +bne $t1, $t2, L7 +lw $t1, 156($fp) +sw $t1, 152($fp) +L7: +lw $t1, 152($fp) +move $a0, $t1 +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -0 +move $fp, $sp +jal write +move $sp, $fp +addi $sp, $sp, 0 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +lw, $v0, 40($fp) +jr $ra +F5: +li $t1, 1991 +sw $t1, 4($fp) +lw $t1, 4($fp) +sw $t1, 0($fp) +li $t1, 1230 +sw $t1, 8($fp) +li $t1, 0 +sw $t1, 16($fp) +li $t1, 1 +sw $t1, 20($fp) +lw $t1, 20($fp) +sw $t1, 12($fp) +li $t1, 0 +sw $t1, 28($fp) +li $t1, 1 +sw $t1, 32($fp) +lw $t1, 32($fp) +sw $t1, 24($fp) +lw $t1, 0($fp) +lw $t2, 0($fp) +bne $t1, $t2, L10 +lw $t1, 28($fp) +sw $t1, 24($fp) +L10: +lw $t1, 24($fp) +lw $t2, 16($fp) +bne $t1, $t2, L9 +li $t1, 47129 +sw $t1, 36($fp) +lw $t1, 36($fp) +lw $t2, 16($fp) +bne $t1, $t2, L9 +lw $t1, 16($fp) +sw $t1, 12($fp) +L9: +lw $t1, 12($fp) +addi $sp, $sp, -4 +sw $t1, 0($sp) +lw $t1, 8($fp) +addi $sp, $sp, -4 +sw $t1, 0($sp) +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -188 +move $fp, $sp +lw $t1, 0($t0) +sw $t1, 0($fp) +lw $t1, 4($t0) +sw $t1, 4($fp) +jal F4 +move $sp, $fp +addi $sp, $sp, 188 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +sw $v0, 40($fp) +lw $t1, 0($fp) +move $a0, $t1 +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -0 +move $fp, $sp +jal write +move $sp, $fp +addi $sp, $sp, 0 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +li $t1, 64344 +sw $t1, 48($fp) +lw, $v0, 48($fp) +jr $ra +main: +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -12 +move $fp, $sp +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -52 +move $fp, $sp +jal F5 +move $sp, $fp +addi $sp, $sp, 52 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +sw $v0, 0($fp) +lw $t1, 0($fp) +move $a0, $t1 +move $t0, $sp +addi $sp, $sp, -8 +sw $fp, 4($sp) +sw $ra, 0($sp) +addi $sp, $sp, -0 +move $fp, $sp +jal write +move $sp, $fp +addi $sp, $sp, 0 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +li $t1, 0 +sw $t1, 8($fp) +lw, $v0, 8($fp) +move $sp, $fp +addi $sp, $sp, 12 +lw $fp, 4($sp) +lw $ra, 0($sp) +addi $sp, $sp, 8 +jr $ra diff --git a/Lab/Code/parser b/Lab/Code/parser index 3501468..a0502d9 100755 Binary files a/Lab/Code/parser and b/Lab/Code/parser differ diff --git a/Lab/Code/semantic.c b/Lab/Code/semantic.c index c96bcd0..b204168 100644 --- a/Lab/Code/semantic.c +++ b/Lab/Code/semantic.c @@ -199,6 +199,7 @@ void Parse_Tree(struct _node *cur) code_head = malloc(sizeof(struct intercode_t)); code_head->prev = code_head; code_head->next = code_head; + code_head->kind = 0; MALLOC(temp, struct type_t); temp->kind = FUNCTION; @@ -240,6 +241,9 @@ void parse_tree(struct _node *cur) child = child->right; if(strcmp(child->token_name, "FunDec") == 0) { + //mips + int startVariable = Variable + 1; + //mips if(child->left->text != NULL)add_entry(child->left->text, Int); stack_new(); @@ -265,7 +269,7 @@ void parse_tree(struct _node *cur) assert(e->var_no == 0); e->var_no = ++Function; } - generate_code(codeFUNCTION, e->var_no, 0, 0); + generate_code(codeFUNCTION, e->var_no, startVariable, 0); e = e->type->structure->down; struct entry_t *temp_e = e; while(temp_e) diff --git a/Lab/Code/semantic.o b/Lab/Code/semantic.o index ef36a87..00da70a 100644 Binary files a/Lab/Code/semantic.o and b/Lab/Code/semantic.o differ diff --git a/Lab/Test/test.cmm b/Lab/Test/test.cmm index 7186927..ad884f6 100644 --- a/Lab/Test/test.cmm +++ b/Lab/Test/test.cmm @@ -1,5 +1,37 @@ +int id_OOj3M(int id_Snrw, int id_HyerJj) +{ + int id_B0; + int id_O7f; + int id_uL; + int id_FGC; + int id_l; + int id_d0q; + int id_FtYelLAw; + id_B0 = 60483; + id_O7f = 63528; + id_uL = 62159; + id_FGC = 23290; + id_l = 23183; + id_d0q = 63380; + id_FtYelLAw = 28578; + if (! id_uL + id_FGC != 6632 != - id_FtYelLAw == id_O7f >= id_B0) + { + write(123); + } + write(!id_uL + id_FGC != 6332); + return id_l; +} -int idyh(int idux) +int id_ertebiIg() { - return idux; + int id_jIJ3GiE8N6; + id_jIJ3GiE8N6 = 1991; + id_OOj3M(1230, id_jIJ3GiE8N6 != id_jIJ3GiE8N6 || 47129); + write(id_jIJ3GiE8N6); + return 64344; +} + +int main() { + write(id_ertebiIg()); + return 0; } diff --git a/Lab/Test/test1.cmm b/Lab/Test/test1.cmm index d98d1ab..6c52b28 100644 --- a/Lab/Test/test1.cmm +++ b/Lab/Test/test1.cmm @@ -1,9 +1,14 @@ int main() { - int n; + int a = 0, b = 1, i = 0, n; n = read(); - if(n > 0) write(1); - else if (n < 0) write(-1); - else write(0); + while(i < n) + { + int c = a + b; + write(b); + a = b; + b = c; + i = i + 1; + } return 0; } diff --git a/Lab/Test/test3.cmm b/Lab/Test/test3.cmm index 61c0f75..7f8965f 100644 --- a/Lab/Test/test3.cmm +++ b/Lab/Test/test3.cmm @@ -1,21 +1,9 @@ -struct Operands +int add(int a, int b) { - int o1; - int o2; -}; - -int add(struct Operands temp) -{ - return (temp.o1 + temp.o2); + return a + b; } - int main() { - int n; - struct Operands op; - op.o1 = 1; - op.o2 = 2; - n = add(op); - write(n); + write(add(3, 2)); return 0; } diff --git a/Lab/Test/test4.cmm b/Lab/Test/test4.cmm index d73cbe4..d686c48 100644 --- a/Lab/Test/test4.cmm +++ b/Lab/Test/test4.cmm @@ -1,24 +1,28 @@ -int add(int temp[2]) +int add(int a, int b) { - return (temp[0] + temp[1]); + return a + b; } - -int main() +int fun(int x) { - int op[2]; - int r[1][3]; - int i = 0, j = 0; - while (i < 2) + int a[10]; + int i = x; + int ans = 0; + while(i) + { + a[i] = i; + i = i - 1; + } + while(x) { - while(j < 2) - { - op[j] = i + j; - j = j + 1; - } - r[0][i] = add(op); - write(r[0][i]); - i = i + 1; - j = 0; + ans = ans + a[x]; + x = x - 1; } + return ans; + +} +int main() +{ + int n = read(); + write(add(fun(n), fun(n / 2))); return 0; } diff --git a/Lab/parser b/Lab/parser new file mode 100755 index 0000000..0c3cfeb Binary files /dev/null and b/Lab/parser differ diff --git a/Lab/report.pdf b/Lab/report.pdf index 211117e..a4b04e0 100644 Binary files a/Lab/report.pdf and b/Lab/report.pdf differ