forked from cil-project/cil
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcprint.ml
929 lines (858 loc) · 24 KB
/
cprint.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
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
(*
*
* Copyright (c) 2001-2003,
* George C. Necula <[email protected]>
* Scott McPeak <[email protected]>
* Wes Weimer <[email protected]>
* Ben Liblit <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The names of the contributors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*)
(* cprint -- pretty printer of C program from abstract syntax
**
** Project: FrontC
** File: cprint.ml
** Version: 2.1e
** Date: 9.1.99
** Author: Hugues Cassé
**
** 1.0 2.22.99 Hugues Cassé First version.
** 2.0 3.18.99 Hugues Cassé Compatible with Frontc 2.1, use of CAML
** pretty printer.
** 2.1 3.22.99 Hugues Cassé More efficient custom pretty printer used.
** 2.1a 4.12.99 Hugues Cassé Correctly handle:
** char *m, *m, *p; m + (n - p)
** 2.1b 4.15.99 Hugues Cassé x + (y + z) stays x + (y + z) for
** keeping computation order.
** 2.1c 7.23.99 Hugues Cassé Improvement of case and default display.
** 2.1d 8.25.99 Hugues Cassé Rebuild escape sequences in string and
** characters.
** 2.1e 9.1.99 Hugues Cassé Fix, recognize and correctly display '\0'.
*)
(* George Necula: I changed this pretty dramatically since CABS changed *)
open Cabs
open Cabshelper
open Escape
open Whitetrack
let version = "Cprint 2.1e 9.1.99 Hugues Cassé"
let printLn = ref true
let printLnComment = ref false
let printCounters = ref false
let printComments = ref false
(*
** FrontC Pretty printer
*)
let width = ref 80
let tab = ref 2
let max_indent = ref 60
let line = ref ""
let line_len = ref 0
let current = ref ""
let current_len = ref 0
let spaces = ref 0
let follow = ref 0
let roll = ref 0
(* stub out the old-style manual space functions *)
(* we may implement some of these later *)
let new_line () = ()
let space () = ()
let indent () = ()
let unindent () = ()
let force_new_line () = ()
let flush () = ()
let commit () = ()
(* sm: for some reason I couldn't just call print from frontc.... ? *)
let print_unescaped_string str = print str
(*
** Useful primitives
*)
let print_list print_sep print_elt lst =
let _ = List.fold_left
(fun com elt ->
if com then print_sep ();
print_elt elt;
true)
false
lst in
()
let print_commas nl fct lst =
print_list (fun () -> print ","; if nl then new_line() else space()) fct lst;
print_maybe ","
let print_string (s:string) =
print ("\"" ^ escape_string s ^ "\"")
let print_wstring (s: int64 list ) =
print ("L\"" ^ escape_wstring s ^ "\"")
(*
** Base Type Printing
*)
let rec print_specifiers (specs: spec_elem list) =
comprint "specifier(";
let print_spec_elem = function
SpecTypedef -> print "typedef"
| SpecInline -> printu "inline"
| SpecNoreturn -> printu "_Noreturn"
| SpecStorage sto ->
printu (match sto with
NO_STORAGE -> (comstring "/*no storage*/")
| AUTO -> "auto"
| STATIC -> "static"
| EXTERN -> "extern"
| REGISTER -> "register")
| SpecCV cv ->
printu (match cv with
| CV_CONST -> "const"
| CV_VOLATILE -> "volatile"
| CV_RESTRICT -> "restrict"
| CV_COMPLEX -> "complex")
| SpecAttr al -> print_attribute al; space ()
| SpecType bt -> print_type_spec bt
| SpecPattern name -> printl ["@specifier";"(";name;")"]
in
List.iter print_spec_elem specs
;comprint ")"
and print_type_spec = function
Tvoid -> print "void "
| Tchar -> print "char "
| Tbool -> print "_Bool "
| Tshort -> print "short "
| Tint -> print "int "
| Tlong -> print "long "
| Tint64 -> print "__int64 "
| Tint128 -> print "__int128 "
| Tfloat -> print "float "
| Tfloat128 -> print "__float128"
| Tdouble -> print "double "
| Tsigned -> printu "signed"
| Tunsigned -> print "unsigned "
| Tsizet -> print "size_t "
| Tnamed s -> comprint "tnamed"; print s; space ();
| Tstruct (n, None, _) -> printl ["struct";n]
| Tstruct (n, Some flds, extraAttrs) ->
(print_struct_name_attr "struct" n extraAttrs);
(print_fields flds)
| Tunion (n, None, _) -> printl ["union";n;" "]
| Tunion (n, Some flds, extraAttrs) ->
(print_struct_name_attr "union" n extraAttrs);
(print_fields flds)
| Tenum (n, None, _) -> printl ["enum";n]
| Tenum (n, Some enum_items, extraAttrs) ->
(print_struct_name_attr "enum" n extraAttrs);
(print_enum_items enum_items)
| TtypeofE e -> printl ["__typeof__";"("]; print_expression e; print ") "
| TtypeofT (s,d) -> printl ["__typeof__";"("]; print_onlytype (s, d); print ") "
| Tdefault -> print "default " (* TODO: is this right? *)
(* print "struct foo", but with specified keyword and a list of
* attributes to put between keyword and name *)
and print_struct_name_attr (keyword: string) (name: string) (extraAttrs: attribute list) =
begin
if extraAttrs = [] then
printl [keyword;name]
else begin
print keyword;
print_attributes extraAttrs; (* prints a final space *)
print name;
end
end
(* This is the main printer for declarations. It is easy because the
* declarations are laid out as they need to be printed. *)
and print_decl (n: string) = function
JUSTBASE -> if n <> "___missing_field_name" then
print n
else
comprint "missing field name"
| PARENTYPE (al1, d, al2) ->
print "(";
print_attributes al1; space ();
print_decl n d; space ();
print_attributes al2; print ")"
| PTR (al, d) ->
print "* ";
print_attributes al; space ();
print_decl n d
| ARRAY (d, al, e) ->
print_decl n d;
print "[";
print_attributes al;
if e <> NOTHING then print_expression e;
print "]"
| PROTO(d, args, isva) ->
comprint "proto(";
print_decl n d;
print "(";
print_params args isva;
print ")";
comprint ")"
and print_fields (flds : field_group list) =
if flds = [] then print " { } "
else begin
print " {";
indent ();
List.iter
(fun fld -> print_field_group fld; print ";"; new_line ())
flds;
unindent ();
print "} "
end
and print_enum_items items =
if items = [] then print " { } "
else begin
print " {";
indent ();
print_commas
true
(fun (id, exp, loc) -> print id;
if exp = NOTHING then ()
else begin
space ();
print "= ";
print_expression exp
end)
items;
unindent ();
print "} ";
end
and print_onlytype (specs, dt) =
print_specifiers specs;
print_decl "" dt
and print_name ((n, decl, attrs, _) : name) =
print_decl n decl;
space ();
print_attributes attrs
and print_init_name ((n, i) : init_name) =
print_name n;
if i <> NO_INIT then begin
space ();
print "= ";
print_init_expression i
end
and print_name_group (specs, names) =
print_specifiers specs;
print_commas false print_name names
and print_field_group (specs, fields) =
print_specifiers specs;
print_commas false print_field fields
and print_field (name, widtho) =
print_name name;
(match widtho with
None -> ()
| Some w -> print " : "; print_expression w)
and print_init_name_group (specs, names) =
print_specifiers specs;
print_commas false print_init_name names
and print_single_name (specs, name) =
print_specifiers specs;
print_name name
and print_params (pars : single_name list) (ell : bool) =
print_commas false print_single_name pars;
if ell then printl (if pars = [] then ["..."] else [",";"..."]) else ()
and print_old_params pars ell =
print_commas false (fun id -> print id) pars;
if ell then printl (if pars = [] then ["..."] else [",";"..."]) else ()
(*
** Expression printing
** Priorities
** 16 variables
** 15 . -> [] call()
** 14 ++, -- (post)
** 13 ++ -- (pre) ~ ! - + & *(cast)
** 12 * / %
** 11 + -
** 10 << >>
** 9 < <= > >=
** 8 == !=
** 7 &
** 6 ^
** 5 |
** 4 &&
** 3 ||
** 2 ? :
** 1 = ?=
** 0 ,
*)
and get_operator exp =
match exp with
NOTHING -> ("", 16)
| PAREN exp -> ("", 16)
| UNARY (op, _) ->
(match op with
MINUS -> ("-", 13)
| PLUS -> ("+", 13)
| NOT -> ("!", 13)
| BNOT -> ("~", 13)
| MEMOF -> ("*", 13)
| ADDROF -> ("&", 13)
| PREINCR -> ("++", 13)
| PREDECR -> ("--", 13)
| POSINCR -> ("++", 14)
| POSDECR -> ("--", 14))
| LABELADDR s -> ("", 16) (* Like a constant *)
| BINARY (op, _, _) ->
(match op with
MUL -> ("*", 12)
| DIV -> ("/", 12)
| MOD -> ("%", 12)
| ADD -> ("+", 11)
| SUB -> ("-", 11)
| SHL -> ("<<", 10)
| SHR -> (">>", 10)
| LT -> ("<", 9)
| LE -> ("<=", 9)
| GT -> (">", 9)
| GE -> (">=", 9)
| EQ -> ("==", 8)
| NE -> ("!=", 8)
| BAND -> ("&", 7)
| XOR -> ("^", 6)
| BOR -> ("|", 5)
| AND -> ("&&", 4)
| OR -> ("||", 3)
| ASSIGN -> ("=", 1)
| ADD_ASSIGN -> ("+=", 1)
| SUB_ASSIGN -> ("-=", 1)
| MUL_ASSIGN -> ("*=", 1)
| DIV_ASSIGN -> ("/=", 1)
| MOD_ASSIGN -> ("%=", 1)
| BAND_ASSIGN -> ("&=", 1)
| BOR_ASSIGN -> ("|=", 1)
| XOR_ASSIGN -> ("^=", 1)
| SHL_ASSIGN -> ("<<=", 1)
| SHR_ASSIGN -> (">>=", 1))
| QUESTION _ -> ("", 2)
| CAST _ -> ("", 13)
| CALL _ -> ("", 15)
| COMMA _ -> ("", 0)
| CONSTANT _ -> ("", 16)
| VARIABLE name -> ("", 16)
| EXPR_SIZEOF exp -> ("", 16)
| TYPE_SIZEOF _ -> ("", 16)
| EXPR_ALIGNOF exp -> ("", 16)
| TYPE_ALIGNOF _ -> ("", 16)
| GENERIC _ -> ("", 16) (* TODO: is this right? *)
| IMAG exp -> ("", 16)
| REAL exp -> ("", 16)
| CLASSIFYTYPE exp -> ("", 16)
| INDEX (exp, idx) -> ("", 15)
| MEMBEROF (exp, fld) -> ("", 15)
| MEMBEROFPTR (exp, fld) -> ("", 15)
| GNU_BODY _ -> ("", 17)
| EXPR_PATTERN _ -> ("", 16) (* sm: not sure about this *)
and print_comma_exps exps =
print_commas false print_expression exps
and print_init_expression (iexp: init_expression) : unit =
match iexp with
NO_INIT -> ()
| SINGLE_INIT e -> print_expression e
| COMPOUND_INIT initexps ->
let doinitexp = function
NEXT_INIT, e -> print_init_expression e
| i, e ->
let rec doinit = function
NEXT_INIT -> ()
| INFIELD_INIT (fn, i) -> printl [".";fn]; doinit i
| ATINDEX_INIT (e, i) ->
print "[";
print_expression e;
print "]";
doinit i
| ATINDEXRANGE_INIT (s, e) ->
print "[";
print_expression s;
print " ... ";
print_expression e;
print "]"
in
doinit i; print " = ";
print_init_expression e
in
print "{";
print_commas false doinitexp initexps;
print "}"
and print_expression (exp: expression) = print_expression_level 1 exp
and print_expression_level (lvl: int) (exp : expression) =
let (txt, lvl') = get_operator exp in
let _ = match exp with
NOTHING -> ()
| PAREN exp -> print "("; print_expression exp; print ")"
| UNARY (op, exp') ->
(match op with
POSINCR | POSDECR ->
print_expression_level lvl' exp';
print txt
| _ ->
print txt; space (); (* Print the space to avoid --5 *)
print_expression_level lvl' exp')
| LABELADDR l -> printl ["&&";l]
| BINARY (op, exp1, exp2) ->
(*if (op = SUB) && (lvl <= lvl') then print "(";*)
print_expression_level lvl' exp1;
space ();
print txt;
space ();
(*print_expression exp2 (if op = SUB then (lvl' + 1) else lvl');*)
print_expression_level (lvl' + 1) exp2
(*if (op = SUB) && (lvl <= lvl') then print ")"*)
| QUESTION (exp1, exp2, exp3) ->
print_expression_level 2 exp1;
space ();
print "? ";
print_expression_level 2 exp2;
space ();
print ": ";
print_expression_level 2 exp3;
| CAST (typ, iexp) ->
print "(";
print_onlytype typ;
print ")";
(* Always print parentheses. In a small number of cases when we print
* constants we don't need them *)
(match iexp with
SINGLE_INIT e -> print_expression_level 15 e
| COMPOUND_INIT _ -> (* print "("; *)
print_init_expression iexp
(* ; print ")" *)
| NO_INIT -> print "<NO_INIT in cast. Should never arise>")
| CALL (VARIABLE "__builtin_va_arg", [arg; TYPE_SIZEOF (bt, dt)]) ->
comprint "variable";
print "__builtin_va_arg";
print "(";
print_expression_level 1 arg;
print ",";
print_onlytype (bt, dt);
print ")"
| CALL (exp, args) ->
print_expression_level 16 exp;
print "(";
print_comma_exps args;
print ")"
| CLASSIFYTYPE exp ->
print "__builtin_classify_type";
print "(";
print_expression_level 1 exp;
print ")"
| COMMA exps ->
print_comma_exps exps
| CONSTANT cst ->
(match cst with
CONST_INT i -> print i
| CONST_FLOAT r -> print r
| CONST_COMPLEX r -> print r
| CONST_CHAR c -> print ("'" ^ escape_wstring c ^ "'")
| CONST_WCHAR c -> print ("L'" ^ escape_wstring c ^ "'")
| CONST_STRING s -> print_string s
| CONST_WSTRING ws -> print_wstring ws)
| VARIABLE name ->
comprint "variable";
print name
| EXPR_SIZEOF exp ->
print "sizeof";
print_expression_level 0 exp
| TYPE_SIZEOF (bt,dt) ->
printl ["sizeof";"("];
print_onlytype (bt, dt);
print ")"
| EXPR_ALIGNOF exp ->
printl ["__alignof__";"("];
print_expression_level 0 exp;
print ")"
| TYPE_ALIGNOF (bt,dt) ->
printl ["__alignof__";"("];
print_onlytype (bt, dt);
print ")"
| GENERIC (exp, lst) ->
let rec print_generic_list l =
match l with
[] -> ()
| (t, e) :: tl ->
print ", ";
print_onlytype t;
print ": ";
print_expression_level 0 e;
print_generic_list tl
in
printl ["_Generic";"("];
print_expression_level 0 exp;
print_generic_list lst;
print ")"
| IMAG exp ->
printl ["__imag__";"("];
print_expression_level 0 exp;
print ")"
| REAL exp ->
printl ["__real__";"("];
print_expression_level 0 exp;
print ")"
| INDEX (exp, idx) ->
print_expression_level 16 exp;
print "[";
print_expression_level 0 idx;
print "]"
| MEMBEROF (exp, fld) ->
print_expression_level 16 exp;
printl [".";fld]
| MEMBEROFPTR (exp, fld) ->
print_expression_level 16 exp;
printl ["->";fld]
| GNU_BODY (blk) ->
print "(";
print_block blk;
print ")"
| EXPR_PATTERN (name) ->
printl ["@expr";"(";name;")"]
in
()
(*
** Statement printing
*)
and print_statement stat =
match stat with
NOP (loc) ->
setLoc(loc);
print ";";
new_line ()
| COMPUTATION (exp, loc) ->
setLoc(loc);
print_expression exp;
print ";";
new_line ()
| BLOCK (blk, loc) -> print_block blk
| SEQUENCE (s1, s2, loc) ->
setLoc(loc);
print_statement s1;
print_statement s2;
| IF (exp, s1, s2, loc, eloc) ->
setLoc(loc);
printl ["if";"("];
print_expression_level 0 exp;
print ")";
print_substatement s1;
(match s2 with
| NOP(_) -> ()
| _ -> begin
print "else";
print_substatement s2;
end)
| WHILE (exp, stat, loc, eloc) ->
setLoc(loc);
printl ["while";"("];
print_expression_level 0 exp;
print ")";
print_substatement stat
| DOWHILE (exp, stat, loc, eloc) ->
setLoc(loc);
print "do";
print_substatement stat;
printl ["while";"("];
print_expression_level 0 exp;
print ");";
new_line ();
| FOR (fc1, exp2, exp3, stat, loc, eloc) ->
setLoc(loc);
printl ["for";"("];
(match fc1 with
FC_EXP exp1 -> print_expression_level 0 exp1; print ";"
| FC_DECL dec1 -> print_def dec1);
space ();
print_expression_level 0 exp2;
print ";";
space ();
print_expression_level 0 exp3;
print ")";
print_substatement stat
| BREAK (loc)->
setLoc(loc);
print "break;"; new_line ()
| CONTINUE (loc) ->
setLoc(loc);
print "continue;"; new_line ()
| RETURN (exp, loc) ->
setLoc(loc);
print "return";
if exp = NOTHING
then ()
else begin
print " ";
print_expression_level 1 exp
end;
print ";";
new_line ()
| SWITCH (exp, stat, loc, eloc) ->
setLoc(loc);
printl ["switch";"("];
print_expression_level 0 exp;
print ")";
print_substatement stat
| CASE (exp, stat, loc, eloc) ->
setLoc(loc);
unindent ();
print "case ";
print_expression_level 1 exp;
print ":";
indent ();
print_substatement stat
| CASERANGE (expl, exph, stat, loc, eloc) ->
setLoc(loc);
unindent ();
print "case ";
print_expression expl;
print " ... ";
print_expression exph;
print ":";
indent ();
print_substatement stat
| DEFAULT (stat, loc, eloc) ->
setLoc(loc);
unindent ();
print "default :";
indent ();
print_substatement stat
| LABEL (name, stat, loc) ->
setLoc(loc);
printl [name;":"];
space ();
print_substatement stat
| GOTO (name, loc) ->
setLoc(loc);
printl ["goto";name;";"];
new_line ()
| COMPGOTO (exp, loc) ->
setLoc(loc);
print ("goto *"); print_expression exp; print ";"; new_line ()
| DEFINITION d ->
print_def d
| ASM (attrs, tlist, details, loc) ->
setLoc(loc);
let print_asm_operand (identop,cnstr, e) =
print_string cnstr; space (); print_expression_level 100 e
in
begin
print "__asm__ ";
print_attributes attrs;
print "(";
print_list (fun () -> new_line()) print_string tlist; (* templates *)
begin
match details with
| None -> ()
| Some { aoutputs = outs; ainputs = ins; aclobbers = clobs } ->
print ":"; space ();
print_commas false print_asm_operand outs;
if ins <> [] || clobs <> [] then begin
print ":"; space ();
print_commas false print_asm_operand ins;
if clobs <> [] then begin
print ":"; space ();
print_commas false print_string clobs
end;
end
end;
print ");"
end;
new_line ()
and print_block blk =
new_line();
print "{";
indent ();
if blk.blabels <> [] then begin
print "__label__ ";
print_commas false print blk.blabels;
print ";";
new_line ();
end;
if blk.battrs <> [] then begin
List.iter print_attribute blk.battrs;
new_line ();
end;
List.iter print_statement blk.bstmts;
unindent ();
print "}";
new_line ()
and print_substatement stat =
match stat with
IF _
| SEQUENCE _
| DOWHILE _ ->
new_line ();
print "{";
indent ();
print_statement stat;
unindent ();
print "}";
new_line ();
| BLOCK _ ->
print_statement stat
| _ ->
indent ();
print_statement stat;
unindent ()
(*
** GCC Attributes
*)
and print_attribute (name,args) =
if args = [] then printu name
else begin
print name;
print "("; if name = "__attribute__" then print "(";
(match args with
[VARIABLE "aconst"] -> printu "const"
| [VARIABLE "restrict"] -> printu "restrict"
| _ -> print_commas false (fun e -> print_expression e) args);
print ")"; if name = "__attribute__" then print ")"
end
(* Print attributes. *)
and print_attributes attrs =
List.iter (fun a -> print_attribute a; space ()) attrs
(*
** Declaration printing
*)
and print_defs defs =
let prev = ref false in
List.iter
(fun def ->
(match def with
DECDEF _ -> prev := false
| _ ->
if not !prev then force_new_line ();
prev := true);
print_def def)
defs
and print_def def =
match def with
FUNDEF (proto, body, loc, _) ->
comprint "fundef";
if !printCounters then begin
try
let fname =
match proto with
(_, (n, _, _, _)) -> n
in
print_def (DECDEF (([SpecType Tint],
[(fname ^ "__counter", JUSTBASE, [], cabslu),
NO_INIT]), loc));
with Not_found -> print "/* can't print the counter */"
end;
setLoc(loc);
print_single_name proto;
print_block body;
force_new_line ();
| DECDEF (names, loc) ->
comprint "decdef";
setLoc(loc);
print_init_name_group names;
print ";";
new_line ()
| TYPEDEF (names, loc) ->
comprint "typedef";
setLoc(loc);
print_name_group names;
print ";";
new_line ();
force_new_line ()
| ONLYTYPEDEF (specs, loc) ->
comprint "onlytypedef";
setLoc(loc);
print_specifiers specs;
print ";";
new_line ();
force_new_line ()
| GLOBASM (asm, loc) ->
setLoc(loc);
printl ["__asm__";"("]; print_string asm; print ");";
new_line ();
force_new_line ()
| PRAGMA (a,loc) ->
setLoc(loc);
force_new_line ();
print "#pragma ";
let oldwidth = !width in
width := 1000000; (* Do not wrap pragmas *)
print_expression a;
width := oldwidth;
force_new_line ()
| LINKAGE (n, loc, dl) ->
setLoc (loc);
force_new_line ();
print "extern "; print_string n; print_string " {";
List.iter print_def dl;
print_string "}";
force_new_line ()
| TRANSFORMER(srcdef, destdeflist, loc) ->
setLoc(loc);
print "@transform {";
force_new_line();
print "{";
force_new_line();
indent ();
print_def srcdef;
unindent();
print "}";
force_new_line();
print "to {";
force_new_line();
indent();
List.iter print_def destdeflist;
unindent();
print "}";
force_new_line()
| EXPRTRANSFORMER(srcexpr, destexpr, loc) ->
setLoc(loc);
print "@transformExpr { ";
print_expression srcexpr;
print " } to { ";
print_expression destexpr;
print " }";
force_new_line()
(* sm: print a comment if the printComments flag is set *)
and comprint (str : string) : unit =
begin
if (!printComments) then (
print "/*";
print str;
print "*/ "
)
else
()
end
(* sm: yield either the given string, or "", depending on printComments *)
and comstring (str : string) : string =
begin
if (!printComments) then
str
else
""
end
(* print abstrac_syntax -> ()
** Pretty printing the given abstract syntax program.
*)
let printFile (result : out_channel) ((fname, defs) : file) =
Whitetrack.setOutput result;
print_defs defs;
Whitetrack.printEOF ();
flush () (* sm: should do this here *)
let set_tab t = tab := t
let set_width w = width := w