0 | {--
 1 | Copyright (C) 2025  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.MLIR.IR.Block
18 |
19 | import Compiler.MLIR.IR.Location
20 | import Compiler.MLIR.IR.Types
21 | import Compiler.MLIR.IR.Value
22 | import Compiler.FFI
23 |
24 | ffi : String -> String
25 | ffi = libxla "c/mlir/IR/Block.h"
26 |
27 | public export
28 | data Block = MkBlock AnyPtr
29 |
30 | export
31 | %foreign (ffi "Block_addArgument")
32 | prim__blockAddArgument : AnyPtr -> GCAnyPtr -> GCAnyPtr -> PrimIO AnyPtr
33 |
34 | export
35 | addArgument : HasIO io => Block -> Type_ -> Location -> io BlockArgument
36 | addArgument (MkBlock block) (MkType_ type _) (MkLocation loc) = do
37 |   arg <- primIO $ prim__blockAddArgument block type loc
38 |   arg <- onCollectAny' arg (primIO . prim__deleteBlockArgument)
39 |   pure (MkBlockArgument arg)
40 |
41 | export
42 | %foreign (ffi "Block_getArgument")
43 | prim__blockGetArgument : AnyPtr -> Bits64 -> PrimIO AnyPtr
44 |
45 | export
46 | getArgument : HasIO io => Block -> Nat -> io BlockArgument
47 | getArgument (MkBlock block) i = do
48 |   arg <- primIO $ prim__blockGetArgument block (cast i)
49 |   arg <- onCollectAny' arg (primIO . prim__deleteBlockArgument)
50 |   pure (MkBlockArgument arg)
51 |
52 | export
53 | %foreign (ffi "Block_dump")
54 | prim__blockDump : AnyPtr -> PrimIO ()
55 |
56 | export
57 | dump : HasIO io => Block -> io ()
58 | dump (MkBlock block) = primIO $ prim__blockDump block
59 |