Source file parse_ctypes.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
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
module Log = Tracelog.Make(struct let category = "Types.Parse_ctypes" end);;
module In_bytes = Units.In_bytes
module type DATA_MODEL = sig
val short_size : int
val int_size : int
val long_size : int
val long_long_size : int
val pointer_size : int
end
module ILP32:DATA_MODEL = struct
let short_size = 2
let int_size = 4
let long_size = 4
let long_long_size = 8
let pointer_size = 4
end
module LP64:DATA_MODEL = struct
let short_size = 2
let int_size = 4
let long_size = 8
let long_long_size = 8
let pointer_size = 8
end
module LLP64:DATA_MODEL = struct
let short_size = 2
let int_size = 4
let long_size = 4
let long_long_size = 8
let pointer_size = 8
end
let r_data_model : (module DATA_MODEL) option ref = ref None
let the_data_model() = match !r_data_model with
| None -> Log.fatal (fun p -> p "This module needs to be configured with a data model")
| Some x -> x
let short_size() = let module X = (val (the_data_model ())) in X.short_size
let int_size() = let module X = (val (the_data_model ())) in X.int_size
let long_size() = let module X = (val (the_data_model ())) in X.long_size
let long_long_size() = let module X = (val (the_data_model ())) in X.long_long_size
let _pointer_size() = let module X = (val (the_data_model ())) in X.pointer_size
let char_size = 1
let float_size = 4
let double_size = 8
let basic_aliases_common =
[ "int_least8_t", "char"
; "int_least16_t", "short"
; "int_least32_t", "int"
; "int_least64_t", "long long"
; "int8_t", "char"
; "int16_t", "short"
; "int32_t", "int"
; "int64_t", "long long"
; "uint_least8_t", "char"
; "uint_least16_t", "short"
; "uint_least32_t", "int"
; "uint_least64_t", "long long"
; "uint8_t", "char"
; "uint16_t", "short"
; "uint32_t", "int"
; "uint64_t", "long long" ];;
let basic_aliases32 = basic_aliases_common @
[ "int_fast8_t", "char"
; "int_fast16_t", "int"
; "int_fast32_t", "int"
; "int_fast64_t", "long long"
; "intptr_t", "int"
; "uint_fast8_t", "char"
; "uint_fast16_t", "int"
; "uint_fast32_t", "int"
; "uint_fast64_t", "long long"
; "uintptr_t", "int" ];;
let basic_aliases64 = basic_aliases_common @
[ "int_fast8_t", "char"
; "int_fast16_t", "long"
; "int_fast32_t", "long"
; "int_fast64_t", "long"
; "intptr_t", "long"
; "uint_fast8_t", "char"
; "uint_fast16_t", "long"
; "uint_fast32_t", "long"
; "uint_fast64_t", "long"
; "uintptr_t", "long" ];;
module StringHash = Hashtbl.Make(struct include String;; let hash = Hashtbl.hash end)
let aliases_map = StringHash.create 127;;
let populate_basic_aliases aliases =
aliases |> List.iter (fun (key,value) -> StringHash.replace aliases_map key value)
let set_data_model = function
| `ILP32 ->
r_data_model := Some (module ILP32);
populate_basic_aliases basic_aliases32
| `LP64 -> r_data_model := Some (module LP64);
populate_basic_aliases basic_aliases64
| `LLP64 -> r_data_model := Some (module LLP64);
populate_basic_aliases basic_aliases64
module Conversion = struct
let pred_nz = TypedC.Pred.(Cmp (NotEqual, Self, Const Z.zero) )
let infer_spec = ref false
(** Type definition map *)
module ConstrHash = Hashtbl.Make (TypedC.Constr);;
exception Undefined_type_constructor of string
let type_map : (TypedC.typ * string list) ConstrHash.t = ConstrHash.create 17 ;;
let add_type_definition constr def =
let constr_map = type_map in
if ConstrHash.mem constr_map constr then
let res = ConstrHash.find constr_map constr in
if res = def then ()
else
Log.fatal (fun p ->
p "There is already a definition for %a" TypedC.Constr.pp constr)
else ConstrHash.add constr_map constr def
let is_type_definition constr =
ConstrHash.mem type_map constr
let get_type_definition constr =
ConstrHash.find_opt type_map constr
let apply_inlining constr args =
let typ, params = match get_type_definition constr
with Some x -> x | None -> raise (Undefined_type_constructor (TypedC.Constr.to_string constr)) in
let bindings = List.combine params args in
TypedC.substitute_in_type typ bindings
let base name size pred = TypedC.{descr = Base (size,name); pred = pred}
let convert_unop op = assert false
let convert_binop op =
let open Type_parse_tree in
let open TypedC.Pred in
match op with
| Plus -> Add
| Minus -> Sub
| Mult -> Mul
| Bitwise_and -> And
| Bitwise_or -> Or
| Mod -> Mod
| _ -> assert false
let rec convert_expr expr =
let open Type_parse_tree in
match expr with
| Self -> TypedC.Pred.Self
| Cst(n) -> TypedC.Pred.(Const n)
| Var v -> TypedC.Pred.(Val(Sym(v.name)))
| Unary (op, e) -> TypedC.Pred.Unop(convert_unop op, convert_expr e)
| Binary(op, e1, e2) ->
TypedC.Pred.Binop(convert_binop op, convert_expr e1, convert_expr e2)
let binop_to_cmpop op e1 e2 =
let open Type_parse_tree in
match op with
| Eq -> TypedC.Pred.(Cmp(Equal, convert_expr e1, convert_expr e2))
| Diff -> TypedC.Pred.(Cmp(NotEqual, convert_expr e1, convert_expr e2))
| Ge -> TypedC.Pred.(Cmp(SGeq, convert_expr e1, convert_expr e2))
| Gt -> TypedC.Pred.(Cmp(SGt, convert_expr e1, convert_expr e2))
| Le -> TypedC.Pred.(Cmp(SLeq, convert_expr e1, convert_expr e2))
| Lt -> TypedC.Pred.(Cmp(SLt, convert_expr e1, convert_expr e2))
| _ -> TypedC.Pred.true_
let rec convert_predicate pred =
let open Type_parse_tree in
match pred with
| Binary(Logical_and, p1, p2) -> TypedC.Pred.conjunction (convert_predicate p1) (convert_predicate p2)
| Binary(op, e1, e2) -> binop_to_cmpop op e1 e2
| _ -> TypedC.Pred.true_
let convert_type_name = let open Type_parse_tree in function
| TypeName n -> TypedC.ConstrName n
| TypeNameUnion n -> TypedC.ConstrNameUnion n
| TypeNameStruct n -> TypedC.ConstrNameStruct n
| TypeNameEnum n -> assert false
let rec provide_name typ : TypedC.name =
let open TypedC in
let default() = Log.fatal (fun p -> p "Cannot provide a name for %a" pp typ) in
match typ.descr with
| Application {constr;args} ->
assert(Pred.is_true typ.pred);
(assert (not @@ is_type_definition constr)) ;
{constr;args}
| Array(t,len) -> begin
let tname = provide_name t in
let len = match len with
| Fixed_length z -> Pred.Const z
| Variable_length l -> Pred.(Val(Sym l))
in
match tname with
| {constr;args = []} ->
let constr = Constr.make (ConstrNameArray constr.name) 1 in
let symbol = "array_length_param" in
add_constr_definition constr (TypedC.({descr= Array(t,Variable_length symbol);
pred= Pred.true_}), [symbol]);
{constr; args = [len]}
| _ -> default()
end
| Ptr{pointed={descr=Application{constr;args = []}; pred}} when Pred.is_true pred -> begin
match constr.name with
| ConstrName ("char" | "double" as name) ->
if name = "char" then Log.warning (fun p -> p "char* may be ambiguous");
let constr = Constr.make (ConstrName (name ^ "star")) 0 in
add_constr_definition constr (typ,[]);
{constr;args = []}
| ConstrNameStruct name -> let constr = Constr.make (ConstrName (name ^ "_s") ) 0 in
add_constr_definition constr (typ,[]);
{constr;args = []}
| ConstrName _
| ConstrNameFunc _
| ConstrNameUnion _
| ConstrNameEnum _
| ConstrNameArray _ -> default ()
end
| Void -> begin
let constr = Constr.make (ConstrName "void") 0 in
Log.warning (fun p -> p "Should not use void* pointer in spec");
add_constr_definition constr TypedC.({descr=Void;pred=Pred.true_},[]);
{constr; args = []}
end
| Base _
| Structure _
| StructureFAM _
| Ptr _
| Function _
| Existential _
| Union _
| Weak _ -> default ()
let rec convert_type_pred typ pred =
let open Type_parse_tree in
let open TypedC in
match typ with
| Struct fields -> { descr = convert_struct fields; pred = pred; }
| Name (TypeName n) when StringHash.mem aliases_map n ->
let rename = StringHash.find aliases_map n in
convert_type_pred (Name (TypeName rename)) pred
| Name (TypeName "word8") -> base "word8" (In_bytes.of_int 8) pred
| Name (TypeName "integer") -> base "integer" (In_bytes.of_int @@ int_size()) pred
| Name (TypeName "word4") -> base "word4" (In_bytes.of_int 4) pred
| Name (TypeName "word2") -> base "word2" (In_bytes.of_int 2) pred
| Name (TypeName "word1") -> base "word1" (In_bytes.of_int 1) pred
| Name (TypeName "void") -> {descr = Void; pred = Pred.true_}
| Name n ->
let constr = Constr.make (convert_type_name n) 0 in
if is_type_definition constr then
let typ = apply_inlining constr [] in
{typ with pred = TypedC.Pred.conjunction typ.pred pred}
else {descr = Application{constr = constr; args = []}; pred = pred}
| Pointer(t,ptr_annot) ->
let name = provide_name @@ convert_type t in
let typ = Build.ptr_to_name name in
let pred = match ptr_annot with
| Maybe_null ->
if !infer_spec then
(Pred.conjunction (Pred.init_mutval pred_nz) pred) else pred
| Non_null -> (Pred.conjunction pred_nz pred)
| Null_or_non_null -> pred in
Build.refine typ pred
| Array(t,e) ->
let elem_typ = convert_type t in
begin match e with
| Cst len -> {descr = Array (elem_typ, Fixed_length len); pred = pred}
| Var symb -> {descr = Array (elem_typ,Variable_length symb.name); pred = pred}
| _ ->
let expr = convert_predicate e in Log.debug (fun p -> p "invalid array size expression : %a" Pred.pp expr);
assert false
end
| Applied(LambdaAlias cons, exprs) ->
let args = List.map convert_expr exprs in
let constr = Constr.make (convert_type_name cons) (List.length args) in
if is_type_definition constr then
let typ = apply_inlining constr args in
{typ with pred = TypedC.Pred.conjunction typ.pred pred}
else {descr = Application{constr; args}; pred=pred }
| Exists (v, tv, t) ->
let bound_typ = convert_type tv in
{descr = Existential {bound_typ;bound_var = v.name;body = convert_type t} ; pred = pred}
| Union types ->
let un_byte_size, un_types = convert_union_types types in
{ descr = TypedC.Union
{ un_byte_size;
un_types
};
pred = pred;
}
| Function (ret,args) ->
let args = List.map convert_type args in
let ret = convert_type ret in
{ descr = Function {ret; args; pure = false}; pred=pred }
| Constraint (t,e) ->
convert_type_pred t (Pred.conjunction (convert_predicate e) pred)
and convert_type typ = convert_type_pred typ TypedC.Pred.true_
and convert_struct fs =
let exception Return of TypedC.descr in
let make_struct st_byte_size fields =
TypedC.Structure { st_byte_size; st_members = List.rev fields }
in
try
let total_size, l = List.fold_left (fun (ofs,acc) (name,typ) ->
let typ = convert_type typ in
try
let byte_size = TypedC.sizeof typ in
let field = (ofs, name, typ) in
In_bytes.(ofs + byte_size), field::acc
with TypedC.Unsizeable_type ->
let structure = TypedC.{descr=make_struct ofs acc;pred=Pred.true_} in
raise (Return (TypedC.StructureFAM {structure;array=typ}))
) (In_bytes.zero, []) fs
in make_struct total_size l
with Return t -> t
and convert_union_types ts : In_bytes.t option * (string * TypedC.typ) list =
let name, t = List.hd ts in
let typ = convert_type t in
let sizeof_opt = (fun t -> try Some (TypedC.sizeof t) with TypedC.Unsizeable_type -> None) in
let size = sizeof_opt typ in
let total_size, l = List.fold_left (fun (size,acc) (name,typ) ->
let typ = convert_type typ in
let sz = sizeof_opt typ in
let new_size = match size, sz with
| Some s1, Some s2 when s1 = s2 -> Some s1
| _ -> None
in
(new_size, (name, typ)::acc)
) (size,[(name, typ)]) (List.tl ts)
in total_size, List.rev l
and convert_constr_def =
let open Type_parse_tree in
function Lambda (vars, typ) -> (convert_type typ, vars)
let convert_function_type typ pure =
let open Type_parse_tree in
let open TypedC in
match typ with
| Exists (v, tv, t) ->
let bound_typ = convert_type tv in
{descr = Existential {bound_typ;bound_var=v.name;body = convert_type t} ; pred = Pred.true_}
| Function(ret,args) ->
let args = List.map convert_type args in
let ret = convert_type ret in
{ descr = Function {ret; args; pure}; pred = Pred.true_ }
| _ -> assert false
let convert_definition =
let open Type_parse_tree in
function
| RegionDefinition(name,type_def) ->
let name = convert_type_name name in
begin match type_def with
| Type t ->
let constr = (TypedC.Constr.make name 0) in
TypedC.add_constr_definition constr (convert_type t,[])
| Constr c ->
let def = (convert_constr_def c) in
let arity = List.length @@ snd def in
let constr = TypedC.Constr.make name arity in
TypedC.add_constr_definition constr def
| _ -> assert false
end
| TypeDefinition(name,type_def) ->
let name = convert_type_name name in
begin match type_def with
| Type t ->
let constr = (TypedC.Constr.make name 0) in
add_type_definition constr (convert_type t,[])
| Constr c ->
let def = (convert_constr_def c) in
let arity = List.length @@ snd def in
let constr = TypedC.Constr.make name arity in
TypedC.add_constr_definition constr def
| _ -> assert false
end
| GlobalDefinition(name,globdef) -> begin match globdef with
| FunDef {inline; pure; funtyp=t} ->
TypedC.add_function_name_definition name (convert_function_type t pure) inline
| Global t -> TypedC.add_global_name_definition name (convert_type t)
| _ -> assert false
end
;;
end
let parse_file ~infer_spec name =
let parse_tree = Type_parser.(parse_file name annotations) in
Conversion.infer_spec := infer_spec;
let type_definitions, region_definitions =
List.partition (fun def ->
match def with Type_parse_tree.TypeDefinition _ -> true | _ -> false) parse_tree in
List.iter Conversion.convert_definition type_definitions;
List.iter Conversion.convert_definition region_definitions;
()
(** Parses a string into [TypedC.typ]
@raise [Failure] if the string is not a valid type *)
let type_of_string str =
let parse_tree = Type_parser.(parse_string str typeexpr) in
Conversion.convert_type parse_tree
open Conversion
let init ~data_model =
assert(!r_data_model = None);
set_data_model data_model;
let base_types =
[ "char", char_size
; "short", short_size()
; "int", int_size()
; "long",long_size()
; "long long", long_long_size()
; "intmax_t", long_long_size()
; "uintmax_t", long_long_size()
; "float", float_size
; "double", double_size
] in
base_types |> List.iter (fun (name,size) ->
let size = In_bytes.of_int size in
let constr = TypedC.Constr.make (TypedC.ConstrName name) 0 in
TypedC.add_constr_definition constr ((base name size TypedC.Pred.true_),[]))