procedure and function calls, - clear.rice.edu · •at runtime, those sequences (prolog, epilog,...

30
Procedure and Function Calls, Part II — Comp 412 COMP 412 FALL 2017 Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Chapter 6 in EaC2e Front End Optimizer Back End IR IR source code target code

Upload: hoangdat

Post on 28-Apr-2018

220 views

Category:

Documents


1 download

TRANSCRIPT

ProcedureandFunctionCalls,— PartII—

Comp412

COMP412FALL2017

Copyright2017,KeithD.Cooper&LindaTorczon,allrightsreserved.

StudentsenrolledinComp412atRiceUniversityhaveexplicitpermissiontomakecopiesofthesematerialsfortheirpersonaluse.

Facultyfromothereducationalinstitutionsmayusethesematerialsfornonprofiteducationalpurposes,providedthiscopyrightnoticeispreserved.

Chapter6 inEaC2e

FrontEnd Optimizer BackEnd

IR IRsource

code

target

code

LinkageConvention:High-LevelView

Thelinkagemust:• Preserve& protectthecaller’senvironmentfromcallee (&itscallees)– Forexample,itmustpreservevaluesinregistersthatareliveacrossthecall

• Createanewenvironmentforthecallee (newnamespace)– Atruntime,itcreatesalocaldataareaforthecallee & tiesittothecontext

• Mapnames& resourcesfromcallerintothecallee,asneeded

Toaccomplishthesegoals:• Conventiondividesresponsibilitybetweencaller& callee• Caller& callee mustusethesamesetofconventions– Implieslimitedopportunitiesforcustomization& optimization

Weexpectcompiledcodetoworkevenwhencreatedwithdifferentcompilers,perhapsindifferentlanguages

COMP412,Fall2017 1

fee fie

foe

foemustoperateinmultipledistinctcallingcontexts.

Callers

Callee

LinkageConvention

Aprocedureorfunctionhastwodistinctrepresentations

COMP412,Fall2017 2

Codetoimplementtheprocedure• Codeforallthestatements• Codetoimplementthelinkageconvention• Labelsonentrypoints andreturnpoints♦ Usenamemanglingtocreateconsistentlabels

Datatorepresentaninvocation• Activationrecord(AR)• Localdata+savedcontextofcaller• Controlinformation• Storagetopreservecaller’senv’t♦ ChainARstogethertocreatecontext

• Staticdataarea(namemangling)

prolog

epilog

@foe_:

prolog

epilog

pre-call

post-return

@fee_:

@fee_r1:

ARP

fee’sAR foe’sAR

#fee_:#foe_:

The“Meta”Issue

Oneofthemostdifficultaspectsofunderstandinghowcompilersworkiskeepingtrackofwhathappensatcompiler-designtime,compiler-buildtime,compiletime,andruntime.

• Atcompiletime,thecompileremitscodefortheapplicationprogram,includingallofitsprocedures(andtheirprologs,epilogs,&sequences)– Thecompilerusessymboltablestokeeptrackofnames– Thosetablesarecompile-time datastructures

• Atruntime,thosesequences(prolog,epilog,pre-call,&post-return)create,populate,use,anddestroyARsforinvocationsofprocedures– Therunningcodestoresdata(bothprogramdataandcontroldata)intheARs– ThoseARsarerun-time datastructures

Toconfusemattersfurther,thecompilermaypreservethesymboltablessothattheruntimedebuggingenvironmentcanlocatevariablesandvalues,andlocatethevariousdataareasandtheARs

COMP412,Fall2017 3

ProcedureLinkages

StandardProcedureLinkage

COMP412,Fall2017 4

Eachprocedurehas• Astandardprolog• Astandardepilog

Eachcalluses• Apre-callsequence• Apost-returnsequence

Thecodeforthepre-callandpost-returnsequencesmustbecompletelypredictablefromthecallsite.Itdependsonthenumber&thetypesoftheactualparameters.

Note: Thegapbetweenpre-call&post-returncodeisartisticlicense.

functionsqrt

prolog

epilog

proceduremainprolog

epilog

…z =a*a+b*bx=sqrt(y,z)

pre-call

post-returnL:

caller

callee

ActivationRecordBasics

Inmostsystems,ActivationRecordshaveasimilarlayout

COMP412,Fall2017 5

spaceforparameters

registersavearea

slot forreturnvalue

slotforreturnaddress

slot foraddressability

slotforcaller’sARP

spaceforlocalvariables

ARP

OneARforeachinvocationofaprocedureOneregisterdedicatedtoholdthecurrentARP

ARP ≅ ActivationRecordPointer

Spaceforparameterstothecurrentprocedure

Contentsofsavedregisters

Iffunction,spaceforreturnvalue

Addresstoresumecaller

Helpwithnon-localaccess

Torestorecaller’sAR onreturn

Spaceforlocalvariables,temporaries,&spills

n

10

IfARsarestackallocated,thestackgrowsinthisdirection

k

0

01

m

ProcedureLinkages

Pre-callSequence• Setsupcallee’sbasicAR• Helpspreserveitsownenvironment

TheDetails• Allocatesspaceforthecallee’sAR– exceptspaceforlocalvariables

• Evaluateseachparameter&storesvalueoraddress• Savesreturnaddress& caller’sARP intocallee’sAR• Addressabilitymaintenance,ifnecessary• Savesanycaller-saveregistersthatareliveacrossthecall– Saveintospaceincaller’sAR

• Jumpstoaddressofcallee’sprologcode

COMP412,Fall2017 6

Wheredoparametervaluesreside?Conventionalwisdomsays:• Inregisters (First3or4 )• Incallee’sAR (therest )

prolog

epilog

pre-call

post-return

prolog

epilog

Pre-callsequenceisalotofwork

ProcedureLinkages

Post-returnSequence• Finishesrestoringcaller’senvironment• Placesanyvaluebackwhereitbelongs– e.g.,anyreferenceparameterorglobal

thatiskeptinaregisterinthecallee

TheDetails• CopiesreturnvaluefromAR intoaregister,ifneeded• Freesthecallee’sAR– Ifaccesslinksareused,thisactiondiscardscallee’slink

• Restoresanycaller-saveregisters• Restoresanycall-by-referenceparameterstoregisters,ifneeded– Alsocopybackcall-by-value/resultparameters

• Continuesexecutionafterthecall

COMP412,Fall2017 7

prolog

epilog

pre-call

post-return

prolog

epilog

ProcedureLinkages

PrologCode• Finishessettingupcallee’senvironment• Preservespartsofcaller’senvironmentthatwillbedisturbedbythecallee

TheDetails• Preservesanycallee-saveregisters• Addressabilitymaintenance,ifnecessary• Allocatesspaceforlocaldata– EasiestscenarioistoextendtheAR

• Findsanystaticdataareasreferencedinthecallee• Handlesanylocalvariableinitializations– Notanycheapertosay“int x=0;”

WithheapallocatedAR,mayneedaseparateheapobjectforlocalvariables.(Mustknowsizeoflocaldataarea)COMP412,Fall2017 8

prolog

epilog

pre-call

post-return

prolog

epilog

ProcedureLinkages

EpilogCode• Windsupthebusinessofthecallee• Startsrestoringthecaller’senvironment

TheDetails• Storesreturnvalue?– Placeit,orapointer,inthereturnvalue

slotofthecaller’sAR 1

– Otherschemesareequallyfeasible

• Restorecallee-saveregisters,asneeded• Addressabilitymaintenance,ifnecessary• Freespaceforlocaldata,ifnecessary(ontheheap)• Restorereturnaddress&caller’sARP fromAR• Jumptothereturnaddress

COMP412,Fall2017 9

prolog

epilog

pre-call

post-return

prolog

epilog

Takeawaypoint:procedurecallshavemanycyclesofoverhead.

1Returnvalueneedsstoragethatsurvivesthecall

IfARsarestackallocated,deallocationisimplicit(&cheap)whenstacktopisresettoitspre-callvalue.

WhereDoSavedRegistersGo?

COMP412,Fall2017 10

spaceforparameters

registersavearea

slot forreturnvalue

slotforreturnaddress

slot foraddressability

slotforcaller’sARP

spaceforlocalvariables

ARP

OneARforeachinvocationofaprocedureOneregisterdedicatedtoholdthecurrentARP

Spaceforparameterstothecurrentprocedure

Contentsofsavedregisters

Iffunction,spaceforreturnvalue

Addresstoresumecaller

Helpwithnon-localaccess

Torestorecaller’sAR onreturn

Spaceforlocalvariables,temporaries,&spills

p savesitsownregistersinitsownRSA

psavesitsspillsinitsownlocaldataarea.

Ifp alwayssavesregistersintoitsownAR,thenthecompilerknowshowmuchspacetoreserveintheARforthosepurposes— p’sowncallersavesregisters(inprolog),p’sowncallee savesregisters(inpre-callwhenpcallsq),andp’sownspillsintheregisterallocatorrunonp. (prologpatchedafterallocation)

WhereDoParametersGo?

COMP412,Fall2017 11

spaceforparameters

registersavearea

slot forreturnvalue

slotforreturnaddress

slot foraddressability

slotforcaller’sARP

spaceforlocalvariables

ARP

Spaceforparameterstothecurrentprocedure

Contentsofsavedregisters

Iffunction,spaceforreturnvalue

Addresstoresumecaller

Helpwithnon-localaccess

Torestorecaller’sAR onreturn

Spaceforlocalvariables,temporaries,&spills

Parametersfromcalleegohere

spaceforparameters

Parameterspassedtoanothercallgohere

Callee’s activationrecord

CreatingandDestroyingActivationRecords

Theactivationrecordembodiestheruntimestateoftheprocedure

• HowareARscreatedanddestroyed?– Procedurecallmustallocate&initialize (preservecaller’sworld)

– Returnmustdismantleenvironment (andrestorecaller’sworld)

• Caller&callee mustcollaborateontheproblems– Calleraloneknowssomeofthenecessarystate➝ Returnaddress,parametervalues,accesstootherscopes

– Calleealoneknowstherest➝ Sizeoflocaldataarea(withspills),registersitwilluse

Theircollaborationtakestheformofalinkageconvention

Assume,forthemoment,anAlgol-60-likeenvironmentwheretheARisdeadafterthereturn.

COMP412,Fall2017 12

1. Linkageconventionisdefined atcompilerdesigntime2. Linkagecodeisemitted atcompiletime3. Linkagecodeexecutes atruntime,whenaprocedureiscalled.

StoringActivationRecords

Ifactivationrecordsarestoredonthestack• Easytoextend— simplybumptopofstackpointer• Caller&callee shareresponsibility– Callercanpushparameters,spaceforregisters,returnvalueslot,return

address,addressabilityinfo,&itsownARP– Calleecanpushspaceforlocalvariables(fixed&variablesize)

Algol-60rules

COMP412,Fall2017 13

Whatisthisstack?

COMP412,Fall2017 14

AddressSpaceLayout

Mostlanguageruntimeslayouttheaddressspaceinasimilarway

• Pieces(stack,heap,code,&globals)maymove,butallwillbethere• Stackandheapgrowtowardeachother(ifheapgrows)

Heap

Stacks

Grow

thsp

ace

forstacks

Glob

als

Code

JavaMemoryLayout

LIFORuntimeStackAR

forfeeAR

forfieAR

forfoeAR

forfieAR

forfumAR

forfoe

NewARs

Lecture21,StorageLayout(slide9)

StoringActivationRecords

Ifactivationrecordsarestoredonthestack• Easytoextend— simplybumptopofstackpointer• Caller&callee shareresponsibility– Callercanpushparameters,spaceforregisters,returnvalueslot,return

address,addressabilityinfo,&itsownARP– Calleecanpushspaceforlocalvariables(fixed&variablesize)

Ifactivationrecordsarestoredontheheap• Hardtoextend• Severaloptions– Callerpasseseverythinginregisters;callee allocates&fillsAR– Storeparameters,returnaddress,etc.,incaller’sAR!– Storecallee’sAR sizeinadefinedstaticconstant

Algol-60rules

MLrules

COMP412,Fall2017 15

Namemangling,again

COMP412,Fall2017 16

AddressSpaceLayout

Mostlanguageruntimeslayouttheaddressspaceinasimilarway

• Pieces(stack,heap,code,&globals)maymove,butallwillbethere• Stackandheapgrowtowardeachother(ifheapgrows)

Stacks

Grow

thsp

ace

forstacks

Glob

als

Code

JavaMemoryLayout

ARforfee

ARforfie

ARforfie

ARforfie

ARforfum

ARforfoe

StoringActivationRecords

Ifactivationrecordsarestoredonthestack• Easytoextend— simplybumptopofstackpointer• Caller&callee shareresponsibility– Callercanpushparameters,spaceforregisters,returnvalueslot,return

address,addressabilityinfo,&itsownARP– Calleecanpushspaceforlocalvariables(fixed&variablesize)

Ifactivationrecordsarestoredontheheap• Hardtoextend• Severaloptions– Callerpasseseverythinginregisters;callee allocates&fillsAR– Storeparameters,returnaddress,etc.,incaller’sAR!– Storecallee’sAR sizeinadefinedstaticconstant

Withoutrecursion,activationrecordscanbestatic

Algol-60rules

MLrules

Fortran66&77

COMP412,Fall2017 17

Namemangling,again

COMP412,Fall2017 18

AddressSpaceLayout

Mostlanguageruntimeslayouttheaddressspaceinasimilarway

• Pieces(stack,heap,code,&globals)maymove,butallwillbethere• Stackandheapgrowtowardeachother(ifheapgrows)

StaticallyallocatedARsgointothe“Global”dataarea—whichwewouldneedtoredrawinasalargerarea…

HEAP

Stacks

Grow

thsp

ace

forstacks

Glob

als

Code

JavaMemoryLayout

WhereDoVariablesLive?

WehaveseenARs,staticdataareas,globaldataareas,…

Howdoesthecompilerdecidewheretoplaceeachvariable• Acombinationofvisibilityandlifetimedeterminesthatplacement

Variablelengthitems?• Putadescriptorinthenaturalplace&allocatespaceinAR orintheheap– Requiresonelevelofindirection(a“pointer”)– Allowsuniformaddressability– InAR ifAR isextendible– OnheapifAR isnotextendible

COMP412,Fall2017 19

Lifetime Scope Location

automatic local declaringprocedure’sAR

static any named staticdataareaforitslifetime

dynamic any heap

Automatic: LifetimematchesprocedureactivationStatic: Lifetimemaybeaslongasentire

executionDynamic: Lifetimeisunderprogramcontrol&not

known atcompiletime

HowDoWeAddressVariables?

Localvariables• NeedamechanismtolocatethelocaldataareainappropriateAR• Representthevariableasastaticcoordinate:< level,offset>– Level isthelexicalnestingleveloftheprocedurethatdeclaresthevariable– Offset istheoffsetwithintheAR’slocaldataareaforthatprocedure

• Mechanismtakesstaticcoordinatetorun-timeaddress

Staticvariables(includingglobal)• Needabase-address,offsetpair (manglednameforbaseaddress)– Generatealoadofthebaseaddress– Addtheoffsetandloadthevalue (loadAI ifoffsetissmallenough)

• Mustberecognizableasnon-localcase (representdifferently)

Dynamicvariables• Programmermanagesaccessthroughpointers,names,…

COMP412,Fall2017 20

HowDoWeAddressVariables?

Localvariableofcurrentprocedure• Converttostaticcoordinate• Addoffset torARP andload

Localvariableinsurroundinglexicalscope• Converttostaticcoordinate• Convertlevel toARP forprocedureatthatlevel• Addoffset tothatARP andload

Localvariablesofsurroundingscopes,staticvariablesatvariousscopes,andglobalvariablesareoftencalled“freevariables”inthePL literature

COMP412,Fall2017 21

MustfindthecorrectAR

FindingtheCorrectAR

EachAR hasafieldforaddressability

COMP412,Fall2017 22

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

ARP

Onecommonschemeusesaccess links• EachAR hasalinktotheAR ofitslexicalancestor(surroundingscope)• Lexicalancestorneednotbetheprocedurethatcalledit• CompilercanusestaticcoordinatetogeneratecodethatchasesthechainofaccesslinkstofindthecorrectAR♦ Referenceto<p,16>runsupchaintofindlevelp’sARP,thenadds16

♦ Numberofderefences is(c– p),wherecisthecurrentlexicallevel

• Costofaccessisproportionaltolexicaldistancebetweenc andprARP isaphysicalregister,

dedicatedtoholdingtheARP

rARP

FindingtheCorrectAR

COMP412,Fall2017 23

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

AR for foe

ARP

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

AR for fie

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

AR for fee

procedure fee {procedure fie {

procedure foe {}call foe()

}call fie()

}

Static Coord GeneratedCode

<3,8> loadAI rARP,8 => r10

<2,12> loadAI rARP,4 => r10loadAI rARP,12 => r10

<1,16> loadAI rARP,4 => r10loadAI rARP,4 => r10loadAI rARP,12 => r10

UsingAccessLinks

FindingtheCorrectAR

COMP412,Fall2017 24

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

ARP

Ateachcall:• Inpre-callsequence,callermustfindtheappropriateARP• Assumecallerisatlevelk♦ Calleeatlevelk+1→ UsecurrentARP aslink

♦ Calleeatlevelj,0<j ≤k→ Walkupaccesslinkchaintolevelj -1→ UsethatARP aslink

♦ Calleeisglobal(e.g.,atlevel0)→ UseNULL aslink→ Caller&calleesharenolexicalcontext

• Aslongasthecodeiscorrect♦ Caller’schainwillbelongenough♦ Calleecannotwalkoffendofchain

MaintainingAccessLinks

FindingtheCorrectAREachAR hasafieldforaddressability

COMP412,Fall2017 25

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

ARP

TheLinkageCanAlsoUseADisplay• GlobalvectorofpointerstotheARsofsurroundingprocedures• AnynameableAR canbereachedinconstanttime♦ CurrentAR isreachedthroughARP♦ Surroundingscopesarereachedbytreatingdisplayasavector

• Referenceto<p,16>,forp <k,looksupthelevelp ARP inthedisplayandadds16toit• Costofaccessisconstant

FindingtheCorrectARUsingADisplay

COMP412,Fall2017 26

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

AR for foe

ARP

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

AR for fie

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

AR for fee

procedure fee {procedure fie {

procedure foe {}call foe()

}call fie()

}

Static Coord GeneratedCode

<3,8> loadAI rARP,8 => r10

<2,12> loadAI _display_ => r10loadAI r10,4 => r10loadAI r10,12 => r10

<1,16> loadI _display_ => r10load r10 => r10loadAI rARP,12 => r10

level1

level2

level 3

_display_:

FindingtheCorrectAR

COMP412,Fall2017 27

parameters

registersreturnvaluereturnaddresscaller’s ARP

addressability

localvariables

ARP

MaintainingADisplay• Displaymaintenanceissimple,aslongasthecompilerwriteriswillingto“waste”onedisplayslot• Inprologofeachleveljprocedure♦ Savethelevelj displayentryintotheprocedure’sownAR (addressabilityslot)

♦ StorecurrentARP intolevelj displayslot

• Inepilogofeachlevelj procedure♦ Restoreoldlevelj displayentryfromthecurrentAR’saddressabilityslot

Simple,clean,andconstanttime

AccessLinksversusDisplay

AccessLinks• Maintenanceaddsoverheadtoeachcall– Overheaddependsonnesting

• Addsoverheadoneachreferencetoalocalinsurroundingscope– Overheaddependsonnesting

• IfARsoutliveprocedure,accesslinksstillwork– AccesslinksbetweenARsensures

thattheyarelive(&uncollected)

COMP412,Fall2017 28

Display• Maintenanceaddsoverheadtoeachcall&return♦ Overheadisconstant

• Addsoverheadoneachreferencetoalocalinsurroundingscope♦ Overheadisconstant

• IfARsoutliveprocedure,displaylinksarenotpreserved♦ NeedtopreservetheminawaythatensuresARsarelive(collectorissue)

Yourmileagewillvary• Dependsonratioofnon-localaccessestocalls• Dependsondemandforregisters (availableregistertohold_display_?)

AccessLinksandDisplays

TheMetaIssue• Decisiontouseoneortheotherismadewhenthelinkage is designed– Maybebeyondthecontrolofthecompilerwriter

• Lexicallevelsforproceduresandvariablesaredeterminedinparsing– Requirestheuseofalexicallyscopedsymboltable

• Codetomaintainaccesslinkordisplayisemittedatcompiletime– Whencompilergeneratesaprocedureoracall,itemitsthecode– Accesslinks: Lexicallevelsareknownatcompiletime,socompilercan

generatethecodetofindalevelj display(knowshowfartorunupthechain)– Display:Lexicallevelsareknownatcompiletime,socompilercangenerate

thesave(inprolog)andrestore(inepilog)

• Eitheranaccesslinkoradisplayisaruntimeobject– Existsatruntime,– PointstoARs,whichareruntimeobjects

COMP412,Fall2017 29