-- ---------------------------------------------------------------------
-- a local table branch
--
-- we find the fall-through cases
--
pprSwitch :: DynFlags -> CmmExpr -> SwitchTargets -> SDoc
pprSwitch dflags e ids
  = (hang (ptext (sLit "switch") <+> parens ( pprExpr e ) <+> lbrace)
                4 (vcat ( map caseify pairs ) $$ def)) $$ rbrace
  where
    (pairs, mbdef) = switchTargetsFallThrough ids

    -- fall through case
    caseify (ix:ixs, ident) = vcat (map do_fallthrough ixs) $$ final_branch ix
        where
        do_fallthrough ix =
                 hsep [ ptext (sLit "case") , pprHexVal ix (wordWidth dflags) <> colon ,
                        ptext (sLit "/* fall through */") ]

        final_branch ix =
                hsep [ ptext (sLit "case") , pprHexVal ix (wordWidth dflags) <> colon ,
                       ptext (sLit "goto") , (pprBlockId ident) <> semi ]

    caseify (_     , _    ) = panic "pprSwitch: switch with no cases!"

    def | Just l <- mbdef = ptext (sLit "default: goto") <+> pprBlockId l <> semi
        | otherwise       = empty

-- ---------------------------------------------------------------------
-- Expressions.
--

-- C Types: the invariant is that the C expression generated by
--
--      pprExpr e
--
-- has a type in C which is also given by
--
--      machRepCType (cmmExprType e)
--
-- (similar invariants apply to the rest of the pretty printer).
pprSwitch :: DynFlags -> CmmExpr -> SwitchTargets -> SDoc
pprSwitch dflags e ids
  = (hang (ptext (sLit "switch") <+> parens ( pprExpr e ) <+> lbrace)
                4 (vcat ( map caseify pairs ) $$ def)) $$ rbrace
  where
    (pairs, mbdef) = switchTargetsFallThrough ids

    -- fall through case
    caseify (ix:ixs, ident) = vcat (map do_fallthrough ixs) $$ final_branch ix
        where
        do_fallthrough ix =
                 hsep [ ptext (sLit "case") , pprHexVal ix (wordWidth dflags) <> colon ,
                        ptext (sLit "/* fall through */") ]

        final_branch ix =
                hsep [ ptext (sLit "case") , pprHexVal ix (wordWidth dflags) <> colon ,
                       ptext (sLit "goto") , (pprBlockId ident) <> semi ]

    caseify (_     , _    ) = panic "pprSwitch: switch with no cases!"

    def | Just l <- mbdef = ptext (sLit "default: goto") <+> pprBlockId l <> semi
        | otherwise       = empty

-- ---------------------------------------------------------------------
-- Expressions.
--

-- C Types: the invariant is that the C expression generated by
--
--      pprExpr e
--
-- has a type in C which is also given by
--
--      machRepCType (cmmExprType e)
--
-- (similar invariants apply to the rest of the pretty printer).
pprSwitch dflags e ids
  = (hang (ptext (sLit "switch") <+> parens ( pprExpr e ) <+> lbrace)
                4 (vcat ( map caseify pairs ) $$ def)) $$ rbrace
  where
    (pairs, mbdef) = switchTargetsFallThrough ids

    -- fall through case
    caseify (ix:ixs, ident) = vcat (map do_fallthrough ixs) $$ final_branch ix
        where
        do_fallthrough ix =
                 hsep [ ptext (sLit "case") , pprHexVal ix (wordWidth dflags) <> colon ,
                        ptext (sLit "/* fall through */") ]

        final_branch ix =
                hsep [ ptext (sLit "case") , pprHexVal ix (wordWidth dflags) <> colon ,
                       ptext (sLit "goto") , (pprBlockId ident) <> semi ]

    caseify (_     , _    ) = panic "pprSwitch: switch with no cases!"

    def | Just l <- mbdef = ptext (sLit "default: goto") <+> pprBlockId l <> semi
        | otherwise       = empty

-- ---------------------------------------------------------------------
-- Expressions.
--

-- C Types: the invariant is that the C expression generated by
--
--      pprExpr e
--
-- has a type in C which is also given by
--
--      machRepCType (cmmExprType e)
--
-- (similar invariants apply to the rest of the pretty printer).