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