Skip to content

Commit 529399d

Browse files
committed
Rename offset to pc refactor to match trailofbits/manticore#938
1 parent aab42d8 commit 529399d

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

pyevmasm/evmasm2.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class EVMAsm(object):
6262
'PUSH1 0x60\\nBLOCKHASH\\nMSTORE\\nPUSH1 0x2\\nPUSH2 0x100'
6363
'''
6464
class Instruction(object):
65-
def __init__(self, opcode, name, operand_size, pops, pushes, fee, description, operand=None, offset=0):
65+
def __init__(self, opcode, name, operand_size, pops, pushes, fee, description, operand=None, pc=0):
6666
'''
6767
This represents an EVM instruction.
6868
EVMAsm will create this for you.
@@ -75,15 +75,15 @@ def __init__(self, opcode, name, operand_size, pops, pushes, fee, description, o
7575
:param fee: gas fee for the instruction
7676
:param description: textual description of the instruction
7777
:param operand: optional immediate operand
78-
:param offset: optional offset of this instruction in the program
78+
:param pc: optional program counter of this instruction in the program
7979
8080
Example use::
8181
8282
instruction = EVMAsm.assemble_one('PUSH1 0x10')
8383
print 'Instruction: %s'% instruction
8484
print '\tdescription:', instruction.description
8585
print '\tgroup:', instruction.group
86-
print '\taddress:', instruction.offset
86+
print '\tpc:', instruction.pc
8787
print '\tsize:', instruction.size
8888
print '\thas_operand:', instruction.has_operand
8989
print '\toperand_size:', instruction.operand_size
@@ -114,7 +114,7 @@ def __init__(self, opcode, name, operand_size, pops, pushes, fee, description, o
114114
mask = (1 << operand_size * 8) - 1
115115
if ~mask & operand:
116116
raise ValueError("operand should be %d bits long" % (operand_size * 8))
117-
self._offset = offset
117+
self._pc = pc
118118

119119
def __eq__(self, other):
120120
''' Instructions are equal if all features match '''
@@ -125,12 +125,12 @@ def __eq__(self, other):
125125
self._pops == other._pops and\
126126
self._pushes == other._pushes and\
127127
self._fee == other._fee and\
128-
self._offset == other._offset and\
128+
self._pc == other._pc and\
129129
self._description == other._description
130130

131131
def __repr__(self):
132132
output = 'Instruction(0x%x, %r, %d, %d, %d, %d, %r, %r, %r)' % (self._opcode, self._name, self._operand_size,
133-
self._pops, self._pushes, self._fee, self._description, self._operand, self._offset)
133+
self._pops, self._pushes, self._fee, self._description, self._operand, self._pc)
134134
return output
135135

136136
def __str__(self):
@@ -227,9 +227,9 @@ def bytes(self):
227227
return ''.join(bytes)
228228

229229
@property
230-
def offset(self):
230+
def pc(self):
231231
'''Location in the program (optional)'''
232-
return self._offset
232+
return self._pc
233233

234234
@property
235235
def group(self):
@@ -481,11 +481,11 @@ def _get_reverse_table():
481481
return reverse_table
482482

483483
@staticmethod
484-
def assemble_one(assembler, offset=0):
484+
def assemble_one(assembler, pc=0):
485485
''' Assemble one EVM instruction from its textual representation.
486486
487487
:param assembler: assembler code for one instruction
488-
:param offset: offset of the instruction in the bytecode (optional)
488+
:param pc: program counter of the instruction (optional)
489489
:return: An Instruction object
490490
491491
Example use::
@@ -505,18 +505,18 @@ def assemble_one(assembler, offset=0):
505505
assert len(assembler) == 1
506506
operand = None
507507

508-
return EVMAsm.Instruction(opcode, name, operand_size, pops, pushes, gas, description, operand=operand, offset=offset)
508+
return EVMAsm.Instruction(opcode, name, operand_size, pops, pushes, gas, description, operand=operand, pc=pc)
509509
except Exception as e:
510510
print "Exception", repr(e)
511511

512-
raise Exception("Something wrong at offset %d" % offset)
512+
raise Exception("Something wrong at pc %d" % pc)
513513

514514
@staticmethod
515-
def assemble_all(assembler, offset=0):
515+
def assemble_all(assembler, pc=0):
516516
''' Assemble a sequence of textual representation of EVM instructions
517517
518518
:param assembler: assembler code for any number of instructions
519-
:param offset: offset of the first instruction in the bytecode(optional)
519+
:param pc: program counter of the first instruction (optional)
520520
:return: An generator of Instruction objects
521521
522522
Example use::
@@ -540,17 +540,17 @@ def assemble_all(assembler, offset=0):
540540
for line in assembler:
541541
if not line.strip():
542542
continue
543-
instr = EVMAsm.assemble_one(line, offset=offset)
543+
instr = EVMAsm.assemble_one(line, pc=pc)
544544
yield instr
545-
offset += instr.size
545+
pc += instr.size
546546

547547
@staticmethod
548-
def disassemble_one(bytecode, offset=0):
548+
def disassemble_one(bytecode, pc=0):
549549
''' Decode a single instruction from a bytecode
550550
551551
:param bytecode: the bytecode stream
552552
:type bytecode: bytearray or str
553-
:param offset: offset of the instruction in the bytecode(optional)
553+
:param pc: program counter of the instruction (optional)
554554
:type bytecode: iterator/sequence/str
555555
:return: an Instruction object
556556
@@ -567,18 +567,18 @@ def disassemble_one(bytecode, offset=0):
567567

568568
invalid = ('INVALID', 0, 0, 0, 0, 'Unknown opcode')
569569
name, operand_size, pops, pushes, gas, description = EVMAsm._table.get(opcode, invalid)
570-
instruction = EVMAsm.Instruction(opcode, name, operand_size, pops, pushes, gas, description, offset=offset)
570+
instruction = EVMAsm.Instruction(opcode, name, operand_size, pops, pushes, gas, description, pc=pc)
571571
if instruction.has_operand:
572572
instruction.parse_operand(bytecode)
573573

574574
return instruction
575575

576576
@staticmethod
577-
def disassemble_all(bytecode, offset=0):
577+
def disassemble_all(bytecode, pc=0):
578578
''' Decode all instructions in bytecode
579579
580580
:param bytecode: an evm bytecode (binary)
581-
:param offset: offset of the first instruction in the bytecode(optional)
581+
:param pc: program counter of the first instruction (optional)
582582
:type bytecode: iterator/sequence/str
583583
:return: An generator of Instruction objects
584584
@@ -606,16 +606,16 @@ def disassemble_all(bytecode, offset=0):
606606
bytecode = bytearray(bytecode)
607607
bytecode = iter(bytecode)
608608
while True:
609-
instr = EVMAsm.disassemble_one(bytecode, offset=offset)
610-
offset += instr.size
609+
instr = EVMAsm.disassemble_one(bytecode, pc=pc)
610+
pc += instr.size
611611
yield instr
612612

613613
@staticmethod
614-
def disassemble(bytecode, offset=0):
614+
def disassemble(bytecode, pc=0):
615615
''' Disassemble an EVM bytecode
616616
617617
:param bytecode: binary representation of an evm bytecode (hexadecimal)
618-
:param offset: offset of the first instruction in the bytecode(optional)
618+
:param pc: program counter of the first instruction (optional)
619619
:type bytecode: str
620620
:return: the text representation of the aseembler code
621621
@@ -630,14 +630,14 @@ def disassemble(bytecode, offset=0):
630630
PUSH2 0x100
631631
632632
'''
633-
return '\n'.join(map(str, EVMAsm.disassemble_all(bytecode, offset=offset)))
633+
return '\n'.join(map(str, EVMAsm.disassemble_all(bytecode, pc=pc)))
634634

635635
@staticmethod
636-
def assemble(asmcode, offset=0):
636+
def assemble(asmcode, pc=0):
637637
''' Assemble an EVM program
638638
639639
:param asmcode: an evm assembler program
640-
:param offset: offset of the first instruction in the bytecode(optional)
640+
:param pc: program counter of the first instruction (optional)
641641
:type asmcode: str
642642
:return: the hex representation of the bytecode
643643
@@ -653,14 +653,14 @@ def assemble(asmcode, offset=0):
653653
...
654654
"\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00"
655655
'''
656-
return ''.join(map(lambda x: x.bytes, EVMAsm.assemble_all(asmcode, offset=offset)))
656+
return ''.join(map(lambda x: x.bytes, EVMAsm.assemble_all(asmcode, pc=pc)))
657657

658658
@staticmethod
659-
def disassemble_hex(bytecode, offset=0):
659+
def disassemble_hex(bytecode, pc=0):
660660
''' Disassemble an EVM bytecode
661661
662662
:param bytecode: canonical representation of an evm bytecode (hexadecimal)
663-
:param int offset: offset of the first instruction in the bytecode(optional)
663+
:param pc: program counter of the first instruction (optional)
664664
:type bytecode: str
665665
:return: the text representation of the aseembler code
666666
@@ -678,14 +678,14 @@ def disassemble_hex(bytecode, offset=0):
678678
if bytecode.startswith('0x'):
679679
bytecode = bytecode[2:]
680680
bytecode = bytecode.decode('hex')
681-
return EVMAsm.disassemble(bytecode, offset=offset)
681+
return EVMAsm.disassemble(bytecode, pc=pc)
682682

683683
@staticmethod
684-
def assemble_hex(asmcode, offset=0):
684+
def assemble_hex(asmcode, pc=0):
685685
''' Assemble an EVM program
686686
687687
:param asmcode: an evm assembler program
688-
:param offset: offset of the first instruction in the bytecode(optional)
688+
:param pc: program counter of the first instruction (optional)
689689
:type asmcode: str
690690
:return: the hex representation of the bytecode
691691
@@ -701,4 +701,4 @@ def assemble_hex(asmcode, offset=0):
701701
...
702702
"0x6060604052600261010"
703703
'''
704-
return '0x' + EVMAsm.assemble(asmcode, offset=offset).encode('hex')
704+
return '0x' + EVMAsm.assemble(asmcode, pc=pc).encode('hex')

0 commit comments

Comments
 (0)