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/CodeGen/AsmPrinter.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSectionMachO.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCValue.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MathExtras.h"
20 #include "llvm/Support/raw_ostream.h"
25 class MCAsmStreamer : public MCStreamer {
30 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
31 AsmPrinter *_AsmPrinter)
32 : MCStreamer(Context), OS(_OS), TAI(tai), Printer(_AsmPrinter) {}
35 /// @name MCStreamer Interface
38 virtual void SwitchSection(const MCSection *Section);
40 virtual void EmitLabel(MCSymbol *Symbol);
42 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
44 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
45 bool MakeAbsolute = false);
47 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
49 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
51 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
53 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
54 unsigned Pow2Alignment, bool IsLocal);
56 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
57 unsigned Size = 0, unsigned Pow2Alignment = 0);
59 virtual void EmitBytes(const StringRef &Data);
61 virtual void EmitValue(const MCValue &Value, unsigned Size);
63 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
64 unsigned ValueSize = 1,
65 unsigned MaxBytesToEmit = 0);
67 virtual void EmitValueToOffset(const MCValue &Offset,
68 unsigned char Value = 0);
70 virtual void EmitInstruction(const MCInst &Inst);
72 virtual void Finish();
77 } // end anonymous namespace.
79 /// Allow printing symbols directly to a raw_ostream with proper quoting.
80 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
85 /// Allow printing values directly to a raw_ostream.
86 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
91 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
92 assert(Bytes && "Invalid size!");
93 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
96 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
97 return MCValue::get(Value.getSymA(), Value.getSymB(),
98 truncateToSize(Value.getConstant(), Bytes));
101 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
102 assert(Section && "Cannot switch to a null section!");
103 if (Section != CurSection) {
104 CurSection = Section;
105 Section->PrintSwitchToSection(TAI, OS);
109 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
110 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
111 assert(CurSection && "Cannot emit before setting section!");
113 OS << Symbol << ":\n";
114 Symbol->setSection(*CurSection);
117 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
119 default: assert(0 && "Invalid flag!");
120 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
125 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
127 // Only absolute symbols can be redefined.
128 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
129 "Cannot define a symbol twice!");
132 OS << ".set " << Symbol << ", " << Value << '\n';
134 // HACK: If the value isn't already absolute, set the symbol value to
135 // itself, we want to use the .set absolute value, not the actual
137 if (!Value.isAbsolute())
138 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
140 getContext().SetSymbolValue(Symbol, Value);
142 OS << Symbol << " = " << Value << '\n';
143 getContext().SetSymbolValue(Symbol, Value);
147 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
148 SymbolAttr Attribute) {
150 case Global: OS << ".globl"; break;
151 case Hidden: OS << ".hidden"; break;
152 case IndirectSymbol: OS << ".indirect_symbol"; break;
153 case Internal: OS << ".internal"; break;
154 case LazyReference: OS << ".lazy_reference"; break;
155 case NoDeadStrip: OS << ".no_dead_strip"; break;
156 case PrivateExtern: OS << ".private_extern"; break;
157 case Protected: OS << ".protected"; break;
158 case Reference: OS << ".reference"; break;
159 case Weak: OS << ".weak"; break;
160 case WeakDefinition: OS << ".weak_definition"; break;
161 case WeakReference: OS << ".weak_reference"; break;
164 OS << ' ' << Symbol << '\n';
167 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
168 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
171 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
172 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
175 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
176 unsigned Pow2Alignment, bool IsLocal) {
181 OS << ' ' << Symbol << ',' << Size;
182 if (Pow2Alignment != 0)
183 OS << ',' << Pow2Alignment;
187 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
188 unsigned Size, unsigned Pow2Alignment) {
189 // Note: a .zerofill directive does not switch sections.
192 // This is a mach-o specific directive.
193 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
194 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
196 if (Symbol != NULL) {
197 OS << ',' << Symbol << ',' << Size;
198 if (Pow2Alignment != 0)
199 OS << ',' << Pow2Alignment;
204 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
205 assert(CurSection && "Cannot emit contents before setting section!");
206 for (unsigned i = 0, e = Data.size(); i != e; ++i)
207 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
210 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
211 assert(CurSection && "Cannot emit contents before setting section!");
212 // Need target hooks to know how to print this.
215 llvm_unreachable("Invalid size for machine code value!");
216 case 1: OS << ".byte"; break;
217 case 2: OS << ".short"; break;
218 case 4: OS << ".long"; break;
219 case 8: OS << ".quad"; break;
222 OS << ' ' << truncateToSize(Value, Size) << '\n';
225 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
227 unsigned MaxBytesToEmit) {
228 // Some assemblers don't support non-power of two alignments, so we always
229 // emit alignments as a power of two if possible.
230 if (isPowerOf2_32(ByteAlignment)) {
232 default: llvm_unreachable("Invalid size for machine code value!");
233 case 1: OS << TAI.getAlignDirective(); break;
234 // FIXME: use TAI for this!
235 case 2: OS << ".p2alignw "; break;
236 case 4: OS << ".p2alignl "; break;
237 case 8: llvm_unreachable("Unsupported alignment size!");
240 if (TAI.getAlignmentIsInBytes())
243 OS << Log2_32(ByteAlignment);
245 if (Value || MaxBytesToEmit) {
246 OS << ", " << truncateToSize(Value, ValueSize);
249 OS << ", " << MaxBytesToEmit;
255 // Non-power of two alignment. This is not widely supported by assemblers.
256 // FIXME: Parameterize this based on TAI.
258 default: llvm_unreachable("Invalid size for machine code value!");
259 case 1: OS << ".balign"; break;
260 case 2: OS << ".balignw"; break;
261 case 4: OS << ".balignl"; break;
262 case 8: llvm_unreachable("Unsupported alignment size!");
265 OS << ' ' << ByteAlignment;
266 OS << ", " << truncateToSize(Value, ValueSize);
268 OS << ", " << MaxBytesToEmit;
272 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
273 unsigned char Value) {
274 // FIXME: Verify that Offset is associated with the current section.
275 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
278 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
280 return OS << "reg:" << Op.getReg();
282 return OS << "imm:" << Op.getImm();
284 return OS << "mbblabel:("
285 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
286 assert(Op.isMCValue() && "Invalid operand!");
287 return OS << "val:" << Op.getMCValue();
290 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
291 assert(CurSection && "Cannot emit contents before setting section!");
293 // If we have an AsmPrinter, use that to print.
295 Printer->printMCInst(&Inst);
299 // Otherwise fall back to a structural printing for now. Eventually we should
300 // always have access to the target specific printer.
302 << "opcode=" << Inst.getOpcode() << ", "
304 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
307 OS << Inst.getOperand(i);
312 void MCAsmStreamer::Finish() {
316 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
317 const MCAsmInfo &TAI, AsmPrinter *AP) {
318 return new MCAsmStreamer(Context, OS, TAI, AP);