2009-06-24 09:03:06 +08:00
|
|
|
//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2009-08-27 08:51:57 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-08-14 11:48:55 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2009-08-31 16:08:38 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-08-27 08:51:57 +08:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2009-06-24 09:03:06 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-08-31 16:08:38 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2009-07-01 14:35:03 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2009-08-11 02:15:01 +08:00
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
2009-06-24 09:03:06 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-19 14:12:02 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2009-08-27 08:51:57 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
2009-06-24 09:03:06 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
class MCAsmStreamer : public MCStreamer {
|
|
|
|
raw_ostream &OS;
|
2009-08-23 05:43:10 +08:00
|
|
|
const MCAsmInfo &MAI;
|
2009-08-17 12:17:34 +08:00
|
|
|
AsmPrinter *Printer;
|
2009-08-27 08:51:57 +08:00
|
|
|
MCCodeEmitter *Emitter;
|
2009-08-17 12:17:34 +08:00
|
|
|
public:
|
2009-08-23 04:48:53 +08:00
|
|
|
MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
|
2009-08-27 08:51:57 +08:00
|
|
|
AsmPrinter *_Printer, MCCodeEmitter *_Emitter)
|
|
|
|
: MCStreamer(Context), OS(_OS), MAI(tai), Printer(_Printer),
|
|
|
|
Emitter(_Emitter) {}
|
2009-08-17 12:17:34 +08:00
|
|
|
~MCAsmStreamer() {}
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
/// @name MCStreamer Interface
|
|
|
|
/// @{
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 13:49:08 +08:00
|
|
|
virtual void SwitchSection(const MCSection *Section);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitLabel(MCSymbol *Symbol);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitAssemblerFlag(AssemblerFlag Flag);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
|
2009-07-14 05:03:15 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
|
2009-08-30 14:17:16 +08:00
|
|
|
unsigned ByteAlignment);
|
2009-07-15 02:17:10 +08:00
|
|
|
|
2009-08-28 13:48:22 +08:00
|
|
|
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
|
2009-08-30 14:17:16 +08:00
|
|
|
unsigned Size = 0, unsigned ByteAlignment = 0);
|
2009-07-15 05:35:03 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitBytes(const StringRef &Data);
|
2009-07-08 04:30:46 +08:00
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
virtual void EmitValue(const MCExpr *Value, unsigned Size);
|
2009-07-11 06:20:30 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
|
|
|
|
unsigned ValueSize = 1,
|
|
|
|
unsigned MaxBytesToEmit = 0);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
virtual void EmitValueToOffset(const MCExpr *Offset,
|
2009-08-17 12:17:34 +08:00
|
|
|
unsigned char Value = 0);
|
|
|
|
|
|
|
|
virtual void EmitInstruction(const MCInst &Inst);
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
virtual void Finish();
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
};
|
2009-06-25 03:25:34 +08:00
|
|
|
|
2009-08-17 12:17:34 +08:00
|
|
|
} // end anonymous namespace.
|
2009-06-24 09:03:06 +08:00
|
|
|
|
2009-08-01 07:04:32 +08:00
|
|
|
/// Allow printing symbols directly to a raw_ostream with proper quoting.
|
|
|
|
static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
|
2009-08-14 11:48:55 +08:00
|
|
|
S->print(os);
|
|
|
|
return os;
|
2009-08-01 07:04:32 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:03:06 +08:00
|
|
|
/// Allow printing values directly to a raw_ostream.
|
2009-08-31 16:09:28 +08:00
|
|
|
static inline raw_ostream &operator<<(raw_ostream &os, const MCExpr &Value) {
|
2009-08-14 11:48:55 +08:00
|
|
|
Value.print(os);
|
2009-06-24 09:03:06 +08:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2009-06-26 05:03:18 +08:00
|
|
|
static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
|
|
|
|
assert(Bytes && "Invalid size!");
|
|
|
|
return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
|
|
|
|
}
|
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
static inline const MCExpr *truncateToSize(const MCExpr *Value,
|
|
|
|
unsigned Bytes) {
|
|
|
|
// FIXME: Do we really need this routine?
|
|
|
|
return Value;
|
2009-06-26 05:03:18 +08:00
|
|
|
}
|
|
|
|
|
2009-08-17 13:49:08 +08:00
|
|
|
void MCAsmStreamer::SwitchSection(const MCSection *Section) {
|
2009-08-19 13:49:37 +08:00
|
|
|
assert(Section && "Cannot switch to a null section!");
|
2009-06-24 09:03:06 +08:00
|
|
|
if (Section != CurSection) {
|
|
|
|
CurSection = Section;
|
2009-08-23 05:43:10 +08:00
|
|
|
Section->PrintSwitchToSection(MAI, OS);
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
|
2009-08-22 15:22:36 +08:00
|
|
|
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
2009-06-25 01:00:42 +08:00
|
|
|
assert(CurSection && "Cannot emit before setting section!");
|
|
|
|
|
2009-08-01 07:04:32 +08:00
|
|
|
OS << Symbol << ":\n";
|
2009-08-22 15:22:36 +08:00
|
|
|
Symbol->setSection(*CurSection);
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-17 01:56:39 +08:00
|
|
|
void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
|
|
|
|
switch (Flag) {
|
2009-08-14 07:36:34 +08:00
|
|
|
default: assert(0 && "Invalid flag!");
|
2009-07-17 01:56:39 +08:00
|
|
|
case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
|
|
|
|
}
|
|
|
|
OS << '\n';
|
2009-07-14 05:03:15 +08:00
|
|
|
}
|
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
|
2009-08-22 15:22:36 +08:00
|
|
|
// Only absolute symbols can be redefined.
|
|
|
|
assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
|
|
|
|
"Cannot define a symbol twice!");
|
2009-06-25 01:00:42 +08:00
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
OS << Symbol << " = " << *Value << '\n';
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
|
|
|
|
SymbolAttr Attribute) {
|
|
|
|
switch (Attribute) {
|
|
|
|
case Global: OS << ".globl"; break;
|
2009-06-25 00:36:52 +08:00
|
|
|
case Hidden: OS << ".hidden"; break;
|
|
|
|
case IndirectSymbol: OS << ".indirect_symbol"; break;
|
|
|
|
case Internal: OS << ".internal"; break;
|
|
|
|
case LazyReference: OS << ".lazy_reference"; break;
|
|
|
|
case NoDeadStrip: OS << ".no_dead_strip"; break;
|
2009-06-24 09:03:06 +08:00
|
|
|
case PrivateExtern: OS << ".private_extern"; break;
|
2009-06-25 00:36:52 +08:00
|
|
|
case Protected: OS << ".protected"; break;
|
|
|
|
case Reference: OS << ".reference"; break;
|
|
|
|
case Weak: OS << ".weak"; break;
|
|
|
|
case WeakDefinition: OS << ".weak_definition"; break;
|
|
|
|
case WeakReference: OS << ".weak_reference"; break;
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
2009-08-01 07:04:32 +08:00
|
|
|
OS << ' ' << Symbol << '\n';
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
2009-07-15 02:17:10 +08:00
|
|
|
void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
2009-08-01 07:04:32 +08:00
|
|
|
OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
|
2009-07-15 02:17:10 +08:00
|
|
|
}
|
|
|
|
|
2009-07-08 04:30:46 +08:00
|
|
|
void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
|
2009-08-30 14:17:16 +08:00
|
|
|
unsigned ByteAlignment) {
|
2009-08-28 13:48:46 +08:00
|
|
|
OS << ".comm";
|
2009-08-01 07:04:32 +08:00
|
|
|
OS << ' ' << Symbol << ',' << Size;
|
2009-08-30 14:17:16 +08:00
|
|
|
if (ByteAlignment != 0)
|
|
|
|
OS << ',' << Log2_32(ByteAlignment);
|
2009-07-08 04:30:46 +08:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-08-28 13:48:22 +08:00
|
|
|
void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
2009-08-30 14:17:16 +08:00
|
|
|
unsigned Size, unsigned ByteAlignment) {
|
2009-08-14 07:36:34 +08:00
|
|
|
// Note: a .zerofill directive does not switch sections.
|
2009-08-09 07:39:42 +08:00
|
|
|
OS << ".zerofill ";
|
|
|
|
|
|
|
|
// This is a mach-o specific directive.
|
2009-08-10 09:39:42 +08:00
|
|
|
const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
|
2009-08-15 02:51:45 +08:00
|
|
|
OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
|
2009-08-09 07:39:42 +08:00
|
|
|
|
2009-07-11 06:20:30 +08:00
|
|
|
if (Symbol != NULL) {
|
2009-08-01 07:04:32 +08:00
|
|
|
OS << ',' << Symbol << ',' << Size;
|
2009-08-30 14:17:16 +08:00
|
|
|
if (ByteAlignment != 0)
|
|
|
|
OS << ',' << Log2_32(ByteAlignment);
|
2009-07-11 06:20:30 +08:00
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-07-28 05:49:56 +08:00
|
|
|
void MCAsmStreamer::EmitBytes(const StringRef &Data) {
|
2009-06-25 01:00:42 +08:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2009-07-28 05:49:56 +08:00
|
|
|
for (unsigned i = 0, e = Data.size(); i != e; ++i)
|
2009-08-15 03:59:24 +08:00
|
|
|
OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
|
2009-06-25 01:00:42 +08:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2009-06-24 09:03:06 +08:00
|
|
|
// Need target hooks to know how to print this.
|
|
|
|
switch (Size) {
|
|
|
|
default:
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("Invalid size for machine code value!");
|
2009-06-24 09:03:06 +08:00
|
|
|
case 1: OS << ".byte"; break;
|
2009-06-25 00:05:35 +08:00
|
|
|
case 2: OS << ".short"; break;
|
2009-06-24 09:03:06 +08:00
|
|
|
case 4: OS << ".long"; break;
|
|
|
|
case 8: OS << ".quad"; break;
|
|
|
|
}
|
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
OS << ' ' << *truncateToSize(Value, Size) << '\n';
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
2009-06-25 03:25:34 +08:00
|
|
|
void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
|
|
|
|
unsigned ValueSize,
|
|
|
|
unsigned MaxBytesToEmit) {
|
2009-08-19 14:12:02 +08:00
|
|
|
// Some assemblers don't support non-power of two alignments, so we always
|
|
|
|
// emit alignments as a power of two if possible.
|
|
|
|
if (isPowerOf2_32(ByteAlignment)) {
|
2009-08-19 14:35:36 +08:00
|
|
|
switch (ValueSize) {
|
|
|
|
default: llvm_unreachable("Invalid size for machine code value!");
|
2009-08-23 05:43:10 +08:00
|
|
|
case 1: OS << MAI.getAlignDirective(); break;
|
|
|
|
// FIXME: use MAI for this!
|
2009-08-19 14:35:36 +08:00
|
|
|
case 2: OS << ".p2alignw "; break;
|
|
|
|
case 4: OS << ".p2alignl "; break;
|
|
|
|
case 8: llvm_unreachable("Unsupported alignment size!");
|
|
|
|
}
|
2009-08-19 14:12:02 +08:00
|
|
|
|
2009-08-23 05:43:10 +08:00
|
|
|
if (MAI.getAlignmentIsInBytes())
|
2009-08-19 14:12:02 +08:00
|
|
|
OS << ByteAlignment;
|
|
|
|
else
|
|
|
|
OS << Log2_32(ByteAlignment);
|
|
|
|
|
|
|
|
if (Value || MaxBytesToEmit) {
|
2009-09-03 12:01:10 +08:00
|
|
|
OS << ", 0x";
|
|
|
|
OS.write_hex(truncateToSize(Value, ValueSize));
|
2009-06-25 03:25:34 +08:00
|
|
|
|
2009-08-19 14:12:02 +08:00
|
|
|
if (MaxBytesToEmit)
|
|
|
|
OS << ", " << MaxBytesToEmit;
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-power of two alignment. This is not widely supported by assemblers.
|
2009-08-23 05:43:10 +08:00
|
|
|
// FIXME: Parameterize this based on MAI.
|
2009-06-25 03:25:34 +08:00
|
|
|
switch (ValueSize) {
|
2009-08-19 14:12:02 +08:00
|
|
|
default: llvm_unreachable("Invalid size for machine code value!");
|
|
|
|
case 1: OS << ".balign"; break;
|
|
|
|
case 2: OS << ".balignw"; break;
|
|
|
|
case 4: OS << ".balignl"; break;
|
|
|
|
case 8: llvm_unreachable("Unsupported alignment size!");
|
2009-06-25 03:25:34 +08:00
|
|
|
}
|
|
|
|
|
2009-08-19 14:12:02 +08:00
|
|
|
OS << ' ' << ByteAlignment;
|
2009-06-26 05:03:18 +08:00
|
|
|
OS << ", " << truncateToSize(Value, ValueSize);
|
2009-06-25 03:25:34 +08:00
|
|
|
if (MaxBytesToEmit)
|
|
|
|
OS << ", " << MaxBytesToEmit;
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-08-31 16:09:28 +08:00
|
|
|
void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
|
2009-06-25 03:25:34 +08:00
|
|
|
unsigned char Value) {
|
|
|
|
// FIXME: Verify that Offset is associated with the current section.
|
2009-08-31 16:09:28 +08:00
|
|
|
OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
|
2009-06-25 03:25:34 +08:00
|
|
|
}
|
|
|
|
|
2009-06-24 09:03:06 +08:00
|
|
|
void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
|
2009-06-25 01:00:42 +08:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2009-08-14 11:48:55 +08:00
|
|
|
|
|
|
|
// If we have an AsmPrinter, use that to print.
|
|
|
|
if (Printer) {
|
|
|
|
Printer->printMCInst(&Inst);
|
2009-08-27 15:58:57 +08:00
|
|
|
|
|
|
|
// Show the encoding if we have a code emitter.
|
|
|
|
if (Emitter) {
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
|
|
|
Emitter->EncodeInstruction(Inst, VecOS);
|
|
|
|
VecOS.flush();
|
|
|
|
|
|
|
|
OS.indent(20);
|
|
|
|
OS << " # encoding: [";
|
|
|
|
for (unsigned i = 0, e = Code.size(); i != e; ++i) {
|
|
|
|
if (i)
|
|
|
|
OS << ',';
|
|
|
|
OS << format("%#04x", uint8_t(Code[i]));
|
|
|
|
}
|
|
|
|
OS << "]\n";
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:48:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise fall back to a structural printing for now. Eventually we should
|
|
|
|
// always have access to the target specific printer.
|
2009-08-31 16:08:38 +08:00
|
|
|
Inst.print(OS);
|
|
|
|
OS << '\n';
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::Finish() {
|
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
2009-08-14 11:48:55 +08:00
|
|
|
MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
|
2009-08-27 08:51:57 +08:00
|
|
|
const MCAsmInfo &MAI, AsmPrinter *AP,
|
|
|
|
MCCodeEmitter *CE) {
|
|
|
|
return new MCAsmStreamer(Context, OS, MAI, AP, CE);
|
2009-06-24 09:03:06 +08:00
|
|
|
}
|