Title: Intermediate Language
1Intermediate Language
- COMP431 (Compilers) by Dr Alex Potanin
2Program Analysis Compilers
Source Code
Parser
Optimiser
Code Generator
IR
AST
Front End
Back End
- Parser translates program code to machine
readable form - Optimiser Translates Abstract Syntax Tree (AST)
into Intermediate Representation (IR) and
performs optimisations where possible. - Code Generator generates machine code as
efficiently as possible for target CPU. Many
compilers (e.g. gcc) can target many different
architectures!
3Abstract Syntax Tree (AST)
- Human readable form of source code
- Produced by parser!
Program
x 0 while(x lt 10) x x 1
While
x
0
lt
x
x
10
x
1
Java Code
Abstract Syntax Tree (AST)
4Example IL Program
- Intermediate Language (IL)
- Close to assembly language / Java Bytecode
- Unstructured control-flow
class Test int i1 void f(int y) 1 x
1 2 z 0 3 if x gt y goto 7 4 x x
2 5 z z 1 6 goto 4 7 this.i1
z
class Test private int i1 public void f(int
y) x 1 z 0 for(xltyxx2)
z i1 z
IL
5An Example of Intermediate Language
c ? Num // Integer constants v ? Var
// Local Variables t ? Terms //
Terminal expressions e,e1,e2 ? AExp //
Arithmetic Expressions s,s1,s2 ? Stmts //
statements L,L1,L2 ? Lab // Labels m ?
Methods // Methods F ? Fields // Fields C
? Classes // Class names t c v e
v.f new N(t1,,t2) t t1 t2 t1 t2
t1 t2 t1 lt t2 s L ve L1 v.f
t s s1s2 L1 goto L2 L1 if e
goto L2 L return t L v1 v2.m(t1,,tn)
6JKIL (Expressions)
- Expr Value LVal UnOp BinOp Invoke
- Cast New Exception
- LVal LocalVar Deref
- Value BoolVal CharVal ByteVal ShortVal
- IntVal LongVal FloatVal DoubleVal
- LocalVar Name
- UnOp ( '!' '' '-' '--' '' ) Expr
- Expr ( '--' '' )
- BinOp Expr ('' '-' '' '/' 'ltlt'
'gtgt' - 'ltltlt' '' '' '' '' ''
'lt' - 'lt' 'gt' 'gt' '' '!') Expr
- Deref Expr '.' Name
- Invoke Expr '.' Name '(' (Expr) ')
- Cast '(' Type ')' Expr
- New 'new' Type '(' (Expr) ')'
7JKIL (Statements)
- Stmt Assign Return Throw Nop
- Assign LVal '' Expr
- Return 'return' (Expr)?
- Throw 'throw' Expr
- The expressions and statements are defined inside
jkit.core.FlowGraph class (for you to examine in
your spare time) - Much more simple language than the AST produced
by the parser (no for loops, etc) - Much closer, but not too close to bytecode
8Lecture 15
9FlowGraph Use Example
- import jkit.core.FlowGraph
- FlowGraph.BinOp add1_2
- new FlowGraph.BinOp(FlowGraph.BinOp.ADD,
- new IntVal(1),
- new IntVal(2),
- Type.intType())
- Produces a representation of 1 2 expression of
type int
10Control-Flow Graphs (CFG)
- Simple graphical notation of instruction sequence
- Branches are represented by directed edges
void f(int y) 1 z 1 2 if y gt 10 goto
6 3 z z y 4 y y 1 5 goto 2 6
y 0
IL
CFG
11More Control-Flow Graphs
y?
void f(int y) 1 z 1 2 if y gt 1 goto 5
3 z y y 4 goto 6 5 z y y 6 y
z 1
y gt 1
yes
no
zyy
zyy
yz1
IL
CFG
12Flow Graph Example in JKit
- void aMethod(int x)
- String r
- if(x gt 0) r Greater"
- else r Smaller"
- System.out.println(r)
-
entry point
labelled edge is only taken if condition holds
(no conditions can overlap)
13Flow Graph Example in JKit
- void anotherMethod(int x)
- while(x lt 10)
- if(x -1) break
- x x 1
-
entry point
labelled edge is only taken if condition holds
(no conditions can overlap)
14JKIL in FlowGraph in JKit
- Maintains a list of edges in the flow graph
(x,y,c) where edge goes from x to y if condition
c (which can be null for nothing) holds - Contains unique names for each variable, thus
avoiding the need for scoping of local variables
(these are uniquely renamed when going from
ANTLRs AST to the JKIL) - Run JKit with -t jkil to view how JKIL looks
like (it is close, but not quite like bytecode)
15Example of JKIL
- void aMethod(int x)
- String 4r
- L0
- if (xint gt 0int)boolean goto L1
- 4rjava.lang.String Smaller"java.lang.Str
ing - goto L2
- L1
- 4rjava.lang.String Greater"java.lang.Str
ing - L2
-
- java.lang.System.outjava.io.PrintStream
- .println(4rjava.lang.String)void
- return
-
16Exception Edges
- In addition to conditional edges we saw, JKIL
also has exceptional edges labelled with the
corresponding exceptions
try is somewhere before here
first statement after catch
17JKIL and other ILs
- See Appels chapter 7 for an alternative IL that
is closer to bytecode than JKIL and contains some
optimisations that JKIL does not - JKil main design criteria was to simplify
dataflow analyses. - Reduced number of constructs compared with Java
- So less cases in every dataflow analysis!
- JKil supports recursive expressions, rather than
e.g.three-address code (e.g. xy op z) as
gimple, jimple do. - JKil does not use SSA form (for simplicity)
- Note that sequences of code statements without
any edges coming out of them (for exceptions or
conditionals) are termed basic blocks - Next lecture well look at generating IL from the
AST