Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Virtual Machine : Semantics and Optimization

Similar presentations


Presentation on theme: "Java Virtual Machine : Semantics and Optimization"— Presentation transcript:

1 Java Virtual Machine : Semantics and Optimization
Moonjoo Kim 11/13/2018

2 Outline Java Virtual Machine Specification
Semantics of Java Bytecode ClassFile contents Thread states Semantics of instructions Performance Optimization Techniques Just-In-Time compiler Object Oriented Programming language Exception handling 11/13/2018

3 Semantics of Java Bytecode
Describe the semantics formally from informal specification verbal specification leads to ambiguities help understand the semantics clearly mathematical proof of certain properties of code can be feasible ex1. max height of operand stack of A.f() == 10 ex2. two bytecode sequences are equivalent in terms of stack operands and local variables 11/13/2018

4 Mathematical Description of JVM
ClassFile contents Thread states Semantics of instructions 11/13/2018

5 ClassFile Contents C=P(Accc) x [Idc] x P(Idc) x FD x CV x MD x MI x CP
P(Accc) : a set of access modifiers [Idc] : a direct super class (optional) P(Idc) : the names of direct super interfaces FD : field declarations CV : constant values MD : method declaration MI :method implementation 11/13/2018

6 ClassFile Contents (cont.)
FD = Idf -> (P(Accf) x Desc) Desc = Simple U Array U Idc Array = N x (Simple U Idc) MD = Sig -> (P(Accm)x[Desc] x P(Idc)) Sig = Idm x Desc * MI = Sig -> Impl Impl = N0 x N0 x Code x H Code – PC -> Instr H = PC -> P(Idc) -> PC 11/13/2018

7 Thread States TS = Frame* x Heap x Env
Frame = PC x Oper* x Locals x (Idc x Sig) Oper = W U DW W = Wv U PC Wv = Int U Float U Ref0 Ref0 = Ref U {null} DW = Long U Double Locals = N0 -> Oper 11/13/2018

8 Thread States (cont.) Heap = Ref -> Obj Env = Idc -> (C X SV)
Obj = Objv U Objc U Obja Obju = Idc X IV Objc = Idc X IV IV = (Idc X Idf ) -> V V = Wv U DW Obja = Array X Int X AV AV = Int -> V Env = Idc -> (C X SV) SV = Idf -> V 11/13/2018

9 Bytecode instruction Arithmetic operations Branching
Object instantiation Method invocation 11/13/2018

10 Arithmetic Operation Ex> integer addition instr(ts) = iadd
s=(k:int):: (k’:Int) :: sr succ(ts) = pc’ ts => ((pc’, (k +k’)::sr,l,m)::fr, h,e) 11/13/2018

11 Branching Ex> conditional jump instr(ts) = ifeq d s =(k:Int)::sr
jump(ts,d) = pc’ succ(ts) = pc’’ If( k = (0:Int), Pc’,pc’’) = pc’’ ts => ((pc’’’,sr,l,,)::fr, h,e) 11/13/2018

12 Object instantiation instr(ts) = new i pool(ts)(i) = idc’:Idc
e(idc’) = ((accc, idc’’,is,fd,cv,md,mi,cp),sv) accc { interface, abstract} =  access(idc’, accc,idc) fields(idc’,e) = iv (idc’,iv) = o:Obju r Ref\ dom(h) size(s) + size(r) <= maxs(ts) succ(ts) = pc’ ts => ((pc’,r::s, l,m) :: fr, h+{r->o},e) 11/13/2018

13 Method invocation instr(ts) = invokevirtual i
pool(ts)(i) = (idc’:Idc,sig’,d):Constm e(idc’) = ((accc, idc’’,is,fd,cv,md,mi,cp),sv) Interface  accc sig’ = (idm’,<d1,d2,d3,…dk>) Idm {<init> , <clinit>} sig’ dom(md) as = 1 <= j <=k. (initialized(aj,h)  compatVal(dj,aj,h,e)) h(r) = (idc’’’,iv):Objc Idc’  supers(idc’’’,e)_ lookup(sig’.idc’’’,e) = (idc’’’’,accm,d’, nl,code’) access(idc’’’’,idc,e) accm  {private,static, abstract,native} =  protected  accm => (idc’  supers(idc,e)  idc  supers(idc’’’,e)) d = d’ size(as) <= nl (minpc(code’), <>,args(as),(idc’’’’,sig’)) = f’:Frame ts => (f’:: (pc,sr,l,m)::fr, h,e) 11/13/2018

14 Performance Issues on Java
Object oriented program issues frequent method invocation dynamic method lookup Exception handling type inclusion testing run-type exception Thread synchronization 11/13/2018

15 Just-In-Time compiler
Techniques Object handle removal Method inlining Loop versioning Fast type inclusion testing Bytecode idiom tables Note. JIT uses traditional compiler optimization techniques, but only quick and fast analysis due to run-time compilation overhead Quick and easy heuristics are valued 11/13/2018

16 Remove object handle Fast access to object
Less information for garbage collection Smaller array index checking code 11/13/2018

17 Method compilation Given a threshhold x, if a method runs more than x (called hotspot), then compile the method When method has a loop with loop varaiable y, y is considered for the decision However, static compilation is difficult Dynamic class loading Method overriding 11/13/2018

18 Method compilation (cont.)
Virtual method inlining Reduce an overhead of frame allocation Encrease a chance of global optimization Heuristic: in most case only one method is invoked If(o->methodtable[mb->offset] == mb) { /* inlined code of mb */ } else { /* originalvirtual invocation code */ O.mb(a,b,c) } 11/13/2018

19 Loop versioning Remove array index check for loop Ex>
if((array != NULL) && (start >= 0) && (end <= array.length) { /*safe loop (eliminate all array index exception checks) */ For(I=start; I < end; I++) { Array[I] = array[I] + const; } }else { /*unsafe loop (original loop) */ 11/13/2018

20 Fast type inclusion test
Keep cache containing the success cases for reducing frequency of calling type checking Ex> Type to = (Type) from; if(from == NULL) then; else if (last_succ == class_of(from)) then; else if (call C function) then last_succ = class_of(from) else throw classcast_exception 11/13/2018

21 Idioms in bytecode sequence
Bytecode idioms dup/getfield/iload/iop2/putfiled Obj.field +=var dup2/iaload/iload/iop2/iastore Array[I] += var; Keep table containing machine codes for the idioms; save a time for stack analysis 11/13/2018

22 Other techniques Common Subexpression Elimination
Scalar replacement Partial redundancy elimination Run-time trace information For any conditional branches encountered, the interpreter keeps the information on whether the branch is taken or not The trace information is used by the JIT compiler for ordering basic blocks. This information also makes a difference in how a program is cut into tiles 11/13/2018

23 Performance Measurement
IBM JDK with JIT Compiler v 3.0 11/13/2018


Download ppt "Java Virtual Machine : Semantics and Optimization"

Similar presentations


Ads by Google