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-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"
|
2010-01-22 07:29:22 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.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"
|
2010-01-22 07:29:22 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2009-06-24 01:03:06 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-08-17 04:17:34 +00:00
|
|
|
class MCAsmStreamer : public MCStreamer {
|
2010-01-22 07:29:22 +00:00
|
|
|
formatted_raw_ostream &OS;
|
2009-08-22 21:43:10 +00:00
|
|
|
const MCAsmInfo &MAI;
|
2010-01-22 07:06:15 +00:00
|
|
|
bool IsLittleEndian, IsVerboseAsm;
|
2009-09-14 03:02:37 +00:00
|
|
|
MCInstPrinter *InstPrinter;
|
2009-08-27 00:51:57 +00:00
|
|
|
MCCodeEmitter *Emitter;
|
2010-01-22 07:29:22 +00:00
|
|
|
|
|
|
|
SmallString<128> CommentToEmit;
|
2010-01-22 19:17:48 +00:00
|
|
|
raw_svector_ostream CommentStream;
|
2009-08-17 04:17:34 +00:00
|
|
|
public:
|
2010-01-22 07:29:22 +00:00
|
|
|
MCAsmStreamer(MCContext &Context, formatted_raw_ostream &os,
|
|
|
|
const MCAsmInfo &mai,
|
2010-01-22 07:06:15 +00:00
|
|
|
bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
|
|
|
|
MCCodeEmitter *emitter)
|
|
|
|
: MCStreamer(Context), OS(os), MAI(mai), IsLittleEndian(isLittleEndian),
|
2010-01-22 19:17:48 +00:00
|
|
|
IsVerboseAsm(isVerboseAsm), InstPrinter(printer), Emitter(emitter),
|
|
|
|
CommentStream(CommentToEmit) {}
|
2009-08-17 04:17:34 +00:00
|
|
|
~MCAsmStreamer() {}
|
2009-06-24 01:03:06 +00:00
|
|
|
|
2010-01-20 06:39:07 +00:00
|
|
|
bool isLittleEndian() const { return IsLittleEndian; }
|
|
|
|
|
2010-01-22 07:29:22 +00:00
|
|
|
|
|
|
|
inline void EmitEOL() {
|
2010-01-22 19:17:48 +00:00
|
|
|
// If we don't have any comments, just emit a \n.
|
|
|
|
if (!IsVerboseAsm) {
|
2010-01-22 07:29:22 +00:00
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
EmitCommentsAndEOL();
|
|
|
|
}
|
|
|
|
void EmitCommentsAndEOL();
|
|
|
|
|
2010-01-22 18:21:35 +00:00
|
|
|
/// AddComment - Add a comment that can be emitted to the generated .s
|
2010-01-22 07:29:22 +00:00
|
|
|
/// file if applicable as a QoI issue to make the output of the compiler
|
|
|
|
/// more readable. This only affects the MCAsmStreamer, and only when
|
|
|
|
/// verbose assembly output is enabled.
|
2010-01-22 18:21:35 +00:00
|
|
|
virtual void AddComment(const Twine &T);
|
2010-01-22 07:29:22 +00:00
|
|
|
|
2010-01-22 19:17:48 +00:00
|
|
|
/// GetCommentOS - Return a raw_ostream that comments can be written to.
|
|
|
|
/// Unlike AddComment, you are required to terminate comments with \n if you
|
|
|
|
/// use this method.
|
|
|
|
virtual raw_ostream &GetCommentOS() {
|
|
|
|
if (!IsVerboseAsm)
|
|
|
|
return nulls(); // Discard comments unless in verbose asm mode.
|
|
|
|
return CommentStream;
|
|
|
|
}
|
|
|
|
|
2010-01-22 19:52:01 +00:00
|
|
|
/// AddBlankLine - Emit a blank line to a .s file to pretty it up.
|
|
|
|
virtual void AddBlankLine() {
|
|
|
|
EmitEOL();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2010-01-23 06:39:22 +00:00
|
|
|
virtual void EmitAssemblerFlag(MCAssemblerFlag 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
|
|
|
|
2010-01-23 06:39:22 +00:00
|
|
|
virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr 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
|
|
|
|
2010-01-25 07:52:13 +00:00
|
|
|
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
|
2010-01-23 07:47:02 +00:00
|
|
|
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
2009-08-30 06:17:16 +00:00
|
|
|
unsigned ByteAlignment);
|
2009-07-14 18:17:10 +00:00
|
|
|
|
2010-01-23 07:47:02 +00:00
|
|
|
/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
|
|
|
|
///
|
|
|
|
/// @param Symbol - The common symbol to emit.
|
|
|
|
/// @param Size - The size of the common symbol.
|
|
|
|
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
|
|
|
|
|
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
|
|
|
|
2010-01-19 19:46:13 +00:00
|
|
|
virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
|
2009-07-07 20:30:46 +00:00
|
|
|
|
2010-01-19 19:46:13 +00:00
|
|
|
virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
|
2010-01-19 22:03:38 +00:00
|
|
|
virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
|
|
|
|
|
2010-01-19 19:46:13 +00:00
|
|
|
virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
|
|
|
|
unsigned AddrSpace);
|
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
|
|
|
|
2010-01-22 18:21:35 +00:00
|
|
|
/// AddComment - Add a comment that can be emitted to the generated .s
|
2010-01-22 07:29:22 +00:00
|
|
|
/// file if applicable as a QoI issue to make the output of the compiler
|
|
|
|
/// more readable. This only affects the MCAsmStreamer, and only when
|
|
|
|
/// verbose assembly output is enabled.
|
2010-01-22 18:21:35 +00:00
|
|
|
void MCAsmStreamer::AddComment(const Twine &T) {
|
2010-01-22 07:29:22 +00:00
|
|
|
if (!IsVerboseAsm) return;
|
2010-01-22 19:17:48 +00:00
|
|
|
|
|
|
|
// Make sure that CommentStream is flushed.
|
|
|
|
CommentStream.flush();
|
|
|
|
|
2010-01-22 07:29:22 +00:00
|
|
|
T.toVector(CommentToEmit);
|
2010-01-22 19:17:48 +00:00
|
|
|
// Each comment goes on its own line.
|
|
|
|
CommentToEmit.push_back('\n');
|
2010-01-22 21:16:10 +00:00
|
|
|
|
|
|
|
// Tell the comment stream that the vector changed underneath it.
|
|
|
|
CommentStream.resync();
|
2010-01-22 07:29:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::EmitCommentsAndEOL() {
|
2010-01-22 19:17:48 +00:00
|
|
|
if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
|
|
|
|
OS << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommentStream.flush();
|
2010-01-22 07:29:22 +00:00
|
|
|
StringRef Comments = CommentToEmit.str();
|
2010-01-22 19:17:48 +00:00
|
|
|
|
|
|
|
assert(Comments.back() == '\n' &&
|
|
|
|
"Comment array not newline terminated");
|
|
|
|
do {
|
2010-01-22 07:29:22 +00:00
|
|
|
// Emit a line of comments.
|
|
|
|
OS.PadToColumn(MAI.getCommentColumn());
|
|
|
|
size_t Position = Comments.find('\n');
|
|
|
|
OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
|
|
|
|
|
|
|
|
Comments = Comments.substr(Position+1);
|
2010-01-22 19:17:48 +00:00
|
|
|
} while (!Comments.empty());
|
2010-01-22 07:29:22 +00:00
|
|
|
|
2010-01-22 21:16:10 +00:00
|
|
|
CommentToEmit.clear();
|
|
|
|
// Tell the comment stream that the vector changed underneath it.
|
|
|
|
CommentStream.resync();
|
2010-01-22 07:29:22 +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!");
|
|
|
|
|
2010-01-22 07:36:39 +00:00
|
|
|
OS << *Symbol << ":";
|
|
|
|
EmitEOL();
|
2009-08-22 07:22:36 +00:00
|
|
|
Symbol->setSection(*CurSection);
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-23 06:39:22 +00:00
|
|
|
void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
2009-07-16 17:56:39 +00:00
|
|
|
switch (Flag) {
|
2009-08-13 23:36:34 +00:00
|
|
|
default: assert(0 && "Invalid flag!");
|
2010-01-23 06:39:22 +00:00
|
|
|
case MCAF_SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
|
2009-07-16 17:56:39 +00:00
|
|
|
}
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
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
|
|
|
|
2010-01-22 07:36:39 +00:00
|
|
|
OS << *Symbol << " = " << *Value;
|
|
|
|
EmitEOL();
|
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,
|
2010-01-23 06:39:22 +00:00
|
|
|
MCSymbolAttr Attribute) {
|
2009-06-24 01:03:06 +00:00
|
|
|
switch (Attribute) {
|
2010-01-23 06:39:22 +00:00
|
|
|
case MCSA_Invalid: assert(0 && "Invalid symbol attribute");
|
2010-01-25 18:30:45 +00:00
|
|
|
case MCSA_ELF_TypeFunction: /// .type _foo, STT_FUNC # aka @function
|
|
|
|
case MCSA_ELF_TypeIndFunction: /// .type _foo, STT_GNU_IFUNC
|
|
|
|
case MCSA_ELF_TypeObject: /// .type _foo, STT_OBJECT # aka @object
|
|
|
|
case MCSA_ELF_TypeTLS: /// .type _foo, STT_TLS # aka @tls_object
|
|
|
|
case MCSA_ELF_TypeCommon: /// .type _foo, STT_COMMON # aka @common
|
|
|
|
case MCSA_ELF_TypeNoType: /// .type _foo, STT_NOTYPE # aka @notype
|
|
|
|
assert(MAI.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
|
2010-01-25 18:33:40 +00:00
|
|
|
OS << "\t.type " << *Symbol << ','
|
2010-01-25 18:30:45 +00:00
|
|
|
<< ((MAI.getCommentString()[0] != '@') ? '@' : '%');
|
|
|
|
switch (Attribute) {
|
|
|
|
default: assert(0 && "Unknown ELF .type");
|
|
|
|
case MCSA_ELF_TypeFunction: OS << "function"; break;
|
|
|
|
case MCSA_ELF_TypeIndFunction: OS << "gnu_indirect_function"; break;
|
|
|
|
case MCSA_ELF_TypeObject: OS << "object"; break;
|
|
|
|
case MCSA_ELF_TypeTLS: OS << "tls_object"; break;
|
|
|
|
case MCSA_ELF_TypeCommon: OS << "common"; break;
|
|
|
|
case MCSA_ELF_TypeNoType: OS << "no_type"; break;
|
|
|
|
}
|
|
|
|
EmitEOL();
|
|
|
|
return;
|
|
|
|
case MCSA_Global: // .globl/.global
|
|
|
|
OS << MAI.getGlobalDirective();
|
|
|
|
break;
|
2010-01-23 06:39:22 +00:00
|
|
|
case MCSA_Hidden: OS << ".hidden "; break;
|
|
|
|
case MCSA_IndirectSymbol: OS << ".indirect_symbol "; break;
|
|
|
|
case MCSA_Internal: OS << ".internal "; break;
|
|
|
|
case MCSA_LazyReference: OS << ".lazy_reference "; break;
|
|
|
|
case MCSA_Local: OS << ".local "; break;
|
|
|
|
case MCSA_NoDeadStrip: OS << ".no_dead_strip "; break;
|
|
|
|
case MCSA_PrivateExtern: OS << ".private_extern "; break;
|
|
|
|
case MCSA_Protected: OS << ".protected "; break;
|
|
|
|
case MCSA_Reference: OS << ".reference "; break;
|
|
|
|
case MCSA_Weak: OS << ".weak "; break;
|
|
|
|
case MCSA_WeakDefinition: OS << ".weak_definition "; break;
|
|
|
|
// .weak_reference
|
|
|
|
case MCSA_WeakReference: OS << MAI.getWeakRefDirective(); break;
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-22 07:36:39 +00:00
|
|
|
OS << *Symbol;
|
|
|
|
EmitEOL();
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2009-07-14 18:17:10 +00:00
|
|
|
void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
2010-01-22 07:36:39 +00:00
|
|
|
OS << ".desc" << ' ' << *Symbol << ',' << DescValue;
|
|
|
|
EmitEOL();
|
2009-07-14 18:17:10 +00:00
|
|
|
}
|
|
|
|
|
2010-01-25 07:52:13 +00:00
|
|
|
void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
|
|
|
|
assert(MAI.hasDotTypeDotSizeDirective());
|
|
|
|
OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
|
|
|
|
}
|
|
|
|
|
2010-01-23 07:47:02 +00:00
|
|
|
void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
2009-08-30 06:17:16 +00:00
|
|
|
unsigned ByteAlignment) {
|
2010-01-23 07:47:02 +00:00
|
|
|
OS << "\t.comm\t" << *Symbol << ',' << Size;
|
2010-01-25 07:29:13 +00:00
|
|
|
if (ByteAlignment != 0) {
|
2010-01-19 06:01:04 +00:00
|
|
|
if (MAI.getAlignmentIsInBytes())
|
|
|
|
OS << ',' << ByteAlignment;
|
|
|
|
else
|
|
|
|
OS << ',' << Log2_32(ByteAlignment);
|
|
|
|
}
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
2009-07-07 20:30:46 +00:00
|
|
|
}
|
|
|
|
|
2010-01-23 07:47:02 +00:00
|
|
|
/// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
|
|
|
|
///
|
|
|
|
/// @param Symbol - The common symbol to emit.
|
|
|
|
/// @param Size - The size of the common symbol.
|
|
|
|
void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
|
|
|
|
assert(MAI.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
|
|
|
|
OS << "\t.lcomm\t" << *Symbol << ',' << Size;
|
|
|
|
EmitEOL();
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-01-17 21:43:43 +00:00
|
|
|
OS << ',' << *Symbol << ',' << Size;
|
2009-08-30 06:17:16 +00:00
|
|
|
if (ByteAlignment != 0)
|
|
|
|
OS << ',' << Log2_32(ByteAlignment);
|
2009-07-10 22:20:30 +00:00
|
|
|
}
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
2009-07-10 22:20:30 +00:00
|
|
|
}
|
|
|
|
|
2010-01-23 00:15:00 +00:00
|
|
|
static inline char toOctal(int X) { return (X&7)+'0'; }
|
|
|
|
|
2010-01-19 19:46:13 +00:00
|
|
|
void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
|
2009-06-24 17:00:42 +00:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2010-01-23 00:15:00 +00:00
|
|
|
if (Data.empty()) return;
|
|
|
|
|
|
|
|
if (Data.size() == 1) {
|
|
|
|
OS << MAI.getData8bitsDirective(AddrSpace);
|
|
|
|
OS << (unsigned)(unsigned char)Data[0];
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
2010-01-23 00:15:00 +00:00
|
|
|
return;
|
2010-01-22 07:36:39 +00:00
|
|
|
}
|
2010-01-23 00:15:00 +00:00
|
|
|
|
|
|
|
// 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 == '\\') {
|
|
|
|
OS << '\\' << (char)C;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isprint((unsigned char)C)) {
|
|
|
|
OS << (char)C;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (C) {
|
|
|
|
case '\b': OS << "\\b"; break;
|
|
|
|
case '\f': OS << "\\f"; break;
|
|
|
|
case '\n': OS << "\\n"; break;
|
|
|
|
case '\r': OS << "\\r"; break;
|
|
|
|
case '\t': OS << "\\t"; break;
|
|
|
|
default:
|
|
|
|
OS << '\\';
|
|
|
|
OS << toOctal(C >> 6);
|
|
|
|
OS << toOctal(C >> 3);
|
|
|
|
OS << toOctal(C >> 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << '"';
|
|
|
|
EmitEOL();
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 22:03:38 +00:00
|
|
|
/// EmitIntValue - Special case of EmitValue that avoids the client having
|
|
|
|
/// to pass in a MCExpr for constant integers.
|
|
|
|
void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
|
|
|
|
unsigned AddrSpace) {
|
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
|
|
|
const char *Directive = 0;
|
|
|
|
switch (Size) {
|
|
|
|
default: break;
|
|
|
|
case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
|
|
|
|
case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
|
|
|
|
case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
|
2010-01-20 06:45:39 +00:00
|
|
|
case 8:
|
|
|
|
Directive = MAI.getData64bitsDirective(AddrSpace);
|
|
|
|
// If the target doesn't support 64-bit data, emit as two 32-bit halves.
|
|
|
|
if (Directive) break;
|
|
|
|
if (isLittleEndian()) {
|
|
|
|
EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
|
|
|
|
EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
|
|
|
|
} else {
|
|
|
|
EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
|
|
|
|
EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
|
|
|
|
}
|
|
|
|
return;
|
2010-01-19 22:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(Directive && "Invalid size for machine code value!");
|
2010-01-22 07:29:22 +00:00
|
|
|
OS << Directive << truncateToSize(Value, Size);
|
|
|
|
EmitEOL();
|
2010-01-19 22:03:38 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 19:46:13 +00:00
|
|
|
void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
|
|
|
|
unsigned AddrSpace) {
|
2009-06-24 17:00:42 +00:00
|
|
|
assert(CurSection && "Cannot emit contents before setting section!");
|
2010-01-19 22:03:38 +00:00
|
|
|
const char *Directive = 0;
|
2009-06-24 01:03:06 +00:00
|
|
|
switch (Size) {
|
2010-01-19 22:03:38 +00:00
|
|
|
default: break;
|
|
|
|
case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
|
|
|
|
case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
|
|
|
|
case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
|
|
|
|
case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
2010-01-19 19:46:13 +00:00
|
|
|
|
2010-01-19 22:03:38 +00:00
|
|
|
assert(Directive && "Invalid size for machine code value!");
|
2010-01-22 07:29:22 +00:00
|
|
|
OS << Directive << *truncateToSize(Value, Size);
|
|
|
|
EmitEOL();
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
2010-01-19 18:52:28 +00:00
|
|
|
/// EmitFill - Emit NumBytes bytes worth of the value specified by
|
|
|
|
/// FillValue. This implements directives such as '.space'.
|
2010-01-19 19:46:13 +00:00
|
|
|
void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
|
|
|
|
unsigned AddrSpace) {
|
2010-01-19 18:58:52 +00:00
|
|
|
if (NumBytes == 0) return;
|
|
|
|
|
2010-01-19 19:46:13 +00:00
|
|
|
if (AddrSpace == 0)
|
|
|
|
if (const char *ZeroDirective = MAI.getZeroDirective()) {
|
|
|
|
OS << ZeroDirective << NumBytes;
|
|
|
|
if (FillValue != 0)
|
|
|
|
OS << ',' << (int)FillValue;
|
2010-01-22 07:29:22 +00:00
|
|
|
EmitEOL();
|
2010-01-19 19:46:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit a byte at a time.
|
|
|
|
MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
|
2010-01-19 18:52:28 +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;
|
|
|
|
}
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
2009-08-19 06:12:02 +00:00
|
|
|
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;
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
2009-06-24 19:25:34 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2010-01-22 07:36:39 +00:00
|
|
|
OS << ".org " << *Offset << ", " << (unsigned) Value;
|
|
|
|
EmitEOL();
|
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);
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
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);
|
2010-01-22 07:36:39 +00:00
|
|
|
EmitEOL();
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCAsmStreamer::Finish() {
|
|
|
|
OS.flush();
|
|
|
|
}
|
|
|
|
|
2010-01-22 07:29:22 +00:00
|
|
|
MCStreamer *llvm::createAsmStreamer(MCContext &Context,
|
|
|
|
formatted_raw_ostream &OS,
|
2010-01-20 06:39:07 +00:00
|
|
|
const MCAsmInfo &MAI, bool isLittleEndian,
|
2010-01-22 07:06:15 +00:00
|
|
|
bool isVerboseAsm, MCInstPrinter *IP,
|
2009-08-27 00:51:57 +00:00
|
|
|
MCCodeEmitter *CE) {
|
2010-01-22 07:06:15 +00:00
|
|
|
return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,
|
|
|
|
IP, CE);
|
2009-06-24 01:03:06 +00:00
|
|
|
}
|