summaryrefslogtreecommitdiff
path: root/compiler/nativeGen/SPARC/Instr.hs
blob: 601e04787a5411df9611dbfe2bc5b43ff39b3345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
-----------------------------------------------------------------------------
--
-- Machine-dependent assembly language
--
-- (c) The University of Glasgow 1993-2004
--
-----------------------------------------------------------------------------

{-# OPTIONS -fno-warn-tabs #-}
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and
-- detab the module (please do the detabbing in a separate patch). See
--     http://ghc.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
-- for details

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

module SPARC.Instr (
	RI(..),
	riZero,

	fpRelEA,
	moveSp,
	
	isUnconditionalJump,
	
	Instr(..),
	maxSpillSlots
)

where

import SPARC.Stack
import SPARC.Imm
import SPARC.AddrMode
import SPARC.Cond
import SPARC.Regs
import SPARC.Base
import TargetReg
import Instruction
import RegClass
import Reg
import Size

import CLabel
import CodeGen.Platform
import BlockId
import DynFlags
import Cmm
import FastString
import FastBool
import Outputable
import Platform


-- | Register or immediate
data RI 
	= RIReg Reg
	| RIImm Imm

-- | Check if a RI represents a zero value.
--  	- a literal zero
--	- register %g0, which is always zero.
--
riZero :: RI -> Bool	
riZero (RIImm (ImmInt 0))			= True
riZero (RIImm (ImmInteger 0))			= True
riZero (RIReg (RegReal (RealRegSingle 0)))	= True
riZero _					= False


-- | Calculate the effective address which would be used by the
-- 	corresponding fpRel sequence. 
fpRelEA :: Int -> Reg -> Instr
fpRelEA n dst
   = ADD False False fp (RIImm (ImmInt (n * wordLength))) dst


-- | Code to shift the stack pointer by n words.
moveSp :: Int -> Instr
moveSp n
   = ADD False False sp (RIImm (ImmInt (n * wordLength))) sp

-- | An instruction that will cause the one after it never to be exectuted
isUnconditionalJump :: Instr -> Bool
isUnconditionalJump ii
 = case ii of
 	CALL{}		-> True
	JMP{}		-> True
	JMP_TBL{}	-> True
	BI ALWAYS _ _	-> True
	BF ALWAYS _ _ 	-> True
	_		-> False


-- | instance for sparc instruction set
instance Instruction Instr where
	regUsageOfInstr		= sparc_regUsageOfInstr
	patchRegsOfInstr	= sparc_patchRegsOfInstr
	isJumpishInstr		= sparc_isJumpishInstr
	jumpDestsOfInstr	= sparc_jumpDestsOfInstr
	patchJumpInstr		= sparc_patchJumpInstr
	mkSpillInstr		= sparc_mkSpillInstr
	mkLoadInstr		= sparc_mkLoadInstr
	takeDeltaInstr		= sparc_takeDeltaInstr
	isMetaInstr		= sparc_isMetaInstr
	mkRegRegMoveInstr	= sparc_mkRegRegMoveInstr
	takeRegRegMoveInstr	= sparc_takeRegRegMoveInstr
	mkJumpInstr		= sparc_mkJumpInstr
        mkStackAllocInstr       = panic "no sparc_mkStackAllocInstr"
        mkStackDeallocInstr     = panic "no sparc_mkStackDeallocInstr"


-- | SPARC instruction set.
--	Not complete. This is only the ones we need.
--
data Instr

	-- meta ops --------------------------------------------------
	-- comment pseudo-op
	= COMMENT FastString		

	-- some static data spat out during code generation.
	-- Will be extracted before pretty-printing.
	| LDATA   Section CmmStatics	

	-- Start a new basic block.  Useful during codegen, removed later.
	-- Preceding instruction should be a jump, as per the invariants
	-- for a BasicBlock (see Cmm).
	| NEWBLOCK BlockId		

	-- specify current stack offset for benefit of subsequent passes.
	| DELTA   Int

	-- real instrs -----------------------------------------------
	-- Loads and stores.
	| LD		Size AddrMode Reg 		-- size, src, dst
	| ST		Size Reg AddrMode 		-- size, src, dst

	-- Int Arithmetic.
	--	x:   add/sub with carry bit. 
	--		In SPARC V9 addx and friends were renamed addc. 
	--
	--	cc:  modify condition codes
	-- 
	| ADD		Bool Bool Reg RI Reg 		-- x?, cc?, src1, src2, dst
	| SUB		Bool Bool Reg RI Reg 		-- x?, cc?, src1, src2, dst

	| UMUL		Bool Reg RI Reg 		--     cc?, src1, src2, dst
	| SMUL	        Bool Reg RI Reg 		--     cc?, src1, src2, dst


	-- The SPARC divide instructions perform 64bit by 32bit division
	--   The Y register is xored into the first operand.

	--   On _some implementations_ the Y register is overwritten by
	--   the remainder, so we have to make sure it is 0 each time.

	--   dst <- ((Y `shiftL` 32) `or` src1) `div` src2
	| UDIV          Bool Reg RI Reg 		--     cc?, src1, src2, dst
	| SDIV          Bool Reg RI Reg 		--     cc?, src1, src2, dst

 	| RDY           Reg                  		-- move contents of Y register to reg
	| WRY           Reg  Reg             		-- Y <- src1 `xor` src2
	
	-- Logic operations.
	| AND		Bool Reg RI Reg 		-- cc?, src1, src2, dst
	| ANDN		Bool Reg RI Reg 		-- cc?, src1, src2, dst
	| OR		Bool Reg RI Reg 		-- cc?, src1, src2, dst
	| ORN		Bool Reg RI Reg 		-- cc?, src1, src2, dst
	| XOR		Bool Reg RI Reg 		-- cc?, src1, src2, dst
	| XNOR		Bool Reg RI Reg 		-- cc?, src1, src2, dst
	| SLL		Reg RI Reg 			-- src1, src2, dst
	| SRL		Reg RI Reg 			-- src1, src2, dst
	| SRA		Reg RI Reg 			-- src1, src2, dst

	-- Load immediates.
	| SETHI		Imm Reg 			-- src, dst

	-- Do nothing.
	-- Implemented by the assembler as SETHI 0, %g0, but worth an alias
	| NOP						

	-- Float Arithmetic.
	-- Note that we cheat by treating F{ABS,MOV,NEG} of doubles as single
	-- instructions right up until we spit them out.
	--
	| FABS		Size Reg Reg	   		-- src dst
	| FADD		Size Reg Reg Reg  		-- src1, src2, dst
	| FCMP		Bool Size Reg Reg 		-- exception?, src1, src2, dst
	| FDIV		Size Reg Reg Reg 		-- src1, src2, dst
	| FMOV		Size Reg Reg     		-- src, dst
	| FMUL		Size Reg Reg Reg 		-- src1, src2, dst
 	| FNEG		Size Reg Reg     		-- src, dst
	| FSQRT		Size Reg Reg     		-- src, dst
	| FSUB		Size Reg Reg Reg 		-- src1, src2, dst
	| FxTOy		Size Size Reg Reg 		-- src, dst

	-- Jumping around.
	| BI		Cond Bool BlockId 		-- cond, annul?, target
	| BF		Cond Bool BlockId 		-- cond, annul?, target

	| JMP		AddrMode     			-- target

	-- With a tabled jump we know all the possible destinations.
	-- We also need this info so we can work out what regs are live across the jump.
	-- 
	| JMP_TBL	AddrMode [Maybe BlockId] CLabel

	| CALL		(Either Imm Reg) Int Bool 	-- target, args, terminal


-- | regUsage returns the sets of src and destination registers used
-- 	by a particular instruction.  Machine registers that are
-- 	pre-allocated to stgRegs are filtered out, because they are
-- 	uninteresting from a register allocation standpoint.  (We wouldn't
-- 	want them to end up on the free list!)  As far as we are concerned,
-- 	the fixed registers simply don't exist (for allocation purposes,
-- 	anyway).

