Skip to content

Commit

Permalink
Add PtrOpaque type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RyanGlScott committed Apr 3, 2023
1 parent 6750fc2 commit bada126
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Text/LLVM/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -320,6 +343,7 @@ isArray ty = case ty of

isPointer :: Type -> Bool
isPointer (PtrTo _) = True
isPointer PtrOpaque = True
isPointer _ = False


Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/Text/LLVM/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions src/Text/LLVM/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit bada126

Please sign in to comment.