2 % (c) The University of Glasgow 2002
4 \section[ByteCodeLink]{Bytecode assembler and linker}
7 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
10 assembleBCOs, assembleBCO,
13 UnlinkedBCO(..), BCOPtr(..), bcoFreeNames,
14 SizedSeq, sizeSS, ssElts,
15 iNTERP_STACK_CHECK_THRESH
18 #include "HsVersions.h"
21 import ByteCodeItbls ( ItblEnv, mkITbls )
23 import Name ( Name, getName )
25 import FiniteMap ( addToFM, lookupFM, emptyFM )
26 import Literal ( Literal(..) )
27 import TyCon ( TyCon )
28 import PrimOp ( PrimOp )
29 import PrimRep ( PrimRep(..), isFollowableRep, is64BitRep )
30 import Constants ( wORD_SIZE )
31 import FastString ( FastString(..), unpackFS )
35 import Control.Monad ( foldM, zipWithM )
36 import Control.Monad.ST ( ST, runST )
38 import GHC.Word ( Word(..) )
39 import Data.Array.MArray
40 import Data.Array.Unboxed ( listArray )
41 import Data.Array.Base ( STUArray, UArray(..), unsafeWrite )
42 import Data.Array.ST ( castSTUArray )
43 import Foreign ( Word16, free )
44 import Data.Int ( Int64 )
46 import GHC.Base ( ByteArray# )
47 import GHC.IOBase ( IO(..) )
48 import GHC.Ptr ( Ptr(..) )
50 -- -----------------------------------------------------------------------------
53 -- CompiledByteCode represents the result of byte-code
54 -- compiling a bunch of functions and data types
57 = ByteCode [UnlinkedBCO] -- Bunch of interpretable bindings
58 ItblEnv -- A mapping from DataCons to their itbls
60 instance Outputable CompiledByteCode where
61 ppr (ByteCode bcos _) = ppr bcos
66 unlinkedBCOName :: Name,
67 unlinkedBCOArity :: Int,
68 unlinkedBCOInstrs :: ByteArray#, -- insns
69 unlinkedBCOBitmap :: ByteArray#, -- bitmap
70 unlinkedBCOLits :: (SizedSeq (Either Word FastString)), -- literals
71 -- Either literal words or a pointer to a asciiz
72 -- string, denoting a label whose *address* should
73 -- be determined at link time
74 unlinkedBCOPtrs :: (SizedSeq BCOPtr), -- ptrs
75 unlinkedBCOItbls :: (SizedSeq Name) -- itbl refs
81 | BCOPtrBCO UnlinkedBCO
83 -- | Finds external references. Remember to remove the names
84 -- defined by this group of BCOs themselves
85 bcoFreeNames :: UnlinkedBCO -> NameSet
87 = bco_refs bco `minusNameSet` mkNameSet [unlinkedBCOName bco]
89 bco_refs (UnlinkedBCO _ _ _ _ _ ptrs itbls)
91 mkNameSet [ n | BCOPtrName n <- ssElts ptrs ] :
92 mkNameSet (ssElts itbls) :
93 map bco_refs [ bco | BCOPtrBCO bco <- ssElts ptrs ]
96 instance Outputable UnlinkedBCO where
97 ppr (UnlinkedBCO nm arity insns bitmap lits ptrs itbls)
98 = sep [text "BCO", ppr nm, text "with",
99 int (sizeSS lits), text "lits",
100 int (sizeSS ptrs), text "ptrs",
101 int (sizeSS itbls), text "itbls"]
103 -- -----------------------------------------------------------------------------
104 -- The bytecode assembler
106 -- The object format for bytecodes is: 16 bits for the opcode, and 16
107 -- for each field -- so the code can be considered a sequence of
108 -- 16-bit ints. Each field denotes either a stack offset or number of
109 -- items on the stack (eg SLIDE), and index into the pointer table (eg
110 -- PUSH_G), an index into the literal table (eg PUSH_I/D/L), or a
111 -- bytecode address in this BCO.
113 -- Top level assembler fn.
114 assembleBCOs :: [ProtoBCO Name] -> [TyCon] -> IO CompiledByteCode
115 assembleBCOs proto_bcos tycons
116 = do itblenv <- mkITbls tycons
117 bcos <- mapM assembleBCO proto_bcos
118 return (ByteCode bcos itblenv)
120 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
121 assembleBCO (ProtoBCO nm instrs bitmap bsize arity origin malloced)
123 -- pass 1: collect up the offsets of the local labels.
124 -- Remember that the first insn starts at offset 1 since offset 0
125 -- (eventually) will hold the total # of insns.
126 label_env = mkLabelEnv emptyFM 1 instrs
128 mkLabelEnv env i_offset [] = env
129 mkLabelEnv env i_offset (i:is)
131 = case i of LABEL n -> addToFM env n i_offset ; _ -> env
132 in mkLabelEnv new_env (i_offset + instrSize16s i) is
135 = case lookupFM label_env lab of
136 Just bco_offset -> bco_offset
137 Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
139 do -- pass 2: generate the instruction, ptr and nonptr bits
140 insns <- return emptySS :: IO (SizedSeq Word16)
141 lits <- return emptySS :: IO (SizedSeq (Either Word FastString))
142 ptrs <- return emptySS :: IO (SizedSeq BCOPtr)
143 itbls <- return emptySS :: IO (SizedSeq Name)
144 let init_asm_state = (insns,lits,ptrs,itbls)
145 (final_insns, final_lits, final_ptrs, final_itbls)
146 <- mkBits findLabel init_asm_state instrs
148 let asm_insns = ssElts final_insns
149 n_insns = sizeSS final_insns
152 | n_insns > 65535 = panic "linkBCO: >= 64k insns in BCO"
153 | otherwise = mkInstrArray n_insns asm_insns
154 insns_barr = case insns_arr of UArray _lo _hi barr -> barr
156 bitmap_arr = mkBitmapArray bsize bitmap
157 bitmap_barr = case bitmap_arr of UArray _lo _hi barr -> barr
159 let ul_bco = UnlinkedBCO nm arity insns_barr bitmap_barr final_lits
160 final_ptrs final_itbls
162 -- 8 Aug 01: Finalisers aren't safe when attached to non-primitive
163 -- objects, since they might get run too early. Disable this until
164 -- we figure out what to do.
165 -- when (notNull malloced) (addFinalizer ul_bco (mapM_ zonk malloced))
169 zonk ptr = do -- putStrLn ("freeing malloc'd block at " ++ show (A# a#))
172 mkBitmapArray :: Int -> [StgWord] -> UArray Int StgWord
173 mkBitmapArray bsize bitmap
174 = listArray (0, 1 + length bitmap) (fromIntegral bsize : bitmap)
176 mkInstrArray :: Int -> [Word16] -> UArray Int Word16
177 mkInstrArray n_insns asm_insns
178 = listArray (0, 1 + n_insns) (fromIntegral n_insns : asm_insns)
180 -- instrs nonptrs ptrs itbls
181 type AsmState = (SizedSeq Word16,
182 SizedSeq (Either Word FastString),
186 data SizedSeq a = SizedSeq !Int [a]
187 emptySS = SizedSeq 0 []
189 -- Why are these two monadic???
190 addToSS (SizedSeq n r_xs) x = return (SizedSeq (n+1) (x:r_xs))
191 addListToSS (SizedSeq n r_xs) xs
192 = return (SizedSeq (n + length xs) (reverse xs ++ r_xs))
194 ssElts :: SizedSeq a -> [a]
195 ssElts (SizedSeq n r_xs) = reverse r_xs
197 sizeSS :: SizedSeq a -> Int
198 sizeSS (SizedSeq n r_xs) = n
200 -- Bring in all the bci_ bytecode constants.
201 #include "Bytecodes.h"
203 -- This is where all the action is (pass 2 of the assembler)
204 mkBits :: (Int -> Int) -- label finder
206 -> [BCInstr] -- instructions (in)
209 mkBits findLabel st proto_insns
210 = foldM doInstr st proto_insns
212 doInstr :: AsmState -> BCInstr -> IO AsmState
215 STKCHECK n -> instr2 st bci_STKCHECK n
216 PUSH_L o1 -> instr2 st bci_PUSH_L o1
217 PUSH_LL o1 o2 -> instr3 st bci_PUSH_LL o1 o2
218 PUSH_LLL o1 o2 o3 -> instr4 st bci_PUSH_LLL o1 o2 o3
219 PUSH_G nm -> do (p, st2) <- ptr st (BCOPtrName nm)
220 instr2 st2 bci_PUSH_G p
221 PUSH_PRIMOP op -> do (p, st2) <- ptr st (BCOPtrPrimOp op)
222 instr2 st2 bci_PUSH_G p
223 PUSH_BCO proto -> do ul_bco <- assembleBCO proto
224 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
225 instr2 st2 bci_PUSH_G p
226 PUSH_ALTS proto -> do ul_bco <- assembleBCO proto
227 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
228 instr2 st2 bci_PUSH_ALTS p
229 PUSH_ALTS_UNLIFTED proto pk -> do
230 ul_bco <- assembleBCO proto
231 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
232 instr2 st2 (push_alts pk) p
233 PUSH_UBX (Left lit) nws
234 -> do (np, st2) <- literal st lit
235 instr3 st2 bci_PUSH_UBX np nws
236 PUSH_UBX (Right aa) nws
237 -> do (np, st2) <- addr st aa
238 instr3 st2 bci_PUSH_UBX np nws
240 PUSH_APPLY_N -> do instr1 st bci_PUSH_APPLY_N
241 PUSH_APPLY_V -> do instr1 st bci_PUSH_APPLY_V
242 PUSH_APPLY_F -> do instr1 st bci_PUSH_APPLY_F
243 PUSH_APPLY_D -> do instr1 st bci_PUSH_APPLY_D
244 PUSH_APPLY_L -> do instr1 st bci_PUSH_APPLY_L
245 PUSH_APPLY_P -> do instr1 st bci_PUSH_APPLY_P
246 PUSH_APPLY_PP -> do instr1 st bci_PUSH_APPLY_PP
247 PUSH_APPLY_PPP -> do instr1 st bci_PUSH_APPLY_PPP
248 PUSH_APPLY_PPPP -> do instr1 st bci_PUSH_APPLY_PPPP
249 PUSH_APPLY_PPPPP -> do instr1 st bci_PUSH_APPLY_PPPPP
250 PUSH_APPLY_PPPPPP -> do instr1 st bci_PUSH_APPLY_PPPPPP
251 PUSH_APPLY_PPPPPPP -> do instr1 st bci_PUSH_APPLY_PPPPPPP
253 SLIDE n by -> instr3 st bci_SLIDE n by
254 ALLOC_AP n -> instr2 st bci_ALLOC_AP n
255 ALLOC_PAP arity n -> instr3 st bci_ALLOC_PAP arity n
256 MKAP off sz -> instr3 st bci_MKAP off sz
257 UNPACK n -> instr2 st bci_UNPACK n
258 PACK dcon sz -> do (itbl_no,st2) <- itbl st dcon
259 instr3 st2 bci_PACK itbl_no sz
260 LABEL lab -> return st
261 TESTLT_I i l -> do (np, st2) <- int st i
262 instr3 st2 bci_TESTLT_I np (findLabel l)
263 TESTEQ_I i l -> do (np, st2) <- int st i
264 instr3 st2 bci_TESTEQ_I np (findLabel l)
265 TESTLT_F f l -> do (np, st2) <- float st f
266 instr3 st2 bci_TESTLT_F np (findLabel l)
267 TESTEQ_F f l -> do (np, st2) <- float st f
268 instr3 st2 bci_TESTEQ_F np (findLabel l)
269 TESTLT_D d l -> do (np, st2) <- double st d
270 instr3 st2 bci_TESTLT_D np (findLabel l)
271 TESTEQ_D d l -> do (np, st2) <- double st d
272 instr3 st2 bci_TESTEQ_D np (findLabel l)
273 TESTLT_P i l -> instr3 st bci_TESTLT_P i (findLabel l)
274 TESTEQ_P i l -> instr3 st bci_TESTEQ_P i (findLabel l)
275 CASEFAIL -> instr1 st bci_CASEFAIL
276 SWIZZLE stkoff n -> instr3 st bci_SWIZZLE stkoff n
277 JMP l -> instr2 st bci_JMP (findLabel l)
278 ENTER -> instr1 st bci_ENTER
279 RETURN -> instr1 st bci_RETURN
280 RETURN_UBX rep -> instr1 st (return_ubx rep)
281 CCALL off m_addr -> do (np, st2) <- addr st m_addr
282 instr3 st2 bci_CCALL off np
287 instr1 (st_i0,st_l0,st_p0,st_I0) i1
288 = do st_i1 <- addToSS st_i0 i1
289 return (st_i1,st_l0,st_p0,st_I0)
291 instr2 (st_i0,st_l0,st_p0,st_I0) i1 i2
292 = do st_i1 <- addToSS st_i0 (i2s i1)
293 st_i2 <- addToSS st_i1 (i2s i2)
294 return (st_i2,st_l0,st_p0,st_I0)
296 instr3 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3
297 = do st_i1 <- addToSS st_i0 (i2s i1)
298 st_i2 <- addToSS st_i1 (i2s i2)
299 st_i3 <- addToSS st_i2 (i2s i3)
300 return (st_i3,st_l0,st_p0,st_I0)
302 instr4 (st_i0,st_l0,st_p0,st_I0) i1 i2 i3 i4
303 = do st_i1 <- addToSS st_i0 (i2s i1)
304 st_i2 <- addToSS st_i1 (i2s i2)
305 st_i3 <- addToSS st_i2 (i2s i3)
306 st_i4 <- addToSS st_i3 (i2s i4)
307 return (st_i4,st_l0,st_p0,st_I0)
309 float (st_i0,st_l0,st_p0,st_I0) f
310 = do let ws = mkLitF f
311 st_l1 <- addListToSS st_l0 (map Left ws)
312 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
314 double (st_i0,st_l0,st_p0,st_I0) d
315 = do let ws = mkLitD d
316 st_l1 <- addListToSS st_l0 (map Left ws)
317 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
319 int (st_i0,st_l0,st_p0,st_I0) i
320 = do let ws = mkLitI i
321 st_l1 <- addListToSS st_l0 (map Left ws)
322 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
324 int64 (st_i0,st_l0,st_p0,st_I0) i
325 = do let ws = mkLitI64 i
326 st_l1 <- addListToSS st_l0 (map Left ws)
327 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
329 addr (st_i0,st_l0,st_p0,st_I0) a
330 = do let ws = mkLitPtr a
331 st_l1 <- addListToSS st_l0 (map Left ws)
332 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
334 litlabel (st_i0,st_l0,st_p0,st_I0) fs
335 = do st_l1 <- addListToSS st_l0 [Right fs]
336 return (sizeSS st_l0, (st_i0,st_l1,st_p0,st_I0))
338 ptr (st_i0,st_l0,st_p0,st_I0) p
339 = do st_p1 <- addToSS st_p0 p
340 return (sizeSS st_p0, (st_i0,st_l0,st_p1,st_I0))
342 itbl (st_i0,st_l0,st_p0,st_I0) dcon
343 = do st_I1 <- addToSS st_I0 (getName dcon)
344 return (sizeSS st_I0, (st_i0,st_l0,st_p0,st_I1))
346 literal st (MachLabel fs) = litlabel st fs
347 literal st (MachWord w) = int st (fromIntegral w)
348 literal st (MachInt j) = int st (fromIntegral j)
349 literal st (MachFloat r) = float st (fromRational r)
350 literal st (MachDouble r) = double st (fromRational r)
351 literal st (MachChar c) = int st c
352 literal st (MachInt64 ii) = int64 st (fromIntegral ii)
353 literal st (MachWord64 ii) = int64 st (fromIntegral ii)
354 literal st other = pprPanic "ByteCodeLink.literal" (ppr other)
357 push_alts WordRep = bci_PUSH_ALTS_N
358 push_alts IntRep = bci_PUSH_ALTS_N
359 push_alts AddrRep = bci_PUSH_ALTS_N
360 push_alts CharRep = bci_PUSH_ALTS_N
361 push_alts FloatRep = bci_PUSH_ALTS_F
362 push_alts DoubleRep = bci_PUSH_ALTS_D
363 push_alts VoidRep = bci_PUSH_ALTS_V
365 | is64BitRep pk = bci_PUSH_ALTS_L
366 | isFollowableRep pk = bci_PUSH_ALTS_P
368 return_ubx WordRep = bci_RETURN_N
369 return_ubx IntRep = bci_RETURN_N
370 return_ubx AddrRep = bci_RETURN_N
371 return_ubx CharRep = bci_RETURN_N
372 return_ubx FloatRep = bci_RETURN_F
373 return_ubx DoubleRep = bci_RETURN_D
374 return_ubx VoidRep = bci_RETURN_V
376 | is64BitRep pk = bci_RETURN_L
377 | isFollowableRep pk = bci_RETURN_P
380 -- The size in 16-bit entities of an instruction.
381 instrSize16s :: BCInstr -> Int
392 PUSH_ALTS_UNLIFTED{} -> 2
401 PUSH_APPLY_PPP{} -> 1
402 PUSH_APPLY_PPPP{} -> 1
403 PUSH_APPLY_PPPPP{} -> 1
404 PUSH_APPLY_PPPPPP{} -> 1
405 PUSH_APPLY_PPPPPPP{} -> 1
429 -- Make lists of host-sized words for literals, so that when the
430 -- words are placed in memory at increasing addresses, the
431 -- bit pattern is correct for the host's word size and endianness.
432 mkLitI :: Int -> [Word]
433 mkLitF :: Float -> [Word]
434 mkLitD :: Double -> [Word]
435 mkLitPtr :: Ptr () -> [Word]
436 mkLitI64 :: Int64 -> [Word]
440 arr <- newArray_ ((0::Int),0)
442 f_arr <- castSTUArray arr
443 w0 <- readArray f_arr 0
450 arr <- newArray_ ((0::Int),1)
452 d_arr <- castSTUArray arr
453 w0 <- readArray d_arr 0
454 w1 <- readArray d_arr 1
455 return [w0 :: Word, w1]
459 arr <- newArray_ ((0::Int),0)
461 d_arr <- castSTUArray arr
462 w0 <- readArray d_arr 0
469 arr <- newArray_ ((0::Int),1)
471 d_arr <- castSTUArray arr
472 w0 <- readArray d_arr 0
473 w1 <- readArray d_arr 1
474 return [w0 :: Word,w1]
478 arr <- newArray_ ((0::Int),0)
480 d_arr <- castSTUArray arr
481 w0 <- readArray d_arr 0
487 arr <- newArray_ ((0::Int),0)
489 i_arr <- castSTUArray arr
490 w0 <- readArray i_arr 0
496 arr <- newArray_ ((0::Int),0)
498 a_arr <- castSTUArray arr
499 w0 <- readArray a_arr 0
503 iNTERP_STACK_CHECK_THRESH = (INTERP_STACK_CHECK_THRESH :: Int)