-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathjava-statements.k
More file actions
654 lines (538 loc) · 18.1 KB
/
java-statements.k
File metadata and controls
654 lines (538 loc) · 18.1 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
require "java-core.k"
module JAVA-STATEMENTS
imports JAVA-CORE
//@ \section{Program stack}
//ListItem content as a stack layer
syntax K ::=
"("
StackLayerType "," //StackLT - type of stack layer, see StackLayerType
K "," //CatchParam, X - layer param
K "," //CatchStm, FinallyStm, ... - layer statement
K "," //K - rest of computation
Map "," //Env
Bag //C - rest of control
")"
//stack layer types
syntax StackLayerType ::=
"return" //generated by method invocations, consumed by return
| "catch" //generated by TryCatch, consumed by throw
| "finally" //generated by TryFinally, consumed by all flow interruptions
| "loop" //generated by While / DoWhile / For,
// consumed by break, continue
| "forBody" //generated by for, consumed by continue
| "label" //generated by Labeled, consumed by break with label.
// Also used by loop rules
// to prepare context for continue with label.
| "switch" //generated by switch, consumed by break
syntax K ::= "popx" //discard the top stack layer
rule [popx]:
<k> popx => . ...</k>
<stack> _:ListItem => . ...</stack>
//@ \section{Blocks} JLS $14.2
rule [Block]:
<k> 'Block(S:K) => S ~> env(Env) ...</k>
<env> Env:Map </env>
[structural]
rule [ListOfStatements]:
'ListWrap(S1:K,, Ss:List{K}) => S1 ~> 'ListWrap(Ss)
[structural]
rule [DiscardEmptyList]:
<k> 'ListWrap(.List{K}) => . ...</k>
[structural]
//@ \texttt{env}
//restore the env cell content from env(...) statement argument
syntax K ::= "env" "(" Map ")"
rule [env]:
<k> env(Env:Map) => . ...</k>
<env> _ => Env </env>
[structural]
rule [envDiscard]:
(env(_) => .) ~> env(_)
[structural]
//@ \section{Local Variable Declaration Statements} JLS $14.4
//@ Not only local but fields also
//local var decs and field decs are reduced to a single case
rule [LocalVarDecStmRed]:
'LocalVarDecStm('LocalVarDec(_,, TypeK:K,, 'ListWrap(Ks:List{K})))
=> 'LocalVarDec(TypeK,, 'ListWrap(Ks))
[structural]
rule [FieldDecToLocalVarDec]:
'FieldDec(_,, TypeK:K,, 'ListWrap(Ks:List{K}))
=> 'LocalVarDec(TypeK,, 'ListWrap(Ks))
[structural]
rule [VarListDecDesugar]:
'LocalVarDec(TypeK:K,, 'ListWrap(Var1:K,, Var2:K,, VarDecs:List{K}))
=> 'LocalVarDec(TypeK,, 'ListWrap(Var1))
~> 'LocalVarDec(TypeK,, 'ListWrap(Var2,, VarDecs))
[structural]
rule [VarDecWithInitDesugar]:
'LocalVarDec(TypeK:K,, 'ListWrap('VarDec(X1:Id,,K3:K)))
=> 'LocalVarDec(TypeK,, 'ListWrap('VarDec(X1:Id)))
~> 'ExprStm('Assign('ExprName(X1),, K3))
[structural]
/*In Java current version of semantics only int and boolean primitive
types are supported*/
context 'LocalVarDec(HOLE,,_)
rule [LocalVarDec]:
<k> 'LocalVarDec(T:Type,,'ListWrap('VarDec(X:Id))) => . ...</k>
<env> Env:Map => Env[L:Int/X] </env>
<store>... . => L |-> unruled(T) :: T ...</store>
<nextLoc> L:Int => L +Int 1 </nextLoc>
//@ \section{Empty statement} JLS $14.6
rule [Empty]:
'Empty(_) => .
//@ \section{Labeled statements} JLS $14.7
rule [Labeled]:
<k> ('Labeled(X:Id,, S:K) => S ~> popx) ~> K:K </k>
<control>
<stack> . => ListItem((label, X, S, K, Env, C)) ...</stack>
C:Bag
</control>
<env> Env:Map </env>
//@ \section{Expression statements} JLS $14.8
context 'ExprStm(HOLE)
rule [ExprStm]:
'ExprStm(_:TypedVal) => .
//@ \section{If statement}
context 'If(HOLE,,_,,_)
rule [IfTrue]:
'If(true::bool,, S:K,, _) => S
rule [IfFalse]:
'If(false::bool,, _,, S:K) => S
rule [IfThenDesugar]:
'If(E:K,,S1:K) => 'If(E,,S1,,'Block('ListWrap(.List{K})))
//@ \section{Assert statement}
context 'AssertStm(HOLE,,_:List{K})
rule [AssertStmTrueDiscard]:
'AssertStm(true::bool,, _:List{K}) => .
rule [AssertStmOneArgDesugar]:
'AssertStm(false::bool) => 'AssertStm(false::bool,, ""::rtString)
[structural]
context 'AssertStm(false::bool,, HOLE)
rule [AssertStm]:
'AssertStm(false::bool,, TV:TypedVal) => 'Throw('NewInstance(
'None(.List{K}),,
'ClassOrInterfaceType( 'TypeName( String2Id("AssertionError") ),, 'None(.List{K}) ),,
'ListWrap( toString(TV) ),,
'None(.List{K})
))
//@ \section{Switch statement}
syntax K ::= "switchImpl"
TypedVal "," // TV - switch param
List{K} "," // switch body - list of 'SwitchGroup labels
K // DefaultStm - default block, if any
context 'Switch(HOLE,,_)
rule [Switch]:
<k>
( 'Switch(TV:TypedVal,, 'SwitchBlock('ListWrap(Ks:List{K}),, TrailingLabels:K))
=> switchImpl TV, (Ks,, 'SwitchGroup(TrailingLabels,, .K)), .K )
~> K:K
</k>
<control>
<stack> . => ListItem((switch, .K, .K, K, Env, C)) ...</stack>
C:Bag
</control>
<env> Env:Map </env>
context switchImpl _,
( 'SwitchGroup('ListWrap('Case(HOLE),,_:List{K}),,_),, _:List{K} ), _
//the type associated to V and V2 is not important for match, only the value.
//JLS3 $14.11, page 377: Every case constant expression associated with
//a switch statement must be assignable (§5.2) to the type of the switch Expression.
rule [SwitchCaseNotMatch]:
switchImpl V:RawVal :: _, ( 'SwitchGroup('ListWrap(
( 'Case(V2:RawVal :: _) => .List{K} ),,
_:List{K} ),,_),, _), _
when
V =/=K V2
/*@Once case of first switch group matches, the whole switch is replaced
by it's block. The execution rules for switch will discard switch-related
constructs and will execute the statements. */
rule [SwitchCaseMatch]:
( switchImpl V:RawVal :: _, ('SwitchGroup('ListWrap('Case(V :: _),, _:List{K}),, S:K),,
Ks:List{K}), _ )
=> 'ListWrap(S,,Ks)
rule [SwitchDefaultSave]:
switchImpl _:TypedVal, (
'SwitchGroup('ListWrap(
( 'Default(_) => .List{K} ),,
_:List{K}),, S:K),, Ks:List{K}
),
( _:K => 'ListWrap(S,, Ks) )
rule [SwitchGroupDiscard]:
switchImpl _:TypedVal, (
( 'SwitchGroup('ListWrap( .List{K} ),,_) => .List{K} ),,
_), _
rule [SwitchDefaultExec]:
switchImpl _, .List{K}, DefaultStm:K => DefaultStm
rule [SwitchGroupExec]:
'SwitchGroup(_:K,, S:K) => S
//@ \section{While statement} and loop infrastructure.
rule [While]:
<k> ( 'While(E:K,,S:K)
=> saveLoop(loopImpl(E, S, .K), K) ~> loopImpl(E, S, .K) ~> popx )
~> K:K
</k>
[structural]
syntax K ::= "loopImpl" "("
K "," // E - test expression
K "," // S - loop body
K // UpdClause - present for For, empty for other loops.
")"
rule [loopImpl]:
<k> loopImpl(E:K, S:K, UpdClause:K)
=> 'If(E,, 'Block(S ~> UpdClause ~> loopImpl(E, S, UpdClause)),, .K)
...
</k>
[structural]
syntax K ::= "saveLoop" "("
K "," // LoopStm - the whole while / do / for statement
K // K - rest of computation
")"
rule [saveLoop]:
<k> (saveLoop(LoopStm:K, K:K) => .) ...</k>
<control>
<stack>
( . => ListItem((loop, getLoopLabel(ParentLayer), LoopStm, K, Env, C)) )
ListItem(ParentLayer:K)
...
</stack>
C:Bag
</control>
<env> Env:Map </env>
/*@ \texttt{Label propagation} through stack layers, used by
continue statement with label.*/
syntax K ::= "getLoopLabel" "(" K ")" [function]
rule getLoopLabel((label, X:Id, S:K, _,_,_:Bag)) => X
when
getKLabel(S) ==KLabel 'While
orBool getKLabel(S) ==KLabel 'DoWhile
orBool getKLabel(S) ==KLabel 'For [anywhere]
rule getLoopLabel((StackLT:StackLayerType, _, S:K, _,_,_:Bag)) => .K
when
StackLT =/=K label
orBool notBool (
getKLabel(S) ==KLabel 'While
orBool getKLabel(S) ==KLabel 'DoWhile
orBool getKLabel(S) ==KLabel 'For
) [anywhere]
//@ \section{Do statement}
rule
<k> ( 'DoWhile(S:K,,E:K)
=> saveLoop(loopImpl(E, S, .K), K) ~> S ~> loopImpl(E, S, .K) ~> popx
)
~> K:K
</k>
[structural]
//@ \section{For statement}
//wrapper of certain clauses of for statement
syntax K ::= "stm" "(" K ")"
rule [stmDiscard]:
stm('ListWrap(.List{K})) => .
[structural]
rule [stmList]:
stm('ListWrap(E:K,, Es:List{K})) => 'ExprStm(E) ~> stm('ListWrap(Es))
[structural]
rule [stmLocalVarDec]:
<k> stm('LocalVarDec(Ks:List{K})) => 'LocalVarDecStm('LocalVarDec(Ks)) ...</k>
[structural]
rule [ForFirstArgV1Red]:
'For((
'ListWrap(InitExps:List{K}) => stm('ListWrap(InitExps))
),,_:List{K})
[structural]
rule [ForFirstArgV2Red]:
'For((
'LocalVarDec(Ks:List{K}) => stm('LocalVarDec(Ks))
),,_:List{K})
[structural]
rule [ForSecondArgRed]:
'For(_:K,,(
'None(.List{K}) => 'Some(true::bool)
),,_:List{K})
[structural]
rule [ForThirdArgRed]:
'For(_:K,,_:K,,(
'ListWrap(UpdExps:List{K}) => stm('ListWrap(UpdExps))
),,_:K)
[structural]
//this form of resulting while is required for compatibility with continue statement
rule [For]:
<k> ( 'For(stm(InitClause:K),, 'Some(TestExp:K),, stm(UpdClause:K),, S:K)
=> saveLoop(loopImpl(TestExp, S, stm(UpdClause)), K) ~> 'Block(stm(InitClause)
~> saveForBody ~> loopImpl(TestExp, S, stm(UpdClause))) ~> popx
)
~> K:K
</k>
[structural]
syntax K ::= "saveForBody"
rule [saveForBody]:
<k> (saveForBody => .) ~> loopImpl(E:K, S:K, UpdClause:K) ~> (. => popx) ~> K:K </k>
<control>
<stack>
( . => ListItem( (forBody, getForBodyLabel(ParentLayer),
loopImpl(E, S, UpdClause), K, Env, C) ) )
ListItem(ParentLayer:K)
...
</stack>
C:Bag
</control>
<env> Env:Map </env>
syntax K ::= "getForBodyLabel" "(" K ")" [function]
//the parent layer of forBody is always loop
rule getForBodyLabel((loop, K:K, _,_,_,_:Bag)) => K [anywhere]
//@ \section{Break statement}
rule [Break]:
<k> 'Break('None(_)) ~> _:K => K </k>
<control>
<stack> ListItem((StackLT:StackLayerType, _, _, K:K, Env:Map, C:Bag)) => . ...</stack>
(_ => C)
</control>
<env> _ => Env </env>
when
StackLT ==K loop
orBool StackLT ==K switch
rule [BreakPassThrough]:
<k> 'Break('None(_)) ...</k>
<stack> ListItem((StackLT:StackLayerType,_,_,_,_,_:Bag)) => . ...</stack>
when
StackLT =/=K loop
andBool StackLT =/=K switch
andBool StackLT =/=K finally
//break with label
rule [BreakLabel]:
<k> 'Break('Some(X:Id)) ~> _:K => K </k>
<control>
<stack> ListItem((label, X, _, K:K, Env:Map, C:Bag)) => . ...</stack>
(_ => C)
</control>
<env> _ => Env </env>
rule [BreakLabelPassThrough]:
<k> 'Break('Some(X:Id)) ...</k>
<stack> ListItem((StackLT:StackLayerType, StackArg2:K, _,_,_,_:Bag)) => . ...</stack>
when
(
StackLT =/=K label
andBool StackLT =/=K finally
)
orBool (
StackLT ==K label
andBool X =/=K StackArg2
)
//works for both break with and without label
rule [BreakAnyInTryFinally]:
<k> (. => FinallyS) ~> 'Break(_) ...</k>
<control>
<stack> ListItem((finally,_,FinallyS:K,_,Env:Map,C:Bag)) => . ...</stack>
(_ => C)
</control>
<env> _ => Env </env>
//@ \section{Continue statement}
rule [Continue]:
<k> 'Continue('None(_)) ~> _:K => UpdClause ~> loopImpl(E, S, UpdClause) ~> popx ~> K </k>
<control>
<stack>
ListItem((StackLT:StackLayerType, _, loopImpl(E:K, S:K, UpdClause:K), K:K, Env:Map, C:Bag))
...
</stack>
(_ => C)
</control>
<env> _ => Env </env>
when
StackLT ==K loop orBool StackLT ==K forBody
rule [ContinuePassThrough]:
<k> 'Continue('None(_)) ...</k>
<stack>
ListItem((StackLT:StackLayerType,_,_,_,_,_:Bag)) => .
...
</stack>
when
StackLT =/=K loop
andBool StackLT =/=K forBody
andBool StackLT =/=K finally
rule [ContinueLabel]:
<k> 'Continue('Some(X:Id)) ~> _:K => UpdClause ~> loopImpl(E, S, UpdClause) ~> popx ~> K </k>
<control>
<stack>
ListItem((StackLT:StackLayerType, X:Id, loopImpl(E:K, S:K, UpdClause:K), K:K, Env:Map, C:Bag))
...
</stack>
(_ => C)
</control>
<env> _ => Env </env>
when
StackLT ==K loop
orBool StackLT ==K forBody
rule [ContinueLabelPassThrough]:
<k> 'Continue('Some(X:Id)) ...</k>
<stack>
ListItem((StackLT:StackLayerType, StackArg2:K, _,_,_,_:Bag)) => .
...
</stack>
when
(
StackLT =/=K loop
andBool StackLT =/=K forBody
andBool StackLT =/=K finally
)
orBool (
(
StackLT ==K loop
orBool StackLT ==K forBody
)
andBool (
X =/=K StackArg2
)
)
//works for both break with and without label
rule [ContinueAnyInTryFinally]:
<k> (. => FinallyS) ~> 'Continue(_) ...</k>
<control>
<stack>
ListItem((finally,_,FinallyS:K,_,Env:Map,C:Bag)) => .
...
</stack>
(_ => C)
</control>
<env> _ => Env </env>
/*@\section{Return} At method return, we have to check that the type
of the returned
value is a subtype of the expected return type. Moreover, if that is
the case, then we also upcast the returned value to one of the
expected type. */
context 'Return('Some(HOLE))
rule [ReturnNothingDesugar]:
'Return('None(.List{K}) => 'Some(nothing :: void))
[structural]
rule [Return]:
<k>
'Return('Some(TV:TypedVal)) ~> _
=> subtype typeOf(TV), T ~> true? ~> unsafeCast(TV, T) ~> K:K
</k>
<control>
<stack> ListItem((return,_,_,K:K,Env:Map,C:Bag)) => . ...</stack>
( <return> T:Type </return> _ => C )
</control>
<env> _ => Env </env>
rule [ReturnInTryFinally]:
<k> (. => FinallyS) ~> 'Return('Some(_:TypedVal)) ...</k>
<control>
<stack> ListItem((finally,_,FinallyS:K,_,Env:Map,C:Bag)) => . ...</stack>
(_ => C)
</control>
<env> _ => Env </env>
rule [ReturnPassThrough]:
<k> 'Return('Some(_:TypedVal)) ...</k>
<stack> ListItem((StackLT:StackLayerType,_,_,_,_,_:Bag)) => . ...</stack>
when
StackLT =/=K return
andBool StackLT =/=K finally
/*@ \section{Throw statement}
Exceptions are propagated now until a catch that can bind them is
encountered. */
context 'Throw(HOLE)
rule [Throw]:
<k>
(. => checkCatch(typeOf(TV), TypeK)) ~> 'Throw(TV:TypedVal)
...
</k>
<stack>
ListItem((catch, .K, 'ListWrap('Catch('Param(_:K,, TypeK:K,, _:Id),, _),, _),
_,_,_))
...
</stack>
syntax K ::= "checkCatch" "(" K "," K ")" [strict]
| "checkCatch" "(" K ")"
rule checkCatch(T1:Type, T2:Type) => checkCatch(subtype T1, T2)
context checkCatch(HOLE)
rule [ThrowCheckedTrue]:
<k>
checkCatch(true::bool) ~> 'Throw(TV:TypedVal) ~> _:K
=> 'Block('ListWrap('LocalVarDecStm('LocalVarDec(
'ListWrap(.List{K}),, TypeK,, 'ListWrap('VarDec(X,, TV)))),,
CatchS))
~> K
</k>
<control>
<stack>
ListItem((catch, .K, 'ListWrap('Catch(
'Param(_:K,, TypeK:K,, X:Id),, CatchS:K),,_), K:K, Env:Map, C:Bag)) => .
...
</stack>
(_ => C)
</control>
<env> _ => Env </env>
rule [ThrowCheckedFalse]:
<k> (checkCatch(false::bool) => .) ~> 'Throw(_) ...</k>
<stack>
ListItem((catch, .K, 'ListWrap(
('Catch(_) => .List{K}),,
_:List{K}), _,_,_))
...
</stack>
rule [ThrowCatchEmptyDiscard]:
<k> 'Throw(_:TypedVal) ...</k>
<stack>
ListItem((catch, .K, 'ListWrap(.List{K}), _,_,_)) => .
...
</stack>
rule [ThrowInTryFinally]:
<k> (. => FinallyS) ~> 'Throw(_:TypedVal) ...</k>
<control>
<stack> ListItem((finally,_,FinallyS:K,_,Env:Map,C:Bag)) => . ...</stack>
(_ => C)
</control>
<env> _ => Env </env>
rule [ThrowPassThrough]:
<k> 'Throw(_:TypedVal) ...</k>
<stack> ListItem((StackLT:StackLayerType,_,_,_,_,_:Bag)) => . ...</stack>
when
StackLT =/=K catch
andBool StackLT =/=K finally
rule [ThrowCausesThreadTermination]:
<k>
'Throw(TV:TypedVal) ~> _:K
=> 'ExprStm('Invoke(rtSystemOutPrintln,, 'ListWrap(
'Plus("Thread terminated with exception: "::rtString,, TV))))
</k>
<stack> .List </stack>
//@ \section{Try statement}
rule [TryCatchFinallyDesugar]:
'Try(TryS:K,, 'ListWrap(K:K,, Ks:List{K}),,FinallyS:K)
=> 'Try('Try(TryS,,'ListWrap(K,, Ks)),,'ListWrap(.List{K}),,FinallyS)
[structural]
//@ \subsection{Execution of trycatch} JLS 14.20.1
rule [TryCatch]:
<k>
( 'Try(TryS:K,, 'ListWrap(Ks:List{K}))
=> TryS ~> popx )
~> K
</k>
<control>
<stack> . => ListItem((catch, .K, 'ListWrap(Ks), K:K, Env:Map, C:Bag)) ...</stack>
C:Bag
</control>
<env> Env </env>
//@ \subsection{Execution of try-finally} JLS 14.20.2
rule [TryFinally]:
<k>
'Try(TryS:K ,,'ListWrap(.List{K}),,FinallyS:K)
=> TryS ~> popx ~> FinallyS
...
</k>
<control>
<stack> . => ListItem((finally, .K, FinallyS, .K, Env, C)) ...</stack>
C:Bag
</control>
<env> Env:Map </env>
//@ \section{Auxiliary operations}
rule [ThreadTermination]:
( <thread>
...
<k>.K</k>
<holds> H:Map </holds>
...
</thread>
=> .)
<busy> Busy:Set => Busy -Set keys(H) </busy>
<dissolveEmptyK> true </dissolveEmptyK>
endmodule