[project @ 2001-08-08 14:40:01 by sewardj]
[ghc-hetmet.git] / ghc / compiler / ghci / ByteCodeLink.lhs
1 %
2 % (c) The University of Glasgow 2000
3 %
4 \section[ByteCodeLink]{Bytecode assembler and linker}
5
6 \begin{code}
7 module ByteCodeLink ( UnlinkedBCO, UnlinkedBCOExpr, assembleBCO,
8                       ClosureEnv, HValue, filterNameMap,
9                       linkIModules, linkIExpr,
10                       iNTERP_STACK_CHECK_THRESH
11                    ) where
12
13 #include "HsVersions.h"
14
15 import Outputable
16 import Name             ( Name, getName, nameModule, toRdrName, isGlobalName )
17 import RdrName          ( rdrNameOcc, rdrNameModule )
18 import OccName          ( occNameString )
19 import FiniteMap        ( FiniteMap, addListToFM, filterFM,
20                           addToFM, lookupFM, emptyFM )
21 import CoreSyn
22 import Literal          ( Literal(..) )
23 import PrimOp           ( PrimOp, primOpOcc )
24 import PrimRep          ( PrimRep(..) )
25 import Constants        ( wORD_SIZE )
26 import Module           ( ModuleName, moduleName, moduleNameFS )
27 import Linker           ( lookupSymbol )
28 import FastString       ( FastString(..) )
29 import ByteCodeInstr    ( BCInstr(..), ProtoBCO(..) )
30 import ByteCodeItbls    ( ItblEnv, ItblPtr )
31
32
33 import Monad            ( when, foldM )
34 import ST               ( runST )
35 import IArray           ( array )
36 import MArray           ( castSTUArray, 
37                           newFloatArray, writeFloatArray,
38                           newDoubleArray, writeDoubleArray,
39                           newIntArray, writeIntArray,
40                           newAddrArray, writeAddrArray,
41                           readWordArray )
42 import Foreign          ( Word16, Ptr(..), free )
43 import Addr             ( Word, Addr(..), nullAddr )
44 import Weak             ( addFinalizer )
45 import FiniteMap
46
47 import PrelBase         ( Int(..) )
48 import PrelGHC          ( BCO#, newBCO#, unsafeCoerce#, 
49                           ByteArray#, Array#, addrToHValue#, mkApUpd0# )
50 import IOExts           ( fixIO )
51 import PrelArr          ( Array(..) )
52 import ArrayBase        ( UArray(..) )
53 import PrelIOBase       ( IO(..) )
54
55 \end{code}
56
57 %************************************************************************
58 %*                                                                      *
59 \subsection{Top-level stuff}
60 %*                                                                      *
61 %************************************************************************
62
63 \begin{code}
64 -- Linking stuff
65 linkIModules :: ItblEnv    -- incoming global itbl env; returned updated
66              -> ClosureEnv -- incoming global closure env; returned updated
67              -> [([UnlinkedBCO], ItblEnv)]
68              -> IO ([HValue], ItblEnv, ClosureEnv)
69 linkIModules gie gce mods 
70    = do let (bcoss, ies) = unzip mods
71             bcos         = concat bcoss
72             final_gie    = foldr plusFM gie ies
73         (final_gce, linked_bcos) <- linkSomeBCOs True final_gie gce bcos
74         return (linked_bcos, final_gie, final_gce)
75
76
77 linkIExpr :: ItblEnv -> ClosureEnv -> UnlinkedBCOExpr
78           -> IO HValue    -- IO BCO# really
79 linkIExpr ie ce (root_ul_bco, aux_ul_bcos)
80    = do (aux_ce, _) <- linkSomeBCOs False ie ce aux_ul_bcos
81         (_, [root_bco]) <- linkSomeBCOs False ie aux_ce [root_ul_bco]
82         return root_bco
83
84 -- Link a bunch of BCOs and return them + updated closure env.
85 linkSomeBCOs :: Bool    -- False <=> add _all_ BCOs to returned closure env
86                         -- True  <=> add only toplevel BCOs to closure env
87              -> ItblEnv 
88              -> ClosureEnv 
89              -> [UnlinkedBCO]
90              -> IO (ClosureEnv, [HValue])
91 linkSomeBCOs toplevs_only ie ce_in ul_bcos
92    = do let nms = map nameOfUnlinkedBCO ul_bcos
93         hvals <- fixIO 
94                     ( \ hvs -> let ce_out = addListToFM ce_in (zipLazily nms hvs)
95                                in  mapM (linkBCO ie ce_out) ul_bcos )
96
97         let ce_all_additions = zip nms hvals
98             ce_top_additions = filter (isGlobalName.fst) ce_all_additions
99             ce_additions     = if toplevs_only then ce_top_additions 
100                                                else ce_all_additions
101             ce_out = -- make sure we're not inserting duplicate names into the 
102                      -- closure environment, which leads to trouble.
103                      ASSERT (all (not . (`elemFM` ce_in)) (map fst ce_additions))
104                      addListToFM ce_in ce_additions
105         return (ce_out, hvals)
106      where
107         -- A lazier zip, in which no demand is propagated to the second
108         -- list unless some demand is propagated to the snd of one of the
109         -- result list elems.
110         zipLazily []     ys = []
111         zipLazily (x:xs) ys = (x, head ys) : zipLazily xs (tail ys)
112
113
114 data UnlinkedBCO
115    = UnlinkedBCO Name
116                  (SizedSeq Word16)               -- insns
117                  (SizedSeq Word)                 -- literals
118                  (SizedSeq (Either Name PrimOp)) -- ptrs
119                  (SizedSeq Name)                 -- itbl refs
120
121 nameOfUnlinkedBCO (UnlinkedBCO nm _ _ _ _) = nm
122
123 -- When translating expressions, we need to distinguish the root
124 -- BCO for the expression
125 type UnlinkedBCOExpr = (UnlinkedBCO, [UnlinkedBCO])
126
127 instance Outputable UnlinkedBCO where
128    ppr (UnlinkedBCO nm insns lits ptrs itbls)
129       = sep [text "BCO", ppr nm, text "with", 
130              int (sizeSS insns), text "insns",
131              int (sizeSS lits), text "lits",
132              int (sizeSS ptrs), text "ptrs",
133              int (sizeSS itbls), text "itbls"]
134
135
136 -- these need a proper home
137 type ClosureEnv = FiniteMap Name HValue
138 data HValue     = HValue  -- dummy type, actually a pointer to some Real Code.
139
140 -- remove all entries for a given set of modules from the environment;
141 -- note that this removes all local names too (ie. temporary bindings from
142 -- the command line).
143 filterNameMap :: [ModuleName] -> FiniteMap Name a -> FiniteMap Name a
144 filterNameMap mods env 
145    = filterFM (\n _ -> isGlobalName n && 
146                         moduleName (nameModule n) `elem` mods) env
147 \end{code}
148
149 %************************************************************************
150 %*                                                                      *
151 \subsection{The bytecode assembler}
152 %*                                                                      *
153 %************************************************************************
154
155 The object format for bytecodes is: 16 bits for the opcode, and 16 for
156 each field -- so the code can be considered a sequence of 16-bit ints.
157 Each field denotes either a stack offset or number of items on the
158 stack (eg SLIDE), and index into the pointer table (eg PUSH_G), an
159 index into the literal table (eg PUSH_I/D/L), or a bytecode address in
160 this BCO.
161
162 \begin{code}
163 -- Top level assembler fn.
164 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
165
166 assembleBCO (ProtoBCO nm instrs origin malloced)
167    = let
168          -- pass 1: collect up the offsets of the local labels.
169          -- Remember that the first insn starts at offset 1 since offset 0
170          -- (eventually) will hold the total # of insns.
171          label_env = mkLabelEnv emptyFM 1 instrs
172
173          mkLabelEnv env i_offset [] = env
174          mkLabelEnv env i_offset (i:is)
175             = let new_env 
176                      = case i of LABEL n -> addToFM env n i_offset ; _ -> env
177               in  mkLabelEnv new_env (i_offset + instrSize16s i) is
178
179          findLabel lab
180             = case lookupFM label_env lab of
181                  Just bco_offset -> bco_offset
182                  Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
183      in
184      do  -- pass 2: generate the instruction, ptr and nonptr bits
185          insns <- return emptySS :: IO (SizedSeq Word16)
186          lits  <- return emptySS :: IO (SizedSeq Word)
187          ptrs  <- return emptySS :: IO (SizedSeq (Either Name PrimOp))
188          itbls <- return emptySS :: IO (SizedSeq Name)
189          let init_asm_state = (insns,lits,ptrs,itbls)
190          (final_insns, final_lits, final_ptrs, final_itbls) 
191             <- mkBits findLabel init_asm_state instrs
192
193          let ul_bco = UnlinkedBCO nm final_insns final_lits final_ptrs final_itbls
194          when (not (null malloced)) (addFinalizer ul_bco (mapM_ zonk malloced))
195          return ul_bco
196      where
197          zonk (A# a#) = do -- putStrLn ("freeing malloc'd block at " ++ show (A# a#))
198                            free (Ptr a#)
199
200 -- instrs nonptrs ptrs itbls
201 type AsmState = (SizedSeq Word16, SizedSeq Word, 
202                  SizedSeq (Either Name PrimOp), SizedSeq Name)
203
204 data SizedSeq a = SizedSeq !Int [a]
205 emptySS = SizedSeq 0 []
206 addToSS (SizedSeq n r_xs) x = return (SizedSeq (n+1) (x:r_xs))
207 addListToSS (SizedSeq n r_xs) xs 
208    = return (SizedSeq (n + length xs) (reverse xs ++ r_xs))
209 sizeSS (SizedSeq n r_xs) = n
210 listFromSS (SizedSeq n r_xs) = return (reverse r_xs)
211
212
213 -- This is where all the action is (pass 2 of the assembler)
214 mkBits :: (Int -> Int)                  -- label finder
215        -> AsmState
216        -> [BCInstr]                     -- instructions (in)
217        -> IO AsmState
218
219 mkBits findLabel st proto_insns
220   = foldM doInstr st proto_insns
221     where
222        doInstr :: AsmState -> BCInstr -> IO AsmState
223        doInstr st i
224           = case i of
225                ARGCHECK  n        -> instr2 st i_ARGCHECK n
226                STKCHECK  n        -> instr2 st i_STKCHECK n
227                PUSH_L    o1       -> instr2 st i_PUSH_L o1
228                PUSH_LL   o1 o2    -> instr3 st i_PUSH_LL o1 o2
229                PUSH_LLL  o1 o2 o3 -> instr4 st i_PUSH_LLL o1 o2 o3
230                PUSH_G    nm       -> do (p, st2) <- ptr st nm
231                                         instr2 st2 i_PUSH_G p
232                PUSH_AS   nm pk    -> do (p, st2)  <- ptr st (Left nm)
233                                         (np, st3) <- ctoi_itbl st2 pk
234                                         instr3 st3 i_PUSH_AS p np
235                PUSH_UBX  (Left lit) nws  
236                                   -> do (np, st2) <- literal st lit
237                                         instr3 st2 i_PUSH_UBX np nws
238                PUSH_UBX  (Right aa) nws  
239                                   -> do (np, st2) <- addr st aa
240                                         instr3 st2 i_PUSH_UBX np nws
241
242                PUSH_TAG  tag      -> instr2 st i_PUSH_TAG tag
243                SLIDE     n by     -> instr3 st i_SLIDE n by
244                ALLOC     n        -> instr2 st i_ALLOC n
245                MKAP      off sz   -> instr3 st i_MKAP off sz
246                UNPACK    n        -> instr2 st i_UNPACK n
247                UPK_TAG   n m k    -> instr4 st i_UPK_TAG n m k
248                PACK      dcon sz  -> do (itbl_no,st2) <- itbl st dcon
249                                         instr3 st2 i_PACK itbl_no sz
250                LABEL     lab      -> return st
251                TESTLT_I  i l      -> do (np, st2) <- int st i
252                                         instr3 st2 i_TESTLT_I np (findLabel l)
253                TESTEQ_I  i l      -> do (np, st2) <- int st i
254                                         instr3 st2 i_TESTEQ_I np (findLabel l)
255                TESTLT_F  f l      -> do (np, st2) <- float st f
256                                         instr3 st2 i_TESTLT_F np (findLabel l)
257                TESTEQ_F  f l      -> do (np, st2) <- float st f
258                                         instr3 st2 i_TESTEQ_F np (findLabel l)
259                TESTLT_D  d l      -> do (np, st2) <- double st d
260                                         instr3 st2 i_TESTLT_D np (findLabel l)
261                TESTEQ_D  d l      -> do (np, st2) <- double st d
262                                         instr3 st2 i_TESTEQ_D np (findLabel l)
263                TESTLT_P  i l      -> instr3 st i_TESTLT_P i (findLabel l)
264                TESTEQ_P  i l      -> instr3 st i_TESTEQ_P i (findLabel l)
265                CASEFAIL           -> instr1 st i_CASEFAIL
266                JMP       l        -> instr2 st i_JMP (findLabel l)
267                ENTER              -> instr1 st i_ENTER
268                RETURN    rep      -> do (itbl_no,st2) <- itoc_itbl st rep
269                                         instr2 st2 i_RETURN itbl_no
270                CCALL     m_addr   -> do (np, st2) <- addr st m_addr
271                                         instr2 st2 i_CCALL np
272
273        i2s :: Int -> Word16
274        i2s = fromIntegral
275
276        instr1 (st_i0,st_l0,st_p0,st_I0) i1
277           = do st_i1 <- addToSS st_i0 (i2s i1)
278                return (st_i1,st_l0,st_p0,st_I0)
279
280        instr2 (st_i0,st_l0,st_p0,st_I0) i1 i2
281           = do st_i1 <- addToSS st_i0 (i2s i1)
282                st_i2 <- addToSS st_i1 (i2s i2)
283                return (st_i2,st_l0,st_p0,st_I0)
284
285        instr3 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3
286           = do st_i1 <- addToSS st_i0 (i2s i1)
287                st_i2 <- addToSS st_i1 (i2s i2)
288                st_i3 <- addToSS st_i2 (i2s i3)
289                return (st_i3,st_l0,st_p0,st_I0)
290
291        instr4 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3 i4
292           = do st_i1 <- addToSS st_i0 (i2s i1)
293                st_i2 <- addToSS st_i1 (i2s i2)
294                st_i3 <- addToSS st_i2 (i2s i3)
295                st_i4 <- addToSS st_i3 (i2s i4)
296                return (st_i4,st_l0,st_p0,st_I0)
297
298        float (st_i0,st_l0,st_p0,st_I0) f
299           = do let ws = mkLitF f
300                st_l1 <- addListToSS st_l0 ws
301                return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
302
303        double (st_i0,st_l0,st_p0,st_I0) d
304           = do let ws = mkLitD d
305                st_l1 <- addListToSS st_l0 ws
306                return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
307
308        int (st_i0,st_l0,st_p0,st_I0) i
309           = do let ws = mkLitI i
310                st_l1 <- addListToSS st_l0 ws
311                return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
312
313        addr (st_i0,st_l0,st_p0,st_I0) a
314           = do let ws = mkLitA a
315                st_l1 <- addListToSS st_l0 ws
316                return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
317
318        ptr (st_i0,st_l0,st_p0,st_I0) p
319           = do st_p1 <- addToSS st_p0 p
320                return (sizeSS st_p0, (st_i0,st_l0,st_p1,st_I0))
321
322        itbl (st_i0,st_l0,st_p0,st_I0) dcon
323           = do st_I1 <- addToSS st_I0 (getName dcon)
324                return (sizeSS st_I0, (st_i0,st_l0,st_p0,st_I1))
325
326        literal st (MachWord w)   = int st (fromIntegral w)
327        literal st (MachInt j)    = int st (fromIntegral j)
328        literal st (MachFloat r)  = float st (fromRational r)
329        literal st (MachDouble r) = double st (fromRational r)
330        literal st (MachChar c)   = int st c
331        literal st other          = pprPanic "ByteCodeLink.literal" (ppr other)
332
333        ctoi_itbl st pk
334           = addr st ret_itbl_addr
335             where
336                ret_itbl_addr 
337                   = case pk of
338                        PtrRep    -> stg_ctoi_ret_R1p_info
339                        WordRep   -> stg_ctoi_ret_R1n_info
340                        IntRep    -> stg_ctoi_ret_R1n_info
341                        AddrRep   -> stg_ctoi_ret_R1n_info
342                        CharRep   -> stg_ctoi_ret_R1n_info
343                        FloatRep  -> stg_ctoi_ret_F1_info
344                        DoubleRep -> stg_ctoi_ret_D1_info
345                        VoidRep   -> stg_ctoi_ret_V_info
346                        other     -> pprPanic "ByteCodeLink.ctoi_itbl" (ppr pk)
347
348        itoc_itbl st pk
349           = addr st ret_itbl_addr
350             where
351                ret_itbl_addr 
352                   = case pk of
353                        CharRep   -> stg_gc_unbx_r1_ret_info
354                        IntRep    -> stg_gc_unbx_r1_ret_info
355                        AddrRep   -> stg_gc_unbx_r1_ret_info
356                        FloatRep  -> stg_gc_f1_ret_info
357                        DoubleRep -> stg_gc_d1_ret_info
358                        VoidRep   -> nullAddr  
359                        -- Interpreter.c spots this special case
360                        other     -> pprPanic "ByteCodeLink.itoc_itbl" (ppr pk)
361                      
362 foreign label "stg_ctoi_ret_R1p_info" stg_ctoi_ret_R1p_info :: Addr
363 foreign label "stg_ctoi_ret_R1n_info" stg_ctoi_ret_R1n_info :: Addr
364 foreign label "stg_ctoi_ret_F1_info"  stg_ctoi_ret_F1_info :: Addr
365 foreign label "stg_ctoi_ret_D1_info"  stg_ctoi_ret_D1_info :: Addr
366 foreign label "stg_ctoi_ret_V_info"   stg_ctoi_ret_V_info :: Addr
367
368 foreign label "stg_gc_unbx_r1_ret_info" stg_gc_unbx_r1_ret_info :: Addr
369 foreign label "stg_gc_f1_ret_info"      stg_gc_f1_ret_info :: Addr
370 foreign label "stg_gc_d1_ret_info"      stg_gc_d1_ret_info :: Addr
371
372 -- The size in 16-bit entities of an instruction.
373 instrSize16s :: BCInstr -> Int
374 instrSize16s instr
375    = case instr of
376         STKCHECK _     -> 2
377         ARGCHECK _     -> 2
378         PUSH_L   _     -> 2
379         PUSH_LL  _ _   -> 3
380         PUSH_LLL _ _ _ -> 4
381         PUSH_G   _     -> 2
382         PUSH_AS  _ _   -> 3
383         PUSH_UBX _ _   -> 3
384         PUSH_TAG _     -> 2
385         SLIDE    _ _   -> 3
386         ALLOC    _     -> 2
387         MKAP     _ _   -> 3
388         UNPACK   _     -> 2
389         UPK_TAG  _ _ _ -> 4
390         PACK     _ _   -> 3
391         LABEL    _     -> 0     -- !!
392         TESTLT_I _ _   -> 3
393         TESTEQ_I _ _   -> 3
394         TESTLT_F _ _   -> 3
395         TESTEQ_F _ _   -> 3
396         TESTLT_D _ _   -> 3
397         TESTEQ_D _ _   -> 3
398         TESTLT_P _ _   -> 3
399         TESTEQ_P _ _   -> 3
400         JMP      _     -> 2
401         CASEFAIL       -> 1
402         ENTER          -> 1
403         RETURN   _     -> 2
404
405
406 -- Make lists of host-sized words for literals, so that when the
407 -- words are placed in memory at increasing addresses, the
408 -- bit pattern is correct for the host's word size and endianness.
409 mkLitI :: Int    -> [Word]
410 mkLitF :: Float  -> [Word]
411 mkLitD :: Double -> [Word]
412 mkLitA :: Addr   -> [Word]
413
414 mkLitF f
415    = runST (do
416         arr <- newFloatArray ((0::Int),0)
417         writeFloatArray arr 0 f
418         f_arr <- castSTUArray arr
419         w0 <- readWordArray f_arr 0
420         return [w0]
421      )
422
423 mkLitD d
424    | wORD_SIZE == 4
425    = runST (do
426         arr <- newDoubleArray ((0::Int),1)
427         writeDoubleArray arr 0 d
428         d_arr <- castSTUArray arr
429         w0 <- readWordArray d_arr 0
430         w1 <- readWordArray d_arr 1
431         return [w0,w1]
432      )
433    | wORD_SIZE == 8
434    = runST (do
435         arr <- newDoubleArray ((0::Int),0)
436         writeDoubleArray arr 0 d
437         d_arr <- castSTUArray arr
438         w0 <- readWordArray d_arr 0
439         return [w0]
440      )
441
442 mkLitI i
443    = runST (do
444         arr <- newIntArray ((0::Int),0)
445         writeIntArray arr 0 i
446         i_arr <- castSTUArray arr
447         w0 <- readWordArray i_arr 0
448         return [w0]
449      )
450
451 mkLitA a
452    = runST (do
453         arr <- newAddrArray ((0::Int),0)
454         writeAddrArray arr 0 a
455         a_arr <- castSTUArray arr
456         w0 <- readWordArray a_arr 0
457         return [w0]
458      )
459
460 \end{code}
461
462 %************************************************************************
463 %*                                                                      *
464 \subsection{Linking interpretables into something we can run}
465 %*                                                                      *
466 %************************************************************************
467
468 \begin{code}
469
470 {- 
471 data BCO# = BCO# ByteArray#             -- instrs   :: Array Word16#
472                  ByteArray#             -- literals :: Array Word32#
473                  PtrArray#              -- ptrs     :: Array HValue
474                  ByteArray#             -- itbls    :: Array Addr#
475 -}
476
477 linkBCO ie ce (UnlinkedBCO nm insnsSS literalsSS ptrsSS itblsSS)
478    = do insns    <- listFromSS insnsSS
479         literals <- listFromSS literalsSS
480         ptrs     <- listFromSS ptrsSS
481         itbls    <- listFromSS itblsSS
482
483         linked_ptrs  <- mapM (lookupCE ce) ptrs
484         linked_itbls <- mapM (lookupIE ie) itbls
485
486         let n_insns    = sizeSS insnsSS
487             n_literals = sizeSS literalsSS
488             n_ptrs     = sizeSS ptrsSS
489             n_itbls    = sizeSS itblsSS
490
491         let ptrs_arr = array (0, n_ptrs-1) (indexify linked_ptrs)
492                        :: Array Int HValue
493             ptrs_parr = case ptrs_arr of Array lo hi parr -> parr
494
495             itbls_arr = array (0, n_itbls-1) (indexify linked_itbls)
496                         :: UArray Int ItblPtr
497             itbls_barr = case itbls_arr of UArray lo hi barr -> barr
498
499             insns_arr | n_insns > 65535
500                       = panic "linkBCO: >= 64k insns in BCO"
501                       | otherwise 
502                       = array (0, n_insns) 
503                               (indexify (fromIntegral n_insns:insns))
504                         :: UArray Int Word16
505             insns_barr = case insns_arr of UArray lo hi barr -> barr
506
507             literals_arr = array (0, n_literals-1) (indexify literals)
508                            :: UArray Int Word
509             literals_barr = case literals_arr of UArray lo hi barr -> barr
510
511             indexify :: [a] -> [(Int, a)]
512             indexify xs = zip [0..] xs
513
514         BCO bco# <- newBCO insns_barr literals_barr ptrs_parr itbls_barr
515
516         -- WAS: return (unsafeCoerce# bco#)
517         case mkApUpd0# (unsafeCoerce# bco#) of
518            (# final_bco #) -> return final_bco
519
520
521 data BCO = BCO BCO#
522
523 newBCO :: ByteArray# -> ByteArray# -> Array# a -> ByteArray# -> IO BCO
524 newBCO a b c d
525    = IO (\s -> case newBCO# a b c d s of (# s1, bco #) -> (# s1, BCO bco #))
526
527
528 lookupCE :: ClosureEnv -> Either Name PrimOp -> IO HValue
529 lookupCE ce (Right primop)
530    = do m <- lookupSymbol (primopToCLabel primop "closure")
531         case m of
532            Just (Ptr addr) -> case addrToHValue# addr of
533                                  (# hval #) -> return hval
534            Nothing -> pprPanic "ByteCodeLink.lookupCE(primop)" (ppr primop)
535 lookupCE ce (Left nm)
536    = case lookupFM ce nm of
537         Just aa -> return aa
538         Nothing 
539            -> do m <- lookupSymbol (nameToCLabel nm "closure")
540                  case m of
541                     Just (Ptr addr) -> case addrToHValue# addr of
542                                           (# hval #) -> return hval
543                     Nothing        -> pprPanic "ByteCodeLink.lookupCE" (ppr nm)
544
545 lookupIE :: ItblEnv -> Name -> IO (Ptr a)
546 lookupIE ie con_nm 
547    = case lookupFM ie con_nm of
548         Just (Ptr a) -> return (Ptr a)
549         Nothing
550            -> do -- try looking up in the object files.
551                  m <- lookupSymbol (nameToCLabel con_nm "con_info")
552                  case m of
553                     Just addr -> return addr
554                     Nothing 
555                        -> do -- perhaps a nullary constructor?
556                              n <- lookupSymbol (nameToCLabel con_nm "static_info")
557                              case n of
558                                 Just addr -> return addr
559                                 Nothing -> pprPanic "ByteCodeLink.lookupIE" (ppr con_nm)
560
561 -- HACKS!!!  ToDo: cleaner
562 nameToCLabel :: Name -> String{-suffix-} -> String
563 nameToCLabel n suffix
564    = _UNPK_(moduleNameFS (rdrNameModule rn)) 
565      ++ '_':occNameString(rdrNameOcc rn) ++ '_':suffix
566      where rn = toRdrName n
567
568 primopToCLabel :: PrimOp -> String{-suffix-} -> String
569 primopToCLabel primop suffix
570    = let str = "PrelPrimopWrappers_" ++ occNameString (primOpOcc primop) ++ '_':suffix
571      in --trace ("primopToCLabel: " ++ str)
572         str
573
574 \end{code}
575
576 %************************************************************************
577 %*                                                                      *
578 \subsection{Connect to actual values for bytecode opcodes}
579 %*                                                                      *
580 %************************************************************************
581
582 \begin{code}
583
584 #include "Bytecodes.h"
585
586 i_ARGCHECK = (bci_ARGCHECK :: Int)
587 i_PUSH_L   = (bci_PUSH_L :: Int)
588 i_PUSH_LL  = (bci_PUSH_LL :: Int)
589 i_PUSH_LLL = (bci_PUSH_LLL :: Int)
590 i_PUSH_G   = (bci_PUSH_G :: Int)
591 i_PUSH_AS  = (bci_PUSH_AS :: Int)
592 i_PUSH_UBX = (bci_PUSH_UBX :: Int)
593 i_PUSH_TAG = (bci_PUSH_TAG :: Int)
594 i_SLIDE    = (bci_SLIDE :: Int)
595 i_ALLOC    = (bci_ALLOC :: Int)
596 i_MKAP     = (bci_MKAP :: Int)
597 i_UNPACK   = (bci_UNPACK :: Int)
598 i_UPK_TAG  = (bci_UPK_TAG :: Int)
599 i_PACK     = (bci_PACK :: Int)
600 i_TESTLT_I = (bci_TESTLT_I :: Int)
601 i_TESTEQ_I = (bci_TESTEQ_I :: Int)
602 i_TESTLT_F = (bci_TESTLT_F :: Int)
603 i_TESTEQ_F = (bci_TESTEQ_F :: Int)
604 i_TESTLT_D = (bci_TESTLT_D :: Int)
605 i_TESTEQ_D = (bci_TESTEQ_D :: Int)
606 i_TESTLT_P = (bci_TESTLT_P :: Int)
607 i_TESTEQ_P = (bci_TESTEQ_P :: Int)
608 i_CASEFAIL = (bci_CASEFAIL :: Int)
609 i_ENTER    = (bci_ENTER :: Int)
610 i_RETURN   = (bci_RETURN :: Int)
611 i_STKCHECK = (bci_STKCHECK :: Int)
612 i_JMP      = (bci_JMP :: Int)
613 #ifdef bci_CCALL
614 i_CCALL    = (bci_CCALL :: Int)
615 #else
616 i_CCALL    = error "Sorry pal, you need to bootstrap to use i_CCALL."
617 #endif
618
619 iNTERP_STACK_CHECK_THRESH = (INTERP_STACK_CHECK_THRESH :: Int)
620
621 \end{code}