📄 compiler.c
字号:
fprintf(file, "\t%s\n", "pushl (%eax)"); push(2.0); } if( (a==0.0 || a==1.0) && b==2.0){ fprintf(file, "\t%s\n", "subl $4, %esp"); fprintf(file, "\t%s\n", "fnstcw (%esp)\t\t# save the cast flag"); fprintf(file, "\t%s\n", "movw (%esp), %cx"); fprintf(file, "\t%s\n", "movb $12, %ch"); fprintf(file, "\t%s\n", "movw %cx, 2(%esp)"); fprintf(file, "\t%s\n", "fldcw 2(%esp)\t\t# set the new cast flag"); fprintf(file, "\t%s\n", "fistpl (%eax)"); fprintf(file, "\t%s\n", "fldcw (%esp)\t\t# restore the cast flag as before"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(1.0); } return; } if(strcmp(tree->value,"-=")==0){ if((b==1.0 || b==0.0) && (a==0.0 || a==1.0)){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %eax"); fprintf(file, "\t%s\n", "subl %ebx, (%eax)"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(1.0); return; } if(b==2.0) fprintf(file, "\t%s\n", "flds (%esp)"); else fprintf(file, "\t%s\n", "fildl (%esp)"); fprintf(file, "\t%s\n", "popl %ebx"); fprintf(file, "\t%s\n", "popl %eax"); if(a==2.0) fprintf(file, "\t%s\n", "flds (%eax)"); else fprintf(file, "\t%s\n", "fildl (%eax)"); fprintf(file, "\t%s\n", "fsubp %st, %st(1)"); if( a==2.0){ fprintf(file, "\t%s\n", "fstps (%eax)"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(2.0); } if( (a==0.0 || a==1.0) && b==2.0){ fprintf(file, "\t%s\n", "subl $4, %esp"); fprintf(file, "\t%s\n", "fnstcw (%esp)\t\t# save the cast flag"); fprintf(file, "\t%s\n", "movw (%esp), %cx"); fprintf(file, "\t%s\n", "movb $12, %ch"); fprintf(file, "\t%s\n", "movw %cx, 2(%esp)"); fprintf(file, "\t%s\n", "fldcw 2(%esp)\t\t# set the new cast flag"); fprintf(file, "\t%s\n", "fistpl (%eax)"); fprintf(file, "\t%s\n", "fldcw (%esp)\t\t# restore the cast flag as before"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(1.0); } return; } if(strcmp(tree->value,"*=")==0){ if((b==1.0 || b==0.0) && (a==0.0 || a==1.0)){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %eax"); fprintf(file, "\t%s\n","movl (%eax), %ecx"); fprintf(file, "\t%s\n", "imull %ebx, %ecx"); fprintf(file, "\t%s\n","movl %ecx, (%eax)"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(1.0); return; } if(b==2.0) fprintf(file, "\t%s\n", "flds (%esp)"); else fprintf(file, "\t%s\n", "fildl (%esp)"); fprintf(file, "\t%s\n", "popl %ebx"); fprintf(file, "\t%s\n", "popl %eax"); if(a==2.0) fprintf(file, "\t%s\n", "flds (%eax)"); else fprintf(file, "\t%s\n", "fildl (%eax)"); fprintf(file, "\t%s\n", "fmulp %st, %st(1)"); if( a==2.0){ fprintf(file, "\t%s\n", "fstps (%eax)"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(2.0); } if( (a==0.0 || a==1.0) && b==2.0){ fprintf(file, "\t%s\n", "subl $4, %esp"); fprintf(file, "\t%s\n", "fnstcw (%esp)\t\t# save the cast flag"); fprintf(file, "\t%s\n", "movw (%esp), %cx"); fprintf(file, "\t%s\n", "movb $12, %ch"); fprintf(file, "\t%s\n", "movw %cx, 2(%esp)"); fprintf(file, "\t%s\n", "fldcw 2(%esp)\t\t# set the new cast flag"); fprintf(file, "\t%s\n", "fistpl (%eax)"); fprintf(file, "\t%s\n", "fldcw (%esp)\t\t# restore the cast flag as before"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(1.0); } return; } if(strcmp(tree->value,"/=")==0){ if((b==1.0 || b==0.0) && (a==0.0 || a==1.0)){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %ecx"); fprintf(file, "\t%s\n", "movl (%ecx), %eax"); fprintf(file, "\tcltd\n"); fprintf(file, "\t%s\n", "idivl %ebx"); fprintf(file, "\t%s\n", "movl %eax, (%ecx)"); fprintf(file, "\t%s\n", "pushl (%ecx)"); push(1.0); return; } if(b==2.0) fprintf(file, "\t%s\n", "flds (%esp)"); else fprintf(file, "\t%s\n", "fildl (%esp)"); fprintf(file, "\t%s\n", "popl %ebx"); fprintf(file, "\t%s\n", "popl %eax"); if(a==2.0) fprintf(file, "\t%s\n", "flds (%eax)"); else fprintf(file, "\t%s\n", "fildl (%eax)"); fprintf(file, "\t%s\n", "fdivp %st, %st(1)"); if( a==2.0){ fprintf(file, "\t%s\n", "fstps (%eax)"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(2.0); } if( (a==0.0 || a==1.0) && b==2.0){ fprintf(file, "\t%s\n", "subl $4, %esp"); fprintf(file, "\t%s\n", "fnstcw (%esp)\t\t# save the cast flag"); fprintf(file, "\t%s\n", "movw (%esp), %cx"); fprintf(file, "\t%s\n", "movb $12, %ch"); fprintf(file, "\t%s\n", "movw %cx, 2(%esp)"); fprintf(file, "\t%s\n", "fldcw 2(%esp)\t\t# set the new cast flag"); fprintf(file, "\t%s\n", "fistpl (%eax)"); fprintf(file, "\t%s\n", "fldcw (%esp)\t\t# restore the cast flag as before"); fprintf(file, "\t%s\n", "pushl (%eax)"); push(1.0); } return; } if(a==1.0 && b==1.0) push(1.0); else { printf("--compiling messege(line %d): only integers can be the operand\n", tree->line); exit(0); } if(strcmp(tree->value,"%=")==0){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %ecx"); fprintf(file, "\t%s\n", "movl (%ecx), %eax"); fprintf(file, "\tcltd\n"); fprintf(file, "\t%s\n", "idivll %ebx"); fprintf(file, "\t%s\n", "movl %edx, (%ecx)"); fprintf(file, "\t%s\n", "pushl (%ecx)"); return; } if(strcmp(tree->value,"&=")==0){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %eax"); //fprintf(file, "\t%s\n", "movl (%ecx), %eax"); fprintf(file, "\t%s\n", "andl %ebx, (%eax)"); //fprintf(file, "\t%s\n", "movl %eax, (%ecx)"); fprintf(file, "\t%s\n", "pushl (%eax)"); return; } if(strcmp(tree->value,"|=")==0){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %eax"); //fprintf(file, "\t%s\n", "movl (%ecx), %eax"); fprintf(file, "\t%s\n", "orl %ebx, (%eax)"); //fprintf(file, "\t%s\n", "movl %eax, (%ecx)"); fprintf(file, "\t%s\n", "pushl (%eax)"); return; } if(strcmp(tree->value,"^=")==0){ fprintf(file, "\t%s\n","popl %ebx"); fprintf(file, "\t%s\n","popl %eax"); //fprintf(file, "\t%s\n", "movl (%ecx), %eax"); fprintf(file, "\t%s\n", "xorl %ebx, (%eax)"); //fprintf(file, "\t%s\n", "movl %eax, (%ecx)"); fprintf(file, "\t%s\n", "pushl (%eax)"); return; } if(strcmp(tree->value,"<<=")==0){ fprintf(file, "\t%s\n","popl %ecx"); fprintf(file, "\t%s\n","popl %eax"); fprintf(file, "\t%s\n", "sall %cl, (%eax)"); fprintf(file, "\t%s\n", "pushl (%eax)"); return; } if(strcmp(tree->value,">>=")==0){ fprintf(file, "\t%s\n","popl %ecx"); fprintf(file, "\t%s\n","popl %eax"); //fprintf(file, "\t%s\n", "movl %ecx, %eax"); //fprintf(file, "\t%s\n", "movl %ebx, %ecx"); fprintf(file, "\t%s\n", "sarl %cl, (%eax)"); //fprintf(file, "\t%s\n", "movl %eax, %ecx"); fprintf(file, "\t%s\n", "pushl (%eax)"); return; } }void gen_func_content(FILE *file,symnode *tree){ if(tree == NULL) return; //printf("node: %s %d\n",tree->name,findingFuncNode); if(strcmp(tree->name, "func")==0){ return; } if(strcmp(tree->name, "struct")==0){ return; } if(strcmp(tree->name, "iDector")==0){ initOrder=0; if(strcmp(tree->next->name,"ASSIGN")==0) gen_init_vals(file,tree); return; } gen_func_content(file, tree->next); if(strcmp(tree->name,"println")==0){ gen_println(file, tree->sibling); print_nl(file); return; } if(strcmp(tree->name, "return")==0){ gen_return(file, tree); return; } if(strcmp(tree->name, "if")==0){ gen_if(file, tree); return; } if(strcmp(tree->name, "switch")==0){ loopType=4; gen_switch(file, tree); loopType=-1; return; } if(strcmp(tree->name, "for")==0){ loopType=3; gen_for(file, tree); loopType=-1; return; } if(strcmp(tree->name, "while")==0){ loopType=1; gen_while(file, tree); loopType=-1; return; } if(strcmp(tree->name, "doWhile")==0){ loopType=2; gen_dowhile(file,tree); loopType=-1; return; } if(strcmp(tree->name, "break")==0){ gen_break(file); return; } if(strcmp(tree->name, "continue")==0){ gen_continue(file); return; } if(strcmp(tree->name,"OP")==0){ gen_assign(file, tree); fprintf(file,"\t%s\n","popl %eax"); pop(); return; } if(strcmp(tree->name, "ASSIGN")==0 ){ ///printf("\nassign:%s\n",tree->value); gen_assign(file, tree); fprintf(file,"\t%s\n","popl %eax"); pop(); return; } if(strcmp(tree->name, "unaryOp")==0){ gen_unary_op(file, tree); fprintf(file,"\t%s\n","popl %eax"); return; } if(strcmp(tree->name, "ID")==0){ if( (tree->symTab->nameType == 1) && findingFuncNode==0){ //gen_call_func(file, tree, tree); gen_assign(file, tree); fprintf(file,"\t%s\n","popl %eax"); pop(); } //printf("ID: %s\n",tree->symTab->name); if(tree->symTab->nameType==1) findingFuncNode = 0; // found its function declarator node, next time meeting func node should be a call return; } if(strcmp(tree->name,"goto")==0){ fprintf(file,"\t%s%s\n","jmp ", tree->value); return; } if(strcmp(tree->name,"label")==0){ fprintf(file,"%s:\n",tree->value); gen_func_content(file, tree->sibling); return; } gen_func_content(file, tree->sibling); }void gen_return(FILE *file, symnode *tree){ //if(tree==NULL) return; gen_assign(file,tree->sibling); //pop(); fprintf(file, "\t%s\n","popl %eax\t\t# save the return value in eax"); func_epilogue(file); if(isMain !=1 ) fprintf(file, "\t%s\n","ret"); else output_end(assembler_file); }void gen_logic_and(FILE *file){ int andl; andl=ANDlabel; ANDlabel++; fprintf(file, "\t%s\n", "popl %ebx"); fprintf(file, "\t%s\n", "popl %eax"); fprintf(file, "\t%s\n", "cmpl $0,%eax"); fprintf(file, "\t%s%d\n", "je ANDlabel",andl); fprintf(file, "\t%s\n", "movl %ebx, %eax"); fprintf(file, "%s%d:\n", "ANDlabel",andl); }void gen_logic_or(FILE *file){ fprintf(file, "\t%s\n", "popl %ebx"); fprintf(file, "\t%s\n", "popl %eax"); fprintf(file, "\t%s\n", "orl %ebx, %eax"); }void gen_relation_code(FILE *file, symnode *tree){ float a,b; int booll; booll=BOOLlabel; BOOLlabel++; b=pop(); a=pop(); if(a==1.0 && b==1.0){ fprintf(file, "\t%s\n", "popl %ebx"); fprintf(file, "\t%s\n", "popl %eax"); fprintf(file, "\t%s\n", "cmpl %ebx, %eax"); if(strcmp(tree->value, "==")==0) fprintf(file, "\t%s\n", "sete %al"); if(strcmp(tree->value, "!=")==0) fprintf(file, "\t%s\n", "setne %al"); if(strcmp(tree->value, ">")==0) fprintf(file, "\t%s\n", "setg %al"); if(strcmp(tree->value, ">=")==0) fprintf(file, "\t%s\n", "setge %al"); if(strcmp(tree->value, "<")==0) fprintf(file, "\t%s\n", "setl %al"); if(strcmp(tree->value, "<=")==0) fprintf(file, "\t%s\n", "setle %al"); fprintf(file, "\t%s\n", "movzbl %al, %eax"); }else{ if(b==2.0) fprintf(file, "\t%s\n", "flds (%esp)"); else fprintf(file, "\t%s\n", "fildl (%esp)"); if(a==2.0) fprintf(file, "\t%s\n", "flds 4(%esp)"); else fprintf(file, "\t%s\n", "fildl 4(%esp)"); fprintf(file, "\t%s\n", "addl $8, %esp\t\t# pop twice to clear the stack"); if(strcmp(tree->value, ">")==0){ //fprintf(file, "\t%s\n", "fxch");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -