Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Procedure Call Instructions Page 166 October 22, 1997 Procedure Call Instructions • Procedure calls involve the following actions 1. passing arguments2. saving a “return address”3. transferring from the caller to the callee 4. returning from the callee to the caller5. returning the results • Simplest examples include assembly-language “leaf” procedures, like the arithmetic intrinsics .mul , etc. a = b*c; ld b,%o0ld c,%o1call .mulnopst %o0,a optimized ld b,%o0call .mulld c,%o1st %o0,aCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Call/Return Instructions Page 167 October 22, 1997 Call/Return Instructions • Procedures are called with either call or jmpl • call instruction a format 1 instructionjumps to leaves PC , i.e. the location of the call , in %o7 ( %r15 ) • jmpl instruction format 3 instructionjumps to 32-bit address by address , which may be any addressing modeleaves PC in reg 01 disp30 31 29 24 18 13 12 4 10 reg 111000 rs1 i=0 0 rs210 reg 111000 rs1 i=1 simm13 31 29 24 18 13 12 4 call label PC 4 zeroextend disp30 ( ) + jmpl address reg ,Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Indirect Calls Page 168 October 22, 1997 Indirect Calls • jmpl implements indirect calls jumps to the 32-bit address specified in reg leaves PC — the return address — in %r15 e.g., for function pointers a = (*apply)(b, c); ld b,%o0ld c,%o1ld apply,%o3jmpl %o3,%r15; nopst %o0,a • jmpl implements procedure return jmpl %r15+8,%g0 transfers control from the callee to the caller (see also ret and retl )why +8 ? jmpl reg %r15 ,Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Procedure Calls Page 169 October 22, 1997 Procedure Calls • Procedure implementation must handle nested and recursive calls e.g., A calls B , B calls C must work when, e.g., B is A , etc. • Other requirements passing a variable number of argumentspassing and returning structuresallocating and deallocating space for localssaving and restoring caller’s registers • Entry and exit sequences collaborate to implement these requirements A:call BreturnB:call CreturnC:returnCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Stack Page 170 October 22, 1997 Stack • Procedure call information is stored in the stack locals, including compiler “temporaries”caller’s registers, if necessarycallee’s arguments, if necessary • SPARC’s stack grows downwards , i.e. from high to low addresses • The stack pointer, %sp ( %r14 ) points to the top 32-bit word on the stack %sp must always be a multiple of 8 • Stack operations to push %o1dec 4,%spst %o1,[%sp] to pop top word into %o1ld [%sp],%o1inc 4,%sp to allocate N bytes of stack space sub %sp, N ,%spCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Arguments and Return Values Page 171 October 22, 1997 Arguments and Return Values • By convention , the first 6 arguments are passed in registers; the rest are passed on the stack (97% of procedures have 6 or fewer arguments) • Caller places the arguments in the “out” registers;callee finds its arguments in the “in” registers caller what callee %o7 return address - 8 %i7%o6 stack pointer %i6 frame pointer %o5 sixth argument %i5 ... ... ... %o1 second argument %i1%o0 first argument %i0 • Callee places its return value in the “in” registers;caller finds the return value in the “out” registers caller what callee %o5 sixth return value %i5 ... ... ... %o1 second return value %i1%o0 first return value %i0Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Register Windows Page 172 October 22, 1997 Register Windows %i7%i0 ... %l7%l0 ... %o7%o0 ...callercallee %i7%i0 ... %l7%l0 ... %o7%o0 ... %i7%i0 ... %l7%l0 ... %o7%o0 ... • SPARC register windows : each procedure gets 16 “new” registers • The window “slides” at a call callee’s in registers become synonymous with the caller’s out registers • The SPARCs have 2–32 windows • save slides the window “forward” • restore slides the window “backwards”Copyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Register Windows, cont’d Page 173 October 22, 1997 Register Windows, cont’d • Most SPARCs have 8 windows • save / restore decrement/increment the c urrent w indow p ointer, CWP inlocaloutoutoutoutoutininininininoutoutoutlocallocallocalinlocallocallocallocal CWP + 1CWPCWP - 1 saverestoreCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Window Management Page 174 October 22, 1997 Window Management • save instruction save %sp, N ,%sp e.g., save %sp,-4*16,%sp slides the register window so the current window becomes the previous windowdecrements the current window pointer ( CWP ) and checks for window overflow adds N to the stack pointer, %sp ; i.e., allocates N bytes if N < 0 • If an overflow occurs, the registers are saved on the stack there must be enough stack space • restore instruction slides the register window so the previous window becomes the current windowincrements the current window pointer ( CWP ) and checks for window underflow • In save and restore source registers refer to the current window destination registers refer to the new windowCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: Stack Frame Page 175 October 22, 1997 Stack Frame • see page 189 in the SPARC Architecture Manual, §7.5 in Paul locals, temporariessaved floating point registersargument 6...argument 2argument 1pointer to struct return value16 words to holdsaved ins and locals %fp (old %sp ) %fp - offset %sp + offset %sp + offset outgoing arguments 7, 8,... %sp + offset %sp previous stack framealways allocatedCopyright 1995 D. Hanson, K. Li & J.P. Singh Computer Science 217: C Calling Convention Page 176 October 22, 1997 C Calling Convention • First 6 arguments are passed in %o0 — %o5 , the rest in the stack char out[30], str[] = "this is a sample string";main() { bcopy(out, str, sizeof str); }bcopy(char *dst, char *src, int nbytes) { ... } • Assembly language . seg "bss".global _out.common _out,30.seg "data".global _str_str:.ascii "this is a sample string\000".seg "text".global _main_main: save %sp,-96,%sp set _out,%o0set _str,%o1call _bcopyset 24,%o2ret; restore.global
View Full Document