aboutsummaryrefslogtreecommitdiffstats
path: root/pyGHDL/dom/_Translate.py
blob: 24f056f334cf5dcf89452ebdd70bc907a6f5d5b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# =============================================================================
#               ____ _   _ ____  _          _
#  _ __  _   _ / ___| | | |  _ \| |      __| | ___  _ __ ___
# | '_ \| | | | |  _| |_| | | | | |     / _` |/ _ \| '_ ` _ \
# | |_) | |_| | |_| |  _  | |_| | |___ | (_| | (_) | | | | | |
# | .__/ \__, |\____|_| |_|____/|_____(_)__,_|\___/|_| |_| |_|
# |_|    |___/
# =============================================================================
# Authors:
#   Patrick Lehmann
#
# Package module:   DOM: Interface items (e.g. generic or port)
#
# License:
# ============================================================================
#  Copyright (C) 2019-2021 Tristan Gingold
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <gnu.org/licenses>.
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ============================================================================
from typing import List

from pydecor import export
from pyVHDLModel.VHDLModel import Constraint, Direction, Expression, SubTypeOrSymbol

from pyGHDL.libghdl import utils
from pyGHDL.libghdl.utils import flist_iter
from pyGHDL.libghdl.vhdl import nodes
from pyGHDL.dom._Utils import NodeToName, GetIirKindOfNode
from pyGHDL.dom.Common import DOMException
from pyGHDL.dom.Range import Range, RangeExpression
from pyGHDL.dom.Symbol import (
    SimpleObjectSymbol,
    SimpleSubTypeSymbol,
    ConstrainedSubTypeSymbol,
)
from pyGHDL.dom.Literal import IntegerLiteral, CharacterLiteral, FloatingPointLiteral
from pyGHDL.dom.Expression import (
    SubtractionExpression,
    AdditionExpression,
    MultiplyExpression,
    DivisionExpression,
    InverseExpression,
    ExponentiationExpression,
    Aggregate,
    NegationExpression,
    ParenthesisExpression,
)

__all__ = []


@export
def GetSubtypeIndicationFromNode(node, entity: str, name: str) -> SubTypeOrSymbol:
    subTypeIndication = nodes.Get_Subtype_Indication(node)
    if subTypeIndication is nodes.Null_Iir:
        return None
    subTypeKind = GetIirKindOfNode(subTypeIndication)

    if subTypeKind == nodes.Iir_Kind.Simple_Name:
        subTypeName = NodeToName(subTypeIndication)

        subType = SimpleSubTypeSymbol(subTypeName)
    elif subTypeKind == nodes.Iir_Kind.Array_Subtype_Definition:
        typeMark = nodes.Get_Subtype_Type_Mark(subTypeIndication)
        typeMarkName = NodeToName(typeMark)

        constraints = GetArrayConstraintsFromSubtypeIndication(subTypeIndication)
        subType = ConstrainedSubTypeSymbol(typeMarkName, constraints)
    elif subTypeKind == nodes.Iir_Kind.Subtype_Definition:
        raise DOMException(
            "Unknown handling of subtype kind '{kind}' of subtype indication '{indication}' while parsing {entity} '{name}'.".format(
                kind=subTypeKind, indication=subTypeIndication, entity=entity, name=name
            )
        )
    else:
        raise DOMException(
            "Unknown subtype kind '{kind}' of subtype indication '{indication}' while parsing {entity} '{name}'.".format(
                kind=subTypeKind, indication=subTypeIndication, entity=entity, name=name
            )
        )

    return subType


@export
def GetArrayConstraintsFromSubtypeIndication(subTypeIndication) -> List[Constraint]:
    constraints = []
    for constraint in flist_iter(nodes.Get_Index_Constraint_List(subTypeIndication)):
        constraintKind = GetIirKindOfNode(constraint)
        if constraintKind == nodes.Iir_Kind.Range_Expression:
            direction = nodes.Get_Direction(constraint)
            leftBound = nodes.Get_Left_Limit_Expr(constraint)
            rightBound = nodes.Get_Right_Limit_Expr(constraint)

            r = Range(
                GetExpressionFromNode(leftBound),
                GetExpressionFromNode(rightBound),
                Direction.DownTo if direction else Direction.To,
            )
            constraints.append(RangeExpression(r))
        elif constraintKind == nodes.Iir_Kind.Attribute_Name:
            raise DOMException("[NOT IMPLEMENTED] Attribute name as range.")
        elif constraintKind == nodes.Iir_Kind.Simple_Name:
            raise DOMException("[NOT IMPLEMENTED] Subtype as range.")
        else:
            raise DOMException(
                "Unknown constraint kind '{kind}' for constraint '{constraint}' in subtype indication '{indication}'.".format(
                    kind=constraintKind,
                    constraint=constraint,
                    indication=subTypeIndication,
                )
            )

    return constraints


