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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
|
%
% (c) The AQUA Project, Glasgow University, 1993-1995
%
\begin{code}
#include "HsVersions.h"
module StixPrim (
genPrimCode, amodeCode, amodeCode',
Target, CAddrMode, StixTree, PrimOp, SplitUniqSupply
) where
IMPORT_Trace -- ToDo: rm debugging
import AbsCSyn
import AbsPrel ( PrimOp(..), PrimOpResultInfo(..), TyCon,
getPrimOpResultInfo, isCompareOp, showPrimOp
IF_ATTACK_PRAGMAS(COMMA tagOf_PrimOp)
IF_ATTACK_PRAGMAS(COMMA pprPrimOp)
)
import AbsUniType ( cmpTyCon ) -- pragmas only
import CgCompInfo ( spARelToInt, spBRelToInt )
import MachDesc
import Pretty
import PrimKind ( isFloatingKind )
import CostCentre
import SMRep ( SMRep(..), SMSpecRepKind(..), SMUpdateKind(..) )
import Stix
import StixMacro ( smStablePtrTable )
import StixInteger {- everything -}
import SplitUniq
import Unique
import Unpretty
import Util
\end{code}
The main honcho here is genPrimCode, which handles the guts of COpStmts.
\begin{code}
arrayOfData_info = sStLitLbl SLIT("ArrayOfData_info") -- out here to avoid CAF (sigh)
imMutArrayOfPtrs_info = sStLitLbl SLIT("ImMutArrayOfPtrs_info")
genPrimCode
:: Target
-> [CAddrMode] -- results
-> PrimOp -- op
-> [CAddrMode] -- args
-> SUniqSM 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.
ToDo ADR: modify this to handle Malloc Ptrs.
btw Why not let programmer use casm to provide assembly code instead
of C code? ADR
\begin{code}
genPrimCode target lhs (CCallOp fn is_asm may_gc arg_tys result_ty) rhs
| is_asm = error "ERROR: Native code generator can't handle casm"
| otherwise =
case lhs of
[] -> returnSUs (\xs -> (StCall fn VoidKind args) : xs)
[lhs] ->
let lhs' = amodeToStix target lhs
pk = if isFloatingKind (getAmodeKind lhs) then DoubleKind else IntKind
call = StAssign pk lhs' (StCall fn pk args)
in
returnSUs (\xs -> call : xs)
where
args = map amodeCodeForCCall rhs
amodeCodeForCCall x =
let base = amodeToStix' target x
in
case getAmodeKind x of
ArrayKind -> StIndex PtrKind base (mutHS target)
ByteArrayKind -> StIndex IntKind base (dataHS target)
MallocPtrKind -> error "ERROR: native-code generator can't handle Malloc Ptrs (yet): use -fvia-C!"
_ -> base
\end{code}
The @ErrorIO@ primitive is actually a bit weird...assign a new value to the root
closure, flush stdout and stderr, and jump to the @ErrorIO_innards@.
\begin{code}
genPrimCode target [] ErrorIOPrimOp [rhs] =
let changeTop = StAssign PtrKind topClosure (amodeToStix target rhs)
in
returnSUs (\xs -> changeTop : flushStdout : flushStderr : errorIO : xs)
\end{code}
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}
genPrimCode target res IntegerAddOp args =
gmpTake2Return1 target res SLIT("mpz_add") args
genPrimCode target res IntegerSubOp args =
gmpTake2Return1 target res SLIT("mpz_sub") args
genPrimCode target res IntegerMulOp args =
gmpTake2Return1 target res SLIT("mpz_mul") args
genPrimCode target res IntegerNegOp arg =
gmpTake1Return1 target res SLIT("mpz_neg") arg
genPrimCode target res IntegerQuotRemOp arg =
gmpTake2Return2 target res SLIT("mpz_divmod") arg
genPrimCode target res IntegerDivModOp arg =
gmpTake2Return2 target res SLIT("mpz_targetivmod") arg
\end{code}
Since we are using the heap for intermediate @MP_INT@ structs, integer comparison
{\em does} require a heap check in the native code implementation.
\begin{code}
genPrimCode target [res] IntegerCmpOp args = gmpCompare target res args
genPrimCode target [res] Integer2IntOp arg = gmpInteger2Int target res arg
genPrimCode target res Int2IntegerOp args = gmpInt2Integer target res args
genPrimCode target res Word2IntegerOp args = panic "genPrimCode:Word2IntegerOp"
genPrimCode target res Addr2IntegerOp args = gmpString2Integer target res args
genPrimCode target res FloatEncodeOp args =
encodeFloatingKind FloatKind target res args
genPrimCode target res DoubleEncodeOp args =
encodeFloatingKind DoubleKind target res args
genPrimCode target res FloatDecodeOp args =
decodeFloatingKind FloatKind target res args
genPrimCode target res DoubleDecodeOp args =
decodeFloatingKind DoubleKind target res args
genPrimCode target res Int2AddrOp arg =
simpleCoercion target AddrKind res arg
genPrimCode target res Addr2IntOp arg =
simpleCoercion target IntKind res arg
genPrimCode target res Int2WordOp arg =
simpleCoercion target IntKind{-WordKind?-} res arg
genPrimCode target res Word2IntOp arg =
simpleCoercion target IntKind res arg
\end{code}
@newArray#@ ops allocate heap space.
\begin{code}
genPrimCode target [res] NewArrayOp args =
let [liveness, n, initial] = map (amodeToStix target) args
result = amodeToStix target res
space = StPrim IntAddOp [n, mutHS target]
loc = StIndex PtrKind stgHp
(StPrim IntNegOp [StPrim IntSubOp [space, StInt 1]])
assign = StAssign PtrKind result loc
initialise = StCall SLIT("newArrZh_init") VoidKind [result, n, initial]
in
heapCheck target liveness space (StInt 0)
`thenSUs` \ heap_chk ->
returnSUs (heap_chk . (\xs -> assign : initialise : xs))
genPrimCode target [res] (NewByteArrayOp pk) args =
let [liveness, count] = map (amodeToStix target) args
result = amodeToStix target res
n = StPrim IntMulOp [count, StInt (toInteger (sizeof target pk))]
slop = StPrim IntAddOp [n, StInt (toInteger (sizeof target IntKind - 1))]
words = StPrim IntDivOp [slop, StInt (toInteger (sizeof target IntKind))]
space = StPrim IntAddOp [n, StPrim IntAddOp [words, dataHS target]]
loc = StIndex PtrKind stgHp
(StPrim IntNegOp [StPrim IntSubOp [space, StInt 1]])
assign = StAssign PtrKind result loc
init1 = StAssign PtrKind (StInd PtrKind loc) arrayOfData_info
init2 = StAssign IntKind
(StInd IntKind
(StIndex IntKind loc
(StInt (toInteger (fixedHeaderSize target)))))
(StPrim IntAddOp [words,
StInt (toInteger (varHeaderSize target
(DataRep 0)))])
in
heapCheck target liveness space (StInt 0)
`thenSUs` \ heap_chk ->
returnSUs (heap_chk . (\xs -> assign : init1 : init2 : xs))
genPrimCode target [res] SameMutableArrayOp args =
let compare = StPrim AddrEqOp (map (amodeToStix target) args)
assign = StAssign IntKind (amodeToStix target res) compare
in
returnSUs (\xs -> assign : xs)
genPrimCode target res SameMutableByteArrayOp args =
genPrimCode target 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}
genPrimCode target [lhs] UnsafeFreezeArrayOp [rhs] =
let lhs' = amodeToStix target lhs
rhs' = amodeToStix target rhs
header = StInd PtrKind lhs'
assign = StAssign PtrKind lhs' rhs'
freeze = StAssign PtrKind header imMutArrayOfPtrs_info
in
returnSUs (\xs -> assign : freeze : xs)
genPrimCode target lhs UnsafeFreezeByteArrayOp rhs =
simpleCoercion target PtrKind lhs rhs
\end{code}
Most other array primitives translate to simple indexing.
\begin{code}
genPrimCode target lhs IndexArrayOp args =
genPrimCode target lhs ReadArrayOp args
genPrimCode target [lhs] ReadArrayOp [obj, ix] =
let lhs' = amodeToStix target lhs
obj' = amodeToStix target obj
ix' = amodeToStix target ix
base = StIndex IntKind obj' (mutHS target)
assign = StAssign PtrKind lhs' (StInd PtrKind (StIndex PtrKind base ix'))
in
returnSUs (\xs -> assign : xs)
genPrimCode target [lhs] WriteArrayOp [obj, ix, v] =
let obj' = amodeToStix target obj
ix' = amodeToStix target ix
v' = amodeToStix target v
base = StIndex IntKind obj' (mutHS target)
assign = StAssign PtrKind (StInd PtrKind (StIndex PtrKind base ix')) v'
in
returnSUs (\xs -> assign : xs)
genPrimCode target lhs (IndexByteArrayOp pk) args =
genPrimCode target lhs (ReadByteArrayOp pk) args
genPrimCode target [lhs] (ReadByteArrayOp pk) [obj, ix] =
let lhs' = amodeToStix target lhs
obj' = amodeToStix target obj
ix' = amodeToStix target ix
base = StIndex IntKind obj' (dataHS target)
assign = StAssign pk lhs' (StInd pk (StIndex CharKind base ix'))
in
returnSUs (\xs -> assign : xs)
genPrimCode target [] (WriteByteArrayOp pk) [obj, ix, v] =
let obj' = amodeToStix target obj
ix' = amodeToStix target ix
v' = amodeToStix target v
base = StIndex IntKind obj' (dataHS target)
assign = StAssign pk (StInd pk (StIndex CharKind base ix')) v'
in
returnSUs (\xs -> assign : xs)
genPrimCode target [lhs] (IndexOffAddrOp pk) [obj, ix] =
let lhs' = amodeToStix target lhs
obj' = amodeToStix target obj
ix' = amodeToStix target ix
assign = StAssign pk lhs' (StInd pk (StIndex CharKind obj' ix'))
in
returnSUs (\xs -> assign : xs)
\end{code}
Stable pointer operations.
First the easy one.
\begin{code}
genPrimCode target [lhs] DeRefStablePtrOp [sp] =
let lhs' = amodeToStix target lhs
pk = getAmodeKind lhs
sp' = amodeToStix target sp
call = StCall SLIT("deRefStablePointer") pk [sp', smStablePtrTable]
assign = StAssign pk lhs' call
in
returnSUs (\xs -> assign : xs)
\end{code}
Now the hard one. For comparison, here's the code from StgMacros:
\begin{verbatim}
#define makeStablePtrZh(stablePtr,liveness,unstablePtr) \
do { \
EXTDATA(MK_INFO_LBL(StablePointerTable)); \
EXTDATA(UnusedSP); \
StgStablePtr newSP; \
\
if (SPT_EMPTY(StorageMgrInfo.StablePointerTable)) { /* free stack is empty */ \
I_ OldNoPtrs = SPT_NoPTRS(StorageMgrInfo.StablePointerTable); \
\
/* any strictly increasing expression will do here */ \
I_ NewNoPtrs = OldNoPtrs * 2 + 100; \
\
I_ NewSize = DYN_VHS + NewNoPtrs + 1 + NewNoPtrs; \
P_ SPTable; \
\
HEAP_CHK(NO_LIVENESS, _FHS+NewSize, 0); \
CC_ALLOC(CCC, _FHS+NewSize, SPT_K); /* cc prof */ \
\
SPTable = Hp + 1 - (_FHS + NewSize); \
SET_DYN_HDR(SPTable,StablePointerTable,CCC,NewSize,NewNoPtrs); \
SAFESTGCALL2(void, (void *, P_, P_), enlargeSPTable, SPTable, StorageMgrInfo.StablePointerTable); \
StorageMgrInfo.StablePointerTable = SPTable; \
} \
\
newSP = SPT_POP(StorageMgrInfo.StablePointerTable); \
SPT_SPTR(StorageMgrInfo.StablePointerTable, newSP) = unstablePtr; \
stablePtr = newSP; \
} while (0)
\end{verbatim}
ToDo ADR: finish this. (Boy, this is hard work!)
Notes for ADR:
trMumbles are now just StMumbles.
StInt 1 is how to write ``1''
temporaries are allocated at the end of the heap (see notes in StixInteger)
Good luck!
--JSM
\begin{pseudocode}
genPrimCode sty md [lhs] MakeStablePtrOp args =
let
-- some useful abbreviations (I'm sure these must exist already)
add = trPrim . IntAddOp
sub = trPrim . IntSubOp
one = trInt [1]
dec x = trAssign IntKind [x, sub [x, one]]
inc x = trAssign IntKind [x, add [x, one]]
-- tedious hardwiring in of closure layout offsets (from SMClosures)
dynHS = 2 + fixedHeaderSize md sty + varHeaderSize md sty DynamicRep
spt_SIZE c = trIndex PtrKind [c, trInt [fhs + gc_reserved] ]
spt_NoPTRS c = trIndex PtrKind [c, trInt [fhs + gc_reserved + 1] ]
spt_SPTR c i = trIndex PtrKind [c, add [trInt [dynHS], i]]
spt_TOP c = trIndex PtrKind [c, add [trInt [dynHS], spt_NoPTRS c]]
spt_FREE c i = trIndex PtrKind [c, add [trInt [dynHS], spt_NoPTRS c]]
-- tedious hardwiring in of stack manipulation macros (from SMClosures)
spt_FULL c lbl =
trCondJump lbl [trPrim IntEqOp [spt_TOP c, spt_NoPTRS c]]
spt_EMPTY c lbl =
trCondJump lbl [trPrim IntEqOp [spt_TOP c, trInt [0]]]
spt_PUSH c f = [
trAssign PtrKind [spt_FREE c (spt_TOP c), f],
inc (spt_TOP c),
spt_POP c x = [
dec (spt_TOP c),
trAssign PtrKind [x, spt_FREE c (spt_TOP c)]
]
-- now to get down to business
lhs' = amodeCode sty md lhs
[liveness, unstable] = map (amodeCode sty md) args
spt = smStablePtrTable
newSPT = -- a temporary (don't know how to allocate it)
newSP = -- another temporary
allocNewTable = -- some sort fo heap allocation needed
copyOldTable = trCall "enlargeSPTable" PtrKind [newSPT, spt]
enlarge =
allocNewTable ++ [
copyOldTable,
trAssign PtrKind [spt, newSPT]
allocate = [
spt_POP spt newSP,
trAssign PtrKind [spt_SPTR spt newSP, unstable],
trAssign StablePtrKind [lhs', newSP]
]
in
getUniqLabelCTS `thenCTS` \ oklbl ->
returnCodes sty md
(spt_EMPTY spt oklbl : (enlarge ++ (trLabel [oklbl] : allocate)))
\end{pseudocode}
Now the more mundane operations.
\begin{code}
genPrimCode target lhs op rhs =
let lhs' = map (amodeToStix target) lhs
rhs' = map (amodeToStix' target) rhs
in
returnSUs (\ xs -> simplePrim target lhs' op rhs' : xs)
simpleCoercion
:: Target
-> PrimKind
-> [CAddrMode]
-> [CAddrMode]
-> SUniqSM StixTreeList
simpleCoercion target pk [lhs] [rhs] =
returnSUs (\xs -> StAssign pk (amodeToStix target lhs) (amodeToStix target 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
:: Target
-> [StixTree]
-> PrimOp
-> [StixTree]
-> StixTree
\end{code}
Now look for something more conventional.
\begin{code}
simplePrim target [lhs] op rest = StAssign pk lhs (StPrim op rest)
where pk = if isCompareOp op then IntKind
else case getPrimOpResultInfo op of
ReturnsPrim pk -> pk
_ -> simplePrim_error op
simplePrim target _ op _ = simplePrim_error op
simplePrim_error op
= error ("ERROR: primitive operation `"++showPrimOp PprDebug 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}
amodeCode, amodeCode'
:: Target
-> CAddrMode
-> StixTree
amodeCode' target am@(CVal rr CharKind)
| mixedTypeLocn am = StPrim ChrOp [amodeToStix target am]
| otherwise = amodeToStix target am
amodeCode' target am = amodeToStix target am
amodeCode target am@(CVal rr CharKind) | mixedTypeLocn am =
StInd IntKind (amodeCode target (CAddr rr))
amodeCode target (CVal rr pk) = StInd pk (amodeCode target (CAddr rr))
amodeCode target (CAddr r@(SpARel spA off)) =
StIndex PtrKind stgSpA (StInt (toInteger (spARelToInt r)))
amodeCode target (CAddr r@(SpBRel spB off)) =
StIndex IntKind stgSpB (StInt (toInteger (spBRelToInt r)))
amodeCode target (CAddr (HpRel hp off)) =
StIndex IntKind stgHp (StInt (toInteger (-(hpRel target (hp `subOff` off)))))
amodeCode target (CAddr (NodeRel off)) =
StIndex IntKind stgNode (StInt (toInteger (hpRel target off)))
amodeCode target (CReg magic) = StReg (StixMagicId magic)
amodeCode target (CTemp uniq pk) = StReg (StixTemp uniq pk)
amodeCode target (CLbl lbl _) = StCLbl lbl
amodeCode target (CUnVecLbl dir _) = StCLbl dir
amodeCode target (CTableEntry base off pk) =
StInd pk (StIndex pk (amodeCode target base) (amodeCode target off))
-- For CharLike and IntLike, we attempt some trivial constant-folding here.
amodeCode target (CCharLike (CLit (MachChar c))) =
StLitLbl (uppBeside (uppPStr SLIT("CHARLIKE_closures+")) (uppInt off))
where off = charLikeClosureSize target * ord c
amodeCode target (CCharLike x) =
StPrim IntAddOp [charLike, off]
where off = StPrim IntMulOp [amodeCode target x,
StInt (toInteger (charLikeClosureSize target))]
amodeCode target (CIntLike (CLit (MachInt i _))) =
StPrim IntAddOp [intLikePtr, StInt off]
where off = toInteger (intLikeClosureSize target) * i
amodeCode target (CIntLike x) =
StPrim IntAddOp [intLikePtr, off]
where off = StPrim IntMulOp [amodeCode target x,
StInt (toInteger (intLikeClosureSize target))]
-- A CString is just a (CLit . MachStr)
amodeCode target (CString s) = StString s
amodeCode target (CLit core) = case core of
(MachChar c) -> StInt (toInteger (ord c))
(MachStr s) -> StString s
(MachAddr a) -> StInt a
(MachInt i _) -> StInt i
(MachLitLit s _) -> StLitLit s
(MachFloat d) -> StDouble d
(MachDouble d) -> StDouble d
_ -> panic "amodeCode:core literal"
-- A CLitLit is just a (CLit . MachLitLit)
amodeCode target (CLitLit s _) = StLitLit s
-- COffsets are in words, not bytes!
amodeCode target (COffset off) = StInt (toInteger (hpRel target off))
amodeCode target (CMacroExpr _ macro [arg]) =
case macro of
INFO_PTR -> StInd PtrKind (amodeToStix target arg)
ENTRY_CODE -> amodeToStix target arg
INFO_TAG -> tag
EVAL_TAG -> StPrim IntGeOp [tag, StInt 0]
where
tag = StInd IntKind (StIndex IntKind (amodeToStix target arg) (StInt (-2)))
-- That ``-2'' really bothers me. (JSM)
amodeCode target (CCostCentre cc print_as_string)
= if noCostCentreAttached cc
then StComment SLIT("") -- sigh
else panic "amodeCode:CCostCentre"
\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 PtrKind (sStLitLbl SLIT("INTLIKE_closures"))
-- The CHARLIKE base
charLike :: StixTree
charLike = sStLitLbl SLIT("CHARLIKE_closures")
-- Trees for the ErrorIOPrimOp
topClosure, flushStdout, flushStderr, errorIO :: StixTree
topClosure = StInd PtrKind (sStLitLbl SLIT("TopClosure"))
flushStdout = StCall SLIT("fflush") VoidKind [StLitLit SLIT("stdout")]
flushStderr = StCall SLIT("fflush") VoidKind [StLitLit SLIT("stderr")]
errorIO = StJump (StInd PtrKind (sStLitLbl SLIT("ErrorIO_innards")))
\end{code}
|