Docsity

Intermediate Representation - Lecture Slides | CMSC 430, Papers of Computer Science

Toggle sidebar
bg1
Intermediate representations
source
code
front
end opt. back
end
target
code
ir ir
front end produces an intermediate representation (IR) for the program.
optimizer transforms the code in IR form into an equivalent program that
may run more efficiently.
back end transforms the code in IR form into native code for the target
machine
The IR encodes knowledge that the compiler has derived about the source
program.
CMSC 430 Lecture 8, Page 1
Intermediate representations
Advantages
•compiler can make multiple passes over program
•break the compiler into manageable pieces
•support multiple languages and architectures using multiple front & back
ends
•enables machine-independent optimization
Desirable properties
•easy & inexpensive to generate and manipulate
•contains sufficient information
Examples
•abstract syntax tree (AST)
•directed acyclic graph (DAG)
•control flow graph (CFG)
•three address code
•stack code
CMSC 430 Lecture 8, Page 2
Intermediate representations
Broadly speaking, IRs fall into three categories:
Structural
•structural IRs are graphically oriented
•examples: trees, directed acyclic graphs
•heavily used in source to source translators
•nodes, edges tend to be large
Linear
•pseudo-code for some abstract machine
•large variation in level of abstraction
•simple, compact data structures
•easier to rearrange
Hybrids
•combination of graphs and linear code
•attempt to take best of each
•examples: control-flow graph
CMSC 430 Lecture 8, Page 3
Abstract syntax tree
An abstract syntax tree (AST) is the procedure’s parse tree with the nodes for
most non-terminal symbols removed.
-
<id,x>*
<num,2><id,y>
This represents “x-2*y”.
For ease of manipulation, can use a linearized (operator) form of the tree.
x2y*-in postfix form.
CMSC 430 Lecture 8, Page 4
pf3
pf4

Partial preview of the text

Download Intermediate Representation - Lecture Slides | CMSC 430 and more Papers Computer Science in PDF only on Docsity!

source Intermediate representations code

end^ front

opt.

endback

target code

ir

ir

front end

(^) produces an intermediate representation (

IR

) for the program.

optimizer

(^) transforms the code in IR form into an equivalent program that

may run more efficiently.

back end

(^) transforms the code in IR form into native code for the target

machine

CMSC 430 program.The IR encodes knowledge that the compiler has derived about the source

Lecture 8, Page 1

Advantages Intermediate representations

(^) compiler can make multiple passes over program

(^) break the compiler into manageable pieces

ends^ support multiple languages and architectures using multiple front & back

(^) enables machine-independent optimization

Desirable properties

(^) easy & inexpensive to generate and manipulate

(^) contains sufficient information

Examples

(^) abstract syntax tree (AST)

(^) directed acyclic graph (DAG)

(^) control flow graph (CFG)

(^) three address code

(^) stack code

CMSC 430

Lecture 8, Page 2

Structural Broadly speaking, IRs fall into three categories: Intermediate representations

(^) structural IRs are graphically oriented

(^) examples: trees, directed acyclic graphs

(^) heavily used in source to source translators

(^) nodes, edges tend to be large

Linear

(^) pseudo-code for some abstract machine

(^) large variation in level of abstraction

(^) simple, compact data structures

(^) easier to rearrange

Hybrids

(^) combination of graphs and linear code

(^) attempt to take best of each

(^) examples: control-flow graph

CMSC 430 most non-terminal symbols removed. An abstract syntax tree (AST) is the procedure’s parse tree with the nodes for Abstract syntax tree

id, x

num,

id, y

This represents “

x - 2 * y

x 2 y * -For ease of manipulation, can use a linearized (operator) form of the tree.

(^) in postfix form.

CMSC 430

A directed acyclic graph (DAG) is an AST with a unique node for each value.Directed acyclic graph

id ,x

sin

id ,y

num

,

< id ,x

id ,z

x ←

2 * y + sin(2*x)

z ←

x / 2

CMSC 430

Lecture 8, Page 5

The control flow graph (CFG) models the transfers of control in the procedure. Control flow graph

(^) nodes in the graph are

