declarative semantics definition - code generation

122
IN4303 2014/15 Compiler Construction Declarative Semantics Definition code generation Guido Wachsmuth

Upload: guido-wachsmuth

Post on 10-May-2015

3.425 views

Category:

Education


5 download

DESCRIPTION

Presentation slides for lecture 5 of course IN4303 on Compiler Construction at TU Delft.

TRANSCRIPT

Page 1: Declarative Semantics Definition - Code Generation

IN4303 2014/15 Compiler Construction

Declarative Semantics Definition code generation

Guido Wachsmuth

Page 2: Declarative Semantics Definition - Code Generation

Code Generation 2

source code

Page 3: Declarative Semantics Definition - Code Generation

Code Generation 2

source code

parse

Page 4: Declarative Semantics Definition - Code Generation

Code Generation 2

source code

errors

parse

check

Page 5: Declarative Semantics Definition - Code Generation

Code Generation 2

source code

errors

parse generate

check

machine code

Page 6: Declarative Semantics Definition - Code Generation

Code Generation 3

source code

Page 7: Declarative Semantics Definition - Code Generation

Code Generation 3

source code

parse

Page 8: Declarative Semantics Definition - Code Generation

Code Generation 3

source code

parse

check

Page 9: Declarative Semantics Definition - Code Generation

Code Generation 3

source code

parse

check

Page 10: Declarative Semantics Definition - Code Generation

Code Generation 3

source code

parse generate

machine code

check

Page 11: Declarative Semantics Definition - Code Generation

Code Generation

Stratego

NaBL TS

SDF3

ESV editor

SPT tests

4

syntax definition

concrete syntax

abstract syntax

static semantics

name binding

type system

dynamic semantics

translation

interpretation

Page 12: Declarative Semantics Definition - Code Generation

Code Generation

Stratego

NaBL TS

SDF3

ESV editor

SPT tests

5

syntax definition

concrete syntax

abstract syntax

static semantics

name binding

type system

dynamic semantics

translation

interpretation

Page 13: Declarative Semantics Definition - Code Generation

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

Page 14: Declarative Semantics Definition - Code Generation

Code Generation 7

Java Virtual Machine

Page 15: Declarative Semantics Definition - Code Generation

Code Generation 8

bytecode instructions

pc: 00

method area

00010203040506

A7000400A7FFFF

goto!04nopgoto!03

Page 16: Declarative Semantics Definition - Code Generation

Code Generation 9

bytecode instructions

pc: 04

method area

00010203040506

A7000400A7FFFF

goto!04nopgoto!03

Page 17: Declarative Semantics Definition - Code Generation

Code Generation 10

bytecode instructions

pc: 03

method area

00010203040506

A7000400A7FFFF

goto!04nopgoto!03

Page 18: Declarative Semantics Definition - Code Generation

Code Generation 11

bytecode instructions

pc: 04

method area

00010203040506

A7000400A7FFFF

goto!04nopgoto!03

Page 19: Declarative Semantics Definition - Code Generation

Code Generation 12

operand stack

Page 20: Declarative Semantics Definition - Code Generation

Code Generation 13

operand stack

pc: 0000010203040506

pc: 00 optop: 0000010203040506

0405102A114303

iconst_1iconst_2bipush!sipush!

method area stack

Page 21: Declarative Semantics Definition - Code Generation

Code Generation 14

operand stack

optop: 01pc: 0100010203040506

0000 000100010203040506

0405102A114303

iconst_1iconst_2bipush!sipush!

method area stack

Page 22: Declarative Semantics Definition - Code Generation

Code Generation

optop: 02pc: 0200010203040506

0000 00010000 0002

00010203040506

0405102A114303

iconst_1iconst_2bipush!sipush!

15

method area stack

operand stack

Page 23: Declarative Semantics Definition - Code Generation

Code Generation

optop: 03pc: 0400010203040506

0000 00010000 00020000 002A

00010203040506

0405102A114303

iconst_1iconst_2bipush!sipush!

16

method area stack

operand stack

Page 24: Declarative Semantics Definition - Code Generation

Code Generation

optop: 04pc: 0700010203040506

0000 00010000 00020000 002A0000 4303

00010203040506

0405102A114303

iconst_1iconst_2bipush!sipush!

17

method area stack

operand stack

Page 25: Declarative Semantics Definition - Code Generation

Code Generation

optop: 0400010203040506

0000 00010000 00020000 002A0000 4303

pc: 070708090A0B0C0D

60685F649AFFF5

iaddimulswapisubifne!00

18

method area stack

operand stack

Page 26: Declarative Semantics Definition - Code Generation

Code Generation

optop: 0300010203040506

0000 00010000 00020000 432D

pc: 080708090A0B0C0D

60685F649AFFF5

iaddimulswapisubifne!00

19

method area stack

operand stack

Page 27: Declarative Semantics Definition - Code Generation

Code Generation

optop: 0200010203040506

0000 00010000 865A

pc: 090708090A0B0C0D

60685F649AFFF5

iaddimulswapisubifne!00

20

method area stack

operand stack

Page 28: Declarative Semantics Definition - Code Generation

Code Generation

optop: 0200010203040506

0000 865A0000 0001

pc: 0A0708090A0B0C0D

60685F649AFFF5

iaddimulswapisubifne!00

21

method area stack

operand stack

Page 29: Declarative Semantics Definition - Code Generation

Code Generation

optop: 0100010203040506

0000 8659pc: 0B0708090A0B0C0D

60685F649AFFF5

iaddimulswapisubifne!00

22

method area stack

operand stack

Page 30: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

pc: 00 optop: 0000010203040506

0405102A114303

iconst_1iconst_2bipush!sipush!

23

method area stack

operand stack

Page 31: Declarative Semantics Definition - Code Generation

Code Generation 24

constant pool

Page 32: Declarative Semantics Definition - Code Generation

Code Generation

pc: 00

25

method area stack

00010203040506

12001201140002

ldc 00ldc01ldc2_w !02

00010203040506

constant pool

00010203040506

0000 002A0000 43030000 00000000 002A

optop: 00

Java Virtual Machineconstant pool

Page 33: Declarative Semantics Definition - Code Generation

Code Generation 26

method area stack

00010203040506

12001201140002

ldc 00ldc01ldc2_w !02

00010203040506

0000 002Aconstant pool

00010203040506

0000 002A0000 43030000 00000000 002A

optop: 01

Java Virtual Machineconstant pool

pc: 02

Page 34: Declarative Semantics Definition - Code Generation

Code Generation 27

method area stack

00010203040506

12001201140002

ldc 00ldc01ldc2_w !02

00010203040506

0000 002A0000 4303

constant pool

00010203040506

0000 002A0000 43030000 00000000 002A

optop: 02

Java Virtual Machineconstant pool

pc: 04

Page 35: Declarative Semantics Definition - Code Generation

Code Generation 28

method area stack

00010203040506

12001201140002

ldc 00ldc01ldc2_w !02

00010203040506

0000 002A0000 43030000 00000000 002A

constant pool

00010203040506

0000 002A0000 43030000 00000000 002A

optop: 04

Java Virtual Machineconstant pool

pc: 07

Page 36: Declarative Semantics Definition - Code Generation

Code Generation 29

local variables

Page 37: Declarative Semantics Definition - Code Generation

Code Generation 30

method area stack

00010203040506

043B1A3C840101

iconst_1istore_0iload_0istore_1iinc0101

00010203040506

local variables

00010203040506

optop: 00

local variables

pc: 00

Page 38: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

0000 0001

31

method area stack

00010203040506

043B1A3C840101

iconst_1istore_0iload_0istore_1iinc0101

local variables

00010203040506

optop: 01

local variables

pc: 01

Page 39: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

32

method area stack

00010203040506

043B1A3C840101

iconst_1istore_0iload_0istore_1iinc0101

local variables

00010203040506

0000 0001optop: 00

local variables

pc: 02

Page 40: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

0000 0001

33

method area stack

00010203040506

043B1A3C840101

iconst_1istore_0iload_0istore_1iinc0101

local variables

00010203040506

0000 0001optop: 01

local variables

pc: 03

Page 41: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

34

method area stack

00010203040506

043B1A3C840101

iconst_1istore_0iload_0istore_1iinc0101

local variables

00010203040506

0000 00010000 0001

optop: 00

local variables

pc: 04

Page 42: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

35

method area stack

00010203040506

043B1A3C840101

iconst_1istore_0iload_0istore_1iinc0101

local variables

00010203040506

0000 00010000 0002

optop: 00

local variables

pc: 07

Page 43: Declarative Semantics Definition - Code Generation

Code Generation 36

heap

Page 44: Declarative Semantics Definition - Code Generation

Code Generation 37

method area stack

heap

4303 4303"Compilers" 002A 002A[20,01,40,02,42]

00010203040506

1200190012012E

ldc 00aload00 ldc01iaload

00010203040506

constant pool

00010203040506

4303 43030000 0004

local variables

00010203040506

002A 002Aoptop: 00

heap

pc: 00

Page 45: Declarative Semantics Definition - Code Generation

Code Generation 38

method area stack

heap

4303 4303"Compilers" 002A 002A[20,01,40,02,42]

00010203040506

1200190012012E

ldc 00aload00 ldc01iaload

00010203040506

4303 4303constant pool

00010203040506

4303 43030000 0004

local variables

00010203040506

002A 002Aoptop: 01

