--------------------------------------------------------------------------------- Building info tables.---- (c) The University of Glasgow 2004-2006-------------------------------------------------------------------------------moduleCgInfoTbls(emitClosureCodeAndInfoTable,emitInfoTableAndCode,dataConTagZ,emitReturnTarget,emitAlgReturnTarget,emitReturnInstr,stdInfoTableSizeB,entryCode,closureInfoPtr,getConstrTag,cmmGetClosureType,infoTable,infoTableClosureType,infoTablePtrs,infoTableNonPtrs,funInfoTable,makeRelativeRefTo)where#include "HsVersions.h"importClosureInfoimportSMRepimportCgBinderyimportCgCallConvimportCgUtilsimportCgMonadimportCmmUtilsimportCmmimportCLabelimportNameimportDataConimportUniqueimportStaticFlagsimportConstantsimportUtilimportOutputable----------------------------------------------------------------------------- Generating the info table and code for a closure----------------------------------------------------------------------------- Here we make an info table of type 'CmmInfo'. The concrete-- representation as a list of 'CmmAddr' is handled later-- in the pipeline by 'cmmToRawCmm'.emitClosureCodeAndInfoTable::ClosureInfo->CmmFormals->CgStmts->CodeemitClosureCodeAndInfoTablecl_infoargsbody=do{blks<-cgStmtsToBlocksbody;info<-mkCmmInfocl_info;emitInfoTableAndCode(infoLblToEntryLblinfo_lbl)infoargsblks}whereinfo_lbl=infoTableLabelFromCIcl_info$clHasCafRefscl_info-- We keep the *zero-indexed* tag in the srt_len field of the info-- table of a data constructor.dataConTagZ::DataCon->ConTagZdataConTagZcon=dataConTagcon-fIRST_TAG-- Convert from 'ClosureInfo' to 'CmmInfo'.-- Not used for return points. (The 'smRepClosureTypeInt' call would panic.)mkCmmInfo::ClosureInfo->FCodeCmmInfomkCmmInfocl_info=doprof<-ifopt_SccProfilingOnthendoty_descr_lit<-mkStringCLit(closureTypeDescrcl_info)cl_descr_lit<-mkStringCLit(closureValDescrcl_info)return$ProfilingInfoty_descr_litcl_descr_litelsereturn$ProfilingInfo(mkIntCLit0)(mkIntCLit0)casecl_infoofConInfo{closureCon=con}->docstr<-mkByteStringCLit$dataConIdentityconletconName=makeRelativeRefToinfo_lblcstrinfo=ConstrInfo(ptrs,nptrs)(fromIntegral(dataConTagZcon))conNamereturn$CmmInfogc_targetNothing(CmmInfoTableFalseprofcl_typeinfo)ClosureInfo{closureName=name,closureLFInfo=lf_info,closureSRT=srt}->return$CmmInfogc_targetNothing(CmmInfoTableFalseprofcl_typeinfo)whereinfo=caself_infoofLFReEntrant_arity_arg_descr->FunInfo(ptrs,nptrs)srt(fromIntegralarity)arg_descr(CmmLabel(mkSlowEntryLabelnamehas_caf_refs))LFThunk___(SelectorThunkoffset)_->ThunkSelectorInfo(fromIntegraloffset)srtLFThunk_____->ThunkInfo(ptrs,nptrs)srt_->panic"unexpected lambda form in mkCmmInfo"whereinfo_lbl=infoTableLabelFromCIcl_infohas_caf_refshas_caf_refs=clHasCafRefscl_infocl_type=smRepClosureTypeInt(closureSMRepcl_info)ptrs=fromIntegral$closurePtrsSizecl_infosize=fromIntegral$closureNonHdrSizecl_infonptrs=size-ptrs-- The gc_target is to inform the CPS pass when it inserts a stack check.-- Since that pass isn't used yet we'll punt for now.-- When the CPS pass is fully integrated, this should-- be replaced by the label that any heap check jumped to,-- so that branch can be shared by both the heap (from codeGen)-- and stack checks (from the CPS pass).gc_target=panic"TODO: gc_target"----------------------------------------------------------------------------- Generating the info table and code for a return point----------------------------------------------------------------------------- The concrete representation as a list of 'CmmAddr' is handled later-- in the pipeline by 'cmmToRawCmm'.emitReturnTarget::Name->CgStmts-- The direct-return code (if any)->FCodeCLabelemitReturnTargetnamestmts=do{srt_info<-getSRTInfo;blks<-cgStmtsToBlocksstmts;frame<-mkStackLayout;letinfo=CmmInfogc_targetNothing(CmmInfoTableFalse(ProfilingInfozeroCLitzeroCLit)rET_SMALL-- cmmToRawCmm may convert it to rET_BIG(ContInfoframesrt_info));emitInfoTableAndCode(infoLblToEntryLblinfo_lbl)infoargsblks;returninfo_lbl}whereargs={- trace "emitReturnTarget: missing args" -}[]uniq=getUniquenameinfo_lbl=mkReturnInfoLabeluniq-- The gc_target is to inform the CPS pass when it inserts a stack check.-- Since that pass isn't used yet we'll punt for now.-- When the CPS pass is fully integrated, this should-- be replaced by the label that any heap check jumped to,-- so that branch can be shared by both the heap (from codeGen)-- and stack checks (from the CPS pass).gc_target=panic"TODO: gc_target"-- Build stack layout information from the state of the 'FCode' monad.-- Should go away once 'codeGen' starts using the CPS conversion-- pass to handle the stack. Until then, this is really just-- here to convert from the 'codeGen' representation of the stack-- to the 'CmmInfo' representation of the stack.---- See 'CmmInfo.mkLiveness' for where this is converted to a bitmap.{-
This seems to be a very error prone part of the code.
It is surprisingly prone to off-by-one errors, because
it converts between offset form (codeGen) and list form (CmmInfo).
Thus a bit of explanation is in order.
Fortunately, this code should go away once the code generator
starts using the CPS conversion pass to handle the stack.
The stack looks like this:
| |
|-------------|
frame_sp --> | return addr |
|-------------|
| dead slot |
|-------------|
| live ptr b |
|-------------|
| live ptr a |
|-------------|
real_sp --> | return addr |
+-------------+
Both 'frame_sp' and 'real_sp' are measured downwards
(i.e. larger frame_sp means smaller memory address).
For that frame we want a result like: [Just a, Just b, Nothing]
Note that the 'head' of the list is the top
of the stack, and that the return address
is not present in the list (it is always assumed).
-}mkStackLayout::FCode[MaybeLocalReg]mkStackLayout=doStackUsage{realSp=real_sp,frameSp=frame_sp}<-getStkUsagebinds<-getLiveStackBindingsletframe_size=real_sp-frame_sp-retAddrSizeWrel_binds=reverse$sortWithfst[(offset-frame_sp-retAddrSizeW,b)|(offset,b)<-binds]WARN(not(all(\bind->fstbind>=0)rel_binds),pprbinds$$pprrel_binds$$pprframe_size$$pprreal_sp$$pprframe_sp)return$stack_layoutrel_bindsframe_sizestack_layout::[(VirtualSpOffset,CgIdInfo)]->WordOff->[MaybeLocalReg]stack_layout[]sizeW=replicatesizeWNothingstack_layout((off,bind):binds)sizeW|off==sizeW-1=(Juststack_bind):(stack_layoutbinds(sizeW-rep_size))whererep_size=cgRepSizeW(cgIdInfoArgRepbind)stack_bind=LocalReguniquemachRepunique=getUnique(cgIdInfoIdbind)machRep=argMachRep(cgIdInfoArgRepbind)stack_layoutbinds@(_:_)sizeW|otherwise=Nothing:(stack_layoutbinds(sizeW-1)){- Another way to write the function that might be less error prone (untested)
stack_layout offsets sizeW = result
where
y = map (flip lookup offsets) [0..]
-- offsets -> nothing and just (each slot is one word)
x = take sizeW y -- set the frame size
z = clip x -- account for multi-word slots
result = map mk_reg z
clip [] = []
clip list@(x : _) = x : clip (drop count list)
ASSERT(all isNothing (tail (take count list)))
count Nothing = 1
count (Just x) = cgRepSizeW (cgIdInfoArgRep x)
mk_reg Nothing = Nothing
mk_reg (Just x) = LocalReg unique machRep kind
where
unique = getUnique (cgIdInfoId x)
machRep = argMachrep (cgIdInfoArgRep bind)
kind = if isFollowableArg (cgIdInfoArgRep bind)
then GCKindPtr
else GCKindNonPtr
-}emitAlgReturnTarget::Name-- Just for its unique->[(ConTagZ,CgStmts)]-- Tagged branches->MaybeCgStmts-- Default branch (if any)->Int-- family size->FCode(CLabel,SemiTaggingStuff)emitAlgReturnTargetnamebranchesmb_defltfam_sz=do{blks<-getCgStmts$-- is the constructor tag in the node reg?ifisSmallFamilyfam_szthendo-- yes, node has constr. taglettag_expr=cmmConstrTag1(CmmRegnodeReg)branches'=[(tag+1,branch)|(tag,branch)<-branches]emitSwitchtag_exprbranches'mb_deflt1fam_szelsedo-- no, get tag from info tablelet-- Note that ptr _always_ has tag 1-- when the family size is big enoughuntagged_ptr=cmmRegOffBnodeReg(-1)tag_expr=getConstrTag(untagged_ptr)emitSwitchtag_exprbranchesmb_deflt0(fam_sz-1);lbl<-emitReturnTargetnameblks;return(lbl,Nothing)}-- Nothing: the internal branches in the switch don't have-- global labels, so we can't use them at the 'call site'--------------------------------emitReturnInstr::CodeemitReturnInstr=do{info_amode<-getSequelAmode;stmtC(CmmJump(entryCodeinfo_amode)[])}--------------------------------------------------------------------------------- Info table offsets-------------------------------------------------------------------------------stdInfoTableSizeW::WordOff-- The size of a standard info table varies with profiling/ticky etc,-- so we can't get it from Constants-- It must vary in sync with mkStdInfoTablestdInfoTableSizeW=size_fixed+size_profwheresize_fixed=2-- layout, typesize_prof|opt_SccProfilingOn=2|otherwise=0stdInfoTableSizeB::ByteOffstdInfoTableSizeB=stdInfoTableSizeW*wORD_SIZEstdSrtBitmapOffset::ByteOff-- Byte offset of the SRT bitmap half-word which is -- in the *higher-addressed* part of the type_litstdSrtBitmapOffset=stdInfoTableSizeB-hALF_WORD_SIZEstdClosureTypeOffset::ByteOff-- Byte offset of the closure type half-word stdClosureTypeOffset=stdInfoTableSizeB-wORD_SIZEstdPtrsOffset,stdNonPtrsOffset::ByteOffstdPtrsOffset=stdInfoTableSizeB-2*wORD_SIZEstdNonPtrsOffset=stdInfoTableSizeB-2*wORD_SIZE+hALF_WORD_SIZE----------------------------------------------------------------------------- Accessing fields of an info table---------------------------------------------------------------------------closureInfoPtr::CmmExpr->CmmExpr-- Takes a closure pointer and returns the info table pointerclosureInfoPtre=CmmLoadebWordentryCode::CmmExpr->CmmExpr-- Takes an info pointer (the first word of a closure)-- and returns its entry codeentryCodee|tablesNextToCode=e|otherwise=CmmLoadebWordgetConstrTag::CmmExpr->CmmExpr-- Takes a closure pointer, and return the *zero-indexed*-- constructor tag obtained from the info table-- This lives in the SRT field of the info table-- (constructors don't need SRTs).getConstrTagclosure_ptr=CmmMachOp(MO_UU_ConvhalfWordWidthwordWidth)[infoTableConstrTaginfo_table]whereinfo_table=infoTable(closureInfoPtrclosure_ptr)cmmGetClosureType::CmmExpr->CmmExpr-- Takes a closure pointer, and return the closure type-- obtained from the info tablecmmGetClosureTypeclosure_ptr=CmmMachOp(MO_UU_ConvhalfWordWidthwordWidth)[infoTableClosureTypeinfo_table]whereinfo_table=infoTable(closureInfoPtrclosure_ptr)infoTable::CmmExpr->CmmExpr-- Takes an info pointer (the first word of a closure)-- and returns a pointer to the first word of the standard-form-- info table, excluding the entry-code word (if present)infoTableinfo_ptr|tablesNextToCode=cmmOffsetBinfo_ptr(-stdInfoTableSizeB)|otherwise=cmmOffsetWinfo_ptr1-- Past the entry code pointerinfoTableConstrTag::CmmExpr->CmmExpr-- Takes an info table pointer (from infoTable) and returns the constr tag-- field of the info table (same as the srt_bitmap field)infoTableConstrTag=infoTableSrtBitmapinfoTableSrtBitmap::CmmExpr->CmmExpr-- Takes an info table pointer (from infoTable) and returns the srt_bitmap-- field of the info tableinfoTableSrtBitmapinfo_tbl=CmmLoad(cmmOffsetBinfo_tblstdSrtBitmapOffset)bHalfWordinfoTableClosureType::CmmExpr->CmmExpr-- Takes an info table pointer (from infoTable) and returns the closure type-- field of the info table.infoTableClosureTypeinfo_tbl=CmmLoad(cmmOffsetBinfo_tblstdClosureTypeOffset)bHalfWordinfoTablePtrs::CmmExpr->CmmExprinfoTablePtrsinfo_tbl=CmmLoad(cmmOffsetBinfo_tblstdPtrsOffset)bHalfWordinfoTableNonPtrs::CmmExpr->CmmExprinfoTableNonPtrsinfo_tbl=CmmLoad(cmmOffsetBinfo_tblstdNonPtrsOffset)bHalfWordfunInfoTable::CmmExpr->CmmExpr-- Takes the info pointer of a function,-- and returns a pointer to the first word of the StgFunInfoExtra struct-- in the info table.funInfoTableinfo_ptr|tablesNextToCode=cmmOffsetBinfo_ptr(-stdInfoTableSizeB-sIZEOF_StgFunInfoExtraRev)|otherwise=cmmOffsetWinfo_ptr(1+stdInfoTableSizeW)-- Past the entry code pointer----------------------------------------------------------------------------- Emit the code for a closure (or return address)-- and its associated info table----------------------------------------------------------------------------- The complication here concerns whether or not we can-- put the info table next to the codeemitInfoTableAndCode::CLabel-- Label of entry or ret->CmmInfo-- ...the info table->CmmFormals-- ...args->[CmmBasicBlock]-- ...and body->CodeemitInfoTableAndCodeentry_ret_lblinfoargsblocks=emitProcinfoentry_ret_lblargsblocks----------------------------------------------------------------------------- Position independent code----------------------------------------------------------------------------- In order to support position independent code, we mustn't put absolute-- references into read-only space. Info tables in the tablesNextToCode-- case must be in .text, which is read-only, so we doctor the CmmLits-- to use relative offsets instead.-- Note that this is done even when the -fPIC flag is not specified,-- as we want to keep binary compatibility between PIC and non-PIC.makeRelativeRefTo::CLabel->CmmLit->CmmLitmakeRelativeRefToinfo_lbl(CmmLabellbl)|tablesNextToCode=CmmLabelDiffOfflblinfo_lbl0makeRelativeRefToinfo_lbl(CmmLabelOfflbloff)|tablesNextToCode=CmmLabelDiffOfflblinfo_lbloffmakeRelativeRefTo_lit=lit