Remove dead generics-related code from OccName
[ghc.git] / compiler / nativeGen / Instruction.hs
1
2 module Instruction (
3 RegUsage(..),
4 noUsage,
5 GenBasicBlock(..), blockId,
6 ListGraph(..),
7 NatCmm,
8 NatCmmDecl,
9 NatBasicBlock,
10 topInfoTable,
11 entryBlocks,
12 Instruction(..)
13 )
14
15 where
16
17 import Reg
18
19 import BlockId
20 import DynFlags
21 import Cmm hiding (topInfoTable)
22 import Platform
23
24 -- | Holds a list of source and destination registers used by a
25 -- particular instruction.
26 --
27 -- Machine registers that are pre-allocated to stgRegs are filtered
28 -- out, because they are uninteresting from a register allocation
29 -- standpoint. (We wouldn't want them to end up on the free list!)
30 --
31 -- As far as we are concerned, the fixed registers simply don't exist
32 -- (for allocation purposes, anyway).
33 --
34 data RegUsage
35 = RU [Reg] [Reg]
36
37 -- | No regs read or written to.
38 noUsage :: RegUsage
39 noUsage = RU [] []
40
41 -- Our flavours of the Cmm types
42 -- Type synonyms for Cmm populated with native code
43 type NatCmm instr
44 = GenCmmGroup
45 CmmStatics
46 (BlockEnv CmmStatics)
47 (ListGraph instr)
48
49 type NatCmmDecl statics instr
50 = GenCmmDecl
51 statics
52 (BlockEnv CmmStatics)
53 (ListGraph instr)
54
55
56 type NatBasicBlock instr
57 = GenBasicBlock instr
58
59
60 -- | Returns the info table associated with the CmmDecl's entry point,
61 -- if any.
62 topInfoTable :: GenCmmDecl a (BlockEnv i) (ListGraph b) -> Maybe i
63 topInfoTable (CmmProc infos _ _ (ListGraph (b:_)))
64 = mapLookup (blockId b) infos
65 topInfoTable _
66 = Nothing
67
68 -- | Return the list of BlockIds in a CmmDecl that are entry points
69 -- for this proc (i.e. they may be jumped to from outside this proc).
70 entryBlocks :: GenCmmDecl a (BlockEnv i) (ListGraph b) -> [BlockId]
71 entryBlocks (CmmProc info _ _ (ListGraph code)) = entries
72 where
73 infos = mapKeys info
74 entries = case code of
75 [] -> infos
76 BasicBlock entry _ : _ -- first block is the entry point
77 | entry `elem` infos -> infos
78 | otherwise -> entry : infos
79 entryBlocks _ = []
80
81 -- | Common things that we can do with instructions, on all architectures.
82 -- These are used by the shared parts of the native code generator,
83 -- specifically the register allocators.
84 --
85 class Instruction instr where
86
87 -- | Get the registers that are being used by this instruction.
88 -- regUsage doesn't need to do any trickery for jumps and such.
89 -- Just state precisely the regs read and written by that insn.
90 -- The consequences of control flow transfers, as far as register
91 -- allocation goes, are taken care of by the register allocator.
92 --
93 regUsageOfInstr
94 :: Platform
95 -> instr
96 -> RegUsage
97
98
99 -- | Apply a given mapping to all the register references in this
100 -- instruction.
101 patchRegsOfInstr
102 :: instr
103 -> (Reg -> Reg)
104 -> instr
105
106
107 -- | Checks whether this instruction is a jump/branch instruction.
108 -- One that can change the flow of control in a way that the
109 -- register allocator needs to worry about.
110 isJumpishInstr
111 :: instr -> Bool
112
113
114 -- | Give the possible destinations of this jump instruction.
115 -- Must be defined for all jumpish instructions.
116 jumpDestsOfInstr
117 :: instr -> [BlockId]
118
119
120 -- | Change the destination of this jump instruction.
121 -- Used in the linear allocator when adding fixup blocks for join
122 -- points.
123 patchJumpInstr
124 :: instr
125 -> (BlockId -> BlockId)
126 -> instr
127
128
129 -- | An instruction to spill a register into a spill slot.
130 mkSpillInstr
131 :: DynFlags
132 -> Reg -- ^ the reg to spill
133 -> Int -- ^ the current stack delta
134 -> Int -- ^ spill slot to use
135 -> instr
136
137
138 -- | An instruction to reload a register from a spill slot.
139 mkLoadInstr
140 :: DynFlags
141 -> Reg -- ^ the reg to reload.
142 -> Int -- ^ the current stack delta
143 -> Int -- ^ the spill slot to use
144 -> instr
145
146 -- | See if this instruction is telling us the current C stack delta
147 takeDeltaInstr
148 :: instr
149 -> Maybe Int
150
151 -- | Check whether this instruction is some meta thing inserted into
152 -- the instruction stream for other purposes.
153 --
154 -- Not something that has to be treated as a real machine instruction
155 -- and have its registers allocated.
156 --
157 -- eg, comments, delta, ldata, etc.
158 isMetaInstr
159 :: instr
160 -> Bool
161
162
163
164 -- | Copy the value in a register to another one.
165 -- Must work for all register classes.
166 mkRegRegMoveInstr
167 :: Platform
168 -> Reg -- ^ source register
169 -> Reg -- ^ destination register
170 -> instr
171
172 -- | Take the source and destination from this reg -> reg move instruction
173 -- or Nothing if it's not one
174 takeRegRegMoveInstr
175 :: instr
176 -> Maybe (Reg, Reg)
177
178 -- | Make an unconditional jump instruction.
179 -- For architectures with branch delay slots, its ok to put
180 -- a NOP after the jump. Don't fill the delay slot with an
181 -- instruction that references regs or you'll confuse the
182 -- linear allocator.
183 mkJumpInstr
184 :: BlockId
185 -> [instr]
186
187
188 -- Subtract an amount from the C stack pointer
189 mkStackAllocInstr
190 :: Platform -- TODO: remove (needed by x86/x86_64
191 -- because they share an Instr type)
192 -> Int
193 -> instr
194
195 -- Add an amount to the C stack pointer
196 mkStackDeallocInstr
197 :: Platform -- TODO: remove (needed by x86/x86_64
198 -- because they share an Instr type)
199 -> Int
200 -> instr