-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: meaningful progress on debug annotations for llvm backend
- Loading branch information
Showing
5 changed files
with
206 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Generate.LLVM.Debug where | ||
|
||
-- TODO: move Medatada stuff here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
module Generate.LLVM.Helper where | ||
import Control.Monad.State (MonadState, get, put) | ||
import Data.Word (Word8) | ||
|
||
newMetadataId :: MonadState Int m => m Word8 | ||
newMetadataId = do | ||
s <- get | ||
put (s + 1) | ||
return $ fromIntegral s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
{-# LANGUAGE DisambiguateRecordFields #-} | ||
module Generate.LLVM.WithMetadata where | ||
import GHC.Stack (HasCallStack) | ||
import LLVM.IRBuilder (MonadIRBuilder, MonadModuleBuilder, emitInstrVoid, emitInstr, ParameterName (NoParameterName, ParameterName), IRBuilderT, named, fresh, runIRBuilderT, emptyIRBuilder, emitDefn) | ||
import Data.ByteString.Short (ShortByteString) | ||
import LLVM.AST (MDRef, MDNode, Instruction (Call, tailCallKind, callingConvention, returnAttributes, function, arguments, functionAttributes, metadata), Type (FunctionType, VoidType, PointerType), Operand (ConstantOperand, LocalReference), Name, Definition (GlobalDefinition), Parameter (Parameter)) | ||
import LLVM.AST.Operand (Operand) | ||
import LLVM.AST.Attribute (ParameterAttribute) | ||
import qualified LLVM.AST.Constant as Constant | ||
import LLVM.AST.Type (void, ptr) | ||
import qualified LLVM.AST.CallingConvention as CC | ||
import LLVM.AST.Typed | ||
import qualified LLVM.AST.Global as Global | ||
import qualified LLVM.AST.Constant as C | ||
import Control.Monad (forM) | ||
|
||
|
||
callWithMetadata :: (HasCallStack, MonadIRBuilder m, MonadModuleBuilder m) => [(ShortByteString, MDRef MDNode)] -> Operand -> [(Operand, [ParameterAttribute])] -> m Operand | ||
callWithMetadata metadata fun args = do | ||
let instr = Call { | ||
tailCallKind = Nothing | ||
, callingConvention = CC.C | ||
, returnAttributes = [] | ||
, function = Right fun | ||
, arguments = args | ||
, functionAttributes = [] | ||
, metadata = metadata | ||
} | ||
case typeOf fun of | ||
(FunctionType r _ _) -> case r of | ||
VoidType -> emitInstrVoid instr >> pure (ConstantOperand (Constant.Undef void)) | ||
_ -> emitInstr r instr | ||
(PointerType (FunctionType r _ _) _) -> case r of | ||
VoidType -> emitInstrVoid instr >> pure (ConstantOperand (Constant.Undef void)) | ||
_ -> emitInstr r instr | ||
_ -> error "Cannot call non-function (Malformed AST)." | ||
|
||
functionWithMetadata | ||
:: MonadModuleBuilder m | ||
=> [(ShortByteString, MDRef MDNode)] | ||
-> Name -- ^ Function name | ||
-> [(Type, ParameterName)] -- ^ Parameter types and name suggestions | ||
-> Type -- ^ Return type | ||
-> ([Operand] -> IRBuilderT m ()) -- ^ Function body builder | ||
-> m Operand | ||
functionWithMetadata metadata label argtys retty body = do | ||
let tys = fst <$> argtys | ||
(paramNames, blocks) <- runIRBuilderT emptyIRBuilder $ do | ||
paramNames <- forM argtys $ \(_, paramName) -> case paramName of | ||
NoParameterName -> fresh | ||
ParameterName p -> fresh `named` p | ||
body $ zipWith LocalReference tys paramNames | ||
return paramNames | ||
let | ||
def = GlobalDefinition Global.functionDefaults | ||
{ Global.name = label | ||
, Global.parameters = (zipWith (\ty nm -> Parameter ty nm []) tys paramNames, False) | ||
, Global.returnType = retty | ||
, Global.basicBlocks = blocks | ||
, Global.metadata = metadata | ||
} | ||
funty = ptr $ FunctionType retty (fst <$> argtys) False | ||
emitDefn def | ||
pure $ ConstantOperand $ C.GlobalReference funty label |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters