jcxz/jecxz support new Label
This commit is contained in:
parent
abae0742fb
commit
59f4d082fc
3 changed files with 327 additions and 285 deletions
|
@ -49,6 +49,7 @@ std::string type2String(int type)
|
|||
void put_jREGz(const char *reg, bool prefix)
|
||||
{
|
||||
printf("void j%sz(const std::string& label) { %sopJmp(label, T_SHORT, 0xe3, 0, 0); }\n", reg, prefix ? "db(0x67); " : "");
|
||||
printf("void j%sz(Label& label) { %sopJmp(label, T_SHORT, 0xe3, 0, 0); }\n", reg, prefix ? "db(0x67); " : "");
|
||||
}
|
||||
|
||||
void put()
|
||||
|
|
229
test/jmp.cpp
229
test/jmp.cpp
|
@ -16,8 +16,12 @@ void putNop(Xbyak::CodeGenerator *gen, int n)
|
|||
gen->nop();
|
||||
}
|
||||
}
|
||||
struct TestJmp : public Xbyak::CodeGenerator {
|
||||
/*
|
||||
|
||||
void test1()
|
||||
{
|
||||
puts("test1");
|
||||
struct TestJmp : public Xbyak::CodeGenerator {
|
||||
/*
|
||||
4 X0:
|
||||
5 00000004 EBFE jmp short X0
|
||||
6
|
||||
|
@ -47,9 +51,25 @@ struct TestJmp : public Xbyak::CodeGenerator {
|
|||
30 00000193 E980000000 jmp near Y128
|
||||
31 00000198 <res 00000080> dummyY128 resb 128
|
||||
32 Y128:
|
||||
*/
|
||||
TestJmp(int offset, bool isBack, bool isShort)
|
||||
*/
|
||||
TestJmp(int offset, bool isBack, bool isShort, bool useNewLabel)
|
||||
{
|
||||
if (useNewLabel) {
|
||||
Label label;
|
||||
if (isBack) {
|
||||
L(label);
|
||||
putNop(this, offset);
|
||||
jmp(label);
|
||||
} else {
|
||||
if (isShort) {
|
||||
jmp(label);
|
||||
} else {
|
||||
jmp(label, T_NEAR);
|
||||
}
|
||||
putNop(this, offset);
|
||||
L(label);
|
||||
}
|
||||
} else {
|
||||
if (isBack) {
|
||||
L("@@");
|
||||
putNop(this, offset);
|
||||
|
@ -64,11 +84,8 @@ struct TestJmp : public Xbyak::CodeGenerator {
|
|||
L("@@");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void test1()
|
||||
{
|
||||
puts("test1");
|
||||
}
|
||||
};
|
||||
static const struct Tbl {
|
||||
int offset;
|
||||
bool isBack;
|
||||
|
@ -87,65 +104,47 @@ void test1()
|
|||
};
|
||||
for (size_t i = 0; i < NUM_OF_ARRAY(tbl); i++) {
|
||||
const Tbl *p = &tbl[i];
|
||||
TestJmp jmp(p->offset, p->isBack, p->isShort);
|
||||
for (int k = 0; k < 2; k++) {
|
||||
TestJmp jmp(p->offset, p->isBack, p->isShort, k == 0);
|
||||
const uint8 *q = (const uint8*)jmp.getCode();
|
||||
if (p->isBack) q += p->offset; /* skip nop */
|
||||
for (int j = 0; j < p->size; j++) {
|
||||
if (q[j] != p->result[j]) {
|
||||
printf("err (%d,%d) %02x assume=%02x\n", (int)i, j, q[j], p->result[j]);
|
||||
printf("err (%d, %d, %d) %02x assume=%02x\n", (int)i, k, j, q[j], p->result[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TestJmp2 : public CodeGenerator {
|
||||
/*
|
||||
1 00000000 90 nop
|
||||
2 00000001 90 nop
|
||||
3 f1:
|
||||
4 00000002 <res 0000007E> dummyX1 resb 126
|
||||
6 00000080 EB80 jmp f1
|
||||
7
|
||||
8 f2:
|
||||
9 00000082 <res 0000007F> dummyX2 resb 127
|
||||
11 00000101 E97CFFFFFF jmp f2
|
||||
12
|
||||
13
|
||||
14 00000106 EB7F jmp f3
|
||||
15 00000108 <res 0000007F> dummyX3 resb 127
|
||||
17 f3:
|
||||
18
|
||||
19 00000187 E980000000 jmp f4
|
||||
20 0000018C <res 00000080> dummyX4 resb 128
|
||||
22 f4:
|
||||
*/
|
||||
explicit TestJmp2(void *p)
|
||||
: Xbyak::CodeGenerator(8192, p)
|
||||
{
|
||||
inLocalLabel();
|
||||
nop();
|
||||
nop();
|
||||
L(".f1");
|
||||
putNop(this, 126);
|
||||
jmp(".f1");
|
||||
L(".f2");
|
||||
putNop(this, 127);
|
||||
jmp(".f2", T_NEAR);
|
||||
|
||||
jmp(".f3");
|
||||
putNop(this, 127);
|
||||
L(".f3");
|
||||
jmp(".f4", T_NEAR);
|
||||
putNop(this, 128);
|
||||
L(".f4");
|
||||
outLocalLabel();
|
||||
}
|
||||
};
|
||||
|
||||
struct TestJmpCx : public CodeGenerator {
|
||||
explicit TestJmpCx(void *p)
|
||||
void testJmpCx()
|
||||
{
|
||||
puts("testJmpCx");
|
||||
struct TestJmpCx : public CodeGenerator {
|
||||
explicit TestJmpCx(void *p, bool useNewLabel)
|
||||
: Xbyak::CodeGenerator(16, p)
|
||||
{
|
||||
if (useNewLabel) {
|
||||
Label lp;
|
||||
L(lp);
|
||||
#ifdef XBYAK64
|
||||
puts("TestJmpCx 64bit");
|
||||
/*
|
||||
67 E3 FD ; jecxz lp
|
||||
E3 FB ; jrcxz lp
|
||||
*/
|
||||
jecxz(lp);
|
||||
jrcxz(lp);
|
||||
#else
|
||||
puts("TestJmpCx 32bit");
|
||||
/*
|
||||
E3FE ; jecxz lp
|
||||
67E3FB ; jcxz lp
|
||||
*/
|
||||
jecxz(lp);
|
||||
jcxz(lp);
|
||||
#endif
|
||||
} else {
|
||||
inLocalLabel();
|
||||
L(".lp");
|
||||
#ifdef XBYAK64
|
||||
|
@ -167,11 +166,8 @@ struct TestJmpCx : public CodeGenerator {
|
|||
#endif
|
||||
outLocalLabel();
|
||||
}
|
||||
};
|
||||
|
||||
void testJmpCx()
|
||||
{
|
||||
puts("testJmpCx");
|
||||
}
|
||||
};
|
||||
const struct {
|
||||
const char *p;
|
||||
size_t len;
|
||||
|
@ -182,16 +178,18 @@ void testJmpCx()
|
|||
"\xe3\xfe\x67\xe3\xfb", 5
|
||||
#endif
|
||||
};
|
||||
for (int j = 0; j < 2; j++) {
|
||||
char buf[16] = {};
|
||||
TestJmpCx code(buf);
|
||||
TestJmpCx code(buf, j == 0);
|
||||
if (memcmp(buf, tbl.p, tbl.len) == 0) {
|
||||
} else {
|
||||
puts("ng");
|
||||
printf("err %d\n", j);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
printf("%02x ", (unsigned char)buf[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
@ -200,6 +198,50 @@ void testJmpCx()
|
|||
void test2()
|
||||
{
|
||||
puts("test2");
|
||||
struct TestJmp2 : public CodeGenerator {
|
||||
/*
|
||||
1 00000000 90 nop
|
||||
2 00000001 90 nop
|
||||
3 f1:
|
||||
4 00000002 <res 0000007E> dummyX1 resb 126
|
||||
6 00000080 EB80 jmp f1
|
||||
7
|
||||
8 f2:
|
||||
9 00000082 <res 0000007F> dummyX2 resb 127
|
||||
11 00000101 E97CFFFFFF jmp f2
|
||||
12
|
||||
13
|
||||
14 00000106 EB7F jmp f3
|
||||
15 00000108 <res 0000007F> dummyX3 resb 127
|
||||
17 f3:
|
||||
18
|
||||
19 00000187 E980000000 jmp f4
|
||||
20 0000018C <res 00000080> dummyX4 resb 128
|
||||
22 f4:
|
||||
*/
|
||||
explicit TestJmp2(void *p)
|
||||
: Xbyak::CodeGenerator(8192, p)
|
||||
{
|
||||
inLocalLabel();
|
||||
nop();
|
||||
nop();
|
||||
L(".f1");
|
||||
putNop(this, 126);
|
||||
jmp(".f1");
|
||||
L(".f2");
|
||||
putNop(this, 127);
|
||||
jmp(".f2", T_NEAR);
|
||||
|
||||
jmp(".f3");
|
||||
putNop(this, 127);
|
||||
L(".f3");
|
||||
jmp(".f4", T_NEAR);
|
||||
putNop(this, 128);
|
||||
L(".f4");
|
||||
outLocalLabel();
|
||||
}
|
||||
};
|
||||
|
||||
std::string ok;
|
||||
ok.resize(0x18C + 128, (char)0x90);
|
||||
ok[0x080] = (char)0xeb;
|
||||
|
@ -241,7 +283,10 @@ void test2()
|
|||
int add5(int x) { return x + 5; }
|
||||
int add2(int x) { return x + 2; }
|
||||
|
||||
struct Grow : Xbyak::CodeGenerator {
|
||||
void test3()
|
||||
{
|
||||
puts("test3");
|
||||
struct Grow : Xbyak::CodeGenerator {
|
||||
Grow(int dummySize)
|
||||
: Xbyak::CodeGenerator(128, Xbyak::AutoGrow)
|
||||
{
|
||||
|
@ -257,11 +302,7 @@ struct Grow : Xbyak::CodeGenerator {
|
|||
db(0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
void test3()
|
||||
{
|
||||
puts("test3");
|
||||
};
|
||||
for (int dummySize = 0; dummySize < 40000; dummySize += 10000) {
|
||||
printf("dummySize=%d\n", dummySize);
|
||||
Grow g(dummySize);
|
||||
|
@ -327,7 +368,10 @@ void diff(const std::string& a, const std::string& b)
|
|||
}
|
||||
}
|
||||
|
||||
struct Test4 : Xbyak::CodeGenerator {
|
||||
void test4()
|
||||
{
|
||||
puts("test4");
|
||||
struct Test4 : Xbyak::CodeGenerator {
|
||||
explicit Test4(int size, void *mode)
|
||||
: CodeGenerator(size, mode)
|
||||
{
|
||||
|
@ -341,10 +385,7 @@ struct Test4 : Xbyak::CodeGenerator {
|
|||
L(".x");
|
||||
ret();
|
||||
}
|
||||
};
|
||||
void test4()
|
||||
{
|
||||
puts("test4");
|
||||
};
|
||||
std::string fm, gm;
|
||||
Test4 fc(1024, 0);
|
||||
Test4 gc(5, Xbyak::AutoGrow);
|
||||
|
@ -356,7 +397,10 @@ void test4()
|
|||
diff(gm, gm);
|
||||
}
|
||||
|
||||
struct Test5 : Xbyak::CodeGenerator {
|
||||
void test5()
|
||||
{
|
||||
puts("test5");
|
||||
struct Test5 : Xbyak::CodeGenerator {
|
||||
explicit Test5(int size, int count, void *mode)
|
||||
: CodeGenerator(size, mode, &myAlloc)
|
||||
{
|
||||
|
@ -386,11 +430,7 @@ struct Test5 : Xbyak::CodeGenerator {
|
|||
ret();
|
||||
outLocalLabel();
|
||||
}
|
||||
};
|
||||
|
||||
void test5()
|
||||
{
|
||||
puts("test5");
|
||||
};
|
||||
std::string fm, gm;
|
||||
const int count = 50;
|
||||
int ret;
|
||||
|
@ -509,16 +549,19 @@ void testMovLabel(bool grow)
|
|||
#endif
|
||||
}
|
||||
|
||||
struct MovLabel2Code : Xbyak::CodeGenerator {
|
||||
void testMovLabel2()
|
||||
{
|
||||
puts("tsetMovLabel2");
|
||||
struct MovLabel2Code : Xbyak::CodeGenerator {
|
||||
MovLabel2Code()
|
||||
{
|
||||
#ifdef XBYAK64
|
||||
#ifdef XBYAK64
|
||||
const Reg64& a = rax;
|
||||
const Reg64& c = rcx;
|
||||
#else
|
||||
#else
|
||||
const Reg32& a = eax;
|
||||
const Reg32& c = ecx;
|
||||
#endif
|
||||
#endif
|
||||
xor(a, a);
|
||||
xor(c, c);
|
||||
jmp("in");
|
||||
|
@ -537,17 +580,15 @@ struct MovLabel2Code : Xbyak::CodeGenerator {
|
|||
add(a, 4);
|
||||
ret();
|
||||
}
|
||||
};
|
||||
|
||||
void testMovLabel2()
|
||||
{
|
||||
puts("tsetMovLabel2");
|
||||
};
|
||||
MovLabel2Code code;
|
||||
int ret = code.getCode<int (*)()>()();
|
||||
if (ret != 7) printf("ERR=%d\n", ret);
|
||||
}
|
||||
|
||||
struct TestLocal : public Xbyak::CodeGenerator {
|
||||
void test6()
|
||||
{
|
||||
struct TestLocal : public Xbyak::CodeGenerator {
|
||||
TestLocal(bool grow)
|
||||
: Xbyak::CodeGenerator(grow ? 128 : 4096, grow ? Xbyak::AutoGrow : 0)
|
||||
{
|
||||
|
@ -610,10 +651,8 @@ struct TestLocal : public Xbyak::CodeGenerator {
|
|||
inc(eax); // 15
|
||||
ret();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
void test6()
|
||||
{
|
||||
for (int i = 0; i < 2; i++) {
|
||||
const bool grow = i == 1;
|
||||
printf("test6 grow=%d\n", i);
|
||||
|
@ -695,7 +734,6 @@ void testNewLabel()
|
|||
|
||||
int main()
|
||||
try {
|
||||
#if 0
|
||||
test1();
|
||||
test2();
|
||||
#ifdef ONLY_32BIT
|
||||
|
@ -708,7 +746,6 @@ try {
|
|||
testMovLabel(false);
|
||||
testMovLabel(true);
|
||||
testMovLabel2();
|
||||
#endif
|
||||
testNewLabel();
|
||||
} catch (std::exception& e) {
|
||||
printf("ERR:%s\n", e.what());
|
||||
|
|
|
@ -304,10 +304,14 @@ void jg(Label& label, LabelType type = T_AUTO) { opJmp(label, type, 0x7F, 0x8F,
|
|||
void setg(const Operand& op) { opR_ModM(op, 8, 0, 0x0F, B10010000 | 15); }
|
||||
#ifdef XBYAK32
|
||||
void jcxz(const std::string& label) { db(0x67); opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
void jcxz(Label& label) { db(0x67); opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
void jecxz(const std::string& label) { opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
void jecxz(Label& label) { opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
#else
|
||||
void jecxz(const std::string& label) { db(0x67); opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
void jecxz(Label& label) { db(0x67); opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
void jrcxz(const std::string& label) { opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
void jrcxz(Label& label) { opJmp(label, T_SHORT, 0xe3, 0, 0); }
|
||||
#endif
|
||||
#ifdef XBYAK64
|
||||
void cdqe() { db(0x48); db(0x98); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue