SlideShare une entreprise Scribd logo
1  sur  122
Télécharger pour lire hors ligne
IN4303 2014/15 
Compiler Construction 
Declarative Semantics Definition 
code generation 
Guido Wachsmuth
Code Generation 2 
source 
code
Code Generation 2 
source 
code 
parse
Code Generation 2 
source 
code 
errors 
parse 
check
Code Generation 2 
source 
code 
parse generate 
check 
errors 
machine 
code
Code Generation 3 
source 
code
Code Generation 3 
source 
code 
parse
Code Generation 3 
source 
code 
parse 
check
Code Generation 3 
source 
code 
parse 
check
Code Generation 3 
source 
code 
parse generate 
machine 
code 
check
Code Generation 
SDF3 
NaBL 
TS 
Stratego 
ESV 
editor 
SPT 
tests 
4 
syntax definition 
concrete syntax 
abstract syntax 
static semantics 
name binding 
type system 
dynamic semantics 
translation 
interpretation
Code Generation 
SDF3 
NaBL 
TS 
Stratego 
ESV 
editor 
SPT 
tests 
5 
syntax definition 
concrete syntax 
abstract syntax 
static semantics 
name binding 
type system 
dynamic semantics 
translation 
interpretation
Code Generation 6 
JVM 
operand stack 
constant pool 
local variables 
heap 
stack frames 
class files 
code generation 
printing strings 
string concatenation 
string interpolation 
transformation 
calling conventions 
JVM 
register-based machines
Java Virtual Machine 
Code Generation 7
Code Generation 8 
bytecode instructions 
pc: 00 
method area 
00 
01 
02 
03 
04 
05 
06 
A7 
00 
04 
00 
A7 
FF 
FF 
goto 
! 
04 
nop 
goto 
! 
03
Code Generation 9 
bytecode instructions 
pc: 04 
method area 
00 
01 
02 
03 
04 
05 
06 
A7 
00 
04 
00 
A7 
FF 
FF 
goto 
! 
04 
nop 
goto 
! 
03
Code Generation 10 
bytecode instructions 
pc: 03 
method area 
00 
01 
02 
03 
04 
05 
06 
A7 
00 
04 
00 
A7 
FF 
FF 
goto 
! 
04 
nop 
goto 
! 
03
Code Generation 11 
bytecode instructions 
pc: 04 
method area 
00 
01 
02 
03 
04 
05 
06 
A7 
00 
04 
00 
A7 
FF 
FF 
goto 
! 
04 
nop 
goto 
! 
03
Code Generation 12 
operand stack
Code Generation 13 
operand stack 
pc: 00 
method area stack 
optop: 00 
00 
01 
02 
03 
04 
05 
06 
00 
01 
02 
03 
04 
05 
06 
04 
05 
10 
2A 
11 
43 
03 
iconst_1 
iconst_2 
bipush 
! 
sipush 
!
Code Generation 14 
operand stack 
method area stack 
pc: 01 optop: 01 
00 04 
iconst_1 
00 
0000 0001 
01 
05 
iconst_2 
01 
02 
10 
bipush 
02 
03 
2A 
! 
03 
04 
11 
sipush 
04 
05 
43 
! 
05 
06 
03 
06
Code Generation 
operand stack 
pc: 02 optop: 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 
! 
15 
method area stack
Code Generation 
operand stack 
pc: 04 optop: 03 
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 
! 
16 
method area stack
Code Generation 
operand stack 
pc: 07 optop: 04 
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 
! 
17 
method area stack
Code Generation 
operand stack 
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 
18 
method area stack
Code Generation 
operand stack 
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 
19 
method area stack
Code Generation 
operand stack 
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 
20 
method area stack
Code Generation 
operand stack 
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 
21 
method area stack
Code Generation 
operand stack 
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 
22 
method area stack
Code Generation 
operand stack 
pc: 00 optop: 00 
00 
04 
iconst_1 
00 
01 
05 
iconst_2 
01 
02 
10 
bipush 
02 
03 
2A 
! 
03 
04 
11 
sipush 
04 
05 
43 
! 
05 
06 
03 
06 
23 
method area stack
Code Generation 24 
constant pool
Code Generation 
Java Virtual Maccohnisnteant pool 
pc: 00 
25 
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
Code Generation 26 
Java Virtual Maccohnisnteant pool 
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 
pc: 02
Code Generation 27 
Java Virtual Maccohnisnteant pool 
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 
pc: 04
Code Generation 28 
Java Virtual Maccohnisnteant pool 
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 
pc: 07
Code Generation 29 
local variables
Code Generation 30 
local variables 
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 
pc: 00
Code Generation 
local variables 
00 
01 
02 
03 
04 
05 
06 
0000 0001 
31 
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 
pc: 01
Code Generation 
local variables 
00 
01 
02 
03 
04 
05 
06 
32 
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 
0000 0001 
01 
02 
03 
04 
05 
06 
optop: 00 
pc: 02
Code Generation 
local variables 
00 
01 
02 
03 
04 
05 
06 
0000 0001 
33 
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 
0000 0001 
01 
02 
03 
04 
05 
06 
optop: 01 
pc: 03
Code Generation 
local variables 
00 
01 
02 
03 
04 
05 
06 
34 
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 
0000 0001 
01 
0000 0001 
02 
03 
04 
05 
06 
optop: 00 
pc: 04
Code Generation 
local variables 
00 
01 
02 
03 
04 
05 
06 
35 
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 
0000 0001 
01 
0000 0002 
02 
03 
04 
05 
06 
optop: 00 
pc: 07
Code Generation 36 
heap
local variables 
00 
002A 002A 
01 
02 
03 
04 
05 
06 
Code Generation 37 
heap 
method area stack 
optop: 00 
00 
01 
02 
03 
04 
05 
06 
heap 
pc: 00 
00 
01 
02 
03 
04 
05 
06 
12 
00 
19 
00 
12 
01 
2E 
ldc 
00 
aload 
00 
ldc 
01 
iaload 
constant pool 
00 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0004 
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
local variables 
00 
002A 002A 
01 
02 
03 
04 
05 
06 
Code Generation 38 
heap 
method area stack 
optop: 01 
00 
01 
02 
03 
04 
05 
06 
heap 
pc: 02 
00 
01 
02 
03 
04 
05 
06 
12 
00 
19 
00 
12 
01 
2E 
ldc 
00 
aload 
00 
ldc 
01 
iaload 
4303 4303 
constant pool 
00 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0004 
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
local variables 
00 
002A 002A 
01 
02 
03 
04 
05 
06 
Code Generation 39 
heap 
method area stack 
optop: 02 
00 
01 
02 
03 
04 
05 
06 
heap 
pc: 04 
00 
01 
02 
03 
04 
05 
06 
12 
00 
19 
00 
12 
01 
2E 
ldc 
00 
aload 
00 
ldc 
01 
iaload 
4303 4303 
002A 002A 
constant pool 
00 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0004 
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
local variables 
00 
002A 002A 
01 
02 
03 
04 
05 
06 
Code Generation 40 
heap 
method area stack 
optop: 03 
00 
01 
02 
03 
04 
05 
06 
heap 
pc: 06 
00 
01 
02 
03 
04 
05 
06 
12 
00 
19 
00 
12 
01 
2E 
ldc 
00 
aload 
00 
ldc 
01 
iaload 
4303 4303 
002A 002A 
0000 0004 
constant pool 
00 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0004 
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
local variables 
00 
002A 002A 
01 
02 
03 
04 
05 
06 
Code Generation 41 
heap 
method area stack 
optop: 02 
00 
01 
02 
03 
04 
05 
06 
heap 
pc: 07 
00 
01 
02 
03 
04 
05 
06 
12 
00 
19 
00 
12 
01 
2E 
ldc 
00 
aload 
00 
ldc 
01 
iaload 
4303 4303 
0000 0042 
constant pool 
00 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0004 
4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
Code Generation 42 
stack frames
Code Generation 43 
Type Systems 
dispatch 
static vs. dynamic dispatch 
link method call to method 
static dispatch 
type information at compile-time 
dynamic dispatch 
recap 
type information at run-time 
single dispatch: one parameter 
multiple dispatch: more parameters
Code Generation 
00 
01 
02 
03 
04 
05 
06 
2A 
10 
40 
B6 
00 
01 
AC 
aload_0 
bipush 
! 
invokevirtual 
! 
01 
ireturn 
optop: 02 
44 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
pc: 03 
stack frames
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 80 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
45 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 00 
stack 
00 
01 
02 
03 
04 
05 
06
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 81 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
46 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 01 
stack 
00 
01 
02 
03 
04 
05 
06 
0000 0040
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 81 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
47 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 02 
stack 
00 
01 
02 
03 
04 
05 
06 
0000 0040 
0000 0040
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 82 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
48 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 01 
stack 
00 
01 
02 
03 
04 
05 
06 
0000 1000
Code Generation 
00 
01 
02 
03 
04 
05 
06 
2A 
10 
40 
B6 
00 
01 
AC 
aload_0 
bipush 
! 
invokevirtual 
! 
01 
ireturn 
optop: 01 
49 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
0000 1000 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
pc: 06 
stack frames
Java Virtual Machine 
Code Generation 50 
class files
Code Generation 51 
> 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 
recap 
traditional compilers
Code Generation 52 
class files 
format 
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
Code Generation 53 
.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 
Jasmin 
intermediate language
Code Generation 54 
code generation 
strings
Code Generation 55 
printing strings 
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"
Code Generation 56 
string concatenation 
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"
Code Generation 57 
string interpolation 
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 58 
code generation 
transformation
parse desugar analyse normalise 
Code Generation 59 
recap 
compilation by transformation 
backend 
frontend 
generate optimise format
Code Generation 60 
transformation 
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()
Code Generation 61 
to-jbc: 
transformation 
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
Code Generation 62 
function fac(n: int): int= 
if 
n = 0 
then 
1 
else 
n * fac(n - 1) 
transformation 
example 
.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
C 
X86 
optimise 
frontends backends 
Code Generation 
63 
LLVM 
compiler infrastructure 
Fortran 
Ada 
PowerPC 
ARM 
intermediate representation
calling conventions 
Code Generation 64 
Java Virtual Machine
Code Generation 65 
function fac(n: int): int= 
if 
n = 0 
then 
1 
else 
n * fac(n - 1) 
example 
static call 
.class public Exp 
.method public static fac(I)I 
! 
iload 1 
ifne else 
! 
iconst_1 
ireturn 
! 
else: iload 1 
iload 1 
iconst_1 
isub 
invokestatic Exp/fac(I)I 
imul 
ireturn 
.end method
Code Generation 66 
function fac(n: int): int= 
if 
n = 0 
then 
1 
else 
n * fac(n - 1) 
example 
dynamic call 
.class public Exp 
.method public fac(I)I 
! 
iload 1 
ifne else 
! 
iconst_1 
ireturn 
! 
else: iload 1 
iload 0 
iload 1 
iconst_1 
isub 
invokevirtual Exp/fac(I)I 
imul 
ireturn 
.end method
Code Generation 
00 
01 
02 
03 
04 
05 
06 
2A 
10 
40 
B6 
00 
01 
AC 
aload_0 
bipush 
! 
invokevirtual 
! 
01 
ireturn 
optop: 02 
67 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
pc: 03 
stack frames
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 80 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
68 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 00 
stack 
00 
01 
02 
03 
04 
05 
06
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 81 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
69 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 01 
stack 
00 
01 
02 
03 
04 
05 
06 
0000 0040
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 81 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
70 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 02 
stack 
00 
01 
02 
03 
04 
05 
06 
0000 0040 
0000 0040
local variables 
00 
01 
02 
03 
04 
05 
06 
Code Generation 
pc: 82 
80 
81 
82 
83 
84 
85 
86 
2B 
59 
68 
AC 
00 
00 
00 
iload_1 
dup 
imul 
ireturn 
! 
! 
stack frames 
optop: 01 
71 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
4303 4303 
0000 0040 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
4303 4303 
0000 0040 
optop: 01 
stack 
00 
01 
02 
03 
04 
05 
06 
0000 1000
Code Generation 
00 
01 
02 
03 
04 
05 
06 
2A 
10 
40 
B6 
00 
01 
AC 
aload_0 
bipush 
! 
invokevirtual 
! 
01 
ireturn 
optop: 01 
72 
method area stack 
00 
01 
02 
03 
04 
05 
06 
heap 
0000 1000 
local variables 
00 
4303 4303 
01 
02 
03 
04 
05 
06 
pc: 06 
stack frames
Code Generation 73 
responsibilities 
Type Systems 
caller 
push object 
push parameters left-to-right 
call method 
virtual machine on call 
method 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 Generation 74 
responsibilities 
return from method call 
Type Systems 
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 Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2)
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2)
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2 
2
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2 
2 
1
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2 
1
implementation 
heap-based 
fac(2) 1 
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
2 
frame 
data 
frame 
data 
fac(1) 
2
implementation 
heap-based 
fac(2) 1 
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
2 
frame 
data 
frame 
data 
fac(1) 
2 1
implementation 
heap-based 
fac(2) 1 
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
2 
frame 
data 
frame 
data 
fac(1) 
2
implementation 
heap-based 
fac(2) 1 
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
2 
frame 
data 
frame 
data 
fac(1) 
2 1
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2 
1
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based 
2 
frame 
data 
fac(2) 
2
Code Generation 75 
3 
frame 
data 
3 
2 
fac(3) 
implementation 
heap-based
3 
frame 
data 
3 
2 
fac(3) 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
2 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
2 
1 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
1 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
1 
frame 
data 
fac(1) 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
1 
frame 
data 
fac(1) 
1 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
1 
frame 
data 
fac(1) 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
1 
frame 
data 
fac(1) 
1 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
fac(3) 
2 
frame 
data 
fac(2) 
2 
1 
Code Generation 76 
implementation 
stack-based
3 
frame 
data 
3 
2 
fac(3) 
Code Generation 76 
implementation 
stack-based
calling conventions 
Code Generation 77 
register-based machines
Code Generation 78 
Type Systems 
general purpose registers 
recap 
x86 family 
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 
flags EFLAGS
Code Generation 79 
Type Systems 
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 
stack and stack frames
Code Generation 80 
Type Systems 
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
Code Generation 80 
Type Systems 
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 
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
Code Generation 81 
calling conventions 
Type Systems 
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 
cleans up the stack 
STDCALL
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 
Code Generation 
81 
calling conventions 
Type Systems 
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 
cleans up the stack 
STDCALL
Code Generation 82 
calling conventions 
Type Systems 
caller 
pass parameters in registers 
push remaining 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 
FASTCALL
Code Generation 82 
calling conventions 
Type Systems 
caller 
pass parameters in registers 
push remaining 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 
FASTCALL 
mov ECX 21 
mov EDX 42 
call @f@8 
push EBP 
mov EBP ESP 
mov EAX ECX 
add EAX EDX 
pop EBP 
ret
Code Generation 83 
calling conventions 
Type Systems 
method declarations 
in principle: full freedom 
project constraints 
target platform constraints 
method calls 
compilation 
need to match method declarations 
precompiled libraries 
avoid recompilation 
source code not always available
Except where otherwise noted, this work is licensed under 
Code Generation 84
Code Generation 85 
attribution 
slide title author license 
1 Matrix Gamaliel Espinoza Macedo CC BY-NC 2.0 
2, 3, 59, 63 PICOL icons Melih Bilgil CC BY 3.0 
12 Gray Legos wallpaper monohex CC BY-NC-SA 2.0 
24 Billiard Balls Darren Hester some rights reserved 
29 Autoturm m.prinke CC BY-SA 2.0 
36 Spy Hill Landfill D'Arcy Norman some rights reserved 
42 Framed LexnGer CC BY-NC 2.0

