Cmm.hs 4.97 KB
Newer Older
1 2 3
-- Cmm representations using Hoopl's Graph CmmNode e x.
{-# LANGUAGE GADTs #-}
{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
Simon Marlow's avatar
Simon Marlow committed
4

5 6
module Cmm (
     -- * Cmm top-level datatypes
Simon Peyton Jones's avatar
Simon Peyton Jones committed
7 8
     CmmProgram, CmmGroup, GenCmmGroup,
     CmmDecl, GenCmmDecl(..),
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
     CmmGraph, GenCmmGraph(..),
     CmmBlock,
     Section(..), CmmStatics(..), CmmStatic(..),

     -- * Cmm graphs
     CmmReplGraph, GenCmmReplGraph, CmmFwdRewrite, CmmBwdRewrite,
   
     -- * Info Tables
     CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..),
     ClosureTypeInfo(..), 
     C_SRT(..), needsSRT,
     ProfilingInfo(..), ConstrDescription, 

     -- * Statements, expressions and types
     module CmmNode,
     module CmmExpr,
  ) where

import CLabel
28
import BlockId
29
import CmmNode
30
import SMRep
31
import CmmExpr
32
import UniqSupply
33
import Compiler.Hoopl
34 35

import Data.Word        ( Word8 )
36 37

#include "HsVersions.h"
38

39 40 41 42
-----------------------------------------------------------------------------
--  Cmm, GenCmm
-----------------------------------------------------------------------------

Simon Peyton Jones's avatar
Simon Peyton Jones committed
43 44
-- A CmmProgram is a list of CmmGroups  
-- A CmmGroup is a list of top-level declarations  
45

Simon Peyton Jones's avatar
Simon Peyton Jones committed
46 47 48 49 50 51 52 53 54 55 56 57 58
-- When object-splitting is on,each group is compiled into a separate
-- .o file. So typically we put closely related stuff in a CmmGroup.

type CmmProgram = [CmmGroup]

type GenCmmGroup d h g = [GenCmmDecl d h g]
type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo CmmGraph

-----------------------------------------------------------------------------
--  CmmDecl, GenCmmDecl
-----------------------------------------------------------------------------

-- GenCmmDecl is abstracted over
59 60 61 62 63 64 65 66 67 68 69
--   d, the type of static data elements in CmmData
--   h, the static info preceding the code of a CmmProc
--   g, the control-flow graph of a CmmProc
--
-- We expect there to be two main instances of this type:
--   (a) C--, i.e. populated with various C-- constructs
--       (Cmm and RawCmm in OldCmm.hs)
--   (b) Native code, populated with data/instructions

-- | A top-level chunk, abstracted over the type of the contents of
-- the basic blocks (Cmm or instructions are the likely instantiations).
Simon Peyton Jones's avatar
Simon Peyton Jones committed
70
data GenCmmDecl d h g
71 72 73 74 75 76 77 78 79
  = CmmProc     -- A procedure
     h                 -- Extra header such as the info table
     CLabel            -- Entry label
     g                 -- Control-flow graph for the procedure's code

  | CmmData     -- Static data
        Section
        d

Simon Peyton Jones's avatar
Simon Peyton Jones committed
80
type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
81 82 83 84

-----------------------------------------------------------------------------
--     Graphs
-----------------------------------------------------------------------------
85

86 87
type CmmGraph = GenCmmGraph CmmNode
data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
88 89
type CmmBlock = Block CmmNode C C

90
type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
91 92 93
type GenCmmReplGraph n e x = UniqSM (Maybe (Graph n e x))
type CmmFwdRewrite f = FwdRewrite UniqSM CmmNode f
type CmmBwdRewrite f = BwdRewrite UniqSM CmmNode f
94

95 96 97 98
-----------------------------------------------------------------------------
--     Info Tables
-----------------------------------------------------------------------------

Simon Marlow's avatar
Simon Marlow committed
99 100
data CmmTopInfo   = TopInfo { info_tbl :: CmmInfoTable
                            , stack_info :: CmmStackInfo }
101 102 103

data CmmStackInfo
   = StackInfo {
Simon Marlow's avatar
Simon Marlow committed
104 105 106 107
       arg_space :: ByteOff,
               -- number of bytes of arguments on the stack on entry to the
               -- the proc.  This is filled in by StgCmm.codeGen, and used
               -- by the stack allocator later.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
       updfr_space :: Maybe ByteOff     -- XXX: comment?
   }

-- | Info table as a haskell data type
data CmmInfoTable
  = CmmInfoTable {
      cit_lbl  :: CLabel, -- Info table label
      cit_rep  :: SMRep,
      cit_prof :: ProfilingInfo,
      cit_srt  :: C_SRT
    }
  | CmmNonInfoTable   -- Procedure doesn't need an info table

data ProfilingInfo
  = NoProfilingInfo
  | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc

-- C_SRT is what StgSyn.SRT gets translated to... 
-- we add a label for the table, and expect only the 'offset/length' form

data C_SRT = NoC_SRT
129
           | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
           deriving (Eq)

needsSRT :: C_SRT -> Bool
needsSRT NoC_SRT       = False
needsSRT (C_SRT _ _ _) = True

-----------------------------------------------------------------------------
--              Static Data
-----------------------------------------------------------------------------

data Section
  = Text
  | Data
  | ReadOnlyData
  | RelocatableReadOnlyData
  | UninitialisedData
  | ReadOnlyData16      -- .rodata.cst16 on x86_64, 16-byte aligned
  | OtherSection String

data CmmStatic
  = CmmStaticLit CmmLit
        -- a literal value, size given by cmmLitRep of the literal.
  | CmmUninitialised Int
        -- uninitialised data, N bytes long
  | CmmString [Word8]
        -- string of 8-bit values only, not zero terminated.

data CmmStatics
   = Statics
       CLabel      -- Label of statics
       [CmmStatic] -- The static data itself