-- 	regUsage doesn't need to do any trickery for jumps and such.  Just
-- 	state precisely the regs read and written by that insn.  The
-- 	consequences of control flow transfers, as far as register
-- 	allocation goes, are taken care of by the register allocator.
--
sparc_regUsageOfInstr :: Platform -> Instr -> RegUsage
sparc_regUsageOfInstr platform instr
 = case instr of
    LD    _ addr reg  		-> usage (regAddr addr, 	[reg])
    ST    _ reg addr  		-> usage (reg : regAddr addr, 	[])
    ADD   _ _ r1 ar r2		-> usage (r1 : regRI ar, 	[r2])
    SUB   _ _ r1 ar r2		-> usage (r1 : regRI ar, 	[r2])
    UMUL    _ r1 ar r2		-> usage (r1 : regRI ar, 	[r2])
    SMUL    _ r1 ar r2		-> usage (r1 : regRI ar, 	[r2])
    UDIV    _ r1 ar r2		-> usage (r1 : regRI ar, 	[r2])
    SDIV    _ r1 ar r2		-> usage (r1 : regRI ar, 	[r2])
    RDY       rd           	-> usage ([], 		 	[rd])
    WRY       r1 r2        	-> usage ([r1, r2], 	 	[])
    AND     _ r1 ar r2  	-> usage (r1 : regRI ar, 	[r2])
    ANDN    _ r1 ar r2 		-> usage (r1 : regRI ar, 	[r2])
    OR      _ r1 ar r2   	-> usage (r1 : regRI ar, 	[r2])
    ORN     _ r1 ar r2  	-> usage (r1 : regRI ar, 	[r2])
    XOR     _ r1 ar r2  	-> usage (r1 : regRI ar, 	[r2])
    XNOR    _ r1 ar r2 		-> usage (r1 : regRI ar, 	[r2])
    SLL       r1 ar r2    	-> usage (r1 : regRI ar, 	[r2])
    SRL       r1 ar r2    	-> usage (r1 : regRI ar,	[r2])
    SRA       r1 ar r2    	-> usage (r1 : regRI ar, 	[r2])
    SETHI   _ reg   		-> usage ([], 			[reg])
    FABS    _ r1 r2    		-> usage ([r1], 		[r2])
    FADD    _ r1 r2 r3 		-> usage ([r1, r2],		[r3])
    FCMP    _ _  r1 r2  	-> usage ([r1, r2], 		[])
    FDIV    _ r1 r2 r3 		-> usage ([r1, r2], 		[r3])
    FMOV    _ r1 r2    		-> usage ([r1],			[r2])
    FMUL    _ r1 r2 r3 		-> usage ([r1, r2],	 	[r3])
    FNEG    _ r1 r2    		-> usage ([r1], 		[r2])
    FSQRT   _ r1 r2   		-> usage ([r1], 		[r2])
    FSUB    _ r1 r2 r3 		-> usage ([r1, r2], 		[r3])
    FxTOy   _ _  r1 r2 		-> usage ([r1], 		[r2])

    JMP     addr 		-> usage (regAddr addr, [])
    JMP_TBL addr _ _    	-> usage (regAddr addr, [])

    CALL  (Left _  )  _ True  	-> noUsage
    CALL  (Left _  )  n False 	-> usage (argRegs n, callClobberedRegs)
    CALL  (Right reg) _ True  	-> usage ([reg], [])
    CALL  (Right reg) n False 	-> usage (reg : (argRegs n), callClobberedRegs)
    _ 	    	    		-> noUsage

  where
    usage (src, dst) 
     = RU (filter (interesting platform) src)
          (filter (interesting platform) dst)

    regAddr (AddrRegReg r1 r2)	= [r1, r2]
    regAddr (AddrRegImm r1 _)	= [r1]

    regRI (RIReg r)		= [r]
    regRI  _			= []


