module GHC.CmmToAsm.RV64.Ppr (pprNatCmmDecl, pprInstr) where

import GHC.Prelude hiding (EQ)

import GHC.CmmToAsm.RV64.Instr
import GHC.CmmToAsm.RV64.Regs
import GHC.CmmToAsm.RV64.Cond
import GHC.CmmToAsm.Ppr
import GHC.CmmToAsm.Format
import GHC.Platform.Reg
import GHC.CmmToAsm.Config
import GHC.CmmToAsm.Types
import GHC.CmmToAsm.Utils

import GHC.Cmm hiding (topInfoTable)
import GHC.Cmm.Dataflow.Collections
import GHC.Cmm.Dataflow.Label
import GHC.Types.Basic (Alignment, mkAlignment, alignmentBytes)

import GHC.Cmm.BlockId
import GHC.Cmm.CLabel

import GHC.Types.Unique ( pprUniqueAlways, getUnique )
import GHC.Platform
import GHC.Utils.Outputable

import GHC.Utils.Panic

pprProcAlignment :: IsDoc doc => NCGConfig -> doc
pprProcAlignment :: forall doc. IsDoc doc => NCGConfig -> doc
pprProcAlignment NCGConfig
config = doc -> (Int -> doc) -> Maybe Int -> doc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe doc
forall doc. IsOutput doc => doc
empty (Platform -> Alignment -> doc
forall doc. IsDoc doc => Platform -> Alignment -> doc
pprAlign Platform
platform (Alignment -> doc) -> (Int -> Alignment) -> Int -> doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Alignment
mkAlignment) (NCGConfig -> Maybe Int
ncgProcAlignment NCGConfig
config)
   where
      platform :: Platform
platform = NCGConfig -> Platform
ncgPlatform NCGConfig
config

pprNatCmmDecl :: IsDoc doc => NCGConfig -> NatCmmDecl RawCmmStatics Instr -> doc
pprNatCmmDecl :: forall doc.
IsDoc doc =>
NCGConfig -> NatCmmDecl RawCmmStatics Instr -> doc
pprNatCmmDecl NCGConfig
config (CmmData Section
section RawCmmStatics
dats) =
  NCGConfig -> Section -> doc
forall doc. IsDoc doc => NCGConfig -> Section -> doc
pprSectionAlign NCGConfig
config Section
section doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ NCGConfig -> RawCmmStatics -> doc
forall doc. IsDoc doc => NCGConfig -> RawCmmStatics -> doc
pprDatas NCGConfig
config RawCmmStatics
dats

pprNatCmmDecl NCGConfig
config proc :: NatCmmDecl RawCmmStatics Instr
proc@(CmmProc LabelMap RawCmmStatics
top_info CLabel
lbl [GlobalReg]
_ (ListGraph [GenBasicBlock Instr]
blocks)) =
  let platform :: Platform
platform = NCGConfig -> Platform
ncgPlatform NCGConfig
config in
  NCGConfig -> doc
forall doc. IsDoc doc => NCGConfig -> doc
pprProcAlignment NCGConfig
config doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
  case NatCmmDecl RawCmmStatics Instr -> Maybe RawCmmStatics
forall a i b. GenCmmDecl a (LabelMap i) (ListGraph b) -> Maybe i
topInfoTable NatCmmDecl RawCmmStatics Instr
proc of
    Maybe RawCmmStatics
Nothing ->
        -- special case for code without info table:
        NCGConfig -> Section -> doc
forall doc. IsDoc doc => NCGConfig -> Section -> doc
pprSectionAlign NCGConfig
config (SectionType -> CLabel -> Section
Section SectionType
Text CLabel
lbl) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
        -- do not
        -- pprProcAlignment config $$
        Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabel Platform
platform CLabel
lbl doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ -- blocks guaranteed not null, so label needed
        [doc] -> doc
forall doc. IsDoc doc => [doc] -> doc
vcat ((GenBasicBlock Instr -> doc) -> [GenBasicBlock Instr] -> [doc]
forall a b. (a -> b) -> [a] -> [b]
map (NCGConfig -> LabelMap RawCmmStatics -> GenBasicBlock Instr -> doc
forall doc.
IsDoc doc =>
NCGConfig -> LabelMap RawCmmStatics -> GenBasicBlock Instr -> doc
pprBasicBlock NCGConfig
config LabelMap RawCmmStatics
top_info) [GenBasicBlock Instr]
blocks) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
        Bool -> doc -> doc
forall doc. IsOutput doc => Bool -> doc -> doc
ppWhen (NCGConfig -> Bool
ncgDwarfEnabled NCGConfig
config)
          (Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprBlockEndLabel Platform
platform CLabel
lbl) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprProcEndLabel Platform
platform CLabel
lbl)) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
        Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprSizeDecl Platform
platform CLabel
lbl

    Just (CmmStaticsRaw CLabel
info_lbl [CmmStatic]
_) ->
      NCGConfig -> Section -> doc
forall doc. IsDoc doc => NCGConfig -> Section -> doc
pprSectionAlign NCGConfig
config (SectionType -> CLabel -> Section
Section SectionType
Text CLabel
info_lbl) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
      -- pprProcAlignment config $$
      (if Platform -> Bool
platformHasSubsectionsViaSymbols Platform
platform
          then Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkDeadStripPreventer CLabel
info_lbl) Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> Line doc
forall doc. IsLine doc => Char -> doc
char Char
':')
          else doc
forall doc. IsOutput doc => doc
empty) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
      [doc] -> doc
forall doc. IsDoc doc => [doc] -> doc
vcat ((GenBasicBlock Instr -> doc) -> [GenBasicBlock Instr] -> [doc]
forall a b. (a -> b) -> [a] -> [b]
map (NCGConfig -> LabelMap RawCmmStatics -> GenBasicBlock Instr -> doc
forall doc.
IsDoc doc =>
NCGConfig -> LabelMap RawCmmStatics -> GenBasicBlock Instr -> doc
pprBasicBlock NCGConfig
config LabelMap RawCmmStatics
top_info) [GenBasicBlock Instr]
blocks) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
      Bool -> doc -> doc
forall doc. IsOutput doc => Bool -> doc -> doc
ppWhen (NCGConfig -> Bool
ncgDwarfEnabled NCGConfig
config) (Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprProcEndLabel Platform
platform CLabel
info_lbl)) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
      -- above: Even the first block gets a label, because with branch-chain
      -- elimination, it might be the target of a goto.
      (if Platform -> Bool
platformHasSubsectionsViaSymbols Platform
platform
       then -- See Note [Subsections Via Symbols]
                Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line
              (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.long "
            Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
info_lbl
            Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Char -> Line doc
forall doc. IsLine doc => Char -> doc
char Char
'-'
            Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkDeadStripPreventer CLabel
info_lbl)
       else doc
forall doc. IsOutput doc => doc
empty) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
      Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprSizeDecl Platform
platform CLabel
info_lbl
{-# SPECIALIZE pprNatCmmDecl :: NCGConfig -> NatCmmDecl RawCmmStatics Instr -> SDoc #-}
{-# SPECIALIZE pprNatCmmDecl :: NCGConfig -> NatCmmDecl RawCmmStatics Instr -> HDoc #-} -- see Note [SPECIALIZE to HDoc] in GHC.Utils.Outputable

pprLabel :: IsDoc doc => Platform -> CLabel -> doc
pprLabel :: forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabel Platform
platform CLabel
lbl =
   Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprGloblDecl Platform
platform CLabel
lbl
   doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprTypeDecl Platform
platform CLabel
lbl
   doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> Line doc
forall doc. IsLine doc => Char -> doc
char Char
':')

pprAlign :: IsDoc doc => Platform -> Alignment -> doc
pprAlign :: forall doc. IsDoc doc => Platform -> Alignment -> doc
pprAlign Platform
_platform Alignment
alignment
        = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.balign " Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int (Alignment -> Int
alignmentBytes Alignment
alignment)

-- | Print appropriate alignment for the given section type.
pprAlignForSection :: IsDoc doc => Platform -> SectionType -> doc
pprAlignForSection :: forall doc. IsDoc doc => Platform -> SectionType -> doc
pprAlignForSection Platform
_platform SectionType
_seg
    -- .balign is stable, whereas .align is platform dependent.
    = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.balign 8") --  always 8

-- | Print section header and appropriate alignment for that section.
--
-- This one will emit the header:
--
--     .section .text
--     .balign 8
--
pprSectionAlign :: IsDoc doc => NCGConfig -> Section -> doc
pprSectionAlign :: forall doc. IsDoc doc => NCGConfig -> Section -> doc
pprSectionAlign NCGConfig
_config (Section (OtherSection String
_) CLabel
_) =
  -- TODO: Valid for RISCV64?
  String -> doc
forall a. HasCallStack => String -> a
panic String
"AArch64.Ppr.pprSectionAlign: unknown section"
pprSectionAlign NCGConfig
config sec :: Section
sec@(Section SectionType
seg CLabel
_) =
    Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (NCGConfig -> Section -> Line doc
forall doc. IsLine doc => NCGConfig -> Section -> doc
pprSectionHeader NCGConfig
config Section
sec)
    doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Platform -> SectionType -> doc
forall doc. IsDoc doc => Platform -> SectionType -> doc
pprAlignForSection (NCGConfig -> Platform
ncgPlatform NCGConfig
config) SectionType
seg

pprProcEndLabel :: IsLine doc => Platform -> CLabel -- ^ Procedure name
                -> doc
pprProcEndLabel :: forall doc. IsLine doc => Platform -> CLabel -> doc
pprProcEndLabel Platform
platform CLabel
lbl =
    Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkAsmTempProcEndLabel CLabel
lbl) doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
colon

pprBlockEndLabel :: IsLine doc => Platform -> CLabel -- ^ Block name
                 -> doc
pprBlockEndLabel :: forall doc. IsLine doc => Platform -> CLabel -> doc
pprBlockEndLabel Platform
platform CLabel
lbl =
    Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (CLabel -> CLabel
mkAsmTempEndLabel CLabel
lbl) doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
colon

-- | Output the ELF .size directive.
pprSizeDecl :: IsDoc doc => Platform -> CLabel -> doc
pprSizeDecl :: forall doc. IsDoc doc => Platform -> CLabel -> doc
pprSizeDecl Platform
platform CLabel
lbl
 = if OS -> Bool
osElfTarget (Platform -> OS
platformOS Platform
platform)
   then Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.size" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
", .-" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl)
   else doc
forall doc. IsOutput doc => doc
empty

pprBasicBlock :: IsDoc doc => NCGConfig -> LabelMap RawCmmStatics -> NatBasicBlock Instr
              -> doc
pprBasicBlock :: forall doc.
IsDoc doc =>
NCGConfig -> LabelMap RawCmmStatics -> GenBasicBlock Instr -> doc
pprBasicBlock NCGConfig
config LabelMap RawCmmStatics
info_env (BasicBlock BlockId
blockid [Instr]
instrs)
  = doc -> doc
maybe_infotable (doc -> doc) -> doc -> doc
forall a b. (a -> b) -> a -> b
$
    Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabel Platform
platform CLabel
asmLbl doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
    [doc] -> doc
forall doc. IsDoc doc => [doc] -> doc
vcat ((Instr -> doc) -> [Instr] -> [doc]
forall a b. (a -> b) -> [a] -> [b]
map (Platform -> Instr -> doc
forall doc. IsDoc doc => Platform -> Instr -> doc
pprInstr Platform
platform) ([Instr] -> [Instr]
forall a. a -> a
id {-detectTrivialDeadlock-} [Instr]
optInstrs)) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
    Bool -> doc -> doc
forall doc. IsOutput doc => Bool -> doc -> doc
ppWhen (NCGConfig -> Bool
ncgDwarfEnabled NCGConfig
config) (
      -- Emit both end labels since this may end up being a standalone
      -- top-level block
      Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprBlockEndLabel Platform
platform CLabel
asmLbl
         Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprProcEndLabel Platform
platform CLabel
asmLbl)
    )
  where
    -- TODO: Check if we can  filter more instructions here.
    -- TODO: Shouldn't this be a more general check on a higher level?
    -- Filter out identity moves. E.g. mov x18, x18 will be dropped.
    optInstrs :: [Instr]
optInstrs = (Instr -> Bool) -> [Instr] -> [Instr]
forall a. (a -> Bool) -> [a] -> [a]
filter Instr -> Bool
f [Instr]
instrs
      where f :: Instr -> Bool
f (MOV Operand
o1 Operand
o2) | Operand
o1 Operand -> Operand -> Bool
forall a. Eq a => a -> a -> Bool
== Operand
o2 = Bool
False
            f Instr
_ = Bool
True

    asmLbl :: CLabel
asmLbl = BlockId -> CLabel
blockLbl BlockId
blockid
    platform :: Platform
platform = NCGConfig -> Platform
ncgPlatform NCGConfig
config
    maybe_infotable :: doc -> doc
maybe_infotable doc
c = case KeyOf LabelMap -> LabelMap RawCmmStatics -> Maybe RawCmmStatics
forall a. KeyOf LabelMap -> LabelMap a -> Maybe a
forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> map a -> Maybe a
mapLookup KeyOf LabelMap
BlockId
blockid LabelMap RawCmmStatics
info_env of
       Maybe RawCmmStatics
Nothing   -> doc
c
       Just (CmmStaticsRaw CLabel
info_lbl [CmmStatic]
info) ->
          --  pprAlignForSection platform Text $$
           doc
infoTableLoc doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
           [doc] -> doc
forall doc. IsDoc doc => [doc] -> doc
vcat ((CmmStatic -> doc) -> [CmmStatic] -> [doc]
forall a b. (a -> b) -> [a] -> [b]
map (NCGConfig -> CmmStatic -> doc
forall doc. IsDoc doc => NCGConfig -> CmmStatic -> doc
pprData NCGConfig
config) [CmmStatic]
info) doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
           Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabel Platform
platform CLabel
info_lbl doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
           doc
c doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$
           Bool -> doc -> doc
forall doc. IsOutput doc => Bool -> doc -> doc
ppWhen (NCGConfig -> Bool
ncgDwarfEnabled NCGConfig
config)
              (Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprBlockEndLabel Platform
platform CLabel
info_lbl))
    -- Make sure the info table has the right .loc for the block
    -- coming right after it. See Note [Info Offset]
    infoTableLoc :: doc
infoTableLoc = case [Instr]
instrs of
      (l :: Instr
l@LOCATION{} : [Instr]
_) -> Platform -> Instr -> doc
forall doc. IsDoc doc => Platform -> Instr -> doc
pprInstr Platform
platform Instr
l
      [Instr]
_other             -> doc
forall doc. IsOutput doc => doc
empty

pprDatas :: IsDoc doc => NCGConfig -> RawCmmStatics -> doc
-- See Note [emit-time elimination of static indirections] in "GHC.Cmm.CLabel".
pprDatas :: forall doc. IsDoc doc => NCGConfig -> RawCmmStatics -> doc
pprDatas NCGConfig
config (CmmStaticsRaw CLabel
alias [CmmStaticLit (CmmLabel CLabel
lbl), CmmStaticLit CmmLit
ind, CmmStatic
_, CmmStatic
_])
  | CLabel
lbl CLabel -> CLabel -> Bool
forall a. Eq a => a -> a -> Bool
== CLabel
mkIndStaticInfoLabel
  , let labelInd :: CmmLit -> Maybe CLabel
labelInd (CmmLabelOff CLabel
l Int
_) = CLabel -> Maybe CLabel
forall a. a -> Maybe a
Just CLabel
l
        labelInd (CmmLabel CLabel
l) = CLabel -> Maybe CLabel
forall a. a -> Maybe a
Just CLabel
l
        labelInd CmmLit
_ = Maybe CLabel
forall a. Maybe a
Nothing
  , Just CLabel
ind' <- CmmLit -> Maybe CLabel
labelInd CmmLit
ind
  , CLabel
alias CLabel -> CLabel -> Bool
`mayRedirectTo` CLabel
ind'
  = Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprGloblDecl (NCGConfig -> Platform
ncgPlatform NCGConfig
config) CLabel
alias
    doc -> doc -> doc
forall doc. IsDoc doc => doc -> doc -> doc
$$ Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
".equiv" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel (NCGConfig -> Platform
ncgPlatform NCGConfig
config) CLabel
alias Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel (NCGConfig -> Platform
ncgPlatform NCGConfig
config) CLabel
ind')

pprDatas NCGConfig
config (CmmStaticsRaw CLabel
lbl [CmmStatic]
dats)
  = [doc] -> doc
forall doc. IsDoc doc => [doc] -> doc
vcat (Platform -> CLabel -> doc
forall doc. IsDoc doc => Platform -> CLabel -> doc
pprLabel Platform
platform CLabel
lbl doc -> [doc] -> [doc]
forall a. a -> [a] -> [a]
: (CmmStatic -> doc) -> [CmmStatic] -> [doc]
forall a b. (a -> b) -> [a] -> [b]
map (NCGConfig -> CmmStatic -> doc
forall doc. IsDoc doc => NCGConfig -> CmmStatic -> doc
pprData NCGConfig
config) [CmmStatic]
dats)
   where
      platform :: Platform
platform = NCGConfig -> Platform
ncgPlatform NCGConfig
config

pprData :: IsDoc doc => NCGConfig -> CmmStatic -> doc
pprData :: forall doc. IsDoc doc => NCGConfig -> CmmStatic -> doc
pprData NCGConfig
_config (CmmString ByteString
str) = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (ByteString -> Line doc
forall doc. IsLine doc => ByteString -> doc
pprString ByteString
str)
pprData NCGConfig
_config (CmmFileEmbed String
path Int
_) = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
pprFileEmbed String
path)

-- TODO: AFAIK there no Darwin for RISCV, so we may consider to simplify this.
pprData NCGConfig
config (CmmUninitialised Int
bytes)
 = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ let platform :: Platform
platform = NCGConfig -> Platform
ncgPlatform NCGConfig
config
          in if Platform -> OS
platformOS Platform
platform OS -> OS -> Bool
forall a. Eq a => a -> a -> Bool
== OS
OSDarwin
                then String -> Line doc
forall doc. IsLine doc => String -> doc
text String
".space " Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int Int
bytes
                else String -> Line doc
forall doc. IsLine doc => String -> doc
text String
".skip "  Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int Int
bytes

pprData NCGConfig
config (CmmStaticLit CmmLit
lit) = NCGConfig -> CmmLit -> doc
forall doc. IsDoc doc => NCGConfig -> CmmLit -> doc
pprDataItem NCGConfig
config CmmLit
lit

pprGloblDecl :: IsDoc doc => Platform -> CLabel -> doc
pprGloblDecl :: forall doc. IsDoc doc => Platform -> CLabel -> doc
pprGloblDecl Platform
platform CLabel
lbl
  | Bool -> Bool
not (CLabel -> Bool
externallyVisibleCLabel CLabel
lbl) = doc
forall doc. IsOutput doc => doc
empty
  | Bool
otherwise = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.globl " Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl)

-- Note [Always use objects for info tables]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- See discussion in X86.Ppr for why this is necessary.  Essentially we need to
-- ensure that we never pass function symbols when we might want to lookup the
-- info table.  If we did, we could end up with procedure linking tables
-- (PLT)s, and thus the lookup wouldn't point to the function, but into the
-- jump table.
--
-- Fun fact: The LLVMMangler exists to patch this issue su on the LLVM side as
-- well.
pprLabelType' :: IsLine doc => Platform -> CLabel -> doc
pprLabelType' :: forall doc. IsLine doc => Platform -> CLabel -> doc
pprLabelType' Platform
platform CLabel
lbl =
  if CLabel -> Bool
isCFunctionLabel CLabel
lbl Bool -> Bool -> Bool
|| Bool
functionOkInfoTable then
    String -> doc
forall doc. IsLine doc => String -> doc
text String
"@function"
  else
    String -> doc
forall doc. IsLine doc => String -> doc
text String
"@object"
  where
    functionOkInfoTable :: Bool
functionOkInfoTable = Platform -> Bool
platformTablesNextToCode Platform
platform Bool -> Bool -> Bool
&&
      CLabel -> Bool
isInfoTableLabel CLabel
lbl Bool -> Bool -> Bool
&& Bool -> Bool
not (CLabel -> Bool
isCmmInfoTableLabel CLabel
lbl) Bool -> Bool -> Bool
&& Bool -> Bool
not (CLabel -> Bool
isConInfoTableLabel CLabel
lbl)

-- this is called pprTypeAndSizeDecl in PPC.Ppr
pprTypeDecl :: IsDoc doc => Platform -> CLabel -> doc
pprTypeDecl :: forall doc. IsDoc doc => Platform -> CLabel -> doc
pprTypeDecl Platform
platform CLabel
lbl
    = if OS -> Bool
osElfTarget (Platform -> OS
platformOS Platform
platform) Bool -> Bool -> Bool
&& CLabel -> Bool
externallyVisibleCLabel CLabel
lbl
      then Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
".type " Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
", " Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprLabelType' Platform
platform CLabel
lbl)
      else doc
forall doc. IsOutput doc => doc
empty

