Skip to content

Commit cc0dd3f

Browse files
authored
Merge pull request #84 from ssoelvsten/fork/dev/lib-cleanup
lib/ cleanup
2 parents bded471 + 93ad06a commit cc0dd3f

File tree

232 files changed

+1028
-2412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

232 files changed

+1028
-2412
lines changed

compiler/src/AddAmbientMethods.hs

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,61 @@
11
-- 2020-05-17, AA
22

33
-- HACK
4-
-- This module add a number of standard
5-
-- ambient methods such as `print` to the
6-
-- beginning of the file. This provides some
7-
-- backward compatibility with prior test cases
8-
-- as well as minimizes some clutter
9-
10-
-- If these methods are unused they are
11-
-- eliminated by the optimization passes in
12-
-- the further passes.
13-
14-
module AddAmbientMethods(addAmbientMethods) where
4+
--
5+
-- This module add a number of standard ambient methods such as `print` to the beginning of the
6+
-- file. This provides some backward compatibility with prior test cases as well as minimizes some
7+
-- clutter.
8+
--
9+
-- If these methods are unused they are eliminated by the optimization passes in the further passes.
10+
11+
-- TODO
12+
--
13+
-- Move this into a '.trp' file of the form
14+
--
15+
-- ```
16+
-- let fun print x = fwrite (stdout authority, (toString x) ^"\n")
17+
-- ...
18+
-- in () end
19+
-- ```
20+
--
21+
-- Which, similar to below, after parsing has the `dummy` value replaced by the actual program. This
22+
-- preamble can then be specified at compile-time.
23+
24+
module AddAmbientMethods(addAmbientMethods) where
1525

1626
import Basics
17-
import Direct
27+
import Direct
1828
import TroupePositionInfo
1929

20-
printDecl :: FunDecl
21-
printDecl = FunDecl "print"
30+
printStringDecl :: FunDecl
31+
printStringDecl = FunDecl "printString"
2232
[Lambda [VarPattern "x"] $
23-
Let [ValDecl (VarPattern "out") (App (Var "getStdout") [Var "authority"]) NoPos]
24-
(App (Var "fprintln") [Tuple [Var "out", Var "x"]])
33+
Let [ ValDecl (VarPattern "fd") (App (Var "stdout") [Var "authority"]) NoPos
34+
, ValDecl (VarPattern "x'") (Bin Concat (Var "x") (Lit $ LString "\\n")) NoPos
35+
]
36+
(App (Var "fwrite") [Tuple [Var "fd", Var "x'"]])
2537
] NoPos
2638

