forked from cseagle/blc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
double.cc
2977 lines (2685 loc) · 87.3 KB
/
double.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
*
* 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 "double.hh"
SplitVarnode::SplitVarnode(int4 sz,uintb v)
{ // Construct a double precision constant
val = v;
wholesize = sz;
lo = (Varnode *)0;
hi = (Varnode *)0;
whole = (Varnode *)0;
defpoint = (PcodeOp *)0;
defblock = (BlockBasic *)0;
}
void SplitVarnode::initPartial(int4 sz,uintb v)
{
val = v;
wholesize = sz;
lo = (Varnode *)0;
hi = (Varnode *)0;
whole = (Varnode *)0;
defpoint = (PcodeOp *)0;
defblock = (BlockBasic *)0;
}
void SplitVarnode::initPartial(Varnode *l,Varnode *h)
{ // Construct a virtual double precision varnode
if (h == (Varnode *)0) { // hi is an implied zero
wholesize = l->getSize();
val = l->getOffset(); // Assume l is a constant
lo = (Varnode *)0;
hi = (Varnode *)0;
}
else {
wholesize = l->getSize() + h->getSize();
if (l->isConstant() && h->isConstant()) {
val = h->getOffset();
val <<= (l->getSize()*8);
val |= l->getOffset();
lo = (Varnode *)0;
hi = (Varnode *)0;
}
else {
lo = l;
hi = h;
}
}
whole = (Varnode *)0;
defpoint = (PcodeOp *)0;
defblock = (BlockBasic *)0;
}
void SplitVarnode::initAll(Varnode *w,Varnode *l,Varnode *h)
{ // A double precision varnode, with an existing whole and pieces
wholesize = w->getSize();
lo = l;
hi = h;
whole = w;
defpoint = (PcodeOp *)0;
defblock = (BlockBasic *)0;
}
bool SplitVarnode::inHandHi(Varnode *h)
{ // Initialize the SplitVarnode with the lo and hi, if we know that -h- is a high precision piece
// and we know its companion low precision piece, otherwise return false
if (!h->isPrecisHi()) return false; // Check for mark, in order to have quick -false- in most cases
// Search for the companion
if (h->isWritten()) {
PcodeOp *op = h->getDef();
// We could check for double loads here
if (op->code() == CPUI_SUBPIECE) {
Varnode *w = op->getIn(0);
if (op->getIn(1)->getOffset() != (uintb)(w->getSize()-h->getSize())) return false;
list<PcodeOp *>::const_iterator iter,enditer;
iter = w->beginDescend();
enditer = w->endDescend();
while(iter != enditer) {
PcodeOp *tmpop = *iter;
++iter;
if (tmpop->code() != CPUI_SUBPIECE) continue;
Varnode *tmplo = tmpop->getOut();
if (!tmplo->isPrecisLo()) continue;
if (tmplo->getSize() + h->getSize() != w->getSize()) continue;
if (tmpop->getIn(1)->getOffset() != 0) continue;
// There could conceivably be more than one, but this shouldn't happen with CSE
initAll(w,tmplo,h);
return true;
}
}
}
return false;
}
bool SplitVarnode::inHandLo(Varnode *l)
{ // Initialize the SplitVarnode with the lo and hi, if we know that -l- is a low precision piece
// and we know its companion high precision piece, otherwise return false
if (!l->isPrecisLo()) return false; // Check for mark, in order to have quick -false- in most cases
// Search for the companion
if (l->isWritten()) {
PcodeOp *op = l->getDef();
// We could check for double loads here
if (op->code() == CPUI_SUBPIECE) {
Varnode *w = op->getIn(0);
if (op->getIn(1)->getOffset() != 0) return false;
list<PcodeOp *>::const_iterator iter,enditer;
iter = w->beginDescend();
enditer = w->endDescend();
while(iter != enditer) {
PcodeOp *tmpop = *iter;
++iter;
if (tmpop->code() != CPUI_SUBPIECE) continue;
Varnode *tmphi = tmpop->getOut();
if (!tmphi->isPrecisHi()) continue;
if (tmphi->getSize() + l->getSize() != w->getSize()) continue;
if (tmpop->getIn(1)->getOffset() != (uintb)l->getSize()) continue;
// There could conceivably be more than one, but this shouldn't happen with CSE
initAll(w,l,tmphi);
return true;
}
}
}
return false;
}
bool SplitVarnode::inHandLoNoHi(Varnode *l)
{ // Initialize the SplitVarnode with lo and the whole it came from, if possible return true;
// Fill in the hi if it exists, otherwise leave as null
if (!l->isPrecisLo()) return false;
if (!l->isWritten()) return false;
PcodeOp *op = l->getDef();
if (op->code() != CPUI_SUBPIECE) return false;
if (op->getIn(1)->getOffset() != 0) return false;
Varnode *w = op->getIn(0);
list<PcodeOp *>::const_iterator iter,enditer;
iter = w->beginDescend();
enditer = w->endDescend();
while(iter != enditer) {
PcodeOp *tmpop = *iter;
++iter;
if (tmpop->code() != CPUI_SUBPIECE) continue;
Varnode *tmphi = tmpop->getOut();
if (!tmphi->isPrecisHi()) continue;
if (tmphi->getSize() + l->getSize() != w->getSize()) continue;
if (tmpop->getIn(1)->getOffset() != (uintb)l->getSize()) continue;
// There could conceivably be more than one, but this shouldn't happen with CSE
initAll(w,l,tmphi);
return true;
}
initAll(w,l,(Varnode *)0);
return true;
}
bool SplitVarnode::inHandHiOut(Varnode *h)
{ // Return true (and initialize -this-) if -h- is combined with a -lo- into an existing whole
list<PcodeOp *>::const_iterator iter,enditer;
iter = h->beginDescend();
enditer = h->endDescend();
Varnode *lo = (Varnode *)0;
Varnode *outvn = (Varnode *)0;
while(iter != enditer) {
PcodeOp *pieceop = *iter;
++iter;
if (pieceop->code() != CPUI_PIECE) continue;
if (pieceop->getIn(0) != h) continue;
Varnode *l = pieceop->getIn(1);
if (!l->isPrecisLo()) continue;
if (lo != (Varnode *)0) return false; // Whole is not unique
lo = l;
outvn = pieceop->getOut();
}
if (lo != (Varnode *)0) {
initAll(outvn,lo,h);
return true;
}
return false;
}
bool SplitVarnode::inHandLoOut(Varnode *l)
{ // Return true (and initialize -this-) if -l- is combined with a -hi- into an existing whole
list<PcodeOp *>::const_iterator iter,enditer;
iter = l->beginDescend();
enditer = l->endDescend();
Varnode *hi = (Varnode *)0;
Varnode *outvn = (Varnode *)0;
while(iter != enditer) {
PcodeOp *pieceop = *iter;
++iter;
if (pieceop->code() != CPUI_PIECE) continue;
if (pieceop->getIn(1) != l) continue;
Varnode *h = pieceop->getIn(0);
if (!h->isPrecisHi()) continue;
if (hi != (Varnode *)0) return false; // Whole is not unique
hi = h;
outvn = pieceop->getOut();
}
if (hi != (Varnode *)0) {
initAll(outvn,l,hi);
return true;
}
return false;
}
bool SplitVarnode::findWholeSplitToPieces(void)
{ // Find whole out of which -hi- and -lo- are split, return -true- if it is found
if (whole == (Varnode *)0) {
if (hi != (Varnode *)0) {
if (!hi->isWritten()) return false;
PcodeOp *subhi = hi->getDef();
if (subhi->code() == CPUI_COPY) { // Go thru one level of copy, if the piece is addrtied
Varnode *otherhi = subhi->getIn(0);
if (!otherhi->isWritten()) return false;
subhi = otherhi->getDef();
}
if (subhi->code() != CPUI_SUBPIECE) return false;
Varnode *res = subhi->getIn(0);
if (subhi->getIn(1)->getOffset() != wholesize - hi->getSize()) return false;
whole = res;
}
if (lo != (Varnode *)0) {
if (!lo->isWritten()) return false;
PcodeOp *sublo = lo->getDef();
if (sublo->code() == CPUI_COPY) { // Go thru one level of copy, if the piece is addrtied
Varnode *otherlo = sublo->getIn(0);
if (!otherlo->isWritten()) return false;
sublo = otherlo->getDef();
}
if (sublo->code() != CPUI_SUBPIECE) return false;
Varnode *res = sublo->getIn(0);
if (whole == (Varnode *)0)
whole = res;
else if (whole != res)
return false; // Doesn't match between pieces
if (sublo->getIn(1)->getOffset() != 0) return false;
}
if (whole==(Varnode *)0) return false;
}
if (whole->isWritten()) {
defpoint = whole->getDef();
defblock = defpoint->getParent();
}
else if (whole->isInput()) {
defpoint = (PcodeOp *)0;
defblock = (BlockBasic *)0;
}
return true;
}
bool SplitVarnode::findDefinitionPoint(void)
{ // Set basic block, where both -lo- and -hi- are defined, set the PcodeOp within block if possible
if (lo == (Varnode *)0) return false;
if (hi == (Varnode *)0) return false;
defblock = (BlockBasic *)0;
PcodeOp *lastop;
if (lo->isConstant()&&hi->isConstant()) {
defblock = (BlockBasic *)0;
defpoint = (PcodeOp *)0;
return true;
}
if (hi->isConstant()) return false; // If one but not both is constant
if (lo->isConstant()) return false;
if (hi->isWritten()) {
if (!lo->isWritten()) return false;
lastop = hi->getDef();
defblock = lastop->getParent();
PcodeOp *lastop2 = lo->getDef();
BlockBasic *otherblock = lastop2->getParent();
if (defblock != otherblock) {
defpoint = lastop;
FlowBlock *curbl = defblock;
while(curbl != (FlowBlock *)0) { // Make sure defblock dominated by otherblock
curbl = curbl->getImmedDom();
if (curbl == otherblock) return true;
}
defblock = otherblock; // Try lo as final defining location
otherblock = lastop->getParent();
defpoint = lastop2;
curbl = defblock;
while(curbl != (FlowBlock *)0) {
curbl = curbl->getImmedDom();
if (curbl == otherblock) return true;
}
defblock = (BlockBasic *)0;
return false; // Not defined in same basic block
}
if (lastop2->getSeqNum().getOrder() > lastop->getSeqNum().getOrder())
lastop = lastop2;
defpoint = lastop;
}
else if (hi->isInput()) {
if (!lo->isInput()) {
defblock = (BlockBasic *)0;
return false;
}
defblock = (BlockBasic *)0;
defpoint = (PcodeOp *)0;
}
return true;
}
PcodeOp *SplitVarnode::findEarliestSplitPoint(void)
{ // Find the earliest definition point of the lo and hi pieces
if (!hi->isWritten()) return (PcodeOp *)0;
if (!lo->isWritten()) return (PcodeOp *)0;
PcodeOp *hiop = hi->getDef();
PcodeOp *loop = lo->getDef();
if (loop->getParent() != hiop->getParent())
return (PcodeOp *)0;
return (loop->getSeqNum().getOrder() < hiop->getSeqNum().getOrder()) ? loop : hiop;
}
bool SplitVarnode::findWholeBuiltFromPieces(void)
{
// We want to scan here for concatenations formed out of hi <-> lo, in order to avoid
// duplicate subexpressions. But we need to be careful that the concatenation isn't created
// AFTER we need it. We assume hi and lo are defined in the same basic block (or both inputs)
if (hi==(Varnode *)0) return false;
if (lo==(Varnode *)0) return false;
list<PcodeOp *>::const_iterator iter,enditer;
iter = lo->beginDescend();
enditer = lo->endDescend();
PcodeOp *res = (PcodeOp *)0;
BlockBasic *bb;
if (lo->isWritten())
bb = lo->getDef()->getParent();
else if (lo->isInput())
bb = (BlockBasic *)0;
else
throw LowlevelError("Trying to find whole on free varnode");
while(iter != enditer) {
PcodeOp *op = *iter;
++iter;
if (op->code() != CPUI_PIECE) continue;
if (op->getIn(0) != hi) continue;
if (bb != (BlockBasic *)0) {
if (op->getParent() != bb) continue; // Not defined in earliest block
}
else if (!op->getParent()->isEntryPoint())
continue;
if (res == (PcodeOp *)0)
res = op;
else {
if (op->getSeqNum().getOrder() < res->getSeqNum().getOrder()) // Find "earliest" whole
res = op;
}
}
if (res == (PcodeOp *)0)
whole = (Varnode *)0;
else {
defpoint = res;
defblock = defpoint->getParent();
whole = res->getOut();
}
return (whole!=(Varnode *)0);
}
bool SplitVarnode::isWholeFeasible(PcodeOp *existop)
{ // Does there exist, or can we construct a whole out of this split varnode that will be defined
// before existop
if (isConstant()) return true;
if ((lo!=(Varnode *)0)&&(hi!=(Varnode *)0))
if (lo->isConstant() != hi->isConstant()) return false; // Mixed constant/non-constant
if (!findWholeSplitToPieces()) {
if (!findWholeBuiltFromPieces()) {
if (!findDefinitionPoint())
return false;
}
}
if (defblock == (BlockBasic *)0) return true;
FlowBlock *curbl = existop->getParent();
if (curbl == defblock) // If defined in same block as -existop- check PcodeOp ordering
return (defpoint->getSeqNum().getOrder() <= existop->getSeqNum().getOrder());
while(curbl != (FlowBlock *)0) { // Make sure defbock dominates block containing -existop-
curbl = curbl->getImmedDom();
if (curbl == defblock) return true;
}
return false;
}
bool SplitVarnode::isWholePhiFeasible(FlowBlock *bl)
{ // This is the same as isWholeFeasible, but for MULTIEQUAL constructions where we have construct
// a whole thats available for a particular branch
if (isConstant()) return false;
if (!findWholeSplitToPieces()) {
if (!findWholeBuiltFromPieces()) {
if (!findDefinitionPoint())
return false;
}
}
if (defblock == (BlockBasic *)0) return true;
if (bl == defblock) // If defined in same block
return true;
while(bl != (FlowBlock *)0) { // Make sure defblock dominates block containing -existop-
bl = bl->getImmedDom();
if (bl == defblock) return true;
}
return false;
}
void SplitVarnode::findCreateWhole(Funcdata &data)
{ // Find or create a whole varnode, we assume isWholeFeasible has returned true
if (isConstant()) {
whole = data.newConstant(wholesize,val);
return;
}
else {
if (lo != (Varnode *)0)
lo->setPrecisLo(); // Mark the pieces
if (hi != (Varnode *)0)
hi->setPrecisHi();
}
if (whole != (Varnode *)0) return; // Already found the whole
PcodeOp *concatop;
Address addr;
BlockBasic *topblock = (BlockBasic *)0;
if (defblock != (BlockBasic *)0)
addr = defpoint->getAddr();
else {
topblock = (BlockBasic *)data.getBasicBlocks().getStartBlock();
addr = topblock->getStart();
}
concatop = data.newOp(2,addr);
// Do we need to pick something other than a unique????
whole = data.newUniqueOut(wholesize,concatop);
data.opSetOpcode(concatop,CPUI_PIECE);
data.opSetOutput(concatop,whole);
data.opSetInput(concatop,hi,0);
data.opSetInput(concatop,lo,1);
if (defblock != (BlockBasic *)0)
data.opInsertAfter(concatop,defpoint);
else
data.opInsertBegin(concatop,topblock);
defpoint = concatop;
defblock = concatop->getParent();
}
void SplitVarnode::findCreateOutputWhole(Funcdata &data)
{ // Create the actual -whole- varnode
lo->setPrecisLo(); // Mark the pieces
hi->setPrecisHi();
if (whole != (Varnode *)0) return;
whole = data.newUnique(wholesize);
}
void SplitVarnode::createJoinedWhole(Funcdata &data)
{ // Create a whole from pieces, respecting the piece addresses
// if the pieces can be treated as a contiguous whole, use that address
// otherwise construct a join address
lo->setPrecisLo();
hi->setPrecisHi();
if (whole != (Varnode *)0) return;
Address newaddr;
if (!isAddrTiedContiguous(lo,hi,newaddr)) {
newaddr = data.getArch()->constructJoinAddress(data.getArch()->translate,hi->getAddr(),hi->getSize(),
lo->getAddr(),lo->getSize());
}
whole = data.newVarnode(wholesize,newaddr);
}
void SplitVarnode::buildLoFromWhole(Funcdata &data)
{ // Assume -lo- was defined in some other way and now needs to be defined as a split from
// a new -whole- varnode
PcodeOp *loop = lo->getDef();
if (loop == (PcodeOp *)0)
throw LowlevelError("Building low piece that was originally undefined");
vector<Varnode *> inlist;
inlist.push_back(whole);
inlist.push_back(data.newConstant(4,0));
if (loop->code() == CPUI_MULTIEQUAL) {
// When converting the MULTIEQUAL to a SUBPIECE, we need to reinsert the op so that we don't
// get a break in the sequence of MULTIEQUALs at the beginning of the block
BlockBasic *bl = loop->getParent();
data.opUninsert(loop);
data.opSetOpcode(loop,CPUI_SUBPIECE);
data.opSetAllInput(loop,inlist);
data.opInsertBegin(loop,bl);
}
else if (loop->code() == CPUI_INDIRECT) {
// When converting an INDIRECT to a SUBPIECE, we need to reinsert the op AFTER the affector
PcodeOp *affector = PcodeOp::getOpFromConst(loop->getIn(1)->getAddr());
data.opUninsert(loop);
data.opSetOpcode(loop,CPUI_SUBPIECE);
data.opSetAllInput(loop,inlist);
data.opInsertAfter(loop,affector);
}
else {
data.opSetOpcode(loop,CPUI_SUBPIECE);
data.opSetAllInput(loop,inlist);
}
}
void SplitVarnode::buildHiFromWhole(Funcdata &data)
{ // Assume -hi- was defined in some other way and now needs to be defined as a split from
// a new -whole- varnode
PcodeOp *hiop = hi->getDef();
if (hiop == (PcodeOp *)0)
throw LowlevelError("Building low piece that was originally undefined");
vector<Varnode *> inlist;
inlist.push_back(whole);
inlist.push_back(data.newConstant(4,lo->getSize()));
if (hiop->code() == CPUI_MULTIEQUAL) {
// When converting the MULTIEQUAL to a SUBPIECE, we need to reinsert the op so that we don't
// get a break in the sequence of MULTIEQUALs at the beginning of the block
BlockBasic *bl = hiop->getParent();
data.opUninsert(hiop);
data.opSetOpcode(hiop,CPUI_SUBPIECE);
data.opSetAllInput(hiop,inlist);
data.opInsertBegin(hiop,bl);
}
else if (hiop->code() == CPUI_INDIRECT) {
// When converting the INDIRECT to a SUBPIECE, we need to reinsert AFTER the affector
PcodeOp *affector = PcodeOp::getOpFromConst(hiop->getIn(1)->getAddr());
data.opUninsert(hiop);
data.opSetOpcode(hiop,CPUI_SUBPIECE);
data.opSetAllInput(hiop,inlist);
data.opInsertAfter(hiop,affector);
}
else {
data.opSetOpcode(hiop,CPUI_SUBPIECE);
data.opSetAllInput(hiop,inlist);
}
}
// void SplitVarnode::buildHiFromLoHalf(Funcdata &data,SplitVarnode &oldin,PcodeOp *newwholeop)
// { // Only the lo half of the new logical whole is explicitly constructed, the old input high
// // is recycled for the hi half of the output, define the new hi half as a SUBPIECE and scan
// // through the uses of the old hi half to see which should be switch to the new hi half
// PcodeOp *newhiop = data.newOp(2,newwholeop->getAddr());
// data.opSetOpcode(newhiop,CPUI_SUBPIECE);
// data.opSetOutput(newhiop,hi); // hi was not defined previously
// data.opSetInput(newhiop,whole,0);
// data.opSetInput(newhiop,data.newConstant(4,lo->getSize()),1);
// data.opInsertAfter(newhiop,newwholeop);
// Varnode *oldhi = oldin.getHi();
// list<PcodeOp *>::const_iterator iter,enditer;
// iter = oldhi->beginDescend();
// enditer = oldhi->endDescend();
// while(iter != enditer) {
// PcodeOp *testop = *iter;
// ++iter;
// int4 ord = testop->compareOrder(newhiop);
// if (ord == 1) { // newhiop executes earlier than testop
// int4 slot = testop->getSlot(oldhi);
// data.opSetInput(testop,hi,slot);
// }
// }
// }
PcodeOp *SplitVarnode::findOutExist(void)
{ // Find the point at which the whole must exist
if (findWholeBuiltFromPieces()) {
return defpoint;
}
return findEarliestSplitPoint();
}
bool SplitVarnode::adjacentOffsets(Varnode *vn1,Varnode *vn2,uintb size1)
{ // Return true, if the (possibly dynamic) offset represented
// by vn1 plus size1 produces the offset in vn2
if (vn1->isConstant()) {
if (!vn2->isConstant()) return false;
return ((vn1->getOffset() + size1) == vn2->getOffset());
}
if (!vn2->isWritten()) return false;
PcodeOp *op2 = vn2->getDef();
if (op2->code() != CPUI_INT_ADD) return false;
if (!op2->getIn(1)->isConstant()) return false;
uintb c2 = op2->getIn(1)->getOffset();
if (op2->getIn(0) == vn1)
return (size1 == c2);
if (!vn1->isWritten()) return false;
PcodeOp *op1 = vn1->getDef();
if (op1->code() != CPUI_INT_ADD) return false;
if (!op1->getIn(1)->isConstant()) return false;
uintb c1 = op1->getIn(1)->getOffset();
if (op1->getIn(0) != op2->getIn(0)) return false;
return ((c1 + size1) == c2);
}
bool SplitVarnode::testContiguousLoad(PcodeOp *most,PcodeOp *least,bool allowfree,PcodeOp *&first,PcodeOp *&second,AddrSpace *&spc,int4 &sizeres)
{ // Determine if -most- and -least- are contiguous loads, given that the outputs form the most and least significant
// parts of a double precision result, sort the ops into -first- address and -second- address
// return 'true' and the -spc- loaded from and the combined load size
if (least->code() != CPUI_LOAD) return false;
if (most->code() != CPUI_LOAD) return false;
spc = Address::getSpaceFromConst(least->getIn(0)->getAddr());
if (Address::getSpaceFromConst(most->getIn(0)->getAddr()) != spc) return false;
if (spc->isBigEndian()) { // Convert significance order to address order
first = most;
second = least;
}
else {
first = least;
second = most;
}
Varnode *firstptr = first->getIn(1);
if (!allowfree)
if (firstptr->isFree()) return false;
sizeres = first->getOut()->getSize(); // # of bytes read by lowest address load
// Check if the loads are adjacent to each other
if (!adjacentOffsets(first->getIn(1),second->getIn(1),(uintb)sizeres))
return false;
sizeres += second->getOut()->getSize();
return true;
}
bool SplitVarnode::isAddrTiedContiguous(Varnode *lo,Varnode *hi,Address &res)
{ // Return true if the pieces -lo- and -hi- can be melded into a contiguous storage location
if (!lo->isAddrTied()) return false;
if (!hi->isAddrTied()) return false;
// Make sure there is no explicit symbol that would prevent the pieces from being joined
SymbolEntry *entry = lo->getSymbolEntry();
if ((entry != (SymbolEntry *)0)&&(entry->getOffset()==0)) return false;
entry = hi->getSymbolEntry();
if ((entry != (SymbolEntry *)0)&&(entry->getOffset()==0)) return false;
AddrSpace *spc = lo->getSpace();
if (spc != hi->getSpace()) return false;
uintb looffset = lo->getOffset();
uintb hioffset = hi->getOffset();
if (spc->isBigEndian()) {
if (hioffset >= looffset) return false;
if (hioffset + hi->getSize() != looffset) return false;
res = hi->getAddr();
}
else {
if (looffset >= hioffset) return false;
if (looffset + lo->getSize() != hioffset) return false;
res = lo->getAddr();
}
return true;
}
void SplitVarnode::wholeList(Varnode *w,vector<SplitVarnode> &splitvec)
{ // Create a list of all the possible pairs that contain the same logical value as -w-
SplitVarnode basic;
basic.whole = w;
basic.hi = (Varnode *)0;
basic.lo = (Varnode *)0;
basic.wholesize = w->getSize();
list<PcodeOp *>::const_iterator iter,enditer;
iter = basic.whole->beginDescend();
enditer = basic.whole->endDescend();
int4 res = 0;
while(iter != enditer) {
PcodeOp *subop = *iter;
++iter;
if (subop->code() != CPUI_SUBPIECE) continue;
Varnode *vn = subop->getOut();
if (vn->isPrecisHi()) {
if (subop->getIn(1)->getOffset() != basic.wholesize - vn->getSize()) continue;
basic.hi = vn;
res |= 2;
}
else if (vn->isPrecisLo()) {
if (subop->getIn(1)->getOffset() != 0) continue;
basic.lo = vn;
res |= 1;
}
}
if (res==0) return;
splitvec.push_back(basic);
findCopies(basic,splitvec);
}
void SplitVarnode::findCopies(const SplitVarnode &in,vector<SplitVarnode> &splitvec)
{ // Find copies from -in- pieces into another logical pair
if (!in.hasBothPieces()) return;
list<PcodeOp *>::const_iterator iter,enditer;
iter = in.getLo()->beginDescend();
enditer = in.getLo()->endDescend();
while(iter != enditer) {
PcodeOp *loop = *iter;
++iter;
if (loop->code() != CPUI_COPY) continue;
Varnode *locpy = loop->getOut();
Address addr = locpy->getAddr(); // Calculate address of hi part
if (addr.isBigEndian())
addr = addr - (in.getHi()->getSize());
else
addr = addr + locpy->getSize();
list<PcodeOp *>::const_iterator iter2,enditer2;
iter2 = in.getHi()->beginDescend();
enditer2 = in.getHi()->endDescend();
while(iter2 != enditer2) {
PcodeOp *hiop = *iter2;
++iter2;
if (hiop->code() != CPUI_COPY) continue;
Varnode *hicpy = hiop->getOut();
if (hicpy->getAddr() != addr) continue;
if (hiop->getParent() != loop->getParent()) continue;
SplitVarnode newsplit;
newsplit.initAll(in.getWhole(),locpy,hicpy);
splitvec.push_back(newsplit);
}
}
}
void SplitVarnode::getTrueFalse(PcodeOp *boolop,bool flip,BlockBasic *&trueout,BlockBasic *&falseout)
{
BlockBasic *parent = boolop->getParent();
BlockBasic *trueblock = (BlockBasic *)parent->getTrueOut();
BlockBasic *falseblock =(BlockBasic *)parent->getFalseOut();
if (boolop->isBooleanFlip() != flip) {
trueout = falseblock;
falseout = trueblock;
}
else {
trueout = trueblock;
falseout = falseblock;
}
}
bool SplitVarnode::otherwiseEmpty(PcodeOp *branchop)
{ // Return true if this block is only used to perform the branch specified by -branchop-
BlockBasic *bl = branchop->getParent();
if (bl->sizeIn() != 1) return false;
PcodeOp *otherop = (PcodeOp *)0;
Varnode *vn = branchop->getIn(1);
if (vn->isWritten())
otherop = vn->getDef();
list<PcodeOp *>::const_iterator iter,enditer;
iter = bl->beginOp();
enditer = bl->endOp();
while(iter != enditer) {
PcodeOp *op = *iter;
++iter;
if (op == otherop) continue;
if (op == branchop) continue;
return false;
}
return true;
}
bool SplitVarnode::verifyMultNegOne(PcodeOp *op)
{
if (op->code() != CPUI_INT_MULT) return false;
Varnode *in1 = op->getIn(1);
if (!in1->isConstant()) return false;
if (in1->getOffset() != calc_mask(in1->getSize())) return false;
return true;
}
PcodeOp *SplitVarnode::prepareBinaryOp(SplitVarnode &out,SplitVarnode &in1,SplitVarnode &in2)
{
PcodeOp *existop = out.findOutExist(); // Find point where output whole needs to exist
if (existop == (PcodeOp *)0) return existop; // If we can find no such point return false;
if (!in1.isWholeFeasible(existop)) return (PcodeOp *)0;
if (!in2.isWholeFeasible(existop)) return (PcodeOp *)0;
return existop;
}
void SplitVarnode::createBinaryOp(Funcdata &data,SplitVarnode &out,SplitVarnode &in1,SplitVarnode &in2,
PcodeOp *existop,OpCode opc)
{ // Rewrite the double precision operation (if possible) by replacing the pieces with unified varnodes
// Make sure all the actual whole varnodes exist
out.findCreateOutputWhole(data);
in1.findCreateWhole(data);
in2.findCreateWhole(data);
if (existop->code() != CPUI_PIECE) { // If the output whole didn't previously exist
PcodeOp *newop = data.newOp(2,existop->getAddr()); // new op which creates the output whole
data.opSetOpcode(newop,opc);
data.opSetOutput(newop,out.getWhole());
data.opSetInput(newop,in1.getWhole(),0);
data.opSetInput(newop,in2.getWhole(),1);
data.opInsertBefore(newop,existop);
out.buildLoFromWhole(data);
out.buildHiFromWhole(data);
}
else { // The whole previously existed
data.opSetOpcode(existop,opc); // our new op replaces the op previously defining the output whole
data.opSetInput(existop,in1.getWhole(),0);
data.opSetInput(existop,in2.getWhole(),1);
}
}
PcodeOp *SplitVarnode::prepareShiftOp(SplitVarnode &out,SplitVarnode &in)
{
PcodeOp *existop = out.findOutExist(); // Find point where output whole needs to exist
if (existop == (PcodeOp *)0) return existop;
if (!in.isWholeFeasible(existop)) return (PcodeOp *)0;
return existop;
}
void SplitVarnode::createShiftOp(Funcdata &data,SplitVarnode &out,SplitVarnode &in,Varnode *sa,
PcodeOp *existop,OpCode opc)
{ // Rewrite the double precision shift by replacing hi/lo pieces with unified varnodes
out.findCreateOutputWhole(data);
in.findCreateWhole(data);
if (sa->isConstant())
sa = data.newConstant(sa->getSize(),sa->getOffset());
if (existop->code() != CPUI_PIECE) { // If the output whole didn't previously exist
PcodeOp *newop = data.newOp(2,existop->getAddr());
data.opSetOpcode(newop,opc);
data.opSetOutput(newop,out.getWhole());
data.opSetInput(newop,in.getWhole(),0);
data.opSetInput(newop,sa,1);
data.opInsertBefore(newop,existop);
out.buildLoFromWhole(data);
out.buildHiFromWhole(data);
}
else { // The whole previously existed, we remake the defining op
data.opSetOpcode(existop,opc);
data.opSetInput(existop,in.getWhole(),0);
data.opSetInput(existop,sa,1);
}
}
int4 SplitVarnode::applyRuleIn(SplitVarnode &in,Funcdata &data)
{ // Try to perform one transform on a logical double precision operation that uses -in- as input
for(int4 i=0;i<2;++i) {
Varnode *vn;
vn = (i==0) ? in.getHi() : in.getLo();
if (vn == (Varnode *)0) continue;
bool workishi = (i==0);
list<PcodeOp *>::const_iterator iter,enditer;
iter = vn->beginDescend();
enditer = vn->endDescend();
while(iter != enditer) {
PcodeOp *workop = *iter;
++iter;
switch(workop->code()) {
case CPUI_INT_ADD:
{
AddForm addform;
if (addform.applyRule(in,workop,workishi,data))
return 1;
SubForm subform;
if (subform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_AND:
{
Equal3Form equal3form;
if (equal3form.applyRule(in,workop,workishi,data))
return 1;
LogicalForm logicalform;
if (logicalform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_OR:
{
Equal2Form equal2form;
if (equal2form.applyRule(in,workop,workishi,data))
return 1;
LogicalForm logicalform;
if (logicalform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_XOR:
{
Equal2Form equal2form;
if (equal2form.applyRule(in,workop,workishi,data))
return 1;
LogicalForm logicalform;
if (logicalform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_EQUAL:
case CPUI_INT_NOTEQUAL:
{
LessThreeWay lessthreeway;
if (lessthreeway.applyRule(in,workop,workishi,data))
return 1;
Equal1Form equal1form;
if (equal1form.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_LESS:
case CPUI_INT_LESSEQUAL:
{
LessThreeWay lessthreeway;
if (lessthreeway.applyRule(in,workop,workishi,data))
return 1;
LessConstForm lessconstform;
if (lessconstform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_SLESS:
{
LessConstForm lessconstform;
if (lessconstform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_SLESSEQUAL:
{
LessConstForm lessconstform;
if (lessconstform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_LEFT:
{
ShiftForm shiftform;
if (shiftform.applyRuleLeft(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_RIGHT:
{
ShiftForm shiftform;
if (shiftform.applyRuleRight(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_SRIGHT:
{
ShiftForm shiftform;
if (shiftform.applyRuleRight(in,workop,workishi,data))
return 1;
}
break;
case CPUI_INT_MULT:
{
MultForm multform;
if (multform.applyRule(in,workop,workishi,data))
return 1;
}
break;
case CPUI_MULTIEQUAL:
{
PhiForm phiform;
if (phiform.applyRule(in,workop,workishi,data))