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 Target machine code generation Figure 1.6, page 5 of text 7.2.3 Calling Sequence What happens during a function call? actual parameters


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

Target machine code generation

slide-3
SLIDE 3

7.2.3 Calling Sequence

What happens during a function call?

slide-4
SLIDE 4

saved machine status local data actual parameters returned value control link access link temporaries

caller's invocation record

top_sp top

Prior to function call.

slide-5
SLIDE 5

7.2.3 Calling Sequence

"Procedure calls are implemented by what are known as calling sequences, which consist of code that allocates an activation record on the stack and enters information into its fields." [p. 436]

slide-6
SLIDE 6

saved machine status local data actual parameters returned value control link access link temporaries saved machine status local data actual parameters returned value control link access link temporaries

caller's invocation record callee's invocation record

top_sp top

During function call.

slide-7
SLIDE 7

7.2.3 Calling Sequence

" A return sequence is similar code to restore the state of the machine so the calling procedure can continue its execution after the call." [p. 436]

slide-8
SLIDE 8

saved machine status local data actual parameters returned value control link access link temporaries … … … returned value … … …

caller's invocation record

After function call.

top_sp top

slide-9
SLIDE 9

Caller vs Callee responsibilities

"In general, if a procedure is called from n different points, then the portion of the calling sequence assigned to the caller is generated n

  • times. However, the portion assigned

to the callee is generated only once." [p. 436]

slide-10
SLIDE 10

Typical calling sequence [p. 437]

"1. The caller evaluates the actual parameters." Recall: formal parameter == parameter actual parameter == argument

slide-11
SLIDE 11

saved machine status local data actual parameters returned value control link access link temporaries

caller's invocation record

top_sp top

Prior to function call.

slide-12
SLIDE 12

saved machine status local data actual parameters returned value control link access link temporaries actual parameters

caller's invocation record

top_sp top

Caller writes arguments (actual parameters) past the end of its own invocation record.

slide-13
SLIDE 13

saved machine status local data actual parameters returned value control link access link temporaries actual parameters returned value

caller's invocation record

top_sp top

Caller knows the

  • ffset of the eventual

returned value. When callee returns the caller will look at this location for the returned value.

slide-14
SLIDE 14

saved machine status local data actual parameters returned value control link access link temporaries actual parameters returned value control link

caller's invocation record

top_sp top

The caller stores its stack pointer here.

slide-15
SLIDE 15

Typical calling sequence [p. 437]

"2. The caller stores a return address and the old value of top_sp into the callee's activation record. … "

slide-16
SLIDE 16

saved machine status local data actual parameters returned value control link access link temporaries actual parameters returned value

top_sp

caller's invocation record

top_sp top

The caller stores its stack pointer

  • here. When the

callee finishes the stack pointer's value will be reset to this value, thereby restoring the caller's invocation record as the active one (the

  • ne on top of the

stack).

slide-17
SLIDE 17

Typical calling sequence [p. 437]

"The caller then increments top_sp […] top_sp is moved past the caller's local data and temporaries and the callee's parameters and status fields."

slide-18
SLIDE 18

saved machine status

top_sp

Move top_sp saved machine status local data actual parameters returned value control link access link temporaries … actual parameters returned value access link …

caller's invocation record

top_sp

slide-19
SLIDE 19

Typical calling sequence [p. 437]

"3. The callee saves the register values and other status information."

slide-20
SLIDE 20

saved machine status local data actual parameters returned value control link access link temporaries

PC + machine status

actual parameters returned value access link

caller's invocation record

top top_sp

Write the return address, the current value of the Program Counter (PC), into the saved machine status. When the callee finishes execution will resume with the address pointed to by this saved address. … …

top_sp

slide-21
SLIDE 21

PC + machine status top_sp

When control transfers to the callee, the top_sp and top are updated. Callee writes local data and temporaries into its invocation record. saved machine status local data actual parameters returned value control link access link temporaries local data actual parameters returned value access link temporaries

caller's invocation record callee's invocation record

top_sp top

slide-22
SLIDE 22

PC + machine status top_sp

If the number of arguments can vary from call to call (e.g. printf) then the caller writes the arguments to the "actual parameters"
 area, as well as information about the number of arguments to the status area saved machine status local data actual parameters returned value control link access link temporaries local data actual parameters returned value access link temporaries

caller's invocation record callee's invocation record

top_sp top

slide-23
SLIDE 23

PC + machine status top_sp

If the callee has variable length local data (e.g. local arrays whose size is determined by the value of a parameter) then the arrays are allocated space at the end of the invocation record, and pointers to those arrays are stored in the "locals" block. saved machine status local data actual parameters returned value control link access link temporaries local data actual parameters returned value access link temporaries

caller's invocation record callee's invocation record

top_sp top

slide-24
SLIDE 24

Relocatable object code

Compiler produces relocatable object code: addresses are not absolute, but relative to known boundaries (e.g. Stack Pointer, start of record, Program Counter). Linker combines object code files into an executable file, in which static relative addresses are made absolute (in virtual address space). Loader copies contents of executable file into memory and starts execution.

slide-25
SLIDE 25

Relocatable object code

Compiler produces relocatable object code: addresses are not absolute, but relative to known boundaries (e.g. Stack Pointer, start of record, Program Counter). Linker combines object code files into an executable file, in which static relative addresses are made absolute (in virtual address space). Loader copies contents of executable file into memory and starts execution.

Leave relative

  • ffsets alone during

translation.

slide-26
SLIDE 26

Desirable characteristics

  • f generated code:

correctness (this is non-negotiable) small execution time small code size small power consumption

slide-27
SLIDE 27

Desirable characteristics

  • f generated code:

correctness (this is non-negotiable) small execution time small code size small power consumption

Associate costs with each instruction, then "minimize" (lower)

  • verall cost, with some

balance since execution time and code size can be in conflict.

slide-28
SLIDE 28

Significant tasks of code generator

instruction selection register allocation and assignment instruction ordering

slide-29
SLIDE 29

Significant tasks of code generator

instruction selection register allocation and assignment instruction ordering

Which variables are kept in registers?

slide-30
SLIDE 30

Significant tasks of code generator

instruction selection register allocation and assignment instruction ordering

Which specific register holds which value?

slide-31
SLIDE 31

Significant tasks of code generator

instruction selection register allocation and assignment instruction ordering

E.g. to minimize the number

  • f registers needed.
slide-32
SLIDE 32

Simple generation strategy vs. code size

If we generate code for each intermediate code instruction in isolation and string the results together the result may include redundant instructions

slide-33
SLIDE 33

Small example [p. 509]

Consider: x = y + z This might be translated as: LD R0, y ADD R0, R0, z ST x, R0

<— load the value of y into register R0 <— put into R0 the result of adding R0 and the value of z <— store the value of register R0 to x

slide-34
SLIDE 34

Larger example [p. 509]

Consider applying the same template to a larger example: a = b + c d = a + e This might be translated as: LD R0, b ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0, e ST d, R0

slide-35
SLIDE 35

Larger example [p. 509]

Consider applying the same template to a larger example: a = b + c d = a + e This might be translated as: LD R0, b ADD R0, R0, c ST a, R0 LD R0, a ADD R0, R0, e ST d, R0

This instruction is redundant: it is loading into R0 the value that is already there.