2 module Main(main) where
4 #include "../../includes/ghcconfig.h"
5 #include "../../includes/MachRegs.h"
6 #include "../../includes/Constants.h"
9 import Text.PrettyPrint
12 import Data.List ( intersperse )
14 import System.Environment
17 -- -----------------------------------------------------------------------------
18 -- Argument kinds (rougly equivalent to PrimRep)
28 -- size of a value in *words*
29 argSize :: ArgRep -> Int
34 argSize D = (SIZEOF_DOUBLE `quot` SIZEOF_VOID_P :: Int)
35 argSize L = (8 `quot` SIZEOF_VOID_P :: Int)
37 showArg :: ArgRep -> Char
45 -- is a value a pointer?
46 isPtr :: ArgRep -> Bool
50 -- -----------------------------------------------------------------------------
53 data RegStatus = Registerised | Unregisterised
57 availableRegs :: RegStatus -> ([Reg],[Reg],[Reg],[Reg])
58 availableRegs Unregisterised = ([],[],[],[])
59 availableRegs Registerised =
60 ( vanillaRegs MAX_REAL_VANILLA_REG,
61 floatRegs MAX_REAL_FLOAT_REG,
62 doubleRegs MAX_REAL_DOUBLE_REG,
63 longRegs MAX_REAL_LONG_REG
66 vanillaRegs, floatRegs, doubleRegs, longRegs :: Int -> [Reg]
67 vanillaRegs n = [ "R" ++ show m | m <- [2..n] ] -- never use R1
68 floatRegs n = [ "F" ++ show m | m <- [1..n] ]
69 doubleRegs n = [ "D" ++ show m | m <- [1..n] ]
70 longRegs n = [ "L" ++ show m | m <- [1..n] ]
72 -- -----------------------------------------------------------------------------
73 -- Loading/saving register arguments to the stack
75 loadRegArgs :: RegStatus -> Int -> [ArgRep] -> (Doc,Int)
76 loadRegArgs regstatus sp args
77 = (loadRegOffs reg_locs, sp')
78 where (reg_locs, _, sp') = assignRegs regstatus sp args
80 loadRegOffs :: [(Reg,Int)] -> Doc
81 loadRegOffs = vcat . map (uncurry assign_stk_to_reg)
83 saveRegOffs :: [(Reg,Int)] -> Doc
84 saveRegOffs = vcat . map (uncurry assign_reg_to_stk)
86 -- a bit like assignRegs in CgRetConv.lhs
88 :: RegStatus -- are we registerised?
89 -> Int -- Sp of first arg
91 -> ([(Reg,Int)], -- regs and offsets to load
92 [ArgRep], -- left-over args
93 Int) -- Sp of left-over args
94 assignRegs regstatus sp args = assign sp args (availableRegs regstatus) []
96 assign sp [] regs doc = (doc, [], sp)
97 assign sp (V : args) regs doc = assign sp args regs doc
98 assign sp (arg : args) regs doc
99 = case findAvailableReg arg regs of
100 Just (reg, regs') -> assign (sp + argSize arg) args regs'
102 Nothing -> (doc, (arg:args), sp)
104 findAvailableReg N (vreg:vregs, fregs, dregs, lregs) =
105 Just (vreg, (vregs,fregs,dregs,lregs))
106 findAvailableReg P (vreg:vregs, fregs, dregs, lregs) =
107 Just (vreg, (vregs,fregs,dregs,lregs))
108 findAvailableReg F (vregs, freg:fregs, dregs, lregs) =
109 Just (freg, (vregs,fregs,dregs,lregs))
110 findAvailableReg D (vregs, fregs, dreg:dregs, lregs) =
111 Just (dreg, (vregs,fregs,dregs,lregs))
112 findAvailableReg L (vregs, fregs, dregs, lreg:lregs) =
113 Just (lreg, (vregs,fregs,dregs,lregs))
114 findAvailableReg _ _ = Nothing
116 assign_reg_to_stk reg sp
117 = loadSpWordOff (regRep reg) sp <> text " = " <> text reg <> semi
119 assign_stk_to_reg reg sp
120 = text reg <> text " = " <> loadSpWordOff (regRep reg) sp <> semi
122 regRep ('F':_) = "F_"
123 regRep ('D':_) = "D_"
124 regRep ('L':_) = "L_"
127 loadSpWordOff :: String -> Int -> Doc
128 loadSpWordOff rep off = text rep <> text "[Sp+WDS(" <> int off <> text ")]"
130 -- make a ptr/non-ptr bitmap from a list of argument types
131 mkBitmap :: [ArgRep] -> Word32
132 mkBitmap args = foldr f 0 args
133 where f arg bm | isPtr arg = bm `shiftL` 1
134 | otherwise = (bm `shiftL` size) .|. ((1 `shiftL` size) - 1)
135 where size = argSize arg
137 -- -----------------------------------------------------------------------------
138 -- Generating the application functions
140 -- A SUBTLE POINT about stg_ap functions (can't think of a better
141 -- place to put this comment --SDM):
143 -- The entry convention to an stg_ap_ function is as follows: all the
144 -- arguments are on the stack (we might revisit this at some point,
145 -- but it doesn't make any difference on x86), and THERE IS AN EXTRA
146 -- EMPTY STACK SLOT at the top of the stack.
148 -- Why? Because in several cases, stg_ap_* will need an extra stack
149 -- slot, eg. to push a return address in the THUNK case, and this is a
150 -- way of pushing the stack check up into the caller which is probably
151 -- doing one anyway. Allocating the extra stack slot in the caller is
152 -- also probably free, because it will be adjusting Sp after pushing
153 -- the args anyway (this might not be true of register-rich machines
154 -- when we start passing args to stg_ap_* in regs).
157 = text "stg_ap_" <> text (map showArg args)
160 = mkApplyName args <> text "_ret"
163 = mkApplyName args <> text "_fast"
166 = mkApplyName args <> text "_info"
168 genMkPAP regstatus macro jump ticker disamb
169 no_load_regs -- don't load argumnet regs before jumping
170 args_in_regs -- arguments are already in regs
171 is_pap args all_args_size fun_info_label
172 = smaller_arity_cases
179 -- offset of arguments on the stack at slow apply calls.
180 stk_args_slow_offset = 1
184 | otherwise = stk_args_slow_offset
186 -- The SMALLER ARITY cases:
189 -- Sp[1] = (W_)&stg_ap_1_info;
190 -- JMP_(GET_ENTRY(R1.cl));
191 smaller_arity_cases = vcat [ smaller_arity i | i <- [1..n_args-1] ]
194 = text "if (arity == " <> int arity <> text ") {" $$
196 -- text "TICK_SLOW_CALL_" <> text ticker <> text "_TOO_MANY();",
198 -- load up regs for the call, if necessary
201 -- If we have more args in registers than are required
202 -- for the call, then we must save some on the stack,
203 -- and set up the stack for the follow-up call.
204 -- If the extra arguments are on the stack, then we must
205 -- instead shuffle them down to make room for the info
206 -- table for the follow-on call.
209 else shuffle_extra_args,
211 -- for a PAP, we have to arrange that the stack contains a
212 -- return address in the even that stg_PAP_entry fails its
213 -- heap check. See stg_PAP_entry in Apply.hc for details.
215 then text "R2 = " <> mkApplyInfoName this_call_args <> semi
218 text "jump " <> text jump <> semi
223 -- offsets in case we need to save regs:
225 = assignRegs regstatus stk_args_offset args
227 -- register assignment for *this function call*
228 (reg_locs', reg_call_leftovers, reg_call_sp_stk_args)
229 = assignRegs regstatus stk_args_offset (take arity args)
232 | no_load_regs || args_in_regs = empty
233 | otherwise = loadRegOffs reg_locs'
235 (this_call_args, rest_args) = splitAt arity args
237 -- the offset of the stack args from initial Sp
239 | args_in_regs = stk_args_offset
240 | no_load_regs = stk_args_offset
241 | otherwise = reg_call_sp_stk_args
243 -- the stack args themselves
245 | args_in_regs = reg_call_leftovers -- sp offsets are wrong
246 | no_load_regs = this_call_args
247 | otherwise = reg_call_leftovers
249 stack_args_size = sum (map argSize this_call_stack_args)
251 overflow_regs = args_in_regs && length reg_locs > length reg_locs'
254 = -- we have extra arguments in registers to save
256 extra_reg_locs = drop (length reg_locs') (reverse reg_locs)
257 adj_reg_locs = [ (reg, off - adj + 1) |
258 (reg,off) <- extra_reg_locs ]
259 adj = case extra_reg_locs of
260 (reg, fst_off):_ -> fst_off
261 size = snd (last adj_reg_locs)
263 text "Sp_adj(" <> int (-size - 1) <> text ");" $$
264 saveRegOffs adj_reg_locs $$
265 loadSpWordOff "W_" 0 <> text " = " <>
266 mkApplyInfoName rest_args <> semi
269 = vcat (map shuffle_down
270 [sp_stk_args .. sp_stk_args+stack_args_size-1]) $$
271 loadSpWordOff "W_" (sp_stk_args+stack_args_size-1)
273 <> mkApplyInfoName rest_args <> semi $$
274 text "Sp_adj(" <> int (sp_stk_args - 1) <> text ");"
277 loadSpWordOff "W_" (i-1) <> text " = " <>
278 loadSpWordOff "W_" i <> semi
280 -- The EXACT ARITY case
284 -- JMP_(GET_ENTRY(R1.cl));
287 = text "if (arity == " <> int n_args <> text ") {" $$
290 | no_load_regs || args_in_regs = (empty, stk_args_offset)
291 | otherwise = loadRegArgs regstatus stk_args_offset args
294 -- text "TICK_SLOW_CALL_" <> text ticker <> text "_CORRECT();",
296 text "Sp_adj(" <> int sp' <> text ");",
298 then text "R2 = " <> fun_info_label <> semi
300 text "jump " <> text jump <> semi
303 -- The LARGER ARITY cases:
305 -- } else /* arity > 1 */ {
306 -- BUILD_PAP(1,0,(W_)&stg_ap_v_info);
314 text "Sp_adj(" <> int (-sp_offset) <> text ");" $$
320 -- text "TICK_SLOW_CALL_" <> text ticker <> text "_TOO_FEW();",
322 text macro <> char '(' <> int n_args <> comma <>
324 text "," <> fun_info_label <>
325 text "," <> text disamb <>
330 -- offsets in case we need to save regs:
331 (reg_locs, leftovers, sp_offset)
332 = assignRegs regstatus stk_args_slow_offset args
333 -- BUILD_PAP assumes args start at offset 1
335 -- -----------------------------------------------------------------------------
336 -- generate an apply function
338 -- args is a list of 'p', 'n', 'f', 'd' or 'l'
340 genApply regstatus args =
342 fun_ret_label = mkApplyRetName args
343 fun_info_label = mkApplyInfoName args
344 all_args_size = sum (map argSize args)
347 text "INFO_TABLE_RET(" <> mkApplyName args <> text ", " <>
348 int all_args_size <> text "/*framsize*/," <>
349 int (fromIntegral (mkBitmap args)) <> text "/*bitmap*/, " <>
350 text "RET_SMALL)\n{",
356 -- print "static void *lbls[] ="
357 -- print " { [FUN] &&fun_lbl,"
358 -- print " [FUN_1_0] &&fun_lbl,"
359 -- print " [FUN_0_1] &&fun_lbl,"
360 -- print " [FUN_2_0] &&fun_lbl,"
361 -- print " [FUN_1_1] &&fun_lbl,"
362 -- print " [FUN_0_2] &&fun_lbl,"
363 -- print " [FUN_STATIC] &&fun_lbl,"
364 -- print " [PAP] &&pap_lbl,"
365 -- print " [THUNK] &&thunk_lbl,"
366 -- print " [THUNK_1_0] &&thunk_lbl,"
367 -- print " [THUNK_0_1] &&thunk_lbl,"
368 -- print " [THUNK_2_0] &&thunk_lbl,"
369 -- print " [THUNK_1_1] &&thunk_lbl,"
370 -- print " [THUNK_0_2] &&thunk_lbl,"
371 -- print " [THUNK_STATIC] &&thunk_lbl,"
372 -- print " [THUNK_SELECTOR] &&thunk_lbl,"
373 -- print " [IND] &&ind_lbl,"
374 -- print " [IND_OLDGEN] &&ind_lbl,"
375 -- print " [IND_STATIC] &&ind_lbl,"
376 -- print " [IND_PERM] &&ind_lbl,"
377 -- print " [IND_OLDGEN_PERM] &&ind_lbl"
381 text "IF_DEBUG(apply,foreign \"C\" debugBelch(\"" <> fun_ret_label <>
382 text "... \"); foreign \"C\" printClosure(R1 \"ptr\"));",
384 text "IF_DEBUG(sanity,foreign \"C\" checkStackFrame(Sp+WDS(" <> int (1 + all_args_size)
385 <> text ")\"ptr\"));",
387 -- text "IF_DEBUG(sanity,checkStackChunk(Sp+" <> int (1 + all_args_size) <>
388 -- text ", CurrentTSO->stack + CurrentTSO->stack_size));",
390 -- text "TICK_SLOW_CALL(" <> int (length args) <> text ");",
392 let do_assert [] _ = []
393 do_assert (arg:args) offset
394 | isPtr arg = this : rest
396 where this = text "ASSERT(LOOKS_LIKE_CLOSURE_PTR(Sp("
397 <> int offset <> text ")));"
398 rest = do_assert args (offset + argSize arg)
400 vcat (do_assert args 1),
403 text "info = %INFO_PTR(R1);",
406 -- print " goto *lbls[info->type];";
408 text "switch [INVALID_OBJECT .. N_CLOSURE_TYPES] (TO_W_(%INFO_TYPE(%STD_INFO(info)))) {",
416 text "arity = TO_W_(StgBCO_arity(R1));",
417 text "ASSERT(arity > 0);",
418 genMkPAP regstatus "BUILD_PAP" "ENTRY_LBL(stg_BCO)" "FUN" "BCO"
419 True{-stack apply-} False{-args on stack-} False{-not a PAP-}
420 args all_args_size fun_info_label
433 text " FUN_STATIC: {",
435 text "arity = TO_W_(StgFunInfoExtra_arity(%FUN_INFO(info)));",
436 text "ASSERT(arity > 0);",
437 genMkPAP regstatus "BUILD_PAP" "%GET_ENTRY(R1)" "FUN" "FUN"
438 False{-reg apply-} False{-args on stack-} False{-not a PAP-}
439 args all_args_size fun_info_label
449 text "arity = TO_W_(StgPAP_arity(R1));",
450 text "ASSERT(arity > 0);",
451 genMkPAP regstatus "NEW_PAP" "stg_PAP_apply" "PAP" "PAP"
452 True{-stack apply-} False{-args on stack-} True{-is a PAP-}
453 args all_args_size fun_info_label
460 -- print " thunk_lbl:"
464 text " CAF_BLACKHOLE,",
466 text " SE_BLACKHOLE,",
467 text " SE_CAF_BLACKHOLE,",
474 text " THUNK_STATIC,",
475 text " THUNK_SELECTOR: {",
477 -- text "TICK_SLOW_CALL_UNEVALD(" <> int (length args) <> text ");",
478 text "Sp(0) = " <> fun_info_label <> text ";",
479 -- CAREFUL! in SMP mode, the info table may already have been
480 -- overwritten by an indirection, so we must enter the original
481 -- info pointer we read, don't read it again, because it might
482 -- not be enterable any more.
483 text "jump %ENTRY_CODE(info);",
495 text " IND_OLDGEN_PERM: {",
497 text "R1 = StgInd_indirectee(R1);",
507 text "foreign \"C\" barf(\"" <> fun_ret_label <> text "\");"
517 -- -----------------------------------------------------------------------------
518 -- Making a fast unknown application, args are in regs
520 genApplyFast regstatus args =
522 fun_fast_label = mkApplyFastName args
523 fun_ret_label = text "RET_LBL" <> parens (mkApplyName args)
524 fun_info_label = mkApplyInfoName args
525 all_args_size = sum (map argSize args)
533 text "info = %GET_STD_INFO(R1);",
534 text "switch [INVALID_OBJECT .. N_CLOSURE_TYPES] (TO_W_(%INFO_TYPE(info))) {",
542 text " FUN_STATIC: {",
544 text "arity = TO_W_(StgFunInfoExtra_arity(%GET_FUN_INFO(R1)));",
545 text "ASSERT(arity > 0);",
546 genMkPAP regstatus "BUILD_PAP" "%GET_ENTRY(R1)" "FUN" "FUN"
547 False{-reg apply-} True{-args in regs-} False{-not a PAP-}
548 args all_args_size fun_info_label
554 (reg_locs, leftovers, sp_offset) = assignRegs regstatus 1 args
555 -- leave a one-word space on the top of the stack when
556 -- calling the slow version
559 text "Sp_adj" <> parens (int (-sp_offset)) <> semi,
560 saveRegOffs reg_locs,
561 text "jump" <+> fun_ret_label <> semi
570 -- -----------------------------------------------------------------------------
571 -- Making a stack apply
573 -- These little functions are like slow entry points. They provide
574 -- the layer between the PAP entry code and the function's fast entry
575 -- point: namely they load arguments off the stack into registers (if
576 -- available) and jump to the function's entry code.
578 -- On entry: R1 points to the function closure
579 -- arguments are on the stack starting at Sp
581 -- Invariant: the list of arguments never contains void. Since we're only
582 -- interested in loading arguments off the stack here, we can ignore
585 mkStackApplyEntryLabel:: [ArgRep] -> Doc
586 mkStackApplyEntryLabel args = text "stg_ap_stk_" <> text (map showArg args)
588 genStackApply :: RegStatus -> [ArgRep] -> Doc
589 genStackApply regstatus args =
590 let fn_entry_label = mkStackApplyEntryLabel args in
593 text "{", nest 4 body, text "}"
596 (assign_regs, sp') = loadRegArgs regstatus 0 args
597 body = vcat [assign_regs,
598 text "Sp_adj" <> parens (int sp') <> semi,
599 text "jump %GET_ENTRY(R1);"
602 -- -----------------------------------------------------------------------------
603 -- Stack save entry points.
605 -- These code fragments are used to save registers on the stack at a heap
606 -- check failure in the entry code for a function. We also have to save R1
607 -- and the return address (stg_gc_fun_info) on the stack. See stg_gc_fun_gen
608 -- in HeapStackCheck.hc for more details.
610 mkStackSaveEntryLabel :: [ArgRep] -> Doc
611 mkStackSaveEntryLabel args = text "stg_stk_save_" <> text (map showArg args)
613 genStackSave :: RegStatus -> [ArgRep] -> Doc
614 genStackSave regstatus args =
615 let fn_entry_label= mkStackSaveEntryLabel args in
618 text "{", nest 4 body, text "}"
621 body = vcat [text "Sp_adj" <> parens (int (-sp_offset)) <> semi,
622 saveRegOffs reg_locs,
624 text "Sp(1) =" <+> int stk_args <> semi,
625 text "Sp(0) = stg_gc_fun_info;",
626 text "jump stg_gc_noregs;"
629 std_frame_size = 3 -- the std bits of the frame. See StgRetFun in Closures.h,
630 -- and the comment on stg_fun_gc_gen in HeapStackCheck.hc.
631 (reg_locs, leftovers, sp_offset) = assignRegs regstatus std_frame_size args
633 -- number of words of arguments on the stack.
634 stk_args = sum (map argSize leftovers) + sp_offset - std_frame_size
636 -- -----------------------------------------------------------------------------
641 regstatus <- case args of
642 [] -> return Registerised
643 ["-u"] -> return Unregisterised
644 _other -> do hPutStrLn stderr "syntax: genapply [-u]"
645 exitWith (ExitFailure 1)
646 let the_code = vcat [
647 text "// DO NOT EDIT!",
648 text "// Automatically generated by GenApply.hs",
650 text "#include \"Cmm.h\"",
651 text "#include \"AutoApply.h\"",
654 vcat (intersperse (text "") $
655 map (genApply regstatus) applyTypes),
656 vcat (intersperse (text "") $
657 map (genStackFns regstatus) stackApplyTypes),
659 vcat (intersperse (text "") $
660 map (genApplyFast regstatus) applyTypes),
662 genStackApplyArray stackApplyTypes,
663 genStackSaveArray stackApplyTypes,
664 genBitmapArray stackApplyTypes,
666 text "" -- add a newline at the end of the file
669 putStr (render the_code)
671 -- These have been shown to cover about 99% of cases in practice...
689 -- No need for V args in the stack apply cases.
690 -- ToDo: the stack apply and stack save code doesn't make a distinction
691 -- between N and P (they both live in the same register), only the bitmap
692 -- changes, so we could share the apply/save code between lots of cases.
719 genStackFns regstatus args
720 = genStackApply regstatus args
721 $$ genStackSave regstatus args
724 genStackApplyArray types =
726 text "section \"relrodata\" {",
727 text "stg_ap_stack_entries:",
728 text "W_ 0; W_ 0; W_ 0;", -- ARG_GEN, ARG_GEN_BIG, ARG_BCO
729 vcat (map arr_ent types),
733 arr_ent ty = text "W_" <+> mkStackApplyEntryLabel ty <> semi
735 genStackSaveArray types =
737 text "section \"relrodata\" {",
738 text "stg_stack_save_entries:",
739 text "W_ 0; W_ 0; W_ 0;", -- ARG_GEN, ARG_GEN_BIG, ARG_BCO
740 vcat (map arr_ent types),
744 arr_ent ty = text "W_" <+> mkStackSaveEntryLabel ty <> semi
746 genBitmapArray :: [[ArgRep]] -> Doc
747 genBitmapArray types =
749 text "section \"rodata\" {",
750 text "stg_arg_bitmaps:",
751 text "W_ 0; W_ 0; W_ 0;", -- ARG_GEN, ARG_GEN_BIG, ARG_BCO
752 vcat (map gen_bitmap types),
756 gen_bitmap ty = text "W_" <+> int bitmap_val <> semi
758 (fromIntegral (mkBitmap ty) `shiftL` BITMAP_BITS_SHIFT)
759 .|. sum (map argSize ty)