pprDataItem :: IsDoc doc => NCGConfig -> CmmLit -> doc
pprDataItem :: forall doc. IsDoc doc => NCGConfig -> CmmLit -> doc
pprDataItem NCGConfig
config CmmLit
lit
  = [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ (Format -> CmmLit -> [Line doc]
ppr_item (CmmType -> Format
cmmTypeFormat (CmmType -> Format) -> CmmType -> Format
forall a b. (a -> b) -> a -> b
$ Platform -> CmmLit -> CmmType
cmmLitType Platform
platform CmmLit
lit) CmmLit
lit)
    where
        platform :: Platform
platform = NCGConfig -> Platform
ncgPlatform NCGConfig
config

        imm :: Imm
imm = CmmLit -> Imm
litToImm CmmLit
lit

        ppr_item :: Format -> CmmLit -> [Line doc]
ppr_item Format
II8  CmmLit
_ = [String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.byte\t"  Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Imm -> Line doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
platform Imm
imm]
        ppr_item Format
II16 CmmLit
_ = [String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.short\t" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Imm -> Line doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
platform Imm
imm]
        ppr_item Format
II32 CmmLit
_ = [String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.long\t"  Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Imm -> Line doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
platform Imm
imm]
        ppr_item Format
II64 CmmLit
_ = [String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.quad\t"  Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Imm -> Line doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
platform Imm
imm]

        ppr_item Format
FF32  (CmmFloat Rational
r Width
_)
           = let bs :: [Word8]
bs = Float -> [Word8]
floatToBytes (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational Rational
r)
             in  (Word8 -> Line doc) -> [Word8] -> [Line doc]
forall a b. (a -> b) -> [a] -> [b]
map (\Word8
b -> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.byte\t" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b)) [Word8]
bs

        ppr_item Format
FF64 (CmmFloat Rational
r Width
_)
           = let bs :: [Word8]
bs = Double -> [Word8]
doubleToBytes (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
r)
             in  (Word8 -> Line doc) -> [Word8] -> [Line doc]
forall a b. (a -> b) -> [a] -> [b]
map (\Word8
b -> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.byte\t" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b)) [Word8]
bs

        ppr_item Format
_ CmmLit
_ = String -> SDoc -> [Line doc]
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"pprDataItem:ppr_item" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ CmmLit -> String
forall a. Show a => a -> String
show CmmLit
lit)

pprImm :: IsLine doc => Platform -> Imm -> doc
pprImm :: forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
_ (ImmInt Int
i)     = Int -> doc
forall doc. IsLine doc => Int -> doc
int Int
i
pprImm Platform
_ (ImmInteger Integer
i) = Integer -> doc
forall doc. IsLine doc => Integer -> doc
integer Integer
i
pprImm Platform
p (ImmCLbl CLabel
l)    = Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
p CLabel
l
pprImm Platform
p (ImmIndex CLabel
l Int
i) = Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
p CLabel
l doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'+' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> doc
forall doc. IsLine doc => Int -> doc
int Int
i
pprImm Platform
_ (ImmLit FastString
s)     = FastString -> doc
forall doc. IsLine doc => FastString -> doc
ftext FastString
s

-- TODO: See pprIm below for why this is a bad idea!
pprImm Platform
_ (ImmFloat Rational
f) = Float -> doc
forall doc. IsLine doc => Float -> doc
float (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational Rational
f)
pprImm Platform
_ (ImmDouble Rational
d) = Double -> doc
forall doc. IsLine doc => Double -> doc
double (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
d)

pprImm Platform
p (ImmConstantSum Imm
a Imm
b) = Platform -> Imm -> doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
p Imm
a doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'+' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Imm -> doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
p Imm
b
pprImm Platform
p (ImmConstantDiff Imm
a Imm
b) = Platform -> Imm -> doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
p Imm
a doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'-'
                   doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
lparen doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Imm -> doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
p Imm
b doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
rparen


-- aarch64 GNU as uses // for comments.
asmComment :: SDoc -> SDoc
asmComment :: SDoc -> SDoc
asmComment SDoc
c = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"#" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc
c

asmDoubleslashComment :: SDoc -> SDoc
asmDoubleslashComment :: SDoc -> SDoc
asmDoubleslashComment SDoc
c = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"//" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc
c

asmMultilineComment :: SDoc -> SDoc
asmMultilineComment :: SDoc -> SDoc
asmMultilineComment SDoc
c =  String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"/*" SDoc -> SDoc -> SDoc
$+$ SDoc
c SDoc -> SDoc -> SDoc
$+$ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"*/"

pprIm :: IsLine doc => Platform -> Imm -> doc
pprIm :: forall doc. IsLine doc => Platform -> Imm -> doc
pprIm Platform
platform Imm
im = case Imm
im of
  ImmInt Int
i     -> Int -> doc
forall doc. IsLine doc => Int -> doc
int Int
i
  ImmInteger Integer
i -> Integer -> doc
forall doc. IsLine doc => Integer -> doc
integer Integer
i

  -- FIXME: This is AArch64 commentry, not necesarily correct for RISCV!
  -- TODO: This will only work for
  -- The floating point value must be expressible as ±n ÷ 16 × 2^r,
  -- where n and r are integers such that 16 ≤ n ≤ 31 and -3 ≤ r ≤ 4.
  -- and 0 needs to be encoded as wzr/xzr.
  --
  -- Except for 0, we might want to either split it up into enough
  -- ADD operations into an Integer register and then just bit copy it into
  -- the double register? See the toBytes + fromRational above for data items.
  -- This is something the x86 backend does.
  --
  -- We could also just turn them into statics :-/ Which is what the
  -- PowerPC backend does.
  ImmFloat Rational
f | Rational
f Rational -> Rational -> Bool
forall a. Eq a => a -> a -> Bool
== Rational
0 -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"zero"
  ImmFloat Rational
f -> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'#' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Float -> doc
forall doc. IsLine doc => Float -> doc
float (Rational -> Float
forall a. Fractional a => Rational -> a
fromRational Rational
f)
  ImmDouble Rational
d | Rational
d Rational -> Rational -> Bool
forall a. Eq a => a -> a -> Bool
== Rational
0 -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"zero"
  ImmDouble Rational
d -> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'#' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Double -> doc
forall doc. IsLine doc => Double -> doc
double (Rational -> Double
forall a. Fractional a => Rational -> a
fromRational Rational
d)
  -- =<lbl> pseudo instruction!
  ImmCLbl CLabel
l    -> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'=' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
l
  ImmIndex CLabel
l Int
o -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"[=" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
l doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
comma doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'#' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> doc
forall doc. IsLine doc => Int -> doc
int Int
o doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
']'
  Imm
_            -> String -> doc
forall a. HasCallStack => String -> a
panic String
"AArch64.pprIm"

negOp :: Operand -> Operand
negOp :: Operand -> Operand
negOp (OpImm (ImmInt Int
i)) = Imm -> Operand
OpImm (Int -> Imm
ImmInt (Int -> Int
forall a. Num a => a -> a
negate Int
i))
negOp (OpImm (ImmInteger Integer
i)) = Imm -> Operand
OpImm (Integer -> Imm
ImmInteger (Integer -> Integer
forall a. Num a => a -> a
negate Integer
i))
negOp Operand
op = String -> SDoc -> Operand
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RV64.negOp" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ Operand -> String
forall a. Show a => a -> String
show Operand
op)

pprOp :: IsLine doc => Platform -> Operand -> doc
pprOp :: forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
plat Operand
op = case Operand
op of
  OpReg Width
w Reg
r           -> Width -> Reg -> doc
forall doc. IsLine doc => Width -> Reg -> doc
pprReg Width
w Reg
r
  OpImm Imm
im          -> Platform -> Imm -> doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprIm Platform
plat Imm
im
  OpAddr (AddrRegImm Reg
r1 Imm
im) -> Platform -> Imm -> doc
forall doc. IsLine doc => Platform -> Imm -> doc
pprImm Platform
plat Imm
im doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'(' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Width -> Reg -> doc
forall doc. IsLine doc => Width -> Reg -> doc
pprReg Width
W64 Reg
r1 doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
')'
  OpAddr (AddrReg Reg
r1)       -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"0(" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Width -> Reg -> doc
forall doc. IsLine doc => Width -> Reg -> doc
pprReg Width
W64 Reg
r1 doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
')'

pprReg :: forall doc. IsLine doc => Width -> Reg -> doc
pprReg :: forall doc. IsLine doc => Width -> Reg -> doc
pprReg Width
w Reg
r = case Reg
r of
  RegReal    (RealRegSingle Int
i) -> Int -> doc
ppr_reg_no Int
i
  -- virtual regs should not show up, but this is helpful for debugging.
  RegVirtual (VirtualRegI Unique
u)   -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"%vI_" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Unique -> doc
forall doc. IsLine doc => Unique -> doc
pprUniqueAlways Unique
u
  RegVirtual (VirtualRegF Unique
u)   -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"%vF_" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Unique -> doc
forall doc. IsLine doc => Unique -> doc
pprUniqueAlways Unique
u
  RegVirtual (VirtualRegD Unique
u)   -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"%vD_" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Unique -> doc
forall doc. IsLine doc => Unique -> doc
pprUniqueAlways Unique
u
  Reg
_                            -> String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RiscV64.pprReg" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (Reg -> String
forall a. Show a => a -> String
show Reg
r) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w)

  where
    ppr_reg_no :: Int -> doc
    -- General Purpose Registers
    ppr_reg_no :: Int -> doc
ppr_reg_no Int
0 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"zero"
    ppr_reg_no Int
1 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ra"
    ppr_reg_no Int
2 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"sp"
    ppr_reg_no Int
3 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"gp"
    ppr_reg_no Int
4 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"tp"
    ppr_reg_no Int
5 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t0"
    ppr_reg_no Int
6 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t1"
    ppr_reg_no Int
7 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t2"
    ppr_reg_no Int
8 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s0"
    ppr_reg_no Int
9 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s1"
    ppr_reg_no Int
10 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a0"
    ppr_reg_no Int
11 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a1"
    ppr_reg_no Int
12 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a2"
    ppr_reg_no Int
13 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a3"
    ppr_reg_no Int
14 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a4"
    ppr_reg_no Int
15 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a5"
    ppr_reg_no Int
16 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a6"
    ppr_reg_no Int
17 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"a7"
    ppr_reg_no Int
18 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s2"
    ppr_reg_no Int
19 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s3"
    ppr_reg_no Int
20 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s4"
    ppr_reg_no Int
21 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s5"
    ppr_reg_no Int
22 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s6"
    ppr_reg_no Int
23 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s7"
    ppr_reg_no Int
24 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s8"
    ppr_reg_no Int
25 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s9"
    ppr_reg_no Int
26 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s10"
    ppr_reg_no Int
27 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"s11"
    ppr_reg_no Int
28 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t3"
    ppr_reg_no Int
29 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t4"
    ppr_reg_no Int
30 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t5"
    ppr_reg_no Int
31 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"t6"

    -- Floating Point Registers
    ppr_reg_no Int
32 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft0"
    ppr_reg_no Int
33 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft1"
    ppr_reg_no Int
34 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft2"
    ppr_reg_no Int
35 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft3"
    ppr_reg_no Int
36 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft4"
    ppr_reg_no Int
37 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft5"
    ppr_reg_no Int
38 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft6"
    ppr_reg_no Int
39 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft7"
    ppr_reg_no Int
40 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs0"
    ppr_reg_no Int
41 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs1"
    ppr_reg_no Int
42 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa0"
    ppr_reg_no Int
