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