CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation

cse443 compilers
SMART_READER_LITE
LIVE PREVIEW

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis - - PowerPoint PPT Presentation

CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Phases of a compiler Intermediate Representation (IR): specification and generation Figure 1.6, page 5 of text Switch [p. 419] TEXTBOOK OUR LANGUAGE switch (E)


slide-1
SLIDE 1

CSE443 Compilers

  • Dr. Carl Alphonce

alphonce@buffalo.edu 343 Davis Hall

slide-2
SLIDE 2

Phases of a compiler

Figure 1.6, page 5 of text

Intermediate Representation (IR): specification and generation

slide-3
SLIDE 3

Switch [p. 419]

switch (E) { case C1 : S1 case C2 : S2 … case Cn-1 : Sn-1 default : Sn } switch (E) { case C1 : sblock1 case C2 : sblock2 … case Cn-1 : sblockn-1

  • therwise : sblockn

} OUR LANGUAGE TEXTBOOK

slide-4
SLIDE 4

Exercise:

switch (E) { case C1 : sblock1 case C2 : sblock2 … case Cn-1 : sblockn-1

  • therwise : sblockn

}

What intermediate code would you generate? Discuss with your team for ~5 minutes.

slide-5
SLIDE 5

Switch (6.8.2) [read p.420-421]

start E.code goto test L1 sblock1.code goto next L2 sblock2.code goto next

Ln-1 sblockn-1.code goto next Ln sblockn.code goto next test if t=C1 goto L1 if t=C2 goto L2

if t=Cn-1 goto Ln-1 if t=Cn goto Ln next

switch (E) { case C1 : sblock1 case C2 : sblock2 … case Cn-1 : sblockn-1

  • therwise : sblockn

}

slide-6
SLIDE 6

Function calls

Basic form: id(e1,e2,…,ek) General form: assignable(e1,e2,…,ek)

  • If f is a function, g(4,5) returns a

function, and r.h yields a function, then the following are legal: f(3) g(4,5)(3) r.h(3)

slide-7
SLIDE 7

How is function call carried out?

1. evaluate each of the argument expressions 2. mark the resulting values as parameters 3. invoke the function

slide-8
SLIDE 8

examples

f(x+1)

slide-9
SLIDE 9

examples

f(x+1)

Remember that the function call has structure.

slide-10
SLIDE 10

examples

f(x+1) t1 = x + 1

Generate code for the argument expression

slide-11
SLIDE 11

examples

f(x+1) t1 = x + 1 param t1

Mark the result as a parameter of the function call

slide-12
SLIDE 12

examples

f(x+1) t1 = x + 1 param t1 call(f,1)

Call the function. The second argument of the call indicates the arity of the function (i.e. how many parameters it has)

slide-13
SLIDE 13

examples

f(x+1,2*y) f(x+1) t1 = x + 1 param t1 call(f,1)

slide-14
SLIDE 14

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y)

Remember that the function call has structure.

slide-15
SLIDE 15

examples

f(x+1,2*y) t1 = x + 1 f(x+1) t1 = x + 1 param t1 call(f,1)

Evaluate the first argument expression.

slide-16
SLIDE 16

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1

Mark the result as a parameter.

slide-17
SLIDE 17

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y

Evaluate the second argument expresssion.

slide-18
SLIDE 18

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2

Mark the result as a parameter.

slide-19
SLIDE 19

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2)

Call the function.

slide-20
SLIDE 20

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

An alternative to intermingling the 'param' instructions with the argument evaluation is to gather them in a queue, then place them between the argument evaluations and before the 'call' instruction.

slide-21
SLIDE 21

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

A slightly more involved example.

slide-22
SLIDE 22

exercise

f(g(3*z),h(a+b,a*b))

What intermediate code do you come up with for this example?

slide-23
SLIDE 23

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

As before, remember the structure…

slide-24
SLIDE 24

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

…but not just the top-level structure!

slide-25
SLIDE 25

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

This translation will happen automatically due to the recursive structure of the function call for f…

slide-26
SLIDE 26

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) g(3*z) t1 = 3 * z param t1 t2 = call(g,1) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

…view this as a function call in isolation.

slide-27
SLIDE 27

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

Mark the result as a parameter.

slide-28
SLIDE 28

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

More structure!

slide-29
SLIDE 29

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t3 = a + b t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

expression

slide-30
SLIDE 30

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t3 = a + b param t3 t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

parameter marking

slide-31
SLIDE 31

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t3 = a + b param t3 t4 = a * b t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

expression

slide-32
SLIDE 32

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t3 = a + b param t3 t4 = a * b param t4 t5 = call(h,2) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

parameter marking and call

slide-33
SLIDE 33

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t3 = a + b param t3 t4 = a * b param t4 t5 = call(h,2) param t5 call(f,2) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2)

parameter marking and call

slide-34
SLIDE 34

