编译原理 第八章 Code Generation

本书以 编译原理及实践 为textbook

Chapter Eight. Code Generation

a compiler typically breaks up this phase into several steps, involving various intermediate data structures, often including some form of abstract code called intermediate code.

issues:

  1. Two popular forms of intermediate code(three address code and P-code)
  2. The basic algorithms for generating intermediate or assembly code.
  3. Code generation techniques for various language features are discussed.
  4. Apply the techniques studied in previous sections to develop an assembly code generator for the TINY language.

intermediate representation(IR): A data structure that represents the source program during translation

  1. Three-Address Code
  2. P-Code

Three-Address Code: x = y op z
Data Structures: four fields are necessary: one for the operation and three for the addresses.
A different implementation of three-address code is use the instructions themselves to represent the temporaries. Such an implementation of three-address code is called a triple.Triples have one major drawback: any movement of their positions becomes difficult.

P-Code:
began as a standard target assembly code produced by a number of Pascal compilers
designed to be the actual code for a hypothetical stack machine

Comparison of P-Code to Three-Address Code:

  1. P-code is in many respects closer to actual machine code than three-address code.
  2. P-code instructions also require fewer addresses.
  3. P-code is less compact than three-address code in terms of numbers of instructions.
  4. P-code is not “ self-contained ” in that the instructions operate implicitly on a stack.

Basic Code Generation Techniques:

  1. Intermediate Code or Target Code as a Synthesized Attribute
    pseudo code:
Procedure gencode (T: treenode);
   Begin
     If T is not nil then
   	Generate code to prepare for code of left child of T;
   	Gencode(left child of T);
   	Generate code to prepare for code of right child of T;
   	Gencode(right child of T);
   	Generate code to implement the action of T;
   End;

Generation of Target Code from Intermediate Code
the final code generation pass must supply:

  1. all the actual locations of variables and temporaries
  2. the code necessary to maintain the runtime environment.

macro expansion: replacing each kind of intermediate code instruction with an equivalent sequence of target code instructions.
static simulation: a straight-line simulation of the effects of the intermediate code and generating target code to match these effects.

Address Calculations:

  1. Three-Address Code for Address Calculations
    an enumerated AddrMode field with possible values None, Address, and Indirect.
  2. P-Code for Address Calculations
    ind ixa

Each address must be computed :

  1. the base address of a (its starting address in memory )
  2. an offset that depends linearly on the value of the subscript.

The offset is computed from the subscript value as follows.

  1. an adjustment must be made to the subscript value if the subscript range does not begin at 0.
  2. the adjusted subscript value must be multiplied by a scale factor .
  3. the resulting scaled subscript is added to the base address to get the final address .

Record Structure and Pointer References:

  1. the base address of the structure variable is computed.
  2. the (usually fixed) offset of the named field is found
  3. the two are added to get the resulting address.

Code Generation for If – and While
Code Generation of Logical Expressions
Code Generation of Procedure and Function Calls

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值