-- | Interesting regs are virtuals, or ones that are allocatable 
--	by the register allocator.
interesting :: Platform -> Reg -> Bool
interesting platform reg
 = case reg of
	RegVirtual _			-> True
	RegReal (RealRegSingle r1)	-> isFastTrue (freeReg platform r1)
	RegReal (RealRegPair r1 _)	-> isFastTrue (freeReg platform r1)



-- | Apply a given mapping to tall the register references in this instruction.
sparc_patchRegsOfInstr :: Instr -> (Reg -> Reg) -> Instr
sparc_patchRegsOfInstr instr env = case instr of
    LD    sz addr reg   	-> LD sz (fixAddr addr) (env reg)
    ST    sz reg addr   	-> ST sz (env reg) (fixAddr addr)

    ADD   x cc r1 ar r2 	-> ADD   x cc  (env r1) (fixRI ar) (env r2)
    SUB   x cc r1 ar r2	 	-> SUB   x cc  (env r1) (fixRI ar) (env r2)
    UMUL    cc r1 ar r2		-> UMUL    cc  (env r1) (fixRI ar) (env r2)
    SMUL    cc r1 ar r2		-> SMUL    cc  (env r1) (fixRI ar) (env r2)
    UDIV    cc r1 ar r2		-> UDIV    cc  (env r1) (fixRI ar) (env r2)
    SDIV    cc r1 ar r2		-> SDIV    cc  (env r1) (fixRI ar) (env r2)
    RDY   rd            	-> RDY         (env rd)
    WRY   r1 r2			-> WRY         (env r1) (env r2)
    AND   b r1 ar r2   	 	-> AND   b     (env r1) (fixRI ar) (env r2)
    ANDN  b r1 ar r2    	-> ANDN  b     (env r1) (fixRI ar) (env r2)
    OR    b r1 ar r2   		-> OR    b     (env r1) (fixRI ar) (env r2)
    ORN   b r1 ar r2    	-> ORN   b     (env r1) (fixRI ar) (env r2)
    XOR   b r1 ar r2    	-> XOR   b     (env r1) (fixRI ar) (env r2)
    XNOR  b r1 ar r2    	-> XNOR  b     (env r1) (fixRI ar) (env r2)
    SLL   r1 ar r2      	-> SLL         (env r1) (fixRI ar) (env r2)
    SRL   r1 ar r2      	-> SRL         (env r1) (fixRI ar) (env r2)
    SRA   r1 ar r2      	-> SRA         (env r1) (fixRI ar) (env r2)

    SETHI imm reg       	-> SETHI imm (env reg)

    FABS  s r1 r2       	-> FABS    s   (env r1) (env r2)
    FADD  s r1 r2 r3    	-> FADD    s   (env r1) (env r2) (env r3)
    FCMP  e s r1 r2     	-> FCMP e  s   (env r1) (env r2)
    FDIV  s r1 r2 r3    	-> FDIV    s   (env r1) (env r2) (env r3)
    FMOV  s r1 r2       	-> FMOV    s   (env r1) (env r2)
    FMUL  s r1 r2 r3    	-> FMUL    s   (env r1) (env r2) (env r3)
    FNEG  s r1 r2       	-> FNEG    s   (env r1) (env r2)
    FSQRT s r1 r2       	-> FSQRT   s   (env r1) (env r2)
    FSUB  s r1 r2 r3   	 	-> FSUB    s   (env r1) (env r2) (env r3)
    FxTOy s1 s2 r1 r2   	-> FxTOy s1 s2 (env r1) (env r2)

    JMP     addr        	-> JMP     (fixAddr addr)
    JMP_TBL addr ids l  	-> JMP_TBL (fixAddr addr) ids l

    CALL  (Left i) n t  	-> CALL (Left i) n t
    CALL  (Right r) n t 	-> CALL (Right (env r)) n t
    _ 				-> instr

  where
    fixAddr (AddrRegReg r1 r2) 	= AddrRegReg   (env r1) (env r2)
    fixAddr (AddrRegImm r1 i)  	= AddrRegImm   (env r1) i

    fixRI (RIReg r) 		= RIReg (env r)
    fixRI other			= other


