add Label::getAddress()

This commit is contained in:
MITSUNARI Shigeo 2016-12-14 11:30:05 +09:00
parent 44f83e6183
commit 86759a3bc8
2 changed files with 77 additions and 0 deletions

View File

@ -962,6 +962,74 @@ CYBOZU_TEST_AUTO(doubleDefine)
} code;
}
struct GetAddressCode1 : Xbyak::CodeGenerator {
void test()
{
Xbyak::Label L1, L2, L3;
nop();
L(L1);
const uint8_t *p1 = getCurr();
CYBOZU_TEST_EQUAL_POINTER(L1.getAddress(), p1);
nop();
jmp(L2);
nop();
jmp(L3);
L(L2);
CYBOZU_TEST_EQUAL_POINTER(L2.getAddress(), getCurr());
// L3 is not defined
CYBOZU_TEST_EQUAL_POINTER(L3.getAddress(), 0);
// L3 is set by L1
assignL(L3, L1);
CYBOZU_TEST_EQUAL_POINTER(L3.getAddress(), p1);
}
};
CYBOZU_TEST_AUTO(getAddress1)
{
GetAddressCode1 c;
c.test();
}
struct GetAddressCode2 : Xbyak::CodeGenerator {
Xbyak::Label L1, L2, L3;
size_t a1;
size_t a3;
explicit GetAddressCode2(int size)
: Xbyak::CodeGenerator(size, size == 4096 ? 0 : Xbyak::AutoGrow)
, a1(0)
, a3(0)
{
nop();
L(L1);
a1 = getSize();
nop();
jmp(L2);
L(L3);
a3 = getSize();
nop();
assignL(L2, L1);
}
};
CYBOZU_TEST_AUTO(getAddress2)
{
const int sizeTbl[] = {
2, 128, // grow
4096 // not grow
};
for (size_t i = 0; i < CYBOZU_NUM_OF_ARRAY(sizeTbl); i++) {
int size = sizeTbl[i];
GetAddressCode2 c(size);
c.ready();
const uint8_t *p = c.getCode();
CYBOZU_TEST_EQUAL(c.L1.getAddress(), p + c.a1);
CYBOZU_TEST_EQUAL(c.L3.getAddress(), p + c.a3);
CYBOZU_TEST_EQUAL(c.L2.getAddress(), p + c.a1);
}
}
#ifdef XBYAK64
CYBOZU_TEST_AUTO(rip)
{

View File

@ -1048,6 +1048,7 @@ public:
Label& operator=(const Label& rhs);
~Label();
int getId() const { return id; }
const uint8 *getAddress() const;
// backward compatibility
static inline std::string toStr(int num)
@ -1256,6 +1257,7 @@ public:
return false;
}
bool hasUndefClabel() const { return hasUndefinedLabel_inner(clabelUndefList_); }
const uint8 *getCode() const { return base_->getCode(); }
};
inline Label::Label(const Label& rhs)
@ -1276,6 +1278,13 @@ inline Label::~Label()
{
if (id && mgr) mgr->decRefCount(id);
}
inline const uint8* Label::getAddress() const
{
if (mgr == 0) return 0;
size_t offset;
if (!mgr->getOffset(&offset, *this)) return 0;
return mgr->getCode() + offset;
}
class CodeGenerator : public CodeArray {
public: