X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Fcmm%2FCmmCPS.hs;h=afb55d589ab23a49ef75e46b6105356aea276764;hb=b2e42d0f4bfd4cb56b3de4d452d33a438a146845;hp=f26e55f835224893c81c4fc359e0eb7a1dbf464b;hpb=f96e9aa0444de0e673b3c4055c6e43299639bc5b;p=ghc-hetmet.git diff --git a/compiler/cmm/CmmCPS.hs b/compiler/cmm/CmmCPS.hs index f26e55f..afb55d5 100644 --- a/compiler/cmm/CmmCPS.hs +++ b/compiler/cmm/CmmCPS.hs @@ -69,6 +69,34 @@ cmmCPS dflags abstractC = do return continuationC +stg_gc_gen = mkRtsApFastLabel SLIT("gen_cg_TODO") --panic "Need the label for gc" +make_gc_block block_id fun_label formals safety = BasicBlock block_id stmts + where + stmts = [CmmCall stg_gc_gen_target [] [] safety, + CmmJump fun_expr actuals] + stg_gc_gen_target = + CmmForeignCall (CmmLit (CmmLabel stg_gc_gen)) CmmCallConv + actuals = map (\x -> (CmmReg (CmmLocal x), NoHint)) formals + fun_expr = CmmLit (CmmLabel fun_label) + +force_gc_block old_info block_id fun_label formals = + case old_info of + CmmNonInfo (Just _) -> (old_info, []) + CmmInfo _ (Just _) _ _ -> (old_info, []) + CmmNonInfo Nothing + -> (CmmNonInfo (Just block_id), + [make_gc_block block_id fun_label formals (CmmSafe NoC_SRT)]) + CmmInfo prof Nothing type_tag type_info + -> (CmmInfo prof (Just block_id) type_tag type_info, + [make_gc_block block_id fun_label formals (CmmSafe srt)]) + where + srt = case type_info of + ConstrInfo _ _ _ -> NoC_SRT + FunInfo _ srt' _ _ _ _ -> srt' + ThunkInfo _ srt' -> srt' + ThunkSelectorInfo _ srt' -> srt' + ContInfo _ srt' -> srt' + ----------------------------------------------------------------------------- -- |CPS a single CmmTop (proceedure) -- Only 'CmmProc' are transformed 'CmmData' will be left alone. @@ -82,14 +110,24 @@ cpsProc uniqSupply (CmmProc info ident params blocks) = info_procs where uniques :: [[Unique]] uniques = map uniqsFromSupply $ listSplitUniqSupply uniqSupply - info_uniques:block_uniques = uniques + (gc_unique:info_uniques):block_uniques = uniques + + -- Ensure that + forced_gc :: (CmmInfo, [CmmBasicBlock]) + forced_gc = force_gc_block info (BlockId gc_unique) ident params + + forced_info = fst forced_gc + forced_blocks = blocks ++ snd forced_gc + forced_gc_id = case forced_info of + CmmNonInfo (Just x) -> x + CmmInfo _ (Just x) _ _ -> x -- Break the block at each function call. -- The part after the function call will have to become a continuation. broken_blocks :: [BrokenBlock] broken_blocks = - concat $ zipWith3 breakBlock block_uniques blocks - (FunctionEntry info ident params:repeat ControlEntry) + concat $ zipWith3 breakBlock block_uniques forced_blocks + (FunctionEntry forced_info ident params:repeat ControlEntry) -- Calculate live variables for each broken block. -- @@ -109,25 +147,25 @@ cpsProc uniqSupply (CmmProc info ident params blocks) = info_procs -- Group the blocks into continuations based on the set of proc-points. continuations :: [Continuation (Either C_SRT CmmInfo)] - continuations = map (gatherBlocksIntoContinuation proc_points block_env) - (uniqSetToList proc_points) + continuations = zipWith + (gatherBlocksIntoContinuation proc_points block_env) + (uniqSetToList proc_points) + (Just forced_gc_id : repeat Nothing) -- Select the stack format on entry to each continuation. -- Return the max stack offset and an association list -- -- This is an association list instead of a UniqFM because -- CLabel's don't have a 'Uniqueable' instance. - formats :: [(CLabel, (Maybe CLabel, [Maybe LocalReg]))] + formats :: [(CLabel, -- key + (Maybe CLabel, -- label in top slot + [Maybe LocalReg]))] -- slots formats = selectStackFormat live continuations -- Do a little meta-processing on the stack formats such as -- getting the individual frame sizes and the maximum frame size formats' :: (WordOff, [(CLabel, StackFormat)]) - formats' = processFormats formats - - -- TODO FIXME NOW: calculate a real max stack (including function call args) - -- TODO: from the maximum frame size get the maximum stack size. - -- The difference is due to the size taken by function calls. + formats' = processFormats formats continuations -- Update the info table data on the continuations with -- the selected stack formats. @@ -163,7 +201,7 @@ cpsProc uniqSupply (CmmProc info ident params blocks) = info_procs continuationLabel (Continuation _ l _ _) = l data Continuation info = Continuation - info --(Either C_SRT CmmInfo) -- Left <=> Continuation created by the CPS + info -- Left <=> Continuation created by the CPS -- Right <=> Function or Proc point CLabel -- Used to generate both info & entry labels CmmFormals -- Argument locals live on entry (C-- procedure params) @@ -191,18 +229,22 @@ data StackFormat collectNonProcPointTargets :: UniqSet BlockId -> BlockEnv BrokenBlock - -> UniqSet BlockId -> BlockId -> UniqSet BlockId -collectNonProcPointTargets proc_points blocks current_targets block = + -> UniqSet BlockId -> [BlockId] -> UniqSet BlockId +collectNonProcPointTargets proc_points blocks current_targets new_blocks = if sizeUniqSet current_targets == sizeUniqSet new_targets then current_targets - else foldl (collectNonProcPointTargets proc_points blocks) new_targets targets + else foldl + (collectNonProcPointTargets proc_points blocks) + new_targets + (map (:[]) targets) where - block' = lookupWithDefaultUFM blocks (panic "TODO") block + blocks' = map (lookupWithDefaultUFM blocks (panic "TODO")) new_blocks targets = -- Note the subtlety that since the extra branch after a call -- will always be to a block that is a proc-point, -- this subtraction will always remove that case - uniqSetToList $ (mkUniqSet $ brokenBlockTargets block') `minusUniqSet` proc_points + uniqSetToList $ (unionManyUniqSets $ map (mkUniqSet . brokenBlockTargets) blocks') + `minusUniqSet` proc_points -- TODO: remove redundant uniqSetToList new_targets = current_targets `unionUniqSets` (mkUniqSet targets) @@ -213,14 +255,16 @@ collectNonProcPointTargets proc_points blocks current_targets block = gatherBlocksIntoContinuation :: UniqSet BlockId -> BlockEnv BrokenBlock - -> BlockId -> Continuation (Either C_SRT CmmInfo) -gatherBlocksIntoContinuation proc_points blocks start = + -> BlockId -> Maybe BlockId -> Continuation (Either C_SRT CmmInfo) +gatherBlocksIntoContinuation proc_points blocks start gc = Continuation info_table clabel params body where - children = (collectNonProcPointTargets proc_points blocks (unitUniqSet start) start) `delOneFromUniqSet` start + start_and_gc = start : maybeToList gc + children = (collectNonProcPointTargets proc_points blocks (mkUniqSet start_and_gc) start_and_gc) `minusUniqSet` (mkUniqSet start_and_gc) start_block = lookupWithDefaultUFM blocks (panic "TODO") start + gc_block = map (lookupWithDefaultUFM blocks (panic "TODO)")) (maybeToList gc) children_blocks = map (lookupWithDefaultUFM blocks (panic "TODO")) (uniqSetToList children) - body = start_block : children_blocks + body = start_block : gc_block ++ children_blocks -- We can't properly annotate the continuation's stack parameters -- at this point because this is before stack selection @@ -228,7 +272,7 @@ gatherBlocksIntoContinuation proc_points blocks start = info_table = case start_block_entry of FunctionEntry info _ _ -> Right info ContinuationEntry _ srt -> Left srt - ControlEntry -> Right CmmNonInfo + ControlEntry -> Right (CmmNonInfo Nothing) start_block_entry = brokenBlockEntry start_block clabel = case start_block_entry of @@ -262,10 +306,12 @@ selectStackFormat live continuations = unknown_block = panic "unknown BlockId in selectStackFormat" processFormats :: [(CLabel, (Maybe CLabel, [Maybe LocalReg]))] + -> [Continuation (Either C_SRT CmmInfo)] -> (WordOff, [(CLabel, StackFormat)]) -processFormats formats = (max_size, formats') +processFormats formats continuations = (max_size, formats') where - max_size = foldl max 0 (map (stack_frame_size . snd) formats') + max_size = maximum $ + 0 : map (continuationMaxStack formats') continuations formats' = map make_format formats make_format (label, format) = (label, @@ -287,6 +333,44 @@ processFormats formats = (max_size, formats') width = machRepByteWidth (localRegRep reg) `quot` wORD_SIZE -- TODO: it would be better if we had a machRepWordWidth +continuationMaxStack :: [(CLabel, StackFormat)] + -> Continuation a + -> WordOff +continuationMaxStack formats (Continuation _ label _ blocks) = + max_arg_size + stack_frame_size stack_format + where + stack_format = maybe unknown_format id $ lookup label formats + unknown_format = panic "Unknown format in continuationMaxStack" + + max_arg_size = maximum $ 0 : map block_max_arg_size blocks + + block_max_arg_size block = + maximum (final_arg_size (brokenBlockExit block) : + map stmt_arg_size (brokenBlockStmts block)) + + final_arg_size (FinalReturn args) = + argumentsSize (cmmExprRep . fst) args + final_arg_size (FinalJump _ args) = + argumentsSize (cmmExprRep . fst) args + final_arg_size (FinalCall next _ _ args) = + -- We have to account for the stack used when we build a frame + -- for the *next* continuation from *this* continuation + argumentsSize (cmmExprRep . fst) args + + stack_frame_size next_format + where + next_format = maybe unknown_format id $ lookup next' formats + next' = mkReturnPtLabel $ getUnique next + + final_arg_size _ = 0 + + stmt_arg_size (CmmJump _ args) = + argumentsSize (cmmExprRep . fst) args + stmt_arg_size (CmmCall _ _ _ (CmmSafe _)) = + panic "Safe call in processFormats" + stmt_arg_size (CmmReturn _) = + panic "CmmReturn in processFormats" + stmt_arg_size _ = 0 + ----------------------------------------------------------------------------- applyStackFormat :: [(CLabel, StackFormat)] -> Continuation (Either C_SRT CmmInfo) @@ -315,9 +399,7 @@ applyStackFormat formats (Continuation (Left srt) label formals blocks) = -- TODO prof: this is the same as the current implementation -- but I think it could be improved prof = ProfilingInfo zeroCLit zeroCLit - tag = if stack_frame_size format > mAX_SMALL_BITMAP_SIZE - then rET_BIG - else rET_SMALL + tag = rET_SMALL -- cmmToRawCmm may convert it to rET_BIG format = maybe unknown_block id $ lookup label formats unknown_block = panic "unknown BlockId in applyStackFormat" @@ -331,6 +413,7 @@ continuationToProc (max_stack, formats) where curr_format = maybe unknown_block id $ lookup label formats unknown_block = panic "unknown BlockId in continuationToProc" + curr_stack = stack_frame_size curr_format continuationToProc' :: BrokenBlock -> CmmBasicBlock continuationToProc' (BrokenBlock ident entry stmts _ exit) = @@ -339,33 +422,35 @@ continuationToProc (max_stack, formats) prefix = case entry of ControlEntry -> [] FunctionEntry (CmmInfo _ (Just gc_block) _ _) _ formals -> - gc_stack_check gc_block max_stack ++ + gc_stack_check gc_block (max_stack - curr_stack) ++ function_entry formals curr_format FunctionEntry (CmmInfo _ Nothing _ _) _ formals -> - panic "continuationToProc: TODO generate GC block" ++ - function_entry formals curr_format - FunctionEntry CmmNonInfo _ formals -> - panic "TODO: gc_stack_check gc_block max_stack" ++ + panic "continuationToProc: missing GC block" + FunctionEntry (CmmNonInfo (Just gc_block)) _ formals -> + gc_stack_check gc_block (max_stack - curr_stack) ++ function_entry formals curr_format + FunctionEntry (CmmNonInfo Nothing) _ formals -> + panic "continuationToProc: missing non-info GC block" ContinuationEntry formals _ -> function_entry formals curr_format postfix = case exit of FinalBranch next -> [CmmBranch next] FinalSwitch expr targets -> [CmmSwitch expr targets] FinalReturn arguments -> - tail_call (stack_frame_size curr_format) + tail_call curr_stack (CmmLoad (CmmReg spReg) wordRep) arguments FinalJump target arguments -> - tail_call (stack_frame_size curr_format) target arguments + tail_call curr_stack target arguments FinalCall next (CmmForeignCall target CmmCallConv) results arguments -> pack_continuation curr_format cont_format ++ - tail_call (stack_frame_size curr_format - stack_frame_size cont_format) + tail_call (curr_stack - cont_stack) target arguments where cont_format = maybe unknown_block id $ lookup (mkReturnPtLabel $ getUnique next) formats + cont_stack = stack_frame_size cont_format FinalCall next _ results arguments -> panic "unimplemented CmmCall" ----------------------------------------------------------------------------- @@ -395,10 +480,12 @@ gc_stack_check gc_block max_frame_size check_stack_limit = [ CmmCondBranch (CmmMachOp (MO_U_Lt $ cmmRegRep spReg) - [CmmRegOff spReg max_frame_size, CmmReg spLimReg]) + [CmmRegOff spReg (-max_frame_size*wORD_SIZE), + CmmReg spLimReg]) gc_block] --- TODO: fix branches to proc point (we have to insert a new block to marshel the continuation) +-- TODO: fix branches to proc point +-- (we have to insert a new block to marshel the continuation) pack_continuation :: StackFormat -> StackFormat -> [CmmStmt] pack_continuation (StackFormat curr_id curr_frame_size _) (StackFormat cont_id cont_frame_size live_regs)