Contenu connexe

Similaire à Declarative Semantics Definition - Code Generation

Windows Debugging with WinDbg
Windows Debugging with WinDbgWindows Debugging with WinDbg
Windows Debugging with WinDbgArno Huetter
 
Windows kernel debugging workshop in florida
Windows kernel debugging   workshop in floridaWindows kernel debugging   workshop in florida
Windows kernel debugging workshop in floridaSisimon Soman
 
basic computer programming and micro programmed control
basic computer programming and micro programmed controlbasic computer programming and micro programmed control
basic computer programming and micro programmed controlRai University
 
Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimonSisimon Soman
 
Symbolic Debugging with DWARF
Symbolic Debugging with DWARFSymbolic Debugging with DWARF
Symbolic Debugging with DWARFSamy Bahra
 
Kernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesKernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesAnne Nicolas
 
Porting NetBSD to the open source LatticeMico32 CPU
Porting NetBSD to the open source LatticeMico32 CPUPorting NetBSD to the open source LatticeMico32 CPU
Porting NetBSD to the open source LatticeMico32 CPUYann Sionneau
 
Ak12 upgrade
Ak12 upgradeAk12 upgrade
Ak12 upgradeAccenture
 
Debug Information And Where They Come From
Debug Information And Where They Come FromDebug Information And Where They Come From
Debug Information And Where They Come FromMin-Yih Hsu
 
