2 % (c) The University of Glasgow 2002-2006
5 ByteCodeLink: Bytecode assembler and linker
8 {-# OPTIONS -optc-DNON_POSIX_SOURCE #-}
11 assembleBCOs, assembleBCO,
14 UnlinkedBCO(..), BCOPtr(..), BCONPtr(..), bcoFreeNames,
15 SizedSeq, sizeSS, ssElts,
16 iNTERP_STACK_CHECK_THRESH
19 #include "HsVersions.h"
36 import Control.Monad ( foldM )
37 import Control.Monad.ST ( runST )
39 import Data.Array.MArray
40 import Data.Array.Unboxed ( listArray )
41 import Data.Array.Base ( UArray(..) )
42 import Data.Array.ST ( castSTUArray )
45 import Data.Int ( Int64 )
46 import Data.Char ( ord )
48 import GHC.Base ( ByteArray#, MutableByteArray#, RealWorld )
49 import GHC.IOBase ( IO(..) )
50 import GHC.Ptr ( Ptr(..) )
52 -- -----------------------------------------------------------------------------
55 -- CompiledByteCode represents the result of byte-code
56 -- compiling a bunch of functions and data types
59 = ByteCode [UnlinkedBCO] -- Bunch of interpretable bindings
60 ItblEnv -- A mapping from DataCons to their itbls
62 instance Outputable CompiledByteCode where
63 ppr (ByteCode bcos _) = ppr bcos
68 unlinkedBCOName :: Name,
69 unlinkedBCOArity :: Int,
70 unlinkedBCOInstrs :: ByteArray#, -- insns
71 unlinkedBCOBitmap :: ByteArray#, -- bitmap
72 unlinkedBCOLits :: (SizedSeq BCONPtr), -- non-ptrs
73 unlinkedBCOPtrs :: (SizedSeq BCOPtr) -- ptrs
79 | BCOPtrBCO UnlinkedBCO
80 | BCOPtrBreakInfo BreakInfo
81 | BCOPtrArray (MutableByteArray# RealWorld)
85 | BCONPtrLbl FastString
88 -- | Finds external references. Remember to remove the names
89 -- defined by this group of BCOs themselves
90 bcoFreeNames :: UnlinkedBCO -> NameSet
92 = bco_refs bco `minusNameSet` mkNameSet [unlinkedBCOName bco]
94 bco_refs (UnlinkedBCO _ _ _ _ nonptrs ptrs)
96 mkNameSet [ n | BCOPtrName n <- ssElts ptrs ] :
97 mkNameSet [ n | BCONPtrItbl n <- ssElts nonptrs ] :
98 map bco_refs [ bco | BCOPtrBCO bco <- ssElts ptrs ]
101 instance Outputable UnlinkedBCO where
102 ppr (UnlinkedBCO nm _arity _insns _bitmap lits ptrs)
103 = sep [text "BCO", ppr nm, text "with",
104 int (sizeSS lits), text "lits",
105 int (sizeSS ptrs), text "ptrs" ]
107 -- -----------------------------------------------------------------------------
108 -- The bytecode assembler
110 -- The object format for bytecodes is: 16 bits for the opcode, and 16
111 -- for each field -- so the code can be considered a sequence of
112 -- 16-bit ints. Each field denotes either a stack offset or number of
113 -- items on the stack (eg SLIDE), and index into the pointer table (eg
114 -- PUSH_G), an index into the literal table (eg PUSH_I/D/L), or a
115 -- bytecode address in this BCO.
117 -- Top level assembler fn.
118 assembleBCOs :: [ProtoBCO Name] -> [TyCon] -> IO CompiledByteCode
119 assembleBCOs proto_bcos tycons
120 = do itblenv <- mkITbls tycons
121 bcos <- mapM assembleBCO proto_bcos
122 return (ByteCode bcos itblenv)
124 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
125 assembleBCO (ProtoBCO nm instrs bitmap bsize arity _origin _malloced)
127 -- pass 1: collect up the offsets of the local labels.
128 -- Remember that the first insn starts at offset 1 since offset 0
129 -- (eventually) will hold the total # of insns.
130 label_env = mkLabelEnv emptyFM 1 instrs
132 mkLabelEnv env _ [] = env
133 mkLabelEnv env i_offset (i:is)
135 = case i of LABEL n -> addToFM env n i_offset ; _ -> env
136 in mkLabelEnv new_env (i_offset + instrSize16s i) is
139 = case lookupFM label_env lab of
140 Just bco_offset -> bco_offset
141 Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
143 do -- pass 2: generate the instruction, ptr and nonptr bits
144 insns <- return emptySS :: IO (SizedSeq Word16)
145 lits <- return emptySS :: IO (SizedSeq BCONPtr)
146 ptrs <- return emptySS :: IO (SizedSeq BCOPtr)
147 let init_asm_state = (insns,lits,ptrs)
148 (final_insns, final_lits, final_ptrs)
149 <- mkBits findLabel init_asm_state instrs
151 let asm_insns = ssElts final_insns
152 n_insns = sizeSS final_insns
155 | n_insns > 65535 = panic "linkBCO: >= 64k insns in BCO"
156 | otherwise = mkInstrArray n_insns asm_insns
157 !insns_barr = case insns_arr of UArray _lo _hi _n barr -> barr
159 bitmap_arr = mkBitmapArray bsize bitmap
160 !bitmap_barr = case bitmap_arr of UArray _lo _hi _n barr -> barr
162 let ul_bco = UnlinkedBCO nm arity insns_barr bitmap_barr final_lits final_ptrs
164 -- 8 Aug 01: Finalisers aren't safe when attached to non-primitive
165 -- objects, since they might get run too early. Disable this until
166 -- we figure out what to do.
167 -- when (notNull malloced) (addFinalizer ul_bco (mapM_ zonk malloced))
171 -- zonk ptr = do -- putStrLn ("freeing malloc'd block at " ++ show (A# a#))
174 mkBitmapArray :: Int -> [StgWord] -> UArray Int StgWord
175 mkBitmapArray bsize bitmap
176 = listArray (0, length bitmap) (fromIntegral bsize : bitmap)
178 mkInstrArray :: Int -> [Word16] -> UArray Int Word16
179 mkInstrArray n_insns asm_insns
180 = listArray (0, n_insns) (fromIntegral n_insns : asm_insns)
182 -- instrs nonptrs ptrs
183 type AsmState = (SizedSeq Word16,
187 data SizedSeq a = SizedSeq !Int [a]
188 emptySS :: SizedSeq a
189 emptySS = SizedSeq 0 []
191 -- Why are these two monadic???
192 addToSS :: SizedSeq a -> a -> IO (SizedSeq a)
193 addToSS (SizedSeq n r_xs) x = return (SizedSeq (n+1) (x:r_xs))
194 addListToSS :: SizedSeq a -> [a] -> IO (SizedSeq a)
195 addListToSS (SizedSeq n r_xs) xs
196 = return (SizedSeq (n + length xs) (reverse xs ++ r_xs))
198 ssElts :: SizedSeq a -> [a]
199 ssElts (SizedSeq _ r_xs) = reverse r_xs
201 sizeSS :: SizedSeq a -> Int
202 sizeSS (SizedSeq n _) = n
204 -- Bring in all the bci_ bytecode constants.
205 #include "Bytecodes.h"
207 largeArgInstr :: Int -> Int
208 largeArgInstr bci = bci_FLAG_LARGE_ARGS .|. bci
210 largeArg :: Int -> [Int]
212 | wORD_SIZE_IN_BITS == 64
213 = [(i .&. 0xFFFF000000000000) `shiftR` 48,
214 (i .&. 0x0000FFFF00000000) `shiftR` 32,
215 (i .&. 0x00000000FFFF0000) `shiftR` 16,
216 (i .&. 0x000000000000FFFF)]
217 | wORD_SIZE_IN_BITS == 32
218 = [(i .&. 0xFFFF0000) `shiftR` 16,
220 | otherwise = error "wORD_SIZE_IN_BITS not 32 or 64?"
222 -- This is where all the action is (pass 2 of the assembler)
223 mkBits :: (Int -> Int) -- label finder
225 -> [BCInstr] -- instructions (in)
228 mkBits findLabel st proto_insns
229 = foldM doInstr st proto_insns
231 doInstr :: AsmState -> BCInstr -> IO AsmState
236 instrn st (largeArgInstr bci_STKCHECK : largeArg n)
237 | otherwise -> instr2 st bci_STKCHECK n
238 PUSH_L o1 -> instr2 st bci_PUSH_L o1
239 PUSH_LL o1 o2 -> instr3 st bci_PUSH_LL o1 o2
240 PUSH_LLL o1 o2 o3 -> instr4 st bci_PUSH_LLL o1 o2 o3
241 PUSH_G nm -> do (p, st2) <- ptr st (BCOPtrName nm)
242 instr2 st2 bci_PUSH_G p
243 PUSH_PRIMOP op -> do (p, st2) <- ptr st (BCOPtrPrimOp op)
244 instr2 st2 bci_PUSH_G p
245 PUSH_BCO proto -> do ul_bco <- assembleBCO proto
246 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
247 instr2 st2 bci_PUSH_G p
248 PUSH_ALTS proto -> do ul_bco <- assembleBCO proto
249 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
250 instr2 st2 bci_PUSH_ALTS p
251 PUSH_ALTS_UNLIFTED proto pk -> do
252 ul_bco <- assembleBCO proto
253 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
254 instr2 st2 (push_alts pk) p
255 PUSH_UBX (Left lit) nws
256 -> do (np, st2) <- literal st lit
257 instr3 st2 bci_PUSH_UBX np nws
258 PUSH_UBX (Right aa) nws
259 -> do (np, st2) <- addr st aa
260 instr3 st2 bci_PUSH_UBX np nws
262 PUSH_APPLY_N -> do instr1 st bci_PUSH_APPLY_N
263 PUSH_APPLY_V -> do instr1 st bci_PUSH_APPLY_V
264 PUSH_APPLY_F -> do instr1 st bci_PUSH_APPLY_F
265 PUSH_APPLY_D -> do instr1 st bci_PUSH_APPLY_D
266 PUSH_APPLY_L -> do instr1 st bci_PUSH_APPLY_L
267 PUSH_APPLY_P -> do instr1 st bci_PUSH_APPLY_P
268 PUSH_APPLY_PP -> do instr1 st bci_PUSH_APPLY_PP
269 PUSH_APPLY_PPP -> do instr1 st bci_PUSH_APPLY_PPP
270 PUSH_APPLY_PPPP -> do instr1 st bci_PUSH_APPLY_PPPP
271 PUSH_APPLY_PPPPP -> do instr1 st bci_PUSH_APPLY_PPPPP
272 PUSH_APPLY_PPPPPP -> do instr1 st bci_PUSH_APPLY_PPPPPP
274 SLIDE n by -> instr3 st bci_SLIDE n by
275 ALLOC_AP n -> instr2 st bci_ALLOC_AP n
276 ALLOC_AP_NOUPD n -> instr2 st bci_ALLOC_AP_NOUPD n
277 ALLOC_PAP arity n -> instr3 st bci_ALLOC_PAP arity n
278 MKAP off sz -> instr3 st bci_MKAP off sz
279 MKPAP off sz -> instr3 st bci_MKPAP off sz
280 UNPACK n -> instr2 st bci_UNPACK n
281 PACK dcon sz -> do (itbl_no,st2) <- itbl st dcon
282 instr3 st2 bci_PACK itbl_no sz
284 TESTLT_I i l -> do (np, st2) <- int st i
285 instr3 st2 bci_TESTLT_I np (findLabel l)
286 TESTEQ_I i l -> do (np, st2) <- int st i
287 instr3 st2 bci_TESTEQ_I np (findLabel l)
288 TESTLT_F f l -> do (np, st2) <- float st f
289 instr3 st2 bci_TESTLT_F np (findLabel l)
290 TESTEQ_F f l -> do (np, st2) <- float st f
291 instr3 st2 bci_TESTEQ_F np (findLabel l)
292 TESTLT_D d l -> do (np, st2) <- double st d
293 instr3 st2 bci_TESTLT_D np (findLabel l)
294 TESTEQ_D d l -> do (np, st2) <- double st d
295 instr3 st2 bci_TESTEQ_D np (findLabel l)
296 TESTLT_P i l -> instr3 st bci_TESTLT_P i (findLabel l)
297 TESTEQ_P i l -> instr3 st bci_TESTEQ_P i (findLabel l)
298 CASEFAIL -> instr1 st bci_CASEFAIL
299 SWIZZLE stkoff n -> instr3 st bci_SWIZZLE stkoff n
300 JMP l -> instr2 st bci_JMP (findLabel l)
301 ENTER -> instr1 st bci_ENTER
302 RETURN -> instr1 st bci_RETURN
303 RETURN_UBX rep -> instr1 st (return_ubx rep)
304 CCALL off m_addr -> do (np, st2) <- addr st m_addr
305 instr3 st2 bci_CCALL off np
306 BRK_FUN array index info -> do
307 (p1, st2) <- ptr st (BCOPtrArray array)
308 (p2, st3) <- ptr st2 (BCOPtrBreakInfo info)
309 instr4 st3 bci_BRK_FUN p1 index p2
314 instrn :: AsmState -> [Int] -> IO AsmState
315 instrn st [] = return st
316 instrn (st_i, st_l, st_p) (i:is)
317 = do st_i' <- addToSS st_i (i2s i)
318 instrn (st_i', st_l, st_p) is
320 instr1 (st_i0,st_l0,st_p0) i1
321 = do st_i1 <- addToSS st_i0 i1
322 return (st_i1,st_l0,st_p0)
324 instr2 (st_i0,st_l0,st_p0) i1 i2
325 = do st_i1 <- addToSS st_i0 (i2s i1)
326 st_i2 <- addToSS st_i1 (i2s i2)
327 return (st_i2,st_l0,st_p0)
329 instr3 (st_i0,st_l0,st_p0) i1 i2 i3
330 = do st_i1 <- addToSS st_i0 (i2s i1)
331 st_i2 <- addToSS st_i1 (i2s i2)
332 st_i3 <- addToSS st_i2 (i2s i3)
333 return (st_i3,st_l0,st_p0)
335 instr4 (st_i0,st_l0,st_p0) i1 i2 i3 i4
336 = do st_i1 <- addToSS st_i0 (i2s i1)
337 st_i2 <- addToSS st_i1 (i2s i2)
338 st_i3 <- addToSS st_i2 (i2s i3)
339 st_i4 <- addToSS st_i3 (i2s i4)
340 return (st_i4,st_l0,st_p0)
342 float (st_i0,st_l0,st_p0) f
343 = do let ws = mkLitF f
344 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
345 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
347 double (st_i0,st_l0,st_p0) d
348 = do let ws = mkLitD d
349 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
350 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
352 int (st_i0,st_l0,st_p0) i
353 = do let ws = mkLitI i
354 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
355 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
357 int64 (st_i0,st_l0,st_p0) i
358 = do let ws = mkLitI64 i
359 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
360 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
362 addr (st_i0,st_l0,st_p0) a
363 = do let ws = mkLitPtr a
364 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
365 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
367 litlabel (st_i0,st_l0,st_p0) fs
368 = do st_l1 <- addListToSS st_l0 [BCONPtrLbl fs]
369 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
371 ptr (st_i0,st_l0,st_p0) p
372 = do st_p1 <- addToSS st_p0 p
373 return (sizeSS st_p0, (st_i0,st_l0,st_p1))
375 itbl (st_i0,st_l0,st_p0) dcon
376 = do st_l1 <- addToSS st_l0 (BCONPtrItbl (getName dcon))
377 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
379 #ifdef mingw32_TARGET_OS
380 literal st (MachLabel fs (Just sz) _)
381 = litlabel st (appendFS fs (mkFastString ('@':show sz)))
382 -- On Windows, stdcall labels have a suffix indicating the no. of
383 -- arg words, e.g. foo@8. testcase: ffi012(ghci)
385 literal st (MachLabel fs _ _) = litlabel st fs
386 literal st (MachWord w) = int st (fromIntegral w)
387 literal st (MachInt j) = int st (fromIntegral j)
388 literal st MachNullAddr = int st (fromIntegral 0)
389 literal st (MachFloat r) = float st (fromRational r)
390 literal st (MachDouble r) = double st (fromRational r)
391 literal st (MachChar c) = int st (ord c)
392 literal st (MachInt64 ii) = int64 st (fromIntegral ii)
393 literal st (MachWord64 ii) = int64 st (fromIntegral ii)
394 literal _ other = pprPanic "ByteCodeAsm.literal" (ppr other)
397 push_alts :: CgRep -> Int
398 push_alts NonPtrArg = bci_PUSH_ALTS_N
399 push_alts FloatArg = bci_PUSH_ALTS_F
400 push_alts DoubleArg = bci_PUSH_ALTS_D
401 push_alts VoidArg = bci_PUSH_ALTS_V
402 push_alts LongArg = bci_PUSH_ALTS_L
403 push_alts PtrArg = bci_PUSH_ALTS_P
405 return_ubx :: CgRep -> Word16
406 return_ubx NonPtrArg = bci_RETURN_N
407 return_ubx FloatArg = bci_RETURN_F
408 return_ubx DoubleArg = bci_RETURN_D
409 return_ubx VoidArg = bci_RETURN_V
410 return_ubx LongArg = bci_RETURN_L
411 return_ubx PtrArg = bci_RETURN_P
414 -- The size in 16-bit entities of an instruction.
415 instrSize16s :: BCInstr -> Int
426 PUSH_ALTS_UNLIFTED{} -> 2
435 PUSH_APPLY_PPP{} -> 1
436 PUSH_APPLY_PPPP{} -> 1
437 PUSH_APPLY_PPPPP{} -> 1
438 PUSH_APPLY_PPPPPP{} -> 1
441 ALLOC_AP_NOUPD{} -> 2
465 -- Make lists of host-sized words for literals, so that when the
466 -- words are placed in memory at increasing addresses, the
467 -- bit pattern is correct for the host's word size and endianness.
468 mkLitI :: Int -> [Word]
469 mkLitF :: Float -> [Word]
470 mkLitD :: Double -> [Word]
471 mkLitPtr :: Ptr () -> [Word]
472 mkLitI64 :: Int64 -> [Word]
476 arr <- newArray_ ((0::Int),0)
478 f_arr <- castSTUArray arr
479 w0 <- readArray f_arr 0
486 arr <- newArray_ ((0::Int),1)
488 d_arr <- castSTUArray arr
489 w0 <- readArray d_arr 0
490 w1 <- readArray d_arr 1
491 return [w0 :: Word, w1]
495 arr <- newArray_ ((0::Int),0)
497 d_arr <- castSTUArray arr
498 w0 <- readArray d_arr 0
502 = panic "mkLitD: Bad wORD_SIZE"
507 arr <- newArray_ ((0::Int),1)
509 d_arr <- castSTUArray arr
510 w0 <- readArray d_arr 0
511 w1 <- readArray d_arr 1
512 return [w0 :: Word,w1]
516 arr <- newArray_ ((0::Int),0)
518 d_arr <- castSTUArray arr
519 w0 <- readArray d_arr 0
523 = panic "mkLitI64: Bad wORD_SIZE"
527 arr <- newArray_ ((0::Int),0)
529 i_arr <- castSTUArray arr
530 w0 <- readArray i_arr 0
536 arr <- newArray_ ((0::Int),0)
538 a_arr <- castSTUArray arr
539 w0 <- readArray a_arr 0
543 iNTERP_STACK_CHECK_THRESH :: Int
544 iNTERP_STACK_CHECK_THRESH = INTERP_STACK_CHECK_THRESH