Browse Source

Export runtime data to global variable in main function

cl-refactor
Paweł Bylica 10 years ago
parent
commit
c87717aa78
  1. 1
      libevmjit/Compiler.cpp
  2. 8
      libevmjit/CompilerHelper.cpp
  3. 3
      libevmjit/CompilerHelper.h
  4. 28
      libevmjit/Runtime.cpp
  5. 12
      libevmjit/Runtime.h

1
libevmjit/Compiler.cpp

@ -175,6 +175,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytesConstRef bytecode)
createBasicBlocks(bytecode);
// Init runtime structures.
RuntimeManager runtimeManager(m_builder);
GasMeter gasMeter(m_builder);
Memory memory(m_builder, gasMeter);
Ext ext(m_builder);

8
libevmjit/CompilerHelper.cpp

@ -21,6 +21,14 @@ llvm::Module* CompilerHelper::getModule()
return m_builder.GetInsertBlock()->getParent()->getParent();
}
llvm::Function* CompilerHelper::getMainFunction()
{
assert(m_builder.GetInsertBlock());
auto mainFunc = m_builder.GetInsertBlock()->getParent();
assert(mainFunc && mainFunc->getName() == "main");
return mainFunc;
}
}
}
}

3
libevmjit/CompilerHelper.h

@ -23,6 +23,9 @@ protected:
/// Reference to the IR module being compiled
llvm::Module* getModule();
/// Reference to the main module function
llvm::Function* getMainFunction();
/// Reference to parent compiler IR builder
llvm::IRBuilder<>& m_builder;
};

28
libevmjit/Runtime.cpp

@ -1,6 +1,9 @@
#include "Runtime.h"
#include <llvm/IR/GlobalVariable.h>
#include <llvm/IR/Function.h>
#include <libevm/VM.h>
#include "Type.h"
@ -14,11 +17,16 @@ namespace jit
llvm::StructType* RuntimeData::getType()
{
llvm::Type* elems[] =
static llvm::StructType* type = nullptr;
if (!type)
{
Type::i256,
};
return llvm::StructType::create(elems, "RuntimeData");
llvm::Type* elems[] =
{
Type::i256,
};
type = llvm::StructType::create(elems, "RuntimeData");
}
return type;
}
static Runtime* g_runtime;
@ -61,6 +69,18 @@ u256 Runtime::getGas()
return llvm2eth(gas);
}
RuntimeManager::RuntimeManager(llvm::IRBuilder<>& _builder): CompilerHelper(_builder)
{
auto dataPtrType = RuntimeData::getType()->getPointerTo();
m_dataPtr = new llvm::GlobalVariable(*getModule(), dataPtrType, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(dataPtrType), "rt");
// Export data
auto mainFunc = getMainFunction();
llvm::Value* dataPtr = &mainFunc->getArgumentList().back();
m_builder.CreateStore(dataPtr, m_dataPtr);
}
}
}
}

12
libevmjit/Runtime.h

@ -5,6 +5,7 @@
#include <libevm/ExtVMFace.h>
#include "CompilerHelper.h"
#include "Utils.h"
@ -56,6 +57,17 @@ private:
ExtVMFace& m_ext;
};
class RuntimeManager: public CompilerHelper
{
public:
RuntimeManager(llvm::IRBuilder<>& _builder);
llvm::Value* getGas();
private:
llvm::GlobalVariable* m_dataPtr;
};
}
}
}

Loading…
Cancel
Save