-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSemanticsTests.fs
709 lines (646 loc) · 29.1 KB
/
SemanticsTests.fs
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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/// <summary>
/// Tests for <c>Starling.Core.Semantics</c>.
/// </summary>
module Starling.Tests.Semantics
open Chessie.ErrorHandling
open NUnit.Framework
open Starling
open Starling.Collections
open Starling.Utils
open Starling.Utils.Testing
open Starling.Core.Command
open Starling.Core.Command.Create
open Starling.Core.TypeSystem
open Starling.Core.Expr
open Starling.Core.Symbolic
open Starling.Core.Var
open Starling.Core.Model
open Starling.Semantics
open Starling.Tests.Studies
/// <summary>
/// Tests for instruction capping.
/// </summary>
module Cap =
open Starling.Core.Pretty
open Starling.Core.Traversal.Pretty
open Starling.Semantics.Pretty
/// <summary>
/// Checks the capped instruction generated from an assign map against
/// a given instruction.
/// </summary>
/// <param name="expectedMap">The expected capped instruction.</param>
/// <param name="expectedInstr">The expected capped instructions.</param>
/// <param name="map">The assign map to use.</param>
/// <param name="instr">The instructions to cap.</param>
let check
(expectedMap : Map<TypedVar, MarkedVar>)
(expectedInstrs : Microcode<CTyped<MarkedVar>, Sym<MarkedVar>> list)
(map : Map<TypedVar, MarkedVar>)
(instrs : Microcode<CTyped<MarkedVar>, Sym<MarkedVar>> list)
: unit =
assertOkAndEqual
(expectedMap, expectedInstrs)
(capInstructions map instrs)
(printTraversalError printSemanticsError >> printUnstyled)
[<Test>]
let ``capping rewrites lvalues in assignments`` () =
check
(Map.ofList
[ (Int (normalRec, "foo"), After "foo")
(Int (normalRec, "bar"), Before "bar") ])
[ Assign
(Int (normalRec, After "foo"),
Some (Expr.Int (normalRec, IVar (Reg (Intermediate (4I, "foo")))))) ]
(Map.ofList
[ (Int (normalRec, "foo"), Intermediate (5I, "foo"))
(Int (normalRec, "bar"), Before "bar") ])
[ Assign
(Int (normalRec, Intermediate (5I, "foo")),
Some (Expr.Int (normalRec, IVar (Reg (Intermediate (4I, "foo")))))) ]
[<Test>]
let ``capping rewrites rvalues in assignments`` () =
check
(Map.ofList
[ (Int (normalRec, "foo"), After "foo")
(Int (normalRec, "bar"), After "bar") ])
[ Assign
(Int (normalRec, After "foo"),
Some (Expr.Int (normalRec, IVar (Reg (After "bar"))))) ]
(Map.ofList
[ (Int (normalRec, "foo"), After ("foo"))
(Int (normalRec, "bar"), Intermediate (2I, "bar")) ])
[ Assign
(Int (normalRec, After "foo"),
Some (Expr.Int (normalRec, IVar (Reg (Intermediate (2I, "bar")))))) ]
[<Test>]
let ``capping rewrites lvalues and rvalues in assignments`` () =
check
(Map.ofList
[ (Int (normalRec, "foo"), After "foo")
(Int (normalRec, "bar"), After "bar") ])
[ Assign
(Int (normalRec, After "foo"),
Some (Expr.Int (normalRec, IVar (Reg (After "bar"))))) ]
(Map.ofList
[ (Int (normalRec, "foo"), Intermediate (5I, "foo"))
(Int (normalRec, "bar"), Intermediate (2I, "bar")) ])
[ Assign
(Int (normalRec, Intermediate (5I, "foo")),
Some (Expr.Int (normalRec, IVar (Reg (Intermediate (2I, "bar")))))) ]
[<Test>]
let ``ticket lock example is capped properly`` () =
check
(Map.ofList
[ (Int (normalRec, "t"), After "t")
(Int (normalRec, "s"), After "s")
(Int (normalRec, "serving"), Before "serving")
(Int (normalRec, "ticket"), Before "ticket") ] )
[ Int (normalRec, After "t") *<- normalIntExpr (siBefore "serving")
Int (normalRec, After "s") *<- normalIntExpr (siAfter "t") ]
(Map.ofList
[ (Int (normalRec, "t"), Intermediate (0I, "t"))
(Int (normalRec, "s"), Intermediate (0I, "s"))
(Int (normalRec, "serving"), Before "serving")
(Int (normalRec, "ticket"), Before "ticket") ] )
[ Int (normalRec, Intermediate (0I, "t")) *<- normalIntExpr (siBefore "serving")
Int (normalRec, Intermediate (0I, "s")) *<- normalIntExpr (IVar (Reg (Intermediate (0I, "t")))) ]
/// Shorthand for expressing an array update.
let aupd' arr idx var : ArrayExpr<'Var> =
AUpd (stripTypeRec arr, idx, var)
let aupd arr idx var =
typedArrayToExpr (mkTypedSub arr.SRec (aupd' arr idx var))
/// <summary>
/// Tests for frame generation.
/// </summary>
module MakeFrame =
/// <summary>
/// Checks the frame generated from an assign map against a given
/// Boolean expression.
/// </summary>
/// <param name="expected">The expected list of frame expressions.</param>
/// <param name="toFrame">The assign map to convert to a frame.</param>
let check
(expected : BoolExpr<Sym<MarkedVar>> list)
(toFrame : Map<TypedVar, MarkedVar>)
: unit =
List.sort expected ?=? List.sort (makeFrame toFrame)
[<Test>]
let ``the empty assign list generates the empty frame`` () =
check [] Map.empty
[<Test>]
let ``only non-after variables are framed`` () =
check
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "t") (siInter 0I "t") ]
(Map.ofList
[ (Int (normalRec, "serving"), Before "serving")
(Int (normalRec, "ticket"), Before "ticket")
(Int (normalRec, "t"), Intermediate (0I, "t"))
(Int (normalRec, "s"), After "s") ] )
/// <summary>
/// Tests for microcode normalisation.
/// </summary>
module Normalisation =
open Starling.Core.Pretty
open Starling.Semantics.Pretty
let checkMicrocode expected actual : unit =
assertOkAndEqual
(Set.ofList expected)
(lift Set.ofList (normaliseMicrocode actual))
(printSemanticsError >> printUnstyled)
[<Test>]
let ``microcode normalisation of x[3][i] <- 3; y[j] <- 4 is correct`` () =
checkMicrocode
[ Assign
(TypedVar.Array
(mkArrayTypeRec
(mkArrayType
(Int (normalRec, ()))
(Some 320))
(Some 240),
"x"),
Some <| aupd
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg "x")))
(IInt 3L)
(aupd
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AIdx
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg "x")),
IInt 3L)))
(IVar (Reg "i"))
(normalIntExpr (IInt 3L))))
Assign
(TypedVar.Array
(mkArrayTypeRec (Int (normalRec, ())) (Some 320), "y"),
Some <| aupd
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AVar (Reg "y")))
(IVar (Reg "j"))
(normalIntExpr (IInt 4L))) ]
[ Assign
(normalIntExpr
(IIdx
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AIdx
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg "x")),
IInt 3L)),
IVar (Reg "i"))),
Some (normalIntExpr (IInt 3L)))
Assign
(normalIntExpr
(IIdx
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AVar (Reg "y")),
(IVar (Reg "j")))),
Some (normalIntExpr (IInt 4L))) ]
[<Test>]
let ``microcode normalisation of CAS(x[3], d[6], 2) is correct`` () =
let xel = Int (normalRec, ())
let xlen = Some 10
let xname = "x"
let xtr = mkArrayTypeRec xel xlen
let xar = Array (xtr, xname)
let xav = mkTypedSub xtr (AVar (Reg xname))
let x3 = IIdx (xav, IInt 3L)
let x3upd v = aupd xav (IInt 3L) v
let del = Int (normalRec, ())
let dlen = Some 10
let dname = "d"
let dtr = mkArrayTypeRec del dlen
let dar = Array (dtr, dname)
let dav = mkTypedSub dtr (AVar (Reg dname))
let d6 = IIdx (dav, IInt 6L)
let d6upd v = aupd dav (IInt 6L) v
checkMicrocode
[ Branch
(iEq x3 d6,
[ xar *<- x3upd (normalIntExpr (IInt 2L))
dar *<- d6upd (normalIntExpr d6) ],
[ xar *<- x3upd (normalIntExpr x3)
dar *<- d6upd (normalIntExpr x3) ])
]
[ Branch
(iEq x3 d6,
[ normalIntExpr x3 *<- normalIntExpr (IInt 2L)
normalIntExpr d6 *<- normalIntExpr d6 ],
[ normalIntExpr x3 *<- normalIntExpr x3
normalIntExpr d6 *<- normalIntExpr x3 ])
]
let testShared =
Map.ofList
[ ("grid",
mkArrayType
(mkArrayType (Type.Int (normalRec, ())) (Some 320))
(Some 240))
("test", Type.Bool (normalRec, ())) ]
let testShared2 =
Map.ofList
[ ("foo", mkArrayType (Type.Bool (normalRec, ())) (Some 10)) ]
let testThread =
Map.ofList
[ ("x", Type.Int (normalRec, ()))
("y", Type.Int (normalRec, ())) ]
/// <summary>
/// Tests for microcode processing.
/// </summary>
module ProcessMicrocode =
/// <summary>
/// Checks a microcode instruction set against an expected
/// processed instruction set and assignment map.
/// </summary>
/// <param name="cap">Whether to cap final intermediates.</param>
/// <param name="svars">The context shared variables.</param>
/// <param name="tvars">The context thread variables.</param>
/// <param name="expectedMap">The expected assignment map.</param>
/// <param name="expectedInstrs">The expected final instructions.</param>
/// <param name="instrs">The instructions to convert.</param>
let check (cap : bool) (svars : VarMap) (tvars : VarMap)
(expectedMap : Map<TypedVar, MarkedVar>)
(expectedInstrs : Microcode<CTyped<MarkedVar>, Sym<MarkedVar>> list)
(instrs : Microcode<Expr<Sym<Var>>, Sym<Var>> list)
: unit =
let svars = VarMap.toTypedVarSeq svars
let tvars = VarMap.toTypedVarSeq tvars
let vars = List.ofSeq (Seq.append svars tvars)
let proc =
if cap
then processMicrocodeRoutine
else processMicrocodeRoutineUncapped
let processedR = proc vars instrs
assertOkAndEqual
(expectedInstrs, expectedMap)
processedR
(Pretty.printSemanticsError >> Core.Pretty.printUnstyled)
[<Test>]
let ``chained assigns are processed properly`` () =
check false ticketLockModel.SharedVars ticketLockModel.ThreadVars
(Map.ofList
[ (Int (normalRec, "t"), Intermediate (0I, "t"))
(Int (normalRec, "s"), Intermediate (0I, "s"))
(Int (normalRec, "serving"), Before "serving")
(Int (normalRec, "ticket"), Before "ticket") ] )
[ Int (normalRec, Intermediate (0I, "t")) *<- normalIntExpr (siBefore "serving")
Int (normalRec, Intermediate (0I, "s")) *<- normalIntExpr (IVar (Reg (Intermediate (0I, "t")))) ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "serving")
normalIntExpr (siVar "s") *<- normalIntExpr (siVar "t") ]
[<Test>]
let ``chained assigns are processed and capped properly`` () =
check true ticketLockModel.SharedVars ticketLockModel.ThreadVars
(Map.ofList
[ (Int (normalRec, "t"), After "t")
(Int (normalRec, "s"), After "s")
(Int (normalRec, "serving"), Before "serving")
(Int (normalRec, "ticket"), Before "ticket") ] )
[ Int (normalRec, After "t") *<- normalIntExpr (siBefore "serving")
Int (normalRec, After "s") *<- normalIntExpr (siAfter "t") ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "serving")
normalIntExpr (siVar "s") *<- normalIntExpr (siVar "t") ]
[<Test>]
let ``unequal branches are processed and capped properly`` () =
check true ticketLockModel.SharedVars ticketLockModel.ThreadVars
(Map.ofList
[ (Int (normalRec, "t"), After "t")
(Int (normalRec, "s"), After "s")
(Int (normalRec, "serving"), Before "serving")
(Int (normalRec, "ticket"), Before "ticket") ] )
[ Branch
(BEq (normalIntExpr (siBefore "t"), normalIntExpr (siBefore "ticket")),
[ Int (normalRec, After "t") *<- normalIntExpr (siBefore "serving")
Int (normalRec, After "s") *<- normalIntExpr (siBefore "s") ],
[ Int (normalRec, After "s") *<- normalIntExpr (siBefore "serving")
Int (normalRec, After "t") *<- normalIntExpr (siBefore "t") ]) ]
[ Branch
(BEq (normalIntExpr (siVar "t"), normalIntExpr (siVar "ticket")),
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "serving") ],
[ normalIntExpr (siVar "s") *<- normalIntExpr (siVar "serving") ]) ]
/// <summary>
/// Tests for microcode compilation to Boolean expressions.
/// </summary>
module MicrocodeToBool =
/// <summary>
/// Checks a microcode instruction set against an expected
/// Boolean expression.
/// </summary>
/// <param name="svars">The context shared variables.</param>
/// <param name="tvars">The context thread variables.</param>
/// <param name="expected">The expected expression.</param>
/// <param name="instrs">The instructions to convert.</param>
let check
(svars : VarMap)
(tvars : VarMap)
(expected : BoolExpr<Sym<MarkedVar>> list)
(instrs : Microcode<Expr<Sym<Var>>, Sym<Var>> list)
: unit =
// Try to make the check order-independent.
let rec trySort bool =
match bool with
| BAnd xs -> BAnd (List.sort (List.map trySort xs))
| BOr xs -> BOr (List.sort (List.map trySort xs))
| x -> x
let svars = VarMap.toTypedVarSeq svars
let tvars = VarMap.toTypedVarSeq tvars
let vars = List.ofSeq (Seq.append svars tvars)
let processedR = processMicrocodeRoutine vars instrs
let microcodeR = lift (uncurry microcodeRoutineToBool) processedR
assertOkAndEqual
(BAnd (List.sort (List.map trySort expected)))
(lift trySort microcodeR)
(Pretty.printSemanticsError >> Core.Pretty.printUnstyled)
[<Test>]
let ``the empty command is translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siAfter "t") (siBefore "t") ]
[]
[<Test>]
let ``lone assumes are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siAfter "t") (siBefore "t")
iEq (siBefore "s") (siBefore "t") ]
[ Assume (iEq (siVar "s") (siVar "t")) ]
[<Test>]
let ``lone havocs are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "t") (siBefore "t") ]
[ havoc (normalIntExpr (siVar "s")) ]
[<Test>]
let ``lone assigns are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "t")
iEq (siAfter "t") (siBefore "t") ]
[ normalIntExpr (siVar "s") *<- normalIntExpr (siVar "t") ]
[<Test>]
let ``chained assigns are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siAfter "t")
iEq (siAfter "t") (siBefore "serving") ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "serving")
normalIntExpr (siVar "s") *<- normalIntExpr (siVar "t") ]
[<Test>]
let ``chained assigns and assumptions are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siBefore "s") (siAfter "t")
iEq (siAfter "t") (siBefore "serving") ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "serving")
Assume (iEq (siVar "s") (siVar "t")) ]
[<Test>]
let ``well-formed branches are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
(mkImplies
(iEq (siBefore "s") (siBefore "t"))
(iEq (siAfter "t") (siBefore "serving")))
(mkImplies
(mkNot (iEq (siBefore "s") (siBefore "t")))
(iEq (siAfter "t") (siBefore "ticket"))) ]
[ Branch
(iEq (siVar "s") (siVar "t"),
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "serving") ],
[ normalIntExpr (siVar "t") *<- normalIntExpr (siVar "ticket") ]) ]
[<Test>]
let ``long compositions are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siInter 0I "t") (mkAdd2 (siBefore "t") (IInt 1L))
iEq (siInter 1I "t") (mkAdd2 (siInter 0I "t") (IInt 1L))
iEq (siAfter "t") (mkAdd2 (siInter 1I "t") (IInt 1L)) ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L))
normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L))
normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L)) ]
[<Test>]
let ``pre-state havocs are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siInter 1I "t") (mkAdd2 (siInter 0I "t") (IInt 1L))
iEq (siAfter "t") (mkAdd2 (siInter 1I "t") (IInt 1L)) ]
[ havoc (normalIntExpr (siVar "t"))
normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L))
normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L)) ]
[<Test>]
let ``intermediate havocs are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siInter 0I "t") (mkAdd2 (siBefore "t") (IInt 1L))
iEq (siAfter "t") (mkAdd2 (siInter 1I "t") (IInt 1L)) ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L))
havoc (normalIntExpr (siVar "t"))
normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L)) ]
[<Test>]
let ``post-state havocs are translated properly`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siInter 0I "t") (mkAdd2 (siBefore "t") (IInt 1L))
iEq (siInter 1I "t") (mkAdd2 (siInter 0I "t") (IInt 1L)) ]
[ normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L))
normalIntExpr (siVar "t") *<- normalIntExpr (mkAdd2 (siVar "t") (IInt 1L))
havoc (normalIntExpr (siVar "t")) ]
[<Test>]
let ``single-dimensional arrays are normalised and translated properly`` () =
check testShared2 testThread
[ iEq (siAfter "x") (siBefore "x")
iEq (siAfter "y") (siBefore "y")
BEq
(typedArrayToExpr
(mkTypedArrayExpr (Bool (normalRec, ())) (Some 10) (AVar (Reg (Intermediate (0I, "foo"))))),
aupd
(mkTypedArrayExpr
(Bool (normalRec, ()))
(Some 10)
(AVar (Reg (Before "foo"))))
(siBefore "x")
(normalBoolExpr BTrue))
BEq
(typedArrayToExpr
(mkTypedArrayExpr (Bool (normalRec, ())) (Some 10) (AVar (Reg (After "foo")))),
aupd
(mkTypedArrayExpr
(Bool (normalRec, ()))
(Some 10)
(AVar (Reg (Intermediate (0I, "foo")))))
(siBefore "y")
(normalBoolExpr BFalse)) ]
[ normalBoolExpr
(BIdx
(mkTypedArrayExpr (Bool (normalRec, ())) (Some 10) (AVar (Reg "foo")),
IVar (Reg "x")))
*<- normalBoolExpr BTrue
normalBoolExpr
(BIdx
(mkTypedArrayExpr (Bool (normalRec, ())) (Some 10) (AVar (Reg "foo")),
IVar (Reg "y")))
*<- normalBoolExpr BFalse ]
[<Test>]
let ``multi-dimensional arrays are normalised and translated properly`` () =
let grid marker =
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg (marker "grid"))))
let gridx marker =
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AIdx (grid marker, IVar (Reg (marker "x")))))
let gridv =
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg "grid")))
let gridxv =
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AIdx (gridv, siVar "x")))
check testShared testThread
[ iEq (siAfter "x") (siBefore "x")
iEq (siAfter "y") (siBefore "y")
bEq (sbAfter "test") (sbBefore "test")
BEq
(typedArrayToExpr (grid After),
aupd (grid Before) (siBefore "x")
(aupd (gridx Before) (siBefore "y")
(normalIntExpr <| mkAdd2
(IIdx ((gridx Before), siBefore "y"))
(IInt 1L)))) ]
[ normalIntExpr
(IIdx (gridxv, IVar (Reg "y")))
*<-
normalIntExpr
(mkAdd2
(IIdx (gridxv, siVar "y"))
(IInt 1L)) ]
module CommandTests =
let check svars tvars command expectedValues =
let actualValues =
command
|> semanticsOfCommand
(Starling.Lang.Modeller.coreSemantics)
svars
tvars
|> lift (function
| { Semantics = BAnd xs } -> xs |> Set.ofList |> Some
| _ -> None)
let pError =
Starling.Semantics.Pretty.printSemanticsError
>> Starling.Core.Pretty.printUnstyled
assertOkAndEqual expectedValues actualValues pError
[<Test>]
let ``Semantically translate <assume(s == t)> using the ticket lock model`` () =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ command "Assume" [] [ normalBoolExpr <| iEq (siVar "s") (siVar "t") ]]
<| Some (Set.ofList
[ iEq (siAfter "serving") (siBefore "serving")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "s") (siBefore "s")
iEq (siAfter "t") (siBefore "t")
iEq (siBefore "s") (siBefore "t") ])
[<Test>]
let ``Semantically translate <grid[x][y]++> using the test environments``() =
let grid marker =
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg (marker "grid"))))
let gridx marker =
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AIdx (grid marker, IVar (Reg (marker "x")))))
let gridv =
(mkTypedArrayExpr
(mkArrayType (Int (normalRec, ())) (Some 320))
(Some 240)
(AVar (Reg "grid")))
let gridxv =
(mkTypedArrayExpr
(Int (normalRec, ()))
(Some 320)
(AIdx (gridv, siVar "x")))
check testShared testThread
[ command "!I++"
[ normalIntExpr (IIdx (gridxv, siVar "y")) ]
[ normalIntExpr (IIdx (gridxv, siVar "y")) ] ]
<| Some (Set.ofList
[ iEq (siAfter "x") (siBefore "x")
iEq (siAfter "y") (siBefore "y")
bEq (sbAfter "test") (sbBefore "test")
BEq
(typedArrayToExpr (grid After),
aupd (grid Before) (siBefore "x")
(aupd (gridx Before) (siBefore "y")
(normalIntExpr <| mkAdd2
(IIdx (gridx Before, siBefore "y"))
(IInt 1L)))) ] )
[<Test>]
let ``Semantically translate <%{foo([|serving|]}> using the ticket lock model``() =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ SymC
[ SymString "foo("
SymArg (normalIntExpr (siVar "serving") )
SymString ")" ] ]
<| Some (Set.ofList
[
iEq (siAfter "s") (siBefore "s")
iEq (siAfter "t") (siBefore "t")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "serving") (siBefore "serving")
BVar (
Sym
[ SymString "foo("
SymArg (normalIntExpr (siBefore "serving") )
SymString ")" ]
)
])
[<Test>]
let ``Semantically translate <serving++> using the ticket lock model``() =
check ticketLockModel.SharedVars ticketLockModel.ThreadVars
[ command "!I++" [ normalIntExpr (siVar "serving") ] [ normalIntExpr <| siVar "serving" ] ]
<| Some (Set.ofList
[
iEq (siAfter "s") (siBefore "s")
iEq (siAfter "t") (siBefore "t")
iEq (siAfter "ticket") (siBefore "ticket")
iEq (siAfter "serving") (mkAdd2 (siBefore "serving") (IInt 1L))
])