Skip to content

Commit

Permalink
Handle function types correctly when pretty-printing call(br)
Browse files Browse the repository at this point in the history
Previously, the code in `ppCallSym` assumed that the function type stored in a
`call` instruction was a pointer type, but this should actually be a raw
function type. See the discussion at
GaloisInc/llvm-pretty-bc-parser#189 (comment)
for more on this.  We now assume the convention of raw function types in `call`
instructions, as well as the related `callbr` and `invoke` instructions.

This addresses one part of #102.
  • Loading branch information
RyanGlScott committed Apr 3, 2023
1 parent bada126 commit b13493f
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/Text/LLVM/PP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ ppCall tc ty f args
ppCallBr :: LLVM => Type -> Value -> [Typed Value] -> BlockLabel -> [BlockLabel] -> Doc
ppCallBr ty f args to indirectDests =
"callbr"
<+> ppType res <+> ppValue f <> parens (commas (map (ppTyped ppValue) args))
<+> ppCallSym res f <> parens (commas (map (ppTyped ppValue) args))
<+> "to" <+> ppLab to <+> brackets (commas (map ppLab indirectDests))
where
ppLab l = ppType (PrimType Label) <+> ppLabel l
Expand All @@ -693,9 +693,30 @@ ppCallBr ty f args to indirectDests =
PtrTo (FunTy r _ _) -> r
_ -> PrimType Void

-- | Print out the @<ty>|<fnty> <fnptrval>@ portion of a @call@, @callbr@, or
-- @invoke@ instruction, where:
--
-- * @<ty>@ is the return type.
--
-- * @<fnty>@ is the overall function type.
--
-- * @<fnptrval>@ is a pointer value, where the memory it points to is treated
-- as a value of type @<fnty>@.
--
-- The LLVM Language Reference Manual indicates that either @<ty>@ or @<fnty>@
-- can be used, but in practice, @<ty>@ is typically preferred unless the
-- function type involves varargs. We adopt the same convention here.
ppCallSym :: LLVM => Type -> Value -> Doc
ppCallSym (PtrTo (FunTy res args va)) val = ppType res <+> ppArgList va (map ppType args) <+> ppValue val
ppCallSym ty val = ppType ty <+> ppValue val
ppCallSym ty val = pp_ty <+> ppValue val
where
pp_ty =
case ty of
FunTy res args va
| va
-> ppType res <+> ppArgList va (map ppType args)
| otherwise
-> ppType res
_ -> ppType ty

ppGEP :: LLVM => Bool -> Typed Value -> [Typed Value] -> Doc
ppGEP ib ptr ixs = "getelementptr" <+> inbounds
Expand All @@ -715,7 +736,7 @@ ppGEP ib ptr ixs = "getelementptr" <+> inbounds
ppInvoke :: LLVM => Type -> Value -> [Typed Value] -> BlockLabel -> BlockLabel -> Doc
ppInvoke ty f args to uw = body
where
body = "invoke" <+> ppType ty <+> ppValue f
body = "invoke" <+> ppCallSym ty f
<> parens (commas (map (ppTyped ppValue) args))
<+> "to" <+> ppType (PrimType Label) <+> ppLabel to
<+> "unwind" <+> ppType (PrimType Label) <+> ppLabel uw
Expand Down

0 comments on commit b13493f

Please sign in to comment.