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 EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
51 unsigned Pow2Alignment, bool IsLocal);
53 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
54 unsigned Size = 0, unsigned Pow2Alignment = 0);
56 virtual void AbortAssembly(const char *AbortReason = NULL);
58 virtual void EmitBytes(const char *Data, unsigned Length);
60 virtual void EmitValue(const MCValue &Value, unsigned Size);
62 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
63 unsigned ValueSize = 1,
64 unsigned MaxBytesToEmit = 0);
66 virtual void EmitValueToOffset(const MCValue &Offset,
67 unsigned char Value = 0);
69 virtual void EmitInstruction(const MCInst &Inst);
71 virtual void Finish();
78 /// Allow printing values directly to a raw_ostream.
79 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
80 if (Value.getSymA()) {
81 os << Value.getSymA()->getName();
83 os << " - " << Value.getSymB()->getName();
84 if (Value.getConstant())
85 os << " + " << Value.getConstant();
87 assert(!Value.getSymB() && "Invalid machine code value!");
88 os << Value.getConstant();
94 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
95 assert(Bytes && "Invalid size!");
96 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
99 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
100 return MCValue::get(Value.getSymA(), Value.getSymB(),
101 truncateToSize(Value.getConstant(), Bytes));
104 void MCAsmStreamer::SwitchSection(MCSection *Section) {
105 if (Section != CurSection) {
106 CurSection = Section;
108 // FIXME: Really we would like the segment, flags, etc. to be separate
109 // values instead of embedded in the name. Not all assemblers understand all
110 // this stuff though.
111 OS << ".section " << Section->getName() << "\n";
115 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
116 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
117 assert(CurSection && "Cannot emit before setting section!");
118 assert(!getContext().GetSymbolValue(Symbol) &&
119 "Cannot emit symbol which was directly assigned to!");
121 OS << Symbol->getName() << ":\n";
122 Symbol->setSection(CurSection);
123 Symbol->setExternal(false);
126 void MCAsmStreamer::SubsectionsViaSymbols(void) {
127 OS << ".subsections_via_symbols\n";
130 void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
132 if (AbortReason != NULL)
133 OS << ' ' << AbortReason;
138 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
140 assert(!Symbol->getSection() && "Cannot assign to a label!");
143 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
145 OS << Symbol->getName() << " = " << Value << '\n';
148 getContext().SetSymbolValue(Symbol, Value);
151 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
152 SymbolAttr Attribute) {
154 case Global: OS << ".globl"; break;
155 case Hidden: OS << ".hidden"; break;
156 case IndirectSymbol: OS << ".indirect_symbol"; break;
157 case Internal: OS << ".internal"; break;
158 case LazyReference: OS << ".lazy_reference"; break;
159 case NoDeadStrip: OS << ".no_dead_strip"; break;
160 case PrivateExtern: OS << ".private_extern"; break;
161 case Protected: OS << ".protected"; break;
162 case Reference: OS << ".reference"; break;
163 case Weak: OS << ".weak"; break;
164 case WeakDefinition: OS << ".weak_definition"; break;
165 case WeakReference: OS << ".weak_reference"; break;
168 OS << ' ' << Symbol->getName() << '\n';
171 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
172 OS << ".desc" << ' ' << Symbol->getName() << ',' << DescValue << '\n';
175 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
176 unsigned Pow2Alignment, bool IsLocal) {
181 OS << ' ' << Symbol->getName() << ',' << Size;
182 if (Pow2Alignment != 0)
183 OS << ',' << Pow2Alignment;
187 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
188 unsigned Size, unsigned Pow2Alignment) {
189 // Note: a .zerofill directive does not switch sections
190 // FIXME: Really we would like the segment and section names as well as the
191 // section type to be separate values instead of embedded in the name. Not
192 // all assemblers understand all this stuff though.
193 OS << ".zerofill " << Section->getName();
194 if (Symbol != NULL) {
195 OS << ',' << Symbol->getName() << ',' << Size;
196 if (Pow2Alignment != 0)
197 OS << ',' << Pow2Alignment;
202 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
203 assert(CurSection && "Cannot emit contents before setting section!");
204 for (unsigned i = 0; i != Length; ++i)
205 OS << ".byte " << (unsigned) 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!");
270 // FIXME: Implement proper printing.
272 << "opcode=" << Inst.getOpcode() << ", "
274 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
277 OS << Inst.getOperand(i);
282 void MCAsmStreamer::Finish() {
286 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
287 return new MCAsmStreamer(Context, OS);