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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
|
;; -*- Lisp -*-
;; file ana-simple.melt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(comment "***
Copyright 2009 Free Software Foundation, Inc.
Contributed by Basile Starynkevitch <basile@starynkevitch.net>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>.
***")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; green transformation
;;; a pass which converts fprintf(stdout, ..) to printf (..)
(defun makegreenpass_gate (pass)
(debug_msg pass "makegreenpass_gate pass at start")
;; retrieve the data associated to the cfun in the pass. If it
;; exists (i.e. is not null) we avoid running the pass again
(let ( (grdata (maptree_get (get_field :gccpass_data pass) (cfun_decl))) )
(debugtree "makegreenpass_gate cfundecl" (cfun_decl))
(debug_msg grdata "makegreenpass_gate grdata")
(if grdata
() ;; return null, so avoid executing the pass
pass)
))
(defclass class_makegreen_data
:super class_proped
:fields (mkgreen_pass
mkgreen_data
mkgreen_stdout_ssa ;map of SSA tree assigned to stdout
mkgreen_fprintf
))
;; this inspired gcc/testsuite/melt/tmatch-4.melt
(defun makegreen_transform (grdata :tree decl :basic_block bb)
(debug_msg grdata "makegreen_transform start grdata")
(assert_msg "check grdata" (is_a grdata class_makegreen_data))
(debugtree "makegreen_transform decl" decl)
(debugbasicblock "makegreen_transform bb" bb)
(eachgimple_in_basicblock
(bb)
(:gimple g)
(debuggimple "makegreen_transform before matching g" g)
(match g
(?(gimple_assign_cast
?lhs
?rhs)
(debugtree "makegreen_transform assign cast lhs" lhs)
(debugtree "makegreen_transform assign cast rhs" rhs)
(compile_warning "some casts should be caught" ())
;; we need to catch a cast like
;;; stdout.5_3 = (struct FILE * restrict) stdout.4_2;
;; where stdout.4_2 has been set to stdout!
)
(?(gimple_assign_single ?lhs
?(and ?rhs
?(tree_var_decl
?_ ?(cstring_same "stdout") ?_)))
(debugtree "makegreen_transform assign single stdout lhs" lhs)
(debugtree "makegreen_transform assign single stdout rhs" rhs)
(maptree_put (get_field :mkgreen_stdout_ssa grdata)
lhs
(make_gimple discr_gimple g))
)
(?(gimple_assign_single ?lhs ?rhs)
(debugtree "makegreen_transform assign single plain lhs" lhs)
(debugtree "makegreen_transform assign single plain rhs" rhs)
)
(?(gimple_call_2_more ?lhs
?(and ?callfndcl
?(tree_function_decl
?(cstring_same "fprintf")
?_)
)
?arg0 ?arg1 ?nbargs)
(debugtree "makegreen_transform call2morefprintf lhs" lhs)
(debugtree "makegreen_transform call2morefprintf fndecl" callfndcl)
(debugtree "makegreen_transform call2morefprintf arg0" arg0)
(debugtree "makegreen_transform call2morefprintf arg1" arg1)
(let ( (gx (maptree_get (get_field :mkgreen_stdout_ssa grdata) arg0)) )
(cond ( gx
(debug_msg gx "makegreen_transform found gx")
(warning_at_gimple g "fprintf to stdout")
(inform_at_gimple (gimple_content gx) "stdout was from here")
)
))
)
(?(gimple_call ?lhs ?callfndcl ?nbargs)
(debugtree "makegreen_transform call lhs" lhs)
(debugtree "makegreen_transform call fndecl" callfndcl)
)
(?_
(debuggimple "makegreen_transform unmatched g" g)
)
)
)
(debug_msg grdata "makegreen_transform end grdata")
)
(defun makegreenpass_exec (pass)
(debug_msg pass "makegreenpass_exec pass at start")
(code_chunk dbgcfun #{ /* $dbgcfun :: */
debugeprintf("makegreenpass_exec start cfun=%p cfg=%p",
(void*)cfun, cfun?cfun->cfg:NULL) ;
}#)
(assert_msg "makegreenpass_exec check cfun has cfg" (cfun_has_cfg))
(debugtree "makegreenpass_exec start cfundecl" (cfun_decl))
(let ( (grdata (instance class_makegreen_data
:mkgreen_pass pass
:mkgreen_stdout_ssa (make_maptree discr_map_trees 10)
)) )
;; add our grdata associated to the cfun_decl in the pass data,
;; hence the gate will avoid running the pass twice
(maptree_put (get_field :gccpass_data pass) (cfun_decl) grdata)
;; it is useless to do a each_cgraph_decl here...
(debug_msg grdata "makegreenpass_exec grdata before eachbbcfun")
;; gimple ssa passes have a cfg..
(each_bb_cfun
()
(:basic_block bb :tree fundecl)
(debug_msg grdata "makegreenpass_exec grdata before makegreen_transform")
(makegreen_transform grdata fundecl bb)
)
(debug_msg pass "makegreenpass_exec pass at end")
(code_chunk dbgcfun #{ debugeprintf("makegreenpass_exec end cfun=%p",
(void*)cfun) ;
}#)
))
(defun makegreen_docmd (cmd moduldata)
(let ( (greenpass
(instance class_gcc_gimple_pass
:named_name '"melt_greenpass"
:gccpass_gate makegreenpass_gate
:gccpass_exec makegreenpass_exec
:gccpass_data (make_maptree discr_map_trees 100)
:gccpass_properties_required ()
)
) )
;;; register our pass after the "phiopt" pass [reassoc is too early,
;;; and phiopt keep the SSA form.]
(install_melt_gcc_pass greenpass "after" "phiopt" 0)
;;; note, register it after "ccp" or "pta" does not work...
(debug_msg greenpass "makegreen_mode installed greenpass")
;; return the greenpass to accept the mode
(return greenpass)
))
;;;
(definstance makegreen_mode
class_melt_mode
:named_name '"makegreen"
:meltmode_help '"enable a pass finding fprintf to stdout..."
:meltmode_fun makegreen_docmd
)
(install_melt_mode makegreen_mode)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; small analysis
(defclass class_smallcfun
:super class_named
:fields (scfun_decl
scfun_entbb
))
(defclass class_smallbb
:super class_proped
:fields (sbb_bbcont
))
(defclass class_smallanalysis
:super class_analysis_state
:fields (sman_cfuns ;list of class_smallcfun-s
sman_cfundict ;dict of class_smallcfun-s
sman_cfuntreemap ;treemap of class_smallcfun-s
sman_bbtable ;hashtable bb -> class_smallbb
))
(defclass class_smallabstractenv
:super class_proped
:fields (abenv_maptree ;a maptree C ident -> abstract values
abenv_parenv ;the parent abstract env
abenv_pplpoly ;the PPL polyhedron
abenv_vectrees ;the vector naming PPL variables as trees
))
(defun put_abstrenv (abenv :tree tr :value val)
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(assert_msg "check tr" tr)
(maptree_put (get_field :abenv_maptree abenv) tr val)
)
(defun get_abstrenv (abenv :tree tr)
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(maptree_get (get_field :abenv_maptree abenv) tr)
)
(defun fresh_abstrenv (abpar)
(assert_msg "check abpar" (is_a abpar class_smallabstractenv))
(let ( (newabenv (instance class_smallabstractenv
:abenv_maptree (make_maptree discr_map_trees 10)
:abenv_parenv abpar)) )
(return newabenv)
))
;;; find a tree inside a small abstract env - return primarily the
;;; boxed tree and secondly its index
(defun find_tree_smallabstractenv (abenv :tree tr)
(debug_msg abenv "find_tree_smallabstractenv abenv")
(debugtree "find_tree_smallabstractenv tr" tr)
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(let ( (:long ix -1)
(btree ())
(varvect (get_field :abenv_vectrees abenv))
(:long curix 0)
(:long vectlen (multiple_length varvect))
)
(forever
loop
(if (>=i curix vectlen) (exit loop))
(let ( (curcomp (multiple_nth varvect curix)) )
(if (==t (tree_content curcomp) tr)
(progn
(debug_msg curcomp "find_tree_smallabstractenv return curcomp")
(return curcomp curix))
)
(setq curix (+i curix 1))
)))
(debug_msg () "find_tree_smallabstractenv return NIL")
(return)
)
(defun dbgout_smallabstractenv (self dbgi :long depth)
(assert_msg "check dbgi" (is_a dbgi class_debug_information))
(assert_msg "check self" (is_a self class_smallabstractenv))
(let ( (dis (discrim self))
(out (unsafe_get_field :dbgi_out dbgi))
(:long onum (obj_num self))
)
(add2sbuf_strconst out "|")
(add2sbuf_string out (get_field :named_name dis))
(add2sbuf_strconst out "/")
(add2sbuf_longhex out (obj_hash self))
(if onum
(progn
(add2sbuf_strconst out "#")
(add2sbuf_longdec out onum)))
(add2sbuf_strconst out "{")
(let ( (:long offpar (get_int abenv_parenv))
(:long offvnam (get_int abenv_vectrees))
(:long oldmaxdepth (get_int (unsafe_get_field :dbgi_maxdepth dbgi)))
(:long newmaxdepth (-i (/i oldmaxdepth 2) 1))
)
(if (<i newmaxdepth 0) (setq newmaxdepth 0))
(if (need_dbglim depth oldmaxdepth)
(progn
(and (>i depth 0) (>i oldmaxdepth 3)
(put_int (unsafe_get_field :dbgi_maxdepth dbgi) newmaxdepth))
(dbgout_fields self dbgi (+i depth 1) 0 offpar)
;(add2sbuf_indentnl out depth)
(let ( (polyv (get_field :abenv_pplpoly self)) )
(if polyv
(progn
(add2sbuf_strconst out "@ppl.poly@[")
(ppl_ppstrbuf out
(get_field :abenv_pplpoly self)
depth
(get_field :abenv_vectrees self))
(add2sbuf_strconst out "]@"))
(add2sbuf_strconst out "@ppl.poly*nil*")))
(add2sbuf_indentnl out depth)
(if (need_dbglim (+i depth 2) newmaxdepth)
(dbgout_fields self dbgi (+i depth 2) offvnam -1)
(add2sbuf_strconst out ".._.."))
(put_int (unsafe_get_field :dbgi_maxdepth dbgi) oldmaxdepth)
)))
(add2sbuf_strconst out "}")
)
)
(install_method class_smallabstractenv dbg_output dbgout_smallabstractenv)
(definstance initial_smallabstractenv class_smallabstractenv
:abenv_maptree (make_maptree discr_map_trees 20))
(definstance smallana_cont class_container)
(defun smallana_cfun_entbb (sman :tree decltree :basic_block bbent)
(debug_msg sman "smallana_cfun_entbb sman")
(debugtree "smallana_cfun_entbb start decltree" decltree)
(match
decltree
;; the joker below is acutally nil
( ?(tree_function_decl ?funam ?(tree_block ?treevars ?_))
(debugtree "smallana_cfun_entbb treevars of function" treevars)
(push_cfun_decl decltree)
;;;;;
(each_param_in_fundecl
(decltree)
(:tree argdtree)
(debugtree "smallana_cfun_entbb argdtree" argdtree)
(match
argdtree
( ?(tree_parm_decl
?(and ?argdtype
; ?(tree_integer_type ?typname ?bigmin ?bigmax ?isiz)) ?argdcl ?argname)
?_) ?_ ?_)
;; (debugtree "smallana_cfun_entbb integer argdtype" argdtype)
;; (debugcstring "smallana_cfun_entbb integer argname" argname)
;; (debugcstring "smallana_cfun_entbb integer typname" typname)
;; (debug_msg bigmin "smallana_cfun_entbb integer bigmin")
;; (debug_msg bigmax "smallana_cfun_entbb integer bigmax")
)
( ?(tree_parm_decl ?argdtype ?argdcl ?argname)
(debugtree "smallana_cfun_entbb argdtype" argdtype)
(debugtree "smallana_cfun_entbb argdcl" argdcl)
(debugcstring "smallana_cfun_entbb argname" argname)
)
( ?_
;; (assert_msg "unexpected argdtree" ())
(shortbacktrace_dbg "unexpected tree in smallana_cfun_entbb" 12)
)
)
(debugtree "smallana_cfun_entbb argdecltree" argdtree)
)
;;;;
(let ( (fname (make_stringconst discr_string funam))
(scf (instance class_smallcfun
:named_name fname
:scfun_decl (make_tree discr_tree decltree)
:scfun_entbb (make_basicblock discr_basic_block bbent)))
)
(debug_msg fname "smallana_cfun_entbb fname")
(list_append (get_field :sman_cfuns sman) scf)
(mapstring_putstr (get_field :sman_cfundict sman) fname scf)
(maptree_put (get_field :sman_cfuntreemap sman) decltree scf)
)
(pop_cfun)
)
( ?(tree_var_decl ?_ ?_ ?_)
(debugtree "smallana_cfun_entbb decl of variable" decltree)
(inform_at_tree decltree "smallana: global variable not analyzed")
)
( ?_
(debugtree "smallana_cfun_entbb other decl" decltree)
(inform_at_tree decltree "smallana: other declaration not analyzed")
;; (assert_msg "smallana_cfun_entbb @$@UNIMPLEMENTED DECL" ())
(shortbacktrace_dbg "smallana_cfun_entbb unexepcted declree" 14)
() )
)
(debug_msg sman "smallana_cfun_entbb sman")
)
(defun smallana_gimple (sman :gimple g)
(debuggimple "smallana_gimple gimple" g)
(match
g
( ?(gimple_assign_single ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_single lhs" lhs)
(debugtree "smallana_gimple gimple_assign_single rhs" rhs)
)
( ?(gimple_assign_cast ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_cast lhs" lhs)
(debugtree "smallana_gimple gimple_assign_cast rhs" rhs)
)
( ?(gimple_assign_copy ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_copy lhs" lhs)
(debugtree "smallana_gimple gimple_assign_copy rhs" rhs)
)
( ?(gimple_assign_ssa_name_copy ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_ssa_name_copy lhs" lhs)
(debugtree "smallana_gimple gimple_assign_ssa_name_copy rhs" rhs)
)
( ?(gimple_assign_unary_nop ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_unary_nop lhs" lhs)
(debugtree "smallana_gimple gimple_assign_unary_nop rhs" rhs)
)
( ?(gimple_assign_plus ?lhs ?rhs1 ?rhs2)
(debugtree "smallana_gimple gimple_assign_plus lhs" lhs)
(debugtree "smallana_gimple gimple_assign_plus rhs1" rhs1)
(debugtree "smallana_gimple gimple_assign_plus rhs2" rhs2)
)
( ?(gimple_assign_pointerplus ?lhs ?rhs1 ?rhs2)
(debugtree "smallana_gimple gimple_assign_pointerplus lhs" lhs)
(debugtree "smallana_gimple gimple_assign_pointerplus rhs1" rhs1)
(debugtree "smallana_gimple gimple_assign_pointerplus rhs2" rhs2)
)
( ?(gimple_assign_mult ?lhs ?rhs1 ?rhs2)
(debugtree "smallana_gimple gimple_assign_mult lhs" lhs)
(debugtree "smallana_gimple gimple_assign_mult rhs1" rhs1)
(debugtree "smallana_gimple gimple_assign_mult rhs2" rhs2)
)
( ?(gimple_assign_binaryop ?lhs ?rhs1 ?rhs2 ?opcod)
(debugtree "smallana_gimple gimple_assign_binaryop lhs" lhs)
(debugtree "smallana_gimple gimple_assign_binaryop rhs1" rhs1)
(debugtree "smallana_gimple gimple_assign_binaryop rhs2" rhs2)
(debugtreecodenum "smallana_gimple gimple_assign_binaryop opcod" opcod)
;;(shortbacktrace_dbg "smallana_gimple gimple_assign_binaryop unexpected" 15)
(assert_msg "smallana_gimple gimple_assign_binaryop unhandled" ())
)
( ?(gimple_cond_lessequal ?lhs ?rhs)
(debugtree "smallana_gimple gimple_cond_lessequal lhs" lhs)
(debugtree "smallana_gimple gimple_cond_lessequal rhs" rhs)
(shortbacktrace_dbg "@$@unimplemented smallana_gimple gimple_cond_lessequal" 15)
)
( ?(gimple_cond_notequal ?lhs ?rhs)
(debugtree "smallana_gimple gimple_cond_notequal lhs" lhs)
(debugtree "smallana_gimple gimple_cond_notequal rhs" rhs)
(shortbacktrace_dbg "@$@unimplemented smallana_gimple gimple_cond_notequal" 15)
)
( ?(gimple_cond_greater ?lhs ?rhs)
(debugtree "smallana_gimple gimple_cond_greater lhs" lhs)
(debugtree "smallana_gimple gimple_cond_greater rhs" rhs)
(shortbacktrace_dbg "@$@unimplemented smallana_gimple gimple_cond_greater" 15)
)
( ?(gimple_cond_true)
(shortbacktrace_dbg "@$@unimplemented smallana_gimple gimple_cond_true" 15)
)
( ?(gimple_call ?lhs ?fndecl ?_)
(debugtree "smallana_gimple gimple_call lhs" lhs)
(debugtree "smallana_gimple gimple_call fndecl" fndecl)
(shortbacktrace_dbg "@$@unimplemented smallana_gimple gimple_call" 15)
)
( ?(gimple_return ?retval)
(debugtree "smallana_gimple gimple_return retval" retval)
(shortbacktrace_dbg "@$@unimplemented smallana_gimple gimple_return" 15)
)
( ?_
(debuggimple "smallana_gimple other!gimple" g)
;;(shortbacktrace_dbg "smallana_gimple unexpected gimple" 15)
(assert_msg "smallana_gimple @$@ UNIMPLEMENTED GIMPLE" ())
)
)
)
(defun smallana_bb (sman :basic_block bb)
(debug_msg sman "smallana_bb start sman")
(assert_msg "check sman" (is_a sman class_smallanalysis))
(debugbasicblock "smallana_bb bb" bb)
(let ( (bbtab (get_field :sman_bbtable sman))
)
(assert_msg "check bbtab" (is_mapbasicblock bbtab))
(let ( (sbb (mapbasicblock_get bbtab bb)) )
(if sbb (return))
(setq sbb (instance class_smallbb
:sbb_bbcont (make_basicblock discr_basic_block bb)))
(mapbasicblock_put bbtab bb sbb)
(eachgimple_in_basicblock
(bb)
(:gimple g)
(smallana_gimple sman g)
)
(debug_msg sman "smallana_bb end sman")
)))
(defun smallana_cfun_bb (sman :tree cfdecl :basic_block fbb)
(debug_msg sman "smallana_bb_cfun start sman")
(debugtree "smallana_bb_cfun cfdecl" cfdecl)
(assert_msg "check sman" (is_a sman class_smallanalysis))
(match cfdecl
( ?(and ?decltree ?(tree_function_decl ?funam ?(tree_block ?treevars ?_)))
(debugtree "smallana_bb_cfun decltree" decltree)
(debugtree "smallana_bb_cfun treevars of function" treevars)
(debugcstring "smallana_cfun_entrybb funam" funam)
))
(smallana_bb sman fbb)
(debug_msg sman "smallana_bb_cfun end sman")
)
(defun smaninterp_cfun (sman abenv cfun)
(debug_msg sman "smaninterp_fun sman")
(debug_msg cfun "smaninterp_fun cfun")
(debug_msg abenv "smaninterp_fun abenv")
(assert_msg "check sman" (is_a sman class_smallanalysis))
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(assert_msg "check cfun" (is_a cfun class_smallcfun))
(let ( (:basic_block bbent
(basicblock_content (get_field :scfun_entbb cfun)))
(:tree fundecl (tree_content (get_field :scfun_decl cfun)))
)
(push_cfun_decl fundecl)
(smaninterp_basicblock sman abenv bbent)
;(assert_msg "@$@unimplemented smaninterp_fun" ())
(shortbacktrace_dbg "@$@unimplemented smaninterp_fun" 15)
(pop_cfun)
))
(defun smaninterp_basicblock (sman abenv :basic_block bb)
(debug_msg abenv "smaninterp_basicblock abenv")
(assert_msg "check sman" (is_a sman class_smallanalysis))
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(forever
bbloop
(debugbasicblock "smaninterp_basicblock bb" bb)
(if (isnull_basicblock bb)
(exit bbloop))
(eachgimple_in_basicblock
(bb)
(:gimple g)
(debuggimple "smaninterp_basicblock g" g)
(smaninterp_gimple sman abenv g bb)
)
(setq bb (basicblock_single_succ bb))
(debugbasicblock "smaninterp_basicblock succ bb" bb)
)
(debug_msg () "smaninterp_basicblock done")
;(assert_msg "@$@unimplemented smaninterp_basicblock" ())
(shortbacktrace_dbg "@$@unimplemented smaninterp_basicblock" 15)
)
;;;;;;;;;;;;;;;;
(defun sman_propagate_constraints_call (abenv newabenv tupvarbind)
(debug_msg tupvarbind "sman_propagate_constraints_call tupvarbind")
(assert_msg "check newabenv" (is_a newabenv class_smallabstractenv))
(let ( (pplvarslist (make_list discr_list))
(:ppl_constraint_system consys (raw_new_ppl_empty_constraint_system))
(:long nbpplvars 0)
)
(foreach_in_multiple
(tupvarbind)
(curvarbind :long bindix)
(debug_msg curvarbind "sman_propagate_constraints_call curvarbind")
(let ( (:tree parmtype (tree_content (multiple_nth curvarbind 0)))
(parmdeclval (multiple_nth curvarbind 1))
(:tree parmdecl (tree_content parmdeclval))
(:tree curarg (tree_content (multiple_nth curvarbind 2)))
)
(debugtree "sman_propagate_constraints_call parmtype" parmtype)
(debugtree "sman_propagate_constraints_call parmdecl" parmdecl)
(debugtree "sman_propagate_constraints_call curarg" curarg)
(messagenum_dbg "sman_propagate_constraints_call nbpplvars" nbpplvars)
(match
curarg
( ?(tree_integer_cst ?k)
(messagenum_dbg "sman_propagate_constraints_call curarg const.int. k" k)
;; add the constraint {parmdecl == k}
(let (
(:ppl_coefficient coefarg (ppl_coefficient_from_tree curarg))
(:ppl_coefficient coefm1 (ppl_coefficient_from_long -1))
(:ppl_linear_expression liex (make_ppl_linear_expression))
)
(debug_ppl_coefficient "sman_propagate_constraints_call coefarg" coefarg)
(messagenum_dbg "sman_propagate_constraints_call nbpplvars" nbpplvars)
(ppl_Linear_Expression_add_to_coefficient liex nbpplvars coefm1)
(ppl_Linear_Expression_add_to_inhomogeneous liex coefarg)
(debug_ppl_linear_expression "sman_propagate_constraints_call liex" liex)
(let ( (:ppl_constraint cons (make_ppl_constraint liex "==")) )
(debug_ppl_constraint "sman_propagate_constraints_call cons" cons)
(ppl_Constraint_System_insert_Constraint consys cons)
(ppl_delete_Constraint cons)
)
(ppl_delete_Coefficient coefm1)
(ppl_delete_Coefficient coefarg)
(ppl_delete_Linear_Expression liex)
)
(setq nbpplvars (+i nbpplvars 1))
(list_append pplvarslist parmdeclval)
)
( ?_
(assert_msg "sman_propagate_constraints_call @$@unimplemented curarg tree" ())))
))
(let ( (:ppl_polyhedron poly (ppl_NNC_Polyhedron_from_Constraint_System consys))
(polyv (make_ppl_polyhedron_same discr_ppl_polyhedron poly))
)
(debug_msg polyv "sman_propagate_constraints_call polyv")
(assert_msg "check polyv" polyv)
(put_fields newabenv
:abenv_pplpoly polyv
:abenv_vectrees (list_to_multiple pplvarslist))
))
(debug_msg newabenv "sman_propagate_constraints_call final newabenv")
)
;;;;;;;;;;;;;;;;
;; pass both the relation and the opposite relation when changing signs
(defun sman_add_cmp_constraint (sman abenv polyv :tree lhs rhs :cstring rel opprel)
(debug_msg abenv "sman_add_cmp_constraint abenv")
(assert_msg "check sman" (is_a sman class_smallanalysis))
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(debugtree "sman_add_cmp_constraint lhs" lhs)
(debugtree "sman_add_cmp_constraint rhs" rhs)
(debug_msg polyv "sman_add_cmp_constraint polyv")
(assert_msg "check polyv" (== (discrim polyv) discr_ppl_polyhedron))
(let ( (:long lvix -1) ;left var index
(:long rvix -1) ;right var index
)
;;; handle the left hand side lhs
(match
lhs
(?(tree_ssa_name
?(and ?ldeclvar ?(tree_decl ?lvar ?lname ?luid))
?lvalu ?lversion ?ldefg)
(debugtree "sman_add_cmp_constraint ssa_name ldeclvar" ldeclvar)
(debugtree "sman_add_cmp_constraint ssa_name lvar" lvar)
(debugtree "sman_add_cmp_constraint ssa_name lvalu" lvalu)
(messagenum_dbg "sman_add_cmp_constraint lversion" lversion)
(debuggimple "sman_add_cmp_constraint ssa_name ldefg" ldefg)
(multicall
(boxtr :long trix)
(find_tree_smallabstractenv abenv lvar)
(debug_msg boxtr "sman_add_cmp_constraint ssa_name boxtr")
(if boxtr
(progn (setq lvix trix) ())
(progn
(assert_msg "sman_add_cmp_constraint dont know what to do lvar" ())
()
))
)
()
)
(?(tree_integer_cst ?lk)
() ;this is ok
)
(?_
(assert_msg "unexpected lhs in sman_add_cmp_constraint" ()))
) ;end match lhs
;;; handle the right hand side rhs
(match
rhs
(?(tree_ssa_name ?rvar ?rvalu ?rversion ?rdefg)
(debugtree "sman_add_cmp_constraint ssa_name rvar" rvar)
(debugtree "sman_add_cmp_constraint ssa_name rvalu" rvalu)
(messagenum_dbg "sman_add_cmp_constraint rversion" rversion)
(debuggimple "sman_add_cmp_constraint ssa_name rdefg" rdefg)
(multicall
(boxtr :long trix)
(find_tree_smallabstractenv abenv rvar)
(debug_msg boxtr "sman_add_cmp_constraint ssa_name boxtr")
(if
boxtr
(progn (setq rvix trix) ())
(assert_msg "sman_add_cmp_constraint dont know what to do rvar" ()))
)
()
)
(?(tree_integer_cst ?rk)
() ;this is ok
)
(?_
(assert_msg "unexpected rhs in sman_add_cmp_constraint" ()))
) ;end match rhs
(cond
( (and (>=i lvix 0) (<i rvix 0)) ;left is variable, right is constant
(let (
(:ppl_polyhedron poly (ppl_polyhedron_content polyv))
(:ppl_linear_expression liex (make_ppl_linear_expression))
(:ppl_coefficient coefright (ppl_coefficient_from_tree rhs))
(:ppl_coefficient coefm1 (ppl_coefficient_from_long -1))
)
(assert_msg "check poly" poly)
(ppl_Linear_Expression_add_to_coefficient liex lvix coefm1)
(ppl_Linear_Expression_add_to_inhomogeneous liex coefright)
(let ( (:ppl_constraint newcons (make_ppl_constraint liex opprel)) )
(debug_ppl_constraint "sman_add_cmp_constraint leftvar constraint" newcons)
(ppl_Polyhedron_add_constraint poly newcons)
(debug_ppl_polyhedron "sman_add_cmp_constraint updated poly" poly)
(ppl_delete_Coefficient coefm1)
(ppl_delete_Coefficient coefright)
(ppl_delete_Linear_Expression liex)
(ppl_delete_Constraint newcons)
)
)
)
( (and (<i lvix 0) (>=i rvix 0)) ;left is constant, right is variable
(let ( (:ppl_coefficient coefleft (ppl_coefficient_from_tree lhs))
(:ppl_coefficient coefm1 (ppl_coefficient_from_long -1))
(:ppl_linear_expression liex (make_ppl_linear_expression))
(:ppl_polyhedron poly (ppl_polyhedron_content polyv))
)
(ppl_Linear_Expression_add_to_coefficient liex rvix coefm1)
(ppl_Linear_Expression_add_to_inhomogeneous liex coefleft)
(let ( (:ppl_constraint newcons (make_ppl_constraint liex opprel)) )
(debug_ppl_constraint "sman_add_cmp_constraint rightvar constraint" newcons)
(ppl_Polyhedron_add_constraint poly newcons)
(ppl_delete_Coefficient coefm1)
(ppl_delete_Coefficient coefleft)
(ppl_delete_Linear_Expression liex)
(ppl_delete_Constraint newcons)
)
)
()
)
( (and (>=i lvix 0) (>=i rvix 0)) ;both left & right are variable
(let ( (:ppl_coefficient coef1 (ppl_coefficient_from_long 1))
(:ppl_coefficient coefm1 (ppl_coefficient_from_long -1))
(:ppl_linear_expression liex (make_ppl_linear_expression))
(:ppl_polyhedron poly (ppl_polyhedron_content polyv))
)
(ppl_Linear_Expression_add_to_coefficient liex lvix coef1)
(ppl_Linear_Expression_add_to_coefficient liex rvix coefm1)
(let ( (:ppl_constraint newcons (make_ppl_constraint liex rel)) )
(debug_ppl_constraint "sman_add_cmp_constraint bothvar constraint" newcons)
(ppl_Polyhedron_add_constraint poly newcons)
(ppl_delete_Coefficient coefm1)
(ppl_delete_Coefficient coef1)
(ppl_delete_Linear_Expression liex)
(ppl_delete_Constraint newcons)
)
)
()
)
(:else
(assert_msg "unexpected left & right in sman_add_cmp_constraint" ())
))
)
(debug_msg polyv "sman_add_cmp_constraint final polyv")
)
;;;;;;;;;;;;;;;;
;;;; utility to interpret an arithmetic compare test
(defun sman_arithm_compare
(sman abenv
;;; the entire gimple and its containing basicblock
:gimple g :basic_block bb
;;; the extracted lefthandside righthandside
:tree lhs rhs
;;; the relation string [eg "<="] its opposite for negative numbers [eg ">="] and
;;; its negation [eg ">"] and the opposite of the negation [eg "<"]
:cstring rel opprel notrel notopprel)
;; check arguments
(assert_msg "check sman" (is_a sman class_smallanalysis))
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(debuggimple "sman_arithm_compare gimple" g)
(debugbasicblock "sman_arithm_compare bb" bb)
(debug_msg abenv "sman_arithm_compare start abenv")
;; extract the polyhedron value and clone it for the other branch
(let ( (polyv (get_field :abenv_pplpoly abenv))
(:ppl_polyhedron poly (ppl_polyhedron_content polyv))
(clonpolyv (make_ppl_polyhedron_cloned discr_ppl_polyhedron poly))
(:ppl_polyhedron clonpoly (ppl_polyhedron_content clonpolyv))
)
(debug_msg polyv "sman_arithm_compare polyv before sman_add_cmp_constraint")
;; handle the true branch by adding the constraint from the test
(sman_add_cmp_constraint sman abenv polyv lhs rhs rel opprel)
(debug_msg polyv "sman_arithm_compare polyv after sman_add_cmp_constraint")
(if (ppl_Polyhedron_is_empty poly)
(progn
(debuggimple "sman_arithm_compare true branch impossible" g)
)
(progn
(debuggimple "sman_arithm_compare true branch possible g" g)
(debugbasicblock "sman_arithm_compare true branch possible bb" bb)
(foreach_basicblock_succ_edge
(bb)
(:edge eg :long eix)
(debugedge "sman_arithm_compare true succedge eg" eg)
(if (edge_for_true_value eg)
(let ( (:basic_block tbb (edge_dest_bb eg))
(oldpolyv (get_field :abenv_pplpoly abenv))
)
;; in the true case, the oldpolyv should be polyv
(assert_msg "check polyv==oldpolyv" (== oldpolyv polyv))
(debugbasicblock "sman_arithm_compare truesucc tbb" tbb)
(debug_msg abenv "sman_arithm_compare true before tbb abenv")
(smaninterp_basicblock sman abenv tbb)
(debug_msg abenv "sman_arithm_compare true after tbb abenv")
))
)
))
;; handle the false branch by adding the constraint incompatible with the test
(debug_msg clonpolyv "sman_arithm_compare clonpolyv before sman_add_cmp_constraint")
(sman_add_cmp_constraint sman abenv clonpolyv lhs rhs notrel notopprel)
(debug_msg clonpolyv "sman_arithm_compare clonpolyv after sman_add_cmp_constraint")
(if (ppl_Polyhedron_is_empty clonpoly)
(progn
(debuggimple "sman_arithm_compare false branch impossible" g)
)
(progn
(debuggimple "sman_arithm_compare false branch possible" g)
(debugbasicblock "sman_arithm_compare false branch possible bb" bb)
(foreach_basicblock_succ_edge
(bb)
(:edge eg :long eix)
(debugedge "sman_arithm_compare false succedge eg" eg)
(if (edge_for_false_value eg)
(let ( (:basic_block fbb (edge_dest_bb eg))
(oldpolyv (get_field :abenv_pplpoly abenv))
)
(debugbasicblock "sman_arithm_compare falsesucc fbb" fbb)
(put_fields abenv :abenv_pplpoly clonpolyv)
(debug_msg abenv "sman_arithm_compare false updated abenv")
(smaninterp_basicblock sman abenv fbb)
(put_fields abenv :abenv_pplpoly oldpolyv)
(debug_msg abenv "sman_arithm_compare false restored abenv")
))
)
))
)
(debug_msg sman "sman_arithm_compare ended sman")
)
;;;;;;;;;;;;;;;;
(defun smaninterp_gimple (sman abenv :gimple g :basic_block bb)
(assert_msg "check sman" (is_a sman class_smallanalysis))
(assert_msg "check abenv" (is_a abenv class_smallabstractenv))
(debug_msg abenv "smaninterp_gimple abenv")
(debuggimple "smaninterp_gimple g" g)
(debugbasicblock "smaninterp_gimple bb" bb)
;; we really should count the number of times we are calling
;; smaninterp_gimple and returns immediately with a warning if calling
;; too often/too deeply. The point is that an small analysis of a
;; buggy program could make the compiler loop.
(compile_warning "we should check that we don't call too often or too deeply in smaninterp_gimple" ())
(match
g
;; handle calls
(?(gimple_call ?lhs ?fndecl ?nargs)
(debugtree "smaninterp_gimple call lhs" lhs)
(debugtree "smaninterp_gimple call fndecl" fndecl)
(let ( (:long argix 0)
(newabenv (fresh_abstrenv abenv))
(tupvarbind (make_multiple discr_multiple nargs))
)
(each_param_in_fundecl
(fndecl)
(:tree curparamdcl)
(messagenum_dbg "smaninterp_gimple curparamdecl argix" argix)
(debugtree "smaninterp_gimple curparamdecl" curparamdcl)
(let ( (:tree curarg (gimple_call_nth_arg g argix)) )
(debugtree "smaninterp_gimple curarg" curarg)
(match
curparamdcl
(?(tree_parm_decl ?parmtype ?parmdecl ?parmname)
(debugtree "smaninterp_gimple parmtype" parmtype)
(debugtree "smaninterp_gimple parmdecl" parmdecl)
(debugcstring "smaninterp_gimple parmname" parmname)
(multiple_put_nth tupvarbind argix
(tuple
(make_tree discr_tree parmtype)
(make_tree discr_tree parmdecl)
(make_tree discr_tree curarg)))
)
(?_
(assert_msg "inexpected curparamdcl" ())
)
)
(setq argix (+i argix 1))
))
(debug_msg tupvarbind "smaninterp_gimple tupvarbind")
(sman_propagate_constraints_call abenv newabenv tupvarbind)
(debug_msg newabenv "smaninterp_gimple newabenv propagated")
(debugtree "smaninterp_gimple call should go into fndecl" fndecl)
(debug_msg sman "smaninterp_gimple sman")
(let (
(calledcfun (maptree_get (get_field :sman_cfuntreemap sman) fndecl)) )
(debug_msg calledcfun "smaninterp_gimple calledcfun")
(if calledcfun
(smaninterp_cfun sman newabenv calledcfun)
)
)
(shortbacktrace_dbg "@$@unimplemented smaninterp_gimple call" 15)
;(assert_msg "@$@unimplemented smaninterp_gimple call" ())
)
)
;; handle conditional <=
( ?(gimple_cond_lessequal ?lhs ?rhs)
(debugtree "smaninterp_gimple cond<= lhs" lhs)
(debugtree "smaninterp_gimple cond<= rhs" rhs)
(debug_msg abenv "smaninterp_gimple cond<= abenv before sman_arithm_compare")
(sman_arithm_compare sman abenv
g bb
lhs rhs
"<=" ">=" ">" "<")
(debug_msg abenv "smaninterp_gimple cond<= abenv after sman_arithm_compare")
(assert_msg "@$@unimplemented smaninterp_gimple cond<=" ())
)
;; handle conditional <
( ?(gimple_cond_less ?lhs ?rhs)
(debugtree "smaninterp_gimple cond< lhs" lhs)
(debugtree "smaninterp_gimple cond< rhs" rhs)
(shortbacktrace_dbg "@$@unimplemented smaninterp_gimple cond<" 15)
(assert_msg "@$@unimplemented smaninterp_gimple cond<" ())
)
;; handle conditional >
( ?(gimple_cond_greater ?lhs ?rhs)
(debugtree "smaninterp_gimple cond> lhs" lhs)
(debugtree "smaninterp_gimple cond> rhs" rhs)
(shortbacktrace_dbg "@$@unimplemented smaninterp_gimple cond>" 15)
(assert_msg "@$@unimplemented smaninterp_gimple cond>" ())
)
;; handle conditional true
( ?(gimple_cond_true)
;;(shortbacktrace_dbg "@$@unimplemented smaninterp_gimple condtrue" 15)
(assert_msg "@$@unimplemented smaninterp_gimple condtrue" ())
)
;; handle conditional false
( ?(gimple_cond_false)
;;(shortbacktrace_dbg "@$@unimplemented smaninterp_gimple condfalse" 15)
(assert_msg "@$@unimplemented smaninterp_gimple condfalse" ())
)
;; handle single assign
( ?(gimple_assign_single ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_single lhs" lhs)
(debugtree "smallana_gimple gimple_assign_single rhs" rhs)
(debuggimple "unimplemented smaninterp_gimple assign_single g" g)
(assert_msg "@$@unimplemented smaninterp_gimple singleassign" ())
)
;; handle cast assign
( ?(gimple_assign_cast ?lhs ?rhs)
(debugtree "smallana_gimple gimple_assign_cast lhs" lhs)
(debugtree "smallana_gimple gimple_assign_cast rhs" rhs)
(let ( (:tree lhstype (tree_type lhs))
(:tree rhstype (tree_type rhs))
(:long widenningcast 0)
)
(debugtree "smallana_gimple gimple_assign_cast lhstype" lhstype)
(debugtree "smallana_gimple gimple_assign_cast rhstype" rhstype)
(match
rhstype
( ?(tree_integer_type ?rname ?rbigmin ?rbigmax ?rsize)
(match
lhstype
( ?(tree_integer_type ?lname ?lbigmin ?lbigmax ?lsize)
(code_chunk
testinside
#{
mpz_t $testinside#_lminz;
mpz_t $testinside#_lmaxz;
mpz_t $testinside#_rminz;
mpz_t $testinside#_rmaxz;
mpz_init_set_ui($testinside#_lminz, 0UL);
mpz_init_set_ui($testinside#_lmaxz, 0UL);
mpz_init_set_ui($testinside#_rminz, 0UL);
mpz_init_set_ui($testinside#_rmaxz, 0UL);
if ($lsize >= $rsize
&& melt_fill_mpz_from_mixbigint($lbigmin, $testinside#_lminz)
&& melt_fill_mpz_from_mixbigint($lbigmax, $testinside#_lmaxz)
&& melt_fill_mpz_from_mixbigint($rbigmin, $testinside#_rminz)
&& melt_fill_mpz_from_mixbigint($rbigmax, $testinside#_rmaxz)
&& mpz_cmp($testinside#_lminz, $testinside#_rminz) <= 0
&& mpz_cmp($testinside#_lmaxz, $testinside#_rmaxz) >= 0)
$widenningcast = 1;
mpz_clear($testinside#_lminz);
mpz_clear($testinside#_lmaxz);
mpz_clear($testinside#_rminz);
mpz_clear($testinside#_rmaxz);
}#)
)
)))
(if widenningcast
(progn
(assert_msg "@$@unimplemented smaninterp_gimple widenningcast" ())
)
(progn
(assert_msg "@$@unimplemented smaninterp_gimple non widenningcast" ())
))
(debuggimple "unimplemented smaninterp_gimple assign_cast g" g)
(assert_msg "@$@unimplemented smaninterp_gimple castassign" ())
))
;; default
(?_
(debuggimple "unimplemented smaninterp_gimple g" g)
(assert_msg "@$@unimplemented smaninterp_gimple" ()))
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun smallana_interpret (sman)
(debug_msg sman "smallana_interpret sman")
(assert_msg "check sman" (is_a sman class_smallanalysis))
(let ( (mainf (mapstring_rawget (get_field :sman_cfundict sman) "main"))
)
(debug_msg mainf "smallana_interpret mainf")
(assert_msg "check mainf" (is_a mainf class_smallcfun))
(smaninterp_cfun sman initial_smallabstractenv mainf)
(debug_msg sman "smallana_interpret end sman")
)
)
;;; our small analysis gate for latessa
;; for some reason, the smallana is triggered twice, and the first run
;; is enough...
(defun smallanapass_gate (latessapass)
(debug_msg smallana_cont "smallanapass_gate smallana_cont at start")
(let ( (oldsman (get_field :container_value smallana_cont)) )
(if oldsman
(progn
(shortbacktrace_dbg "smallanapass_gate already got oldsman" 20)
(debug_msg oldsman "smallanapass_gate already got oldsman")
(return ()))
(return smallana_cont)
)))
;;; apparently only the entrybb loop matter in the latessa pass...
(defun smallanapass_exec (latessapass)
(debug_msg latessapass "smallanapass_exec start")
(let ( (cfuns (make_list discr_list))
(cfundict (make_mapstring discr_map_strings 20))
(cfuntreemap (make_maptree discr_map_trees 20))
(sman (instance class_smallanalysis
:sman_cfuns cfuns
:sman_cfundict cfundict
:sman_cfuntreemap cfuntreemap
:sman_bbtable (make_mapbasicblock discr_map_basic_blocks 20)
))
)
(debug_msg sman "smallanapass_exec sman at start")
;; don't bother do_each_cfun_body here.. it is not done...
(each_cgraph_fun_entryblock
()
(:tree decl :basic_block bbent bbexit)
(debugtree "smallana exec eachcgraph decl" decl)
(smallana_cfun_entbb sman decl bbent bbexit)
)
;;
(debug_msg sman "smallanapass_exec sman after eachentrybb")
;;
(each_bb_cfun
()
(:basic_block cfbb :tree cfdecl)
(smallana_cfun_bb sman cfdecl cfbb)
)
;;
(debug_msg sman "smallanapass_exec sman after eachbbcfun")
;;
(smallana_interpret sman)
;;
(put_fields smallana_cont :container_value sman)
(debug_msg smallana_cont "smallanapass_exec final smallana_cont")
))
;;; our small analysis mode -fmelt=smallana
(defun smallana_docmd (cmd moduldata)
(let ( (smallana_ipa_gccpass
(compile_warning "should create and register a simple ipa pass" ())) )
;; fill the ipa pass
(put_fields smallana_ipa_gccpass
:gccpass_gate smallanapass_gate
:gccpass_exec smallanapass_exec)
(debug_msg smallana_ipa_gccpass "smallana_ipa_gccpass")
(assert_msg "$@$incomplete smallana_mode" ())
(return cmd) ;return non-nil to continue compilation
))
;;;
(definstance smallana_mode
class_melt_mode
:named_name '"smallana"
:meltmode_help '"install a small analysis pass"
:meltmode_fun smallana_docmd
)
(install_melt_mode smallana_mode)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; a pass to test that all relevant cmatcher-s are eough to scan a source program
(defclass class_justscan_data
:super class_proped
:fields (juscan_pass
juscan_data
juscan_fundecl ;boxed current function decl tree
juscan_curbb ;boxed current basic block
))
(defun justscanpass_gate (pass)
(debug_msg pass "justscanpass_gate pass at start")
;; retrieve the data associated to the cfun in the pass. If it
;; exists (i.e. is not null) we avoid running the pass again
(let ( (grdata (maptree_get (get_field :gccpass_data pass) (cfun_decl))) )
(debugtree "justscanpass_gate cfundecl" (cfun_decl))
(debug_msg grdata "justscanpass_gate grdata")
(if grdata
() ;; return null, so avoid executing the pass
pass)
))
(defun justscanpass_exec (pass)
(debug_msg pass "justscanpass_exec pass at start")
(code_chunk dbgcfun #{ /* $dbgcfun :: */
debugeprintf("justscanpass_exec start cfun=%p cfg=%p",
(void*)cfun, cfun?cfun->cfg:NULL) ;
}#)
(assert_msg "justscanpass_exec check cfun has cfg" (cfun_has_cfg))
(let ( (jsdata (instance class_justscan_data
:juscan_pass pass
)) )
;; add our grdata associated to the cfun_decl in the pass data,
;; hence the gate will avoid running the pass twice
(maptree_put (get_field :gccpass_data pass) (cfun_decl) jsdata)
(justscan_tree jsdata (cfun_decl))
;; gimple ssa passes have a cfg..
(each_bb_cfun
()
(:basic_block bb :tree fundecl)
(put_fields jsdata :juscan_fundecl (make_tree discr_tree fundecl))
(if bb (justscan_bb jsdata bb))
)
(debug_msg jsdata "justscanpass_exec final jsdata")
)
)
(defun justscan_tree (jsdata :tree t)
(if t
(match
t
(?(tree_function_decl ?_ ?tfinit)
(if tfinit (justscan_tree jsdata tfinit)))
(?(tree_of_type ?ttyp)
(if ttyp (justscan_tree jsdata ttyp)))
(?(tree_var_decl ?_ ?_ ?_)
)
(?(tree_block ?tvars ?tsublocks)
(if tvars (justscan_tree jsdata tvars))
(if tsublocks (justscan_tree jsdata tsublocks))
)
(?(tree_parm_decl ?tptyp ?tpdecl ?_)
(if tptyp (justscan_tree jsdata tptyp))
(if tpdecl (justscan_tree jsdata tpdecl))
)
(?(tree_decl ?tdecl ?_ ?_)
(if tdecl (justscan_tree jsdata tdecl)))
(?(tree_integer_type ?_ ?_ ?_ ?_)
)
(?(tree_integer_cst ?_)
)
(?(tree_ssa_name ?tvar ?tvalu ?_ ?defg)
(if tvar (justscan_tree jsdata tvar))
(if tvalu (justscan_tree jsdata tvalu))
(if defg (justscan_gimple jsdata defg))
)
(?_
(debugtree "unhandled tree" t)
(warning_at_tree t "unhandled tree by justscan")
(warning_at_tree (tree_content (get_field :juscan_fundecl jsdata)) "justscan function decl")
(assert_msg "unhandled tree justscan" ())
)))
)
(defun justscan_gimple (jsdata :gimple g)
(if g
(match
g
(?(gimple_assign_single ?lhs ?rhs)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs)
)
(?(gimple_assign_cast ?lhs ?rhs)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs)
)
(?(gimple_assign_copy ?lhs ?rhs)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs)
)
(?(gimple_assign_ssa_name_copy ?lhs ?rhs)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs)
)
(?(gimple_assign_unary_nop ?lhs ?rhs)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs)
)
(?(gimple_assign_plus ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_minus ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_pointerplus ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_mult ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_trunc_div ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_ceil_div ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_floor_div ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_round_div ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?(gimple_assign_rdiv ?lhs ?rhs1 ?rhs2)
(justscan_tree jsdata lhs)
(justscan_tree jsdata rhs1)
(justscan_tree jsdata rhs2)
)
(?_
(debuggimple "unhandled gimple" g)
(warning_at_gimple g "unhandled gimple by justscan")
(warning_at_tree (tree_content (get_field :juscan_fundecl jsdata)) "justscan function decl")
(assert_msg "unhandled gimple justscan" ())
)
)
))
(defun justscan_bb (jsdata :basic_block bb)
(debug_msg jsdata "justscan_bbdcl jsdata start")
(if bb
(let ( (oldbb (get_field :juscan_curbb jsdata)) )
(debugbasicblock "justscan_bb bb" bb)
(put_fields jsdata :juscan_curbb (make_basicblock discr_basic_block bb))
(eachgimple_in_basicblock
(bb)
(:gimple g)
(justscan_gimple jsdata bb)
)
(put_fields jsdata :juscan_curbb oldbb)
)))
;;;;;;;;;;;;;;;;
;;; our just scan mode -fmelt=justscan
(defun justscan_docmd (cmd moduldata)
(let ( (justscanpass
(instance class_gcc_gimple_pass
:named_name '"melt_justscanpass"
:gccpass_gate justscanpass_gate
:gccpass_exec justscanpass_exec
:gccpass_data (make_maptree discr_map_trees 100)
:gccpass_properties_required ()
))
)
(install_melt_gcc_pass justscanpass "after" "phiopt" 0)
(debug_msg justscanpass "justscan_mode installed pass")
(return cmd) ;return non-nil to continue compilation
))
(definstance justscan_mode
class_melt_mode
:named_name '"justscan"
:meltmode_help '"install a pass scanning all the code"
:meltmode_fun justscan_docmd
)
(install_melt_mode justscan_mode "justscan")
;; eof ana-simple.melt
|