Intermediate Language Generation - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Intermediate Language Generation

Description:

Note how it resolves scopes using a stack but gives each local variable a unique ... Reducing number of pipeline stalls by instruction reordering ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 16
Provided by: mcsV
Category:

less

Transcript and Presenter's Notes

Title: Intermediate Language Generation


1
Intermediate Language Generation
  • COMP431 (Compilers) by Dr Alex Potanin

2
JKIL Generation in JKit
  • Done inside JavaFileReader
  • Note how it resolves scopes using a stack but
    gives each local variable a unique name
  • Constructs CFG as it parses the expressions
    inside methods
  • Parses a basic block at a time and connects these
    using conditional or exception edges
  • Look inside JavaFileReaders comments to see the
    strategies it uses to from from AST to JKIL!
  • Some examples are in this lecture

3
IL Generation (basic statements)
  • Breaking down compound expressions
  • Introduce temporary variables!

1 tmp y 2 2 x x tmp
x x (y 2)
1 tmp p.f 2 x tmp 2
x p.f 2
4
IL Generation (if statements)
  • Breaking down if statements
  • Use conditional branch instruction!
  • Breaking down if-then-else statements
  • Use conditional and unconditional branch
    instructions!

if(x lt 2) x x 1
1 if x gt 2 goto 3 2 x x 1 3
1 if x gt 2 goto 4 2 x x 1 3 goto 5 4
x x 2 5
if(x lt 2) x x 1 else x x 2
5
IL Generation (Short Circuiting)
  • Breaking compound if-conditions
  • Dont forget that and in Java use
    short-circuiting
  • When evaluating X Y, evaluate Y iff X
    evaluates to true
  • When evaluating X Y, evaluate Y iff X
    evaluates to false
  • Useful for statements which have side-effects
    (e.g. methods calls)

class Test int x 0 void f(Test p) 1
if x gt 2 goto 5 2 tmp p.f(p) 3 if tmp
! 0 goto 5 4 x x 1 5
class Test int x 0 int f(Test f) if(x
lt 2 f(p) 0) x x 1 return x
6
IL Generation (While Loops)
  • Breaking down while statements
  • Use conditional and unconditional branch
    instructions!
  • Breaking down do-while statements
  • Very similar to while statements!

1 if x gt 2 goto 4 2 x x 1 3 goto 1 4

while(x lt 2) x x 1
do x x 1 while(x lt 2)
1 x x 1 2 if x lt 2 goto 1 3
7
IL Generation (Field Declarations)
  • Parse modifiers
  • Parse type
  • Parse name
  • Parse initialiser
  • Create a new Field class
  • Special classes in jkit.core take care of storing
    information about classes, methods, and fields as
    a whole

8
IL Generation (Variables)
  • Important to figure out the right scope given
    just a name
  • Checks first if it is a local variable or field
  • Checks if it is inherited field
  • Checks if it is a filed in outer class
  • Check if it is a class
  • Otheriwse fail

9
IL Generation (Switch Statements)
  • Creates a flow graph with a block for each case
  • Connects multiple edges to each case, but some
    cases may fall through

switch (ltcondgt) case 1 X case 2 Y break
case 3 Z default W
ltcondgt
1
default
2
3
X
Y
Z
W
10
IL Generation (Exceptions)
  • To parse try, first translates the entire body
    inside the try statement
  • Then creates CFG for each catch clause
  • Then adds the exception edges for each possible
    exception and finally block
  • See JavaFileReader.parseTry method for more
    details!

11
Possible Optimisations
int f(int x) 1 y x 2 z y 3 goto 5
4 goto 6 5 goto 3 6 return z
int f(int x) 1 return x
  • Q) Optimisation what is it all about?
  • A) Many things! But typical objectives are
  • Reducing number of instructions representing
    program
  • Reducing number of pipeline stalls by instruction
    reordering
  • Improving performance in other ways, e.g.
    constant propagation, strength reduction.

12
Possible Optimisations
  • Constant Propagation
  • Propagate constant values to reduce number of
    local variables
  • One of the classic optimisations used by most
    compilers!

int f(int u) 1 w 1 2 x 2 3 y w
x 4 z y u 5 return z
int f(int u) 1 return 3 u
13
Possible Optimisations
  • Translating IL to Machine Code
  • IL statements correspond to 1 instructions on
    most CPUs
  • IL has unbounded number of variables
  • Most CPUs have a fixed number of registers
  • Must allocate each variable to a register - this
    is called register allocation
  • Reducing number of registers required can
    preventing spilling

add 1,1,0 // x is 1 mult 2,1,1 // y
is 2 sgt 3,2,1 bne 3,zero,label1 addi
2,2,1 label1
1 x 1 2 y x x 3 if y gt 1 goto 5 4 y
y 1 5
MIPS
IL
14
And finally, Java Bytecode
  • IL is very similar to Java Bytecode!
  • gt javap verbose Test
  • public int f(int)
  • Code
  • Stack2, Locals2, Args_size2
  • 0 iload_1
  • 1 iconst_2
  • 2 iadd
  • 3 istore_1
  • 4 iload_1
  • 5 iconst_2
  • 6 if_icmpne 13
  • 9 iload_1
  • 10 iconst_2
  • 11 imul
  • 12 istore_1
  • 13 iload_1
  • 14 ireturn

class Test public int f(int x) x x 2
if(x 2) x x 2 return x
15
Java Bytecode
  • ClassFileWriter takes care of translation JKil to
    Bytecode
  • Well look at bytecode first, before discussion
    the ClassFileWriter
Write a Comment
User Comments (0)
About PowerShow.com