From bada126187ba0dccef9a9b60b4c99c4c700d8c74 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 19 Mar 2023 15:14:15 -0400 Subject: [PATCH] Add PtrOpaque type This adds basic support for representing and pretty-printing opaque `ptr` types, which were introduced in LLVM 13 as part of a longer-term plan to stop using non-opaque pointer types (e.g., `i8*`). See https://llvm.org/docs/OpaquePointers.html for further details. The next step in `llvm-pretty` is to make sure that all of the instructions that involve pointers in some way (e.g., `call`) have all the information that they need to work with opaque pointers. I will be addressing this in subsequent commits. This is one step towards #102. --- src/Text/LLVM/AST.hs | 25 +++++++++++++++++++++++++ src/Text/LLVM/PP.hs | 1 + src/Text/LLVM/Parser.hs | 1 + 3 files changed, 27 insertions(+) diff --git a/src/Text/LLVM/AST.hs b/src/Text/LLVM/AST.hs index 7c9b32f..29aca19 100644 --- a/src/Text/LLVM/AST.hs +++ b/src/Text/LLVM/AST.hs @@ -260,10 +260,32 @@ data Type' ident | Array Word64 (Type' ident) | FunTy (Type' ident) [Type' ident] Bool | PtrTo (Type' ident) + -- ^ A pointer to a memory location of a particular type. See also + -- 'PtrOpaque', which represents a pointer without a pointee type. + -- + -- LLVM pointers can also have an optional address space attribute, but this + -- is not currently represented in the @llvm-pretty@ AST. + | PtrOpaque + -- ^ A pointer to a memory location. Unlike 'PtrTo', a 'PtrOpaque' does not + -- have a pointee type. Instead, instructions interacting through opaque + -- pointers specify the type of the underlying memory they are interacting + -- with. + -- + -- LLVM pointers can also have an optional address space attribute, but this + -- is not currently represented in the @llvm-pretty@ AST. + -- + -- 'PtrOpaque' should not be confused with 'Opaque', which is a completely + -- separate type with a similar-sounding name. | Struct [Type' ident] | PackedStruct [Type' ident] | Vector Word64 (Type' ident) | Opaque + -- ^ An opaque structure type, used to represent structure types that do not + -- have a body specified. This is similar to C's notion of a + -- forward-declared structure. + -- + -- 'Opaque' should not be confused with 'PtrOpaque', which is a completely + -- separate type with a similar-sounding name. deriving (Data, Eq, Functor, Generic, Generic1, Ord, Show, Typeable) -- | Applicatively traverse a type, updating or removing aliases. @@ -274,6 +296,7 @@ updateAliasesA f = loop Array len ety -> Array len <$> (loop ety) FunTy res ps var -> FunTy <$> (loop res) <*> (traverse loop ps) <*> pure var PtrTo pty -> PtrTo <$> (loop pty) + PtrOpaque -> pure PtrOpaque Struct fs -> Struct <$> (traverse loop fs) PackedStruct fs -> PackedStruct <$> (traverse loop fs) Vector len ety -> Vector <$> pure len <*> (loop ety) @@ -320,6 +343,7 @@ isArray ty = case ty of isPointer :: Type -> Bool isPointer (PtrTo _) = True +isPointer PtrOpaque = True isPointer _ = False @@ -345,6 +369,7 @@ floatTypeNull _ = error "must be a float type" typeNull :: Type -> NullResult lab typeNull (PrimType pt) = HasNull (primTypeNull pt) typeNull PtrTo{} = HasNull ValNull +typeNull PtrOpaque = HasNull ValNull typeNull (Alias i) = ResolveNull i typeNull _ = HasNull ValZeroInit diff --git a/src/Text/LLVM/PP.hs b/src/Text/LLVM/PP.hs index 3627312..1d93885 100644 --- a/src/Text/LLVM/PP.hs +++ b/src/Text/LLVM/PP.hs @@ -241,6 +241,7 @@ ppType (PrimType pt) = ppPrimType pt ppType (Alias i) = ppIdent i ppType (Array len ty) = brackets (integral len <+> char 'x' <+> ppType ty) ppType (PtrTo ty) = ppType ty <> char '*' +ppType PtrOpaque = "ptr" ppType (Struct ts) = structBraces (commas (map ppType ts)) ppType (PackedStruct ts) = angles (structBraces (commas (map ppType ts))) ppType (FunTy r as va) = ppType r <> ppArgList va (map ppType as) diff --git a/src/Text/LLVM/Parser.hs b/src/Text/LLVM/Parser.hs index ba76a15..a689be2 100644 --- a/src/Text/LLVM/Parser.hs +++ b/src/Text/LLVM/Parser.hs @@ -73,6 +73,7 @@ pType = pType0 >>= pFunPtr , angles (braces (PackedStruct <$> pTypeList) <|> spaced (pNumType Vector)) , string "opaque" >> return Opaque , PrimType <$> pPrimType + , string "ptr" >> return PtrOpaque ] pTypeList :: Parser [Type]