examples

f(x+1) t1 = x + 1 param t1 call(f,1) f(x+1,2*y) t1 = x + 1 param t1 t2 = 2 * y param t2 call(f,2) f(g(3*z),h(a+b,a*b)) t1 = 3 * z param t1 t2 = call(g,1) param t2 t3 = a + b param t3 t4 = a * b param t4 t5 = call(h,2) param t5 call(f,2) t1 = x + 1 t2 = 2 * y param t1 param t2 call(f,2) t1 = 3 * z param t1 t2 = call(g,1) t3 = a + b t4 = a * b param t3 param t4 t5 = call(h,2) param t2 param t5 call(f,2) Alternate translation gathering 'param' instructions together with call to function.

slide-35
SLIDE 35

exercise

How will you modify your grammar rules to generate intermediate code for function calls? Assume that type checking and argument list length checking has already been accounted for in the semantic actions attached to productions:

  • type checking of each argument with

corresponding parameter declaration (remembering that there is no coercion allowed in either an explicit or an implicit assignment)

  • checking that the number of arguments and the

number of parameters is the same

slide-36
SLIDE 36

SP17 exercise outcome

Basic approach teams took was to gather up information about argument expressions in an expression list, and generate the 'param' instructions at the end of the 'assignable ablock' rule, but only if assignable is a function (as opposed to an array). After the param instructions have been generated the 'call' instruction is generated, including the arity of the function (which is determined either by looking it up in the symbol table

  • r by counting the number of arguments

supplied).

slide-37
SLIDE 37

Phases of a compiler

Figure 1.6, page 5 of text

Target machine code generation

slide-38
SLIDE 38

Memory organization

code static heap free memory stack

slide-39
SLIDE 39

Memory organization

static heap free memory stack machine language instructions of the program code

slide-40
SLIDE 40

Memory organization

heap free memory stack code static statically allocated memory (e.g. constants, string literals)

slide-41
SLIDE 41

Memory organization

free memory stack code static dynamically allocated memory (e.g. records, arrays) heap

slide-42
SLIDE 42

Memory organization

free memory stack code static heap grows towards stack heap

slide-43
SLIDE 43

Memory organization

stack code static 'free memory' denotes the unallocated memory between heap and stack heap free memory

slide-44
SLIDE 44

free memory

Memory organization

code static stack is used for function invocation records ("stack frames") heap stack

slide-45
SLIDE 45

free memory

Memory organization

code static stack grows towards heap heap stack

slide-46
SLIDE 46

stack free memory

Memory organization

code static The size, layout and contents of both the code and static regions are determined at compile time heap

slide-47
SLIDE 47

stack free memory

Memory organization

code static These regions are handled dynamically (i.e. at runtime) heap

slide-48
SLIDE 48

stack free memory

Memory organization

code static Heap allocation: reserve & release heap

slide-49
SLIDE 49

stack free memory

Memory organization

code static Stack allocation: function call heap

slide-50
SLIDE 50

temporaries

Stack frame organization

actual parameters

(arguments)

returned value control link

(dynamic link)

access link

(static link)

saved machine status

(return address)

local data

slide-51
SLIDE 51

temporaries

Stack frame organization

returned value control link

(dynamic link)

access link

(static link)

saved machine status

(return address)

local data

Initialized by caller, used by callee. May be in CPU registers.

actual parameters

(arguments)

slide-52
SLIDE 52

temporaries

Stack frame organization

control link

(dynamic link)

access link

(static link)

saved machine status

(return address)

local data

Initialized by callee, read by caller. May be in a CPU register.

actual parameters

(arguments)

returned value

slide-53
SLIDE 53

temporaries

Stack frame organization

access link

(static link)

saved machine status

(return address)

local data

The address

  • f the caller's

invocation record (stack frame).

actual parameters

(arguments)

returned value control link

(dynamic link)

slide-54
SLIDE 54

temporaries

Stack frame organization

saved machine status

(return address)

local data Used to achieve static scope for nested function definitions. Our language does not use this. Scheme/ML do. actual parameters

(arguments)

returned value control link

(dynamic link)

access link

(static link)

slide-55
SLIDE 55

temporaries

Stack frame organization

local data Information needed to restore machine to state at function call, including the return address (the value of the Program Counter at the time of the call). actual parameters

(arguments)

returned value control link

(dynamic link)

access link

(static link)

saved machine status

(return address)

slide-56
SLIDE 56

temporaries

Stack frame organization

saved machine status

(return address)

local data

Space for local variables.

actual parameters

(arguments)

returned value control link

(dynamic link)

access link

(static link)

slide-57
SLIDE 57

Stack frame organization

saved machine status

(return address)

local data Space for temporary variables, and variable-length local data Temporaries may be in CPU registers. actual parameters

(arguments)

returned value control link

(dynamic link)

access link

(static link)

temporaries