
SubroutineCalls CS217 SubroutineCalls • Callingasubroutineinvolvesfollowingactions passarguments saveareturnaddress transfercontroltocallee transfercontrolbacktocaller returnresults intaddthree(int a,int b,int c) { returna+b+c; } int main() { intd=add(3,4,5); printf(“%d\n”,d); return0; } 1 SubroutineCalls • Requirements SetPCtoarbitraryaddress ReturnPCtoinstructionaftercallsequence Handlenestedsubroutinecalls Saveandrestorecaller’sregisters Passanarbitrarynumberofarguments Passandreturnstructures Allocateanddeallocatespaceforlocalvariables • Subroutinecallandreturnsequencescollaborate toimplementtheserequirements SubroutineCalls • Requirements SetPCtoarbitraryaddress ReturnPCtoinstructionaftercallsequence Handlenestedsubroutinecalls Saveandrestorecaller’sregisters Passanarbitrarynumberofarguments Passandreturnstructures Allocateanddeallocatespaceforlocalvariables • Subroutinecallandreturnsequencescollaborate toimplementtheserequirements 2 CallInstruction calllabel Example: lda,%o0 ldb,%o1 ldc,%o2 calladdthree nop st%o0,d CallInstruction calllabel 01 disp30 31 29 Jumpstoaddressoflabeland leavesPC (locationofcall)in%o7 (%r15) %o7=PC;/*returnaddress*/ PC=nPC; nPC=PC+sign_extend(disp30)<<2; Example: lda,%o0 ldb,%o1 ldc,%o2 calladdthree nop st%o0,d 3 CallswithFunctionPointers jmplreg,%o7 jumpstothe32-bitaddressspecifiedinreg leavesPC (returnaddress)in%o7(%r15) example: d=(*apply)(a,b,c); lda,%o0 ldb,%o1 ldc,%o2 ldapply,%o3 jmpl %o3,%o7;nop st%o0,d JumpInstruction jmpl address, reg 10 reg 111000 rs1 0 0 rs2 10 reg 111000 rs1 1 simm13 31 29 24 18 13 12 4 leavesPC in reg reg =PC;/*returnaddress*/ PC=nPC; nPC=rs1+op2; 4 ReturnInstructions retandretl returnstocalleratendofsubroutine retl issyntheticinstructionsfor jmpl %o7+8,%g0 Example: lda,%o0 ldb,%o1 ldc,%o2 calladdthree nop st%o0,d addthree:add%o0,%o1,%o0 TThhiissiissaa add%o0,%o2,%o0 ““lleeaaff””ssuubbrroouuttiinnee retl LeafSubroutines • Simplyusecaller’sregisters Donotsave/restore Useonly%o0-%o5,%g0-%g1 • Advantages Simple lda,%o0 ldb,%o1 Littleoverhead ldc,%o2 calladdthree nop st%o0,d addthree:add%o0,%o1,%o0 add%o0,%o2,%o0 retl 5 LeafSubroutines • Limitations Cannottakemorethan6parameters Cannothavearbitrarynumberoflocalvariables Cannotcallanotherfunction Cannotreturnstructure main(){ t(1,2,3,4,5,6,7,8); } int t(inta1,inta2,inta3,inta4, inta5,inta6,inta7,inta8){ intb1=a1; returns(b1,a8); } int s(intc1,intc2){ returnc1+c2; } NestedSubroutineCalls • A callsB,whichcallsC A: B: C: callBcallC returnreturn return MustevenworkwhenB isA 6 SubroutineCalls • Requirements SetPCtoarbitraryaddress ReturnPCtoinstructionaftercallsequence Handlenestedsubroutinecalls Saveandrestorecaller’sregisters Passanarbitrarynumberofarguments Passandreturnstructures Allocateanddeallocatespaceforlocalvariables • Subroutinecallandreturnsequencescollaborate toimplementtheserequirements RegisterWindows • Machinehasmorethan32registers cwp in Eachsubroutinegets16“new”registers local Allsubroutinescanuseglobals out • Thewindow“slides”atcalltime caller’soutregistersbecome callee’s inregisters • Instructions save slidesthewindowforward restore slidesthewindowbackwards decrement/incrementsCWPregister • Finitenumberofwindows(usually8) wim 7 RegisterWindows • Machinehasmorethan32registers Eachsubroutinegets16“new”registers Allsubroutinescanuseglobals cwp in local • Thewindow“slides”atcalltime out caller’soutregistersbecome callee’s inregisters • Instructions save slidesthewindowforward restore slidesthewindowbackwards decrement/incrementsCWPregister • Finitenumberofwindows(usually8) wim RegisterWindows • Machinehasmorethan32registers Eachsubroutinegets16“new”registers Allsubroutinescanuseglobals in • Thewindow“slides”atcalltime cwp local caller’soutregistersbecome out callee’s inregisters • Instructions save slidesthewindowforward restore slidesthewindowbackwards decrement/incrementsCWPregister • Finitenumberofwindows(usually8) wim 8 RegisterWindows • Machinehasmorethan32registers Eachsubroutinegets16“new”registers Allsubroutinescanuseglobals cwp in local • Thewindow“slides”atcalltime out caller’soutregistersbecome callee’s inregisters • Instructions save slidesthewindowforward restore slidesthewindowbackwards decrement/incrementsCWPregister • Finitenumberofwindows(usually8) wim RegisterWindows • Machinehasmorethan32registers cwp in Eachsubroutinegets16“new”registers local Allsubroutinescanuseglobals out • Thewindow“slides”atcalltime caller’soutregistersbecome callee’s inregisters • Instructions save slidesthewindowforward restore slidesthewindowbackwards decrement/incrementsCWPregister • Finitenumberofwindows(usually8) wim 9 RegisterWindows(cont) %i7 … %i0 %l7 … %l0 %o7 %i7 … … %o0 %i0 save restore %l7 … %l0 %o7 %i7 … … %o0 %i0 %l7 … %l0 %o7 … %o0 SaveandRestoreCommands • Calltime(save%sp,N,%sp) decrementsCWP » currentregisterwindowbecomespreviouswindow » ifoverflowoccurs,saveregistersonthestack addsNtothestackpointer » framepointerbecomespreviousstackpointer • Returntime(restore) incrementsCWP » previouswindowbecomescurrentwindow subtractsNfromthestackpointer » restorespreviousstackpointerandframepointer 10 ArgumentsandReturnValues • Byconvention callerplacesargumentsinthe“out”registers callee findsitsargumentsinthe“in”registers onlythefirst6argumentsarepassedinregisters therestarepassedonthestack lda,%o0 ldb,%o1 ldc,%o2 calladdthree nop st%o0,d addthree:save%sp,-64,%sp add%i0,%i1,%i0 ret restore ArgumentsandReturnValue(cont) • Registersatcalltime caller callee %o7%i7returnaddress-8(%r15) %o6%i6stack/framepointer(%r14) %o5%i5sixthargument …… %o0%i0firstargument • Registersatreturntime caller callee %o5%i5sixthreturnvalue %o4%i4fifthreturnvalue …… %o0%i0firstreturnvalue 11 ComparisonofSubroutineTypes • Example: lda,%o0 lda,%o0 ldb,%o1 ldb,%o1 ldc,%o2 ldc,%o2 calladd calladd nop nop st%o0,c st%o0,c add:add%o0,%o1,%o0 add:save%sp,-64,%sp add%o0,%o2,%o0 add%i0,%i1,%i0 retl add%i0,%i2,%i0 ret restore LeafSubroutine “Regular”Subroutine SubroutineCalls • Requirements SetPCtoarbitraryaddress ReturnPCtoinstructionaftercallsequence Handlenestedsubroutinecalls Saveandrestorecaller’sregisters Passanarbitrarynumberofarguments Passandreturnstructures Allocateanddeallocatespaceforlocalvariables • Subroutinecallandreturnsequencescollaborate toimplementtheserequirements 12 Stack • Subroutinecallinformationstoredonstack callee’s arguments,ifnecessary localvariables,ifnecessary caller’sregisters,ifnecessary • Informationaddedtostackbeforecalland removedfromstackuponreturn Thestackpointer(%sp)pointstotopwordonthestack (mustbemultipleof8) Theframepointer(%fp)pointstothepositionofthe stackpointerinthecaller’sframe(beforethesave instruction) StackFrame %fp(old %sp) previousframe local&tempvars %fp - offset savedFPregs %sp + offset arguments7,8,… argument6 %sp + offset argument5 argument4 argument3 argument2 argument1 ptr tostructrtrn 16wordstohold %sp savedin/localregs 13 ExampleStackFrames main(){ t(1,2,3,4,5,6,7,8); } t(inta1,inta2,inta3,inta4, inta5,inta6,inta7,inta8){ intb1=a1; returns(b1,a8); } s(intc1,intc2){ returnc1+c2; } Example(cont) a8 +96 a7 +92 a6 +88 _main:save%sp,-104,%sp a5 set1,%o0 a4 set2,%o1 main a3 a2 set3,%o2 a1 set4,%o3 struct * +64 set5,%o4 16words set6,%05 %sp set7,%i5 st%i5,[%sp+4*6+68] set8,%i5 st %i5,[%sp+4*7+68] call_t;nop ret;restore 14 Example(cont) a8 +96 a7 +92 a6 +88 _t:save%sp,-96,%sp a5 st%i0,[%fp-4] a4 main a3 ld[%fp-4],%o0 a2 a1 ld[%fp+96],%o1 struct * +64 call_s;nop 16words mov%o0,%i0 %fp b1 -4 ret;restore +88 +84 _s:add%o0,%01,%o0 t c2 retl;nop c1 +64 struct * 16words %sp Summary • Caller Loadsfirstsixargumentsinto%o0,%o1,… Loadsotherargumentsontostack(%sp+68+4*i) Usescallorjmpl tosetPC(savesoldPCin%o7) Receivesreturnvaluesin%o0,%o1,… • Callee Executessaveinstructionatbeginning Accessesfirstsixargumentsinregisters Accessesotherargumentsonstack(%fp +68+4*i) Accesseslocalvariablesonstack(%fp– 4*i) Usesret toreturn(%jmpl%o7+8,%g0) Executesrestoreinstructionatend 15.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-