Skip to content

evmdasm registry

tintin edited this page Oct 11, 2018 · 2 revisions

Library

accessing the instruction registry

  • registry.INSTRUCTIONS holds instruction templates. These are the initial set ob instructions available to the evm. Keep the templates static/unchanged.
  • To create a new instruction from a template either use instruction.clone() or registry.create_instruction(name=; or opcode=). Feel free to do anything you want with this new instance of an evm instruction.
  • To add new instructions just create an Instruction(...) object and put it into registry.INSTRUCTIONS
from evm_instruction import registry

# access via named dict
jmp = registry.instruction.JUMP

# access via dict
## accessing the template objects (avoid modifying them)

jmp = registry.INSTRUCTIONS_BY_OPCODE["JUMP"]  # get the template object from the instruction registry 
jmp = registry.INSTRUCTIONS_BY_NAME["JUMP"]  # get the template object from the instruction registry 

jmp = jmp.clone()  # clone a new instruction from the template object

## creating new instruction objects from the template 
jmp = registry.create_instruction(name="JUMP")  # create a new jump instruction object in order to keep (

# access via categories lookup
terminating_instructions = registry.INSTRUCTIONS_BY_CATEGORY["terminate"]

# access all instructions as a list (no guarantee this is sorted in the future)
list_of_all_instructions = registry.INSTRUCTIONS

# extract certain instructions
list_of_gas_heavy_instructions = [i for i in registry.INSTRUCTIONS if i.gas > 500]

creating a custom registry

class MyInstruction(instructions.Instruction):

    def __init__(self, opcode, name, length_of_operand=0, description=None, args=None, returns=None, gas=-1,
                 category=None, pops=None, pushes=None, fork=None):
        super().__init__(opcode=opcode, name=name,
                         length_of_operand=length_of_operand,
                         description=description,
                         args=args, returns=returns,
                         gas=gas, category=category,
                         pops=pops, pushes=pushes, fork=fork)

        # additional attribs
        self.annotations = []
        self.xrefs = set([])
        self.jumpto = None
        self.basicblock = None

# create registry instance based on MyInstruction()
registry = registry.InstructionRegistry(instructions=registry.INSTRUCTIONS, _template_cls=MyInstruction)

Clone this wiki locally