Theory JVMExecInstr
section ‹JVM Instruction Semantics›
theory JVMExecInstr
imports JVMInstructions JVMState "../Common/Exceptions"
begin
primrec
exec_instr :: "[instr, jvm_prog, heap, val list, val list,
cname, mname, pc, frame list] => jvm_state"
where
exec_instr_Load:
"exec_instr (Load n) P h stk loc C⇩0 M⇩0 pc frs =
(None, h, ((loc ! n) # stk, loc, C⇩0, M⇩0, pc+1)#frs)"
| "exec_instr (Store n) P h stk loc C⇩0 M⇩0 pc frs =
(None, h, (tl stk, loc[n:=hd stk], C⇩0, M⇩0, pc+1)#frs)"
| exec_instr_Push:
"exec_instr (Push v) P h stk loc C⇩0 M⇩0 pc frs =
(None, h, (v # stk, loc, C⇩0, M⇩0, pc+1)#frs)"
| exec_instr_New:
"exec_instr (New C) P h stk loc C⇩0 M⇩0 pc frs =
(case new_Addr h of
None ⇒ (Some (addr_of_sys_xcpt OutOfMemory), h, (stk, loc, C⇩0, M⇩0, pc)#frs)
| Some a ⇒ (None, h(a↦blank P C), (Addr a#stk, loc, C⇩0, M⇩0, pc+1)#frs))"
| "exec_instr (Getfield F C) P h stk loc C⇩0 M⇩0 pc frs =
(let v = hd stk;
xp' = if v=Null then ⌊addr_of_sys_xcpt NullPointer⌋ else None;
(D,fs) = the(h(the_Addr v))
in (xp', h, (the(fs(F,C))#(tl stk), loc, C⇩0, M⇩0, pc+1)#frs))"
| "exec_instr (Putfield F C) P h stk loc C⇩0 M⇩0 pc frs =
(let v = hd stk;
r = hd (tl stk);
xp' = if r=Null then ⌊addr_of_sys_xcpt NullPointer⌋ else None;
a = the_Addr r;
(D,fs) = the (h a);
h' = h(a ↦ (D, fs((F,C) ↦ v)))
in (xp', h', (tl (tl stk), loc, C⇩0, M⇩0, pc+1)#frs))"
| "exec_instr (Checkcast C) P h stk loc C⇩0 M⇩0 pc frs =
(let v = hd stk;
xp' = if ¬cast_ok P C h v then ⌊addr_of_sys_xcpt ClassCast⌋ else None
in (xp', h, (stk, loc, C⇩0, M⇩0, pc+1)#frs))"
| exec_instr_Invoke:
"exec_instr (Invoke M n) P h stk loc C⇩0 M⇩0 pc frs =
(let ps = take n stk;
r = stk!n;
xp' = if r=Null then ⌊addr_of_sys_xcpt NullPointer⌋ else None;
C = fst(the(h(the_Addr r)));
(D,M',Ts,mxs,mxl⇩0,ins,xt)= method P C M;
f' = ([],[r]@(rev ps)@(replicate mxl⇩0 undefined),D,M,0)
in (xp', h, f'#(stk, loc, C⇩0, M⇩0, pc)#frs))"
| "exec_instr Return P h stk⇩0 loc⇩0 C⇩0 M⇩0 pc frs =
(if frs=[] then (None, h, []) else
let v = hd stk⇩0;
(stk,loc,C,m,pc) = hd frs;
n = length (fst (snd (method P C⇩0 M⇩0)))
in (None, h, (v#(drop (n+1) stk),loc,C,m,pc+1)#tl frs))"
| "exec_instr Pop P h stk loc C⇩0 M⇩0 pc frs =
(None, h, (tl stk, loc, C⇩0, M⇩0, pc+1)#frs)"
| "exec_instr IAdd P h stk loc C⇩0 M⇩0 pc frs =
(let i⇩2 = the_Intg (hd stk);
i⇩1 = the_Intg (hd (tl stk))
in (None, h, (Intg (i⇩1+i⇩2)#(tl (tl stk)), loc, C⇩0, M⇩0, pc+1)#frs))"
| "exec_instr (IfFalse i) P h stk loc C⇩0 M⇩0 pc frs =
(let pc' = if hd stk = Bool False then nat(int pc+i) else pc+1
in (None, h, (tl stk, loc, C⇩0, M⇩0, pc')#frs))"
| "exec_instr CmpEq P h stk loc C⇩0 M⇩0 pc frs =
(let v⇩2 = hd stk;
v⇩1 = hd (tl stk)
in (None, h, (Bool (v⇩1=v⇩2) # tl (tl stk), loc, C⇩0, M⇩0, pc+1)#frs))"
| exec_instr_Goto:
"exec_instr (Goto i) P h stk loc C⇩0 M⇩0 pc frs =
(None, h, (stk, loc, C⇩0, M⇩0, nat(int pc+i))#frs)"
| "exec_instr Throw P h stk loc C⇩0 M⇩0 pc frs =
(let xp' = if hd stk = Null then ⌊addr_of_sys_xcpt NullPointer⌋ else ⌊the_Addr(hd stk)⌋
in (xp', h, (stk, loc, C⇩0, M⇩0, pc)#frs))"
lemma exec_instr_Store:
"exec_instr (Store n) P h (v#stk) loc C⇩0 M⇩0 pc frs =
(None, h, (stk, loc[n:=v], C⇩0, M⇩0, pc+1)#frs)"
by simp
lemma exec_instr_Getfield:
"exec_instr (Getfield F C) P h (v#stk) loc C⇩0 M⇩0 pc frs =
(let xp' = if v=Null then ⌊addr_of_sys_xcpt NullPointer⌋ else None;
(D,fs) = the(h(the_Addr v))
in (xp', h, (the(fs(F,C))#stk, loc, C⇩0, M⇩0, pc+1)#frs))"
by simp
lemma exec_instr_Putfield:
"exec_instr (Putfield F C) P h (v#r#stk) loc C⇩0 M⇩0 pc frs =
(let xp' = if r=Null then ⌊addr_of_sys_xcpt NullPointer⌋ else None;
a = the_Addr r;
(D,fs) = the (h a);
h' = h(a ↦ (D, fs((F,C) ↦ v)))
in (xp', h', (stk, loc, C⇩0, M⇩0, pc+1)#frs))"
by simp
lemma exec_instr_Checkcast:
"exec_instr (Checkcast C) P h (v#stk) loc C⇩0 M⇩0 pc frs =
(let xp' = if ¬cast_ok P C h v then ⌊addr_of_sys_xcpt ClassCast⌋ else None
in (xp', h, (v#stk, loc, C⇩0, M⇩0, pc+1)#frs))"
by simp
lemma exec_instr_Return:
"exec_instr Return P h (v#stk⇩0) loc⇩0 C⇩0 M⇩0 pc frs =
(if frs=[] then (None, h, []) else
let (stk,loc,C,m,pc) = hd frs;
n = length (fst (snd (method P C⇩0 M⇩0)))
in (None, h, (v#(drop (n+1) stk),loc,C,m,pc+1)#tl frs))"
by simp
lemma exec_instr_IPop:
"exec_instr Pop P h (v#stk) loc C⇩0 M⇩0 pc frs =
(None, h, (stk, loc, C⇩0, M⇩0, pc+1)#frs)"
by simp
lemma exec_instr_IAdd:
"exec_instr IAdd P h (Intg i⇩2 # Intg i⇩1 # stk) loc C⇩0 M⇩0 pc frs =
(None, h, (Intg (i⇩1+i⇩2)#stk, loc, C⇩0, M⇩0, pc+1)#frs)"
by simp
lemma exec_instr_IfFalse:
"exec_instr (IfFalse i) P h (v#stk) loc C⇩0 M⇩0 pc frs =
(let pc' = if v = Bool False then nat(int pc+i) else pc+1
in (None, h, (stk, loc, C⇩0, M⇩0, pc')#frs))"
by simp
lemma exec_instr_CmpEq:
"exec_instr CmpEq P h (v⇩2#v⇩1#stk) loc C⇩0 M⇩0 pc frs =
(None, h, (Bool (v⇩1=v⇩2) # stk, loc, C⇩0, M⇩0, pc+1)#frs)"
by simp
lemma exec_instr_Throw:
"exec_instr Throw P h (v#stk) loc C⇩0 M⇩0 pc frs =
(let xp' = if v = Null then ⌊addr_of_sys_xcpt NullPointer⌋ else ⌊the_Addr v⌋
in (xp', h, (v#stk, loc, C⇩0, M⇩0, pc)#frs))"
by simp
end