mcstreamerize .file and .file. This also fixes an issue where the

normal form of .file would fail if the filename had a weird character
in it.

llvm-svn: 94437
This commit is contained in:
Chris Lattner 2010-01-25 18:58:59 +00:00
parent bc696445e1
commit 601ef33c77
8 changed files with 87 additions and 82 deletions

View File

@ -265,9 +265,6 @@ namespace llvm {
///
void EmitInt64(uint64_t Value) const;
/// EmitFile - Emit a .file directive.
void EmitFile(unsigned Number, StringRef Name) const;
//===------------------------------------------------------------------===//
/// EmitAlignment - Emit an alignment directive to the specified power of

View File

@ -234,6 +234,15 @@ namespace llvm {
/// @}
/// EmitFileDirective - Switch to a new logical file. This is used to
/// implement the '.file "foo.c"' assembler directive.
virtual void EmitFileDirective(StringRef Filename) = 0;
/// EmitDwarfFileDirective - Associate a filename with a specified logical
/// file number. This implements the DWARF2 '.file 4 "foo.c"' assembler
/// directive.
virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) = 0;
/// EmitInstruction - Emit the given @param Instruction into the current
/// section.
virtual void EmitInstruction(const MCInst &Inst) = 0;

View File

@ -115,11 +115,11 @@ bool AsmPrinter::doInitialization(Module &M) {
// Allow the target to emit any magic that it wants at the start of the file.
EmitStartOfAsmFile(M);
// 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 global came from.
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";
// .file "foo.c"
OutStreamer.EmitFileDirective(M.getModuleIdentifier());
}
GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
@ -236,6 +236,9 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
} else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
// .globl _foo
OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
// FIXME: linkonce should be a section attribute, handled by COFF Section
// assignment.
// http://sourceware.org/binutils/docs-2.20/as/Linkonce.html#Linkonce
// .linkonce same_size
O << LinkOnce;
} else {
@ -679,48 +682,6 @@ void AsmPrinter::EmitInt64(uint64_t Value) const {
OutStreamer.EmitIntValue(Value, 8, 0/*addrspace*/);
}
/// toOctal - Convert the low order bits of X into an octal digit.
///
static inline char toOctal(int X) {
return (X&7)+'0';
}
/// printStringChar - Print a char, escaped if necessary.
///
static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
if (C == '"') {
O << "\\\"";
} else if (C == '\\') {
O << "\\\\";
} else if (isprint((unsigned char)C)) {
O << C;
} else {
switch(C) {
case '\b': O << "\\b"; break;
case '\f': O << "\\f"; break;
case '\n': O << "\\n"; break;
case '\r': O << "\\r"; break;
case '\t': O << "\\t"; break;
default:
O << '\\';
O << toOctal(C >> 6);
O << toOctal(C >> 3);
O << toOctal(C >> 0);
break;
}
}
}
/// EmitFile - Emit a .file directive.
void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
O << "\t.file\t" << Number << " \"";
for (unsigned i = 0, N = Name.size(); i < N; ++i)
printStringChar(O, Name[i]);
O << '\"';
}
//===----------------------------------------------------------------------===//
// EmitAlignment - Emit an alignment directive to the specified power of

View File

@ -1779,8 +1779,7 @@ void DwarfDebug::beginModule(Module *M, MachineModuleInfo *mmi) {
FullPath.appendComponent(getSourceFileName(Id.second));
assert(AppendOk && "Could not append filename to directory!");
AppendOk = false;
Asm->EmitFile(i, FullPath.str());
Asm->O << '\n';
Asm->OutStreamer.EmitDwarfFileDirective(i, FullPath.str());
}
}

View File

