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.Ptr ( Ptr(..) )
51 #if __GLASGOW_HASKELL__ >= 611
52 import GHC.IO ( IO(..) )
54 import GHC.IOBase ( IO(..) )
57 -- -----------------------------------------------------------------------------
60 -- CompiledByteCode represents the result of byte-code
61 -- compiling a bunch of functions and data types
64 = ByteCode [UnlinkedBCO] -- Bunch of interpretable bindings
65 ItblEnv -- A mapping from DataCons to their itbls
67 instance Outputable CompiledByteCode where
68 ppr (ByteCode bcos _) = ppr bcos
73 unlinkedBCOName :: Name,
74 unlinkedBCOArity :: Int,
75 unlinkedBCOInstrs :: ByteArray#, -- insns
76 unlinkedBCOBitmap :: ByteArray#, -- bitmap
77 unlinkedBCOLits :: (SizedSeq BCONPtr), -- non-ptrs
78 unlinkedBCOPtrs :: (SizedSeq BCOPtr) -- ptrs
84 | BCOPtrBCO UnlinkedBCO
85 | BCOPtrBreakInfo BreakInfo
86 | BCOPtrArray (MutableByteArray# RealWorld)
90 | BCONPtrLbl FastString
93 -- | Finds external references. Remember to remove the names
94 -- defined by this group of BCOs themselves
95 bcoFreeNames :: UnlinkedBCO -> NameSet
97 = bco_refs bco `minusNameSet` mkNameSet [unlinkedBCOName bco]
99 bco_refs (UnlinkedBCO _ _ _ _ nonptrs ptrs)
100 = unionManyNameSets (
101 mkNameSet [ n | BCOPtrName n <- ssElts ptrs ] :
102 mkNameSet [ n | BCONPtrItbl n <- ssElts nonptrs ] :
103 map bco_refs [ bco | BCOPtrBCO bco <- ssElts ptrs ]
106 instance Outputable UnlinkedBCO where
107 ppr (UnlinkedBCO nm _arity _insns _bitmap lits ptrs)
108 = sep [text "BCO", ppr nm, text "with",
109 int (sizeSS lits), text "lits",
110 int (sizeSS ptrs), text "ptrs" ]
112 -- -----------------------------------------------------------------------------
113 -- The bytecode assembler
115 -- The object format for bytecodes is: 16 bits for the opcode, and 16
116 -- for each field -- so the code can be considered a sequence of
117 -- 16-bit ints. Each field denotes either a stack offset or number of
118 -- items on the stack (eg SLIDE), and index into the pointer table (eg
119 -- PUSH_G), an index into the literal table (eg PUSH_I/D/L), or a
120 -- bytecode address in this BCO.
122 -- Top level assembler fn.
123 assembleBCOs :: [ProtoBCO Name] -> [TyCon] -> IO CompiledByteCode
124 assembleBCOs proto_bcos tycons
125 = do itblenv <- mkITbls tycons
126 bcos <- mapM assembleBCO proto_bcos
127 return (ByteCode bcos itblenv)
129 assembleBCO :: ProtoBCO Name -> IO UnlinkedBCO
130 assembleBCO (ProtoBCO nm instrs bitmap bsize arity _origin _malloced)
132 -- pass 1: collect up the offsets of the local labels.
133 -- Remember that the first insn starts at offset 1 since offset 0
134 -- (eventually) will hold the total # of insns.
135 label_env = mkLabelEnv emptyFM 1 instrs
137 mkLabelEnv env _ [] = env
138 mkLabelEnv env i_offset (i:is)
140 = case i of LABEL n -> addToFM env n i_offset ; _ -> env
141 in mkLabelEnv new_env (i_offset + instrSize16s i) is
144 = case lookupFM label_env lab of
145 Just bco_offset -> bco_offset
146 Nothing -> pprPanic "assembleBCO.findLabel" (int lab)
148 do -- pass 2: generate the instruction, ptr and nonptr bits
149 insns <- return emptySS :: IO (SizedSeq Word16)
150 lits <- return emptySS :: IO (SizedSeq BCONPtr)
151 ptrs <- return emptySS :: IO (SizedSeq BCOPtr)
152 let init_asm_state = (insns,lits,ptrs)
153 (final_insns, final_lits, final_ptrs)
154 <- mkBits findLabel init_asm_state instrs
156 let asm_insns = ssElts final_insns
157 n_insns = sizeSS final_insns
160 | n_insns > 65535 = panic "linkBCO: >= 64k insns in BCO"
161 | otherwise = mkInstrArray n_insns asm_insns
162 !insns_barr = case insns_arr of UArray _lo _hi _n barr -> barr
164 bitmap_arr = mkBitmapArray bsize bitmap
165 !bitmap_barr = case bitmap_arr of UArray _lo _hi _n barr -> barr
167 let ul_bco = UnlinkedBCO nm arity insns_barr bitmap_barr final_lits final_ptrs
169 -- 8 Aug 01: Finalisers aren't safe when attached to non-primitive
170 -- objects, since they might get run too early. Disable this until
171 -- we figure out what to do.
172 -- when (notNull malloced) (addFinalizer ul_bco (mapM_ zonk malloced))
176 -- zonk ptr = do -- putStrLn ("freeing malloc'd block at " ++ show (A# a#))
179 mkBitmapArray :: Int -> [StgWord] -> UArray Int StgWord
180 mkBitmapArray bsize bitmap
181 = listArray (0, length bitmap) (fromIntegral bsize : bitmap)
183 mkInstrArray :: Int -> [Word16] -> UArray Int Word16
184 mkInstrArray n_insns asm_insns
185 = listArray (0, n_insns) (fromIntegral n_insns : asm_insns)
187 -- instrs nonptrs ptrs
188 type AsmState = (SizedSeq Word16,
192 data SizedSeq a = SizedSeq !Int [a]
193 emptySS :: SizedSeq a
194 emptySS = SizedSeq 0 []
196 -- Why are these two monadic???
197 addToSS :: SizedSeq a -> a -> IO (SizedSeq a)
198 addToSS (SizedSeq n r_xs) x = return (SizedSeq (n+1) (x:r_xs))
199 addListToSS :: SizedSeq a -> [a] -> IO (SizedSeq a)
200 addListToSS (SizedSeq n r_xs) xs
201 = return (SizedSeq (n + length xs) (reverse xs ++ r_xs))
203 ssElts :: SizedSeq a -> [a]
204 ssElts (SizedSeq _ r_xs) = reverse r_xs
206 sizeSS :: SizedSeq a -> Int
207 sizeSS (SizedSeq n _) = n
209 -- Bring in all the bci_ bytecode constants.
210 #include "Bytecodes.h"
212 largeArgInstr :: Int -> Int
213 largeArgInstr bci = bci_FLAG_LARGE_ARGS .|. bci
215 largeArg :: Int -> [Int]
217 | wORD_SIZE_IN_BITS == 64
218 = [(i .&. 0xFFFF000000000000) `shiftR` 48,
219 (i .&. 0x0000FFFF00000000) `shiftR` 32,
220 (i .&. 0x00000000FFFF0000) `shiftR` 16,
221 (i .&. 0x000000000000FFFF)]
222 | wORD_SIZE_IN_BITS == 32
223 = [(i .&. 0xFFFF0000) `shiftR` 16,
225 | otherwise = error "wORD_SIZE_IN_BITS not 32 or 64?"
227 -- This is where all the action is (pass 2 of the assembler)
228 mkBits :: (Int -> Int) -- label finder
230 -> [BCInstr] -- instructions (in)
233 mkBits findLabel st proto_insns
234 = foldM doInstr st proto_insns
236 doInstr :: AsmState -> BCInstr -> IO AsmState
241 instrn st (largeArgInstr bci_STKCHECK : largeArg n)
242 | otherwise -> instr2 st bci_STKCHECK n
243 PUSH_L o1 -> instr2 st bci_PUSH_L o1
244 PUSH_LL o1 o2 -> instr3 st bci_PUSH_LL o1 o2
245 PUSH_LLL o1 o2 o3 -> instr4 st bci_PUSH_LLL o1 o2 o3
246 PUSH_G nm -> do (p, st2) <- ptr st (BCOPtrName nm)
247 instr2 st2 bci_PUSH_G p
248 PUSH_PRIMOP op -> do (p, st2) <- ptr st (BCOPtrPrimOp op)
249 instr2 st2 bci_PUSH_G p
250 PUSH_BCO proto -> do ul_bco <- assembleBCO proto
251 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
252 instr2 st2 bci_PUSH_G p
253 PUSH_ALTS proto -> do ul_bco <- assembleBCO proto
254 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
255 instr2 st2 bci_PUSH_ALTS p
256 PUSH_ALTS_UNLIFTED proto pk -> do
257 ul_bco <- assembleBCO proto
258 (p, st2) <- ptr st (BCOPtrBCO ul_bco)
259 instr2 st2 (push_alts pk) p
260 PUSH_UBX (Left lit) nws
261 -> do (np, st2) <- literal st lit
262 instr3 st2 bci_PUSH_UBX np nws
263 PUSH_UBX (Right aa) nws
264 -> do (np, st2) <- addr st aa
265 instr3 st2 bci_PUSH_UBX np nws
267 PUSH_APPLY_N -> do instr1 st bci_PUSH_APPLY_N
268 PUSH_APPLY_V -> do instr1 st bci_PUSH_APPLY_V
269 PUSH_APPLY_F -> do instr1 st bci_PUSH_APPLY_F
270 PUSH_APPLY_D -> do instr1 st bci_PUSH_APPLY_D
271 PUSH_APPLY_L -> do instr1 st bci_PUSH_APPLY_L
272 PUSH_APPLY_P -> do instr1 st bci_PUSH_APPLY_P
273 PUSH_APPLY_PP -> do instr1 st bci_PUSH_APPLY_PP
274 PUSH_APPLY_PPP -> do instr1 st bci_PUSH_APPLY_PPP
275 PUSH_APPLY_PPPP -> do instr1 st bci_PUSH_APPLY_PPPP
276 PUSH_APPLY_PPPPP -> do instr1 st bci_PUSH_APPLY_PPPPP
277 PUSH_APPLY_PPPPPP -> do instr1 st bci_PUSH_APPLY_PPPPPP
279 SLIDE n by -> instr3 st bci_SLIDE n by
280 ALLOC_AP n -> instr2 st bci_ALLOC_AP n
281 ALLOC_AP_NOUPD n -> instr2 st bci_ALLOC_AP_NOUPD n
282 ALLOC_PAP arity n -> instr3 st bci_ALLOC_PAP arity n
283 MKAP off sz -> instr3 st bci_MKAP off sz
284 MKPAP off sz -> instr3 st bci_MKPAP off sz
285 UNPACK n -> instr2 st bci_UNPACK n
286 PACK dcon sz -> do (itbl_no,st2) <- itbl st dcon
287 instr3 st2 bci_PACK itbl_no sz
289 TESTLT_I i l -> do (np, st2) <- int st i
290 instr3 st2 bci_TESTLT_I np (findLabel l)
291 TESTEQ_I i l -> do (np, st2) <- int st i
292 instr3 st2 bci_TESTEQ_I np (findLabel l)
293 TESTLT_F f l -> do (np, st2) <- float st f
294 instr3 st2 bci_TESTLT_F np (findLabel l)
295 TESTEQ_F f l -> do (np, st2) <- float st f
296 instr3 st2 bci_TESTEQ_F np (findLabel l)
297 TESTLT_D d l -> do (np, st2) <- double st d
298 instr3 st2 bci_TESTLT_D np (findLabel l)
299 TESTEQ_D d l -> do (np, st2) <- double st d
300 instr3 st2 bci_TESTEQ_D np (findLabel l)
301 TESTLT_P i l -> instr3 st bci_TESTLT_P i (findLabel l)
302 TESTEQ_P i l -> instr3 st bci_TESTEQ_P i (findLabel l)
303 CASEFAIL -> instr1 st bci_CASEFAIL
304 SWIZZLE stkoff n -> instr3 st bci_SWIZZLE stkoff n
305 JMP l -> instr2 st bci_JMP (findLabel l)
306 ENTER -> instr1 st bci_ENTER
307 RETURN -> instr1 st bci_RETURN
308 RETURN_UBX rep -> instr1 st (return_ubx rep)
309 CCALL off m_addr -> do (np, st2) <- addr st m_addr
310 instr3 st2 bci_CCALL off np
311 BRK_FUN array index info -> do
312 (p1, st2) <- ptr st (BCOPtrArray array)
313 (p2, st3) <- ptr st2 (BCOPtrBreakInfo info)
314 instr4 st3 bci_BRK_FUN p1 index p2
319 instrn :: AsmState -> [Int] -> IO AsmState
320 instrn st [] = return st
321 instrn (st_i, st_l, st_p) (i:is)
322 = do st_i' <- addToSS st_i (i2s i)
323 instrn (st_i', st_l, st_p) is
325 instr1 (st_i0,st_l0,st_p0) i1
326 = do st_i1 <- addToSS st_i0 i1
327 return (st_i1,st_l0,st_p0)
329 instr2 (st_i0,st_l0,st_p0) i1 i2
330 = do st_i1 <- addToSS st_i0 (i2s i1)
331 st_i2 <- addToSS st_i1 (i2s i2)
332 return (st_i2,st_l0,st_p0)
334 instr3 (st_i0,st_l0,st_p0) i1 i2 i3
335 = do st_i1 <- addToSS st_i0 (i2s i1)
336 st_i2 <- addToSS st_i1 (i2s i2)
337 st_i3 <- addToSS st_i2 (i2s i3)
338 return (st_i3,st_l0,st_p0)
340 instr4 (st_i0,st_l0,st_p0) i1 i2 i3 i4
341 = do st_i1 <- addToSS st_i0 (i2s i1)
342 st_i2 <- addToSS st_i1 (i2s i2)
343 st_i3 <- addToSS st_i2 (i2s i3)
344 st_i4 <- addToSS st_i3 (i2s i4)
345 return (st_i4,st_l0,st_p0)
347 float (st_i0,st_l0,st_p0) f
348 = do let ws = mkLitF f
349 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
350 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
352 double (st_i0,st_l0,st_p0) d
353 = do let ws = mkLitD d
354 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
355 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
357 int (st_i0,st_l0,st_p0) i
358 = do let ws = mkLitI i
359 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
360 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
362 int64 (st_i0,st_l0,st_p0) i
363 = do let ws = mkLitI64 i
364 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
365 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
367 addr (st_i0,st_l0,st_p0) a
368 = do let ws = mkLitPtr a
369 st_l1 <- addListToSS st_l0 (map BCONPtrWord ws)
370 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
372 litlabel (st_i0,st_l0,st_p0) fs
373 = do st_l1 <- addListToSS st_l0 [BCONPtrLbl fs]
374 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
376 ptr (st_i0,st_l0,st_p0) p
377 = do st_p1 <- addToSS st_p0 p
378 return (sizeSS st_p0, (st_i0,st_l0,st_p1))
380 itbl (st_i0,st_l0,st_p0) dcon
381 = do st_l1 <- addToSS st_l0 (BCONPtrItbl (getName dcon))
382 return (sizeSS st_l0, (st_i0,st_l1,st_p0))
384 #ifdef mingw32_TARGET_OS
385 literal st (MachLabel fs (Just sz) _)
386 = litlabel st (appendFS fs (mkFastString ('@':show sz)))
387 -- On Windows, stdcall labels have a suffix indicating the no. of
388 -- arg words, e.g. foo@8. testcase: ffi012(ghci)
390 literal st (MachLabel fs _ _) = litlabel st fs
391 literal st (MachWord w) = int st (fromIntegral w)
392 literal st (MachInt j) = int st (fromIntegral j)
393 literal st MachNullAddr = int st 0
394 literal st (MachFloat r) = float st (fromRational r)
395 literal st (MachDouble r) = double st (fromRational r)
396 literal st (MachChar c) = int st (ord c)
397 literal st (MachInt64 ii) = int64 st (fromIntegral ii)
398 literal st (MachWord64 ii) = int64 st (fromIntegral ii)
399 literal _ other = pprPanic "ByteCodeAsm.literal" (ppr other)
402 push_alts :: CgRep -> Int
403 push_alts NonPtrArg = bci_PUSH_ALTS_N
404 push_alts FloatArg = bci_PUSH_ALTS_F
405 push_alts DoubleArg = bci_PUSH_ALTS_D
406 push_alts VoidArg = bci_PUSH_ALTS_V
407 push_alts LongArg = bci_PUSH_ALTS_L
408 push_alts PtrArg = bci_PUSH_ALTS_P
410 return_ubx :: CgRep -> Word16
411 return_ubx NonPtrArg = bci_RETURN_N
412 return_ubx FloatArg = bci_RETURN_F
413 return_ubx DoubleArg = bci_RETURN_D
414 return_ubx VoidArg = bci_RETURN_V
415 return_ubx LongArg = bci_RETURN_L
416 return_ubx PtrArg = bci_RETURN_P
419 -- The size in 16-bit entities of an instruction.
420 instrSize16s :: BCInstr -> Int
431 PUSH_ALTS_UNLIFTED{} -> 2
440 PUSH_APPLY_PPP{} -> 1
441 PUSH_APPLY_PPPP{} -> 1
442 PUSH_APPLY_PPPPP{} -> 1
443 PUSH_APPLY_PPPPPP{} -> 1
446 ALLOC_AP_NOUPD{} -> 2
470 -- Make lists of host-sized words for literals, so that when the
471 -- words are placed in memory at increasing addresses, the
472 -- bit pattern is correct for the host's word size and endianness.
473 mkLitI :: Int -> [Word]
474 mkLitF :: Float -> [Word]
475 mkLitD :: Double -> [Word]
476 mkLitPtr :: Ptr () -> [Word]
477 mkLitI64 :: Int64 -> [Word]
481 arr <- newArray_ ((0::Int),0)
483 f_arr <- castSTUArray arr
484 w0 <- readArray f_arr 0
491 arr <- newArray_ ((0::Int),1)
493 d_arr <- castSTUArray arr
494 w0 <- readArray d_arr 0
495 w1 <- readArray d_arr 1
496 return [w0 :: Word, w1]
500 arr <- newArray_ ((0::Int),0)
502 d_arr <- castSTUArray arr
503 w0 <- readArray d_arr 0
507 = panic "mkLitD: Bad wORD_SIZE"
512 arr <- newArray_ ((0::Int),1)
514 d_arr <- castSTUArray arr
515 w0 <- readArray d_arr 0
516 w1 <- readArray d_arr 1
517 return [w0 :: Word,w1]
521 arr <- newArray_ ((0::Int),0)
523 d_arr <- castSTUArray arr
524 w0 <- readArray d_arr 0
528 = panic "mkLitI64: Bad wORD_SIZE"
532 arr <- newArray_ ((0::Int),0)
534 i_arr <- castSTUArray arr
535 w0 <- readArray i_arr 0
541 arr <- newArray_ ((0::Int),0)
543 a_arr <- castSTUArray arr
544 w0 <- readArray a_arr 0
548 iNTERP_STACK_CHECK_THRESH :: Int
549 iNTERP_STACK_CHECK_THRESH = INTERP_STACK_CHECK_THRESH