f32bb99037990cd3de85a5c5a60673041589511a
[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 Literal          ( Literal(..), word2IntLit )
20 import CallConv         ( cCallConv )
21 import PrimOp           ( PrimOp(..), CCall(..), CCallTarget(..) )
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 \end{code}
115
116 Returning the size of (mutable) byte arrays is just
117 an indexing operation.
118
119 \begin{code}
120 primCode [lhs] SizeofByteArrayOp [rhs]
121   = let
122         lhs' = amodeToStix lhs
123         rhs' = amodeToStix rhs
124         sz   = StIndex IntRep rhs' fixedHS
125         assign = StAssign IntRep lhs' (StInd IntRep sz)
126     in
127     returnUs (\xs -> assign : xs)
128
129 primCode [lhs] SizeofMutableByteArrayOp [rhs]
130   = let
131         lhs' = amodeToStix lhs
132         rhs' = amodeToStix rhs
133         sz   = StIndex IntRep rhs' fixedHS
134         assign = StAssign IntRep lhs' (StInd IntRep sz)
135     in
136     returnUs (\xs -> assign : xs)
137
138 \end{code}
139
140 Most other array primitives translate to simple indexing.
141
142 \begin{code}
143 primCode lhs@[_] IndexArrayOp args
144   = primCode lhs ReadArrayOp args
145
146 primCode [lhs] ReadArrayOp [obj, ix]
147   = let
148         lhs' = amodeToStix lhs
149         obj' = amodeToStix obj
150         ix' = amodeToStix ix
151         base = StIndex IntRep obj' arrPtrsHS
152         assign = StAssign PtrRep lhs' (StInd PtrRep (StIndex PtrRep base ix'))
153     in
154     returnUs (\xs -> assign : xs)
155
156 primCode [] WriteArrayOp [obj, ix, v]
157   = let
158         obj' = amodeToStix obj
159         ix' = amodeToStix ix
160         v' = amodeToStix v
161         base = StIndex IntRep obj' arrPtrsHS
162         assign = StAssign PtrRep (StInd PtrRep (StIndex PtrRep base ix')) v'
163     in
164     returnUs (\xs -> assign : xs)
165
166 primCode lhs@[_] (IndexByteArrayOp pk) args
167   = primCode lhs (ReadByteArrayOp pk) args
168
169 -- NB: indexing in "pk" units, *not* in bytes (WDP 95/09)
170
171 primCode [lhs] (ReadByteArrayOp pk) [obj, ix]
172   = let
173         lhs' = amodeToStix lhs
174         obj' = amodeToStix obj
175         ix' = amodeToStix ix
176         base = StIndex IntRep obj' arrWordsHS
177         assign = StAssign pk lhs' (StInd pk (StIndex pk base ix'))
178     in
179     returnUs (\xs -> assign : xs)
180
181 primCode lhs@[_] (ReadOffAddrOp pk) args
182   = primCode lhs (IndexOffAddrOp pk) args
183
184 primCode [lhs] (IndexOffAddrOp pk) [obj, ix]
185   = let
186         lhs' = amodeToStix lhs
187         obj' = amodeToStix obj
188         ix' = amodeToStix ix
189         assign = StAssign pk lhs' (StInd pk (StIndex pk obj' ix'))
190     in
191     returnUs (\xs -> assign : xs)
192
193 primCode [lhs] (IndexOffForeignObjOp pk) [obj, ix]
194   = let
195         lhs' = amodeToStix lhs
196         obj' = amodeToStix obj
197         ix' = amodeToStix ix
198         obj'' = StIndex PtrRep obj' fixedHS
199         assign = StAssign pk lhs' (StInd pk (StIndex pk obj'' ix'))
200     in
201     returnUs (\xs -> assign : xs)
202
203 primCode [] (WriteByteArrayOp pk) [obj, ix, v]
204   = let
205         obj' = amodeToStix obj
206         ix' = amodeToStix ix
207         v' = amodeToStix v
208         base = StIndex IntRep obj' arrWordsHS
209         assign = StAssign pk (StInd pk (StIndex pk base ix')) v'
210     in
211     returnUs (\xs -> assign : xs)
212 \end{code}
213
214 \begin{code}
215 --primCode lhs (CCallOp fn is_asm may_gc) rhs
216 primCode lhs (CCallOp (CCall (StaticTarget fn) is_asm may_gc cconv)) rhs
217   | is_asm = error "ERROR: Native code generator can't handle casm"
218   | may_gc = error "ERROR: Native code generator can't handle _ccall_GC_\n"
219   | otherwise
220   = case lhs of
221       [] -> returnUs (\xs -> (StCall fn cconv VoidRep args) : xs)
222       [lhs] ->
223           let lhs' = amodeToStix lhs
224               pk = if isFloatingRep (getAmodeRep lhs) then DoubleRep else IntRep
225               call = StAssign pk lhs' (StCall fn cconv pk args)
226           in
227               returnUs (\xs -> call : xs)
228   where
229     args = map amodeCodeForCCall rhs
230     amodeCodeForCCall x =
231         let base = amodeToStix' x
232         in
233             case getAmodeRep x of
234               ArrayRep      -> StIndex PtrRep base arrPtrsHS
235               ByteArrayRep  -> StIndex IntRep base arrWordsHS
236               ForeignObjRep -> StIndex PtrRep base fixedHS
237               _ -> base
238 \end{code}
239
240 DataToTagOp won't work for 64-bit archs, as it is.
241
242 \begin{code}
243 primCode [lhs] DataToTagOp [arg]
244   = let lhs'        = amodeToStix lhs
245         arg'        = amodeToStix arg
246         infoptr     = StInd PtrRep arg'
247         word_32     = StInd WordRep (StIndex PtrRep infoptr (StInt (-1)))
248         masked_le32 = StPrim SrlOp [word_32, StInt 16]
249         masked_be32 = StPrim AndOp [word_32, StInt 65535]
250 #ifdef WORDS_BIGENDIAN
251         masked      = masked_be32
252 #else
253         masked      = masked_le32
254 #endif
255         assign      = StAssign IntRep lhs' masked
256     in
257     returnUs (\xs -> assign : xs)
258 \end{code}
259
260 MutVars are pretty simple.
261 #define writeMutVarzh(a,v)       (P_)(((StgMutVar *)(a))->var)=(v)
262
263 \begin{code}
264 primCode [] WriteMutVarOp [aa,vv]
265    = let aa_s      = amodeToStix aa
266          vv_s      = amodeToStix vv
267          var_field = StIndex PtrRep aa_s fixedHS
268          assign    = StAssign PtrRep (StInd PtrRep var_field) vv_s
269      in
270      returnUs (\xs -> assign : xs)
271
272 primCode [rr] ReadMutVarOp [aa]
273    = let aa_s      = amodeToStix aa
274          rr_s      = amodeToStix rr
275          var_field = StIndex PtrRep aa_s fixedHS
276          assign    = StAssign PtrRep rr_s (StInd PtrRep var_field)
277      in
278      returnUs (\xs -> assign : xs)
279 \end{code}
280
281 Now the more mundane operations.
282
283 \begin{code}
284 primCode lhs op rhs
285   = let
286         lhs' = map amodeToStix  lhs
287         rhs' = map amodeToStix' rhs
288         pk   = getAmodeRep (head lhs)
289     in
290     returnUs (\ xs -> simplePrim pk lhs' op rhs' : xs)
291 \end{code}
292
293 \begin{code}
294 simpleCoercion
295       :: PrimRep
296       -> CAddrMode
297       -> CAddrMode
298       -> UniqSM StixTreeList
299
300 simpleCoercion pk lhs rhs
301   = returnUs (\xs -> StAssign pk (amodeToStix lhs) (amodeToStix rhs) : xs)
302 \end{code}
303
304 Here we try to rewrite primitives into a form the code generator can
305 understand.  Any primitives not handled here must be handled at the
306 level of the specific code generator.
307
308 \begin{code}
309 simplePrim
310     :: PrimRep          -- Rep of first destination
311     -> [StixTree]       -- Destinations
312     -> PrimOp
313     -> [StixTree]
314     -> StixTree
315 \end{code}
316
317 Now look for something more conventional.
318
319 \begin{code}
320 simplePrim pk [lhs] op rest  = StAssign pk lhs (StPrim op rest)
321 simplePrim pk as    op bs    = simplePrim_error op
322
323 simplePrim_error op
324     = 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")
325 \end{code}
326
327 %---------------------------------------------------------------------
328
329 Here we generate the Stix code for CAddrModes.
330
331 When a character is fetched from a mixed type location, we have to do
332 an extra cast.  This is reflected in amodeCode', which is for rhs
333 amodes that might possibly need the extra cast.
334
335 \begin{code}
336 amodeToStix, amodeToStix' :: CAddrMode -> StixTree
337
338 amodeToStix'{-'-} am@(CVal rr CharRep)
339     | mixedTypeLocn am = StPrim ChrOp [amodeToStix am]
340     | otherwise = amodeToStix am
341
342 amodeToStix' am = amodeToStix am
343
344 -----------
345 amodeToStix am@(CVal rr CharRep)
346   | mixedTypeLocn am
347   = StInd IntRep (amodeToStix (CAddr rr))
348
349 amodeToStix (CVal rr pk) = StInd pk (amodeToStix (CAddr rr))
350
351 amodeToStix (CAddr (SpRel off))
352   = StIndex PtrRep stgSp (StInt (toInteger IBOX(off)))
353
354 amodeToStix (CAddr (HpRel off))
355   = StIndex IntRep stgHp (StInt (toInteger (- IBOX(off))))
356
357 amodeToStix (CAddr (NodeRel off))
358   = StIndex IntRep stgNode (StInt (toInteger IBOX(off)))
359
360 amodeToStix (CAddr (CIndex base off pk))
361   = StIndex pk (amodeToStix base) (amodeToStix off)
362
363 amodeToStix (CReg magic)    = StReg (StixMagicId magic)
364 amodeToStix (CTemp uniq pk) = StReg (StixTemp uniq pk)
365
366 amodeToStix (CLbl      lbl _) = StCLbl lbl
367
368  -- For CharLike and IntLike, we attempt some trivial constant-folding here.
369
370 amodeToStix (CCharLike (CLit (MachChar c)))
371   = StLitLbl ((<>) (ptext SLIT("CHARLIKE_closure+")) (int off))
372   where
373     off = charLikeSize * ord c
374
375 amodeToStix (CCharLike x)
376   = StIndex CharRep charLike off
377   where
378     off = StPrim IntMulOp [amodeToStix x, StInt (toInteger charLikeSize)]
379
380 amodeToStix (CIntLike (CLit (MachInt i)))
381   = StLitLbl ((<>) (ptext SLIT("INTLIKE_closure+")) (int off))
382   where
383     off = intLikeSize * (fromInteger (i - mIN_INTLIKE))
384
385 amodeToStix (CIntLike x)
386   = panic "CIntLike"
387
388 amodeToStix (CLit core)
389   = case core of
390       MachChar c     -> StInt (toInteger (ord c))
391       MachStr s      -> StString s
392       MachAddr a     -> StInt a
393       MachInt i      -> StInt i
394       MachWord w     -> case word2IntLit core of MachInt iw -> StInt iw
395       MachLitLit s _ -> litLitToStix (_UNPK_ s)
396       MachFloat d    -> StDouble d
397       MachDouble d   -> StDouble d
398       _ -> panic "amodeToStix:core literal"
399
400 amodeToStix (CLitLit s _)
401    = litLitToStix (_UNPK_ s)
402
403 amodeToStix (CMacroExpr _ macro [arg])
404   = case macro of
405       ENTRY_CODE -> amodeToStix arg
406       ARG_TAG    -> amodeToStix arg -- just an integer no. of words
407       GET_TAG    -> 
408 #ifdef WORDS_BIGENDIAN
409                     StPrim AndOp 
410                         [StInd WordRep (StIndex PtrRep (amodeToStix arg)
411                                                 (StInt (toInteger (-1)))),
412                          StInt 65535]
413 #else
414                     StPrim SrlOp 
415                         [StInd WordRep (StIndex PtrRep (amodeToStix arg)
416                                                 (StInt (toInteger (-1)))),
417                          StInt 16]
418 #endif
419       UPD_FRAME_UPDATEE
420          -> StInd PtrRep (StIndex PtrRep (amodeToStix arg) 
421                                          (StInt (toInteger uF_UPDATEE)))
422 -- XXX!!!
423 -- GET_TAG(info_ptr) is supposed to be  get_itbl(info_ptr)->srt_len,
424 -- which we've had to hand-code here.
425
426 litLitToStix :: String -> StixTree
427 litLitToStix nm
428    = case nm of
429         "stdout" -> stixFor_stdout
430         "stderr" -> stixFor_stderr
431         "stdin"  -> stixFor_stdin
432         other    -> error ("\nlitLitToStix: can't handle `" ++ nm ++ "'\n" 
433                            ++ "suggested workaround: use flag -fvia-C\n")
434 \end{code}
435
436 Sizes of the CharLike and IntLike closures that are arranged as arrays
437 in the data segment.  (These are in bytes.)
438
439 \begin{code}
440 -- The INTLIKE base pointer
441
442 intLikePtr :: StixTree
443
444 intLikePtr = StInd PtrRep (sStLitLbl SLIT("INTLIKE_closure"))
445
446 -- The CHARLIKE base
447
448 charLike :: StixTree
449
450 charLike = sStLitLbl SLIT("CHARLIKE_closure")
451
452 -- Trees for the ErrorIOPrimOp
453
454 topClosure, errorIO :: StixTree
455
456 topClosure = StInd PtrRep (sStLitLbl SLIT("TopClosure"))
457 errorIO = StJump (StInd PtrRep (sStLitLbl SLIT("ErrorIO_innards")))
458
459 mutArrPtrsFrozen_info = sStLitLbl SLIT("MUT_ARR_PTRS_FROZEN_info")
460
461 charLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
462 intLikeSize  = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
463 \end{code}