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 EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
42 bool MakeAbsolute = false);
44 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
46 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
47 unsigned Pow2Alignment, bool IsLocal);
49 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
50 unsigned Size = 0, unsigned Pow2Alignment = 0);
52 virtual void EmitBytes(const char *Data, unsigned Length);
54 virtual void EmitValue(const MCValue &Value, unsigned Size);
56 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
57 unsigned ValueSize = 1,
58 unsigned MaxBytesToEmit = 0);
60 virtual void EmitValueToOffset(const MCValue &Offset,
61 unsigned char Value = 0);
63 virtual void EmitInstruction(const MCInst &Inst);
65 virtual void Finish();
72 /// Allow printing values directly to a raw_ostream.
73 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
74 if (Value.getSymA()) {
75 os << Value.getSymA()->getName();
77 os << " - " << Value.getSymB()->getName();
78 if (Value.getConstant())
79 os << " + " << Value.getConstant();
81 assert(!Value.getSymB() && "Invalid machine code value!");
82 os << Value.getConstant();
88 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
89 assert(Bytes && "Invalid size!");
90 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
93 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
94 return MCValue::get(Value.getSymA(), Value.getSymB(),
95 truncateToSize(Value.getConstant(), Bytes));
98 void MCAsmStreamer::SwitchSection(MCSection *Section) {
99 if (Section != CurSection) {
100 CurSection = Section;
102 // FIXME: Really we would like the segment, flags, etc. to be separate
103 // values instead of embedded in the name. Not all assemblers understand all
104 // this stuff though.
105 OS << ".section " << Section->getName() << "\n";
109 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
110 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
111 assert(CurSection && "Cannot emit before setting section!");
112 assert(!getContext().GetSymbolValue(Symbol) &&
113 "Cannot emit symbol which was directly assigned to!");
115 OS << Symbol->getName() << ":\n";
116 Symbol->setSection(CurSection);
117 Symbol->setExternal(false);
120 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
122 assert(!Symbol->getSection() && "Cannot assign to a label!");
125 OS << ".set " << Symbol->getName() << ", " << Value << '\n';
127 OS << Symbol->getName() << " = " << Value << '\n';
130 getContext().SetSymbolValue(Symbol, Value);
133 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
134 SymbolAttr Attribute) {
136 case Global: OS << ".globl"; break;
137 case Hidden: OS << ".hidden"; break;
138 case IndirectSymbol: OS << ".indirect_symbol"; break;
139 case Internal: OS << ".internal"; break;
140 case LazyReference: OS << ".lazy_reference"; break;
141 case NoDeadStrip: OS << ".no_dead_strip"; break;
142 case PrivateExtern: OS << ".private_extern"; break;
143 case Protected: OS << ".protected"; break;
144 case Reference: OS << ".reference"; break;
145 case Weak: OS << ".weak"; break;
146 case WeakDefinition: OS << ".weak_definition"; break;
147 case WeakReference: OS << ".weak_reference"; break;
150 OS << ' ' << Symbol->getName() << '\n';
153 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
154 unsigned Pow2Alignment, bool IsLocal) {
159 OS << ' ' << Symbol->getName() << ',' << Size;
160 if (Pow2Alignment != 0)
161 OS << ',' << Pow2Alignment;
165 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
166 unsigned Size, unsigned Pow2Alignment) {
167 // Note: a .zerofill directive does not switch sections
168 // FIXME: Really we would like the segment and section names as well as the
169 // section type to be separate values instead of embedded in the name. Not
170 // all assemblers understand all this stuff though.
171 OS << ".zerofill " << Section->getName();
172 if (Symbol != NULL) {
173 OS << ',' << Symbol->getName() << ',' << Size;
174 if (Pow2Alignment != 0)
175 OS << ',' << Pow2Alignment;
180 void MCAsmStreamer::EmitBytes(const char *Data, unsigned Length) {
181 assert(CurSection && "Cannot emit contents before setting section!");
182 for (unsigned i = 0; i != Length; ++i)
183 OS << ".byte " << (unsigned) Data[i] << '\n';
186 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
187 assert(CurSection && "Cannot emit contents before setting section!");
188 // Need target hooks to know how to print this.
191 LLVM_UNREACHABLE("Invalid size for machine code value!");
192 case 1: OS << ".byte"; break;
193 case 2: OS << ".short"; break;
194 case 4: OS << ".long"; break;
195 case 8: OS << ".quad"; break;
198 OS << ' ' << truncateToSize(Value, Size) << '\n';
201 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
203 unsigned MaxBytesToEmit) {
204 // Some assemblers don't support .balign, so we always emit as .p2align if
205 // this is a power of two. Otherwise we assume the client knows the target
206 // supports .balign and use that.
207 unsigned Pow2 = Log2_32(ByteAlignment);
208 bool IsPow2 = (1U << Pow2) == ByteAlignment;
212 LLVM_UNREACHABLE("Invalid size for machine code value!");
214 LLVM_UNREACHABLE("Unsupported alignment size!");
215 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
216 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
217 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
220 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
222 OS << ", " << truncateToSize(Value, ValueSize);
224 OS << ", " << MaxBytesToEmit;
228 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
229 unsigned char Value) {
230 // FIXME: Verify that Offset is associated with the current section.
231 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
234 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
236 return OS << "reg:" << Op.getReg();
238 return OS << "imm:" << Op.getImm();
240 return OS << "mbblabel:("
241 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
242 assert(Op.isMCValue() && "Invalid operand!");
243 return OS << "val:" << Op.getMCValue();
246 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
247 assert(CurSection && "Cannot emit contents before setting section!");
248 // FIXME: Implement proper printing.
250 << "opcode=" << Inst.getOpcode() << ", "
252 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
255 OS << Inst.getOperand(i);
260 void MCAsmStreamer::Finish() {
264 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
265 return new MCAsmStreamer(Context, OS);