Make ARM Shellcode Great Again - HITB2018PEK
Make ARM Shellcode Great Again - HITB2018PEKMake ARM Shellcode Great Again - HITB2018PEK
Make ARM Shellcode Great Again - HITB2018PEKSaumil Shah
 

Similaire à Declarative Semantics Definition - Code Generation (20)

Windows Debugging with WinDbg
Windows Debugging with WinDbgWindows Debugging with WinDbg
Windows Debugging with WinDbg
 
Writing bios
Writing biosWriting bios
Writing bios
 
Windows kernel debugging workshop in florida
Windows kernel debugging   workshop in floridaWindows kernel debugging   workshop in florida
Windows kernel debugging workshop in florida
 
basic computer programming and micro programmed control
basic computer programming and micro programmed controlbasic computer programming and micro programmed control
basic computer programming and micro programmed control
 
Windows debugging sisimon
Windows debugging   sisimonWindows debugging   sisimon
Windows debugging sisimon
 
Symbolic Debugging with DWARF
Symbolic Debugging with DWARFSymbolic Debugging with DWARF
Symbolic Debugging with DWARF
 
Kernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering OopsiesKernel Recipes 2013 - Deciphering Oopsies
Kernel Recipes 2013 - Deciphering Oopsies
 
8086 labmanual
8086 labmanual8086 labmanual
8086 labmanual
 
