📄 myparser.y
字号:
if(root->nodekind==Repk) {
// printf("%d Repeat Statement ",root->count);
}
else if(root->nodekind==AssignK){
// printf("%d Assign Statement ",root->count);
}
else if(root->nodekind==IDk){
// printf("%d ID Declaration symbol %s ",root->count,root->name);
}
else if(root->nodekind==8){
// printf("%d Const Declaration value %d ",root->count,root->val);
}
else if(root->nodekind==Exprk) {
// printf("%d Expr op%d ",root->count,root->op);
}
else if(root->nodekind==Com){
// printf("%d Compound Statement ",root->count);
}
else if(root->nodekind==Vark){
// printf("%d Var Declaration ",root->count);
}
else if(root->nodekind==Typek){
// printf("%d Type Specifier %s ",root->count,root->name);
}
else if(root->nodekind==Prink){
// printf("%d Print Statement ",root->count);
}
// printf("Children :");
output(root);
// printf("\n");
if(root->sibling!=NULL) {
find(root->sibling);
// output(root->sibling);
}
}
void header()
{
fprintf(out,"Welcome to GLY's complier. StudentID:0510615 Computer Science\n");
fprintf(out,".586\n.model flat, stdcall\noption casemap :none\ninclude \\masm32\\include\\windows.inc\ninclude \\masm32\\include\\user32.inc\ninclude \\masm32\\include\\kernel32.inc\n");
fprintf(out,"include \\masm32\\include\\masm32.inc\nincludelib \\masm32\\lib\\user32.lib\nincludelib \\masm32\\lib\\kernel32.lib\n includelib \\masm32\\lib\\masm32.lib\n");
fprintf(out,".data\n");
fprintf(out,"buffer BYTE 128 dup(0)\n");
fprintf(out,"LF BYTE 13, 10, 0\n");
}
void genVari(TreeNode *root){
if(root->nodekind==IDk)
fprintf(out,"%s",root->name);
else if(root->nodekind==Constk)
fprintf(out,"%d",root->val);
else
fprintf(out,"_t%d",root->count);
}
void genStmt(TreeNode * root){
// switch(root->nodekind){}
int l1,l2;
if(root->nodekind==Com){
cGen(root->child[0]);
}
else if(root->nodekind==Prink){
genExp(root->child[0]);
// if(root->child[0]->nodekind==IDk)
// fprintf(out,"mov eax,%s\n",root->child[0]->name);
// else
// fprintf(out,"mov eax,_t%d\n",root->child[0]->count);
fprintf(out," mov eax,");
genVari(root->child[0]);
fprintf(out,"\n");
fprintf(out," invoke dwtoa, eax, ADDR buffer\n");
fprintf(out," invoke StdOut, ADDR buffer\n");
fprintf(out," invoke StdOut, ADDR LF\n");
}
else if(root->nodekind==Inputk){
fprintf(out," invoke StdIn, ADDR buffer, 127\n");
fprintf(out," invoke StripLF, ADDR buffer\n");
fprintf(out," invoke atodw, ADDR buffer\n");
fprintf(out," mov %s, eax\n",root->child[0]->name);
}
else if(root->nodekind==AssignK)
{
//cGen(root->root[0]);
genExp(root->child[1]);
// if(root->child[1]->nodekind==IDk)
// fprintf(out,"mov eax,%s\n",root->child[1]->name);
// else
// fprintf(out,"mov eax,_t%d\n",root->child[1]->count);
// genVari(root->child[1],"eax");
fprintf(out," mov eax,");
genVari(root->child[1]);
fprintf(out,"\n");
fprintf(out," mov %s,eax\n",root->child[0]->name);
}
else if(root->nodekind==Repk){
//*********************************************
l1=label;l2=label+1;
label=label+2;
fprintf(out,"@%d:\n",l1);
genExp(root->child[0]);
/* if(root->child[0]->nodekind==IDk)
fprintf(out,"cmp %s,0\n",root->child[0]->name);
else if(root->child[0]->nodekind==Constk)
fprintf(out,"cmp %d,0\n",root->child[0]->val);
else
fprintf(out,"cmp _t%d,0\n",root->child[0]->count);
*/
fprintf(out," cmp ");
genVari(root->child[0]);
fprintf(out,",0\n");
fprintf(out," jz @%d\n",l2);
cGen(root->child[1]);
fprintf(out," jmp @%d\n",l1);
fprintf(out,"@%d:\n",l2);
}
else if(root->nodekind==IfK){
//*********************************************
l1=label;l2=label+1;
label=label+2;
fprintf(out,"@%d:\n",l1);
genExp(root->child[0]);
fprintf(out," cmp ");
genVari(root->child[0]);
fprintf(out,",0\n");
/* if(root->child[0]->nodekind==IDk)
fprintf(out,"cmp %s,0\n",root->child[0]->name);
else if(root->child[0]->nodekind==Constk)
fprintf(out,"cmp %d,0\n",root->child[0]->val);
else
fprintf(out,"cmp _t%d,0\n",root->child[0]->count);*/
fprintf(out," jz @%d\n",l2);
cGen(root->child[1]);
// fprintf(out,"jmp @%d\n",l1);
fprintf(out,"@%d:\n",l2);
}
return ;
}
void genExp(TreeNode * root){
// switch(root->nodekind){}
int l1,l2;
// printf("oooooooooooooooooooops %d\n",root->nodekind);
if(root->nodekind==Constk){
// printf("oooooooooooooooooooops\n");
//fprintf(out,"mov _t%d ,%d\n",root->count,root->val);
}
else if(root->nodekind==IDk){
}
else if(root->nodekind==Exprk){
cGen(root->child[0]);
///////////////////////////////////
// if(root->child[0]->nodekind==IDk)
// fprintf(out,"mov eax , %s\n",root->child[0]->name);
// else
// fprintf(out,"mov eax , _t%d\n",root->child[0]->count);
fprintf(out," mov eax,");
genVari(root->child[0]);
fprintf(out,"\n");
fprintf(out," mov _t%d , eax\n",root->count);
if(root->child[1]!=NULL){
cGen(root->child[1]);
fprintf(out," mov ebx,");
genVari(root->child[1]);
fprintf(out,"\n");
// if(root->child[1]->nodekind==IDk)
// fprintf(out,"mov ebx , %s\n",root->child[1]->name);
// else
// fprintf(out,"mov ebx , _t%d\n",root->child[1]->count);
}
//////////////////////////////////
if(root->op==PLUS){
fprintf(out," add _t%d ,ebx\n",root->count);
}
else if(root->op==MINUS){
fprintf(out," sub _t%d ,ebx\n",root->count);
}
else if(root->op==TIMES){
fprintf(out," mul ebx\n");
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count); */
}
else if(root->op==OVER){
if(root->child[0]->nodekind==IDk)
fprintf(out," mov eax, %s\n",root->child[0]->name);
else
fprintf(out,"mov eax, _t%d\n",root->child[0]->count);
if(root->child[1]->nodekind==IDk)
fprintf(out,"div %s\n",root->child[1]->name);
else
fprintf(out,"div _t%d\n",root->child[1]->count);
if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count);
}
else if(root->op==LT){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,ebx\n");
fprintf(out," jl @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
/*if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count); */
}
else if(root->op==EQ_OP){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,ebx\n");
fprintf(out," jz @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count);
*/
}
else if(root->op==GT){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,ebx\n");
fprintf(out," jg @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count); */
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
}
else if(root->op==LE){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,ebx\n");
fprintf(out," jle @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
fprintf(out," mov ");
genVari(root) ;
fprintf(out,",eax\n");
/*
if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count); */
}
else if(root->op==GE){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,ebx\n");
fprintf(out," jge @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count); */
}
else if(root->op==NEQ){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,ebx\n");
fprintf(out," jne @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count);*/
}
else if(root->op==NOT){
// printf("***********zzzzzzzzzzz******\n");
l1=label;l2=label+1;label+=2;
fprintf(out," cmp eax,0\n");
fprintf(out," jz @%d\n",l1);
fprintf(out," mov eax,0\n");
fprintf(out," jmp @%d\n",l2);
fprintf(out,"@%d:\n",l1);
fprintf(out," mov eax,1\n");
fprintf(out,"@%d:\n",l2);
fprintf(out," mov ");
genVari(root);
fprintf(out,",eax\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,eax\n",root->name);
else
fprintf(out,"mov _t%d,eax\n",root->count); */
}
else if(root->op==AND){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," mov ecx,0\n");
fprintf(out," cmp eax,0\n");
fprintf(out," jz @%d\n",l1);
fprintf(out," cmp ebx,0\n");
fprintf(out," jz @%d\n",l1);
fprintf(out," mov ecx,1\n");
fprintf(out,"@%d:\n",l1);
fprintf(out," mov ");
genVari(root);
fprintf(out,",ecx\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,ecx\n",root->name);
else
fprintf(out,"mov _t%d,ecx\n",root->count);*/
}
else if(root->op==OR){
// printf("*******************************\n");
l1=label;l2=label+1;label+=2;
fprintf(out," mov ecx,1\n");
fprintf(out," cmp eax,1\n");
fprintf(out," jz @%d\n",l1);
fprintf(out," cmp ebx,1\n");
fprintf(out," jz @%d\n",l1);
fprintf(out," mov ecx,0\n");
fprintf(out,"@%d:\n",l1);
fprintf(out," mov ");
genVari(root);
fprintf(out,",ecx\n");
/* if(root->nodekind==IDk)
fprintf(out,"mov %s,ecx\n",root->name);
else
fprintf(out,"mov _t%d,ecx\n",root->count);*/
}
}
return ;
}
static void cGen( TreeNode * tree)
{ if (tree != NULL)
{ switch (tree->kind) {
case StmtKind:
// printf("opoooooooooooooooo!\n");
// printf("%d\n",tree->nodekind);
genStmt(tree);
break;
case ExprKind:
genExp(tree);
break;
default:
break;
}
cGen(tree->sibling);
}
}
int main(void)
{
yyin=fopen("tmp.c","r");
out= fopen( "code.asm", "w" );
header();
printf("欢迎来到高连永的编译器,编译器正在编译tmp.c文件,请稍后......\n");
yyparse();
// printTab();
fprintf(out,".code\n");
// find(savedTree);
fprintf(out,"_start:\n");
cGen(savedTree);
fprintf(out,"invoke ExitProcess, 0\n");
fprintf(out,"end _start\n");
printf("编译完成,请打开code.asm文件查看相应的汇编代码......\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -