1 /* Bra86.c -- Branch converter for X86 code (BCJ)
2 2023-04-02 : Igor Pavlov : Public domain */
10 #if defined(MY_CPU_SIZEOF_POINTER) \
11 && ( MY_CPU_SIZEOF_POINTER == 4 \
12 || MY_CPU_SIZEOF_POINTER == 8)
13 #define BR_CONV_USE_OPT_PC_PTR
16 #ifdef BR_CONV_USE_OPT_PC_PTR
17 #define BR_PC_INIT pc -= (UInt32)(SizeT)p; // (MY_uintptr_t)
18 #define BR_PC_GET (pc + (UInt32)(SizeT)p)
20 #define BR_PC_INIT pc += (UInt32)size;
21 #define BR_PC_GET (pc - (UInt32)(SizeT)(lim - p))
23 // #define BR_PC_GET (pc + (UInt32)(SizeT)(p - data))
26 #define BR_CONVERT_VAL(v, c) if (encoding) v += c; else v -= c;
27 // #define BR_CONVERT_VAL(v, c) if (!encoding) c = (UInt32)0 - c; v += c;
29 #define Z7_BRANCH_CONV_ST(name) z7_BranchConvSt_ ## name
31 #define BR86_NEED_CONV_FOR_MS_BYTE(b) ((((b) + 1) & 0xfe) == 0)
33 #ifdef MY_CPU_LE_UNALIGN
34 #define BR86_PREPARE_BCJ_SCAN const UInt32 v = GetUi32(p) ^ 0xe8e8e8e8;
35 #define BR86_IS_BCJ_BYTE(n) ((v & ((UInt32)0xfe << (n) * 8)) == 0)
37 #define BR86_PREPARE_BCJ_SCAN
38 // bad for MSVC X86 (partial write to byte reg):
39 #define BR86_IS_BCJ_BYTE(n) ((p[n - 4] & 0xfe) == 0xe8)
40 // bad for old MSVC (partial write to byte reg):
41 // #define BR86_IS_BCJ_BYTE(n) (((*p ^ 0xe8) & 0xfe) == 0)
47 Byte *Z7_BRANCH_CONV_ST(X86)(Byte *p, SizeT size, UInt32 pc, UInt32 *state, int encoding)
53 const Byte *lim = p + size - 4;
54 unsigned mask = (unsigned)*state; // & 7;
55 #ifdef BR_CONV_USE_OPT_PC_PTR
56 /* if BR_CONV_USE_OPT_PC_PTR is defined: we need to adjust (pc) for (+4),
57 because call/jump offset is relative to the next instruction.
58 if BR_CONV_USE_OPT_PC_PTR is not defined : we don't need to adjust (pc) for (+4),
59 because BR_PC_GET uses (pc - (lim - p)), and lim was adjusted for (-4) before.
75 if (BR86_IS_BCJ_BYTE(0)) { goto m0; } mask >>= 1;
76 if (BR86_IS_BCJ_BYTE(1)) { goto m1; } mask >>= 1;
77 if (BR86_IS_BCJ_BYTE(2)) { goto m2; } mask = 0;
78 if (BR86_IS_BCJ_BYTE(3)) { goto a3; }
90 // if (((0x17u >> mask) & 1) == 0)
91 if (mask > 4 || mask == 3)
94 continue; // goto cont;
97 if (BR86_NEED_CONV_FOR_MS_BYTE(p[mask]))
98 continue; // goto cont;
99 // if (!BR86_NEED_CONV_FOR_MS_BYTE(p[3])) continue; // goto cont;
101 UInt32 v = GetUi32(p);
103 v += (1 << 24); if (v & 0xfe000000) continue; // goto cont;
108 if (BR86_NEED_CONV_FOR_MS_BYTE(v >> mask))
110 v ^= (((UInt32)0x100 << mask) - 1);
112 // for X86 : we can recalculate (c) to reduce register pressure
119 // v = (v & ((1 << 24) - 1)) - (v & (1 << 24));
120 v &= (1 << 25) - 1; v -= (1 << 24);
131 BR86_PREPARE_BCJ_SCAN
133 if (BR86_IS_BCJ_BYTE(0)) { goto a0; }
134 if (BR86_IS_BCJ_BYTE(1)) { goto a1; }
135 if (BR86_IS_BCJ_BYTE(2)) { goto a2; }
136 if (BR86_IS_BCJ_BYTE(3)) { goto a3; }
147 // if (!BR86_NEED_CONV_FOR_MS_BYTE(p[3])) continue; // goto cont;
149 UInt32 v = GetUi32(p);
151 v += (1 << 24); if (v & 0xfe000000) continue; // goto cont;
154 // v = (v & ((1 << 24) - 1)) - (v & (1 << 24));
155 v &= (1 << 25) - 1; v -= (1 << 24);
165 // the following processing for tail is optional and can be commented
168 for (; p < lim; p++, mask >>= 1)
169 if ((*p & 0xfe) == 0xe8)
172 *state = (UInt32)mask;
178 #define Z7_BRANCH_CONV_ST_FUNC_IMP(name, m, encoding) \
180 Z7_ATTRIB_NO_VECTOR \
181 Byte *m(name)(Byte *data, SizeT size, UInt32 pc, UInt32 *state) \
182 { return Z7_BRANCH_CONV_ST(name)(data, size, pc, state, encoding); }
184 Z7_BRANCH_CONV_ST_FUNC_IMP(X86, Z7_BRANCH_CONV_ST_DEC, 0)
185 #ifndef Z7_EXTRACT_ONLY
186 Z7_BRANCH_CONV_ST_FUNC_IMP(X86, Z7_BRANCH_CONV_ST_ENC, 1)