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 Compiler.LLVM.ADT.APInt
18 |
19 | import Compiler.FFI
20 |
21 | public export
22 | data APInt = MkAPInt GCAnyPtr
23 |
24 | ffi : String -> String
25 | ffi = libxla "c/llvm/ADT/APInt.h"
26 |
27 | %foreign (ffi "delete_APInt")
28 | prim__deleteAPInt : AnyPtr -> PrimIO ()
29 |
30 | %foreign (ffi "APInt_getMaxValue")
31 | prim__apIntGetMaxValue : Bits16 -> PrimIO AnyPtr
32 |
33 | export
34 | getMaxValue : HasIO io => Bits16 -> io APInt
35 | getMaxValue numBits = do
36 |   int <- primIO $ prim__apIntGetMaxValue numBits
37 |   int <- onCollectAny' int (primIO . prim__deleteAPInt)
38 |   pure (MkAPInt int)
39 |
40 | %foreign (ffi "APInt_getSignedMaxValue")
41 | prim__apIntGetSignedMaxValue : Bits16 -> PrimIO AnyPtr
42 |
43 | export
44 | getSignedMaxValue : HasIO io => Bits16 -> io APInt
45 | getSignedMaxValue numBits = do
46 |   int <- primIO $ prim__apIntGetSignedMaxValue numBits
47 |   int <- onCollectAny' int (primIO . prim__deleteAPInt)
48 |   pure (MkAPInt int)
49 |
50 | %foreign (ffi "APInt_getMinValue")
51 | prim__apIntGetMinValue : Bits16 -> PrimIO AnyPtr
52 |
53 | export
54 | getMinValue : HasIO io => Bits16 -> io APInt
55 | getMinValue numBits = do
56 |   int <- primIO $ prim__apIntGetMinValue numBits
57 |   int <- onCollectAny' int (primIO . prim__deleteAPInt)
58 |   pure (MkAPInt int)
59 |
60 | %foreign (ffi "APInt_getSignedMinValue")
61 | prim__apIntGetSignedMinValue : Bits16 -> PrimIO AnyPtr
62 |
63 | export
64 | getSignedMinValue : HasIO io => Bits16 -> io APInt
65 | getSignedMinValue numBits = do
66 |   int <- primIO $ prim__apIntGetSignedMinValue numBits
67 |   int <- onCollectAny' int (primIO . prim__deleteAPInt)
68 |   pure (MkAPInt int)
69 |