]> gitweb.factorcode.org Git - factor.git/commitdiff
llvm.examples.sumfunc: making the example work with the wrapper-based api
authorBjörn Lindqvist <bjourne@gmail.com>
Fri, 14 Jul 2017 23:22:47 +0000 (01:22 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Fri, 14 Jul 2017 23:22:47 +0000 (01:22 +0200)
extra/llvm/examples/sumfunc/sumfunc.factor

index 8dd1c664140f8cc9db00f5fedcd7da1144ba2d0c..6bc9c2064f08c51332d4db66110dc0e13ec9749b 100644 (file)
@@ -1,61 +1,32 @@
-USING: alien.c-types alien.data arrays destructors kernel llvm.ffi
-locals math sequences ;
+USING: accessors alien.c-types alien.data arrays destructors kernel
+llvm.ffi llvm.wrappers sequences ;
 IN: llvm.examples.sumfunc
 
 ! From:
 ! https://pauladamsmith.com/blog/2015/01/how-to-get-started-with-llvm-c-api.html
-ERROR: llvm-verify message ;
+: add-function ( module name type -- value )
+    [ value>> ] 2dip LLVMAddFunction ;
 
-: declare-function ( module name ret params -- value )
-    [ void* >c-array ] [ length ] bi 0 LLVMFunctionType LLVMAddFunction ;
+: dump-module ( module -- )
+    value>> LLVMDumpModule ;
 
-: verify-module ( module -- )
-    ! Does it leak?
-    LLVMReturnStatusAction
-    { c-string } [ LLVMVerifyModule ] with-out-parameters
-    swap 0 = [ drop ] [ llvm-verify ] if ;
-
-: with-module ( name quot -- )
-    [
-        swap LLVMModuleCreateWithName
-        &LLVMDisposeModule
-        [ swap call ]
-        [ dup verify-module LLVMDumpModule ] bi
-    ] with-destructors ; inline
-
-: with-builder ( quot -- )
-    [
-        LLVMCreateBuilder &LLVMDisposeBuilder swap call
-    ] with-destructors ; inline
-
-
-: create-execution-engine-for-module ( module -- engine )
-    [ f LLVMExecutionEngineRef <ref> dup ] dip f
-    LLVMCreateExecutionEngineForModule drop
-    LLVMExecutionEngineRef deref ;
-
-: with-execution-engine ( module quot -- )
-    [
-        swap create-execution-engine-for-module
-        &LLVMDisposeExecutionEngine
-        swap call
-    ] with-destructors ; inline
+: create-sum-type ( -- type )
+    LLVMInt32Type LLVMInt32Type LLVMInt32Type 2array
+    [ void* >c-array ] [ length ] bi 0 LLVMFunctionType ;
 
 : create-sum-body ( sum -- )
-    [
-        ! sum builder
-        over "entry" LLVMAppendBasicBlock
-        ! sum builder bb
-        dupd LLVMPositionBuilderAtEnd
+    dup <builder> [
+        value>>
         ! sum builder
-        swap dupd [ 0 LLVMGetParam ] [ 1 LLVMGetParam ] bi
+        swap dupd
+        [ 0 LLVMGetParam ] [ 1 LLVMGetParam ] bi
         ! builder builder p0 p1
         "tmp" LLVMBuildAdd
         LLVMBuildRet drop
-    ] with-builder ;
+    ] with-disposal ;
 
-:: create-sum-function ( -- )
-    "my_module" [
-        "sum" LLVMInt32Type LLVMInt32Type LLVMInt32Type 2array
-        declare-function create-sum-body
-    ] with-module ;
+: create-sum-function ( -- )
+    "my_module" <module> [
+        [ "sum" create-sum-type add-function create-sum-body ]
+        [ verify-module ] [ dump-module ] tri
+    ] with-disposal ;