IR2-中间代码继承
1. 控制流语句与布尔表达式的中间代码继承
label(xxx)表示名为xxx的标签
S → i f ( B ) S 1 S → i f ( B ) S 1 e l s e S 2 S → w h i l e ( B ) S 1 \begin{array}{l} S \rightarrow if\ (B)\ S_1 \\ S \rightarrow if\ (B)\ S_1\ else\ S_2\\ S \rightarrow while\ (B)\ S_1 \\ \end{array} S→if (B) S1S→if (B) S1 else S2S→while (B) S1
1.1. 生成式转化为语义规则示例
1.2. 一些语句得到的中间代码示例
继承属性S.next:S的下一条指令
S.next为语句S指明了"跳出"S的目标
S . c o d e S . n e x t : \begin{aligned} &S.code \\ &S.next: \\ \end{aligned} S.codeS.next:
代表了表达式的翻译,包括数组引用
a s s i g n . c o d e \begin{aligned} &assign.code \\ \end{aligned} assign.code
B B B为真,则 n e w l a b e l newlabel newlabel,否则跳出 S 1 S_1 S1,上面写法不好,会给人在属性的类型上带来歧义,第二行应该是如下两行:
B . f a l s e = S . n e x t S 1 . n e x t = S . n e x t \begin{array}{l} B.false = S.next \\ S_1.next = S.next \\ \end{array} B.false=S.nextS1.next=S.next
翻译为中间代码如下
B . c o d e B . t r u e : S 1 . c o d e B . f a l s e ( S . n e x t ) : \begin{aligned} &B.code\\ &B.true:\ S_1.code\\ &B.false(S.next):\\ \end{aligned} B.codeB.true: S1.codeB.false(S.next):
- 左侧的
goto S.next
代表了执行完S1后,跳过else S2继续执行 - 由此右侧的代码可以翻译为如下的伪代码,其中第一行代表 S S S, t r u e true true的位置为 B B B,第二行为 S 1 S_1 S1, t r u e true true的位置为 B B B,第一个 a s s i g n assign assign为 S 11 S_{11} S11,第二个 a s s i g n assign assign为 S 12 S_{12} S12,剩下的一个 a s s i g n assign assign所在行为 S 2 S_2 S2。
B . c o d e B . t r u e : S 1 . c o d e g o t o S . n e x t B . f a l s e : S 2 . c o d e S . n e x t : \begin{aligned} &B.code \\ &B.true: S_1.code \\ &\qquad\qquad goto\ S.next \\ &B.false: S_2.code \\ &S.next:\\ \end{aligned} B.codeB.true:S1.codegoto S.nextB.false:S2.codeS.next:
- 继续进行翻译,同样的规定,其中第一行代表S,true的位置为B,第二行为S1,true的位置为B,第一个assign为S11,第二个assign为S12。
- 中间代码格式
b e g i n : B . c o d e B . t r u e : S 1 . c o d e g o t o b e g i n B . f a l s e : \begin{aligned} &begin: B.code \\ &B.true: S_1.code \\ &\qquad\qquad goto\ begin\\ &B.false: \\ \end{aligned} begin:B.codeB.true:S1.codegoto beginB.false:
中间代码如下
S 1 . c o d e S 1 . n e x t : S 2 . c o d e \begin{aligned} &S_1.code \\ &S_1.next: S_2.code \\ \end{aligned} S1.codeS1.next:S2.code
1.3. 生成式与语义规则对应表
![]() | ![]() |
---|
2. 短路求值
中间代码示例
B 1 . c o d e B 1 . f a l s e : B 2. c o d e B 1 . t r u e ( B . t r u e / B 2 . t r u e ) : B 2 . f a l s e : \begin{aligned} &B_1.code \\ &B_1.false: B2.code \\ &B_1.true(B.true/B_2.true):\\ &B_2.false:\\ \end{aligned} B1.codeB1.false:B2.codeB1.true(B.true/B2.true):B2.false:
中间代码示例
B 1 . c o d e B 1 . t r u e : B 2. c o d e B 2 . t r u e ( B . t r u e ) : B 1 . f a l s e ( B 2 . f a l s e / B . f a l s e ) : \begin{aligned} &B_1.code \\ &B_1.true: B2.code \\ &B_2.true(B.true):\\ &B_1.false(B_2.false/B.false):\\ \end{aligned} B1.codeB1.true:B2.codeB2.true(B.true):B1.false(B2.false/B.false):
rel是比较符号,得到的中间代码如下
中间代码示例
E 1 . c o d e E 2 . c o d e i f E 1 . a d d r r e l . o p E 2 . a d d r g o t o B . t r u e g o t o B . f a l s e B . t r u e : B . f a l s e : \begin{aligned} &E_1.code \\ &E_2.code \\ &if\ E_1.addr\ rel.op\ E_2.addr\ goto\ B.true \\ &\qquad goto\ B.false\\ &B.true:\\ &B.false:\\ \end{aligned} E1.codeE2.codeif E1.addr rel.op E2.addr goto B.truegoto B.falseB.true:B.false:
- 对于这个例子: E 1 . c o d e E_1.code E1.code是空,但是 E 1 . a d d r E_1.addr E1.addr是非空的
- 拆开分别分析,慢慢深入
3. 布尔表达式的作用: 布尔值vs. 控制流跳转
- 函数 j u m p ( t , f ) jump(t, f) jump(t,f): 生成控制流代码
- 函数 r v a l u e ( ) rvalue() rvalue(): 生成计算布尔值的代码, 并将结果存储在临时变量中
- 为E生成跳转代码, 在真假出口处将true或false存储到临时变量