0 | {--
 1 | Copyright (C) 2026  Joel Berkeley
 2 |
 3 | This program is free software: you can redistribute it and/or modify
 4 | it under the terms of the GNU Affero General Public License as published
 5 | by the Free Software Foundation, either version 3 of the License, or
 6 | (at your option) any later version.
 7 |
 8 | This program is distributed in the hope that it will be useful,
 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 | GNU Affero General Public License for more details.
12 |
13 | You should have received a copy of the GNU Affero General Public License
14 | along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 | --}
16 | ||| For internal spidr use only.
17 | module Spidr.Compiler.DType
18 |
19 | import Spidr.Compiler.MLIR.IR.MLIRContext
20 | import Spidr.Compiler.MLIR.IR.Types
21 | import Spidr.Compiler.MLIR.IR.BuiltinTypes
22 | import Spidr.DType
23 |
24 | export
25 | numBits : DType -> Bits16
26 | numBits PRED = 1
27 | numBits S8   = 8
28 | numBits S16  = 16
29 | numBits S32  = 32
30 | numBits S64  = 64
31 | numBits U8   = 8
32 | numBits U16  = 16
33 | numBits U32  = 32
34 | numBits U64  = 64
35 | numBits F64  = 64
36 |
37 | export
38 | isSigned : DType -> Bool
39 | isSigned PRED = True
40 | isSigned S8   = True
41 | isSigned S16  = True
42 | isSigned S32  = True
43 | isSigned S64  = True
44 | isSigned U8   = False
45 | isSigned U16  = False
46 | isSigned U32  = False
47 | isSigned U64  = False
48 | isSigned F64  = True
49 |
50 | export
51 | mlirType : HasIO io => MLIRContext -> DType -> io Type_
52 | mlirType ctx PRED = cast <$> IntegerType.get ctx 1  Signless
53 | mlirType ctx S8   = cast <$> IntegerType.get ctx 8  Signless
54 | mlirType ctx S16  = cast <$> IntegerType.get ctx 16 Signless
55 | mlirType ctx S32  = cast <$> IntegerType.get ctx 32 Signless
56 | mlirType ctx S64  = cast <$> IntegerType.get ctx 64 Signless
57 | mlirType ctx U8   = cast <$> IntegerType.get ctx 8  Unsigned
58 | mlirType ctx U16  = cast <$> IntegerType.get ctx 16 Unsigned
59 | mlirType ctx U32  = cast <$> IntegerType.get ctx 32 Unsigned
60 | mlirType ctx U64  = cast <$> IntegerType.get ctx 64 Unsigned
61 | mlirType ctx F64  = cast <$> Float64Type.get ctx
62 |