(^) basic blocks

maximal-length straight-line blocks of code

loops, if-then-else, case, goto^ edges in the graph represent control flow

Example

if (x=y)

then

s

else

s

s

becomes

x=y

s

s

s

CMSC 430

Lecture 8, Page 6

Three address code generally allow statements of the form: Three address code

x ←

y op

z

Complex expressions like with a single operator and, at most, three names.

x - 2 * y

are simplified to

t

←

2 * y

t

←

x - t

Advantages

(^) compact form (direct naming)

(^) names for intermediate values

Register transfer language (RTL)

(^) only load/store instructions access memory

(^) all other operands are registers

(^) version of three address code for RISC

CMSC 430 Typical statement types Three address code

  1. assignments —

(^) x

←

y op

z

  1. assignments —

(^) x

←

op

y

  1. assignments —

(^) x

←

y[i]

  1. assignments —

(^) x

←

y

  1. branches —

(^) goto L

  1. conditional branches —

(^) if x

relop

y goto L

  1. procedure calls —

(^) param x

(^) and

(^) call p

  1. address and pointer assignments Can represent three address code using

(^) quadruples

x - 2 * y

(1) load

t

y

(2) loadi t

(3) mult

t3 t2 t

(4) load

t

x

(5) sub

t5 t4 t

CMSC 430

Arithmetic instructionsJava byte codes ineg

[...:i]

[...:-i]

iadd

[...:i1,i2]

(^) [...:i1+i2]

isub

[...:i1,i2]

(^) [...:i1-i2]

imul

[...:i1,i2]

(^) [...:i1*i2]

idiv

[...:i1,i2]

(^) [...:i1/i2]

Direct instructions iinc k a

[...]

[...]

local[k]

(^) local[k]+a

Branch instructions goto L

[...]

[...]

branch to L

ifeq L

[...:i]

[...]

branch if i = 0

ifne L

[...:i]

[...]

branch if i != 0

ifnull L

[...:o]

[...]

branch if o = null

ifnonnull L

[...:o]

[...]

branch if o != null

if icmpeq L

[...:i1:i2]

[...]

branch if i1 = i

if icmpne L

[...:i1:i2]

[...]

branch if i1 != i

if icmpgt L

[...:i1:i2]

[...]

branch if i

(^) i

if icmplt L

[...:i1:i2]

[...]

branch if i

(^) i

if acmpeq L

[...:o1:o2]

(^) [...] branch if o1 = o

if acmpne L

[...:o1:o2]

(^) [...] branch if o1 != o

CMSC 430

Lecture 8, Page 13

Constant loading Java byte codes iconst 0

[...]

[...:0]

iconst 1

[...]

[...:1]

aconst null

[...]

(^) [...:null]

ldc int i

[...]

(^) [...:i]

ldc string s

[...]

(^) [...:str(s)]

Locals operations iload k

[...]

(^) [...:local[k]]

aload k

[...]

(^) [...:local[k]]

istore k

[...:i]

[...]

local[k]

i

astore k

[...:o]

[...]

local[k]

o

Stack operations dup

[...:v]

(^) [...:v,v]

pop

[...:v]

[...]

swap

[...:v1,v2]

(^) [...:v2,v1]

Functions invoke

[...:args]

[...]

push stack frame, ...

ireturn

[...:i]

[...]

ret i, pop stack frame

areturn

[...:o]

[...]

ret o, pop stack frame

return

[...]

[...]

pop stack frame

CMSC 430

Lecture 8, Page 14

while (true) pc = code.start Java bytecode interpreter

switch (opcode(code[pc])) new pc = pc + inst len(code[pc]);

case iconst 1:

push(1); break;

case iload:

push(local[code[pc+1]]); break;

case istore:

t ← (^) pop();

local[code[pc+1]]

(^) t; break;

case iadd:

t (^) ←

(^) pop(); t

(^) pop();

push(t1 + t2); break;

case ifeq:

t ← (^) pop();

if (t = 0) new pc = code[pc+1]; break;

pc } (^) ←

new pc;

CMSC 430^ }

Lecture 8, Page 15