|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Diagnostics.CodeAnalysis; |
4 | | -using System.Linq; |
5 | 1 | using System.Text; |
6 | | -using Cpp2IL.Core.ISIL; |
7 | | -using Gee.External.Capstone.Arm; |
8 | | -using Gee.External.Capstone.Arm64; |
9 | | -using Iced.Intel; |
10 | | -using Instruction = Iced.Intel.Instruction; |
11 | 2 |
|
12 | 3 | namespace Cpp2IL.Core.Extensions |
13 | 4 | { |
14 | 5 | public static class MiscExtensions |
15 | 6 | { |
16 | | - public static InstructionSetIndependentOperand MakeIndependent(this Register reg) => InstructionSetIndependentOperand.MakeRegister(reg.ToString().ToLower()); |
17 | | - |
18 | | - public static ulong GetImmediateSafe(this Instruction instruction, int op) => instruction.GetOpKind(op).IsImmediate() ? instruction.GetImmediate(op) : 0; |
19 | | - |
20 | | - public static bool IsJump(this Mnemonic mnemonic) => mnemonic is Mnemonic.Call or >= Mnemonic.Ja and <= Mnemonic.Js; |
21 | | - public static bool IsConditionalJump(this Mnemonic mnemonic) => mnemonic.IsJump() && mnemonic != Mnemonic.Jmp && mnemonic != Mnemonic.Call; |
22 | | - |
23 | | - //Arm Extensions |
24 | | - public static ArmRegister? RegisterSafe(this ArmOperand operand) => operand.Type != ArmOperandType.Register ? null : operand.Register; |
25 | | - public static bool IsImmediate(this ArmOperand operand) => operand.Type is ArmOperandType.CImmediate or ArmOperandType.Immediate or ArmOperandType.PImmediate; |
26 | | - public static int ImmediateSafe(this ArmOperand operand) => operand.IsImmediate() ? operand.Immediate : 0; |
27 | | - private static ArmOperand? MemoryOperand(ArmInstruction instruction) => instruction.Details.Operands.FirstOrDefault(a => a.Type == ArmOperandType.Memory); |
28 | | - |
29 | | - public static ArmRegister? MemoryBase(this ArmInstruction instruction) => MemoryOperand(instruction)?.Memory.Base; |
30 | | - public static ArmRegister? MemoryIndex(this ArmInstruction instruction) => MemoryOperand(instruction)?.Memory.Index; |
31 | | - public static int MemoryOffset(this ArmInstruction instruction) => MemoryOperand(instruction)?.Memory.Displacement ?? 0; |
32 | | - |
33 | | - //Arm64 Extensions |
34 | | - public static Arm64Register? RegisterSafe(this Arm64Operand operand) => operand.Type != Arm64OperandType.Register ? null : operand.Register; |
35 | | - public static bool IsImmediate(this Arm64Operand operand) => operand.Type is Arm64OperandType.CImmediate or Arm64OperandType.Immediate; |
36 | | - public static long ImmediateSafe(this Arm64Operand operand) => operand.IsImmediate() ? operand.Immediate : 0; |
37 | | - internal static Arm64Operand? MemoryOperand(this Arm64Instruction instruction) => instruction.Details.Operands.FirstOrDefault(a => a.Type == Arm64OperandType.Memory); |
38 | | - |
39 | | - public static Arm64Register? MemoryBase(this Arm64Instruction instruction) => instruction.MemoryOperand()?.Memory.Base; |
40 | | - public static Arm64Register? MemoryIndex(this Arm64Instruction instruction) => instruction.MemoryOperand()?.Memory.Index; |
41 | | - public static int MemoryOffset(this Arm64Instruction instruction) => instruction.MemoryOperand()?.Memory.Displacement ?? 0; |
42 | | - |
43 | | - public static bool IsConditionalMove(this Instruction instruction) |
44 | | - { |
45 | | - switch (instruction.Mnemonic) |
46 | | - { |
47 | | - case Mnemonic.Cmove: |
48 | | - case Mnemonic.Cmovne: |
49 | | - case Mnemonic.Cmovs: |
50 | | - case Mnemonic.Cmovns: |
51 | | - case Mnemonic.Cmovg: |
52 | | - case Mnemonic.Cmovge: |
53 | | - case Mnemonic.Cmovl: |
54 | | - case Mnemonic.Cmovle: |
55 | | - case Mnemonic.Cmova: |
56 | | - case Mnemonic.Cmovae: |
57 | | - case Mnemonic.Cmovb: |
58 | | - case Mnemonic.Cmovbe: |
59 | | - return true; |
60 | | - default: |
61 | | - return false; |
62 | | - } |
63 | | - } |
64 | 7 | public static Stack<T> Clone<T>(this Stack<T> original) |
65 | 8 | { |
66 | 9 | var arr = new T[original.Count]; |
@@ -130,9 +73,6 @@ public static T[] SubArray<T>(this T[] source, Range range) |
130 | 73 | return arr[i]; |
131 | 74 | } |
132 | 75 |
|
133 | | - public static bool IsImmediate(this OpKind opKind) => opKind is >= OpKind.Immediate8 and <= OpKind.Immediate32to64; |
134 | | - |
135 | | - |
136 | 76 | public static void TrimEndWhile<T>(this List<T> instructions, Func<T, bool> predicate) |
137 | 77 | { |
138 | 78 | var i = instructions.Count - 1; |
|
0 commit comments