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/MCCodeEmitter.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCSectionMachO.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/MC/MCAsmInfo.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 MCValue &Value,
50 bool MakeAbsolute = false);
52 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
54 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
56 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
58 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
59 unsigned Pow2Alignment);
61 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
62 unsigned Size = 0, unsigned Pow2Alignment = 0);
64 virtual void EmitBytes(const StringRef &Data);
66 virtual void EmitValue(const MCValue &Value, unsigned Size);
68 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
69 unsigned ValueSize = 1,
70 unsigned MaxBytesToEmit = 0);
72 virtual void EmitValueToOffset(const MCValue &Offset,
73 unsigned char Value = 0);
75 virtual void EmitInstruction(const MCInst &Inst);
77 virtual void Finish();
82 } // end anonymous namespace.
84 /// Allow printing symbols directly to a raw_ostream with proper quoting.
85 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
90 /// Allow printing values directly to a raw_ostream.
91 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
96 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
97 assert(Bytes && "Invalid size!");
98 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
101 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
102 return MCValue::get(Value.getSymA(), Value.getSymB(),
103 truncateToSize(Value.getConstant(), Bytes));
106 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
107 assert(Section && "Cannot switch to a null section!");
108 if (Section != CurSection) {
109 CurSection = Section;
110 Section->PrintSwitchToSection(MAI, OS);
114 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
115 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
116 assert(CurSection && "Cannot emit before setting section!");
118 OS << Symbol << ":\n";
119 Symbol->setSection(*CurSection);
122 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
124 default: assert(0 && "Invalid flag!");
125 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
130 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
132 // Only absolute symbols can be redefined.
133 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
134 "Cannot define a symbol twice!");
137 OS << ".set " << Symbol << ", " << Value << '\n';
139 // HACK: If the value isn't already absolute, set the symbol value to
140 // itself, we want to use the .set absolute value, not the actual
142 if (!Value.isAbsolute())
143 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
145 getContext().SetSymbolValue(Symbol, Value);
147 OS << Symbol << " = " << Value << '\n';
148 getContext().SetSymbolValue(Symbol, Value);
152 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
153 SymbolAttr Attribute) {
155 case Global: OS << ".globl"; break;
156 case Hidden: OS << ".hidden"; break;
157 case IndirectSymbol: OS << ".indirect_symbol"; break;
158 case Internal: OS << ".internal"; break;
159 case LazyReference: OS << ".lazy_reference"; break;
160 case NoDeadStrip: OS << ".no_dead_strip"; break;
161 case PrivateExtern: OS << ".private_extern"; break;
162 case Protected: OS << ".protected"; break;
163 case Reference: OS << ".reference"; break;
164 case Weak: OS << ".weak"; break;
165 case WeakDefinition: OS << ".weak_definition"; break;
166 case WeakReference: OS << ".weak_reference"; break;
169 OS << ' ' << Symbol << '\n';
172 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
173 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
176 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
177 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
180 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
181 unsigned Pow2Alignment) {
183 OS << ' ' << Symbol << ',' << Size;
184 if (Pow2Alignment != 0)
185 OS << ',' << Pow2Alignment;
189 void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
190 unsigned Size, unsigned Pow2Alignment) {
191 // Note: a .zerofill directive does not switch sections.
194 // This is a mach-o specific directive.
195 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
196 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
198 if (Symbol != NULL) {
199 OS << ',' << Symbol << ',' << Size;
200 if (Pow2Alignment != 0)
201 OS << ',' << Pow2Alignment;
206 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
207 assert(CurSection && "Cannot emit contents before setting section!");
208 for (unsigned i = 0, e = Data.size(); i != e; ++i)
209 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
212 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
213 assert(CurSection && "Cannot emit contents before setting section!");
214 // Need target hooks to know how to print this.
217 llvm_unreachable("Invalid size for machine code value!");
218 case 1: OS << ".byte"; break;
219 case 2: OS << ".short"; break;
220 case 4: OS << ".long"; break;
221 case 8: OS << ".quad"; break;
224 OS << ' ' << truncateToSize(Value, Size) << '\n';
227 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
229 unsigned MaxBytesToEmit) {
230 // Some assemblers don't support non-power of two alignments, so we always
231 // emit alignments as a power of two if possible.
232 if (isPowerOf2_32(ByteAlignment)) {
234 default: llvm_unreachable("Invalid size for machine code value!");
235 case 1: OS << MAI.getAlignDirective(); break;
236 // FIXME: use MAI for this!
237 case 2: OS << ".p2alignw "; break;
238 case 4: OS << ".p2alignl "; break;
239 case 8: llvm_unreachable("Unsupported alignment size!");
242 if (MAI.getAlignmentIsInBytes())
245 OS << Log2_32(ByteAlignment);
247 if (Value || MaxBytesToEmit) {
248 OS << ", " << truncateToSize(Value, ValueSize);
251 OS << ", " << MaxBytesToEmit;
257 // Non-power of two alignment. This is not widely supported by assemblers.
258 // FIXME: Parameterize this based on MAI.
260 default: llvm_unreachable("Invalid size for machine code value!");
261 case 1: OS << ".balign"; break;
262 case 2: OS << ".balignw"; break;
263 case 4: OS << ".balignl"; break;
264 case 8: llvm_unreachable("Unsupported alignment size!");
267 OS << ' ' << ByteAlignment;
268 OS << ", " << truncateToSize(Value, ValueSize);
270 OS << ", " << MaxBytesToEmit;
274 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
275 unsigned char Value) {
276 // FIXME: Verify that Offset is associated with the current section.
277 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
280 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
282 return OS << "reg:" << Op.getReg();
284 return OS << "imm:" << Op.getImm();
286 return OS << "mbblabel:("
287 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
288 assert(Op.isMCValue() && "Invalid operand!");
289 return OS << "val:" << Op.getMCValue();
292 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
293 assert(CurSection && "Cannot emit contents before setting section!");
295 // If we have an AsmPrinter, use that to print.
297 Printer->printMCInst(&Inst);
299 // Show the encoding if we have a code emitter.
301 SmallString<256> Code;
302 raw_svector_ostream VecOS(Code);
303 Emitter->EncodeInstruction(Inst, VecOS);
307 OS << " # encoding: [";
308 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
311 OS << format("%#04x", uint8_t(Code[i]));
319 // Otherwise fall back to a structural printing for now. Eventually we should
320 // always have access to the target specific printer.
322 << "opcode=" << Inst.getOpcode() << ", "
324 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
327 OS << Inst.getOperand(i);
332 void MCAsmStreamer::Finish() {
336 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
337 const MCAsmInfo &MAI, AsmPrinter *AP,
339 return new MCAsmStreamer(Context, OS, MAI, AP, CE);