Skip to content

Commit 7ba96c1

Browse files
committed
Use constexpr arrays for metadata about IL instruction operands
This avoids constructing two unordered maps and numerous vectors for each IL level at library load time for each plug-in that uses the C++ API. Additionally, the arrays allow for more efficient look-ups.
1 parent 8440e4f commit 7ba96c1

File tree

7 files changed

+1003
-867
lines changed

7 files changed

+1003
-867
lines changed

highlevelilinstruction.cpp

Lines changed: 258 additions & 225 deletions
Large diffs are not rendered by default.

highlevelilinstruction.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ namespace BinaryNinja
6262
/*!
6363
\ingroup highlevelil
6464
*/
65-
enum HighLevelILOperandType
65+
enum HighLevelILOperandType : uint8_t
6666
{
6767
IntegerHighLevelOperand,
6868
ConstantDataHighLevelOperand,
@@ -79,7 +79,7 @@ namespace BinaryNinja
7979
/*!
8080
\ingroup highlevelil
8181
*/
82-
enum HighLevelILOperandUsage
82+
enum HighLevelILOperandUsage : uint8_t
8383
{
8484
SourceExprHighLevelOperandUsage,
8585
VariableHighLevelOperandUsage,
@@ -358,10 +358,6 @@ namespace BinaryNinja
358358
size_t exprIndex, instructionIndex;
359359
bool ast;
360360

361-
static _STD_UNORDERED_MAP<HighLevelILOperandUsage, HighLevelILOperandType> operandTypeForUsage;
362-
static _STD_UNORDERED_MAP<BNHighLevelILOperation, _STD_VECTOR<HighLevelILOperandUsage>> operationOperandUsage;
363-
static _STD_UNORDERED_MAP<BNHighLevelILOperation, _STD_UNORDERED_MAP<HighLevelILOperandUsage, size_t>>
364-
operationOperandIndex;
365361

366362
HighLevelILOperandList GetOperands() const;
367363

@@ -856,28 +852,29 @@ namespace BinaryNinja
856852
typedef value_type reference;
857853

858854
const HighLevelILOperandList* owner;
859-
_STD_VECTOR<HighLevelILOperandUsage>::const_iterator pos;
860-
bool operator==(const ListIterator& a) const { return pos == a.pos; }
861-
bool operator!=(const ListIterator& a) const { return pos != a.pos; }
862-
bool operator<(const ListIterator& a) const { return pos < a.pos; }
855+
size_t index;
856+
constexpr bool operator==(const ListIterator& a) const { return index == a.index; }
857+
constexpr auto operator<=>(const ListIterator& a) const { return index <=> a.index; }
863858
ListIterator& operator++()
864859
{
865-
++pos;
860+
++index;
866861
return *this;
867862
}
868863
const HighLevelILOperand operator*();
869864
};
870865

871866
HighLevelILInstruction m_instr;
872-
const _STD_VECTOR<HighLevelILOperandUsage>& m_usageList;
873-
const _STD_UNORDERED_MAP<HighLevelILOperandUsage, size_t>& m_operandIndexMap;
867+
const HighLevelILOperandUsage* m_usages;
868+
const uint8_t* m_indices;
869+
uint8_t m_count;
874870

875871
public:
876872
typedef ListIterator const_iterator;
877873

878874
HighLevelILOperandList(const HighLevelILInstruction& instr,
879-
const _STD_VECTOR<HighLevelILOperandUsage>& usageList,
880-
const _STD_UNORDERED_MAP<HighLevelILOperandUsage, size_t>& operandIndexMap);
875+
const HighLevelILOperandUsage* usages,
876+
const uint8_t* indices,
877+
uint8_t count);
881878

882879
const_iterator begin() const;
883880
const_iterator end() const;

lowlevelilinstruction.cpp

Lines changed: 328 additions & 298 deletions
Large diffs are not rendered by default.

lowlevelilinstruction.h

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ namespace BinaryNinja
162162
/*!
163163
\ingroup lowlevelil
164164
*/
165-
enum LowLevelILOperandType
165+
enum LowLevelILOperandType : uint8_t
166166
{
167167
IntegerLowLevelOperand,
168168
IndexLowLevelOperand,
@@ -192,7 +192,7 @@ namespace BinaryNinja
192192
/*!
193193
\ingroup lowlevelil
194194
*/
195-
enum LowLevelILOperandUsage
195+
enum LowLevelILOperandUsage : uint8_t
196196
{
197197
SourceExprLowLevelOperandUsage,
198198
SourceRegisterLowLevelOperandUsage,
@@ -750,11 +750,6 @@ namespace BinaryNinja
750750
#endif
751751
size_t exprIndex, instructionIndex;
752752

753-
static _STD_UNORDERED_MAP<LowLevelILOperandUsage, LowLevelILOperandType> operandTypeForUsage;
754-
static _STD_UNORDERED_MAP<BNLowLevelILOperation, _STD_VECTOR<LowLevelILOperandUsage>> operationOperandUsage;
755-
static _STD_UNORDERED_MAP<BNLowLevelILOperation, _STD_UNORDERED_MAP<LowLevelILOperandUsage, size_t>>
756-
operationOperandIndex;
757-
758753
LowLevelILOperandList GetOperands() const;
759754

760755
uint64_t GetRawOperandAsInteger(size_t operand) const;
@@ -1290,27 +1285,27 @@ namespace BinaryNinja
12901285
typedef value_type reference;
12911286

12921287
const LowLevelILOperandList* owner;
1293-
_STD_VECTOR<LowLevelILOperandUsage>::const_iterator pos;
1294-
bool operator==(const ListIterator& a) const { return pos == a.pos; }
1295-
bool operator!=(const ListIterator& a) const { return pos != a.pos; }
1296-
bool operator<(const ListIterator& a) const { return pos < a.pos; }
1288+
size_t index;
1289+
constexpr bool operator==(const ListIterator& a) const { return index == a.index; }
1290+
constexpr auto operator<=>(const ListIterator& a) const { return index <=> a.index; }
12971291
ListIterator& operator++()
12981292
{
1299-
++pos;
1293+
++index;
13001294
return *this;
13011295
}
13021296
const LowLevelILOperand operator*();
13031297
};
13041298

13051299
LowLevelILInstruction m_instr;
1306-
const _STD_VECTOR<LowLevelILOperandUsage>& m_usageList;
1307-
const _STD_UNORDERED_MAP<LowLevelILOperandUsage, size_t>& m_operandIndexMap;
1300+
const LowLevelILOperandUsage* m_usages;
1301+
const uint8_t* m_indices;
1302+
uint8_t m_count;
13081303

13091304
public:
13101305
typedef ListIterator const_iterator;
13111306

1312-
LowLevelILOperandList(const LowLevelILInstruction& instr, const _STD_VECTOR<LowLevelILOperandUsage>& usageList,
1313-
const _STD_UNORDERED_MAP<LowLevelILOperandUsage, size_t>& operandIndexMap);
1307+
LowLevelILOperandList(const LowLevelILInstruction& instr, const LowLevelILOperandUsage* usages,
1308+
const uint8_t* indices, uint8_t count);
13141309

13151310
const_iterator begin() const;
13161311
const_iterator end() const;

0 commit comments

Comments
 (0)