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);
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 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
82 assert(Bytes && "Invalid size!");
83 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
86 static inline const MCExpr *truncateToSize(const MCExpr *Value,
88 // FIXME: Do we really need this routine?
92 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
93 assert(Section && "Cannot switch to a null section!");
94 if (Section != CurSection) {
96 Section->PrintSwitchToSection(MAI, OS);
100 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
101 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
102 assert(CurSection && "Cannot emit before setting section!");
104 OS << *Symbol << ":\n";
105 Symbol->setSection(*CurSection);
108 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
110 default: assert(0 && "Invalid flag!");
111 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
116 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
117 // Only absolute symbols can be redefined.
118 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
119 "Cannot define a symbol twice!");
121 OS << *Symbol << " = ";
122 Value->print(OS, &MAI);
125 // FIXME: Lift context changes into super class.
126 // FIXME: Set associated section.
127 Symbol->setValue(Value);
130 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
131 SymbolAttr Attribute) {
133 case Global: OS << ".globl"; break;
134 case Hidden: OS << ".hidden"; break;
135 case IndirectSymbol: OS << ".indirect_symbol"; break;
136 case Internal: OS << ".internal"; break;
137 case LazyReference: OS << ".lazy_reference"; break;
138 case NoDeadStrip: OS << ".no_dead_strip"; break;
139 case PrivateExtern: OS << ".private_extern"; break;
140 case Protected: OS << ".protected"; break;
141 case Reference: OS << ".reference"; break;
142 case Weak: OS << ".weak"; break;
143 case WeakDefinition: OS << ".weak_definition"; break;
144 case WeakReference: OS << ".weak_reference"; break;
147 OS << ' ' << *Symbol << '\n';
150 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
151 OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n';
154 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
155 unsigned ByteAlignment) {
156 OS << ".comm " << *Symbol << ',' << Size;
157 if (ByteAlignment != 0)
158 OS << ',' << Log2_32(ByteAlignment);
162 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
163 unsigned Size, unsigned ByteAlignment) {
164 // Note: a .zerofill directive does not switch sections.
167 // This is a mach-o specific directive.
168 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
169 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
171 if (Symbol != NULL) {
172 OS << ',' << *Symbol << ',' << Size;
173 if (ByteAlignment != 0)
174 OS << ',' << Log2_32(ByteAlignment);
179 void MCAsmStreamer::EmitBytes(StringRef Data) {
180 assert(CurSection && "Cannot emit contents before setting section!");
181 for (unsigned i = 0, e = Data.size(); i != e; ++i)
182 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
185 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
186 assert(CurSection && "Cannot emit contents before setting section!");
187 // Need target hooks to know how to print this.
190 llvm_unreachable("Invalid size for machine code value!");
191 case 1: OS << ".byte"; break;
192 case 2: OS << ".short"; break;
193 case 4: OS << ".long"; break;
194 case 8: OS << ".quad"; break;
198 truncateToSize(Value, Size)->print(OS, &MAI);
202 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
204 unsigned MaxBytesToEmit) {
205 // Some assemblers don't support non-power of two alignments, so we always
206 // emit alignments as a power of two if possible.
207 if (isPowerOf2_32(ByteAlignment)) {
209 default: llvm_unreachable("Invalid size for machine code value!");
210 case 1: OS << MAI.getAlignDirective(); break;
211 // FIXME: use MAI for this!
212 case 2: OS << ".p2alignw "; break;
213 case 4: OS << ".p2alignl "; break;
214 case 8: llvm_unreachable("Unsupported alignment size!");
217 if (MAI.getAlignmentIsInBytes())
220 OS << Log2_32(ByteAlignment);
222 if (Value || MaxBytesToEmit) {
224 OS.write_hex(truncateToSize(Value, ValueSize));
227 OS << ", " << MaxBytesToEmit;
233 // Non-power of two alignment. This is not widely supported by assemblers.
234 // FIXME: Parameterize this based on MAI.
236 default: llvm_unreachable("Invalid size for machine code value!");
237 case 1: OS << ".balign"; break;
238 case 2: OS << ".balignw"; break;
239 case 4: OS << ".balignl"; break;
240 case 8: llvm_unreachable("Unsupported alignment size!");
243 OS << ' ' << ByteAlignment;
244 OS << ", " << truncateToSize(Value, ValueSize);
246 OS << ", " << MaxBytesToEmit;
250 void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
251 unsigned char Value) {
252 // FIXME: Verify that Offset is associated with the current section.
254 Offset->print(OS, &MAI);
255 OS << ", " << (unsigned) Value << '\n';
258 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
259 assert(CurSection && "Cannot emit contents before setting section!");
261 // If we have an AsmPrinter, use that to print.
263 InstPrinter->printInst(&Inst);
266 // Show the encoding if we have a code emitter.
268 SmallString<256> Code;
269 raw_svector_ostream VecOS(Code);
270 Emitter->EncodeInstruction(Inst, VecOS);
274 OS << " # encoding: [";
275 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
278 OS << format("%#04x", uint8_t(Code[i]));
286 // Otherwise fall back to a structural printing for now. Eventually we should
287 // always have access to the target specific printer.
288 Inst.print(OS, &MAI);
292 void MCAsmStreamer::Finish() {
296 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
297 const MCAsmInfo &MAI, MCInstPrinter *IP,
299 return new MCAsmStreamer(Context, OS, MAI, IP, CE);