1 -----------------------------------------------------------------------------
3 -- Code generator utilities; mostly monadic
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
12 emitDataLits, emitRODataLits, emitIf, emitIfThenElse,
13 emitRtsCall, emitRtsCallWithVols, emitRtsCallWithResult,
16 emitSwitch, emitLitSwitch,
19 cmmAndWord, cmmOrWord, cmmNegate, cmmEqWord, cmmNeWord,
20 cmmOffsetExprW, cmmOffsetExprB,
21 cmmRegOffW, cmmRegOffB,
22 cmmLabelOffW, cmmLabelOffB,
23 cmmOffsetW, cmmOffsetB,
24 cmmOffsetLitW, cmmOffsetLitB,
34 #include "HsVersions.h"
37 import TyCon ( TyCon, tyConName )
39 import Constants ( wORD_SIZE )
40 import SMRep ( CgRep, StgWord, hALF_WORD_SIZE_IN_BITS, ByteOff,
42 import PprCmm ( {- instances -} )
46 import MachOp ( MachRep(..), wordRep, MachOp(..), MachHint(..),
47 mo_wordOr, mo_wordAnd, mo_wordNe, mo_wordEq,
48 mo_wordULt, machRepByteWidth )
49 import ForeignCall ( CCallConv(..) )
50 import Literal ( Literal(..) )
51 import CLabel ( CLabel, mkStringLitLabel )
52 import Digraph ( SCC(..), stronglyConnComp )
53 import ListSetOps ( assocDefault )
54 import Util ( filterOut, sortLe )
56 import FastString ( LitString, FastString, unpackFS )
61 #include "../includes/ghcconfig.h"
62 -- For WORDS_BIGENDIAN
64 -------------------------------------------------------------------------
66 -- Random small functions
68 -------------------------------------------------------------------------
70 addIdReps :: [Id] -> [(CgRep, Id)]
71 addIdReps ids = [(idCgRep id, id) | id <- ids]
73 -------------------------------------------------------------------------
77 -------------------------------------------------------------------------
79 cgLit :: Literal -> FCode CmmLit
80 cgLit (MachStr s) = mkStringCLit (unpackFS s)
81 cgLit other_lit = return (mkSimpleLit other_lit)
83 mkSimpleLit :: Literal -> CmmLit
84 mkSimpleLit (MachChar c) = CmmInt (fromIntegral (ord c)) wordRep
85 mkSimpleLit MachNullAddr = zeroCLit
86 mkSimpleLit (MachInt i) = CmmInt i wordRep
87 mkSimpleLit (MachInt64 i) = CmmInt i I64
88 mkSimpleLit (MachWord i) = CmmInt i wordRep
89 mkSimpleLit (MachWord64 i) = CmmInt i I64
90 mkSimpleLit (MachFloat r) = CmmFloat r F32
91 mkSimpleLit (MachDouble r) = CmmFloat r F64
92 mkSimpleLit (MachLabel fs ms) = CmmLabel (mkForeignLabel fs ms is_dyn)
94 is_dyn = False -- ToDo: fix me
96 mkLtOp :: Literal -> MachOp
97 -- On signed literals we must do a signed comparison
98 mkLtOp (MachInt _) = MO_S_Lt wordRep
99 mkLtOp (MachFloat _) = MO_S_Lt F32
100 mkLtOp (MachDouble _) = MO_S_Lt F64
101 mkLtOp lit = MO_U_Lt (cmmLitRep (mkSimpleLit lit))
104 ---------------------------------------------------
106 -- Cmm data type functions
108 ---------------------------------------------------
110 -----------------------
111 -- The "B" variants take byte offsets
112 cmmRegOffB :: CmmReg -> ByteOff -> CmmExpr
113 cmmRegOffB = cmmRegOff
115 cmmOffsetB :: CmmExpr -> ByteOff -> CmmExpr
116 cmmOffsetB = cmmOffset
118 cmmOffsetExprB :: CmmExpr -> CmmExpr -> CmmExpr
119 cmmOffsetExprB = cmmOffsetExpr
121 cmmLabelOffB :: CLabel -> ByteOff -> CmmLit
122 cmmLabelOffB = cmmLabelOff
124 cmmOffsetLitB :: CmmLit -> ByteOff -> CmmLit
125 cmmOffsetLitB = cmmOffsetLit
127 -----------------------
128 -- The "W" variants take word offsets
129 cmmOffsetExprW :: CmmExpr -> CmmExpr -> CmmExpr
130 -- The second arg is a *word* offset; need to change it to bytes
131 cmmOffsetExprW e (CmmLit (CmmInt n _)) = cmmOffsetW e (fromInteger n)
132 cmmOffsetExprW e wd_off = cmmIndexExpr wordRep e wd_off
134 cmmOffsetW :: CmmExpr -> WordOff -> CmmExpr
135 cmmOffsetW e n = cmmOffsetB e (wORD_SIZE * n)
137 cmmRegOffW :: CmmReg -> WordOff -> CmmExpr
138 cmmRegOffW reg wd_off = cmmRegOffB reg (wd_off * wORD_SIZE)
140 cmmOffsetLitW :: CmmLit -> WordOff -> CmmLit
141 cmmOffsetLitW lit wd_off = cmmOffsetLitB lit (wORD_SIZE * wd_off)
143 cmmLabelOffW :: CLabel -> WordOff -> CmmLit
144 cmmLabelOffW lbl wd_off = cmmLabelOffB lbl (wORD_SIZE * wd_off)
146 cmmLoadIndexW :: CmmExpr -> Int -> CmmExpr
147 cmmLoadIndexW base off
148 = CmmLoad (cmmOffsetW base off) wordRep
150 -----------------------
151 cmmNeWord, cmmEqWord, cmmOrWord, cmmAndWord :: CmmExpr -> CmmExpr -> CmmExpr
152 cmmOrWord e1 e2 = CmmMachOp mo_wordOr [e1, e2]
153 cmmAndWord e1 e2 = CmmMachOp mo_wordAnd [e1, e2]
154 cmmNeWord e1 e2 = CmmMachOp mo_wordNe [e1, e2]
155 cmmEqWord e1 e2 = CmmMachOp mo_wordEq [e1, e2]
156 cmmULtWord e1 e2 = CmmMachOp mo_wordULt [e1, e2]
158 cmmNegate :: CmmExpr -> CmmExpr
159 cmmNegate (CmmLit (CmmInt n rep)) = CmmLit (CmmInt (-n) rep)
160 cmmNegate e = CmmMachOp (MO_S_Neg (cmmExprRep e)) [e]
162 blankWord :: CmmStatic
163 blankWord = CmmUninitialised wORD_SIZE
165 -----------------------
168 mkWordCLit :: StgWord -> CmmLit
169 mkWordCLit wd = CmmInt (fromIntegral wd) wordRep
171 packHalfWordsCLit :: (Integral a, Integral b) => a -> b -> CmmLit
172 -- Make a single word literal in which the lower_half_word is
173 -- at the lower address, and the upper_half_word is at the
175 -- ToDo: consider using half-word lits instead
176 -- but be careful: that's vulnerable when reversed
177 packHalfWordsCLit lower_half_word upper_half_word
178 #ifdef WORDS_BIGENDIAN
179 = mkWordCLit ((fromIntegral lower_half_word `shiftL` hALF_WORD_SIZE_IN_BITS)
180 .|. fromIntegral upper_half_word)
182 = mkWordCLit ((fromIntegral lower_half_word)
183 .|. (fromIntegral upper_half_word `shiftL` hALF_WORD_SIZE_IN_BITS))
186 --------------------------------------------------------------------------
188 -- Incrementing a memory location
190 --------------------------------------------------------------------------
192 addToMem :: MachRep -- rep of the counter
193 -> CmmExpr -- Address
194 -> Int -- What to add (a word)
196 addToMem rep ptr n = addToMemE rep ptr (CmmLit (CmmInt (toInteger n) rep))
198 addToMemE :: MachRep -- rep of the counter
199 -> CmmExpr -- Address
200 -> CmmExpr -- What to add (a word-typed expression)
203 = CmmStore ptr (CmmMachOp (MO_Add rep) [CmmLoad ptr rep, n])
205 -------------------------------------------------------------------------
207 -- Converting a closure tag to a closure for enumeration types
208 -- (this is the implementation of tagToEnum#).
210 -------------------------------------------------------------------------
212 tagToClosure :: TyCon -> CmmExpr -> CmmExpr
213 tagToClosure tycon tag
214 = CmmLoad (cmmOffsetExprW closure_tbl tag) wordRep
215 where closure_tbl = CmmLit (CmmLabel (mkClosureTblLabel (tyConName tycon)))
217 -------------------------------------------------------------------------
219 -- Conditionals and rts calls
221 -------------------------------------------------------------------------
223 emitIf :: CmmExpr -- Boolean
226 -- Emit (if e then x)
227 -- ToDo: reverse the condition to avoid the extra branch instruction if possible
228 -- (some conditionals aren't reversible. eg. floating point comparisons cannot
229 -- be inverted because there exist some values for which both comparisons
230 -- return False, such as NaN.)
231 emitIf cond then_part
232 = do { then_id <- newLabelC
233 ; join_id <- newLabelC
234 ; stmtC (CmmCondBranch cond then_id)
235 ; stmtC (CmmBranch join_id)
241 emitIfThenElse :: CmmExpr -- Boolean
245 -- Emit (if e then x else y)
246 emitIfThenElse cond then_part else_part
247 = do { then_id <- newLabelC
248 ; else_id <- newLabelC
249 ; join_id <- newLabelC
250 ; stmtC (CmmCondBranch cond then_id)
252 ; stmtC (CmmBranch join_id)
258 emitRtsCall :: LitString -> [(CmmExpr,MachHint)] -> Code
259 emitRtsCall fun args = emitRtsCall' [] fun args Nothing
260 -- The 'Nothing' says "save all global registers"
262 emitRtsCallWithVols :: LitString -> [(CmmExpr,MachHint)] -> [GlobalReg] -> Code
263 emitRtsCallWithVols fun args vols
264 = emitRtsCall' [] fun args (Just vols)
266 emitRtsCallWithResult :: CmmReg -> MachHint -> LitString
267 -> [(CmmExpr,MachHint)] -> Code
268 emitRtsCallWithResult res hint fun args
269 = emitRtsCall' [(res,hint)] fun args Nothing
271 -- Make a call to an RTS C procedure
273 :: [(CmmReg,MachHint)]
275 -> [(CmmExpr,MachHint)]
278 emitRtsCall' res fun args vols = stmtC (CmmCall target res args vols)
280 target = CmmForeignCall fun_expr CCallConv
281 fun_expr = mkLblExpr (mkRtsCodeLabel fun)
284 -------------------------------------------------------------------------
286 -- Strings gnerate a top-level data block
288 -------------------------------------------------------------------------
290 emitDataLits :: CLabel -> [CmmLit] -> Code
291 -- Emit a data-segment data block
292 emitDataLits lbl lits
293 = emitData Data (CmmDataLabel lbl : map CmmStaticLit lits)
295 emitRODataLits :: CLabel -> [CmmLit] -> Code
296 -- Emit a read-only data block
297 emitRODataLits lbl lits
298 = emitData ReadOnlyData (CmmDataLabel lbl : map CmmStaticLit lits)
300 mkStringCLit :: String -> FCode CmmLit
301 -- Make a global definition for the string,
302 -- and return its label
304 = do { uniq <- newUnique
305 ; let lbl = mkStringLitLabel uniq
306 ; emitData ReadOnlyData [CmmDataLabel lbl, CmmString str]
307 ; return (CmmLabel lbl) }
309 -------------------------------------------------------------------------
311 -- Assigning expressions to temporaries
313 -------------------------------------------------------------------------
315 assignTemp :: CmmExpr -> FCode CmmExpr
316 -- For a non-trivial expression, e, create a local
317 -- variable and assign the expression to it
319 | isTrivialCmmExpr e = return e
320 | otherwise = do { reg <- newTemp (cmmExprRep e)
321 ; stmtC (CmmAssign reg e)
322 ; return (CmmReg reg) }
325 newTemp :: MachRep -> FCode CmmReg
326 newTemp rep = do { uniq <- newUnique; return (CmmLocal (LocalReg uniq rep)) }
329 -------------------------------------------------------------------------
331 -- Building case analysis
333 -------------------------------------------------------------------------
336 :: CmmExpr -- Tag to switch on
337 -> [(ConTagZ, CgStmts)] -- Tagged branches
338 -> Maybe CgStmts -- Default branch (if any)
339 -> ConTagZ -> ConTagZ -- Min and Max possible values; behaviour
340 -- outside this range is undefined
343 -- ONLY A DEFAULT BRANCH: no case analysis to do
344 emitSwitch tag_expr [] (Just stmts) _ _
348 emitSwitch tag_expr branches mb_deflt lo_tag hi_tag
349 = -- Just sort the branches before calling mk_sritch
352 Nothing -> return Nothing
353 Just stmts -> do id <- forkCgStmts stmts; return (Just id)
355 ; stmts <- mk_switch tag_expr (sortLe le branches)
356 mb_deflt_id lo_tag hi_tag
360 (t1,_) `le` (t2,_) = t1 <= t2
363 mk_switch :: CmmExpr -> [(ConTagZ, CgStmts)]
364 -> Maybe BlockId -> ConTagZ -> ConTagZ
367 -- SINGLETON TAG RANGE: no case analysis to do
368 mk_switch tag_expr [(tag,stmts)] _ lo_tag hi_tag
370 = ASSERT( tag == lo_tag )
373 -- SINGLETON BRANCH, NO DEFUALT: no case analysis to do
374 mk_switch tag_expr [(tag,stmts)] Nothing lo_tag hi_tag
376 -- The simplifier might have eliminated a case
377 -- so we may have e.g. case xs of
379 -- In that situation we can be sure the (:) case
380 -- can't happen, so no need to test
382 -- SINGLETON BRANCH: one equality check to do
383 mk_switch tag_expr [(tag,stmts)] (Just deflt) lo_tag hi_tag
384 = return (CmmCondBranch cond deflt `consCgStmt` stmts)
386 cond = cmmNeWord tag_expr (CmmLit (mkIntCLit tag))
387 -- We have lo_tag < hi_tag, but there's only one branch,
388 -- so there must be a default
390 -- ToDo: we might want to check for the two branch case, where one of
391 -- the branches is the tag 0, because comparing '== 0' is likely to be
392 -- more efficient than other kinds of comparison.
394 -- DENSE TAG RANGE: use a switch statment
395 mk_switch tag_expr branches mb_deflt lo_tag hi_tag
396 | use_switch -- Use a switch
397 = do { branch_ids <- mapM forkCgStmts (map snd branches)
399 tagged_blk_ids = zip (map fst branches) (map Just branch_ids)
401 find_branch :: ConTagZ -> Maybe BlockId
402 find_branch i = assocDefault mb_deflt tagged_blk_ids i
404 arms = [ find_branch (i+lo_tag) | i <- [0..n_tags-1]]
406 switch_stmt = CmmSwitch (cmmOffset tag_expr (- lo_tag)) arms
408 ; return (oneCgStmt switch_stmt)
411 | otherwise -- Use an if-tree
412 = do { (assign_tag, tag_expr') <- assignTemp' tag_expr
413 -- To avoid duplication
414 ; lo_stmts <- mk_switch tag_expr' lo_branches mb_deflt lo_tag (mid_tag-1)
415 ; hi_stmts <- mk_switch tag_expr' hi_branches mb_deflt mid_tag hi_tag
416 ; lo_id <- forkCgStmts lo_stmts
417 ; let cond = cmmULtWord tag_expr' (CmmLit (mkIntCLit mid_tag))
418 branch_stmt = CmmCondBranch cond lo_id
419 ; return (assign_tag `consCgStmt` (branch_stmt `consCgStmt` hi_stmts))
422 use_switch = ASSERT( n_branches > 1 && n_tags > 1 )
423 n_tags > 2 && (small || dense)
424 -- a 2-branch switch always turns into an if.
426 dense = n_branches > (n_tags `div` 2)
427 exhaustive = n_tags == n_branches
428 n_tags = hi_tag - lo_tag + 1
429 n_branches = length branches
431 -- INVARIANT: Provided hi_tag > lo_tag (which is true)
432 -- lo_tag <= mid_tag < hi_tag
433 -- lo_branches have tags < mid_tag
434 -- hi_branches have tags >= mid_tag
436 (mid_tag,_) = branches !! (n_branches `div` 2)
437 -- 2 branches => n_branches `div` 2 = 1
438 -- => branches !! 1 give the *second* tag
439 -- There are always at least 2 branches here
441 (lo_branches, hi_branches) = span is_lo branches
442 is_lo (t,_) = t < mid_tag
446 | isTrivialCmmExpr e = return (CmmNop, e)
447 | otherwise = do { reg <- newTemp (cmmExprRep e)
448 ; return (CmmAssign reg e, CmmReg reg) }
451 emitLitSwitch :: CmmExpr -- Tag to switch on
452 -> [(Literal, CgStmts)] -- Tagged branches
453 -> CgStmts -- Default branch (always)
454 -> Code -- Emit the code
455 -- Used for general literals, whose size might not be a word,
456 -- where there is always a default case, and where we don't know
457 -- the range of values for certain. For simplicity we always generate a tree.
458 emitLitSwitch scrut [] deflt
460 emitLitSwitch scrut branches deflt_blk
461 = do { scrut' <- assignTemp scrut
462 ; deflt_blk_id <- forkCgStmts deflt_blk
463 ; blk <- mk_lit_switch scrut' deflt_blk_id (sortLe le branches)
466 le (t1,_) (t2,_) = t1 <= t2
468 mk_lit_switch :: CmmExpr -> BlockId
469 -> [(Literal,CgStmts)]
471 mk_lit_switch scrut deflt_blk_id [(lit,blk)]
472 = return (consCgStmt if_stmt blk)
474 cmm_lit = mkSimpleLit lit
475 rep = cmmLitRep cmm_lit
476 cond = CmmMachOp (MO_Ne rep) [scrut, CmmLit cmm_lit]
477 if_stmt = CmmCondBranch cond deflt_blk_id
479 mk_lit_switch scrut deflt_blk_id branches
480 = do { hi_blk <- mk_lit_switch scrut deflt_blk_id hi_branches
481 ; lo_blk <- mk_lit_switch scrut deflt_blk_id lo_branches
482 ; lo_blk_id <- forkCgStmts lo_blk
483 ; let if_stmt = CmmCondBranch cond lo_blk_id
484 ; return (if_stmt `consCgStmt` hi_blk) }
486 n_branches = length branches
487 (mid_lit,_) = branches !! (n_branches `div` 2)
488 -- See notes above re mid_tag
490 (lo_branches, hi_branches) = span is_lo branches
491 is_lo (t,_) = t < mid_lit
493 cond = CmmMachOp (mkLtOp mid_lit)
494 [scrut, CmmLit (mkSimpleLit mid_lit)]
496 -------------------------------------------------------------------------
498 -- Simultaneous assignment
500 -------------------------------------------------------------------------
503 emitSimultaneously :: CmmStmts -> Code
504 -- Emit code to perform the assignments in the
505 -- input simultaneously, using temporary variables when necessary.
507 -- The Stmts must be:
508 -- CmmNop, CmmComment, CmmAssign, CmmStore
512 -- We use the strongly-connected component algorithm, in which
513 -- * the vertices are the statements
514 -- * an edge goes from s1 to s2 iff
515 -- s1 assigns to something s2 uses
516 -- that is, if s1 should *follow* s2 in the final order
518 type CVertex = (Int, CmmStmt) -- Give each vertex a unique number,
519 -- for fast comparison
521 emitSimultaneously stmts
523 case filterOut isNopStmt (stmtList stmts) of
526 [stmt] -> stmtC stmt -- It's often just one stmt
527 stmt_list -> doSimultaneously1 (zip [(1::Int)..] stmt_list)
529 doSimultaneously1 :: [CVertex] -> Code
530 doSimultaneously1 vertices
532 edges = [ (vertex, key1, edges_from stmt1)
533 | vertex@(key1, stmt1) <- vertices
535 edges_from stmt1 = [ key2 | (key2, stmt2) <- vertices,
536 stmt1 `mustFollow` stmt2
538 components = stronglyConnComp edges
540 -- do_components deal with one strongly-connected component
541 -- Not cyclic, or singleton? Just do it
542 do_component (AcyclicSCC (n,stmt)) = stmtC stmt
543 do_component (CyclicSCC [(n,stmt)]) = stmtC stmt
545 -- Cyclic? Then go via temporaries. Pick one to
546 -- break the loop and try again with the rest.
547 do_component (CyclicSCC ((n,first_stmt) : rest))
548 = do { from_temp <- go_via_temp first_stmt
549 ; doSimultaneously1 rest
552 go_via_temp (CmmAssign dest src)
553 = do { tmp <- newTemp (cmmRegRep dest)
554 ; stmtC (CmmAssign tmp src)
555 ; return (CmmAssign dest (CmmReg tmp)) }
556 go_via_temp (CmmStore dest src)
557 = do { tmp <- newTemp (cmmExprRep src)
558 ; stmtC (CmmAssign tmp src)
559 ; return (CmmStore dest (CmmReg tmp)) }
561 mapCs do_component components
563 mustFollow :: CmmStmt -> CmmStmt -> Bool
564 CmmAssign reg _ `mustFollow` stmt = anySrc (reg `regUsedIn`) stmt
565 CmmStore loc e `mustFollow` stmt = anySrc (locUsedIn loc (cmmExprRep e)) stmt
566 CmmNop `mustFollow` stmt = False
567 CmmComment _ `mustFollow` stmt = False
570 anySrc :: (CmmExpr -> Bool) -> CmmStmt -> Bool
571 -- True if the fn is true of any input of the stmt
572 anySrc p (CmmAssign _ e) = p e
573 anySrc p (CmmStore e1 e2) = p e1 || p e2 -- Might be used in either side
574 anySrc p (CmmComment _) = False
575 anySrc p CmmNop = False
576 anySrc p other = True -- Conservative
578 regUsedIn :: CmmReg -> CmmExpr -> Bool
579 reg `regUsedIn` CmmLit _ = False
580 reg `regUsedIn` CmmLoad e _ = reg `regUsedIn` e
581 reg `regUsedIn` CmmReg reg' = reg == reg'
582 reg `regUsedIn` CmmRegOff reg' _ = reg == reg'
583 reg `regUsedIn` CmmMachOp _ es = any (reg `regUsedIn`) es
585 locUsedIn :: CmmExpr -> MachRep -> CmmExpr -> Bool
586 -- (locUsedIn a r e) checks whether writing to r[a] could affect the value of
587 -- 'e'. Returns True if it's not sure.
588 locUsedIn loc rep (CmmLit _) = False
589 locUsedIn loc rep (CmmLoad e ld_rep) = possiblySameLoc loc rep e ld_rep
590 locUsedIn loc rep (CmmReg reg') = False
591 locUsedIn loc rep (CmmRegOff reg' _) = False
592 locUsedIn loc rep (CmmMachOp _ es) = any (locUsedIn loc rep) es
594 possiblySameLoc :: CmmExpr -> MachRep -> CmmExpr -> MachRep -> Bool
595 -- Assumes that distinct registers (eg Hp, Sp) do not
596 -- point to the same location, nor any offset thereof.
597 possiblySameLoc (CmmReg r1) rep1 (CmmReg r2) rep2 = r1==r2
598 possiblySameLoc (CmmReg r1) rep1 (CmmRegOff r2 0) rep2 = r1==r2
599 possiblySameLoc (CmmRegOff r1 0) rep1 (CmmReg r2) rep2 = r1==r2
600 possiblySameLoc (CmmRegOff r1 start1) rep1 (CmmRegOff r2 start2) rep2
601 = r1==r2 && end1 > start2 && end2 > start1
603 end1 = start1 + machRepByteWidth rep1
604 end2 = start2 + machRepByteWidth rep2
606 possiblySameLoc l1 rep1 (CmmLit _) rep2 = False
607 possiblySameLoc l1 rep1 l2 rep2 = True -- Conservative