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 SubsectionsViaSymbols(void);
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 SwitchInputAssemblyFile(const char *FileName);
62 virtual void DumpSymbolsandMacros(const char *FileName);
64 virtual void LoadSymbolsandMacros(const char *FileName);
66 virtual void EmitBytes(const char *Data, unsigned Length);
68 virtual void EmitValue(const MCValue &Value, unsigned Size);
70 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
71 unsigned ValueSize = 1,
72 unsigned MaxBytesToEmit = 0);
74 virtual void EmitValueToOffset(const MCValue &Offset,
75 unsigned char Value = 0);
77 virtual void EmitInstruction(const MCInst &Inst);
79 virtual void Finish();
86 /// Allow printing values directly to a raw_ostream.
87 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
88 if (Value.getSymA()) {
89 os << Value.getSymA()->getName();
91 os << " - " << Value.getSymB()->getName();
92 if (Value.getConstant())
93 os << " + " << Value.getConstant();
95 assert(!Value.getSymB() && "Invalid machine code value!");
96 os << Value.getConstant();
102 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
103 assert(Bytes && "Invalid size!");
104 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
107 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
108 return MCValue::get(Value.getSymA(), Value.getSymB(),
109 truncateToSize(Value.getConstant(), Bytes));
112 void MCAsmStreamer::SwitchSection(MCSection *Section) {
113 if (Section != CurSection) {
114 CurSection = Section;
116 // FIXME: Really we would like the segment, flags, etc. to be separate
117 // values instead of embedded in the name. Not all assemblers understand all
118 // this stuff though.
119 OS << ".section " << Section->getName() << "\n";
123 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
124 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
125 assert(CurSection && "Cannot emit before setting section!");
126 assert(!getContext().GetSymbolValue(Symbol) &&
127 "Cannot emit symbol which was directly assigned to!");
129 OS << Symbol->getName() << ":\n";
130 Symbol->setSection(CurSection);
131 Symbol->setExternal(false);
134 void MCAsmStreamer::SubsectionsViaSymbols(void) {
135 OS << ".subsections_via_symbols\n";
138 void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
140 if (AbortReason != NULL)
141 OS << ' ' << AbortReason;
146 void MCAsmStreamer::SwitchInputAssemblyFile(const char *FileName) {
147 OS << ".include" << ' ' << FileName << '\n';
150 void MCAsmStreamer::DumpSymbolsandMacros(const char *FileName) {
151 OS << ".dump" << ' ' << FileName << '\n';
154 void MCAsmStreamer::LoadSymbolsandMacros(const char *FileName) {
155 OS << ".load" << ' ' << FileName << '\n';
158 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
160 assert(!Symbol->getSection() && "Cannot assign to a label!");
163 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
165 OS << Symbol->getName() << " = " << Value << '\n';
168 getContext().SetSymbolValue(Symbol, Value);
171 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
172 SymbolAttr Attribute) {
174 case Global: OS << ".globl"; break;
175 case Hidden: OS << ".hidden"; break;
176 case IndirectSymbol: OS << ".indirect_symbol"; break;
177 case Internal: OS << ".internal"; break;
178 case LazyReference: OS << ".lazy_reference"; break;
179 case NoDeadStrip: OS << ".no_dead_strip"; break;
180 case PrivateExtern: OS << ".private_extern"; break;
181 case Protected: OS << ".protected"; break;
182 case Reference: OS << ".reference"; break;
183 case Weak: OS << ".weak"; break;
184 case WeakDefinition: OS << ".weak_definition"; break;
185 case WeakReference: OS << ".weak_reference"; break;
188 OS << ' ' << Symbol->getName() << '\n';
191 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
192 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
195 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
196 OS << ".lsym" << ' ' << Symbol->getName() << ',' << Value << '\n';
199 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
200 unsigned Pow2Alignment, bool IsLocal) {
205 OS << ' ' << Symbol->getName() << ',' << Size;
206 if (Pow2Alignment != 0)
207 OS << ',' << Pow2Alignment;
211 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
212 unsigned Size, unsigned Pow2Alignment) {
213 // Note: a .zerofill directive does not switch sections
214 // FIXME: Really we would like the segment and section names as well as the
215 // section type to be separate values instead of embedded in the name. Not
216 // all assemblers understand all this stuff though.
217 OS << ".zerofill " << Section->getName();
218 if (Symbol != NULL) {
219 OS << ',' << Symbol->getName() << ',' << Size;
220 if (Pow2Alignment != 0)
221 OS << ',' << Pow2Alignment;
226 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
227 assert(CurSection && "Cannot emit contents before setting section!");
228 for (unsigned i = 0; i != Length; ++i)
229 OS << ".byte " << (unsigned) Data[i] << '\n';
232 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
233 assert(CurSection && "Cannot emit contents before setting section!");
234 // Need target hooks to know how to print this.
237 llvm_unreachable("Invalid size for machine code value!");
238 case 1: OS << ".byte"; break;
239 case 2: OS << ".short"; break;
240 case 4: OS << ".long"; break;
241 case 8: OS << ".quad"; break;
244 OS << ' ' << truncateToSize(Value, Size) << '\n';
247 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
249 unsigned MaxBytesToEmit) {
250 // Some assemblers don't support .balign, so we always emit as .p2align if
251 // this is a power of two. Otherwise we assume the client knows the target
252 // supports .balign and use that.
253 unsigned Pow2 = Log2_32(ByteAlignment);
254 bool IsPow2 = (1U << Pow2) == ByteAlignment;
258 llvm_unreachable("Invalid size for machine code value!");
260 llvm_unreachable("Unsupported alignment size!");
261 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
262 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
263 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
266 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
268 OS << ", " << truncateToSize(Value, ValueSize);
270 OS << ", " << MaxBytesToEmit;
274 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
275 unsigned char Value) {
276 // FIXME: Verify that Offset is associated with the current section.
277 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
280 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
282 return OS << "reg:" << Op.getReg();
284 return OS << "imm:" << Op.getImm();
286 return OS << "mbblabel:("
287 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
288 assert(Op.isMCValue() && "Invalid operand!");
289 return OS << "val:" << Op.getMCValue();
292 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
293 assert(CurSection && "Cannot emit contents before setting section!");
294 // FIXME: Implement proper printing.
296 << "opcode=" << Inst.getOpcode() << ", "
298 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
301 OS << Inst.getOperand(i);
306 void MCAsmStreamer::Finish() {
310 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
311 return new MCAsmStreamer(Context, OS);