Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 750793d

Browse files
committed
make evm2wasm/evm2wast accept a vector of bytes instead of string, add line breaks (where appropriate) to wast
1 parent 380e19a commit 750793d

File tree

3 files changed

+86
-57
lines changed

3 files changed

+86
-57
lines changed

include/evm2wasm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,9 @@ struct WastCode
351351
std::string imports;
352352
};
353353

354-
std::string evm2wasm(const std::string& input);
354+
std::string evm2wasm(const std::vector<char>& input);
355355

356-
std::string evm2wast(const std::string& input, bool stackTrace = false, bool useAsyncAPI = false,
356+
std::string evm2wast(const std::vector<char>& input, bool stackTrace = false, bool useAsyncAPI = false,
357357
bool inlineOps = true);
358358

359359
std::string assembleSegments(const std::vector<JumpSegment>& segments);
@@ -362,7 +362,7 @@ std::string opcodeToString(opcodeEnum opcode);
362362

363363
Op opcodes(int op);
364364

365-
size_t findNextJumpDest(const std::string& evmCode, size_t i);
365+
size_t findNextJumpDest(const std::vector<char>& evmCode, size_t i);
366366

367367
std::set<opcodeEnum> resolveFunctionDeps(const std::set<opcodeEnum>& funcSet);
368368

libs/evm2wasm/evm2wasm.cpp

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,19 @@ string wast2wasm(const string& input, bool debug = false)
7171
return output.str();
7272
}
7373

74-
string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool inlineOps)
74+
std::string HexToBytes(const std::string& hex) {
75+
std::vector<char> bytes;
76+
77+
for (unsigned int i = 0; i < hex.length(); i += 2) {
78+
std::string byteString = hex.substr(i, 2);
79+
char byte = (char) strtol(byteString.c_str(), NULL, 16);
80+
bytes.push_back(byte);
81+
}
82+
83+
return std::string(bytes.begin(), bytes.end());
84+
}
85+
86+
string evm2wast(const std::vector<char>& evmCode, bool stackTrace, bool useAsyncAPI, bool inlineOps)
7587
{
7688
// FIXME: do evm magic here
7789
// this keep track of the opcode we have found so far. This will be used to
@@ -136,12 +148,12 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
136148
addMetering();
137149
};
138150