--------------------------------------------------------------------------------
sparc_isJumpishInstr :: Instr -> Bool
sparc_isJumpishInstr instr
 = case instr of
	BI{}		-> True
	BF{}		-> True
	JMP{}		-> True
	JMP_TBL{}	-> True
	CALL{}		-> True
	_		-> False

sparc_jumpDestsOfInstr :: Instr -> [BlockId]
sparc_jumpDestsOfInstr insn
  = case insn of
	BI   _ _ id	-> [id]
	BF   _ _ id	-> [id]
	JMP_TBL _ ids _	-> [id | Just id <- ids]
	_		-> []


sparc_patchJumpInstr :: Instr -> (BlockId -> BlockId) -> Instr
sparc_patchJumpInstr insn patchF
  = case insn of
	BI cc annul id	-> BI cc annul (patchF id)
	BF cc annul id	-> BF cc annul (patchF id)
	JMP_TBL n ids l	-> JMP_TBL n (map (fmap patchF) ids) l
	_		-> insn


--------------------------------------------------------------------------------
-- | Make a spill instruction.
-- 	On SPARC we spill below frame pointer leaving 2 words/spill
sparc_mkSpillInstr
    :: DynFlags
    -> Reg      -- ^ register to spill
    -> Int      -- ^ current stack delta
    -> Int      -- ^ spill slot to use
    -> Instr

