Top Banner
Compiler Design Spring 2017 7.0 Code generation Dr. Zoltán Majó Compiler Group – Java HotSpot Virtual Machine Oracle Corporation 46
47

Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Jun 04, 2018

Download

Documents

trinhbao
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

CompilerDesignSpring2017

7.0Codegeneration

Dr.Zoltán Majó

CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation

46

Page 2: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Outline

§ 7.1Accesstooperands

§ 7.2Assignmentstatement

§ 7.3Conditionalstatement

§ 7.4Loops

§ 7.5Methodinvocation

47

Page 3: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

48

A B

B

Page 4: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Extendingthecodegenerator

§ NewIRnodes

§ ARRAY§ (Index inframework)§ Specifiesarrayelement§ Requiresnameofarray…§ ...andtreeforindexexpression

§ FIELD

§ Couldincludesubtree tocomputeaddress§ Asdiscussedearlier

49

Page 5: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Otherrestrictions

§ Didnothandlestatementsthatrequiredmoreregistersthanavailable§ Virtualregisterspostponedealingwiththisaspect

§ CouldbreakatreethatrequiresmorethanRregistersintoseparatetrees§ Introducetemporaryvariablestoholdintermediateresults

§ Recommendedoption:Handleregistershortageonthefly§ Needaregister?Freearegister§ Saveregistercontentsontostack§ Exampleonnextslide

50

Page 6: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

51

Page 7: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Outline

§ 7.1Accesstooperands

§ 7.2Assignmentstatement

§ 7.3Conditionalstatement

§ 7.4Loops

§ 7.5Methodinvocation

53

Page 8: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

7.3Conditionalstatement

JavaLi allowstwokindsofconditionalstatements

1. “if-then”if (expression) { <body> }

2. “if-then-else”if (expression) { <body> }

else { <body> }

54

Page 9: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

1. “if-then”if (expression) { <then-part> }InvokecodegeneratoronexpressionCodetodecideifthen-partisexecutedInvokecodegeneratoronthen-part

2. “if-then-else”if (expression) { <then-part> }

else { <else-part> }InvokecodegeneratoronexpressionCodetodecideifthen-partisexecutedInvokecodegeneratoronthen-partCodetoskiparoundelse-partInvokecodegeneratoronelse-part

55

Page 10: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Codeoutline

57

Eval expression

Test

Branch

dThen-part

…continue

Eval expression

Test

Branch

Then-part

…continue

Else-part

d

d

Uncond.branch

Page 11: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

58

Page 12: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Outline

§ 7.1Accesstooperands

§ 7.2Assignmentstatement

§ 7.3Conditionalstatement

§ 7.4Loops

§ 7.5Methodinvocation

59

Page 13: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

7.4Loops

§ JavaLi hasonlyoneloopingconstructwhile (expression) {

<loop-body>}

§ Cantranslateallotherkindsofloopingconstructsintowhile-loops

60

Page 14: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

§ Translationsimilartoconditionalstatement§ Samerulesforexpression

while-loopwhile (expression) { <loop-body> }

InvokecodegeneratoronexpressionCodetodecideifloop-bodyisexecutedInvokecodegeneratoronloop-bodyCodetojumpbacktostartofloop

61

Page 15: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Codeoutline

62

Eval expression

Test

Branch

Body

…continue

d

Uncond.branch

Page 16: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

63

Page 17: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

§ Expressionmustbeevaluatedforeveryiteration

§ Simplescheme

§ Straightforwardtoimplement§ Closetoconditionalstatement

§ Oneunconditionalbranchforeachiteration

64

Page 18: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Codeoutline2

§ Idea:eliminateunconditionalbranchattheendofeachloopbody

§ Moveloopcontinuationtesttotheendoftheloop

65

Page 19: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Codeoutline2

66

Eval expression

Test

Branch

Body

…continue

d

Uncond.branch

Page 20: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

§ Possibleperformanceimprovement§ Reducednumberofunconditionalbranches§ Basedontheguessthattheloopbodyisexecuted§ Processorimplementationmayprefetch /pre-executeunconditional

branches

§ Otheroptionsexist

67

Page 21: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

68

Page 22: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Otherloopconstructs

Takeasheetofpaperandsketchhowyoucouldtranslatea“for”loop

for (int i = low; i < high; i++) {<body>

}

Showafigurethatoutlinesthecode(similartoslidesshownduringthelecture)

69

Page 23: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Codeoutline

70

Eval expression

Test

Branch

Body

…continue

d

Uncond.branch

Init loopcounter

Updateloopcounter

Page 24: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

71

Page 25: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Outline

§ 7.1Accesstooperands

§ 7.2Assignmentstatement

§ 7.3Conditionalstatement

§ 7.4Loops

§ 7.5Methodinvocation

72

Page 26: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

7.5Methodinvocation

§ Findingthecorrectbodyofcode§ Coveredearlier

§ Storageformethod-localvariables

§ Passingofparameters(arguments)

§ Handlingthereturnvalue

73

Page 27: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Terminology

Considerasimpleclasswithonemethod:class B {

int bar(int x1, int x2, ..., int xn) { … }}// somewhere in some method fooB bref; int val = bref.bar(y1,y2, …, yn);

74

callee

target(instance)receiver(y0– firstactual)

argumentsactualparametersactuals

callsite

caller

Page 28: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

75

Page 29: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Callingconvention

controlflow

infoo

continue

76

controlflow

inbar

return

Page 30: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Callingconvention

