Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
jmp_table.cpp
Go to the documentation of this file.
1/*
2 sample of move(reg, LABEL);, L(LABEL), putL(LABEL);
3*/
4#include <stdio.h>
5#define XBYAK_NO_OP_NAMES
6#include <xbyak/xbyak.h>
7
8const int expectTbl[] = {
9 5, 9, 12
10};
11
13 explicit Code(int mode, size_t size, void *p)
14 : Xbyak::CodeGenerator(size, p)
15 {
17#ifdef XBYAK64
18 const Xbyak::Reg64& a = rax;
19 const Xbyak::Reg64& c = rcx;
20#ifdef XBYAK64_WIN
21 mov(rax, rcx);
22#else
23 mov(rax, rdi);
24#endif
25#else
26 const Xbyak::Reg32& a = eax;
27 const Xbyak::Reg32& c = ecx;
28 mov(a, ptr [esp + 4]);
29#endif
30
31 switch (mode) {
32 case 0:
33 mov(c, ".jmp_table");
34 lea(c, ptr [c + a * 8]);
35 jmp(c);
36 align(8);
37 L(".jmp_table");
38 mov(a, expectTbl[0]);
39 ret();
40 align(8);
41 mov(a, expectTbl[1]);
42 ret();
43 align(8);
44 mov(a, expectTbl[2]);
45 ret();
46 break;
47
48 case 1:
49 /*
50 the label for putL is defined when called
51 */
52 mov(c, ".jmp_table");
53 jmp(ptr [c + a * (int)sizeof(size_t)]);
54 L(".label1");
55 mov(a, expectTbl[0]);
56 jmp(".end");
57 L(".label2");
58 mov(a, expectTbl[1]);
59 jmp(".end");
60 L(".label3");
61 mov(a, expectTbl[2]);
62 jmp(".end");
63 L(".end");
64 ret();
65 ud2();
66
67 align(8);
68 L(".jmp_table");
69 putL(".label1");
70 putL(".label2");
71 putL(".label3");
72 break;
73
74 case 2:
75 /*
76 the label for putL is not defined when called
77 */
78 jmp(".in");
79 ud2();
80 align(8);
81 L(".jmp_table");
82 putL(".label1");
83 putL(".label2");
84 putL(".label3");
85 L(".in");
86 mov(c, ".jmp_table");
87 jmp(ptr [c + a * (int)sizeof(size_t)]);
88 L(".label1");
89 mov(a, expectTbl[0]);
90 jmp(".end");
91 L(".label2");
92 mov(a, expectTbl[1]);
93 jmp(".end");
94 L(".label3");
95 mov(a, expectTbl[2]);
96 jmp(".end");
97 L(".end");
98 ret();
99 break;
100 }
102 }
103};
104
105int main()
106 try
107{
108 for (int mode = 0; mode < 3; mode++) {
109 printf("mode=%d\n", mode);
110 for (int grow = 0; grow < 2; grow++) {
111 printf("auto grow=%s\n", grow ? "on" : "off");
112 Code c(mode, grow ? 30 : 4096, grow ? Xbyak::AutoGrow : 0);
113 int (*f)(int) = c.getCode<int (*)(int)>();
114 c.ready();
115 for (int i = 0; i < 3; i++) {
116 const int a = expectTbl[i];
117 const int b = f(i);
118 if (a != b) {
119 printf("ERR i=%d, a=%d, b=%d\n", i, a, b);
120 exit(1);
121 }
122 }
123 }
124 }
125 puts("ok");
126} catch (std::exception& e) {
127 printf("ERR %s\n", e.what());
128}
const mie::Vuint & p
Definition bn.cpp:27
const uint8 * getCode() const
Definition xbyak.h:905
void align(size_t x=16, bool useMultiByteNop=true)
Definition xbyak.h:2475
const Reg32 esp
Definition xbyak.h:2087
const Reg32 eax
Definition xbyak.h:2087
void jmp(const Operand &op)
Definition xbyak.h:2144
const Reg32 ecx
Definition xbyak.h:2087
void mov(const Operand &reg1, const Operand &reg2)
Definition xbyak.h:2210
void lea(const Reg &reg, const Address &addr)
Definition xbyak.h:432
void L(const std::string &label)
Definition xbyak.h:2126
void putL(std::string label)
Definition xbyak.h:2141
const AddressFrame ptr
Definition xbyak.h:2090
const int expectTbl[]
Definition jmp_table.cpp:8
int main()
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
Definition xbyak.h:104
void *const AutoGrow
Definition xbyak.h:788
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
Definition bench.cpp:18
Code(int mode, size_t size, void *p)
Definition jmp_table.cpp:13
Xbyak ; JIT assembler for x86(IA32)/x64 by C++.
CK_RV ret