27-
printWithLabelsDecl :: FunDecl
28-
printWithLabelsDecl = FunDecl "printWithLabels"
29-
[Lambda [VarPattern "x"] $
30-
Let [ValDecl (VarPattern "out") (App (Var "getStdout") [Var "authority"]) NoPos]
31-
(App (Var "fprintlnWithLabels") [Tuple [Var "out", Var "x"]])
39+
printDecl :: FunDecl
40+
printDecl = FunDecl "print"
41+
[Lambda [ VarPattern "x" ] $
42+
(App (Var "printString") [App (Var "toString") [Var "x"]])
3243
] NoPos
3344

34-
35-
printStringDecl :: FunDecl
36-
printStringDecl = FunDecl "printString"
37-
[Lambda [VarPattern "x"] $
38-
Let [ValDecl (VarPattern "out") (App (Var "getStdout") [Var "authority"]) NoPos]
39-
(App (Var "fwrite") [Tuple [Var "out", Bin Concat (Var "x") (Lit (LString "\\n"))]])
45+
printLDecl :: FunDecl
46+
printLDecl = FunDecl "printL"
47+
[Lambda [ VarPattern "x" ] $
48+
(App (Var "printString") [App (Var "toStringL") [Var "x"]])
4049
] NoPos
4150

51+
inputLineDecl :: FunDecl
52+
inputLineDecl = FunDecl "inputLine"
53+
[Lambda [ VarPattern "_" ] $
54+
Let [ValDecl (VarPattern "fd") (App (Var "stdin") [Var "authority"]) NoPos]
55+
(App (Var "freadln") [App (Var "stdin") [Var "authority"]])
56+
] NoPos
4257

43-
44-
addAmbientMethods :: Prog -> Prog
45-
addAmbientMethods (Prog imports atoms t) =
46-
let t' = Let [FunDecs [printDecl,printWithLabelsDecl,printStringDecl]] t
47-
in Prog imports atoms t'
58+
addAmbientMethods :: Prog -> Prog
59+
addAmbientMethods (Prog imports atoms t) =
60+
let t' = Let [FunDecs [printStringDecl,printDecl,printLDecl,inputLineDecl]] t
61+
in Prog imports atoms t'

compiler/src/IR.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ instance WellFormedIRCheck IRExpr where
261261
-- code over wire. Such malformed code would result
262262
-- in a JS output returning a runtime error (which should
263263
-- generally be avoided)
264-
if fname `elem`[
264+
if fname `elem`[
265265
"$$authorityarg"
266266
, "adv"
267267
, "ladv"
@@ -285,16 +285,13 @@ instance WellFormedIRCheck IRExpr where
285285
, "endorse"
286286
, "floor"
287287
, "flowsTo"
288-
, "fprintln"
289-
, "fprintlnWithLabels"
290-
, "fwrite"
288+
, "freadln"
289+
, "fwrite"
291290
, "getTime"
292291
, "getType"
293292
, "getNanoTime"
294-
, "getStdout"
295293
, "_getSystemProcess"
296294
, "guard"
297-
, "inputLine"
298295
, "intToString"
299296
, "listToTuple"
300297
, "lowermbox"
@@ -305,13 +302,13 @@ instance WellFormedIRCheck IRExpr where
305302
, "newlabel"
306303
, "node"
307304
, "_pc"
305+
, "_bl"
308306
, "pcpop"
309307
, "peek"
310308
, "pinipush"
311309
, "pinipushto"
312310
, "pinipop"
313311
, "pcpush"
314-
, "question"
315312
, "raisembox"
316313
, "raiseTrust"
317314
, "random"
@@ -333,6 +330,9 @@ instance WellFormedIRCheck IRExpr where
333330
, "spawn"
334331
, "sqrt"
335332
, "substring"
333+
, "stdin"
334+
, "stdout"
335+
, "stderr"
336336
, "stringToInt"
337337
, "strlen"
338338
, "restore"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import List
2-
printWithLabels (List.map ( fn i => i + 1) [1,2,3])
2+
printL (List.map ( fn i => i + 1) [1,2,3])
33

44

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let fun foo () =
2-
receive [hn x => printWithLabels ("foo received", x)]
2+
receive [hn x => printL ("foo received", x)]
33
val p = spawn foo
44
in send (p, "hello")
55
end

examples/fromuserguide/basic_spawn.trp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import List
2-
let fun printwait x = let val _ = printWithLabels x in sleep 10 end
2+
let fun printwait x = let val _ = printL x in sleep 10 end
33
fun foo () = List.map printwait [1,2,3]
44
fun bar () = List.map printwait ["A", "B", "C"]
55
in (spawn foo, spawn bar)

examples/fromuserguide/basic_updateableserver.trp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import timeout
1+
import ThreadUtil
2+
23
let fun v_one n =
34
receive [ hn ("REQUEST", senderid) =>
45
let val _ = send (senderid, n)
@@ -26,7 +27,8 @@ let fun v_one n =
2627
val _ = send (service, ("UPDATE", v_two))
2728
val _ = send (service, ("COMPUTE", self(), fn x => x * x, 42))
2829
val _ = receive [ hn x => print x]
29-
in exitAfterTimeout
30-
authority 1000 0 "force terminating the server example after 1s"
30+
in ThreadUtil.spawnTimeout (fn () => print "force terminating the server example after 1s";
31+
exit (authority, 0))
32+
1000
3133
end
3234

examples/fromuserguide/ifc_type_labels.trp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ let val x = 100 raisedTo `{alice}`
55
val a = if y > 10 then z else "not an integer"
66
in a
77
end
8-
val _ = printWithLabels a
8+
val _ = printL a
99
val _ = debugpc()
1010
val w = a + x
11-
val _ = printWithLabels w
11+
val _ = printL w
1212
in debugpc()
1313
end
1414

lib/DeclassifyUtil.trp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import List
2+
3+
let (* Mutually recursive deep declassification of tuples, lists, ... *)
4+
5+
(* Declassification for (most) tuples *)
6+
fun declassify2 ((x,y), a, lev) =
7+
( declassifyDeep (x, a, lev)
8+
, declassifyDeep (y, a, lev))
9+
10+
and declassify3 ((x,y,z), a, lev) =
11+
( declassifyDeep (x, a, lev)
12+
, declassifyDeep (y, a, lev)
13+
, declassifyDeep (z, a, lev))
14+
15+
and declassify4 ((x,y,z,w), a, lev) =
16+
( declassifyDeep (x, a, lev)
17+
, declassifyDeep (y, a, lev)
18+
, declassifyDeep (z, a, lev)
19+
, declassifyDeep (w, a, lev))
20+
21+
and declassify5 ((x1,x2,x3,x4,x5), a, lev) =
22+
( declassifyDeep (x1, a, lev)
23+
, declassifyDeep (x2, a, lev)
24+
, declassifyDeep (x3, a, lev)
25+
, declassifyDeep (x4, a, lev)
26+
, declassifyDeep (x5, a, lev)
27+
)
28+
29+
and declassify6 ((x1,x2,x3,x4,x5,x6), a, lev) =
30+
( declassifyDeep (x1, a, lev)
31+
, declassifyDeep (x2, a, lev)
32+
, declassifyDeep (x3, a, lev)
33+
, declassifyDeep (x4, a, lev)
34+
, declassifyDeep (x5, a, lev)
35+
, declassifyDeep (x6, a, lev)
36+
)
37+
38+
and declassify7 ((x1,x2,x3,x4,x5,x6,x7), a, lev) =
39+
( declassifyDeep (x1, a, lev)
40+
, declassifyDeep (x2, a, lev)
41+
, declassifyDeep (x3, a, lev)
42+
, declassifyDeep (x4, a, lev)
43+
, declassifyDeep (x5, a, lev)
44+
, declassifyDeep (x6, a, lev)
45+
, declassifyDeep (x7, a, lev)
46+
)
47+
48+
and declassify8 ((x1,x2,x3,x4,x5,x6,x7,x8), a, lev) =
49+
( declassifyDeep (x1, a, lev)
50+
, declassifyDeep (x2, a, lev)
51+
, declassifyDeep (x3, a, lev)
52+
, declassifyDeep (x4, a, lev)
53+
, declassifyDeep (x5, a, lev)
54+
, declassifyDeep (x6, a, lev)
55+
, declassifyDeep (x7, a, lev)
56+
, declassifyDeep (x8, a, lev)
57+
)
58+
59+
and declassify9 ((x1,x2,x3,x4,x5,x6,x7,x8,x9), a, lev) =
60+
( declassifyDeep (x1, a, lev)
61+
, declassifyDeep (x2, a, lev)
62+
, declassifyDeep (x3, a, lev)
63+
, declassifyDeep (x4, a, lev)
64+
, declassifyDeep (x5, a, lev)
65+
, declassifyDeep (x6, a, lev)
66+
, declassifyDeep (x7, a, lev)
67+
, declassifyDeep (x8, a, lev)
68+
, declassifyDeep (x9, a, lev)
69+
)
70+
71+
(* Declassification for lists *)
72+
and declassifyList (xs, a, lev) =
73+
List.map (fn x => declassifyDeep (x, a, lev) ) xs
74+
75+
(* TODO: Declassification of records? *)
76+
77+
(** Deep declassification of value `x` to `level` via the given `authority`. *)
78+
and declassifyDeep (x, authority, level) =
79+
let (* Declassify the blocking label before touching the value. *)
80+
val _ = blockdeclto (authority, level)
81+
82+
(* declassification is a 2-step process:
83+
*
84+
* 1. We pattern match on the given value and figure out which function to apply;
85+
* this choice of the function needs to be declassified itself.
86+
*
87+
* 2. We proceed onto the application of the function. *)
88+
val x' = declassify (x, authority, level)
89+
90+
val f = case x' of (_,_) => declassify2
91+
| (_,_,_) => declassify3
92+
| (_,_,_,_) => declassify4
93+
| (_,_,_,_,_) => declassify5
94+
| (_,_,_,_,_,_) => declassify6
95+
| (_,_,_,_,_,_,_) => declassify7
96+
| (_,_,_,_,_,_,_,_) => declassify8
97+
| (_,_,_,_,_,_,_,_,_) => declassify9
98+
| (_::_) => declassifyList
99+
| _ => declassify
100+
101+
in f(x', authority, level)
102+
end
103+
104+
(*--- Module ---*)
105+
val DeclassifyUtil = {
106+
declassifyDeep
107+
}
108+
109+
in [ ("DeclassifyUtil", DeclassifyUtil) ]
110+
end

lib/Hash.trp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ let
1010
*)
1111
fun hashString s =
1212
(* String hash with fast paths for small strings *)
13-
let val len = strlen s
14-
val radix = 127
15-
fun charCodeAt i = String.subCode (s,i)
13+
let val len = String.size s
14+
val radix = 127
15+
val codeAt = String.sub' s
1616
in case len of 0 => 0
17-
| 1 => charCodeAt 0
18-
| 2 => (radix * charCodeAt 0 + charCodeAt 1) andb Number.maxInt32
17+
| 1 => codeAt 0
18+
| 2 => (radix * codeAt 0 + codeAt 1) andb Number.maxInt32
1919
| _ => let fun go idx acc =
2020
if len <= idx then acc
21-
else go (idx + 1) ((acc * radix + charCodeAt idx) andb Number.maxInt32)
21+
else go (idx + 1) ((acc * radix + codeAt idx) andb Number.maxInt32)
2222
in go 0 0
2323
end
2424
end

lib/List.trp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ let (* -- List Access -- *)
7272
(** Same as `List.map` but `f` is applied to the index of the element as first argument
7373
(counting from 0), and the element itself as second argument. Not tail-recursive. *)
7474
fun mapi f list =
75-
let fun mapj j [] = []
76-
| mapj j x::xs = (f (j,x)) :: (mapj (j+1) xs)
77-
in mapj 0 list
75+
let fun mapi_aux j [] = []
76+
| mapi_aux j x::xs = (f (j,x)) :: (mapi_aux (j+1) xs)
77+
in mapi_aux 0 list
7878
end
7979

8080
(* TODO: revMap *)
@@ -83,6 +83,9 @@ let (* -- List Access -- *)
8383
fun foldl f y [] = y
8484
| foldl f y x::xs = foldl f (f (x,y)) xs
8585

86+
(** Left-fold of `f` on a non-empty list using the head as the initial value. *)
87+
fun foldl1 f x::xs = foldl f x xs
88+
8689
(* TODO: foldr *)
8790

8891
(** Returns the sublist of elements that satisfy `f`. Not tail-recursive. *)
@@ -183,6 +186,7 @@ let (* -- List Access -- *)
183186
map,
184187
mapi,
185188
foldl,
189+
foldl1,
186190
filter,
187191
filteri,
188192
partition,

0 commit comments

Comments
 (0)