43 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa1"
    ppr_reg_no Int
44 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa2"
    ppr_reg_no Int
45 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa3"
    ppr_reg_no Int
46 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa4"
    ppr_reg_no Int
47 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa5"
    ppr_reg_no Int
48 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa6"
    ppr_reg_no Int
49 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fa7"
    ppr_reg_no Int
50 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs2"
    ppr_reg_no Int
51 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs3"
    ppr_reg_no Int
52 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs4"
    ppr_reg_no Int
53 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs5"
    ppr_reg_no Int
54 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs6"
    ppr_reg_no Int
55 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs7"
    ppr_reg_no Int
56 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs8"
    ppr_reg_no Int
57 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs9"
    ppr_reg_no Int
58 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs10"
    ppr_reg_no Int
59 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"fs11"
    ppr_reg_no Int
60 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft8"
    ppr_reg_no Int
61 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft9"
    ppr_reg_no Int
62 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft10"
    ppr_reg_no Int
63 = String -> doc
forall doc. IsLine doc => String -> doc
text String
"ft11"

    ppr_reg_no Int
i
         | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Unexpected register number (min is 0)" (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
i)
         | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
63 = String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Unexpected register number (max is 63)" (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
i)
         -- no support for widths > W64.
         | Bool
otherwise = String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Unsupported width in register (max is 64)" (Width -> SDoc
forall a. Outputable a => a -> SDoc
ppr Width
w SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
i)

isSingleOp :: Operand -> Bool
isSingleOp :: Operand -> Bool
isSingleOp (OpReg Width
W32 Reg
_) = Bool
True
isSingleOp Operand
_ = Bool
False

isDoubleOp :: Operand -> Bool
isDoubleOp :: Operand -> Bool
isDoubleOp (OpReg Width
W64 Reg
_) = Bool
True
isDoubleOp Operand
_ = Bool
False

isImmOp :: Operand -> Bool
isImmOp :: Operand -> Bool
isImmOp (OpImm Imm
_) = Bool
True
isImmOp Operand
_ = Bool
False

isImmZero :: Operand -> Bool
isImmZero :: Operand -> Bool
isImmZero (OpImm (ImmFloat Rational
0)) = Bool
True
isImmZero (OpImm (ImmDouble Rational
0)) = Bool
True
isImmZero (OpImm (ImmInt Int
0)) = Bool
True
isImmZero Operand
_ = Bool
False

isLabel :: Target -> Bool
isLabel :: Target -> Bool
isLabel (TBlock BlockId
_) = Bool
True
isLabel (TLabel CLabel
_) = Bool
True
isLabel Target
_ = Bool
False

getLabel :: IsLine doc => Platform -> Target -> doc
getLabel :: forall doc. IsLine doc => Platform -> Target -> doc
getLabel Platform
platform (TBlock BlockId
bid) = Platform -> BlockId -> doc
forall doc. IsLine doc => Platform -> BlockId -> doc
pprBlockId Platform
platform BlockId
bid
getLabel Platform
platform (TLabel CLabel
lbl) = Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl
getLabel Platform
_platform Target
_other = String -> doc
forall a. HasCallStack => String -> a
panic String
"Cannot turn this into a label"

pprBlockId :: IsLine doc => Platform -> BlockId -> doc
pprBlockId :: forall doc. IsLine doc => Platform -> BlockId -> doc
pprBlockId Platform
platform BlockId
bid = Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform (Unique -> CLabel
mkLocalBlockLabel (BlockId -> Unique
forall a. Uniquable a => a -> Unique
getUnique BlockId
bid))

pprInstr :: IsDoc doc => Platform -> Instr -> doc
pprInstr :: forall doc. IsDoc doc => Platform -> Instr -> doc
pprInstr Platform
platform Instr
instr = case Instr
instr of
  -- Meta Instructions ---------------------------------------------------------
  -- see Note [dualLine and dualDoc] in GHC.Utils.Outputable
  COMMENT SDoc
s  -> SDoc -> HDoc -> doc
forall doc. IsDoc doc => SDoc -> HDoc -> doc
dualDoc (SDoc -> SDoc
asmComment SDoc
s) HDoc
forall doc. IsOutput doc => doc
empty
  MULTILINE_COMMENT SDoc
s -> SDoc -> HDoc -> doc
forall doc. IsDoc doc => SDoc -> HDoc -> doc
dualDoc (SDoc -> SDoc
asmMultilineComment SDoc
s) HDoc
forall doc. IsOutput doc => doc
empty
  ANN SDoc
d Instr
i -> SDoc -> HDoc -> doc
forall doc. IsDoc doc => SDoc -> HDoc -> doc
dualDoc (Platform -> Instr -> SDoc
forall doc. IsDoc doc => Platform -> Instr -> doc
pprInstr Platform
platform Instr
i SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc -> SDoc
asmDoubleslashComment SDoc
d) (Platform -> Instr -> HDoc
forall doc. IsDoc doc => Platform -> Instr -> doc
pprInstr Platform
platform Instr
i)

  LOCATION Int
file Int
line' Int
col String
_name
    -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t.loc" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int Int
file Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int Int
line' Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int Int
col)
  DELTA Int
d   -> SDoc -> HDoc -> doc
forall doc. IsDoc doc => SDoc -> HDoc -> doc
dualDoc (SDoc -> SDoc
asmComment (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"\tdelta = " SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Int -> SDoc
forall doc. IsLine doc => Int -> doc
int Int
d) HDoc
forall doc. IsOutput doc => doc
empty
  NEWBLOCK BlockId
_ -> String -> doc
forall a. HasCallStack => String -> a
panic String
"PprInstr: NEWBLOCK"
  LDATA Section
_ RawCmmStatics
_  -> String -> doc
forall a. HasCallStack => String -> a
panic String
"pprInstr: LDATA"

  -- Pseudo Instructions -------------------------------------------------------

  Instr
PUSH_STACK_FRAME -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi sp, sp, -16"
                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsd x1, 8(sp)"     -- store RA
                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsd x8, 0(sp)"     -- store FP/s0
                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi x8, sp, 16"]

  Instr
POP_STACK_FRAME -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_  [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tld x8, 0(sp)" -- restore FP/s0
                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tld x1, 8(sp)" -- restore RA
                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi sp, sp, 16" ]
  -- ===========================================================================
  -- AArch64 Instruction Set
  -- 1. Arithmetic Instructions ------------------------------------------------
  ADD  Operand
o1 Operand
o2 Operand
o3
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfadd." Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> if Operand -> Bool
isSingleOp Operand
o1 then String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"s" else String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"d") Operand
o1 Operand
o2 Operand
o3
    -- This case is used for sign extension: SEXT.W op
    | OpReg Width
W64 Reg
_ <- Operand
o1 , OpReg Width
W32 Reg
_ <- Operand
o2, Operand -> Bool
isImmOp Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddiw") Operand
o1 Operand
o2 Operand
o3
    | Bool
otherwise -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tadd") Operand
o1 Operand
o2 Operand
o3
  -- CMN  o1 o2    -> op2 (text "\tcmn") o1 o2
  -- CMP  o1 o2
  --   | isFloatOp o1 && isFloatOp o2 -> op2 (text "\tfcmp") o1 o2
  --   | otherwise -> op2 (text "\tcmp") o1 o2
  MUL  Operand
o1 Operand
o2 Operand
o3
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmul." Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> if Operand -> Bool
isSingleOp Operand
o1 then String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"s" else String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"d") Operand
o1 Operand
o2 Operand
o3
    | Bool
otherwise -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tmul") Operand
o1 Operand
o2 Operand
o3
  SMULH Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tmulh") Operand
o1 Operand
o2 Operand
o3
  NEG Operand
o1 Operand
o2 | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfneg.s") Operand
o1 Operand
o2
  NEG Operand
o1 Operand
o2 | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfneg.d") Operand
o1 Operand
o2
  NEG Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tneg") Operand
o1 Operand
o2
  DIV Operand
o1 Operand
o2 Operand
o3 | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o3
  -- TODO: This must (likely) be refined regarding width
    -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfdiv." Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> if Operand -> Bool
isSingleOp Operand
o1 then String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"s" else String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"d") Operand
o1 Operand
o2 Operand
o3
  DIV Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tdiv") Operand
