[project @ 2001-08-08 14:11:58 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                    ) where
11
12 #include "HsVersions.h"
13
14 import Outputable
15 import Name             ( Name, getName )
16 import Id               ( Id, idType, isDataConId_maybe, isPrimOpId_maybe, isFCallId,
17                           idPrimRep, mkSysLocal, idName, isFCallId_maybe )
18 import ForeignCall      ( ForeignCall(..), CCallTarget(..), CCallSpec(..) )
19 import OrdList          ( OrdList, consOL, snocOL, appOL, unitOL, 
20                           nilOL, toOL, concatOL, fromOL )
21 import FiniteMap        ( FiniteMap, addListToFM, listToFM,
22                           addToFM, lookupFM, fmToList )
23 import CoreSyn
24 import PprCore          ( pprCoreExpr )
25 import Literal          ( Literal(..), literalPrimRep )
26 import PrimRep          ( PrimRep(..) )
27 import PrimOp           ( PrimOp(..) )
28 import CoreFVs          ( freeVars )
29 import Type             ( typePrimRep, splitTyConApp_maybe, isTyVarTy )
30 import DataCon          ( dataConTag, fIRST_TAG, dataConTyCon, 
31                           dataConWrapId, isUnboxedTupleCon )
32 import TyCon            ( TyCon(..), tyConFamilySize, isDataTyCon, tyConDataCons,
33                           isFunTyCon, isUnboxedTupleTyCon )
34 import Class            ( Class, classTyCon )
35 import Type             ( Type, repType, splitRepFunTys )
36 import Util             ( zipEqual, zipWith4Equal, naturalMergeSortLe, nOfThem )
37 import Var              ( isTyVar )
38 import VarSet           ( VarSet, varSetElems )
39 import PrimRep          ( isFollowableRep )
40 import CmdLineOpts      ( DynFlags, DynFlag(..) )
41 import ErrUtils         ( showPass, dumpIfSet_dyn )
42 import Unique           ( mkPseudoUnique3 )
43 import FastString       ( FastString(..) )
44 import Panic            ( GhcException(..) )
45 import PprType          ( pprType )
46 import ByteCodeInstr    ( BCInstr(..), ProtoBCO(..), nameOfProtoBCO, bciStackUse )
47 import ByteCodeItbls    ( ItblEnv, mkITbls )
48 import ByteCodeLink     ( UnlinkedBCO, UnlinkedBCOExpr, assembleBCO,
49                           ClosureEnv, HValue, filterNameMap,
50                           iNTERP_STACK_CHECK_THRESH )
51 import ByteCodeFFI      ( taggedSizeW, untaggedSizeW, mkMarshalCode )
52 import Linker           ( lookupSymbol )
53
54 import List             ( intersperse, sortBy, zip4 )
55 import Foreign          ( Ptr(..), mallocBytes )
56 import Addr             ( Addr(..), writeCharOffAddr )
57 import CTypes           ( CInt )
58 import Exception        ( throwDyn )
59
60 import PrelBase         ( Int(..) )
61 import PrelGHC          ( ByteArray# )
62 import PrelIOBase       ( IO(..) )
63 import Monad            ( when )
64
65 \end{code}
66
67 %************************************************************************
68 %*                                                                      *
69 \subsection{Functions visible from outside this module.}
70 %*                                                                      *
71 %************************************************************************
72
73 \begin{code}
74
75 byteCodeGen :: DynFlags
76             -> [CoreBind] 
77             -> [TyCon] -> [Class]
78             -> IO ([UnlinkedBCO], ItblEnv)
79 byteCodeGen dflags binds local_tycons local_classes
80    = do showPass dflags "ByteCodeGen"
81         let tycs = local_tycons ++ map classTyCon local_classes
82         itblenv <- mkITbls tycs
83
84         let flatBinds = concatMap getBind binds
85             getBind (NonRec bndr rhs) = [(bndr, freeVars rhs)]
86             getBind (Rec binds)       = [(bndr, freeVars rhs) | (bndr,rhs) <- binds]
87
88         (BcM_State proto_bcos final_ctr mallocd, ())
89            <- runBc (BcM_State [] 0 []) 
90                     (mapBc (schemeR True) flatBinds `thenBc_` returnBc ())
91
92         when (not (null mallocd))
93              (panic "ByteCodeGen.byteCodeGen: missing final emitBc?")
94
95         dumpIfSet_dyn dflags Opt_D_dump_BCOs
96            "Proto-bcos" (vcat (intersperse (char ' ') (map ppr proto_bcos)))
97
98         bcos <- mapM assembleBCO proto_bcos
99
100         return (bcos, itblenv)
101         
102
103 -- Returns: (the root BCO for this expression, 
104 --           a list of auxilary BCOs resulting from compiling closures)
105 coreExprToBCOs :: DynFlags
106                -> CoreExpr
107                -> IO UnlinkedBCOExpr
108 coreExprToBCOs dflags expr
109  = do showPass dflags "ByteCodeGen"
110
111       -- create a totally bogus name for the top-level BCO; this
112       -- should be harmless, since it's never used for anything
113       let invented_id   = mkSysLocal SLIT("Expr-Top-Level") (mkPseudoUnique3 0) 
114                                      (panic "invented_id's type")
115       let invented_name = idName invented_id
116
117       (BcM_State all_proto_bcos final_ctr mallocd, ()) 
118          <- runBc (BcM_State [] 0 []) 
119                   (schemeR True (invented_id, freeVars expr))
120
121       when (not (null mallocd))
122            (panic "ByteCodeGen.coreExprToBCOs: missing final emitBc?")
123
124       dumpIfSet_dyn dflags Opt_D_dump_BCOs
125          "Proto-bcos" (vcat (intersperse (char ' ') (map ppr all_proto_bcos)))
126
127       let root_proto_bco 
128              = case filter ((== invented_name).nameOfProtoBCO) all_proto_bcos of
129                   [root_bco] -> root_bco
130           auxiliary_proto_bcos
131              = filter ((/= invented_name).nameOfProtoBCO) all_proto_bcos
132
133       auxiliary_bcos <- mapM assembleBCO auxiliary_proto_bcos
134       root_bco <- assembleBCO root_proto_bco
135
136       return (root_bco, auxiliary_bcos)
137 \end{code}
138
139 %************************************************************************
140 %*                                                                      *
141 \subsection{Compilation schema for the bytecode generator.}
142 %*                                                                      *
143 %************************************************************************
144
145 \begin{code}
146
147 type BCInstrList = OrdList BCInstr
148
149 type Sequel = Int       -- back off to this depth before ENTER
150
151 -- Maps Ids to the offset from the stack _base_ so we don't have
152 -- to mess with it after each push/pop.
153 type BCEnv = FiniteMap Id Int   -- To find vars on the stack
154
155 ppBCEnv :: BCEnv -> SDoc
156 ppBCEnv p
157    = text "begin-env"
158      $$ nest 4 (vcat (map pp_one (sortBy cmp_snd (fmToList p))))
159      $$ text "end-env"
160      where
161         pp_one (var, offset) = int offset <> colon <+> ppr var
162         cmp_snd x y = compare (snd x) (snd y)
163
164 -- Create a BCO and do a spot of peephole optimisation on the insns
165 -- at the same time.
166 mkProtoBCO nm instrs_ordlist origin mallocd_blocks
167    = ProtoBCO nm maybe_with_stack_check origin mallocd_blocks
168      where
169         -- Overestimate the stack usage (in words) of this BCO,
170         -- and if >= iNTERP_STACK_CHECK_THRESH, add an explicit
171         -- stack check.  (The interpreter always does a stack check
172         -- for iNTERP_STACK_CHECK_THRESH words at the start of each
173         -- BCO anyway, so we only need to add an explicit on in the
174         -- (hopefully rare) cases when the (overestimated) stack use
175         -- exceeds iNTERP_STACK_CHECK_THRESH.
176         maybe_with_stack_check
177            | stack_overest >= 65535
178            = pprPanic "mkProtoBCO: stack use won't fit in 16 bits" 
179                       (int stack_overest)
180            | stack_overest >= iNTERP_STACK_CHECK_THRESH
181            = (STKCHECK stack_overest) : peep_d
182            | otherwise
183            = peep_d     -- the supposedly common case
184              
185         stack_overest = sum (map bciStackUse peep_d)
186                         + 10 {- just to be really really sure -}
187
188
189         -- Merge local pushes
190         peep_d = peep (fromOL instrs_ordlist)
191
192         peep (PUSH_L off1 : PUSH_L off2 : PUSH_L off3 : rest)
193            = PUSH_LLL off1 (off2-1) (off3-2) : peep rest
194         peep (PUSH_L off1 : PUSH_L off2 : rest)
195            = PUSH_LL off1 (off2-1) : peep rest
196         peep (i:rest)
197            = i : peep rest
198         peep []
199            = []
200
201
202 -- Compile code for the right hand side of a let binding.
203 -- Park the resulting BCO in the monad.  Also requires the
204 -- variable to which this value was bound, so as to give the
205 -- resulting BCO a name.  Bool indicates top-levelness.
206
207 schemeR :: Bool -> (Id, AnnExpr Id VarSet) -> BcM ()
208 schemeR is_top (nm, rhs) 
209 {-
210    | trace (showSDoc (
211               (char ' '
212                $$ (ppr.filter (not.isTyVar).varSetElems.fst) rhs
213                $$ pprCoreExpr (deAnnotate rhs)
214                $$ char ' '
215               ))) False
216    = undefined
217 -}
218    | otherwise
219    = schemeR_wrk is_top rhs nm (collect [] rhs)
220
221
222 collect xs (_, AnnNote note e)
223    = collect xs e
224 collect xs (_, AnnLam x e) 
225    = collect (if isTyVar x then xs else (x:xs)) e
226 collect xs not_lambda
227    = (reverse xs, not_lambda)
228
229 schemeR_wrk is_top original_body nm (args, body)
230    | Just dcon <- maybe_toplevel_null_con_rhs
231    = --trace ("nullary constructor! " ++ showSDocDebug (ppr nm)) (
232      emitBc (mkProtoBCO (getName nm) (toOL [PACK dcon 0, ENTER])
233                                      (Right original_body))
234      --)
235
236    | otherwise
237    = let fvs       = filter (not.isTyVar) (varSetElems (fst original_body))
238          all_args  = reverse args ++ fvs
239          szsw_args = map taggedIdSizeW all_args
240          szw_args  = sum szsw_args
241          p_init    = listToFM (zip all_args (mkStackOffsets 0 szsw_args))
242          argcheck  = unitOL (ARGCHECK szw_args)
243      in
244      schemeE szw_args 0 p_init body             `thenBc` \ body_code ->
245      emitBc (mkProtoBCO (getName nm) (appOL argcheck body_code) 
246                                      (Right original_body))
247
248      where
249         maybe_toplevel_null_con_rhs
250            | is_top && null args
251            = case snd body of
252                 AnnVar v_wrk 
253                    -> case isDataConId_maybe v_wrk of
254                          Nothing -> Nothing
255                          Just dc_wrk |  nm == dataConWrapId dc_wrk
256                                      -> Just dc_wrk
257                                      |  otherwise 
258                                      -> Nothing
259                 other -> Nothing
260            | otherwise
261            = Nothing
262
263 -- Let szsw be the sizes in words of some items pushed onto the stack,
264 -- which has initial depth d'.  Return the values which the stack environment
265 -- should map these items to.
266 mkStackOffsets :: Int -> [Int] -> [Int]
267 mkStackOffsets original_depth szsw
268    = map (subtract 1) (tail (scanl (+) original_depth szsw))
269
270 -- Compile code to apply the given expression to the remaining args
271 -- on the stack, returning a HNF.
272 schemeE :: Int -> Sequel -> BCEnv -> AnnExpr Id VarSet -> BcM BCInstrList
273
274 -- Delegate tail-calls to schemeT.
275 schemeE d s p e@(fvs, AnnApp f a) 
276    = schemeT d s p (fvs, AnnApp f a)
277
278 schemeE d s p e@(fvs, AnnVar v)
279    | isFollowableRep v_rep
280    =  -- Ptr-ish thing; push it in the normal way
281      schemeT d s p (fvs, AnnVar v)
282
283    | otherwise
284    = -- returning an unboxed value.  Heave it on the stack, SLIDE, and RETURN.
285      pushAtom True d p (AnnVar v)       `thenBc` \ (push, szw) ->
286      returnBc (push                     -- value onto stack
287                `appOL`  mkSLIDE szw (d-s)       -- clear to sequel
288                `snocOL` RETURN v_rep)   -- go
289    where
290       v_rep = typePrimRep (idType v)
291
292 schemeE d s p (fvs, AnnLit literal)
293    = pushAtom True d p (AnnLit literal) `thenBc` \ (push, szw) ->
294      let l_rep = literalPrimRep literal
295      in  returnBc (push                         -- value onto stack
296                    `appOL`  mkSLIDE szw (d-s)   -- clear to sequel
297                    `snocOL` RETURN l_rep)       -- go
298
299 schemeE d s p (fvs, AnnLet binds b)
300    = let (xs,rhss) = case binds of AnnNonRec x rhs  -> ([x],[rhs])
301                                    AnnRec xs_n_rhss -> unzip xs_n_rhss
302          n     = length xs
303          fvss  = map (filter (not.isTyVar).varSetElems.fst) rhss
304
305          -- Sizes of tagged free vars, + 1 for the fn
306          sizes = map (\rhs_fvs -> 1 + sum (map taggedIdSizeW rhs_fvs)) fvss
307
308          -- This p', d' defn is safe because all the items being pushed
309          -- are ptrs, so all have size 1.  d' and p' reflect the stack
310          -- after the closures have been allocated in the heap (but not
311          -- filled in), and pointers to them parked on the stack.
312          p'    = addListToFM p (zipE xs (mkStackOffsets d (nOfThem n 1)))
313          d'    = d + n
314
315          infos = zipE4 fvss sizes xs [n, n-1 .. 1]
316          zipE  = zipEqual "schemeE"
317          zipE4 = zipWith4Equal "schemeE" (\a b c d -> (a,b,c,d))
318
319          -- ToDo: don't build thunks for things with no free variables
320          buildThunk dd ([], size, id, off)
321             = returnBc (PUSH_G (Left (getName id))
322                         `consOL` unitOL (MKAP (off+size-1) size))
323          buildThunk dd ((fv:fvs), size, id, off)
324             = pushAtom True dd p' (AnnVar fv) 
325                                         `thenBc` \ (push_code, pushed_szw) ->
326               buildThunk (dd+pushed_szw) (fvs, size, id, off)
327                                         `thenBc` \ more_push_code ->
328               returnBc (push_code `appOL` more_push_code)
329
330          genThunkCode = mapBc (buildThunk d') infos     `thenBc` \ tcodes ->
331                         returnBc (concatOL tcodes)
332
333          allocCode = toOL (map ALLOC sizes)
334      in
335      schemeE d' s p' b                                  `thenBc`  \ bodyCode ->
336      mapBc (schemeR False) (zip xs rhss)                `thenBc_`
337      genThunkCode                                       `thenBc` \ thunkCode ->
338      returnBc (allocCode `appOL` thunkCode `appOL` bodyCode)
339
340
341
342
343
344 schemeE d s p (fvs_case, AnnCase (fvs_scrut, scrut) bndr 
345                                  [(DEFAULT, [], (fvs_rhs, rhs))])
346
347    | let isFunType var_type 
348             = case splitTyConApp_maybe var_type of
349                  Just (tycon,_) | isFunTyCon tycon -> True
350                  _ -> False
351          ty_bndr = repType (idType bndr)
352      in isFunType ty_bndr || isTyVarTy ty_bndr
353
354    -- Nasty hack; treat
355    --     case scrut::suspect of bndr { DEFAULT -> rhs }
356    --     as 
357    --     let bndr = scrut in rhs
358    --     when suspect is polymorphic or arrowtyped
359    -- So the required strictness properties are not observed.
360    -- At some point, must fix this properly.
361    = let new_expr
362             = (fvs_case, 
363                AnnLet 
364                   (AnnNonRec bndr (fvs_scrut, scrut)) (fvs_rhs, rhs)
365               )
366
367      in  trace ("WARNING: ignoring polymorphic case in interpreted mode.\n" ++
368                 "   Possibly due to strict polymorphic/functional constructor args.\n" ++
369                 "   Your program may leak space unexpectedly.\n")
370          (schemeE d s p new_expr)
371
372
373
374 {- Convert case .... of (# VoidRep'd-thing, a #) -> ...
375       as
376    case .... of a -> ...
377    Use  a  as the name of the binder too.
378
379    Also    case .... of (# a #) -> ...
380       to
381    case .... of a -> ...
382 -}
383 schemeE d s p (fvs, AnnCase scrut bndr [(DataAlt dc, [bind1, bind2], rhs)])
384    | isUnboxedTupleCon dc && VoidRep == typePrimRep (idType bind1)
385    = --trace "automagic mashing of case alts (# VoidRep, a #)" (
386      schemeE d s p (fvs, AnnCase scrut bind2 [(DEFAULT, [bind2], rhs)])
387      --)
388
389 schemeE d s p (fvs, AnnCase scrut bndr [(DataAlt dc, [bind1], rhs)])
390    | isUnboxedTupleCon dc
391    = --trace "automagic mashing of case alts (# a #)" (
392      schemeE d s p (fvs, AnnCase scrut bind1 [(DEFAULT, [bind1], rhs)])
393      --)
394
395 schemeE d s p (fvs, AnnCase scrut bndr alts)
396    = let
397         -- Top of stack is the return itbl, as usual.
398         -- underneath it is the pointer to the alt_code BCO.
399         -- When an alt is entered, it assumes the returned value is
400         -- on top of the itbl.
401         ret_frame_sizeW = 2
402
403         -- Env and depth in which to compile the alts, not including
404         -- any vars bound by the alts themselves
405         d' = d + ret_frame_sizeW + taggedIdSizeW bndr
406         p' = addToFM p bndr (d' - 1)
407
408         scrut_primrep = typePrimRep (idType bndr)
409         isAlgCase
410            | scrut_primrep == PtrRep
411            = True
412            | scrut_primrep `elem`
413              [CharRep, AddrRep, WordRep, IntRep, FloatRep, DoubleRep,
414               VoidRep, Int8Rep, Int16Rep, Int32Rep, Int64Rep,
415               Word8Rep, Word16Rep, Word32Rep, Word64Rep]
416            = False
417            | otherwise
418            =  pprPanic "ByteCodeGen.schemeE" (ppr scrut_primrep)
419
420         -- given an alt, return a discr and code for it.
421         codeAlt alt@(discr, binds_f, rhs)
422            | isAlgCase 
423            = let (unpack_code, d_after_unpack, p_after_unpack)
424                     = mkUnpackCode (filter (not.isTyVar) binds_f) d' p'
425              in  schemeE d_after_unpack s p_after_unpack rhs
426                                         `thenBc` \ rhs_code -> 
427                  returnBc (my_discr alt, unpack_code `appOL` rhs_code)
428            | otherwise 
429            = ASSERT(null binds_f) 
430              schemeE d' s p' rhs        `thenBc` \ rhs_code ->
431              returnBc (my_discr alt, rhs_code)
432
433         my_discr (DEFAULT, binds, rhs) = NoDiscr
434         my_discr (DataAlt dc, binds, rhs) 
435            | isUnboxedTupleCon dc
436            = unboxedTupleException
437            | otherwise
438            = DiscrP (dataConTag dc - fIRST_TAG)
439         my_discr (LitAlt l, binds, rhs)
440            = case l of MachInt i     -> DiscrI (fromInteger i)
441                        MachFloat r   -> DiscrF (fromRational r)
442                        MachDouble r  -> DiscrD (fromRational r)
443                        MachChar i    -> DiscrI i
444                        _ -> pprPanic "schemeE(AnnCase).my_discr" (ppr l)
445
446         maybe_ncons 
447            | not isAlgCase = Nothing
448            | otherwise 
449            = case [dc | (DataAlt dc, _, _) <- alts] of
450                 []     -> Nothing
451                 (dc:_) -> Just (tyConFamilySize (dataConTyCon dc))
452
453      in 
454      mapBc codeAlt alts                                 `thenBc` \ alt_stuff ->
455      mkMultiBranch maybe_ncons alt_stuff                `thenBc` \ alt_final ->
456      let 
457          alt_final_ac = ARGCHECK (taggedIdSizeW bndr) `consOL` alt_final
458          alt_bco_name = getName bndr
459          alt_bco      = mkProtoBCO alt_bco_name alt_final_ac (Left alts)
460      in
461      schemeE (d + ret_frame_sizeW) 
462              (d + ret_frame_sizeW) p scrut              `thenBc` \ scrut_code ->
463
464      emitBc alt_bco                                     `thenBc_`
465      returnBc (PUSH_AS alt_bco_name scrut_primrep `consOL` scrut_code)
466
467
468 schemeE d s p (fvs, AnnNote note body)
469    = schemeE d s p body
470
471 schemeE d s p other
472    = pprPanic "ByteCodeGen.schemeE: unhandled case" 
473                (pprCoreExpr (deAnnotate other))
474
475
476 -- Compile code to do a tail call.  Specifically, push the fn,
477 -- slide the on-stack app back down to the sequel depth,
478 -- and enter.  Four cases:
479 --
480 -- 0.  (Nasty hack).
481 --     An application "PrelGHC.tagToEnum# <type> unboxed-int".
482 --     The int will be on the stack.  Generate a code sequence
483 --     to convert it to the relevant constructor, SLIDE and ENTER.
484 --
485 -- 1.  A nullary constructor.  Push its closure on the stack 
486 --     and SLIDE and RETURN.
487 --
488 -- 2.  (Another nasty hack).  Spot (# a::VoidRep, b #) and treat
489 --     it simply as  b  -- since the representations are identical
490 --     (the VoidRep takes up zero stack space).  Also, spot
491 --     (# b #) and treat it as  b.
492 --
493 -- 3.  Application of a non-nullary constructor, by defn saturated.
494 --     Split the args into ptrs and non-ptrs, and push the nonptrs, 
495 --     then the ptrs, and then do PACK and RETURN.
496 --
497 -- 4.  Otherwise, it must be a function call.  Push the args
498 --     right to left, SLIDE and ENTER.
499
500 schemeT :: Int          -- Stack depth
501         -> Sequel       -- Sequel depth
502         -> BCEnv        -- stack env
503         -> AnnExpr Id VarSet 
504         -> BcM BCInstrList
505
506 schemeT d s p app
507
508 --   | trace ("schemeT: env in = \n" ++ showSDocDebug (ppBCEnv p)) False
509 --   = panic "schemeT ?!?!"
510
511 --   | trace ("\nschemeT\n" ++ showSDoc (pprCoreExpr (deAnnotate app)) ++ "\n") False
512 --   = error "?!?!" 
513
514    -- Handle case 0
515    | Just (arg, constr_names) <- maybe_is_tagToEnum_call
516    = pushAtom True d p arg              `thenBc` \ (push, arg_words) ->
517      implement_tagToId constr_names     `thenBc` \ tagToId_sequence ->
518      returnBc (push `appOL`  tagToId_sequence            
519                     `appOL`  mkSLIDE 1 (d+arg_words-s)
520                     `snocOL` ENTER)
521
522    -- Handle case 1
523    | is_con_call && null args_r_to_l
524    = returnBc (
525         (PUSH_G (Left (getName con)) `consOL` mkSLIDE 1 (d-s))
526         `snocOL` ENTER
527      )
528
529    -- Handle case 2
530    | let isVoidRepAtom (_, AnnVar v)    = VoidRep == typePrimRep (idType v)
531          isVoidRepAtom (_, AnnNote n e) = isVoidRepAtom e
532      in  is_con_call && isUnboxedTupleCon con 
533          && ( (length args_r_to_l == 2 && isVoidRepAtom (last (args_r_to_l)))
534               || (length args_r_to_l == 1)
535             )
536    = --trace (if length args_r_to_l == 1
537      --       then "schemeT: unboxed singleton"
538      --       else "schemeT: unboxed pair with Void first component") (
539      schemeT d s p (head args_r_to_l)
540      --)
541
542    | Just (CCall ccall_spec) <- isFCallId_maybe fn
543    = generateCCall d s p ccall_spec fn args_r_to_l
544
545    -- Cases 3 and 4
546    | otherwise
547    = if   is_con_call && isUnboxedTupleCon con
548      then unboxedTupleException
549      else do_pushery d (map snd args_final_r_to_l)
550
551    where
552       -- Detect and extract relevant info for the tagToEnum kludge.
553       maybe_is_tagToEnum_call
554          = let extract_constr_Names ty
555                   = case splitTyConApp_maybe (repType ty) of
556                        (Just (tyc, [])) |  isDataTyCon tyc
557                                         -> map getName (tyConDataCons tyc)
558                        other -> panic "maybe_is_tagToEnum_call.extract_constr_Ids"
559            in 
560            case app of
561               (_, AnnApp (_, AnnApp (_, AnnVar v) (_, AnnType t)) arg)
562                  -> case isPrimOpId_maybe v of
563                        Just TagToEnumOp -> Just (snd arg, extract_constr_Names t)
564                        other            -> Nothing
565               other -> Nothing
566
567       -- Extract the args (R->L) and fn
568       (args_r_to_l_raw, fn) = chomp app
569       chomp expr
570          = case snd expr of
571               AnnVar v    -> ([], v)
572               AnnApp f a  -> case chomp f of (az, f) -> (a:az, f)
573               AnnNote n e -> chomp e
574               other       -> pprPanic "schemeT" 
575                                 (ppr (deAnnotate (panic "schemeT.chomp", other)))
576          
577       args_r_to_l = filter (not.isTypeAtom.snd) args_r_to_l_raw
578       isTypeAtom (AnnType _) = True
579       isTypeAtom _           = False
580
581       -- decide if this is a constructor call, and rearrange
582       -- args appropriately.
583       maybe_dcon  = isDataConId_maybe fn
584       is_con_call = case maybe_dcon of Nothing -> False; Just _ -> True
585       (Just con)  = maybe_dcon
586
587       args_final_r_to_l
588          | not is_con_call
589          = args_r_to_l
590          | otherwise
591          = filter (not.isPtr.snd) args_r_to_l ++ filter (isPtr.snd) args_r_to_l
592            where isPtr = isFollowableRep . atomRep
593
594       -- make code to push the args and then do the SLIDE-ENTER thing
595       tag_when_push = not is_con_call
596       narg_words    = sum (map (get_arg_szw . atomRep . snd) args_r_to_l)
597       get_arg_szw   = if tag_when_push then taggedSizeW else untaggedSizeW
598
599       do_pushery d (arg:args)
600          = pushAtom tag_when_push d p arg       `thenBc` \ (push, arg_words) ->
601            do_pushery (d+arg_words) args        `thenBc` \ more_push_code ->
602            returnBc (push `appOL` more_push_code)
603       do_pushery d []
604          | Just (CCall ccall_spec) <- isFCallId_maybe fn
605          = panic "schemeT.do_pushery: unexpected ccall"
606          | otherwise
607          = case maybe_dcon of
608               Just con -> returnBc (
609                              (PACK con narg_words `consOL`
610                               mkSLIDE 1 (d - narg_words - s)) `snocOL`
611                               ENTER
612                           )
613               Nothing
614                  -> pushAtom True d p (AnnVar fn)       
615                                                 `thenBc` \ (push, arg_words) ->
616                     returnBc (push `appOL` mkSLIDE (narg_words+arg_words) 
617                                                    (d - s - narg_words)
618                               `snocOL` ENTER)
619
620
621
622 {- Deal with a CCall.  Taggedly push the args onto the stack R->L,
623    deferencing ForeignObj#s and (ToDo: adjusting addrs to point to
624    payloads in Ptr/Byte arrays).  Then, generate the marshalling
625    (machine) code for the ccall, and create bytecodes to call that and
626    then return in the right way.  
627 -}
628 generateCCall :: Int -> Sequel          -- stack and sequel depths
629               -> BCEnv
630               -> CCallSpec              -- where to call
631               -> Id                     -- of target, for type info
632               -> [AnnExpr Id VarSet]    -- args (atoms)
633               -> BcM BCInstrList
634
635 generateCCall d0 s p ccall_spec@(CCallSpec target cconv safety) fn args_r_to_l
636    = let 
637          -- useful constants
638          addr_usizeW = untaggedSizeW AddrRep
639          addr_tsizeW = taggedSizeW AddrRep
640
641          -- Get the args on the stack, with tags and suitably
642          -- dereferenced for the CCall.  For each arg, return the
643          -- depth to the first word of the bits for that arg, and the
644          -- PrimRep of what was actually pushed.
645
646          pargs d [] = returnBc []
647          pargs d ((_,a):az) 
648             = let rep_arg = atomRep a
649               in case rep_arg of
650                     -- Don't push the FO; instead push the Addr# it
651                     -- contains.
652                     ForeignObjRep
653                        -> pushAtom False{-irrelevant-} d p a
654                                                         `thenBc` \ (push_fo, _) ->
655                           let foro_szW = taggedSizeW ForeignObjRep
656                               d_now    = d + addr_tsizeW
657                               code     = push_fo `appOL` toOL [
658                                             UPK_TAG addr_usizeW 0 0,
659                                             SLIDE addr_tsizeW foro_szW
660                                          ]
661                           in  pargs d_now az            `thenBc` \ rest ->
662                               returnBc ((code, AddrRep) : rest)
663                     -- Default case: push taggedly, but otherwise intact.
664                     other
665                        -> pushAtom True d p a           `thenBc` \ (code_a, sz_a) ->
666                           pargs (d+sz_a) az             `thenBc` \ rest ->
667                           returnBc ((code_a, rep_arg) : rest)
668      in
669          pargs d0 args_r_to_l                           `thenBc` \ code_n_reps ->
670      let
671          (pushs_arg, a_reps_pushed_r_to_l) = unzip code_n_reps
672
673          push_args    = concatOL pushs_arg
674          d_after_args = d0 + sum (map taggedSizeW a_reps_pushed_r_to_l)
675          a_reps_pushed_RAW
676             | null a_reps_pushed_r_to_l || head a_reps_pushed_r_to_l /= VoidRep
677             = panic "ByteCodeGen.generateCCall: missing or invalid World token?"
678             | otherwise
679             = reverse (tail a_reps_pushed_r_to_l)
680
681          -- Now: a_reps_pushed_RAW are the reps which are actually on the stack.
682          -- push_args is the code to do that.
683          -- d_after_args is the stack depth once the args are on.
684
685          -- Get the result rep.
686          (returns_void, r_rep)
687             = case maybe_getCCallReturnRep (idType fn) of
688                  Nothing -> (True,  VoidRep)
689                  Just rr -> (False, rr) 
690          {-
691          Because the Haskell stack grows down, the a_reps refer to 
692          lowest to highest addresses in that order.  The args for the call
693          are on the stack.  Now push an unboxed, tagged Addr# indicating
694          the C function to call.  Then push a dummy placeholder for the 
695          result.  Finally, emit a CCALL insn with an offset pointing to the 
696          Addr# just pushed, and a literal field holding the mallocville
697          address of the piece of marshalling code we generate.
698          So, just prior to the CCALL insn, the stack looks like this 
699          (growing down, as usual):
700                  
701             <arg_n>
702             ...
703             <arg_1>
704             Addr# address_of_C_fn
705             <placeholder-for-result#> (must be an unboxed type)
706
707          The interpreter then calls the marshall code mentioned
708          in the CCALL insn, passing it (& <placeholder-for-result#>), 
709          that is, the addr of the topmost word in the stack.
710          When this returns, the placeholder will have been
711          filled in.  The placeholder is slid down to the sequel
712          depth, and we RETURN.
713
714          This arrangement makes it simple to do f-i-dynamic since the Addr#
715          value is the first arg anyway.  It also has the virtue that the
716          stack is GC-understandable at all times.
717
718          The marshalling code is generated specifically for this
719          call site, and so knows exactly the (Haskell) stack
720          offsets of the args, fn address and placeholder.  It
721          copies the args to the C stack, calls the stacked addr,
722          and parks the result back in the placeholder.  The interpreter
723          calls it as a normal C call, assuming it has a signature
724             void marshall_code ( StgWord* ptr_to_top_of_stack )
725          -}
726          -- resolve static address
727          get_target_info
728             = case target of
729                  DynamicTarget
730                     -> returnBc (False, panic "ByteCodeGen.generateCCall(dyn)")
731                  StaticTarget target
732                     -> ioToBc (lookupSymbol (_UNPK_ target)) `thenBc` \res ->
733                        case res of
734                            Just aa -> case aa of Ptr a# -> returnBc (True, A# a#)
735                            Nothing -> returnBc invalid
736                  CasmTarget _
737                     -> returnBc invalid
738                  where
739                     invalid = pprPanic ("ByteCodeGen.generateCCall: unfindable " 
740                                         ++ "symbol or otherwise invalid target")
741                                        (ppr ccall_spec)
742      in
743          get_target_info        `thenBc` \ (is_static, static_target_addr) ->
744      let
745
746          -- Get the arg reps, zapping the leading Addr# in the dynamic case
747          a_reps -- | trace (showSDoc (ppr a_reps_pushed_RAW)) False = error "???"
748                 | is_static = a_reps_pushed_RAW
749                 | otherwise = if null a_reps_pushed_RAW 
750                               then panic "ByteCodeGen.generateCCall: dyn with no args"
751                               else tail a_reps_pushed_RAW
752
753          -- push the Addr#
754          (push_Addr, d_after_Addr)
755             | is_static
756             = (toOL [PUSH_UBX (Right static_target_addr) addr_usizeW,
757                      PUSH_TAG addr_usizeW],
758                d_after_args + addr_tsizeW)
759             | otherwise -- is already on the stack
760             = (nilOL, d_after_args)
761
762          -- Push the return placeholder.  For a call returning nothing,
763          -- this is a VoidRep (tag).
764          r_usizeW  = untaggedSizeW r_rep
765          r_tsizeW  = taggedSizeW r_rep
766          d_after_r = d_after_Addr + r_tsizeW
767          r_lit     = mkDummyLiteral r_rep
768          push_r    = (if   returns_void 
769                       then nilOL 
770                       else unitOL (PUSH_UBX (Left r_lit) r_usizeW))
771                       `appOL` 
772                       unitOL (PUSH_TAG r_usizeW)
773
774          -- generate the marshalling code we're going to call
775          r_offW       = 0 
776          addr_offW    = r_tsizeW
777          arg1_offW    = r_tsizeW + addr_tsizeW
778          args_offW    = map (arg1_offW +) 
779                             (init (scanl (+) 0 (map taggedSizeW a_reps)))
780      in
781          ioToBc (mkMarshalCode cconv
782                     (r_offW, r_rep) addr_offW
783                     (zip args_offW a_reps))     `thenBc` \ addr_of_marshaller ->
784          recordMallocBc addr_of_marshaller      `thenBc_`
785      let
786          -- do the call
787          do_call      = unitOL (CCALL addr_of_marshaller)
788          -- slide and return
789          wrapup       = mkSLIDE r_tsizeW (d_after_r - r_tsizeW - s)
790                         `snocOL` RETURN r_rep
791      in
792          --trace (show (arg1_offW, args_offW  ,  (map taggedSizeW a_reps) )) (
793          returnBc (
794          push_args `appOL`
795          push_Addr `appOL` push_r `appOL` do_call `appOL` wrapup
796          )
797          --)
798
799
800 -- Make a dummy literal, to be used as a placeholder for FFI return
801 -- values on the stack.
802 mkDummyLiteral :: PrimRep -> Literal
803 mkDummyLiteral pr
804    = case pr of
805         IntRep    -> MachInt 0
806         DoubleRep -> MachDouble 0
807         FloatRep  -> MachFloat 0
808         AddrRep   | taggedSizeW AddrRep == taggedSizeW WordRep -> MachWord 0
809         _         -> pprPanic "mkDummyLiteral" (ppr pr)
810
811
812 -- Convert (eg) 
813 --     PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
814 --                   -> (# PrelGHC.State# PrelGHC.RealWorld, PrelGHC.Int# #)
815 --
816 -- to  Just IntRep
817 -- and check that an unboxed pair is returned wherein the first arg is VoidRep'd.
818 --
819 -- Alternatively, for call-targets returning nothing, convert
820 --
821 --     PrelGHC.Char# -> PrelGHC.State# PrelGHC.RealWorld
822 --                   -> (# PrelGHC.State# PrelGHC.RealWorld #)
823 --
824 -- to  Nothing
825
826 maybe_getCCallReturnRep :: Type -> Maybe PrimRep
827 maybe_getCCallReturnRep fn_ty
828    = let (a_tys, r_ty) = splitRepFunTys fn_ty
829          maybe_r_rep_to_go  
830             = if length r_reps == 1 then Nothing else Just (r_reps !! 1)
831          (r_tycon, r_reps) 
832             = case splitTyConApp_maybe (repType r_ty) of
833                       (Just (tyc, tys)) -> (tyc, map typePrimRep tys)
834                       Nothing -> blargh
835          ok = ( (length r_reps == 2 && VoidRep == head r_reps)
836                 || r_reps == [VoidRep] )
837               && isUnboxedTupleTyCon r_tycon
838               && case maybe_r_rep_to_go of
839                     Nothing    -> True
840                     Just r_rep -> r_rep /= PtrRep
841                                   -- if it was, it would be impossible 
842                                   -- to create a valid return value 
843                                   -- placeholder on the stack
844          blargh = pprPanic "maybe_getCCallReturn: can't handle:" 
845                            (pprType fn_ty)
846      in 
847      --trace (showSDoc (ppr (a_reps, r_reps))) (
848      if ok then maybe_r_rep_to_go else blargh
849      --)
850
851 atomRep (AnnVar v)    = typePrimRep (idType v)
852 atomRep (AnnLit l)    = literalPrimRep l
853 atomRep (AnnNote n b) = atomRep (snd b)
854 atomRep (AnnApp f (_, AnnType _)) = atomRep (snd f)
855 atomRep (AnnLam x e) | isTyVar x = atomRep (snd e)
856 atomRep other = pprPanic "atomRep" (ppr (deAnnotate (undefined,other)))
857
858
859 -- Compile code which expects an unboxed Int on the top of stack,
860 -- (call it i), and pushes the i'th closure in the supplied list 
861 -- as a consequence.
862 implement_tagToId :: [Name] -> BcM BCInstrList
863 implement_tagToId names
864    = ASSERT(not (null names))
865      getLabelsBc (length names)                 `thenBc` \ labels ->
866      getLabelBc                                 `thenBc` \ label_fail ->
867      getLabelBc                                 `thenBc` \ label_exit ->
868      zip4 labels (tail labels ++ [label_fail])
869                  [0 ..] names                   `bind`   \ infos ->
870      map (mkStep label_exit) infos              `bind`   \ steps ->
871      returnBc (concatOL steps
872                `appOL` 
873                toOL [LABEL label_fail, CASEFAIL, LABEL label_exit])
874      where
875         mkStep l_exit (my_label, next_label, n, name_for_n)
876            = toOL [LABEL my_label, 
877                    TESTEQ_I n next_label, 
878                    PUSH_G (Left name_for_n), 
879                    JMP l_exit]
880
881
882 -- Make code to unpack the top-of-stack constructor onto the stack, 
883 -- adding tags for the unboxed bits.  Takes the PrimReps of the 
884 -- constructor's arguments.  off_h and off_s are travelling offsets
885 -- along the constructor and the stack.
886 --
887 -- Supposing a constructor in the heap has layout
888 --
889 --      Itbl p_1 ... p_i np_1 ... np_j
890 --
891 -- then we add to the stack, shown growing down, the following:
892 --
893 --    (previous stack)
894 --         p_i
895 --         ...
896 --         p_1
897 --         np_j
898 --         tag_for(np_j)
899 --         ..
900 --         np_1
901 --         tag_for(np_1)
902 --
903 -- so that in the common case (ptrs only) a single UNPACK instr can
904 -- copy all the payload of the constr onto the stack with no further ado.
905
906 mkUnpackCode :: [Id]    -- constr args
907              -> Int     -- depth before unpack
908              -> BCEnv   -- env before unpack
909              -> (BCInstrList, Int, BCEnv)
910 mkUnpackCode vars d p
911    = --trace ("mkUnpackCode: " ++ showSDocDebug (ppr vars)
912      --       ++ " --> " ++ show d' ++ "\n" ++ showSDocDebug (ppBCEnv p')
913      --       ++ "\n") (
914      (code_p `appOL` code_np, d', p')
915      --)
916      where
917         -- vars with reps
918         vreps = [(var, typePrimRep (idType var)) | var <- vars]
919
920         -- ptrs and nonptrs, forward
921         vreps_p  = filter (isFollowableRep.snd) vreps
922         vreps_np = filter (not.isFollowableRep.snd) vreps
923
924         -- the order in which we will augment the environment
925         vreps_env = reverse vreps_p ++ reverse vreps_np
926
927         -- new env and depth
928         vreps_env_tszsw = map (taggedSizeW.snd) vreps_env
929         p' = addListToFM p (zip (map fst vreps_env) 
930                                 (mkStackOffsets d vreps_env_tszsw))
931         d' = d + sum vreps_env_tszsw
932
933         -- code to unpack the ptrs
934         ptrs_szw = sum (map (untaggedSizeW.snd) vreps_p)
935         code_p | null vreps_p = nilOL
936                | otherwise    = unitOL (UNPACK ptrs_szw)
937
938         -- code to unpack the nonptrs
939         vreps_env_uszw = sum (map (untaggedSizeW.snd) vreps_env)
940         code_np = do_nptrs vreps_env_uszw ptrs_szw (reverse (map snd vreps_np))
941         do_nptrs off_h off_s [] = nilOL
942         do_nptrs off_h off_s (npr:nprs)
943            | npr `elem` [IntRep, WordRep, FloatRep, DoubleRep, CharRep, AddrRep]
944            = approved
945            | otherwise
946            = pprPanic "ByteCodeGen.mkUnpackCode" (ppr npr)
947              where
948                 approved = UPK_TAG usizeW (off_h-usizeW) off_s   `consOL` theRest
949                 theRest  = do_nptrs (off_h-usizeW) (off_s + tsizeW) nprs
950                 usizeW   = untaggedSizeW npr
951                 tsizeW   = taggedSizeW npr
952
953
954 -- Push an atom onto the stack, returning suitable code & number of
955 -- stack words used.  Pushes it either tagged or untagged, since 
956 -- pushAtom is used to set up the stack prior to copying into the
957 -- heap for both APs (requiring tags) and constructors (which don't).
958 --
959 -- NB this means NO GC between pushing atoms for a constructor and
960 -- copying them into the heap.  It probably also means that 
961 -- tail calls MUST be of the form atom{atom ... atom} since if the
962 -- expression head was allowed to be arbitrary, there could be GC
963 -- in between pushing the arg atoms and completing the head.
964 -- (not sure; perhaps the allocate/doYouWantToGC interface means this
965 -- isn't a problem; but only if arbitrary graph construction for the
966 -- head doesn't leave this BCO, since GC might happen at the start of
967 -- each BCO (we consult doYouWantToGC there).
968 --
969 -- Blargh.  JRS 001206
970 --
971 -- NB (further) that the env p must map each variable to the highest-
972 -- numbered stack slot for it.  For example, if the stack has depth 4 
973 -- and we tagged-ly push (v :: Int#) on it, the value will be in stack[4],
974 -- the tag in stack[5], the stack will have depth 6, and p must map v to
975 -- 5 and not to 4.  Stack locations are numbered from zero, so a depth
976 -- 6 stack has valid words 0 .. 5.
977
978 pushAtom :: Bool -> Int -> BCEnv -> AnnExpr' Id VarSet -> BcM (BCInstrList, Int)
979 pushAtom tagged d p (AnnVar v)
980
981    | idPrimRep v == VoidRep
982    = if tagged then returnBc (unitOL (PUSH_TAG 0), 1) 
983                else panic "ByteCodeGen.pushAtom(VoidRep,untaggedly)"
984
985    | isFCallId v
986    = pprPanic "pushAtom: shouldn't get an FCallId here" (ppr v)
987
988    | Just primop <- isPrimOpId_maybe v
989    = returnBc (unitOL (PUSH_G (Right primop)), 1)
990
991    | otherwise
992    = let  {-
993           str = "\npushAtom " ++ showSDocDebug (ppr v) 
994                ++ " :: " ++ showSDocDebug (pprType (idType v))
995                ++ ", depth = " ++ show d
996                ++ ", tagged = " ++ show tagged ++ ", env =\n" ++ 
997                showSDocDebug (ppBCEnv p)
998                ++ " --> words: " ++ show (snd result) ++ "\n" ++
999                showSDoc (nest 4 (vcat (map ppr (fromOL (fst result)))))
1000                ++ "\nendPushAtom " ++ showSDocDebug (ppr v)
1001          -}
1002
1003          result
1004             = case lookupBCEnv_maybe p v of
1005                  Just d_v -> (toOL (nOfThem nwords (PUSH_L (d-d_v+sz_t-2))), nwords)
1006                  Nothing  -> ASSERT(sz_t == 1) (unitOL (PUSH_G (Left nm)), nwords)
1007
1008          nm = case isDataConId_maybe v of
1009                  Just c  -> getName c
1010                  Nothing -> getName v
1011
1012          sz_t   = taggedIdSizeW v
1013          sz_u   = untaggedIdSizeW v
1014          nwords = if tagged then sz_t else sz_u
1015      in
1016          returnBc result
1017
1018 pushAtom True d p (AnnLit lit)
1019    = pushAtom False d p (AnnLit lit)            `thenBc` \ (ubx_code, ubx_size) ->
1020      returnBc (ubx_code `snocOL` PUSH_TAG ubx_size, 1 + ubx_size)
1021
1022 pushAtom False d p (AnnLit lit)
1023    = case lit of
1024         MachWord w   -> code WordRep
1025         MachInt i    -> code IntRep
1026         MachFloat r  -> code FloatRep
1027         MachDouble r -> code DoubleRep
1028         MachChar c   -> code CharRep
1029         MachStr s    -> pushStr s
1030      where
1031         code rep
1032            = let size_host_words = untaggedSizeW rep
1033              in  returnBc (unitOL (PUSH_UBX (Left lit) size_host_words), 
1034                            size_host_words)
1035
1036         pushStr s 
1037            = let getMallocvilleAddr
1038                     = case s of
1039                          CharStr s i -> returnBc (A# s)
1040
1041                          FastString _ l ba -> 
1042                             -- sigh, a string in the heap is no good to us.
1043                             -- We need a static C pointer, since the type of 
1044                             -- a string literal is Addr#.  So, copy the string 
1045                             -- into C land and introduce a memory leak 
1046                             -- at the same time.
1047                             let n = I# l
1048                             -- CAREFUL!  Chars are 32 bits in ghc 4.09+
1049                             in  ioToBc (mallocBytes (n+1)) `thenBc` \ (Ptr a#) ->
1050                                 recordMallocBc (A# a#)     `thenBc_`
1051                                 ioToBc (
1052                                    do strncpy (Ptr a#) ba (fromIntegral n)
1053                                       writeCharOffAddr (A# a#) n '\0'
1054                                       return (A# a#)
1055                                    )
1056                          other -> panic "ByteCodeGen.pushAtom.pushStr"
1057              in
1058                 getMallocvilleAddr `thenBc` \ addr ->
1059                 -- Get the addr on the stack, untaggedly
1060                    returnBc (unitOL (PUSH_UBX (Right addr) 1), 1)
1061
1062
1063
1064
1065
1066 pushAtom tagged d p (AnnApp f (_, AnnType _))
1067    = pushAtom tagged d p (snd f)
1068
1069 pushAtom tagged d p (AnnNote note e)
1070    = pushAtom tagged d p (snd e)
1071
1072 pushAtom tagged d p (AnnLam x e) 
1073    | isTyVar x 
1074    = pushAtom tagged d p (snd e)
1075
1076 pushAtom tagged d p other
1077    = pprPanic "ByteCodeGen.pushAtom" 
1078               (pprCoreExpr (deAnnotate (undefined, other)))
1079
1080 foreign import "strncpy" strncpy :: Ptr a -> ByteArray# -> CInt -> IO ()
1081
1082
1083 -- Given a bunch of alts code and their discrs, do the donkey work
1084 -- of making a multiway branch using a switch tree.
1085 -- What a load of hassle!
1086 mkMultiBranch :: Maybe Int      -- # datacons in tycon, if alg alt
1087                                 -- a hint; generates better code
1088                                 -- Nothing is always safe
1089               -> [(Discr, BCInstrList)] 
1090               -> BcM BCInstrList
1091 mkMultiBranch maybe_ncons raw_ways
1092    = let d_way     = filter (isNoDiscr.fst) raw_ways
1093          notd_ways = naturalMergeSortLe 
1094                         (\w1 w2 -> leAlt (fst w1) (fst w2))
1095                         (filter (not.isNoDiscr.fst) raw_ways)
1096
1097          mkTree :: [(Discr, BCInstrList)] -> Discr -> Discr -> BcM BCInstrList
1098          mkTree [] range_lo range_hi = returnBc the_default
1099
1100          mkTree [val] range_lo range_hi
1101             | range_lo `eqAlt` range_hi 
1102             = returnBc (snd val)
1103             | otherwise
1104             = getLabelBc                                `thenBc` \ label_neq ->
1105               returnBc (mkTestEQ (fst val) label_neq 
1106                         `consOL` (snd val
1107                         `appOL`   unitOL (LABEL label_neq)
1108                         `appOL`   the_default))
1109
1110          mkTree vals range_lo range_hi
1111             = let n = length vals `div` 2
1112                   vals_lo = take n vals
1113                   vals_hi = drop n vals
1114                   v_mid = fst (head vals_hi)
1115               in
1116               getLabelBc                                `thenBc` \ label_geq ->
1117               mkTree vals_lo range_lo (dec v_mid)       `thenBc` \ code_lo ->
1118               mkTree vals_hi v_mid range_hi             `thenBc` \ code_hi ->
1119               returnBc (mkTestLT v_mid label_geq
1120                         `consOL` (code_lo
1121                         `appOL`   unitOL (LABEL label_geq)
1122                         `appOL`   code_hi))
1123  
1124          the_default 
1125             = case d_way of [] -> unitOL CASEFAIL
1126                             [(_, def)] -> def
1127
1128          -- None of these will be needed if there are no non-default alts
1129          (mkTestLT, mkTestEQ, init_lo, init_hi)
1130             | null notd_ways
1131             = panic "mkMultiBranch: awesome foursome"
1132             | otherwise
1133             = case fst (head notd_ways) of {
1134               DiscrI _ -> ( \(DiscrI i) fail_label -> TESTLT_I i fail_label,
1135                             \(DiscrI i) fail_label -> TESTEQ_I i fail_label,
1136                             DiscrI minBound,
1137                             DiscrI maxBound );
1138               DiscrF _ -> ( \(DiscrF f) fail_label -> TESTLT_F f fail_label,
1139                             \(DiscrF f) fail_label -> TESTEQ_F f fail_label,
1140                             DiscrF minF,
1141                             DiscrF maxF );
1142               DiscrD _ -> ( \(DiscrD d) fail_label -> TESTLT_D d fail_label,
1143                             \(DiscrD d) fail_label -> TESTEQ_D d fail_label,
1144                             DiscrD minD,
1145                             DiscrD maxD );
1146               DiscrP _ -> ( \(DiscrP i) fail_label -> TESTLT_P i fail_label,
1147                             \(DiscrP i) fail_label -> TESTEQ_P i fail_label,
1148                             DiscrP algMinBound,
1149                             DiscrP algMaxBound )
1150               }
1151
1152          (algMinBound, algMaxBound)
1153             = case maybe_ncons of
1154                  Just n  -> (0, n - 1)
1155                  Nothing -> (minBound, maxBound)
1156
1157          (DiscrI i1) `eqAlt` (DiscrI i2) = i1 == i2
1158          (DiscrF f1) `eqAlt` (DiscrF f2) = f1 == f2
1159          (DiscrD d1) `eqAlt` (DiscrD d2) = d1 == d2
1160          (DiscrP i1) `eqAlt` (DiscrP i2) = i1 == i2
1161          NoDiscr     `eqAlt` NoDiscr     = True
1162          _           `eqAlt` _           = False
1163
1164          (DiscrI i1) `leAlt` (DiscrI i2) = i1 <= i2
1165          (DiscrF f1) `leAlt` (DiscrF f2) = f1 <= f2
1166          (DiscrD d1) `leAlt` (DiscrD d2) = d1 <= d2
1167          (DiscrP i1) `leAlt` (DiscrP i2) = i1 <= i2
1168          NoDiscr     `leAlt` NoDiscr     = True
1169          _           `leAlt` _           = False
1170
1171          isNoDiscr NoDiscr = True
1172          isNoDiscr _       = False
1173
1174          dec (DiscrI i) = DiscrI (i-1)
1175          dec (DiscrP i) = DiscrP (i-1)
1176          dec other      = other         -- not really right, but if you
1177                 -- do cases on floating values, you'll get what you deserve
1178
1179          -- same snotty comment applies to the following
1180          minF, maxF :: Float
1181          minD, maxD :: Double
1182          minF = -1.0e37
1183          maxF =  1.0e37
1184          minD = -1.0e308
1185          maxD =  1.0e308
1186      in
1187          mkTree notd_ways init_lo init_hi
1188
1189 \end{code}
1190
1191 %************************************************************************
1192 %*                                                                      *
1193 \subsection{Supporting junk for the compilation schemes}
1194 %*                                                                      *
1195 %************************************************************************
1196
1197 \begin{code}
1198
1199 -- Describes case alts
1200 data Discr 
1201    = DiscrI Int
1202    | DiscrF Float
1203    | DiscrD Double
1204    | DiscrP Int
1205    | NoDiscr
1206
1207 instance Outputable Discr where
1208    ppr (DiscrI i) = int i
1209    ppr (DiscrF f) = text (show f)
1210    ppr (DiscrD d) = text (show d)
1211    ppr (DiscrP i) = int i
1212    ppr NoDiscr    = text "DEF"
1213
1214
1215 -- Find things in the BCEnv (the what's-on-the-stack-env)
1216 -- See comment preceding pushAtom for precise meaning of env contents
1217 --lookupBCEnv :: BCEnv -> Id -> Int
1218 --lookupBCEnv env nm
1219 --   = case lookupFM env nm of
1220 --        Nothing -> pprPanic "lookupBCEnv" 
1221 --                            (ppr nm $$ char ' ' $$ vcat (map ppr (fmToList env)))
1222 --        Just xx -> xx
1223
1224 lookupBCEnv_maybe :: BCEnv -> Id -> Maybe Int
1225 lookupBCEnv_maybe = lookupFM
1226
1227
1228 taggedIdSizeW, untaggedIdSizeW :: Id -> Int
1229 taggedIdSizeW   = taggedSizeW   . typePrimRep . idType
1230 untaggedIdSizeW = untaggedSizeW . typePrimRep . idType
1231
1232 unboxedTupleException :: a
1233 unboxedTupleException 
1234    = throwDyn 
1235         (Panic 
1236            ("Bytecode generator can't handle unboxed tuples.  Possibly due\n" ++
1237             "\tto foreign import/export decls in source.  Workaround:\n" ++
1238             "\tcompile this module to a .o file, then restart session."))
1239
1240
1241 mkSLIDE n d = if d == 0 then nilOL else unitOL (SLIDE n d)
1242 bind x f    = f x
1243
1244 \end{code}
1245
1246 %************************************************************************
1247 %*                                                                      *
1248 \subsection{The bytecode generator's monad}
1249 %*                                                                      *
1250 %************************************************************************
1251
1252 \begin{code}
1253 data BcM_State 
1254    = BcM_State { bcos      :: [ProtoBCO Name],  -- accumulates completed BCOs
1255                  nextlabel :: Int,              -- for generating local labels
1256                  malloced  :: [Addr] }          -- ptrs malloced for current BCO
1257                                                 -- Should be free()d when it is GCd
1258 type BcM r = BcM_State -> IO (BcM_State, r)
1259
1260 ioToBc :: IO a -> BcM a
1261 ioToBc io st = do x <- io 
1262                   return (st, x)
1263
1264 runBc :: BcM_State -> BcM r -> IO (BcM_State, r)
1265 runBc st0 m = do (st1, res) <- m st0
1266                  return (st1, res)
1267
1268 thenBc :: BcM a -> (a -> BcM b) -> BcM b
1269 thenBc expr cont st0
1270    = do (st1, q) <- expr st0
1271         (st2, r) <- cont q st1
1272         return (st2, r)
1273
1274 thenBc_ :: BcM a -> BcM b -> BcM b
1275 thenBc_ expr cont st0
1276    = do (st1, q) <- expr st0
1277         (st2, r) <- cont st1
1278         return (st2, r)
1279
1280 returnBc :: a -> BcM a
1281 returnBc result st = return (st, result)
1282
1283
1284 mapBc :: (a -> BcM b) -> [a] -> BcM [b]
1285 mapBc f []     = returnBc []
1286 mapBc f (x:xs)
1287   = f x          `thenBc` \ r  ->
1288     mapBc f xs   `thenBc` \ rs ->
1289     returnBc (r:rs)
1290
1291 emitBc :: ([Addr] -> ProtoBCO Name) -> BcM ()
1292 emitBc bco st
1293    = return (st{bcos = bco (malloced st) : bcos st, malloced=[]}, ())
1294
1295 newbcoBc :: BcM ()
1296 newbcoBc st
1297    | not (null (malloced st)) 
1298    = panic "ByteCodeGen.newbcoBc: missed prior emitBc?"
1299    | otherwise
1300    = return (st, ())
1301
1302 recordMallocBc :: Addr -> BcM ()
1303 recordMallocBc a st
1304    = return (st{malloced = a : malloced st}, ())
1305
1306 getLabelBc :: BcM Int
1307 getLabelBc st
1308    = return (st{nextlabel = 1 + nextlabel st}, nextlabel st)
1309
1310 getLabelsBc :: Int -> BcM [Int]
1311 getLabelsBc n st
1312    = let ctr = nextlabel st 
1313      in return (st{nextlabel = ctr+n}, [ctr .. ctr+n-1])
1314
1315 \end{code}