heap

pc: 02

Page 46: Declarative Semantics Definition - Code Generation

Code Generation 39

method area stack

heap

4303 4303"Compilers" 002A 002A[20,01,40,02,42]

00010203040506

1200190012012E

ldc 00aload00 ldc01iaload

00010203040506

4303 4303002A 002A

constant pool

00010203040506

4303 43030000 0004

local variables

00010203040506

002A 002Aoptop: 02

heap

pc: 04

Page 47: Declarative Semantics Definition - Code Generation

Code Generation 40

method area stack

heap

4303 4303"Compilers" 002A 002A[20,01,40,02,42]

00010203040506

1200190012012E

ldc 00aload00 ldc01iaload

00010203040506

4303 4303002A 002A0000 0004

constant pool

00010203040506

4303 43030000 0004

local variables

00010203040506

002A 002Aoptop: 03

heap

pc: 06

Page 48: Declarative Semantics Definition - Code Generation

Code Generation 41

method area stack

heap

4303 4303"Compilers" 002A 002A[20,01,40,02,42]

00010203040506

1200190012012E

ldc 00aload00 ldc01iaload

00010203040506

4303 43030000 0042

constant pool

00010203040506

4303 43030000 0004

local variables

00010203040506

002A 002Aoptop: 02

heap

pc: 07

Page 49: Declarative Semantics Definition - Code Generation

Code Generation 42

stack frames

Page 50: Declarative Semantics Definition - Code Generation

Code Generation 43

Type Systems

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

recap static vs. dynamic dispatch

Page 51: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

2A1040B60001AC

aload_0bipush!invokevirtual!01ireturn

optop: 02

44

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303pc: 03

stack frames

Page 52: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8080818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

45

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 00

stack

00010203040506

stack frames

Page 53: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8180818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

46

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 01

stack

00010203040506

0000 0040

stack frames

Page 54: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8180818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

47

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 02

stack

00010203040506

0000 00400000 0040

stack frames

Page 55: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8280818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

48

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 01

stack

00010203040506

0000 1000

stack frames

Page 56: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

2A1040B60001AC

aload_0bipush!invokevirtual!01ireturn

optop: 01

49

method area stack

heap

00010203040506

0000 1000local variables

00010203040506

4303 4303pc: 06

stack frames

Page 57: Declarative Semantics Definition - Code Generation

Code Generation 50

Java Virtual Machine class files

Page 58: Declarative Semantics Definition - Code Generation

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

Page 59: Declarative Semantics Definition - Code Generation

Code Generation 52

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

class files format

Page 60: Declarative Semantics Definition - Code Generation

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

Page 61: Declarative Semantics Definition - Code Generation

Code Generation 54

code generation strings

Page 62: Declarative Semantics Definition - Code Generation

Code Generation 55

to-jbc = ?Nil() ; <printstring> "aconst_null\n"to-jbc = ?NoVal() ; <printstring> "nop\n"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> "iadd\n"to-jbc = ?MINUS() ; <printstring> "isub\n"to-jbc = ?MUL() ; <printstring> "imul\n" to-jbc = ?DIV() ; <printstring> "idiv\n"

printing strings

Page 63: Declarative Semantics Definition - Code Generation

Code Generation 56

to-jbc: Nil() -> "aconst_null\n"to-jbc: NoVal() -> "nop\n"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() -> "iadd\n"to-jbc: MINUS() -> "isub\n"to-jbc: MUL() -> "imul\n" to-jbc: DIV() -> "idiv\n"

string concatenation

Page 64: Declarative Semantics Definition - Code Generation

Code Generation 57

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]

string interpolation

Page 65: Declarative Semantics Definition - Code Generation

Code Generation 58

code generation transformation

Page 66: Declarative Semantics Definition - Code Generation

Code Generation 59

recap compilation by transformation

backend

frontend

parse desugar analyse normalise

generate optimise format

Page 67: Declarative Semantics Definition - Code Generation

Code Generation 60

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

Page 68: Declarative Semantics Definition - Code Generation

Code Generation 61

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

Page 69: Declarative Semantics Definition - Code Generation

Code Generation 62

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

transformation example

Page 70: Declarative Semantics Definition - Code Generation

Code Generation

backendsfrontends

63

LLVM compiler infrastructure

Fortran

C

Ada

PowerPC

X86

ARM

intermediate representation

optimise

Page 71: Declarative Semantics Definition - Code Generation

Code Generation 64

calling conventions Java Virtual Machine

Page 72: Declarative Semantics Definition - Code Generation

Code Generation 65

function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1)

.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

example static call

Page 73: Declarative Semantics Definition - Code Generation

Code Generation 66

function fac(n: int): int= if n = 0 then 1 else n * fac(n - 1)

.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

example dynamic call

Page 74: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

