Small cleaup commit

This commit is contained in:
stefandesouza
2023-12-16 16:00:37 +01:00
parent cb5e0bdc38
commit 0b3508abf8
13 changed files with 53 additions and 115 deletions

View File

@@ -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",

View File

@@ -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

View File

@@ -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__()

View File

@@ -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__()

View File

@@ -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__()

View File

@@ -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})"
)

View File

@@ -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 = []

View File

@@ -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__()

View File

@@ -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):

View File

@@ -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__()

View File

@@ -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,

View File

@@ -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"""

View File

@@ -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},"