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'
339 formalParam :: ArgRep -> Int -> Doc
340 formalParam V _ = empty
342 formalParamType arg <> space <>
343 text "arg" <> int n <> text ", "
344 formalParamType arg | isPtr arg = text "\"ptr\"" <> space <> argRep arg
345 | otherwise = argRep arg
352 genApply regstatus args =
354 fun_ret_label = mkApplyRetName args
355 fun_info_label = mkApplyInfoName args
356 all_args_size = sum (map argSize args)
359 text "INFO_TABLE_RET(" <> mkApplyName args <> text ", " <>
360 text "RET_SMALL, " <> (cat $ zipWith formalParam args [1..]) <>
367 -- print "static void *lbls[] ="
368 -- print " { [FUN] &&fun_lbl,"
369 -- print " [FUN_1_0] &&fun_lbl,"
370 -- print " [FUN_0_1] &&fun_lbl,"
371 -- print " [FUN_2_0] &&fun_lbl,"
372 -- print " [FUN_1_1] &&fun_lbl,"
373 -- print " [FUN_0_2] &&fun_lbl,"
374 -- print " [FUN_STATIC] &&fun_lbl,"
375 -- print " [PAP] &&pap_lbl,"
376 -- print " [THUNK] &&thunk_lbl,"
377 -- print " [THUNK_1_0] &&thunk_lbl,"
378 -- print " [THUNK_0_1] &&thunk_lbl,"
379 -- print " [THUNK_2_0] &&thunk_lbl,"
380 -- print " [THUNK_1_1] &&thunk_lbl,"
381 -- print " [THUNK_0_2] &&thunk_lbl,"
382 -- print " [THUNK_STATIC] &&thunk_lbl,"
383 -- print " [THUNK_SELECTOR] &&thunk_lbl,"
384 -- print " [IND] &&ind_lbl,"
385 -- print " [IND_OLDGEN] &&ind_lbl,"
386 -- print " [IND_STATIC] &&ind_lbl,"
387 -- print " [IND_PERM] &&ind_lbl,"
388 -- print " [IND_OLDGEN_PERM] &&ind_lbl"
392 text "IF_DEBUG(apply,foreign \"C\" debugBelch(\"" <> fun_ret_label <>
393 text "... \"); foreign \"C\" printClosure(R1 \"ptr\"));",
395 text "IF_DEBUG(sanity,foreign \"C\" checkStackFrame(Sp+WDS(" <> int (1 + all_args_size)
396 <> text ")\"ptr\"));",
398 -- text "IF_DEBUG(sanity,checkStackChunk(Sp+" <> int (1 + all_args_size) <>
399 -- text ", CurrentTSO->stack + CurrentTSO->stack_size));",
401 -- text "TICK_SLOW_CALL(" <> int (length args) <> text ");",
403 let do_assert [] _ = []
404 do_assert (arg:args) offset
405 | isPtr arg = this : rest
407 where this = text "ASSERT(LOOKS_LIKE_CLOSURE_PTR(Sp("
408 <> int offset <> text ")));"
409 rest = do_assert args (offset + argSize arg)
411 vcat (do_assert args 1),
414 text "info = %INFO_PTR(R1);",
417 -- print " goto *lbls[info->type];";
419 text "switch [INVALID_OBJECT .. N_CLOSURE_TYPES] (TO_W_(%INFO_TYPE(%STD_INFO(info)))) {",
427 text "arity = TO_W_(StgBCO_arity(R1));",
428 text "ASSERT(arity > 0);",
429 genMkPAP regstatus "BUILD_PAP" "ENTRY_LBL(stg_BCO)" "FUN" "BCO"
430 True{-stack apply-} False{-args on stack-} False{-not a PAP-}
431 args all_args_size fun_info_label
444 text " FUN_STATIC: {",
446 text "arity = TO_W_(StgFunInfoExtra_arity(%FUN_INFO(info)));",
447 text "ASSERT(arity > 0);",
448 genMkPAP regstatus "BUILD_PAP" "%GET_ENTRY(R1)" "FUN" "FUN"
449 False{-reg apply-} False{-args on stack-} False{-not a PAP-}
450 args all_args_size fun_info_label
460 text "arity = TO_W_(StgPAP_arity(R1));",
461 text "ASSERT(arity > 0);",
462 genMkPAP regstatus "NEW_PAP" "stg_PAP_apply" "PAP" "PAP"
463 True{-stack apply-} False{-args on stack-} True{-is a PAP-}
464 args all_args_size fun_info_label
471 -- print " thunk_lbl:"
475 text " CAF_BLACKHOLE,",
477 text " SE_BLACKHOLE,",
478 text " SE_CAF_BLACKHOLE,",
485 text " THUNK_STATIC,",
486 text " THUNK_SELECTOR: {",
488 -- text "TICK_SLOW_CALL_UNEVALD(" <> int (length args) <> text ");",
489 text "Sp(0) = " <> fun_info_label <> text ";",
490 -- CAREFUL! in SMP mode, the info table may already have been
491 -- overwritten by an indirection, so we must enter the original
492 -- info pointer we read, don't read it again, because it might
493 -- not be enterable any more.
494 text "jump %ENTRY_CODE(info);",
506 text " IND_OLDGEN_PERM: {",
508 text "R1 = StgInd_indirectee(R1);",
518 text "foreign \"C\" barf(\"" <> fun_ret_label <> text "\");"
528 -- -----------------------------------------------------------------------------
529 -- Making a fast unknown application, args are in regs
531 genApplyFast regstatus args =
533 fun_fast_label = mkApplyFastName args
534 fun_ret_label = text "RET_LBL" <> parens (mkApplyName args)
535 fun_info_label = mkApplyInfoName args
536 all_args_size = sum (map argSize args)
544 text "info = %GET_STD_INFO(R1);",
545 text "switch [INVALID_OBJECT .. N_CLOSURE_TYPES] (TO_W_(%INFO_TYPE(info))) {",
553 text " FUN_STATIC: {",
555 text "arity = TO_W_(StgFunInfoExtra_arity(%GET_FUN_INFO(R1)));",
556 text "ASSERT(arity > 0);",
557 genMkPAP regstatus "BUILD_PAP" "%GET_ENTRY(R1)" "FUN" "FUN"
558 False{-reg apply-} True{-args in regs-} False{-not a PAP-}
559 args all_args_size fun_info_label
565 (reg_locs, leftovers, sp_offset) = assignRegs regstatus 1 args
566 -- leave a one-word space on the top of the stack when
567 -- calling the slow version
570 text "Sp_adj" <> parens (int (-sp_offset)) <> semi,
571 saveRegOffs reg_locs,
572 text "jump" <+> fun_ret_label <> semi
581 -- -----------------------------------------------------------------------------
582 -- Making a stack apply
584 -- These little functions are like slow entry points. They provide
585 -- the layer between the PAP entry code and the function's fast entry
586 -- point: namely they load arguments off the stack into registers (if
587 -- available) and jump to the function's entry code.
589 -- On entry: R1 points to the function closure
590 -- arguments are on the stack starting at Sp
592 -- Invariant: the list of arguments never contains void. Since we're only
593 -- interested in loading arguments off the stack here, we can ignore
596 mkStackApplyEntryLabel:: [ArgRep] -> Doc
597 mkStackApplyEntryLabel args = text "stg_ap_stk_" <> text (map showArg args)
599 genStackApply :: RegStatus -> [ArgRep] -> Doc
600 genStackApply regstatus args =
601 let fn_entry_label = mkStackApplyEntryLabel args in
604 text "{", nest 4 body, text "}"
607 (assign_regs, sp') = loadRegArgs regstatus 0 args
608 body = vcat [assign_regs,
609 text "Sp_adj" <> parens (int sp') <> semi,
610 text "jump %GET_ENTRY(R1);"
613 -- -----------------------------------------------------------------------------
614 -- Stack save entry points.
616 -- These code fragments are used to save registers on the stack at a heap
617 -- check failure in the entry code for a function. We also have to save R1
618 -- and the return address (stg_gc_fun_info) on the stack. See stg_gc_fun_gen
619 -- in HeapStackCheck.hc for more details.
621 mkStackSaveEntryLabel :: [ArgRep] -> Doc
622 mkStackSaveEntryLabel args = text "stg_stk_save_" <> text (map showArg args)
624 genStackSave :: RegStatus -> [ArgRep] -> Doc
625 genStackSave regstatus args =
626 let fn_entry_label= mkStackSaveEntryLabel args in
629 text "{", nest 4 body, text "}"
632 body = vcat [text "Sp_adj" <> parens (int (-sp_offset)) <> semi,
633 saveRegOffs reg_locs,
635 text "Sp(1) =" <+> int stk_args <> semi,
636 text "Sp(0) = stg_gc_fun_info;",
637 text "jump stg_gc_noregs;"
640 std_frame_size = 3 -- the std bits of the frame. See StgRetFun in Closures.h,
641 -- and the comment on stg_fun_gc_gen in HeapStackCheck.hc.
642 (reg_locs, leftovers, sp_offset) = assignRegs regstatus std_frame_size args
644 -- number of words of arguments on the stack.
645 stk_args = sum (map argSize leftovers) + sp_offset - std_frame_size
647 -- -----------------------------------------------------------------------------
652 regstatus <- case args of
653 [] -> return Registerised
654 ["-u"] -> return Unregisterised
655 _other -> do hPutStrLn stderr "syntax: genapply [-u]"
656 exitWith (ExitFailure 1)
657 let the_code = vcat [
658 text "// DO NOT EDIT!",
659 text "// Automatically generated by GenApply.hs",
661 text "#include \"Cmm.h\"",
662 text "#include \"AutoApply.h\"",
665 vcat (intersperse (text "") $
666 map (genApply regstatus) applyTypes),
667 vcat (intersperse (text "") $
668 map (genStackFns regstatus) stackApplyTypes),
670 vcat (intersperse (text "") $
671 map (genApplyFast regstatus) applyTypes),
673 genStackApplyArray stackApplyTypes,
674 genStackSaveArray stackApplyTypes,
675 genBitmapArray stackApplyTypes,
677 text "" -- add a newline at the end of the file
680 putStr (render the_code)
682 -- These have been shown to cover about 99% of cases in practice...
700 -- No need for V args in the stack apply cases.
701 -- ToDo: the stack apply and stack save code doesn't make a distinction
702 -- between N and P (they both live in the same register), only the bitmap
703 -- changes, so we could share the apply/save code between lots of cases.
730 genStackFns regstatus args
731 = genStackApply regstatus args
732 $$ genStackSave regstatus args
735 genStackApplyArray types =
737 text "section \"relrodata\" {",
738 text "stg_ap_stack_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_" <+> mkStackApplyEntryLabel ty <> semi
746 genStackSaveArray types =
748 text "section \"relrodata\" {",
749 text "stg_stack_save_entries:",
750 text "W_ 0; W_ 0; W_ 0;", -- ARG_GEN, ARG_GEN_BIG, ARG_BCO
751 vcat (map arr_ent types),
755 arr_ent ty = text "W_" <+> mkStackSaveEntryLabel ty <> semi
757 genBitmapArray :: [[ArgRep]] -> Doc
758 genBitmapArray types =
760 text "section \"rodata\" {",
761 text "stg_arg_bitmaps:",
762 text "W_ 0; W_ 0; W_ 0;", -- ARG_GEN, ARG_GEN_BIG, ARG_BCO
763 vcat (map gen_bitmap types),
767 gen_bitmap ty = text "W_" <+> int bitmap_val <> semi
769 (fromIntegral (mkBitmap ty) `shiftL` BITMAP_BITS_SHIFT)
770 .|. sum (map argSize ty)