35
35
PUSH1 0x20
36
36
PUSH1 0x00
37
37
RETURN
38
- Function:\n
38
+ Function:
39
39
"""
40
40
41
41
template_2_1 = """
52
52
PUSH1 0x20
53
53
PUSH1 0x00
54
54
RETURN
55
- Function:\n
55
+ Function:
56
56
"""
57
57
58
58
def generate_header_str (inputs : List [int ]) -> str :
@@ -76,6 +76,9 @@ def generate_function_binary(filename: str, offset: int) -> str:
76
76
def generate_contract (inputs : List [int ], func : str ) -> str :
77
77
assert len (inputs ) < 3
78
78
complete_str = generate_header_str (inputs ) + func
79
+ f = open ("./generated_contract.s" , "w" )
80
+ f .write (complete_str )
81
+ f .close ()
79
82
return asm .assemble_hex (complete_str )
80
83
81
84
def execute_in_evm (code : str , expected : str ) -> str :
@@ -91,13 +94,28 @@ def execute_in_evm(code: str, expected: str) -> str:
91
94
result .check_returncode ()
92
95
return result .stdout
93
96
97
+ def should_remove (input : str ) -> bool :
98
+ sline = input .strip ()
99
+ if sline .startswith ("." ):
100
+ return True
101
+ elif sline .startswith ("#" ):
102
+ return True
103
+ return False
104
+
105
+ def process_line (input : str ) -> str :
106
+ index = input .find ("#" )
107
+ if index is - 1 :
108
+ return input
109
+ return input [:index ]
110
+
94
111
def remove_directives_in_assembly (input : str ) -> str :
112
+ print ("EVM TEST: removing directives" )
95
113
cleaned_input = []
96
- for line in str .split ("\n " ):
114
+ for line in input .split ("\n " ):
97
115
# ignore directives
98
- if not line . trim (). startswith ( "\." ):
99
- cleaned_input .append (line )
100
- return "" .join (cleaned_input )
116
+ if not should_remove ( line ):
117
+ cleaned_input .append (process_line ( line ) )
118
+ return "\n " .join (cleaned_input )
101
119
102
120
def generate_asm_file (infilename : str , outfilename : str ) -> str :
103
121
defined_llc = False
@@ -115,13 +133,21 @@ def generate_asm_file(infilename: str, outfilename: str) -> str:
115
133
llc_exec = "llc"
116
134
117
135
command = [llc_exec , "-mtriple=evm" , "-filetype=asm" , infilename , "-o" , outfilename ]
136
+ print ("EVM TEST: executing command:" )
137
+ print (' ' .join (command ))
138
+
118
139
result = subprocess .run (command , stdout = subprocess .PIPE )
119
140
result .check_returncode ()
120
141
return
121
142
122
- #contract = generate_contract(
123
- # inputs=["0x12345678", "0x87654321"], func="JUMPDEST\nADD\nSWAP1\nJUMP")
124
- #result = execute_in_evm(contract, "")
125
- #print(result)
143
+
126
144
127
145
generate_asm_file ("./test.ll" , "./test.s" )
146
+ f = open ("./test.s" , "r" )
147
+ content = f .read ()
148
+ f .close ()
149
+ cleaned_content = remove_directives_in_assembly (content )
150
+
151
+ contract = generate_contract (
152
+ inputs = ["0x12345678" , "0x87654321" ], func = cleaned_content )
153
+ result = execute_in_evm (contract , "" )
0 commit comments