-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.ml
239 lines (193 loc) · 6.66 KB
/
ast.ml
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
(* variables *)
type var = string
type channel = char
(* (E, id), N, (rho, id) *)
type mode = Enclave of var | Normal | ModeVar of var * var
module VarSet = Set.Make(struct
type t = var
let compare = Stdlib.compare
end)
(* sets of condition variables *)
type cndset = VarSet.t
(* set of enclave identifiers *)
type eidset = VarSet.t
type varloc = Reg of var | Mem of int
(* maps with variables and locations as keys *)
module VarLocMap = Map.Make(struct
type t = varloc
let compare = Stdlib.compare
end)
(* Base types *)
type basetype =
BtInt (* int *)
| BtBool (* bool *)
| BtCond (* cond *)
| BtRef of labeltype (* tau ref *)
| BtFunc of context * policy * cndset * context (* func *)
and
labeltype = basetype * policy
and policy =
Low
|High
|Top
|Erase of policy * var * policy
(* expressions *)
and exp =
Var of var (* x *)
| Loc of int
| Lam of context * policy * cndset * context * policy * stmt (* (lambda(G_pre, p, {}, G_post).stmt)_q *)
| Constant of int (* n *)
| Plus of exp * exp (* e1 + e2 *)
| Modulo of exp * exp (* e1 % e2 *)
| True (* true *)
| False (* false *)
| Eq of exp * exp (* e1 = e2 *)
| Neq of exp * exp (* e1 != e2 *)
| Deref of exp
| Isunset of var
and
stmt =
If of exp * stmt * stmt (* if e1 then e2 else e3 *)
| Skip
| Assign of var * exp
| Declassify of var * exp
| Update of exp * exp
| Seq of stmt * stmt
| While of exp * stmt
| Output of channel * exp
| Call of exp
| Set of var
(* typechecking environments - maps variables to types *)
and context = labeltype VarLocMap.t
(* values *)
type value =
VInt of int
| VBool of bool
| VFun of stmt
| VLoc of int
(* evaluation environments *)
type env = value VarLocMap.t
(* Encalve Base types *)
type encbasetype =
EBtInt (* int *)
| EBtBool (* bool *)
| EBtCond of mode (* cond *)
| EBtRef of mode * enclabeltype (* tau ref *)
| EBtFunc of mode* enccontext* policy * cndset * enccontext (* func *)
and
enclabeltype = encbasetype * policy
(* typechecking environments - maps variables to types *)
and enccontext = enclabeltype VarLocMap.t
type encexp =
EVar of mode * var (* mode |- x *)
| ELoc of mode * mode * int (* mode |- l^ mode *)
| ELam of mode * mode * enccontext * policy* cndset * enccontext * policy* encstmt (* First mode|-lambda^mode(gpre, p,u, gpost)_q *)
| EConstant of mode * int (* n *)
| EPlus of mode * encexp * encexp (* e1 + e2 *)
| EModulo of mode * encexp * encexp (* e1 % e2 *)
| ETrue of mode (* true *)
| EFalse of mode (* false *)
| EEq of mode * encexp * encexp (* e1 = e2 *)
| ENeq of mode * encexp * encexp (* e1 != e2 *)
| EDeref of mode * encexp
| EIsunset of mode * var
(* Translation data structure. Each Statement has an associated mode in which it executes *)
and encstmt =
EIf of mode*encexp * encstmt * encstmt * var (* if e1 then e2 else e3 *)
|ESkip of mode * mode * var
|EAssign of mode * var * encexp * var
|EDeclassify of mode * var * encexp * var
|EUpdate of mode * encexp * encexp * var
|ESeq of mode * encstmt * encstmt * var
|EESeq of mode * (encstmt list) * var
|EWhile of mode * encexp * encstmt * var
|EOutput of mode* channel * encexp * var
|ECall of mode * encexp * var
|ESet of mode * var * var
type progbody = Exp of exp | Stmt of stmt | EncExp of encexp
type mode_cond= (mode * mode)
type eid_cond = (var * int)
type kill_cond = (var * int)
type constr_cond =
| Modecond of mode_cond (* Represents (rho, id) = (Enclave, i) *)
| Eidcond of eid_cond (* Represents (b_ij = 0/1 *)
| Killcond of kill_cond (* Represents alpha = 0/1 *)
| Cnfclause of constr_cond list (* Represents (rho, id) \/ (rho2, id2) \/ (rho3, id3) *)
(* sets of pairs of types *)
module Constr = Set.Make(struct
type t = constr_cond
let compare = Stdlib.compare
end)
type pre_cond =
|Premodecond of mode_cond (* E.g: x = E -> *)
|Preeidcond of eid_cond * eid_cond (* bij = 0 /\ bjk = 0 -> *)
|Prekillcond of kill_cond * mode_cond (* alpha = 0/1 /\ x = E -> *)
|Prekillexitcond of mode_cond * mode_cond (* x1 = E /\ x2 = N -> *)
module Constr2 = Set.Make(struct
type t = pre_cond * constr_cond
let compare = Stdlib.compare
end)
(* constraints *)
type constr = Constr.t
(* Conditional constrains *)
type constr2 = Constr2.t
(* maps with mode variables as keys *)
module ModeVarMap = Map.Make(struct
type t = var
let compare = Stdlib.compare
end)
(* mode substitutions *)
type subst = mode ModeVarMap.t
type program = context * stmt
(* maps with mode variables as keys *)
module ModeProgSet = Set.Make(struct
type t = mode * progbody
let compare = Stdlib.compare
end)
(* evaluation environments *)
type modeenv = ModeProgSet.t
(* Set of mode variables *)
module ModeSet = Set.Make(struct
type t = mode
let compare = Stdlib.compare
end)
type modeset = ModeSet.t
type costvar =
| Mode of mode
| Eid of var
| Kvar of var
(* Polynomial representation for cost function *)
type polyterm =
| Mono of costvar
| Poly of costvar * polyterm
type polynomial =
|PConstant of int (* e.g., 42 *)
|PMonoterm of int * polyterm (* e.g., 42*x_1*x_2 *)
|PUminusterm of int * polyterm (* e.g., -42*x_1*x_2 *)
|PPlus of polynomial * polynomial (* 1 + 42*x_1*x_2 *)
|PMinus of polynomial * polynomial (* 1 - 42*x_1*x_2 *)
type cost = polynomial
type totalcost = polynomial*polynomial
(* Mode SAT *)
module ModeSAT = Map.Make(struct
type t = costvar
let compare = Stdlib.compare
end)
type modesat = int ModeSAT.t
(* Map bij to modes *)
module EnclaveidMap = Map.Make(struct
type t = var
let compare = Stdlib.compare
end)
type modepair = (mode * mode )
type enclaveidmap = modepair EnclaveidMap.t
module EnclaveidRevMap = Map.Make(struct
type t = modepair
let compare = Stdlib.compare
end)
type enclaveidrevmap = var EnclaveidRevMap.t
module EnclaveidConstraints = Map.Make(struct
type t = mode
let compare = Stdlib.compare
end)
type enclaveidconstraints = eidset EnclaveidConstraints.t