diff --git a/sample/test0.cpp b/sample/test0.cpp index 852b8c6..91288bd 100644 --- a/sample/test0.cpp +++ b/sample/test0.cpp @@ -94,6 +94,37 @@ public: int (*get() const)(const char *) { return (int (*)(const char *))(const void*)getCode(); } }; +struct Reset : public Xbyak::CodeGenerator { + void init(int n) + { + xor(eax, eax); + mov(ecx, n); + test(ecx, ecx); + jnz("@f"); + ret(); + L("@@"); + for (int i = 0; i < 10 - n; i++) { + add(eax, ecx); + } + sub(ecx, 1); + jnz("@b"); + ret(); + } +}; + +void testReset() +{ + puts("tsetReset"); + Reset code; + int (*f)(int) = (int (*)(int))code.getCode(); + for (int i = 0; i < 10; i++) { + code.init(i); + int v = f(i); + printf("%d %d\n", i, v); + code.reset(); + } +} + int main() { try { @@ -137,6 +168,7 @@ int main() CodeArray::protect(p, codeSize, false); } puts("OK"); + testReset(); } catch (Xbyak::Error err) { printf("ERR:%s(%d)\n", Xbyak::ConvertErrorToString(err), err); } catch (...) { diff --git a/xbyak/xbyak.h b/xbyak/xbyak.h index b6ac8b3..cd5959f 100644 --- a/xbyak/xbyak.h +++ b/xbyak/xbyak.h @@ -542,6 +542,11 @@ public: if (type_ != FIXED_BUF) throw ERR_CODE_ISNOT_COPYABLE; for (size_t i = 0; i < size_; i++) top_[i] = rhs.top_[i]; } + void resetSize() + { + size_ = 0; + addrInfoList_.clear(); + } void db(int code) { if (size_ >= maxSize_) { @@ -795,6 +800,16 @@ public: , localCount_(0) { } + void reset() + { + base_ = 0; + anonymousCount_ = 0; + stackPos_ = 1; + usedCount_ = 0; + localCount_ = 0; + definedList_.clear(); + undefinedList_.clear(); + } void enterLocal() { if (stackPos_ == maxStack) throw ERR_OVER_LOCAL_LABEL; @@ -1603,6 +1618,12 @@ public: { label_.set(this); } + void reset() + { + resetSize(); + label_.reset(); + label_.set(this); + } bool hasUndefinedLabel() const { return label_.hasUndefinedLabel(); } /* call ready() to complete generating code on AutoGrow