From f1a5c7ec04002769f1638e64f7439589f0f926e6 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sat, 30 Apr 2011 03:44:37 +0000 Subject: [PATCH] Add all the plumbing needed for MC to expand cfi to the old tables in the final assembly. It is the same technique used when targeting assemblers that don't support .loc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@130587 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCStreamer.h | 1 + include/llvm/Target/TargetMachine.h | 7 ++++ include/llvm/Target/TargetRegistry.h | 6 ++-- lib/CodeGen/LLVMTargetMachine.cpp | 1 + lib/MC/MCAsmStreamer.cpp | 48 +++++++++++++++++++++++++--- lib/Target/PTX/PTXMCAsmStreamer.cpp | 2 +- lib/Target/PTX/PTXTargetMachine.cpp | 1 + lib/Target/TargetMachine.cpp | 3 +- tools/llc/llc.cpp | 6 ++++ tools/llvm-mc/llvm-mc.cpp | 3 +- 10 files changed, 69 insertions(+), 9 deletions(-) diff --git a/include/llvm/MC/MCStreamer.h b/include/llvm/MC/MCStreamer.h index 5c7608be80c..7f529c67f06 100644 --- a/include/llvm/MC/MCStreamer.h +++ b/include/llvm/MC/MCStreamer.h @@ -510,6 +510,7 @@ namespace llvm { MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, bool isVerboseAsm, bool useLoc, + bool useCFI, MCInstPrinter *InstPrint = 0, MCCodeEmitter *CE = 0, TargetAsmBackend *TAB = 0, diff --git a/include/llvm/Target/TargetMachine.h b/include/llvm/Target/TargetMachine.h index 627ab427ef8..78f770cc41c 100644 --- a/include/llvm/Target/TargetMachine.h +++ b/include/llvm/Target/TargetMachine.h @@ -108,6 +108,7 @@ protected: // Can only create subclasses. unsigned MCNoExecStack : 1; unsigned MCSaveTempLabels : 1; unsigned MCUseLoc : 1; + unsigned MCUseCFI : 1; public: virtual ~TargetMachine(); @@ -193,6 +194,12 @@ public: /// setMCUseLoc - Set whether all we should use dwarf's .loc directive. void setMCUseLoc(bool Value) { MCUseLoc = Value; } + /// hasMCUseCFI - Check whether we should use dwarf's .cfi_* directives. + bool hasMCUseCFI() const { return MCUseCFI; } + + /// setMCUseCFI - Set whether all we should use dwarf's .cfi_* directives. + void setMCUseCFI(bool Value) { MCUseCFI = Value; } + /// getRelocationModel - Returns the code generation relocation model. The /// choices are static, PIC, and dynamic-no-pic, and target default. static Reloc::Model getRelocationModel(); diff --git a/include/llvm/Target/TargetRegistry.h b/include/llvm/Target/TargetRegistry.h index 3739a0f83a4..a464822893b 100644 --- a/include/llvm/Target/TargetRegistry.h +++ b/include/llvm/Target/TargetRegistry.h @@ -43,7 +43,7 @@ namespace llvm { MCStreamer *createAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, bool isVerboseAsm, - bool useLoc, + bool useLoc, bool useCFI, MCInstPrinter *InstPrint, MCCodeEmitter *CE, TargetAsmBackend *TAB, @@ -96,6 +96,7 @@ namespace llvm { formatted_raw_ostream &OS, bool isVerboseAsm, bool useLoc, + bool useCFI, MCInstPrinter *InstPrint, MCCodeEmitter *CE, TargetAsmBackend *TAB, @@ -329,12 +330,13 @@ namespace llvm { formatted_raw_ostream &OS, bool isVerboseAsm, bool useLoc, + bool useCFI, MCInstPrinter *InstPrint, MCCodeEmitter *CE, TargetAsmBackend *TAB, bool ShowInst) const { // AsmStreamerCtorFn is default to llvm::createAsmStreamer - return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, + return AsmStreamerCtorFn(Ctx, OS, isVerboseAsm, useLoc, useCFI, InstPrint, CE, TAB, ShowInst); } diff --git a/lib/CodeGen/LLVMTargetMachine.cpp b/lib/CodeGen/LLVMTargetMachine.cpp index 8c2794a729a..e1dad2efa98 100644 --- a/lib/CodeGen/LLVMTargetMachine.cpp +++ b/lib/CodeGen/LLVMTargetMachine.cpp @@ -149,6 +149,7 @@ bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, MCStreamer *S = getTarget().createAsmStreamer(*Context, Out, getVerboseAsm(), hasMCUseLoc(), + hasMCUseCFI(), InstPrinter, MCE, TAB, ShowMCInst); diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp index 2538475e4e0..b0b948e2b9c 100644 --- a/lib/MC/MCAsmStreamer.cpp +++ b/lib/MC/MCAsmStreamer.cpp @@ -45,20 +45,20 @@ class MCAsmStreamer : public MCStreamer { unsigned IsVerboseAsm : 1; unsigned ShowInst : 1; unsigned UseLoc : 1; + unsigned UseCFI : 1; bool needsSet(const MCExpr *Value); public: MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os, - bool isVerboseAsm, - bool useLoc, + bool isVerboseAsm, bool useLoc, bool useCFI, MCInstPrinter *printer, MCCodeEmitter *emitter, TargetAsmBackend *asmbackend, bool showInst) : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()), InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend), CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm), - ShowInst(showInst), UseLoc(useLoc) { + ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI) { if (InstPrinter && IsVerboseAsm) InstPrinter->setCommentStream(CommentStream); } @@ -740,6 +740,9 @@ void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo, unsigned Line, void MCAsmStreamer::EmitCFIStartProc() { MCStreamer::EmitCFIStartProc(); + if (!UseCFI) + return; + OS << "\t.cfi_startproc"; EmitEOL(); } @@ -747,6 +750,9 @@ void MCAsmStreamer::EmitCFIStartProc() { void MCAsmStreamer::EmitCFIEndProc() { MCStreamer::EmitCFIEndProc(); + if (!UseCFI) + return; + OS << "\t.cfi_endproc"; EmitEOL(); } @@ -754,6 +760,9 @@ void MCAsmStreamer::EmitCFIEndProc() { void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { MCStreamer::EmitCFIDefCfa(Register, Offset); + if (!UseCFI) + return; + OS << ".cfi_def_cfa " << Register << ", " << Offset; EmitEOL(); } @@ -761,6 +770,9 @@ void MCAsmStreamer::EmitCFIDefCfa(int64_t Register, int64_t Offset) { void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) { MCStreamer::EmitCFIDefCfaOffset(Offset); + if (!UseCFI) + return; + OS << "\t.cfi_def_cfa_offset " << Offset; EmitEOL(); } @@ -768,6 +780,9 @@ void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset) { void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) { MCStreamer::EmitCFIDefCfaRegister(Register); + if (!UseCFI) + return; + OS << "\t.cfi_def_cfa_register " << Register; EmitEOL(); } @@ -775,6 +790,9 @@ void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register) { void MCAsmStreamer::EmitCFIOffset(int64_t Register, int64_t Offset) { this->MCStreamer::EmitCFIOffset(Register, Offset); + if (!UseCFI) + return; + OS << "\t.cfi_offset " << Register << ", " << Offset; EmitEOL(); } @@ -783,6 +801,9 @@ void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding) { MCStreamer::EmitCFIPersonality(Sym, Encoding); + if (!UseCFI) + return; + OS << "\t.cfi_personality " << Encoding << ", " << *Sym; EmitEOL(); } @@ -790,6 +811,9 @@ void MCAsmStreamer::EmitCFIPersonality(const MCSymbol *Sym, void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) { MCStreamer::EmitCFILsda(Sym, Encoding); + if (!UseCFI) + return; + OS << "\t.cfi_lsda " << Encoding << ", " << *Sym; EmitEOL(); } @@ -797,6 +821,9 @@ void MCAsmStreamer::EmitCFILsda(const MCSymbol *Sym, unsigned Encoding) { void MCAsmStreamer::EmitCFIRememberState() { MCStreamer::EmitCFIRememberState(); + if (!UseCFI) + return; + OS << "\t.cfi_remember_state"; EmitEOL(); } @@ -804,6 +831,9 @@ void MCAsmStreamer::EmitCFIRememberState() { void MCAsmStreamer::EmitCFIRestoreState() { MCStreamer::EmitCFIRestoreState(); + if (!UseCFI) + return; + OS << "\t.cfi_restore_state"; EmitEOL(); } @@ -811,6 +841,9 @@ void MCAsmStreamer::EmitCFIRestoreState() { void MCAsmStreamer::EmitCFISameValue(int64_t Register) { MCStreamer::EmitCFISameValue(Register); + if (!UseCFI) + return; + OS << "\t.cfi_same_value " << Register; EmitEOL(); } @@ -818,6 +851,9 @@ void MCAsmStreamer::EmitCFISameValue(int64_t Register) { void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) { MCStreamer::EmitCFIRelOffset(Register, Offset); + if (!UseCFI) + return; + OS << "\t.cfi_rel_offset " << Register << ", " << Offset; EmitEOL(); } @@ -825,6 +861,9 @@ void MCAsmStreamer::EmitCFIRelOffset(int64_t Register, int64_t Offset) { void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment) { MCStreamer::EmitCFIAdjustCfaOffset(Adjustment); + if (!UseCFI) + return; + OS << "\t.cfi_adjust_cfa_offset " << Adjustment; EmitEOL(); } @@ -1008,8 +1047,9 @@ void MCAsmStreamer::Finish() { MCStreamer *llvm::createAsmStreamer(MCContext &Context, formatted_raw_ostream &OS, bool isVerboseAsm, bool useLoc, + bool useCFI, MCInstPrinter *IP, MCCodeEmitter *CE, TargetAsmBackend *TAB, bool ShowInst) { - return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, + return new MCAsmStreamer(Context, OS, isVerboseAsm, useLoc, useCFI, IP, CE, TAB, ShowInst); } diff --git a/lib/Target/PTX/PTXMCAsmStreamer.cpp b/lib/Target/PTX/PTXMCAsmStreamer.cpp index 0d1f7846ef3..63bac790cc3 100644 --- a/lib/Target/PTX/PTXMCAsmStreamer.cpp +++ b/lib/Target/PTX/PTXMCAsmStreamer.cpp @@ -531,7 +531,7 @@ void PTXMCAsmStreamer::Finish() {} namespace llvm { MCStreamer *createPTXAsmStreamer(MCContext &Context, formatted_raw_ostream &OS, - bool isVerboseAsm, bool useLoc, + bool isVerboseAsm, bool useLoc, bool useCFI, MCInstPrinter *IP, MCCodeEmitter *CE, TargetAsmBackend *TAB, bool ShowInst) { diff --git a/lib/Target/PTX/PTXTargetMachine.cpp b/lib/Target/PTX/PTXTargetMachine.cpp index 78a7b0daf15..1b737c9d863 100644 --- a/lib/Target/PTX/PTXTargetMachine.cpp +++ b/lib/Target/PTX/PTXTargetMachine.cpp @@ -23,6 +23,7 @@ using namespace llvm; namespace llvm { MCStreamer *createPTXAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS, bool isVerboseAsm, bool useLoc, + bool useCFI, MCInstPrinter *InstPrint, MCCodeEmitter *CE, TargetAsmBackend *TAB, diff --git a/lib/Target/TargetMachine.cpp b/lib/Target/TargetMachine.cpp index 4a1ac94826f..76ccc09195a 100644 --- a/lib/Target/TargetMachine.cpp +++ b/lib/Target/TargetMachine.cpp @@ -227,7 +227,8 @@ TargetMachine::TargetMachine(const Target &T) MCRelaxAll(false), MCNoExecStack(false), MCSaveTempLabels(false), - MCUseLoc(true) { + MCUseLoc(true), + MCUseCFI(true) { // Typically it will be subtargets that will adjust FloatABIType from Default // to Soft or Hard. if (UseSoftFloat) diff --git a/tools/llc/llc.cpp b/tools/llc/llc.cpp index 77e3ddce3af..a36b6d77b95 100644 --- a/tools/llc/llc.cpp +++ b/tools/llc/llc.cpp @@ -99,6 +99,9 @@ cl::opt NoVerify("disable-verify", cl::Hidden, cl::opt DisableDotLoc("disable-dot-loc", cl::Hidden, cl::desc("Do not use .loc entries")); +cl::opt DisableCFI("disable-cfi", cl::Hidden, + cl::desc("Do not use .cfi_* directives")); + static cl::opt DisableRedZone("disable-red-zone", cl::desc("Do not emit code that uses the red zone."), @@ -279,6 +282,9 @@ int main(int argc, char **argv) { if (DisableDotLoc) Target.setMCUseLoc(false); + if (DisableCFI) + Target.setMCUseCFI(false); + // Disable .loc support for older OS X versions. if (TheTriple.isMacOSX() && TheTriple.isMacOSXVersionLT(10, 6)) diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp index f76b6edb80e..24cc263c2ec 100644 --- a/tools/llvm-mc/llvm-mc.cpp +++ b/tools/llvm-mc/llvm-mc.cpp @@ -356,7 +356,8 @@ static int AssembleInput(const char *ProgName) { TAB = TheTarget->createAsmBackend(TripleName); } Str.reset(TheTarget->createAsmStreamer(Ctx, FOS, /*asmverbose*/true, - /*useLoc*/ true, IP, CE, TAB, + /*useLoc*/ true, + /*useCFI*/ true, IP, CE, TAB, ShowInst)); } else if (FileType == OFT_Null) { Str.reset(createNullStreamer(Ctx));