]> gitweb.factorcode.org Git - factor.git/commitdiff
llvm: "salvaging" these vocabs from factor-unmaintained
authorBjörn Lindqvist <bjourne@gmail.com>
Tue, 11 Jul 2017 01:56:47 +0000 (03:56 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Tue, 11 Jul 2017 01:59:38 +0000 (03:59 +0200)
I think a lot of it needs to be updated to work with current llvm

extra/llvm/examples/kaleidoscope/authors.txt [new file with mode: 0644]
extra/llvm/examples/kaleidoscope/kaleidoscope-docs.factor [new file with mode: 0644]
extra/llvm/examples/kaleidoscope/kaleidoscope-tests.factor [new file with mode: 0644]
extra/llvm/examples/kaleidoscope/kaleidoscope.factor [new file with mode: 0644]
extra/llvm/examples/sumfunc/sumfunc.factor [new file with mode: 0644]
extra/llvm/ffi/authors.txt [new file with mode: 0644]
extra/llvm/ffi/ffi-tests.factor [new file with mode: 0644]
extra/llvm/ffi/ffi.factor [new file with mode: 0644]
extra/llvm/llvm.factor [new file with mode: 0644]

diff --git a/extra/llvm/examples/kaleidoscope/authors.txt b/extra/llvm/examples/kaleidoscope/authors.txt
new file mode 100644 (file)
index 0000000..8c0a152
--- /dev/null
@@ -0,0 +1 @@
+Björn Lindqvist
diff --git a/extra/llvm/examples/kaleidoscope/kaleidoscope-docs.factor b/extra/llvm/examples/kaleidoscope/kaleidoscope-docs.factor
new file mode 100644 (file)
index 0000000..42c0128
--- /dev/null
@@ -0,0 +1,7 @@
+USING: help.markup help.syntax ;
+IN: llvm.examples.kaleidoscope
+
+ARTICLE: "llvm.kaleidoscope" "The Kaleidoscope language"
+"This vocab implements the Kaleidoscope language which is the example language described in the official LLVM tutorial" ;
+
+ABOUT: "llvm.kaleidoscope"
diff --git a/extra/llvm/examples/kaleidoscope/kaleidoscope-tests.factor b/extra/llvm/examples/kaleidoscope/kaleidoscope-tests.factor
new file mode 100644 (file)
index 0000000..f79d924
--- /dev/null
@@ -0,0 +1,11 @@
+! Copyright (C) 2017 Björn Lindqvist
+! See http://factorcode.org/license.txt for BSD license.
+USING: llvm.examples.kaleidoscope tools.test ;
+IN: llvm.examples.kaleidoscope.tests
+
+
+{
+    V{ T{ ast-binop { lhs 3 } { rhs 4 } { operator "+" } } }
+} [
+    "3 + 4" parse-kaleidoscope
+] unit-test
diff --git a/extra/llvm/examples/kaleidoscope/kaleidoscope.factor b/extra/llvm/examples/kaleidoscope/kaleidoscope.factor
new file mode 100644 (file)
index 0000000..75be6da
--- /dev/null
@@ -0,0 +1,58 @@
+USING: accessors arrays combinators io kernel math.parser peg prettyprint
+sequences strings unicode peg.ebnf ;
+IN: llvm.examples.kaleidoscope
+
+TUPLE: ast-binop lhs rhs operator ;
+TUPLE: ast-name value ;
+TUPLE: ast-number value ;
+TUPLE: ast-def name params expr ;
+TUPLE: ast-unop expr ;
+TUPLE: ast-call name args ;
+TUPLE: ast-if condition true false ;
+
+EBNF: tokenize-kaleidoscope
+Letter            = [a-zA-Z]
+Digit             = [0-9]
+Digits            = Digit+
+SingleLineComment = "#" (!("\n") .)* "\n" => [[ ignore ]]
+Space             = [ \t\r\n] | SingleLineComment
+Spaces            = Space* => [[ ignore ]]
+NameFirst         = Letter
+NameRest          = NameFirst | Digit
+iName             = NameFirst NameRest* => [[ first2 swap prefix >string ]]
+Name              = !(Keyword) iName  => [[ ast-name boa ]]
+Number            = Digits:ws '.' Digits:fs => [[ ws "." fs 3array "" concat-as string>number ast-number boa ]]
+                  | Digits => [[ >string string>number ast-number boa ]]
+Special           = "(" | ")" | "*" | "+" | "/" | "-" | "<" | ">" | ","
+Keyword           = ("def" | "extern" | "if" | "then" | "else") !(NameRest)
+Tok               = Spaces (Keyword | Name | Number | Special)
+Toks              = Tok* Spaces
+;EBNF
+
+EBNF: parse-kaleidoscope
+tokenizer         = <foreign tokenize-kaleidoscope Tok>
+Name              = . ?[ ast-name?   ]?         => [[ value>> ]]
+Number            = . ?[ ast-number? ]?         => [[ value>> ]]
+CondOp            = "<" | ">"
+AddOp             = "+" | "-"
+MulOp             = "*" | "%" | "/"
+Unary             = "-" Unary:p                   => [[ p ast-unop boa ]]
+                  | PrimExpr
+MulExpr           = MulExpr:x MulOp:op Unary:y    => [[ x y op ast-binop boa ]]
+                  | Unary
+AddExpr           = AddExpr:x AddOp:op MulExpr:y        => [[ x y op ast-binop boa ]]
+                  | MulExpr
+RelExpr           = RelExpr:x CondOp:op AddExpr:y       => [[ x y op ast-binop boa ]]
+                  | AddExpr
+CondExpr          = "if" RelExpr:c "then" CondExpr:e1 "else" CondExpr:e2 => [[ c e1 e2 ast-if boa ]]
+                  | RelExpr
+Args              = (RelExpr ("," RelExpr => [[ second ]])* => [[ first2 swap prefix ]])?
+PrimExpr          = "(" CondExpr:e ")"  => [[ e ]]
+                  | Name:n "(" Args:a ")"           => [[ n a ast-call boa ]]
+                  | Name
+                  | Number
+SrcElem           = "def" Name:n "(" Name*:fs ")" CondExpr:expr => [[ n fs expr ast-def boa ]]
+                  | RelExpr
+SrcElems          = SrcElem*
+TopLevel          = SrcElems
+;EBNF
diff --git a/extra/llvm/examples/sumfunc/sumfunc.factor b/extra/llvm/examples/sumfunc/sumfunc.factor
new file mode 100644 (file)
index 0000000..8dd1c66
--- /dev/null
@@ -0,0 +1,61 @@
+USING: alien.c-types alien.data arrays destructors kernel llvm.ffi
+locals math 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 ;
+
+: declare-function ( module name ret params -- value )
+    [ void* >c-array ] [ length ] bi 0 LLVMFunctionType LLVMAddFunction ;
+
+: 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-body ( sum -- )
+    [
+        ! sum builder
+        over "entry" LLVMAppendBasicBlock
+        ! sum builder bb
+        dupd LLVMPositionBuilderAtEnd
+        ! sum builder
+        swap dupd [ 0 LLVMGetParam ] [ 1 LLVMGetParam ] bi
+        ! builder builder p0 p1
+        "tmp" LLVMBuildAdd
+        LLVMBuildRet drop
+    ] with-builder ;
+
+:: create-sum-function ( -- )
+    "my_module" [
+        "sum" LLVMInt32Type LLVMInt32Type LLVMInt32Type 2array
+        declare-function create-sum-body
+    ] with-module ;
diff --git a/extra/llvm/ffi/authors.txt b/extra/llvm/ffi/authors.txt
new file mode 100644 (file)
index 0000000..94f66da
--- /dev/null
@@ -0,0 +1,2 @@
+Matthew Willis
+Björn Lindqvist
diff --git a/extra/llvm/ffi/ffi-tests.factor b/extra/llvm/ffi/ffi-tests.factor
new file mode 100644 (file)
index 0000000..4836c21
--- /dev/null
@@ -0,0 +1,9 @@
+! Copyright (C) 2017 Björn Lindqvist
+USING: kernel llvm.ffi tools.test ;
+IN: llvm.ffi.tests
+
+{ } [
+    "my_module" LLVMModuleCreateWithName
+    dup LLVMDumpModule
+    LLVMDisposeModule
+] unit-test
diff --git a/extra/llvm/ffi/ffi.factor b/extra/llvm/ffi/ffi.factor
new file mode 100644 (file)
index 0000000..871bb0a
--- /dev/null
@@ -0,0 +1,88 @@
+! Copyright (C) 2009 Matthew Willis, 2017 Björn Lindqvist
+! See http://factorcode.org/license.txt for BSD license.
+USING: alien alien.c-types alien.destructors alien.libraries
+alien.syntax ldcache ;
+IN: llvm.ffi
+
+<<
+"llvm" "LLVM-3.8" find-so cdecl add-library
+>>
+
+LIBRARY: llvm
+
+CONSTANT: LLVMAbortProcessAction    0
+CONSTANT: LLVMPrintMessageAction    1
+CONSTANT: LLVMReturnStatusAction    2
+
+TYPEDEF: uint unsigned
+TYPEDEF: unsigned enum
+TYPEDEF: int LLVMBool
+
+! Reference types
+TYPEDEF: void* LLVMExecutionEngineRef
+TYPEDEF: void* LLVMModuleRef
+TYPEDEF: void* LLVMPassManagerRef
+TYPEDEF: void* LLVMModuleProviderRef
+TYPEDEF: void* LLVMTypeRef
+TYPEDEF: void* LLVMTypeHandleRef
+TYPEDEF: void* LLVMValueRef
+TYPEDEF: void* LLVMBasicBlockRef
+TYPEDEF: void* LLVMBuilderRef
+TYPEDEF: void* LLVMMemoryBufferRef
+
+! Modules
+FUNCTION: LLVMModuleRef LLVMModuleCreateWithName ( c-string ModuleID )
+FUNCTION: void LLVMDisposeModule ( LLVMModuleRef M )
+FUNCTION: void LLVMDumpModule ( LLVMModuleRef M )
+FUNCTION: LLVMBool LLVMVerifyModule ( LLVMModuleRef M, int Action, char **OutMessage )
+DESTRUCTOR: LLVMDisposeModule
+
+! Types
+FUNCTION: LLVMTypeRef LLVMInt1Type ( )
+FUNCTION: LLVMTypeRef LLVMInt8Type ( )
+FUNCTION: LLVMTypeRef LLVMInt16Type ( )
+FUNCTION: LLVMTypeRef LLVMInt32Type ( )
+FUNCTION: LLVMTypeRef LLVMInt64Type ( )
+FUNCTION: LLVMTypeRef LLVMIntType ( unsigned NumBits )
+FUNCTION: LLVMTypeRef LLVMFunctionType ( LLVMTypeRef ReturnType,
+                                         LLVMTypeRef* ParamTypes,
+                                         unsigned ParamCount, int IsVarArg )
+
+! Values
+FUNCTION: LLVMValueRef LLVMAddFunction ( LLVMModuleRef M,
+                                         c-string Name,
+                                         LLVMTypeRef FunctionTy )
+FUNCTION: LLVMValueRef LLVMGetParam ( LLVMValueRef Fn,
+                                      unsigned index )
+
+! Basic blocks
+FUNCTION: LLVMBasicBlockRef LLVMAppendBasicBlock ( LLVMValueRef Fn,
+                                                   c-string Name )
+
+! Builders
+FUNCTION: LLVMBuilderRef LLVMCreateBuilder ( )
+FUNCTION: void LLVMDisposeBuilder ( LLVMBuilderRef Builder )
+FUNCTION: void LLVMPositionBuilderBefore ( LLVMBuilderRef Builder,
+                                           LLVMValueRef Instr )
+FUNCTION: void LLVMPositionBuilderAtEnd ( LLVMBuilderRef Builder,
+                                          LLVMBasicBlockRef Block )
+
+FUNCTION: LLVMValueRef LLVMBuildAdd ( LLVMBuilderRef Builder,
+                                      LLVMValueRef LHS,
+                                      LLVMValueRef RHS,
+                                      c-string Name )
+FUNCTION: LLVMValueRef LLVMBuildSub ( LLVMBuilderRef Builder,
+                                      LLVMValueRef LHS,
+                                      LLVMValueRef RHS,
+                                      c-string Name )
+FUNCTION: LLVMValueRef LLVMBuildRet ( LLVMBuilderRef Builder,
+                                      LLVMValueRef V )
+DESTRUCTOR: LLVMDisposeBuilder
+
+! Engines
+FUNCTION: LLVMBool LLVMCreateExecutionEngineForModule (
+    LLVMExecutionEngineRef* OutEE,
+    LLVMModuleRef M,
+    char **OutMessage )
+FUNCTION: void LLVMDisposeExecutionEngine ( LLVMExecutionEngineRef E )
+DESTRUCTOR: LLVMDisposeExecutionEngine
diff --git a/extra/llvm/llvm.factor b/extra/llvm/llvm.factor
new file mode 100644 (file)
index 0000000..aad9279
--- /dev/null
@@ -0,0 +1,2 @@
+USING: kernel ;
+IN: llvm