Theory TranslComp
theory TranslComp imports TranslCompTp begin
primrec compExpr :: "java_mb => expr => instr list"
and compExprs :: "java_mb => expr list => instr list"
where
"compExpr jmb (NewC c) = [New c]" |
"compExpr jmb (Cast c e) = compExpr jmb e @ [Checkcast c]" |
"compExpr jmb (Lit val) = [LitPush val]" |
"compExpr jmb (BinOp bo e1 e2) = compExpr jmb e1 @ compExpr jmb e2 @
(case bo of Eq => [Ifcmpeq 3,LitPush(Bool False),Goto 2,LitPush(Bool True)]
| Add => [IAdd])" |
"compExpr jmb (LAcc vn) = [Load (index jmb vn)]" |
"compExpr jmb (vn::=e) = compExpr jmb e @ [Dup , Store (index jmb vn)]" |
"compExpr jmb ( {cn}e..fn ) = compExpr jmb e @ [Getfield fn cn]" |
"compExpr jmb (FAss cn e1 fn e2 ) =
compExpr jmb e1 @ compExpr jmb e2 @ [Dup_x1 , Putfield fn cn]" |
"compExpr jmb (Call cn e1 mn X ps) =
compExpr jmb e1 @ compExprs jmb ps @ [Invoke cn mn X]" |
"compExprs jmb [] = []" |
"compExprs jmb (e#es) = compExpr jmb e @ compExprs jmb es"
primrec compStmt :: "java_mb => stmt => instr list" where
"compStmt jmb Skip = []" |
"compStmt jmb (Expr e) = ((compExpr jmb e) @ [Pop])" |
"compStmt jmb (c1;; c2) = ((compStmt jmb c1) @ (compStmt jmb c2))" |
"compStmt jmb (If(e) c1 Else c2) =
(let cnstf = LitPush (Bool False);
cnd = compExpr jmb e;
thn = compStmt jmb c1;
els = compStmt jmb c2;
test = Ifcmpeq (int(length thn +2));
thnex = Goto (int(length els +1))
in
[cnstf] @ cnd @ [test] @ thn @ [thnex] @ els)" |
"compStmt jmb (While(e) c) =
(let cnstf = LitPush (Bool False);
cnd = compExpr jmb e;
bdy = compStmt jmb c;
test = Ifcmpeq (int(length bdy +2));
loop = Goto (-(int((length bdy) + (length cnd) +2)))
in
[cnstf] @ cnd @ [test] @ bdy @ [loop])"
definition load_default_val :: "ty => instr" where
"load_default_val ty == LitPush (default_val ty)"
definition compInit :: "java_mb => (vname * ty) => instr list" where
"compInit jmb == λ (vn,ty). [load_default_val ty, Store (index jmb vn)]"
definition compInitLvars :: "[java_mb, (vname × ty) list] ⇒ bytecode" where
"compInitLvars jmb lvars == concat (map (compInit jmb) lvars)"
definition compMethod :: "java_mb prog ⇒ cname ⇒ java_mb mdecl ⇒ jvm_method mdecl" where
"compMethod G C jmdl == let (sig, rT, jmb) = jmdl;
(pns,lvars,blk,res) = jmb;
mt = (compTpMethod G C jmdl);
bc = compInitLvars jmb lvars @
compStmt jmb blk @ compExpr jmb res @
[Return]
in (sig, rT, max_ssize mt, length lvars, bc, [])"
definition compClass :: "java_mb prog => java_mb cdecl=> jvm_method cdecl" where
"compClass G == λ (C,cno,fdls,jmdls). (C,cno,fdls, map (compMethod G C) jmdls)"
definition comp :: "java_mb prog => jvm_prog" where
"comp G == map (compClass G) G"
end