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/MC/MCContext.h"
13 #include "llvm/MC/MCInst.h"
14 #include "llvm/MC/MCSection.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"
24 class MCAsmStreamer : public MCStreamer {
27 MCSection *CurSection;
30 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
31 : MCStreamer(Context), OS(_OS), CurSection(0) {}
34 /// @name MCStreamer Interface
37 virtual void SwitchSection(MCSection *Section);
39 virtual void EmitLabel(MCSymbol *Symbol);
41 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
43 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
44 bool MakeAbsolute = false);
46 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
48 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
50 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
52 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
53 unsigned Pow2Alignment, bool IsLocal);
55 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
56 unsigned Size = 0, unsigned Pow2Alignment = 0);
58 virtual void AbortAssembly(const char *AbortReason = NULL);
60 virtual void EmitBytes(const StringRef &Data);
62 virtual void EmitValue(const MCValue &Value, unsigned Size);
64 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
65 unsigned ValueSize = 1,
66 unsigned MaxBytesToEmit = 0);
68 virtual void EmitValueToOffset(const MCValue &Offset,
69 unsigned char Value = 0);
71 virtual void EmitInstruction(const MCInst &Inst);
73 virtual void Finish();
80 /// Allow printing values directly to a raw_ostream.
81 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
82 if (Value.getSymA()) {
83 os << Value.getSymA()->getName();
85 os << " - " << Value.getSymB()->getName();
86 if (Value.getConstant())
87 os << " + " << Value.getConstant();
89 assert(!Value.getSymB() && "Invalid machine code value!");
90 os << Value.getConstant();
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(MCSection *Section) {
107 if (Section != CurSection) {
108 CurSection = Section;
110 // FIXME: Really we would like the segment, flags, etc. to be separate
111 // values instead of embedded in the name. Not all assemblers understand all
112 // this stuff though.
113 OS << ".section " << Section->getName() << "\n";
117 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
118 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
119 assert(CurSection && "Cannot emit before setting section!");
120 assert(!getContext().GetSymbolValue(Symbol) &&
121 "Cannot emit symbol which was directly assigned to!");
123 OS << Symbol->getName() << ":\n";
124 Symbol->setSection(CurSection);
125 Symbol->setExternal(false);
128 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
130 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
135 void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
137 if (AbortReason != NULL)
138 OS << ' ' << AbortReason;
143 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
145 assert(!Symbol->getSection() && "Cannot assign to a label!");
148 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
150 OS << Symbol->getName() << " = " << Value << '\n';
153 getContext().SetSymbolValue(Symbol, Value);
156 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
157 SymbolAttr Attribute) {
159 case Global: OS << ".globl"; break;
160 case Hidden: OS << ".hidden"; break;
161 case IndirectSymbol: OS << ".indirect_symbol"; break;
162 case Internal: OS << ".internal"; break;
163 case LazyReference: OS << ".lazy_reference"; break;
164 case NoDeadStrip: OS << ".no_dead_strip"; break;
165 case PrivateExtern: OS << ".private_extern"; break;
166 case Protected: OS << ".protected"; break;
167 case Reference: OS << ".reference"; break;
168 case Weak: OS << ".weak"; break;
169 case WeakDefinition: OS << ".weak_definition"; break;
170 case WeakReference: OS << ".weak_reference"; break;
173 OS << ' ' << Symbol->getName() << '\n';
176 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
177 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
180 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
181 OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
184 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
185 unsigned Pow2Alignment, bool IsLocal) {
190 OS << ' ' << Symbol->getName() << ',' << Size;
191 if (Pow2Alignment != 0)
192 OS << ',' << Pow2Alignment;
196 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
197 unsigned Size, unsigned Pow2Alignment) {
198 // Note: a .zerofill directive does not switch sections
199 // FIXME: Really we would like the segment and section names as well as the
200 // section type to be separate values instead of embedded in the name. Not
201 // all assemblers understand all this stuff though.
202 OS << ".zerofill " << Section->getName();
203 if (Symbol != NULL) {
204 OS << ',' << Symbol->getName() << ',' << Size;
205 if (Pow2Alignment != 0)
206 OS << ',' << Pow2Alignment;
211 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
212 assert(CurSection && "Cannot emit contents before setting section!");
213 for (unsigned i = 0, e = Data.size(); i != e; ++i)
214 OS << ".byte " << (unsigned) Data[i] << '\n';
217 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
218 assert(CurSection && "Cannot emit contents before setting section!");
219 // Need target hooks to know how to print this.
222 llvm_unreachable("Invalid size for machine code value!");
223 case 1: OS << ".byte"; break;
224 case 2: OS << ".short"; break;
225 case 4: OS << ".long"; break;
226 case 8: OS << ".quad"; break;
229 OS << ' ' << truncateToSize(Value, Size) << '\n';
232 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
234 unsigned MaxBytesToEmit) {
235 // Some assemblers don't support .balign, so we always emit as .p2align if
236 // this is a power of two. Otherwise we assume the client knows the target
237 // supports .balign and use that.
238 unsigned Pow2 = Log2_32(ByteAlignment);
239 bool IsPow2 = (1U << Pow2) == ByteAlignment;
243 llvm_unreachable("Invalid size for machine code value!");
245 llvm_unreachable("Unsupported alignment size!");
246 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
247 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
248 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
251 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
253 OS << ", " << truncateToSize(Value, ValueSize);
255 OS << ", " << MaxBytesToEmit;
259 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
260 unsigned char Value) {
261 // FIXME: Verify that Offset is associated with the current section.
262 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
265 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
267 return OS << "reg:" << Op.getReg();
269 return OS << "imm:" << Op.getImm();
271 return OS << "mbblabel:("
272 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
273 assert(Op.isMCValue() && "Invalid operand!");
274 return OS << "val:" << Op.getMCValue();
277 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
278 assert(CurSection && "Cannot emit contents before setting section!");
279 // FIXME: Implement proper printing.
281 << "opcode=" << Inst.getOpcode() << ", "
283 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
286 OS << Inst.getOperand(i);
291 void MCAsmStreamer::Finish() {
295 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
296 return new MCAsmStreamer(Context, OS);