sparc_mkSpillInstr dflags reg _ slot
 = let  platform = targetPlatform dflags
        off      = spillSlotToOffset dflags slot
        off_w    = 1 + (off `div` 4)
        sz 	= case targetClassOfReg platform reg of
			RcInteger -> II32
			RcFloat   -> FF32
			RcDouble  -> FF64
			_         -> panic "sparc_mkSpillInstr"
		
    in ST sz reg (fpRel (negate off_w))


-- | Make a spill reload instruction.
sparc_mkLoadInstr
    :: DynFlags
    -> Reg      -- ^ register to load into
    -> Int      -- ^ current stack delta
    -> Int      -- ^ spill slot to use
    -> Instr

sparc_mkLoadInstr dflags reg _ slot
  = let platform = targetPlatform dflags
        off      = spillSlotToOffset dflags slot
	off_w	= 1 + (off `div` 4)
        sz	= case targetClassOfReg platform reg of
			RcInteger -> II32
			RcFloat   -> FF32
			RcDouble  -> FF64
			_         -> panic "sparc_mkLoadInstr"

        in LD sz (fpRel (- off_w)) reg


--------------------------------------------------------------------------------
-- | See if this instruction is telling us the current C stack delta
sparc_takeDeltaInstr
	:: Instr
	-> Maybe Int
	
sparc_takeDeltaInstr instr
 = case instr of
 	DELTA i		-> Just i
	_		-> Nothing


sparc_isMetaInstr
	:: Instr
	-> Bool
	
sparc_isMetaInstr instr
 = case instr of
 	COMMENT{}	-> True
	LDATA{}		-> True
	NEWBLOCK{}	-> True
	DELTA{}		-> True
	_		-> False
	

-- | Make a reg-reg move instruction.
--	On SPARC v8 there are no instructions to move directly between
--	floating point and integer regs. If we need to do that then we
--	have to go via memory.
--
sparc_mkRegRegMoveInstr
    :: Platform
    -> Reg
    -> Reg
    -> Instr

sparc_mkRegRegMoveInstr platform src dst
	| srcClass	<- targetClassOfReg platform src
	, dstClass	<- targetClassOfReg platform dst
	, srcClass == dstClass
	= case srcClass of
		RcInteger -> ADD  False False src (RIReg g0) dst
		RcDouble  -> FMOV FF64 src dst
		RcFloat   -> FMOV FF32 src dst
                _         -> panic "sparc_mkRegRegMoveInstr"
	
	| otherwise
	= panic "SPARC.Instr.mkRegRegMoveInstr: classes of src and dest not the same"


-- | Check whether an instruction represents a reg-reg move.
-- 	The register allocator attempts to eliminate reg->reg moves whenever it can,
-- 	by assigning the src and dest temporaries to the same real register.
--
sparc_takeRegRegMoveInstr :: Instr -> Maybe (Reg,Reg)
sparc_takeRegRegMoveInstr instr
 = case instr of
 	ADD False False src (RIReg src2) dst
	 | g0 == src2		-> Just (src, dst)

	FMOV FF64 src dst	-> Just (src, dst)
	FMOV FF32  src dst	-> Just (src, dst)
	_			-> Nothing


-- | Make an unconditional branch instruction.
sparc_mkJumpInstr
	:: BlockId
	-> [Instr]

sparc_mkJumpInstr id 
 = 	 [BI ALWAYS False id
	, NOP]			-- fill the branch delay slot.