§ Thecallingconventiondecideswhichsteps/actionsofmethodinvocation/returnare§ Donebythecaller§ Donebythecallee

§ …andhowanydatatransferisimplemented§ Wherethedataarestored§ Dataincludesreturnaddressandreturnvalue§ Mayhavetoincludetypecasts

§ Usuallydefinedforallcompilers/softwaredevelopersonagivenoperatingsystem§ Ensuresinteroperability

77

Page 31: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Callingconvention

§ Stackprincipaldatastructure§ “Controlstack”

§ Setupbyrun-timesystem§ Supportedbyinstructionsand/orresources(registers)onsome

systems

§ Activationrecord keepsinformationonmethodinvocationsthatarecurrently“active”(or“live”)§ Alsoknownasframe

78

Page 32: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Actions

Considerthismethodinvocation.// in some method fooB bref; int val = bref.bar(y1,y2, …, yn);

Listallthestepsthatarenecessarytoimplementthecallanditsreturn(onasheetofpaper).

79

Page 33: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

80

Page 34: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Actions (relatedtomethodinvocation)

§ Identifytarget

§ Identifystartingaddressofcallee

§ Handleparameters§ Evaluateyi

§ Castasnecessary§ Putintolocationwhereitcanbefound

§ Pushontostack§ Placeintoregister§ Leavesomewhereelse

§ Saveregisters

§ Findspacefortemporaries

81

Page 35: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Actions (relatedtomethodinvocation)

§ Determineandstorereturnaddress

§ Findspaceforreturnvalue

§ Setupactivationrecordforcallee§ Linktoactivationrecordofcaller(“controllink”)

§ Transfercontroltocallee

82

Page 36: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Callling convention

§ Someofthesesteps§ Mustbedonebycaller§ Mustbedonebycallee

§ Methodthatwasinvoked§ Canbedonebyeitherone

83

Page 37: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Actions (relatedtomethodreturn)

§ Transfercontroltocaller/returnaddress

§ Deliverreturnvalue

§ Removeactivationrecordforcallee§ Reclaimanytemporarystorage§ Switchtoactivationrecordofcaller

84

Page 38: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Simpleactivationrecord

85

Temp k

...

Temp0

Local m

Local0

oldSP

oldFP

Returnaddress

target(parametery0)

parametery1

parameteryn

Returnvalue

framepointer

stackpointer

Page 39: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

86

Page 40: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Comments

§ SufficientforJavaLi§ Willnotworkfor“varargs”

§ Methodswithavariablenumberofarguments(actuals)

§ Parametersarefoundonthestack§ Actuals:positiveoffsetfromframepointer§ Offsetrecordedinsymboltable

§ Localsarefoundonthestack§ Negativeoffsetfromframepointer§ Offsetrecordedinsymboltable

§ Temporaryregioncangroww/olimit§ Temporariescanbeaccessedviaframepointer

87

Page 41: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Symboltable

§ Mustcompletesymboltablepriortocodegeneration

88

local0 - 4

local1 - 8

parameter1 +16

parameter 2 +20

return value +24

this (implicitparameter) + 12

Page 42: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Parameterpassing

§ (Recall)JavaLi passesallargumentsbyvalue

§ Nottheaddressofavariableispassedbutitsvalue

§ Thevalueofareferencecanbeusedtoread/writetheobjectthereferencerefersto§ Butthereferencecannotbechanged

§ Atcallsite(ofbref.bar(y1,y2, …, yn))§ Evaluateyn,pushontostack

§ Evaluateyn-1,yn-2,…y1,y0§ Couldbeanexpression,i.e.bref.bar(y1, a × b, …, yn)

§ Mayrequirecast

89

Page 43: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Actual-formalcorrespondence

§ Otherlanguagesarebasedon(orinclude)differentmodels§ Languagespecshouldspecifymodel

§ Herewelookatthecompiler’sside7.5.1Call-by-value7.5.2Call-by-reference7.5.3Call-by-result7.5.4Call-by-name

90

Page 44: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

7.5.1Call-by-value

§ TheonlyoptioninJava(andJavaLi)

§ Easytounderstand§ Lookatmethodinisolation§ Withreferencesriskofaliasing

§ Easytoimplement§ Callerknowshowtoevaluateactuals§ Callee usesaparameterlikealocal– initializedbycaller

§ Potentiallyinefficient§ Passanarray/anobjectbyvalue?§ Mayrequirecopy(timeconsuming)§ Languagerestrictscall-by-valueto“word-size”entities(ints,references)

91

Page 45: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

92

Page 46: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Adminissues

§ Norecitationsessiontoday

§ Easterbreakstartssoon§ Usefreetime(ifany)toworkonHomework3(dueonApril27)§ Andonoptionalparsing-relatedhomework(HWPRS)

§ Outtoday

§ Nextrecitation:April27at10:15inCABG61§ DiscussionofHomework4(completecodegenerator)

§ Willsendoutreminder

93

Page 47: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w08_02-code... · Compiler Design Spring 2017 7.0 Code generation Dr. ZoltánMajó Compiler Group –Java HotSpotVirtual

Wherearewenow?

§ Pastfirsthalfofthelecture§ Covered8weeksofmaterial

§ Lexing§ Parsing§ Semanticanalysis§ Codegeneration§ SomeVM/runtime-relatedissues

§ Intheremaining6weekswe’llcover§ Programanalysis§ Registerallocation§ OtherIRs/JITcompilation(iftimepermits)

§ Nextlecture:May4at10:15inCABG61

94