Functions

35
Functions Functions and Parameters

description

Functions. Functions and Parameters. History. A function call needs to save the registers in use The called function will use the registers The registers need to be restored when the called function is completed. This requires three things: Who saves the registers - PowerPoint PPT Presentation

Transcript of Functions

Page 1: Functions

Functions

Functions and Parameters

Page 2: Functions

History

• A function call needs to save the registers in use• The called function will use the registers• The registers need to be restored when the called

function is completed.• This requires three things:

– Who saves the registers – Who restores the registers– Where are they saved

Page 3: Functions

Who Saves/Restores

• Who saves– The calling function– The called function

• Who restores– The calling function– The called function

• Any combination of the above

Page 4: Functions

Where

• A location in memory– In registers– Pointed to by a register– In the caller’s frame– In the called frame– On the stack– Register sets

• Sparc Architecture uses the frame and register sets

Page 5: Functions

Sparc Register File

• In the Sparc architecture there are a minimum of 128 registers and 8 global registers

• These are grouped into 8 global registers and 24 mapped programmer registers

• The save instruction changes the register mapping to a new set

• The restore instruction restores the old set. • The CWP and WIM are two system registers pointing

to the current window of registers and the last free register set.

Page 6: Functions

Sparc Register File

Sixteen General Registers

InLocalOut

CWP ->

WIM ->

Page 7: Functions

Sparc Register File

Sixteen General Registers

InLocalOut In Local Out

CWP ->

WIM ->

Page 8: Functions

Sparc Register File

Sixteen General Registers

InLocalOut In Local Out In Local Out

CWP ->

WIM ->

Page 9: Functions

Window Overflow

Sixteen General Registers

InLocalOut In Local Out

CWP ->

WIM ->

Page 10: Functions

Window Overflow

InLocalOut In Local Out

CWP ->

WIM ->InLocal

CWP ->

WIM ->Save to Frame where this %sp points

Out In Local Out

Page 11: Functions

Window Overflow

InLocalCWP ->

WIM -> CWP ->

WIM ->Save to Frame

InLocalOut In Local Out

Page 12: Functions

Function Registers

• Each function gets its own registers• The global registers (one copy for all)• Its own copy of the other registers

– The I registers– The L registers– The O registers

Page 13: Functions

The frame pointer %i6

The stack pointer %o6

R31

R30

R29

R28

R27

R26

R25

R24

R23

R22

R21

R20

R19

R18

R17

R16

R15

R14

R13

R12

R11

R10

R9

R8

I registers

L registers

O registers

Page 14: Functions

A Function Call

• A function call creates a new frame• A called function gets access to the %g

registers• A called function’s I registers are the calling

function’s O registers• The called function gets its own L registers

and O registers. These are called a register set.

Page 15: Functions

<- i6 = fp

I registers

L registers

sp =o6 -> O registers

I registers

L registers

sp =o6 ->

O registers

Register

Set

Figure 7.1

Page 16: Functions

Save Instruction

• save %sp, some_value, %sp• The old sp, %o6 becomes %i6, %fp in the new

register set after the save• The save instruction is also an ADD

instruction. It adds some_value to the current %sp, putting the result into the new %sp, leaving the old %sp unchanged.

Page 17: Functions

Restore

• The restore instruction restores the register window set.

• If underflow occurs (the opposite of overflow), the system restores the registers from the frame where the %sp points

Page 18: Functions

Functions vs Subroutines

• A subroutine / procedure can be viewed as a function that returns void (C/C++/Java)

procedure print_list(Listtype L); (PASCAL)void print_list( Listtype L);• A function can be viewed as a procedure that

returns a value (ALGOL)integer procedure factorial(integer N);

Page 19: Functions

function calls

• Put parameters they should be• call function_name• If function returns a value, look for return

value where it should be

Page 20: Functions

The call statement

The call statement is equivalent to a jump long instruction

call fun_name

is equivalent

set fun_name, %o0jmpl %o0, %o7

The %pc at the call is saved in %o7 (= %i7 of the called function )

Page 21: Functions

The ret Statement

• The ret statement is also a jmpl instructionretrestoreis equivalent to

jmpl %i7 + 8, %g0// why %i7 + 8restore

Page 22: Functions

functions.datadata stuff.text.global fun_name

fun_name: save %sp, value, %spget parameters

// code for function …

put answerretrestore

Page 23: Functions

Where, and How, are the Arguments

• After the call statement• On the stack• In registers• In memory• By value• By reference (address)• By address of values• By address of addresses

Page 24: Functions

After the call (1)

call addem ! Addr = %o7nop ! Addr = %o7 + 4.word 3 ! Addr = %o7 + 8.word 4 ! Addr = %o7 + 12! Return here Addr = %o7 + 16

Page 25: Functions

After the call (2)

.text

.global addemaddem: save %sp, -64, %sp

ld [%i7 + 8], %i0ld [%i7 + 12], %i1add %i0, %i1, %i0jmpl %i7 + 16, %g0restore

Page 26: Functions

Remarks

• Very efficient• Non-recursive• Cannot compute the parameters

Modern computers will not allow a program to store into the code section

• Cannot have a variable number of parameters

Page 27: Functions

Arguments on Stack

• This technique is very flexible• Allows recursion• Number of parameters can vary for the same

function• But requires memory access to load and

store parameters’ values and results

Page 28: Functions

Sparc Solution

• First six arguments are placed into the O registers.

• These O registers become the called functions I registers.

• No memory references are necessary for these parameters.

Page 29: Functions

But …

• More than six arguments requires the use of the stack

• Each stack argument occupies one word, even byte arguments

• Byte arguments must be moved into word quantities before stored in the stack.

• First argument is at %sp + 68• The frame size must allow for all the parameters• Frame size = 16 + locals + parameters + 68

Page 30: Functions

Example

/****************************

* File: par_reg.m

* main reads two numbers x

* and y, and calls the function

* f(x,y,&result) to compute* x*y + 2 and return the value

* in result.*********************************

Page 31: Functions

Exercise

Write a program to read in two integers a and b, compute a*a + b*b, and print out the sum. The program should loop until two integers are not read. Use a separate functions for each part. Equivalent C code is next.Pass parameters using registers.

Page 32: Functions

Exercise (con’t)

void print_prompt();int read(int & a, int & b);void write(int a, int b);while(print_prompt(), read(a, b) != 2){ s = fun(a, b); write(a, b, s);}

Page 33: Functions

Stack Parameters

• Parameters may be put on the stack• Which frame: caller or called?

– Many machines it is the called– For the Sparc it is the caller

• Therefore, the caller refers to them using the stack pointer

• The called function uses the frame pointer

Page 34: Functions

Example 2

/**************************************** File: par_st.m* Purpose: To demonstrate* parameter passing using* the system stack.* Computes the same as par_reg.m******************************************/

Page 35: Functions

Exercise

Repeat the previous exercise, but pass the parameters using the stack.