o1 Operand
o2 Operand
o3
  REM Operand
o1 Operand
o2 Operand
o3 | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o3
    -> String -> doc
forall a. HasCallStack => String -> a
panic String
"pprInstr - REM not implemented for floats (yet)"
  REM Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\trem") Operand
o1 Operand
o2 Operand
o3
  REMU Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tremu") Operand
o1 Operand
o2 Operand
o3

  SUB  Operand
o1 Operand
o2 Operand
o3
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfsub." Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> if Operand -> Bool
isSingleOp Operand
o1 then String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"s" else String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"d") Operand
o1 Operand
o2 Operand
o3
    | Operand -> Bool
isImmOp Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi") Operand
o1 Operand
o2 (Operand -> Operand
negOp Operand
o3)
    | Bool
otherwise -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsub")  Operand
o1 Operand
o2 Operand
o3
  DIVU Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tdivu") Operand
o1 Operand
o2 Operand
o3

  -- 2. Bit Manipulation Instructions ------------------------------------------
  SBFM Operand
o1 Operand
o2 Operand
o3 Operand
o4 -> Line doc -> Operand -> Operand -> Operand -> Operand -> doc
op4 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsbfm") Operand
o1 Operand
o2 Operand
o3 Operand
o4
  UBFM Operand
o1 Operand
o2 Operand
o3 Operand
o4 -> Line doc -> Operand -> Operand -> Operand -> Operand -> doc
op4 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tubfm") Operand
o1 Operand
o2 Operand
o3 Operand
o4
  -- signed and unsigned bitfield extract
  UBFX Operand
o1 Operand
o2 Operand
o3 Operand
o4 -> Line doc -> Operand -> Operand -> Operand -> Operand -> doc
op4 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tubfx") Operand
o1 Operand
o2 Operand
o3 Operand
o4

  -- 3. Logical and Move Instructions ------------------------------------------
  AND Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tand") Operand
o1 Operand
o2 Operand
o3
  OR Operand
o1 Operand
o2 Operand
o3   -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tor") Operand
o1 Operand
o2 Operand
o3
  -- ANDS o1 o2 o3 -> op3 (text "\tands") o1 o2 o3
  ASR Operand
o1 Operand
o2 Operand
o3 | Operand -> Bool
isImmOp Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsrai") Operand
o1 Operand
o2 Operand
o3
  ASR Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsra") Operand
o1 Operand
o2 Operand
o3
  BIC Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tbic") Operand
o1 Operand
o2 Operand
o3
  BICS Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tbics") Operand
o1 Operand
o2 Operand
o3
  XOR Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txor") Operand
o1 Operand
o2 Operand
o3
  LSL Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsll") Operand
o1 Operand
o2 Operand
o3
  LSR Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsrl") Operand
o1 Operand
o2 Operand
o3
  MOV Operand
o1 Operand
o2
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmv.d") Operand
o1 Operand
o2 -- fmv.d rd, rs is pseudo op fsgnj.d rd, rs, rs
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmv.s") Operand
o1 Operand
o2 -- fmv.s rd, rs is pseudo op fsgnj.s rd, rs, rs
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isImmZero Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
o1 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.d.w") Operand
o1 Operand
zero
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Operand -> Bool
isImmZero Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
o1 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.s.w") Operand
o1 Operand
zero
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Bool -> Bool
not (Operand -> Bool
isFloatOp Operand
o2) Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
o1 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmv.w.x") Operand
o1 Operand
o2
    | Operand -> Bool
isFloatOp Operand
o1 Bool -> Bool -> Bool
&& Bool -> Bool
not (Operand -> Bool
isFloatOp Operand
o2) Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
o1 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmv.d.x") Operand
o1 Operand
o2
    | Bool -> Bool
not (Operand -> Bool
isFloatOp Operand
o1) Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmv.x.w") Operand
o1 Operand
o2
    | Bool -> Bool
not (Operand -> Bool
isFloatOp Operand
o1) Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
o2 Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfmv.x.d") Operand
o1 Operand
o2
    | (OpImm (ImmInteger Integer
i)) <- Operand
o2
    , Integer -> Bool
forall a. (Num a, Ord a) => a -> Bool
fitsIn12bitImm Integer
i
          -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
x0 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 ]
    | (OpImm (ImmInt Int
i)) <- Operand
o2
    , Int -> Bool
forall a. (Num a, Ord a) => a -> Bool
fitsIn12bitImm Int
i
          -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
x0 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 ]
    | (OpImm (ImmInteger Integer
i)) <- Operand
o2
    , Integer -> Bool
forall a. (Num a, Ord a, Bits a) => a -> Bool
fitsIn32bits Integer
i
        -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlui" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"%hi(" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
")"
                                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddw" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"%lo(" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
")" ]
    | (OpImm (ImmInt Int
i)) <- Operand
o2
    , Int -> Bool
forall a. (Num a, Ord a, Bits a) => a -> Bool
fitsIn32bits Int
i
        -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlui" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"%hi(" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
")"
                                             , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddw" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"%lo(" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
")" ]
    | Operand -> Bool
isImmOp Operand
o2
        -- Surrender! Let the assembler figure out the right expressions with pseudo-op LI.
        -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tli" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+>  Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 ]
    | Bool
otherwise                    -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi") Operand
o1 Operand
o2 (Imm -> Operand
OpImm (Int -> Imm
ImmInt Int
0))
  ORI Operand
o1 Operand
o2 Operand
o3  -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tori") Operand
o1 Operand
o2 Operand
o3
  XORI Operand
o1 Operand
o2 Operand
o3 -> Line doc -> Operand -> Operand -> Operand -> doc
op3 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txori") Operand
o1 Operand
o2 Operand
o3

  -- 4. Branch Instructions ----------------------------------------------------
  J Target
t             -> Platform -> Instr -> doc
forall doc. IsDoc doc => Platform -> Instr -> doc
pprInstr Platform
platform (Target -> Instr
B Target
t)
  -- TODO: This is odd: (B)ranch and branch and link (BL) do the same: branch and link
  B Target
l | Target -> Bool
isLabel Target
l -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tjal" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
x0 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Target -> Line doc
forall doc. IsLine doc => Platform -> Target -> doc
getLabel Platform
platform Target
l
  B (TReg Reg
r)      -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tjalr" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"x0" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Width -> Reg -> Line doc
forall doc. IsLine doc => Width -> Reg -> doc
pprReg Width
W64 Reg
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"0"

  B_FAR BlockId
bid  -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tla" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
ip Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> BlockId -> Line doc
forall doc. IsLine doc => Platform -> BlockId -> doc
pprBlockId Platform
platform BlockId
bid
                            , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tjalr" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"x0" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
ip Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"0" ]

  BL Target
l [Reg]
_ [Reg]
_ | Target -> Bool
isLabel Target
l-> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tcall" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Target -> Line doc
forall doc. IsLine doc => Platform -> Target -> doc
getLabel Platform
platform Target
l
  BL (TReg Reg
r)     [Reg]
_ [Reg]
_ -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tjalr" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"x1" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Width -> Reg -> Line doc
forall doc. IsLine doc => Width -> Reg -> doc
pprReg Width
W64 Reg
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"0"

  BCOND Cond
c Operand
l Operand
r Target
t | Target -> Bool
isLabel Target
t ->
    Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Cond -> Line doc
forall doc. IsLine doc => Cond -> doc
pprBcond Cond
c Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Target -> Line doc
forall doc. IsLine doc => Platform -> Target -> doc
getLabel Platform
platform Target
t

  BCOND Cond
_ Operand
_ Operand
_ (TReg Reg
_)     -> String -> doc
forall a. HasCallStack => String -> a
panic String
"RV64.ppr: No conditional branching to registers!"

  -- This is the far branches trick: Negate the condition and either do a
  -- register based jump (ignoring the link result in register zero) or just
  -- branch to the end of the block, jumping over the far jump instructions.
  BCOND_FAR Cond
c Operand
l Operand
r Target
b Target
t | Target -> Bool
isLabel Target
t ->
    [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\t" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Cond -> Line doc
forall doc. IsLine doc => Cond -> doc
pprBcond (Cond -> Cond
negateCond Cond
c) Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Target -> Line doc
forall doc. IsLine doc => Platform -> Target -> doc
getLabel Platform
platform Target
b Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"_end"
           , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tla" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
ip Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Target -> Line doc
forall doc. IsLine doc => Platform -> Target -> doc
getLabel Platform
platform Target
t
           , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tjalr" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"x0" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
ip Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"0"
           ]

  BCOND_FAR Cond
_ Operand
_ Operand
_ Target
_ (TReg Reg
_)     -> String -> doc
forall a. HasCallStack => String -> a
panic String
"RV64.ppr: No conditional branching to registers!"


  -- 5. Atomic Instructions ----------------------------------------------------
  -- 6. Conditional Instructions -----------------------------------------------
  CSET Operand
o Operand
l Operand
r Cond
c  -> case Cond
c of
    Cond
EQ | Operand -> Bool
isIntOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isIntOp Operand
r -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
subFor Operand
l Operand
r
                  , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tseqz" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o]
    Cond
EQ | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
binOp (String
"\tfeq." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Platform -> Operand -> Operand -> String
floatOpPrecision Platform
platform Operand
l Operand
r)
    Cond
NE | Operand -> Bool
isIntOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isIntOp Operand
r -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
subFor Operand
l Operand
r
                  , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsnez" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o]
    --    feq.s   a0,fa0,fa1
    --    xori    a0,a0,1
    Cond
NE | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [String -> Line doc
forall doc. IsLine doc => String -> doc
binOp (String
"\tfeq." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Platform -> Operand -> Operand -> String
floatOpPrecision Platform
platform Operand
l Operand
r)
                                              , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txori" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+>  Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"1"]
    Cond
SLT -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r ]
    Cond
SLE -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l
                  , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txori" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+>  Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"1" ]
    Cond
SGE -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r
                  , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txori" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+>  Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"1" ]
    Cond
SGT -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l ]
    Cond
