Skip to content

Commit

Permalink
enconde only those values in assigments_dict that are used
Browse files Browse the repository at this point in the history
  • Loading branch information
tutugordillo committed Jul 5, 2024
1 parent 057b2bf commit 58b6afc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
37 changes: 23 additions & 14 deletions src/parser/cfg_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from parser.cfg_instruction import CFGInstruction, build_push_spec
from parser.utils_parser import is_in_input_stack, is_in_output_stack
from parser.utils_parser import is_in_input_stack, is_in_output_stack, is_assigment_var_used
import parser.constants as constants
import json

Expand Down Expand Up @@ -149,19 +149,6 @@ def _build_spec_for_block(self, instructions, map_instructions: Dict):
input_stack = []
output_stack = []

for assigment in self.assignment_dict:
in_val = self.assignment_dict.get(assigment)
if in_val.startswith("0x"): #It is a push value
func = map_instructions.get(("PUSH",tuple([in_val])),-1)
if func == -1:
push_name = "PUSH" if int(in_val,16) != 0 else "PUSH0"
inst_idx = instrs_idx.get(push_name, 0)
instrs_idx[push_name] = inst_idx+1
push_ins = build_push_spec(in_val,inst_idx,in_val)

map_instructions[("PUSH",tuple([in_val]))] = push_ins

uninter_functions.append(push_ins)

for i in range(len(instructions)):
#Check if it has been already created
Expand All @@ -188,6 +175,28 @@ def _build_spec_for_block(self, instructions, map_instructions: Dict):
if member:
output_stack = [o_arg]+output_stack



for assigment in self.assignment_dict:

is_used = is_assigment_var_used(assigment, uninter_functions)

if is_used:

in_val = self.assignment_dict.get(assigment)
if in_val.startswith("0x"): #It is a push value
func = map_instructions.get(("PUSH",tuple([in_val])),-1)
if func == -1:
push_name = "PUSH" if int(in_val,16) != 0 else "PUSH0"
inst_idx = instrs_idx.get(push_name, 0)
instrs_idx[push_name] = inst_idx+1
push_ins = build_push_spec(in_val,inst_idx,assigment)

map_instructions[("PUSH",tuple([in_val]))] = push_ins

uninter_functions.append(push_ins)


spec["original_instrs"] = ""
spec["yul_expressions"] = '\n'.join(list(map(lambda x: x.get_instruction_representation(),instructions)))
spec["src_ws"] = input_stack
Expand Down
24 changes: 14 additions & 10 deletions src/parser/cfg_instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,28 @@ def build_spec(self, out_idx, instrs_idx, map_instructions, assignments: Dict[st

if inp.startswith("0x") or inp in assignments:
# Retrieve the corresponding value
inp = assignments.get(inp, inp)
func = map_instructions.get(("PUSH",tuple([inp])),-1)
inp_value = assignments.get(inp, inp)
func = map_instructions.get(("PUSH",tuple([inp_value])),-1)

if func != -1:
inp_var = func["outpt_sk"][0]
else:
push_name = "PUSH" if int(inp,16) != 0 else "PUSH0"
inst_idx = instrs_idx.get(push_name, 0)
instrs_idx[push_name] = inst_idx+1
if inp.startswith("0x"):
push_name = "PUSH" if int(inp_value,16) != 0 else "PUSH0"
inst_idx = instrs_idx.get(push_name, 0)
instrs_idx[push_name] = inst_idx+1

push_ins = build_push_spec(inp,inst_idx,new_out)
push_ins = build_push_spec(inp_value,inst_idx,new_out)

map_instructions[("PUSH",tuple([inp]))] = push_ins
map_instructions[("PUSH",tuple([inp_value]))] = push_ins

new_out +=1
instructions.append(push_ins)
inp_var = push_ins["outpt_sk"][0]
new_out +=1
instructions.append(push_ins)
inp_var = push_ins["outpt_sk"][0]

elif inp in assignments:
inp_var = inp

input_args.append(inp_var)

op_name = self.op.upper()
Expand Down
5 changes: 5 additions & 0 deletions src/parser/utils_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ def is_in_output_stack(var, instructions):

candidate = any(filter(lambda x: var in x.get_in_args(),instructions))
return not candidate


def is_assigment_var_used(var, instructions):
candidates = list(filter(lambda x: var in x["inpt_sk"],instructions))
return len(candidates) != 0 #If it is !=0 means that the var generated in the assignment is used in the block

0 comments on commit 58b6afc

Please sign in to comment.