llvm-hs-pure 7.0.0 → 8.0.0
raw patch · 7 files changed
+143/−35 lines, 7 files
Files
- CHANGELOG.md +6/−0
- llvm-hs-pure.cabal +3/−3
- src/LLVM/AST/Operand.hs +26/−5
- src/LLVM/AST/Type.hs +2/−2
- src/LLVM/IRBuilder/Constant.hs +3/−0
- src/LLVM/IRBuilder/Instruction.hs +103/−1
- src/LLVM/IRBuilder/Module.hs +0/−24
CHANGELOG.md view
@@ -1,3 +1,9 @@+## 8.0.0 (2019-03-10)++* Upgrade to LLVM 8+* Change type of `value` field in `DITemplateValueParameter` to+ `Maybe Metadata` to reflect that it can be null.+ ## 7.0.0 (2018-09-28) * Track type definitions in `MonadModuleBuilder`. This allows us to
llvm-hs-pure.cabal view
@@ -1,5 +1,5 @@ name: llvm-hs-pure-version: 7.0.0+version: 8.0.0 license: BSD3 license-file: LICENSE author: Anthony Cowley, Stephen Diehl, Moritz Kiefer <moritz.kiefer@purelyfunctional.org>, Benjamin S. Scarlet@@ -16,13 +16,13 @@ llvm-hs-pure is a set of pure Haskell types and functions for interacting with LLVM <http://llvm.org/>. It includes an ADT to represent LLVM IR (<http://llvm.org/docs/LangRef.html>). The llvm-hs package builds on this one with FFI bindings to LLVM, but llvm-hs-pure does not require LLVM to be available.-tested-with: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.1+tested-with: GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.4, GHC == 8.6.4 extra-source-files: CHANGELOG.md source-repository head type: git location: git://github.com/llvm-hs/llvm-hs.git- branch: llvm-7+ branch: llvm-8 library default-language: Haskell2010
src/LLVM/AST/Operand.hs view
@@ -65,10 +65,21 @@ | DW_OP_StackValue -- ^ Must be the last one or followed by a DW_OP_LLVM_Fragment | DW_OP_Swap | DW_OP_ConstU Word64+ | DW_OP_Lit0 | DW_OP_PlusUConst Word64 | DW_OP_Plus | DW_OP_Minus | DW_OP_Mul+ | DW_OP_Div+ | DW_OP_Mod+ | DW_OP_Not+ | DW_OP_Or+ | DW_OP_Xor+ | DW_OP_And+ | DW_OP_Shr+ | DW_OP_Shra+ | DW_OP_Shl+ | DW_OP_Dup | DW_OP_Deref | DW_OP_XDeref deriving (Eq, Ord, Read, Show, Typeable, Data, Generic)@@ -235,6 +246,9 @@ data DebugEmissionKind = NoDebug | FullDebug | LineTablesOnly deriving (Eq, Ord, Read, Show, Typeable, Data, Generic) +data DebugNameTableKind = NameTableKindDefault | NameTableKindGNU | NameTableKindNone+ deriving (Eq, Ord, Read, Show, Typeable, Data, Generic)+ -- | <https://llvm.org/docs/LangRef.html#dicompileunit> data DICompileUnit = CompileUnit { language :: Word32@@ -253,7 +267,8 @@ , dWOId :: Word64 , splitDebugInlining :: Bool , debugInfoForProfiling :: Bool- , gnuPubnames :: Bool+ , nameTableKind :: DebugNameTableKind+ , debugBaseAddress :: Bool } deriving (Eq, Ord, Read, Show, Typeable, Data, Generic) -- | <https://llvm.org/docs/LangRef.html#difile>@@ -322,6 +337,7 @@ , alignInBits :: Word32 , encoding :: Maybe Encoding , tag :: BasicTypeTag+ , flags :: [DIFlag] } deriving (Eq, Ord, Read, Show, Typeable, Data, Generic) -- | <https://llvm.org/docs/LangRef.html#disubroutinetype>@@ -356,7 +372,8 @@ , file :: Maybe (MDRef DIFile) , line :: Word32 , scope :: Maybe (MDRef DIScope)- , baseType :: MDRef DIType+ , baseType :: Maybe (MDRef DIType)+ -- ^ This can be `Nothing` to represent @void *@ , sizeInBits :: Word64 , alignInBits :: Word32 , offsetInBits :: Word64@@ -440,6 +457,7 @@ | SignedCharEncoding | UnsignedEncoding | UnsignedCharEncoding+ | UTFEncoding deriving (Eq, Ord, Read, Show, Typeable, Data, Generic) data TemplateValueParameterTag@@ -452,13 +470,15 @@ data DITemplateParameter = DITemplateTypeParameter { name :: ShortByteString- , type' :: MDRef DIType+ , type' :: Maybe (MDRef DIType)+ -- ^ For DITemplateTypeParameter this field is required,+ -- for DITemplateValueParameter it is optional. } -- ^ <https://llvm.org/docs/LangRef.html#ditemplatetypeparameter> | DITemplateValueParameter { name :: ShortByteString- , type' :: MDRef DIType- , value :: Metadata+ , type' :: Maybe (MDRef DIType)+ , value :: Maybe Metadata , tag :: TemplateValueParameterTag } -- ^ <https://llvm.org/docs/LangRef.html#ditemplatevalueparameter>@@ -498,6 +518,7 @@ , local :: Bool , definition :: Bool , staticDataMemberDeclaration :: Maybe (MDRef DIDerivedType)+ , templateParams :: [MDRef DITemplateParameter] , alignInBits :: Word32 } deriving (Eq, Ord, Read, Show, Typeable, Data, Generic)
src/LLVM/AST/Type.hs view
@@ -38,9 +38,9 @@ | NamedTypeReference Name -- | <http://llvm.org/docs/LangRef.html#metadata-type> | MetadataType -- only to be used as a parameter type for a few intrinsics- -- | <http://llvm.org/docs/LangRef.html#token-type>- | LabelType -- only to be used as the type of block names -- | <http://llvm.org/docs/LangRef.html#label-type>+ | LabelType -- only to be used as the type of block names+ -- | <http://llvm.org/docs/LangRef.html#token-type> | TokenType deriving (Eq, Ord, Read, Show, Typeable, Data, Generic)
src/LLVM/IRBuilder/Constant.hs view
@@ -13,6 +13,9 @@ int32 :: Applicative f => Integer -> f Operand int32 = pure . ConstantOperand . Int 32 +int8 :: Applicative f => Integer -> f Operand+int8 = pure . ConstantOperand . Int 8+ bit :: Applicative f => Integer -> f Operand bit = pure . ConstantOperand . Int 1
src/LLVM/IRBuilder/Instruction.hs view
@@ -7,6 +7,7 @@ import Control.Monad.State (gets) import qualified Data.Map.Lazy as Map import Data.Word+import Data.Char (ord) import LLVM.AST hiding (args, dests) import LLVM.AST.Type as AST@@ -18,63 +19,89 @@ import qualified LLVM.AST.IntegerPredicate as IP import qualified LLVM.AST.FloatingPointPredicate as FP +import LLVM.AST.Global+import LLVM.AST.Linkage+ import LLVM.IRBuilder.Monad import LLVM.IRBuilder.Module +-- | See <https://llvm.org/docs/LangRef.html#fadd-instruction reference>. fadd :: MonadIRBuilder m => Operand -> Operand -> m Operand fadd a b = emitInstr (typeOf a) $ FAdd noFastMathFlags a b [] +-- | See <https://llvm.org/docs/LangRef.html#fmul-instruction reference>. fmul :: MonadIRBuilder m => Operand -> Operand -> m Operand fmul a b = emitInstr (typeOf a) $ FMul noFastMathFlags a b [] +-- | See <https://llvm.org/docs/LangRef.html#fsub-instruction reference>. fsub :: MonadIRBuilder m => Operand -> Operand -> m Operand fsub a b = emitInstr (typeOf a) $ FSub noFastMathFlags a b [] +-- | See <https://llvm.org/docs/LangRef.html#fdiv-instruction reference>. fdiv :: MonadIRBuilder m => Operand -> Operand -> m Operand fdiv a b = emitInstr (typeOf a) $ FDiv noFastMathFlags a b [] +-- | See <https://llvm.org/docs/LangRef.html#frem-instruction reference>. frem :: MonadIRBuilder m => Operand -> Operand -> m Operand frem a b = emitInstr (typeOf a) $ FRem noFastMathFlags a b [] +-- | See <https://llvm.org/docs/LangRef.html#add-instruction reference>. add :: MonadIRBuilder m => Operand -> Operand -> m Operand add a b = emitInstr (typeOf a) $ Add False False a b [] +-- | See <https://llvm.org/docs/LangRef.html#mul-instruction reference>. mul :: MonadIRBuilder m => Operand -> Operand -> m Operand mul a b = emitInstr (typeOf a) $ Mul False False a b [] +-- | See <https://llvm.org/docs/LangRef.html#sub-instruction reference>. sub :: MonadIRBuilder m => Operand -> Operand -> m Operand sub a b = emitInstr (typeOf a) $ Sub False False a b [] +-- | See <https://llvm.org/docs/LangRef.html#udiv-instruction reference>. udiv :: MonadIRBuilder m => Operand -> Operand -> m Operand udiv a b = emitInstr (typeOf a) $ UDiv False a b [] +-- | See <https://llvm.org/docs/LangRef.html#sdiv-instruction reference>. sdiv :: MonadIRBuilder m => Operand -> Operand -> m Operand sdiv a b = emitInstr (typeOf a) $ SDiv False a b [] +-- | See <https://llvm.org/docs/LangRef.html#urem-instruction reference>. urem :: MonadIRBuilder m => Operand -> Operand -> m Operand urem a b = emitInstr (typeOf a) $ URem a b [] +-- | See <https://llvm.org/docs/LangRef.html#srem-instruction reference>.+srem :: MonadIRBuilder m => Operand -> Operand -> m Operand+srem a b = emitInstr (typeOf a) $ SRem a b []++-- | See <https://llvm.org/docs/LangRef.html#shl-instruction reference>. shl :: MonadIRBuilder m => Operand -> Operand -> m Operand shl a b = emitInstr (typeOf a) $ Shl False False a b [] +-- | See <https://llvm.org/docs/LangRef.html#lshl-instruction reference>. lshr :: MonadIRBuilder m => Operand -> Operand -> m Operand lshr a b = emitInstr (typeOf a) $ LShr True a b [] +-- | See <https://llvm.org/docs/LangRef.html#ashl-instruction reference>. ashr :: MonadIRBuilder m => Operand -> Operand -> m Operand ashr a b = emitInstr (typeOf a) $ AShr True a b [] +-- | See <https://llvm.org/docs/LangRef.html#and-instruction reference>. and :: MonadIRBuilder m => Operand -> Operand -> m Operand and a b = emitInstr (typeOf a) $ And a b [] +-- | See <https://llvm.org/docs/LangRef.html#or-instruction reference>. or :: MonadIRBuilder m => Operand -> Operand -> m Operand or a b = emitInstr (typeOf a) $ Or a b [] +-- | See <https://llvm.org/docs/LangRef.html#xor-instruction reference>. xor :: MonadIRBuilder m => Operand -> Operand -> m Operand xor a b = emitInstr (typeOf a) $ Xor a b [] +-- | See <https://llvm.org/docs/LangRef.html#alloca-instruction reference>. alloca :: MonadIRBuilder m => Type -> Maybe Operand -> Word32 -> m Operand alloca ty count align = emitInstr (ptr ty) $ Alloca ty count align [] +-- | See <https://llvm.org/docs/LangRef.html#load-instruction reference>. load :: MonadIRBuilder m => Operand -> Word32 -> m Operand load a align = emitInstr retty $ Load False a Nothing align [] where@@ -82,9 +109,12 @@ PointerType ty _ -> ty _ -> error "Cannot load non-pointer (Malformed AST)." +-- | See <https://llvm.org/docs/LangRef.html#store-instruction reference>. store :: MonadIRBuilder m => Operand -> Word32 -> Operand -> m () store addr align val = emitInstrVoid $ Store False addr val Nothing align [] +-- | Emit the @getelementptr@ instruction.+-- See <https://llvm.org/docs/LangRef.html#getelementptr-instruction reference>. gep :: (MonadIRBuilder m, MonadModuleBuilder m) => Operand -> [Operand] -> m Operand gep addr is = do ty <- gepType (typeOf addr) is@@ -106,42 +136,67 @@ Just ty -> gepType ty is' gepType t (_:_) = error $ "gep: Can't index into a " ++ show t +-- | Emit the @trunc ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#trunc-to-instruction reference>. trunc :: MonadIRBuilder m => Operand -> Type -> m Operand trunc a to = emitInstr to $ Trunc a to [] +-- | Emit the @fptrunc ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#fptrunc-to-instruction reference>. fptrunc :: MonadIRBuilder m => Operand -> Type -> m Operand fptrunc a to = emitInstr to $ FPTrunc a to [] +-- | Emit the @zext ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#zext-to-instruction reference>. zext :: MonadIRBuilder m => Operand -> Type -> m Operand zext a to = emitInstr to $ ZExt a to [] +-- | Emit the @sext ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#sext-to-instruction reference>. sext :: MonadIRBuilder m => Operand -> Type -> m Operand sext a to = emitInstr to $ SExt a to [] +-- | Emit the @fptoui ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#fptoui-to-instruction reference>. fptoui :: MonadIRBuilder m => Operand -> Type -> m Operand fptoui a to = emitInstr to $ FPToUI a to [] +-- | Emit the @fptosi ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#fptosi-to-instruction reference>. fptosi :: MonadIRBuilder m => Operand -> Type -> m Operand fptosi a to = emitInstr to $ FPToSI a to [] +-- | Emit the @fpext ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#fpext-to-instruction reference>. fpext :: MonadIRBuilder m => Operand -> Type -> m Operand fpext a to = emitInstr to $ FPExt a to [] +-- | Emit the @uitofp ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#uitofp-to-instruction reference>. uitofp :: MonadIRBuilder m => Operand -> Type -> m Operand uitofp a to = emitInstr to $ UIToFP a to [] +-- | Emit the @sitofp ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#sitofp-to-instruction reference>. sitofp :: MonadIRBuilder m => Operand -> Type -> m Operand sitofp a to = emitInstr to $ SIToFP a to [] +-- | Emit the @ptrtoint ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#ptrtoint-to-instruction reference>. ptrtoint :: MonadIRBuilder m => Operand -> Type -> m Operand ptrtoint a to = emitInstr to $ PtrToInt a to [] +-- | Emit the @inttoptr ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#inttoptr-to-instruction reference>. inttoptr :: MonadIRBuilder m => Operand -> Type -> m Operand inttoptr a to = emitInstr to $ IntToPtr a to [] +-- | Emit the @bitcast ... to@ instruction.+-- See <https://llvm.org/docs/LangRef.html#bitcast-to-instruction reference>. bitcast :: MonadIRBuilder m => Operand -> Type -> m Operand bitcast a to = emitInstr to $ BitCast a to [] +-- | See <https://llvm.org/docs/LangRef.html#extractelement-instruction reference>. extractElement :: MonadIRBuilder m => Operand -> Operand -> m Operand extractElement v i = emitInstr elemTyp $ ExtractElement v i [] where elemTyp =@@ -149,9 +204,11 @@ VectorType _ typ -> typ _ -> error "extractElement: Expected a vector type (malformed AST)." +-- | See <https://llvm.org/docs/LangRef.html#insertelement-instruction reference>. insertElement :: MonadIRBuilder m => Operand -> Operand -> Operand -> m Operand insertElement v e i = emitInstr (typeOf v) $ InsertElement v e i [] +-- | See <https://llvm.org/docs/LangRef.html#shufflevector-instruction reference>. shuffleVector :: MonadIRBuilder m => Operand -> Operand -> C.Constant -> m Operand shuffleVector a b m = emitInstr retType $ ShuffleVector a b m [] where retType =@@ -160,31 +217,41 @@ _ -> error "shuffleVector: Expected two vectors and a vector mask" +-- | See <https://llvm.org/docs/LangRef.html#extractvalue-instruction reference>. extractValue :: MonadIRBuilder m => Operand -> [Word32] -> m Operand extractValue a i = emitInstr (extractValueType i (typeOf a)) $ ExtractValue a i [] +-- | See <https://llvm.org/docs/LangRef.html#insertvalue-instruction reference>. insertValue :: MonadIRBuilder m => Operand -> Operand -> [Word32] -> m Operand insertValue a e i = emitInstr (typeOf a) $ InsertValue a e i [] +-- | See <https://llvm.org/docs/LangRef.html#icmp-instruction reference>. icmp :: MonadIRBuilder m => IP.IntegerPredicate -> Operand -> Operand -> m Operand icmp pred a b = emitInstr i1 $ ICmp pred a b [] +-- | See <https://llvm.org/docs/LangRef.html#fcmp-instruction reference>. fcmp :: MonadIRBuilder m => FP.FloatingPointPredicate -> Operand -> Operand -> m Operand fcmp pred a b = emitInstr i1 $ FCmp pred a b [] --- | Unconditional branch+-- | Unconditional branch.+-- Emit a @br label <dest>@ instruction+-- See <https://llvm.org/docs/LangRef.html#br-instruction reference>. br :: MonadIRBuilder m => Name -> m () br val = emitTerm (Br val []) +-- | See <https://llvm.org/docs/LangRef.html#phi-instruction reference>. phi :: MonadIRBuilder m => [(Operand, Name)] -> m Operand phi [] = emitInstr AST.void $ Phi AST.void [] [] phi incoming@(i:_) = emitInstr ty $ Phi ty incoming [] where ty = typeOf (fst i) -- result type +-- | Emit a @ret void@ instruction.+-- See <https://llvm.org/docs/LangRef.html#ret-instruction reference>. retVoid :: MonadIRBuilder m => m () retVoid = emitTerm (Ret Nothing []) +-- | See <https://llvm.org/docs/LangRef.html#call-instruction reference>. call :: MonadIRBuilder m => Operand -> [(Operand, [ParameterAttribute])] -> m Operand call fun args = do let instr = Call {@@ -205,17 +272,52 @@ _ -> emitInstr r instr _ -> error "Cannot call non-function (Malformed AST)." +-- | See <https://llvm.org/docs/LangRef.html#ret-instruction reference>. ret :: MonadIRBuilder m => Operand -> m () ret val = emitTerm (Ret (Just val) []) +-- | See <https://llvm.org/docs/LangRef.html#switch-instruction reference>. switch :: MonadIRBuilder m => Operand -> Name -> [(C.Constant, Name)] -> m () switch val def dests = emitTerm $ Switch val def dests [] +-- | See <https://llvm.org/docs/LangRef.html#select-instruction reference>. select :: MonadIRBuilder m => Operand -> Operand -> Operand -> m Operand select cond t f = emitInstr (typeOf t) $ Select cond t f [] +-- | Conditional branch (see 'br' for unconditional instructions).+-- See <https://llvm.org/docs/LangRef.html#br-instruction reference>. condBr :: MonadIRBuilder m => Operand -> Name -> Name -> m () condBr cond tdest fdest = emitTerm $ CondBr cond tdest fdest [] +-- | See <https://llvm.org/docs/LangRef.html#unreachable-instruction reference>. unreachable :: MonadIRBuilder m => m () unreachable = emitTerm $ Unreachable []++-- | Creates a series of instructions to generate a pointer to a string+-- constant. Useful for making format strings to pass to @printf@, for example+globalStringPtr+ :: (MonadModuleBuilder m, MonadIRBuilder m)+ => String -- ^ The string to generate+ -> Name -- ^ Variable name of the pointer+ -> m Operand+globalStringPtr str nm = do+ let asciiVals = map (fromIntegral . ord) str+ llvmVals = map (C.Int 8) (asciiVals ++ [0]) -- append null terminator+ char = IntegerType 8+ charStar = ptr char+ charArray = C.Array char llvmVals+ ty = LLVM.AST.Typed.typeOf charArray+ emitDefn $ GlobalDefinition globalVariableDefaults+ { name = nm+ , LLVM.AST.Global.type' = ty+ , linkage = External+ , isConstant = True+ , initializer = Just charArray+ , unnamedAddr = Just GlobalAddr+ }+ emitInstr charStar GetElementPtr+ { inBounds = True+ , address = ConstantOperand $ C.GlobalReference (ptr ty) nm+ , indices = [ConstantOperand (C.Int 32 0), ConstantOperand (C.Int 32 0)]+ , AST.metadata = []+ }
src/LLVM/IRBuilder/Module.hs view
@@ -47,7 +47,6 @@ import LLVM.AST.Global import LLVM.AST.Linkage import LLVM.AST.Type (ptr)-import qualified LLVM.AST.Typed import qualified LLVM.AST.Constant as C import LLVM.IRBuilder.Internal.SnocList@@ -200,29 +199,6 @@ , initializer = Just initVal } pure $ ConstantOperand $ C.GlobalReference (ptr ty) nm---- | Creates a series of instructions to generate a pointer to a string--- constant. Useful for making format strings to pass to @printf@, for example-globalStringPtr- :: MonadModuleBuilder m- => String -- ^ The string to generate- -> Name -- ^ Variable name of the pointer- -> m Operand-globalStringPtr str nm = do- let asciiVals = map (fromIntegral . ord) str- llvmVals = map (C.Int 8) (asciiVals ++ [0]) -- append null terminator- char = IntegerType 8- charStar = ptr char- charArray = C.Array char llvmVals- emitDefn $ GlobalDefinition globalVariableDefaults- { name = nm- , LLVM.AST.Global.type' = LLVM.AST.Typed.typeOf charArray- , linkage = External- , isConstant = True- , initializer = Just charArray- , unnamedAddr = Just GlobalAddr- }- pure $ ConstantOperand $ C.BitCast (C.GlobalReference charStar nm) charStar -- | A named type definition typedef