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