Is it enough to type llvm-general net AST like llvm-ir?

I used llvm-general-pure to create abstract syntax trees for a program in LLVM .

Using the pretty printer provided , I get an output that looks like

A.Module { A.moduleName = "main", A.moduleDataLayout = Nothing, A.moduleTargetTriple = Nothing, A.moduleDefinitions = [ ... A.GlobalDefinition AGFunction { AGlinkage = ALExternal, AGvisibility = AVDefault, AGcallingConvention = A.CC.C, AGreturnAttributes = [], AGreturnType = A.IntegerType {A.typeBits = 32}, AGname = A.Name "Main", AGparameters = ([], False), AGfunctionAttributes = [], AGsection = Nothing, AGalignment = 0, AGgarbageCollectorName = Nothing, AGbasicBlocks = [ AGBasicBlock (A.Name "mainBlock") [ A.Name "n57" A.:= A.Alloca { A.allocatedType = A.IntegerType {A.typeBits = 64}, A.numElements = Nothing, A.alignment = 0, A.metadata = [] }, ... 

I need a conclusion that looks like

 define i32 @main() { mainBlock: %n57 = alloca i64 ... } ... 

It looks suspicious, like there is an automatically created parser for LLVM language in the llvm-general-quote package, but not a corresponding pretty printer.

Steven Dichl's excellent article hints at what is called moduleString .

+6
source share
1 answer

llvm-general-pure does not have a clean, pretty printer, you need to go through llvm-general . It can print IR by going through withModuleFromAST in Haskell AST to demonstrate the representation of the IR module (i.e. C ++ Module) and then call moduleLLVMAssembly to invoke a beautiful printer.

 moduleLLVMAssembly :: Mod.Module -> IO String withModuleFromAST :: Context -> AST.Module -> (Mod.Module -> IO a) -> ErrorT String IO a 

This is not pure Haskell, although all this happens through FFI to call the LLVM internal functions.

 import LLVM.General.Module as Mod import qualified LLVM.General.AST as AST ppModule :: AST.Module -> IO () ppModule ast = withContext $ \ctx -> runExceptT $ withModuleFromAST ctx ast $ \m -> do llstr <- moduleLLVMAssembly m putStrLn llstr 

There is no reason why we would not have a clean, pretty printer, although I started working on a project called llvm-pp , but it's just a lot of breathtakingly boring work to write a beautiful printer for the entire LLVM specification.

+6
source

Source: https://habr.com/ru/post/980790/


All Articles