X-Git-Url: http://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=Main.hs;fp=Main.hs;h=749f77188172479f341a8018650af7549c2d0c02;hp=468573d865ab4fccb350bc78bf446d4e9d400563;hb=e3ba36b0fd73d28d041de61ed7d6bf590ef34f18;hpb=0d4ed473c520786af90ab70dac51451ea2b8b941 diff --git a/Main.hs b/Main.hs index 468573d..749f771 100644 --- a/Main.hs +++ b/Main.hs @@ -1,4 +1,5 @@ {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecursiveDo #-} import AST as K -- K for Kaleidoscope import Utils @@ -141,3 +142,24 @@ buildExpr (Call callee params) = do ptrTyp = Type.PointerType typ (AddrSpace 0) ref = GlobalReference ptrTyp nam call (ConstantOperand ref) (zip paramOps (repeat [])) + +buildExpr (If cond thenE elseE) = mdo + _ifB <- block `named` "if" + + -- since everything is a double, false == 0 + let zero = ConstantOperand (Float (Double 0)) + condV <- buildExpr cond + cmp <- fcmp ONE zero condV `named` "cmp" + + condBr cmp thenB elseB + + thenB <- block `named` "then" + thenOp <- buildExpr thenE + br mergeB + + elseB <- block `named` "else" + elseOp <- buildExpr elseE + br mergeB + + mergeB <- block `named` "ifcont" + phi [(thenOp, thenB), (elseOp, elseB)]