src/HOL/MicroJava/Comp/TranslComp.thy
author wenzelm
Sat, 07 Apr 2012 16:41:59 +0200
changeset 47389 e8552cba702d
parent 39758 b8a53e3a0ee2
permissions -rw-r--r--
explicit checks stable_finished_theory/stable_command allow parallel asynchronous command transactions; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     1
(*  Title:      HOL/MicroJava/Comp/TranslComp.thy
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     2
    Author:     Martin Strecker
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     3
*)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     4
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     5
(* Compiling MicroJava into MicroJVM -- Translation functions *)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     6
16417
9bc16273c2d4 migrated theory headers to new format
haftmann
parents: 14981
diff changeset
     7
theory TranslComp imports  TranslCompTp begin
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     8
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
     9
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    10
(* parameter java_mb only serves to define function index later *)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    11
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    12
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    13
(**********************************************************************)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    14
(** code generation for expressions **)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    15
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    16
primrec compExpr :: "java_mb => expr => instr list"
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    17
  and compExprs :: "java_mb => expr list => instr list"
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    18
where
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    19
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    20
(*class instance creation*)
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    21
"compExpr jmb (NewC c) = [New c]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    22
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    23
(*type cast*)
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    24
"compExpr jmb (Cast c e) = compExpr jmb e @ [Checkcast c]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    25
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    26
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    27
(*literals*)
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    28
"compExpr jmb (Lit val) = [LitPush val]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    29
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    30
      
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    31
(* binary operation *)                         
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    32
"compExpr jmb (BinOp bo e1 e2) = compExpr jmb e1 @ compExpr jmb e2 @ 
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    33
  (case bo of Eq => [Ifcmpeq 3,LitPush(Bool False),Goto 2,LitPush(Bool True)]
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    34
            | Add => [IAdd])" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    35
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    36
(*local variable*)
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    37
"compExpr jmb (LAcc vn) = [Load (index jmb vn)]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    38
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    39
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    40
(*assignement*)
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    41
"compExpr jmb (vn::=e) = compExpr jmb e @ [Dup , Store (index jmb vn)]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    42
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    43
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    44
(*field access*)
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    45
"compExpr jmb ( {cn}e..fn ) = compExpr jmb e @ [Getfield fn cn]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    46
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    47
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    48
(*field assignement - expected syntax:  {_}_.._:=_ *)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    49
"compExpr jmb (FAss cn e1 fn e2 ) = 
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    50
       compExpr jmb e1 @ compExpr jmb e2 @ [Dup_x1 , Putfield fn cn]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    51
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    52
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    53
(*method call*)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    54
"compExpr jmb (Call cn e1 mn X ps) = 
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    55
        compExpr jmb e1 @ compExprs jmb ps @ [Invoke cn mn X]" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    56
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    57
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    58
(** code generation expression lists **)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    59
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    60
"compExprs jmb []     = []" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    61
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    62
"compExprs jmb (e#es) = compExpr jmb e @ compExprs jmb es"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    63
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    64
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    65
primrec compStmt :: "java_mb => stmt => instr list" where
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    66
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    67
(** code generation for statements **)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    68
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    69
"compStmt jmb Skip = []" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    70
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    71
"compStmt jmb (Expr e) = ((compExpr jmb e) @ [Pop])" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    72
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    73
"compStmt jmb (c1;; c2) = ((compStmt jmb c1) @ (compStmt jmb c2))" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    74
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    75
"compStmt jmb (If(e) c1 Else c2) =
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    76
        (let cnstf = LitPush (Bool False);
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    77
             cnd   = compExpr jmb e;
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    78
             thn   = compStmt jmb c1;
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    79
             els   = compStmt jmb c2;
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    80
             test  = Ifcmpeq (int(length thn +2)); 
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    81
             thnex = Goto (int(length els +1))
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    82
         in
39758
b8a53e3a0ee2 modernized primrecs
haftmann
parents: 35416
diff changeset
    83
         [cnstf] @ cnd @ [test] @ thn @ [thnex] @ els)" |
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    84
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    85
"compStmt jmb (While(e) c) =
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    86
        (let cnstf = LitPush (Bool False);
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    87
             cnd   = compExpr jmb e;
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    88
             bdy   = compStmt jmb c;
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    89
             test  = Ifcmpeq (int(length bdy +2)); 
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    90
             loop  = Goto (-(int((length bdy) + (length cnd) +2)))
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    91
         in
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 16417
diff changeset
    92
         [cnstf] @ cnd @ [test] @ bdy @ [loop])"
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    93
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    94
(**********************************************************************)
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    95
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    96
(*compiling methods, classes and programs*) 
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    97
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
    98
(*initialising a single variable*)
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32960
diff changeset
    99
definition load_default_val :: "ty => instr" where
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   100
"load_default_val ty == LitPush (default_val ty)"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   101
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32960
diff changeset
   102
definition compInit :: "java_mb => (vname * ty) => instr list" where
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   103
"compInit jmb == \<lambda> (vn,ty). [load_default_val ty, Store (index jmb vn)]"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   104
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32960
diff changeset
   105
definition compInitLvars :: "[java_mb, (vname \<times> ty) list] \<Rightarrow> bytecode" where
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   106
 "compInitLvars jmb lvars == concat (map (compInit jmb) lvars)"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   107
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32960
diff changeset
   108
definition compMethod :: "java_mb prog \<Rightarrow> cname \<Rightarrow> java_mb mdecl \<Rightarrow> jvm_method mdecl" where
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   109
  "compMethod G C jmdl == let (sig, rT, jmb) = jmdl;
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   110
                        (pns,lvars,blk,res) = jmb;
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   111
                        mt = (compTpMethod G C jmdl);
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   112
                        bc = compInitLvars jmb lvars @ 
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   113
                             compStmt jmb blk @ compExpr jmb res @
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   114
                             [Return]
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   115
                  in (sig, rT, max_ssize mt, length lvars, bc, [])"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   116
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32960
diff changeset
   117
definition compClass :: "java_mb prog => java_mb cdecl=> jvm_method cdecl" where
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   118
  "compClass G == \<lambda> (C,cno,fdls,jmdls). (C,cno,fdls, map (compMethod G C) jmdls)"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   119
35416
d8d7d1b785af replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
haftmann
parents: 32960
diff changeset
   120
definition comp :: "java_mb prog => jvm_prog" where
13673
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   121
  "comp G == map (compClass G) G"
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   122
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   123
end
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   124
2950128b8206 First checkin of compiler
streckem
parents:
diff changeset
   125