/
testrewrite.sml
1886 lines (1709 loc) · 87 KB
/
testrewrite.sml
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
val () = app Meta.load
["Int", "Real", "Mosml", "Substring", "Regex", "Listsort", "Jit",
"BitSet", "RewriteMain", "CSyntax", "Prolog", "TextIO"];
Meta.quotation := true;
fun match s = Rewrite.tree_match "%aquote"
(Rewrite.compileRewrites
(Rewrite.parse_rewrites_string s));
fun rewrite s = Rewrite.tree_rewrite "%aquote" "%quote"
(Rewrite.compileRewrites
(Rewrite.parse_rewrites_string s));
fun rewritetd s = Rewrite.tree_rewrite_td "%aquote" "%quote"
(Rewrite.compileRewrites
(Rewrite.parse_rewrites_string s));
fun rewrite_param fns s = Rewrite.tree_rewrite_param "%aquote" "%quote"
(Rewrite.compileRewrites
(Rewrite.parse_rewrites_string s)) fns;
fun rewrite_paramtd fns s = Rewrite.tree_rewrite_param_td "%aquote" "%quote"
(Rewrite.compileRewrites
(Rewrite.parse_rewrites_string s)) fns;
fun printTree t = Rewrite.printTreeDirect 0 t;
fun cstring s = RewriteMain.parse_c_string s;
fun qlToString l =
let fun iter r [] = r
| iter r ((QUOTE s)::fs) = iter (r^s) fs
| iter r ((ANTIQUOTE s)::fs) = iter (r^s) fs
in iter "" l
end;
local open GrammarSyntax
in
type decls = {define_enum : string -> string, get_decl : string -> Tree,
get_macro : string -> Tree * (string list option * string),
get_tag : string -> Tree, get_typedef : string -> Tree,
print_decl : string -> unit, define_enum_hash : string -> string,
macros : unit -> (string * (Tree * (string list option * string))) list}
end
fun parse fname cppflags trunit =
let
open GrammarSyntax
open CSyntax
val cppcmd = "LD_LIBRARY_PATH=/home/ian3/usr/lib cpp -undef -std=iso9899:1999 "^cppflags
val cppmcmd = cppcmd^" -dM"
val cdeft = RewriteMain.parse_cpp_pipe cppmcmd fname trunit
val _ = load_macros cdeft
val nCMacros = List.length (macro_names())
fun hashtToList t = Hasht.fold (fn a => fn b => fn c => (a,b)::c) [] t
val macros = hashtToList CSyntax.macro_table
val cdeclt = RewriteMain.parse_c_pipe cppcmd fname trunit
val (cdecls,canondecls) =
declsFoldl
(fn (t,(a,a')) =>
(case decl_id t
of [Term(s)] => ((s,t)::a,a')
| _ => (a,t::a')))
([],[]) cdeclt
val nCDecls = List.length cdecls
val nCAnonDecls = List.length canondecls
val CEnumTypedefs =
List.foldl (fn ((n, SOME l),r) => (n,l)::r | ((_,NONE),r) => r) []
(List.map
(fn n => (n, Option.map enumdec ((enum_typedef o typedef_decl) n)))
(typedef_names()));
val (nCEnumTypedefs,
nCEnumTypedefConsts) =
List.foldl (fn ((_,l),(m,n)) => (m+1,n+List.length l)) (0,0) CEnumTypedefs
val CAnonEnums =
let fun name [] = ""
| name l = (case lcprefixl (List.map (fn (s,_) => mlvar s) l)
of "" => (#1 o hd) l
| n => n)
in
rev (List.foldl
(fn (t,a) =>
case enumdeclmatch t
of [e] =>
let val l=enumdec (e,())
val ename = (name l)^"Consts"
val ename' =
let fun iter n =
if List.exists (fn (s,_) => s = (ename^(Int.toString n))) a
then iter (n+1)
else ename^(Int.toString n)
in if List.exists (fn (s,_) => s = ename) a
then iter 2
else ename
end
in ((ename',l)::a)
end
| _ => a)
[]
canondecls)
end
val _ = print ("CPP Macro definitions: "^(Int.toString nCMacros))
val _ = print ("\nNamed Declarations: "^(Int.toString nCDecls))
val _ = print ("\nEnumeration Typedefs: "^(Int.toString nCEnumTypedefs))
val _ = print (" (defining "^(Int.toString nCEnumTypedefConsts)^" constants)")
val _ = print ("\nAnonymous Enumeration Declarations: "^(Int.toString nCAnonDecls))
val _ = print "\n"
val tags = hashtToList CSyntax.tag_table
val typedefs = List.map (fn (s,(t,_)) => (s,t)) (hashtToList CSyntax.typedef_table)
fun find_cdecl ds = fn s => List.find (fn (s',t) => s'=s) ds
fun exists optn = case optn of SOME v => v | NONE => raise Subscript
val cdecl = fn ds => #2 o exists o (find_cdecl ds)
val print_cdecl = fn ds => (Rewrite.printTreeDirect 20) o (cdecl ds)
fun define_enum s = enum_datatype (CEnumTypedefs@CAnonEnums) s
fun define_enum_hash s = enum_hash (CEnumTypedefs@CAnonEnums) s
in {get_decl=cdecl cdecls, print_decl=print_cdecl cdecls, define_enum=define_enum,
define_enum_hash=define_enum_hash,
get_tag=cdecl tags, get_macro=cdecl macros, get_typedef=cdecl typedefs, macros=(fn () => macros)}
end
fun parseq fname cppflags trunit =
parse fname cppflags (qlToString trunit)
val absyntq = RewriteMain.parse_c_string o qlToString
val rewriteq = rewrite o qlToString
val rewritepq = fn params => (rewrite_param params) o qlToString
val rewritetdq = rewritetd o qlToString
val rewritetdpq = fn params => (rewrite_paramtd params) o qlToString
val matchq = match o qlToString
val _ = Rewrite.printTreeDirect 0
(RewriteMain.parse_c_string "typedef unsigned int ui;\
\ typedef ui *pui;\
\ typedef unsigned int *pui2;")
(* Now we take the declaration-specifiers list of a typedef
declaration and turn it into the implied specifier-qualifier-list
of a structure declaration. This is a little involved, because we
must eliminate all the declaration-specifiers other than
type-specifiers (int, typedef-name, etc.) and type-qualifiers
(const etc.), and we simultaneously turn the
right-associative declaration-specifiers list into a
left-associative specifier-qualifier-list.
To be able to understand a set of rewrites like this it is
important to know the order in which things happen. The rewrites
are applied, depth-first (i.e. starting from the leaves,) to each
leaf and branch of the abstract syntax tree. Each node (a branch or
a leaf) is tested against each of the rules in the list, starting
with the first rule. When one of the LHSs matches, the matching
parameters ⌜x⌝ are substituted in the RHS of that rule, and then
the function abstractions ⌜fn(⌜x⌝)⌝ are made. Finally the recursive
rewrites (in ⟦semantic brackets⟧) are applied. When each branch is
complete, the resulting term is substituted in the original tree,
and the set of rewrites are then applied to that new branch,
starting again with the first rule. So the rules below start out at
the leaves, the first two of which rules introduce new types of
branch which in turn trigger the introduction of more new branch
types via the subsequent rules.
It may be difficult to learn to read these rules before one has
learned to write them. If so, then some writing practice will
help. One might try implementing some simple functors like
list.map, list.fold, list.rev etc. which act on abstract syntax
representations of lists using cons and nil non-terminals. *)
val specqualrw = rewriteq `
⟦declaration-specifiers ⌜x:type-qualifier⌝⟧
= (specifier-qualifier-list ⌜x⌝)
⟦declaration-specifiers ⌜x:type-specifier⌝⟧
= (specifier-qualifier-list ⌜x⌝)
⟦declaration-specifiers ⌜_⌝⟧
= (⌜ε⌝)
⟦declaration-specifiers
⌜x:type-specifier⌝
⌜y:specifier-qualifier-list⌝⟧
= (specifier-qualifier-list ⌜x⌝ ⌜y⌝)
⟦declaration-specifiers
⌜x:type-qualifier⌝
⌜y:specifier-qualifier-list⌝⟧
= (specifier-qualifier-list ⌜x⌝ ⌜y⌝)
⟦declaration-specifiers
⌜x⌝
⌜y:specifier-qualifier-list⌝⟧
= (⌜y⌝)
⟦declaration (specifier-qualifier-list ⌜x⌝ ⌜y⌝)
(init-declarator-list
(init-declarator ⌜z⌝))⟧
= (struct-declaration
⟦specifier-qualifier-list
(specifier-qualifier-list ⌜x⌝)
⌜y⌝⟧
(struct-declarator-list
(struct-declarator ⌜z⌝)))
⟦declaration (specifier-qualifier-list ⌜x⌝)
(init-declarator-list
(init-declarator ⌜z⌝))⟧
= (struct-declaration
(specifier-qualifier-list ⌜x⌝)
(struct-declarator-list
(struct-declarator ⌜z⌝)))
⟦specifier-qualifier-list
⌜x:specifier-qualifier-list⌝
(specifier-qualifier-list ⌜y⌝)⟧
= (specifier-qualifier-list ⌜x⌝ ⌜y⌝)
⟦specifier-qualifier-list
⌜x:specifier-qualifier-list⌝
(specifier-qualifier-list ⌜y⌝ ⌜z⌝)⟧
= (⟦specifier-qualifier-list
(specifier-qualifier-list ⌜x⌝ ⌜y⌝)
⌜z⌝⟧)`;
val split_structdecl_list = rewriteq `
⟦struct-declaration-list
(struct-declaration
⌜w:specifier-qualifier-list⌝
(struct-declarator-list
⌜x:struct-declarator-list⌝
⌜y:struct-declarator⌝))⟧ =
(struct-declaration-list
⟦struct-declaration-list (struct-declaration ⌜w⌝ ⌜x⌝)⟧
(struct-declaration ⌜w⌝ (struct-declarator-list ⌜y⌝)))
⟦struct-declaration-list
⌜v:struct-declaration-list⌝
(struct-declaration
⌜w:specifier-qualifier-list⌝
(struct-declarator-list
⌜x:struct-declarator-list⌝
⌜y:struct-declarator⌝))⟧ =
(struct-declaration-list
⟦struct-declaration-list ⌜v⌝ (struct-declaration ⌜w⌝ ⌜x⌝)⟧
(struct-declaration ⌜w⌝ (struct-declarator-list ⌜y⌝)))
⟦struct-declaration-list
⌜x:struct-declaration-list⌝⟧ = (⌜x⌝)`
fun foldStructDecl f = rewritepq [("f",f)] `
⟦struct-declaration-list
⌜x:struct-declaration-list⌝
⌜y:struct-declaration⌝⟧ =
(struct-declaration-list ⌜x⌝ ⌜f(⌜y⌝)⌝)
⟦struct-declaration-list
⌜x:struct-declaration⌝⟧ =
(struct-declaration-list ⌜f(⌜x⌝)⌝)`;
fun foldParameterDecl f = rewritepq [("f",f)] `
⟦parameter-list
⌜x:parameter-list⌝
⌜y:parameter-declaration⌝⟧ =
(parameter-list ⌜x⌝ ⌜f(⌜y⌝)⌝)
⟦parameter-list
⌜x:parameter-declaration⌝⟧ =
(parameter-list ⌜f(⌜x⌝)⌝)`
val elimtdrw = rewriteq `
⟦declaration-specifiers
(storage-class-specifier typedef)
⌜x:declaration-specifiers⌝⟧
= (⟦⌜x⌝⟧)
⟦declaration-specifiers
⌜x⌝
(declaration-specifiers
(storage-class-specifier typedef))⟧
= (⟦declaration-specifiers ⌜x⌝⟧)`
val mergepointer = rewriteq `
⟦merge ⌜x:type-qualifier⌝
(pointer ⌜w:type-qualifier-list⌝ ⌜z⌝)⟧
= (pointer (type-qualifier-list ⌜w⌝ ⌜x⌝) ⌜z⌝)
⟦merge ⌜x:type-qualifier⌝
(pointer ⌜z⌝)⟧
= (pointer (type-qualifier-list ⌜x⌝) ⌜z⌝)
⟦merge ⌜x:type-qualifier⌝
(pointer)⟧
= (pointer (type-qualifier-list ⌜x⌝))`
val pointer = ("pf",mergepointer)
fun mergedecltq dss tds = rewritepq [pointer] `
⟦merge (declaration-specifiers
⌜x⌝
⌜y:declaration-specifiers⌝)
⌜z⌝⟧
= (⟦merge ⌜y⌝ ⟦merge (declaration-specifiers ⌜x⌝) ⌜z⌝⟧⟧)
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(declaration
⌜y:declaration-specifiers⌝
(init-declarator-list
(init-declarator
(declarator ⌜w:pointer⌝ ⌜z⌝))))⟧
= (declaration
⌜y⌝
(init-declarator-list
(init-declarator
(declarator ⌜pf(merge ⌜x⌝ ⌜w⌝)⌝ ⌜z⌝))))
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(declaration
⌜y:declaration-specifiers⌝
(init-declarator-list
(init-declarator
(declarator
(direct-declarator
⌜w:direct-declarator⌝)))))⟧
= (declaration
⌜y⌝
(init-declarator-list
(init-declarator
(declarator
(direct-declarator
⟦merge
(declaration-specifiers ⌜x⌝)
⌜w⌝⟧)))))
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(declaration
⌜y:declaration-specifiers⌝
(init-declarator-list
(init-declarator
(declarator
(direct-declarator
⌜w:direct-declarator⌝
⌜z:parameter-type-list⌝)))))⟧
= (declaration
⌜y⌝
(init-declarator-list
(init-declarator
(declarator
(direct-declarator
⟦merge (declaration-specifiers ⌜x⌝) ⌜w⌝⟧ ⌜z⌝)))))
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(parameter-declaration
⌜y:declaration-specifiers⌝
(declarator
(direct-declarator
⌜w:direct-declarator⌝)))⟧
= (declaration
⌜y⌝
(declarator
(direct-declarator
⟦merge
(declaration-specifiers ⌜x⌝)
⌜w⌝⟧)))
⟦merge (declaration-specifiers
⌜_:type-specifier⌝)
⌜x⌝⟧
= (⌜x⌝)
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(parameter-declaration
⌜y:declaration-specifiers⌝
(declarator ⌜w:pointer⌝ ⌜z⌝))⟧
= (parameter-declaration
⌜y⌝
(declarator ⌜pf(merge ⌜x⌝ ⌜w⌝)⌝ ⌜z⌝))
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(direct-declarator ⌜w⌝ ⌜z:parameter-type-list⌝)⟧
= (direct-declarator
⟦merge (declaration-specifiers ⌜x⌝) ⌜w⌝⟧ ⌜z⌝)
⟦merge (declaration-specifiers
⌜x:type-qualifier⌝)
(direct-declarator
(declarator ⌜w:pointer⌝ ⌜z⌝))⟧
= (direct-declarator
(declarator ⌜pf(merge ⌜x⌝ ⌜w⌝)⌝ ⌜z⌝))
⟦merge (declaration-specifiers
⌜x⌝)
(parameter-declaration
⌜y:declaration-specifiers⌝
⌜z⌝)⟧
= (parameter-declaration
(declaration-specifiers ⌜x⌝ ⌜y⌝)
⌜z⌝)
⟦merge (declaration-specifiers
⌜x⌝)
(declaration
⌜y:declaration-specifiers⌝
⌜z⌝)⟧
= (declaration
(declaration-specifiers ⌜x⌝ ⌜y⌝)
⌜z⌝)` (GrammarSyntax.NonTerm("merge",[dss,tds]))
(* merge type-qualifiers with structure declarations *)
fun mergestructtq sqs dss = rewritepq [pointer] `
⟦merge (specifier-qualifier-list
⌜y:specifier-qualifier-list⌝
⌜x⌝)
⌜z⌝⟧
= (⟦merge ⌜y⌝ ⟦merge (specifier-qualifier-list ⌜x⌝) ⌜z⌝⟧⟧)
⟦merge (specifier-qualifier-list
⌜x:type-qualifier⌝)
(declaration
⌜y:declaration-specifiers⌝
(init-declarator-list
(init-declarator
(declarator ⌜w:pointer⌝ ⌜z⌝))))⟧
= (declaration
⌜y⌝
(init-declarator-list
(init-declarator
(declarator ⌜pf(merge ⌜x⌝ ⌜w⌝)⌝ ⌜z⌝))))
⟦merge (specifier-qualifier-list
⌜x:type-qualifier⌝)
(struct-declaration
⌜y:declaration-specifiers⌝
(struct-declarator-list
(struct-declarator
(declarator
(direct-declarator
⌜w:direct-declarator⌝)))))⟧
= (struct-declaration
⌜y⌝
(struct-declarator-list
(struct-declarator
(declarator
(direct-declarator
⟦merge (specifier-qualifier-list ⌜x⌝) ⌜w⌝⟧)))))
⟦merge (specifier-qualifier-list
⌜_:type-specifier⌝)
⌜x⌝⟧
= (⌜x⌝)
⟦merge (specifier-qualifier-list
⌜x⌝)
(declaration
⌜y:declaration-specifiers⌝
⌜z⌝)⟧
= (declaration
(declaration-specifiers ⌜x⌝ ⌜y⌝)
⌜z⌝)
⟦merge (specifier-qualifier-list
⌜x:type-qualifier⌝)
(struct-declaration
⌜y:specifier-qualifier-list⌝
(struct-declarator-list
(struct-declarator
(declarator ⌜w:pointer⌝ ⌜z⌝))))⟧
= (struct-declaration
⌜y⌝
(struct-declarator-list
(struct-declarator
(declarator ⌜pf(merge ⌜x⌝ ⌜w⌝)⌝ ⌜z⌝))))
⟦merge (specifier-qualifier-list
⌜x⌝)
(struct-declaration
⌜y:specifier-qualifier-list⌝
⌜z⌝)⟧
= (struct-declaration
(specifier-qualifier-list ⌜x⌝ ⌜y⌝)
⌜z⌝)` (GrammarSyntax.NonTerm("merge",[sqs,dss]))
fun mergedss dss tds = rewriteq `
⟦merge (declaration-specifiers ⌜_:type-specifier⌝)
⌜t:=(declaration-specifiers ⌜_:type-specifier⌝)⌝⟧
= (⌜t⌝)
⟦merge (declaration-specifiers ⌜_:type-specifier⌝ ⌜dss:declaration-specifiers⌝)
(declaration-specifiers ⌜t:type-specifier⌝ ⌜tds⌝)⟧
= (declaration-specifiers ⌜t⌝ ⟦merge ⌜dss⌝ ⌜tds⌝⟧)
⟦merge (declaration-specifiers ⌜ds⌝ ⌜dss:declaration-specifiers⌝)
⌜tds:declaration-specifiers⌝⟧
= (declaration-specifiers ⌜ds⌝ ⟦merge ⌜dss⌝ ⌜tds⌝⟧)`
(GrammarSyntax.NonTerm("merge",[dss,tds]))
fun dstDecl t =
let open GrammarSyntax
in case t
of (NonTerm(s as "declaration",[t as (NonTerm("declaration-specifiers",_)),
t' as (NonTerm("init-declarator-list",_))]))
=> (s,(t,t'))
| (NonTerm(s as "struct-declaration",[t as (NonTerm("specifier-qualifier-list",_)),
t' as (NonTerm("struct-declarator-list",_))]))
=> (s,(t,t'))
| (NonTerm(s as "parameter-declaration",[t as (NonTerm("declaration-specifiers",_)),
t']))
=> (s,(t,t'))
| _ => (printTree t;raise Fail "dstDecl: bad decl")
end
fun mkDecl s (dss,idl) =
let open GrammarSyntax
in (NonTerm(s,[dss,idl]))
end
fun resolve substruct (decls : decls) t =
let open CSyntax
val typedef_decl = #get_typedef decls
val tag_decl = #get_tag decls
fun resolve_decl tags t =
rewritetdpq [("dss",resolve_decl_specs tags),
("scs",resolve_struct_contents tags),
("fail",fn t => (Rewrite.printTree t; raise Fail "resolve_decl: no case"))] `
⟦declaration ⌜x⌝ ⌜y⌝⟧ = (⌜dss(declaration ⌜x⌝ ⌜y⌝)⌝)
⟦declaration ⌜t⌝⟧ = (declaration ⌜scs(⌜t⌝)⌝)
⟦parameter-declaration ⌜x⌝ ⌜y⌝⟧ = (⌜dss(parameter-declaration ⌜x⌝ ⌜y⌝)⌝)
⟦parameter-declaration ⌜t⌝⟧ = (parameter-declaration ⌜scs(⌜t⌝)⌝)
⟦struct-declaration ⌜x⌝ ⌜y⌝⟧ = (⌜dss(struct-declaration ⌜x⌝ ⌜y⌝)⌝)
⟦⌜t⌝⟧ = (⌜fail(⌜t⌝)⌝)` t
and resolve_struct_contents tags t =
let fun resolve_sc (NonTerm("sc",[t])) = foldStructDecl (resolve_decl tags) t
| resolve_sc (NonTerm("sc",[Term(name),t])) = foldStructDecl (resolve_decl (name::tags)) t
| resolve_sc t = (Rewrite.printTree t; raise Fail "resolve_sc: no case")
in rewritetdpq [("resolve",resolve_sc),
("split",split_structdecl_list),
("fail",fn t => (Rewrite.printTree t;
raise Fail "resolve_struct_contents: no case"))] `
⟦declaration-specifiers ⌜t⌝⟧ = (declaration-specifiers ⟦⌜t⌝⟧)
⟦type-specifier ⌜t⌝⟧ = (type-specifier ⟦⌜t⌝⟧)
⟦struct-or-union-specifier
⌜su:struct-or-union⌝
⌜id:=(identifier ⌜n⌝)⌝
(struct-contents ⌜t⌝)⟧ =
(struct-or-union-specifier
⌜su⌝
⌜id⌝
(struct-contents ⌜resolve(sc ⌜n⌝ ⌜split(⌜t⌝)⌝)⌝))
⟦struct-or-union-specifier
⌜su:struct-or-union⌝
(struct-contents ⌜t⌝)⟧ =
(struct-or-union-specifier
⌜su⌝
(struct-contents ⌜resolve(sc ⌜split(⌜t⌝)⌝)⌝))
⟦⌜t⌝⟧ = (⌜fail(⌜t⌝)⌝)` t
end
and resolve_decl_specs tags t =
let val fname = "resolve_decl_specs"
val (decl_class,(tdd,tdid)) = dstDecl (debug fname "" t)
in case decl_types (decl_specs tdd)
of [NonTerm("typedef-name",[Term(n)])] =>
let val tddecl = (typedef_decl n)
handle Subscript => raise Fail (fname^": can't find typedef "^n)
val td = debug fname "resolve_decl returned" (resolve_decl tags tddecl)
val td' = if decl_class = "struct-declaration"
then debug fname "mergestructtq returned"
(mergestructtq tdd
(debug fname "specqualrw returned" (specqualrw td)))
else debug fname "mergedecltq returned" (mergedecltq tdd td)
val (_,(tdd',tdid')) = dstDecl (td')
val rtd = debug fname "resolve_decls returned" (resolve_decls tags tdid tdid')
in elimtdrw (mkDecl decl_class (tdd', rtd))
end
| [NonTerm("struct-or-union-specifier",
[su,id as NonTerm("identifier",
[Term(name)])])] =>
let val tdopt = SOME (tag_decl name) handle Subscript => NONE
val nop = fn () => mkDecl
decl_class
(mergedss tdd
(NonTerm("declaration-specifiers",
[NonTerm("type-specifier",
[NonTerm("struct-or-union-specifier",
[su,id])])])),
tdid)
in if not substruct then nop () else
case tdopt
of NONE => nop ()
| SOME td =>
if List.exists (fn s => s = name) tags
then nop ()
else resolve_decl (name::tags)
(mkDecl
decl_class
(mergedss tdd
(NonTerm("declaration-specifiers",
[NonTerm("type-specifier",
[NonTerm("struct-or-union-specifier",
[su,id,td])])])),
tdid))
end
| [NonTerm("struct-or-union-specifier",
[su,id as NonTerm("identifier",
[Term(name)]) ,NonTerm("struct-contents",[sc])])] =>
let val sc' = foldStructDecl
(resolve_decl (name::tags))
(split_structdecl_list sc)
val td = NonTerm("struct-contents",[sc'])
in mkDecl
decl_class
(mergedss tdd (NonTerm("declaration-specifiers",
[NonTerm("type-specifier",
[NonTerm("struct-or-union-specifier",
[su,id,td])])])),
tdid)
end
| [NonTerm("enum-specifier",_)] =>
let
in mkDecl
decl_class
(mergedss tdd (NonTerm("declaration-specifiers",
[NonTerm("type-specifier",
[Term("int")])])),
tdid)
end
| ((Term_)::_) =>
let val rtd = debug fname "resolve_decls returned"
(resolve_decls tags tdid
(NonTerm("init-declarator-list",
[NonTerm("init-declarator",
[NonTerm("declarator",
[NonTerm("direct-declarator",
[NonTerm("identifier",
[Term("fred")])])])])])))
in mkDecl decl_class (tdd, rtd)
end
| _ => t
end
and new_resolve_decls tags t t' =
let val fold_pl = foldParameterDecl (resolve_decl tags)
in rewritetdpq [("fold",fold_pl),
("fail",fn t => (Rewrite.printTree t;
raise Fail "resolve_decls: no case"))] `
⟦resolve (init-declarator-list ⌜t1⌝)
(init-declarator-list ⌜t2⌝)⟧ =
(init-declarator-list ⟦resolve ⌜t1⌝ ⌜t2⌝⟧)
⟦resolve ⌜t1:declarator⌝
(init-declarator-list
(init-declarator ⌜t2:declarator⌝))⟧ =
(⟦resolve ⌜t1⌝ ⌜t2⌝⟧)
⟦resolve
(init-declarator ⌜t1⌝)
(init-declarator ⌜t2⌝)⟧ =
(init-declarator ⟦resolve ⌜t1⌝ ⌜t2⌝⟧)
⟦resolve
(struct-declarator-list ⌜t1⌝)
(struct-declarator-list ⌜t2⌝)⟧ =
(struct-declarator-list ⟦resolve ⌜t1⌝ ⌜t2⌝⟧)
⟦resolve
(struct-declarator ⌜t1⌝)
(struct-declarator ⌜t2⌝)⟧ =
(struct-declarator ⟦resolve ⌜t1⌝ ⌜t2⌝⟧)
⟦resolve
(declarator
(direct-declarator
⌜dd:direct-declarator⌝
(parameter-type-list ⌜l:parameter-list⌝)))
(declarator
(direct-declarator ⌜_:identifier⌝))⟧ =
(declarator
(direct-declarator
⌜dd⌝
(parameter-type-list ⌜fold(⌜l⌝)⌝)))
⟦resolve
(declarator
⌜p⌝
(direct-declarator
⌜dd:direct-declarator⌝
(parameter-type-list ⌜l:parameter-list⌝)))
(declarator
(direct-declarator ⌜_:identifier⌝))⟧ =
(declarator
⌜p⌝
(direct-declarator
⌜dd⌝
(parameter-type-list ⌜fold(⌜l⌝)⌝)))
⟦resolve ⌜t⌝
(declarator
(direct-declarator ⌜_:identifier⌝))⟧ = (⌜t⌝)
⟦resolve
(declarator
⌜p1:pointer⌝
⌜d1:direct-declarator⌝)
(declarator
(pointer ⌜p2:pointer⌝)
⌜d2:direct-declarator⌝)⟧ =
(⟦resolve (declarator
⌜p1⌝ ⌜d1⌝)
(declarator
(pointer ⌜p2⌝ ⌜d2⌝))⟧)
` end
and mergepointer t = rewriteq `
⟦merge ⌜x:type-qualifier⌝
(pointer ⌜w:type-qualifier-list⌝ ⌜z⌝)⟧
= (pointer (type-qualifier-list ⌜w⌝ ⌜x⌝) ⌜z⌝)
⟦merge ⌜x:type-qualifier⌝
(pointer ⌜z⌝)⟧
= (pointer (type-qualifier-list ⌜x⌝) ⌜z⌝)
⟦merge ⌜x:type-qualifier⌝
(pointer)⟧
= (pointer (type-qualifier-list ⌜x⌝))` t
and resolve_decls tags t t' =
let fun iter (NonTerm("init-declarator-list",[t]))
(NonTerm("init-declarator-list",[t'])) =
NonTerm("init-declarator-list",[iter t t'])
| iter (t as NonTerm("declarator",_))
(NonTerm("init-declarator-list",
[NonTerm("init-declarator",
[t' as NonTerm("declarator",_)])])) =
iter t t'
| iter (t as NonTerm("abstract-declarator",_))
(NonTerm("init-declarator-list",
[NonTerm("init-declarator",
[t' as NonTerm("declarator",_)])])) =
iter t t'
| iter (NonTerm("init-declarator",[t]))
(NonTerm("init-declarator",[t'])) =
NonTerm("init-declarator",[iter t t'])
| iter (NonTerm("struct-declarator-list",
[NonTerm("struct-declarator",[t])]))
(NonTerm("init-declarator-list",
[NonTerm("init-declarator",
[t' as NonTerm("declarator",_)])])) =
iter t t'
| iter (NonTerm("struct-declarator-list",[t]))
(NonTerm("struct-declarator-list",[t'])) =
NonTerm("struct-declarator-list",[iter t t'])
| iter (NonTerm("struct-declarator",[t]))
(NonTerm("struct-declarator",[t'])) =
NonTerm("struct-declarator",[iter t t'])
| iter (NonTerm("declarator",
[NonTerm("direct-declarator",
[dd as NonTerm("direct-declarator",_),
NonTerm("parameter-type-list",
[pl as NonTerm("parameter-list",_)])])]))
(NonTerm("declarator",
[NonTerm("direct-declarator",
[NonTerm("identifier",_)])])) =
(NonTerm("declarator",
[NonTerm("direct-declarator",
[dd,NonTerm("parameter-type-list",
[foldParameterDecl (resolve_decl tags) pl])])]))
| iter (NonTerm("declarator",
[p,
NonTerm("direct-declarator",
[dd as NonTerm("direct-declarator",_),
NonTerm("parameter-type-list",
[pl as NonTerm("parameter-list",_)])])]))
(NonTerm("declarator",
[NonTerm("direct-declarator",
[NonTerm("identifier",_)])])) =
(NonTerm("declarator",
[p, NonTerm("direct-declarator",
[dd,NonTerm("parameter-type-list",
[foldParameterDecl (resolve_decl tags) pl])])]))
| iter t (NonTerm("declarator",
[NonTerm("direct-declarator",
[NonTerm("identifier",_)])])) = t
(* | iter t (NonTerm("declarator",
[NonTerm("direct-declarator",
[d as NonTerm("declarator",_)])])) =
NonTerm("declarator",
[NonTerm("direct-declarator",
[iter t d])]) *)
| iter (NonTerm("declarator",
[p as NonTerm("pointer",_),
d as NonTerm("direct-declarator",_)]))
(NonTerm("declarator",
[NonTerm("pointer",l as [NonTerm("pointer",_)]),
d' as NonTerm("direct-declarator",_)])) =
iter (NonTerm("declarator",[NonTerm("pointer",[p]),d]))
(NonTerm("declarator",l@[d']))
| iter (NonTerm("declarator",
[p as NonTerm("pointer",_),
d as NonTerm("direct-declarator",_)]))
(NonTerm("declarator",
[NonTerm("pointer",l),
d' as NonTerm("direct-declarator",_)])) =
iter (NonTerm("declarator",[NonTerm("pointer",l@[p]),d]))
(NonTerm("declarator",[d']))
| iter (NonTerm("declarator",
[d as NonTerm("direct-declarator",_)]))
(NonTerm("declarator", [NonTerm("pointer",l as [NonTerm("pointer",_)]),
d' as NonTerm("direct-declarator", _)])) =
iter (NonTerm("declarator",[NonTerm("pointer",[]),d]))
(NonTerm("declarator",l@[d']))
| iter (NonTerm("declarator",
[d as NonTerm("direct-declarator",_)]))
(NonTerm("declarator", [p as NonTerm("pointer",_),
d' as NonTerm("direct-declarator", _)])) =
iter (NonTerm("declarator",[p,d]))
(NonTerm("declarator",[d']))
| iter (NonTerm("declarator",
[d as NonTerm("direct-declarator",_)]))
(NonTerm("declarator",
[NonTerm("direct-declarator",
[dd,ad as NonTerm("array-declarator",_)])])) =
iter (NonTerm("declarator",[NonTerm("direct-declarator",[d,ad])]))
(NonTerm("declarator",[dd]))
| iter (d as (NonTerm("declarator",
[NonTerm("direct-declarator",_)])))
(NonTerm("declarator",
[NonTerm("direct-declarator",
[NonTerm("direct-declarator",[d']),
ptl as NonTerm("parameter-type-list",_)])])) =
(NonTerm("declarator",
[NonTerm("direct-declarator",
[NonTerm("direct-declarator",
[iter d d'])]),ptl]))
| iter t t' = (Rewrite.printTreeDirect 0 t; Rewrite.printTreeDirect 0 t';
raise Fail "resolve_decls: no case")
in iter t t'
end
in resolve_decl [] t
end
fun findpmacros (pat,pat') =
let val regex = Regex.regcomp pat [Regex.Extended]
val regex' = Regex.regcomp pat' [Regex.Extended]
in List.filter (fn (n,_,d) => Regex.regexecBool regex [] n andalso
Regex.regexecBool regex' [] d)
end
val rewrite_typedef =
rewriteq `
⟦declaration
⌜w:declaration-specifiers⌝
(init-declarator-list
⌜x:init-declarator-list⌝
⌜y:init-declarator⌝)⟧ =
(declaration-list
(declaration-list
⟦declaration ⌜w⌝ ⌜x⌝⟧)
(declaration ⌜w⌝ (init-declarator-list ⌜y⌝)))`
val td = absyntq `
typedef unsigned int ui, *pui, ( *funcp) (int i);
`
val td = parseq "structdef" "" `
typedef unsigned int *pui;
typedef unsigned int ui;
struct tag {const pui puiv, *ppui; const ui *pcui;};
extern ui func (const ui *pcui);
static const ui *puiv;
static const pui *ppuiv;
typedef struct tag *ps;`
val _ = printTree (#get_tag td "tag")
val _ = printTree (resolve true td (#get_decl td "ps"))
val _ = printTree (resolve true td (#get_decl td "puiv"))
val _ = printTree (resolve true td (#get_decl td "ppuiv"))
val _ = printTree (resolve true td (#get_decl td "func"))
val fdecl = parseq "fdecl" "" `
typedef unsigned int ui;
typedef ui ( *funcp ) (ui i);
extern ui *func (ui i, const funcp cfp[]);
`;
val _ = printTree (#get_decl fdecl "func")
val _ = printTree (resolve true fdecl (#get_decl fdecl "func"))
val _ = printTree (#get_typedef fdecl "ui")
val _ = printTree (resolve true fdecl (#get_decl fdecl "funcp"))
val cpdecl = parseq "cpdecl" "" `
typedef unsigned int *pui;
typedef const pui *cppui;
typedef unsigned int * const *cppui2;
`;
val _ = printTree (resolve true cpdecl (#get_typedef cpdecl "cppui"))
val _ = printTree (#get_typedef cpdecl "cppui2")
val ctdecl = parseq "ctdecl" "" `
typedef long int size_t;
typedef long unsigned int __off_t;
typedef void *ptr_t;
extern void *mmap (void *__addr, size_t __len, int __prot, int __flags, int __fd, __off_t __offset);
`;
val _ = printTree (resolve true ctdecl (#get_typedef ctdecl "size_t"))
val _ = printTree (resolve true ctdecl (#get_decl ctdecl "mmap"))
val enumdecl = parseq "enumdecl" "" `
typedef enum scm_t_foreign_type
{
SCM_FOREIGN_TYPE_VOID,
SCM_FOREIGN_TYPE_FLOAT,
SCM_FOREIGN_TYPE_DOUBLE,
SCM_FOREIGN_TYPE_UINT8,
SCM_FOREIGN_TYPE_INT8,
SCM_FOREIGN_TYPE_UINT16,
SCM_FOREIGN_TYPE_INT16,
SCM_FOREIGN_TYPE_UINT32,
SCM_FOREIGN_TYPE_INT32,
SCM_FOREIGN_TYPE_UINT64,
SCM_FOREIGN_TYPE_INT64,
SCM_FOREIGN_TYPE_LAST = SCM_FOREIGN_TYPE_INT64
} scm_t_foreign_type;
`;
val _ = printTree ((#get_decl enumdecl "scm_t_foreign_type"))
val _ = printTree ((#get_tag enumdecl "scm_t_foreign_type"))
val _ = print (#define_enum enumdecl "scm_t_foreign_type")
(*
val _ = Meta.quietdec := true
val gdkdecls = parse "gdk/gdk.h" "$(pkg-config --cflags-only-I gdk-3.0)" "#include \"gdk/gdk.h\"\n"
val _ = Meta.quietdec := false;
val _ = #print_decl gdkdecls "gdk_window_new";
val _ = Meta.exec (#define_enum gdkdecls "GdkEventType");
val _ = printTree (resolve true gdkdecls (#get_decl gdkdecls "_GdkWindowAttr"))
val _ = printTree (resolve true gdkdecls (#get_typedef gdkdecls "GdkWindowAttr"))
val _ = printTree (resolve true gdkdecls (#get_typedef gdkdecls "GdkWindow"))
val _ = printTree (resolve true gdkdecls (#get_decl gdkdecls "gdk_window_new"))
val _ = printTree (resolve false gdkdecls (#get_decl gdkdecls "gdk_window_new"))
*)
(* val _ = GrammarSyntax.debug_on "tree_rewrite_param" (GrammarSyntax.On 10)
val _ = GrammarSyntax.debug_on "resolve_decl_specs" (GrammarSyntax.On 10)
val _ = GrammarSyntax.debug_on "matches" (GrammarSyntax.On 10)
val _ = GrammarSyntax.debug_on "tree_tree_list_subst_leaves" (GrammarSyntax.On 10) *)
fun findmacros (pat,pat') =
let val regex = Regex.regcomp pat [Regex.Extended]
val regex' = Regex.regcomp pat' [Regex.Extended]
in List.filter (fn (n,d) => Regex.regexecBool regex [] n andalso
Regex.regexecBool regex' [] d)
end
val evalStringInEnv = fn env => (CSyntax.eval env) o RewriteMain.parse_c_cexp_string
val evalString = evalStringInEnv []
val ascval = fn ((n,d),(n',d')) => (Word.compare (evalString d, evalString d'))
val ascnm = fn ((n,d),(n',d')) => (String.compare (n, n'))
val sortByAscVal = Listsort.sort ascval
val sortByAscName = Listsort.sort ascnm
val IntegerConstant = "^(0x[0-9a-fA-F]+|[0-9]+)[LUlu]*$"
val mmandecls = parse "sys/mman.h" "" "#include <unistd.h>\n#include <sys/mman.h>\n"
val simple = List.filter (fn (_,(_,(NONE,_))) => true | _ => false)
val mmapmacros = List.map (fn (n,(_,(a,d))) => (n,d)) (simple ((#macros mmandecls)()))
val mmapraw = printTree (#get_decl mmandecls "mmap")
val mmap = printTree (resolve false mmandecls (#get_decl mmandecls "mmap"))
val getpagesize = printTree (resolve false mmandecls (#get_decl mmandecls "getpagesize"))
val munmap = printTree (resolve true mmandecls (#get_decl mmandecls "munmap"))
val msync = printTree (resolve true mmandecls (#get_decl mmandecls "msync"))
val mprotect = printTree (resolve false mmandecls (#get_decl mmandecls "mprotect"))
val macs = (sortByAscVal o (findmacros ("^MAP_.*",IntegerConstant))) mmapmacros
val macs' = List.map (fn (n,d) => (n,evalString d)) macs
val dt = CSyntax.enum_datatype [("MMap",macs')] "MMap"
val _ = Meta.exec dt
val macs'' = (sortByAscVal o (findmacros ("^PROT_.*",IntegerConstant))) mmapmacros
val macs''' = List.map (fn (n,d) => (n,evalString d)) macs''
val dt'' = CSyntax.enum_datatype [("MMapProt",macs''')] "MMapProt"
val _ = Meta.exec dt''
val smacs'' = (sortByAscVal o (findmacros ("^MS_.*",IntegerConstant))) mmapmacros
val smacs''' = List.map (fn (n,d) => (n,evalString d)) smacs''
val sdt'' = CSyntax.enum_datatype [("MSync",smacs''')] "MSync"
val _ = Meta.exec sdt'';
structure MMapProtBits = BitSet(structure Enum = MMapProt)
structure MMapBits = BitSet(structure Enum = MMap)
structure MSyncBits = BitSet(structure Enum = MSync)
val scetd = #define_enum mmandecls "SC_Consts"
val _ = Meta.exec scetd
val csetd = #define_enum mmandecls "CS_Consts"
val _ = Meta.exec csetd
val pcetd = #define_enum mmandecls "PC_Consts"
val _ = Meta.exec pcetd;
val posix_consts'' = (sortByAscName o (findmacros ("^_POSIX_.*",".*"))) mmapmacros
val smacs''' = List.map (fn (n,d) => (n,evalString d)) posix_consts''
val pcnstdt'' = CSyntax.enum_datatype [("Posix",smacs''')] "Posix"
val _ = Meta.exec pcnstdt'';
val pcs = List.map (fn v => (Posix.toString v,Word.toInt (Posix.toWord v)
handle Overflow => (~1))) Posix.flags
val posix2_consts = (sortByAscName o (findmacros ("^__POSIX2.*",".*"))) mmapmacros
val posix2_consts' = List.map (fn (n,v) => (CSyntax.mlvar n, evalString v)) posix2_consts
val posix2_consts'' = (sortByAscName o (findmacros ("^_POSIX2.*",".*"))) mmapmacros
val posix2_consts''' = List.map (fn (n,v) => (n,CSyntax.mlvar v)) posix2_consts''
val smacs''' = List.map (fn (n,d) => (n,evalStringInEnv posix2_consts' d)) (posix2_consts''')
val p2cnstdt'' = CSyntax.enum_datatype [("Posix2",smacs''')] "Posix2"
val _ = Meta.exec p2cnstdt'';
val p2cs = List.map (fn v => (Posix2.toString v,Word.toInt (Posix2.toWord v))) Posix2.flags;
val dlxh = Dynlib.dlopen {lib = "", flag = Dynlib.RTLD_LAZY, global = false}
(*
#define Push_roots(name, size) \
value name [(size) + 2]; \
{ long _; for (_ = 0; _ < (size); name [_++] = Val_long (0)); } \
name [(size)] = (value) (size); \
name [(size) + 1] = (value) c_roots_head; \
c_roots_head = &(name [(size)]);
#define Pop_roots() {c_roots_head = (value* ) c_roots_head [1]; }
*)
val c_roots_headp = Jit.Pointer (Ffi.svec_getcptrvalue (Dynlib.cptr (Dynlib.dlsym dlxh "c_roots_head")))
val sysconfp = Jit.Pointer (Ffi.svec_getcptrvalue (Dynlib.cptr (Dynlib.dlsym dlxh "sysconf")))
val pathconfp = Jit.Pointer (Ffi.svec_getcptrvalue (Dynlib.cptr (Dynlib.dlsym dlxh "pathconf")))