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/MCSectionMachO.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 EmitBytes(const StringRef &Data);
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 /// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it
79 /// does not match [a-zA-Z_.][a-zA-Z0-9_.]*.
81 // FIXME: This could be more permissive, do we care?
82 static inline bool NeedsQuoting(const StringRef &Str) {
86 // Check that first character is in [a-zA-Z_.].
87 if (!((Str[0] >= 'a' && Str[0] <= 'z') ||
88 (Str[0] >= 'A' && Str[0] <= 'Z') ||
89 (Str[0] == '_' || Str[0] == '.')))
92 // Check subsequent characters are in [a-zA-Z0-9_.].
93 for (unsigned i = 1, e = Str.size(); i != e; ++i)
94 if (!((Str[i] >= 'a' && Str[i] <= 'z') ||
95 (Str[i] >= 'A' && Str[i] <= 'Z') ||
96 (Str[i] >= '0' && Str[i] <= '9') ||
97 (Str[i] == '_' || Str[i] == '.')))
103 /// Allow printing symbols directly to a raw_ostream with proper quoting.
104 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
105 if (NeedsQuoting(S->getName()))
106 return os << '"' << S->getName() << '"';
107 return os << S->getName();
110 /// Allow printing values directly to a raw_ostream.
111 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
112 if (Value.getSymA()) {
113 os << Value.getSymA();
115 os << " - " << Value.getSymB();
116 if (Value.getConstant())
117 os << " + " << Value.getConstant();
119 assert(!Value.getSymB() && "Invalid machine code value!");
120 os << Value.getConstant();
126 static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
127 assert(Bytes && "Invalid size!");
128 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
131 static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
132 return MCValue::get(Value.getSymA(), Value.getSymB(),
133 truncateToSize(Value.getConstant(), Bytes));
136 void MCAsmStreamer::SwitchSection(MCSection *Section) {
137 if (Section != CurSection) {
138 CurSection = Section;
140 // FIXME: Needs TargetAsmInfo!
141 Section->PrintSwitchToSection(*(const TargetAsmInfo*)0, OS);
145 void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
146 assert(Symbol->getSection() == 0 && "Cannot emit a symbol twice!");
147 assert(CurSection && "Cannot emit before setting section!");
148 assert(!getContext().GetSymbolValue(Symbol) &&
149 "Cannot emit symbol which was directly assigned to!");
151 OS << Symbol << ":\n";
152 Symbol->setSection(CurSection);
153 Symbol->setExternal(false);
156 void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
158 default: assert(0 && "Invalid flag!");
159 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
164 void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
166 assert(!Symbol->getSection() && "Cannot assign to a label!");
169 OS << ".set " << Symbol << ", " << Value << '\n';
171 OS << Symbol << " = " << Value << '\n';
174 getContext().SetSymbolValue(Symbol, Value);
177 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
178 SymbolAttr Attribute) {
180 case Global: OS << ".globl"; break;
181 case Hidden: OS << ".hidden"; break;
182 case IndirectSymbol: OS << ".indirect_symbol"; break;
183 case Internal: OS << ".internal"; break;
184 case LazyReference: OS << ".lazy_reference"; break;
185 case NoDeadStrip: OS << ".no_dead_strip"; break;
186 case PrivateExtern: OS << ".private_extern"; break;
187 case Protected: OS << ".protected"; break;
188 case Reference: OS << ".reference"; break;
189 case Weak: OS << ".weak"; break;
190 case WeakDefinition: OS << ".weak_definition"; break;
191 case WeakReference: OS << ".weak_reference"; break;
194 OS << ' ' << Symbol << '\n';
197 void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
198 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
201 void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
202 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
205 void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
206 unsigned Pow2Alignment, bool IsLocal) {
211 OS << ' ' << Symbol << ',' << Size;
212 if (Pow2Alignment != 0)
213 OS << ',' << Pow2Alignment;
217 void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
218 unsigned Size, unsigned Pow2Alignment) {
219 // Note: a .zerofill directive does not switch sections.
222 // This is a mach-o specific directive.
223 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
224 OS << '"' << MOSection->getSegmentName() << ","
225 << MOSection->getSectionName() << '"';
227 if (Symbol != NULL) {
228 OS << ',' << Symbol << ',' << Size;
229 if (Pow2Alignment != 0)
230 OS << ',' << Pow2Alignment;
235 void MCAsmStreamer::EmitBytes(const StringRef &Data) {
236 assert(CurSection && "Cannot emit contents before setting section!");
237 for (unsigned i = 0, e = Data.size(); i != e; ++i)
238 OS << ".byte " << (unsigned) Data[i] << '\n';
241 void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
242 assert(CurSection && "Cannot emit contents before setting section!");
243 // Need target hooks to know how to print this.
246 llvm_unreachable("Invalid size for machine code value!");
247 case 1: OS << ".byte"; break;
248 case 2: OS << ".short"; break;
249 case 4: OS << ".long"; break;
250 case 8: OS << ".quad"; break;
253 OS << ' ' << truncateToSize(Value, Size) << '\n';
256 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
258 unsigned MaxBytesToEmit) {
259 // Some assemblers don't support .balign, so we always emit as .p2align if
260 // this is a power of two. Otherwise we assume the client knows the target
261 // supports .balign and use that.
262 unsigned Pow2 = Log2_32(ByteAlignment);
263 bool IsPow2 = (1U << Pow2) == ByteAlignment;
267 llvm_unreachable("Invalid size for machine code value!");
269 llvm_unreachable("Unsupported alignment size!");
270 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
271 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
272 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
275 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
277 OS << ", " << truncateToSize(Value, ValueSize);
279 OS << ", " << MaxBytesToEmit;
283 void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
284 unsigned char Value) {
285 // FIXME: Verify that Offset is associated with the current section.
286 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
289 static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
291 return OS << "reg:" << Op.getReg();
293 return OS << "imm:" << Op.getImm();
295 return OS << "mbblabel:("
296 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
297 assert(Op.isMCValue() && "Invalid operand!");
298 return OS << "val:" << Op.getMCValue();
301 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
302 assert(CurSection && "Cannot emit contents before setting section!");
303 // FIXME: Implement proper printing.
305 << "opcode=" << Inst.getOpcode() << ", "
307 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
310 OS << Inst.getOperand(i);
315 void MCAsmStreamer::Finish() {
319 MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
320 return new MCAsmStreamer(Context, OS);