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"
Chris Lattner
committed
#include "llvm/CodeGen/MachineConstantPool.h"
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Mangler.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Streams.h"
#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson
committed
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetLowering.h"
#include "llvm/Target/TargetMachine.h"
#include <cerrno>
using namespace llvm;
static cl::opt<bool>
AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
const TargetAsmInfo *T)
: MachineFunctionPass((intptr_t)&ID), FunctionNumber(0), O(o), TM(tm), TAI(T)
std::string AsmPrinter::getSectionForFunction(const Function &F) const {
return TAI->getTextSection();
}
/// 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';
}
/// 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';
bool AsmPrinter::doInitialization(Module &M) {
Mang = new Mangler(M, TAI->getGlobalPrefix());
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.
if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
MMI->AnalyzeModule(M);
}
return false;
}
bool AsmPrinter::doFinalization(Module &M) {
if (!ExtWeakSymbols.empty())
SwitchToDataSection("");
for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(),
e = ExtWeakSymbols.end(); i != e; ++i) {
const GlobalValue *GV = *i;
std::string Name = Mang->getValueName(GV);
O << TAI->getWeakRefDirective() << Name << "\n";
}
}
if (TAI->getSetDirective()) {
if (!M.alias_empty())
SwitchToTextSection(TAI->getTextSection());
O << "\n";
for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
I!=E; ++I) {
std::string Name = Mang->getValueName(I);
std::string Target;
Anton Korobeynikov
committed
const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Target = Mang->getValueName(GV);
Anton Korobeynikov
committed
if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
O << "\t.globl\t" << Name << "\n";
else if (I->hasWeakLinkage())
O << TAI->getWeakRefDirective() << Name << "\n";
else if (!I->hasInternalLinkage())
assert(0 && "Invalid alias linkage");
O << TAI->getSetDirective() << Name << ", " << Target << "\n";
Anton Korobeynikov
committed
// If the aliasee has external weak linkage it can be referenced only by
// alias itself. In this case it can be not in ExtWeakSymbols list. Emit
// weak reference in such case.
if (GV->hasExternalWeakLinkage())
if (TAI->getWeakRefDirective())
O << TAI->getWeakRefDirective() << Target << "\n";
else
O << "\t.globl\t" << Target << "\n";
}
}
delete Mang; Mang = 0;
return false;
}
std::string AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) {
assert(MF && "No machine function?");
return Mang->makeNameProper(MF->getFunction()->getName() + ".eh",
TAI->getGlobalPrefix());
void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
// What's my mangled name?
CurrentFnName = Mang->getValueName(MF.getFunction());
Chris Lattner
committed
/// EmitConstantPool - Print to the current output stream assembly
/// representations of the constants in the constant pool MCP. This is
/// used to print out constants which have been "spilled to memory" by
/// the code generator.
///
void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner
committed
if (CP.empty()) return;
// Some targets require 4-, 8-, and 16- byte constant literals to be placed
// in special sections.
std::vector<std::pair<MachineConstantPoolEntry,unsigned> > FourByteCPs;
std::vector<std::pair<MachineConstantPoolEntry,unsigned> > EightByteCPs;
std::vector<std::pair<MachineConstantPoolEntry,unsigned> > SixteenByteCPs;
std::vector<std::pair<MachineConstantPoolEntry,unsigned> > OtherCPs;
std::vector<std::pair<MachineConstantPoolEntry,unsigned> > TargetCPs;
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
MachineConstantPoolEntry CPE = CP[i];
const Type *Ty = CPE.getType();
if (TAI->getFourByteConstantSection() &&
TM.getTargetData()->getABITypeSize(Ty) == 4)
FourByteCPs.push_back(std::make_pair(CPE, i));
else if (TAI->getEightByteConstantSection() &&
TM.getTargetData()->getABITypeSize(Ty) == 8)
Loading
Loading full blame...