139-
for (size_t pc = 0; pc < evmCode.length(); pc++)
151+
for (size_t pc = 0; pc < evmCode.size(); pc++)
140152
{
141153
auto opint = evmCode[pc];
142154
auto op = opcodes(opint);
143155

144-
std::string bytes;
156+
std::vector<char> bytes;
145157
gasCount += op.fee;
146158

147159
segmentStackDeta += op.on;
@@ -219,7 +231,7 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
219231
{
220232
pc++;
221233
size_t sliceSize = std::min(op.number, 32ul);
222-
bytes = evmCode.substr(pc, pc + sliceSize);
234+
bytes = std::vector<char>(evmCode.begin() + pc, evmCode.begin() + pc + sliceSize);
223235
pc += op.number;
224236
if (op.number < 32)
225237
{
@@ -239,7 +251,9 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
239251

240252
for (; q < 4; q++)
241253
{
242-
auto int64 = reinterpret_cast<int64_t>(bytes.substr(q * 8, q * 8 + 8).c_str());
254+
//auto int64 = reinterpret_cast<int64_t>(bytes.substr(q * 8, q * 8 + 8).c_str());
255+
auto int64 = reinterpret_cast<int64_t>(&bytes[q*8]);
256+
243257
push << "(i64.const {int64})"_format("int64"_a = int64);
244258
}
245259

@@ -259,7 +273,7 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
259273
else
260274
{
261275
// the rest is dead code;
262-
pc = evmCode.length();
276+
pc = evmCode.size();
263277
}
264278
break;
265279
case opcodeEnum::SELFDESTRUCT:
@@ -272,7 +286,7 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
272286
else
273287
{
274288
// the rest is dead code
275-
pc = evmCode.length();
289+
pc = evmCode.size();
276290
}
277291
break;
278292
case opcodeEnum::INVALID:
@@ -394,9 +408,10 @@ std::string assembleSegments(const std::vector<JumpSegment>& segments)
394408
return result;
395409
}
396410

397-
string evm2wasm(const string& input)
411+
string evm2wasm(const std::vector<char>& input)
398412
{
399-
return wast2wasm(evm2wast(input));
413+
std::string wast = evm2wast(input);
414+
return wast2wasm(wast, true);
400415
}
401416

402417
std::string opcodeToString(opcodeEnum opcode)
@@ -583,9 +598,9 @@ Op opcodes(int op)
583598
// @param {Array} evmCode
584599
// @param {Integer} index
585600
// @return {Integer}
586-
size_t findNextJumpDest(const std::string& evmCode, size_t i)
601+
size_t findNextJumpDest(const std::vector<char>& evmCode, size_t i)
587602
{
588-
for (; i < evmCode.length(); i++)
603+
for (; i < evmCode.size(); i++)
589604
{
590605
auto opint = evmCode[i];
591606
auto op = opcodes(opint);
@@ -667,10 +682,10 @@ std::string buildModule(const std::vector<std::string>& funcs,
667682
callbacksBuf << " " << callbacks[i];
668683
}
669684
callbackTableBuf << "\
670-
(table\
671-
(export \"callback\") ;; name of table\
672-
anyfunc\
673-
(elem {callbacksStr}) ;; elements will have indexes in order\
685+
(table\n\
686+
(export \"callback\") ;; name of table\n\
687+
anyfunc\n\
688+
(elem {callbacksStr}) ;; elements will have indexes in order\n\
674689
)"_format("callbacksStr"_a = callbacksBuf.str());
675690
}
676691

@@ -685,26 +700,26 @@ std::string buildModule(const std::vector<std::string>& funcs,
685700
}
686701

687702
return "\
688-
(module\
689-
{importsStr}\
690-
(global $cb_dest (mut i32) (i32.const 0))\
691-
(global $sp (mut i32) (i32.const -32))\
692-
(global $init (mut i32) (i32.const 0))\
693-
\
694-
;; memory related global\
695-
(global $memstart i32 (i32.const 33832))\
696-
;; the number of 256 words stored in memory\
697-
(global $wordCount (mut i64) (i64.const 0))\
698-
;; what was charged for the last memory allocation\
699-
(global $prevMemCost (mut i64) (i64.const 0))\
700-
\
701-
;; TODO: memory should only be 1, but can\'t resize right now\
702-
(memory 500)\
703-
(export \"memory\" (memory 0))\
704-
\
705-
{callbackTableStr}\
706-
\
707-
{funcStr}\
703+
(module\n\
704+
{importsStr}\n\
705+
(global $cb_dest (mut i32) (i32.const 0))\n\
706+
(global $sp (mut i32) (i32.const -32))\n\
707+
(global $init (mut i32) (i32.const 0))\n\
708+
\n\
709+
;; memory related global\n\
710+
(global $memstart i32 (i32.const 33832))\n\
711+
;; the number of 256 words stored in memory\n\
712+
(global $wordCount (mut i64) (i64.const 0))\n\
713+
;; what was charged for the last memory allocation\n\
714+
(global $prevMemCost (mut i64) (i64.const 0))\n\
715+
\n\
716+
;; TODO: memory should only be 1, but can\'t resize right now\n\
717+
(memory 500)\n\
718+
(export \"memory\" (memory 0))\n\
719+
\n\
720+
{callbackTableStr}\n\
721+
\n\
722+
{funcStr}\n\
708723
)"_format("importsStr"_a = importsBuf.str(), "callbackTableStr"_a = callbackTableBuf.str(),
709724
"funcStr"_a = funcBuf.str());
710725
}
@@ -737,23 +752,23 @@ std::string buildJumpMap(const std::vector<JumpSegment>& segments)
737752
std::string wasmStr = wasmBuf.str();
738753
wasmBuf.clear();
739754
wasmBuf << "\
740-
(block $0\
741-
(if\
742-
(i32.eqz (get_global $init))\
743-
(then\
744-
(set_global $init (i32.const 1))\
745-
(br $0))\
746-
(else\
747-
;; the callback dest can never be in the first block\
748-
(if (i32.eq (get_global $cb_dest) (i32.const 0)) \
749-
(then\
750-
{wasm}\
751-
)\
752-
(else \
753-
;; return callback destination and zero out $cb_dest \
754-
get_global $cb_dest\
755-
(set_global $cb_dest (i32.const 0))\
756-
(br_table $0 {brTable})\
755+
(block $0\n\
756+
(if\n\
757+
(i32.eqz (get_global $init))\n\
758+
(then\n\
759+
(set_global $init (i32.const 1))\n\
760+
(br $0))\n\
761+
(else\n\
762+
;; the callback dest can never be in the first block\n\
763+
(if (i32.eq (get_global $cb_dest) (i32.const 0)) \n\
764+
(then\n\
765+
{wasm}\n\
766+
)\n\
767+
(else \n\
768+
;; return callback destination and zero out $cb_dest \n\
769+
get_global $cb_dest\n\
770+
(set_global $cb_dest (i32.const 0))\n\
771+
(br_table $0 {brTable})\n\
757772
)))))"_format("wasm"_a = wasmStr, "brTable"_a = brTableBuf.str());
758773

759774
return wasmBuf.str();

tools/evm2wasm/main.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
using namespace std;
99

10+
std::vector<char> HexToBytes(const std::string& hex) {
11+
std::vector<char> bytes;
12+
13+
for (unsigned int i = 0; i < hex.length()-1; i += 2) {
14+
std::string byteString = hex.substr(i, 2);
15+
char byte = (char) strtol(byteString.c_str(), NULL, 16);
16+
bytes.push_back(byte);
17+
}
18+
19+
return bytes;
20+
}
21+
1022
int main(int argc, char **argv) {
1123
if (argc < 2) {
1224
cerr << "Usage: " << argv[0] << " <EVM file> [--wast]" << endl;
@@ -33,10 +45,12 @@ int main(int argc, char **argv) {
3345
std::istreambuf_iterator<char>()
3446
);
3547

48+
std::vector<char> evmCode = HexToBytes(input_str);
49+
3650
if(wast) {
37-
cout << evm2wasm::evm2wast(input_str) << endl;
51+
cout << evm2wasm::evm2wast(evmCode) << endl;
3852
} else {
39-
cout << evm2wasm::evm2wasm(input_str) << endl;
53+
cout << evm2wasm::evm2wasm(evmCode) << endl;
4054
}
4155

4256
return 0;

0 commit comments

Comments
 (0)