compiler design - eth zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... ·...

34
Compiler Design Spring 2017 4.0 Semantic analysis Dr. Zoltán Majó Compiler Group – Java HotSpot Virtual Machine Oracle Corporation 1

Upload: phamdat

Post on 28-May-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

CompilerDesignSpring2017

4.0Semanticanalysis

Dr.Zoltán Majó

CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation

1

Page 2: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Symboltable

§ Centralrepositoryofinformationaboutprogramsymbols

§ Mirrorsstructureofprogram

§ Lasttime:Onepossiblesetup

2

Page 3: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Onepossiblesetup

3

SymboltableclassA

SymboltableclassB

Name Type

a int

n int

foo int func

Name Type

a int

j int

bar voidfunc

Name Type

GlobalSymbolTable

A class

B class

true booleanconstant

Name Type

SymboltablemethodA::foo

Name Type

SymboltablemethodB::bar

k

n

int

int

Page 4: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4

Page 5: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Implementation

§ Thereexistmanyoptions§ Map§ Hashtable,tree,linkedlist§ Arrays

§ Keepitsimpleandflexible§ Useiterators§ Useaccessfunctions§ Useinterfaces

§ …toallowmodificationslater

6

Page 6: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Foodforthought§ JavaLi issimple§ Featuresfoundinotherprogramminglanguagesthatare

absentfromJavaLi§ private§ protected§ package§ synchronized§ final§ register§ volatile

§ Whichfeaturesinfluencesymboltable?§ Howcanthesefeaturesbehandled?

7

Page 7: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.3“this”reference

