@@ -71,7 +71,19 @@ string wast2wasm(const string& input, bool debug = false)
71
71
return output.str ();
72
72
}
73
73
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)
75
87
{
76
88
// FIXME: do evm magic here
77
89
// 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
136
148
addMetering ();
137
149
};
138
150
139
- for (size_t pc = 0 ; pc < evmCode.length (); pc++)
151
+ for (size_t pc = 0 ; pc < evmCode.size (); pc++)
140
152
{
141
153
auto opint = evmCode[pc];
142
154
auto op = opcodes (opint);
143
155
144
- std::string bytes;
156
+ std::vector< char > bytes;
145
157
gasCount += op.fee ;
146
158
147
159
segmentStackDeta += op.on ;
@@ -219,7 +231,7 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
219
231
{
220
232
pc++;
221
233
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);
223
235
pc += op.number ;
224
236
if (op.number < 32 )
225
237
{
@@ -239,7 +251,9 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
239
251
240
252
for (; q < 4 ; q++)
241
253
{
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
+
243
257
push << " (i64.const {int64})" _format (" int64" _a = int64);
244
258
}
245
259
@@ -259,7 +273,7 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
259
273
else
260
274
{
261
275
// the rest is dead code;
262
- pc = evmCode.length ();
276
+ pc = evmCode.size ();
263
277
}
264
278
break ;
265
279
case opcodeEnum::SELFDESTRUCT:
@@ -272,7 +286,7 @@ string evm2wast(const string& evmCode, bool stackTrace, bool useAsyncAPI, bool i
272
286
else
273
287
{
274
288
// the rest is dead code
275
- pc = evmCode.length ();
289
+ pc = evmCode.size ();
276
290
}
277
291
break ;
278
292
case opcodeEnum::INVALID:
@@ -394,9 +408,10 @@ std::string assembleSegments(const std::vector<JumpSegment>& segments)
394
408
return result;
395
409
}
396
410
397
- string evm2wasm (const string & input)
411
+ string evm2wasm (const std::vector< char > & input)
398
412
{
399
- return wast2wasm (evm2wast (input));
413
+ std::string wast = evm2wast (input);
414
+ return wast2wasm (wast, true );
400
415
}
401
416
402
417
std::string opcodeToString (opcodeEnum opcode)
@@ -583,9 +598,9 @@ Op opcodes(int op)
583
598
// @param {Array} evmCode
584
599
// @param {Integer} index
585
600
// @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)
587
602
{
588
- for (; i < evmCode.length (); i++)
603
+ for (; i < evmCode.size (); i++)
589
604
{
590
605
auto opint = evmCode[i];
591
606
auto op = opcodes (opint);
@@ -667,10 +682,10 @@ std::string buildModule(const std::vector<std::string>& funcs,
667
682
callbacksBuf << " " << callbacks[i];
668
683
}
669
684
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 \
674
689
)" _format (" callbacksStr" _a = callbacksBuf.str ());
675
690
}
676
691
@@ -685,26 +700,26 @@ std::string buildModule(const std::vector<std::string>& funcs,
685
700
}
686
701
687
702
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 \
708
723
)" _format (" importsStr" _a = importsBuf.str (), " callbackTableStr" _a = callbackTableBuf.str (),
709
724
" funcStr" _a = funcBuf.str ());
710
725
}
@@ -737,23 +752,23 @@ std::string buildJumpMap(const std::vector<JumpSegment>& segments)
737
752
std::string wasmStr = wasmBuf.str ();
738
753
wasmBuf.clear ();
739
754
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 \
757
772
)))))" _format (" wasm" _a = wasmStr, " brTable" _a = brTableBuf.str ());
758
773
759
774
return wasmBuf.str ();
0 commit comments