-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcilType.ml
More file actions
752 lines (635 loc) · 15.3 KB
/
cilType.ml
File metadata and controls
752 lines (635 loc) · 15.3 KB
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
(** Printables for CIL types. *)
open GoblintCil
open Pretty
module type S =
sig
include Printable.S
end
module Std =
struct
include Printable.StdLeaf
end
module Location:
sig
include S with type t = location
val pp: Format.formatter -> t -> unit (* for Messages *)
val of_yojson: Yojson.Safe.t -> (t, string) result
end =
struct
include Std
type t = location
let name () = "location"
(* Identity *)
let compare x y = Cil.compareLoc x y
let equal x y = compare x y = 0
let hash x = Hashtbl.hash x (* struct of primitives, so this is fine *)
(* Output *)
let show x =
(* TODO: add special output for locUnknown *)
x.file ^ ":" ^ string_of_int x.line ^ (
if x.column >= 0 then
":" ^ string_of_int x.column
else
""
) ^ (
if x.endByte >= 0 then
"-" ^ string_of_int x.endLine ^ (
if x.endColumn >= 0 then
":" ^ string_of_int x.endColumn
else
""
)
else
""
)
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
let to_yojson x = `Assoc (
[
("file", `String x.file);
("line", `Int x.line);
("column", `Int x.column);
("byte", `Int x.byte);
]
@
if x.endByte >= 0 then
[
("endLine", `Int x.endLine);
("endColumn", `Int x.endColumn);
("endByte", `Int x.endByte);
]
else
[]
)
let pp fmt x = Format.fprintf fmt "%s" (show x) (* for Messages *)
let of_yojson = function
| `Assoc l ->
begin match List.assoc_opt "file" l, List.assoc_opt "line" l, List.assoc_opt "column" l, Option.value ~default:(`Int (-1)) (List.assoc_opt "byte" l) with
| Some (`String file), Some (`Int line), Some (`Int column), `Int byte ->
let loc = {file; line; column; byte; endLine = -1; endColumn = -1; endByte = -1; synthetic = false} in
begin match List.assoc_opt "endLine" l, List.assoc_opt "endColumn" l, List.assoc_opt "endByte" l with
| Some (`Int endLine), Some (`Int endColumn), Some (`Int endByte) ->
Result.Ok {loc with endLine; endColumn; endByte}
| _, _, _ ->
Result.Ok loc
end
| _, _, _, _ -> Result.Error "CilType.Location.of_yojson"
end
| _ -> Result.Error "CilType.Location.of_yojson"
end
module Ikind: S with type t = ikind =
struct
include Std
(* Re-export constructors for monomorphization and deriving. *)
type t = ikind =
| IChar
| ISChar
| IUChar
| IBool
| IInt
| IUInt
| IShort
| IUShort
| ILong
| IULong
| ILongLong
| IULongLong
| IInt128
| IUInt128
[@@deriving hash]
(* Hashtbl.hash doesn't monomorphize, so derive instead. *)
let name () = "ikind"
(* Identity *)
(* Enum type, so polymorphic identity is fine. *)
(* Monomorphize polymorphic operations for optimization. *)
let equal (x: t) (y: t) = x = y
let compare (x: t) (y: t) = Stdlib.compare x y
(* Output *)
let pretty () x = d_ikind () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
module Fkind: S with type t = fkind =
struct
include Std
(* Re-export constructors for monomorphization and deriving. *)
type t = fkind =
| FFloat
| FDouble
| FLongDouble
| FFloat128
| FFloat16
| FComplexFloat
| FComplexDouble
| FComplexLongDouble
| FComplexFloat128
| FComplexFloat16
[@@deriving hash]
(* Hashtbl.hash doesn't monomorphize, so derive instead. *)
let name () = "fkind"
(* Identity *)
(* Enum type, so polymorphic identity is fine. *)
(* Monomorphize polymorphic operations for optimization. *)
let equal (x: t) (y: t) = x = y
let compare (x: t) (y: t) = Stdlib.compare x y
(* Output *)
let pretty () x = d_fkind () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
module Unop: S with type t = unop =
struct
include Std
(* Re-export constructors for monomorphization and deriving. *)
type t = unop =
| Neg
| BNot
| LNot
[@@deriving hash]
(* Hashtbl.hash doesn't monomorphize, so derive instead. *)
let name () = "unop"
(* Identity *)
(* Enum type, so polymorphic identity is fine. *)
(* Monomorphize polymorphic operations for optimization. *)
let equal (x: t) (y: t) = x = y
let compare (x: t) (y: t) = Stdlib.compare x y
(* Output *)
let pretty () x = d_unop () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
module Binop: S with type t = binop =
struct
include Std
(* Re-export constructors for monomorphization and deriving. *)
type t = binop =
| PlusA
| PlusPI
| IndexPI
| MinusA
| MinusPI
| MinusPP
| Mult
| Div
| Mod
| Shiftlt
| Shiftrt
| Lt
| Gt
| Le
| Ge
| Eq
| Ne
| BAnd
| BXor
| BOr
| LAnd
| LOr
[@@deriving hash]
(* Hashtbl.hash doesn't monomorphize, so derive instead. *)
let name () = "binop"
(* Identity *)
(* Enum type, so polymorphic identity is fine. *)
(* Monomorphize polymorphic operations for optimization. *)
let equal (x: t) (y: t) = x = y
let compare (x: t) (y: t) = Stdlib.compare x y
(* Output *)
let pretty () x = d_binop () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
module Wstring_type: S with type t = wstring_type =
struct
include Std
(* Re-export constructors for monomorphization and deriving. *)
type t = wstring_type =
| Wchar_t
| Char16_t
| Char32_t
[@@deriving hash]
(* Hashtbl.hash doesn't monomorphize, so derive instead. *)
let name () = "wstring_type"
(* Identity *)
(* Enum type, so polymorphic identity is fine. *)
(* Monomorphize polymorphic operations for optimization. *)
let equal (x: t) (y: t) = x = y
let compare (x: t) (y: t) = Stdlib.compare x y
(* Output *)
let show = function
| Wchar_t -> "wchar_t"
| Char16_t -> "char16_t"
| Char32_t -> "char32_t"
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
end
module Encoding: S with type t = encoding =
struct
include Std
(* Re-export constructors for monomorphization and deriving. *)
type t = encoding =
| No_encoding
| Utf8
[@@deriving hash]
(* Hashtbl.hash doesn't monomorphize, so derive instead. *)
let name () = "encoding"
(* Identity *)
(* Enum type, so polymorphic identity is fine. *)
(* Monomorphize polymorphic operations for optimization. *)
let equal (x: t) (y: t) = x = y
let compare (x: t) (y: t) = Stdlib.compare x y
(* Output *)
let show = function
| No_encoding -> "no encoding"
| Utf8 -> "utf-8"
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
end
module Varinfo:
sig
include S with type t = varinfo
val pp: Format.formatter -> t -> unit (* for deriving show *)
end =
struct
include Std
type t = varinfo
let name () = "varinfo"
(* Identity *)
let equal x y = x.vid = y.vid
let compare x y = Stdlib.compare x.vid y.vid
let hash x = x.vid
(* Output *)
let show x = x.vname
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
let pp fmt x = Format.fprintf fmt "%s" x.vname (* for deriving show *)
end
module Fundec: S with type t = fundec =
struct
include Std
type t = fundec
let name () = "fundec"
(* Identity *)
let equal x y = Varinfo.equal x.svar y.svar
let compare x y = Varinfo.compare x.svar y.svar
let hash x = Varinfo.hash x.svar
(* Output *)
let pretty () x = Varinfo.pretty () x.svar
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
module Compinfo: S with type t = compinfo =
struct
include Std
type t = compinfo
let name () = "compinfo"
(* Identity *)
let equal x y = x.ckey = y.ckey
let compare x y = Stdlib.compare x.ckey y.ckey
let hash x = x.ckey
(* Output *)
let show x = compFullName x
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
end
module Fieldinfo: S with type t = fieldinfo =
struct
include Std
type t = fieldinfo
let name () = "fieldinfo"
(* Identity *)
let equal x y = x.fname = y.fname && Compinfo.equal x.fcomp y.fcomp
let compare x y =
let r = String.compare x.fname y.fname in
if r <> 0 then
r
else
Compinfo.compare x.fcomp y.fcomp
let hash x = 31 * (Hashtbl.hash x.fname) + Compinfo.hash x.fcomp
(* Output *)
let show x = x.fname
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
end
module Enuminfo: S with type t = enuminfo =
struct
include Std
type t = enuminfo
let name () = "enuminfo"
(* Identity *)
(* TODO: Is this enough or do we have to recursively compare eitems, etc? *)
let equal x y = x.ename = y.ename
let compare x y = String.compare x.ename y.ename
let hash x = Hashtbl.hash x.ename
(* Output *)
let show x = x.ename
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
end
module Typeinfo: S with type t = typeinfo =
struct
include Std
type t = typeinfo
let name () = "typeinfo"
(* Identity *)
(* TODO: Is this enough or do we have to recursively compare ttype? *)
let equal x y = x.tname = y.tname
let compare x y = String.compare x.tname y.tname
let hash x = Hashtbl.hash x.tname
(* Output *)
let show x = x.tname
include Printable.SimpleShow (
struct
type nonrec t = t
let show = show
end
)
end
module Stmt: S with type t = stmt =
struct
include Std
type t = stmt
let name () = "stmt"
(* Identity *)
let equal x y = x.sid = y.sid
let compare x y = Stdlib.compare x.sid y.sid
let hash x = x.sid
(* Output *)
let pretty () x = dn_stmt () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
module rec Typ:
sig
include S with type t = typ
val pp: Format.formatter -> t -> unit (* for deriving show *)
end =
struct
include Std
type t = typ =
| TVoid of Attributes.t
| TInt of Ikind.t * Attributes.t
| TFloat of Fkind.t * Attributes.t
| TPtr of t * Attributes.t
| TArray of t * Exp.t option * Attributes.t
| TFun of t * (string * t * Attributes.t) list option * bool * Attributes.t
| TNamed of Typeinfo.t * Attributes.t
| TComp of Compinfo.t * Attributes.t
| TEnum of Enuminfo.t * Attributes.t
| TBuiltin_va_list of Attributes.t
[@@deriving eq, ord, hash]
let name () = "typ"
(* Identity *)
(* Optimize derived with physical equality. *)
let equal x y = x == y || equal x y
let compare x y =
if x == y then
0
else
compare x y
(* Output *)
let pretty () x = dn_type () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
let pp fmt x = Format.fprintf fmt "%s" (show x) (* for deriving show *)
end
and Typsig: S with type t = typsig =
struct
include Std
type t = typsig =
| TSArray of t * Z.t option * Attributes.t
| TSPtr of t * Attributes.t
| TSComp of bool * string * Attributes.t
| TSFun of t * t list option * bool * Attributes.t
| TSEnum of string * Attributes.t
| TSBase of Typ.t
[@@deriving eq, ord, hash]
let name () = "typsig"
(* Output *)
let pretty () x = d_typsig () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Constant: S with type t = constant =
struct
include Std
type t = constant =
| CInt of Z.t * Ikind.t * string option
| CStr of string * Encoding.t
| CWStr of int64 list * Wstring_type.t
| CChr of char
| CReal of float * Fkind.t * string option
| CEnum of Exp.t * string * Enuminfo.t
[@@deriving eq, ord, hash]
let name () = "constant"
(* Output *)
let pretty () x = d_const () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Offset: S with type t = offset =
struct
include Std
type t = offset =
| NoOffset
| Field of Fieldinfo.t * t
| Index of Exp.t * t
[@@deriving eq, ord, hash]
let name () = "offset"
(* Output *)
let pretty () x = d_offset nil () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Lval: S with type t = lval =
struct
include Std
type lhost = Cil.lhost =
| Var of Varinfo.t
| Mem of Exp.t
[@@deriving eq, ord, hash]
type t = lhost * Offset.t [@@deriving eq, ord, hash]
let name () = "lval"
(* Identity *)
(* Optimize derived with physical equality. *)
let equal x y = x == y || equal x y
let compare x y =
if x == y then
0
else
compare x y
(* Output *)
let pretty () x = dn_lval () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Exp: S with type t = exp =
struct
include Std
type t = exp =
| Const of Constant.t
| Lval of Lval.t
| SizeOf of Typ.t
| Real of t
| Imag of t
| SizeOfE of t
| SizeOfStr of string
| AlignOf of Typ.t
| AlignOfE of t
| UnOp of Unop.t * t * Typ.t
| BinOp of Binop.t * t * t * Typ.t
| Question of t * t * t * Typ.t
| CastE of Typ.t * t
| AddrOf of Lval.t
| AddrOfLabel of Stmt.t ref
| StartOf of Lval.t
[@@deriving eq, ord, hash]
let name () = "exp"
(* Identity *)
(* Optimize derived with physical equality. *)
let equal x y = x == y || equal x y
let compare x y =
if x == y then
0
else
compare x y
(* Output *)
let pretty () x = dn_exp () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Attrparam: S with type t = attrparam =
struct
include Std
type t = attrparam =
| AInt of int
| AStr of string
| ACons of string * t list
| ASizeOf of Typ.t
| ASizeOfE of t
| ASizeOfS of Typsig.t
| AAlignOf of Typ.t
| AAlignOfE of t
| AAlignOfS of Typsig.t
| AUnOp of Unop.t * t
| ABinOp of Binop.t * t * t
| ADot of t * string
| AStar of t
| AAddrOf of t
| AIndex of t * t
| AQuestion of t * t * t
| AAssign of t * t
[@@deriving eq, ord, hash]
let name () = "attrparam"
(* Output *)
let pretty () x = dn_attrparam () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Attribute: S with type t = attribute =
struct
include Std
type t = attribute = Attr of string * Attrparam.t list [@@deriving eq, ord, hash]
let name () = "attribute"
(* Output *)
let pretty () x = dn_attr () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end
and Attributes: S with type t = attributes =
struct
include Std
type t = Attribute.t list [@@deriving eq, ord, hash]
let name () = "attributes"
(* Output *)
let pretty () x = dn_attrlist () x
include Printable.SimplePretty (
struct
type nonrec t = t
let pretty = pretty
end
)
end