§ Considerthisexample:class X {

void f () {g(); // implicit target: this

}void g () { … }

}class Y {

X xref;void h () {

xref.g(); // explicit target: xref}

}

8

Page 8: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

9

Page 9: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Representation

§ Differenttargetspecifications§ Implicittarget§ Explicittarget

§ Compilerslovesimplicity

§ Recommendation:Allinvocationsitessamecanonicalformat§ E.g.<target>.<method_name>(param_list)

§ Options1. Parsertransformsprogramastheparsetreeisconstructed2. ASTunifiesformat

§ Semanticanalysis:Lastphaseincompilerthatcandotransformationwithminimalimpactontherestofthesystem

11

Page 10: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4Checkingprogramproperties

§ Input:AST(orparsetree)

§ Buildthesymboltable§ Checkpropertiesaspartofbuildingsymboltable§ E.g.,fieldsuniquelydefined§ E.g.,variablesuniquelydefined

§ Processparsetree(usingsymboltable)§ Process:performchecks§ Oneclassatatime

§ Output:(modified)AST,errormessagesasappropriate

§ Examplesfollow

12

Page 11: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.1Uniqueidentifiers

§ Checkasthesymboltableisconstructed§ Doesasymbolwiththecurrentnamealreadyexist?

13

Page 12: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.2Undeclaredfields,…

§ VisitallnodesoftheAST§ VARnode§ CALLnode§ Producesymbolname

§ Checkforsymbolname:Canwefindnameinsymboltable?§ Check:Fromlocaltoglobal

§ Insidefunction/method§ Fieldsofclass§ Maybe(global)constant§ Firsthit:defined&typeresolved§ Nohit:undefined,error

§ Undeclaredvariablesormethods:Sameapproach14

Page 13: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

16

Page 14: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Typeconversions

§ Simplerulesfromlanguagespecification

§ Expression§ Operator§ Source1,Source2

§ Exampleforoperator“+”

§ Languagespeclistsallowedconversions§ Insertconversionoperators

18

Source1 Source2 Result

int int int

int float float

float double double

Page 15: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Typeconversions(cont’d)

§ Insertoperators intoAST§ intà float§ floatà double§ byteà int§ intà long§ …

19

Op

VARint

VARfloat

+

tofloat

VARfloat

VARint

Page 16: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4Arrayaccesschecks

§ ConsideranarrayA,expressionexpr,andanaccesstoA[expr]§ exprmusthavecorrecttypeorwillbeconverted

§ MustmakesureA[expr]referstoexistingelement

0≤expr <A.length

§ Ingeneralthischeckcannotbedoneatcompiletime§ Needinformationaboutactualvalueofexpr§ Postponespecialcasesandoptimizationtolaterlectures

20

Page 17: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4.1Explicitchecks

§ RewriteASTtoincludeexplicitchecksA[i] =

21

=

Array …

VarA

Vari

COND<=

Const0

Vari

True

False

Exit()

Page 18: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

22

Page 19: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4.1Explicitchecks

§ Completepictureif (i < 0) exit()else if (i ≥ A.length) exit()else A[i] = …

§ ManyIRnodesadded§ FineifA[i]ontheleft-handside§ ProblematicifA[i]

§ Appearsmultipletimesinanexpression§ Iftherearemultiplearrayelementsinanexpression§ IfA[i]isanactualparameter§ IfA[i]isareturnvalue

23

Page 20: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

24

Page 21: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4.1Explicitchecks(cont’d)

§ Inserttemporaries…A[i] = A[k] + A[j]

§ …andthenproceedasshownearliertemp1 = A[k];

temp2 = A[j];

if ((i<0)|(i ≥ A.length)) exit()

else A[i] = temp1 + temp2

§ Betterbutstillcumbersome

25

Page 22: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4.2Delayedhandling

§ Divideworkintotwosteps

§ Step1:Markaccessesthatmustbechecked§ Donebysemanticanalyzer

§ Step2:Producecodethatchecksforbounds§ Donebythecodegenerator§ Cangodirectlytoassemblycode

§ Easytoavoidcheckingthesameexpression

27

Page 23: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

30

Page 24: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4.2Nullchecks

§ ConsiderthefollowingJavacodeint a[] = new int[3];int i = -1;System.out.println(a[i]);

§ Whenexecuted:ExceptionthrownduetofailedrangecheckException in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1

32

Page 25: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.4.4.2Nullchecks

§ ConsiderthefollowingJavacodeint a[] = null; // instead of new int[3]int i = -1;System.out.println(a[i]);

§ Whenexecuted:ExceptionthrownfailednullcheckException in thread "main" java.lang.NullPointerException

33

Page 26: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Implementingnullchecks

§ AccessesoftheformA[…]mustbechecked§ Runtimecheckisrequired

§ Compilernotalwaysabletodeterminethatarrayisnon-null

§ InsertcodetocheckconditionA!=null§ Approachespresentedbeforeapply

§ Option1:Explicitchecks(re-writeAST)§ Option2:Delayedhandling

§ Markaccessesthatmustbechecked§ Leavetheresttothecodegenerator§ Exploitinghardwaresupport:Implicitnullchecks§ Advantage:Lessoverhead

34

Page 27: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

ImplicitnullchecksintheHotSpot JavaVM

…; block B30 [6, 12]0xe6afb1a0: mov 0x18(%eax),%edx ;*getfield classLoader {reexecute=0 rethrow=0 return_oop=0}

; - java.lang.Class::getClassLoader0@1 (line 813); - java.lang.ClassLoader::getClassLoader@7 (line 1882); - java.lang.ClassLoader::checkClassLoaderPermission@9 (line

1894); - java.lang.Class::getClassLoader@23 (line 807); - java.lang.Class::desiredAssertionStatus@1 (line 3448); implicit exception: dispatches to 0xe6afb4a0

…;; ImplicitNullCheckStub slow case0xe6afb4a0: call 0xe6751d00 ; ImmutableOopMap{[68]=Oop [64]=Oop ecx=Oop }

;*invokevirtual getClassLoader0 {reexecute=0 rethrow=0 return_oop=0} ; - java.lang.ClassLoader::getClassLoader@7 (line 1882)

; - java.lang.ClassLoader::checkClassLoaderPermission@9 (line1894)

; - java.lang.Class::getClassLoader@23 (line 807); - java.lang.Class::desiredAssertionStatus@1 (line 3448); {runtime_call throw_null_pointer_exception Runtime1 stub}

35

Page 28: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

36

Page 29: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Implicitnullchecks

§ Instruction: mov 0x18(%eax),%edx

§ Thecompilerknows:§ %eax isaJavaobjectornull§ Javaobjectsareallocatedintheheap§ Theheapstartsatsomehigh virtualaddress

§ E.g.,0x0000000351000000 = 13GB

§ If%eax isnull,generatedcodewillaccessalow virtualaddress§ Incurrentcase:0x0+0x18§ Ingeneral:0x0+smalloffset(language/VMsupportslimitednumberoffields)

§ Memorylayout

37

Lowregion(nullaccesses) … Heap …

Page 30: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Implicitnullchecks

§ Idea:Protect“lowregion”usingprocessor/OSmemoryprotectionmechanism§ E.g.,implementedbymprotect()onUNIX-likesystems

§ Nullaccesstriggersprotectionerror§ E.g.,SIGSEGV onUnix-likesystems§ SignalcaughtbyVM(runtimesystem)and“transformed”intoanexception

; block B30 [6, 12]0xe6afb1a0: mov 0x18(%eax),%edx ; implicit exception: dispatches to 0xe6afb4a0…;; ImplicitNullCheckStub slow case0xe6afb4a0: call 0xe6751d00 ; {runtime_call throw_null_pointer_exception Runtime1 stub}

§ Fornon-nullaccesses:Onlyfieldaccessisperformed§ Noneedforcompare+jump§ Hardware-basedprotectionmechanismhasloweroverhead§ Closecouplingbetweencompilerandruntimesystem

§ Protectionenabledbyruntime,meta-informationrecordedbycompilerimportant 38

Page 31: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

39

Page 32: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

4.5Checkingprogramproperties

§ Categories

§ P:canbecheckedafter(orduring)parsing

§ D:dynamiccheckneeded,codemustbeinserted§ Orhardwaresupportassistsindetection

§ U:undecidable

§ ?:Moreinformationisneeded§ Needtomodel“programexecution”

40

Page 33: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Issues/properties(fromearlierlecture)

§ Allvariables/methods/symbolsdefined– P§ Allvariablesinitializedbeforeuse– ?§ Programhasnosyntaxerrors– P§ Definedprogramstart(mainmethodifappropriate)– P§ Exceptionsarecaught – ?§ Nonull-pointeraccesses– D§ Noout-of-boundaccessesforarrays,records/instances– D§ Programtypechecks– P§ Nounusedvariables/fields– P§ Returnstatementisreachable– ?§ Executionreachesendofprogram– ?§ Programterminates– U

42

P:canbecheckedafter(orduring)parsingD:dynamiccheckneededU:undecidable?:moreinformationisneeded

Note:Checkingifallvariablesinitializedbeforeuseneedsmoreinformation.(Slidecorrectedafterlecture– zmajo)

Page 34: Compiler Design - ETH Zpeople.inf.ethz.ch/zmajo/teaching/cd_ss17/slides/w06_01-semantic... · Compiler Design Spring 2017 4.0 Semantic analysis Dr. ZoltánMajó Compiler Group –Java

Issues/Properties(fromearlierlecture)– cont’d

§ Allfunctionsreturnvalue– P§ Rulesofthemodulesystemfollowed– P

§ Noaccesstoprivate/protectedmembersunlesslegal§ Nocycleininheritancegraph– P§ Interfaces/abstractfunctionsimplemented– P§ Loopsterminate– U§ Programexecuteswithinagiventimebudget– ?/U§ Programstayswithinpowerbudget – ?/U

44

P:canbecheckedafter(orduring)parsingD:dynamiccheckneededU:undecidable?:moreinformationisneeded