forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlevelilinstruction.h
1455 lines (1361 loc) · 49.2 KB
/
highlevelilinstruction.h
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) 2019 Vector 35 Inc
//
// 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.
#pragma once
#include <functional>
#include <unordered_map>
#include <vector>
#ifdef BINARYNINJACORE_LIBRARY
#include "variable.h"
#else
#include "binaryninjaapi.h"
#endif
#include "mediumlevelilinstruction.h"
#include <fmt/core.h>
#ifdef BINARYNINJACORE_LIBRARY
namespace BinaryNinjaCore
#else
namespace BinaryNinja
#endif
{
class HighLevelILFunction;
/*!
\ingroup highlevelil
*/
template <BNHighLevelILOperation N>
struct HighLevelILInstructionAccessor
{};
struct HighLevelILInstruction;
struct HighLevelILConstantInstruction;
struct HighLevelILConstantDataInstruction;
struct HighLevelILOneOperandInstruction;
struct HighLevelILTwoOperandInstruction;
struct HighLevelILTwoOperandWithCarryInstruction;
struct HighLevelILDoublePrecisionInstruction;
struct MediumLevelILInstruction;
class HighLevelILOperand;
class HighLevelILOperandList;
/*!
\ingroup highlevelil
*/
enum HighLevelILOperandType
{
IntegerHighLevelOperand,
ConstantDataHighLevelOperand,
IndexHighLevelOperand,
IntrinsicHighLevelOperand,
ExprHighLevelOperand,
VariableHighLevelOperand,
SSAVariableHighLevelOperand,
ExprListHighLevelOperand,
SSAVariableListHighLevelOperand,
IndexListHighLevelOperand
};
/*!
\ingroup highlevelil
*/
enum HighLevelILOperandUsage
{
SourceExprHighLevelOperandUsage,
VariableHighLevelOperandUsage,
DestVariableHighLevelOperandUsage,
SSAVariableHighLevelOperandUsage,
DestSSAVariableHighLevelOperandUsage,
DestExprHighLevelOperandUsage,
LeftExprHighLevelOperandUsage,
RightExprHighLevelOperandUsage,
CarryExprHighLevelOperandUsage,
IndexExprHighLevelOperandUsage,
ConditionExprHighLevelOperandUsage,
ConditionPhiExprHighLevelOperandUsage,
TrueExprHighLevelOperandUsage,
FalseExprHighLevelOperandUsage,
LoopExprHighLevelOperandUsage,
InitExprHighLevelOperandUsage,
UpdateExprHighLevelOperandUsage,
DefaultExprHighLevelOperandUsage,
HighExprHighLevelOperandUsage,
LowExprHighLevelOperandUsage,
OffsetHighLevelOperandUsage,
MemberIndexHighLevelOperandUsage,
ConstantHighLevelOperandUsage,
ConstantDataHighLevelOperandUsage,
VectorHighLevelOperandUsage,
IntrinsicHighLevelOperandUsage,
TargetHighLevelOperandUsage,
ParameterExprsHighLevelOperandUsage,
SourceExprsHighLevelOperandUsage,
DestExprsHighLevelOperandUsage,
BlockExprsHighLevelOperandUsage,
CasesHighLevelOperandUsage,
ValueExprsHighLevelOperandUsage,
SourceSSAVariablesHighLevelOperandUsage,
SourceMemoryVersionHighLevelOperandUsage,
SourceMemoryVersionsHighLevelOperandUsage,
DestMemoryVersionHighLevelOperandUsage
};
} // namespace BinaryNinjaCore
namespace std {
template <>
struct hash<BNHighLevelILOperation>
{
typedef BNHighLevelILOperation argument_type;
typedef int result_type;
result_type operator()(argument_type const& value) const { return (result_type)value; }
};
#ifdef BINARYNINJACORE_LIBRARY
template <>
struct hash<BinaryNinjaCore::HighLevelILOperandUsage>
#else
template <>
struct hash<BinaryNinja::HighLevelILOperandUsage>
#endif
{
#ifdef BINARYNINJACORE_LIBRARY
typedef BinaryNinjaCore::HighLevelILOperandUsage argument_type;
#else
typedef BinaryNinja::HighLevelILOperandUsage argument_type;
#endif
typedef int result_type;
result_type operator()(argument_type const& value) const { return (result_type)value; }
};
} // namespace std
#ifdef BINARYNINJACORE_LIBRARY
namespace BinaryNinjaCore
#else
namespace BinaryNinja
#endif
{
#ifdef BINARYNINJACORE_LIBRARY
#define _STD_VECTOR vector
#define _STD_SET set
#define _STD_STACK stack
#define _STD_UNORDERED_MAP unordered_map
#else
#define _STD_VECTOR std::vector
#define _STD_SET std::set
#define _STD_STACK std::stack
#define _STD_UNORDERED_MAP std::unordered_map
#endif
/*!
\ingroup highlevelil
*/
class HighLevelILInstructionAccessException : public ExceptionWithStackTrace
{
public:
HighLevelILInstructionAccessException() : ExceptionWithStackTrace("invalid access to HLIL instruction") {}
};
/*!
\ingroup highlevelil
*/
class HighLevelILIntegerList
{
struct ListIterator
{
#ifdef BINARYNINJACORE_LIBRARY
HighLevelILFunction* function;
#else
Ref<HighLevelILFunction> function;
#endif
BNHighLevelILInstruction instr;
size_t operand, count;
bool operator==(const ListIterator& a) const;
bool operator!=(const ListIterator& a) const;
bool operator<(const ListIterator& a) const;
ListIterator& operator++();
uint64_t operator*();
HighLevelILFunction* GetFunction() const { return function; }
};
ListIterator m_start;
public:
typedef ListIterator const_iterator;
HighLevelILIntegerList(HighLevelILFunction* func, const BNHighLevelILInstruction& instr, size_t count);
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
uint64_t operator[](size_t i) const;
operator _STD_VECTOR<uint64_t>() const;
};
/*!
\ingroup highlevelil
*/
class HighLevelILIndexList
{
struct ListIterator
{
HighLevelILIntegerList::const_iterator pos;
bool operator==(const ListIterator& a) const { return pos == a.pos; }
bool operator!=(const ListIterator& a) const { return pos != a.pos; }
bool operator<(const ListIterator& a) const { return pos < a.pos; }
ListIterator& operator++()
{
++pos;
return *this;
}
size_t operator*();
};
HighLevelILIntegerList m_list;
public:
typedef ListIterator const_iterator;
HighLevelILIndexList(HighLevelILFunction* func, const BNHighLevelILInstruction& instr, size_t count);
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
size_t operator[](size_t i) const;
operator _STD_VECTOR<size_t>() const;
};
/*!
\ingroup highlevelil
*/
class HighLevelILInstructionList
{
struct ListIterator
{
HighLevelILIntegerList::const_iterator pos;
bool ast;
size_t instructionIndex;
bool operator==(const ListIterator& a) const { return pos == a.pos; }
bool operator!=(const ListIterator& a) const { return pos != a.pos; }
bool operator<(const ListIterator& a) const { return pos < a.pos; }
ListIterator& operator++()
{
++pos;
return *this;
}
const HighLevelILInstruction operator*();
};
HighLevelILIntegerList m_list;
bool m_ast;
size_t m_instructionIndex;
public:
typedef ListIterator const_iterator;
HighLevelILInstructionList(HighLevelILFunction* func, const BNHighLevelILInstruction& instr, size_t count,
bool asFullAst, size_t instructionIndex);
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
const HighLevelILInstruction operator[](size_t i) const;
operator _STD_VECTOR<HighLevelILInstruction>() const;
};
/*!
\ingroup highlevelil
*/
class HighLevelILSSAVariableList
{
struct ListIterator
{
HighLevelILIntegerList::const_iterator pos;
bool operator==(const ListIterator& a) const { return pos == a.pos; }
bool operator!=(const ListIterator& a) const { return pos != a.pos; }
bool operator<(const ListIterator& a) const { return pos < a.pos; }
ListIterator& operator++()
{
++pos;
++pos;
return *this;
}
const SSAVariable operator*();
};
HighLevelILIntegerList m_list;
public:
typedef ListIterator const_iterator;
HighLevelILSSAVariableList(HighLevelILFunction* func, const BNHighLevelILInstruction& instr, size_t count);
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
const SSAVariable operator[](size_t i) const;
operator _STD_VECTOR<SSAVariable>() const;
};
/*!
\ingroup highlevelil
*/
struct HighLevelILInstructionBase : public BNHighLevelILInstruction
{
#ifdef BINARYNINJACORE_LIBRARY
HighLevelILFunction* function = nullptr;
#else
Ref<HighLevelILFunction> function = nullptr;
#endif
size_t exprIndex, instructionIndex;
bool ast;
static _STD_UNORDERED_MAP<HighLevelILOperandUsage, HighLevelILOperandType> operandTypeForUsage;
static _STD_UNORDERED_MAP<BNHighLevelILOperation, _STD_VECTOR<HighLevelILOperandUsage>> operationOperandUsage;
static _STD_UNORDERED_MAP<BNHighLevelILOperation, _STD_UNORDERED_MAP<HighLevelILOperandUsage, size_t>>
operationOperandIndex;
HighLevelILOperandList GetOperands() const;
uint64_t GetRawOperandAsInteger(size_t operand) const;
ConstantData GetRawOperandAsConstantData(size_t operand) const;
size_t GetRawOperandAsIndex(size_t operand) const;
HighLevelILInstruction GetRawOperandAsExpr(size_t operand) const;
Variable GetRawOperandAsVariable(size_t operand) const;
SSAVariable GetRawOperandAsSSAVariable(size_t operand) const;
HighLevelILInstructionList GetRawOperandAsExprList(size_t operand) const;
HighLevelILSSAVariableList GetRawOperandAsSSAVariableList(size_t operand) const;
HighLevelILIndexList GetRawOperandAsIndexList(size_t operand) const;
void UpdateRawOperand(size_t operandIndex, ExprId value);
void UpdateRawOperandAsInteger(size_t operandIndex, uint64_t value);
void UpdateRawOperandAsSSAVariableList(size_t operandIndex, const _STD_VECTOR<SSAVariable>& vars);
void UpdateRawOperandAsExprList(size_t operandIndex, const _STD_VECTOR<HighLevelILInstruction>& exprs);
void UpdateRawOperandAsExprList(size_t operandIndex, const _STD_VECTOR<size_t>& exprs);
RegisterValue GetValue() const;
PossibleValueSet GetPossibleValues(
const _STD_SET<BNDataFlowQueryOption>& options = _STD_SET<BNDataFlowQueryOption>()) const;
Confidence<Ref<Type>> GetType() const;
size_t GetSSAExprIndex() const;
size_t GetNonSSAExprIndex() const;
HighLevelILInstruction GetSSAForm() const;
HighLevelILInstruction GetNonSSAForm() const;
size_t GetMediumLevelILExprIndex() const;
bool HasMediumLevelIL() const;
MediumLevelILInstruction GetMediumLevelIL() const;
MediumLevelILInstruction GetMediumLevelILSSAForm() const;
// Return (and leak) a string describing the instruction for debugger use
char* Dump() const;
void Replace(ExprId expr);
void SetAttributes(uint32_t attributes);
void SetAttribute(BNILInstructionAttribute attribute, bool state = true);
void ClearAttribute(BNILInstructionAttribute attribute);
size_t GetInstructionIndex() const;
HighLevelILInstruction GetInstruction() const;
HighLevelILInstruction AsAST() const;
HighLevelILInstruction AsNonAST() const;
bool HasParent() const;
HighLevelILInstruction GetParent() const;
uint64_t GetInstructionHash(size_t discriminator = 0) const
{
constexpr auto rotl = [](uint64_t value, int shift)
{ return (value << shift) | (value >> (64 - shift)); };
std::hash<uint64_t> hasher;
uint64_t hash = hasher(operation);
hash ^= rotl(hasher(address), 23);
hash ^= rotl(hasher(discriminator), 47);
return hash;
}
template <BNHighLevelILOperation N>
HighLevelILInstructionAccessor<N>& As()
{
if (operation != N)
throw HighLevelILInstructionAccessException();
return *(HighLevelILInstructionAccessor<N>*)this;
}
HighLevelILOneOperandInstruction& AsOneOperand() { return *(HighLevelILOneOperandInstruction*)this; }
HighLevelILTwoOperandInstruction& AsTwoOperand() { return *(HighLevelILTwoOperandInstruction*)this; }
HighLevelILTwoOperandWithCarryInstruction& AsTwoOperandWithCarry()
{
return *(HighLevelILTwoOperandWithCarryInstruction*)this;
}
template <BNHighLevelILOperation N>
const HighLevelILInstructionAccessor<N>& As() const
{
if (operation != N)
throw HighLevelILInstructionAccessException();
return *(const HighLevelILInstructionAccessor<N>*)this;
}
const HighLevelILConstantInstruction& AsConstant() const
{
return *(const HighLevelILConstantInstruction*)this;
}
const HighLevelILConstantDataInstruction& AsConstantData() const
{
return *(const HighLevelILConstantDataInstruction*)this;
}
const HighLevelILOneOperandInstruction& AsOneOperand() const
{
return *(const HighLevelILOneOperandInstruction*)this;
}
const HighLevelILTwoOperandInstruction& AsTwoOperand() const
{
return *(const HighLevelILTwoOperandInstruction*)this;
}
const HighLevelILTwoOperandWithCarryInstruction& AsTwoOperandWithCarry() const
{
return *(const HighLevelILTwoOperandWithCarryInstruction*)this;
}
};
/*!
\ingroup highlevelil
*/
struct HighLevelILInstruction : public HighLevelILInstructionBase
{
HighLevelILInstruction();
HighLevelILInstruction(HighLevelILFunction* func, const BNHighLevelILInstruction& instr, size_t expr,
bool asFullAst, size_t instructionIndex);
HighLevelILInstruction(const HighLevelILInstructionBase& instr);
void CollectSubExprs(_STD_STACK<size_t>& toProcess) const;
void VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& func) const;
void VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& preFunc,
const std::function<void(const HighLevelILInstruction& expr)>& postFunc) const;
ExprId CopyTo(HighLevelILFunction* dest) const;
ExprId CopyTo(HighLevelILFunction* dest,
const std::function<ExprId(const HighLevelILInstruction& subExpr)>& subExprHandler) const;
bool operator<(const HighLevelILInstruction& other) const;
bool operator==(const HighLevelILInstruction& other) const;
bool operator!=(const HighLevelILInstruction& other) const;
// Templated accessors for instruction operands, use these for efficient access to a known instruction
template <BNHighLevelILOperation N>
HighLevelILInstruction GetSourceExpr() const
{
return As<N>().GetSourceExpr();
}
template <BNHighLevelILOperation N>
Variable GetVariable() const
{
return As<N>().GetVariable();
}
template <BNHighLevelILOperation N>
Variable GetDestVariable() const
{
return As<N>().GetDestVariable();
}
template <BNHighLevelILOperation N>
SSAVariable GetSSAVariable() const
{
return As<N>().GetSSAVariable();
}
template <BNHighLevelILOperation N>
SSAVariable GetDestSSAVariable() const
{
return As<N>().GetDestSSAVariable();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetDestExpr() const
{
return As<N>().GetDestExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetLeftExpr() const
{
return As<N>().GetLeftExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetRightExpr() const
{
return As<N>().GetRightExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetCarryExpr() const
{
return As<N>().GetCarryExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetIndexExpr() const
{
return As<N>().GetIndexExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetConditionPhiExpr() const
{
return As<N>().GetConditionPhiExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetConditionExpr() const
{
return As<N>().GetConditionExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetTrueExpr() const
{
return As<N>().GetTrueExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetFalseExpr() const
{
return As<N>().GetFalseExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetLoopExpr() const
{
return As<N>().GetLoopExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetInitExpr() const
{
return As<N>().GetInitExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetUpdateExpr() const
{
return As<N>().GetUpdateExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetDefaultExpr() const
{
return As<N>().GetDefaultExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetHighExpr() const
{
return As<N>().GetHighExpr();
}
template <BNHighLevelILOperation N>
HighLevelILInstruction GetLowExpr() const
{
return As<N>().GetLowExpr();
}
template <BNHighLevelILOperation N>
uint64_t GetOffset() const
{
return As<N>().GetOffset();
}
template <BNHighLevelILOperation N>
size_t GetMemberIndex() const
{
return As<N>().GetMemberIndex();
}
template <BNHighLevelILOperation N>
int64_t GetConstant() const
{
return As<N>().GetConstant();
}
template <BNHighLevelILOperation N>
ConstantData GetConstantData() const
{
return As<N>().GetConstantData();
}
template <BNHighLevelILOperation N>
int64_t GetVector() const
{
return As<N>().GetVector();
}
template <BNHighLevelILOperation N>
uint32_t GetIntrinsic() const
{
return As<N>().GetIntrinsic();
}
template <BNHighLevelILOperation N>
uint64_t GetTarget() const
{
return As<N>().GetTarget();
}
template <BNHighLevelILOperation N>
HighLevelILInstructionList GetParameterExprs() const
{
return As<N>().GetParameterExprs();
}
template <BNHighLevelILOperation N>
HighLevelILInstructionList GetSourceExprs() const
{
return As<N>().GetSourceExprs();
}
template <BNHighLevelILOperation N>
HighLevelILInstructionList GetDestExprs() const
{
return As<N>().GetDestExprs();
}
template <BNHighLevelILOperation N>
HighLevelILInstructionList GetBlockExprs() const
{
return As<N>().GetBlockExprs();
}
template <BNHighLevelILOperation N>
HighLevelILInstructionList GetCases() const
{
return As<N>().GetCases();
}
template <BNHighLevelILOperation N>
HighLevelILInstructionList GetValueExprs() const
{
return As<N>().GetValueExprs();
}
template <BNHighLevelILOperation N>
HighLevelILSSAVariableList GetSourceSSAVariables() const
{
return As<N>().GetSourceSSAVariables();
}
template <BNHighLevelILOperation N>
size_t GetSourceMemoryVersion() const
{
return As<N>().GetSourceMemoryVersion();
}
template <BNHighLevelILOperation N>
HighLevelILIndexList GetSourceMemoryVersions() const
{
return As<N>().GetSourceMemoryVersions();
}
template <BNHighLevelILOperation N>
size_t GetDestMemoryVersion() const
{
return As<N>().GetDestMemoryVersion();
}
template <BNHighLevelILOperation N>
void SetSSAVersion(size_t version)
{
As<N>().SetSSAVersion(version);
}
template <BNHighLevelILOperation N>
void SetDestSSAVersion(size_t version)
{
As<N>().SetDestSSAVersion(version);
}
template <BNHighLevelILOperation N>
void SetParameterExprs(const _STD_VECTOR<MediumLevelILInstruction>& params)
{
As<N>().SetParameterExprs(params);
}
template <BNHighLevelILOperation N>
void SetParameterExprs(const _STD_VECTOR<ExprId>& params)
{
As<N>().SetParameterExprs(params);
}
template <BNHighLevelILOperation N>
void SetSourceExprs(const _STD_VECTOR<MediumLevelILInstruction>& params)
{
As<N>().SetSourceExprs(params);
}
template <BNHighLevelILOperation N>
void SetSourceExprs(const _STD_VECTOR<ExprId>& params)
{
As<N>().SetSourceExprs(params);
}
template <BNHighLevelILOperation N>
void SetDestExprs(const _STD_VECTOR<MediumLevelILInstruction>& params)
{
As<N>().SetDestExprs(params);
}
template <BNHighLevelILOperation N>
void SetDestExprs(const _STD_VECTOR<ExprId>& params)
{
As<N>().SetDestExprs(params);
}
template <BNHighLevelILOperation N>
void SetBlockExprs(const _STD_VECTOR<MediumLevelILInstruction>& params)
{
As<N>().SetBlockExprs(params);
}
template <BNHighLevelILOperation N>
void SetBlockExprs(const _STD_VECTOR<ExprId>& params)
{
As<N>().SetBlockExprs(params);
}
template <BNHighLevelILOperation N>
void SetCases(const _STD_VECTOR<MediumLevelILInstruction>& params)
{
As<N>().SetCases(params);
}
template <BNHighLevelILOperation N>
void SetCases(const _STD_VECTOR<ExprId>& params)
{
As<N>().SetCases(params);
}
template <BNHighLevelILOperation N>
void SetSourceSSAVariables(const _STD_VECTOR<SSAVariable>& vars)
{
As<N>().SetSourceSSAVariables(vars);
}
template <BNHighLevelILOperation N>
void SetSourceMemoryVersion(size_t version)
{
return As<N>().SetSourceMemoryVersion(version);
}
template <BNHighLevelILOperation N>
void SetDestMemoryVersion(size_t version)
{
return As<N>().SetDestMemoryVersion(version);
}
template <BNHighLevelILOperation N>
void SetTarget(uint64_t target)
{
As<N>().SetTarget(target);
}
bool GetOperandIndexForUsage(HighLevelILOperandUsage usage, size_t& operandIndex) const;
// Generic accessors for instruction operands, these will throw a HighLevelILInstructionAccessException
// on type mismatch. These are slower than the templated versions above.
HighLevelILInstruction GetSourceExpr() const;
Variable GetVariable() const;
Variable GetDestVariable() const;
SSAVariable GetSSAVariable() const;
SSAVariable GetDestSSAVariable() const;
HighLevelILInstruction GetDestExpr() const;
HighLevelILInstruction GetLeftExpr() const;
HighLevelILInstruction GetRightExpr() const;
HighLevelILInstruction GetCarryExpr() const;
HighLevelILInstruction GetIndexExpr() const;
HighLevelILInstruction GetConditionExpr() const;
HighLevelILInstruction GetConditionPhiExpr() const;
HighLevelILInstruction GetTrueExpr() const;
HighLevelILInstruction GetFalseExpr() const;
HighLevelILInstruction GetLoopExpr() const;
HighLevelILInstruction GetInitExpr() const;
HighLevelILInstruction GetUpdateExpr() const;
HighLevelILInstruction GetDefaultExpr() const;
HighLevelILInstruction GetHighExpr() const;
HighLevelILInstruction GetLowExpr() const;
uint64_t GetOffset() const;
size_t GetMemberIndex() const;
int64_t GetConstant() const;
ConstantData GetConstantData() const;
int64_t GetVector() const;
uint32_t GetIntrinsic() const;
uint64_t GetTarget() const;
HighLevelILInstructionList GetParameterExprs() const;
HighLevelILInstructionList GetSourceExprs() const;
HighLevelILInstructionList GetDestExprs() const;
HighLevelILInstructionList GetBlockExprs() const;
HighLevelILInstructionList GetCases() const;
HighLevelILInstructionList GetValueExprs() const;
HighLevelILSSAVariableList GetSourceSSAVariables() const;
size_t GetSourceMemoryVersion() const;
HighLevelILIndexList GetSourceMemoryVersions() const;
size_t GetDestMemoryVersion() const;
static bool CanCollapse(int operand);
};
/*!
\ingroup highlevelil
*/
class HighLevelILOperand
{
HighLevelILInstruction m_instr;
HighLevelILOperandUsage m_usage;
HighLevelILOperandType m_type;
size_t m_operandIndex;
public:
HighLevelILOperand(const HighLevelILInstruction& instr, HighLevelILOperandUsage usage, size_t operandIndex);
HighLevelILOperandType GetType() const { return m_type; }
HighLevelILOperandUsage GetUsage() const { return m_usage; }
uint64_t GetInteger() const;
ConstantData GetConstantData() const;
size_t GetIndex() const;
uint32_t GetIntrinsic() const;
HighLevelILInstruction GetExpr() const;
Variable GetVariable() const;
SSAVariable GetSSAVariable() const;
HighLevelILInstructionList GetExprList() const;
HighLevelILSSAVariableList GetSSAVariableList() const;
HighLevelILIndexList GetIndexList() const;
};
/*!
\ingroup highlevelil
*/
class HighLevelILOperandList
{
struct ListIterator
{
const HighLevelILOperandList* owner;
_STD_VECTOR<HighLevelILOperandUsage>::const_iterator pos;
bool operator==(const ListIterator& a) const { return pos == a.pos; }
bool operator!=(const ListIterator& a) const { return pos != a.pos; }
bool operator<(const ListIterator& a) const { return pos < a.pos; }
ListIterator& operator++()
{
++pos;
return *this;
}
const HighLevelILOperand operator*();
};
HighLevelILInstruction m_instr;
const _STD_VECTOR<HighLevelILOperandUsage>& m_usageList;
const _STD_UNORDERED_MAP<HighLevelILOperandUsage, size_t>& m_operandIndexMap;
public:
typedef ListIterator const_iterator;
HighLevelILOperandList(const HighLevelILInstruction& instr,
const _STD_VECTOR<HighLevelILOperandUsage>& usageList,
const _STD_UNORDERED_MAP<HighLevelILOperandUsage, size_t>& operandIndexMap);
const_iterator begin() const;
const_iterator end() const;
size_t size() const;
const HighLevelILOperand operator[](size_t i) const;
operator _STD_VECTOR<HighLevelILOperand>() const;
};
/*!
\ingroup highlevelil
*/
struct HighLevelILConstantInstruction : public HighLevelILInstructionBase
{
int64_t GetConstant() const { return GetRawOperandAsInteger(0); }
};
/*!
\ingroup highlevelil
*/
struct HighLevelILConstantDataInstruction : public HighLevelILInstructionBase
{
ConstantData GetConstantData() const { return GetRawOperandAsConstantData(0); }
};
/*!
\ingroup highlevelil
*/
struct HighLevelILOneOperandInstruction : public HighLevelILInstructionBase
{
HighLevelILInstruction GetSourceExpr() const { return GetRawOperandAsExpr(0); }
};
/*!
\ingroup highlevelil
*/
struct HighLevelILTwoOperandInstruction : public HighLevelILInstructionBase
{
HighLevelILInstruction GetLeftExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetRightExpr() const { return GetRawOperandAsExpr(1); }
};
/*!
\ingroup highlevelil
*/
struct HighLevelILTwoOperandWithCarryInstruction : public HighLevelILInstructionBase
{
HighLevelILInstruction GetLeftExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetRightExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstruction GetCarryExpr() const { return GetRawOperandAsExpr(2); }
};
// Implementations of each instruction to fetch the correct operand value for the valid operands, these
// are derived from HighLevelILInstructionBase so that invalid operand accessor functions will generate
// a compiler error.
template <>
struct HighLevelILInstructionAccessor<HLIL_BLOCK> : public HighLevelILInstructionBase
{
HighLevelILInstructionList GetBlockExprs() const { return GetRawOperandAsExprList(0); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_IF> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetTrueExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstruction GetFalseExpr() const { return GetRawOperandAsExpr(2); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_WHILE> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetLoopExpr() const { return GetRawOperandAsExpr(1); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_WHILE_SSA> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetConditionPhiExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstruction GetLoopExpr() const { return GetRawOperandAsExpr(2); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_DO_WHILE> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetLoopExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(1); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_DO_WHILE_SSA> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetLoopExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetConditionPhiExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(2); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_FOR> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetInitExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstruction GetUpdateExpr() const { return GetRawOperandAsExpr(2); }
HighLevelILInstruction GetLoopExpr() const { return GetRawOperandAsExpr(3); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_FOR_SSA> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetInitExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetConditionPhiExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(2); }
HighLevelILInstruction GetUpdateExpr() const { return GetRawOperandAsExpr(3); }
HighLevelILInstruction GetLoopExpr() const { return GetRawOperandAsExpr(4); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_SWITCH> : public HighLevelILInstructionBase
{
HighLevelILInstruction GetConditionExpr() const { return GetRawOperandAsExpr(0); }
HighLevelILInstruction GetDefaultExpr() const { return GetRawOperandAsExpr(1); }
HighLevelILInstructionList GetCases() const { return GetRawOperandAsExprList(2); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_CASE> : public HighLevelILInstructionBase
{
HighLevelILInstructionList GetValueExprs() const { return GetRawOperandAsExprList(0); }
HighLevelILInstruction GetTrueExpr() const { return GetRawOperandAsExpr(2); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_GOTO> : public HighLevelILInstructionBase
{
uint64_t GetTarget() const { return GetRawOperandAsInteger(0); }
void SetTarget(uint64_t target) { UpdateRawOperandAsInteger(0, target); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_LABEL> : public HighLevelILInstructionBase
{
uint64_t GetTarget() const { return GetRawOperandAsInteger(0); }
void SetTarget(uint64_t target) { UpdateRawOperandAsInteger(0, target); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_RET> : public HighLevelILInstructionBase
{
HighLevelILInstructionList GetSourceExprs() const { return GetRawOperandAsExprList(0); }
void SetSourceExprs(const _STD_VECTOR<ExprId>& exprs) { UpdateRawOperandAsExprList(0, exprs); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_VAR_DECLARE> : public HighLevelILInstructionBase
{
Variable GetVariable() const { return GetRawOperandAsVariable(0); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_VAR_INIT> : public HighLevelILInstructionBase
{
Variable GetDestVariable() const { return GetRawOperandAsVariable(0); }
HighLevelILInstruction GetSourceExpr() const { return GetRawOperandAsExpr(1); }
};
template <>
struct HighLevelILInstructionAccessor<HLIL_VAR_INIT_SSA> : public HighLevelILInstructionBase
{
SSAVariable GetDestSSAVariable() const { return GetRawOperandAsSSAVariable(0); }
void SetDestSSAVersion(size_t version) { UpdateRawOperand(1, version); }
HighLevelILInstruction GetSourceExpr() const { return GetRawOperandAsExpr(2); }
};
template <>