Moduleghc-10.0.0.20260917GHC2024
GHC.CmmToAsm
Note [Native code generator] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The native-code generator has machine-independent and machine-dependent modules.
This module (GHC.CmmToAsm) is the top-level machine-independent
module. Before entering machine-dependent land, we do some
machine-independent optimisations (defined below) on the
CmmStmtss. (Which ideally would be folded into CmmOpt ...)
We convert to the machine-specific Instr datatype with
cmmCodeGen, assuming an infinite supply of registers. We then use
a (mostly) machine-independent register allocator to rejoin
reality. Obviously, regAlloc has machine-specific helper
functions (see the used register allocator for details).
Finally, we order the basic blocks of the function so as to minimise the number of jumps between blocks, by utilising fallthrough wherever possible.
The machine-dependent bits are generally contained under GHCCmmToAsmArch/* and generally breaks down as follows:
Regs: Everything about the target platform's machine registers (and immediate operands, and addresses, which tend to intermingle/interact with registers).Instr: Includes the Instr datatype plus a miscellany of other things (e.g.,targetDoubleSize,smStablePtrTable, ...)CodeGen: is whereCmmstuff turns into machine instructions.Ppr: pprInstr turns an Instr into text (well, really a SDoc).
The register allocators lives under GHC.CmmToAsm.Reg.*, there is both a Linear and a Graph based register allocator. Both of which have their own notes describing them. They are mostly platform independent but there are some platform specific files encoding architecture details under RegAllocatorArch.hs
}
- 1 type
- 2 values
- Packageghc-10.0.0.20260917
- Exports3
- LanguageGHC2024
- LicenceBSD-3-Clause
- SourceCmmToAsm.hs
Test-only exports: see #12744
2 declarationscmmNativeGen :: (Instruction instr, OutputableP Platform statics, Outputable jumpDest)=> Logger-> NcgImpl statics instr jumpDest-> DUniqSupply-> DwarfFiles-> LabelMap DebugBlock-> RawCmmDeclthe cmm to generate code for
-> Intsequence number of this top thing
-> IO (DUniqSupply, DwarfFiles, [NatCmmDecl statics instr], [CLabel], Maybe [RegAllocStats statics instr], Maybe [RegAllocStats], LabelMap [UnwindPoint], Maybe CFG)
Complete native code generation phase for a single top-level chunk of Cmm. Dumping the output of each stage along the way. Global conflict graph and NGC stats
A Native Code Generator implementation is parametrised over
* The type of static data (typically related to CmmStatics)
* The type of instructions
* The type of jump destinations
Constructors
NcgImplncgConfig :: !NCGConfigcmmTopCodeGen :: RawCmmDecl -> NatM [NatCmmDecl statics instr]generateJumpTableForInstr :: instr -> Maybe (NatCmmDecl statics instr)getJumpDestBlockId :: jumpDest -> Maybe BlockIdGiven a jump destination, if it refers to a block, return the block id of the destination.
canShortcut :: instr -> Maybe jumpDestDoes this jump always jump to a single destination and is shortcutable?
We use this to determine whether the given instruction is a shortcutable jump to some destination - See Note [supporting shortcutting] Note that if we return a destination here we *most* support the relevant shortcutting in shortcutStatics for jump tables and shortcutJump for the instructions itself.
shortcutStatics :: (BlockId -> Maybe jumpDest) -> statics -> staticsReplace references to blockIds with other destinations - used to update jump tables.
shortcutJump :: (BlockId -> Maybe jumpDest) -> instr -> instrChange the jump destination(s) of an instruction.
Rewrites the destination of a jump instruction to another destination, if the given function returns a new jump destination for the BlockId of the original destination.
For instance, for a mapping
block_a -> dest_band a instructiongoto block_awe would rewrite the instruction togoto dest_bpprNatCmmDeclS :: NatCmmDecl statics instr -> SDocModule is only for printing internal labels. See Note [Internal proc labels] in CLabel.
pprNatCmmDeclH :: NatCmmDecl statics instr -> HDocmaxSpillSlots :: IntallocatableRegs :: [RealReg]ncgAllocMoreStack :: Int -> NatCmmDecl statics instr -> UniqDSM (NatCmmDecl statics instr, [(BlockId, BlockId)])The list of block ids records the redirected jumps to allow us to update the CFG.
ncgMakeFarBranches :: Platform -> LabelMap RawCmmStatics -> [NatBasicBlock instr] -> UniqDSM [NatBasicBlock instr]extractUnwindPoints :: [instr] -> [UnwindPoint]given the instruction sequence of a block, produce a list of the block's UnwindPoints See Note [What is this unwinding business?] in GHC.Cmm.DebugBlock and Note [Unwinding information in the NCG] in this module.
invertCondBranches :: Maybe CFG -> LabelMap RawCmmStatics -> [NatBasicBlock instr] -> [NatBasicBlock instr]Turn the sequence of
jcc l1; jmp l2intojncc l2; <block_l1>when possible.