33import sys
44import binascii
55
6- from .evmasm import assemble_hex , disassemble_all , instruction_table , assemble_all
6+ from .evmasm import assemble_hex , disassemble_all , instruction_tables , assemble_all , block_to_fork , DEFAULT_FORK
77
88
99def main ():
@@ -18,9 +18,26 @@ def main():
1818 help = 'Input file, default=stdin' )
1919 parser .add_argument ('-o' , '--output' , nargs = '?' , default = sys .stdout , type = argparse .FileType ('w' ),
2020 help = 'Output file, default=stdout' )
21+ parser .add_argument ('-f' , '--fork' , default = DEFAULT_FORK , type = str ,
22+ help = 'Fork, default: byzantium. Possible: [pre-byzantium, byzantium, constantinople].'
23+ 'Also an unsigned block number is accepted to select the fork.' )
2124
2225 args = parser .parse_args (sys .argv [1 :])
2326
27+ accepted_forks = ['pre-byzantium' , 'byzantium' , 'constantinople' ]
28+ arg_fork = args .fork .lower ()
29+ if arg_fork not in accepted_forks :
30+ try :
31+ block_number = abs (int (arg_fork ))
32+ fork = block_to_fork (block_number )
33+ except ValueError :
34+ sys .stderr .write ('Wrong fork name or block number. '
35+ 'Please provide an integer or one of %s.\n ' % accepted_forks )
36+ sys .exit (1 )
37+ else :
38+ fork = arg_fork
39+
40+ instruction_table = instruction_tables [fork ]
2441 if args .print_opcode_table :
2542 for instr in instruction_table :
2643 print ('0x{:02x}: {:16s} {:s}' .format (instr .opcode , instr .name , instr .description ))
@@ -32,13 +49,13 @@ def main():
3249 except KeyboardInterrupt :
3350 sys .exit (0 )
3451 if args .binary_output :
35- for i in assemble_all (asm ):
52+ for i in assemble_all (asm , fork = fork ):
3653 if sys .version_info >= (3 , 2 ):
3754 args .output .buffer .write (i .bytes )
3855 else :
3956 args .output .write (i .bytes )
4057 else :
41- args .output .write (assemble_hex (asm ) + "\n " )
58+ args .output .write (assemble_hex (asm , fork = fork ) + "\n " )
4259
4360 if args .disassemble :
4461 if args .binary_input and sys .version_info >= (3 , 2 ):
@@ -65,7 +82,7 @@ def main():
6582 if buf_set <= hex_set : # subset
6683 buf = binascii .unhexlify (buf )
6784
68- insns = list (disassemble_all (buf ))
85+ insns = list (disassemble_all (buf , fork = fork ))
6986 for i in insns :
7087 args .output .write ("%08x: %s\n " % (i .pc , str (i )))
7188
0 commit comments