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"
12 #include "llvm/CodeGen/AsmPrinter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/MC/MCSectionMachO.h"
16 #include "llvm/MC/MCSymbol.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
25 class MCAsmStreamer : public MCStreamer {
30 MCSection *CurSection;
33 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, AsmPrinter *_AsmPrinter)
34 : MCStreamer(Context), OS(_OS), Printer(_AsmPrinter), CurSection(0) {}
37 /// @name MCStreamer Interface
40 virtual void SwitchSection(MCSection *Section);
42 virtual void EmitLabel(MCSymbol *Symbol);
44 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
46 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
47 bool MakeAbsolute = false);
49 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
51 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
53 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
55 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
56 unsigned Pow2Alignment, bool IsLocal);
58 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
59 unsigned Size = 0, unsigned Pow2Alignment = 0);
61 virtual void EmitBytes(const StringRef &Data);
63 virtual void EmitValue(const MCValue &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 MCValue &Offset,
70 unsigned char Value = 0);
72 virtual void EmitInstruction(const MCInst &Inst);
74 virtual void Finish();
81 /// Allow printing symbols directly to a raw_ostream with proper quoting.
82 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
88 /// Allow printing values directly to a raw_ostream.
89 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
95 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
96 assert(Bytes && "Invalid size!");
97 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
100 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
101 return MCValue::get(Value.getSymA(), Value.getSymB(),
102 truncateToSize(Value.getConstant(), Bytes));
105 void MCAsmStreamer::SwitchSection(MCSection *Section) {
106 if (Section != CurSection) {
107 CurSection = Section;
109 // FIXME: Needs TargetAsmInfo!
110 Section->PrintSwitchToSection(*(const TargetAsmInfo*)0, OS);
114 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
115 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
116 assert(CurSection && "Cannot emit before setting section!");
117 assert(!getContext().GetSymbolValue(Symbol) &&
118 "Cannot emit symbol which was directly assigned to!");
120 OS << Symbol << ":\n";
121 Symbol->setSection(CurSection);
122 Symbol->setExternal(false);
125 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
127 default: assert(0 && "Invalid flag!");
128 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
133 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
135 assert(!Symbol->getSection() && "Cannot assign to a label!");
138 OS << ".set " << Symbol << ", " << Value << '\n';
140 // HACK: If the value isn't already absolute, set the symbol value to
141 // itself, we want to use the .set absolute value, not the actual
143 if (!Value.isAbsolute())
144 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
146 getContext().SetSymbolValue(Symbol, Value);
148 OS << Symbol << " = " << Value << '\n';
149 getContext().SetSymbolValue(Symbol, Value);
153 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
154 SymbolAttr Attribute) {
156 case Global: OS << ".globl"; break;
157 case Hidden: OS << ".hidden"; break;
158 case IndirectSymbol: OS << ".indirect_symbol"; break;
159 case Internal: OS << ".internal"; break;
160 case LazyReference: OS << ".lazy_reference"; break;
161 case NoDeadStrip: OS << ".no_dead_strip"; break;
162 case PrivateExtern: OS << ".private_extern"; break;
163 case Protected: OS << ".protected"; break;
164 case Reference: OS << ".reference"; break;
165 case Weak: OS << ".weak"; break;
166 case WeakDefinition: OS << ".weak_definition"; break;
167 case WeakReference: OS << ".weak_reference"; break;
170 OS << ' ' << Symbol << '\n';
173 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
174 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
177 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
178 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
181 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
182 unsigned Pow2Alignment, bool IsLocal) {
187 OS << ' ' << Symbol << ',' << Size;
188 if (Pow2Alignment != 0)
189 OS << ',' << Pow2Alignment;
193 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
194 unsigned Size, unsigned Pow2Alignment) {
195 // Note: a .zerofill directive does not switch sections.
198 // This is a mach-o specific directive.
199 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
200 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
202 if (Symbol != NULL) {
203 OS << ',' << Symbol << ',' << Size;
204 if (Pow2Alignment != 0)
205 OS << ',' << Pow2Alignment;
210 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
211 assert(CurSection && "Cannot emit contents before setting section!");
212 for (unsigned i = 0, e = Data.size(); i != e; ++i)
213 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
216 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
217 assert(CurSection && "Cannot emit contents before setting section!");
218 // Need target hooks to know how to print this.
221 llvm_unreachable("Invalid size for machine code value!");
222 case 1: OS << ".byte"; break;
223 case 2: OS << ".short"; break;
224 case 4: OS << ".long"; break;
225 case 8: OS << ".quad"; break;
228 OS << ' ' << truncateToSize(Value, Size) << '\n';
231 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
233 unsigned MaxBytesToEmit) {
234 // Some assemblers don't support .balign, so we always emit as .p2align if
235 // this is a power of two. Otherwise we assume the client knows the target
236 // supports .balign and use that.
237 unsigned Pow2 = Log2_32(ByteAlignment);
238 bool IsPow2 = (1U << Pow2) == ByteAlignment;
242 llvm_unreachable("Invalid size for machine code value!");
244 llvm_unreachable("Unsupported alignment size!");
245 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
246 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
247 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
250 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
252 OS << ", " << truncateToSize(Value, ValueSize);
254 OS << ", " << MaxBytesToEmit;
258 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
259 unsigned char Value) {
260 // FIXME: Verify that Offset is associated with the current section.
261 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
264 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
266 return OS << "reg:" << Op.getReg();
268 return OS << "imm:" << Op.getImm();
270 return OS << "mbblabel:("
271 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
272 assert(Op.isMCValue() && "Invalid operand!");
273 return OS << "val:" << Op.getMCValue();
276 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
277 assert(CurSection && "Cannot emit contents before setting section!");
279 // If we have an AsmPrinter, use that to print.
281 Printer->printMCInst(&Inst);
285 // Otherwise fall back to a structural printing for now. Eventually we should
286 // always have access to the target specific printer.
288 << "opcode=" << Inst.getOpcode() << ", "
290 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
293 OS << Inst.getOperand(i);
298 void MCAsmStreamer::Finish() {
302 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
304 return new MCAsmStreamer(Context, OS, AP);