ULT -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltuFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r ]
    Cond
ULE -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltuFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l
                  , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txori" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+>  Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"1" ]
    Cond
UGE -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltuFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r
                  , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\txori" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+>  Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"1" ]
    Cond
UGT -> [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ Operand -> Operand -> Line doc
forall {doc}. IsLine doc => Operand -> Operand -> doc
sltuFor Operand
l Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l ]
    Cond
FLT | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
binOp (String
"\tflt." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Platform -> Operand -> Operand -> String
floatOpPrecision Platform
platform Operand
l Operand
r)
    Cond
FLE | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
binOp (String
"\tfle." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Platform -> Operand -> Operand -> String
floatOpPrecision Platform
platform Operand
l Operand
r)
    Cond
FGT | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
binOp (String
"\tfgt." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Platform -> Operand -> Operand -> String
floatOpPrecision Platform
platform Operand
l Operand
r)
    Cond
FGE | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
binOp (String
"\tfge." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Platform -> Operand -> Operand -> String
floatOpPrecision Platform
platform Operand
l Operand
r)
    Cond
x  -> String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RV64.ppr: unhandled CSET conditional" (String -> SDoc
forall doc. IsLine doc => String -> doc
text (Cond -> String
forall a. Show a => a -> String
show Cond
x) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
comma SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
comma SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l)
    where
      subFor :: Operand -> Operand -> Line doc
subFor Operand
l Operand
r | (OpImm Imm
_) <- Operand
r = String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform (Operand -> Operand
negOp Operand
r)
                 | (OpImm Imm
_) <- Operand
l = String -> Line doc
forall a. HasCallStack => String -> a
panic String
"RV64.ppr: Cannot SUB IMM _"
                 | Bool
otherwise      = String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsub" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r
      sltFor :: Operand -> Operand -> doc
sltFor Operand
l Operand
r | (OpImm Imm
_) <- Operand
r = String -> doc
forall doc. IsLine doc => String -> doc
text String
"\tslti"
                 | (OpImm Imm
_) <- Operand
l = String -> doc
forall a. HasCallStack => String -> a
panic String
"PV64.ppr: Cannot SLT IMM _"
                 | Bool
otherwise      = String -> doc
forall doc. IsLine doc => String -> doc
text String
"\tslt"
      sltuFor :: Operand -> Operand -> doc
sltuFor Operand
l Operand
r| (OpImm Imm
_) <- Operand
r = String -> doc
forall doc. IsLine doc => String -> doc
text String
"\tsltui"
                 | (OpImm Imm
_) <- Operand
l = String -> doc
forall a. HasCallStack => String -> a
panic String
"PV64.ppr: Cannot SLTU IMM _"
                 | Bool
otherwise      = String -> doc
forall doc. IsLine doc => String -> doc
text String
"\tsltu"
      binOp :: (IsLine doc) => String -> doc
      binOp :: forall doc. IsLine doc => String -> doc
binOp String
op = String -> doc
forall doc. IsLine doc => String -> doc
text String
op doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
comma doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
l doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> doc
forall doc. IsLine doc => doc
comma doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
r

  -- 7. Load and Store Instructions --------------------------------------------
  -- NOTE: GHC may do whacky things where it only load the lower part of an
  --       address. Not observing the correct size when loading will lead
  --       inevitably to crashes.
  STR Format
II8  Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsb") Operand
o1 Operand
o2
  STR Format
II16 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsh") Operand
o1 Operand
o2
  STR Format
II32 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsw") Operand
o1 Operand
o2
  STR Format
II64 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tsd") Operand
o1 Operand
o2
  STR Format
FF32 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfsw") Operand
o1 Operand
o2
  STR Format
FF64 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfsd") Operand
o1 Operand
o2

  LDR Format
_f Operand
o1 (OpImm (ImmIndex CLabel
lbl Int
off)) ->
    [Line doc] -> doc
forall doc. IsDoc doc => [Line doc] -> doc
lines_ [ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tla" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl
           , String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\taddi" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Int -> Line doc
forall doc. IsLine doc => Int -> doc
int Int
off
           ]

  LDR Format
_f Operand
o1 (OpImm (ImmCLbl CLabel
lbl)) ->
    -- fixing this is _really_ annoyin we need to generate code like:
    -- 1: auipc x16, %pcrel_hi(<lbl>)
    --    addi x16, x16, %pcrel_lo(1b)
    -- I really dislike this (refer back to label 1 syntax from the assembler.)
    --
    -- So we'll go with pseudo ops. la and li it is.
    -- op_adrp o1 (text "%pcrel_hi(" <> pprAsmLabel platform lbl <> text ")") $$
    -- op_add o1 (text "%pcrel_lo(" <> pprAsmLabel platform lbl <> text ")")
    Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tla" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> CLabel -> Line doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
platform CLabel
lbl

  LDR Format
II8  Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlb") Operand
o1 Operand
o2
  LDR Format
II16 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlh") Operand
o1 Operand
o2
  LDR Format
II32 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlw") Operand
o1 Operand
o2
  LDR Format
II64 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tld") Operand
o1 Operand
o2
  LDR Format
FF32 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tflw") Operand
o1 Operand
o2
  LDR Format
FF64 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfld") Operand
o1 Operand
o2

  LDRU Format
II8  Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlbu") Operand
o1 Operand
o2
  LDRU Format
II16 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlhu") Operand
o1 Operand
o2
  LDRU Format
II32 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tlwu") Operand
o1 Operand
o2
  -- double words (64bit) cannot be sign extended by definition
  LDRU Format
II64 Operand
o1 Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tld") Operand
o1 Operand
o2
  LDRU Format
FF32 Operand
o1 o2 :: Operand
o2@(OpAddr (AddrReg Reg
_)) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tflw") Operand
o1 Operand
o2
  LDRU Format
FF32 Operand
o1 o2 :: Operand
o2@(OpAddr (AddrRegImm Reg
_ Imm
_)) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tflw") Operand
o1 Operand
o2
  LDRU Format
FF64 Operand
o1 o2 :: Operand
o2@(OpAddr (AddrReg Reg
_)) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfld") Operand
o1 Operand
o2
  LDRU Format
FF64 Operand
o1 o2 :: Operand
o2@(OpAddr (AddrRegImm Reg
_ Imm
_)) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfld") Operand
o1 Operand
o2
  LDRU Format