@ -121,6 +121,9 @@ public:
virtual void EmitValueToOffset(const MCExpr *Offset,
unsigned char Value = 0);
virtual void EmitFileDirective(StringRef Filename);
virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
virtual void EmitInstruction(const MCInst &Inst);
virtual void Finish();
@ -320,27 +323,9 @@ void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
static inline char toOctal(int X) { return (X&7)+'0'; }
void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
assert(CurSection && "Cannot emit contents before setting section!");
if (Data.empty()) return;
static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
OS << '"';
if (Data.size() == 1) {
OS << MAI.getData8bitsDirective(AddrSpace);
OS << (unsigned)(unsigned char)Data[0];
EmitEOL();
return;
}
// If the data ends with 0 and the target supports .asciz, use it, otherwise
// use .ascii
if (MAI.getAscizDirective() && Data.back() == 0) {
OS << MAI.getAscizDirective();
Data = Data.substr(0, Data.size()-1);
} else {
OS << MAI.getAsciiDirective();
}
OS << " \"";
for (unsigned i = 0, e = Data.size(); i != e; ++i) {
unsigned char C = Data[i];
if (C == '"' || C == '\\') {
@ -367,7 +352,33 @@ void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
break;
}
}
OS << '"';
}
void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
assert(CurSection && "Cannot emit contents before setting section!");
if (Data.empty()) return;
if (Data.size() == 1) {
OS << MAI.getData8bitsDirective(AddrSpace);
OS << (unsigned)(unsigned char)Data[0];
EmitEOL();
return;
}
// If the data ends with 0 and the target supports .asciz, use it, otherwise
// use .ascii
if (MAI.getAscizDirective() && Data.back() == 0) {
OS << MAI.getAscizDirective();
Data = Data.substr(0, Data.size()-1);
} else {
OS << MAI.getAsciiDirective();
}
OS << ' ';
PrintQuotedString(Data, OS);
EmitEOL();
}
@ -492,6 +503,21 @@ void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
EmitEOL();
}
void MCAsmStreamer::EmitFileDirective(StringRef Filename) {
assert(MAI.hasSingleParameterDotFile());
OS << "\t.file\t";
PrintQuotedString(Filename, OS);
EmitEOL();
}
void MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo, StringRef Filename){
OS << "\t.file\t" << FileNo << ' ';
PrintQuotedString(Filename, OS);
EmitEOL();
}
void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
assert(CurSection && "Cannot emit contents before setting section!");

View File

@ -139,6 +139,14 @@ public:
unsigned MaxBytesToEmit = 0);
virtual void EmitValueToOffset(const MCExpr *Offset,
unsigned char Value = 0);
virtual void EmitFileDirective(StringRef Filename) {
errs() << "FIXME: MCMachoStreamer:EmitFileDirective not implemented\n";
}
virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
errs() << "FIXME: MCMachoStreamer:EmitDwarfFileDirective not implemented\n";
}
virtual void EmitInstruction(const MCInst &Inst);
virtual void Finish();

View File

@ -58,6 +58,8 @@ namespace {
virtual void EmitValueToOffset(const MCExpr *Offset,
unsigned char Value = 0) {}
virtual void EmitFileDirective(StringRef Filename) {}
virtual void EmitDwarfFileDirective(unsigned FileNo,StringRef Filename) {}
virtual void EmitInstruction(const MCInst &Inst) {}
virtual void Finish() {}

View File

@ -96,6 +96,9 @@ void X86AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
O << MAI->getWeakDefDirective() << *CurrentFnSym << '\n';
} else if (Subtarget->isTargetCygMing()) {
OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_Global);
// FIXME: linkonce should be a section attribute, handled by COFF Section
// assignment.
// http://sourceware.org/binutils/docs-2.20/as/Linkonce.html#Linkonce
O << "\t.linkonce discard\n";
} else {
O << "\t.weak\t" << *CurrentFnSym << '\n';
@ -105,7 +108,7 @@ void X86AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
printVisibility(CurrentFnSym, F->getVisibility());
if (Subtarget->isTargetELF()) {
if (MAI->hasDotTypeDotSizeDirective()) {
OutStreamer.EmitSymbolAttribute(CurrentFnSym, MCSA_ELF_TypeFunction);
} else if (Subtarget->isTargetCygMing()) {
O << "\t.def\t " << *CurrentFnSym;