From c90a2a28bdf1e4d60bc65d52a94ee750bcdd6299 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindqvist?= Date: Tue, 11 Jul 2017 03:56:47 +0200 Subject: [PATCH] llvm: "salvaging" these vocabs from factor-unmaintained I think a lot of it needs to be updated to work with current llvm --- extra/llvm/examples/kaleidoscope/authors.txt | 1 + .../kaleidoscope/kaleidoscope-docs.factor | 7 ++ .../kaleidoscope/kaleidoscope-tests.factor | 11 +++ .../examples/kaleidoscope/kaleidoscope.factor | 58 ++++++++++++ extra/llvm/examples/sumfunc/sumfunc.factor | 61 +++++++++++++ extra/llvm/ffi/authors.txt | 2 + extra/llvm/ffi/ffi-tests.factor | 9 ++ extra/llvm/ffi/ffi.factor | 88 +++++++++++++++++++ extra/llvm/llvm.factor | 2 + 9 files changed, 239 insertions(+) create mode 100644 extra/llvm/examples/kaleidoscope/authors.txt create mode 100644 extra/llvm/examples/kaleidoscope/kaleidoscope-docs.factor create mode 100644 extra/llvm/examples/kaleidoscope/kaleidoscope-tests.factor create mode 100644 extra/llvm/examples/kaleidoscope/kaleidoscope.factor create mode 100644 extra/llvm/examples/sumfunc/sumfunc.factor create mode 100644 extra/llvm/ffi/authors.txt create mode 100644 extra/llvm/ffi/ffi-tests.factor create mode 100644 extra/llvm/ffi/ffi.factor create mode 100644 extra/llvm/llvm.factor diff --git a/extra/llvm/examples/kaleidoscope/authors.txt b/extra/llvm/examples/kaleidoscope/authors.txt new file mode 100644 index 0000000000..8c0a152f90 --- /dev/null +++ b/extra/llvm/examples/kaleidoscope/authors.txt @@ -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 index 0000000000..42c01284d6 --- /dev/null +++ b/extra/llvm/examples/kaleidoscope/kaleidoscope-docs.factor @@ -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 index 0000000000..f79d924426 --- /dev/null +++ b/extra/llvm/examples/kaleidoscope/kaleidoscope-tests.factor @@ -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 index 0000000000..75be6da824 --- /dev/null +++ b/extra/llvm/examples/kaleidoscope/kaleidoscope.factor @@ -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 = +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 index 0000000000..8dd1c66414 --- /dev/null +++ b/extra/llvm/examples/sumfunc/sumfunc.factor @@ -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 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 index 0000000000..94f66da443 --- /dev/null +++ b/extra/llvm/ffi/authors.txt @@ -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 index 0000000000..4836c219f1 --- /dev/null +++ b/extra/llvm/ffi/ffi-tests.factor @@ -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 index 0000000000..871bb0a871 --- /dev/null +++ b/extra/llvm/ffi/ffi.factor @@ -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 index 0000000000..aad9279d7e --- /dev/null +++ b/extra/llvm/llvm.factor @@ -0,0 +1,2 @@ +USING: kernel ; +IN: llvm