Skip to content

Instantly share code, notes, and snippets.

@cocreature
Created January 29, 2017 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocreature/93625b5afac68eb7de8b2eaa44561ab6 to your computer and use it in GitHub Desktop.
Save cocreature/93625b5afac68eb7de8b2eaa44561ab6 to your computer and use it in GitHub Desktop.
module Main where
import LLVM.AST
import qualified LLVM.AST as AST
import LLVM.AST.AddrSpace
import qualified LLVM.AST.CallingConvention as CC
import qualified LLVM.AST.Constant as C
import LLVM.AST.Global
import LLVM.AST.Linkage
import LLVM.Context
import LLVM.Module
import Control.Monad.Except
module_ :: AST.Module
module_ =
defaultModule
{ moduleName = "<string>"
, moduleSourceFileName = "foo.c"
, moduleDefinitions =
[ GlobalDefinition
(globalVariableDefaults
{ name = Name ".str"
, linkage = Private
, unnamedAddr = Just GlobalAddr
, isConstant = True
, LLVM.AST.Global.type' =
ArrayType
{nArrayElements = 4, elementType = IntegerType {typeBits = 8}}
, initializer =
Just
(C.Array
{ C.memberType = IntegerType {typeBits = 8}
, C.memberValues =
[ C.Int {C.integerBits = 8, C.integerValue = 37}
, C.Int {C.integerBits = 8, C.integerValue = 100}
, C.Int {C.integerBits = 8, C.integerValue = 10}
, C.Int {C.integerBits = 8, C.integerValue = 0}
]
})
, LLVM.AST.Global.alignment = 1
})
, GlobalDefinition
(functionDefaults
{ returnType = VoidType
, name = Name "f"
, parameters = ([], False)
, basicBlocks =
[ BasicBlock
(UnName 0)
[ UnName 1 :=
Call
{ tailCallKind = Nothing
, AST.callingConvention = CC.C
, AST.returnAttributes = []
, function =
Right
(ConstantOperand
(C.GlobalReference
(PointerType
{ pointerReferent =
FunctionType
{ resultType = IntegerType {typeBits = 32}
, argumentTypes =
[ PointerType
{ pointerReferent =
IntegerType {typeBits = 8}
, pointerAddrSpace = AddrSpace 0
}
]
, isVarArg = True
}
, pointerAddrSpace = AddrSpace 0
})
(Name "printf")))
, arguments =
[ ( ConstantOperand
(C.GetElementPtr
{ C.inBounds = True
, C.address =
C.GlobalReference
(PointerType
{ pointerReferent =
ArrayType
{ nArrayElements = 4
, elementType =
IntegerType {typeBits = 8}
}
, pointerAddrSpace = AddrSpace 0
})
(Name ".str")
, C.indices =
[ C.Int
{C.integerBits = 32, C.integerValue = 0}
, C.Int
{C.integerBits = 32, C.integerValue = 0}
]
})
, [])
, ( ConstantOperand
(C.Int {C.integerBits = 32, C.integerValue = 42})
, [])
]
, AST.functionAttributes = []
, metadata = []
}
]
(Do (Ret {returnOperand = Nothing, metadata' = []}))
]
})
, GlobalDefinition
(functionDefaults
{ returnType = IntegerType {typeBits = 32}
, name = Name "printf"
, parameters =
( [ Parameter
(PointerType
{ pointerReferent = IntegerType {typeBits = 8}
, pointerAddrSpace = AddrSpace 0
})
(UnName 0)
[]
]
, True)
})
]
}
toLLVM :: AST.Module -> IO ()
toLLVM mod = withContext $ \ctx -> do
errOrLLVM <- runExceptT $ withModuleFromAST ctx mod moduleLLVMAssembly
case errOrLLVM of
Left err -> putStrLn $ "error: " ++ err
Right llvm -> putStrLn llvm
main :: IO ()
main = toLLVM module_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment