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/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
23 class MCAsmStreamer : public MCStreamer {
25 const TargetAsmInfo &TAI;
28 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const TargetAsmInfo &tai,
29 AsmPrinter *_AsmPrinter)
30 : MCStreamer(Context), OS(_OS), TAI(tai), Printer(_AsmPrinter) {}
33 /// @name MCStreamer Interface
36 virtual void SwitchSection(const MCSection *Section);
38 virtual void EmitLabel(MCSymbol *Symbol);
40 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
42 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
43 bool MakeAbsolute = false);
45 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
47 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
49 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
51 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
52 unsigned Pow2Alignment, bool IsLocal);
54 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
55 unsigned Size = 0, unsigned Pow2Alignment = 0);
57 virtual void EmitBytes(const StringRef &Data);
59 virtual void EmitValue(const MCValue &Value, unsigned Size);
61 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
62 unsigned ValueSize = 1,
63 unsigned MaxBytesToEmit = 0);
65 virtual void EmitValueToOffset(const MCValue &Offset,
66 unsigned char Value = 0);
68 virtual void EmitInstruction(const MCInst &Inst);
70 virtual void Finish();
75 } // end anonymous namespace.
77 /// Allow printing symbols directly to a raw_ostream with proper quoting.
78 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
83 /// Allow printing values directly to a raw_ostream.
84 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
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 MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
95 return MCValue::get(Value.getSymA(), Value.getSymB(),
96 truncateToSize(Value.getConstant(), Bytes));
99 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
100 if (Section != CurSection) {
101 CurSection = Section;
102 Section->PrintSwitchToSection(TAI, OS);
106 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
107 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
108 assert(CurSection && "Cannot emit before setting section!");
109 assert(!getContext().GetSymbolValue(Symbol) &&
110 "Cannot emit symbol which was directly assigned to!");
112 OS << Symbol << ":\n";
113 Symbol->setSection(CurSection);
114 Symbol->setExternal(false);
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 assert(!Symbol->getSection() && "Cannot assign to a label!");
130 OS << ".set " << Symbol << ", " << Value << '\n';
132 // HACK: If the value isn't already absolute, set the symbol value to
133 // itself, we want to use the .set absolute value, not the actual
135 if (!Value.isAbsolute())
136 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
138 getContext().SetSymbolValue(Symbol, Value);
140 OS << Symbol << " = " << Value << '\n';
141 getContext().SetSymbolValue(Symbol, Value);
145 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
146 SymbolAttr Attribute) {
148 case Global: OS << ".globl"; break;
149 case Hidden: OS << ".hidden"; break;
150 case IndirectSymbol: OS << ".indirect_symbol"; break;
151 case Internal: OS << ".internal"; break;
152 case LazyReference: OS << ".lazy_reference"; break;
153 case NoDeadStrip: OS << ".no_dead_strip"; break;
154 case PrivateExtern: OS << ".private_extern"; break;
155 case Protected: OS << ".protected"; break;
156 case Reference: OS << ".reference"; break;
157 case Weak: OS << ".weak"; break;
158 case WeakDefinition: OS << ".weak_definition"; break;
159 case WeakReference: OS << ".weak_reference"; break;
162 OS << ' ' << Symbol << '\n';
165 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
166 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
169 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
170 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
173 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
174 unsigned Pow2Alignment, bool IsLocal) {
179 OS << ' ' << Symbol << ',' << Size;
180 if (Pow2Alignment != 0)
181 OS << ',' << Pow2Alignment;
185 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
186 unsigned Size, unsigned Pow2Alignment) {
187 // Note: a .zerofill directive does not switch sections.
190 // This is a mach-o specific directive.
191 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
192 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
194 if (Symbol != NULL) {
195 OS << ',' << Symbol << ',' << Size;
196 if (Pow2Alignment != 0)
197 OS << ',' << Pow2Alignment;
202 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
203 assert(CurSection && "Cannot emit contents before setting section!");
204 for (unsigned i = 0, e = Data.size(); i != e; ++i)
205 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
208 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
209 assert(CurSection && "Cannot emit contents before setting section!");
210 // Need target hooks to know how to print this.
213 llvm_unreachable("Invalid size for machine code value!");
214 case 1: OS << ".byte"; break;
215 case 2: OS << ".short"; break;
216 case 4: OS << ".long"; break;
217 case 8: OS << ".quad"; break;
220 OS << ' ' << truncateToSize(Value, Size) << '\n';
223 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
225 unsigned MaxBytesToEmit) {
226 // Some assemblers don't support .balign, so we always emit as .p2align if
227 // this is a power of two. Otherwise we assume the client knows the target
228 // supports .balign and use that.
229 unsigned Pow2 = Log2_32(ByteAlignment);
230 bool IsPow2 = (1U << Pow2) == ByteAlignment;
234 llvm_unreachable("Invalid size for machine code value!");
236 llvm_unreachable("Unsupported alignment size!");
237 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
238 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
239 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
242 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
244 OS << ", " << truncateToSize(Value, ValueSize);
246 OS << ", " << MaxBytesToEmit;
250 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
251 unsigned char Value) {
252 // FIXME: Verify that Offset is associated with the current section.
253 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
256 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
258 return OS << "reg:" << Op.getReg();
260 return OS << "imm:" << Op.getImm();
262 return OS << "mbblabel:("
263 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
264 assert(Op.isMCValue() && "Invalid operand!");
265 return OS << "val:" << Op.getMCValue();
268 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
269 assert(CurSection && "Cannot emit contents before setting section!");
271 // If we have an AsmPrinter, use that to print.
273 Printer->printMCInst(&Inst);
277 // Otherwise fall back to a structural printing for now. Eventually we should
278 // always have access to the target specific printer.
280 << "opcode=" << Inst.getOpcode() << ", "
282 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
285 OS << Inst.getOperand(i);
290 void MCAsmStreamer::Finish() {
294 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
295 const TargetAsmInfo &TAI, AsmPrinter *AP) {
296 return new MCAsmStreamer(Context, OS, TAI, AP);