[project @ 2000-07-11 16:24:57 by simonmar]
[ghc-hetmet.git] / ghc / compiler / nativeGen / StixMacro.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1993-1998
3 %
4
5 \begin{code}
6 module StixMacro ( macroCode, checkCode ) where
7
8 #include "HsVersions.h"
9 #include "nativeGen/NCG.h"
10
11 import {-# SOURCE #-} StixPrim ( amodeToStix )
12
13 import MachRegs
14 import AbsCSyn          ( CStmtMacro(..), MagicId(..), CAddrMode, tagreg,
15                           CCheckMacro(..) )
16 import Constants        ( uF_RET, uF_SU, uF_UPDATEE, uF_SIZE, sEQ_FRAME_SIZE )
17 import CallConv         ( cCallConv )
18 import PrimOp           ( PrimOp(..) )
19 import PrimRep          ( PrimRep(..) )
20 import Stix
21 import UniqSupply       ( returnUs, thenUs, UniqSM )
22 import CLabel           ( mkBlackHoleInfoTableLabel, mkIndStaticInfoLabel,
23                           mkIndInfoLabel, mkUpdInfoLabel, mkSeqInfoLabel,
24                           mkRtsGCEntryLabel, mkStgUpdatePAPLabel )
25 \end{code}
26
27 The @ARGS_CHK_A{_LOAD_NODE}@ macros check for sufficient arguments on
28 the A stack, and perform a tail call to @UpdatePAP@ if the arguments are
29 not there.  The @_LOAD_NODE@ version also loads R1 with an appropriate
30 closure address.
31
32 \begin{code}
33 macroCode
34     :: CStmtMacro           -- statement macro
35     -> [CAddrMode]          -- args
36     -> UniqSM StixTreeList
37 \end{code}
38
39 -----------------------------------------------------------------------------
40 Argument satisfaction checks.
41
42 \begin{code}
43 macroCode ARGS_CHK_LOAD_NODE args
44   = getUniqLabelNCG                                     `thenUs` \ ulbl ->
45     let
46           [words, lbl] = map amodeToStix args
47           temp = StIndex PtrRep stgSp words
48           test = StPrim AddrGeOp [stgSu, temp]
49           cjmp = StCondJump ulbl test
50           assign = StAssign PtrRep stgNode lbl
51           join = StLabel ulbl
52     in
53     returnUs (\xs -> cjmp : assign : updatePAP : join : xs)
54
55 macroCode ARGS_CHK [words]
56   = getUniqLabelNCG                                     `thenUs` \ ulbl ->
57     let temp = StIndex PtrRep stgSp (amodeToStix words)
58         test = StPrim AddrGeOp [stgSu, temp]
59         cjmp = StCondJump ulbl test
60         join = StLabel ulbl
61     in
62     returnUs (\xs -> cjmp : updatePAP : join : xs)
63 \end{code}
64
65 -----------------------------------------------------------------------------
66 Updating a CAF
67
68 @UPD_CAF@ involves changing the info pointer of the closure, and
69 adding an indirection.
70
71 \begin{code}
72 macroCode UPD_CAF args
73   = let
74         [cafptr,bhptr] = map amodeToStix args
75         w0 = StInd PtrRep cafptr
76         w1 = StInd PtrRep (StIndex PtrRep cafptr fixedHS)
77         blocking_queue = StInd PtrRep (StIndex PtrRep bhptr fixedHS)
78         a1 = StAssign PtrRep w0 ind_static_info
79         a2 = StAssign PtrRep w1 bhptr
80         a3 = StCall SLIT("newCAF") cCallConv VoidRep [cafptr]
81     in
82     returnUs (\xs -> a1 : a2 : a3 : xs)
83 \end{code}
84
85 -----------------------------------------------------------------------------
86 Blackholing
87
88 We do lazy blackholing: no need to overwrite thunks with blackholes
89 the minute they're entered, as long as we do it before a context
90 switch or garbage collection, that's ok.
91
92 Don't blackhole single entry closures, for the following reasons:
93         
94         - if the compiler has decided that they won't be entered again,
95           that probably means that nothing has a pointer to it
96           (not necessarily true, but...)
97
98         - no need to blackhole for concurrency reasons, because nothing
99           can block on the result of this computation.
100
101 \begin{code}
102 macroCode UPD_BH_UPDATABLE args = returnUs id
103
104 macroCode UPD_BH_SINGLE_ENTRY args = returnUs id
105 {-
106   = let
107         update = StAssign PtrRep (StInd PtrRep (amodeToStix arg)) bh_info
108     in
109     returnUs (\xs -> update : xs)
110 -}
111 \end{code}
112
113 -----------------------------------------------------------------------------
114 Update frames
115
116 Push a four word update frame on the stack and slide the Su registers
117 to the current Sp location.
118
119 \begin{code}
120 macroCode PUSH_UPD_FRAME args
121   = let
122         [bhptr, _{-0-}] = map amodeToStix args
123         frame n = StInd PtrRep
124             (StIndex PtrRep stgSp (StInt (toInteger (n-uF_SIZE))))
125
126         -- HWL: these values are *wrong* in a GranSim setup; ToDo: fix
127         a1 = StAssign PtrRep (frame uF_RET)     upd_frame_info
128         a3 = StAssign PtrRep (frame uF_SU)      stgSu
129         a4 = StAssign PtrRep (frame uF_UPDATEE) bhptr
130
131         updSu = StAssign PtrRep stgSu
132                 (StIndex PtrRep stgSp (StInt (toInteger (-uF_SIZE))))
133     in
134     returnUs (\xs -> a1 : a3 : a4 : updSu : xs)
135
136
137 macroCode PUSH_SEQ_FRAME args
138    = let [arg_frame] = map amodeToStix args
139          frame n = StInd PtrRep
140                      (StIndex PtrRep arg_frame (StInt (toInteger n)))
141          a1 = StAssign PtrRep (frame 0) seq_frame_info
142          a2 = StAssign PtrRep (frame 1) stgSu
143          updSu = StAssign PtrRep stgSu arg_frame 
144      in
145      returnUs (\xs -> a1 : a2 : updSu : xs)
146
147
148 macroCode UPDATE_SU_FROM_UPD_FRAME args
149    = let [arg_frame] = map amodeToStix args
150          frame n = StInd PtrRep
151                       (StIndex PtrRep arg_frame (StInt (toInteger n)))
152          updSu
153             = StAssign PtrRep stgSu (frame uF_SU)
154      in
155      returnUs (\xs -> updSu : xs)
156 \end{code}
157
158 -----------------------------------------------------------------------------
159 Setting the tag register
160
161 This one only applies if we have a machine register devoted to TagReg.
162
163 \begin{code}
164 macroCode SET_TAG [tag]
165   = let set_tag = StAssign IntRep stgTagReg (amodeToStix tag)
166     in
167     case stgReg tagreg of
168       Always _ -> returnUs id
169       Save   _ -> returnUs (\ xs -> set_tag : xs)
170 \end{code}
171
172 -----------------------------------------------------------------------------
173
174 \begin{code}
175 macroCode REGISTER_IMPORT [arg]
176    = returnUs (
177         \xs -> StAssign WordRep (StInd WordRep stgSp) (amodeToStix arg)
178              : StAssign PtrRep  stgSp (StPrim IntAddOp [stgSp, StInt 4])
179              : xs
180      )
181
182 macroCode REGISTER_FOREIGN_EXPORT [arg]
183    = returnUs (
184         \xs -> StCall SLIT("getStablePtr") cCallConv VoidRep [amodeToStix arg]
185              : xs
186      )
187
188 macroCode other args
189    = case other of
190         SET_TAG -> error "foobarxyzzy8"
191         _       -> error "StixMacro.macroCode: unknown macro/args"
192 \end{code}
193
194
195 Do the business for a @HEAP_CHK@, having converted the args to Trees
196 of StixOp.
197
198 -----------------------------------------------------------------------------
199 Let's make sure that these CAFs are lifted out, shall we?
200
201 \begin{code}
202 -- Some common labels
203
204 bh_info, ind_static_info, ind_info :: StixTree
205
206 bh_info         = StCLbl mkBlackHoleInfoTableLabel
207 ind_static_info = StCLbl mkIndStaticInfoLabel
208 ind_info        = StCLbl mkIndInfoLabel
209 upd_frame_info  = StCLbl mkUpdInfoLabel
210 seq_frame_info  = StCLbl mkSeqInfoLabel
211 stg_update_PAP  = StCLbl mkStgUpdatePAPLabel
212 -- Some common call trees
213
214 updatePAP, stackOverflow :: StixTree
215
216 updatePAP     = StJump stg_update_PAP
217 stackOverflow = StCall SLIT("StackOverflow") cCallConv VoidRep []
218 \end{code}
219
220 -----------------------------------------------------------------------------
221 Heap/Stack checks
222
223 \begin{code}
224 checkCode :: CCheckMacro -> [CAddrMode] -> StixTreeList -> UniqSM StixTreeList
225 checkCode macro args assts
226   = getUniqLabelNCG             `thenUs` \ ulbl_fail ->
227     getUniqLabelNCG             `thenUs` \ ulbl_pass ->
228
229     let args_stix = map amodeToStix args
230         newHp wds = StIndex PtrRep stgHp wds
231         assign_hp wds = StAssign PtrRep stgHp (newHp wds)
232         test_hp = StPrim AddrLeOp [stgHp, stgHpLim]
233         cjmp_hp = StCondJump ulbl_pass test_hp
234
235         newSp wds = StIndex PtrRep stgSp (StPrim IntNegOp [wds])
236         test_sp_pass wds = StPrim AddrGeOp [newSp wds, stgSpLim]
237         test_sp_fail wds = StPrim AddrLtOp [newSp wds, stgSpLim]
238         cjmp_sp_pass wds = StCondJump ulbl_pass (test_sp_pass wds)
239         cjmp_sp_fail wds = StCondJump ulbl_fail (test_sp_fail wds)
240
241         assign_ret r ret = StAssign CodePtrRep r ret
242
243         fail = StLabel ulbl_fail
244         join = StLabel ulbl_pass
245
246         -- see includes/StgMacros.h for explaination of these magic consts
247         aLL_NON_PTRS
248            = IF_ARCH_alpha(16383,65535)
249
250         assign_liveness ptr_regs 
251            = StAssign WordRep stgR9
252                       (StPrim XorOp [StInt aLL_NON_PTRS, ptr_regs])
253         assign_reentry reentry 
254            = StAssign WordRep stgR10 reentry
255     in  
256
257     returnUs (
258     case macro of
259         HP_CHK_NP      -> 
260                 let [words,ptrs] = args_stix
261                 in  (\xs -> assign_hp words : cjmp_hp : 
262                             assts (gc_enter ptrs : join : xs))
263
264         HP_CHK_SEQ_NP  -> 
265                 let [words,ptrs] = args_stix
266                 in  (\xs -> assign_hp words : cjmp_hp : 
267                             assts (gc_seq ptrs : join : xs))
268
269         STK_CHK_NP     -> 
270                 let [words,ptrs] = args_stix
271                 in  (\xs -> cjmp_sp_pass words :
272                             assts (gc_enter ptrs : join : xs))
273
274         HP_STK_CHK_NP  -> 
275                 let [sp_words,hp_words,ptrs] = args_stix
276                 in  (\xs -> cjmp_sp_fail sp_words : 
277                             assign_hp hp_words : cjmp_hp :
278                             fail :
279                             assts (gc_enter ptrs : join : xs))
280
281         HP_CHK         -> 
282                 let [words,ret,r,ptrs] = args_stix
283                 in  (\xs -> assign_hp words : cjmp_hp :
284                             assts (assign_ret r ret : gc_chk ptrs : join : xs))
285
286         STK_CHK        -> 
287                 let [words,ret,r,ptrs] = args_stix
288                 in  (\xs -> cjmp_sp_pass words :
289                             assts (assign_ret r ret : gc_chk ptrs : join : xs))
290
291         HP_STK_CHK     -> 
292                 let [sp_words,hp_words,ret,r,ptrs] = args_stix
293                 in  (\xs -> cjmp_sp_fail sp_words :
294                             assign_hp hp_words : cjmp_hp :
295                             fail :
296                             assts (assign_ret r ret : gc_chk ptrs : join : xs))
297
298         HP_CHK_NOREGS  -> 
299                 let [words] = args_stix
300                 in  (\xs -> assign_hp words : cjmp_hp : 
301                             assts (gc_noregs : join : xs))
302
303         HP_CHK_UNPT_R1 -> 
304                 let [words] = args_stix
305                 in  (\xs -> assign_hp words : cjmp_hp : 
306                             assts (gc_unpt_r1 : join : xs))
307
308         HP_CHK_UNBX_R1 -> 
309                 let [words] = args_stix
310                 in  (\xs -> assign_hp words : cjmp_hp : 
311                             assts (gc_unbx_r1 : join : xs))
312
313         HP_CHK_F1      -> 
314                 let [words] = args_stix
315                 in  (\xs -> assign_hp words : cjmp_hp : 
316                             assts (gc_f1 : join : xs))
317
318         HP_CHK_D1      -> 
319                 let [words] = args_stix
320                 in  (\xs -> assign_hp words : cjmp_hp : 
321                             assts (gc_d1 : join : xs))
322
323         HP_CHK_UT_ALT  -> 
324                 let [words,ptrs,nonptrs,r,ret] = args_stix
325                 in (\xs -> assign_hp words : cjmp_hp :
326                            assts (assign_ret r ret : gc_ut ptrs nonptrs 
327                                   : join : xs))
328
329         HP_CHK_GEN     -> 
330                 let [words,liveness,reentry] = args_stix
331                 in (\xs -> assign_hp words : cjmp_hp :
332                            assts (assign_liveness liveness :
333                                   assign_reentry reentry :
334                                   gc_gen : join : xs))
335     )
336         
337 -- Various canned heap-check routines
338
339 mkStJump_to_GCentry :: String -> StixTree
340 mkStJump_to_GCentry gcname
341 --   | opt_Static
342    = StJump (StCLbl (mkRtsGCEntryLabel gcname))
343 --   | otherwise -- it's in a different DLL
344 --   = StJump (StInd PtrRep (StLitLbl True sdoc))
345
346 gc_chk (StInt n)   = mkStJump_to_GCentry ("stg_chk_" ++ show n)
347 gc_enter (StInt n) = mkStJump_to_GCentry ("stg_gc_enter_" ++ show n)
348 gc_seq (StInt n)   = mkStJump_to_GCentry ("stg_gc_seq_" ++ show n)
349 gc_noregs          = mkStJump_to_GCentry "stg_gc_noregs"
350 gc_unpt_r1         = mkStJump_to_GCentry "stg_gc_unpt_r1"
351 gc_unbx_r1         = mkStJump_to_GCentry "stg_gc_unbx_r1"
352 gc_f1              = mkStJump_to_GCentry "stg_gc_f1"
353 gc_d1              = mkStJump_to_GCentry "stg_gc_d1"
354 gc_gen             = mkStJump_to_GCentry "stg_gen_chk"
355 gc_ut (StInt p) (StInt np)
356                    = mkStJump_to_GCentry ("stg_gc_ut_" ++ show p 
357                                           ++ "_" ++ show np)
358 \end{code}