1-CodeGeneration Unit5 Chap8 Lecture44
1-CodeGeneration Unit5 Chap8 Lecture44
04/07/2025
Unit 5
Code Generation
The final phase in our compiler model is the code generator. It takes as input the
intermediate representation (IR) produced by the front end of the compiler,
along with relevant symbol table information, and produces as output a
semantically equivalent target program.
04/07/2025
04:29 PM
Task of a code Generator
Target code should be of high quality
execution time or space or energy
Code generator itself should run efficiently.
A code generator has three primary tasks:
Instruction selection,
Register allocation and assignment,
and Instruction ordering.
Instruction selection involves choosing appropriate target-machine instructions to
implement the IR statements.
Register allocation and assignment involves deciding what values to keep in which
registers.
Instruction ordering involves deciding in what order to schedule the execution of
instructions. 04/07/2025
04:29 PM
Code Generator in Reality
04/07/2025
04:29 PM
Issues in the design of a code
generator
Code generation depends on the intermediate representation, the target
language, and the run-time system, tasks such as instruction selection, register
allocation and assignment, and instruction ordering.
04/07/2025
04:29 PM
Input to the code generator
The input to the code generator is the intermediate representation of the source
program produced by the front end, along with information in the symbol table that
is used to determine the run-time addresses of the data objects denoted by the
names in the IR.
The choices for the IR include three-address representations such as quadruples,
triples, indirect triples; virtual machine representations such as bytecodes and
stack-machine code; linear representations such as postfix notation; and
graphical representations such as syntax trees and DAG's.
Front end has scanned, parsed, and translated the source program into a relatively
low-level IR, so that the values of the names appearing in the IR can be represented
by quantities that the target machine can directly manipulate, such as integers and
floating-point numbers.
04/07/2025
04:29 PM
The Target Program
The code generator must map the IR program into a code sequence that can be executed by the
target machine. The complexity of performing this mapping is determined by factors such as
the level of the IR
Low-level IR can help generate more efficient code.
If the IR is high level, the code generator may translate each IR statement into a
sequence of machine instructions using code templates. Such statement by-statement
code generation, however, often produces poor code that needs further optimization.
the nature of the instruction-set architecture
Uniformity and completeness of ISA affects the code
e.g., floats required to be loaded in special registers.
04/07/2025
04:29 PM
Instruction Selection contd..
04/07/2025
04:29 PM
Register allocation
Deciding what values to hold in what registers. Registers are the fastest
computational unit on the target machine, but we usually do not have enough of
them to hold all values.
Values not held in registers need to reside in memory. Instructions involving
register operands are invariably shorter and faster than those involving operands
in memory, so efficient utilization of registers is particularly important.
The use of registers is often subdivided into two subproblems:
1. Register allocation: which variables to be put into registers .
2. Register assignment: which register to use for a variable.
04/07/2025
04:29 PM
Evaluation Order
The order in which computations are performed can affect the efficiency of the target code.
some computation orders require fewer registers to hold intermediate results than others.
Have to provide the best evaluation order .
Instruction order affects execution efficiency.
Picking the best order is NP-complete.
Optimizer / Code generator needs to
look at multiple instructions at a time.
Our target computer models a three-address machine with load and store
operations, computation operations, jump operations, and conditional jumps. The
underlying computer is a byte-addressable machine with n general-purpose
registers, R0,R1,……Rn-1.
Load operations: The instruction LD dst , addr loads the value in location addr
into location dst.
An instruction of the form LD r1,r2 is a register-to-register copy in which the contents
of register r2 are copied into register r1.
Store operations: The instruction ST x,r stores the value in register r into the
location x. This instruction denotes the assignment x = r.
Computation operations of the form OP dst,src1,src2 ; where OP is a operator
like ADD or SUB, and dst, src1, and src2 are locations, not necessarily distinct.
04/07/2025
04:29 PM
Target Machine Model contd..
04/07/2025
04:29 PM
Addressing Modes
04/07/2025
04:29 PM