0% found this document useful (0 votes)
18 views15 pages

28-Code Generation - Issues-11-07-2023

The document discusses the role and challenges of code generation, emphasizing the need to preserve the semantics of the source program while producing high-quality target code. It outlines various types of code that can be generated, such as high-level languages and machine code, and highlights issues like instruction selection, register allocation, and instruction ordering. The document also notes that generating optimal target programs is undecidable and relies on approximation algorithms and heuristics due to the complexity of the problems involved.

Uploaded by

Piyush Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

28-Code Generation - Issues-11-07-2023

The document discusses the role and challenges of code generation, emphasizing the need to preserve the semantics of the source program while producing high-quality target code. It outlines various types of code that can be generated, such as high-level languages and machine code, and highlights issues like instruction selection, register allocation, and instruction ordering. The document also notes that generating optimal target programs is undecidable and relies on approximation algorithms and heuristics due to the complexity of the problems involved.

Uploaded by

Piyush Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Code Generation

Role of Code Generator


• From IR to target program.
• Must preserve the semantics of the source program.
• Meaning intended by the programmer in the original source program should
carry forward in each compilation stage until code-generation.
• Target code should be of high quality
• execution time or space or energy or …
• Code generator itself should run efficiently.
• instruction selection,
• register allocation and
• instruction ordering.
What Kind of Code Can We Generate?

• Code in another high-level language E.g. C


• An intermediate representation E.g. LLVM
• Executable virtual machine code E.g. JVM
• Assembly language
• Relocatable machine language
• Absolute machine language
Issues in the design of a code generator

1. Input to code generator

2. Target program

3. Instruction selection

4. Register allocation

5. Evaluation order
Code Generator in Reality

• The problem of generating an optimal target program is undecidable.

• Several subproblems are NP-Hard (such as register allocation).

• Need to depend upon


• Approximation algorithms

• Heuristics

• Conservative estimates
1. Input and Output
2. IR and Target Code
2. IR and Target Code (Contd…)
3. Instruction Selection
• Factors determining the complexity of the target code
1. Level of the IR
• High-level
• translate each IR statement into a sequence of machine instructions using code templates

• often produces poor code that needs further optimization

• Low-level
• generate more efficient code sequences
3. Instruction Selection (Contd…)
• Factors determining the complexity of the target code
2. Nature of the instruction-set architecture
• Uniformity and completeness of ISA affects the code E.g. floating-point operations are
done using separate registers.
• E.g: x = y + z a=b+c
d=a+e
LD R0, b // R0 = b
LD R0, y // R0 = y (load y into register R0) ADD R0, R0, c // R0 = R0 + c
ADD R0, R0, z // R0 = R0 + z (add z to R0 ) ST a, R0 // a = R0
LD R0, a // R0 = a Redundant
ST x, R0 // x = R0 (store R0 into x)
ADD R0, R0, e // R0 = R0 + e
ST d, R0 // d = R0
3. Instruction Selection (Contd…)
• Factors determining the complexity of the target code
3. Desired quality of the generated code
• Context and amount of information to process affects the code quality

• Speed and size plays a major role

E.g. Increment operation at target machine

LD R0, a // R0 = a
INC A ADD R0, R0, 1 // R0 = R0 + 1
ST a, R0 // a = R0

Which one is efficient?


4. Register Allocation
• Register allocation involves
• Allocation: which variables to be put into registers
• Assignment: which register to use for a variable

• Finding an optimal assignment of registers to variables is NP-Complete.

• Architectural conventions complicate matters.


• Combination of registers used for double-precision arithmetic.
• Result is stored in accumulator.
• Registers are reserved for special instructions.
• E.g: M x,y (Certain machine requires register pairs consist of an even and next odd-numbered register.)
5. Instruction Ordering
• Instruction order affects execution efficiency.
• Picking the best order is NP-complete.
• Optimizer / Code generator needs to look at multiple instructions at a
time.
Code Generation - Example

You might also like