-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.hs
49 lines (42 loc) · 1.08 KB
/
Main.hs
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
module Main where
import Types
import ToDot
nop = Arith $ \s -> [s]
pushI i = Arith $ \s -> [IntValue i:s]
pushB b = Arith $ \s -> [BoolValue b:s]
unopI op = Arith $ \(IntValue a:s) -> [IntValue (op a):s]
unopB op = Arith $ \(BoolValue a:s) -> [BoolValue (op a):s]
cmp op = Arith $ \(IntValue b:IntValue a:s) -> [BoolValue (op a b):s]
-- while(true) v++
incrementer v = compile [
Label "loop",
Get v,
unopI (+1),
Set v,
Jmp "loop"
]
-- while(true) {lock(mon) {if(v1 <= v2) v1++}}
syncIncrementer v1 v2 mon = compile [
Label "loop",
Enter mon,
Get v1,
Get v2,
cmp (<=),
JmpCond "ok",
Jmp "leave",
Label "ok",
Get v1,
unopI (+1),
Set v1,
Label "leave",
Leave mon,
Jmp "loop"
]
main1 = initState [("a", IntValue 1), ("b", IntValue 1)] [] (compile [
Spawn "a" (incrementer "a"),
Spawn "b" (incrementer "b")
])
main2 = initState [("a", IntValue 1), ("b", IntValue 1)] ["m"] (compile [
Spawn "a" (syncIncrementer "a" "b" "m")
,Spawn "b" (syncIncrementer "b" "a" "m")
])