Source code for m2isar.metamodel.type_info

# SPDX-License-Identifier: Apache-2.0
#
# This file is part of the M2-ISA-R project: https://github.com/tum-ei-eda/M2-ISA-R
#
# Copyright (C) 2026
# Chair of Embedded Computing Systems
# Technical University of Vienna

"""This module contains helper data_type classes for modeling Symbol and data types
for the architectural part of an M2-ISA-R model. The architectural part is
anything but the functional behavior of functions and instructions.
"""

from dataclasses import dataclass
from enum import Enum, auto
from typing import Any, Union
import logging

[docs] logger = logging.getLogger("type_info_logger")
[docs] class TypeKind(Enum):
[docs] NONE = auto() # NumberLiteral with no type, e.g. 0 or 1
[docs] VOID = auto()
[docs] UINT = auto()
[docs] INT = auto()
[docs] CHAR = auto()
# BOOL = auto() Expressed as unsigned<1>
[docs] FLOAT = auto()
[docs] STR = auto()
@property
[docs] def is_int(self): return self in (TypeKind.INT, TypeKind.UINT)
@property
[docs] def is_numeric(self): return self in (TypeKind.INT, TypeKind.UINT, TypeKind.FLOAT)
@property
[docs] def is_void(self): return self == TypeKind.VOID
@property
[docs] def is_scalar(self): return self in (TypeKind.INT, TypeKind.UINT, TypeKind.CHAR, TypeKind.FLOAT)
@property
[docs] def is_literal(self): return self is not (TypeKind.VOID, TypeKind.NONE)
[docs] class PrimitiveType: def __init__(self, kind: TypeKind, size: int):
[docs] self.kind = kind
[docs] self.size = size
[docs] def __str__(self): return f"{self.kind.name}<{self.size}>"
[docs] class BitFieldType(): def __init__(self, kind: TypeKind):
[docs] self.kind = kind
[docs] def __str__(self): return f"BITFIELD<{self.kind.name}>"
[docs] class FloatType: def __init__(self, exponent: int, mantissa: int, size: int):
[docs] self.exponent = exponent
[docs] self.mantissa = mantissa
[docs] self.size = size
[docs] def __str__(self): return f"FLOAT<e{self.exponent}m{self.mantissa}s{self.size}>"
[docs] class ArrayType: def __init__(self, element_type : Union[PrimitiveType, FloatType], length: int):
[docs] self.element_type : Union[PrimitiveType, FloatType] = element_type
[docs] self.length = length # allow shaped later or TYPE_ARRAY in element_type?
[docs] def __str__(self): return f"ARRAY<{self.element_type}, {self.length}>"
@dataclass
[docs] class PointerType:
[docs] ty: Union[PrimitiveType, FloatType, ArrayType]
[docs] def __str__(self): return f"POINTER<{self.ty}>"
[docs] class FunctionType():
[docs] size : int
[docs] kind : TypeKind
def __init__(self, size: int, kind: TypeKind): self.size = size self.kind = kind
[docs] def __str__(self): return f"FUNCTION<{self.kind.name} {self.size}>"