-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrolflow.cpp
1087 lines (850 loc) · 32.5 KB
/
controlflow.cpp
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
//
// Copyright (c) 2009 Eric
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// $Id$
#include <boost/dynamic_bitset.hpp>
#include <stack>
#include "idainternal.hpp"
#include "controlflow.hpp"
#include "ida-x86.hpp"
#include "collatenode.hpp"
#include "collateexpr.hpp"
//work goes here. ;-)
void ControlFlowAnalysis::FindDominators(Node_list &blocks)
{
int nNodes = blocks.size();
int i = 0;
for (Node_list::iterator n = blocks.begin();
n != blocks.end();
n++)
{
Node_ptr node = *n;
//boost::dynamic_bitset<> b(nNodes);
//b.set();
//node->SetDominators(b);
node->mDominators.resize(nNodes);
node->mDominators.set();
node->mPostDominators.resize(nNodes);
node->mPostDominators.set();
node->domId = i++;
node->mPreds.clear();
}
//wireup predecessors.
for (Node_list::iterator n = blocks.begin();
n != blocks.end();
n++)
{
Node_ptr node = *n;
for (int i = 0; i < node->SuccessorCount(); i++)
{
Node_ptr succ = node->Successor(i);
succ->ConnectPredecessor(node);
}
}
Node_ptr n = *blocks.begin();
n->mDominators.reset();
n->mDominators.set(n->domId);
boost::dynamic_bitset<> t(nNodes);
bool changed;
do
{
changed = false;
for (Node_list::iterator n = blocks.begin();
n != blocks.end();
n++)
{
Node_ptr node = *n;
if (n == blocks.begin()) //hack need to store entry node.
continue;
for (Node_list::iterator p = node->mPreds.begin();
p != node->mPreds.end();
p++)
{
Node_ptr pred = *p;
t.reset();
t |= node->mDominators;
node->mDominators &= pred->mDominators;
node->mDominators.set(node->domId);
if (node->mDominators != t)
changed = true;
}
}
} while (changed);
//Post Dominators
n = blocks.back();
n->mPostDominators.reset();
n->mPostDominators.set(n->domId);
do
{
changed = false;
for (Node_list::iterator n = blocks.begin();
n != blocks.end();
n++)
{
Node_ptr node = *n;
if (node == blocks.back()) //this should be the exit node. FIXME need better way of finding exit block.
continue;
t.set();
//t |= node->mPostDominators;
for (int i = 0; i < node->SuccessorCount(); i++)
{
Node_ptr succ = node->Successor(i);
t &= succ->mPostDominators;
}
t.set(node->domId);
if (node->mPostDominators != t)
{
changed = true;
node->mPostDominators = t;
}
}
} while (changed);
}
bool list_sort_helper(Node_ptr n, Node_ptr n1)
{
return (n->Address() < n1->Address());
}
bool loop_compare(Loop *loop, Loop *loop1)
{
Node_list n1 = loop->nodes;
Node_list n2 = loop1->nodes;
if (n2.size() > n1.size())
return false;
n1.sort(list_sort_helper);
n2.sort(list_sort_helper);
return includes(n1.begin(), n1.end(), n2.begin(), n2.end(), list_sort_helper);
}
void ControlFlowAnalysis::FindLoops()
{
for (Node_list::iterator n = mNodeList.begin();
n != mNodeList.end();
n++)
{
if (n == mNodeList.begin()) //hack need to store entry node.
continue;
Node_ptr node = *n;
for (int i = 0; i < node->SuccessorCount(); i++)
{
Node_ptr succ = node->Successor(i);
if (node->mDominators.test(succ->domId))
mLoops.push_back(FindNaturalLoopForEdge(succ, node));
}
}
if (!mLoops.empty())
{
mLoops.sort(loop_compare);
mLoops.reverse();
}
}
Loop *ControlFlowAnalysis::FindNaturalLoopForEdge(Node_ptr header, Node_ptr tail)
{
msg("Loop (%a,%a)\n", header->Address(), tail->Address());
std::stack<Node_ptr> workList;
Loop *loop = new Loop();
loop->header = header;
loop->tail = tail;
loop->nodes.push_back(header);
if (header != tail)
{
Node_ptr np(tail);
loop->nodes.push_back(np);
Node_ptr wp(tail);
workList.push(wp);
msg("loop tail node added at %a\n", tail->Address());
}
while (!workList.empty())
{
Node_ptr node = workList.top();
workList.pop();
for (Node_list::iterator p = node->mPreds.begin();
p != node->mPreds.end();
p++)
{
Node_ptr pred = *p;
//msg("pred = %a\n", pred->Address());
Node_list::iterator it = find(loop->nodes.begin(), loop->nodes.end(), pred);
if (it == loop->nodes.end())
{
Node_ptr np(pred);
loop->nodes.push_back(np);
Node_ptr wp(pred);
workList.push(wp);
msg("#loop node added at %a\n", pred->Address());
}
}
}
//loop->nodes.sort(list_sort_helper);
msg("loop count = %d\n", loop->nodes.size());
return loop;
}
Node_ptr find_node_by_addr(Node_list &list, Addr address)
{
for (Node_list::iterator n = list.begin();
n != list.end();
n++)
{
Node_ptr node = *n;
if (node->Address() == address)
return node;
}
return Node_ptr();
}
Node_ptr find_node_by_domId(Node_list &list, int domId)
{
for (Node_list::iterator n = list.begin();
n != list.end();
n++)
{
Node_ptr node = *n;
if (node->domId == domId)
return node;
}
return Node_ptr();
}
Node_ptr find_imm_post_dominator(Node_list &list, Node_list &loop_list, Node_ptr node)
{
Node_list plist;
for (int i = 0; i < node->mPostDominators.size(); i++)
{
if (node->mPostDominators[i])
{
Node_ptr postdom_node = find_node_by_domId(list, i);
if (postdom_node.use_count() > 0)
{
//Node_ptr np(postdom_node);
plist.push_back(postdom_node);//np);
msg("node at %a post dominates node at %a\n", postdom_node->Address(), node->Address());
}
}
}
if (plist.size() != 0)
{
Node_list q;
Node_list visited;
//Node_ptr np(node);
//q.push_back(np);
q.push_back(node);
for (; !q.empty();)
{
Node_ptr ptr = q.front();
q.pop_front();
Node_list::iterator it;
it = find(visited.begin(), visited.end(), ptr);
if (it != visited.end())
continue; //already visited this node must be a loop.
if (ptr != node)
{
it = find(plist.begin(), plist.end(), ptr);
if (it != plist.end())
{
Node_ptr test_node = find_node_by_addr(loop_list, ptr->Address());
if (test_node.use_count() <= 0)
{
return ptr;
}
/*
it = find(loop_list.begin(), loop_list.end(), ptr); //post dominator cannot be a loop node.
if(it == loop_list.end())
{
msg("found imm post dominator of %a at %a\n", node->Address(), ptr->Address());
return ptr; //found immediate post-dominator.
}
*/
}
}
Node_ptr vnp(ptr);
visited.push_back(vnp);
for (int i = 0; i < ptr->SuccessorCount(); i++)
{
Node_ptr snp(ptr->Successor(i));
msg("pushback %a\n", snp->Address());
q.push_back(ptr->Successor(i)); //snp);
}
}
}
return Node_ptr();
}
void collect_stray_loop_nodes(Loop *loop)
{
Node_list q;
Node_list visited;
Node_ptr np(loop->header);
q.push_back(np);
for (; !q.empty();)
{
Node_ptr ptr = q.front();
q.pop_front();
msg("ptr usage_count = %d\n", ptr.use_count());
if (ptr == loop->breakNode) //we've hit the loop post dominator
continue;
Node_list::iterator it;
it = find(visited.begin(), visited.end(), ptr);
if (it != visited.end())
continue;
Node_ptr vlnp(ptr);
visited.push_back(vlnp);
it = find(loop->nodes.begin(), loop->nodes.end(), ptr);
if (it == loop->nodes.end() && loop->header->DominatesNode(ptr))
{
Node_ptr vnp(ptr);
loop->nodes.push_back(vnp);
msg("Adding another node to loop at %a\n", ptr->Address());
}
for (int i = 0; i < ptr->SuccessorCount(); i++)
{
Node_ptr snp(ptr->Successor(i));
q.push_back(snp);
}
}
}
void ControlFlowAnalysis::StructureLoops()
{
for (std::list<Loop *>::iterator l = mLoops.begin();
l != mLoops.end();
l++)
{
Loop *loop = *l;
loop->breakNode = find_imm_post_dominator(mNodeList, loop->nodes, loop->header);
//if(breakNode.use_count() > 0)
// msg("break node at %a\n", breakNode->Address());
//doWhileNode->continueAddr = loop->nodes.back()->Address();
msg("loop header = %a, block count = %d continue address = %a\n", loop->header->Address(), loop->nodes.size(),
loop->nodes.back()->Address());
for (Node_list::iterator n = loop->nodes.begin();
n != loop->nodes.end();
n++)
{
Node_ptr node = *n;
msg("LoopNode: %a %s\n", node->Address(), node->TypeString());
}
msg("END\n");
//FIXME.. need to get last compare of last loop block.
//loop->nodes.back()->Instructions().pop_back();
// msg("removing node at %a\n", loop->nodes.back()->Address());
//mNodeList.remove(loop->nodes.back());
//loop->nodes.pop_back(); //get rid of loop goto.
Instruction::InstructionType loopType = Instruction::DO_WHILE;
if (loop->header->SuccessorCount() == 2)
{
for (int i = 0; i < loop->header->SuccessorCount(); i++)
{
if (loop->breakNode.use_count() > 0 && loop->header->Successor(i)->Address() == loop->breakNode->Address() ||
!loop->header->DominatesNode(loop->header->Successor(i)))
{
loopType = Instruction::WHILE;
//loop->breakNode = find_imm_post_dominator(mNodeList, loop->nodes, loop->header->Successor(i==0?1:0));
}
}
}
if (loop->breakNode.use_count() > 0)
msg("break node at %a\n", loop->breakNode->Address());
if (loopType == Instruction::DO_WHILE)
StructureDoWhileLoop(loop);
else
StructureWhileLoop(loop);
}
StructureIfs(mNodeList);
}
void ControlFlowAnalysis::StructureWhileLoop(Loop *loop)
{
msg("while loop\n");
if (loop->nodes.size() == 0)
return;
Instruction_ptr expr;
//expr = loop->nodes.front()->Instructions().back(); //while
Node_ptr first_node = loop->nodes.front();
if (first_node->Type() != Node::CONDITIONAL_JUMP)
{
msg("Error: first loop node not a conditional jump! %a\n", first_node->Address());
return;
}
expr = static_cast<ConditionalJumpNode *>(loop->nodes.front().get())->FindJumpInstruction();
ExpressionNegationHelper negExpr;
//test
Expression_ptr ep(expr->Operand(0));
Expression_ptr jumpExpr(expr->Operand(1));
Addr jumpAddr = static_cast<GlobalVariable *>(jumpExpr.get())->Address();
if (jumpAddr == loop->breakNode->Address()) //negate expression if the jump exits the loop.
negExpr.negateExpression(ep);
While *whileInstruction = new While(loop->header->Address(), ep);
//removed unneeded label instruction from header block.
//if(loop->header->Instructions().front()->Type() == Instruction::InstructionType::LABEL)
// loop->header->Instructions().pop_front();
loop->header->Instructions().insert(
loop->header->Instructions().end(),
Instruction_ptr(whileInstruction)
);
msg("loop header. %s\n", loop->header->TypeString());
loop->nodes.front()->Instructions().remove(expr);
//convert the header node into a fallthrough node.
Node_ptr while_node = Node_ptr(new FallThroughNode(loop->breakNode->Address(), loop->header->Instructions().begin(),
loop->header->Instructions().end()));
while_node->ConnectSuccessor(0, loop->breakNode);
loop->header->MarkForDeletion();
// msg("removing node at %a\n", loop->nodes.back()->Address());
//mNodeList.remove(loop->nodes.back());
//loop->nodes.pop_back(); //get rid of loop expr.
collect_stray_loop_nodes(loop);
Node_list::iterator it = find(mNodeList.begin(), mNodeList.end(), loop->header);
mNodeList.insert(it, while_node);
//helper stub nodes.
Node_ptr loop_break = Node_ptr(new Node(Node::LOOP_BREAK, loop->breakNode->Address()));
Node_ptr loop_continue = Node_ptr(new Node(Node::LOOP_CONTINUE, loop->header->Address()));
for (Node_list::iterator n = loop->nodes.begin();
n != loop->nodes.end();
n++)
{
Node_ptr node = *n;
if (node == loop->header)
{
//doWhileNode->mNodes.push_back(node);
//doWhile->AddLoopNode(node);
continue;
}
Node_ptr replace_node = find_node_by_addr(mNodeList, node->Address());
if (replace_node.use_count() > 0)
{
msg("replacing node of type %d at %a\n", replace_node->Type(), replace_node->Address());
whileInstruction->AddLoopNode(replace_node);
//connect up new break/continue stub nodes. These are used when finding dominators.
for (int i = 0; i < replace_node->SuccessorCount(); i++)
{
Node_ptr s = replace_node->Successor(i);
if (s == loop->header)
{
replace_node->ReconnectSuccessor(loop->header, loop_continue);
}
if (s == loop->breakNode)
{
replace_node->ReconnectSuccessor(loop->breakNode, loop_break);
}
}
mNodeList.remove(replace_node);
}
}
whileInstruction->AddLoopNode(loop_continue);
whileInstruction->AddLoopNode(loop_break);
for (Node_list::iterator n = loop->header->mPreds.begin();
n != loop->header->mPreds.end();
n++)
{
Node_ptr predNode = *n;
Node_list::const_iterator loop_iter = find(loop->nodes.begin(), loop->nodes.end(), predNode);
if (loop_iter == loop->nodes.end()) //only link to nodes that are not part of the loop.
{
predNode->ReconnectSuccessor(loop->header, while_node); //reconnect successors
}
}
if (whileInstruction->Statements().front()->Instructions().empty() == false)
{
//removed unneeded label instruction from first loop block.
if (whileInstruction->Statements().front()->Instructions().front()->Type() == Instruction::LABEL)
whileInstruction->Statements().front()->Instructions().pop_front();
}
//BreakContinueAnalysis bca(whileInstruction->Statements(), loop->breakNode, Node_ptr());
//bca.AnalyzeNodeList();
Node::RemoveDeletedNodes(mNodeList);
FindDominators(mNodeList);
StructureIfs(whileInstruction->Statements());
whileInstruction->Statements().sort(list_sort_helper);
}
void ControlFlowAnalysis::StructureDoWhileLoop(Loop *loop)
{
msg("Do while loop\n");
Instruction_ptr expr;
// Node_list::iterator n = loop->nodes.end();
//
// n--;
// for (; (*n)->Type() != Node::CONDITIONAL_JUMP;)
// {
// msg("dowhile searching for header. skipping %s at %a\n", (*n)->TypeString(), (*n)->Address());
// n--;
// }
// n--; //FIXME big hack! dropping last fall through node.
// msg("dowhile exprNode = %a %s\n", (*n)->Address(), (*n)->TypeString());
// n--;
//expr = (*n)->Instructions().back(); //do while
expr = loop->tail->Instructions().back();
msg("dowhile insn = %a\n", expr->Address());
//test
Expression_ptr ep(expr->Operand(0));
if (loop->tail->Successor(0) != loop->header) //if the jump exits the loop then negate the expression.
{
ExpressionNegationHelper negExpr;
negExpr.negateExpression(ep);
}
DoWhile *doWhileInstruction = new DoWhile(loop->header->Address(), ep);
/*
loop->header->Instructions().insert(
loop->header->Instructions().begin(),
Instruction_ptr(doWhileInstruction)
);
*/
//loop->nodes.back()->Instructions().remove(expr);
//(*n)->Instructions().remove(expr);
//FIXME loop->tail->Instructions().remove(expr);
Instruction_list tmpInstructions;
tmpInstructions.push_back(Instruction_ptr(doWhileInstruction));
//convert the header node into a fallthrough node.
Node_ptr while_node = Node_ptr(
new FallThroughNode(loop->breakNode->Address(), tmpInstructions.begin(), tmpInstructions.end()));
while_node->ConnectSuccessor(0, find_node_by_addr(mNodeList, loop->breakNode->Address()));//loop->breakNode);
//loop->header->MarkForDeletion();
collect_stray_loop_nodes(loop);
Node_list::iterator it = find(mNodeList.begin(), mNodeList.end(), loop->header);
mNodeList.insert(it, while_node);
//helper stub nodes.
Node_ptr loop_break = Node_ptr(new Node(Node::LOOP_BREAK, loop->breakNode->Address()));
Node_ptr loop_continue = Node_ptr(new Node(Node::LOOP_CONTINUE, loop->header->Address()));
for (Node_list::iterator n = loop->nodes.begin();
n != loop->nodes.end();
n++)
{
Node_ptr node = *n;
/*
if(node == loop->header)
{
//doWhileNode->mNodes.push_back(node);
//doWhile->AddLoopNode(node);
continue;
}
*/
Node_ptr replace_node = find_node_by_addr(mNodeList, node->Address());
if (node ==
loop->header) //if this is the case then we have probable selected the while_node which is wrong. We need the original header node.
replace_node = loop->header;
if (replace_node.use_count() > 0)
{
msg("replacing node of type %s at %a\n", replace_node->TypeString(), replace_node->Address());
doWhileInstruction->AddLoopNode(replace_node);
//connect up new break/continue stub nodes. These are used when finding dominators.
for (int i = 0; i < replace_node->SuccessorCount(); i++)
{
Node_ptr s = replace_node->Successor(i);
if (s == loop->header)
{
replace_node->ReconnectSuccessor(loop->header, loop_continue);
}
else if (s == loop->breakNode)
{
replace_node->ReconnectSuccessor(loop->breakNode, loop_break);
}
else
{
Node_list::const_iterator loop_iter = find(loop->nodes.begin(), loop->nodes.end(), s);
if (loop_iter == loop->nodes.end())
{
replace_node->ReconnectSuccessor(s, loop_break);
}
}
}
mNodeList.remove(replace_node);
}
}
doWhileInstruction->AddLoopNode(loop_continue);
doWhileInstruction->AddLoopNode(loop_break);
for (Node_list::iterator n = loop->header->mPreds.begin();
n != loop->header->mPreds.end();
n++)
{
Node_ptr predNode = *n;
Node_list::const_iterator loop_iter = find(loop->nodes.begin(), loop->nodes.end(), predNode);
if (loop_iter == loop->nodes.end()) //only link to nodes that are not part of the loop.
{
predNode->ReconnectSuccessor(loop->header, while_node); //reconnect successors
}
}
doWhileInstruction->Statements().sort(list_sort_helper);
if (doWhileInstruction->Statements().front()->Instructions().empty() == false)
{
//removed unneeded label instruction from first loop block.
if (doWhileInstruction->Statements().front()->Instructions().front()->Type() == Instruction::LABEL)
doWhileInstruction->Statements().front()->Instructions().pop_front();
}
//BreakContinueAnalysis bca(whileInstruction->Statements(), loop->breakNode, Node_ptr());
//bca.AnalyzeNodeList();
Node::RemoveDeletedNodes(mNodeList);
FindDominators(mNodeList);
StructureIfs(doWhileInstruction->Statements());
doWhileInstruction->Statements().sort(list_sort_helper);
}
void ControlFlowAnalysis::StructureBreakContinue(Node_list &blocks, Node_ptr continueNode, Node_ptr breakNode)
{
for (Node_list::iterator n = blocks.begin();
n != blocks.end();
n++)
{
Node_ptr node = *n;
for (Instruction_list::iterator i = node->Instructions().begin();
i != node->Instructions().end();
i++)
{
Instruction_ptr insn = *i;
if (insn->Type() == Instruction::JUMP)
{
// replace stuff here.
Expression_ptr jumpExpr = static_cast<Jump *>(insn.get())->Operand();
Addr jumpAddr = static_cast<GlobalVariable *>(jumpExpr.get())->Address();
if (jumpAddr == breakNode->Address())
{
node->Instructions().insert(i, Instruction_ptr(new Break(insn->Address())));
//node->Instructions().remove(insn);
}
else if (continueNode.use_count() > 0 && jumpAddr == continueNode->Address())
{
node->Instructions().insert(i, Instruction_ptr(new Continue(insn->Address())));
//node->Instructions().remove(insn);
}
}
else if (insn->Type() == Instruction::CONDITIONAL_JUMP)
{
Expression_ptr jumpExpr = static_cast<ConditionalJump *>(insn.get())->Second();
Addr jumpAddr = static_cast<GlobalVariable *>(jumpExpr.get())->Address();
if (jumpAddr == breakNode->Address())
{
node->Instructions().insert(i, Instruction_ptr(new Break(insn->Address())));
//node->Instructions().remove(insn);
}
//replace stuff here.
}
}
}
}
void ControlFlowAnalysis::StructureIfs(Node_list &blocks)
{
int i = 0;
FindDominators(blocks);
do
{
i = 0;
msg("performing if analysis pass..\n");
CollateExpr collateExpr(blocks);
i += collateExpr.Run();
FindDominators(blocks);
for (Node_list::iterator n = blocks.begin();
n != blocks.end();
n++)
{
Node_ptr node = *n;
if (node->Type() == Node::TO_BE_DELETED)
{
msg("skipping deleted node at %a\n", node->Address());
continue;
}
if (StructureIfElse(blocks, node) > 0 || StructureIf(blocks, node) > 0)
{
i++;
break;
}
}
Node::RemoveDeletedNodes(blocks);
FindDominators(blocks);
CollateNode collateNode(blocks);
i += collateNode.Run();
FindDominators(blocks);
//CollateExpr collateExpr(blocks);
//i += collateExpr.Run();
//FindDominators(blocks);
} while (i > 0);
return;
}
int ControlFlowAnalysis::StructureIfElse(Node_list &blocks, Node_ptr node)
{
if (node->Type() == Node::TO_BE_DELETED || node->SuccessorCount() != 2)
return 0;
Node_ptr trueNode = node->Successor(0);
Node_ptr falseNode = node->Successor(1);
if (trueNode->SuccessorCount() != 1 || falseNode->SuccessorCount() != 1 || !node->DominatesNode(trueNode) ||
!node->DominatesNode(falseNode))
return 0;
if (falseNode->Successor(0) != trueNode->Successor(0))
return 0;
if (trueNode->PredecessorCount() != 1 || falseNode->PredecessorCount() != 1)
return 0;
msg("found if/else at %a\n", node->Address());
Instruction_ptr expr = node->Instructions().back(); //FIXME find actual jump instruction
Expression_ptr ep(expr->Operand(0));
trueNode->Cleanup(true); //cleanup all goto and label instructions.
falseNode->Cleanup(true); //cleanup all goto and label instructions.
If *ifInstruction = new If(expr->Address(), ep);
ifInstruction->trueNode = trueNode;
ifInstruction->falseNode = falseNode;
if (trueNode->Instructions().front()->Type() == Instruction::LABEL)
trueNode->Instructions().pop_front();
node->Instructions().insert(
node->Instructions().end(),
Instruction_ptr(ifInstruction)
);
node->Instructions().remove(expr);
Node_ptr new_node = Node_ptr(new FallThroughNode(trueNode->Successor(0)->Address(), node->Instructions().begin(),
node->Instructions().end()));
Node_list::iterator it = find(blocks.begin(), blocks.end(), node);
blocks.insert(it, new_node);
//blocks.remove(trueNode);
//blocks.remove(falseNode);
//blocks.remove(node);
trueNode->MarkForDeletion();
falseNode->MarkForDeletion();
node->MarkForDeletion();
new_node->ConnectSuccessor(0, trueNode->Successor(0));
//fixup preds
Node_ptr followerNode = trueNode->Successor(0);
for (Node_list::iterator n = node->mPreds.begin();
n != node->mPreds.end();
n++)
{
Node_ptr predNode = *n;
predNode->ReconnectSuccessor(node, new_node); //reconnect successors
}
return 1;
}
int ControlFlowAnalysis::StructureIf(Node_list &blocks, Node_ptr node)
{
if (node->Type() == Node::TO_BE_DELETED || node->SuccessorCount() != 2)
return 0;
Node_ptr jumpNode = node->Successor(0);
Node_ptr followerNode = node->Successor(1);
if (node->DominatesNode(jumpNode) && jumpNode->PredecessorCount() == 1 && jumpNode->SuccessorCount() == 1 &&
jumpNode->Successor(0) == followerNode)
{
msg("found if at %a\n", node->Address());
Instruction_ptr expr = node->Instructions().back();
Expression_ptr ep(expr->Operand(0));
jumpNode->Cleanup(true); //cleanup all goto and label instructions.
If *ifInstruction = new If(expr->Address(), ep);
ifInstruction->trueNode = jumpNode;
node->Instructions().insert(
node->Instructions().end(),
Instruction_ptr(ifInstruction)
);
node->Instructions().remove(expr);
Node_ptr new_node = Node_ptr(
new FallThroughNode(followerNode->Address(), node->Instructions().begin(), node->Instructions().end()));
Node_list::iterator it = find(blocks.begin(), blocks.end(), node);
blocks.insert(it, new_node);
//blocks.remove(jumpNode);
//blocks.remove(node);
jumpNode->MarkForDeletion();
node->MarkForDeletion();
new_node->ConnectSuccessor(0, followerNode);
//fixup preds
for (Node_list::iterator n = node->mPreds.begin();
n != node->mPreds.end();
n++)
{
Node_ptr predNode = *n;
predNode->ReconnectSuccessor(node, new_node); //reconnect successors
}
return 1;
}
else if (node->DominatesNode(followerNode) && followerNode->PredecessorCount() == 1 &&
followerNode->SuccessorCount() == 1 && followerNode->Successor(0)->Type() == Node::RETURN)
{
msg("found if reverse return at %a\n", node->Address());
Instruction_ptr expr = node->Instructions().back();
Expression_ptr ep(expr->Operand(0));
ExpressionNegationHelper negExpr;
negExpr.negateExpression(ep);
followerNode->Cleanup(true); //cleanup all goto and label instructions.
//FIXME should be configurable with D:NoReturnValue
//Add Return ax;
followerNode->Instructions().push_back(
Instruction_ptr(new Return(node->Address(), Expression_ptr(new Register(REG_AX)))));
If *ifInstruction = new If(expr->Address(), ep);
ifInstruction->trueNode = followerNode;
node->Instructions().insert(
node->Instructions().end(),
Instruction_ptr(ifInstruction)
);
node->Instructions().remove(expr);
Node_ptr new_node = Node_ptr(
new FallThroughNode(jumpNode->Address(), node->Instructions().begin(), node->Instructions().end()));
Node_list::iterator it = find(blocks.begin(), blocks.end(), node);
blocks.insert(it, new_node);
//blocks.remove(jumpNode);
//blocks.remove(node);
followerNode->MarkForDeletion();
node->MarkForDeletion();