2009-06-24 01:03:06 +00: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 00:51:57 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-08-31 08:08:38 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2009-08-27 00:51:57 +00:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2009-06-24 01:03:06 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-08-31 08:08:38 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2009-07-01 06:35:03 +00:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2009-09-14 03:02:37 +00:00
|
|
|
#include "llvm/MC/MCInstPrinter.h"
|
2009-08-10 18:15:01 +00:00
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
2009-06-24 01:03:06 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-19 06:12:02 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2009-08-27 00:51:57 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2009-06-24 01:03:06 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
class MCAsmStreamer : public MCStreamer {
|
|
|
|
raw_ostream &OS;
|
2009-08-22 21:43:10 +00:00
|
|
|
const MCAsmInfo &MAI;
|
2009-09-14 03:02:37 +00:00
|
|
|
MCInstPrinter *InstPrinter;
|
2009-08-27 00:51:57 +00:00
|
|
|
MCCodeEmitter *Emitter;
|
2009-08-17 04:17:34 +00:00
|
|
|
public:
|
2009-08-22 20:48:53 +00:00
|
|
|
MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
|
2009-09-14 03:02:37 +00:00
|
|
|
MCInstPrinter *_Printer, MCCodeEmitter *_Emitter)
|
|
|
|
: MCStreamer(Context), OS(_OS), MAI(tai), InstPrinter(_Printer),
|
2009-08-27 00:51:57 +00:00
|
|
|
Emitter(_Emitter) {}
|
2009-08-17 04:17:34 +00:00
|
|
|
~MCAsmStreamer() {}
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
/// @name MCStreamer Interface
|
|
|
|
/// @{
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 05:49:08 +00:00
|
|
|
virtual void SwitchSection(const MCSection *Section);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void EmitLabel(MCSymbol *Symbol);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void EmitAssemblerFlag(AssemblerFlag Flag);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-31 08:09:28 +00:00
|
|
|
virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
|
2009-07-13 21:03:15 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
|
2009-08-30 06:17:16 +00:00
|
|
|
unsigned ByteAlignment);
|
2009-07-14 18:17:10 +00:00
|
|
|
|
2009-08-28 05:48:22 +00:00
|
|
|
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
|
2009-08-30 06:17:16 +00:00
|
|
|
unsigned Size = 0, unsigned ByteAlignment = 0);
|
2009-07-14 21:35:03 +00:00
|
|
|
|
2009-11-06 10:58:06 +00:00
|
|
|
virtual void EmitBytes(StringRef Data);
|
2009-07-07 20:30:46 +00:00
|
|
|
|
2009-08-31 08:09:28 +00:00
|
|
|
virtual void EmitValue(const MCExpr *Value, unsigned Size);
|
2009-07-10 22:20:30 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
|
|
|
|
unsigned ValueSize = 1,
|
|
|
|
unsigned MaxBytesToEmit = 0);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-31 08:09:28 +00:00
|
|
|
virtual void EmitValueToOffset(const MCExpr *Offset,
|
2009-08-17 04:17:34 +00:00
|
|
|
unsigned char Value = 0);
|
|
|
|
|
|
|
|
virtual void EmitInstruction(const MCInst &Inst);
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
virtual void Finish();
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
};
|
2009-06-24 19:25:34 +00:00
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
} // end anonymous namespace.
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2009-06-25 21:03:18 +00: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 08:09:28 +00:00
|
|
|
static inline const MCExpr *truncateToSize(const MCExpr *Value,
|
|
|
|
unsigned Bytes) {
|
|
|
|
// FIXME: Do we really need this routine?
|
|
|
|
return Value;
|
2009-06-25 21:03:18 +00:00
|
|
|
}
|
|
|
|
|
2009-08-17 05:49:08 +00:00
|
|
|
void MCAsmStreamer::SwitchSection(const MCSection *Section) {
|
2009-08-19 05:49:37 +00:00
|
|
|
assert(Section && "Cannot switch to a null section!");
|
2009-06-24 01:03:06 +00:00
|
|
|
if (Section != CurSection) {
|
|
|
|
CurSection = Section;
|
2009-08-22 21:43:10 +00:00
|
|
|
Section->PrintSwitchToSection(MAI, OS);
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
|
2009-08-22 07:22:36 +00:00
|
|
|
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
2009-06-24 17:00:42 +00:00
|
|
|
assert(CurSection && "Cannot emit before setting section!");
|
|
|
|
|
2009-09-03 05:46:51 +00:00
|
|
|
Symbol->print(OS, &MAI);
|
|
|
|
OS << ":\n";
|
2009-08-22 07:22:36 +00:00
|
|
|
Symbol->setSection(*CurSection);
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-16 17:56:39 +00:00
|
|
|
void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
|
|
|
|
switch (Flag) {
|
2009-08-13 23:36:34 +00:00
|
|
|
default: assert(0 && "Invalid flag!");
|
2009-07-16 17:56:39 +00:00
|
|
|
case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
|
|
|
|
}
|
|
|
|
OS << '\n';
|
2009-07-13 21:03:15 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 08:09:28 +00:00
|
|
|
void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
|
2009-08-22 07:22:36 +00:00
|
|
|
// Only absolute symbols can be redefined.
|
|
|
|
assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
|
|
|
|
"Cannot define a symbol twice!");
|
2009-06-24 17:00:42 +00:00
|
|
|
|
2009-09-03 05:46:51 +00:00
|
|
|
Symbol->print(OS, &MAI);
|
|
|
|
OS << " = ";
|
|
|
|
Value->print(OS, &MAI);
|
2009-09-03 05:33:01 +00:00
|
|
|
OS << '\n';
|
2009-10-16 01:34:54 +00:00
|
|
|
|
|
|
|
// FIXME: Lift context changes into super class.
|
2009-10-16 01:57:39 +00:00
|
|
|
// FIXME: Set associated section.
|
2009-10-16 01:34:54 +00:00
|
|
|
Symbol->setValue(Value);
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-10-16 01:34:54 +00:00
|
|
|
void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
|
2009-06-24 01:03:06 +00:00
|
|
|
SymbolAttr Attribute) {
|
|
|
|
switch (Attribute) {
|
2009-10-16 01:58:15 +00:00
|
|
|
case Global: OS << ".globl"; break;
|
|
|
|
case Hidden: OS << ".hidden"; break;
|
2009-06-24 16:36:52 +00:00
|
|
|
case IndirectSymbol: OS << ".indirect_symbol"; break;
|
2009-10-16 01:58:15 +00:00
|
|
|
case Internal: OS << ".internal"; break;
|
|
|
|
case LazyReference: OS << ".lazy_reference"; break;
|
|
|
|
case NoDeadStrip: OS << ".no_dead_strip"; break;
|
|
|
|
case PrivateExtern: OS << ".private_extern"; break;
|
|
|
|
case Protected: OS << ".protected"; break;
|
|
|
|
case Reference: OS << ".reference"; break;
|
|
|
|
case Weak: OS << ".weak"; break;
|
2009-06-24 16:36:52 +00:00
|
|
|
case WeakDefinition: OS << ".weak_definition"; break;
|
2009-10-16 01:58:15 +00:00
|
|
|
case WeakReference: OS << ".weak_reference"; break;
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-09-03 05:46:51 +00:00
|
|
|
OS << ' ';
|
|
|
|
Symbol->print(OS, &MAI);
|
|
|
|
OS << '\n';
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 18:17:10 +00:00
|
|
|
void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
2009-09-03 05:46:51 +00:00
|
|
|
OS << ".desc" << ' ';
|
|
|
|
Symbol->print(OS, &MAI);
|
|
|
|
OS << ',' << DescValue << '\n';
|
2009-07-14 18:17:10 +00:00
|
|
|
}
|
|
|
|
|
2009-07-07 20:30:46 +00:00
|
|
|
void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
|
2009-08-30 06:17:16 +00:00
|
|
|
unsigned ByteAlignment) {
|
2009-09-03 05:46:51 +00:00
|
|
|
OS << ".comm ";
|
|
|
|
Symbol->print(OS, &MAI);
|
|
|
|
OS << ',' << Size;
|
2009-08-30 06:17:16 +00:00
|
|
|
if (ByteAlignment != 0)
|
|
|
|
OS << ',' << Log2_32(ByteAlignment);
|
2009-07-07 20:30:46 +00:00
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-08-28 05:48:22 +00:00
|
|
|
void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
2009-08-30 06:17:16 +00:00
|
|
|
unsigned Size, unsigned ByteAlignment) {
|
2009-08-13 23:36:34 +00:00
|
|
|
// Note: a .zerofill directive does not switch sections.
|
2009-08-08 23:39:42 +00:00
|
|
|
OS << ".zerofill ";
|
|
|
|
|
|
|
|
// This is a mach-o specific directive.
|
2009-08-10 01:39:42 +00:00
|
|
|
const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
|
2009-08-14 18:51:45 +00:00
|
|
|
OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
|
2009-08-08 23:39:42 +00:00
|
|
|
|
2009-07-10 22:20:30 +00:00
|
|
|
if (Symbol != NULL) {
|
2009-09-03 05:46:51 +00:00
|
|
|
OS << ',';
|
|
|
|
Symbol->print(OS, &MAI);
|
|
|
|
OS << ',' << Size;
|
2009-08-30 06:17:16 +00:00
|
|
|
if (ByteAlignment != 0)
|
|
|
|
OS << ',' << Log2_32(ByteAlignment);
|
2009-07-10 22:20:30 +00:00
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-11-06 10:58:06 +00:00
|
|
|
void MCAsmStreamer::EmitBytes(StringRef Data) {
|
2009-06-24 17:00:42 +00:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2009-07-27 21:49:56 +00:00
|
|
|
for (unsigned i = 0, e = Data.size(); i != e; ++i)
|
2009-08-14 19:59:24 +00:00
|
|
|
OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 08:09:28 +00:00
|
|
|
void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
|
2009-06-24 17:00:42 +00:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2009-06-24 01:03:06 +00:00
|
|
|
// Need target hooks to know how to print this.
|
|
|
|
switch (Size) {
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Invalid size for machine code value!");
|
2009-06-24 01:03:06 +00:00
|
|
|
case 1: OS << ".byte"; break;
|
2009-06-24 16:05:35 +00:00
|
|
|
case 2: OS << ".short"; break;
|
2009-06-24 01:03:06 +00:00
|
|
|
case 4: OS << ".long"; break;
|
|
|
|
case 8: OS << ".quad"; break;
|
|
|
|
}
|
|
|
|
|
2009-09-03 05:33:01 +00:00
|
|
|
OS << ' ';
|
2009-09-03 05:46:51 +00:00
|
|
|
truncateToSize(Value, Size)->print(OS, &MAI);
|
2009-09-03 05:33:01 +00:00
|
|
|
OS << '\n';
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 19:25:34 +00:00
|
|
|
void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
|
|
|
|
unsigned ValueSize,
|
|
|
|
unsigned MaxBytesToEmit) {
|
2009-08-19 06:12:02 +00: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 06:35:36 +00:00
|
|
|
switch (ValueSize) {
|
|
|
|
default: llvm_unreachable("Invalid size for machine code value!");
|
2009-08-22 21:43:10 +00:00
|
|
|
case 1: OS << MAI.getAlignDirective(); break;
|
|
|
|
// FIXME: use MAI for this!
|
2009-08-19 06:35:36 +00:00
|
|
|
case 2: OS << ".p2alignw "; break;
|
|
|
|
case 4: OS << ".p2alignl "; break;
|
|
|
|
case 8: llvm_unreachable("Unsupported alignment size!");
|
|
|
|
}
|
2009-08-19 06:12:02 +00:00
|
|
|
|
2009-08-22 21:43:10 +00:00
|
|
|
if (MAI.getAlignmentIsInBytes())
|
2009-08-19 06:12:02 +00:00
|
|
|
OS << ByteAlignment;
|
|
|
|
else
|
|
|
|
OS << Log2_32(ByteAlignment);
|
|
|
|
|
|
|
|
if (Value || MaxBytesToEmit) {
|
2009-09-03 04:01:10 +00:00
|
|
|
OS << ", 0x";
|
|
|
|
OS.write_hex(truncateToSize(Value, ValueSize));
|
2009-06-24 19:25:34 +00:00
|
|
|
|
2009-08-19 06:12:02 +00:00
|
|
|
if (MaxBytesToEmit)
|
|
|
|
OS << ", " << MaxBytesToEmit;
|
|
|
|
}
|
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-power of two alignment. This is not widely supported by assemblers.
|
2009-08-22 21:43:10 +00:00
|
|
|
// FIXME: Parameterize this based on MAI.
|
2009-06-24 19:25:34 +00:00
|
|
|
switch (ValueSize) {
|
2009-08-19 06:12:02 +00: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-24 19:25:34 +00:00
|
|
|
}
|
|
|
|
|
2009-08-19 06:12:02 +00:00
|
|
|
OS << ' ' << ByteAlignment;
|
2009-06-25 21:03:18 +00:00
|
|
|
OS << ", " << truncateToSize(Value, ValueSize);
|
2009-06-24 19:25:34 +00:00
|
|
|
if (MaxBytesToEmit)
|
|
|
|
OS << ", " << MaxBytesToEmit;
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2009-08-31 08:09:28 +00:00
|
|
|
void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
|
2009-06-24 19:25:34 +00:00
|
|
|
unsigned char Value) {
|
|
|
|
// FIXME: Verify that Offset is associated with the current section.
|
2009-09-03 05:33:01 +00:00
|
|
|
OS << ".org ";
|
2009-09-03 05:46:51 +00:00
|
|
|
Offset->print(OS, &MAI);
|
2009-09-03 05:33:01 +00:00
|
|
|
OS << ", " << (unsigned) Value << '\n';
|
2009-06-24 19:25:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 01:03:06 +00:00
|
|
|
void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
|
2009-06-24 17:00:42 +00:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2009-08-14 03:48:55 +00:00
|
|
|
|
|
|
|
// If we have an AsmPrinter, use that to print.
|
2009-09-14 03:02:37 +00:00
|
|
|
if (InstPrinter) {
|
|
|
|
InstPrinter->printInst(&Inst);
|
2009-09-13 22:24:34 +00:00
|
|
|
OS << '\n';
|
2009-08-27 07:58:57 +00: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 03:48:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise fall back to a structural printing for now. Eventually we should
|
|
|
|
// always have access to the target specific printer.
|
2009-09-03 05:46:51 +00:00
|
|
|
Inst.print(OS, &MAI);
|
2009-08-31 08:08:38 +00:00
|
|
|
OS << '\n';
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::Finish() {
|
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
2009-08-14 03:48:55 +00:00
|
|
|
MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
|
2009-09-14 03:02:37 +00:00
|
|
|
const MCAsmInfo &MAI, MCInstPrinter *IP,
|
2009-08-27 00:51:57 +00:00
|
|
|
MCCodeEmitter *CE) {
|
2009-09-14 03:02:37 +00:00
|
|
|
return new MCAsmStreamer(Context, OS, MAI, IP, CE);
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|