From 3264a6e3032dad8826dbb63dec58b0c1d66d6ffd Mon Sep 17 00:00:00 2001 From: tutugordillo Date: Fri, 2 Aug 2024 09:10:38 +0200 Subject: [PATCH] adding get_expression --- src/parser/utils_parser.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/parser/utils_parser.py b/src/parser/utils_parser.py index 54036a58..6884c8b1 100644 --- a/src/parser/utils_parser.py +++ b/src/parser/utils_parser.py @@ -145,3 +145,28 @@ def get_empty_spec(): spec["rules"] = "" return spec + +#It returns the expression that a instruction takes as argument +#It returns a expression of the form: +# op -> opcode_name +# exp -> [int_val] | [input_var] | [op, [exp]] + +def get_expression(var, instructions): + if var.startswith("0x"): + return [var] + + candidates = list(filter(lambda x: var in x.get_out_args(), instructions)) + if len(candidates) == 0: + return [var] + + assert(len(candidates) == 1, "[ERROR]: A variable cannot be generated by more than one instruction") + + new_instruction = candidates[0] + + sub_expression = [] + for v in new_instruction.get_in_args(): + new_subexp = get_expression(v,instructions) + sub_expression.append(new_subexp) + + return [new_instruction.get_op_name(),sub_expression] +