From: Luke Lau Date: Sun, 19 May 2019 13:23:31 +0000 (+0100) Subject: Generate code for call expressions X-Git-Url: http://git.lukelau.me/?p=kaleidoscope-hs.git;a=commitdiff_plain;h=795fc872e603cc359ec6e307969ac925c3b5dc4d Generate code for call expressions In order to call a function in LLVM IR, we need to grab a GlobalReference to it. Currently we just make it ourselves, inferring the type of it based on what arguments the caller provided and the fact that all functions return a double. --- diff --git a/Main.hs b/Main.hs index f32003b..2a5a7e0 100644 --- a/Main.hs +++ b/Main.hs @@ -7,6 +7,7 @@ import Control.Monad.IO.Class import Data.String import qualified Data.Map as Map import qualified Data.Text.Lazy.IO as Text +import LLVM.AST.AddrSpace import LLVM.AST.Constant import LLVM.AST.Float import LLVM.AST.FloatingPointPredicate hiding (False, True) @@ -80,3 +81,12 @@ buildExpr (BinOp op a b) = do K.Cmp LT -> fcmp OLT K.Cmp GT -> fcmp OGT K.Cmp EQ -> fcmp OEQ + +buildExpr (Call callee params) = do + paramOps <- mapM buildExpr params + let nam = fromString callee + -- get a pointer to the function + typ = FunctionType Type.double (replicate (length params) Type.double) False + ptrTyp = Type.PointerType typ (AddrSpace 0) + ref = GlobalReference ptrTyp nam + call (ConstantOperand ref) (zip paramOps (repeat []))