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 DumpSymbolsandMacros(const char *FileName);
62 virtual void LoadSymbolsandMacros(const char *FileName);
64 virtual void EmitBytes(const char *Data, unsigned Length);
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();
84 /// Allow printing values directly to a raw_ostream.
85 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
86 if (Value.getSymA()) {
87 os << Value.getSymA()->getName();
89 os << " - " << Value.getSymB()->getName();
90 if (Value.getConstant())
91 os << " + " << Value.getConstant();
93 assert(!Value.getSymB() && "Invalid machine code value!");
94 os << Value.getConstant();
100 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
101 assert(Bytes && "Invalid size!");
102 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
105 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
106 return MCValue::get(Value.getSymA(), Value.getSymB(),
107 truncateToSize(Value.getConstant(), Bytes));
110 void MCAsmStreamer::SwitchSection(MCSection *Section) {
111 if (Section != CurSection) {
112 CurSection = Section;
114 // FIXME: Really we would like the segment, flags, etc. to be separate
115 // values instead of embedded in the name. Not all assemblers understand all
116 // this stuff though.
117 OS << ".section " << Section->getName() << "\n";
121 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
122 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
123 assert(CurSection && "Cannot emit before setting section!");
124 assert(!getContext().GetSymbolValue(Symbol) &&
125 "Cannot emit symbol which was directly assigned to!");
127 OS << Symbol->getName() << ":\n";
128 Symbol->setSection(CurSection);
129 Symbol->setExternal(false);
132 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
134 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
139 void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
141 if (AbortReason != NULL)
142 OS << ' ' << AbortReason;
147 void MCAsmStreamer::DumpSymbolsandMacros(const char *FileName) {
148 OS << ".dump" << ' ' << FileName << '\n';
151 void MCAsmStreamer::LoadSymbolsandMacros(const char *FileName) {
152 OS << ".load" << ' ' << FileName << '\n';
155 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
157 assert(!Symbol->getSection() && "Cannot assign to a label!");
160 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
162 OS << Symbol->getName() << " = " << Value << '\n';
165 getContext().SetSymbolValue(Symbol, Value);
168 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
169 SymbolAttr Attribute) {
171 case Global: OS << ".globl"; break;
172 case Hidden: OS << ".hidden"; break;
173 case IndirectSymbol: OS << ".indirect_symbol"; break;
174 case Internal: OS << ".internal"; break;
175 case LazyReference: OS << ".lazy_reference"; break;
176 case NoDeadStrip: OS << ".no_dead_strip"; break;
177 case PrivateExtern: OS << ".private_extern"; break;
178 case Protected: OS << ".protected"; break;
179 case Reference: OS << ".reference"; break;
180 case Weak: OS << ".weak"; break;
181 case WeakDefinition: OS << ".weak_definition"; break;
182 case WeakReference: OS << ".weak_reference"; break;
185 OS << ' ' << Symbol->getName() << '\n';
188 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
189 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
192 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
193 OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
196 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
197 unsigned Pow2Alignment, bool IsLocal) {
202 OS << ' ' << Symbol->getName() << ',' << Size;
203 if (Pow2Alignment != 0)
204 OS << ',' << Pow2Alignment;
208 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
209 unsigned Size, unsigned Pow2Alignment) {
210 // Note: a .zerofill directive does not switch sections
211 // FIXME: Really we would like the segment and section names as well as the
212 // section type to be separate values instead of embedded in the name. Not
213 // all assemblers understand all this stuff though.
214 OS << ".zerofill " << Section->getName();
215 if (Symbol != NULL) {
216 OS << ',' << Symbol->getName() << ',' << Size;
217 if (Pow2Alignment != 0)
218 OS << ',' << Pow2Alignment;
223 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
224 assert(CurSection && "Cannot emit contents before setting section!");
225 for (unsigned i = 0; i != Length; ++i)
226 OS << ".byte " << (unsigned) Data[i] << '\n';
229 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
230 assert(CurSection && "Cannot emit contents before setting section!");
231 // Need target hooks to know how to print this.
234 llvm_unreachable("Invalid size for machine code value!");
235 case 1: OS << ".byte"; break;
236 case 2: OS << ".short"; break;
237 case 4: OS << ".long"; break;
238 case 8: OS << ".quad"; break;
241 OS << ' ' << truncateToSize(Value, Size) << '\n';
244 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
246 unsigned MaxBytesToEmit) {
247 // Some assemblers don't support .balign, so we always emit as .p2align if
248 // this is a power of two. Otherwise we assume the client knows the target
249 // supports .balign and use that.
250 unsigned Pow2 = Log2_32(ByteAlignment);
251 bool IsPow2 = (1U << Pow2) == ByteAlignment;
255 llvm_unreachable("Invalid size for machine code value!");
257 llvm_unreachable("Unsupported alignment size!");
258 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
259 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
260 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
263 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
265 OS << ", " << truncateToSize(Value, ValueSize);
267 OS << ", " << MaxBytesToEmit;
271 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
272 unsigned char Value) {
273 // FIXME: Verify that Offset is associated with the current section.
274 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
277 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
279 return OS << "reg:" << Op.getReg();
281 return OS << "imm:" << Op.getImm();
283 return OS << "mbblabel:("
284 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
285 assert(Op.isMCValue() && "Invalid operand!");
286 return OS << "val:" << Op.getMCValue();
289 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
290 assert(CurSection && "Cannot emit contents before setting section!");
291 // FIXME: Implement proper printing.
293 << "opcode=" << Inst.getOpcode() << ", "
295 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
298 OS << Inst.getOperand(i);
303 void MCAsmStreamer::Finish() {
307 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
308 return new MCAsmStreamer(Context, OS);