Regs.hs 12.5 KB
Newer Older
1
module X86.Regs (
Ian Lynagh's avatar
Ian Lynagh committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        -- squeese functions for the graph allocator
        virtualRegSqueeze,
        realRegSqueeze,

        -- immediates
        Imm(..),
        strImmLit,
        litToImm,

        -- addressing modes
        AddrMode(..),
        addrOffset,

        -- registers
        spRel,
        argRegs,
18
        allArgRegs,
19
        allIntArgRegs,
Ian Lynagh's avatar
Ian Lynagh committed
20
        callClobberedRegs,
21
        instrClobberedRegs,
Ian Lynagh's avatar
Ian Lynagh committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
        allMachRegNos,
        classOfRealReg,
        showReg,

        -- machine specific
        EABase(..), EAIndex(..), addrModeRegs,

        eax, ebx, ecx, edx, esi, edi, ebp, esp,
        fake0, fake1, fake2, fake3, fake4, fake5, firstfake,

        rax, rbx, rcx, rdx, rsi, rdi, rbp, rsp,
        r8,  r9,  r10, r11, r12, r13, r14, r15,
        xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7,
        xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15,
        xmm,

        ripRel,
        allFPArgRegs,

        allocatableRegs
42
43
44
45
46
47
48
)

where

#include "nativeGen/NCG.h"
#include "HsVersions.h"

49
import CodeGen.Platform
50
51
import Reg
import RegClass
52

53
import Cmm
54
import CLabel           ( CLabel )
55
import DynFlags
56
import Outputable
Ian Lynagh's avatar
Ian Lynagh committed
57
import Platform
58
import FastTypes
59
60
61
import FastBool


