From 0b3508abf810877778695a43401da72d319ec3a1 Mon Sep 17 00:00:00 2001 From: stefandesouza Date: Sat, 16 Dec 2023 16:00:37 +0100 Subject: [PATCH] Small cleaup commit --- osaca/parser/__init__.py | 2 -- osaca/parser/attr_dict.py | 53 -------------------------------- osaca/parser/condition.py | 5 ++- osaca/parser/directive.py | 2 +- osaca/parser/identifier.py | 12 ++++---- osaca/parser/immediate.py | 10 +++--- osaca/parser/instruction_form.py | 1 - osaca/parser/label.py | 4 +-- osaca/parser/memory.py | 9 +----- osaca/parser/operand.py | 8 ++--- osaca/parser/parser_AArch64.py | 29 ++++++++--------- osaca/parser/parser_x86att.py | 31 ++++++++++--------- osaca/parser/register.py | 2 +- 13 files changed, 53 insertions(+), 115 deletions(-) delete mode 100644 osaca/parser/attr_dict.py diff --git a/osaca/parser/__init__.py b/osaca/parser/__init__.py index 96d7cf9..c649904 100644 --- a/osaca/parser/__init__.py +++ b/osaca/parser/__init__.py @@ -3,7 +3,6 @@ Collection of parsers supported by OSACA. Only the parser below will be exported, so please add new parsers to __all__. """ -from .attr_dict import AttrDict from .base_parser import BaseParser from .parser_x86att import ParserX86ATT from .parser_AArch64 import ParserAArch64 @@ -13,7 +12,6 @@ from .operand import Operand __all__ = [ "Operand", "instructionForm", - "AttrDict", "BaseParser", "ParserX86ATT", "ParserAArch64", diff --git a/osaca/parser/attr_dict.py b/osaca/parser/attr_dict.py deleted file mode 100644 index 35f6c40..0000000 --- a/osaca/parser/attr_dict.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python3 -"""Attribute Dictionary to access dictionary entries as attributes.""" - - -class AttrDict(dict): - def __init__(self, *args, **kwargs): - super(AttrDict, self).__init__(*args, **kwargs) - self.__dict__ = self - - @staticmethod - def convert_dict(dictionary): - """ - Convert given dictionary to `AttrDict`. - - :param dictionary: `dict` to be converted - :type dictionary: `dict` - :returns: `AttrDict` representation of ``dictionary`` - """ - if isinstance(dictionary, type(list())): - return [AttrDict.convert_dict(x) for x in dictionary] - if isinstance(dictionary, type(dict())): - for key in list(dictionary.keys()): - entry = dictionary[key] - if isinstance(entry, type(dict())) or isinstance(entry, type(AttrDict())): - dictionary[key] = AttrDict.convert_dict(dictionary[key]) - if isinstance(entry, type(list())): - dictionary[key] = [AttrDict.convert_dict(x) for x in entry] - return AttrDict(dictionary) - return dictionary - - @staticmethod - def get_dict(attrdict): - """ - Convert given `AttrDict` to a standard dictionary. - - :param attrdict: `AttrDict` to be converted - :type attrdict: `AttrDict` - :returns: `dict` representation of ``AttrDict`` - """ - if isinstance(attrdict, type(list())): - return [AttrDict.get_dict(x) for x in attrdict] - if isinstance(attrdict, type(AttrDict())): - newdict = {} - for key in list(attrdict.keys()): - entry = attrdict[key] - if isinstance(entry, type(dict())) or isinstance(entry, type(AttrDict())): - newdict[key] = AttrDict.get_dict(attrdict[key]) - elif isinstance(entry, type(list())): - newdict[key] = [AttrDict.get_dict(x) for x in entry] - else: - newdict[key] = entry - return newdict - return attrdict diff --git a/osaca/parser/condition.py b/osaca/parser/condition.py index 9583fd0..0cab22d 100644 --- a/osaca/parser/condition.py +++ b/osaca/parser/condition.py @@ -22,4 +22,7 @@ class ConditionOperand(Operand): self._ccode = ccode def __str__(self): - return f"ConditionOperand(ccode={self._ccode}, source={self._source}, destination={self._destination})" + return f"Condition(ccode={self._ccode}, source={self._source}, destination={self._destination})" + + def __repr__(self): + return self.__str__() diff --git a/osaca/parser/directive.py b/osaca/parser/directive.py index 9c5b5ca..e68be74 100644 --- a/osaca/parser/directive.py +++ b/osaca/parser/directive.py @@ -48,4 +48,4 @@ class DirectiveOperand(Operand): return f"Directive(name={self._name}, parameters={self._parameter_id}, comment={self._comment_id})" def __repr__(self): - return f"DirectiveOperand(name={self._name}, parameters={self._parameter_id}, comment={self._comment_id})" + return self.__str__() diff --git a/osaca/parser/identifier.py b/osaca/parser/identifier.py index c105f00..33a8850 100644 --- a/osaca/parser/identifier.py +++ b/osaca/parser/identifier.py @@ -13,22 +13,22 @@ class IdentifierOperand(Operand): def offset(self): return self._offset - @offset.setter - def offset(self, offset): - self._offset = offset - @property def relocation(self): return self._relocation + @offset.setter + def offset(self, offset): + self._offset = offset + @relocation.setter def relocation(self, relocation): self._relocation = relocation def __str__(self): return ( - f"IdentifierOperand({self.name}, offset={self.offset}, relocation={self.relocation})" + f"Identifier(name={self._name}, offset={self._offset}, relocation={self._relocation})" ) def __repr__(self): - return f"IdentifierOperand(name={self.name}, offset={self.offset}, relocation={self.relocation}, source =relocation={self.source}, destination =relocation={self.destination})" + return self.__str__() diff --git a/osaca/parser/immediate.py b/osaca/parser/immediate.py index 7edf4f1..2c8cd84 100644 --- a/osaca/parser/immediate.py +++ b/osaca/parser/immediate.py @@ -35,14 +35,14 @@ class ImmediateOperand(Operand): def shift(self): return self._type_id - @identifier.setter - def identifier(self, identifier): - self._identifier_id = identifier - @type.setter def type(self, type): self._type_id = type + @identifier.setter + def identifier(self, identifier): + self._identifier_id = identifier + @value.setter def value(self, value): self._value_id = value @@ -53,7 +53,7 @@ class ImmediateOperand(Operand): def __str__(self): return ( - f"ImmediateOperand(identifier_id={self._identifier_id}, type_id={self._type_id}, " + f"Immediate(identifier_id={self._identifier_id}, type_id={self._type_id}, " f"value_id={self._value_id}, shift_id={self._shift_id}, source={self._source}, destination={self._destination})" ) diff --git a/osaca/parser/instruction_form.py b/osaca/parser/instruction_form.py index 1024607..6c7781c 100644 --- a/osaca/parser/instruction_form.py +++ b/osaca/parser/instruction_form.py @@ -33,7 +33,6 @@ class instructionForm: self._operation = operation self._uops = uops self._breaks_dep = breaks_dep - # self.semantic_operands = {"source": [], "destination": [], "src_dst": []} self._latency = latency self._throughput = throughput self._latency_cp = [] diff --git a/osaca/parser/label.py b/osaca/parser/label.py index 08ed77c..bfc9716 100644 --- a/osaca/parser/label.py +++ b/osaca/parser/label.py @@ -25,7 +25,7 @@ class LabelOperand(Operand): return self._comment_id.pop(0) def __str__(self): - return f"LabelOperand(name={self._name}, comment={self._comment_id})" + return f"Label(name={self._name}, comment={self._comment_id})" def __repr__(self): - return f"LabelOperand(name={self._name}, comment={self._comment_id})" + return self.__str__() diff --git a/osaca/parser/memory.py b/osaca/parser/memory.py index 1750066..a8c4be0 100644 --- a/osaca/parser/memory.py +++ b/osaca/parser/memory.py @@ -136,14 +136,7 @@ class MemoryOperand(Operand): ) def __repr__(self): - return ( - f"MemoryOperand(name={self._name}, offset_ID={self._offset_ID}, " - f"base_id={self._base_id}, index_id={self._index_id}, scale_id={self._scale_id}, " - f"segment_ext_id={self._segment_ext_id}, mask={self._mask}, " - f"pre_indexed={self._pre_indexed}, post_indexed={self._post_indexed}, " - f"indexed_val={self._indexed_val}, port_pressure={self._port_pressure})," - f"source={self._source}, destination={self._destination})" - ) + return self.__str__() def __eq__(self, other): if isinstance(other, MemoryOperand): diff --git a/osaca/parser/operand.py b/osaca/parser/operand.py index 8860195..5c8a185 100644 --- a/osaca/parser/operand.py +++ b/osaca/parser/operand.py @@ -31,8 +31,8 @@ class Operand: def destination(self, destination): self._destination = destination - def __repr__(self): - return f"Operand(name={self._name},source={self._source},destination={self._destination})" - def __str__(self): - return f"Name: {self._name}, Source: {self._source}, Destination: {self._destination}" + return f"Operand(Name: {self._name}, Source: {self._source}, Destination: {self._destination})" + + def __repr__(self): + return self.__str__() diff --git a/osaca/parser/parser_AArch64.py b/osaca/parser/parser_AArch64.py index bdf7f2d..c21bd30 100644 --- a/osaca/parser/parser_AArch64.py +++ b/osaca/parser/parser_AArch64.py @@ -383,17 +383,20 @@ class ParserAArch64(BaseParser): if self.REGISTER_ID in operand: return self.process_register_operand(operand[self.REGISTER_ID]) if self.directive_id in operand: - return DirectiveOperand( - name=operand["directive"]["name"], - parameter_id=operand["directive"]["parameters"], - comment_id=operand["directive"]["comment"] - if "comment" in operand["directive"] - else None, - ) + return self.process_directive_operand(operand[self.directive_id]) if self.CONDITION_ID in operand: return self.process_condition(operand[self.CONDITION_ID]) return operand + def process_directive_operand(self, operand): + return DirectiveOperand( + name=operand["name"], + parameter_id=operand["parameters"], + comment_id=operand["comment"] + if "comment" in operand + else None, + ) + def process_register_operand(self, operand): return RegisterOperand( prefix_id=operand["prefix"], @@ -451,8 +454,7 @@ class ParserAArch64(BaseParser): def process_sp_register(self, register): """Post-process stack pointer register""" - new_reg = RegisterOperand(prefix_id="x", name="sp") - return new_reg + return RegisterOperand(prefix_id="x", name="sp") def process_condition(self, condition): return ConditionOperand(ccode=condition.upper()) @@ -507,8 +509,6 @@ class ParserAArch64(BaseParser): rlist.append(self.list_element.parseString(r, parseAll=True).asDict()) index = register_list.get("index", None) new_dict = {dict_name: rlist, "index": index} - if len(new_dict[dict_name]) == 1: - return {self.REGISTER_ID: new_dict} return {self.REGISTER_ID: new_dict} def process_immediate(self, immediate): @@ -554,18 +554,13 @@ class ParserAArch64(BaseParser): def process_label(self, label): """Post-process label asm line""" # remove duplicated 'name' level due to identifier - # label["name"] = label["name"]["name"] - new_label = LabelOperand( + return LabelOperand( name=label["name"]["name"], comment_id=label["comment"] if self.comment_id in label else None, ) - return new_label def process_identifier(self, identifier): """Post-process identifier operand""" - # remove value if it consists of symbol+offset - if "value" in identifier: - del identifier["value"] return IdentifierOperand( name=identifier["name"] if "name" in identifier else None, offset=identifier["offset"] if "offset" in identifier else None, diff --git a/osaca/parser/parser_x86att.py b/osaca/parser/parser_x86att.py index 964376c..ee390a5 100644 --- a/osaca/parser/parser_x86att.py +++ b/osaca/parser/parser_x86att.py @@ -298,22 +298,25 @@ class ParserX86ATT(BaseParser): if self.directive_id in operand: return self.process_directive(operand[self.directive_id]) if self.REGISTER_ID in operand: - return RegisterOperand( - prefix_id=operand["register"]["prefix"] - if "prefix" in operand["register"] - else None, - name=operand["register"]["name"], - shape=operand["register"]["shape"] if "shape" in operand["register"] else None, - lanes=operand["register"]["lanes"] if "lanes" in operand["register"] else None, - index=operand["register"]["index"] if "index" in operand["register"] else None, - predication=operand["register"]["predication"] - if "predication" in operand["register"] - else None, - ) + return self.process_register(operand[self.REGISTER_ID]) if self.IDENTIFIER_ID in operand: return self.process_identifier(operand[self.IDENTIFIER_ID]) return operand + def process_register(self, operand): + return RegisterOperand( + prefix_id=operand["prefix"] + if "prefix" in operand + else None, + name=operand["name"], + shape=operand["shape"] if "shape" in operand else None, + lanes=operand["lanes"] if "lanes" in operand else None, + index=operand["index"] if "index" in operand else None, + predication=operand["predication"] + if "predication" in operand + else None, + ) + def process_directive(self, directive): directive_new = DirectiveOperand(name=directive["name"], parameter_id=[]) if "parameters" in directive: @@ -360,10 +363,10 @@ class ParserX86ATT(BaseParser): """Post-process label asm line""" # remove duplicated 'name' level due to identifier label["name"] = label["name"][0]["name"] - new_label = LabelOperand( + return LabelOperand( name=label["name"], comment_id=label["comment"] if "comment" in label else None ) - return new_label + def process_immediate(self, immediate): """Post-process immediate operand""" diff --git a/osaca/parser/register.py b/osaca/parser/register.py index ad6a796..4391d5c 100644 --- a/osaca/parser/register.py +++ b/osaca/parser/register.py @@ -134,7 +134,7 @@ class RegisterOperand(Operand): def __str__(self): return ( - f"RegisterOperand(name={self._name}, width_id={self._width_id}, " + f"Register(name={self._name}, width_id={self._width_id}, " f"prefix_id={self._prefix_id}, reg_id={self._reg_id}, REGtype_id={self._regtype_id}, " f"lanes={self._lanes}, shape={self._shape}, index={self._index}, " f"mask={self._mask}, zeroing={self._zeroing},source={self._source},destination={self._destination},"