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 |
|
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
|
|
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],
|
12442
|
75 |
Load 0,
|
|
76 |
Load 2,
|
12951
|
77 |
Invoke list_name append_name [Class list_name],
|
12442
|
78 |
LitPush Unit,
|
12951
|
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
|
12522
|
97 |
"new_Addr" ("new'_addr {* %x. case x of None => True | Some y => False *}/ {* None *}/ {* Loc *}")
|
12442
|
98 |
|
|
99 |
"arbitrary" ("(raise ERROR)")
|
|
100 |
"arbitrary" :: "val" ("{* Unit *}")
|
12951
|
101 |
"arbitrary" :: "cname" ("Object")
|
12442
|
102 |
|
12951
|
103 |
"list_nam" ("\"list\"")
|
|
104 |
"test_nam" ("\"test\"")
|
12442
|
105 |
"append_name" ("\"append\"")
|
|
106 |
"makelist_name" ("\"makelist\"")
|
|
107 |
"val_nam" ("\"val\"")
|
|
108 |
"next_nam" ("\"next\"")
|
|
109 |
|
|
110 |
ML {*
|
12522
|
111 |
fun new_addr p none loc hp =
|
|
112 |
let fun nr i = if p (hp (loc i)) then (loc i, none) else nr (i+1);
|
12442
|
113 |
in nr 0 end;
|
|
114 |
*}
|
|
115 |
|
|
116 |
subsection {* Single step execution *}
|
|
117 |
|
12951
|
118 |
generate_code
|
13052
|
119 |
test = "exec (E, start_state E test_name makelist_name)"
|
12442
|
120 |
|
|
121 |
ML {* test *}
|
12951
|
122 |
ML {* exec (E, the it) *}
|
|
123 |
ML {* exec (E, the it) *}
|
|
124 |
ML {* exec (E, the it) *}
|
|
125 |
ML {* exec (E, the it) *}
|
|
126 |
ML {* exec (E, the it) *}
|
|
127 |
ML {* exec (E, the it) *}
|
|
128 |
ML {* exec (E, the it) *}
|
|
129 |
ML {* exec (E, the it) *}
|
|
130 |
ML {* exec (E, the it) *}
|
|
131 |
ML {* exec (E, the it) *}
|
|
132 |
ML {* exec (E, the it) *}
|
|
133 |
ML {* exec (E, the it) *}
|
|
134 |
ML {* exec (E, the it) *}
|
|
135 |
ML {* exec (E, the it) *}
|
|
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) *}
|
12442
|
175 |
|
12973
|
176 |
end
|