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 bool IsLittleEndian, IsVerboseAsm;
32 MCInstPrinter *InstPrinter;
33 MCCodeEmitter *Emitter;
35 MCAsmStreamer(MCContext &Context, raw_ostream &os, const MCAsmInfo &mai,
36 bool isLittleEndian, bool isVerboseAsm, MCInstPrinter *printer,
37 MCCodeEmitter *emitter)
38 : MCStreamer(Context), OS(os), MAI(mai), IsLittleEndian(isLittleEndian),
39 IsVerboseAsm(isVerboseAsm), InstPrinter(printer), Emitter(emitter) {}
42 bool isLittleEndian() const { return IsLittleEndian; }
44 /// @name MCStreamer Interface
47 virtual void SwitchSection(const MCSection *Section);
49 virtual void EmitLabel(MCSymbol *Symbol);
51 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
53 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
55 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
57 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
59 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
60 unsigned ByteAlignment);
62 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
63 unsigned Size = 0, unsigned ByteAlignment = 0);
65 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
67 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
68 virtual void EmitIntValue(uint64_t Value, unsigned Size, unsigned AddrSpace);
70 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
73 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
74 unsigned ValueSize = 1,
75 unsigned MaxBytesToEmit = 0);
77 virtual void EmitValueToOffset(const MCExpr *Offset,
78 unsigned char Value = 0);
80 virtual void EmitInstruction(const MCInst &Inst);
82 virtual void Finish();
87 } // end anonymous namespace.
89 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
90 assert(Bytes && "Invalid size!");
91 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
94 static inline const MCExpr *truncateToSize(const MCExpr *Value,
96 // FIXME: Do we really need this routine?
100 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
101 assert(Section && "Cannot switch to a null section!");
102 if (Section != CurSection) {
103 CurSection = Section;
104 Section->PrintSwitchToSection(MAI, OS);
108 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
109 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
110 assert(CurSection && "Cannot emit before setting section!");
112 OS << *Symbol << ":\n";
113 Symbol->setSection(*CurSection);
116 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
118 default: assert(0 && "Invalid flag!");
119 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
124 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
125 // Only absolute symbols can be redefined.
126 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
127 "Cannot define a symbol twice!");
129 OS << *Symbol << " = " << *Value << '\n';
131 // FIXME: Lift context changes into super class.
132 // FIXME: Set associated section.
133 Symbol->setValue(Value);
136 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
137 SymbolAttr Attribute) {
139 case Global: OS << MAI.getGlobalDirective(); break; // .globl
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) {
162 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
163 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
164 if (MAI.getAlignmentIsInBytes())
165 OS << ',' << ByteAlignment;
167 OS << ',' << Log2_32(ByteAlignment);
172 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
173 unsigned Size, unsigned ByteAlignment) {
174 // Note: a .zerofill directive does not switch sections.
177 // This is a mach-o specific directive.
178 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
179 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
181 if (Symbol != NULL) {
182 OS << ',' << *Symbol << ',' << Size;
183 if (ByteAlignment != 0)
184 OS << ',' << Log2_32(ByteAlignment);
189 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
190 assert(CurSection && "Cannot emit contents before setting section!");
191 const char *Directive = MAI.getData8bitsDirective(AddrSpace);
192 for (unsigned i = 0, e = Data.size(); i != e; ++i)
193 OS << Directive << (unsigned)(unsigned char)Data[i] << '\n';
196 /// EmitIntValue - Special case of EmitValue that avoids the client having
197 /// to pass in a MCExpr for constant integers.
198 void MCAsmStreamer::EmitIntValue(uint64_t Value, unsigned Size,
199 unsigned AddrSpace) {
200 assert(CurSection && "Cannot emit contents before setting section!");
201 const char *Directive = 0;
204 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
205 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
206 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
208 Directive = MAI.getData64bitsDirective(AddrSpace);
209 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
210 if (Directive) break;
211 if (isLittleEndian()) {
212 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
213 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
215 EmitIntValue((uint32_t)(Value >> 32), 4, AddrSpace);
216 EmitIntValue((uint32_t)(Value >> 0 ), 4, AddrSpace);
221 assert(Directive && "Invalid size for machine code value!");
222 OS << Directive << truncateToSize(Value, Size) << '\n';
225 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
226 unsigned AddrSpace) {
227 assert(CurSection && "Cannot emit contents before setting section!");
228 const char *Directive = 0;
231 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
232 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
233 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
234 case 8: Directive = MAI.getData64bitsDirective(AddrSpace); break;
237 assert(Directive && "Invalid size for machine code value!");
238 OS << Directive << *truncateToSize(Value, Size) << '\n';
241 /// EmitFill - Emit NumBytes bytes worth of the value specified by
242 /// FillValue. This implements directives such as '.space'.
243 void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
244 unsigned AddrSpace) {
245 if (NumBytes == 0) return;
248 if (const char *ZeroDirective = MAI.getZeroDirective()) {
249 OS << ZeroDirective << NumBytes;
251 OS << ',' << (int)FillValue;
256 // Emit a byte at a time.
257 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
260 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
262 unsigned MaxBytesToEmit) {
263 // Some assemblers don't support non-power of two alignments, so we always
264 // emit alignments as a power of two if possible.
265 if (isPowerOf2_32(ByteAlignment)) {
267 default: llvm_unreachable("Invalid size for machine code value!");
268 case 1: OS << MAI.getAlignDirective(); break;
269 // FIXME: use MAI for this!
270 case 2: OS << ".p2alignw "; break;
271 case 4: OS << ".p2alignl "; break;
272 case 8: llvm_unreachable("Unsupported alignment size!");
275 if (MAI.getAlignmentIsInBytes())
278 OS << Log2_32(ByteAlignment);
280 if (Value || MaxBytesToEmit) {
282 OS.write_hex(truncateToSize(Value, ValueSize));
285 OS << ", " << MaxBytesToEmit;
291 // Non-power of two alignment. This is not widely supported by assemblers.
292 // FIXME: Parameterize this based on MAI.
294 default: llvm_unreachable("Invalid size for machine code value!");
295 case 1: OS << ".balign"; break;
296 case 2: OS << ".balignw"; break;
297 case 4: OS << ".balignl"; break;
298 case 8: llvm_unreachable("Unsupported alignment size!");
301 OS << ' ' << ByteAlignment;
302 OS << ", " << truncateToSize(Value, ValueSize);
304 OS << ", " << MaxBytesToEmit;
308 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
309 unsigned char Value) {
310 // FIXME: Verify that Offset is associated with the current section.
311 OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
314 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
315 assert(CurSection && "Cannot emit contents before setting section!");
317 // If we have an AsmPrinter, use that to print.
319 InstPrinter->printInst(&Inst);
322 // Show the encoding if we have a code emitter.
324 SmallString<256> Code;
325 raw_svector_ostream VecOS(Code);
326 Emitter->EncodeInstruction(Inst, VecOS);
330 OS << " # encoding: [";
331 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
334 OS << format("%#04x", uint8_t(Code[i]));
342 // Otherwise fall back to a structural printing for now. Eventually we should
343 // always have access to the target specific printer.
344 Inst.print(OS, &MAI);
348 void MCAsmStreamer::Finish() {
352 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
353 const MCAsmInfo &MAI, bool isLittleEndian,
354 bool isVerboseAsm, MCInstPrinter *IP,
356 return new MCAsmStreamer(Context, OS, MAI, isLittleEndian, isVerboseAsm,