__EXPRESSION_TRANSLATION = {
    nodes.Iir_Kind.Simple_Name: SimpleObjectSymbol,
    nodes.Iir_Kind.Integer_Literal: IntegerLiteral,
    nodes.Iir_Kind.Floating_Point_Literal: FloatingPointLiteral,
    nodes.Iir_Kind.Character_Literal: CharacterLiteral,
    nodes.Iir_Kind.Negation_Operator: NegationExpression,
    nodes.Iir_Kind.Addition_Operator: AdditionExpression,
    nodes.Iir_Kind.Not_Operator: InverseExpression,
    nodes.Iir_Kind.Parenthesis_Expression: ParenthesisExpression,
    nodes.Iir_Kind.Substraction_Operator: SubtractionExpression,
    nodes.Iir_Kind.Multiplication_Operator: MultiplyExpression,
    nodes.Iir_Kind.Division_Operator: DivisionExpression,
    nodes.Iir_Kind.Exponentiation_Operator: ExponentiationExpression,
    nodes.Iir_Kind.Aggregate: Aggregate,
}


@export
def GetExpressionFromNode(node) -> Expression:
    kind = GetIirKindOfNode(node)

    try:
        cls = __EXPRESSION_TRANSLATION[kind]
    except KeyError:
        raise DOMException(
            "Unknown expression kind '{kindName}'({kind}) in expression '{expr}'.".format(
                kind=kind, kindName=kind.name, expr=node
            )
        )

    return cls.parse(node)


# FIXME: rewrite to generator
@export
def GetGenericsFromChainedNodes(nodeChain):
    result = []
    for generic in utils.chain_iter(nodeChain):
        kind = GetIirKindOfNode(generic)
        if kind == nodes.Iir_Kind.Interface_Constant_Declaration:
            from pyGHDL.dom.InterfaceItem import GenericConstantInterfaceItem

            genericConstant = GenericConstantInterfaceItem.parse(generic)

            result.append(genericConstant)
        else:
            raise DOMException(
                "Unknown generic kind '{kindName}'({kind}) in generic '{generic}'.".format(
                    kind=kind, kindName=kind.name, generic=generic
                )
            )

    return result


# FIXME: rewrite to generator
@export
def GetPortsFromChainedNodes(nodeChain):
    result = []
    for port in utils.chain_iter(nodeChain):
        kind = GetIirKindOfNode(port)
        if kind == nodes.Iir_Kind.Interface_Signal_Declaration:
            from pyGHDL.dom.InterfaceItem import PortSignalInterfaceItem

            portSignal = PortSignalInterfaceItem.parse(port)

            result.append(portSignal)
        else:
            raise DOMException(
                "Unknown port kind '{kindName}'({kind}) in port '{port}'.".format(
                    kind=kind, kindName=kind.name, port=port
                )
            )

    return result


def GetDeclaredItemsFromChainedNodes(nodeChain, entity: str, name: str):
    result = []
    for item in utils.chain_iter(nodeChain):
        kind = GetIirKindOfNode(item)
        if kind == nodes.Iir_Kind.Constant_Declaration:
            from pyGHDL.dom.Object import Constant

            result.append(Constant.parse(item))
        elif kind == nodes.Iir_Kind.Signal_Declaration:
            from pyGHDL.dom.Object import Signal

            result.append(Signal.parse(item))
        elif kind == nodes.Iir_Kind.Anonymous_Type_Declaration:
            typeName = NodeToName(item)
            print("found type '{name}'".format(name=typeName))
        elif kind == nodes.Iir_Kind.Subtype_Declaration:
            subTypeName = NodeToName(item)
            print("found subtype '{name}'".format(name=subTypeName))
        elif kind == nodes.Iir_Kind.Function_Declaration:
            functionName = NodeToName(item)
            print("found function '{name}'".format(name=functionName))
        elif kind == nodes.Iir_Kind.Function_Body:
            #                functionName = NodeToName(item)
            print("found function body '{name}'".format(name="????"))
        elif kind == nodes.Iir_Kind.Object_Alias_Declaration:
            aliasName = NodeToName(item)
            print("found alias '{name}'".format(name=aliasName))
        else:
            raise DOMException(
                "Unknown declared item kind '{kindName}'({kind}) in {entity} '{name}'.".format(
                    kind=kind, kindName=kind.name, entity=entity, name=name
                )
            )

    return result