0% found this document useful (0 votes)
23 views20 pages

Code Generation-20241219074111

The document outlines the course Comp6062 - Compilation Techniques, focusing on code generation in compiler design. It covers key topics such as issues in code generator design, target programs, memory management, instruction selection, and register allocation. The learning outcomes include the ability to demonstrate code generation results and create optimal compiler designs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views20 pages

Code Generation-20241219074111

The document outlines the course Comp6062 - Compilation Techniques, focusing on code generation in compiler design. It covers key topics such as issues in code generator design, target programs, memory management, instruction selection, and register allocation. The learning outcomes include the ability to demonstrate code generation results and create optimal compiler designs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Course : Comp6062 - Compilation

Techniques
Effective Period : September 2023

Code Generation

Session 25 - 26
Learning Outcomes

At the end of this meeting, expected student


will be able to:
• Students can show results from a case code
Genarator program compilation (C3)

• Students can make a diagram / schema


compiler is the most optimal design (C4)

Bina Nusantara University 2


Content Outline

• Issues in the design of code generator

• The target machine

• DAG representation of basic blocks

• Register allocation and assignment

Bina Nusantara University 3


Code Generation

• The last stage of the compiler is a code generator. Input


code generator is the intermediate representation of
source programs, and the output is the target of an
equivalent program.
• Position code generator in the compiler are as follows:

front intermediate code intermediate code


source target
end code optimizer code generator
program program

symbol

table

Bina Nusantara University 4


Terms or Requirement
code generator

• output code should be correct and of high


quality, which must use the resources of the
target machine effectively, must be efficient.

• Mathematically, the problem to generate


optimal code that is undecidable.

• In practice, we will use the heuristic


techniques are nice, but not necessarily
optimal code.

Bina Nusantara University 5


Issues in the Code Generator
Design

• Although highly dependent on the target


machine and operating system, issues that are
important in code generation are:
• Input to the code generator
• Target program
• Memory Management
• Instruction Selection
• Register Allocation
• Order Selection Evaluation
• Approach Code Generator

Bina Nusantara University 6


Input for Code
Generator

• Input to the code generator consists of an


intermediate representation of source programs
generated by the front-end, along with information in
the symbol table that is used to determine the run
time address of the data object indicated by the
names of the intermediate representation.
• It is assumed that prior to code generation, front-end
has to scan, to parse, and translate the source
program into an intermediate representation detail.
• In addition, assumed to have been done type
checking, so that type-conversion operator has been
inserted where necessary and semantic error has
been detected.

Bina Nusantara University 7


Target Program

• The output of code generator is the target program,


which could take the form: Absolute Machine
Language, Relocatable Machine Language, Assembly
Language

• Target programs in the form of Absolute machine


language had a profit can be placed in a fixed location
in memory and can be executed directly. Small
program can be compiled and executed quickly.

• Example: The number of "student-jobs' compiler, such


as WATFIV and PL / C Profit target program in the form
of Relocatable Machine Language Program (Object
Module) is a sub-program can be compiled separately.

Bina Nusantara University 8


Target Program

• One set of relocatable object modules can be linked


together and loaded by the linking loader to be
executed.

• Though out the extra effort for linking & loading, but
we have more flexibility because it can compile the
subroutine separately.

• Assembly Language program as a target program has


the advantage of ease of code generation process. We
can generate the symbolic instruction & use the
macro facilities of the assembler to generate code.
The cost is the stage of assembly after the code
generation.

Bina Nusantara University 9


Memory Management

• Mapping a name in the source program to the address of the


data object in the run-time memory is done jointly by the
front-end and code generator.
• It is assumed that the name in three-address statement
referring to the symbol-table entry for that name.
• Type in the declaration determines the width (number of
storage) required for the declared name.
– Register R0, R1, . . ., Rn-1
– op source, destination
MOV (move source to destination)
ADD (add source to destination)
SUB (subtract source to destination)
– Address mode
– Instruction Cost

Bina Nusantara University 10


Instruction Selection

Uniformity and completeness of the instruction set is an


important factor.
In addition, other important factors are speed and
machine instruction idioms.

Example:
Each three-address statement in the form: x: = y + z
where x, y, and z is allocated in a static, can be
translated into a sequence code:

LD y, R0 / * load y into register R0 * /


ADD z, R0 / * add z to R0 * /
ST R0, x / * store R0 into x * /

Bina Nusantara University 11


Instruction Selection

But the statement-by-statement code generation often


produces bad code. Example: The sequence of
statements
a: = b + c
d: = a + e

can be translated into:


LD b, R0
ADD c, R0
ST R0, a
LD a, R0
ADD e, R0
ST R0, d

Here the third and fourth statements would be redundant,


as well as a third statement if a is not used in sequence.
Bina Nusantara University 12
Register Allocation

• Instructions involving register operands are usually


shorter and faster than those involving operands in
memory.

• Therefore, the efficient use of registers is very important


to generate good code.

• The use of registers is divided into 2 sub-problems:


– Register Allocation, during which we select the set of
variables that will reside in registers at each point in
the program.
– Register assignment, during which wet pick the specific
register that a variable will be reside in.

• Finding an optimal assignment of registers to variables is


difficult, even with a single register. Mathematically, this
problem is NP-Complete.
Bina Nusantara University 13
Order Selection Evaluation

The order did computing can affect the efficiency of


target code. Several computational sequence requires
fewer registers to hold intermediate result than the
other. Taking the best order is NP-Complete Problem.

Bina Nusantara University 14


Approach Code Generator

• The most important criteria for code


generator is to produce good code.

• One of the important design objectives of the


Code Generator is:
– easily implemented
– easily tested
– easy to maintain

Bina Nusantara University 15


Basic Blocks as DAGs

Like expressions, we can represent basic blocks as


DAGs
• Create a node for the initial value of each
variable that appears in the block
• For each statement s, create a node N whose
children are the nodes defining the operands of
s
• Each node N is labelled by the operation used
• Each node N has a list of variables that it
defines
• A node N is marked as an output node if its
value is used outside the basic block

Bina Nusantara University 16


Basic Block Example

Bina Nusantara University 17


Run-Time Storage

• Allocation strategies: Static and Stack

Static allocation
In static allocation call statement is translated into:
Move #here + 20, callee.static_area
GOTO calle.code_area

Three-Address Activation Record Activation Record


Code for c (64 bytes) for p (88 bytes)
/* code for c */ 0:return address 0:return address
action1 8: 4:
call p arr buf
action2
halt 56:i 84:n
/* code for p */ 60:j
action3
return 18
Codes Generator - Example

01: st 1, x 14: add 1, R0


15: st R0, y
x:=1; 02: ld x,R0 16: jmp
y:=x+10; 03: add 10, R0 ,(20)
while (x<y) { 04: st R0, y 17: ld y, R0
x:=x+1; 05: ld x, R0 18: sub 2, R0
if (x%2==1) then 05: lt y,R0 19: st R0, y
y:=y+1; 06: jmpf R0, 20: jmp
else y:=y-2; (21) ,(5)
} 07: ld x, R0 21:
08: add 1, R0
09: st R0, x
10: mod 2, R0
11: eq 1, R0
12: jmpf R0,
(17)
13: ld y, R0
Bina Nusantara University 19
References
• Aho, A.V., Ravi, S., & Ullman, J.D. (2007). Compiler
: Principle, techniques and tools. 2nd. Addison-
Wesley. New York. ISBN : 0321491696, Chapter 8.5
– 8.11 (page 533-575)
• http://bears.ece.ucsb.edu/class/ece253/compiler_o
pt/c2.pdf

Bina Nusantara University 20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy