-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlift.cc
58 lines (47 loc) · 1.29 KB
/
lift.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "evmjit/libevmjit/Compiler.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <llvm/IR/Module.h>
#include <llvm/Support/raw_os_ostream.h>
using namespace std;
using namespace dev::eth::jit;
struct Code {
string name;
byte* code;
ssize_t code_size;
static Code load(const char* fname) {
ifstream f(fname);
stringstream buf;
buf << f.rdbuf();
string contents = buf.str();
Code res;
res.name = "foo";
res.code_size = buf.str().length()/2;
res.code = new byte[res.code_size];
char strbuf[3] = {0,0,0};
for (ssize_t i = 0; i < res.code_size; i++) {
strbuf[0] = contents[i*2];
strbuf[1] = contents[i*2+1];
res.code[i] = strtol(strbuf, NULL, 16);
}
return res;
}
};
unique_ptr<llvm::Module> compile(llvm::LLVMContext &ctx, const Code& evm_code) {
evmc_revision rev;
bool staticCall = false;
Compiler c({}, rev, staticCall, ctx);
return c.compile(evm_code.code, evm_code.code + evm_code.code_size, evm_code.name);
}
int main(int argc, char** argv) {
llvm::LLVMContext ctx;
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " file.bin" << endl;
return -1;
}
auto c = Code::load(argv[1]);
auto m = compile(ctx, c);
llvm::raw_os_ostream lcout{cout};
m->print(lcout, nullptr);
}