编译原理中间代码生成.ppt
文本预览下载声明
machunyan 西北工业大学软件与微电子学院 * 上述简单控制语句的三地址码生成过程实现: void genCode(SyntaxTree t,char *label) { char * lab1,*lab2; if(t!=NULL) switch(t-kind) {case ExpKind: t-strval=newtemp(); if(t-val==0) emit(t-strval“=false” ); else emit(t-strval“=true”); 初值为空 6.4.3 if和while语句的代码生成过程样例(续) machunyan 西北工业大学软件与微电子学院 * case IfKind: genCode(t-child[0],label); lab1=genLabel(); emit(“if_false” t-child[0]-strval “goto” lab1) genCode(t-child[1],label); if(t-child[2]!=NULL) { lab2=genLabel(); emit(“goto” lab2); } emit(“label” lab1); if(t-child[2]!=NULL) { genCode(t-child[2],label); emit(“label” lab2); } break; 无参过程返回标号名(L1,L2,…) 6.4.3 if和while语句的代码生成过程样例(续) machunyan 西北工业大学软件与微电子学院 * case WhileKind: lab1=genLabel(); emit (“label” lab1); genCode(t-child[0],label); Lab2=genLabel(); emit(“if_false” t-child[0]-strval “goto” lab2) genCode(t-child[1],lab2); emit(“goto” lab1); emit(“label” lab2); break; 6.4.3 if和while语句的代码生成过程样例(续) Lab2 传递给break语句 machunyan 西北工业大学软件与微电子学院 * case BreakKind: emit(“goto” label); break; case OtherKind: emit(“Other”); break; default: emit(“Error”); break; } } 6.4.3 if和while语句的代码生成过程样例(续) machunyan 西北工业大学软件与微电子学院 * 根据代码生成的递归过程描述,语句 if (true) while (true) if (false) break else other 生成的三地址码: t3=false if_false t3 goto L4 goto L3 goto L5 label L4 other label L5 label L2 t2=true if_false t2 goto L3 goto L2 label L3 t1=true if_false t1 goto L1 label L1 Break语句对应的三地址码 6.4.3 if和while语句的代码生成过程样例(续) machunyan 西北工业大学软件与微电子学院 * 第6章 代码生成 6.1 中间代码概览 6.2 基本的代码生成技术 6.3 数据结构引用的代码生成 6.4 控制语句和逻辑表达式的代码生成 6.5 过程和函数调用的代码生成 machunyan 西北工业大学软件与微电子学院 * 6.5 过程和函数调用的代码生成 6.5.1 过程和函数的中间代码 6.5.2 函数定义和调用的代码生成过程(自学内容) 函数定义包括函数名、参数和函数代码。 函数定义的中间代码必须包括一条标志开始的指令,称函数代码的入口点(entry point),一条标志结束的指令,称为函数返回点(return point),翻译模式如下: Entry instruction code for the function body Return instruction machunyan 西北工业大学软件与微电子学院 * 6.5.1 过程和函数的中间代码(续) machunyan 西北工业大学软件与微电子学院 * 例如,考虑C函数定义 int f(int x, int y) {return x + y + 1;} 翻译成如下的三地址码: entry
显示全部