Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
test0.cpp
Go to the documentation of this file.
1#if defined(_MSC_VER) && (_MSC_VER <= 1200)
2 #pragma warning(disable:4514)
3 #pragma warning(disable:4786)
4#endif
5#if defined(_MSC_VER) && (_MSC_VER >= 1900)
6 #pragma warning(disable:4456)
7#endif
8#include <stdio.h>
9#include <stdlib.h>
10#define XBYAK_NO_OP_NAMES
11#include "xbyak/xbyak.h"
12
14 void operator=(const Sample&);
15public:
16 Sample(void *userPtr = 0, size_t size = Xbyak::DEFAULT_MAX_CODE_SIZE) : Xbyak::CodeGenerator(size, userPtr)
17 {
18 inLocalLabel(); // use local label for multiple instance
19#ifdef XBYAK32
20 mov(ecx, ptr [esp + 4]); // n
21#elif defined(XBYAK64_GCC)
22 mov(ecx, edi); // n
23#else
24 // n = ecx
25#endif
26 xor_(eax, eax); // sum
27 test(ecx, ecx);
28 jz(".exit");
29 xor_(edx, edx); // i
30 L(".lp");
31 add(eax, edx);
32 inc(edx);
33
34 cmp(edx, ecx);
35 jbe(".lp"); // jmp to previous @@
36 L(".exit"); // <B>
37 ret();
38 outLocalLabel(); // end of local label
39 }
40};
41
43 void operator=(const AddFunc&);
44public:
45 AddFunc(int y)
46 {
47#ifdef XBYAK32
48 mov(eax, ptr [esp + 4]);
49 add(eax, y);
50#elif defined(XBYAK64_WIN)
51 lea(rax, ptr [rcx + y]);
52#else
53 lea(eax, ptr [edi + y]);
54#endif
55 ret();
56 }
57 int (*get() const)(int) { return getCode<int(*)(int)>(); }
58};
59
61 void operator=(const CallAtoi&);
62public:
64 {
65#ifdef XBYAK64
66#ifdef XBYAK64_WIN
67 sub(rsp, 32); // return-address is destroied if 64bit debug mode
68#endif
69 mov(rax, (size_t)atoi);
70 call(rax);
71#ifdef XBYAK64_WIN
72 add(rsp, 32);
73#endif
74#else
75 mov(eax, ptr [esp + 4]);
76 push(eax);
77#ifdef XBYAK_VARIADIC_TEMPLATE
78 call(atoi);
79#else
81#endif
82 add(esp, 4);
83#endif
84 ret();
85 }
86 int (*get() const)(const char *) { return getCode<int (*)(const char *)>(); }
87};
88
90 void operator=(const JmpAtoi&);
91public:
93 {
94 /* already pushed "456" */
95#ifdef XBYAK64
96 mov(rax, (size_t)atoi);
97 jmp(rax);
98#else
100#endif
101 }
102 int (*get() const)(const char *) { return getCode<int (*)(const char *)>(); }
103};
104
106 void init(int n)
107 {
108 xor_(eax, eax);
109 mov(ecx, n);
110 test(ecx, ecx);
111 jnz("@f");
112 ret();
113 L("@@");
114 for (int i = 0; i < 10 - n; i++) {
115 add(eax, ecx);
116 }
117 sub(ecx, 1);
118 jnz("@b");
119 ret();
120 }
121};
122
124{
125 puts("testReset");
126 Reset code;
127 int (*f)(int) = code.getCode<int(*)(int)>();
128 for (int i = 0; i < 10; i++) {
129 code.init(i);
130 int v = f(i);
131 printf("%d %d\n", i, v);
132 code.reset();
133 }
134}
135
136int main()
137{
138 try {
139 Sample s;
140 printf("Xbyak version=%s\n", s.getVersionString());
141#ifdef XBYAK64_GCC
142 puts("64bit mode(gcc)");
143#elif defined(XBYAK64_WIN)
144 puts("64bit mode(win)");
145#else
146 puts("32bit");
147#endif
148 int (*func)(int) = s.getCode<int (*)(int)>();
149 for (int i = 0; i <= 10; i++) {
150 printf("0 + ... + %d = %d\n", i, func(i));
151 }
152 for (int i = 0; i < 10; i++) {
153 AddFunc a(i);
154 int (*add)(int) = a.get();
155 int y = add(i);
156 printf("%d + %d = %d\n", i, i, y);
157 }
158 CallAtoi c;
159 printf("call atoi(\"123\") = %d\n", c.get()("123"));
160 JmpAtoi j;
161 printf("jmp atoi(\"456\") = %d\n", j.get()("456"));
162 {
163 // use memory allocated by user
164 using namespace Xbyak;
165 const size_t codeSize = 1024;
166 uint8 buf[codeSize + 16];
167 uint8 *p = CodeArray::getAlignedAddress(buf);
168 CodeArray::protect(p, codeSize, true);
169 Sample s(p, codeSize);
170 int (*func)(int) = s.getCode<int (*)(int)>();
171 if (Xbyak::CastTo<uint8*>(func) != p) {
172 fprintf(stderr, "internal error %p %p\n", p, Xbyak::CastTo<uint8*>(func));
173 return 1;
174 }
175 printf("0 + ... + %d = %d\n", 100, func(100));
176 CodeArray::protect(p, codeSize, false);
177 }
178 puts("OK");
179 testReset();
180 } catch (std::exception& e) {
181 printf("ERR:%s\n", e.what());
182 } catch (...) {
183 printf("unknown error\n");
184 }
185}
186
const mie::Vuint & p
Definition bn.cpp:27
AddFunc(int y)
Definition test0.cpp:45
int(*)(int) get() const
Definition test0.cpp:57
CallAtoi()
Definition test0.cpp:63
int(*)(const char *) get() const
Definition test0.cpp:86
int(*)(const char *) get() const
Definition test0.cpp:102
JmpAtoi()
Definition test0.cpp:92
Sample(void *userPtr=0, size_t size=Xbyak::DEFAULT_MAX_CODE_SIZE)
Definition test0.cpp:16
const uint8 * getCode() const
Definition xbyak.h:905
void call(const Operand &op)
Definition xbyak.h:2150
void xor_(const Operand &op, uint32 imm)
Definition xbyak.h:1279
void cmp(const Operand &op, uint32 imm)
Definition xbyak.h:94
void jbe(const Label &label, LabelType type=T_AUTO)
Definition xbyak.h:321
const Reg32 esp
Definition xbyak.h:2087
const Reg32 eax
Definition xbyak.h:2087
void jmp(const Operand &op)
Definition xbyak.h:2144
void inc(const Operand &op)
Definition xbyak.h:307
const Reg32 ecx
Definition xbyak.h:2087
void test(const Operand &op, const Reg &reg)
Definition xbyak.h:2162
void add(const Operand &op, uint32 imm)
Definition xbyak.h:6
void sub(const Operand &op, uint32 imm)
Definition xbyak.h:746
const Reg32 edi
Definition xbyak.h:2087
void jz(const Label &label, LabelType type=T_AUTO)
Definition xbyak.h:425
const Reg32 edx
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 jnz(const Label &label, LabelType type=T_AUTO)
Definition xbyak.h:401
void L(const std::string &label)
Definition xbyak.h:2126
void push(const Operand &op)
Definition xbyak.h:2189
const AddressFrame ptr
Definition xbyak.h:2090
Definition xbyak.h:104
@ DEFAULT_MAX_CODE_SIZE
Definition xbyak.h:107
const To CastTo(From p)
Definition xbyak.h:279
unsigned char uint8
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
int add(int a, int b)
void init(int n)
Definition test0.cpp:106
void testReset()
Definition test0.cpp:123
int main()
Definition test0.cpp:136
Xbyak ; JIT assembler for x86(IA32)/x64 by C++.
CK_RV ret
char * s
uint16_t j
uint8_t buf[2048]