[project @ 2000-01-28 09:40:05 by sewardj]
[ghc-hetmet.git] / ghc / compiler / nativeGen / StixPrim.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4
5 \begin{code}
6 module StixPrim ( primCode, amodeToStix, amodeToStix' ) where
7
8 #include "HsVersions.h"
9
10 import MachMisc
11 import MachRegs
12 import Stix
13 import StixInteger
14
15 import AbsCSyn          hiding ( spRel )
16 import AbsCUtils        ( getAmodeRep, mixedTypeLocn )
17 import Constants        ( uF_UPDATEE )
18 import SMRep            ( fixedHdrSize )
19 import Const            ( Literal(..) )
20 import CallConv         ( cCallConv )
21 import PrimOp           ( PrimOp(..) )
22 import PrimRep          ( PrimRep(..), isFloatingRep )
23 import UniqSupply       ( returnUs, thenUs, UniqSM )
24 import Constants        ( mIN_INTLIKE )
25 import Outputable
26
27 import Char             ( ord )
28 \end{code}
29
30 The main honcho here is primCode, which handles the guts of COpStmts.
31
32 \begin{code}
33 primCode
34     :: [CAddrMode]      -- results
35     -> PrimOp           -- op
36     -> [CAddrMode]      -- args
37     -> UniqSM StixTreeList
38 \end{code}
39
40 First, the dreaded @ccall@.  We can't handle @casm@s.
41
42 Usually, this compiles to an assignment, but when the left-hand side
43 is empty, we just perform the call and ignore the result.
44
45 btw Why not let programmer use casm to provide assembly code instead
46 of C code?  ADR
47
48 The (MP) integer operations are a true nightmare.  Since we don't have
49 a convenient abstract way of allocating temporary variables on the (C)
50 stack, we use the space just below HpLim for the @MP_INT@ structures,
51 and modify our heap check accordingly.
52
53 \begin{code}
54 -- NB: ordering of clauses somewhere driven by
55 -- the desire to getting sane patt-matching behavior
56 primCode res@[sr,dr] IntegerNegOp arg@[sa,da]
57   = gmpNegate (sr,dr) (sa,da)
58
59 primCode [res] IntegerCmpOp args@[sa1,da1, sa2,da2]
60   = gmpCompare res (sa1,da1, sa2,da2)
61
62 primCode [res] IntegerCmpIntOp args@[sa1,da1,ai]
63   = gmpCompareInt res (sa1,da1,ai)
64
65 primCode [res] Integer2IntOp arg@[sa,da]
66   = gmpInteger2Int res (sa,da)
67
68 primCode [res] Integer2WordOp arg@[sa,da]
69   = gmpInteger2Word res (sa,da)
70
71 primCode [res] Int2AddrOp [arg]
72   = simpleCoercion AddrRep res arg
73
74 primCode [res] Addr2IntOp [arg]
75   = simpleCoercion IntRep res arg
76
77 primCode [res] Int2WordOp [arg]
78   = simpleCoercion IntRep{-WordRep?-} res arg
79
80 primCode [res] Word2IntOp [arg]
81   = simpleCoercion IntRep res arg
82 \end{code}
83
84 \begin{code}
85 primCode [res] SameMutableArrayOp args
86   = let
87         compare = StPrim AddrEqOp (map amodeToStix args)
88         assign = StAssign IntRep (amodeToStix res) compare
89     in
90     returnUs (\xs -> assign : xs)
91
92 primCode res@[_] SameMutableByteArrayOp args
93   = primCode res SameMutableArrayOp args
94 \end{code}
95
96 Freezing an array of pointers is a double assignment.  We fix the
97 header of the ``new'' closure because the lhs is probably a better
98 addressing mode for the indirection (most likely, it's a VanillaReg).
99
100 \begin{code}
101
102 primCode [lhs] UnsafeFreezeArrayOp [rhs]
103   = let
104         lhs' = amodeToStix lhs
105         rhs' = amodeToStix rhs
106         header = StInd PtrRep lhs'
107         assign = StAssign PtrRep lhs' rhs'
108         freeze = StAssign PtrRep header mutArrPtrsFrozen_info
109     in
110     returnUs (\xs -> assign : freeze : xs)
111
112 primCode [lhs] UnsafeFreezeByteArrayOp [rhs]
113   = simpleCoercion PtrRep lhs rhs
114 primCode [lhs] UnsafeThawByteArrayOp [rhs]
115   = simpleCoercion PtrRep lhs rhs
116 \end{code}
117
118 Returning the size of (mutable) byte arrays is just
119 an indexing operation.
120
121 \begin{code}
122 primCode [lhs] SizeofByteArrayOp [rhs]
123   = let
124         lhs' = amodeToStix lhs
125         rhs' = amodeToStix rhs
126         sz   = StIndex IntRep rhs' fixedHS
127         assign = StAssign IntRep lhs' (StInd IntRep sz)
128     in
129     returnUs (\xs -> assign : xs)
130
131 primCode [lhs] SizeofMutableByteArrayOp [rhs]
132   = let
133         lhs' = amodeToStix lhs
134         rhs' = amodeToStix rhs
135         sz   = StIndex IntRep rhs' fixedHS
136         assign = StAssign IntRep lhs' (StInd IntRep sz)
137     in
138     returnUs (\xs -> assign : xs)
139
140 \end{code}
141
142 Most other array primitives translate to simple indexing.
143
144 \begin{code}
145 primCode lhs@[_] IndexArrayOp args
146   = primCode lhs ReadArrayOp args
147
148 primCode [lhs] ReadArrayOp [obj, ix]
149   = let
150         lhs' = amodeToStix lhs
151         obj' = amodeToStix obj
152         ix' = amodeToStix ix
153         base = StIndex IntRep obj' arrPtrsHS
154         assign = StAssign PtrRep lhs' (StInd PtrRep (StIndex PtrRep base ix'))
155     in
156     returnUs (\xs -> assign : xs)
157
158 primCode [] WriteArrayOp [obj, ix, v]
159   = let
160         obj' = amodeToStix obj
161         ix' = amodeToStix ix
162         v' = amodeToStix v
163         base = StIndex IntRep obj' arrPtrsHS
164         assign = StAssign PtrRep (StInd PtrRep (StIndex PtrRep base ix')) v'
165     in
166     returnUs (\xs -> assign : xs)
167
168 primCode lhs@[_] (IndexByteArrayOp pk) args
169   = primCode lhs (ReadByteArrayOp pk) args
170
171 -- NB: indexing in "pk" units, *not* in bytes (WDP 95/09)
172
173 primCode [lhs] (ReadByteArrayOp pk) [obj, ix]
174   = let
175         lhs' = amodeToStix lhs
176         obj' = amodeToStix obj
177         ix' = amodeToStix ix
178         base = StIndex IntRep obj' arrWordsHS
179         assign = StAssign pk lhs' (StInd pk (StIndex pk base ix'))
180     in
181     returnUs (\xs -> assign : xs)
182
183 primCode [lhs] (IndexOffAddrOp pk) [obj, ix]
184   = let
185         lhs' = amodeToStix lhs
186         obj' = amodeToStix obj
187         ix' = amodeToStix ix
188         assign = StAssign pk lhs' (StInd pk (StIndex pk obj' ix'))
189     in
190     returnUs (\xs -> assign : xs)
191
192 primCode [lhs] (IndexOffForeignObjOp pk) [obj, ix]
193   = let
194         lhs' = amodeToStix lhs
195         obj' = amodeToStix obj
196         ix' = amodeToStix ix
197         obj'' = StIndex PtrRep obj' fixedHS
198         assign = StAssign pk lhs' (StInd pk (StIndex pk obj'' ix'))
199     in
200     returnUs (\xs -> assign : xs)
201
202 primCode [] (WriteByteArrayOp pk) [obj, ix, v]
203   = let
204         obj' = amodeToStix obj
205         ix' = amodeToStix ix
206         v' = amodeToStix v
207         base = StIndex IntRep obj' arrWordsHS
208         assign = StAssign pk (StInd pk (StIndex pk base ix')) v'
209     in
210     returnUs (\xs -> assign : xs)
211 \end{code}
212
213 \begin{code}
214 --primCode lhs (CCallOp fn is_asm may_gc) rhs
215 primCode lhs (CCallOp (Left fn) is_asm may_gc cconv) rhs
216   | is_asm = error "ERROR: Native code generator can't handle casm"
217   | may_gc = error "ERROR: Native code generator can't handle _ccall_GC_\n"
218   | otherwise
219   = case lhs of
220       [] -> returnUs (\xs -> (StCall fn cconv VoidRep args) : xs)
221       [lhs] ->
222           let lhs' = amodeToStix lhs
223               pk = if isFloatingRep (getAmodeRep lhs) then DoubleRep else IntRep
224               call = StAssign pk lhs' (StCall fn cconv pk args)
225           in
226               returnUs (\xs -> call : xs)
227   where
228     args = map amodeCodeForCCall rhs
229     amodeCodeForCCall x =
230         let base = amodeToStix' x
231         in
232             case getAmodeRep x of
233               ArrayRep      -> StIndex PtrRep base arrPtrsHS
234               ByteArrayRep  -> StIndex IntRep base arrWordsHS
235               ForeignObjRep -> StIndex PtrRep base fixedHS
236               _ -> base
237 \end{code}
238
239 DataToTagOp won't work for 64-bit archs, as it is.
240
241 \begin{code}
242 primCode [lhs] DataToTagOp [arg]
243   = let lhs'        = amodeToStix lhs
244         arg'        = amodeToStix arg
245         infoptr     = StInd PtrRep arg'
246         word_32     = StInd WordRep (StIndex PtrRep infoptr (StInt (-1)))
247         masked_le32 = StPrim SrlOp [word_32, StInt 16]
248         masked_be32 = StPrim AndOp [word_32, StInt 65535]
249 #ifdef WORDS_BIGENDIAN
250         masked      = masked_be32
251 #else
252         masked      = masked_le32
253 #endif
254         assign      = StAssign IntRep lhs' masked
255     in
256     returnUs (\xs -> assign : xs)
257 \end{code}
258
259 Now the more mundane operations.
260
261 \begin{code}
262 primCode lhs op rhs
263   = let
264         lhs' = map amodeToStix  lhs
265         rhs' = map amodeToStix' rhs
266         pk   = getAmodeRep (head lhs)
267     in
268     returnUs (\ xs -> simplePrim pk lhs' op rhs' : xs)
269 \end{code}
270
271 \begin{code}
272 simpleCoercion
273       :: PrimRep
274       -> CAddrMode
275       -> CAddrMode
276       -> UniqSM StixTreeList
277
278 simpleCoercion pk lhs rhs
279   = returnUs (\xs -> StAssign pk (amodeToStix lhs) (amodeToStix rhs) : xs)
280 \end{code}
281
282 Here we try to rewrite primitives into a form the code generator can
283 understand.  Any primitives not handled here must be handled at the
284 level of the specific code generator.
285
286 \begin{code}
287 simplePrim
288     :: PrimRep          -- Rep of first destination
289     -> [StixTree]       -- Destinations
290     -> PrimOp
291     -> [StixTree]
292     -> StixTree
293 \end{code}
294
295 Now look for something more conventional.
296
297 \begin{code}
298 simplePrim pk [lhs] op rest  = StAssign pk lhs (StPrim op rest)
299 simplePrim pk as    op bs    = simplePrim_error op
300
301 simplePrim_error op
302     = error ("ERROR: primitive operation `"++show op++"'cannot be handled\nby the native-code generator.  Workaround: use -fvia-C.\n(Perhaps you should report it as a GHC bug, also.)\n")
303 \end{code}
304
305 %---------------------------------------------------------------------
306
307 Here we generate the Stix code for CAddrModes.
308
309 When a character is fetched from a mixed type location, we have to do
310 an extra cast.  This is reflected in amodeCode', which is for rhs
311 amodes that might possibly need the extra cast.
312
313 \begin{code}
314 amodeToStix, amodeToStix' :: CAddrMode -> StixTree
315
316 amodeToStix'{-'-} am@(CVal rr CharRep)
317     | mixedTypeLocn am = StPrim ChrOp [amodeToStix am]
318     | otherwise = amodeToStix am
319
320 amodeToStix' am = amodeToStix am
321
322 -----------
323 amodeToStix am@(CVal rr CharRep)
324   | mixedTypeLocn am
325   = StInd IntRep (amodeToStix (CAddr rr))
326
327 amodeToStix (CVal rr pk) = StInd pk (amodeToStix (CAddr rr))
328
329 amodeToStix (CAddr (SpRel off))
330   = StIndex PtrRep stgSp (StInt (toInteger IBOX(off)))
331
332 amodeToStix (CAddr (HpRel off))
333   = StIndex IntRep stgHp (StInt (toInteger (- IBOX(off))))
334
335 amodeToStix (CAddr (NodeRel off))
336   = StIndex IntRep stgNode (StInt (toInteger IBOX(off)))
337
338 amodeToStix (CAddr (CIndex base off pk))
339   = StIndex pk (amodeToStix base) (amodeToStix off)
340
341 amodeToStix (CReg magic)    = StReg (StixMagicId magic)
342 amodeToStix (CTemp uniq pk) = StReg (StixTemp uniq pk)
343
344 amodeToStix (CLbl      lbl _) = StCLbl lbl
345
346  -- For CharLike and IntLike, we attempt some trivial constant-folding here.
347
348 amodeToStix (CCharLike (CLit (MachChar c)))
349   = StLitLbl ((<>) (ptext SLIT("CHARLIKE_closure+")) (int off))
350   where
351     off = charLikeSize * ord c
352
353 amodeToStix (CCharLike x)
354   = StIndex CharRep charLike off
355   where
356     off = StPrim IntMulOp [amodeToStix x, StInt (toInteger charLikeSize)]
357
358 amodeToStix (CIntLike (CLit (MachInt i _)))
359   = StLitLbl ((<>) (ptext SLIT("INTLIKE_closure+")) (int off))
360   where
361     off = intLikeSize * (fromInteger (i - mIN_INTLIKE))
362
363 amodeToStix (CIntLike x)
364   = panic "CIntLike"
365
366 amodeToStix (CLit core)
367   = case core of
368       MachChar c     -> StInt (toInteger (ord c))
369       MachStr s      -> StString s
370       MachAddr a     -> StInt a
371       MachInt i _    -> StInt (toInteger i)
372       MachLitLit s _ -> {-trace (_UNPK_ s ++ "\n")-} (litLitToStix (_UNPK_ s))
373       MachFloat d    -> StDouble d
374       MachDouble d   -> StDouble d
375       _ -> panic "amodeToStix:core literal"
376
377 amodeToStix (CLitLit s _)
378    = litLitToStix (_UNPK_ s)
379
380 amodeToStix (CMacroExpr _ macro [arg])
381   = case macro of
382       ENTRY_CODE -> amodeToStix arg
383       ARG_TAG    -> amodeToStix arg -- just an integer no. of words
384       GET_TAG    -> 
385 #ifdef WORDS_BIGENDIAN
386                     StPrim AndOp 
387                         [StInd WordRep (StIndex PtrRep (amodeToStix arg)
388                                                 (StInt (toInteger (-1)))),
389                          StInt 65535]
390 #else
391                     StPrim SrlOp 
392                         [StInd WordRep (StIndex PtrRep (amodeToStix arg)
393                                                 (StInt (toInteger (-1)))),
394                          StInt 16]
395 #endif
396       UPD_FRAME_UPDATEE
397          -> StInd PtrRep (StIndex PtrRep (amodeToStix arg) 
398                                          (StInt (toInteger uF_UPDATEE)))
399 -- XXX!!!
400 -- GET_TAG(info_ptr) is supposed to be  get_itbl(info_ptr)->srt_len,
401 -- which we've had to hand-code here.
402
403 litLitToStix :: String -> StixTree
404 litLitToStix nm
405    = case nm of
406         "stdout" -> stixFor_stdout
407         "stderr" -> stixFor_stderr
408         "stdin"  -> stixFor_stdin
409         other    -> error ("\nlitLitToStix: can't handle `" ++ nm ++ "'\n" 
410                            ++ "suggested workaround: use flag -fvia-C\n")
411 \end{code}
412
413 Sizes of the CharLike and IntLike closures that are arranged as arrays
414 in the data segment.  (These are in bytes.)
415
416 \begin{code}
417 -- The INTLIKE base pointer
418
419 intLikePtr :: StixTree
420
421 intLikePtr = StInd PtrRep (sStLitLbl SLIT("INTLIKE_closure"))
422
423 -- The CHARLIKE base
424
425 charLike :: StixTree
426
427 charLike = sStLitLbl SLIT("CHARLIKE_closure")
428
429 -- Trees for the ErrorIOPrimOp
430
431 topClosure, errorIO :: StixTree
432
433 topClosure = StInd PtrRep (sStLitLbl SLIT("TopClosure"))
434 errorIO = StJump (StInd PtrRep (sStLitLbl SLIT("ErrorIO_innards")))
435
436 mutArrPtrsFrozen_info = sStLitLbl SLIT("MUT_ARR_PTRS_FROZEN_info")
437
438 charLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
439 intLikeSize  = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
440 \end{code}