https://github.com/spydr06/libgccjit-haskell
Safe Haskell bindings for the `libgccjit` library
https://github.com/spydr06/libgccjit-haskell
cabal gcc gcc-complier gnu gpl gplv3 haskell haskell-language haskell-library libgcc libgccjit library
Last synced: 2 months ago
JSON representation
Safe Haskell bindings for the `libgccjit` library
- Host: GitHub
- URL: https://github.com/spydr06/libgccjit-haskell
- Owner: Spydr06
- License: gpl-3.0
- Created: 2023-10-28T22:34:14.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-04T00:00:02.000Z (over 1 year ago)
- Last Synced: 2025-01-16T00:34:41.019Z (4 months ago)
- Topics: cabal, gcc, gcc-complier, gnu, gpl, gplv3, haskell, haskell-language, haskell-library, libgcc, libgccjit, library
- Language: Haskell
- Homepage:
- Size: 85.9 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# libgccjit-haskell
Haskell bindings for the `libgccjit` library## Example:
Examples are located in the `example/` directory. To compile an example, use:
```console
$ cabal run TutorialXX
```The available examples are:
- `Tutorial01` "Hello world" [(origin)](https://gcc.gnu.org/onlinedocs/jit/intro/tutorial01.html)
- `Tutorial02` Creating a trivial machine code function [(origin)](https://gcc.gnu.org/onlinedocs/jit/intro/tutorial02.html)
- `Tutorial03` Loops and variables [(origin)](https://gcc.gnu.org/onlinedocs/jit/intro/tutorial03.html)
- `Tutorial04` Adding JIT-compilation to a toy interpreter [(origin)](https://gcc.gnu.org/onlinedocs/jit/intro/tutorial03.html)
- `Tutorial05` Implementing an Ahead-of-Time compiler [(origin)](https://gcc.gnu.org/onlinedocs/jit/intro/tutorial05.html)> **Note**
> All the examples are ported from the official `libgccjit` tutorials. All credits go to the Freesoftware Foundation, Inc.The "Hello World" example:
```haskell
{-# LANGUAGE ForeignFunctionInterface #-}{- Ported from https://gcc.gnu.org/onlinedocs/jit/intro/tutorial01.html -}
module Main (main) where
import qualified GccJit
import GccJit.Utils (release)import Foreign.Ptr
import Foreign.C (CString, newCString)
import System.Exit
import System.IOtype GreetFunction = CString -> IO ()
foreign import ccall "dynamic" mkFun :: FunPtr GreetFunction -> GreetFunctionunwrapOrDie :: IO (Maybe a) -> String -> IO a
unwrapOrDie x msg = do
x' <- x
case x' of
Nothing -> die msg
Just x'' -> return x''createCode :: Ptr GccJit.Context -> IO ()
createCode ctxt = do
{-
Let's try to inject the equivalent of:
void greet (const char *name)
{
printf ("hello %s\n", name);
}
-}
voidType <- GccJit.contextGetType ctxt GccJit.Void
constCharPtrType <- GccJit.contextGetType ctxt GccJit.ConstCharPtr
paramName <- GccJit.contextNewParam ctxt nullPtr constCharPtrType "format"
func <- GccJit.contextNewFunction ctxt nullPtr GccJit.FunctionExported voidType "greet" [paramName] FalseparamFormat <- GccJit.contextNewParam ctxt nullPtr constCharPtrType "format"
intType <- GccJit.contextGetType ctxt GccJit.Int
printFunc <- GccJit.contextNewFunction ctxt nullPtr GccJit.FunctionImported intType "printf" [paramFormat] TrueformatArg <- GccJit.contextNewStringLiteral ctxt "Hello %s!\n"
nameArg <- GccJit.paramAsRValue paramNameblock <- unwrapOrDie (GccJit.functionNewBlock func Nothing) "NULL block"
printCall <- GccJit.contextNewCall ctxt nullPtr printFunc [formatArg, nameArg]GccJit.blockAddEval block nullPtr printCall
GccJit.blockEndWithVoidReturn block nullPtrmain :: IO ()
main = do
-- Get a "context" object for working with the library.
ctxt <- unwrapOrDie GccJit.contextAcquire "NULL ctxt"
-- Set some options on the context.
-- Let's see the code being generated, in assembler form.
GccJit.setBoolOption ctxt GccJit.DumpGeneratedCode True-- Populate the context.
createCode ctxt-- Compile the code.
result <- unwrapOrDie (GccJit.contextCompile ctxt) "NULL result"-- Extract the generated code from "result".
greet <- unwrapOrDie (GccJit.resultGetCode result "greet") "NULL greet"
-- Now call the generated function:
worldStr <- newCString "World"
mkFun greet worldStr
hFlush stdoutrelease ctxt
release result
```See [examples/](./examples) for more example files.
## License
`libgccjit` is part of the GCC project, which belongs to the Free Software Foundation, Inc. I take no credit of this software.
This code is licensed under the GNU General Public License V3. See [LICENSE](./LICENSE) for more information.