8fe590f1e94abb12027ec4cbbe2ff3b2c9ae44bb
[ghc.git] / compiler / nativeGen / Size.hs
1 -- | Sizes on this architecture
2 -- A Size is a combination of width and class
3 --
4 -- TODO: Rename this to "Format" instead of "Size" to reflect
5 -- the fact that it represents floating point vs integer.
6 --
7 -- TODO: Signed vs unsigned?
8 --
9 -- TODO: This module is currenly shared by all architectures because
10 -- NCGMonad need to know about it to make a VReg. It would be better
11 -- to have architecture specific formats, and do the overloading
12 -- properly. eg SPARC doesn't care about FF80.
13 --
14 module Size (
15 Size(..),
16 intSize,
17 floatSize,
18 isFloatSize,
19 cmmTypeSize,
20 sizeToWidth,
21 sizeInBytes
22 )
23
24 where
25
26 import Cmm
27 import Outputable
28
29 -- It looks very like the old MachRep, but it's now of purely local
30 -- significance, here in the native code generator. You can change it
31 -- without global consequences.
32 --
33 -- A major use is as an opcode qualifier; thus the opcode
34 -- mov.l a b
35 -- might be encoded
36 -- MOV II32 a b
37 -- where the Size field encodes the ".l" part.
38
39 -- ToDo: it's not clear to me that we need separate signed-vs-unsigned sizes
40 -- here. I've removed them from the x86 version, we'll see what happens --SDM
41
42 -- ToDo: quite a few occurrences of Size could usefully be replaced by Width
43
44 data Size
45 = II8
46 | II16
47 | II32
48 | II64
49 | FF32
50 | FF64
51 | FF80
52 deriving (Show, Eq)
53
54
55 -- | Get the integer size of this width.
56 intSize :: Width -> Size
57 intSize width
58 = case width of
59 W8 -> II8
60 W16 -> II16
61 W32 -> II32
62 W64 -> II64
63 other -> pprPanic "Size.intSize" (ppr other)
64
65
66 -- | Get the float size of this width.
67 floatSize :: Width -> Size
68 floatSize width
69 = case width of
70 W32 -> FF32
71 W64 -> FF64
72 other -> pprPanic "Size.floatSize" (ppr other)
73
74
75 -- | Check if a size represents a floating point value.
76 isFloatSize :: Size -> Bool
77 isFloatSize size
78 = case size of
79 FF32 -> True
80 FF64 -> True
81 FF80 -> True
82 _ -> False
83
84
85 -- | Convert a Cmm type to a Size.
86 cmmTypeSize :: CmmType -> Size
87 cmmTypeSize ty
88 | isFloatType ty = floatSize (typeWidth ty)
89 | otherwise = intSize (typeWidth ty)
90
91
92 -- | Get the Width of a Size.
93 sizeToWidth :: Size -> Width
94 sizeToWidth size
95 = case size of
96 II8 -> W8
97 II16 -> W16
98 II32 -> W32
99 II64 -> W64
100 FF32 -> W32
101 FF64 -> W64
102 FF80 -> W80
103
104 sizeInBytes :: Size -> Int
105 sizeInBytes = widthInBytes . sizeToWidth