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
|
%
% (c) The AQUA Project, Glasgow University, 1993-1998
%
\begin{code}
module StixPrim ( primCode, amodeToStix, amodeToStix' ) where
#include "HsVersions.h"
import MachMisc
import MachRegs
import Stix
import StixInteger
import AbsCSyn hiding ( spRel )
import AbsCUtils ( getAmodeRep, mixedTypeLocn )
import Constants ( uF_UPDATEE )
import SMRep ( fixedHdrSize )
import Const ( Literal(..) )
import CallConv ( cCallConv )
import PrimOp ( PrimOp(..) )
import PrimRep ( PrimRep(..), isFloatingRep )
import UniqSupply ( returnUs, thenUs, UniqSM )
import Constants ( mIN_INTLIKE )
import Outputable
import Char ( ord )
\end{code}
The main honcho here is primCode, which handles the guts of COpStmts.
\begin{code}
primCode
:: [CAddrMode] -- results
-> PrimOp -- op
-> [CAddrMode] -- args
-> UniqSM StixTreeList
\end{code}
First, the dreaded @ccall@. We can't handle @casm@s.
Usually, this compiles to an assignment, but when the left-hand side
is empty, we just perform the call and ignore the result.
btw Why not let programmer use casm to provide assembly code instead
of C code? ADR
The (MP) integer operations are a true nightmare. Since we don't have
a convenient abstract way of allocating temporary variables on the (C)
stack, we use the space just below HpLim for the @MP_INT@ structures,
and modify our heap check accordingly.
\begin{code}
-- NB: ordering of clauses somewhere driven by
-- the desire to getting sane patt-matching behavior
primCode res@[ar,sr,dr] IntegerNegOp arg@[aa,sa,da]
= gmpNegate (ar,sr,dr) (aa,sa,da)
\end{code}
\begin{code}
primCode [res] IntegerCmpOp args@[aa1,sa1,da1, aa2,sa2,da2]
= gmpCompare res (aa1,sa1,da1, aa2,sa2,da2)
primCode [res] Integer2IntOp arg@[aa,sa,da]
= gmpInteger2Int res (aa,sa,da)
primCode [res] Integer2WordOp arg@[aa,sa,da]
= gmpInteger2Word res (aa,sa,da)
primCode [res] Int2AddrOp [arg]
= simpleCoercion AddrRep res arg
primCode [res] Addr2IntOp [arg]
= simpleCoercion IntRep res arg
primCode [res] Int2WordOp [arg]
= simpleCoercion IntRep{-WordRep?-} res arg
primCode [res] Word2IntOp [arg]
= simpleCoercion IntRep res arg
\end{code}
\begin{code}
primCode [res] SameMutableArrayOp args
= let
compare = StPrim AddrEqOp (map amodeToStix args)
assign = StAssign IntRep (amodeToStix res) compare
in
returnUs (\xs -> assign : xs)
primCode res@[_] SameMutableByteArrayOp args
= primCode res SameMutableArrayOp args
\end{code}
Freezing an array of pointers is a double assignment. We fix the
header of the ``new'' closure because the lhs is probably a better
addressing mode for the indirection (most likely, it's a VanillaReg).
\begin{code}
primCode [lhs] UnsafeFreezeArrayOp [rhs]
= let
lhs' = amodeToStix lhs
rhs' = amodeToStix rhs
header = StInd PtrRep lhs'
assign = StAssign PtrRep lhs' rhs'
freeze = StAssign PtrRep header mutArrPtrsFrozen_info
in
returnUs (\xs -> assign : freeze : xs)
primCode [lhs] UnsafeFreezeByteArrayOp [rhs]
= simpleCoercion PtrRep lhs rhs
primCode [lhs] UnsafeThawByteArrayOp [rhs]
= simpleCoercion PtrRep lhs rhs
\end{code}
Returning the size of (mutable) byte arrays is just
an indexing operation.
\begin{code}
primCode [lhs] SizeofByteArrayOp [rhs]
= let
lhs' = amodeToStix lhs
rhs' = amodeToStix rhs
sz = StIndex IntRep rhs' fixedHS
assign = StAssign IntRep lhs' (StInd IntRep sz)
in
returnUs (\xs -> assign : xs)
primCode [lhs] SizeofMutableByteArrayOp [rhs]
= let
lhs' = amodeToStix lhs
rhs' = amodeToStix rhs
sz = StIndex IntRep rhs' fixedHS
assign = StAssign IntRep lhs' (StInd IntRep sz)
in
returnUs (\xs -> assign : xs)
\end{code}
Most other array primitives translate to simple indexing.
\begin{code}
primCode lhs@[_] IndexArrayOp args
= primCode lhs ReadArrayOp args
primCode [lhs] ReadArrayOp [obj, ix]
= let
lhs' = amodeToStix lhs
obj' = amodeToStix obj
ix' = amodeToStix ix
base = StIndex IntRep obj' arrHS
assign = StAssign PtrRep lhs' (StInd PtrRep (StIndex PtrRep base ix'))
in
returnUs (\xs -> assign : xs)
primCode [] WriteArrayOp [obj, ix, v]
= let
obj' = amodeToStix obj
ix' = amodeToStix ix
v' = amodeToStix v
base = StIndex IntRep obj' arrHS
assign = StAssign PtrRep (StInd PtrRep (StIndex PtrRep base ix')) v'
in
returnUs (\xs -> assign : xs)
primCode lhs@[_] (IndexByteArrayOp pk) args
= primCode lhs (ReadByteArrayOp pk) args
-- NB: indexing in "pk" units, *not* in bytes (WDP 95/09)
primCode [lhs] (ReadByteArrayOp pk) [obj, ix]
= let
lhs' = amodeToStix lhs
obj' = amodeToStix obj
ix' = amodeToStix ix
base = StIndex IntRep obj' arrHS
assign = StAssign pk lhs' (StInd pk (StIndex pk base ix'))
in
returnUs (\xs -> assign : xs)
primCode [lhs] (IndexOffAddrOp pk) [obj, ix]
= let
lhs' = amodeToStix lhs
obj' = amodeToStix obj
ix' = amodeToStix ix
assign = StAssign pk lhs' (StInd pk (StIndex pk obj' ix'))
in
returnUs (\xs -> assign : xs)
primCode [lhs] (IndexOffForeignObjOp pk) [obj, ix]
= let
lhs' = amodeToStix lhs
obj' = amodeToStix obj
ix' = amodeToStix ix
obj'' = StIndex PtrRep obj' fixedHS
assign = StAssign pk lhs' (StInd pk (StIndex pk obj'' ix'))
in
returnUs (\xs -> assign : xs)
primCode [] (WriteByteArrayOp pk) [obj, ix, v]
= let
obj' = amodeToStix obj
ix' = amodeToStix ix
v' = amodeToStix v
base = StIndex IntRep obj' arrHS
assign = StAssign pk (StInd pk (StIndex pk base ix')) v'
in
returnUs (\xs -> assign : xs)
\end{code}
\begin{code}
--primCode lhs (CCallOp fn is_asm may_gc) rhs
primCode lhs (CCallOp (Left fn) is_asm may_gc cconv) rhs
| is_asm = error "ERROR: Native code generator can't handle casm"
| may_gc = error "ERROR: Native code generator can't handle _ccall_GC_\n"
| otherwise
= case lhs of
[] -> returnUs (\xs -> (StCall fn cconv VoidRep args) : xs)
[lhs] ->
let lhs' = amodeToStix lhs
pk = if isFloatingRep (getAmodeRep lhs) then DoubleRep else IntRep
call = StAssign pk lhs' (StCall fn cconv pk args)
in
returnUs (\xs -> call : xs)
where
args = map amodeCodeForCCall rhs
amodeCodeForCCall x =
let base = amodeToStix' x
in
case getAmodeRep x of
ArrayRep -> StIndex PtrRep base arrHS
ByteArrayRep -> StIndex IntRep base arrHS
ForeignObjRep -> StIndex PtrRep base fixedHS
_ -> base
\end{code}
DataToTagOp won't work for 64-bit archs, as it is.
\begin{code}
primCode [lhs] DataToTagOp [arg]
= let lhs' = amodeToStix lhs
arg' = amodeToStix arg
infoptr = StInd PtrRep arg'
word_32 = StInd WordRep (StIndex PtrRep infoptr (StInt (-1)))
masked_le32 = StPrim SrlOp [word_32, StInt 16]
masked_be32 = StPrim AndOp [word_32, StInt 65535]
#ifdef WORDS_BIGENDIAN
masked = masked_be32
#else
masked = masked_le32
#endif
assign = StAssign IntRep lhs' masked
in
returnUs (\xs -> assign : xs)
\end{code}
Now the more mundane operations.
\begin{code}
primCode lhs op rhs
= let
lhs' = map amodeToStix lhs
rhs' = map amodeToStix' rhs
pk = getAmodeRep (head lhs)
in
returnUs (\ xs -> simplePrim pk lhs' op rhs' : xs)
\end{code}
\begin{code}
simpleCoercion
:: PrimRep
-> CAddrMode
-> CAddrMode
-> UniqSM StixTreeList
simpleCoercion pk lhs rhs
= returnUs (\xs -> StAssign pk (amodeToStix lhs) (amodeToStix rhs) : xs)
\end{code}
Here we try to rewrite primitives into a form the code generator can
understand. Any primitives not handled here must be handled at the
level of the specific code generator.
\begin{code}
simplePrim
:: PrimRep -- Rep of first destination
-> [StixTree] -- Destinations
-> PrimOp
-> [StixTree]
-> StixTree
\end{code}
Now look for something more conventional.
\begin{code}
simplePrim pk [lhs] op rest = StAssign pk lhs (StPrim op rest)
simplePrim pk as op bs = simplePrim_error op
simplePrim_error op
= 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")
\end{code}
%---------------------------------------------------------------------
Here we generate the Stix code for CAddrModes.
When a character is fetched from a mixed type location, we have to do
an extra cast. This is reflected in amodeCode', which is for rhs
amodes that might possibly need the extra cast.
\begin{code}
amodeToStix, amodeToStix' :: CAddrMode -> StixTree
amodeToStix'{-'-} am@(CVal rr CharRep)
| mixedTypeLocn am = StPrim ChrOp [amodeToStix am]
| otherwise = amodeToStix am
amodeToStix' am = amodeToStix am
-----------
amodeToStix am@(CVal rr CharRep)
| mixedTypeLocn am
= StInd IntRep (amodeToStix (CAddr rr))
amodeToStix (CVal rr pk) = StInd pk (amodeToStix (CAddr rr))
amodeToStix (CAddr (SpRel off))
= StIndex PtrRep stgSp (StInt (toInteger IBOX(off)))
amodeToStix (CAddr (HpRel off))
= StIndex IntRep stgHp (StInt (toInteger (- IBOX(off))))
amodeToStix (CAddr (NodeRel off))
= StIndex IntRep stgNode (StInt (toInteger IBOX(off)))
amodeToStix (CAddr (CIndex base off pk))
= StIndex pk (amodeToStix base) (amodeToStix off)
amodeToStix (CReg magic) = StReg (StixMagicId magic)
amodeToStix (CTemp uniq pk) = StReg (StixTemp uniq pk)
amodeToStix (CLbl lbl _) = StCLbl lbl
-- For CharLike and IntLike, we attempt some trivial constant-folding here.
amodeToStix (CCharLike (CLit (MachChar c)))
= StLitLbl ((<>) (ptext SLIT("CHARLIKE_closure+")) (int off))
where
off = charLikeSize * ord c
amodeToStix (CCharLike x)
= StIndex PtrRep charLike off
where
off = StPrim IntMulOp [amodeToStix x, StInt (toInteger (fixedHdrSize+1))]
amodeToStix (CIntLike (CLit (MachInt i _)))
= StLitLbl ((<>) (ptext SLIT("INTLIKE_closure+")) (int off))
where
off = intLikeSize * (fromInteger (i - mIN_INTLIKE))
amodeToStix (CIntLike x)
= panic "CIntLike"
amodeToStix (CLit core)
= case core of
MachChar c -> StInt (toInteger (ord c))
MachStr s -> StString s
MachAddr a -> StInt a
MachInt i _ -> StInt (toInteger i)
MachLitLit s _ -> {-trace (_UNPK_ s ++ "\n")-} (litLitToStix (_UNPK_ s))
MachFloat d -> StDouble d
MachDouble d -> StDouble d
_ -> panic "amodeToStix:core literal"
amodeToStix (CLitLit s _)
= litLitToStix (_UNPK_ s)
amodeToStix (CMacroExpr _ macro [arg])
= case macro of
ENTRY_CODE -> amodeToStix arg
ARG_TAG -> amodeToStix arg -- just an integer no. of words
GET_TAG -> StPrim SrlOp
[StInd WordRep (StPrim IntSubOp [amodeToStix arg,
StInt 1]),
StInt 16]
UPD_FRAME_UPDATEE
-> StInd PtrRep (StIndex PtrRep (amodeToStix arg)
(StInt (toInteger uF_UPDATEE)))
-- XXX!!!
-- GET_TAG(info_ptr) is supposed to be get_itbl(info_ptr)->srt_len,
-- which we've had to hand-code here.
litLitToStix :: String -> StixTree
litLitToStix nm
= case nm of
"stdout" -> stixFor_stdout
"stderr" -> stixFor_stderr
"stdin" -> stixFor_stdin
other -> error ("\nlitLitToStix: can't handle `" ++ nm ++ "'\n"
++ "suggested workaround: use flag -fvia-C\n")
\end{code}
Sizes of the CharLike and IntLike closures that are arranged as arrays
in the data segment. (These are in bytes.)
\begin{code}
-- The INTLIKE base pointer
intLikePtr :: StixTree
intLikePtr = StInd PtrRep (sStLitLbl SLIT("INTLIKE_closure"))
-- The CHARLIKE base
charLike :: StixTree
charLike = sStLitLbl SLIT("CHARLIKE_closure")
-- Trees for the ErrorIOPrimOp
topClosure, errorIO :: StixTree
topClosure = StInd PtrRep (sStLitLbl SLIT("TopClosure"))
errorIO = StJump (StInd PtrRep (sStLitLbl SLIT("ErrorIO_innards")))
mutArrPtrsFrozen_info = sStLitLbl SLIT("MUT_ARR_PTRS_FROZEN_info")
charLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
intLikeSize = (fixedHdrSize + 1) * (fromInteger (sizeOf PtrRep))
\end{code}
|