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 EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
49 unsigned Pow2Alignment, bool IsLocal);
51 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
52 unsigned Size = 0, unsigned Pow2Alignment = 0);
54 virtual void AbortAssembly(const char *AbortReason = NULL);
56 virtual void EmitBytes(const char *Data, unsigned Length);
58 virtual void EmitValue(const MCValue &Value, unsigned Size);
60 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
61 unsigned ValueSize = 1,
62 unsigned MaxBytesToEmit = 0);
64 virtual void EmitValueToOffset(const MCValue &Offset,
65 unsigned char Value = 0);
67 virtual void EmitInstruction(const MCInst &Inst);
69 virtual void Finish();
76 /// Allow printing values directly to a raw_ostream.
77 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
78 if (Value.getSymA()) {
79 os << Value.getSymA()->getName();
81 os << " - " << Value.getSymB()->getName();
82 if (Value.getConstant())
83 os << " + " << Value.getConstant();
85 assert(!Value.getSymB() && "Invalid machine code value!");
86 os << Value.getConstant();
92 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
93 assert(Bytes && "Invalid size!");
94 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
97 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
98 return MCValue::get(Value.getSymA(), Value.getSymB(),
99 truncateToSize(Value.getConstant(), Bytes));
102 void MCAsmStreamer::SwitchSection(MCSection *Section) {
103 if (Section != CurSection) {
104 CurSection = Section;
106 // FIXME: Really we would like the segment, flags, etc. to be separate
107 // values instead of embedded in the name. Not all assemblers understand all
108 // this stuff though.
109 OS << ".section " << Section->getName() << "\n";
113 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
114 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
115 assert(CurSection && "Cannot emit before setting section!");
116 assert(!getContext().GetSymbolValue(Symbol) &&
117 "Cannot emit symbol which was directly assigned to!");
119 OS << Symbol->getName() << ":\n";
120 Symbol->setSection(CurSection);
121 Symbol->setExternal(false);
124 void MCAsmStreamer::SubsectionsViaSymbols(void) {
125 OS << ".subsections_via_symbols\n";
128 void MCAsmStreamer::AbortAssembly(const char *AbortReason) {
130 if (AbortReason != NULL)
131 OS << ' ' << AbortReason;
136 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
138 assert(!Symbol->getSection() && "Cannot assign to a label!");
141 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
143 OS << Symbol->getName() << " = " << Value << '\n';
146 getContext().SetSymbolValue(Symbol, Value);
149 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
150 SymbolAttr Attribute) {
152 case Global: OS << ".globl"; break;
153 case Hidden: OS << ".hidden"; break;
154 case IndirectSymbol: OS << ".indirect_symbol"; break;
155 case Internal: OS << ".internal"; break;
156 case LazyReference: OS << ".lazy_reference"; break;
157 case NoDeadStrip: OS << ".no_dead_strip"; break;
158 case PrivateExtern: OS << ".private_extern"; break;
159 case Protected: OS << ".protected"; break;
160 case Reference: OS << ".reference"; break;
161 case Weak: OS << ".weak"; break;
162 case WeakDefinition: OS << ".weak_definition"; break;
163 case WeakReference: OS << ".weak_reference"; break;
166 OS << ' ' << Symbol->getName() << '\n';
169 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
170 unsigned Pow2Alignment, bool IsLocal) {
175 OS << ' ' << Symbol->getName() << ',' << Size;
176 if (Pow2Alignment != 0)
177 OS << ',' << Pow2Alignment;
181 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
182 unsigned Size, unsigned Pow2Alignment) {
183 // Note: a .zerofill directive does not switch sections
184 // FIXME: Really we would like the segment and section names as well as the
185 // section type to be separate values instead of embedded in the name. Not
186 // all assemblers understand all this stuff though.
187 OS << ".zerofill " << Section->getName();
188 if (Symbol != NULL) {
189 OS << ',' << Symbol->getName() << ',' << Size;
190 if (Pow2Alignment != 0)
191 OS << ',' << Pow2Alignment;
196 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
197 assert(CurSection && "Cannot emit contents before setting section!");
198 for (unsigned i = 0; i != Length; ++i)
199 OS << ".byte " << (unsigned) Data[i] << '\n';
202 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
203 assert(CurSection && "Cannot emit contents before setting section!");
204 // Need target hooks to know how to print this.
207 LLVM_UNREACHABLE("Invalid size for machine code value!");
208 case 1: OS << ".byte"; break;
209 case 2: OS << ".short"; break;
210 case 4: OS << ".long"; break;
211 case 8: OS << ".quad"; break;
214 OS << ' ' << truncateToSize(Value, Size) << '\n';
217 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
219 unsigned MaxBytesToEmit) {
220 // Some assemblers don't support .balign, so we always emit as .p2align if
221 // this is a power of two. Otherwise we assume the client knows the target
222 // supports .balign and use that.
223 unsigned Pow2 = Log2_32(ByteAlignment);
224 bool IsPow2 = (1U << Pow2) == ByteAlignment;
228 LLVM_UNREACHABLE("Invalid size for machine code value!");
230 LLVM_UNREACHABLE("Unsupported alignment size!");
231 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
232 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
233 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
236 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
238 OS << ", " << truncateToSize(Value, ValueSize);
240 OS << ", " << MaxBytesToEmit;
244 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
245 unsigned char Value) {
246 // FIXME: Verify that Offset is associated with the current section.
247 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
250 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
252 return OS << "reg:" << Op.getReg();
254 return OS << "imm:" << Op.getImm();
256 return OS << "mbblabel:("
257 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
258 assert(Op.isMCValue() && "Invalid operand!");
259 return OS << "val:" << Op.getMCValue();
262 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
263 assert(CurSection && "Cannot emit contents before setting section!");
264 // FIXME: Implement proper printing.
266 << "opcode=" << Inst.getOpcode() << ", "
268 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
271 OS << Inst.getOperand(i);
276 void MCAsmStreamer::Finish() {
280 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
281 return new MCAsmStreamer(Context, OS);