Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion python/lira/arch.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Optional
from typing import ClassVar, Dict, Optional, TypeVar
from dataclasses import dataclass, field

from .ir import *

OperationT = TypeVar("OperationT", bound="Operation")

@dataclass
class Component:
name: str
Expand All @@ -24,6 +26,22 @@ class Operation(Component):
semantic_func_128: Optional[str] = None # Snippet
semantic_table: Optional[str] = None # TableInt

typed_ops: ClassVar[Dict[str, type]] = {}

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
sb = getattr(cls, "op_base", None)
if sb is not None:
Operation.typed_ops[sb] = cls

def _restore_from(self: "OperationT", other: "Operation") -> "OperationT":
self.name = other.name
self.attributes = other.attributes
self.semantic_func = other.semantic_func
self.semantic_func_128 = other.semantic_func_128
self.semantic_table = other.semantic_table
return self

def __eq__(self, other):
if not isinstance(other, Operation):
return NotImplemented
Expand Down Expand Up @@ -95,6 +113,7 @@ class Instruction(Component):

semantic: StatementSeq


@dataclass
class Arch(Component):
# Constant Architecture part
Expand Down
6 changes: 5 additions & 1 deletion python/lira/arch_ser_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .arch import *
from .ir import StatementSeq
from .ir_ops import from_operation
from .ir_ser_txt import serialize_statement_seq, deserialize_statement_seq

def to_serializable(obj: Any) -> Any:
Expand All @@ -33,7 +34,10 @@ def from_serializable(cls: Type, data: Any) -> Any:
kwargs[name] = deserialize_statement_seq(value)
else:
kwargs[name] = from_serializable(types[name], value)
return cls(**kwargs)
obj = cls(**kwargs)
if cls is Operation:
return from_operation(obj)
return obj
if origin is list:
item_cls = get_args(cls)[0]
return [from_serializable(item_cls, item) for item in data]
Expand Down
Loading
Loading