mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 19:30:44 +00:00
Staring with Stack helper
This commit is contained in:
parent
f124be65b7
commit
dfa141a971
2 changed files with 66 additions and 3 deletions
|
|
@ -14,6 +14,7 @@ struct
|
|||
llvm::Type* word256ptr;
|
||||
llvm::Type* word256arr;
|
||||
llvm::Type* size;
|
||||
llvm::Type* Void;
|
||||
} Types;
|
||||
|
||||
Compiler::Compiler()
|
||||
|
|
@ -25,6 +26,7 @@ Compiler::Compiler()
|
|||
Types.word256ptr = Types.word256->getPointerTo();
|
||||
Types.word256arr = llvm::ArrayType::get(Types.word256, 100);
|
||||
Types.size = llvm::Type::getInt64Ty(context);
|
||||
Types.Void = llvm::Type::getVoidTy(context);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -44,7 +46,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
|||
auto memSize = new GlobalVariable(*module, Types.size, false,
|
||||
GlobalValue::LinkageTypes::PrivateLinkage,
|
||||
ConstantInt::get(Types.size, 0), "memsize");
|
||||
auto stack = new GlobalVariable(*module, Types.word256arr, false,
|
||||
auto stack2 = new GlobalVariable(*module, Types.word256arr, false,
|
||||
GlobalValue::LinkageTypes::PrivateLinkage,
|
||||
ConstantAggregateZero::get(Types.word256arr), "stack");
|
||||
auto stackTop = new GlobalVariable(*module, Types.size, false,
|
||||
|
|
@ -52,10 +54,16 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
|||
ConstantInt::get(Types.size, 0), "stackTop");
|
||||
|
||||
// Create value for void* malloc(size_t)
|
||||
std::vector<Type*> mallocArgTypes = { Types.size };
|
||||
Value* mallocVal = Function::Create(FunctionType::get(Types.word8ptr, mallocArgTypes, false),
|
||||
auto mallocVal = Function::Create(FunctionType::get(Types.word8ptr, { Types.size }, false),
|
||||
GlobalValue::LinkageTypes::ExternalLinkage, "malloc", module.get());
|
||||
|
||||
// Create stack_create declaration
|
||||
auto stackCreate = Function::Create(FunctionType::get(Types.word8ptr, false),
|
||||
GlobalValue::LinkageTypes::ExternalLinkage, "evmccrt_stack_create", module.get());
|
||||
|
||||
auto stackPush = Function::Create(FunctionType::get(Types.Void, std::vector<Type*>{ Types.word8ptr, Types.word256 }, false),
|
||||
GlobalValue::LinkageTypes::ExternalLinkage, "evmccrt_stack_push", module.get());
|
||||
|
||||
// Create main function
|
||||
FunctionType* funcType = FunctionType::get(llvm::Type::getInt32Ty(context), false);
|
||||
Function* mainFunc = Function::Create(funcType, Function::ExternalLinkage, "main", module.get());
|
||||
|
|
@ -69,6 +77,15 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
|||
builder.CreateStore(mallocMemCall, memory);
|
||||
builder.CreateStore(ConstantInt::get(Types.size, 100), memSize);
|
||||
|
||||
auto stack = builder.CreateCall(stackCreate, "stack");
|
||||
|
||||
uint64_t words[] = { 1, 2, 3, 4 };
|
||||
auto val = llvm::APInt(256, 4, words);
|
||||
auto c = ConstantInt::get(Types.word256, val);
|
||||
|
||||
Value* args[] = { stack, c };
|
||||
builder.CreateCall(stackPush, args);
|
||||
|
||||
/*
|
||||
std::vector<Value*> mallocStackArgs = { ConstantInt::get(sizeTy, 200) };
|
||||
auto mallocStackCall = builder.CreateCall(mallocVal, mallocStackArgs, "malloc_stack");
|
||||
|
|
|
|||
46
evmcc/Stack.cpp
Normal file
46
evmcc/Stack.cpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <cstdint>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define EXPORT
|
||||
#endif
|
||||
|
||||
struct i256
|
||||
{
|
||||
uint64_t a;
|
||||
uint64_t b;
|
||||
uint64_t c;
|
||||
uint64_t d;
|
||||
};
|
||||
|
||||
using Stack = std::vector<i256>;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
EXPORT void* evmccrt_stack_create()
|
||||
{
|
||||
std::cerr << "STACK create: ";
|
||||
auto stack = new Stack;
|
||||
std::cerr << stack << "\n";
|
||||
return stack;
|
||||
}
|
||||
|
||||
EXPORT void evmccrt_stack_push(void* _stack, uint64_t _partA, uint64_t _partB, uint64_t _partC, uint64_t _partD)
|
||||
{
|
||||
std::cerr << "STACK push: " << _partA << " (" << std::hex << std::setfill('0')
|
||||
<< std::setw(16) << _partD << " "
|
||||
<< std::setw(16) << _partC << " "
|
||||
<< std::setw(16) << _partB << " "
|
||||
<< std::setw(16) << _partA;
|
||||
auto stack = static_cast<Stack*>(_stack);
|
||||
stack->push_back({_partA, _partB, _partC, _partD});
|
||||
std::cerr << ")\n";
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
Loading…
Reference in a new issue