Skip to content
AsmPrinter.cpp 64.2 KiB
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/DerivedTypes.h"
#include "llvm/Constants.h"
#include "llvm/Module.h"
#include "llvm/CodeGen/GCMetadataPrinter.h"
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greene's avatar
 
David Greene committed
#include "llvm/CodeGen/MachineLoopInfo.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/DwarfWriter.h"
David Greene's avatar
 
David Greene committed
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Support/ErrorHandling.h"
David Greene's avatar
 
David Greene committed
#include "llvm/Support/FormattedStream.h"
David Greene's avatar
 
David Greene committed
#include "llvm/Support/IOManip.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Target/TargetAsmInfo.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
static cl::opt<cl::boolOrDefault>
AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
           cl::init(cl::BOU_UNSET));

David Greene's avatar
 
David Greene committed
static cl::opt<cl::boolOrDefault>
AsmExuberant("asm-exuberant", cl::desc("Add many comments."),
           cl::init(cl::BOU_FALSE));

Devang Patel's avatar
Devang Patel committed
char AsmPrinter::ID = 0;
David Greene's avatar
 
David Greene committed
AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
                       const TargetAsmInfo *T, bool VDef)
  : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
    TM(tm), TAI(T), TRI(tm.getRegisterInfo()),

    OutContext(*new MCContext()),
    OutStreamer(*createAsmStreamer(OutContext, O)),

    PrevDLT(0, ~0U, ~0U) {
  switch (AsmVerbose) {
  case cl::BOU_UNSET: VerboseAsm = VDef;  break;
  case cl::BOU_TRUE:  VerboseAsm = true;  break;
  case cl::BOU_FALSE: VerboseAsm = false; break;
  }
David Greene's avatar
 
David Greene committed
  switch (AsmExuberant) {
  case cl::BOU_UNSET: ExuberantAsm = false;  break;
  case cl::BOU_TRUE:  ExuberantAsm = true;  break;
  case cl::BOU_FALSE: ExuberantAsm = false; break;
  }
AsmPrinter::~AsmPrinter() {
  for (gcp_iterator I = GCMetadataPrinters.begin(),
                    E = GCMetadataPrinters.end(); I != E; ++I)
    delete I->second;
TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
  return TM.getTargetLowering()->getObjFileLowering();
}

/// SwitchToSection - Switch to the specified section of the executable if we
/// are not already in it!  If "NS" is null, then this causes us to exit the
/// current section and not reenter another one.  This is generally used for
/// asmprinter hacks.
///
/// FIXME: Remove support for null sections.
///
void AsmPrinter::SwitchToSection(const MCSection *NS) {
  // If we're already in this section, we're done.
  NS->PrintSwitchToSection(*TAI, O);
void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
  MachineFunctionPass::getAnalysisUsage(AU);
David Greene's avatar
 
David Greene committed
  if (ExuberantAsm) {
    AU.addRequired<MachineLoopInfo>();
  }
bool AsmPrinter::doInitialization(Module &M) {
  // Initialize TargetLoweringObjectFile.
  const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
    .Initialize(OutContext, TM);
  
  Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
                     TAI->getLinkerPrivateGlobalPrefix());
  if (TAI->doesAllowQuotesInName())
    Mang->setUseQuotes(true);
  
  GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
  assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola's avatar
Rafael Espindola committed

  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);
Chris Lattner's avatar
Chris Lattner committed
  if (!M.getModuleInlineAsm().empty())
    O << TAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner's avatar
Chris Lattner committed
      << M.getModuleInlineAsm()
      << '\n' << TAI->getCommentString()
      << " End of file scope inline assembly\n";
  SwitchToSection(0);   // Reset back to no section to close off sections.
  if (TAI->doesSupportDebugInformation() ||
      TAI->doesSupportExceptionHandling()) {
    MMI = getAnalysisIfAvailable<MachineModuleInfo>();
    if (MMI)
    DW = getAnalysisIfAvailable<DwarfWriter>();
    if (DW)
      DW->BeginModule(&M, MMI, O, this, TAI);
  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 (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
    DW->EndModule();
  
  // If the target wants to know about weak references, print them all.
  if (TAI->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 << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
    for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
      if (I->hasExternalWeakLinkage())
        O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
    for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Loading
Loading full blame...