File: IR.py

package info (click to toggle)
hvcc 0.13.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,072 kB
  • sloc: python: 8,135; ansic: 7,968; cpp: 4,538; objc: 698; javascript: 671; cs: 344; xml: 119; makefile: 94; sh: 4
file content (152 lines) | stat: -rw-r--r-- 3,001 bytes parent folder | download
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
# Heavy Compiler Collection
# Copyright (C) 2024 Wasted Audio
#
# SPDX-License-Identifier: GPL-3.0-only

from pydantic import BaseModel, RootModel
from typing import Dict, List, Optional, Union

from hvcc.types.Lang import LangLetType
from hvcc.version import VERSION


# IR Object Definitions

IRValue = Union[float, int, str, List[float], List[int]]


class IRArg(BaseModel):
    name: str
    value_type: str
    description: str = ""
    default: Optional[IRValue] = None
    required: bool


class IR(BaseModel):
    control: bool
    signal: bool
    init: bool


class Perf(BaseModel):
    avx: float = 0
    sse: float = 0
    neon: float = 0


class IRNode(BaseModel):
    inlets: List[LangLetType]
    ir: IR
    outlets: List[LangLetType]
    args: List[IRArg] = []
    perf: Optional[Perf] = Perf()
    # perf: Perf
    description: Optional[str] = None
    alias: List[str] = []
    tags: List[str] = []
    keywords: List[str] = []


class HeavyIRType(RootModel):
    root: Dict[str, IRNode]


# IR Graph Definitions

class IRName(BaseModel):
    escaped: str
    display: str


class IRObjectdict(BaseModel):
    args: Dict
    type: str


class IRInit(BaseModel):
    order: List[str] = []


class IRReceiver(BaseModel):
    display: str
    hash: str
    extern: Optional[str] = None
    attributes: Dict
    ids: List[str]


class IROnMessage(BaseModel):
    id: str
    inletIndex: Optional[int] = None


class IRSendMessage(BaseModel):
    """ This object is a mess and should be split up
    """
    id: str
    onMessage: List[List[IROnMessage]]  # TODO: why is it a nested List?
    type: Optional[str] = None
    extern: Optional[str] = ''
    attributes: Optional[Union[List, Dict]] = None  # TODO: List from IR and Dict from Lang
    hash: Optional[str] = None
    display: Optional[str] = None
    name: str = ''


class IRTable(BaseModel):
    id: str
    display: str
    hash: str
    extern: Optional[bool] = None


class IRControl(BaseModel):
    receivers: Dict[str, IRReceiver]
    sendMessage: List[IRSendMessage]


class IRNumTempBuffer(BaseModel):
    float: int
    integer: int


class IRBuffer(BaseModel):
    type: str
    index: int


class IRSignalList(BaseModel):
    id: str
    inputBuffers: List[IRBuffer]
    outputBuffers: List[IRBuffer]


class IRSignal(BaseModel):
    numInputBuffers: int
    numOutputBuffers: int
    numTemporaryBuffers: IRNumTempBuffer
    processOrder: List[IRSignalList]


class IRGraph(BaseModel):
    version: str = VERSION
    name: IRName
    objects: Dict[str, IRObjectdict] = {}
    init: IRInit
    tables: Dict[str, IRTable] = {}
    control: IRControl
    signal: IRSignal


if __name__ == "__main__":
    """ Test object definitions
    """
    import json
    import importlib.resources

    heavy_ir_json = importlib.resources.files('hvcc') / 'core/json/heavy.ir.json'
    with open(heavy_ir_json, "r") as f:
        data = json.load(f)
        heavy_ir = HeavyIRType(root=data)
        print(heavy_ir.root.keys())