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"
Chris Lattner
committed
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
Evan Cheng
committed
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Mangler.h"
#include "llvm/MC/MCAsmInfo.h"
Owen Anderson
committed
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetLoweringObjectFile.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 MCAsmInfo *T, bool VDef)
: MachineFunctionPass(&ID), FunctionNumber(0), O(o),
TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner
committed
OutContext(*new MCContext()),
// FIXME: Pass instprinter to streamer.
OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner
committed
Chris Lattner
committed
LastMI(0), LastFn(0), Counter(~0U),
Devang Patel
committed
PrevDLT(0, 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;
Chris Lattner
committed
delete &OutStreamer;
delete &OutContext;
Gordon Henriksen
committed
}
TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
return TM.getTargetLowering()->getObjFileLowering();
}
Chris Lattner
committed
/// getCurrentSection() - Return the current section we are emitting to.
const MCSection *AsmPrinter::getCurrentSection() const {
return OutStreamer.getCurrentSection();
Chris Lattner
committed
Gordon Henriksen
committed
void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
Gordon Henriksen
committed
MachineFunctionPass::getAnalysisUsage(AU);
AU.addRequired<GCModuleInfo>();
Gordon Henriksen
committed
}
bool AsmPrinter::doInitialization(Module &M) {
// Initialize TargetLoweringObjectFile.
const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
.Initialize(OutContext, TM);
Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
MAI->getLinkerPrivateGlobalPrefix());
if (MAI->doesAllowQuotesInName())
Mang->setUseQuotes(true);
Anton Korobeynikov
committed
if (MAI->doesAllowNameToStartWithDigit())
Mang->setSymbolsCanStartWithDigit(true);
// Allow the target to emit any magic that it wants at the start of the file.
EmitStartOfAsmFile(M);
if (MAI->hasSingleParameterDotFile()) {
/* Very minimal debug info. It is ignored if we emit actual
debug info. If we don't, this at least helps the user find where
a function came from. */
O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
}
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
assert(MI && "AsmPrinter didn't require GCModuleInfo?");
for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
MP->beginAssembly(O, *this, *MAI);
Gordon Henriksen
committed
O << MAI->getCommentString() << " Start of file scope inline assembly\n"
<< '\n' << MAI->getCommentString()
<< " End of file scope inline assembly\n";
Chris Lattner
committed
MMI = getAnalysisIfAvailable<MachineModuleInfo>();
if (MMI)
MMI->AnalyzeModule(M);
DW = getAnalysisIfAvailable<DwarfWriter>();
Chris Lattner
committed
DW->BeginModule(&M, MMI, O, this, MAI);
return false;
}
bool AsmPrinter::doFinalization(Module &M) {
// Emit global variables.
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I)
PrintGlobalVariable(I);
// Emit final debug information.
if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
DW->EndModule();
// If the target wants to know about weak references, print them all.
if (MAI->getWeakRefDirective()) {
// FIXME: This is not lazy, it would be nice to only print weak references
// to stuff that is actually used. Note that doing so would require targets
// to notice uses in operands (due to constant exprs etc). This should
// happen with the MC stuff eventually.
// Print out module-level global variables here.
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I) {
if (I->hasExternalWeakLinkage())
O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
if (I->hasExternalWeakLinkage())
O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
if (MAI->getSetDirective()) {
O << '\n';
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ++I) {
std::string Name = Mang->getMangledName(I);
Anton Korobeynikov
committed
const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
std::string Target = Mang->getMangledName(GV);
if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
O << "\t.globl\t" << Name << '\n';
else if (I->hasWeakLinkage())
O << MAI->getWeakRefDirective() << Name << '\n';
llvm_unreachable("Invalid alias linkage");
O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
}
}
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Loading
Loading full blame...