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/Support/ErrorHandling.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/raw_ostream.h"
28 class MCAsmStreamer : public MCStreamer {
32 MCCodeEmitter *Emitter;
34 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
35 AsmPrinter *_Printer, MCCodeEmitter *_Emitter)
36 : MCStreamer(Context), OS(_OS), MAI(tai), Printer(_Printer),
40 /// @name MCStreamer Interface
43 virtual void SwitchSection(const MCSection *Section);
45 virtual void EmitLabel(MCSymbol *Symbol);
47 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
49 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
51 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
53 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
55 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
56 unsigned ByteAlignment);
58 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
59 unsigned Size = 0, unsigned ByteAlignment = 0);
61 virtual void EmitBytes(const StringRef &Data);
63 virtual void EmitValue(const MCExpr *Value, unsigned Size);
65 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
66 unsigned ValueSize = 1,
67 unsigned MaxBytesToEmit = 0);
69 virtual void EmitValueToOffset(const MCExpr *Offset,
70 unsigned char Value = 0);
72 virtual void EmitInstruction(const MCInst &Inst);
74 virtual void Finish();
79 } // end anonymous namespace.
81 /// Allow printing symbols directly to a raw_ostream with proper quoting.
82 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
87 /// Allow printing values directly to a raw_ostream.
88 static inline raw_ostream &operator<<(raw_ostream &os, const MCExpr &Value) {
93 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
94 assert(Bytes && "Invalid size!");
95 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
98 static inline const MCExpr *truncateToSize(const MCExpr *Value,
100 // FIXME: Do we really need this routine?
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 MCExpr *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 MCExpr *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) {
229 OS.write_hex(truncateToSize(Value, ValueSize));
232 OS << ", " << MaxBytesToEmit;
238 // Non-power of two alignment. This is not widely supported by assemblers.
239 // FIXME: Parameterize this based on MAI.
241 default: llvm_unreachable("Invalid size for machine code value!");
242 case 1: OS << ".balign"; break;
243 case 2: OS << ".balignw"; break;
244 case 4: OS << ".balignl"; break;
245 case 8: llvm_unreachable("Unsupported alignment size!");
248 OS << ' ' << ByteAlignment;
249 OS << ", " << truncateToSize(Value, ValueSize);
251 OS << ", " << MaxBytesToEmit;
255 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
256 unsigned char Value) {
257 // FIXME: Verify that Offset is associated with the current section.
258 OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
261 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
262 assert(CurSection && "Cannot emit contents before setting section!");
264 // If we have an AsmPrinter, use that to print.
266 Printer->printMCInst(&Inst);
268 // Show the encoding if we have a code emitter.
270 SmallString<256> Code;
271 raw_svector_ostream VecOS(Code);
272 Emitter->EncodeInstruction(Inst, VecOS);
276 OS << " # encoding: [";
277 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
280 OS << format("%#04x", uint8_t(Code[i]));
288 // Otherwise fall back to a structural printing for now. Eventually we should
289 // always have access to the target specific printer.
294 void MCAsmStreamer::Finish() {
298 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
299 const MCAsmInfo &MAI, AsmPrinter *AP,
301 return new MCAsmStreamer(Context, OS, MAI, AP, CE);