From 7e77fe05d51d07be85ec7bf8ba0cdd9d317e3c06 Mon Sep 17 00:00:00 2001 From: alexcere <48130030+alexcere@users.noreply.github.com> Date: Thu, 4 Jul 2024 12:42:05 +0200 Subject: [PATCH] Handle custom functions --- parser/cfg_instruction.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/parser/cfg_instruction.py b/parser/cfg_instruction.py index 3f00d732..0c933f2c 100644 --- a/parser/cfg_instruction.py +++ b/parser/cfg_instruction.py @@ -51,7 +51,7 @@ def build_push_spec(val, idx, out_idx): def build_verbatim_spec(function_name: str, input_args: List[str], output_args: List[str], builting_args: List[str]): """ - Generates the specification of a custom function + Generates the specification of a verbatim """ obj = {} @@ -72,6 +72,30 @@ def build_verbatim_spec(function_name: str, input_args: List[str], output_args: return obj +def build_custom_function_spec(function_name: str, input_args: List[str], output_args: List[str], builting_args: List[str]): + """ + Generates the specification of a custom function + """ + obj = {} + + # The function name is of the form 'verbatim_i_o("", ...)' + # (see "verbatim" in https://docs.soliditylang.org/en/latest/yul.html) + # Hence, different functions can have the same function name + obj["id"] = function_name + obj["opcode"] = function_name + obj["disasm"] = function_name + obj["inpt_sk"] = input_args + obj["outpt_sk"] = output_args + obj["gas"] = 100 # Random value for gas, as it is unknown + obj["commutative"] = False + obj["push"] = False + obj["storage"] = True # Assuming it must be performed always + obj["size"] = 5 # Assuming builtin args contain the corresponding bytecode + + return obj + + + class CFGInstruction: def __init__(self, op : str, in_args: List[str], out_args: List[str]): self.op = op @@ -128,7 +152,8 @@ def build_spec(self, out_idx, instrs_idx, map_instructions): elif opcodes.exists_opcode(op_name): instr_spec = build_instr_spec(op_name, idx, input_args, self.out_args) else: - raise ValueError("[ERROR]: Opcode name not found " + op_name) + # TODO: separate wrong opcodes from custom functions + instr_spec = build_custom_function_spec(op_name, input_args, self.out_args, self.builtin_args) instrs_idx[op_name] = idx+1 map_instructions[(op_name,tuple(self.in_args))] = instr_spec