Newer
Older
//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the AsmPrinter class.
//
//===----------------------------------------------------------------------===//
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner
committed
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidis
committed
#include "llvm/Analysis/DebugInfo.h"
Evan Cheng
committed
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson
committed
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include <cerrno>
using namespace llvm;
Evan Cheng
committed
static cl::opt<cl::boolOrDefault>
AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
cl::init(cl::BOU_UNSET));
const TargetAsmInfo *T, bool VDef)
: MachineFunctionPass(&ID), FunctionNumber(0), O(o),
TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
IsInTextSection(false), LastMI(0), LastFn(0), Counter(~0U),
PrevDLT(0, ~0U, ~0U) {
Chris Lattner
committed
DW = 0; MMI = 0;
Evan Cheng
committed
switch (AsmVerbose) {
case cl::BOU_UNSET: VerboseAsm = VDef; break;
case cl::BOU_TRUE: VerboseAsm = true; break;
case cl::BOU_FALSE: VerboseAsm = false; break;
}
}
Gordon Henriksen
committed
AsmPrinter::~AsmPrinter() {
for (gcp_iterator I = GCMetadataPrinters.begin(),
E = GCMetadataPrinters.end(); I != E; ++I)
delete I->second;
}
/// SwitchToTextSection - Switch to the specified text section of the executable
/// if we are not already in it!
void AsmPrinter::SwitchToTextSection(const char *NewSection,
const GlobalValue *GV) {
std::string NS;
if (GV && GV->hasSection())
NS = TAI->getSwitchToSectionDirective() + GV->getSection();
else
NS = NewSection;
// If we're already in this section, we're done.
if (CurrentSection == NS) return;
Chris Lattner
committed
// Close the current section, if applicable.
if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner
committed
CurrentSection = NS;
if (!CurrentSection.empty())
O << CurrentSection << TAI->getTextSectionStartSuffix() << '\n';
Evan Cheng
committed
IsInTextSection = true;
}
/// SwitchToDataSection - Switch to the specified data section of the executable
/// if we are not already in it!
///
void AsmPrinter::SwitchToDataSection(const char *NewSection,
const GlobalValue *GV) {
std::string NS;
Chris Lattner
committed
if (GV && GV->hasSection())
NS = TAI->getSwitchToSectionDirective() + GV->getSection();
Chris Lattner
committed
else
NS = NewSection;
Chris Lattner
committed
// If we're already in this section, we're done.
if (CurrentSection == NS) return;
Chris Lattner
committed
// Close the current section, if applicable.
if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
Chris Lattner
committed
CurrentSection = NS;
Chris Lattner
committed
if (!CurrentSection.empty())
O << CurrentSection << TAI->getDataSectionStartSuffix() << '\n';
Evan Cheng
committed
IsInTextSection = false;
/// SwitchToSection - Switch to the specified section of the executable if we
/// are not already in it!
void AsmPrinter::SwitchToSection(const Section* NS) {
const std::string& NewSection = NS->getName();
// If we're already in this section, we're done.
if (CurrentSection == NewSection) return;
// Close the current section, if applicable.
if (TAI->getSectionEndDirectiveSuffix() && !CurrentSection.empty())
O << CurrentSection << TAI->getSectionEndDirectiveSuffix() << '\n';
// FIXME: Make CurrentSection a Section* in the future
CurrentSection = NewSection;
CurrentSection_ = NS;
Anton Korobeynikov
committed
if (!CurrentSection.empty()) {
// If section is named we need to switch into it via special '.section'
// directive and also append funky flags. Otherwise - section name is just
// some magic assembler directive.
if (NS->isNamed())
O << TAI->getSwitchToSectionDirective()
<< CurrentSection
<< TAI->getSectionFlags(NS->getFlags());
else
O << CurrentSection;
O << TAI->getDataSectionStartSuffix() << '\n';
}
IsInTextSection = (NS->getFlags() & SectionFlags::Code);
}
Gordon Henriksen
committed
void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
MachineFunctionPass::getAnalysisUsage(AU);
AU.addRequired<GCModuleInfo>();
Gordon Henriksen
committed
}
bool AsmPrinter::doInitialization(Module &M) {
Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix());
if (TAI->doesAllowQuotesInName())
Mang->setUseQuotes(true);
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
assert(MI && "AsmPrinter didn't require GCModuleInfo?");
if (TAI->hasSingleParameterDotFile()) {
/* Very minimal debug info. It is ignored if we emit actual
debug info. If we don't, this at helps the user find where
a function came from. */
O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
}
for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
MP->beginAssembly(O, *this, *TAI);
Gordon Henriksen
committed
O << TAI->getCommentString() << " Start of file scope inline assembly\n"
<< '\n' << TAI->getCommentString()
<< " End of file scope inline assembly\n";
SwitchToDataSection(""); // Reset back to no section.
Chris Lattner
committed
if (TAI->doesSupportDebugInformation() ||
TAI->doesSupportExceptionHandling()) {
MMI = getAnalysisIfAvailable<MachineModuleInfo>();
if (MMI)
MMI->AnalyzeModule(M);
Chris Lattner
committed
DW = getAnalysisIfAvailable<DwarfWriter>();
if (DW)
DW->BeginModule(&M, MMI, O, this, TAI);
return false;
}
bool AsmPrinter::doFinalization(Module &M) {
// Emit final debug information.
if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
DW->EndModule();
Loading
Loading full blame...