8086 microprocessor lab manual
8086 microprocessor lab manual8086 microprocessor lab manual
8086 microprocessor lab manual
 
Porting NetBSD to the open source LatticeMico32 CPU
Porting NetBSD to the open source LatticeMico32 CPUPorting NetBSD to the open source LatticeMico32 CPU
Porting NetBSD to the open source LatticeMico32 CPU
 
Debugging TV Frame 0x05
Debugging TV Frame 0x05Debugging TV Frame 0x05
Debugging TV Frame 0x05
 
Ak12 upgrade
Ak12 upgradeAk12 upgrade
Ak12 upgrade
 
Mona cheatsheet
Mona cheatsheetMona cheatsheet
Mona cheatsheet
 
Debug Information And Where They Come From
Debug Information And Where They Come FromDebug Information And Where They Come From
Debug Information And Where They Come From
 
RISC-V Zce Extension
RISC-V Zce ExtensionRISC-V Zce Extension
RISC-V Zce Extension
 
Mach-O par Stéphane Sudre
Mach-O par Stéphane SudreMach-O par Stéphane Sudre
Mach-O par Stéphane Sudre
 
crack satellite
crack satellite crack satellite
crack satellite
 
Make ARM Shellcode Great Again - HITB2018PEK
Make ARM Shellcode Great Again - HITB2018PEKMake ARM Shellcode Great Again - HITB2018PEK
Make ARM Shellcode Great Again - HITB2018PEK
 