2A1040B60001AC

aload_0bipush!invokevirtual!01ireturn

optop: 02

67

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303pc: 03

stack frames

Page 75: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8080818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

68

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 00

stack

00010203040506

stack frames

Page 76: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8180818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

69

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 01

stack

00010203040506

0000 0040

stack frames

Page 77: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8180818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

70

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 02

stack

00010203040506

0000 00400000 0040

stack frames

Page 78: Declarative Semantics Definition - Code Generation

Code Generation

pc: 8280818283848586

2B5968AC000000

iload_1dupimulireturn!!

optop: 01

71

method area stack

heap

00010203040506

4303 43030000 0040

local variables

00010203040506

4303 4303local variables

00010203040506

4303 43030000 0040

optop: 01

stack

00010203040506

0000 1000

stack frames

Page 79: Declarative Semantics Definition - Code Generation

Code Generation

00010203040506

2A1040B60001AC

aload_0bipush!invokevirtual!01ireturn

optop: 01

72

method area stack

heap

00010203040506

0000 1000local variables

00010203040506

4303 4303pc: 06

stack frames

Page 80: Declarative Semantics Definition - Code Generation

Code Generation 73

Type Systems

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

responsibilities method call

Page 81: Declarative Semantics Definition - Code Generation

Code Generation 74

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

responsibilities return from method call

Page 82: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

implementation heap-based

Page 83: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

implementation heap-based

Page 84: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

implementation heap-based

Page 85: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

implementation heap-based

Page 86: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

implementation heap-based

Page 87: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

2

implementation heap-based

Page 88: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

2

1

implementation heap-based

Page 89: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

1

implementation heap-based

Page 90: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

1

frame data

fac(

1)

2

implementation heap-based

Page 91: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

1

frame data

fac(

1)

2 1

implementation heap-based

Page 92: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

1

frame data

fac(

1)

2

implementation heap-based

Page 93: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

1

frame data

fac(

1)

2 1

implementation heap-based

Page 94: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

1

implementation heap-based

Page 95: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)2

implementation heap-based

Page 96: Declarative Semantics Definition - Code Generation

Code Generation 75

3

frame data

3

2

fac(

3)

implementation heap-based

Page 97: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

implementation stack-based

Page 98: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

implementation stack-based

Page 99: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

implementation stack-based

Page 100: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

implementation stack-based

Page 101: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

implementation stack-based

Page 102: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

2

implementation stack-based

Page 103: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

2

1

implementation stack-based

Page 104: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

1

implementation stack-based

Page 105: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

1

frame data

fac(

1)

implementation stack-based

Page 106: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

1

frame data

fac(

1)

1

implementation stack-based

Page 107: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

1

frame data

fac(

1)

implementation stack-based

Page 108: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

1

frame data

fac(

1)

1

implementation stack-based

Page 109: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

2

frame data

fac(

2)

2

1

implementation stack-based

Page 110: Declarative Semantics Definition - Code Generation

Code Generation 76

3

frame data

3

2

fac(

3)

implementation stack-based

Page 111: Declarative Semantics Definition - Code Generation

Code Generation 77

calling conventions register-based machines

Page 112: Declarative Semantics Definition - Code Generation

Code Generation 78

Type Systems

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

flags EFLAGS

recap x86 family

Page 113: Declarative Semantics Definition - Code Generation

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

Page 114: Declarative Semantics Definition - Code Generation

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

Page 115: Declarative Semantics Definition - Code Generation

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 21push 42call _fadd ESP 8

push EBPmov EBP ESPmov EAX [EBP + 8]mov EDX [EBP + 12]add EAX EDXpop EBPret

Page 116: Declarative Semantics Definition - Code Generation

Code Generation 81

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

calling conventions STDCALL

Page 117: Declarative Semantics Definition - Code Generation

Code Generation

push 21push 42call _f@8

push EBPmov EBP ESPmov EAX [EBP + 8]mov EDX [EBP + 12]add EAX EDXpop EBPret 8

81

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

calling conventions STDCALL

Page 118: Declarative Semantics Definition - Code Generation

Code Generation 82

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

calling conventions FASTCALL

Page 119: Declarative Semantics Definition - Code Generation

Code Generation 82

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

calling conventions FASTCALL

mov ECX 21mov EDX 42call @f@8

push EBPmov EBP ESPmov EAX ECXadd EAX EDXpop EBPret

Page 120: Declarative Semantics Definition - Code Generation

Code Generation 83

Type Systems

method declarations

in principle: full freedom

project constraints

target platform constraints

method calls

need to match method declarations

precompiled libraries

avoid recompilation

source code not always available

calling conventions compilation

Page 121: Declarative Semantics Definition - Code Generation

Code Generation 84

Except where otherwise noted, this work is licensed under