Repository navigation
Expand file tree
/
Copy pathapi-core.k
More file actions
293 lines (255 loc) · 8.92 KB
/
Copy pathapi-core.k
File metadata and controls
293 lines (255 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*@ \section{Module API-CORE}
All the methods defined in this module through invokeImpl are native, their body is 'NoMethodBody(\_), thus
they cannot be matched by rules in METHOD-INVOKE.
*/
module API-CORE
imports CORE-SORTS
imports METHOD-INVOKE-REST //for invokeImpl, module EXPRESSIONS
imports ARRAYS // for allocAndInitArray()
/*@ This module contains the minimal part of java API required to perform console read/write operations,
as well as a few other API functions mentioned in JLS.*/
//@system-out-print-convert heat argument
context
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[HOLE]
)
when
Class ==K class String2Id("java.io.PrintWriter") andBool Id2String(MethodName) ==String "print"
//@ System.out.print(). For integers and strings, print their value. For classes, print class type.
rule [system-out-print-string]:
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[Str:String :: _]
) => nothing::void
...
</k>
<out>... .List => ListItem(Str) </out>
when
Class ==K class String2Id("java.io.PrintWriter") andBool Id2String(MethodName) ==String "print"
//With [transition-threading], the time to produce 2 solutions for the test threads_11_two_th_msg is 14s.
//But with [transition-strictness], the same test takes 3m52s, with java heap = 16GB.
[transition-threading]
//@Is only used for primitive type arguments. Object arguments are converted by java code inside the class PrintWriter.
rule [system-out-print-convert]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[RV:RawVal :: T:Type => toString(RV::T)]
)
when
Class ==K class String2Id("java.io.PrintWriter")
andBool (Id2String(MethodName) ==String "print")
andBool notBool (isString(RV) ==K true)
/*@Scanner.nextInt(), used to read from the console.*/
rule [scanner-nextInt]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[.KList]
) => readInt
when
Class ==K class String2Id("java.util.Scanner") andBool Id2String(MethodName) ==String "nextInt"
rule [scanner-nextLine]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), _,
[.KList]
) => readString
when
Class ==K class String2Id("java.util.Scanner") andBool Id2String(MethodName) ==String "nextLine"
//@An intermediate construct for reading from console, meant mostly to simplify debugging.
syntax KItem ::= "readInt"
| "readString"
rule [readInt]:
<k> readInt => I :: int ...</k>
<in> ListItem(I:Int) => .List ...</in>
rule [readString]:
<k> readString => Str :: classString ...</k>
<in> ListItem(Str:String) => .List ...</in>
/*@ object.getClass()*/
rule [object-getClass-on-object]:
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), objectRef(OL:Int,_)::_,
[.KList]
) => 'Lit('Class(BottomClass))
...
</k>
<store>
...
OL |-> objectRef(OId:Int, _) :: _
...
</store>
<objectId> OId </objectId>
<objectType> BottomClass:ClassType </objectType>
when
Class ==K classObject andBool Id2String(MethodName) ==String "getClass"
rule [object-getClass-on-String]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), Str:String::_,
[.KList]
) => 'Lit('Class(classString))
when
Class ==K classObject andBool Id2String(MethodName) ==String "getClass"
rule [object-getClass-on-array]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), arrayRef(ArrT:Type,_,_)::_,
[.KList]
) => 'Lit('Class(ArrT))
when
Class ==K classObject andBool Id2String(MethodName) ==String "getClass"
/*@ String.toString(). For Object.toString() the implementation is in the library java code.*/
rule [object-toString-on-String]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), Str:String::_,
[.KList]
) => Str::classString
when
Class ==K classObject andBool Id2String(MethodName) ==String "toString"
/*@Object.hashCode(). Returns the canonical memory location of the objectRef. */
rule [object-hashCode]:
<k>
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType), objectRef(OL:Int,_)::_,
[.KList]
) => OId :: int
...
</k>
<store>
...
OL |-> objectRef(OId:Int, _) :: _
...
</store>
when
Class ==K classObject andBool Id2String(MethodName) ==String "hashCode"
//@\subsection{String methods}
//@ int String.length()
rule [String-length]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
Str:String::Class,
[.KList]
)
=> lengthString(Str)
when
Class ==K classString andBool Id2String(MethodName) ==String "length"
rule [String-charAt]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
Str:String::Class,
[I:Int :: int]
)
=> ordChar(substrString(Str, I, I +Int 1)) :: char
when
Class ==K classString andBool Id2String(MethodName) ==String "charAt"
//@static native String String.valueOf(int i);
rule [String-valueOf]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
Str:String::Class,
[I:Int :: int]
)
=> Int2String(I)
when
Class ==K classString andBool Id2String(MethodName) ==String "valueOf"
//@boolean String.equals(Object)
rule [String-equals]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
Str:String::Class,
[ParamStr:String::_]
)
=> 'Eq(Str,, ParamStr)
when
Class ==K classString andBool Id2String(MethodName) ==String "equals"
//@String.toString()
rule [String-toString]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
Str:String::Class,
[.KList]
)
=> Str
when
Class ==K classString andBool Id2String(MethodName) ==String "toString"
//@ int String.compareTo(Object another)
rule [String-compareTo]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
Str:String::Class,
[ParamStr:String::_]
)
=> ifAux(Str <String ParamStr, -1, ifAux(Str ==String ParamStr,0,1))
when
Class ==K classString andBool Id2String(MethodName) ==String "compareTo"
//@\subsection{Array clone}
rule [array-clone]:
invokeImpl(
methodRef(sig(MethodName:Id, _), Class:ClassType),
arrayRef(arrayOf ElemT, Loc:Int, Len:Int)::CompileT:RefType,
[.KList]
)
=> arrayCopy(
arrayRef(arrayOf ElemT:Type, Loc:Int, Len:Int)::CompileT,
allocAndInitArray(ElemT, [Len::int], undefined :: ElemT)
)
when
Class ==K classArrayImpl andBool Id2String(MethodName) ==String "clone"
//@ Construct to copy content of an array to another after copy, the term is rewritten into the destination array.
syntax KItem ::= arrayCopy (
TypedVal, // source array
K // destination array, computes to TypedVal
)
[strict(2)]
rule arrayCopy(arrayRef(T:Type, L1:Int, Len:Int)::_, arrayRef(T, L2:Int, Len)::_)
=> storeCopy(L1, L2, Len) ~> arrayRef(T,L2:Int,Len)
//@Copy a sequence of store values from one place to another.
syntax KItem ::= storeCopy (
Int, //source address in store
Int, //destination address in store
Int //amount of elements to copy
)
rule [storeCopy]:
<k>
storeCopy(L1:Int, L2:Int, Len:Int) => storeCopy(L1 +Int 1, L2 +Int 1, Len -Int 1)
...
</k>
<store>
...
L1 |-> K:K
//...
L2 |-> (_ => K)
...
</store>
when
Len >Int 0
rule [storeCopy-discard]:
storeCopy(_, _, 0) => .
//@\subsection{Class literal - <Class>.class}
/*@ We need to synchronize access to class literals to avoid instantiation of the came .class by multiple threads.
This is done by the term temp in <classLiteralsMap>. We cannot use 'Synchronized because it required an
objectRef as argument, and we might not have any objects instantiated at the moment.
*/
rule [Lit-Class-Instantiate]:
<k>
(.K => saveClassLiteral(T, new class String2Id("java.lang.Class") (toString(T)) ) )
~> 'Lit('Class(T:Type))
...
</k>
<classLiteralsMap> CLMap:Map (.Map => T |-> temp) </classLiteralsMap>
when notBool T in keys(CLMap)
[transition-threading]
//@ Wraps a newly created java.lang.Class object
syntax KItem ::= saveClassLiteral (
Type, // The type for this class literal is being instantiated
K // The class literal instantiation expression
)
[strict(2)]
rule [saveClassLiteral]:
<k> saveClassLiteral(T:Type, TV:TypedVal) => . ...</k>
<classLiteralsMap>... T |-> (temp => TV) ...</classLiteralsMap>
rule [Lit-Class]:
<k> 'Lit('Class(T:Type)) => TV ...</k>
<classLiteralsMap>... T |-> TV:TypedVal ...</classLiteralsMap>
//@Used inside <classLiteralsMap> only for synchronization purposes
syntax KItem ::= "temp"
endmodule