a bcbc assign a assign b a uminus
a: =b*(-c)+b*(-c)的图表示法 assign a assign + * b a + * * uminus b uminus c 抽象语法树 c b uminus c DAG 编译原理
产生赋值语句抽象语法树的属性文法 产生式 S→id: =E E→E 1+E 2 E→E 1*E 2 E→-E 1 E→ (E 1) E→id 语义规则 S. nptr: =mknode(‘assign’, mkleaf(id, id. place), E. nptr) E. nptr: =mknode(‘+’, E 1. nptr, E 2. nptr) E. nptr: =mknode(‘*’, E 1. nptr, E 2. nptr) E. nptr: =mknode(‘uminus’, E 1. nptr) E. nptr: =E 1. nptr E. nptr: =mkleaf(id, id. place) 编译原理
a: =b*(-c)+b*(-c)的图表示法 assign a assign + * b a + * * uminus b uminus c 抽象语法树 c b uminus c DAG 编译原理
为赋值语句生成三地址代码的S-属性文法定义 产生式 S→id: =E E→E 1+E 2 E→E 1*E 2 E→-E 1 E→ (E 1) E→id 语义规则 S. code: =E. code || gen(id. place ‘: =’ E. place) E. place: =newtemp; E. code: =E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘+’ E 2. place) E. place: =newtemp; E. code: =E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘*’ E 2. place) E. place: =newtemp; E. code: =E 1. code || gen(E. place ‘: =’ ‘uminus’ E 1. place) E. place: =E 1. place; E. code: =E 1. code E. place: =id. place; E. code=‘ ’ 编译原理
三地址语句 x[i]: =y op (0) [ ] = (1) arg 1 x y arg 2 i x: =y[i] op (0) = [ ] (1) assign arg 1 y x arg 2 i n n 编译原理 (0)
为赋值语句生成三地址代码的S-属性文法定义 产生式 S→id: =E E→E 1+E 2 E→E 1*E 2 E→-E 1 E→ (E 1) E→id 语义规则 S. code: =E. code || gen(id. place ‘: =’ E. place) E. place: =newtemp; E. code: =E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘+’ E 2. place) E. place: =newtemp; E. code: =E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘*’ E 2. place) E. place: =newtemp; E. code: =E 1. code || gen(E. place ‘: =’ ‘uminus’ E 1. place) E. place: =E 1. place; E. code: =E 1. code E. place: =id. place; E. code=‘ ’ 编译原理
S→id: =E S. code: =E. code || gen(id. place ‘: =’ E. place) E→E 1+E 2 E. place: =newtemp; E. code: =E 1. code || E 2. code ||gen(E. place ‘: =’ E 1. place ‘+’ E 2. place) E→E 1*E 2 E. place: =newtemp; E. code: =E 1. code || E 2. code || gen(E. place ‘: =’ E 1. place ‘*’ E 2. place) 产生赋值语句三地址代码的翻译模式 S→id: =E { p: =lookup(id. name); if p nil then emit(p ‘: =’ E. place) else error } E→E 1+E 2 { E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘+’ E 2. place)} E→E 1*E 2 { E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘*’ E 2. place)} 编译原理
E→-E 1 E→ (E 1) E→id E. place: =newtemp; E. code: =E 1. code || gen(E. place ‘: =’ ‘uminus’ E 1. place) E. place: =E 1. place; E. code: =E 1. code E. place: =id. place; E. code=‘ ’ 产生赋值语句三地址代码的翻译模式 E→-E 1 { E. place: =newtemp; emit(E. place‘: =’ ‘uminus’E 1. place)} E→(E 1) { E. place: =E 1. place} E→id { p: =lookup(id. name); if p nil then E. place: =p else error } 编译原理
(1) (2) (3) (4) (5) (6) (7) (8) S→L: =E E→E+E E→(E) E→L L→Elist ] L→id Elist→ Elist, E Elist→id [ E 编译原理
(1) S→L: =E { if L. offset=null then /*L是简单变量*/ emit(L. place ‘: =’ E. place) else emit( L. place ‘ [’ L. offset ‘]’ ‘: =’ E. place)} (2) E→E 1 +E 2 { E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘+’ E 2. place)} 编译原理
(3) E→(E 1) {E. place: =E 1. place} (4) E→L { if L. offset=null then E. place: =L. place else begin E. place: =newtemp; emit(E. place ‘: =’ L. place ‘[’ L. offset ‘]’ ) end } 编译原理
A[i 1, i 2, …, ik] ((…i 1 n 2+i 2)n 3+i 3)…)nk+ik)×w + base-((…((low 1 n 2+low 2)n 3+low 3)…)nk+lowk)×w (8) Elist→id [ E { Elist. place: =E. place; Elist. ndim: =1; Elist. array: =id. place } 编译原理
A[ i 1, i 2, …, ik ] ( (…i 1 n 2+i 2)n 3+i 3)…)nk+ik)×w + base-((…((low 1 n 2+low 2)n 3+low 3)…)nk+lowk)×w (7) Elist→ Elist 1, E { t: =newtemp; m: =Elist 1. ndim+1; emit(t ‘: =’ Elist 1. place ‘*’ limit(Elist 1. array, m) ); emit(t ‘: =’ t ‘+’ E. place); Elist. array: = Elist 1. array; Elist. place: =t; Elist. ndim: =m 编译原理 }
A[i 1, i 2, …, ik] ((…i 1 n 2+i 2)n 3+i 3)…)nk+ik) ×w + base-((…((low 1 n 2+low 2)n 3+low 3)…)nk+lowk)×w (5) L→Elist ] { L. place: =newtemp; emit(L. place ‘: =’ Elist. array ‘-’ C); L. offset: =newtemp; emit(L. offset ‘: =’ w ‘*’ Elist. place) } (6) L→id { L. place: =id. place; L. offset: =null } 编译原理
关于产生式E→E 1 +E 2 的语义动作 { E. place: =newtemp; if E 1. type=integer and E 2. type=integer then begin emit (E. place ‘: =’ E 1. place ‘int+’ E 2. place); E. type: =integer end else if E 1. type=real and E 2. type=real then begin emit (E. place ‘: =’ E 1. place ‘real+’ E 2. place); E. type: =real end 编译原理
else if E 1. type=integer and E 2. type=real then begin u: =newtemp; emit (u ‘: =’ ‘inttoreal’ E 1. place); emit (E. place ‘: =’ u ‘real+’ E 2. palce); E. type: =real end else if E 1. type=real and E 1. type=integer then begin u: =newtemp; emit (u ‘: =’ ‘inttoreal’ E 2. place); emit (E. place ‘: =’ E 1. place ‘real+’ u); E. type: =real end else E. type: =type_error} 编译原理
7. 4. 1 数值表示法 n a or b and not c 翻译成 T 1: =not c T 2: =b and T 1 T 3: =a or T 1 n a<b的关系表达式可等价地写成 if a<b then 1 else 0 ,翻译成 100: if a<b goto 103 101: T: =0 102: goto 104 103: T: =1 104: 编译原理
关于布尔表达式的数值表示法的翻译模式 E→E 1 or E 2 {E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘or’ E 2. place)} E→E 1 and E 2 {E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘and’ E 2. place)} E→not E 1 {E. place: =newtemp; emit(E. place ‘: =’ ‘not’ E 1. place)} E→(E 1) {E. place: =E 1. place} 编译原理
关于布尔表达式的数值表示法的翻译模式 100: 101: 102: 103: 104: a<b 翻译成 if a<b goto 103 T: =0 goto 104 T: =1 E id 1 relop id 2 { E. place: =newtemp; emit(‘if’ id 1. place relop. op id 2. place ‘goto’ nextstat+3); emit(E. place ‘: =’ ‘ 0’); emit(‘goto’ nextstat+2); emit(E. place‘: =’ ‘ 1’) } E→id { E. place: =id. place } 编译原理
布尔表达式a<b or c<d and e<f的翻译结果 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: if a<b goto 103 T 1: =0 goto 104 T 1: =1 if c<d goto 107 T 2: =0 goto 108 T 2: =1 if e<f goto 111 T 3: =0 goto 112 T 3: =1 T 4: =T 2 and T 3 T 5: =T 1 or T 4 E id 1 relop id 2 { E. place: =newtemp; emit(‘if’ id 1. place relop. op id 2. place ‘goto’ nextstat+3); emit(E. place ‘: =’ ‘ 0’); emit(‘goto’ nextstat+2); emit(E. place‘: =’ ‘ 1’) } E→id { E. place: =id. place } E→E 1 or E 2 { E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘or’ E 2. place)} E→E 1 and E 2 { E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘and’ E 2. place) } 编译原理
7. 4. 2 作为条件控制的布尔式翻译 n 条件语句 if E then S 1 else S 2 赋予 E 两种出口: 一真一假 E. code E. true: E. false: S. next S 1. code goto S. next S 2. code …… 编译原理 To E. true To E. false
产生布尔表达式三地址代码的语义规则 产生式 E→E 1 or E 2 E 1. code To E. true To E 1. false E 2. code To E. true To E. false 语义规则 E 1. true: =E. true; E 1. false: =newlabel; E 2. true: =E. true; E 2. false: =E. false; E. code: =E 1. code || gen(E 1. false ‘: ’) || E 2. code 编译原理
产生布尔表达式三地址代码的语义规则 产生式 E→E 1 and E 2 E 1. code To E. false To E 1. true E 2. code To E. true To E. false 语义规则 E 1. true: =newlabel; E 1. false: =E. false; E 2. true: =E. true; E 2. false: =E. fasle; E. code: =E 1. code || gen(E 1. true ‘: ’) || E 2. code 编译原理
产生布尔表达式三地址代码的语义规则 产生式 语义规则 E→not E 1. true: =E. false; E 1. false: =E. true; E. code: =E 1. code E→ (E 1) E 1. true: =E. true; E 1. false: =E. false; E. code: =E 1. code 编译原理
产生布尔表达式三地址代码的语义规则 产生式 E→id 1 relop id 2 语义规则 E. code: =gen(‘if ’ id 1. place relop. op id 2. place ‘goto’ E. true) || gen(‘goto’ E. false) E→true E. code: =gen(‘goto’ E. true) E→false E. code: =gen(‘goto’ E. false) 编译原理
考虑如下表达式: a<b or c<d and e<f 假定整个表达式的真假出口已分别置为Ltrue 和Lfalse,则按定义将生成如下的代码: L 1: L 2: if a<b goto Ltrue goto L 1 if c<d goto L 2 goto Lfalse if e<f goto Ltrue goto Lfalse 编译原理
E 1. code 布尔表达式的翻译模式 To. E. E. true To false To E 11. . false true E 2. code To E. true To E. false (1) E→E 1 or M E 2 { backpatch(E 1. falselist, M. quad); E. truelist: =merge(E 1. truelist, E 2. truelist); E. falselist: =E 2. falselist } (2) E→E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist: =E 2. truelist; E. falselist: =merge(E 1. falselist, E 2. falselist) } 编译原理
布尔表达式的翻译模式 (3) E→not E 1 { E. truelist: =E 1. falselist; E. falselist: =E 1. truelist} (4) E→(E 1) { E. truelist: =E 1. truelist; E. falselist: =E 1. falselist} 编译原理
布尔表达式的翻译模式 (5) E→id 1 relop id 2 { E. truelist: =makelist(nextquad); E. falselist: =makelist(nextquad+1); emit(‘j’ relop. op ‘, ’ id 1. place ‘, ’ id 2. place‘, ’ emit(‘j, -, -, 0’) } (6) E→id { E. truelist: =makelist(nextquad); E. falselist: =makelist(nextquad+1); emit(‘jnz’ ‘, ’ id. place ‘, ’ ‘-’ ‘, ’ ‘ 0’); emit(‘ j, -, -, 0’) } 编译原理 ‘ 0’);
a<b or c<d and e<f 100 101 102 103 104 105 (j<, a, b, 0) (j, -, -, 102) (j<, c, d, 104) (j, -, -, 0) (j<, e, f, 100) (j, -, -, 103) truelist falselist 编译原理
关于布尔表达式的数值表示法的翻译模式 E id 1 relop id 2 { E. place: =newtemp; emit(‘if’ id 1. place relop. op id 2. place ‘goto’ nextstat+3); emit(E. place ‘: =’ ‘ 0’); emit(‘goto’ nextstat+2); emit(E. place‘: =’ ‘ 1’) } E→id { E. place: =id. place } E→E 1 or E 2 { E. place: =newtemp; emit(E. place ‘: =’ E 1. place ‘or’ E 2. place)} E→E 1 and E 2 { E. place: =newtemp; 编译原理 emit(E. place ‘: =’ E 1. place ‘and’ E 2. place) }
7. 4. 2 作为条件控制的布尔式翻译 n 条件语句 if E then S 1 else S 2 赋予 E 两种出口: 一真一假 E. code E. true: E. false: S. next S 1. code goto S. next S 2. code …… 编译原理 To E. true To E. false
产生布尔表达式三地址代码的语义规则 产生式 E→E 1 or E 2 E 1. code To E. true To E 1. false E 2. code To E. true To E. false 语义规则 E 1. true: =E. true; E 1. false: =newlabel; E 2. true: =E. true; E 2. false: =E. false; E. code: =E 1. code || gen(E 1. false ‘: ’) || E 2. code 编译原理
产生布尔表达式三地址代码的语义规则 产生式 E→E 1 and E 2 E 1. code To E. false To E 1. true E 2. code To E. true To E. false 语义规则 E 1. true: =newlabel; E 1. false: =E. false; E 2. true: =E. true; E 2. false: =E. fasle; E. code: =E 1. code || gen(E 1. true ‘: ’) || E 2. code 编译原理
产生布尔表达式三地址代码的语义规则 产生式 语义规则 E→not E 1. true: =E. false; E 1. false: =E. true; E. code: =E 1. code E→ (E 1) E 1. true: =E. true; E 1. false: =E. false; E. code: =E 1. code 编译原理
产生布尔表达式三地址代码的语义规则 产生式 E→id 1 relop id 2 语义规则 E. code: =gen(‘if ’ id 1. place relop. op id 2. place ‘goto’ E. true) || gen(‘goto’ E. false) E→true E. code: =gen(‘goto’ E. true) E→false E. code: =gen(‘goto’ E. false) 编译原理
布尔表达式的翻译模式 (1) E→E 1 or M E 2 { backpatch(E 1. falselist, M. quad); E. truelist: =merge(E 1. truelist, E 2. truelist); E. falselist: =E 2. falselist } (2) E→E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist: =E 2. truelist; E. falselist: =merge(E 1. falselist, E 2. falselist) } (3) E→not E 1 { E. truelist: =E 1. falselist; E. falselist: =E 1. truelist} (4) E→(E 1) { E. truelist: =E 1. truelist; 编译原理 E. falselist: =E 1. falselist}
布尔表达式的翻译模式 (5) E→id 1 relop id 2 { E. truelist: =makelist(nextquad); E. falselist: =makelist(nextquad+1); emit(‘j’ relop. op ‘, ’ id 1. place ‘, ’ id 2. place‘, ’ ‘ 0’); emit(‘j, -, -, 0’) } (6) E→id { E. truelist: =makelist(nextquad); E. falselist: =makelist(nextquad+1); emit(‘jnz’ ‘, ’ id. place ‘, ’ ‘-’ ‘, ’ ‘ 0’); emit(‘ j, -, -, 0’) } (7) M→ { M. quad: =nextquad } 编译原理
n if-then语句 S → if E then S 1 E. code E. true: E. false: S 1. code …… 编译原理 To E. true To E. false
if-then语句的属性文法 产生式 S→if E then S 1 E. code E. true: S. code 1 E. false: …… 语义规则 E. true: =newlabel; E. flase: =S. next; S 1. next: =S. next S. code: =E. code || To E. true gen(E. true ‘: ’) || S 1. code To E. false 编译原理
n if-then-else语句 S → if E then S 1 else S 2 E. code E. true: E. false: S. next S 1. code goto S. next S 2. code …… 编译原理 To E. true To E. false
if-then-else语句的属性文法 产生式 S→if E then S 1 else S 2 E. code E. true: E. false: S. next S 1. code goto S. next S 2. code …… To E. true To E. false 语义规则 E. true: =newlabel; E. false: =newlabel; S 1. next: =S. next S 2. next: =S. next; S. code: =E. code || gen(E. true ‘: ’) || S 1. code || gen(‘goto’ S. next) || gen(E. false ‘: ’) || S 2. code 编译原理
n while-do语句 S → while E do S 1 S. begin: E. true: E. false: E. code S 1. code goto S. begin …… 编译原理 To E. true To E. false
while-do语句的属性文法 产生式 S→while E do S 1 语义规则 S. begin: =newlabel; E. true: =newlabel; E. false: =S. next; S 1. next: =S. begin; S. code: =gen(S. begin ‘: ’) || S. begin: To E. true E. code || E. code To E. false gen(E. true ‘: ’) || S. code || E. true: S. code 1 1 gen(‘goto’ S. begin) goto S. begin E. false: …… 编译原理
考虑如下语句 : while a<b do if c<d then x: =y+z else x: =y-z n 生成下列代码: L 1: L 2: L 3: L 4: Lnext: if a<b goto L 2 goto Lnext if c<d goto L 3 goto L 4 T 1: =y+z x: =T 1 goto L 1 T 2: =y-z x: =T 2 goto L 1 编译原理
n if 语句的翻译 相关产生式 S → if E then S(1) | if E then S(1) else S(2) 改写后产生式 S → if E then M S 1 S → if E then M 1 S 1 N else M 2 S 2 M→ N→ 编译原理
翻译模式: 1. S→if E then M S 1 { backpatch(E. truelist, M. quad); S. nextlist: =merge(E. falselist, S 1. nextlist) } 2. S→if E then M 1 S 1 N else M 2 S 2 { backpatch(E. truelist, M 1. quad); backpatch(E. falselist, M 2. quad); S. nextlist: =merge(S 1. nextlist, N. nextlist, S 2. nextlist) } 3. M→ { M. quad: =nextquad } 4. N→ { N. nextlist: =makelist(nextquad); emit(‘j, -, -, -’) } 编译原理
n 翻译模式: 1. S→while M 1 E do M 2 S 1 { backpatch(S 1. nextlist, M 1. quad); backpatch(E. truelist, M 2. quad); S. nextlist: =E. falselist emit(‘j, -, -, ’ M 1. quad) } 2. M→ { M. quad: =nextquad } 编译原理
n 翻译模式: 1. L→L 1; M S { backpatch(L 1. nextlist, M. quad); L. nextlist: =S. nextlist } 2. M→ M. quad: =nextquad } 编译原理 {
n 其它几个语句的翻译 1. S→begin L end S. nextlist: =L. nextlist } { 2. S→A S. nextlist: =makelist( ) } { 3. L→S L. nextlist: =S. nextlist } { 编译原理
翻译语句 while (a<b) do if (c<d) then x: =y+z; P 195 P 190 (5) E→id S→if E then M id S 12 { E. truelist: =makelist(nextquad); 1 relop E. falselist: =makelist(nextquad+1); { backpatch(E. truelist, M. quad); S. nextlist: =merge(E. falselist, S 1. nextlist) emit(‘j’ relop. op ‘, ’ id 1. place ‘, ’ id 2. place‘, ’ ‘ 0’); } emit(‘j, -, -, 0’) } P 195 M→ { M. quad: =nextquad } S→while M 1 E do M 2 S 1 P 179 S→id: =E { p: =lookup(id. name); S→A { S. nextlist: =makelist( ) } { backpatch(S 1. nextlist, M 1. quad); if p nil then backpatch(E. truelist, M 2. quad); emit(p ‘: =’ E. place) S. nextlist: =E. falselist else error } E→E 1+E 2 { E. place: =newtemp; emit(‘j, -, -, ’ M. quad) } 1 emit(E. place ‘: =’ E 1. place ‘+’ E 2. place)} M→ { M. quad: =nextquad } 编译原理
翻译语句 while (a<b) do if (c<d) then x: =y+z; 100 101 102 103 104 105 106 107 (j<, a, b, 102) (j, -, -, 107) (j<, c, d, 104) (j, -, -, 100) (+, y, z, T) (: =, T, -, x) (j, -, -, 100) 编译原理
7. 5. 3 CASE语句的翻译 n 语句结构 case E of C 1: S 1; C 2: S 2; … Cn-1: Sn-1; otherwise: Sn end 编译原理
n ¨改进: 翻译法(一): L 1: L 2: T: =E if T C 1 goto L 2 S 1的代码 goto next if T C 2 goto L 3 S 2的代码 goto next L 3: … Ln-1: if T Cn-1 goto Ln Sn-1的代码 goto next Ln: Sn的代码 next: 编译原理
n 翻译法(二): L 1: 计算E并放入T中 goto test 关于S 1的中间码 goto next … Ln-1: 关于Sn-1的中间码 goto next Ln: 关于Sn的中间码 goto next (case, test: if T=C 1 goto L 1 (case, if T=C 2 goto L 2 … … if T=Cn-1 goto Ln-1 (case, goto Ln 编译原理 next: (label, Pi是Li在 符号表中 的位置 C 1, C 2, P 1) P 2) Cn-1, Pn-1) T, P n) NEXT, -, -)
n 翻译模式 3. Elist→E { 初始化queue仅包含E. place } 2. Elist→Elist, E { 将E. place加入到queue的队尾 } 1. S→call id (Elist) { for 队列queue中的每一项p do emit(‘param’ p); emit(‘call’ id. place) } 编译原理
- Slides: 116