Rapport
RapportRapport
Rapport
 
14941634.ppt
14941634.ppt14941634.ppt
14941634.ppt
 

Plus de Guido Wachsmuth

Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type SystemsGuido Wachsmuth
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingGuido Wachsmuth
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingGuido Wachsmuth
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisGuido Wachsmuth
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisGuido Wachsmuth
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedGuido Wachsmuth
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido Wachsmuth
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsGuido Wachsmuth
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingGuido Wachsmuth
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingGuido Wachsmuth
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesGuido Wachsmuth
 

Plus de Guido Wachsmuth (16)

Language
LanguageLanguage
Language
 
Domain-Specific Type Systems
Domain-Specific Type SystemsDomain-Specific Type Systems
Domain-Specific Type Systems
 
Declarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty PrintingDeclarative Syntax Definition - Pretty Printing
Declarative Syntax Definition - Pretty Printing
 
Compiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR ParsingCompiler Components and their Generators - LR Parsing
Compiler Components and their Generators - LR Parsing
 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
 
Compiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register AllocationCompiling Imperative and Object-Oriented Languages - Register Allocation
Compiling Imperative and Object-Oriented Languages - Register Allocation
 
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow AnalysisCompiling Imperative and Object-Oriented Languages - Dataflow Analysis
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Compiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation RecordsCompiling Imperative and Object-Oriented Languages - Activation Records
Compiling Imperative and Object-Oriented Languages - Activation Records
 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
Syntax Definition
Syntax DefinitionSyntax Definition
Syntax Definition
 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
 
Software Languages
Software LanguagesSoftware Languages
Software Languages
 

Dernier

UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...CaraSkikne1
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfYu Kanazawa / Osaka University
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 

Dernier (20)

UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
Finals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quizFinals of Kant get Marx 2.0 : a general politics quiz
Finals of Kant get Marx 2.0 : a general politics quiz
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...5 charts on South Africa as a source country for international student recrui...
5 charts on South Africa as a source country for international student recrui...
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdfP4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
P4C x ELT = P4ELT: Its Theoretical Background (Kanazawa, 2024 March).pdf
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 

