Sunteți pe pagina 1din 22

CODE GENERATION

CODE GENERATION IN A NUTSHELL


This phase takes as input the intermediate representation (IR)
produced by the frond end of the compiler, along with the relevant
symbol table information, and produces as output a semantically
equivalent target program

CODE GENERATION: IS IT THE END?


Compilers that need to produce efficient target programs, include an
optimization phase prior to code generation.

- In general, the IR passes through the back end of a


compiler (code-optimization and code-generation phases) multiple
times before generating the target program

TASKS
1. Instruction selection
- involves choosing appropriate target-machine instructions to
implement the IR statements

2. Register allocation and assignment


- involves deciding what values to keep in which registers

3. Instruction ordering
- involves deciding in what order to schedule the execution of
instructions

ISSUES IN THE DESIGN OF THE CODE GENERATOR


1. Input to the Code Generator
2. The Target Program

3. Instruction Selection
4. Register Allocation

5. Evaluation Order

1. INPUT TO THE CODE GENERATOR

PUT THE BLAME ON THE INPUT


oFirst, the IR of the source program is the input to the code generator.

oSecond, the many choices for the IR include three-address


representations, virtual machine representations, linear representations
and graphical representations. Example of these representations, like
the three-address code, trees and DAGs are discussed in Chapter 6

PUT THE BLAME ON THE INPUT


Therefore, we assume that the front end has scanned, parsed and
translated the source program into a relatively low-level IR. Also,
we assume that all syntactic and static semantic errors have been
detected, necessary type checking has taken place, and typeconversion operators have been inserted when needed.

2. THE TARGET PROGRAM

GETS NA BANG YUNG TARGET [PROGRAM]?


o Most common target-machine architectures:
1. RISC (reduced instruction set computer)

2. CISC (complex instruction set computer)


3. Stack based

GETS NA BANG YUNG TARGET [PROGRAM]?


o If an absolute machine-language program is produced as the
output, it will be advantageous as it can be placed in a fixed
location in memory and immediately executed thus programs can
be compiled and executed quickly.
o If a relocatable machine-language program is produced as the
output, it will allow subprograms to be compiled separately.

o If an assembly-language program is produced as output, the


process of code generation will be easier.

3. INSTRUCTION SELECTION

ANG AKSYON SA MGA INSTRUCTION


o The code generator must map the IR program into a code
sequence that can be executed by the target machine complexity
of the said mapping is determined by:

The level of the IR


The nature of the instruction-set architecture
The desired quality of the generated code

ANG AKSYON SA MGA INSTRUCTION


For example:

-> redundant since it loads that has just


been stored

ANG AKSYON SA MGA INSTRUCTION


o Rule of thumb (or not): quality of the generated code is usually
determined by its speed and size
For example, the three-address statement a = a + 1 may be
implemented more efficiently by INC a

INC a

VS

ANG AKSYON SA MGA INSTRUCTION


o Instruction costs should also be studied to design good code
sequences. However, cost information is difficult to obtain. Deciding
which machine-code sequence is best for a given three-address
construct may also require knowledge about the context in which
that construct appears.

4. REGISTER ALLOCATION

TER, KUMUSTA IMONG REGISTER?


o Use of registers is subdivided into two subproblems:
1. Register allocation we select the set of variables that
will reside in registers at each point in the program

2. Register assignment we select the specific register that


a variable will reside in

TER, KUMUSTA IMONG REGISTER?


Register-pairs (an even and next odd-numbered register)

Example:
M x, y

- x is the even register, y is odd, product occupies the


entire even/odd register pair

D x, y

- x is the even register, y is odd, remainder occupies


the even register, quotient occupies the odd one

TER, KUMUSTA IMONG REGISTER?

5. EVALUATION ORDER

WAITER, PACHECK NG ORDER


o Some computation orders require fewer registers to hold
intermediate results than others this can affect the efficiency of
the target code.

S-ar putea să vă placă și