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

Post on 28-May-2018

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

CompilerDesignSpring2017

4.0Semanticanalysis

Dr.Zoltán Majó

CompilerGroup– JavaHotSpot VirtualMachineOracleCorporation

1

Symboltable

§ Centralrepositoryofinformationaboutprogramsymbols

§ Mirrorsstructureofprogram

§ Lasttime:Onepossiblesetup

2

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

4

Implementation

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

§ Keepitsimpleandflexible§ Useiterators§ Useaccessfunctions§ Useinterfaces

§ …toallowmodificationslater

6

Foodforthought§ JavaLi issimple§ Featuresfoundinotherprogramminglanguagesthatare

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

§ Whichfeaturesinfluencesymboltable?§ Howcanthesefeaturesbehandled?

7

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

9

Representation

§ Differenttargetspecifications§ Implicittarget§ Explicittarget

§ Compilerslovesimplicity

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

§ Options1. Parsertransformsprogramastheparsetreeisconstructed2. ASTunifiesformat

§ Semanticanalysis:Lastphaseincompilerthatcandotransformationwithminimalimpactontherestofthesystem

11

4.4Checkingprogramproperties

§ Input:AST(orparsetree)

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

§ Processparsetree(usingsymboltable)§ Process:performchecks§ Oneclassatatime

§ Output:(modified)AST,errormessagesasappropriate

§ Examplesfollow

12

4.4.1Uniqueidentifiers

§ Checkasthesymboltableisconstructed§ Doesasymbolwiththecurrentnamealreadyexist?

13

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

16

Typeconversions

§ Simplerulesfromlanguagespecification

§ Expression§ Operator§ Source1,Source2

§ Exampleforoperator“+”

§ Languagespeclistsallowedconversions§ Insertconversionoperators

18

Source1 Source2 Result

int int int

int float float

float double double

Typeconversions(cont’d)

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

19

Op

VARint

VARfloat

+

tofloat

VARfloat

VARint

4.4.4Arrayaccesschecks

§ ConsideranarrayA,expressionexpr,andanaccesstoA[expr]§ exprmusthavecorrecttypeorwillbeconverted

§ MustmakesureA[expr]referstoexistingelement

0≤expr <A.length

§ Ingeneralthischeckcannotbedoneatcompiletime§ Needinformationaboutactualvalueofexpr§ Postponespecialcasesandoptimizationtolaterlectures

20

4.4.4.1Explicitchecks

§ RewriteASTtoincludeexplicitchecksA[i] =

21

=

Array …

VarA

Vari

COND<=

Const0

Vari

True

False

Exit()

22

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

24

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

4.4.4.2Delayedhandling

§ Divideworkintotwosteps

§ Step1:Markaccessesthatmustbechecked§ Donebysemanticanalyzer

§ Step2:Producecodethatchecksforbounds§ Donebythecodegenerator§ Cangodirectlytoassemblycode

§ Easytoavoidcheckingthesameexpression

27

30

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

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

Implementingnullchecks

§ AccessesoftheformA[…]mustbechecked§ Runtimecheckisrequired

§ Compilernotalwaysabletodeterminethatarrayisnon-null

§ InsertcodetocheckconditionA!=null§ Approachespresentedbeforeapply

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

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

34

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

36

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 …

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

39

4.5Checkingprogramproperties

§ Categories

§ P:canbecheckedafter(orduring)parsing

§ D:dynamiccheckneeded,codemustbeinserted§ Orhardwaresupportassistsindetection

§ U:undecidable

§ ?:Moreinformationisneeded§ Needtomodel“programexecution”

40

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)

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

top related