Declarative Semantics Definition - Code Generation

  • 1. IN4303 2014/15 Compiler Construction Declarative Semantics Definition code generation Guido Wachsmuth
  • 2. Code Generation 2 source code
  • 3. Code Generation 2 source code parse
  • 4. Code Generation 2 source code errors parse check
  • 5. Code Generation 2 source code parse generate check errors machine code
  • 6. Code Generation 3 source code
  • 7. Code Generation 3 source code parse
  • 8. Code Generation 3 source code parse check
  • 9. Code Generation 3 source code parse check
  • 10. Code Generation 3 source code parse generate machine code check
  • 11. Code Generation SDF3 NaBL TS Stratego ESV editor SPT tests 4 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
  • 12. Code Generation SDF3 NaBL TS Stratego ESV editor SPT tests 5 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
  • 13. Code Generation 6 JVM operand stack constant pool local variables heap stack frames class files code generation printing strings string concatenation string interpolation transformation calling conventions JVM register-based machines
  • 14. Java Virtual Machine Code Generation 7
  • 15. Code Generation 8 bytecode instructions pc: 00 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
  • 16. Code Generation 9 bytecode instructions pc: 04 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
  • 17. Code Generation 10 bytecode instructions pc: 03 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
  • 18. Code Generation 11 bytecode instructions pc: 04 method area 00 01 02 03 04 05 06 A7 00 04 00 A7 FF FF goto ! 04 nop goto ! 03
  • 19. Code Generation 12 operand stack
  • 20. Code Generation 13 operand stack pc: 00 method area stack optop: 00 00 01 02 03 04 05 06 00 01 02 03 04 05 06 04 05 10 2A 11 43 03 iconst_1 iconst_2 bipush ! sipush !
  • 21. Code Generation 14 operand stack method area stack pc: 01 optop: 01 00 04 iconst_1 00 0000 0001 01 05 iconst_2 01 02 10 bipush 02 03 2A ! 03 04 11 sipush 04 05 43 ! 05 06 03 06
  • 22. Code Generation operand stack pc: 02 optop: 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 ! 15 method area stack
  • 23. Code Generation operand stack pc: 04 optop: 03 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 ! 16 method area stack
  • 24. Code Generation operand stack pc: 07 optop: 04 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 ! 17 method area stack
  • 25. Code Generation operand stack 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 18 method area stack
  • 26. Code Generation operand stack 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 19 method area stack
  • 27. Code Generation operand stack 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 20 method area stack
  • 28. Code Generation operand stack 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 21 method area stack
  • 29. Code Generation operand stack 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 22 method area stack
  • 30. Code Generation operand stack pc: 00 optop: 00 00 04 iconst_1 00 01 05 iconst_2 01 02 10 bipush 02 03 2A ! 03 04 11 sipush 04 05 43 ! 05 06 03 06 23 method area stack
  • 31. Code Generation 24 constant pool
  • 32. Code Generation Java Virtual Maccohnisnteant pool pc: 00 25 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
  • 33. Code Generation 26 Java Virtual Maccohnisnteant pool 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 pc: 02
  • 34. Code Generation 27 Java Virtual Maccohnisnteant pool 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 pc: 04
  • 35. Code Generation 28 Java Virtual Maccohnisnteant pool 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 pc: 07
  • 36. Code Generation 29 local variables
  • 37. Code Generation 30 local variables 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 pc: 00
  • 38. Code Generation local variables 00 01 02 03 04 05 06 0000 0001 31 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 pc: 01
  • 39. Code Generation local variables 00 01 02 03 04 05 06 32 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 0000 0001 01 02 03 04 05 06 optop: 00 pc: 02
  • 40. Code Generation local variables 00 01 02 03 04 05 06 0000 0001 33 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 0000 0001 01 02 03 04 05 06 optop: 01 pc: 03
  • 41. Code Generation local variables 00 01 02 03 04 05 06 34 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 0000 0001 01 0000 0001 02 03 04 05 06 optop: 00 pc: 04
  • 42. Code Generation local variables 00 01 02 03 04 05 06 35 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 0000 0001 01 0000 0002 02 03 04 05 06 optop: 00 pc: 07
  • 44. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 37 heap method area stack optop: 00 00 01 02 03 04 05 06 heap pc: 00 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
  • 45. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 38 heap method area stack optop: 01 00 01 02 03 04 05 06 heap pc: 02 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
  • 46. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 39 heap method area stack optop: 02 00 01 02 03 04 05 06 heap pc: 04 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 002A 002A constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
  • 47. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 40 heap method area stack optop: 03 00 01 02 03 04 05 06 heap pc: 06 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 002A 002A 0000 0004 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
  • 48. local variables 00 002A 002A 01 02 03 04 05 06 Code Generation 41 heap method area stack optop: 02 00 01 02 03 04 05 06 heap pc: 07 00 01 02 03 04 05 06 12 00 19 00 12 01 2E ldc 00 aload 00 ldc 01 iaload 4303 4303 0000 0042 constant pool 00 01 02 03 04 05 06 4303 4303 0000 0004 4303 4303 "Compilers" 002A 002A [20,01,40,02,42]
  • 49. Code Generation 42 stack frames
  • 50. Code Generation 43 Type Systems dispatch static vs. dynamic dispatch link method call to method static dispatch type information at compile-time dynamic dispatch recap type information at run-time single dispatch: one parameter multiple dispatch: more parameters
  • 51. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 02 44 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 pc: 03 stack frames
  • 52. local variables 00 01 02 03 04 05 06 Code Generation pc: 80 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 45 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 00 stack 00 01 02 03 04 05 06
  • 53. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 46 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 0040
  • 54. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 47 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 02 stack 00 01 02 03 04 05 06 0000 0040 0000 0040
  • 55. local variables 00 01 02 03 04 05 06 Code Generation pc: 82 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 48 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 1000
  • 56. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 01 49 method area stack 00 01 02 03 04 05 06 heap 0000 1000 local variables 00 4303 4303 01 02 03 04 05 06 pc: 06 stack frames
  • 57. Java Virtual Machine Code Generation 50 class files
  • 58. Code Generation 51 > 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 recap traditional compilers
  • 59. Code Generation 52 class files format 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
  • 60. Code Generation 53 .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 Jasmin intermediate language
  • 61. Code Generation 54 code generation strings
  • 62. Code Generation 55 printing strings 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"
  • 63. Code Generation 56 string concatenation 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"
  • 64. Code Generation 57 string interpolation 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]
  • 65. Code Generation 58 code generation transformation
  • 66. parse desugar analyse normalise Code Generation 59 recap compilation by transformation backend frontend generate optimise format
  • 67. Code Generation 60 transformation 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()
  • 68. Code Generation 61 to-jbc: transformation 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
  • 69. Code Generation 62 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) transformation example .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
  • 70. C X86 optimise frontends backends Code Generation 63 LLVM compiler infrastructure Fortran Ada PowerPC ARM intermediate representation
  • 71. calling conventions Code Generation 64 Java Virtual Machine
  • 72. Code Generation 65 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) example static call .class public Exp .method public static fac(I)I ! iload 1 ifne else ! iconst_1 ireturn ! else: iload 1 iload 1 iconst_1 isub invokestatic Exp/fac(I)I imul ireturn .end method
  • 73. Code Generation 66 function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1) example dynamic call .class public Exp .method public fac(I)I ! iload 1 ifne else ! iconst_1 ireturn ! else: iload 1 iload 0 iload 1 iconst_1 isub invokevirtual Exp/fac(I)I imul ireturn .end method
  • 74. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 02 67 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 pc: 03 stack frames
  • 75. local variables 00 01 02 03 04 05 06 Code Generation pc: 80 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 68 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 00 stack 00 01 02 03 04 05 06
  • 76. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 69 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 0040
  • 77. local variables 00 01 02 03 04 05 06 Code Generation pc: 81 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 70 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 02 stack 00 01 02 03 04 05 06 0000 0040 0000 0040
  • 78. local variables 00 01 02 03 04 05 06 Code Generation pc: 82 80 81 82 83 84 85 86 2B 59 68 AC 00 00 00 iload_1 dup imul ireturn ! ! stack frames optop: 01 71 method area stack 00 01 02 03 04 05 06 heap 4303 4303 0000 0040 local variables 00 4303 4303 01 02 03 04 05 06 4303 4303 0000 0040 optop: 01 stack 00 01 02 03 04 05 06 0000 1000
  • 79. Code Generation 00 01 02 03 04 05 06 2A 10 40 B6 00 01 AC aload_0 bipush ! invokevirtual ! 01 ireturn optop: 01 72 method area stack 00 01 02 03 04 05 06 heap 0000 1000 local variables 00 4303 4303 01 02 03 04 05 06 pc: 06 stack frames
  • 80. Code Generation 73 responsibilities Type Systems caller push object push parameters left-to-right call method virtual machine on call method 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
  • 81. Code Generation 74 responsibilities return from method call Type Systems 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
  • 82. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based
  • 83. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2)
  • 84. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2
  • 85. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2)
  • 86. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2
  • 87. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 2
  • 88. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 2 1
  • 89. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 1
  • 90. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2
  • 91. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2 1
  • 92. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2
  • 93. implementation heap-based fac(2) 1 Code Generation 75 3 frame data 3 2 fac(3) 2 frame data frame data fac(1) 2 1
  • 94. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2 1
  • 95. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based 2 frame data fac(2) 2
  • 96. Code Generation 75 3 frame data 3 2 fac(3) implementation heap-based
  • 97. 3 frame data 3 2 fac(3) Code Generation 76 implementation stack-based
  • 98. 3 frame data 3 fac(3) 2 frame data fac(2) Code Generation 76 implementation stack-based
  • 99. 3 frame data 3 fac(3) 2 frame data fac(2) 2 Code Generation 76 implementation stack-based
  • 100. 3 frame data 3 fac(3) 2 frame data fac(2) Code Generation 76 implementation stack-based
  • 101. 3 frame data 3 fac(3) 2 frame data fac(2) 2 Code Generation 76 implementation stack-based
  • 102. 3 frame data 3 fac(3) 2 frame data fac(2) 2 2 Code Generation 76 implementation stack-based
  • 103. 3 frame data 3 fac(3) 2 frame data fac(2) 2 2 1 Code Generation 76 implementation stack-based
  • 104. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 Code Generation 76 implementation stack-based
  • 105. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) Code Generation 76 implementation stack-based
  • 106. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) 1 Code Generation 76 implementation stack-based
  • 107. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) Code Generation 76 implementation stack-based
  • 108. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 frame data fac(1) 1 Code Generation 76 implementation stack-based
  • 109. 3 frame data 3 fac(3) 2 frame data fac(2) 2 1 Code Generation 76 implementation stack-based
  • 110. 3 frame data 3 2 fac(3) Code Generation 76 implementation stack-based
  • 111. calling conventions Code Generation 77 register-based machines
  • 112. Code Generation 78 Type Systems general purpose registers recap x86 family 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 flags EFLAGS
  • 113. Code Generation 79 Type Systems 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 stack and stack frames
  • 114. Code Generation 80 Type Systems 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
  • 115. Code Generation 80 Type Systems 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 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
  • 116. Code Generation 81 calling conventions Type Systems 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 cleans up the stack STDCALL
  • 117. 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 Code Generation 81 calling conventions Type Systems 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 cleans up the stack STDCALL
  • 118. Code Generation 82 calling conventions Type Systems caller pass parameters in registers push remaining 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 FASTCALL
  • 119. Code Generation 82 calling conventions Type Systems caller pass parameters in registers push remaining 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 FASTCALL mov ECX 21 mov EDX 42 call @f@8 push EBP mov EBP ESP mov EAX ECX add EAX EDX pop EBP ret
  • 120. Code Generation 83 calling conventions Type Systems method declarations in principle: full freedom project constraints target platform constraints method calls compilation need to match method declarations precompiled libraries avoid recompilation source code not always available
  • 121. Except where otherwise noted, this work is licensed under Code Generation 84
  • 122. Code Generation 85 attribution slide title author license 1 Matrix Gamaliel Espinoza Macedo CC BY-NC 2.0 2, 3, 59, 63 PICOL icons Melih Bilgil CC BY 3.0 12 Gray Legos wallpaper monohex CC BY-NC-SA 2.0 24 Billiard Balls Darren Hester some rights reserved 29 Autoturm m.prinke CC BY-SA 2.0 36 Spy Hill Landfill D'Arcy Norman some rights reserved 42 Framed LexnGer CC BY-NC 2.0

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. feedback loop\n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. console interaction\n
  56. console interaction\n
  57. console interaction\n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n
  67. \n
  68. \n
  69. \n
  70. \n
  71. \n
  72. \n
  73. \n
  74. \n
  75. \n
  76. \n
  77. \n
  78. \n
  79. \n
  80. \n
  81. \n
  82. \n
  83. \n
  84. \n
  85. \n
  86. \n
  87. \n
  88. 1. ldc 0 if_icmpeq label0 &amp;#x2192; ifeq label0\n2. ldc 0 goto label1 ifeq else0 &amp;#x2192; goto else0\n3. ifeq label0 goto else0 &amp;#x2192; ifne else0\n4. ldc 1 ifeq else0 &amp;#x2192; nop\n5. goto end0 &amp;#x2192; ireturn\n6. iload 1 &amp;#x2192; iload_1\n7. ldc 1 &amp;#x2192; iconst_1\n
  89. \n
  90. \n
  91. \n
  92. \n
  93. \n
  94. \n
  95. \n
  96. round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  97. \n
  98. \n