1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/CodeGen/AsmPrinter.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCCodeEmitter.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCValue.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/raw_ostream.h"
29 class MCAsmStreamer : public MCStreamer {
33 MCCodeEmitter *Emitter;
35 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
36 AsmPrinter *_Printer, MCCodeEmitter *_Emitter)
37 : MCStreamer(Context), OS(_OS), MAI(tai), Printer(_Printer),
41 /// @name MCStreamer Interface
44 virtual void SwitchSection(const MCSection *Section);
46 virtual void EmitLabel(MCSymbol *Symbol);
48 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
50 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value);
52 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
54 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
56 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
57 unsigned ByteAlignment);
59 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
60 unsigned Size = 0, unsigned ByteAlignment = 0);
62 virtual void EmitBytes(const StringRef &Data);
64 virtual void EmitValue(const MCValue &Value, unsigned Size);
66 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
67 unsigned ValueSize = 1,
68 unsigned MaxBytesToEmit = 0);
70 virtual void EmitValueToOffset(const MCValue &Offset,
71 unsigned char Value = 0);
73 virtual void EmitInstruction(const MCInst &Inst);
75 virtual void Finish();
80 } // end anonymous namespace.
82 /// Allow printing symbols directly to a raw_ostream with proper quoting.
83 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
88 /// Allow printing values directly to a raw_ostream.
89 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
94 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
95 assert(Bytes && "Invalid size!");
96 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
99 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
100 return MCValue::get(Value.getSymA(), Value.getSymB(),
101 truncateToSize(Value.getConstant(), Bytes));
104 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
105 assert(Section && "Cannot switch to a null section!");
106 if (Section != CurSection) {
107 CurSection = Section;
108 Section->PrintSwitchToSection(MAI, OS);
112 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
113 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
114 assert(CurSection && "Cannot emit before setting section!");
116 OS << Symbol << ":\n";
117 Symbol->setSection(*CurSection);
120 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
122 default: assert(0 && "Invalid flag!");
123 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
128 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value) {
129 // Only absolute symbols can be redefined.
130 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
131 "Cannot define a symbol twice!");
133 OS << Symbol << " = " << Value << '\n';
136 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
137 SymbolAttr Attribute) {
139 case Global: OS << ".globl"; break;
140 case Hidden: OS << ".hidden"; break;
141 case IndirectSymbol: OS << ".indirect_symbol"; break;
142 case Internal: OS << ".internal"; break;
143 case LazyReference: OS << ".lazy_reference"; break;
144 case NoDeadStrip: OS << ".no_dead_strip"; break;
145 case PrivateExtern: OS << ".private_extern"; break;
146 case Protected: OS << ".protected"; break;
147 case Reference: OS << ".reference"; break;
148 case Weak: OS << ".weak"; break;
149 case WeakDefinition: OS << ".weak_definition"; break;
150 case WeakReference: OS << ".weak_reference"; break;
153 OS << ' ' << Symbol << '\n';
156 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
157 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
160 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
161 unsigned ByteAlignment) {
163 OS << ' ' << Symbol << ',' << Size;
164 if (ByteAlignment != 0)
165 OS << ',' << Log2_32(ByteAlignment);
169 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
170 unsigned Size, unsigned ByteAlignment) {
171 // Note: a .zerofill directive does not switch sections.
174 // This is a mach-o specific directive.
175 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
176 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
178 if (Symbol != NULL) {
179 OS << ',' << Symbol << ',' << Size;
180 if (ByteAlignment != 0)
181 OS << ',' << Log2_32(ByteAlignment);
186 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
187 assert(CurSection && "Cannot emit contents before setting section!");
188 for (unsigned i = 0, e = Data.size(); i != e; ++i)
189 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
192 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
193 assert(CurSection && "Cannot emit contents before setting section!");
194 // Need target hooks to know how to print this.
197 llvm_unreachable("Invalid size for machine code value!");
198 case 1: OS << ".byte"; break;
199 case 2: OS << ".short"; break;
200 case 4: OS << ".long"; break;
201 case 8: OS << ".quad"; break;
204 OS << ' ' << truncateToSize(Value, Size) << '\n';
207 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
209 unsigned MaxBytesToEmit) {
210 // Some assemblers don't support non-power of two alignments, so we always
211 // emit alignments as a power of two if possible.
212 if (isPowerOf2_32(ByteAlignment)) {
214 default: llvm_unreachable("Invalid size for machine code value!");
215 case 1: OS << MAI.getAlignDirective(); break;
216 // FIXME: use MAI for this!
217 case 2: OS << ".p2alignw "; break;
218 case 4: OS << ".p2alignl "; break;
219 case 8: llvm_unreachable("Unsupported alignment size!");
222 if (MAI.getAlignmentIsInBytes())
225 OS << Log2_32(ByteAlignment);
227 if (Value || MaxBytesToEmit) {
228 OS << ", " << truncateToSize(Value, ValueSize);
231 OS << ", " << MaxBytesToEmit;
237 // Non-power of two alignment. This is not widely supported by assemblers.
238 // FIXME: Parameterize this based on MAI.
240 default: llvm_unreachable("Invalid size for machine code value!");
241 case 1: OS << ".balign"; break;
242 case 2: OS << ".balignw"; break;
243 case 4: OS << ".balignl"; break;
244 case 8: llvm_unreachable("Unsupported alignment size!");
247 OS << ' ' << ByteAlignment;
248 OS << ", " << truncateToSize(Value, ValueSize);
250 OS << ", " << MaxBytesToEmit;
254 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
255 unsigned char Value) {
256 // FIXME: Verify that Offset is associated with the current section.
257 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
260 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
261 assert(CurSection && "Cannot emit contents before setting section!");
263 // If we have an AsmPrinter, use that to print.
265 Printer->printMCInst(&Inst);
267 // Show the encoding if we have a code emitter.
269 SmallString<256> Code;
270 raw_svector_ostream VecOS(Code);
271 Emitter->EncodeInstruction(Inst, VecOS);
275 OS << " # encoding: [";
276 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
279 OS << format("%#04x", uint8_t(Code[i]));
287 // Otherwise fall back to a structural printing for now. Eventually we should
288 // always have access to the target specific printer.
293 void MCAsmStreamer::Finish() {
297 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
298 const MCAsmInfo &MAI, AsmPrinter *AP,
300 return new MCAsmStreamer(Context, OS, MAI, AP, CE);