[project @ 2000-12-18 12:43:04 by sewardj]
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeGen.lhs
1 %
2 % (c) The University of Glasgow 2000
3 %
4 \section[ByteCodeGen]{Generate bytecode from Core}
5
6 \begin{code}
7 module ByteCodeGen ( UnlinkedBCO, UnlinkedBCOExpr, ItblEnv, ClosureEnv, HValue,
8                      filterNameMap,
9                      byteCodeGen, coreExprToBCOs, 
10                      linkIModules, linkIExpr
11                    ) where
12
13 #include "HsVersions.h"
14
15 import Outputable
16 import Name             ( Name, getName, nameModule )
17 import Id               ( Id, idType, isDataConId_maybe )
18 import OrdList          ( OrdList, consOL, snocOL, appOL, unitOL, 
19                           nilOL, toOL, concatOL, fromOL )
20 import FiniteMap        ( FiniteMap, addListToFM, listToFM, filterFM,
21                           addToFM, lookupFM, fmToList, emptyFM, plusFM )
22 import CoreSyn
23 import PprCore          ( pprCoreExpr, pprCoreAlt )
24 import Literal          ( Literal(..) )
25 import PrimRep          ( PrimRep(..) )
26 import CoreFVs          ( freeVars )
27 import Type             ( typePrimRep )
28 import DataCon          ( DataCon, dataConTag, fIRST_TAG, dataConTyCon, 
29                           dataConRepArgTys )
30 import TyCon            ( TyCon, tyConFamilySize, isDataTyCon, tyConDataCons )
31 import Class            ( Class, classTyCon )
32 import Util             ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem, global )
33 import Var              ( isTyVar )
34 import VarSet           ( VarSet, varSetElems )
35 import PrimRep          ( getPrimRepSize, isFollowableRep )
36 import Constants        ( wORD_SIZE )
37 import CmdLineOpts      ( DynFlags, DynFlag(..) )
38 import ErrUtils         ( showPass, dumpIfSet_dyn )
39 import ClosureInfo      ( mkVirtHeapOffsets )
40 import Module           ( ModuleName, moduleName )
41
42 import List             ( intersperse )
43 import Monad            ( foldM )
44 import ST               ( runST )
45 import MArray           ( MArray(..), IOArray, IOUArray, HasBounds(..), freeze, 
46                           mapArray,
47                           castSTUArray, readWord32Array,
48                           newFloatArray, writeFloatArray,
49                           newDoubleArray,  writeDoubleArray,
50                           newIntArray, writeIntArray,
51                           newAddrArray, writeAddrArray )
52 import Foreign          ( Storable(..), Word8, Word16, Word32, Ptr(..), 
53                           malloc, castPtr, plusPtr )
54 import Addr             ( Addr, addrToInt, nullAddr )
55 import Bits             ( Bits(..), shiftR )
56
57 import PrelGHC          ( BCO#, newBCO#, unsafeCoerce#, ByteArray#, Array# )
58 import IOExts           ( IORef, readIORef, writeIORef, fixIO )
59 import ArrayBase        
60 import PrelArr          ( Array(..) )
61 import PrelIOBase       ( IO(..) )
62
63 \end{code}
64
65 %************************************************************************
66 %*                                                                      *
67 \subsection{Functions visible from outside this module.}
68 %*                                                                      *
69 %************************************************************************
70
71 \begin{code}
72
73 byteCodeGen :: DynFlags
74             -> [CoreBind] 
75             -> [TyCon] -> [Class]
76             -> IO ([UnlinkedBCO], ItblEnv)
77 byteCodeGen dflags binds local_tycons local_classes
78    = do showPass dflags "ByteCodeGen"
79         let tycs = local_tycons ++ map classTyCon local_classes
80         itblenv <- mkITbls tycs
81
82         let flatBinds = concatMap getBind binds
83             getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
84             getBind (Rec binds)       = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
85             final_state = runBc (BcM_State [] 0) 
86                                 (mapBc schemeR flatBinds `thenBc_` returnBc ())
87             (BcM_State proto_bcos final_ctr) = final_state
88
89         dumpIfSet_dyn dflags Opt_D_dump_BCOs
90            "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
91
92         bcos <- mapM assembleBCO proto_bcos
93
94         return (bcos, itblenv)
95         
96
97 -- Returns: (the root BCO for this expression, 
98 --           a list of auxilary BCOs resulting from compiling closures)
99 coreExprToBCOs :: DynFlags
100                -> CoreExpr
101                -> IO UnlinkedBCOExpr
102 coreExprToBCOs dflags expr
103  = do showPass dflags "ByteCodeGen"
104       let invented_id = panic "invented_id" :: Id
105           (BcM_State all_proto_bcos final_ctr) 
106              = runBc (BcM_State [] 0) 
107                      (schemeR (invented_id, freeVars expr))
108       dumpIfSet_dyn dflags Opt_D_dump_InterpSyn
109          "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
110
111       let invented_name = getName invented_id
112       let root_proto_bco 
113              = case filter ((== invented_name).nameOfProtoBCO) all_proto_bcos of
114                   [root_bco] -> root_bco
115           auxiliary_proto_bcos
116              = filter ((/= invented_name).nameOfProtoBCO) all_proto_bcos
117
118       auxiliary_bcos <- mapM assembleBCO auxiliary_proto_bcos
119       root_bco <- assembleBCO root_proto_bco
120
121       return (root_bco, auxiliary_bcos)
122
123
124
125
126 data UnlinkedBCO
127    = UnlinkedBCO Name
128                  Int (IOUArray Int Word16)      -- insns
129                  Int (IOUArray Int Word32)      -- literals
130                  Int (IOArray Int Name)         -- ptrs
131                  Int (IOArray Int Name)         -- itbl refs
132
133 nameOfUnlinkedBCO (UnlinkedBCO nm _ _ _ _ _ _ _ _) = nm
134
135 -- When translating expressions, we need to distinguish the root
136 -- BCO for the expression
137 type UnlinkedBCOExpr = (UnlinkedBCO, [UnlinkedBCO])
138
139 instance Outputable UnlinkedBCO where
140    ppr (UnlinkedBCO nm n_insns insns n_lits lits n_ptrs ptrs n_itbls itbls)
141       = sep [text "BCO", ppr nm, text "with", 
142              int n_insns, text "insns",
143              int n_lits, text "lits",
144              int n_ptrs, text "ptrs",
145              int n_itbls, text "itbls"]
146
147
148 -- these need a proper home
149 type ItblEnv    = FiniteMap Name (Ptr StgInfoTable)
150 type ClosureEnv = FiniteMap Name HValue
151 data HValue     = HValue  -- dummy type, actually a pointer to some Real Code.
152
153 -- remove all entries for a given set of modules from the environment
154 filterNameMap :: [ModuleName] -> FiniteMap Name a -> FiniteMap Name a
155 filterNameMap mods env 
156    = filterFM (\n _ -> moduleName (nameModule n) `notElem` mods) env
157 \end{code}
158
159 %************************************************************************
160 %*                                                                      *
161 \subsection{Bytecodes, and Outputery.}
162 %*                                                                      *
163 %************************************************************************
164
165 \begin{code}
166
167 type LocalLabel = Int
168
169 data UnboxedLit = UnboxedI Int | UnboxedF Float | UnboxedD Double
170
171 data BCInstr
172    -- Messing with the stack
173    = ARGCHECK  Int
174    -- Push locals (existing bits of the stack)
175    | PUSH_L    Int{-offset-}
176    | PUSH_LL   Int Int{-2 offsets-}
177    | PUSH_LLL  Int Int Int{-3 offsets-}
178    -- Push a ptr
179    | PUSH_G    Name
180    -- Push an alt continuation
181    | PUSH_AS   Name PrimRep     -- push alts and BCO_ptr_ret_info
182                                 -- PrimRep so we know which itbl
183    -- Pushing literals
184    | PUSH_UBX  Literal  Int 
185                         -- push this int/float/double, NO TAG, on the stack
186                         -- Int is # of items in literal pool to push
187    | PUSH_TAG  Int      -- push this tag on the stack
188
189    | SLIDE     Int{-this many-} Int{-down by this much-}
190    -- To do with the heap
191    | ALLOC     Int      -- make an AP_UPD with this many payload words, zeroed
192    | MKAP      Int{-ptr to AP_UPD is this far down stack-} Int{-# words-}
193    | UNPACK    Int      -- unpack N ptr words from t.o.s Constr
194    | UPK_TAG   Int Int Int
195                         -- unpack N non-ptr words from offset M in constructor
196                         -- K words down the stack
197    | PACK      DataCon Int
198                         -- after assembly, the DataCon is an index into the
199                         -- itbl array
200    -- For doing case trees
201    | LABEL     LocalLabel
202    | TESTLT_I  Int    LocalLabel
203    | TESTEQ_I  Int    LocalLabel
204    | TESTLT_F  Float  LocalLabel
205    | TESTEQ_F  Float  LocalLabel
206    | TESTLT_D  Double LocalLabel
207    | TESTEQ_D  Double LocalLabel
208    | TESTLT_P  Int    LocalLabel
209    | TESTEQ_P  Int    LocalLabel
210    | CASEFAIL
211    -- To Infinity And Beyond
212    | ENTER
213    | RETURN     -- unboxed value on TOS.  Use tag to find underlying ret itbl
214                 -- and return as per that.
215
216
217 instance Outputable BCInstr where
218    ppr (ARGCHECK n)          = text "ARGCHECK" <+> int n
219    ppr (PUSH_L offset)       = text "PUSH_L  " <+> int offset
220    ppr (PUSH_LL o1 o2)       = text "PUSH_LL " <+> int o1 <+> int o2
221    ppr (PUSH_LLL o1 o2 o3)   = text "PUSH_LLL" <+> int o1 <+> int o2 <+> int o3
222    ppr (PUSH_G nm)           = text "PUSH_G  " <+> ppr nm
223    ppr (PUSH_AS nm pk)       = text "PUSH_AS " <+> ppr nm <+> ppr pk
224    ppr (SLIDE n d)           = text "SLIDE   " <+> int n <+> int d
225    ppr (ALLOC sz)            = text "ALLOC   " <+> int sz
226    ppr (MKAP offset sz)      = text "MKAP    " <+> int offset <+> int sz
227    ppr (UNPACK sz)           = text "UNPACK  " <+> int sz
228    ppr (PACK dcon sz)        = text "PACK    " <+> ppr dcon <+> ppr sz
229    ppr (LABEL     lab)       = text "__"       <> int lab <> colon
230    ppr (TESTLT_I  i lab)     = text "TESTLT_I" <+> int i <+> text "__" <> int lab
231    ppr (TESTEQ_I  i lab)     = text "TESTEQ_I" <+> int i <+> text "__" <> int lab
232    ppr (TESTLT_F  f lab)     = text "TESTLT_F" <+> float f <+> text "__" <> int lab
233    ppr (TESTEQ_F  f lab)     = text "TESTEQ_F" <+> float f <+> text "__" <> int lab
234    ppr (TESTLT_D  d lab)     = text "TESTLT_D" <+> double d <+> text "__" <> int lab
235    ppr (TESTEQ_D  d lab)     = text "TESTEQ_D" <+> double d <+> text "__" <> int lab
236    ppr (TESTLT_P  i lab)     = text "TESTLT_P" <+> int i <+> text "__" <> int lab
237    ppr (TESTEQ_P  i lab)     = text "TESTEQ_P" <+> int i <+> text "__" <> int lab
238    ppr CASEFAIL              = text "CASEFAIL"
239    ppr ENTER                 = text "ENTER"
240    ppr RETURN                = text "RETURN"
241
242 pprAltCode discrs_n_codes
243    = vcat (map f discrs_n_codes)
244      where f (discr, code) = ppr discr <> colon <+> vcat (map ppr (fromOL code))
245
246 instance Outputable a => Outputable (ProtoBCO a) where
247    ppr (ProtoBCO name instrs origin)
248       = (text "ProtoBCO" <+> ppr name <> colon)
249         $$ nest 6 (vcat (map ppr instrs))
250         $$ case origin of
251               Left alts -> vcat (map (pprCoreAlt.deAnnAlt) alts)
252               Right rhs -> pprCoreExpr (deAnnotate rhs)
253 \end{code}
254
255 %************************************************************************
256 %*                                                                      *
257 \subsection{Compilation schema for the bytecode generator.}
258 %*                                                                      *
259 %************************************************************************
260
261 \begin{code}
262
263 type BCInstrList = OrdList BCInstr
264
265 data ProtoBCO a 
266    = ProtoBCO a                         -- name, in some sense
267               [BCInstr]                 -- instrs
268                                         -- what the BCO came from
269               (Either [AnnAlt Id VarSet]
270                       (AnnExpr Id VarSet))
271
272 nameOfProtoBCO (ProtoBCO nm insns origin) = nm
273
274
275 type Sequel = Int       -- back off to this depth before ENTER
276
277 -- Maps Ids to the offset from the stack _base_ so we don't have
278 -- to mess with it after each push/pop.
279 type BCEnv = FiniteMap Id Int   -- To find vars on the stack
280
281
282 -- Create a BCO and do a spot of peephole optimisation on the insns
283 -- at the same time.
284 mkProtoBCO nm instrs_ordlist origin
285    = ProtoBCO nm (peep (fromOL instrs_ordlist)) origin
286      where
287         peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
288            = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
289         peep (PUSH_L off1 : PUSH_L off2 : rest)
290            = PUSH_LL off1 off2 : peep rest
291         peep (i:rest)
292            = i : peep rest
293         peep []
294            = []
295
296
297 -- Compile code for the right hand side of a let binding.
298 -- Park the resulting BCO in the monad.  Also requires the
299 -- variable to which this value was bound, so as to give the
300 -- resulting BCO a name.
301 schemeR :: (Id, AnnExpr Id VarSet) -> BcM ()
302 schemeR (nm, rhs) = schemeR_wrk rhs nm (collect [] rhs)
303
304 collect xs (_, AnnLam x e) 
305    = collect (if isTyVar x then xs else (x:xs)) e
306 collect xs not_lambda
307    = (reverse xs, not_lambda)
308
309 schemeR_wrk original_body nm (args, body)
310    = let fvs       = filter (not.isTyVar) (varSetElems (fst original_body))
311          all_args  = fvs ++ reverse args
312          szsw_args = map taggedIdSizeW all_args
313          szw_args  = sum szsw_args
314          p_init    = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
315          argcheck  = if null args then nilOL else unitOL (ARGCHECK szw_args)
316      in
317      schemeE szw_args 0 p_init body             `thenBc` \ body_code ->
318      emitBc (mkProtoBCO (getName nm) (appOL argcheck body_code) (Right original_body))
319
320 -- Let szsw be the sizes in words of some items pushed onto the stack,
321 -- which has initial depth d'.  Return the values which the stack environment
322 -- should map these items to.
323 mkStackOffsets :: Int -> [Int] -> [Int]
324 mkStackOffsets original_depth szsw
325    = map (subtract 1) (tail (scanl (+) original_depth szsw))
326
327 -- Compile code to apply the given expression to the remaining args
328 -- on the stack, returning a HNF.
329 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr Id VarSet -> BcM BCInstrList
330
331 -- Delegate tail-calls to schemeT.
332 schemeE d s p e@(fvs, AnnApp f a) 
333    = returnBc (schemeT (should_args_be_tagged e) d s 0 p (fvs, AnnApp f a))
334 schemeE d s p e@(fvs, AnnVar v)
335    | isFollowableRep (typePrimRep (idType v))
336    = returnBc (schemeT (should_args_be_tagged e) d s 0 p (fvs, AnnVar v))
337    | otherwise
338    = -- returning an unboxed value.  Heave it on the stack, SLIDE, and RETURN.
339      let (push, szw) = pushAtom True d p (AnnVar v)
340      in  returnBc (push                         -- value onto stack
341                    `snocOL` SLIDE szw (d-s)     -- clear to sequel
342                    `snocOL` RETURN)             -- go
343
344 schemeE d s p (fvs, AnnLit literal)
345    = let (push, szw) = pushAtom True d p (AnnLit literal)
346      in  returnBc (push                         -- value onto stack
347                    `snocOL` SLIDE szw (d-s)     -- clear to sequel
348                    `snocOL` RETURN)             -- go
349
350 schemeE d s p (fvs, AnnLet binds b)
351    = let (xs,rhss) = case binds of AnnNonRec x rhs  -> ([x],[rhs])
352                                    AnnRec xs_n_rhss -> unzip xs_n_rhss
353          n     = length xs
354          fvss  = map (filter (not.isTyVar).varSetElems.fst) rhss
355          sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
356
357          -- This p', d' defn is safe because all the items being pushed
358          -- are ptrs, so all have size 1.  d' and p' reflect the stack
359          -- after the closures have been allocated in the heap (but not
360          -- filled in), and pointers to them parked on the stack.
361          p'    = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n 1)))
362          d'    = d + n
363
364          infos = zipE4 fvss sizes xs [n, n-1 .. 1]
365          zipE  = zipEqual "schemeE"
366          zipE4 = zipWith4Equal "schemeE" (\a b c d -> (a,b,c,d))
367
368          -- ToDo: don't build thunks for things with no free variables
369          buildThunk dd ([], size, id, off)
370             = PUSH_G (getName id) 
371               `consOL` unitOL (MKAP (off+size-1) size)
372          buildThunk dd ((fv:fvs), size, id, off)
373             = case pushAtom True dd p' (AnnVar fv) of
374                  (push_code, pushed_szw)
375                     -> push_code `appOL`
376                        buildThunk (dd+pushed_szw) (fvs, size, id, off)
377
378          thunkCode = concatOL (map (buildThunk d') infos)
379          allocCode = toOL (map ALLOC sizes)
380      in
381      schemeE d' s p' b                                  `thenBc`  \ bodyCode ->
382      mapBc schemeR (zip xs rhss)                        `thenBc_`
383      returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
384
385
386 schemeE d s p (fvs, AnnCase scrut bndr alts)
387    = let
388         -- Top of stack is the return itbl, as usual.
389         -- underneath it is the pointer to the alt_code BCO.
390         -- When an alt is entered, it assumes the returned value is
391         -- on top of the itbl.
392         ret_frame_sizeW = 2
393
394         -- Env and depth in which to compile the alts, not including
395         -- any vars bound by the alts themselves
396         d' = d + ret_frame_sizeW + taggedIdSizeW bndr
397         p' = addToFM p bndr (d' - 1)
398
399         scrut_primrep = typePrimRep (idType bndr)
400         isAlgCase
401            = case scrut_primrep of
402                 IntRep -> False ; FloatRep -> False ; DoubleRep -> False
403                 PtrRep -> True
404                 other  -> pprPanic "ByteCodeGen.schemeE" (ppr other)
405
406         -- given an alt, return a discr and code for it.
407         codeAlt alt@(discr, binds_f, rhs)
408            | isAlgCase 
409            = let binds_r      = reverse binds_f
410                  binds_r_szsw = map untaggedIdSizeW binds_r
411                  binds_szw    = sum binds_r_szsw
412                  p''          = addListToFM 
413                                    p' (zip binds_r (mkStackOffsets d' binds_r_szsw))
414                  d''          = d' + binds_szw
415                  unpack_code  = mkUnpackCode 0 0 (map (typePrimRep.idType) binds_f)
416              in schemeE d'' s p'' rhs   `thenBc` \ rhs_code -> 
417                 returnBc (my_discr alt, unpack_code `appOL` rhs_code)
418            | otherwise 
419            = ASSERT(null binds_f) 
420              schemeE d' s p' rhs        `thenBc` \ rhs_code ->
421              returnBc (my_discr alt, rhs_code)
422
423         my_discr (DEFAULT, binds, rhs)  = NoDiscr
424         my_discr (DataAlt dc, binds, rhs) = DiscrP (dataConTag dc)
425         my_discr (LitAlt l, binds, rhs)
426            = case l of MachInt i     -> DiscrI (fromInteger i)
427                        MachFloat r   -> DiscrF (fromRational r)
428                        MachDouble r  -> DiscrD (fromRational r)
429
430         maybe_ncons 
431            | not isAlgCase = Nothing
432            | otherwise 
433            = case [dc | (DataAlt dc, _, _) <- alts] of
434                 []     -> Nothing
435                 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
436
437      in 
438      mapBc codeAlt alts                                 `thenBc` \ alt_stuff ->
439      mkMultiBranch maybe_ncons alt_stuff                `thenBc` \ alt_final ->
440      let 
441          alt_bco_name = getName bndr
442          alt_bco      = mkProtoBCO alt_bco_name alt_final (Left alts)
443      in
444      schemeE (d + ret_frame_sizeW) 
445              (d + ret_frame_sizeW) p scrut              `thenBc` \ scrut_code ->
446
447      emitBc alt_bco                                     `thenBc_`
448      returnBc (PUSH_AS alt_bco_name scrut_primrep `consOL` scrut_code)
449
450
451 schemeE d s p (fvs, AnnNote note body)
452    = schemeE d s p body
453
454 schemeE d s p other
455    = pprPanic "ByteCodeGen.schemeE: unhandled case" 
456                (pprCoreExpr (deAnnotate other))
457
458
459 -- Compile code to do a tail call.  Doesn't need to be monadic.
460 schemeT :: Bool         -- do tagging?
461         -> Int          -- Stack depth
462         -> Sequel       -- Sequel depth
463         -> Int          -- # arg words so far
464         -> BCEnv        -- stack env
465         -> AnnExpr Id VarSet 
466         -> BCInstrList
467
468 schemeT enTag d s narg_words p (_, AnnApp f a)
469    = case snd a of
470         AnnType _ -> schemeT enTag d s narg_words p f
471         other
472            -> let (push, arg_words) = pushAtom enTag d p (snd a)
473               in push 
474                  `appOL` schemeT enTag (d+arg_words) s (narg_words+arg_words) p f
475
476 schemeT enTag d s narg_words p (_, AnnVar f)
477    | Just con <- isDataConId_maybe f
478    = ASSERT(enTag == False)
479      PACK con narg_words `consOL` (mkSLIDE 1 (d-s-1) `snocOL` ENTER)
480    | otherwise
481    = ASSERT(enTag == True)
482      let (push, arg_words) = pushAtom True d p (AnnVar f)
483      in  push 
484          `appOL`  mkSLIDE (narg_words+arg_words) (d - s - narg_words)
485          `snocOL` ENTER
486
487 mkSLIDE n d 
488    = if d == 0 then nilOL else unitOL (SLIDE n d)
489
490 should_args_be_tagged (_, AnnVar v)
491    = case isDataConId_maybe v of
492         Just dcon -> False; Nothing -> True
493 should_args_be_tagged (_, AnnApp f a)
494    = should_args_be_tagged f
495 should_args_be_tagged (_, other)
496    = panic "should_args_be_tagged: tail call to non-con, non-var"
497
498
499 -- Make code to unpack a constructor onto the stack, adding
500 -- tags for the unboxed bits.  Takes the PrimReps of the constructor's
501 -- arguments, and a travelling offset along both the constructor
502 -- (off_h) and the stack (off_s).
503 mkUnpackCode :: Int -> Int -> [PrimRep] -> BCInstrList
504 mkUnpackCode off_h off_s [] = nilOL
505 mkUnpackCode off_h off_s (r:rs)
506    | isFollowableRep r
507    = let (rs_ptr, rs_nptr) = span isFollowableRep (r:rs)
508          ptrs_szw = sum (map untaggedSizeW rs_ptr) 
509      in  ASSERT(ptrs_szw == length rs_ptr)
510          ASSERT(off_h == 0)
511          ASSERT(off_s == 0)
512          UNPACK ptrs_szw 
513          `consOL` mkUnpackCode (off_h + ptrs_szw) (off_s + ptrs_szw) rs_nptr
514    | otherwise
515    = case r of
516         IntRep    -> approved
517         FloatRep  -> approved
518         DoubleRep -> approved
519      where
520         approved = UPK_TAG usizeW off_h off_s   `consOL` theRest
521         theRest  = mkUnpackCode (off_h + usizeW) (off_s + tsizeW) rs
522         usizeW   = untaggedSizeW r
523         tsizeW   = taggedSizeW r
524
525 -- Push an atom onto the stack, returning suitable code & number of
526 -- stack words used.  Pushes it either tagged or untagged, since 
527 -- pushAtom is used to set up the stack prior to copying into the
528 -- heap for both APs (requiring tags) and constructors (which don't).
529 --
530 -- NB this means NO GC between pushing atoms for a constructor and
531 -- copying them into the heap.  It probably also means that 
532 -- tail calls MUST be of the form atom{atom ... atom} since if the
533 -- expression head was allowed to be arbitrary, there could be GC
534 -- in between pushing the arg atoms and completing the head.
535 -- (not sure; perhaps the allocate/doYouWantToGC interface means this
536 -- isn't a problem; but only if arbitrary graph construction for the
537 -- head doesn't leave this BCO, since GC might happen at the start of
538 -- each BCO (we consult doYouWantToGC there).
539 --
540 -- Blargh.  JRS 001206
541 --
542 -- NB (further) that the env p must map each variable to the highest-
543 -- numbered stack slot for it.  For example, if the stack has depth 4 
544 -- and we tagged-ly push (v :: Int#) on it, the value will be in stack[4],
545 -- the tag in stack[5], the stack will have depth 6, and p must map v to
546 -- 5 and not to 4.  Stack locations are numbered from zero, so a depth
547 -- 6 stack has valid words 0 .. 5.
548
549 pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> (BCInstrList, Int)
550 pushAtom tagged d p (AnnVar v) 
551    = let str = "\npushAtom " ++ showSDocDebug (ppr v) ++ ", depth = " ++ show d
552                ++ ", env =\n" ++ 
553                showSDocDebug (nest 4 (vcat (map ppr (fmToList p))))
554                ++ " -->\n" ++
555                showSDoc (nest 4 (vcat (map ppr (fromOL (fst result)))))
556                ++ "\nendPushAtom " ++ showSDocDebug (ppr v)
557          str' = if str == str then str else str
558
559          result
560             = case lookupBCEnv_maybe p v of
561                  Just d_v -> (toOL (nOfThem nwords (PUSH_L (d-d_v+sz_t-2))), sz_t)
562                  Nothing  -> ASSERT(sz_t == 1) (unitOL (PUSH_G nm), sz_t)
563
564          nm     = getName v
565          sz_t   = taggedIdSizeW v
566          sz_u   = untaggedIdSizeW v
567          nwords = if tagged then sz_t else sz_u
568      in
569          --trace str'
570          result
571
572 pushAtom True d p (AnnLit lit)
573    = let (ubx_code, ubx_size) = pushAtom False d p (AnnLit lit)
574      in  (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
575
576 pushAtom False d p (AnnLit lit)
577    = case lit of
578         MachInt i    -> code IntRep
579         MachFloat r  -> code FloatRep
580         MachDouble r -> code DoubleRep
581      where
582         code rep
583            = let size_host_words = untaggedSizeW rep
584                  size_in_word32s = (size_host_words * wORD_SIZE) `div` 4
585              in (unitOL (PUSH_UBX lit size_in_word32s), size_host_words)
586
587 pushAtom tagged d p (AnnApp f (_, AnnType _))
588    = pushAtom tagged d p (snd f)
589
590 pushAtom tagged d p other
591    = pprPanic "ByteCodeGen.pushAtom" 
592               (pprCoreExpr (deAnnotate (undefined, other)))
593
594
595 -- Given a bunch of alts code and their discrs, do the donkey work
596 -- of making a multiway branch using a switch tree.
597 -- What a load of hassle!
598 mkMultiBranch :: Maybe Int      -- # datacons in tycon, if alg alt
599                                 -- a hint; generates better code
600                                 -- Nothing is always safe
601               -> [(Discr, BCInstrList)] 
602               -> BcM BCInstrList
603 mkMultiBranch maybe_ncons raw_ways
604    = let d_way     = filter (isNoDiscr.fst) raw_ways
605          notd_ways = naturalMergeSortLe 
606                         (\w1 w2 -> leAlt (fst w1) (fst w2))
607                         (filter (not.isNoDiscr.fst) raw_ways)
608
609          mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
610          mkTree [] range_lo range_hi = returnBc the_default
611
612          mkTree [val] range_lo range_hi
613             | range_lo `eqAlt` range_hi 
614             = returnBc (snd val)
615             | otherwise
616             = getLabelBc                                `thenBc` \ label_neq ->
617               returnBc (mkTestEQ (fst val) label_neq 
618                         `consOL` (snd val
619                         `appOL`   unitOL (LABEL label_neq)
620                         `appOL`   the_default))
621
622          mkTree vals range_lo range_hi
623             = let n = length vals `div` 2
624                   vals_lo = take n vals
625                   vals_hi = drop n vals
626                   v_mid = fst (head vals_hi)
627               in
628               getLabelBc                                `thenBc` \ label_geq ->
629               mkTree vals_lo range_lo (dec v_mid)       `thenBc` \ code_lo ->
630               mkTree vals_hi v_mid range_hi             `thenBc` \ code_hi ->
631               returnBc (mkTestLT v_mid label_geq
632                         `consOL` (code_lo
633                         `appOL`   unitOL (LABEL label_geq)
634                         `appOL`   code_hi))
635  
636          the_default 
637             = case d_way of [] -> unitOL CASEFAIL
638                             [(_, def)] -> def
639
640          -- None of these will be needed if there are no non-default alts
641          (mkTestLT, mkTestEQ, init_lo, init_hi)
642             | null notd_ways
643             = panic "mkMultiBranch: awesome foursome"
644             | otherwise
645             = case fst (head notd_ways) of {
646               DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
647                             \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
648                             DiscrI minBound,
649                             DiscrI maxBound );
650               DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
651                             \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
652                             DiscrF minF,
653                             DiscrF maxF );
654               DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
655                             \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
656                             DiscrD minD,
657                             DiscrD maxD );
658               DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
659                             \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
660                             DiscrP algMinBound,
661                             DiscrP algMaxBound )
662               }
663
664          (algMinBound, algMaxBound)
665             = case maybe_ncons of
666                  Just n  -> (fIRST_TAG, fIRST_TAG + n - 1)
667                  Nothing -> (minBound, maxBound)
668
669          (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
670          (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
671          (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
672          (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
673          NoDiscr     `eqAlt` NoDiscr     = True
674          _           `eqAlt` _           = False
675
676          (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
677          (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
678          (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
679          (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
680          NoDiscr     `leAlt` NoDiscr     = True
681          _           `leAlt` _           = False
682
683          isNoDiscr NoDiscr = True
684          isNoDiscr _       = False
685
686          dec (DiscrI i) = DiscrI (i-1)
687          dec (DiscrP i) = DiscrP (i-1)
688          dec other      = other         -- not really right, but if you
689                 -- do cases on floating values, you'll get what you deserve
690
691          -- same snotty comment applies to the following
692          minF, maxF :: Float
693          minD, maxD :: Double
694          minF = -1.0e37
695          maxF =  1.0e37
696          minD = -1.0e308
697          maxD =  1.0e308
698      in
699          mkTree notd_ways init_lo init_hi
700
701 \end{code}
702
703 %************************************************************************
704 %*                                                                      *
705 \subsection{Supporting junk for the compilation schemes}
706 %*                                                                      *
707 %************************************************************************
708
709 \begin{code}
710
711 -- Describes case alts
712 data Discr 
713    = DiscrI Int
714    | DiscrF Float
715    | DiscrD Double
716    | DiscrP Int
717    | NoDiscr
718
719 instance Outputable Discr where
720    ppr (DiscrI i) = int i
721    ppr (DiscrF f) = text (show f)
722    ppr (DiscrD d) = text (show d)
723    ppr (DiscrP i) = int i
724    ppr NoDiscr    = text "DEF"
725
726
727 -- Find things in the BCEnv (the what's-on-the-stack-env)
728 -- See comment preceding pushAtom for precise meaning of env contents
729 lookupBCEnv :: BCEnv -> Id -> Int
730 lookupBCEnv env nm
731    = case lookupFM env nm of
732         Nothing -> pprPanic "lookupBCEnv" 
733                             (ppr nm $$ char ' ' $$ vcat (map ppr (fmToList env)))
734         Just xx -> xx
735
736 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
737 lookupBCEnv_maybe = lookupFM
738
739
740 -- When I push one of these on the stack, how much does Sp move by?
741 taggedSizeW :: PrimRep -> Int
742 taggedSizeW pr
743    | isFollowableRep pr = 1
744    | otherwise          = 1{-the tag-} + getPrimRepSize pr
745
746
747 -- The plain size of something, without tag.
748 untaggedSizeW :: PrimRep -> Int
749 untaggedSizeW pr
750    | isFollowableRep pr = 1
751    | otherwise          = getPrimRepSize pr
752
753
754 taggedIdSizeW, untaggedIdSizeW :: Id -> Int
755 taggedIdSizeW   = taggedSizeW   . typePrimRep . idType
756 untaggedIdSizeW = untaggedSizeW . typePrimRep . idType
757
758 \end{code}
759
760 %************************************************************************
761 %*                                                                      *
762 \subsection{The bytecode generator's monad}
763 %*                                                                      *
764 %************************************************************************
765
766 \begin{code}
767 data BcM_State 
768    = BcM_State { bcos      :: [ProtoBCO Name],  -- accumulates completed BCOs
769                  nextlabel :: Int }             -- for generating local labels
770
771 type BcM result = BcM_State -> (result, BcM_State)
772
773 mkBcM_State :: [ProtoBCO Name] -> Int -> BcM_State
774 mkBcM_State = BcM_State
775
776 runBc :: BcM_State -> BcM () -> BcM_State
777 runBc init_st m = case m init_st of { (r,st) -> st }
778
779 thenBc :: BcM a -> (a -> BcM b) -> BcM b
780 thenBc expr cont st
781   = case expr st of { (result, st') -> cont result st' }
782
783 thenBc_ :: BcM a -> BcM b -> BcM b
784 thenBc_ expr cont st
785   = case expr st of { (result, st') -> cont st' }
786
787 returnBc :: a -> BcM a
788 returnBc result st = (result, st)
789
790 mapBc :: (a -> BcM b) -> [a] -> BcM [b]
791 mapBc f []     = returnBc []
792 mapBc f (x:xs)
793   = f x          `thenBc` \ r  ->
794     mapBc f xs   `thenBc` \ rs ->
795     returnBc (r:rs)
796
797 emitBc :: ProtoBCO Name -> BcM ()
798 emitBc bco st
799    = ((), st{bcos = bco : bcos st})
800
801 getLabelBc :: BcM Int
802 getLabelBc st
803    = (nextlabel st, st{nextlabel = 1 + nextlabel st})
804
805 \end{code}
806
807 %************************************************************************
808 %*                                                                      *
809 \subsection{The bytecode assembler}
810 %*                                                                      *
811 %************************************************************************
812
813 The object format for bytecodes is: 16 bits for the opcode, and 16 for
814 each field -- so the code can be considered a sequence of 16-bit ints.
815 Each field denotes either a stack offset or number of items on the
816 stack (eg SLIDE), and index into the pointer table (eg PUSH_G), an
817 index into the literal table (eg PUSH_I/D/L), or a bytecode address in
818 this BCO.
819
820 \begin{code}
821 -- Top level assembler fn.
822 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
823
824 assembleBCO (ProtoBCO nm instrs origin)
825    = let
826          -- pass 1: collect up the offsets of the local labels
827          label_env = mkLabelEnv emptyFM 0 instrs
828
829          mkLabelEnv env i_offset [] = env
830          mkLabelEnv env i_offset (i:is)
831             = let new_env 
832                      = case i of LABEL n -> addToFM env n i_offset ; _ -> env
833               in  mkLabelEnv new_env (i_offset + instrSizeB i) is
834
835          findLabel lab
836             = case lookupFM label_env lab of
837                  Just bco_offset -> bco_offset
838                  Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
839
840          init_n_insns = 10
841          init_n_lits  = 4
842          init_n_ptrs  = 4
843          init_n_itbls = 4
844      in
845      do  insns <- newXIOUArray init_n_insns :: IO (XIOUArray Word16)
846          lits  <- newXIOUArray init_n_lits  :: IO (XIOUArray Word32)
847          ptrs  <- newXIOArray  init_n_ptrs  -- :: IO (XIOArray Name)
848          itbls <- newXIOArray  init_n_itbls -- :: IO (XIOArray Name)
849
850          -- pass 2: generate the instruction, ptr and nonptr bits
851          let init_asm_state = (insns,lits,ptrs,itbls)
852          final_asm_state <- mkBits findLabel init_asm_state instrs         
853
854          -- unwrap the expandable arrays
855          let final_insns = stuffXIOU insns
856              final_lits  = stuffXIOU lits
857              final_ptrs  = stuffXIO  ptrs
858              final_itbls = stuffXIO  itbls
859
860          return (UnlinkedBCO nm
861                              (usedXIOU insns) final_insns 
862                              (usedXIOU lits)  final_lits 
863                              (usedXIO  ptrs)  final_ptrs 
864                              (usedXIO  itbls) final_itbls)
865
866
867 -- instrs nonptrs ptrs itbls
868 type AsmState = (XIOUArray Word16, XIOUArray Word32, XIOArray Name, XIOArray Name)
869
870
871 -- This is where all the action is (pass 2 of the assembler)
872 mkBits :: (Int -> Int)                  -- label finder
873        -> AsmState
874        -> [BCInstr]                     -- instructions (in)
875        -> IO AsmState
876
877 mkBits findLabel st proto_insns
878   = foldM doInstr st proto_insns
879     where
880        doInstr :: AsmState -> BCInstr -> IO AsmState
881        doInstr st i
882           = case i of
883                ARGCHECK  n        -> instr2 st i_ARGCHECK n
884                PUSH_L    o1       -> instr2 st i_PUSH_L o1
885                PUSH_LL   o1 o2    -> instr3 st i_PUSH_LL o1 o2
886                PUSH_LLL  o1 o2 o3 -> instr4 st i_PUSH_LLL o1 o2 o3
887                PUSH_G    nm       -> do (p, st2) <- ptr st nm
888                                         instr2 st2 i_PUSH_G p
889                PUSH_AS   nm pk    -> do (p, st2)  <- ptr st nm
890                                         (np, st3) <- ret_itbl st2 pk
891                                         instr3 st3 i_PUSH_AS p np
892                PUSH_UBX lit nw32s -> do (np, st2) <- literal st lit
893                                         instr3 st2 i_PUSH_UBX np nw32s
894                PUSH_TAG  tag      -> instr2 st i_PUSH_TAG tag
895                SLIDE     n by     -> instr3 st i_SLIDE n by
896                ALLOC     n        -> instr2 st i_ALLOC n
897                MKAP      off sz   -> instr3 st i_MKAP off sz
898                UNPACK    n        -> instr2 st i_UNPACK n
899                UPK_TAG   n m k    -> instr4 st i_UPK_TAG n m k
900                PACK      dcon sz  -> do (itbl_no,st2) <- itbl st dcon
901                                         instr3 st2 i_PACK itbl_no sz
902                LABEL     lab      -> return st
903                TESTLT_I  i l      -> do (np, st2) <- int st i
904                                         instr3 st2 i_TESTLT_I np (findLabel l)
905                TESTEQ_I  i l      -> do (np, st2) <- int st i
906                                         instr3 st2 i_TESTEQ_I np (findLabel l)
907                TESTLT_F  f l      -> do (np, st2) <- float st f
908                                         instr3 st2 i_TESTLT_F np (findLabel l)
909                TESTEQ_F  f l      -> do (np, st2) <- float st f
910                                         instr3 st2 i_TESTEQ_F np (findLabel l)
911                TESTLT_D  d l      -> do (np, st2) <- double st d
912                                         instr3 st2 i_TESTLT_D np (findLabel l)
913                TESTEQ_D  d l      -> do (np, st2) <- double st d
914                                         instr3 st2 i_TESTEQ_D np (findLabel l)
915                TESTLT_P  i l      -> do (np, st2) <- int st i
916                                         instr3 st2 i_TESTLT_P np (findLabel l)
917                TESTEQ_P  i l      -> do (np, st2) <- int st i
918                                         instr3 st2 i_TESTEQ_P np (findLabel l)
919                CASEFAIL           -> instr1 st i_CASEFAIL
920                ENTER              -> instr1 st i_ENTER
921                RETURN             -> instr1 st i_RETURN
922
923        i2s :: Int -> Word16
924        i2s = fromIntegral
925
926        instr1 (st_i0,st_l0,st_p0,st_I0) i1
927           = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
928                return (st_i1,st_l0,st_p0,st_I0)
929
930        instr2 (st_i0,st_l0,st_p0,st_I0) i1 i2
931           = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
932                st_i2 <- addToXIOUArray st_i1 (i2s i2)
933                return (st_i2,st_l0,st_p0,st_I0)
934
935        instr3 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3
936           = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
937                st_i2 <- addToXIOUArray st_i1 (i2s i2)
938                st_i3 <- addToXIOUArray st_i2 (i2s i3)
939                return (st_i3,st_l0,st_p0,st_I0)
940
941        instr4 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3 i4
942           = do st_i1 <- addToXIOUArray st_i0 (i2s i1)
943                st_i2 <- addToXIOUArray st_i1 (i2s i2)
944                st_i3 <- addToXIOUArray st_i2 (i2s i3)
945                st_i4 <- addToXIOUArray st_i3 (i2s i4)
946                return (st_i4,st_l0,st_p0,st_I0)
947
948        float (st_i0,st_l0,st_p0,st_I0) f
949           = do let w32s = mkLitF f
950                st_l1 <- addListToXIOUArray st_l0 w32s
951                return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
952
953        double (st_i0,st_l0,st_p0,st_I0) d
954           = do let w32s = mkLitD d
955                st_l1 <- addListToXIOUArray st_l0 w32s
956                return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
957
958        int (st_i0,st_l0,st_p0,st_I0) i
959           = do let w32s = mkLitI i
960                st_l1 <- addListToXIOUArray st_l0 w32s
961                return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
962
963        addr (st_i0,st_l0,st_p0,st_I0) a
964           = do let w32s = mkLitA a
965                st_l1 <- addListToXIOUArray st_l0 w32s
966                return (usedXIOU st_l0, (st_i0,st_l1,st_p0,st_I0))
967
968        ptr (st_i0,st_l0,st_p0,st_I0) p
969           = do st_p1 <- addToXIOArray st_p0 p
970                return (usedXIO st_p0, (st_i0,st_l0,st_p1,st_I0))
971
972        itbl (st_i0,st_l0,st_p0,st_I0) dcon
973           = do st_I1 <- addToXIOArray st_I0 (getName dcon)
974                return (usedXIO st_I0, (st_i0,st_l0,st_p0,st_I1))
975
976        literal st (MachInt j)    = int st (fromIntegral j)
977        literal st (MachFloat r)  = float st (fromRational r)
978        literal st (MachDouble r) = double st (fromRational r)
979
980        ret_itbl st pk
981           = addr st ret_itbl_addr
982             where
983                ret_itbl_addr 
984                   = case pk of
985                        IntRep    -> stg_ctoi_ret_R1_info
986                        FloatRep  -> stg_ctoi_ret_F1_info
987                        DoubleRep -> stg_ctoi_ret_D1_info
988                     where  -- TEMP HACK
989                        stg_ctoi_ret_F1_info = nullAddr
990                        stg_ctoi_ret_D1_info = nullAddr
991                      
992 foreign label "stg_ctoi_ret_R1_info" stg_ctoi_ret_R1_info :: Addr
993 --foreign label "stg_ctoi_ret_F1_info" stg_ctoi_ret_F1_info :: Addr
994 --foreign label "stg_ctoi_ret_D1_info" stg_ctoi_ret_D1_info :: Addr
995
996 -- The size in bytes of an instruction.
997 instrSizeB :: BCInstr -> Int
998 instrSizeB instr
999    = case instr of
1000         ARGCHECK _     -> 4
1001         PUSH_L   _     -> 4
1002         PUSH_LL  _ _   -> 6
1003         PUSH_LLL _ _ _ -> 8
1004         PUSH_G   _     -> 4
1005         SLIDE    _ _   -> 6
1006         ALLOC    _     -> 4
1007         MKAP     _ _   -> 6
1008         UNPACK   _     -> 4
1009         PACK     _ _   -> 6
1010         LABEL    _     -> 4
1011         TESTLT_I _ _   -> 6
1012         TESTEQ_I _ _   -> 6
1013         TESTLT_F _ _   -> 6
1014         TESTEQ_F _ _   -> 6
1015         TESTLT_D _ _   -> 6
1016         TESTEQ_D _ _   -> 6
1017         TESTLT_P _ _   -> 6
1018         TESTEQ_P _ _   -> 6
1019         CASEFAIL       -> 2
1020         ENTER          -> 2
1021         RETURN         -> 2
1022
1023
1024 -- Sizes of Int, Float and Double literals, in units of 32-bitses
1025 intLitSz32s, floatLitSz32s, doubleLitSz32s, addrLitSz32s :: Int
1026 intLitSz32s    = wORD_SIZE `div` 4
1027 floatLitSz32s  = 1      -- Assume IEEE floats
1028 doubleLitSz32s = 2
1029 addrLitSz32s   = intLitSz32s
1030
1031 -- Make lists of 32-bit words for literals, so that when the
1032 -- words are placed in memory at increasing addresses, the
1033 -- bit pattern is correct for the host's word size and endianness.
1034 mkLitI :: Int    -> [Word32]
1035 mkLitF :: Float  -> [Word32]
1036 mkLitD :: Double -> [Word32]
1037 mkLitA :: Addr   -> [Word32]
1038
1039 mkLitF f
1040    = runST (do
1041         arr <- newFloatArray ((0::Int),0)
1042         writeFloatArray arr 0 f
1043         f_arr <- castSTUArray arr
1044         w0 <- readWord32Array f_arr 0
1045         return [w0]
1046      )
1047
1048 mkLitD d
1049    = runST (do
1050         arr <- newDoubleArray ((0::Int),0)
1051         writeDoubleArray arr 0 d
1052         d_arr <- castSTUArray arr
1053         w0 <- readWord32Array d_arr 0
1054         w1 <- readWord32Array d_arr 1
1055         return [w0,w1]
1056      )
1057
1058 mkLitI i
1059    | wORD_SIZE == 4
1060    = runST (do
1061         arr <- newIntArray ((0::Int),0)
1062         writeIntArray arr 0 i
1063         i_arr <- castSTUArray arr
1064         w0 <- readWord32Array i_arr 0
1065         return [w0]
1066      )
1067    | wORD_SIZE == 8
1068    = runST (do
1069         arr <- newIntArray ((0::Int),0)
1070         writeIntArray arr 0 i
1071         i_arr <- castSTUArray arr
1072         w0 <- readWord32Array i_arr 0
1073         w1 <- readWord32Array i_arr 1
1074         return [w0,w1]
1075      )
1076    
1077 mkLitA a
1078    | wORD_SIZE == 4
1079    = runST (do
1080         arr <- newAddrArray ((0::Int),0)
1081         writeAddrArray arr 0 a
1082         a_arr <- castSTUArray arr
1083         w0 <- readWord32Array a_arr 0
1084         return [w0]
1085      )
1086    | wORD_SIZE == 8
1087    = runST (do
1088         arr <- newAddrArray ((0::Int),0)
1089         writeAddrArray arr 0 a
1090         a_arr <- castSTUArray arr
1091         w0 <- readWord32Array a_arr 0
1092         w1 <- readWord32Array a_arr 1
1093         return [w0,w1]
1094      )
1095    
1096
1097
1098 -- Zero-based expandable arrays
1099 data XIOUArray ele 
1100    = XIOUArray { usedXIOU :: Int, stuffXIOU :: (IOUArray Int ele) }
1101 data XIOArray ele 
1102    = XIOArray { usedXIO :: Int , stuffXIO :: (IOArray Int ele) }
1103
1104 newXIOUArray size
1105    = do arr <- newArray (0, size-1)
1106         return (XIOUArray 0 arr)
1107
1108 addListToXIOUArray xarr []
1109    = return xarr
1110 addListToXIOUArray xarr (x:xs)
1111    = addToXIOUArray xarr x >>= \ xarr' -> addListToXIOUArray xarr' xs
1112
1113
1114 addToXIOUArray :: MArray IOUArray a IO
1115                   => XIOUArray a -> a -> IO (XIOUArray a)
1116 addToXIOUArray (XIOUArray n_arr arr) x
1117    = case bounds arr of
1118         (lo, hi) -> ASSERT(lo == 0)
1119                     if   n_arr > hi
1120                     then do new_arr <- newArray (0, 2*hi-1)
1121                             copy hi arr new_arr
1122                             addToXIOUArray (XIOUArray n_arr new_arr) x
1123                     else do writeArray arr n_arr x
1124                             return (XIOUArray (n_arr+1) arr)
1125      where
1126         copy :: MArray IOUArray a IO
1127                 => Int -> IOUArray Int a -> IOUArray Int a -> IO ()
1128         copy n src dst
1129            | n < 0     = return ()
1130            | otherwise = do nx <- readArray src n
1131                             writeArray dst n nx
1132                             copy (n-1) src dst
1133
1134
1135
1136 newXIOArray size
1137    = do arr <- newArray (0, size-1)
1138         return (XIOArray 0 arr)
1139
1140 addToXIOArray :: XIOArray a -> a -> IO (XIOArray a)
1141 addToXIOArray (XIOArray n_arr arr) x
1142    = case bounds arr of
1143         (lo, hi) -> ASSERT(lo == 0)
1144                     if   n_arr > hi
1145                     then do new_arr <- newArray (0, 2*hi-1)
1146                             copy hi arr new_arr
1147                             addToXIOArray (XIOArray n_arr new_arr) x
1148                     else do writeArray arr n_arr x
1149                             return (XIOArray (n_arr+1) arr)
1150      where
1151         copy :: Int -> IOArray Int a -> IOArray Int a -> IO ()
1152         copy n src dst
1153            | n < 0     = return ()
1154            | otherwise = do nx <- readArray src n
1155                             writeArray dst n nx
1156                             copy (n-1) src dst
1157
1158 \end{code}
1159
1160 %************************************************************************
1161 %*                                                                      *
1162 \subsection{Linking interpretables into something we can run}
1163 %*                                                                      *
1164 %************************************************************************
1165
1166 \begin{code}
1167
1168 {- 
1169 data UnlinkedBCO
1170    = UnlinkedBCO Int (IOUArray Int Word16)      -- #insns insns
1171                  Int (IOUArray Int Word32)      -- #literals literals
1172                  Int (IOArray Int Name)         -- #ptrs ptrs
1173                  Int (IOArray Int Name)         -- #itblrefs itblrefs
1174
1175 data BCO# = BCO# ByteArray#             -- instrs   :: array Word16#
1176                  ByteArray#             -- literals :: array Word32#
1177                  PtrArray#              -- ptrs     :: Array HValue
1178                  ByteArray#             -- itbls    :: Array Addr#
1179 -}
1180
1181 data LinkedBCO = LinkedBCO BCO#
1182
1183
1184
1185 GLOBAL_VAR(v_cafTable, [], [HValue])
1186
1187 addCAF :: HValue -> IO ()
1188 addCAF x = do xs <- readIORef v_cafTable; writeIORef v_cafTable (x:xs)
1189
1190 bcosToHValue :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr -> IO HValue
1191 bcosToHValue ie ce (root_bco, other_bcos)
1192    = do linked_expr <- linkIExpr ie ce (root_bco, other_bcos)
1193         return linked_expr
1194
1195
1196 linkIModules :: ItblEnv    -- incoming global itbl env; returned updated
1197              -> ClosureEnv -- incoming global closure env; returned updated
1198              -> [([UnlinkedBCO], ItblEnv)]
1199              -> IO ([HValue], ItblEnv, ClosureEnv)
1200 linkIModules gie gce mods = do
1201   let (bcoss, ies) = unzip mods
1202       bcos = concat bcoss
1203       top_level_binders = map nameOfUnlinkedBCO bcos
1204       final_gie = foldr plusFM gie ies
1205   
1206   (new_bcos, new_gce) <-
1207     fixIO (\ ~(new_bcos, new_gce) -> do
1208       new_bcos <- linkBCOs final_gie new_gce bcos
1209       let new_gce = addListToFM gce (zip top_level_binders new_bcos)
1210       return (new_bcos, new_gce))
1211
1212   return (new_bcos, final_gie, new_gce)
1213
1214
1215 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr
1216           -> IO HValue    -- IO BCO# really
1217 linkIExpr ie ce (root_ul_bco, aux_ul_bcos)
1218    = do let aux_ul_binders = map nameOfUnlinkedBCO aux_ul_bcos
1219         (aux_bcos, aux_ce) 
1220            <- fixIO 
1221                 (\ ~(aux_bcos, new_ce) 
1222                  -> do new_bcos <- linkBCOs ie new_ce aux_ul_bcos
1223                        let new_ce = addListToFM ce (zip aux_ul_binders new_bcos)
1224                        return (new_bcos, new_ce)
1225                 )
1226         [root_bco]
1227            <- linkBCOs ie aux_ce [root_ul_bco]
1228         return root_bco
1229
1230
1231 linkBCOs :: ItblEnv -> ClosureEnv -> [UnlinkedBCO] 
1232          -> IO [HValue]   -- IO [BCO#] really
1233 linkBCOs ie ce binds = mapM (linkBCO ie ce) binds
1234
1235 linkBCO ie ce (UnlinkedBCO nm 
1236                            n_insns insns n_literals literals 
1237                            n_ptrs ptrs n_itbls itbls)
1238    = do linked_ptrs <- mapArray (lookupCE ce) ptrs
1239         linked_itbls <- mapArray (lookupIE ie) itbls
1240
1241         ptrs_froz <- freeze linked_ptrs
1242         let ptrs_parr = case ptrs_froz of Array lo hi parr -> parr
1243
1244         insns_froz <- freeze insns
1245         let insns_barr = case insns_froz of UArray lo hi barr -> barr
1246
1247         literals_froz <- freeze literals
1248         let literals_barr = case literals_froz of UArray lo hi barr -> barr
1249
1250         itbls_froz <- freeze linked_itbls
1251         let itbls_barr = case itbls_froz of UArray lo hi barr -> barr
1252
1253         BCO bco# <- newBCO insns_barr literals_barr ptrs_parr itbls_barr
1254      
1255         return (unsafeCoerce# bco#)
1256
1257 data BCO = BCO BCO#
1258
1259 newBCO :: ByteArray# -> ByteArray# -> Array# a -> ByteArray# -> IO BCO
1260 newBCO a b c d = IO (\s -> case newBCO# a b c d s of (# s1, bco #) -> (# s1, BCO bco #))
1261
1262
1263 lookupCE :: ClosureEnv -> Name -> HValue
1264 lookupCE ce nm 
1265    = case lookupFM ce nm of
1266         Just aa -> unsafeCoerce# aa
1267         Nothing -> pprPanic "ByteCodeGen.lookupCE" (ppr nm)
1268
1269 lookupIE :: ItblEnv -> Name -> Addr
1270 lookupIE ie nm 
1271    = case lookupFM ie nm of
1272         Just (Ptr a) -> a
1273         Nothing      -> pprPanic "ByteCodeGen.lookupIE" (ppr nm)
1274
1275
1276
1277 {-
1278 lookupCon ie con = 
1279   case lookupFM ie con of
1280     Just (Ptr addr) -> return addr
1281     Nothing   -> do
1282         -- try looking up in the object files.
1283         m <- lookupSymbol (nameToCLabel con "con_info")
1284         case m of
1285             Just addr -> return addr
1286             Nothing   -> pprPanic "linkIExpr" (ppr con)
1287
1288 -- nullary constructors don't have normal _con_info tables.
1289 lookupNullaryCon ie con =
1290   case lookupFM ie con of
1291     Just (Ptr addr) -> return (ConApp addr)
1292     Nothing -> do
1293         -- try looking up in the object files.
1294         m <- lookupSymbol (nameToCLabel con "closure")
1295         case m of
1296             Just (A# addr) -> return (Native (unsafeCoerce# addr))
1297             Nothing   -> pprPanic "lookupNullaryCon" (ppr con)
1298
1299
1300 lookupNative ce var =
1301   unsafeInterleaveIO (do
1302       case lookupFM ce var of
1303         Just e  -> return (Native e)
1304         Nothing -> do
1305             -- try looking up in the object files.
1306             let lbl = (nameToCLabel var "closure")
1307             m <- lookupSymbol lbl
1308             case m of
1309                 Just (A# addr)
1310                     -> do addCAF (unsafeCoerce# addr)
1311                           return (Native (unsafeCoerce# addr))
1312                 Nothing   -> pprPanic "linkIExpr" (ppr var)
1313   )
1314
1315 -- some VarI/VarP refer to top-level interpreted functions; we change
1316 -- them into Natives here.
1317 lookupVar ce f v =
1318   unsafeInterleaveIO (
1319         case lookupFM ce (getName v) of
1320             Nothing -> return (f v)
1321             Just e  -> return (Native e)
1322   )
1323
1324 -- HACK!!!  ToDo: cleaner
1325 nameToCLabel :: Name -> String{-suffix-} -> String
1326 nameToCLabel n suffix =
1327   _UNPK_(moduleNameFS (rdrNameModule rn)) 
1328   ++ '_':occNameString(rdrNameOcc rn) ++ '_':suffix
1329   where rn = toRdrName n
1330 -}
1331 \end{code}
1332
1333 %************************************************************************
1334 %*                                                                      *
1335 \subsection{Manufacturing of info tables for DataCons}
1336 %*                                                                      *
1337 %************************************************************************
1338
1339 \begin{code}
1340
1341 #if __GLASGOW_HASKELL__ <= 408
1342 type ItblPtr = Addr
1343 #else
1344 type ItblPtr = Ptr StgInfoTable
1345 #endif
1346
1347 -- Make info tables for the data decls in this module
1348 mkITbls :: [TyCon] -> IO ItblEnv
1349 mkITbls [] = return emptyFM
1350 mkITbls (tc:tcs) = do itbls  <- mkITbl tc
1351                       itbls2 <- mkITbls tcs
1352                       return (itbls `plusFM` itbls2)
1353
1354 mkITbl :: TyCon -> IO ItblEnv
1355 mkITbl tc
1356 --   | trace ("TYCON: " ++ showSDoc (ppr tc)) False
1357 --   = error "?!?!"
1358    | not (isDataTyCon tc) 
1359    = return emptyFM
1360    | n == length dcs  -- paranoia; this is an assertion.
1361    = make_constr_itbls dcs
1362      where
1363         dcs = tyConDataCons tc
1364         n   = tyConFamilySize tc
1365
1366 cONSTR :: Int
1367 cONSTR = 1  -- as defined in ghc/includes/ClosureTypes.h
1368
1369 -- Assumes constructors are numbered from zero, not one
1370 make_constr_itbls :: [DataCon] -> IO ItblEnv
1371 make_constr_itbls cons
1372    | length cons <= 8
1373    = do is <- mapM mk_vecret_itbl (zip cons [0..])
1374         return (listToFM is)
1375    | otherwise
1376    = do is <- mapM mk_dirret_itbl (zip cons [0..])
1377         return (listToFM is)
1378      where
1379         mk_vecret_itbl (dcon, conNo)
1380            = mk_itbl dcon conNo (vecret_entry conNo)
1381         mk_dirret_itbl (dcon, conNo)
1382            = mk_itbl dcon conNo stg_interp_constr_entry
1383
1384         mk_itbl :: DataCon -> Int -> Addr -> IO (Name,ItblPtr)
1385         mk_itbl dcon conNo entry_addr
1386            = let (tot_wds, ptr_wds, _) 
1387                     = mkVirtHeapOffsets typePrimRep (dataConRepArgTys dcon)
1388                  ptrs = ptr_wds
1389                  nptrs  = tot_wds - ptr_wds
1390                  itbl  = StgInfoTable {
1391                            ptrs = fromIntegral ptrs, nptrs = fromIntegral nptrs,
1392                            tipe = fromIntegral cONSTR,
1393                            srtlen = fromIntegral conNo,
1394                            code0 = fromIntegral code0, code1 = fromIntegral code1,
1395                            code2 = fromIntegral code2, code3 = fromIntegral code3,
1396                            code4 = fromIntegral code4, code5 = fromIntegral code5,
1397                            code6 = fromIntegral code6, code7 = fromIntegral code7 
1398                         }
1399                  -- Make a piece of code to jump to "entry_label".
1400                  -- This is the only arch-dependent bit.
1401                  -- On x86, if entry_label has an address 0xWWXXYYZZ,
1402                  -- emit   movl $0xWWXXYYZZ,%eax  ;  jmp *%eax
1403                  -- which is
1404                  -- B8 ZZ YY XX WW FF E0
1405                  (code0,code1,code2,code3,code4,code5,code6,code7)
1406                     = (0xB8, byte 0 entry_addr_w, byte 1 entry_addr_w, 
1407                              byte 2 entry_addr_w, byte 3 entry_addr_w, 
1408                        0xFF, 0xE0, 
1409                        0x90 {-nop-})
1410
1411                  entry_addr_w :: Word32
1412                  entry_addr_w = fromIntegral (addrToInt entry_addr)
1413              in
1414                  do addr <- malloc
1415                     --putStrLn ("SIZE of itbl is " ++ show (sizeOf itbl))
1416                     --putStrLn ("# ptrs  of itbl is " ++ show ptrs)
1417                     --putStrLn ("# nptrs of itbl is " ++ show nptrs)
1418                     poke addr itbl
1419                     return (getName dcon, addr `plusPtr` 8)
1420
1421
1422 byte :: Int -> Word32 -> Word32
1423 byte 0 w = w .&. 0xFF
1424 byte 1 w = (w `shiftR` 8) .&. 0xFF
1425 byte 2 w = (w `shiftR` 16) .&. 0xFF
1426 byte 3 w = (w `shiftR` 24) .&. 0xFF
1427
1428
1429 vecret_entry 0 = stg_interp_constr1_entry
1430 vecret_entry 1 = stg_interp_constr2_entry
1431 vecret_entry 2 = stg_interp_constr3_entry
1432 vecret_entry 3 = stg_interp_constr4_entry
1433 vecret_entry 4 = stg_interp_constr5_entry
1434 vecret_entry 5 = stg_interp_constr6_entry
1435 vecret_entry 6 = stg_interp_constr7_entry
1436 vecret_entry 7 = stg_interp_constr8_entry
1437
1438 -- entry point for direct returns for created constr itbls
1439 foreign label "stg_interp_constr_entry" stg_interp_constr_entry :: Addr
1440 -- and the 8 vectored ones
1441 foreign label "stg_interp_constr1_entry" stg_interp_constr1_entry :: Addr
1442 foreign label "stg_interp_constr2_entry" stg_interp_constr2_entry :: Addr
1443 foreign label "stg_interp_constr3_entry" stg_interp_constr3_entry :: Addr
1444 foreign label "stg_interp_constr4_entry" stg_interp_constr4_entry :: Addr
1445 foreign label "stg_interp_constr5_entry" stg_interp_constr5_entry :: Addr
1446 foreign label "stg_interp_constr6_entry" stg_interp_constr6_entry :: Addr
1447 foreign label "stg_interp_constr7_entry" stg_interp_constr7_entry :: Addr
1448 foreign label "stg_interp_constr8_entry" stg_interp_constr8_entry :: Addr
1449
1450
1451
1452 data Constructor = Constructor Int{-ptrs-} Int{-nptrs-}
1453
1454
1455 -- Ultra-minimalist version specially for constructors
1456 data StgInfoTable = StgInfoTable {
1457    ptrs :: Word16,
1458    nptrs :: Word16,
1459    srtlen :: Word16,
1460    tipe :: Word16,
1461    code0, code1, code2, code3, code4, code5, code6, code7 :: Word8
1462 }
1463
1464
1465 instance Storable StgInfoTable where
1466
1467    sizeOf itbl 
1468       = (sum . map (\f -> f itbl))
1469         [fieldSz ptrs, fieldSz nptrs, fieldSz srtlen, fieldSz tipe,
1470          fieldSz code0, fieldSz code1, fieldSz code2, fieldSz code3, 
1471          fieldSz code4, fieldSz code5, fieldSz code6, fieldSz code7]
1472
1473    alignment itbl 
1474       = (sum . map (\f -> f itbl))
1475         [fieldAl ptrs, fieldAl nptrs, fieldAl srtlen, fieldAl tipe,
1476          fieldAl code0, fieldAl code1, fieldAl code2, fieldAl code3, 
1477          fieldAl code4, fieldAl code5, fieldAl code6, fieldAl code7]
1478
1479    poke a0 itbl
1480       = do a1 <- store (ptrs   itbl) (castPtr a0)
1481            a2 <- store (nptrs  itbl) a1
1482            a3 <- store (tipe   itbl) a2
1483            a4 <- store (srtlen itbl) a3
1484            a5 <- store (code0  itbl) a4
1485            a6 <- store (code1  itbl) a5
1486            a7 <- store (code2  itbl) a6
1487            a8 <- store (code3  itbl) a7
1488            a9 <- store (code4  itbl) a8
1489            aA <- store (code5  itbl) a9
1490            aB <- store (code6  itbl) aA
1491            aC <- store (code7  itbl) aB
1492            return ()
1493
1494    peek a0
1495       = do (a1,ptrs)   <- load (castPtr a0)
1496            (a2,nptrs)  <- load a1
1497            (a3,tipe)   <- load a2
1498            (a4,srtlen) <- load a3
1499            (a5,code0)  <- load a4
1500            (a6,code1)  <- load a5
1501            (a7,code2)  <- load a6
1502            (a8,code3)  <- load a7
1503            (a9,code4)  <- load a8
1504            (aA,code5)  <- load a9
1505            (aB,code6)  <- load aA
1506            (aC,code7)  <- load aB
1507            return StgInfoTable { ptrs = ptrs, nptrs = nptrs, 
1508                                  srtlen = srtlen, tipe = tipe,
1509                                  code0 = code0, code1 = code1, code2 = code2,
1510                                  code3 = code3, code4 = code4, code5 = code5,
1511                                  code6 = code6, code7 = code7 }
1512
1513 fieldSz :: (Storable a, Storable b) => (a -> b) -> a -> Int
1514 fieldSz sel x = sizeOf (sel x)
1515
1516 fieldAl :: (Storable a, Storable b) => (a -> b) -> a -> Int
1517 fieldAl sel x = alignment (sel x)
1518
1519 store :: Storable a => a -> Ptr a -> IO (Ptr b)
1520 store x addr = do poke addr x
1521                   return (castPtr (addr `plusPtr` sizeOf x))
1522
1523 load :: Storable a => Ptr a -> IO (Ptr b, a)
1524 load addr = do x <- peek addr
1525                return (castPtr (addr `plusPtr` sizeOf x), x)
1526
1527 \end{code}
1528
1529 %************************************************************************
1530 %*                                                                      *
1531 \subsection{Connect to actual values for bytecode opcodes}
1532 %*                                                                      *
1533 %************************************************************************
1534
1535 \begin{code}
1536
1537 #include "Bytecodes.h"
1538
1539 i_ARGCHECK = (bci_ARGCHECK :: Int)
1540 i_PUSH_L   = (bci_PUSH_L :: Int)
1541 i_PUSH_LL  = (bci_PUSH_LL :: Int)
1542 i_PUSH_LLL = (bci_PUSH_LLL :: Int)
1543 i_PUSH_G   = (bci_PUSH_G :: Int)
1544 i_PUSH_AS  = (bci_PUSH_AS :: Int)
1545 i_PUSH_UBX = (bci_PUSH_UBX :: Int)
1546 i_PUSH_TAG = (bci_PUSH_TAG :: Int)
1547 i_SLIDE    = (bci_SLIDE :: Int)
1548 i_ALLOC    = (bci_ALLOC :: Int)
1549 i_MKAP     = (bci_MKAP :: Int)
1550 i_UNPACK   = (bci_UNPACK :: Int)
1551 i_UPK_TAG  = (bci_UPK_TAG :: Int)
1552 i_PACK     = (bci_PACK :: Int)
1553 i_LABEL    = (bci_LABEL :: Int)
1554 i_TESTLT_I = (bci_TESTLT_I :: Int)
1555 i_TESTEQ_I = (bci_TESTEQ_I :: Int)
1556 i_TESTLT_F = (bci_TESTLT_F :: Int)
1557 i_TESTEQ_F = (bci_TESTEQ_F :: Int)
1558 i_TESTLT_D = (bci_TESTLT_D :: Int)
1559 i_TESTEQ_D = (bci_TESTEQ_D :: Int)
1560 i_TESTLT_P = (bci_TESTLT_P :: Int)
1561 i_TESTEQ_P = (bci_TESTEQ_P :: Int)
1562 i_CASEFAIL = (bci_CASEFAIL :: Int)
1563 i_ENTER    = (bci_ENTER :: Int)
1564 i_RETURN   = (bci_RETURN :: Int)
1565
1566 \end{code}