0 | {--
 1 | Copyright (C) 2022  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.FFI
18 |
19 | import public System.FFI
20 |
21 | public export
22 | libxla : String -> String -> String
23 | libxla header fname = "C:\{fname},libc_xla,\{header}"
24 |
25 | ffi : String -> String
26 | ffi = libxla "c/ffi.h"
27 |
28 | export
29 | onCollectAny' : HasIO io => AnyPtr -> (AnyPtr -> IO ()) -> io GCAnyPtr
30 | onCollectAny' x f = onCollectAny x f
31 |
32 | export
33 | cIntToBool : Int -> Bool
34 | cIntToBool 0 = False
35 | cIntToBool 1 = True
36 | cIntToBool x =
37 |   let msg = "Internal error: expected 0 or 1 from XLA C API for boolean conversion, got " ++ show x
38 |   in (assert_total idris_crash) msg
39 |
40 | export
41 | boolToCInt : Bool -> Int
42 | boolToCInt True = 1
43 | boolToCInt False = 0
44 |
45 | %foreign (ffi "is_nullptr")
46 | prim__isNullPtr : AnyPtr -> Int
47 |
48 | export
49 | isNullPtr : AnyPtr -> Bool
50 | isNullPtr ptr = cIntToBool $ prim__isNullPtr ptr
51 |