diff --git a/llvm-hs-pure/src/LLVM/IRBuilder/Constant.hs b/llvm-hs-pure/src/LLVM/IRBuilder/Constant.hs index 88925d90..d249c3d0 100644 --- a/llvm-hs-pure/src/LLVM/IRBuilder/Constant.hs +++ b/llvm-hs-pure/src/LLVM/IRBuilder/Constant.hs @@ -13,6 +13,9 @@ int64 = pure . ConstantOperand . Int 64 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 diff --git a/llvm-hs-pure/src/LLVM/IRBuilder/Instruction.hs b/llvm-hs-pure/src/LLVM/IRBuilder/Instruction.hs index 5818efe9..8bcd9ab2 100644 --- a/llvm-hs-pure/src/LLVM/IRBuilder/Instruction.hs +++ b/llvm-hs-pure/src/LLVM/IRBuilder/Instruction.hs @@ -7,6 +7,7 @@ import Prelude hiding (and, or, pred) 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,6 +19,9 @@ import qualified LLVM.AST.Constant as C 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 @@ -288,3 +292,32 @@ condBr cond tdest fdest = emitTerm $ CondBr cond tdest fdest [] -- | See . 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 + } + let address = ConstantOperand $ C.GlobalReference (ptr ty) nm + inBounds = True + indices = [ConstantOperand (C.Int 32 0), ConstantOperand (C.Int 32 0)] + metaData = [] + gepInstr = GetElementPtr inBounds address indices metaData + emitInstr charStar gepInstr diff --git a/llvm-hs-pure/src/LLVM/IRBuilder/Module.hs b/llvm-hs-pure/src/LLVM/IRBuilder/Module.hs index 09fc1e28..f76fd59c 100644 --- a/llvm-hs-pure/src/LLVM/IRBuilder/Module.hs +++ b/llvm-hs-pure/src/LLVM/IRBuilder/Module.hs @@ -201,30 +201,6 @@ global nm ty initVal = do } 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 - 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 - } - pure $ ConstantOperand $ C.BitCast (C.GlobalReference (ptr ty) nm) charStar - -- | A named type definition typedef :: MonadModuleBuilder m pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy