- Notifications
You must be signed in to change notification settings - Fork0
Safe Haskell bindings for the `libgccjit` library
License
NotificationsYou must be signed in to change notification settings
Spydr06/libgccjit-haskell
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Haskell bindings for thelibgccjit library
Examples are located in theexample/ directory. To compile an example, use:
$cabal run TutorialXXThe available examples are:
Tutorial01"Hello world"(origin)Tutorial02Creating a trivial machine code function(origin)Tutorial03Loops and variables(origin)Tutorial04Adding JIT-compilation to a toy interpreter(origin)Tutorial05Implementing an Ahead-of-Time compiler(origin)
NoteAll the examples are ported from the official
libgccjittutorials. All credits go to the Freesoftware Foundation, Inc.
The "Hello World" example:
{-#LANGUAGE ForeignFunctionInterface #-}{- Ported from https://gcc.gnu.org/onlinedocs/jit/intro/tutorial01.html-}moduleMain (main)whereimportqualifiedGccJitimportGccJit.Utils (release)importForeign.PtrimportForeign.C (CString,newCString)importSystem.ExitimportSystem.IOtypeGreetFunction=CString->IO()foreignimport ccall"dynamic" mkFun::FunPtrGreetFunction->GreetFunctionunwrapOrDie::IO (Maybea)->String->IOaunwrapOrDie x msg=do x'<- xcase x'ofNothing-> die msgJust x''->return x''createCode::PtrGccJit.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 ctxtGccJit.Void constCharPtrType<-GccJit.contextGetType ctxtGccJit.ConstCharPtr paramName<-GccJit.contextNewParam ctxt nullPtr constCharPtrType"format" func<-GccJit.contextNewFunction ctxt nullPtrGccJit.FunctionExported voidType"greet" [paramName]False paramFormat<-GccJit.contextNewParam ctxt nullPtr constCharPtrType"format" intType<-GccJit.contextGetType ctxtGccJit.Int printFunc<-GccJit.contextNewFunction ctxt nullPtrGccJit.FunctionImported intType"printf" [paramFormat]True formatArg<-GccJit.contextNewStringLiteral ctxt"Hello %s!\n" nameArg<-GccJit.paramAsRValue paramName block<- unwrapOrDie (GccJit.functionNewBlock funcNothing)"NULL block" printCall<-GccJit.contextNewCall ctxt nullPtr printFunc [formatArg, nameArg]GccJit.blockAddEval block nullPtr printCallGccJit.blockEndWithVoidReturn block nullPtrmain::IO()main=do-- Get a "context" object for working with the library. ctxt<- unwrapOrDieGccJit.contextAcquire"NULL ctxt"-- Set some options on the context.-- Let's see the code being generated, in assembler form.GccJit.setBoolOption ctxtGccJit.DumpGeneratedCodeTrue-- 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 stdout release ctxt release resultSeeexamples/ for more example files.
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. SeeLICENSE for more information.
About
Safe Haskell bindings for the `libgccjit` library
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published