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/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.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 {
31 MCInstPrinter *InstPrinter;
32 MCCodeEmitter *Emitter;
34 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
35 MCInstPrinter *_Printer, MCCodeEmitter *_Emitter)
36 : MCStreamer(Context), OS(_OS), MAI(tai), InstPrinter(_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(StringRef Data, unsigned AddrSpace);
63 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
64 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
66 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
69 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
70 unsigned ValueSize = 1,
71 unsigned MaxBytesToEmit = 0);
73 virtual void EmitValueToOffset(const MCExpr *Offset,
74 unsigned char Value = 0);
76 virtual void EmitInstruction(const MCInst &Inst);
78 virtual void Finish();
83 } // end anonymous namespace.
85 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
86 assert(Bytes && "Invalid size!");
87 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
90 static inline const MCExpr *truncateToSize(const MCExpr *Value,
92 // FIXME: Do we really need this routine?
96 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
97 assert(Section && "Cannot switch to a null section!");
98 if (Section != CurSection) {
100 Section->PrintSwitchToSection(MAI, OS);
104 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
105 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
106 assert(CurSection && "Cannot emit before setting section!");
108 OS << *Symbol << ":\n";
109 Symbol->setSection(*CurSection);
112 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
114 default: assert(0 && "Invalid flag!");
115 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
120 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
121 // Only absolute symbols can be redefined.
122 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
123 "Cannot define a symbol twice!");
125 OS << *Symbol << " = " << *Value << '\n';
127 // FIXME: Lift context changes into super class.
128 // FIXME: Set associated section.
129 Symbol->setValue(Value);
132 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
133 SymbolAttr Attribute) {
135 case Global: OS << ".globl"; break;
136 case Hidden: OS << ".hidden"; break;
137 case IndirectSymbol: OS << ".indirect_symbol"; break;
138 case Internal: OS << ".internal"; break;
139 case LazyReference: OS << ".lazy_reference"; break;
140 case NoDeadStrip: OS << ".no_dead_strip"; break;
141 case PrivateExtern: OS << ".private_extern"; break;
142 case Protected: OS << ".protected"; break;
143 case Reference: OS << ".reference"; break;
144 case Weak: OS << ".weak"; break;
145 case WeakDefinition: OS << ".weak_definition"; break;
146 case WeakReference: OS << ".weak_reference"; break;
149 OS << ' ' << *Symbol << '\n';
152 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
153 OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n';
156 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
157 unsigned ByteAlignment) {
158 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
159 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
160 if (MAI.getAlignmentIsInBytes())
161 OS << ',' << ByteAlignment;
163 OS << ',' << Log2_32(ByteAlignment);
168 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
169 unsigned Size, unsigned ByteAlignment) {
170 // Note: a .zerofill directive does not switch sections.
173 // This is a mach-o specific directive.
174 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
175 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
177 if (Symbol != NULL) {
178 OS << ',' << *Symbol << ',' << Size;
179 if (ByteAlignment != 0)
180 OS << ',' << Log2_32(ByteAlignment);
185 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
186 assert(CurSection && "Cannot emit contents before setting section!");
187 const char *Directive = MAI.getData8bitsDirective(AddrSpace);
188 for (unsigned i = 0, e = Data.size(); i != e; ++i)
189 OS << Directive << (unsigned)(unsigned char)Data[i] << '\n';
192 /// EmitIntValue - Special case of EmitValue that avoids the client having
193 /// to pass in a MCExpr for constant integers.
194 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
195 unsigned AddrSpace) {
196 assert(CurSection && "Cannot emit contents before setting section!");
197 // Need target hooks to know how to print this.
198 const char *Directive = 0;
201 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
202 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
203 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
204 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
207 assert(Directive && "Invalid size for machine code value!");
208 OS << Directive << truncateToSize(Value, Size) << '\n';
211 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
212 unsigned AddrSpace) {
213 assert(CurSection && "Cannot emit contents before setting section!");
214 // Need target hooks to know how to print this.
215 const char *Directive = 0;
218 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
219 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
220 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
221 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
224 assert(Directive && "Invalid size for machine code value!");
225 OS << Directive << *truncateToSize(Value, Size) << '\n';
228 /// EmitFill - Emit NumBytes bytes worth of the value specified by
229 /// FillValue. This implements directives such as '.space'.
230 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
231 unsigned AddrSpace) {
232 if (NumBytes == 0) return;
235 if (const char *ZeroDirective = MAI.getZeroDirective()) {
236 OS << ZeroDirective << NumBytes;
238 OS << ',' << (int)FillValue;
243 // Emit a byte at a time.
244 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
247 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
249 unsigned MaxBytesToEmit) {
250 // Some assemblers don't support non-power of two alignments, so we always
251 // emit alignments as a power of two if possible.
252 if (isPowerOf2_32(ByteAlignment)) {
254 default: llvm_unreachable("Invalid size for machine code value!");
255 case 1: OS << MAI.getAlignDirective(); break;
256 // FIXME: use MAI for this!
257 case 2: OS << ".p2alignw "; break;
258 case 4: OS << ".p2alignl "; break;
259 case 8: llvm_unreachable("Unsupported alignment size!");
262 if (MAI.getAlignmentIsInBytes())
265 OS << Log2_32(ByteAlignment);
267 if (Value || MaxBytesToEmit) {
269 OS.write_hex(truncateToSize(Value, ValueSize));
272 OS << ", " << MaxBytesToEmit;
278 // Non-power of two alignment. This is not widely supported by assemblers.
279 // FIXME: Parameterize this based on MAI.
281 default: llvm_unreachable("Invalid size for machine code value!");
282 case 1: OS << ".balign"; break;
283 case 2: OS << ".balignw"; break;
284 case 4: OS << ".balignl"; break;
285 case 8: llvm_unreachable("Unsupported alignment size!");
288 OS << ' ' << ByteAlignment;
289 OS << ", " << truncateToSize(Value, ValueSize);
291 OS << ", " << MaxBytesToEmit;
295 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
296 unsigned char Value) {
297 // FIXME: Verify that Offset is associated with the current section.
298 OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
301 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
302 assert(CurSection && "Cannot emit contents before setting section!");
304 // If we have an AsmPrinter, use that to print.
306 InstPrinter->printInst(&Inst);
309 // Show the encoding if we have a code emitter.
311 SmallString<256> Code;
312 raw_svector_ostream VecOS(Code);
313 Emitter->EncodeInstruction(Inst, VecOS);
317 OS << " # encoding: [";
318 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
321 OS << format("%#04x", uint8_t(Code[i]));
329 // Otherwise fall back to a structural printing for now. Eventually we should
330 // always have access to the target specific printer.
331 Inst.print(OS, &MAI);
335 void MCAsmStreamer::Finish() {
339 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
340 const MCAsmInfo &MAI, MCInstPrinter *IP,
342 return new MCAsmStreamer(Context, OS, MAI, IP, CE);