-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathpasses.jl
1230 lines (1074 loc) · 43.3 KB
/
passes.jl
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
"""
AbstractPass
Supertype for all passes.
"""
abstract type AbstractPass end
"""
CollectTopLevelNode <: AbstractPass
In this pass, all tags and identifiers in all translation units are collected for
CTU analysis.
See also [`collect_top_level_nodes!`](@ref).
"""
mutable struct CollectTopLevelNode <: AbstractPass
trans_units::Vector{TranslationUnit}
dependent_headers::Vector{String}
system_dirs::Vector{String}
show_info::Bool
end
CollectTopLevelNode(tus, dhs, sys; info=false) = CollectTopLevelNode(tus, dhs, sys, info)
function (x::CollectTopLevelNode)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
is_local_only = get(general_options, "is_local_header_only", true)
show_info = get(log_options, "CollectTopLevelNode_log", x.show_info)
empty!(dag.nodes)
empty!(dag.sys)
for tu in x.trans_units
tu_cursor = getTranslationUnitCursor(tu)
header_name = normpath(spelling(tu_cursor))
@info "Processing header: $header_name"
for cursor in children(tu_cursor)
file_name = get_filename(cursor)
# Ignore items where no source file is available.
# e.g. built-in defines like `__llvm__` and `__clang__`.
if !is_local_only && isempty(file_name)
continue
end
file_name = normpath(file_name)
if is_local_only && header_name != file_name && file_name ∉ x.dependent_headers
if any(sysdir -> startswith(file_name, sysdir), x.system_dirs)
collect_top_level_nodes!(dag.sys, cursor, general_options)
end
continue
end
collect_top_level_nodes!(dag.nodes, cursor, general_options)
end
end
return dag
end
"""
CollectDependentSystemNode <: AbstractPass
In this pass, those dependent tags/identifiers are to the `dag.nodes`.
See also [`collect_system_nodes!`](@ref).
"""
mutable struct CollectDependentSystemNode <: AbstractPass
dependents::Dict{ExprNode,Int}
show_info::Bool
end
CollectDependentSystemNode(; info=true) = CollectDependentSystemNode(Dict{ExprNode,Int}(), info)
# FIXME: refactor and improve the support for system nodes
function (x::CollectDependentSystemNode)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "CollectDependentSystemNode_log", x.show_info)
empty!(x.dependents)
for node in dag.nodes
collect_dependent_system_nodes!(dag, node, x.dependents)
end
isempty(x.dependents) && return dag
new_deps = copy(x.dependents)
old_deps = copy(x.dependents)
while true
for (node, i) in new_deps
collect_dependent_system_nodes!(dag, node, x.dependents)
end
new_deps = setdiff(x.dependents, old_deps)
isempty(new_deps) && break
old_deps = copy(x.dependents)
end
show_info && @warn "[CollectDependentSystemNode]: found symbols in the system headers: $([n.id for (n,v) in x.dependents])"
prepend!(dag.nodes, collect(v[1] for v in sort(collect(x.dependents), by=x->x[2])))
return dag
end
"""
IndexDefinition <: AbstractPass
In this pass, the indices of struct/union/enum tags and those of function/typedef/macro
identifiers are cached in the DAG for future use. Note that, the "Definition" in the type
name means a little bit more.
The `adj` list of each node is also cleared in this pass.
"""
mutable struct IndexDefinition <: AbstractPass
show_info::Bool
end
IndexDefinition(; info=false) = IndexDefinition(info)
function (x::IndexDefinition)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "IndexDefinition_log", x.show_info)
empty!(dag.tags)
empty!(dag.ids)
for (i, node) in enumerate(dag.nodes)
!isempty(node.adj) && empty!(node.adj)
if is_tag_def(node)
if haskey(dag.tags, node.id)
n = dag.nodes[dag.tags[node.id]]
if !is_same(n.cursor, node.cursor)
file1, line1, col1 = get_file_line_column(n.cursor)
file2, line2, col2 = get_file_line_column(node.cursor)
@error "duplicated definitions should be exactly the same! [DEBUG]: identifier $(n.cursor) at $(normpath(file1)):$line1:$col1 is different from identifier $(node.cursor) at $(normpath(file2)):$line2:$col2"
end
show_info && @info "[IndexDefinition]: marked an indexed tag $(node.id) at nodes[$i]"
ty = dup_type(node.type)
dag.nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
else
show_info && @info "[IndexDefinition]: indexing tag $(node.id) at nodes[$i]"
dag.tags[node.id] = i
end
end
if is_identifier(node)
if haskey(dag.ids, node.id)
show_info &&
@info "[IndexDefinition]: found duplicated identifier $(node.id) at nodes[$i]"
ty = dup_type(node.type)
dag.nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
else
show_info && @info "[IndexDefinition]: indexing identifier $(node.id) at nodes[$i]"
dag.ids[node.id] = i
end
end
end
return dag
end
"""
CollectNestedRecord <: AbstractPass
In this pass, nested record nodes are collected and pushed into the DAG.
"""
mutable struct CollectNestedRecord <: AbstractPass
show_info::Bool
end
CollectNestedRecord(; info=false) = CollectNestedRecord(info)
function (x::CollectNestedRecord)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "CollectNestedRecord_log", x.show_info)
use_deterministic_sym = get(general_options, "use_deterministic_symbol", false)
new_tags = Dict{Symbol,Int}()
for (id, i) in dag.tags
node = dag.nodes[i]
!is_record(node) && continue
is_dup_tagtype(node) && continue
collect_nested_record!(dag, node, new_tags, use_deterministic_sym)
end
merge!(dag.tags, new_tags)
return dag
end
"""
FindOpaques <: AbstractPass
Those opaque structs/unions/enums are marked in this pass.
"""
mutable struct FindOpaques <: AbstractPass
show_info::Bool
end
FindOpaques(; info=false) = FindOpaques(info)
function (x::FindOpaques)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "FindOpaques_log", x.show_info)
for (i, node) in enumerate(dag.nodes)
is_forward_decl(node) || continue
if !haskey(dag.tags, node.id)
ty = opaque_type(node.type)
dag.nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
dag.tags[node.id] = i
show_info &&
@info "[FindOpaques]: marked an opaque tag-type $(node.id)"
end
end
return dag
end
"""
LinkTypedefToAnonymousTagType <: AbstractPass
In this pass, the id info of anonymous tag-type nodes is added to those typedef nodes that
directly/indirectly have a reference to these nodes. The id info is injected in the upstream
passes.
"""
mutable struct LinkTypedefToAnonymousTagType <: AbstractPass
cache::Dict{Int,Vector{Int}}
is_system::Bool
show_info::Bool
end
LinkTypedefToAnonymousTagType(;is_system=false, info=false) = LinkTypedefToAnonymousTagType(Dict(), is_system, info)
function (x::LinkTypedefToAnonymousTagType)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "LinkTypedefToAnonymousTagType_log", x.show_info)
empty!(x.cache)
nodes = x.is_system ? dag.sys : dag.nodes
# loop through all the nodes to cache the indices of all the typedefs that refer to
# an anonymous tag-type
for i in 1:(length(nodes) - 1)
cur = nodes[i]
is_anonymous(cur) || continue
x.cache[i] = Int[]
# since an anonymous tag-type may have mutiple typedefs, we need another loop
for j in (i + 1):length(nodes)
n = nodes[j]
is_typedef(n) || break # keep searching until we hit a non-typedef node
refback = children(n.cursor)
if !isempty(refback) && first(refback) == cur.cursor
push!(x.cache[i], j)
end
end
end
# loop through all anonymous tag-types and apply node editing
for (k, v) in x.cache
isempty(v) && continue # skip non-typedef anonymous tag-types e.g. enum
anonymous = nodes[k]
for i in v
node = nodes[i]
ty = TypedefToAnonymous(anonymous.id)
nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
show_info &&
@info "[LinkTypedefToAnonymousTagType_log]: store $(anonymous.cursor)'s id to typedef node $(node.id)"
end
end
return dag
end
"""
ResolveDependency <: AbstractPass
In this pass, the `adj` list of each node in the DAG is populated by its parent definition
nodes. Make sure you run the [`IndexDefinition`](@ref) pass before this pass.
Note that, in the case of circular forward decls, circular dependencies may be introduced
in the DAG, this should be handled in the downstream passes.
See also [`resolve_dependency!`](@ref)
"""
mutable struct ResolveDependency <: AbstractPass
show_info::Bool
end
ResolveDependency(; info=false) = ResolveDependency(info)
function (x::ResolveDependency)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "ResolveDependency_log", x.show_info)
general_options["nested_tags"] = collect_nested_tags(dag)
for node in dag.nodes
resolve_dependency!(dag, node, general_options)
unique!(node.adj) # FIXME: check this
if show_info
deps = Dict(n => dag.nodes[n] for n in node.adj)
@info "[ResolveDependency]: resolved dependency for $(node.cursor)" deps
end
end
delete!(general_options, "nested_tags")
return dag
end
@enum DFSMarkStatus begin
UNMARKED = 0x00
TEMPORARY = 0x01
PERMANENT = 0x02
end
"""
RemoveCircularReference <: AbstractPass
In this pass, circular dependencies that are introduced by mutually referenced structs are
removed, so we can do a topological sort on the DAG.
Make sure you run the [`ResolveDependency`](@ref) pass before this pass.
"""
mutable struct RemoveCircularReference <: AbstractPass
show_info::Bool
end
RemoveCircularReference(; info=false) = RemoveCircularReference(info)
function detect_cycle!(nodes, marks, cycle, i)
node = nodes[i]
mark = marks[i]
# skip if this node has already been visited
mark == PERMANENT && return cycle
# hit a backward edge, record the index
if mark == TEMPORARY
push!(cycle, i)
return cycle
end
marks[i] = TEMPORARY
for n in node.adj
detect_cycle!(nodes, marks, cycle, n)
if !isempty(cycle)
push!(cycle, i)
return cycle
end
end
marks[i] = PERMANENT
return cycle
end
function is_non_pointer_ref(child, parent)
is_non_pointer_dep = false
for fc in fields(getCursorType(child.cursor))
fty = getCursorType(fc)
is_jl_pointer(tojulia(fty)) && continue
c = getTypeDeclaration(fty)
if c == parent.cursor
is_non_pointer_dep = true
end
end
return is_non_pointer_dep
end
const MAX_CIRCIR_DETECTION_COUNT = 100000
function (x::RemoveCircularReference)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "RemoveCircularReference_log", x.show_info)
marks = fill(UNMARKED, size(dag.nodes))
count = 0
while any(x -> x != PERMANENT, marks) && (count += 1) < MAX_CIRCIR_DETECTION_COUNT
fill!(marks, UNMARKED)
for (node_idx, node) in enumerate(dag.nodes)
marks[node_idx] == UNMARKED || continue
cycle = Int[]
detect_cycle!(dag.nodes, marks, cycle, node_idx)
isempty(cycle) && continue
# firstly remove cycle reference caused by mutually referenced structs
typedef_only = true
for i in 1:(length(cycle) - 1)
np, nc = cycle[i], cycle[i + 1]
parent, child = dag.nodes[np], dag.nodes[nc]
if child.type isa AbstractStructNodeType
# only pointer references can be safely removed
if !is_non_pointer_ref(child, parent)
typedef_only = false
idx = findfirst(x -> x == np, child.adj)
deleteat!(child.adj, idx)
id = child.id
ty = StructMutualRef()
dag.nodes[nc] = ExprNode(id, ty, child.cursor, child.exprs, child.adj)
show_info &&
@info "[RemoveCircularReference]: removed $(child.id)'s dependency $(parent.id)"
# Now the cycle is broken and we don't need to look at
# any other nodes in the cycle path.
break
end
end
# exit earlier
(node_idx + 1) == first(cycle) && break
end
# there are cases where the circular reference can only be de-referenced at a
# typedef, so we do the for-loop another round for that.
if typedef_only
for i in 1:(length(cycle) - 1)
np, nc = cycle[i], cycle[i + 1]
parent, child = dag.nodes[np], dag.nodes[nc]
is_typedef_elaborated(child) || continue
jlty = tojulia(getTypedefDeclUnderlyingType(child.cursor))
# make sure the underlying type is a pointer
is_jl_pointer(jlty) || continue
empty!(child.adj)
id = child.id
ty = TypedefMutualRef()
dag.nodes[nc] = ExprNode(id, ty, child.cursor, child.exprs, child.adj)
show_info &&
@info "[RemoveCircularReference]: removed $(child.id)'s dependency $(parent.id)"
# exit earlier
break
end
end
# whenever a cycle is found, we reset all of the marks and restart again
# FIXME: optimize this
break
end
end
if count == MAX_CIRCIR_DETECTION_COUNT
culprits = map(node for (i, node) in enumerate(dag.nodes) if marks[i] != PERMANENT) do node
file, line, column = get_file_line_column(node.cursor)
"$(node.id) at $file:$line:$column"
end
@error "10 suggested culprits: $(culprits[1:min(end, 10)])"
error("Could not remove circular reference after $MAX_CIRCIR_DETECTION_COUNT trials.")
end
return dag
end
"""
TopologicalSort <: AbstractPass
Make sure you run the [`RemoveCircularReference`](@ref) pass before this pass.
"""
mutable struct TopologicalSort <: AbstractPass
show_info::Bool
end
TopologicalSort(; info=false) = TopologicalSort(info)
function dfs_visit!(list, nodes, marks, i)
node = nodes[i]
mark = marks[i]
mark == PERMANENT && return nothing
@assert mark != TEMPORARY
marks[i] = TEMPORARY
for n in node.adj
dfs_visit!(list, nodes, marks, n)
end
marks[i] = PERMANENT
push!(list, node)
return nothing
end
function (x::TopologicalSort)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "TopologicalSort_log", x.show_info)
show_info && @info "[TopologicalSort]: sorting the DAG ..."
marks = fill(UNMARKED, size(dag.nodes))
list = []
for (i, node) in enumerate(dag.nodes)
marks[i] == UNMARKED || continue
dfs_visit!(list, dag.nodes, marks, i)
end
dag.nodes .= list
# clean up indices because they are no longer valid
empty!(dag.tags)
empty!(dag.ids)
return dag
end
"""
CatchDuplicatedAnonymousTags <: AbstractPass
Most duplicated tags are marked as StructDuplicated/UnionDuplicated/EnumDuplicated except
those anonymous tag-types. That's why this pass is necessary.
"""
mutable struct CatchDuplicatedAnonymousTags <: AbstractPass
show_info::Bool
end
CatchDuplicatedAnonymousTags(; info=false) = CatchDuplicatedAnonymousTags(info)
function (x::CatchDuplicatedAnonymousTags)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "CatchDuplicatedAnonymousTags_log", x.show_info)
for (i, node) in enumerate(dag.nodes)
!haskey(dag.tags, node.id) && continue
is_dup_tagtype(node) && continue
# `is_anonymous` cannot be used here because the node type may have been changed.
sid = string(node.id)
!(startswith(sid, "##Ctag") || startswith(sid, "__JL_Ctag")) && continue
for (id2, idx2) in dag.tags
node2 = dag.nodes[idx2]
node == node2 && continue
is_dup_tagtype(node2) && continue
!(startswith(sid, "##Ctag") || startswith(sid, "__JL_Ctag")) && continue
!is_same_loc(node.cursor, node2.cursor) && continue
show_info &&
@info "[CatchDuplicatedAnonymousTags]: found duplicated anonymous tag-type $(node2.id) at dag.nodes[$idx2]."
ty = dup_type(node2.type)
dag.nodes[idx2] = ExprNode(node2.id, ty, node2.cursor, node2.exprs, node2.adj)
end
end
end
"""
LinkEnumAlias <: AbstractPass
Link hard-coded enum types to the corresponding enums. This pass only works in `no_audit` mode.
"""
mutable struct LinkEnumAlias <: AbstractPass
show_info::Bool
end
LinkEnumAlias(; info=false) = LinkEnumAlias(info)
function (x::LinkEnumAlias)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "LinkEnumAlias_log", x.show_info)
for (id,i) in dag.ids
node = dag.nodes[i]
node.type isa AbstractTypedefNodeType || continue
ty = getTypedefDeclUnderlyingType(node.cursor) |> getCanonicalType
typeKind = kind(ty)
typeKind == CXType_Int || typeKind == CXType_Short || typeKind == CXType_Long || typeKind == CXType_LongLong ||
typeKind == CXType_UInt || typeKind == CXType_UShort || typeKind == CXType_ULong || typeKind == CXType_ULongLong ||
continue
for (tagid, j) in dag.tags
dag.nodes[j].type isa AbstractEnumNodeType || continue
tagid == id || continue
dag.nodes[i] = ExprNode(id, SoftSkip(), node.cursor, node.exprs, node.adj)
show_info &&
@info "[LinkEnumAlias]: skip $id at dag.nodes[$i]."
end
end
end
"""
DeAnonymize <: AbstractPass
In this pass, naive anonymous tag-types are de-anonymized and the correspoding typedefs
are marked [`Skip`](@ref).
```c
typedef struct {
int x;
} my_struct;
```
In the C code above, what the C programmer really want to do is to bring the name
"my_struct" from the tag scope into the identifier scope and force users to use `my_struct`
instead of `struct my_struct`. In Julia world, we can just do:
```julia
struct my_struct
x::Cint
end
```
Make sure you run the [`ResolveDependency`](@ref) pass before this pass.
"""
mutable struct DeAnonymize <: AbstractPass
show_info::Bool
end
DeAnonymize(; info=false) = DeAnonymize(info)
function (x::DeAnonymize)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "DeAnonymize_log", x.show_info)
for (i, node) in enumerate(dag.nodes)
is_typedef_to_anonymous(node) || continue
# a typedef to anonymous node should have only one dependent node and
# this node must be an anonymous tag-type node.
@assert length(node.adj) == 1
dep_idx = node.adj[1]
dep_node = dag.nodes[dep_idx]
# skip earlier if the dependent node is a duplicated anonymous tag-type
is_dup_tagtype(dep_node) && continue
# loop through the `adj`-list of all nodes in the DAG to find whether this
# typedef node is the only node that refers to the anonymous tag-type node.
apply_edit = true
for n in dag.nodes
n == node && continue
if any(i -> dag.nodes[i] == dep_node, n.adj)
apply_edit = false
end
end
apply_edit || continue
# apply node editing
dn = dep_node
dag.nodes[dep_idx] = ExprNode(node.id, dn.type, dn.cursor, dn.exprs, dn.adj)
dag.nodes[i] = ExprNode(node.id, SoftSkip(), node.cursor, node.exprs, node.adj)
show_info && @info "[DeAnonymize]: adding name $(node.id) to $dep_node"
end
return dag
end
"""
CodegenPreprocessing <: AbstractPass
In this pass, additional info are added into expression nodes for codegen.
"""
mutable struct CodegenPreprocessing <: AbstractPass
skip_nodes::Vector{Int}
show_info::Bool
end
CodegenPreprocessing(; info=false) = CodegenPreprocessing(Int[], info)
function (x::CodegenPreprocessing)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "CodegenPreprocessing_log", x.show_info)
empty!(x.skip_nodes)
for (i, node) in enumerate(dag.nodes)
if skip_check(dag, node)
skip_mode = is_dup_tagtype(node) ? SoftSkip() : Skip()
skip_mode == Skip() && push!(x.skip_nodes, i)
dag.nodes[i] = ExprNode(node.id, skip_mode, node.cursor, node.exprs, node.adj)
show_info &&
@info "[CodegenPreprocessing]: skip a $(node.type) node named $(node.id)"
end
if attribute_check(dag, node)
ty = attribute_type(node.type)
dag.nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
show_info &&
@info "[CodegenPreprocessing]: mark an attribute $(node.type) node named $(node.id)"
end
if nested_anonymous_check(dag, node)
ty = nested_anonymous_type(node.type)
dag.nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
show_info &&
@info "[CodegenPreprocessing]: mark a nested anonymous $(node.type) node named $(node.id)"
end
if bitfield_check(dag, node)
ty = bitfield_type(node.type)
dag.nodes[i] = ExprNode(node.id, ty, node.cursor, node.exprs, node.adj)
show_info &&
@info "[CodegenPreprocessing]: mark a bitfield $(node.type) node named $(node.id)"
end
end
has_new_skip_node = true
while has_new_skip_node
has_new_skip_node = false
for (i, n) in enumerate(dag.nodes)
is_hardskip(n) && continue
# if any dependent node is a `Skip` node, mark this node as `Skip`
for j in n.adj
dn = dag.nodes[j]
is_hardskip(dn) || continue
push!(x.skip_nodes, i)
dag.nodes[i] = ExprNode(n.id, Skip(), n.cursor, n.exprs, n.adj)
show_info &&
@info "[CodegenPreprocessing]: skip a $(n.type) node named $(n.id)"
has_new_skip_node = true
end
end
end
has_new_layout_type = true
while has_new_layout_type
has_new_layout_type = false
for (i, n) in enumerate(dag.nodes)
if n.type isa StructDefinition || n.type isa StructMutualRef
# if any dependent node is a `RecordLayouts` node, mark this node as `StructLayout`
for j in n.adj
dn = dag.nodes[j]
dn.type isa RecordLayouts || continue
# ignore pointer fields
field_cursors = fields(getCursorType(n.cursor))
field_cursors = isempty(field_cursors) ? children(n.cursor) : field_cursors
for field_cursor in field_cursors
can_type = getCanonicalType(getCursorType(field_cursor))
def_cursor = getTypeDeclaration(can_type)
if name(def_cursor) == name(dn.cursor)
ty = nested_anonymous_type(n.type)
dag.nodes[i] = ExprNode(n.id, ty, n.cursor, n.exprs, n.adj)
show_info &&
@info "[CodegenPreprocessing]: mark a special-padding struct $(n.type) node named $(n.id)"
has_new_layout_type = true
end
end
end
end
end
end
return dag
end
"""
Codegen <: AbstractPass
In this pass, Julia expressions are emitted to `node.exprs`.
"""
mutable struct Codegen <: AbstractPass
show_info::Bool
end
Codegen(; info=false) = Codegen(info)
function (x::Codegen)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "Codegen_log", x.show_info)
codegen_options = get(options, "codegen", Dict())
# forward general options
if haskey(general_options, "library_name")
codegen_options["library_name"] = general_options["library_name"]
end
if haskey(general_options, "library_names")
codegen_options["library_names"] = general_options["library_names"]
end
# store definitions which would be used during codegen
codegen_options["DAG_tags"] = dag.tags
codegen_options["DAG_ids"] = dag.ids
codegen_options["DAG_ids_extra"] = dag.ids_extra
# collect and map nested anonymous tags
codegen_options["nested_tags"] = collect_nested_tags(dag)
for (i, node) in enumerate(dag.nodes)
!isempty(node.exprs) && empty!(node.exprs)
emit!(dag, node, codegen_options; idx=i)
show_info && @info "[Codegen]: emit Julia expression for $(node.id)"
end
# Make sure that all nodes have been fully emitted
if !isempty(dag.partially_emitted_nodes)
error("Codegen error, these nodes have not been fully emitted: $(keys(dag.partially_emitted_nodes))")
end
# clean up
delete!(codegen_options, "DAG_tags")
delete!(codegen_options, "DAG_ids")
delete!(codegen_options, "DAG_ids_extra")
delete!(codegen_options, "nested_tags")
return dag
end
"""
CodegenPostprocessing <: AbstractPass
This pass is reserved for future use.
"""
mutable struct CodegenPostprocessing <: AbstractPass
show_info::Bool
end
CodegenPostprocessing(; info=false) = CodegenPostprocessing(info)
function (x::CodegenPostprocessing)(dag::ExprDAG, options::Dict)
# TODO: find a use case
end
"""
TweakMutability <: AbstractPass
In this pass, the mutability of those structs which are not necessary to be immutable
will be reset to `true` according to the following rules:
if this type is not used as a field type in any other types
if this type is in the includelist
then reset
if this type is in the ignore list
then skip
if this type is used as the argument type in some function protos
if all of the argument type are non-pointer-type
then reset
elseif the argument type is pointer-type
if all other argument types in this function are not integer type (to pass a vector to the C function, both pointer and size are needed)
then reset
if this type is not used as the argument type in any functions (but there is an implicit usage)
then reset
"""
mutable struct TweakMutability <: AbstractPass
idxs::Vector{Int}
show_info::Bool
end
TweakMutability(; info=false) = TweakMutability(Int[], info)
function (x::TweakMutability)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "TweakMutability_log", x.show_info)
ignorelist = get(general_options, "auto_mutability_ignorelist", get(general_options, "auto_mutability_blacklist", []))
includelist = get(general_options, "auto_mutability_includelist", get(general_options, "auto_mutability_whitelist", []))
add_new = get(general_options, "auto_mutability_with_new", true)
# collect referenced node ids
empty!(x.idxs)
for node in dag.nodes
t = node.type
t isa StructAnonymous || t isa StructDefinition || t isa StructMutualRef || continue
for i in node.adj
find_and_append_deps!(x.idxs, dag.nodes, i)
end
end
unique!(x.idxs)
for (i, node) in enumerate(dag.nodes)
t = node.type
t isa StructAnonymous || t isa StructDefinition || t isa StructMutualRef || continue
i ∈ x.idxs && continue
isempty(node.exprs) && continue
exprs = filter(x -> Meta.isexpr(x, :struct), node.exprs)
@assert length(exprs) == 1
expr = first(exprs)
type_name = string(expr.args[2])
apply_reset = false
if type_name ∈ includelist
apply_reset = true
elseif type_name ∈ ignorelist
apply_reset = false
else
apply_reset = should_tweak(dag.nodes, i)
end
if apply_reset
expr.args[1] = true
add_new && push!(expr.args[3].args, Expr(:(=), Expr(:call, expr.args[2]), Expr(:call, :new)))
show_info &&
@info "[TweakMutability]: reset the mutability of $type_name to mutable"
end
end
return dag
end
"""
AddFPtrMethods <: AbstractPass
This pass adds a method definition for each function prototype method which `ccall`s into a library.
The generated method allows the use of a function pointer to `ccall` into directly, instead
of relying on a library to give the pointer via a symbol look up. This is useful for libraries that
use runtime loaders to dynamically resolve function pointers for API calls.
"""
struct AddFPtrMethods <: AbstractPass end
function (::AddFPtrMethods)(dag::ExprDAG, options::Dict)
codegen_options = get(options, "codegen", Dict())
use_ccall_macro = get(codegen_options, "use_ccall_macro", false)
for node in dag.nodes
node.type isa FunctionProto || continue
ex = copy(first(node.exprs))
call = ex.args[1]
push!(call.args, :fptr)
body = ex.args[findfirst(x -> Base.is_expr(x, :block), ex.args)]
stmt_idx = if use_ccall_macro
findfirst(x -> Base.is_expr(x, :macrocall) && x.args[1] == Symbol("@ccall"), body.args)
else
findfirst(x -> Base.is_expr(x, :call) && x.args[1] == :ccall, body.args)
end
stmt = body.args[stmt_idx]
if use_ccall_macro
typeassert = stmt.args[findfirst(x -> Base.is_expr(x, :(::)), stmt.args)]
call = typeassert.args[findfirst(x -> Base.is_expr(x, :call), typeassert.args)]
call.args[1] = Expr(:$, :fptr)
else
stmt.args[2] = :fptr
end
push!(node.exprs, ex)
end
end
const DEFAULT_AUDIT_FUNCTIONS = [
audit_library_name,
sanity_check,
report_default_tag_types,
]
mutable struct Audit <: AbstractPass
funcs::Vector{Function}
show_info::Bool
end
Audit(; show_info=true) = Audit(DEFAULT_AUDIT_FUNCTIONS, show_info)
function (x::Audit)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
if !haskey(log_options, "Audit_log")
log_options["Audit_log"] = x.show_info
end
for f in x.funcs
f(dag, options)
end
return dag
end
function should_exclude_node(node, ignorelist, exclusivelist, isystem_ignorelist=[])
str_node = string(node.id)
str_node ∈ isystem_ignorelist && return true
for item ∈ ignorelist
the_match = match(Regex(item), str_node)
if the_match !== nothing && the_match.match == str_node
return true
end
end
if exclusivelist !== nothing && str_node ∉ exclusivelist
return true
end
return false
end
"""
AbstractPrinter <: AbstractPass
Supertype for printers.
"""
abstract type AbstractPrinter <: AbstractPass end
"""
FunctionPrinter <: AbstractPrinter
In this pass, only those functions are dumped to file.
"""
mutable struct FunctionPrinter <: AbstractPrinter
file::AbstractString
show_info::Bool
end
FunctionPrinter(file::AbstractString; info=true) = FunctionPrinter(file, info)
function (x::FunctionPrinter)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "FunctionPrinter_log", x.show_info)
ignorelist = get(general_options, "output_ignorelist", get(general_options, "printer_blacklist", []))
exclusivelist = get(general_options, "output_exclusivelist", nothing)
isystem_ignorelist = []
!get(general_options, "generate_isystem_symbols", true) && append!(isystem_ignorelist, string(x.id) for x in dag.sys)
show_info && @info "[FunctionPrinter]: print to $(x.file)"
open(x.file, "w") do io
for node in dag.nodes
should_exclude_node(node, ignorelist, exclusivelist, isystem_ignorelist) && continue
node.type isa AbstractFunctionNodeType || continue
pretty_print(io, node, general_options)
end
end
return dag
end
"""
CommonPrinter <: AbstractPrinter
In this pass, only those non-functions are dumped to file.
"""
mutable struct CommonPrinter <: AbstractPrinter
file::AbstractString
show_info::Bool
end
CommonPrinter(file::AbstractString; info=true) = CommonPrinter(file, info)
function (x::CommonPrinter)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "CommonPrinter_log", x.show_info)
ignorelist = get(general_options, "output_ignorelist", get(general_options, "printer_blacklist", []))
exclusivelist = get(general_options, "output_exclusivelist", nothing)
isystem_ignorelist = []
!get(general_options, "generate_isystem_symbols", true) && append!(isystem_ignorelist, string(x.id) for x in dag.sys)
show_info && @info "[CommonPrinter]: print to $(x.file)"
open(x.file, "w") do io
for node in dag.nodes
should_exclude_node(node, ignorelist, exclusivelist, isystem_ignorelist) && continue
(node.type isa AbstractMacroNodeType || node.type isa AbstractFunctionNodeType) && continue
pretty_print(io, node, general_options)
end
# print macros in the bottom of the file
for node in dag.nodes
should_exclude_node(node, ignorelist, exclusivelist, isystem_ignorelist) && continue
node.type isa AbstractMacroNodeType || continue
pretty_print(io, node, options)
end
end
return dag
end
"""