Moved addFunc() from X86Compiler to Compiler.

Fixed CompilerContext not being reset after the first function is translated.
This commit is contained in:
kobalicek
2016-03-28 23:18:41 +02:00
parent 7b0e362ac6
commit 95aacf9fd1
8 changed files with 87 additions and 16 deletions

View File

@@ -165,6 +165,21 @@ HLHint* Compiler::newHintNode(Var& var, uint32_t hint, uint32_t value) noexcept
// [asmjit::Compiler - Code-Stream]
// ============================================================================
HLNode* Compiler::addFunc(HLFunc* func) noexcept {
ASMJIT_ASSERT(_func == nullptr);
_func = func;
addNode(func); // Add function node.
addNode(func->getEntryNode()); // Add function entry.
HLNode* cursor = getCursor();
addNode(func->getExitNode()); // Add function exit / epilog marker.
addNode(func->getEnd()); // Add function end.
setCursor(cursor);
return func;
}
HLNode* Compiler::addNode(HLNode* node) noexcept {
ASMJIT_ASSERT(node != nullptr);
ASMJIT_ASSERT(node->_prev == nullptr);

View File

@@ -291,6 +291,9 @@ struct ASMJIT_VIRTAPI Compiler : public ExternalTool {
// [Code-Stream]
// --------------------------------------------------------------------------
//! Add a function `node` to the stream.
ASMJIT_API HLNode* addFunc(HLFunc* func) noexcept;
//! Add node `node` after current and set current to `node`.
ASMJIT_API HLNode* addNode(HLNode* node) noexcept;
//! Insert `node` before `ref`.

View File

@@ -3293,7 +3293,7 @@ struct ASMJIT_VIRTAPI X86Assembler : public Assembler {
//! Packed DWORD unsigned maximum (SSE4.1).
INST_2x(pmaxud, kX86InstIdPmaxud, X86XmmReg, X86XmmReg)
//! \overload
INST_2x(pmaxud,kX86InstIdPmaxud , X86XmmReg, X86Mem)
INST_2x(pmaxud, kX86InstIdPmaxud, X86XmmReg, X86Mem)
//! Packed WORD unsigned maximum (SSE4.1).
INST_2x(pmaxuw, kX86InstIdPmaxuw, X86XmmReg, X86XmmReg)

View File

@@ -292,6 +292,7 @@ Error X86Compiler::finalize() noexcept {
error = context.serialize(assembler, start, node);
context.cleanup();
context.reset(false);
if (error != kErrorOk)
break;
@@ -642,18 +643,7 @@ X86FuncNode* X86Compiler::addFunc(const FuncPrototype& p) noexcept {
return nullptr;
}
ASMJIT_ASSERT(_func == nullptr);
_func = func;
addNode(func); // Add function node.
addNode(func->getEntryNode()); // Add function entry.
HLNode* cursor = getCursor();
addNode(func->getExitNode()); // Add function exit / epilog marker.
addNode(func->getEnd()); // Add function end.
setCursor(cursor);
return func;
return static_cast<X86FuncNode*>(addFunc(func));
}
HLSentinel* X86Compiler::endFunc() noexcept {

View File

@@ -856,6 +856,8 @@ struct ASMJIT_VIRTAPI X86Compiler : public Compiler {
//! Create a new `X86FuncNode`.
ASMJIT_API X86FuncNode* newFunc(const FuncPrototype& p) noexcept;
using Compiler::addFunc;
//! Add a new function.
//!
//! \param p Function prototype.

View File

@@ -270,7 +270,6 @@ X86Context::X86Context(X86Compiler* compiler) : Context(compiler) {
_state = &_x86State;
reset();
}
X86Context::~X86Context() {}
// ============================================================================

View File

@@ -280,7 +280,7 @@ struct X86Context : public Context {
// [Reset]
// --------------------------------------------------------------------------
virtual void reset(bool releaseMemory = false);
virtual void reset(bool releaseMemory = false) override;
// --------------------------------------------------------------------------
// [Arch]

View File

@@ -2804,6 +2804,67 @@ struct X86Test_MiscMultiRet : public X86Test {
}
};
// ============================================================================
// [X86Test_MiscMultiFunc]
// ============================================================================
struct X86Test_MiscMultiFunc : public X86Test {
X86Test_MiscMultiFunc() : X86Test("[Misc] MultiFunc") {}
static void add(PodVector<X86Test*>& tests) {
tests.append(new X86Test_MiscMultiFunc());
}
virtual void compile(X86Compiler& c) {
X86FuncNode* f1 = c.newFunc(FuncBuilder2<int, int, int>(kCallConvHost));
X86FuncNode* f2 = c.newFunc(FuncBuilder2<int, int, int>(kCallConvHost));
{
X86GpVar a = c.newInt32("a");
X86GpVar b = c.newInt32("b");
c.addFunc(f1);
c.setArg(0, a);
c.setArg(1, b);
X86CallNode* call = c.call(f2->getEntryLabel(), FuncBuilder2<int, int, int>(kCallConvHost));
call->setArg(0, a);
call->setArg(1, b);
call->setRet(0, a);
c.ret(a);
c.endFunc();
}
{
X86GpVar a = c.newInt32("a");
X86GpVar b = c.newInt32("b");
c.addFunc(f2);
c.setArg(0, a);
c.setArg(1, b);
c.add(a, b);
c.ret(a);
c.endFunc();
}
}
virtual bool run(void* _func, StringBuilder& result, StringBuilder& expect) {
typedef int (*Func)(int, int);
Func func = asmjit_cast<Func>(_func);
int resultRet = func(56, 22);
int expectRet = 56 + 22;
result.setFormat("ret=%d", resultRet);
expect.setFormat("ret=%d", expectRet);
return result.eq(expect);
}
};
// ============================================================================
// [X86Test_MiscUnfollow]
// ============================================================================
@@ -2959,8 +3020,9 @@ X86TestSuite::X86TestSuite() :
// Misc.
ADD_TEST(X86Test_MiscConstPool);
ADD_TEST(X86Test_MiscUnfollow);
ADD_TEST(X86Test_MiscMultiRet);
ADD_TEST(X86Test_MiscMultiFunc);
ADD_TEST(X86Test_MiscUnfollow);
}
X86TestSuite::~X86TestSuite() {