62
-- | regSqueeze_class reg
Ian Lynagh's avatar
Ian Lynagh committed
63
64
65
--      Calculuate the maximum number of register colors that could be
--      denied to a node of this class due to having this reg
--      as a neighbour.
66
67
68
69
70
71
--
{-# INLINE virtualRegSqueeze #-}
virtualRegSqueeze :: RegClass -> VirtualReg -> FastInt

virtualRegSqueeze cls vr
 = case cls of
Ian Lynagh's avatar
Ian Lynagh committed
72
73
74
75
        RcInteger
         -> case vr of
                VirtualRegI{}           -> _ILIT(1)
                VirtualRegHi{}          -> _ILIT(1)
76
                _other                  -> _ILIT(0)
77

Ian Lynagh's avatar
Ian Lynagh committed
78
79
80
81
        RcDouble
         -> case vr of
                VirtualRegD{}           -> _ILIT(1)
                VirtualRegF{}           -> _ILIT(0)
82
                _other                  -> _ILIT(0)
83

Ian Lynagh's avatar
Ian Lynagh committed
84
85
86
        RcDoubleSSE
         -> case vr of
                VirtualRegSSE{}         -> _ILIT(1)
87
                _other                  -> _ILIT(0)
88

89
        _other -> _ILIT(0)
90

91
92
{-# INLINE realRegSqueeze #-}
realRegSqueeze :: RegClass -> RealReg -> FastInt
93
94
realRegSqueeze cls rr
 = case cls of
Ian Lynagh's avatar
Ian Lynagh committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
        RcInteger
         -> case rr of
                RealRegSingle regNo
                        | regNo < firstfake -> _ILIT(1)
                        | otherwise     -> _ILIT(0)

                RealRegPair{}           -> _ILIT(0)

        RcDouble
         -> case rr of
                RealRegSingle regNo
                        | regNo >= firstfake && regNo <= lastfake -> _ILIT(1)
                        | otherwise     -> _ILIT(0)

                RealRegPair{}           -> _ILIT(0)
110

111
        RcDoubleSSE
Ian Lynagh's avatar
Ian Lynagh committed
112
113
         -> case rr of
                RealRegSingle regNo | regNo >= firstxmm -> _ILIT(1)
114
                _otherwise                        -> _ILIT(0)
115

116
        _other -> _ILIT(0)
117

118
119
120
121
-- -----------------------------------------------------------------------------
-- Immediates

data Imm
Ian Lynagh's avatar
Ian Lynagh committed
122
123
124
  = ImmInt      Int
  | ImmInteger  Integer     -- Sigh.
  | ImmCLbl     CLabel      -- AbstractC Label (with baggage)
125
  | ImmLit      SDoc        -- Simple string
126
  | ImmIndex    CLabel Int
Ian Lynagh's avatar
Ian Lynagh committed
127
128
  | ImmFloat    Rational
  | ImmDouble   Rational
129
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
  | ImmConstantSum Imm Imm
  | ImmConstantDiff Imm Imm


strImmLit :: String -> Imm
strImmLit s = ImmLit (text s)


litToImm :: CmmLit -> Imm
litToImm (CmmInt i w)        = ImmInteger (narrowS w i)
                -- narrow to the width: a CmmInt might be out of
                -- range, but we assume that ImmInteger only contains
                -- in-range values.  A signed value should be fine here.
litToImm (CmmFloat f W32)    = ImmFloat f
litToImm (CmmFloat f W64)    = ImmDouble f
litToImm (CmmLabel l)        = ImmCLbl l
litToImm (CmmLabelOff l off) = ImmIndex l off
litToImm (CmmLabelDiffOff l1 l2 off)
                             = ImmConstantSum
                               (ImmConstantDiff (ImmCLbl l1) (ImmCLbl l2))
                               (ImmInt off)
litToImm _                   = panic "X86.Regs.litToImm: no match"

-- addressing modes ------------------------------------------------------------

data AddrMode
Ian Lynagh's avatar
Ian Lynagh committed
155
156
        = AddrBaseIndex EABase EAIndex Displacement
        | ImmAddr Imm Int
157
158
159
160
161
162
163
164
165

data EABase       = EABaseNone  | EABaseReg Reg | EABaseRip
data EAIndex      = EAIndexNone | EAIndex Reg Int
type Displacement = Imm


addrOffset :: AddrMode -> Int -> Maybe AddrMode
addrOffset addr off
  = case addr of
Ian Lynagh's avatar
Ian Lynagh committed
166
      ImmAddr i off0      -> Just (ImmAddr i (off0 + off))
167
168
169

      AddrBaseIndex r i (ImmInt n) -> Just (AddrBaseIndex r i (ImmInt (n + off)))
      AddrBaseIndex r i (ImmInteger n)
Ian Lynagh's avatar
Ian Lynagh committed
170
        -> Just (AddrBaseIndex r i (ImmInt (fromInteger (n + toInteger off))))
171
172

      AddrBaseIndex r i (ImmCLbl lbl)
Ian Lynagh's avatar
Ian Lynagh committed
173
        -> Just (AddrBaseIndex r i (ImmIndex lbl off))
174
175

      AddrBaseIndex r i (ImmIndex lbl ix)
Ian Lynagh's avatar
Ian Lynagh committed
176
        -> Just (AddrBaseIndex r i (ImmIndex lbl (ix+off)))
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

      _ -> Nothing  -- in theory, shouldn't happen


addrModeRegs :: AddrMode -> [Reg]
addrModeRegs (AddrBaseIndex b i _) =  b_regs ++ i_regs
  where
   b_regs = case b of { EABaseReg r -> [r]; _ -> [] }
   i_regs = case i of { EAIndex r _ -> [r]; _ -> [] }
addrModeRegs _ = []


-- registers -------------------------------------------------------------------

-- @spRel@ gives us a stack relative addressing mode for volatile
-- temporaries and for excess call arguments.  @fpRel@, where
-- applicable, is the same but for the frame pointer.


196
spRel :: DynFlags
197
      -> Int -- ^ desired stack offset in bytes, positive or negative
198
      -> AddrMode
199
200
spRel dflags n
 | target32Bit (targetPlatform dflags)
201
    = AddrBaseIndex (EABaseReg esp) EAIndexNone (ImmInt n)
Ian Lynagh's avatar
Ian Lynagh committed
202
 | otherwise
203
    = AddrBaseIndex (EABaseReg rsp) EAIndexNone (ImmInt n)
204

205
206
207
208
-- The register numbers must fit into 32 bits on x86, so that we can
-- use a Word32 to represent the set of free registers in the register
-- allocator.

209
210
211
212
firstfake, lastfake :: RegNo
firstfake = 16
lastfake  = 21

213
firstxmm :: RegNo
214
firstxmm  = 24
215
216
217
218
219

lastxmm :: Platform -> RegNo
lastxmm platform
 | target32Bit platform = 31
 | otherwise            = 39
220

221
222
223
224
225
226
227
lastint :: Platform -> RegNo
lastint platform
 | target32Bit platform = 7 -- not %r8..%r15
 | otherwise            = 15

intregnos :: Platform -> [RegNo]
intregnos platform = [0 .. lastint platform]
228

229
fakeregnos :: [RegNo]
230
fakeregnos  = [firstfake .. lastfake]
231
232
233
234
235
236

xmmregnos :: Platform -> [RegNo]
xmmregnos platform = [firstxmm  .. lastxmm platform]

floatregnos :: Platform -> [RegNo]
floatregnos platform = fakeregnos ++ xmmregnos platform
237

238
239
240
241
242

-- argRegs is the set of regs which are read for an n-argument call to C.
-- For archs which pass all args on the stack (x86), is empty.
-- Sparc passes up to the first 6 args in regs.
argRegs :: RegNo -> [Reg]
Ian Lynagh's avatar
Ian Lynagh committed
243
argRegs _       = panic "MachRegs.argRegs(x86): should not be used!"
244
245

-- | The complete set of machine registers.
246
allMachRegNos :: Platform -> [RegNo]
247
allMachRegNos platform = intregnos platform ++ floatregnos platform
248
249

-- | Take the class of a register.
250
251
{-# INLINE classOfRealReg #-}
classOfRealReg :: Platform -> RealReg -> RegClass
252
253
254
255
-- On x86, we might want to have an 8-bit RegClass, which would
-- contain just regs 1-4 (the others don't have 8-bit versions).
-- However, we can get away without this at the moment because the
-- only allocatable integer regs are also 8-bit compatible (1, 3, 4).
256
classOfRealReg platform reg
257
 = case reg of
Ian Lynagh's avatar
Ian Lynagh committed
258
        RealRegSingle i
259
260
261
          | i <= lastint platform -> RcInteger
          | i <= lastfake         -> RcDouble
          | otherwise             -> RcDoubleSSE
262

Ian Lynagh's avatar
Ian Lynagh committed
263
        RealRegPair{}   -> panic "X86.Regs.classOfRealReg: RegPairs on this arch"
264
265

-- | Get the name of the register with this number.
Ian Lynagh's avatar
Ian Lynagh committed
266
267
showReg :: Platform -> RegNo -> String
showReg platform n
Ian Lynagh's avatar
Ian Lynagh committed
268
        | n >= firstxmm  = "%xmm" ++ show (n-firstxmm)
269
        | n >= firstfake = "%fake" ++ show (n-firstfake)
Ian Lynagh's avatar
Ian Lynagh committed
270
        | n >= 8         = "%r" ++ show n
Ian Lynagh's avatar
Ian Lynagh committed
271
272
273
274
275
276
277
        | otherwise      = regNames platform !! n

regNames :: Platform -> [String]
regNames platform
    = if target32Bit platform
      then ["%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "%ebp", "%esp"]
      else ["%rax", "%rbx", "%rcx", "%rdx", "%rsi", "%rdi", "%rbp", "%rsp"]
278
279


280

281
282
283
284
285
286
287
288
289
-- machine specific ------------------------------------------------------------


{-
Intel x86 architecture:
- All registers except 7 (esp) are available for use.
- Only ebx, esi, edi and esp are available across a C call (they are callee-saves).
- Registers 0-7 have 16-bit counterparts (ax, bx etc.)
- Registers 0-3 have 8 bit counterparts (ah, bh etc.)
290
- Registers fake0..fake5 are fakes; we pretend x86 has 6 conventionally-addressable
291
292
293
294
295
296
297
298
  fp registers, and 3-operand insns for them, and we translate this into
  real stack-based x86 fp code after register allocation.

The fp registers are all Double registers; we don't have any RcFloat class
regs.  @regClass@ barfs if you give it a VirtualRegF, and mkVReg above should
never generate them.
-}

Ian Lynagh's avatar
Ian Lynagh committed
299
fake0, fake1, fake2, fake3, fake4, fake5,
300
       eax, ebx, ecx, edx, esp, ebp, esi, edi :: Reg
301

302
303
304
305
306
307
308
309
eax   = regSingle 0
ebx   = regSingle 1
ecx   = regSingle 2
edx   = regSingle 3
esi   = regSingle 4
edi   = regSingle 5
ebp   = regSingle 6
esp   = regSingle 7
310
311
312
313
314
315
fake0 = regSingle 16
fake1 = regSingle 17
fake2 = regSingle 18
fake3 = regSingle 19
fake4 = regSingle 20
fake5 = regSingle 21
316
317
318
319
320



{-
AMD x86_64 architecture:
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
- All 16 integer registers are addressable as 8, 16, 32 and 64-bit values:

  8     16    32    64
  ---------------------
  al    ax    eax   rax
  bl    bx    ebx   rbx
  cl    cx    ecx   rcx
  dl    dx    edx   rdx
  sil   si    esi   rsi
  dil   si    edi   rdi
  bpl   bp    ebp   rbp
  spl   sp    esp   rsp
  r10b  r10w  r10d  r10
  r11b  r11w  r11d  r11
  r12b  r12w  r12d  r12
  r13b  r13w  r13d  r13
  r14b  r14w  r14d  r14
  r15b  r15w  r15d  r15
339
340
-}

Ian Lynagh's avatar
Ian Lynagh committed
341
rax, rbx, rcx, rdx, rsp, rbp, rsi, rdi,
342
343
344
345
  r8, r9, r10, r11, r12, r13, r14, r15,
  xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7,
  xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15 :: Reg

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
rax   = regSingle 0
rbx   = regSingle 1
rcx   = regSingle 2
rdx   = regSingle 3
rsi   = regSingle 4
rdi   = regSingle 5
rbp   = regSingle 6
rsp   = regSingle 7
r8    = regSingle 8
r9    = regSingle 9
r10   = regSingle 10
r11   = regSingle 11
r12   = regSingle 12
r13   = regSingle 13
r14   = regSingle 14
r15   = regSingle 15
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
xmm0  = regSingle 24
xmm1  = regSingle 25
xmm2  = regSingle 26
xmm3  = regSingle 27
xmm4  = regSingle 28
xmm5  = regSingle 29
xmm6  = regSingle 30
xmm7  = regSingle 31
xmm8  = regSingle 32
xmm9  = regSingle 33
xmm10 = regSingle 34
xmm11 = regSingle 35
xmm12 = regSingle 36
xmm13 = regSingle 37
xmm14 = regSingle 38
xmm15 = regSingle 39
378

379
ripRel :: Displacement -> AddrMode
Ian Lynagh's avatar
Ian Lynagh committed
380
ripRel imm      = AddrBaseIndex EABaseRip EAIndexNone imm
381
382


383
 -- so we can re-use some x86 code:
384
{-
385
386
387
388
389
390
391
392
eax = rax
ebx = rbx
ecx = rcx
edx = rdx
esi = rsi
edi = rdi
ebp = rbp
esp = rsp
393
-}
394

395
xmm :: RegNo -> Reg
396
xmm n = regSingle (firstxmm+n)
397
398
399
400




401
-- | these are the regs which we cannot assume stay alive over a C call.
402
callClobberedRegs       :: Platform -> [Reg]
403
404
405
406
407
408
409
410
-- caller-saves registers
callClobberedRegs platform
 | target32Bit platform = [eax,ecx,edx] ++ map regSingle (floatregnos platform)
 | otherwise
    -- all xmm regs are caller-saves
    -- caller-saves registers
    = [rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11]
   ++ map regSingle (floatregnos platform)
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
allArgRegs :: Platform -> [(Reg, Reg)]
allArgRegs platform
 | platformOS platform == OSMinGW32 = zip [rcx,rdx,r8,r9]
                                          (map regSingle [firstxmm ..])
 | otherwise = panic "X86.Regs.allArgRegs: not defined for this arch"

allIntArgRegs :: Platform -> [Reg]
allIntArgRegs platform
 | (platformOS platform == OSMinGW32) || target32Bit platform
    = panic "X86.Regs.allIntArgRegs: not defined for this platform"
 | otherwise = [rdi,rsi,rdx,rcx,r8,r9]

allFPArgRegs :: Platform -> [Reg]
allFPArgRegs platform
 | platformOS platform == OSMinGW32
    = panic "X86.Regs.allFPArgRegs: not defined for this platform"
 | otherwise = map regSingle [firstxmm .. firstxmm+7]

430
431
432
433
434
435
436
437
-- Machine registers which might be clobbered by instructions that
-- generate results into fixed registers, or need arguments in a fixed
-- register.
instrClobberedRegs :: Platform -> [Reg]
instrClobberedRegs platform
 | target32Bit platform = [ eax, ecx, edx ]
 | otherwise            = [ rax, rcx, rdx ]

Ian Lynagh's avatar
Ian Lynagh committed
438
--
Ben.Lippmeier@anu.edu.au's avatar
Ben.Lippmeier@anu.edu.au committed
439

440
441
442
-- allocatableRegs is allMachRegNos with the fixed-use regs removed.
-- i.e., these are the regs for which we are prepared to allow the
-- register allocator to attempt to map VRegs to.
443
444
allocatableRegs :: Platform -> [RealReg]
allocatableRegs platform
445
   = let isFree i = isFastTrue (freeReg platform i)
446
     in  map RealRegSingle $ filter isFree (allMachRegNos platform)
447