author | wenzelm |
Mon, 30 Jul 2007 19:46:15 +0200 | |
changeset 24074 | 40f414b87655 |
parent 22921 | 475ff421a6a3 |
child 24225 | 348e982c694b |
permissions | -rw-r--r-- |
12442 | 1 |
(* Title: HOL/MicroJava/JVM/JVMListExample.thy |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer |
|
4 |
*) |
|
5 |
||
12973 | 6 |
header {* \isaheader{Example for generating executable code from JVM semantics}\label{sec:JVMListExample} *} |
12442 | 7 |
|
16417 | 8 |
theory JVMListExample imports SystemClasses JVMExec begin |
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 |
|
17 |
||
18 |
constdefs |
|
12951 | 19 |
list_name :: cname |
20 |
"list_name == Cname list_nam" |
|
21 |
||
22 |
test_name :: cname |
|
23 |
"test_name == Cname test_nam" |
|
24 |
||
12442 | 25 |
val_name :: vname |
26 |
"val_name == VName val_nam" |
|
27 |
||
28 |
next_name :: vname |
|
29 |
"next_name == VName next_nam" |
|
30 |
||
12951 | 31 |
append_ins :: bytecode |
32 |
"append_ins == |
|
12442 | 33 |
[Load 0, |
34 |
Getfield next_name list_name, |
|
35 |
Dup, |
|
36 |
LitPush Null, |
|
37 |
Ifcmpeq 4, |
|
12951 | 38 |
Load 1, |
39 |
Invoke list_name append_name [Class list_name], |
|
12442 | 40 |
Return, |
41 |
Pop, |
|
42 |
Load 0, |
|
43 |
Load 1, |
|
44 |
Putfield next_name list_name, |
|
45 |
LitPush Unit, |
|
12951 | 46 |
Return]" |
12442 | 47 |
|
12951 | 48 |
list_class :: "jvm_method class" |
49 |
"list_class == |
|
50 |
(Object, |
|
51 |
[(val_name, PrimT Integer), (next_name, Class list_name)], |
|
52 |
[((append_name, [Class list_name]), PrimT Void, |
|
53 |
(3, 0, append_ins,[(1,2,8,Xcpt NullPointer)]))])" |
|
54 |
||
55 |
make_list_ins :: bytecode |
|
56 |
"make_list_ins == |
|
12442 | 57 |
[New list_name, |
58 |
Dup, |
|
59 |
Store 0, |
|
60 |
LitPush (Intg 1), |
|
61 |
Putfield val_name list_name, |
|
62 |
New list_name, |
|
63 |
Dup, |
|
64 |
Store 1, |
|
65 |
LitPush (Intg 2), |
|
66 |
Putfield val_name list_name, |
|
67 |
New list_name, |
|
68 |
Dup, |
|
69 |
Store 2, |
|
70 |
LitPush (Intg 3), |
|
71 |
Putfield val_name list_name, |
|
72 |
Load 0, |
|
73 |
Load 1, |
|
12951 | 74 |
Invoke list_name append_name [Class list_name], |
13101 | 75 |
Pop, |
12442 | 76 |
Load 0, |
77 |
Load 2, |
|
12951 | 78 |
Invoke list_name append_name [Class list_name], |
79 |
Return]" |
|
80 |
||
81 |
test_class :: "jvm_method class" |
|
82 |
"test_class == |
|
83 |
(Object, [], |
|
84 |
[((makelist_name, []), PrimT Void, (3, 2, make_list_ins,[]))])" |
|
12442 | 85 |
|
12951 | 86 |
E :: jvm_prog |
87 |
"E == SystemClasses @ [(list_name, list_class), (test_name, test_class)]" |
|
88 |
||
12442 | 89 |
|
90 |
types_code |
|
12951 | 91 |
cnam ("string") |
12442 | 92 |
vnam ("string") |
93 |
mname ("string") |
|
12522 | 94 |
loc_ ("int") |
12442 | 95 |
|
96 |
consts_code |
|
16770
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16644
diff
changeset
|
97 |
"new_Addr" ("\<module>new'_addr {* %x. case x of None => True | Some y => False *}/ {* None *}/ {* Loc *}") |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16644
diff
changeset
|
98 |
attach {* |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16644
diff
changeset
|
99 |
fun new_addr p none loc hp = |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16644
diff
changeset
|
100 |
let fun nr i = if p (hp (loc i)) then (loc i, none) else nr (i+1); |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16644
diff
changeset
|
101 |
in nr 0 end; |
1f1b1fae30e4
Auxiliary functions to be used in generated code are now defined using "attach".
berghofe
parents:
16644
diff
changeset
|
102 |
*} |
12442 | 103 |
|
18679 | 104 |
"arbitrary" ("(raise Match)") |
22921
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
21404
diff
changeset
|
105 |
"arbitrary :: val" ("{* Unit *}") |
475ff421a6a3
consts in consts_code Isar commands are now referred to by usual term syntax
haftmann
parents:
21404
diff
changeset
|
106 |
"arbitrary :: cname" ("{* Object *}") |
12442 | 107 |
|
12951 | 108 |
"list_nam" ("\"list\"") |
109 |
"test_nam" ("\"test\"") |
|
12442 | 110 |
"append_name" ("\"append\"") |
111 |
"makelist_name" ("\"makelist\"") |
|
112 |
"val_nam" ("\"val\"") |
|
113 |
"next_nam" ("\"next\"") |
|
114 |
||
20971 | 115 |
code_type cnam and vnam and mname and loc_ |
21113 | 116 |
(SML "string" and "string" and "string" and "IntInf.int") |
20971 | 117 |
|
118 |
instance cnam :: eq |
|
119 |
and cname :: eq |
|
120 |
and vname :: eq |
|
121 |
and mname :: eq |
|
122 |
and ty :: eq |
|
21063 | 123 |
and val :: eq |
20971 | 124 |
and instr :: eq .. |
125 |
||
126 |
definition |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21113
diff
changeset
|
127 |
arbitrary_val :: val where |
21063 | 128 |
[symmetric, code inline]: "arbitrary_val = arbitrary" |
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21113
diff
changeset
|
129 |
definition |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21113
diff
changeset
|
130 |
arbitrary_cname :: cname where |
21063 | 131 |
[symmetric, code inline]: "arbitrary_cname = arbitrary" |
20971 | 132 |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21113
diff
changeset
|
133 |
definition "unit' = Unit" |
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21113
diff
changeset
|
134 |
definition "object' = Object" |
20971 | 135 |
|
21063 | 136 |
code_axioms |
20971 | 137 |
arbitrary_val \<equiv> unit' |
138 |
arbitrary_cname \<equiv> object' |
|
139 |
||
140 |
code_const list_nam and test_nam and append_name and makelist_name and val_nam and next_nam |
|
21113 | 141 |
(SML "\"list\"" and "\"test\"" and "\"append\"" |
142 |
and "\"makelist\"" and "\"val\"" and "\"next\"") |
|
20971 | 143 |
|
144 |
axiomatization |
|
145 |
incr_loc :: "loc_ \<Rightarrow> loc_" |
|
146 |
and zero_loc :: "loc_" |
|
147 |
||
148 |
code_const incr_loc and zero_loc |
|
21113 | 149 |
(SML "(op +)/ (_, 1)" and "0") |
20971 | 150 |
|
151 |
axiomatization |
|
152 |
test_loc :: "(loc_ \<Rightarrow> bool) \<Rightarrow> (loc_ \<Rightarrow> 'a) \<Rightarrow> loc_ \<Rightarrow> 'a" where |
|
153 |
"test_loc p v l = (if p l then v l else test_loc p v (incr l))" |
|
154 |
||
155 |
definition |
|
21404
eb85850d3eb7
more robust syntax for definition/abbreviation/notation;
wenzelm
parents:
21113
diff
changeset
|
156 |
new_addr' :: "(loc \<Rightarrow> (cname \<times> (vname \<times> cname \<Rightarrow> val option)) option) \<Rightarrow> loc \<times> val option" where |
20971 | 157 |
"new_addr' hp = |
21113 | 158 |
test_loc (\<lambda>i. hp (Loc i) = None) (\<lambda>i. (Loc i, None)) zero_loc" |
20971 | 159 |
|
160 |
lemma [code func]: |
|
161 |
"wf_class = wf_class" .. |
|
162 |
||
163 |
code_const |
|
21113 | 164 |
wf_class (SML "(fn `_ => true)") |
20971 | 165 |
|
21063 | 166 |
code_axioms |
20971 | 167 |
new_Addr \<equiv> new_addr' |
168 |
||
169 |
definition |
|
170 |
"test = exec (E, start_state E test_name makelist_name)" |
|
171 |
||
172 |
||
12442 | 173 |
subsection {* Single step execution *} |
174 |
||
17145 | 175 |
code_module JVM |
176 |
contains |
|
13052 | 177 |
test = "exec (E, start_state E test_name makelist_name)" |
12442 | 178 |
|
24074 | 179 |
ML {* |
180 |
JVM.test; |
|
181 |
JVM.exec (JVM.E, JVM.the it); |
|
182 |
JVM.exec (JVM.E, JVM.the it); |
|
183 |
JVM.exec (JVM.E, JVM.the it); |
|
184 |
JVM.exec (JVM.E, JVM.the it); |
|
185 |
JVM.exec (JVM.E, JVM.the it); |
|
186 |
JVM.exec (JVM.E, JVM.the it); |
|
187 |
JVM.exec (JVM.E, JVM.the it); |
|
188 |
JVM.exec (JVM.E, JVM.the it); |
|
189 |
JVM.exec (JVM.E, JVM.the it); |
|
190 |
JVM.exec (JVM.E, JVM.the it); |
|
191 |
JVM.exec (JVM.E, JVM.the it); |
|
192 |
JVM.exec (JVM.E, JVM.the it); |
|
193 |
JVM.exec (JVM.E, JVM.the it); |
|
194 |
JVM.exec (JVM.E, JVM.the it); |
|
195 |
JVM.exec (JVM.E, JVM.the it); |
|
196 |
JVM.exec (JVM.E, JVM.the it); |
|
197 |
JVM.exec (JVM.E, JVM.the it); |
|
198 |
JVM.exec (JVM.E, JVM.the it); |
|
199 |
JVM.exec (JVM.E, JVM.the it); |
|
200 |
JVM.exec (JVM.E, JVM.the it); |
|
201 |
JVM.exec (JVM.E, JVM.the it); |
|
202 |
JVM.exec (JVM.E, JVM.the it); |
|
203 |
JVM.exec (JVM.E, JVM.the it); |
|
204 |
JVM.exec (JVM.E, JVM.the it); |
|
205 |
JVM.exec (JVM.E, JVM.the it); |
|
206 |
JVM.exec (JVM.E, JVM.the it); |
|
207 |
JVM.exec (JVM.E, JVM.the it); |
|
208 |
JVM.exec (JVM.E, JVM.the it); |
|
209 |
JVM.exec (JVM.E, JVM.the it); |
|
210 |
JVM.exec (JVM.E, JVM.the it); |
|
211 |
JVM.exec (JVM.E, JVM.the it); |
|
212 |
JVM.exec (JVM.E, JVM.the it); |
|
213 |
JVM.exec (JVM.E, JVM.the it); |
|
214 |
JVM.exec (JVM.E, JVM.the it); |
|
215 |
JVM.exec (JVM.E, JVM.the it); |
|
216 |
JVM.exec (JVM.E, JVM.the it); |
|
217 |
JVM.exec (JVM.E, JVM.the it); |
|
218 |
JVM.exec (JVM.E, JVM.the it); |
|
219 |
JVM.exec (JVM.E, JVM.the it); |
|
220 |
JVM.exec (JVM.E, JVM.the it); |
|
221 |
JVM.exec (JVM.E, JVM.the it); |
|
222 |
JVM.exec (JVM.E, JVM.the it); |
|
223 |
JVM.exec (JVM.E, JVM.the it); |
|
224 |
JVM.exec (JVM.E, JVM.the it); |
|
225 |
JVM.exec (JVM.E, JVM.the it); |
|
226 |
JVM.exec (JVM.E, JVM.the it); |
|
227 |
JVM.exec (JVM.E, JVM.the it); |
|
228 |
JVM.exec (JVM.E, JVM.the it); |
|
229 |
JVM.exec (JVM.E, JVM.the it); |
|
230 |
JVM.exec (JVM.E, JVM.the it); |
|
231 |
JVM.exec (JVM.E, JVM.the it); |
|
232 |
JVM.exec (JVM.E, JVM.the it); |
|
233 |
JVM.exec (JVM.E, JVM.the it); |
|
234 |
*} |
|
12442 | 235 |
|
12973 | 236 |
end |