SlideShare a Scribd company logo
Eelco Visser
IN4303 Compiler Construction
TU Delft
November 2017
Declare Your Language
Chapter 10:
Virtual Machines & Code Generation
Virtual Machines & Code Generation
2
backend
frontend
parse desugar analyse normalise
generate optimise format
Linguistic Abstraction
- High-level programming languages abstract from low-level machine mechanics

- Abstractions in imperative and object-oriented languages

- Calling Conventions

- Register machines vs stack machines

Java Virtual Machine
- Architecture

- Operation stack

- Constant pool

- Local variables

- Heap

- Stack frames

- Class ïŹles

Code Generation Mechanics
- Printing strings

- String concatenation

- String interpolation

- Transformation
Outline
3
Reading Material
4
5
Java Virtual Machine
Tim Lindholm, Frank Yellin: The Java Virtual Machine SpeciïŹcation, 2nd
edition. Addison-Wesley, 1999.

Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000.

Activation Records
Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java,
2nd edition. 2002
Literature: VMs & Compilation
6
Imperative Languages
- Carl A. Gunter: Semantics of Programming Languages: Structures and
Techniques. MIT Press, 1992

- Kenneth C. Louden: Programming Languages: Principles and Practice. Course
Technology, 2002

Object-Oriented Languages
- Martin Abadi, Luca Cardelli: A Theory of Objects. Springer, 1996.

- Kim B. Bruce: Foundations of Object-Oriented Programming Languages:
Types and Semantics. MIT Press, 2002.

- Timothy Budd: An Introduction to Object-Oriented Programming. Addison-
Wesley, 2002.
Literature: Semantics of Programming Languages
7
State & Control
8
Machine state
- a pile of data stored in memory

- memory hierarchy: registers, RAM, disk, network, 
 

Imperative program
- computation is series of changes to memory

- basic operations on memory (increment register)

- controlling such operations (jump, return address, 
)

- control represented by state (program counter, stack, 
)
State
9
General purpose registers
- accumulator AX - arithmetic operations

- counter CX - shift/rotate instructions, loops

- data DX - arithmetic operations, I/O

- base BX - pointer to data

- stack pointer SP, base pointer BP - top and base of stack

- source SI, destination DI - stream operations

Special purpose registers
- segments SS, CS, DS, ES, FS, GS