f Operand
o1 Operand
o2 -> String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Unsupported unsigned load" ((String -> SDoc
forall doc. IsLine doc => String -> doc
text(String -> SDoc) -> (Format -> String) -> Format -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Format -> String
forall a. Show a => a -> String
show) Format
f SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2)
  -- LDAR _f o1 o2 -> op2 (text "\tldar") o1 o2

  -- STP _f o1 o2 o3 -> op3 (text "\tstp") o1 o2 o3
  -- LDP _f o1 o2 o3 -> op3 (text "\tldp") o1 o2 o3

  -- 8. Synchronization Instructions -------------------------------------------
  DMBSY DmbType
r DmbType
w -> Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfence" Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> DmbType -> Line doc
forall {doc}. IsLine doc => DmbType -> doc
pprDmbType DmbType
r Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> Line doc
forall doc. IsLine doc => Char -> doc
char Char
',' Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> DmbType -> Line doc
forall {doc}. IsLine doc => DmbType -> doc
pprDmbType DmbType
w

  -- 9. Floating Point Instructions --------------------------------------------
  FCVT o1 :: Operand
o1@(OpReg Width
W32 Reg
_) o2 :: Operand
o2@(OpReg Width
W64 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.s.d") Operand
o1 Operand
o2
  FCVT o1 :: Operand
o1@(OpReg Width
W64 Reg
_) o2 :: Operand
o2@(OpReg Width
W32 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.d.s") Operand
o1 Operand
o2
  FCVT Operand
o1 Operand
o2 -> String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RV64.pprInstr - impossible float conversion" (SDoc -> doc) -> SDoc -> doc
forall a b. (a -> b) -> a -> b
$
                  Line SDoc -> SDoc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"->" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2)

  SCVTF o1 :: Operand
o1@(OpReg Width
W32 Reg
_) o2 :: Operand
o2@(OpReg Width
W32 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.s.w") Operand
o1 Operand
o2
  SCVTF o1 :: Operand
o1@(OpReg Width
W32 Reg
_) o2 :: Operand
o2@(OpReg Width
W64 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.s.l") Operand
o1 Operand
o2
  SCVTF o1 :: Operand
o1@(OpReg Width
W64 Reg
_) o2 :: Operand
o2@(OpReg Width
W32 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.d.w") Operand
o1 Operand
o2
  SCVTF o1 :: Operand
o1@(OpReg Width
W64 Reg
_) o2 :: Operand
o2@(OpReg Width
W64 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.d.l") Operand
o1 Operand
o2
  SCVTF Operand
o1 Operand
o2 -> String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RV64.pprInstr - impossible integer to float conversion" (SDoc -> doc) -> SDoc -> doc
forall a b. (a -> b) -> a -> b
$
                  Line SDoc -> SDoc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"->" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2)

  FCVTZS o1 :: Operand
o1@(OpReg Width
W32 Reg
_) o2 :: Operand
o2@(OpReg Width
W32 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.w.s") Operand
o1 Operand
o2
  FCVTZS o1 :: Operand
o1@(OpReg Width
W32 Reg
_) o2 :: Operand
o2@(OpReg Width
W64 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.w.d") Operand
o1 Operand
o2
  FCVTZS o1 :: Operand
o1@(OpReg Width
W64 Reg
_) o2 :: Operand
o2@(OpReg Width
W32 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.l.s") Operand
o1 Operand
o2
  FCVTZS o1 :: Operand
o1@(OpReg Width
W64 Reg
_) o2 :: Operand
o2@(OpReg Width
W64 Reg
_) -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfcvt.l.d") Operand
o1 Operand
o2
  FCVTZS Operand
o1 Operand
o2 -> String -> SDoc -> doc
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"RV64.pprInstr - impossible float to integer conversion" (SDoc -> doc) -> SDoc -> doc
forall a b. (a -> b) -> a -> b
$
                  Line SDoc -> SDoc
forall doc. IsDoc doc => Line doc -> doc
line (Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"->" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2)

  FABS Operand
o1 Operand
o2 | Operand -> Bool
isSingleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfabs.s") Operand
o1 Operand
o2
  FABS Operand
o1 Operand
o2 | Operand -> Bool
isDoubleOp Operand
o2 -> Line doc -> Operand -> Operand -> doc
op2 (String -> Line doc
forall doc. IsLine doc => String -> doc
text String
"\tfabs.d") Operand
o1 Operand
o2
  Instr
instr -> String -> doc
forall a. HasCallStack => String -> a
panic (String -> doc) -> String -> doc
forall a b. (a -> b) -> a -> b
$ String
"RV64.pprInstr - Unknown instruction: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Instr -> String
instrCon Instr
instr
 where op2 :: Line doc -> Operand -> Operand -> doc
op2 Line doc
op Operand
o1 Operand
o2        = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ Line doc
op Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2
       op3 :: Line doc -> Operand -> Operand -> Operand -> doc
op3 Line doc
op Operand
o1 Operand
o2 Operand
o3     = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ Line doc
op Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o3
       op4 :: Line doc -> Operand -> Operand -> Operand -> Operand -> doc
op4 Line doc
op Operand
o1 Operand
o2 Operand
o3 Operand
o4  = Line doc -> doc
forall doc. IsDoc doc => Line doc -> doc
line (Line doc -> doc) -> Line doc -> doc
forall a b. (a -> b) -> a -> b
$ Line doc
op Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o1 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o2 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o3 Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<> Line doc
forall doc. IsLine doc => doc
comma Line doc -> Line doc -> Line doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> Line doc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
platform Operand
o4
      --  op_ldr o1 rest      = line $ text "\tld" <+> pprOp platform o1 <> comma <+> rest  <+> text "(" <> pprOp platform o1 <> text ")"
      --  op_adrp o1 rest     = line $ text "\tauipc" <+> pprOp platform o1 <> comma <+> rest
      --  op_add o1 rest      = line $ text "\taddi" <+> pprOp platform o1 <> comma <+> pprOp platform o1 <> comma <+> rest
       pprDmbType :: DmbType -> doc
pprDmbType DmbType
DmbRead = String -> doc
forall doc. IsLine doc => String -> doc
text String
"r"
       pprDmbType DmbType
DmbWrite = String -> doc
forall doc. IsLine doc => String -> doc
text String
"w"
       pprDmbType DmbType
DmbReadWrite = String -> doc
forall doc. IsLine doc => String -> doc
text String
"rw"

floatOpPrecision :: Platform -> Operand -> Operand -> String
floatOpPrecision :: Platform -> Operand -> Operand -> String
floatOpPrecision Platform
_p Operand
l Operand
r | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isSingleOp Operand
r = String
"s" -- single precision
floatOpPrecision Platform
_p Operand
l Operand
r | Operand -> Bool
isFloatOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isFloatOp Operand
r Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
l Bool -> Bool -> Bool
&& Operand -> Bool
isDoubleOp Operand
r = String
"d" -- double precision
floatOpPrecision Platform
p Operand
l Operand
r = String -> SDoc -> String
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Cannot determine floating point precission" (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"op1" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
p Operand
l SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"op2" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> Platform -> Operand -> SDoc
forall doc. IsLine doc => Platform -> Operand -> doc
pprOp Platform
p Operand
r)

pprBcond :: (IsLine doc) => Cond -> doc
pprBcond :: forall doc. IsLine doc => Cond -> doc
pprBcond Cond
c = String -> doc
forall doc. IsLine doc => String -> doc
text String
"b" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Cond -> doc
forall doc. IsLine doc => Cond -> doc
pprCond Cond
c
  where
    pprCond :: (IsLine doc) => Cond -> doc
    pprCond :: forall doc. IsLine doc => Cond -> doc
pprCond Cond
c = case Cond
c of
      Cond
EQ -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"eq"
      Cond
NE -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"ne"
      Cond
SLT -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"lt"
      Cond
SLE -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"le"
      Cond
SGE -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"ge"
      Cond
SGT -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"gt"
      Cond
ULT -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"ltu"
      Cond
ULE -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"leu"
      Cond
UGE -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"geu"
      Cond
UGT -> String -> doc
forall doc. IsLine doc => String -> doc
text String
"gtu"
      -- BCOND cannot handle floating point comparisons / registers
      Cond
_ -> String -> doc
forall a. HasCallStack => String -> a
panic (String -> doc) -> String -> doc
forall a b. (a -> b) -> a -> b
$ String
"RV64.ppr: unhandled BCOND conditional: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Cond -> String
forall a. Show a => a -> String
show Cond
c