functions functions and parameters. history a function call needs to save the registers in use the...
TRANSCRIPT
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
Who Saves/Restores
• Who saves– The calling function– The called function
• Who restores– The calling function– The called function
• Any combination of the above
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
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.
Window Overflow
InLocalOut In Local Out
CWP ->
WIM ->InLocal
CWP ->
WIM ->Save to Frame where this %sp points
Out In Local Out
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
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
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.
<- i6 = fp
I registers
L registers
sp =o6 -> O registers
I registers
L registers
sp =o6 ->
O registers
Register
Set
Figure 7.1
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.
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
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);
function calls
• Put parameters they should be• call function_name• If function returns a value, look for return
value where it should be
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 )
The ret Statement
• The ret statement is also a jmpl instructionretrestoreis equivalent to
jmpl %i7 + 8, %g0// why %i7 + 8restore
functions.datadata stuff.text.global fun_name
fun_name: save %sp, value, %spget parameters
// code for function …
put answerretrestore
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
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
After the call (2)
.text
.global addemaddem: save %sp, -64, %sp
ld [%i7 + 8], %i0ld [%i7 + 12], %i1add %i0, %i1, %i0jmpl %i7 + 16, %g0restore
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
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
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.
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
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.*********************************
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.
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);}
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
Example 2
/**************************************** File: par_st.m* Purpose: To demonstrate* parameter passing using* the system stack.* Computes the same as par_reg.m******************************************/