12442
|
1 |
(* Title: HOL/MicroJava/JVM/JVMListExample.thy
|
|
2 |
ID: $Id$
|
|
3 |
Author: Stefan Berghofer
|
|
4 |
*)
|
|
5 |
|
12911
|
6 |
header {* \isaheader{Example for generating executable code from JVM semantics} *}
|
12442
|
7 |
|
12951
|
8 |
theory JVMListExample = SystemClasses + JVMExec:
|
12442
|
9 |
|
|
10 |
consts
|
12951
|
11 |
list_nam :: cnam
|
|
12 |
test_nam :: cnam
|
12442
|
13 |
append_name :: mname
|
|
14 |
makelist_name :: mname
|
|
15 |
val_nam :: vnam
|
|
16 |
next_nam :: vnam
|
12951
|
17 |
test_name_loc :: loc_
|
12442
|
18 |
|
|
19 |
constdefs
|
12951
|
20 |
list_name :: cname
|
|
21 |
"list_name == Cname list_nam"
|
|
22 |
|
|
23 |
test_name :: cname
|
|
24 |
"test_name == Cname test_nam"
|
|
25 |
|
12442
|
26 |
val_name :: vname
|
|
27 |
"val_name == VName val_nam"
|
|
28 |
|
|
29 |
next_name :: vname
|
|
30 |
"next_name == VName next_nam"
|
|
31 |
|
12951
|
32 |
append_ins :: bytecode
|
|
33 |
"append_ins ==
|
12442
|
34 |
[Load 0,
|
|
35 |
Getfield next_name list_name,
|
|
36 |
Dup,
|
|
37 |
LitPush Null,
|
|
38 |
Ifcmpeq 4,
|
12951
|
39 |
Load 1,
|
|
40 |
Invoke list_name append_name [Class list_name],
|
12442
|
41 |
Return,
|
|
42 |
Pop,
|
|
43 |
Load 0,
|
|
44 |
Load 1,
|
|
45 |
Putfield next_name list_name,
|
|
46 |
LitPush Unit,
|
12951
|
47 |
Return]"
|
12442
|
48 |
|
12951
|
49 |
list_class :: "jvm_method class"
|
|
50 |
"list_class ==
|
|
51 |
(Object,
|
|
52 |
[(val_name, PrimT Integer), (next_name, Class list_name)],
|
|
53 |
[((append_name, [Class list_name]), PrimT Void,
|
|
54 |
(3, 0, append_ins,[(1,2,8,Xcpt NullPointer)]))])"
|
|
55 |
|
|
56 |
make_list_ins :: bytecode
|
|
57 |
"make_list_ins ==
|
12442
|
58 |
[New list_name,
|
|
59 |
Dup,
|
|
60 |
Store 0,
|
|
61 |
LitPush (Intg 1),
|
|
62 |
Putfield val_name list_name,
|
|
63 |
New list_name,
|
|
64 |
Dup,
|
|
65 |
Store 1,
|
|
66 |
LitPush (Intg 2),
|
|
67 |
Putfield val_name list_name,
|
|
68 |
New list_name,
|
|
69 |
Dup,
|
|
70 |
Store 2,
|
|
71 |
LitPush (Intg 3),
|
|
72 |
Putfield val_name list_name,
|
|
73 |
Load 0,
|
|
74 |
Load 1,
|
12951
|
75 |
Invoke list_name append_name [Class list_name],
|
12442
|
76 |
Load 0,
|
|
77 |
Load 2,
|
12951
|
78 |
Invoke list_name append_name [Class list_name],
|
12442
|
79 |
LitPush Unit,
|
12951
|
80 |
Return]"
|
|
81 |
|
|
82 |
test_class :: "jvm_method class"
|
|
83 |
"test_class ==
|
|
84 |
(Object, [],
|
|
85 |
[((makelist_name, []), PrimT Void, (3, 2, make_list_ins,[]))])"
|
12442
|
86 |
|
12951
|
87 |
E :: jvm_prog
|
|
88 |
"E == SystemClasses @ [(list_name, list_class), (test_name, test_class)]"
|
|
89 |
|
|
90 |
start_heap :: aheap
|
|
91 |
"start_heap == empty (XcptRef NullPointer \<mapsto> (Xcpt NullPointer, empty))
|
|
92 |
(XcptRef ClassCast \<mapsto> (Xcpt ClassCast, empty))
|
|
93 |
(XcptRef OutOfMemory \<mapsto> (Xcpt OutOfMemory, empty))
|
|
94 |
(Loc test_name_loc \<mapsto> (test_name, empty))"
|
12442
|
95 |
|
|
96 |
start_state :: jvm_state
|
|
97 |
"start_state ==
|
12951
|
98 |
(None, start_heap, [([], [Addr (Loc test_name_loc), arbitrary, arbitrary], test_name, (makelist_name, []), 0)])"
|
12442
|
99 |
|
|
100 |
types_code
|
12951
|
101 |
cnam ("string")
|
12442
|
102 |
vnam ("string")
|
|
103 |
mname ("string")
|
12522
|
104 |
loc_ ("int")
|
12442
|
105 |
|
|
106 |
consts_code
|
12522
|
107 |
"new_Addr" ("new'_addr {* %x. case x of None => True | Some y => False *}/ {* None *}/ {* Loc *}")
|
12442
|
108 |
|
|
109 |
"wf" ("true?")
|
|
110 |
|
|
111 |
"arbitrary" ("(raise ERROR)")
|
|
112 |
"arbitrary" :: "val" ("{* Unit *}")
|
12951
|
113 |
"arbitrary" :: "cname" ("Object")
|
12442
|
114 |
|
12951
|
115 |
"test_name_loc" ("0")
|
|
116 |
|
|
117 |
"list_nam" ("\"list\"")
|
|
118 |
"test_nam" ("\"test\"")
|
12442
|
119 |
"append_name" ("\"append\"")
|
|
120 |
"makelist_name" ("\"makelist\"")
|
|
121 |
"val_nam" ("\"val\"")
|
|
122 |
"next_nam" ("\"next\"")
|
|
123 |
|
|
124 |
ML {*
|
12522
|
125 |
fun new_addr p none loc hp =
|
|
126 |
let fun nr i = if p (hp (loc i)) then (loc i, none) else nr (i+1);
|
12442
|
127 |
in nr 0 end;
|
|
128 |
*}
|
|
129 |
|
|
130 |
subsection {* Single step execution *}
|
|
131 |
|
12951
|
132 |
generate_code
|
|
133 |
test = "exec (E, start_state)"
|
12442
|
134 |
|
|
135 |
ML {* test *}
|
12951
|
136 |
ML {* exec (E, the it) *}
|
|
137 |
ML {* exec (E, the it) *}
|
|
138 |
ML {* exec (E, the it) *}
|
|
139 |
ML {* exec (E, the it) *}
|
|
140 |
ML {* exec (E, the it) *}
|
|
141 |
ML {* exec (E, the it) *}
|
|
142 |
ML {* exec (E, the it) *}
|
|
143 |
ML {* exec (E, the it) *}
|
|
144 |
ML {* exec (E, the it) *}
|
|
145 |
ML {* exec (E, the it) *}
|
|
146 |
ML {* exec (E, the it) *}
|
|
147 |
ML {* exec (E, the it) *}
|
|
148 |
ML {* exec (E, the it) *}
|
|
149 |
ML {* exec (E, the it) *}
|
|
150 |
ML {* exec (E, the it) *}
|
|
151 |
ML {* exec (E, the it) *}
|
|
152 |
ML {* exec (E, the it) *}
|
|
153 |
ML {* exec (E, the it) *}
|
|
154 |
ML {* exec (E, the it) *}
|
|
155 |
ML {* exec (E, the it) *}
|
|
156 |
ML {* exec (E, the it) *}
|
|
157 |
ML {* exec (E, the it) *}
|
|
158 |
ML {* exec (E, the it) *}
|
|
159 |
ML {* exec (E, the it) *}
|
|
160 |
ML {* exec (E, the it) *}
|
|
161 |
ML {* exec (E, the it) *}
|
|
162 |
ML {* exec (E, the it) *}
|
|
163 |
ML {* exec (E, the it) *}
|
|
164 |
ML {* exec (E, the it) *}
|
|
165 |
ML {* exec (E, the it) *}
|
|
166 |
ML {* exec (E, the it) *}
|
|
167 |
ML {* exec (E, the it) *}
|
|
168 |
ML {* exec (E, the it) *}
|
|
169 |
ML {* exec (E, the it) *}
|
|
170 |
ML {* exec (E, the it) *}
|
|
171 |
ML {* exec (E, the it) *}
|
|
172 |
ML {* exec (E, the it) *}
|
|
173 |
ML {* exec (E, the it) *}
|
|
174 |
ML {* exec (E, the it) *}
|
|
175 |
ML {* exec (E, the it) *}
|
|
176 |
ML {* exec (E, the it) *}
|
|
177 |
ML {* exec (E, the it) *}
|
|
178 |
ML {* exec (E, the it) *}
|
|
179 |
ML {* exec (E, the it) *}
|
|
180 |
ML {* exec (E, the it) *}
|
|
181 |
ML {* exec (E, the it) *}
|
|
182 |
ML {* exec (E, the it) *}
|
|
183 |
ML {* exec (E, the it) *}
|
|
184 |
ML {* exec (E, the it) *}
|
|
185 |
ML {* exec (E, the it) *}
|
|
186 |
ML {* exec (E, the it) *}
|
|
187 |
ML {* exec (E, the it) *}
|
|
188 |
ML {* exec (E, the it) *}
|
12442
|
189 |
|
12522
|
190 |
end |