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