forked from cseagle/blc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.cc
3501 lines (3033 loc) · 104 KB
/
block.cc
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
/* ###
* IP: GHIDRA
* NOTE: Cooper, Harvey, Kennedy dominance algorithm
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "block.hh"
#include "funcdata.hh"
/// The edge is saved assuming we already know what block we are in
/// \param s is the output stream
void BlockEdge::saveXml(ostream &s) const
{
s << "<edge";
// We are not saving label currently
a_v_i(s,"end",point->getIndex()); // Reference to other end of edge
a_v_i(s,"rev",reverse_index); // Position within other blocks edgelist
s << "/>\n";
}
/// \param el is the \<edge> tag
/// \param resolver is used to cross-reference the edge's FlowBlock endpoints
void BlockEdge::restoreXml(const Element *el,BlockMap &resolver)
{
label = 0; // Tag does not currently contain info about label
int4 endIndex;
istringstream s(el->getAttributeValue("end"));
s.unsetf(ios::dec | ios::hex | ios::oct);
s >> endIndex;
point = resolver.findLevelBlock(endIndex);
if (point == (FlowBlock *)0)
throw LowlevelError("Bad serialized edge in block graph");
istringstream s2(el->getAttributeValue("rev"));
s2.unsetf(ios::dec | ios::hex | ios::oct);
s2 >> reverse_index;
}
FlowBlock::FlowBlock(void)
{
flags = 0;
index = 0;
visitcount = 0;
parent = (FlowBlock *)0;
immed_dom = (FlowBlock *)0;
}
/// \param b is the FlowBlock coming in
/// \param lab is a label for the edge
void FlowBlock::addInEdge(FlowBlock *b,uint4 lab)
{
int4 ourrev = b->outofthis.size();
int4 brev = intothis.size();
intothis.push_back(BlockEdge(b,lab,ourrev));
b->outofthis.push_back(BlockEdge(this,lab,brev));
}
/// \param el is the \<edge> element
/// \param resolver is used to resolve block references
void FlowBlock::restoreNextInEdge(const Element *el,BlockMap &resolver)
{
intothis.emplace_back();
BlockEdge &inedge(intothis.back());
inedge.restoreXml(el,resolver);
while(inedge.point->outofthis.size() <= inedge.reverse_index)
inedge.point->outofthis.emplace_back();
BlockEdge &outedge(inedge.point->outofthis[inedge.reverse_index]);
outedge.label = 0;
outedge.point = this;
outedge.reverse_index = intothis.size()-1;
}
/// \param slot is the index of the incoming edge being altered
void FlowBlock::halfDeleteInEdge(int4 slot)
{
while(slot < intothis.size()-1) {
BlockEdge &edge( intothis[slot] );
edge = intothis[slot+1]; // Slide the edge entry over
// Correct the index coming the other way
BlockEdge &edger( edge.point->outofthis[edge.reverse_index] );
edger.reverse_index -= 1;
slot += 1;
}
intothis.pop_back();
}
/// \param slot is the index of the outgoing edge being altered
void FlowBlock::halfDeleteOutEdge(int4 slot)
{
while(slot < outofthis.size()-1) {
BlockEdge &edge( outofthis[slot] );
edge = outofthis[slot+1]; // Slide the edge
// Correct the index coming the other way
BlockEdge &edger( edge.point->intothis[edge.reverse_index] );
edger.reverse_index -= 1;
slot += 1;
}
outofthis.pop_back();
}
/// \param slot is the index of the incoming edge to remove
void FlowBlock::removeInEdge(int4 slot)
{
FlowBlock *b = intothis[slot].point;
int4 rev = intothis[slot].reverse_index;
halfDeleteInEdge(slot);
b->halfDeleteOutEdge(rev);
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
b->checkEdges();
#endif
}
/// \param slot is the index of the outgoing edge to remove
void FlowBlock::removeOutEdge(int4 slot)
{
FlowBlock *b = outofthis[slot].point;
int4 rev = outofthis[slot].reverse_index;
halfDeleteOutEdge(slot);
b->halfDeleteInEdge(rev);
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
b->checkEdges();
#endif
}
/// The original edge, which must exist, is replaced.
/// \param num is the index of the incoming edge
/// \param b is the new incoming block
void FlowBlock::replaceInEdge(int4 num,FlowBlock *b)
{
FlowBlock *oldb = intothis[num].point;
oldb->halfDeleteOutEdge(intothis[num].reverse_index);
intothis[num].point = b;
intothis[num].reverse_index = b->outofthis.size();
b->outofthis.push_back(BlockEdge(this,intothis[num].label,num));
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
b->checkEdges();
oldb->checkEdges();
#endif
}
/// The original edge, which must exist is replaced.
/// \param num is the index of the outgoing edge
/// \param b is the new outgoing block
void FlowBlock::replaceOutEdge(int4 num,FlowBlock *b)
{
FlowBlock *oldb = outofthis[num].point;
oldb->halfDeleteInEdge(outofthis[num].reverse_index);
outofthis[num].point = b;
outofthis[num].reverse_index = b->intothis.size();
b->intothis.push_back(BlockEdge(this,outofthis[num].label,num));
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
b->checkEdges();
oldb->checkEdges();
#endif
}
/// Remove edge \b in and \b out from \b this block, but create
/// a new edge between the in-block and the out-block, preserving
/// position in the in/out edge lists.
/// \param in is the index of the incoming block
/// \param out is the index of the outgoing block
void FlowBlock::replaceEdgesThru(int4 in,int4 out)
{
FlowBlock *inb = intothis[in].point;
int4 inblock_outslot = intothis[in].reverse_index;
FlowBlock *outb = outofthis[out].point;
int4 outblock_inslot = outofthis[out].reverse_index;
inb->outofthis[inblock_outslot].point = outb;
inb->outofthis[inblock_outslot].reverse_index = outblock_inslot;
outb->intothis[outblock_inslot].point = inb;
outb->intothis[outblock_inslot].reverse_index = inblock_outslot;
halfDeleteInEdge(in);
halfDeleteOutEdge(out);
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
inb->checkEdges();
outb->checkEdges();
#endif
}
void FlowBlock::swapEdges(void)
{
#ifdef BLOCKCONSISTENT_DEBUG
if (outofthis.size() != 2)
throw LowlevelError("Swapping edges for block that doesn't have two edges");
#endif
BlockEdge tmp = outofthis[0];
outofthis[0] = outofthis[1];
outofthis[1] = tmp;
FlowBlock *bl = outofthis[0].point;
bl->intothis[ outofthis[0].reverse_index ].reverse_index = 0;
bl = outofthis[1].point;
bl->intothis[ outofthis[1].reverse_index ].reverse_index = 1;
flags ^= f_flip_path;
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
#endif
}
/// \param i is the index of the outgoing edge
/// \param lab is the new edge label
void FlowBlock::setOutEdgeFlag(int4 i,uint4 lab)
{
FlowBlock *bbout = outofthis[i].point;
outofthis[i].label |= lab;
bbout->intothis[ outofthis[i].reverse_index ].label |= lab;
}
/// \param i is the index of the outgoing edge
/// \param lab is the edge label to remove
void FlowBlock::clearOutEdgeFlag(int4 i,uint4 lab)
{
FlowBlock *bbout = outofthis[i].point;
outofthis[i].label &= ~lab;
bbout->intothis[ outofthis[i].reverse_index ].label &= ~lab;
}
/// \param bump if \b true, mark that labels for this block are printed by somebody higher in hierarchy
void FlowBlock::markLabelBumpUp(bool bump)
{
if (bump)
flags |= f_label_bumpup;
}
/// Block references are updated using the getCopyMap() reference on the original block
/// \param vec is the list of edges whose block references should be updated
void FlowBlock::replaceEdgeMap(vector<BlockEdge> &vec)
{
vector<BlockEdge>::iterator iter;
for(iter=vec.begin();iter!=vec.end();++iter)
(*iter).point = (*iter).point->getCopyMap();
}
/// Run through incoming and outgoing edges and replace FlowBlock references with
/// the FlowBlock accessed via the getCopyMap() method.
void FlowBlock::replaceUsingMap(void)
{
replaceEdgeMap(intothis);
replaceEdgeMap(outofthis);
if (immed_dom != (FlowBlock *)0)
immed_dom = immed_dom->getCopyMap();
}
/// Flip the order of outgoing edges (at least).
/// This should also affect the original op causing the condition.
/// Note: we don't have to flip at all levels of the hierarchy
/// only at the top and at the bottom
/// \param toporbottom is \b true if \b this is the top outermost block of the hierarchy getting negated
/// \return \b true if a change was made to data-flow
bool FlowBlock::negateCondition(bool toporbottom)
{
if (!toporbottom) return false; // No change was made to data-flow
swapEdges();
return false;
}
/// This is the main entry point for marking a branch
/// from one block to another as unstructured.
/// \param i is the index of the outgoing edge to mark
void FlowBlock::setGotoBranch(int4 i)
{ if ((i>=0)&&(i < outofthis.size()))
setOutEdgeFlag(i,f_goto_edge);
else
throw LowlevelError("Could not find block edge to mark unstructured");
flags |= f_interior_gotoout; // Mark that there is a goto out of this block
outofthis[i].point->flags |= f_interior_gotoin;
}
/// \b return \b true if block is the target of a jump
bool FlowBlock::isJumpTarget(void) const
{
for(int4 i=0;i<intothis.size();++i)
if (intothis[i].point->index != index-1) return true;
return false;
}
/// Keep descending tree hierarchy, taking the front block,
/// until we get to the bottom copy block
/// \return the first leaf FlowBlock to execute
const FlowBlock *FlowBlock::getFrontLeaf(void) const
{
const FlowBlock *bl = this;
while(bl->getType() != t_copy) {
bl = bl->subBlock(0);
if (bl == (const FlowBlock *)0) return bl;
}
return bl;
}
/// Keep descending tree hierarchy, taking the front block,
/// until we get to the bottom copy block
/// \return the first leaf FlowBlock to execute
FlowBlock *FlowBlock::getFrontLeaf(void)
{
FlowBlock *bl = this;
while(bl->getType() != t_copy) {
bl = bl->subBlock(0);
if (bl == (FlowBlock *)0) return bl;
}
return bl;
}
/// How many getParent() calls from the leaf to \b this
/// \param leaf is the component FlowBlock
/// \return the depth count
int4 FlowBlock::calcDepth(const FlowBlock *leaf) const
{
int4 depth = 0;
while(leaf != this) {
if (leaf == (const FlowBlock *)0)
return -1;
leaf = leaf->getParent();
depth += 1;
}
return depth;
}
/// Return \b true if \b this block \e dominates the given block (or is equal to it).
/// This assumes that block indices have been set with a reverse post order so that having a
/// smaller index is a necessary condition for dominance.
/// \param subBlock is the given block to test against \b this for dominance
/// \return \b true if \b this dominates
bool FlowBlock::dominates(const FlowBlock *subBlock) const
{
while(subBlock != (const FlowBlock *)0 && index <= subBlock->index) {
if (subBlock == this) return true;
subBlock = subBlock->getImmedDom();
}
return false;
}
/// \brief Check if the condition from the given block holds for \b this block
///
/// We assume the given block has 2 out-edges and that \b this block is immediately reached by
/// one of these two edges. Some condition holds when traversing the out-edge to \b this, and the complement
/// of the condition holds for traversing the other out-edge. We verify that the condition holds for
/// this entire block. More specifically, we check that that there is no path to \b this through the
/// sibling edge, where the complement of the condition holds (unless we loop back through the conditional block).
/// \param cond is the conditional block with 2 out-edges
/// \return \b true if the condition holds for this block
bool FlowBlock::restrictedByConditional(const FlowBlock *cond) const
{
if (sizeIn() == 1) return true; // Its impossible for any path to come through sibling to this
if (getImmedDom() != cond) return false; // This is not dominated by conditional block at all
for(int4 i=0;i<sizeIn();++i) {
const FlowBlock *inBlock = getIn(i);
if (inBlock == cond) continue; // The unique edge from cond to this
while(inBlock != this) {
if (inBlock == cond) return false; // Must have come through sibling
inBlock = inBlock->getImmedDom();
}
}
return true;
}
/// \return \b true if \b this is the top of a loop
bool FlowBlock::hasLoopIn(void) const
{
for(int4 i=0;i<intothis.size();++i)
if ((intothis[i].label & f_loop_edge)!=0) return true;
return false;
}
/// \return \b true if \b this is the bottom of a loop
bool FlowBlock::hasLoopOut(void) const
{
for(int4 i=0;i<outofthis.size();++i)
if ((outofthis[i].label & f_loop_edge)!=0) return true;
return false;
}
/// \param bl is the given block
void FlowBlock::eliminateInDups(FlowBlock *bl)
{
int4 indval = -1;
int4 i=0;
while(i < intothis.size()) {
if (intothis[i].point == bl) {
if (indval == -1) { // The first instance of bl
indval = i; // We keep it
i += 1;
}
else {
intothis[indval].label |= intothis[i].label;
int4 rev = intothis[i].reverse_index;
halfDeleteInEdge(i);
bl->halfDeleteOutEdge(rev);
}
}
else
i += 1;
}
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
bl->checkEdges();
#endif
}
/// \param bl is the given block
void FlowBlock::eliminateOutDups(FlowBlock *bl)
{
int4 indval = -1;
int4 i=0;
while(i < outofthis.size()) {
if (outofthis[i].point == bl) {
if (indval == -1) { // The first instance of bl
indval = i; // We keep it
i += 1;
}
else {
outofthis[indval].label |= outofthis[i].label;
int4 rev = outofthis[i].reverse_index;
halfDeleteOutEdge(i);
bl->halfDeleteInEdge(rev);
}
}
else
i += 1;
}
#ifdef BLOCKCONSISTENT_DEBUG
checkEdges();
bl->checkEdges();
#endif
}
/// \brief Find blocks that are at the end of multiple edges
///
/// \param ref is the list of BlockEdges to search
/// \param duplist will contain the list of blocks with duplicate edges
void FlowBlock::findDups(const vector<BlockEdge> &ref,vector<FlowBlock *> &duplist)
{
vector<BlockEdge>::const_iterator iter;
for(iter=ref.begin();iter!=ref.end();++iter) {
if (((*iter).point->flags&f_mark2)!=0) continue; // Already marked as a duplicate
if (((*iter).point->flags&f_mark)!=0) { // We have a duplicate
duplist.push_back((*iter).point);
(*iter).point->flags |= f_mark2;
}
else
(*iter).point->flags |= f_mark;
}
for(iter=ref.begin();iter!=ref.end();++iter) // Erase our marks
(*iter).point->flags &= ~(f_mark | f_mark2);
}
void FlowBlock::dedup(void)
{
vector<FlowBlock *> duplist;
vector<FlowBlock *>::iterator iter;
findDups(intothis,duplist);
for(iter=duplist.begin();iter!=duplist.end();++iter)
eliminateInDups(*iter);
duplist.clear();
findDups(outofthis,duplist);
for(iter=duplist.begin();iter!=duplist.end();++iter)
eliminateOutDups(*iter);
}
#ifdef BLOCKCONSISTENT_DEBUG
/// Make sure block references in the BlockEdge objects owned
/// by \b this block, and any other block at the other end of these edges,
/// are consistent.
void FlowBlock::checkEdges(void)
{
for(int4 i=0;i<intothis.size();++i) {
BlockEdge &edge( intothis[i] );
int4 rev = edge.reverse_index;
FlowBlock *bl = edge.point;
if (bl->outofthis.size() <= rev)
throw LowlevelError("Not enough outofthis blocks");
BlockEdge &edger( bl->outofthis[rev] );
if (edger.point != this)
throw LowlevelError("Intothis edge mismatch");
if (edger.reverse_index != i)
throw LowlevelError("Intothis index mismatch");
}
for(int4 i=0;i<outofthis.size();++i) {
BlockEdge &edge( outofthis[i] );
int4 rev = edge.reverse_index;
FlowBlock *bl = edge.point;
if (bl->intothis.size() <= rev)
throw LowlevelError("Not enough intothis blocks");
BlockEdge &edger( bl->intothis[rev] );
if (edger.point != this)
throw LowlevelError("Outofthis edge mismatch");
if (edger.reverse_index != i)
throw LowlevelError("Outofthis index mismatch");
}
}
#endif
/// Search through incoming blocks in edge order for the given block.
/// \param bl is the given FlowBlock
/// \return the matching edge index or -1 if \b bl doesn't flow into \b this
int4 FlowBlock::getInIndex(const FlowBlock *bl) const
{
int4 blocknum;
for(blocknum=0;blocknum<intothis.size();++blocknum)
if (intothis[blocknum].point==bl) return blocknum;
return -1; // That block not found
}
/// Search through outgoing blocks in edge order for the given block.
/// \param bl is the given FlowBlock
/// \return the matching edge index or -1 if \b bl doesn't flow out of \b this
int4 FlowBlock::getOutIndex(const FlowBlock *bl) const
{
int4 blocknum;
for(blocknum=0;blocknum<outofthis.size();++blocknum)
if (outofthis[blocknum].point==bl) return blocknum;
return -1;
}
/// Only print a header for \b this single block
/// \param s is the output stream
void FlowBlock::printHeader(ostream &s) const
{
s << dec << index;
if (!getStart().isInvalid() && !getStop().isInvalid()) {
s << ' ' << getStart() << '-' << getStop();
}
}
/// Recursively print out the hierarchical structure of \b this FlowBlock.
/// \param s is the output stream
/// \param level is the current level of indentation
void FlowBlock::printTree(ostream &s,int4 level) const
{
int4 i;
for(i=0;i<level;++i)
s << " ";
printHeader(s);
s << endl;
}
/// If \b this FlowBlock was ends with a computed jump, retrieve
/// the associated JumpTable object
/// \return the JumpTable object or NULL
JumpTable *FlowBlock::getJumptable(void) const
{
JumpTable *jt = (JumpTable *)0;
if (!isSwitchOut()) return jt;
PcodeOp *indop = lastOp();
if (indop != (PcodeOp *)0)
jt = indop->getParent()->getFuncdata()->findJumpTable(indop);
return jt;
}
/// Given a string describing a FlowBlock type, return the block_type.
/// This is currently only used by the restoreXml() process.
/// TODO: Fill in the remaining names and types
/// \param nm is the name string
/// \return the corresponding block_type
FlowBlock::block_type FlowBlock::nameToType(const string &nm)
{
FlowBlock::block_type bt = FlowBlock::t_plain;
if (nm == "graph")
bt = FlowBlock::t_graph;
else if (nm == "copy")
bt = FlowBlock::t_copy;
return bt;
}
/// For use in serializng FlowBlocks to XML.
/// \param bt is the block_type
/// \return the corresponding name string
string FlowBlock::typeToName(FlowBlock::block_type bt)
{
switch(bt) {
case t_plain:
return "plain";
case t_basic:
return "basic";
case t_graph:
return "graph";
case t_copy:
return "copy";
case t_goto:
return "goto";
case t_multigoto:
return "multigoto";
case t_ls:
return "list";
case t_condition:
return "condition";
case t_if:
return "properif";
case t_whiledo:
return "whiledo";
case t_dowhile:
return "dowhile";
case t_switch:
return "switch";
case t_infloop:
return "infloop";
}
return "";
}
/// Comparator for ordering the final 0-exit blocks
/// \param bl1 is the first FlowBlock to compare
/// \param bl2 is the second FlowBlock
/// \return true if the first comes before the second
bool FlowBlock::compareFinalOrder(const FlowBlock *bl1,const FlowBlock *bl2)
{
if (bl1->getIndex() == 0) return true; // Make sure the entry point comes first
if (bl2->getIndex() == 0) return false;
PcodeOp *op1 = bl1->lastOp();
PcodeOp *op2 = bl2->lastOp();
if (op1 != (PcodeOp *)0) { // Make sure return blocks come last
if (op2 != (PcodeOp *)0) {
if ((op1->code() == CPUI_RETURN)&&(op2->code() != CPUI_RETURN))
return false;
else if ((op1->code() != CPUI_RETURN)&&(op2->code() == CPUI_RETURN))
return true;
}
if (op1->code() == CPUI_RETURN) return false;
}
else if (op2 != (PcodeOp *)0) {
if (op2->code() == CPUI_RETURN) return true;
}
return (bl1->getIndex() < bl2->getIndex()); // Otherwise use index
}
/// Within the dominator tree, find the earliest common ancestor of two FlowBlocks
/// \param bl1 is the first FlowBlock
/// \param bl2 is the second
/// \return the common ancestor which dominates both
FlowBlock *FlowBlock::findCommonBlock(FlowBlock *bl1,FlowBlock *bl2)
{
FlowBlock *b1,*b2,*common;
common = (FlowBlock *)0;
b1 = bl1;
b2 = bl2;
for(;;) {
if (b2 == (FlowBlock *)0) {
while(b1 != (FlowBlock *)0) {
if (b1->isMark()) {
common = b1;
break;
}
b1 = b1->getImmedDom();
}
break;
}
if (b1 == (FlowBlock *)0) {
while(b2 != (FlowBlock *)0) {
if (b2->isMark()) {
common = b2;
break;
}
b2 = b2->getImmedDom();
}
break;
}
if (b1->isMark()) {
common = b1;
break;
}
b1->setMark();
if (b2->isMark()) {
common = b2;
break;
}
b2->setMark();
b1 = b1->getImmedDom();
b2 = b2->getImmedDom();
}
// Clear our marks
while(bl1!=(FlowBlock *)0) {
if (!bl1->isMark()) break;
bl1->clearMark();
bl1 = bl1->getImmedDom();
}
while(bl2!=(FlowBlock *)0) {
if (!bl2->isMark()) break;
bl2->clearMark();
bl2 = bl2->getImmedDom();
}
return common;
}
/// Find the most immediate dominating FlowBlock of all blocks in the given set.
/// The container must not be empty.
/// \param blockSet is the given set of blocks
/// \return the most immediate dominating FlowBlock
FlowBlock *FlowBlock::findCommonBlock(const vector<FlowBlock *> &blockSet)
{
vector<FlowBlock *> markedSet;
FlowBlock *bl;
FlowBlock *res = blockSet[0];
int4 bestIndex = res->getIndex();
bl = res;
do {
bl->setMark();
markedSet.push_back(bl);
bl = bl->getImmedDom();
} while (bl != (FlowBlock *)0);
for(int4 i=1;i<blockSet.size();++i) {
if (bestIndex == 0)
break;
bl = blockSet[i];
while(!bl->isMark()) {
bl->setMark();
markedSet.push_back(bl);
bl = bl->getImmedDom();
}
if (bl->getIndex() < bestIndex) { // If first meeting with old paths is higher than ever before
res = bl; // we have a new best
bestIndex = res->getIndex();
}
}
for(int4 i=0;i<markedSet.size();++i)
markedSet[i]->clearMark();
return res;
}
/// Add the given FlowBlock to the list and make \b this the parent
/// Update \b index so that it has the minimum over all components
/// \param bl is the given FlowBlock
void BlockGraph::addBlock(FlowBlock *bl)
{
int4 min = bl->index;
if (list.empty()) {
index = min;
}
else {
if (min < index) index = min;
}
bl->parent = this;
list.push_back(bl);
}
/// Force \b this FlowBlock to have the indicated number of outputs.
/// Create edges back into itself if necessary.
/// \param i is the number of out edges to force
void BlockGraph::forceOutputNum(int4 i)
{
#ifdef BLOCKCONSISTENT_DEBUG
if (sizeOut() > i)
throw LowlevelError("Bad block output force");
#endif
while(sizeOut() < i)
addInEdge(this,f_loop_edge|f_back_edge);
}
/// Examine the set of components and their incoming and outgoing edges. If both
/// ends of the edge are not within the set, then \b this block inherits the edge.
/// A formal BlockEdge is added between \b this and the FlowBlock outside the set.
/// The edges are deduplicated.
void BlockGraph::selfIdentify(void)
{
vector<BlockEdge>::iterator tmp;
vector<FlowBlock *>::iterator iter;
FlowBlock *mybl,*otherbl;
if (list.empty()) return;
for(iter=list.begin();iter!=list.end();++iter) {
mybl = *iter;
int4 i = 0;
while(i<mybl->intothis.size()) {
otherbl = mybl->intothis[i].point;
if (otherbl->parent == this)
i += 1;
else {
for(int4 j=0;j<otherbl->outofthis.size();++j)
if (otherbl->outofthis[j].point == mybl)
otherbl->replaceOutEdge(j,this);
// Dont increment i
}
}
i = 0;
while(i<mybl->outofthis.size()) {
otherbl = mybl->outofthis[i].point;
if (otherbl->parent == this)
i += 1;
else {
for(int4 j=0;j<otherbl->intothis.size();++j)
if (otherbl->intothis[j].point == mybl)
otherbl->replaceInEdge(j,this);
if (mybl->isSwitchOut()) // Check for indirect branch out
setFlag(f_switch_out);
}
}
}
dedup();
}
/// \brief Move nodes from \b this into a new BlockGraph
///
/// This does most of the work of collapsing a set of components in \b this
/// into a single node. The components are removed from \b this, put in the new FlowBlock
/// and adjusts edges. The new FlowBlock must be added back into \b this.
/// \param ident is the new FlowBlock
/// \param nodes is the list component FlowBlocks to move
void BlockGraph::identifyInternal(BlockGraph *ident,const vector<FlowBlock *> &nodes)
{
vector<FlowBlock *>::const_iterator iter;
for(iter=nodes.begin();iter!=nodes.end();++iter) {
#ifdef BLOCKCONSISTENT_DEBUG
if ((*iter)->parent != this)
throw LowlevelError("Bad block identify");
#endif
(*iter)->setMark();
ident->addBlock(*iter); // Maintain order of blocks
ident->flags |= ((*iter)->flags & (f_interior_gotoout | f_interior_gotoin));
}
vector<FlowBlock *> newlist;
for(iter=list.begin();iter!=list.end();++iter) { // Remove -nodes- from our list
if (!(*iter)->isMark())
newlist.push_back(*iter);
else
(*iter)->clearMark();
}
list = newlist;
ident->selfIdentify();
}
/// \param flags is the set of boolean properties
void BlockGraph::clearEdgeFlags(uint4 flags)
{
flags = ~flags;
int4 size = list.size();
for(int4 i=0;i<size;++i) {
FlowBlock *bl = list[i];
for(int4 i=0;i<bl->intothis.size();++i)
bl->intothis[i].label &= flags;
for(int4 i=0;i<bl->outofthis.size();++i)
bl->outofthis[i].label &= flags;
}
}
/// \brief Create a single root block
///
/// Some algorithms need a graph with a single entry node. Given multiple entry points,
/// this routine creates an artificial root with no \e in edges and an \e out
/// edge to each of the real entry points. The resulting root FlowBlock isn't
/// owned by any BlockGraph, and the caller is responsible for freeing it.
/// \param rootlist is the given set of entry point FlowBlocks
/// \return the new artificial root FlowBlock
FlowBlock *BlockGraph::createVirtualRoot(const vector<FlowBlock *> &rootlist)
{
FlowBlock *newroot = new FlowBlock();
for(int4 i=0;i<rootlist.size();++i)
rootlist[i]->addInEdge(newroot,0);
return newroot;
}
/// \brief Find a spanning tree (skipping irreducible edges).
///
/// - Label pre and reverse-post orderings, tree, forward, cross, and back edges.
/// - Calculate number of descendants.
/// - Put the blocks of the graph in reverse post order.
/// - Return an array of all nodes in pre-order.
/// - If the graph does not have a real root, create one and return it, otherwise return null.
///
/// Algorithm originally due to Tarjan.
/// The first block is the entry block, and should remain the first block
/// \param preorder will hold the list of FlowBlock components in pre-order
/// \param rootlist will hold the list of entry points
void BlockGraph::findSpanningTree(vector<FlowBlock *> &preorder,vector<FlowBlock *> &rootlist)
{
if (list.size()==0) return;
vector<FlowBlock *> rpostorder;
vector<FlowBlock *> state;
vector<int4> istate;
FlowBlock *tmpbl;
int4 origrootpos;
preorder.reserve(list.size());
rpostorder.resize(list.size());
state.reserve(list.size());
istate.reserve(list.size());
for(int4 i=0;i<list.size();++i) {
tmpbl = list[i];
tmpbl->index = -1; // reverse post-order starts at 0
tmpbl->visitcount = -1;
tmpbl->copymap = tmpbl;
if (tmpbl->sizeIn()==0) // Keep track of all potential roots of the tree
rootlist.push_back(tmpbl);
}
if (rootlist.size() > 1) { // Make sure orighead is visited last, (so it is first in the reverse post order)
tmpbl = rootlist[rootlist.size()-1];
rootlist[rootlist.size()-1] = rootlist[0];
rootlist[0] = tmpbl;
}
else if (rootlist.size() == 0) { // If there's no obvious starting block
rootlist.push_back(list[0]); // Assume first block is entry point
}
origrootpos = rootlist.size()-1; // Position of original head in rootlist
for(int4 repeat=0;repeat<2;++repeat) {
bool extraroots = false;
int4 rpostcount = list.size();
int4 rootindex = 0;
clearEdgeFlags(~((uint4)0)); // Clear all edge flags
while(preorder.size() < list.size()) {
FlowBlock *startbl = (FlowBlock *)0;
while(rootindex<rootlist.size()) { // Go thru blocks with no in edges
startbl = rootlist[rootindex];
rootindex += 1;
if (startbl->visitcount == -1) break;
// If we reach here, startbl isn't really a root (root from previous pass)
for(int4 i=rootindex;i<rootlist.size();++i)
rootlist[i-1] = rootlist[i];
rootlist.pop_back(); // Remove it
rootindex -= 1;
startbl = (FlowBlock *)0;
}
if (startbl == (FlowBlock *)0) { // If we didn't find one, just take next unvisited
extraroots = true;
for(int4 i=0;i<list.size();++i) {
startbl = list[i];
if (startbl->visitcount == -1) break;
}
rootlist.push_back(startbl); // We have to treat this block as another root
rootindex += 1; // Update root traversal state
}
state.push_back(startbl);
istate.push_back(0);
startbl->visitcount = preorder.size();