- ïŹ‚ags EFLAGS
Registers: x86 Family
10
Example: x86 Assembler
11
mov AX [1]
mov CX AX
L: dec CX
mul CX
cmp CX 1
ja L
mov [2] AX
read memory
write memory
calculation
jump
Example: Java Bytecode
12
.method static public m(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Math/m(I)I
imul
ireturn
read memory
calculation
jump
Memory abstractions
- variables: abstract over data storage

- expressions: combine data into new data

- assignment: abstract over storage operations

Control-ïŹ‚ow abstractions
- structured control-ïŹ‚ow: abstract over unstructured jumps

- ‘go to statement considered harmful’ Edgser Dijkstra, 1968
Memory & Control Abstractions
13
Example: C
14
int f = 1
int x = 5
int s = f + x
while (x > 1) {
f = x * f ;
x = x - 1
}
variable
expression
assignment
control flow
Example: Tiger
15
/* factorial function */
let
var f := 1
var x := 5
var s := f + x
in
while x > 1 do (
f := x * f ;
x := x - 1
)
end
variable
expression
assignment
control flow
Procedures
16
Control-ïŹ‚ow abstraction
- Procedure: named unit of computation

- Procedure call: jump to unit of computation and return

Memory abstraction
- Formal parameter: the name of the parameter

- Actual parameter: value that is passed to procedure

- Local variable: temporary memory

Recursion
- Procedure may (indirectly) call itself

- Consequence?
Procedural Abstraction
17
Example: Procedures in C
18
#include <stio.h>
/* factorial function */
int fac( int num ) {
if (num < 1)
return 1;
else
return num * fac(num - 1) ;
}
int main() {
int x = 10 ;
int f = fac( x ) ;
int x printf(“%d! = %dn”, x, f);
return 0;
}
formal parameter
actual parameter
local variable
recursive call
Procedures in Tiger
19
/* factorial function */
let
function fac( n: int ) : int =
let
var f := 1
in
if n < 1 then
f := 1
else
f := (n * fac(n - 1) );
f
end
var f := 0
var x := 5
in
f := fac( x )
end
formal parameter
actual parameter
local variable
recursive call
Stack
- temporary storage

- grows from high to low memory addresses

- starts at SS

Stack frames
- return address

- local variables

- parameters

- stack base: BP

- stack top: SP
Implementing Procedures with Stack and Stack Frames
20
General purpose registers
- accumulator AX - arithmetic operations

- counter CX - shift/rotate instructions, loops

- data DX - arithmetic operations, I/O

- base BX - pointer to data

- stack pointer SP, base pointer BP - top and base of stack

- source SI, destination DI - stream operations

Special purpose registers
- segments SS, CS, DS, ES, FS, GS

- ïŹ‚ags EFLAGS
Reminder: Registers in x86 Family
21
Example: Procedures in x86 Assembler
22
push 21
push 42
call _f
add SP 8
push BP
mov BP SP
mov AX [BP + 8]
mov DX [BP + 12]
add AX DX
pop BP
ret
pass parameter
access parameter
new stack frame
old stack frame
free parameters
Caller
- push parameters right-to-left on the stack

- clean-up stack after call 

Callee
- save old BP

- initialise new BP

- save registers

- return result in AX

- restore registers

- restore BP
Calling Conventions: CDECL
23
push 21
push 42
call _f
add ESP 8
push EBP
mov EBP ESP
mov EAX [EBP + 8]
mov EDX [EBP + 12]
add EAX EDX
pop EBP
ret
caller
- push parameters right-to-left on the stack

callee
- save old BP

- initialise new BP

- save registers

- return result in AX

- restore registers

- restore BP
Calling Conventions: STDECL
24
push 21
push 42
call _f@8
push EBP
mov EBP ESP
mov EAX [EBP + 8]
mov EDX [EBP + 12]
add EAX EDX
pop EBP
ret 8
Caller
- passes parameters in registers

- pushes additional parameters right-to-left ‹
on the stack

Callee
- save old BP, initialise new BP

- save registers

- return result in AX

- restore registers

- restore BP

- cleans up the stack
Calling Conventions: FASTCALL
25
mov ECX 21
mov EDX 42
call @f@8
push EBP
mov EBP ESP
mov EAX ECX
add EAX EDX
pop EBP
ret
Procedure declarations
- in principle: full freedom

- project constraints

- target platform constraints

Procedure calls
- need to match procedure declarations

Precompiled libraries
- avoid recompilation

- source code not always available

Standardization
- compilers / high-level languages standardize use of calling conventions

- portable code: does not depend on particular calling convention
Calling Conventions
26
Object-Oriented Languages
(home work)
27
Objects
- generalisation of records

- identity

- state

- behaviour

Messages
- objects send and receive messages

- trigger behaviour

- imperative realisation: method calls
Modularity: Objects & Messages
28
Classes
- generalisation of record types

- characteristics of objects: attributes, ïŹelds, properties

- behaviour of objects: methods, operations, features

Encapsulation
- interface exposure

- hide attributes & methods

- hide implementation
Modularity: Classes
29
public class C {
public int f1;
private int f2;
public void m1() { return; }
private C m2(C c) { return c; }
}
Inheritance
- inherit attributes & methods

- additional attributes & methods

- override behaviour

- nominative subtyping

Interfaces
- avoid multiple inheritance 

- interface: contract for attributes & methods

- class: provide attributes & methods

- nominative subtyping
Inheritance vs Interfaces
30
public class C {
public int f1;
public void m1() {
}
public void m2() {
}
}
public class D extends C {
public int f2;
public void m2() {
}
public void m3() {
}
}
public interface I {
public int f;
public void m();
}
public class E implements I {
public int f;
public void m() {
}
public void m’() {
}
}
Ad-hoc polymorphism
- overloading

‣ same method name, independent classes

‣ same method name, same class, diïŹ€erent parameter types

- overriding

‣ same method name, subclass, compatible types 

Universal polymorphism
- subtype polymorphism

‣ inheritance, interfaces

- parametric polymorphism
Polymorphism
31
Dispatch
- link method call to method

Static dispatch
- type information at compile-time

Dynamic dispatch
- type information at run-time

- single dispatch: one parameter

- multiple dispatch: more parameters
Static vs. Dynamic Dispatch
32
Single Dispatch in Java
33
public class A {} public class B extends A {} public class C extends B {}
public class D {
public A m(A a) { System.out.println("D.m(A a)"); return a; }
public A m(B b) { System.out.println("D.m(B b)"); return b; }
}
public class E extends D {
public A m(A a) { System.out.println("E.m(A a)"); return a; }
public B m(B b) { System.out.println("E.m(B b)"); return b; }
}
A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E();
A ab = b; A ac = c; D de = e;
d. m(a); d. m(b); d. m(ab); d. m(c); d. m(ac);
e. m(a); e. m(b); e. m(ab); e. m(c); e. m(ac);
de.m(a); de.m(b); de.m(ab); de.m(c); de.m(ac);
Methods
- parameter types

- return type

Covariance
- method in subclass

- return type: subtype of original return type

Contravariance
- method in subclass

- parameter types: supertypes of original parameter types
Overriding
34
Overloading vs Overriding
35
public class F {
public A m(B b) { System.out.println("F.m(B b)"); return b; }
}
public class G extends F {
public A m(A a) { System.out.println("G.m(A a)"); return a; }
}
public class H extends F {
public B m(B b) { System.out.println("H.m(B b)"); return b; }
}
A a = new A(); B b = new B(); F f = new F(); G g = new G(); H h = new H();
A ab = b;
f.m(b);
g.m(a); g.m(b); g.m(ab);
h.m(a); h.m(b); h.m(ab);
Invariance
36
public class X {
public A a;
public A getA() { return a ; }
public void setA(A a) { this.a = a ; }
}
public class Y extends X {
public B a;
public B getA() { return a ; }
public void setA(B a) { this.a = a ; }
}
A a = new A(); B b = new B(); X y = new Y();
y.getA(); y.setA(b); y.setA(a);
String[] s = new String[3] ; Object[] o = s ; o[1] = new A();
Summary: Abstractions
37
Imperative languages
- subroutines, routines, procedures, functions, methods

- scoping: local variables

- declarations with parameters (formal parameters)

- calls with arguments (actual parameters)

- pass by value, pass by reference 

Machine code
- jumps: call and return

- call stack: return address, parameters, private data

- procedure prologue and epilogue
Abstractions for Memory and Control
38
Imperative languages
- state & statements

- abstraction over machine code

- control ïŹ‚ow & procedures

- types

Object-oriented languages
- objects & messages

- classes

- inheritance

- types
Imperative vs Object-Oriented
39
Java Virtual Machine:
Architecture
40
JVM Architecture
41
JVM Architecture: Threads
42
JVM Architecture: Thread
43
JVM Architecture: Stack
44
JVM Architecture: Heap
45
JVM Architecture: Method Area
46
JVM Architecture: JVM Stack
47
48
Reverse Engineering the JVM Instruction Set
JVM: Control Flow
49
Bytecode Instructions: Goto
50
pc: 00
method area
00
01
02
03
04
05
06
A7
00
04
00
A7
FF
FF
goto
04
nop
goto
03
Bytecode Instructions: Goto
51
pc: 04
method area
00
01
02
03
04
05
06
A7
00
04
00
A7
FF
FF
goto
04
nop
goto
03
Bytecode Instructions: Goto
52
pc: 03
method area
00
01
02
03
04
05
06
A7
00
04
00
A7
FF
FF
goto
04
nop
goto
03
Bytecode Instructions: Goto
53
pc: 04
method area
00
01
02
03
04
05
06
A7
00
04
00
A7
FF
FF
goto
04
nop
goto
03
JVM: Operand Stack
54
Operand Stack
55
pc: 00
00
01
02
03
04
05
06
pc: 00 optop: 00
00
01
02
03
04
05
06
04
05
10
2A
11
43
03
iconst_1
iconst_2
bipush
sipush
method area stack
Operand Stack
56
optop: 01pc: 01
00
01
02
03
04
05
06
0000 000100
01
02
03
04
05
06
04
05
10
2A
11
43
03
iconst_1
iconst_2
bipush
sipush
method area stack
Operand Stack
57
optop: 02pc: 02
00
01
02
03
04
05
06
0000 0001
0000 0002
00
01
02
03
04
05
06
04
05
10
2A
11
43
03
iconst_1
iconst_2
bipush
sipush
method area stack
Operand Stack
58
optop: 03pc: 04
00
01
02
03
04
05
06
0000 0001
0000 0002
0000 002A
00
01
02
03
04
05
06
04
05
10
2A
11
43
03
iconst_1
iconst_2
bipush
sipush
method area stack
Operand Stack
59
optop: 04pc: 07
00
01
02
03
04
05
06
0000 0001
0000 0002
0000 002A
0000 4303
00
01
02
03
04
05
06
04
05
10
2A
11
43
03
iconst_1
iconst_2
bipush
sipush
method area stack
Operand Stack
60
optop: 04
00
01
02
03
04
05
06
0000 0001
0000 0002
0000 002A
0000 4303
pc: 07
07
08
09
0A
0B
0C
0D
60
68
5F
64
9A
FF
F5
iadd
imul
swap
isub
ifne
00
method area stack
Operand Stack
61
optop: 03
00
01
02
03
04
05
06
0000 0001
0000 0002
0000 432D
pc: 08
07
08
09
0A
0B
0C
0D
60
68
5F
64
9A
FF
F5
iadd
imul
swap
isub
ifne
00
method area stack
Operand Stack
62
optop: 02
00
01
02
03
04
05
06
0000 0001
0000 865A
pc: 09
07
08
09
0A
0B
0C
0D
60
68
5F
64
9A
FF
F5
iadd
imul
swap
isub
ifne
00
method area stack
Operand Stack
63
optop: 02
00
01
02
03
04
05
06
0000 865A
0000 0001
pc: 0A
07
08
09
0A
0B
0C
0D
60
68
5F
64
9A
FF
F5
iadd
imul
swap
isub
ifne
00
method area stack
Operand Stack
64
optop: 01
00
01
02
03
04
05
06
0000 8659
pc: 0B
07
08
09
0A
0B
0C
0D
60
68
5F
64
9A
FF
F5
iadd
imul
swap
isub
ifne
00
method area stack
Operand Stack
65
00
01
02
03
04
05
06
pc: 00 optop: 00
00
01
02
03
04
05
06
04
05
10
2A
11
43
03
iconst_1
iconst_2
bipush
sipush
method area stack
JVM: Constant Pool
66
Constant Pool
67
pc: 00
method area stack
00
01
02
03
04
05
06
12
00
12
01
14
00
02
ldc
00
ldc
01
ldc2_w
02
00
01
02
03
04
05
06
constant pool
00
01
02
03
04
05
06
0000 002A
0000 4303
0000 0000
0000 002A
optop: 00
Constant Pool
68
pc: 02
method area stack
00
01
02
03
04
05
06
12
00
12
01
14
00
02
ldc
00
ldc
01
ldc2_w
02
00
01
02
03
04
05
06
0000 002A
constant pool
00
01
02
03
04
05
06
0000 002A
0000 4303
0000 0000
0000 002A
optop: 01
Constant Pool
69
pc: 04
method area stack
00
01
02
03
04
05
06
12
00
12
01
14
00
02
ldc
00
ldc
01
ldc2_w
02
00
01
02
03
04
05
06
0000 002A
0000 4303
constant pool
00
01
02
03
04
05
06
0000 002A
0000 4303
0000 0000
0000 002A
optop: 02
Constant Pool
70
pc: 07
method area stack
00
01
02
03
04
05
06
12
00
12
01
14
00
02
ldc
00
ldc
01
ldc2_w
02
00
01
02
03
04
05
06
0000 002A
0000 4303
0000 0000
0000 002A
constant pool
00
01
02
03
04
05
06
0000 002A
0000 4303
0000 0000
0000 002A
optop: 04
JVM: Local Variables
71
Local Variables
72
pc: 00
method area stack
00
01
02
03
04
05
06
04
3B
1A
3C
84
01
01
iconst_1
istore_0
iload_0
istore_1
iinc
01
01
00
01
02
03
04
05
06
local variables
00
01
02
03
04
05
06
optop: 00
Local Variables
73
pc: 01
00
01
02
03
04
05
06
0000 0001
method area stack
00
01
02
03
04
05
06
04
3B
1A
3C
84
01
01
iconst_1
istore_0
iload_0
istore_1
iinc
01
01
local variables
00
01
02
03
04
05
06
optop: 01
Local Variables
74
pc: 02
00
01
02
03
04
05
06
method area stack
00
01
02
03
04
05
06
04
3B
1A
3C
84
01
01
iconst_1
istore_0
iload_0
istore_1
iinc
01
01
local variables
00
01
02
03
04
05
06
0000 0001
optop: 00
Local Variables
75
pc: 03
00
01
02
03
04
05
06
0000 0001
method area stack
00
01
02
03
04
05
06
04
3B
1A
3C
84
01
01
iconst_1
istore_0
iload_0
istore_1
iinc
01
01
local variables
00
01
02
03
04
05
06
0000 0001
optop: 01
Local Variables
76
pc: 04
00
01
02
03
04
05
06
method area stack
00
01
02
03
04
05
06
04
3B
1A
3C
84
01
01
iconst_1
istore_0
iload_0
istore_1
iinc
01
01
local variables
00
01
02
03
04
05
06
0000 0001
0000 0001
optop: 00
Local Variables
77
pc: 07
00
01
02
03
04
05
06
method area stack
00
01
02
03
04
05
06
04
3B
1A
3C
84
01
01
iconst_1
istore_0
iload_0
istore_1
iinc
01
01
local variables
00
01
02
03
04
05
06
0000 0001
0000 0002
optop: 00
JVM: Heap
78
Heap
79
pc: 00
method area stack
heap
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
00
01
02
03
04
05
06
12
00
19
00
12
01
2E
ldc
00
aload
00
ldc
01
iaload
00
01
02
03
04
05
06
constant pool
00
01
02
03
04
05
06
4303 4303
0000 0004
local variables
00
01
02
03
04
05
06
002A 002A
optop: 00
Heap
80
pc: 02
method area stack
heap
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
00
01
02
03
04
05
06
12
00
19
00
12
01
2E
ldc
00
aload
00
ldc
01
iaload
00
01
02
03
04
05
06
4303 4303
constant pool
00
01
02
03
04
05
06
4303 4303
0000 0004
local variables
00
01
02
03
04
05
06
002A 002A
optop: 01
Heap
81
pc: 04
method area stack
heap
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
00
01
02
03
04
05
06
12
00
19
00
12
01
2E
ldc
00
aload
00
ldc
01
iaload
00
01
02
03
04
05
06
4303 4303
002A 002A
constant pool
00
01
02
03
04
05
06
4303 4303
0000 0004
local variables
00
01
02
03
04
05
06
002A 002A
optop: 02
Heap
82
pc: 06
method area stack
heap
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
00
01
02
03
04
05
06
12
00
19
00
12
01
2E
ldc
00
aload
00
ldc
01
iaload
00
01
02
03
04
05
06
4303 4303
002A 002A
0000 0004
constant pool
00
01
02
03
04
05
06
4303 4303
0000 0004
local variables
00
01
02
03
04
05
06
002A 002A
optop: 03
Heap
83
pc: 07
method area stack
heap
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
00
01
02
03
04
05
06
12
00
19
00
12
01
2E
ldc
00
aload
00
ldc
01
iaload
00
01
02
03
04
05
06
4303 4303
0000 0042
constant pool
00
01
02
03
04
05
06
4303 4303
0000 0004
local variables
00
01
02
03
04
05
06
002A 002A
optop: 02
JVM: Stack Frames
84
Dispatch
- link method call to method

Static dispatch
- type information at compile-time

Dynamic dispatch
- type information at run-time

- single dispatch: one parameter

- multiple dispatch: more parameters
Static vs. Dynamic Dispatch
85
Example: Static Call
86
.class public Exp
.method public static fac(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Exp/fac(I)I
imul
ireturn
.end method
function fac(n: int): int=
if
n = 0
then
1
else
n * fac(n - 1)
Example: Dynamic Call
87
.class public Exp
.method public fac(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 0
iload 1
dup
iconst_1
isub
invokevirtual Exp/fac(I)I
imul
ireturn
.end method
function fac(n: int): int=
if
n = 0
then
1
else
n * fac(n - 1)
Caller
- push object 

- push parameters left-to-right

- call method 

Virtual machine on call
- allocate space (frame data, operand stack, local variables)

- store frame data (data pointer, return address, exception table) 

- store parameters as local variables

- dynamic dispatch

- point pc to method code
Code Pattern: Dynamic Method Call
88
Callee
- parameters in local variables

- leave result on operand stack

- return to caller

Virtual machine on return
- push result on caller’s operand stack

- point pc to return address

- destroy frame
Code Pattern: Return from Method Call
89
Implementation: Heap-Based
90
3
frame
data
3
2
fac(3)
2
frame
data
fac(2)
1
frame
data
fac(1)
22
21
11
1
Implementation: Stack-Based
91
3
frame
data
3
2
fac(3)
2
frame
data
fac(2)
22
2
1
111
frame
data
fac(1)
11
Stack Frames
92
00
01
02
03
04
05
06
2A
10
40
B6
00
01
AC
aload_0
bipush
invokevirtual
01
ireturn
optop: 02
method area stack
heap
00
01
02
03
04
05
06
4303 4303
0000 0040
local variables
00
01
02
03
04
05
06
4303 4303
pc: 03
Stack Frames
93
pc: 80
80
81
82
83
84
85
86
2B
59
68
AC
00
00
00
iload_1
dup
imul
ireturn
optop: 01
method area stack
heap
00
01
02
03
04
05
06
4303 4303
0000 0040
local variables
00
01
02
03
04
05
06
4303 4303
local variables
00
01
02
03
04
05
06
4303 4303
0000 0040
optop: 00
stack
00
01
02
03
04
05
06
Stack Frames
94
pc: 81
80
81
82
83
84
85
86
2B
59
68
AC
00
00
00
iload_1
dup
imul
ireturn
optop: 01
method area stack
heap
00
01
02
03
04
05
06
4303 4303
0000 0040
local variables
00
01
02
03
04
05
06
4303 4303
local variables
00
01
02
03
04
05
06
4303 4303
0000 0040
optop: 01
stack
00
01
02
03
04
05
06
0000 0040
Stack Frames
95
pc: 81
80
81
82
83
84
85
86
2B
59
68
AC
00
00
00
iload_1
dup
imul
ireturn
optop: 01
method area stack
heap
00
01
02
03
04
05
06
4303 4303
0000 0040
local variables
00
01
02
03
04
05
06
4303 4303
local variables
00
01
02
03
04
05
06
4303 4303
0000 0040
optop: 02
stack
00
01
02
03
04
05
06
0000 0040
0000 0040
Stack Frames
96
pc: 82
80
81
82
83
84
85
86
2B
59
68
AC
00
00
00
iload_1
dup
imul
ireturn
optop: 01
method area stack
heap
00
01
02
03
04
05
06
4303 4303
0000 0040
local variables
00
01
02
03
04
05
06
4303 4303
local variables
00
01
02
03
04
05
06
4303 4303
0000 0040
optop: 01
stack
00
01
02
03
04
05
06
0000 1000
Stack Frames
97
00
01
02
03
04
05
06
2A
10
40
B6
00
01
AC
aload_0
bipush
invokevirtual
01
ireturn
optop: 01
method area stack
heap
00
01
02
03
04
05
06
0000 1000
local variables
00
01
02
03
04
05
06
4303 4303
pc: 06
JVM: Class Files
98
Java Compiler
99
> ls
Course.java
> javac -verbose Course.java
[parsing started Course.java]
[parsing completed 8ms]
[loading java/lang/Object.class(java/lang:Object.class)]
[checking university.Course]
[wrote Course.class]
[total 411ms]
> ls
Course.class Course.java
Class Files: Format
100
magic number CAFEBABE
class file version (minor, major)
constant pool count + constant pool
access flags
this class
super class
interfaces count + interfaces
fields count + fields
methods count + methods
attribute count + attributes
Jasmin Intermediate Language
101
.class public Exp
.method public static fac(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Exp/fac(I)I
imul
ireturn
.end method
Optimization
102
Reasons
- code overhead

- execution overhead

Inlining
- replace calls by body of the procedure 

- source code level

Tail recursion
- replace recursive calls by loops or jumps

- source or machine code level
Optimizations
103
Code Generation
104
function fac0(n0: int): int=
if
n0 = 0
then
1
else
n0 * fac0(n0 - 1)
.method public static fac0(I)I
iload 1
ldc 0
if_icmpeq label0
ldc 0
goto label1
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
105
.method public static fac0(I)I
iload_1
ifne else0
iconst_1
ireturn
else0: iload_1
dup
iconst_1
isub
invokestatic
Exp/fac0(I)I
imul
ireturn
.end method
.method public static fac0(I)I
iload 1
ldc 0
if_icmpeq label0
ldc 0
goto label1
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
106
.method public static fac0(I)I
iload 1
ifeq label0
ldc 0
goto label1
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ldc 0
if_icmpeq label0
ldc 0
goto label1
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
107
.method public static fac0(I)I
iload 1
ifeq label0
ldc 0
ifeq else0
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifeq label0
ldc 0
goto label1
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
108
.method public static fac0(I)I
iload 1
ifeq label0
goto else0
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifeq label0
ldc 0
ifeq else0
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
109
.method public static fac0(I)I
iload 1
ifeq label0
goto else0
label0: ldc 1
ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifeq label0
goto else0
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
110
.method public static fac0(I)I
iload 1
ifeq label0
goto else0
label0: ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifeq label0
goto else0
label0: ldc 1
ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
111
.method public static fac0(I)I
iload 1
ifneq else0
label0: ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifeq label0
goto else0
label0: ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
112
.method public static fac0(I)I
iload 1
ifneq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifneq else0
label0: ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
113
.method public static fac0(I)I
iload 1
ifneq else0
ldc 1
ireturn
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifneq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
114
.method public static fac0(I)I
iload 1
ifneq else0
ldc 1
ireturn
else0: iload 1
dup
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifneq else0
ldc 1
ireturn
else0: iload 1
iload 1
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
115
.method public static fac0(I)I
iload_1
ifneq else0
ldc 1
ireturn
else0: iload_1
dup
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload 1
ifneq else0
ldc 1
ireturn
else0: iload 1
dup
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Optimization
116
.method public static fac0(I)I
iload_1
ifneq else0
iconst_1
ireturn
else0: iload_1
dup
iconst_1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
.method public static fac0(I)I
iload_1
ifneq else0
ldc 1
ireturn
else0: iload_1
dup
ldc 1
isub
invokestatic
Exp/fac0(I)I
imul
end0: ireturn
.end method
Tail Recursion Elimination
117
Example: Tail Recursion
118
.class public Exp
.method public static fac(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Exp/fac(I)I
imul
ireturn
.end method
Example: Tail Recursion
119
.class public Exp
.method public static fac(I)I
iload 1
ifne else
iconst_1
ireturn
else: iload 1
dup
iconst_1
isub
invokestatic Exp/fac(I)I
imul
ireturn
.end method
.class public Exp
.method public static fac(II)I
iload 1
ifne else
iload 2
ireturn
else: iload 1
iconst_1
isub
iload 1
iload 2
imul
invokestatic Exp/fac(II)I
ireturn
.end method
Example: Tail Recursion
120
.class public Exp
.method public static fac(II)I
iload 1
ifne else
iload 2
ireturn
else: iload 1
iconst_1
isub
iload 1
iload 2
imul
invokestatic Exp/fac(II)I
ireturn
.end method
.class public Exp
.method public static fac(II)I
strt: iload 1
ifne else
iload 2
ireturn
else: iload 1
iconst_1
isub
iload 1
iload 2
imul
istore 2
istore 1
goto strt
Code Generation: Strings
121
Printing Strings
122
to-jbc = ?Nil() ; <printstring> "aconst_nulln"
to-jbc = ?NoVal() ; <printstring> "nopn"
to-jbc = ?Seq(es) ; <list-loop(to-jbc)> es
to-jbc =
?Int(i);
<printstring> "ldc ";
<printstring> i;
<printstring> "n"
to-jbc = ?Bop(op, e1, e2) ; <to-jbc> e1 ; <to-jbc> e2 ; <to-jbc> op
to-jbc = ?PLUS() ; <printstring> "iaddn"
to-jbc = ?MINUS() ; <printstring> "isubn"
to-jbc = ?MUL() ; <printstring> "imuln"
to-jbc = ?DIV() ; <printstring> "idivn"
String Concatenation
123
to-jbc: Nil() -> "aconst_nulln"
to-jbc: NoVal() -> "nopn"
to-jbc: Seq(es) -> <concat-strings> <map(to-jbc)> es
to-jbc: Int(i) -> <concat-strings> ["ldc ", i, "n"]
to-jbc: Bop(op, e1, e2) -> <concat-strings> [ <to-jbc> e1,
<to-jbc> e2,
<to-jbc> op ]
to-jbc: PLUS() -> "iaddn"
to-jbc: MINUS() -> "isubn"
to-jbc: MUL() -> "imuln"
to-jbc: DIV() -> "idivn"
String Interpolation
124
to-jbc: Nil() -> $[aconst_null]
to-jbc: NoVal() -> $[nop]
to-jbc: Seq(es) -> <map-to-jbc> es
map-to-jbc: [] -> $[]
map-to-jbc: [h|t] ->
$[[<to-jbc> h]
[<map-to-jbc> t]]
to-jbc: Int(i) -> $[ldc [i]]

to-jbc: Bop(op, e1, e2) ->
$[[<to-jbc> e1]
[<to-jbc> e2]
[<to-jbc> op]]
to-jbc: PLUS() -> $[iadd]
to-jbc: MINUS() -> $[isub]
to-jbc: MUL() -> $[imul]
to-jbc: DIV() -> $[idiv]
Code Generation:
Transformation
125
Compilation by Transformation
126
backend
frontend
parse desugar analyse normalise
generate optimise format
Transformation
127
to-jbc: Nil() -> [ ACONST_NULL() ]
to-jbc: NoVal() -> [ NOP() ]
to-jbc: Seq(es) -> <mapconcat(to-jbc)> es
to-jbc: Int(i) -> [ LDC(Int(i)) ]
to-jbc: String(s) -> [ LDC(String(s)) ]
to-jbc: Bop(op, e1, e2) -> <mapconcat(to-jbc)> [ e1, e2, op ]
to-jbc: PLUS() -> [ IADD() ]
to-jbc: MINUS() -> [ ISUB() ]
to-jbc: MUL() -> [ IMUL() ]
to-jbc: DIV() -> [ IDIV() ]
to-jbc: Assign(lhs, e) -> <concat> [ <to-jbc> e, <lhs-to-jbc> lhs ]
to-jbc: Var(x) -> [ ILOAD(x) ] where <type-of> Var(x) => INT()
to-jbc: Var(x) -> [ ALOAD(x) ] where <type-of> Var(x) => STRING()
lhs-to-jbc: Var(x) -> [ ISTORE(x) ] where <type-of> Var(x) => INT()
lhs-to-jbc: Var(x) -> [ ASTORE(x) ] where <type-of> Var(x) => STRING()
Transformation
128
to-jbc:
IfThenElse(e1, e2, e3) -> <concat> [ <to-jbc> e1
, [ IFEQ(LabelRef(else)) ]
, <to-jbc> e2
, [ GOTO(LabelRef(end)), Label(else) ]
, <to-jbc> e3
, [ Label(end) ]
]
where <newname> "else" => else
where <newname> "end" => end
to-jbc:
While(e1, e2) -> <concat>[ [ GOTO(LabelRef(check)), Label(body) ]
, <to-jbc> e2
, [ Label(check) ]
, <to-jbc> e1
, [ IFNE(LabelRef(body)) ]
]
where <newname> "test" => check
where <newname> "body" => body
Transformation: Example
129
function fac(n: int): int=
if
n = 0
then
1
else
n * fac(n - 1)
.method public static fac(I)I
iload 1
ldc 0
if_icmpeq label0
ldc 0
goto label1
label0: ldc 1
label1: ifeq else0
ldc 1
goto end0
else0: iload 1
iload 1
ldc 1
isub
invokestatic Exp/fac(I)I
imul
end0: ireturn
.end method
Compiler Infrastructure: LLVM
backendsfrontends
130
Fortran
C
Ada
PowerPC
X86
ARM
intermediate representation
optimise
Next
131
Back-End
- Lecture 11: DataïŹ‚ow Analysis (Nov 28)

- Lecture 12: Garbage Collection (Dec 5)

- Lecture 13: Just-in-Time Compilation (Interpreters & Partial Evaluation) (Dec 12)

Parsing
- Lecture 14: LL Parsing (Dec 19)

- Lecture 15: LR Parsing (Jan 9)

Lab
- Lab 6: Testing Type Analysis (Nov 22)

- Lab 7: Type Analyis (Dec 6)

- Lab 8: Compiling Minimal Programs (Dec 13)

- Lab 9: Compiling Expressions and Statements (Dec 22)

- Lab 10: Compiling Fields, Parameters, and Variables (Jan 19)
Next
132
133

More Related Content

PDF
Compiler Construction | Lecture 11 | Monotone Frameworks
PDF
Apache Hadoop Java API
PDF
Alp 05
PDF
Monad presentation scala as a category
PDF
Compiler unit 4
PDF
Matlab ch1 intro
PPT
Compiler Construction | Lecture 11 | Monotone Frameworks
Apache Hadoop Java API
Alp 05
Monad presentation scala as a category
Compiler unit 4
Matlab ch1 intro

What's hot (20)

PPT
PPT
Python (1)
PPTX
C Assignment Help
PDF
Survey onhpcs languages
PPTX
Programming Assignment Help
PDF
GNU octave
PPT
10 8086 instruction set
PPTX
C++ theory
PPTX
Sequential pattern mining
PPT
Csc1100 lecture14 ch16_pt2
 
PPTX
Register allocation and assignment
PPTX
Dynamic memory allocation
PDF
Assembly language part I
PDF
instruction set of 8086
PPT
r,rstats,r language,r packages
PPT
C++ Overview
PDF
Run time storage
DOCX
Yacc topic beyond syllabus
PDF
Programming with matlab session 1
Python (1)
C Assignment Help
Survey onhpcs languages
Programming Assignment Help
GNU octave
10 8086 instruction set
C++ theory
Sequential pattern mining
Csc1100 lecture14 ch16_pt2
 
Register allocation and assignment
Dynamic memory allocation
Assembly language part I
instruction set of 8086
r,rstats,r language,r packages
C++ Overview
Run time storage
Yacc topic beyond syllabus
Programming with matlab session 1
Ad

Similar to Declare Your Language: Virtual Machines & Code Generation (20)

PDF
Compiler Construction | Lecture 12 | Virtual Machines
PPT
C language
PPT
C language
PDF
Programming languages
PPT
sonam Kumari python.ppt
PPTX
Advanced procedures in assembly language Full chapter ppt
PDF
Introduction - Imperative and Object-Oriented Languages
PPTX
Introduction to c
PPT
Sedna XML Database: Executor Internals
PPT
python.ppt
PPS
Clanguage
PPT
python.ppt
PDF
Dr archana dhawan bajaj - csharp fundamentals slides
PPT
other-architectures.ppt
PDF
Bozorgmeh os lab
PDF
7986-lect 7.pdf
PPTX
RHCSA EX200 - Summary
PPT
Al2ed chapter17
PDF
DML Syntax and Invocation process
PDF
S1 DML Syntax and Invocation
Compiler Construction | Lecture 12 | Virtual Machines
C language
C language
Programming languages
sonam Kumari python.ppt
Advanced procedures in assembly language Full chapter ppt
Introduction - Imperative and Object-Oriented Languages
Introduction to c
Sedna XML Database: Executor Internals
python.ppt
Clanguage
python.ppt
Dr archana dhawan bajaj - csharp fundamentals slides
other-architectures.ppt
Bozorgmeh os lab
7986-lect 7.pdf
RHCSA EX200 - Summary
Al2ed chapter17
DML Syntax and Invocation process
S1 DML Syntax and Invocation
Ad

More from Eelco Visser (20)

PDF
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
PDF
CS4200 2019 | Lecture 4 | Syntactic Services
PDF
CS4200 2019 | Lecture 3 | Parsing
PDF
CS4200 2019 | Lecture 2 | syntax-definition
PDF
CS4200 2019 Lecture 1: Introduction
PDF
A Direct Semantics of Declarative Disambiguation Rules
PDF
Declarative Type System Specification with Statix
PDF
Compiler Construction | Lecture 17 | Beyond Compiler Construction
PDF
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
PDF
Compiler Construction | Lecture 15 | Memory Management
PDF
Compiler Construction | Lecture 14 | Interpreters
PDF
Compiler Construction | Lecture 13 | Code Generation
PDF
Compiler Construction | Lecture 10 | Data-Flow Analysis
PDF
Compiler Construction | Lecture 9 | Constraint Resolution
PDF
Compiler Construction | Lecture 8 | Type Constraints
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Compiler Construction | Lecture 6 | Introduction to Static Analysis
PDF
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
PDF
Compiler Construction | Lecture 4 | Parsing
PDF
Compiler Construction | Lecture 3 | Syntactic Editor Services
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 Lecture 1: Introduction
A Direct Semantics of Declarative Disambiguation Rules
Declarative Type System Specification with Statix
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 3 | Syntactic Editor Services

Recently uploaded (20)

PPTX
Introduction to Artificial Intelligence
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Transform Your Business with a Software ERP System
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Understanding Forklifts - TECH EHS Solution
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Digital Strategies for Manufacturing Companies
PDF
medical staffing services at VALiNTRY
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Introduction to Artificial Intelligence
Wondershare Filmora 15 Crack With Activation Key [2025
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
How Creative Agencies Leverage Project Management Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Transform Your Business with a Software ERP System
Softaken Excel to vCard Converter Software.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
wealthsignaloriginal-com-DS-text-... (1).pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Understanding Forklifts - TECH EHS Solution
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Migrate SBCGlobal Email to Yahoo Easily
Digital Strategies for Manufacturing Companies
medical staffing services at VALiNTRY
Internet Downloader Manager (IDM) Crack 6.42 Build 41

Declare Your Language: Virtual Machines & Code Generation

  • 1. Eelco Visser IN4303 Compiler Construction TU Delft November 2017 Declare Your Language Chapter 10: Virtual Machines & Code Generation
  • 2. Virtual Machines & Code Generation 2 backend frontend parse desugar analyse normalise generate optimise format
  • 3. Linguistic Abstraction - High-level programming languages abstract from low-level machine mechanics - Abstractions in imperative and object-oriented languages - Calling Conventions - Register machines vs stack machines Java Virtual Machine - Architecture - Operation stack - Constant pool - Local variables - Heap - Stack frames - Class ïŹles Code Generation Mechanics - Printing strings - String concatenation - String interpolation - Transformation Outline 3
  • 5. 5
  • 6. Java Virtual Machine Tim Lindholm, Frank Yellin: The Java Virtual Machine SpeciïŹcation, 2nd edition. Addison-Wesley, 1999. Bill Venners: Inside the Java 2 Virtual Machine. McGraw-Hill, 2000. Activation Records Andrew W. Appel, Jens Palsberg: Modern Compiler Implementation in Java, 2nd edition. 2002 Literature: VMs & Compilation 6
  • 7. Imperative Languages - Carl A. Gunter: Semantics of Programming Languages: Structures and Techniques. MIT Press, 1992 - Kenneth C. Louden: Programming Languages: Principles and Practice. Course Technology, 2002 Object-Oriented Languages - Martin Abadi, Luca Cardelli: A Theory of Objects. Springer, 1996. - Kim B. Bruce: Foundations of Object-Oriented Programming Languages: Types and Semantics. MIT Press, 2002. - Timothy Budd: An Introduction to Object-Oriented Programming. Addison- Wesley, 2002. Literature: Semantics of Programming Languages 7
  • 9. Machine state - a pile of data stored in memory - memory hierarchy: registers, RAM, disk, network, 
 Imperative program - computation is series of changes to memory - basic operations on memory (increment register) - controlling such operations (jump, return address, 
) - control represented by state (program counter, stack, 
) State 9
  • 10. General purpose registers - accumulator AX - arithmetic operations - counter CX - shift/rotate instructions, loops - data DX - arithmetic operations, I/O - base BX - pointer to data - stack pointer SP, base pointer BP - top and base of stack - source SI, destination DI - stream operations Special purpose registers - segments SS, CS, DS, ES, FS, GS - ïŹ‚ags EFLAGS Registers: x86 Family 10
  • 11. Example: x86 Assembler 11 mov AX [1] mov CX AX L: dec CX mul CX cmp CX 1 ja L mov [2] AX read memory write memory calculation jump
  • 12. Example: Java Bytecode 12 .method static public m(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Math/m(I)I imul ireturn read memory calculation jump
  • 13. Memory abstractions - variables: abstract over data storage - expressions: combine data into new data - assignment: abstract over storage operations Control-ïŹ‚ow abstractions - structured control-ïŹ‚ow: abstract over unstructured jumps - ‘go to statement considered harmful’ Edgser Dijkstra, 1968 Memory & Control Abstractions 13
  • 14. Example: C 14 int f = 1 int x = 5 int s = f + x while (x > 1) { f = x * f ; x = x - 1 } variable expression assignment control flow
  • 15. Example: Tiger 15 /* factorial function */ let var f := 1 var x := 5 var s := f + x in while x > 1 do ( f := x * f ; x := x - 1 ) end variable expression assignment control flow
  • 17. Control-ïŹ‚ow abstraction - Procedure: named unit of computation - Procedure call: jump to unit of computation and return Memory abstraction - Formal parameter: the name of the parameter - Actual parameter: value that is passed to procedure - Local variable: temporary memory Recursion - Procedure may (indirectly) call itself - Consequence? Procedural Abstraction 17
  • 18. Example: Procedures in C 18 #include <stio.h> /* factorial function */ int fac( int num ) { if (num < 1) return 1; else return num * fac(num - 1) ; } int main() { int x = 10 ; int f = fac( x ) ; int x printf(“%d! = %dn”, x, f); return 0; } formal parameter actual parameter local variable recursive call
  • 19. Procedures in Tiger 19 /* factorial function */ let function fac( n: int ) : int = let var f := 1 in if n < 1 then f := 1 else f := (n * fac(n - 1) ); f end var f := 0 var x := 5 in f := fac( x ) end formal parameter actual parameter local variable recursive call
  • 20. Stack - temporary storage - grows from high to low memory addresses - starts at SS Stack frames - return address - local variables - parameters - stack base: BP - stack top: SP Implementing Procedures with Stack and Stack Frames 20
  • 21. General purpose registers - accumulator AX - arithmetic operations - counter CX - shift/rotate instructions, loops - data DX - arithmetic operations, I/O - base BX - pointer to data - stack pointer SP, base pointer BP - top and base of stack - source SI, destination DI - stream operations Special purpose registers - segments SS, CS, DS, ES, FS, GS - ïŹ‚ags EFLAGS Reminder: Registers in x86 Family 21
  • 22. Example: Procedures in x86 Assembler 22 push 21 push 42 call _f add SP 8 push BP mov BP SP mov AX [BP + 8] mov DX [BP + 12] add AX DX pop BP ret pass parameter access parameter new stack frame old stack frame free parameters
  • 23. Caller - push parameters right-to-left on the stack - clean-up stack after call Callee - save old BP - initialise new BP - save registers - return result in AX - restore registers - restore BP Calling Conventions: CDECL 23 push 21 push 42 call _f add ESP 8 push EBP mov EBP ESP mov EAX [EBP + 8] mov EDX [EBP + 12] add EAX EDX pop EBP ret
  • 24. caller - push parameters right-to-left on the stack callee - save old BP - initialise new BP - save registers - return result in AX - restore registers - restore BP Calling Conventions: STDECL 24 push 21 push 42 call _f@8 push EBP mov EBP ESP mov EAX [EBP + 8] mov EDX [EBP + 12] add EAX EDX pop EBP ret 8
  • 25. Caller - passes parameters in registers - pushes additional parameters right-to-left ‹ on the stack Callee - save old BP, initialise new BP - save registers - return result in AX - restore registers - restore BP - cleans up the stack Calling Conventions: FASTCALL 25 mov ECX 21 mov EDX 42 call @f@8 push EBP mov EBP ESP mov EAX ECX add EAX EDX pop EBP ret
  • 26. Procedure declarations - in principle: full freedom - project constraints - target platform constraints Procedure calls - need to match procedure declarations Precompiled libraries - avoid recompilation - source code not always available Standardization - compilers / high-level languages standardize use of calling conventions - portable code: does not depend on particular calling convention Calling Conventions 26
  • 28. Objects - generalisation of records - identity - state - behaviour Messages - objects send and receive messages - trigger behaviour - imperative realisation: method calls Modularity: Objects & Messages 28
  • 29. Classes - generalisation of record types - characteristics of objects: attributes, ïŹelds, properties - behaviour of objects: methods, operations, features Encapsulation - interface exposure - hide attributes & methods - hide implementation Modularity: Classes 29 public class C { public int f1; private int f2; public void m1() { return; } private C m2(C c) { return c; } }
  • 30. Inheritance - inherit attributes & methods - additional attributes & methods - override behaviour - nominative subtyping Interfaces - avoid multiple inheritance - interface: contract for attributes & methods - class: provide attributes & methods - nominative subtyping Inheritance vs Interfaces 30 public class C { public int f1; public void m1() {
} public void m2() {
} } public class D extends C { public int f2; public void m2() {
} public void m3() {
} } public interface I { public int f; public void m(); } public class E implements I { public int f; public void m() {
} public void m’() {
} }
  • 31. Ad-hoc polymorphism - overloading ‣ same method name, independent classes ‣ same method name, same class, diïŹ€erent parameter types - overriding ‣ same method name, subclass, compatible types Universal polymorphism - subtype polymorphism ‣ inheritance, interfaces - parametric polymorphism Polymorphism 31
  • 32. Dispatch - link method call to method Static dispatch - type information at compile-time Dynamic dispatch - type information at run-time - single dispatch: one parameter - multiple dispatch: more parameters Static vs. Dynamic Dispatch 32
  • 33. Single Dispatch in Java 33 public class A {} public class B extends A {} public class C extends B {} public class D { public A m(A a) { System.out.println("D.m(A a)"); return a; } public A m(B b) { System.out.println("D.m(B b)"); return b; } } public class E extends D { public A m(A a) { System.out.println("E.m(A a)"); return a; } public B m(B b) { System.out.println("E.m(B b)"); return b; } } A a = new A(); B b = new B(); C c = new C(); D d = new D(); E e = new E(); A ab = b; A ac = c; D de = e; d. m(a); d. m(b); d. m(ab); d. m(c); d. m(ac); e. m(a); e. m(b); e. m(ab); e. m(c); e. m(ac); de.m(a); de.m(b); de.m(ab); de.m(c); de.m(ac);
  • 34. Methods - parameter types - return type Covariance - method in subclass - return type: subtype of original return type Contravariance - method in subclass - parameter types: supertypes of original parameter types Overriding 34
  • 35. Overloading vs Overriding 35 public class F { public A m(B b) { System.out.println("F.m(B b)"); return b; } } public class G extends F { public A m(A a) { System.out.println("G.m(A a)"); return a; } } public class H extends F { public B m(B b) { System.out.println("H.m(B b)"); return b; } } A a = new A(); B b = new B(); F f = new F(); G g = new G(); H h = new H(); A ab = b; f.m(b); g.m(a); g.m(b); g.m(ab); h.m(a); h.m(b); h.m(ab);
  • 36. Invariance 36 public class X { public A a; public A getA() { return a ; } public void setA(A a) { this.a = a ; } } public class Y extends X { public B a; public B getA() { return a ; } public void setA(B a) { this.a = a ; } } A a = new A(); B b = new B(); X y = new Y(); y.getA(); y.setA(b); y.setA(a); String[] s = new String[3] ; Object[] o = s ; o[1] = new A();
  • 38. Imperative languages - subroutines, routines, procedures, functions, methods - scoping: local variables - declarations with parameters (formal parameters) - calls with arguments (actual parameters) - pass by value, pass by reference Machine code - jumps: call and return - call stack: return address, parameters, private data - procedure prologue and epilogue Abstractions for Memory and Control 38
  • 39. Imperative languages - state & statements - abstraction over machine code - control ïŹ‚ow & procedures - types Object-oriented languages - objects & messages - classes - inheritance - types Imperative vs Object-Oriented 39
  • 48. 48 Reverse Engineering the JVM Instruction Set
  • 50. Bytecode Instructions: Goto 50 pc: 00 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto 04 nop goto 03
  • 51. Bytecode Instructions: Goto 51 pc: 04 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto 04 nop goto 03
  • 52. Bytecode Instructions: Goto 52 pc: 03 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto 04 nop goto 03
  • 53. Bytecode Instructions: Goto 53 pc: 04 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto 04 nop goto 03
  • 55. Operand Stack 55 pc: 00 00 01 02 03 04 05 06 pc: 00 optop: 00 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush sipush method area stack
  • 56. Operand Stack 56 optop: 01pc: 01 00 01 02 03 04 05 06 0000 000100 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush sipush method area stack
  • 57. Operand Stack 57 optop: 02pc: 02 00 01 02 03 04 05 06 0000 0001 0000 0002 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush sipush method area stack
  • 58. Operand Stack 58 optop: 03pc: 04 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 002A 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush sipush method area stack
  • 59. Operand Stack 59 optop: 04pc: 07 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 002A 0000 4303 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush sipush method area stack
  • 60. Operand Stack 60 optop: 04 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 002A 0000 4303 pc: 07 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne 00 method area stack
  • 61. Operand Stack 61 optop: 03 00 01 02 03 04 05 06 0000 0001 0000 0002 0000 432D pc: 08 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne 00 method area stack
  • 62. Operand Stack 62 optop: 02 00 01 02 03 04 05 06 0000 0001 0000 865A pc: 09 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne 00 method area stack
  • 63. Operand Stack 63 optop: 02 00 01 02 03 04 05 06 0000 865A 0000 0001 pc: 0A 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne 00 method area stack
  • 64. Operand Stack 64 optop: 01 00 01 02 03 04 05 06 0000 8659 pc: 0B 07 08 09 0A 0B 0C 0D 60 68 5F 64 9A FF F5 iadd imul swap isub ifne 00 method area stack
  • 65. Operand Stack 65 00 01 02 03 04 05 06 pc: 00 optop: 00 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush sipush method area stack
  • 67. Constant Pool 67 pc: 00 method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w 02 00 01 02 03 04 05 06 constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 00
  • 68. Constant Pool 68 pc: 02 method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w 02 00 01 02 03 04 05 06 0000 002A constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 01
  • 69. Constant Pool 69 pc: 04 method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w 02 00 01 02 03 04 05 06 0000 002A 0000 4303 constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 02
  • 70. Constant Pool 70 pc: 07 method area stack 00 01 02 03 04 05 06 12 00 12 01 14 00 02 ldc 00 ldc 01 ldc2_w 02 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A constant pool 00 01 02 03 04 05 06 0000 002A 0000 4303 0000 0000 0000 002A optop: 04
  • 72. Local Variables 72 pc: 00 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 00 01 02 03 04 05 06 local variables 00 01 02 03 04 05 06 optop: 00
  • 73. Local Variables 73 pc: 01 00 01 02 03 04 05 06 0000 0001 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 01 02 03 04 05 06 optop: 01
  • 74. Local Variables 74 pc: 02 00 01 02 03 04 05 06 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 01 02 03 04 05 06 0000 0001 optop: 00
  • 75. Local Variables 75 pc: 03 00 01 02 03 04 05 06 0000 0001 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 01 02 03 04 05 06 0000 0001 optop: 01
  • 76. Local Variables 76 pc: 04 00 01 02 03 04 05 06 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 01 02 03 04 05 06 0000 0001 0000 0001 optop: 00
  • 77. Local Variables 77 pc: 07 00 01 02 03 04 05 06 method area stack 00 01 02 03 04 05 06 04 3B 1A 3C 84 01 01 iconst_1 istore_0 iload_0 istore_1 iinc 01 01 local variables 00 01 02 03 04 05 06 0000 0001 0000 0002 optop: 00
  • 79. Heap 79 pc: 00 method area stack heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 00 01 02 03 04 05 06 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 local variables 00 01 02 03 04 05 06 002A 002A optop: 00
  • 80. Heap 80 pc: 02 method area stack heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 00 01 02 03 04 05 06 4303 4303 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 local variables 00 01 02 03 04 05 06 002A 002A optop: 01
  • 81. Heap 81 pc: 04 method area stack heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 00 01 02 03 04 05 06 4303 4303 002A 002A constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 local variables 00 01 02 03 04 05 06 002A 002A optop: 02
  • 82. Heap 82 pc: 06 method area stack heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 00 01 02 03 04 05 06 4303 4303 002A 002A 0000 0004 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 local variables 00 01 02 03 04 05 06 002A 002A optop: 03
  • 83. Heap 83 pc: 07 method area stack heap 4303 4303 "Compilers" 002A 002A [20,01,40,02,42] 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 00 01 02 03 04 05 06 4303 4303 0000 0042 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 local variables 00 01 02 03 04 05 06 002A 002A optop: 02
  • 85. Dispatch - link method call to method Static dispatch - type information at compile-time Dynamic dispatch - type information at run-time - single dispatch: one parameter - multiple dispatch: more parameters Static vs. Dynamic Dispatch 85
  • 86. Example: Static Call 86 .class public Exp .method public static fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1)
  • 87. Example: Dynamic Call 87 .class public Exp .method public fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 0 iload 1 dup iconst_1 isub invokevirtual Exp/fac(I)I imul ireturn .end method function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1)
  • 88. Caller - push object - push parameters left-to-right - call method Virtual machine on call - allocate space (frame data, operand stack, local variables) - store frame data (data pointer, return address, exception table) - store parameters as local variables - dynamic dispatch - point pc to method code Code Pattern: Dynamic Method Call 88
  • 89. Callee - parameters in local variables - leave result on operand stack - return to caller Virtual machine on return - push result on caller’s operand stack - point pc to return address - destroy frame Code Pattern: Return from Method Call 89
  • 92. Stack Frames 92 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush invokevirtual 01 ireturn optop: 02 method area stack heap 00 01 02 03 04 05 06 4303 4303 0000 0040 local variables 00 01 02 03 04 05 06 4303 4303 pc: 03
  • 93. Stack Frames 93 pc: 80 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn optop: 01 method area stack heap 00 01 02 03 04 05 06 4303 4303 0000 0040 local variables 00 01 02 03 04 05 06 4303 4303 local variables 00 01 02 03 04 05 06 4303 4303 0000 0040 optop: 00 stack 00 01 02 03 04 05 06
  • 94. Stack Frames 94 pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn optop: 01 method area stack heap 00 01 02 03 04 05 06 4303 4303 0000 0040 local variables 00 01 02 03 04 05 06 4303 4303 local variables 00 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 0040
  • 95. Stack Frames 95 pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn optop: 01 method area stack heap 00 01 02 03 04 05 06 4303 4303 0000 0040 local variables 00 01 02 03 04 05 06 4303 4303 local variables 00 01 02 03 04 05 06 4303 4303 0000 0040 optop: 02 stack 00 01 02 03 04 05 06 0000 0040 0000 0040
  • 96. Stack Frames 96 pc: 82 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn optop: 01 method area stack heap 00 01 02 03 04 05 06 4303 4303 0000 0040 local variables 00 01 02 03 04 05 06 4303 4303 local variables 00 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 1000
  • 97. Stack Frames 97 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush invokevirtual 01 ireturn optop: 01 method area stack heap 00 01 02 03 04 05 06 0000 1000 local variables 00 01 02 03 04 05 06 4303 4303 pc: 06
  • 99. Java Compiler 99 > ls Course.java > javac -verbose Course.java [parsing started Course.java] [parsing completed 8ms] [loading java/lang/Object.class(java/lang:Object.class)] [checking university.Course] [wrote Course.class] [total 411ms] > ls Course.class Course.java
  • 100. Class Files: Format 100 magic number CAFEBABE class file version (minor, major) constant pool count + constant pool access flags this class super class interfaces count + interfaces fields count + fields methods count + methods attribute count + attributes
  • 101. Jasmin Intermediate Language 101 .class public Exp .method public static fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method
  • 103. Reasons - code overhead - execution overhead Inlining - replace calls by body of the procedure - source code level Tail recursion - replace recursive calls by loops or jumps - source or machine code level Optimizations 103
  • 104. Code Generation 104 function fac0(n0: int): int= if n0 = 0 then 1 else n0 * fac0(n0 - 1) .method public static fac0(I)I iload 1 ldc 0 if_icmpeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 105. Optimization 105 .method public static fac0(I)I iload_1 ifne else0 iconst_1 ireturn else0: iload_1 dup iconst_1 isub invokestatic Exp/fac0(I)I imul ireturn .end method .method public static fac0(I)I iload 1 ldc 0 if_icmpeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 106. Optimization 106 .method public static fac0(I)I iload 1 ifeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ldc 0 if_icmpeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 107. Optimization 107 .method public static fac0(I)I iload 1 ifeq label0 ldc 0 ifeq else0 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 108. Optimization 108 .method public static fac0(I)I iload 1 ifeq label0 goto else0 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifeq label0 ldc 0 ifeq else0 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 109. Optimization 109 .method public static fac0(I)I iload 1 ifeq label0 goto else0 label0: ldc 1 ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifeq label0 goto else0 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 110. Optimization 110 .method public static fac0(I)I iload 1 ifeq label0 goto else0 label0: ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifeq label0 goto else0 label0: ldc 1 ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 111. Optimization 111 .method public static fac0(I)I iload 1 ifneq else0 label0: ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifeq label0 goto else0 label0: ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 112. Optimization 112 .method public static fac0(I)I iload 1 ifneq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifneq else0 label0: ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 113. Optimization 113 .method public static fac0(I)I iload 1 ifneq else0 ldc 1 ireturn else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifneq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 114. Optimization 114 .method public static fac0(I)I iload 1 ifneq else0 ldc 1 ireturn else0: iload 1 dup ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifneq else0 ldc 1 ireturn else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 115. Optimization 115 .method public static fac0(I)I iload_1 ifneq else0 ldc 1 ireturn else0: iload_1 dup ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload 1 ifneq else0 ldc 1 ireturn else0: iload 1 dup ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 116. Optimization 116 .method public static fac0(I)I iload_1 ifneq else0 iconst_1 ireturn else0: iload_1 dup iconst_1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method .method public static fac0(I)I iload_1 ifneq else0 ldc 1 ireturn else0: iload_1 dup ldc 1 isub invokestatic Exp/fac0(I)I imul end0: ireturn .end method
  • 118. Example: Tail Recursion 118 .class public Exp .method public static fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method
  • 119. Example: Tail Recursion 119 .class public Exp .method public static fac(I)I iload 1 ifne else iconst_1 ireturn else: iload 1 dup iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method .class public Exp .method public static fac(II)I iload 1 ifne else iload 2 ireturn else: iload 1 iconst_1 isub iload 1 iload 2 imul invokestatic Exp/fac(II)I ireturn .end method
  • 120. Example: Tail Recursion 120 .class public Exp .method public static fac(II)I iload 1 ifne else iload 2 ireturn else: iload 1 iconst_1 isub iload 1 iload 2 imul invokestatic Exp/fac(II)I ireturn .end method .class public Exp .method public static fac(II)I strt: iload 1 ifne else iload 2 ireturn else: iload 1 iconst_1 isub iload 1 iload 2 imul istore 2 istore 1 goto strt
  • 122. Printing Strings 122 to-jbc = ?Nil() ; <printstring> "aconst_nulln" to-jbc = ?NoVal() ; <printstring> "nopn" to-jbc = ?Seq(es) ; <list-loop(to-jbc)> es to-jbc = ?Int(i); <printstring> "ldc "; <printstring> i; <printstring> "n" to-jbc = ?Bop(op, e1, e2) ; <to-jbc> e1 ; <to-jbc> e2 ; <to-jbc> op to-jbc = ?PLUS() ; <printstring> "iaddn" to-jbc = ?MINUS() ; <printstring> "isubn" to-jbc = ?MUL() ; <printstring> "imuln" to-jbc = ?DIV() ; <printstring> "idivn"
  • 123. String Concatenation 123 to-jbc: Nil() -> "aconst_nulln" to-jbc: NoVal() -> "nopn" to-jbc: Seq(es) -> <concat-strings> <map(to-jbc)> es to-jbc: Int(i) -> <concat-strings> ["ldc ", i, "n"] to-jbc: Bop(op, e1, e2) -> <concat-strings> [ <to-jbc> e1, <to-jbc> e2, <to-jbc> op ] to-jbc: PLUS() -> "iaddn" to-jbc: MINUS() -> "isubn" to-jbc: MUL() -> "imuln" to-jbc: DIV() -> "idivn"
  • 124. String Interpolation 124 to-jbc: Nil() -> $[aconst_null] to-jbc: NoVal() -> $[nop] to-jbc: Seq(es) -> <map-to-jbc> es map-to-jbc: [] -> $[] map-to-jbc: [h|t] -> $[[<to-jbc> h] [<map-to-jbc> t]] to-jbc: Int(i) -> $[ldc [i]]
 to-jbc: Bop(op, e1, e2) -> $[[<to-jbc> e1] [<to-jbc> e2] [<to-jbc> op]] to-jbc: PLUS() -> $[iadd] to-jbc: MINUS() -> $[isub] to-jbc: MUL() -> $[imul] to-jbc: DIV() -> $[idiv]
  • 126. Compilation by Transformation 126 backend frontend parse desugar analyse normalise generate optimise format
  • 127. Transformation 127 to-jbc: Nil() -> [ ACONST_NULL() ] to-jbc: NoVal() -> [ NOP() ] to-jbc: Seq(es) -> <mapconcat(to-jbc)> es to-jbc: Int(i) -> [ LDC(Int(i)) ] to-jbc: String(s) -> [ LDC(String(s)) ] to-jbc: Bop(op, e1, e2) -> <mapconcat(to-jbc)> [ e1, e2, op ] to-jbc: PLUS() -> [ IADD() ] to-jbc: MINUS() -> [ ISUB() ] to-jbc: MUL() -> [ IMUL() ] to-jbc: DIV() -> [ IDIV() ] to-jbc: Assign(lhs, e) -> <concat> [ <to-jbc> e, <lhs-to-jbc> lhs ] to-jbc: Var(x) -> [ ILOAD(x) ] where <type-of> Var(x) => INT() to-jbc: Var(x) -> [ ALOAD(x) ] where <type-of> Var(x) => STRING() lhs-to-jbc: Var(x) -> [ ISTORE(x) ] where <type-of> Var(x) => INT() lhs-to-jbc: Var(x) -> [ ASTORE(x) ] where <type-of> Var(x) => STRING()
  • 128. Transformation 128 to-jbc: IfThenElse(e1, e2, e3) -> <concat> [ <to-jbc> e1 , [ IFEQ(LabelRef(else)) ] , <to-jbc> e2 , [ GOTO(LabelRef(end)), Label(else) ] , <to-jbc> e3 , [ Label(end) ] ] where <newname> "else" => else where <newname> "end" => end to-jbc: While(e1, e2) -> <concat>[ [ GOTO(LabelRef(check)), Label(body) ] , <to-jbc> e2 , [ Label(check) ] , <to-jbc> e1 , [ IFNE(LabelRef(body)) ] ] where <newname> "test" => check where <newname> "body" => body
  • 129. Transformation: Example 129 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) .method public static fac(I)I iload 1 ldc 0 if_icmpeq label0 ldc 0 goto label1 label0: ldc 1 label1: ifeq else0 ldc 1 goto end0 else0: iload 1 iload 1 ldc 1 isub invokestatic Exp/fac(I)I imul end0: ireturn .end method
  • 132. Back-End - Lecture 11: DataïŹ‚ow Analysis (Nov 28) - Lecture 12: Garbage Collection (Dec 5) - Lecture 13: Just-in-Time Compilation (Interpreters & Partial Evaluation) (Dec 12) Parsing - Lecture 14: LL Parsing (Dec 19) - Lecture 15: LR Parsing (Jan 9) Lab - Lab 6: Testing Type Analysis (Nov 22) - Lab 7: Type Analyis (Dec 6) - Lab 8: Compiling Minimal Programs (Dec 13) - Lab 9: Compiling Expressions and Statements (Dec 22) - Lab 10: Compiling Fields, Parameters, and Variables (Jan 19) Next 132
  • 133. 133