blob: 7dcca7bd5c5d5995449dc358f5cb1bb09908c1c4 [file] [log] [blame]
Daniel Dunbara11af532009-06-24 01:03:06 +00001//===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/MC/MCStreamer.h"
11
Daniel Dunbara11af532009-06-24 01:03:06 +000012#include "llvm/MC/MCContext.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000013#include "llvm/MC/MCInst.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000014#include "llvm/MC/MCSection.h"
15#include "llvm/MC/MCSymbol.h"
16#include "llvm/MC/MCValue.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000017#include "llvm/Support/ErrorHandling.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000018#include "llvm/Support/raw_ostream.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000019
Daniel Dunbara11af532009-06-24 01:03:06 +000020using namespace llvm;
21
22namespace {
23
24 class MCAsmStreamer : public MCStreamer {
25 raw_ostream &OS;
26
27 MCSection *CurSection;
28
29 public:
30 MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
Torok Edwin48de5d82009-06-29 19:59:10 +000031 : MCStreamer(Context), OS(_OS), CurSection(0) {}
Daniel Dunbara11af532009-06-24 01:03:06 +000032 ~MCAsmStreamer() {}
33
34 /// @name MCStreamer Interface
35 /// @{
36
37 virtual void SwitchSection(MCSection *Section);
38
39 virtual void EmitLabel(MCSymbol *Symbol);
40
Kevin Enderbyf96db462009-07-16 17:56:39 +000041 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Kevin Enderbya5c78322009-07-13 21:03:15 +000042
Daniel Dunbara11af532009-06-24 01:03:06 +000043 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
44 bool MakeAbsolute = false);
45
46 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
47
Kevin Enderby95cf30c2009-07-14 18:17:10 +000048 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
49
Kevin Enderby71148242009-07-14 21:35:03 +000050 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
51
Chris Lattner4e4db7a2009-07-07 20:30:46 +000052 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +000053 unsigned Pow2Alignment, bool IsLocal);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000054
Chris Lattner9be3fee2009-07-10 22:20:30 +000055 virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
56 unsigned Size = 0, unsigned Pow2Alignment = 0);
57
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000058 virtual void EmitBytes(const StringRef &Data);
Daniel Dunbara11af532009-06-24 01:03:06 +000059
60 virtual void EmitValue(const MCValue &Value, unsigned Size);
61
Daniel Dunbar84a29262009-06-24 19:25:34 +000062 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
63 unsigned ValueSize = 1,
64 unsigned MaxBytesToEmit = 0);
65
66 virtual void EmitValueToOffset(const MCValue &Offset,
67 unsigned char Value = 0);
68
Daniel Dunbara11af532009-06-24 01:03:06 +000069 virtual void EmitInstruction(const MCInst &Inst);
70
71 virtual void Finish();
72
73 /// @}
74 };
75
76}
77
Daniel Dunbarad4555c2009-07-31 23:04:32 +000078/// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it
79/// does not match [a-zA-Z_.][a-zA-Z0-9_.]*.
80//
81// FIXME: This could be more permissive, do we care?
82static inline bool NeedsQuoting(const StringRef &Str) {
83 if (Str.empty())
84 return true;
85
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] == '.')))
90 return true;
91
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] == '.')))
98 return true;
99
100 return false;
101}
102
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000103/// Allow printing symbols directly to a raw_ostream with proper quoting.
104static 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();
108}
109
Daniel Dunbara11af532009-06-24 01:03:06 +0000110/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000111static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbara11af532009-06-24 01:03:06 +0000112 if (Value.getSymA()) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000113 os << Value.getSymA();
Daniel Dunbara11af532009-06-24 01:03:06 +0000114 if (Value.getSymB())
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000115 os << " - " << Value.getSymB();
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000116 if (Value.getConstant())
117 os << " + " << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +0000118 } else {
119 assert(!Value.getSymB() && "Invalid machine code value!");
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000120 os << Value.getConstant();
Daniel Dunbara11af532009-06-24 01:03:06 +0000121 }
122
123 return os;
124}
125
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000126static 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));
129}
130
131static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
132 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000133 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000134}
135
Daniel Dunbara11af532009-06-24 01:03:06 +0000136void MCAsmStreamer::SwitchSection(MCSection *Section) {
137 if (Section != CurSection) {
138 CurSection = Section;
139
Chris Lattner93b6db32009-08-08 23:39:42 +0000140 // FIXME: Needs TargetAsmInfo!
141 Section->PrintSwitchToSection(*(const TargetAsmInfo*)0, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000142 }
143}
144
145void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000146 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!");
150
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000151 OS << Symbol << ":\n";
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000152 Symbol->setSection(CurSection);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000153 Symbol->setExternal(false);
Daniel Dunbara11af532009-06-24 01:03:06 +0000154}
155
Kevin Enderbyf96db462009-07-16 17:56:39 +0000156void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
157 switch (Flag) {
158 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
159 }
160 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000161}
162
Daniel Dunbara11af532009-06-24 01:03:06 +0000163void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
164 bool MakeAbsolute) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000165 assert(!Symbol->getSection() && "Cannot assign to a label!");
166
Daniel Dunbara11af532009-06-24 01:03:06 +0000167 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000168 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000169 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000170 OS << Symbol << " = " << Value << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000171 }
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000172
173 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000174}
175
176void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
177 SymbolAttr Attribute) {
178 switch (Attribute) {
179 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000180 case Hidden: OS << ".hidden"; break;
181 case IndirectSymbol: OS << ".indirect_symbol"; break;
182 case Internal: OS << ".internal"; break;
183 case LazyReference: OS << ".lazy_reference"; break;
184 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000185 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000186 case Protected: OS << ".protected"; break;
187 case Reference: OS << ".reference"; break;
188 case Weak: OS << ".weak"; break;
189 case WeakDefinition: OS << ".weak_definition"; break;
190 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000191 }
192
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000193 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000194}
195
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000196void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000197 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000198}
199
Kevin Enderby71148242009-07-14 21:35:03 +0000200void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000201 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
Kevin Enderby71148242009-07-14 21:35:03 +0000202}
203
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000204void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Chris Lattner1fc3d752009-07-09 17:25:12 +0000205 unsigned Pow2Alignment, bool IsLocal) {
206 if (IsLocal)
207 OS << ".lcomm";
208 else
209 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000210 OS << ' ' << Symbol << ',' << Size;
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000211 if (Pow2Alignment != 0)
212 OS << ',' << Pow2Alignment;
213 OS << '\n';
214}
215
Chris Lattner9be3fee2009-07-10 22:20:30 +0000216void MCAsmStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
217 unsigned Size, unsigned Pow2Alignment) {
218 // Note: a .zerofill directive does not switch sections
219 // FIXME: Really we would like the segment and section names as well as the
220 // section type to be separate values instead of embedded in the name. Not
221 // all assemblers understand all this stuff though.
Chris Lattner93b6db32009-08-08 23:39:42 +0000222 OS << ".zerofill ";
223
224 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000225 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
226 OS << '"' << MOSection->getSegmentName() << ","
227 << MOSection->getSectionName() << '"';
Chris Lattner93b6db32009-08-08 23:39:42 +0000228
229
Chris Lattner9be3fee2009-07-10 22:20:30 +0000230 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000231 OS << ',' << Symbol << ',' << Size;
Chris Lattner9be3fee2009-07-10 22:20:30 +0000232 if (Pow2Alignment != 0)
233 OS << ',' << Pow2Alignment;
234 }
235 OS << '\n';
236}
237
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000238void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000239 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000240 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbara11af532009-06-24 01:03:06 +0000241 OS << ".byte " << (unsigned) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000242}
243
244void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000245 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000246 // Need target hooks to know how to print this.
247 switch (Size) {
248 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000249 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000250 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000251 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000252 case 4: OS << ".long"; break;
253 case 8: OS << ".quad"; break;
254 }
255
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000256 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000257}
258
Daniel Dunbar84a29262009-06-24 19:25:34 +0000259void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
260 unsigned ValueSize,
261 unsigned MaxBytesToEmit) {
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000262 // Some assemblers don't support .balign, so we always emit as .p2align if
263 // this is a power of two. Otherwise we assume the client knows the target
264 // supports .balign and use that.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000265 unsigned Pow2 = Log2_32(ByteAlignment);
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000266 bool IsPow2 = (1U << Pow2) == ByteAlignment;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000267
268 switch (ValueSize) {
269 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000270 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000271 case 8:
Torok Edwinc23197a2009-07-14 16:55:14 +0000272 llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000273 case 1: OS << (IsPow2 ? ".p2align" : ".balign"); break;
274 case 2: OS << (IsPow2 ? ".p2alignw" : ".balignw"); break;
275 case 4: OS << (IsPow2 ? ".p2alignl" : ".balignl"); break;
Daniel Dunbar84a29262009-06-24 19:25:34 +0000276 }
277
Daniel Dunbarc29dfa72009-06-29 23:46:59 +0000278 OS << ' ' << (IsPow2 ? Pow2 : ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000279
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000280 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000281 if (MaxBytesToEmit)
282 OS << ", " << MaxBytesToEmit;
283 OS << '\n';
284}
285
286void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
287 unsigned char Value) {
288 // FIXME: Verify that Offset is associated with the current section.
289 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
290}
291
Daniel Dunbarabde2982009-07-01 06:35:03 +0000292static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
293 if (Op.isReg())
294 return OS << "reg:" << Op.getReg();
295 if (Op.isImm())
296 return OS << "imm:" << Op.getImm();
297 if (Op.isMBBLabel())
298 return OS << "mbblabel:("
299 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
300 assert(Op.isMCValue() && "Invalid operand!");
301 return OS << "val:" << Op.getMCValue();
302}
303
Daniel Dunbara11af532009-06-24 01:03:06 +0000304void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000305 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarabde2982009-07-01 06:35:03 +0000306 // FIXME: Implement proper printing.
307 OS << "MCInst("
308 << "opcode=" << Inst.getOpcode() << ", "
309 << "operands=[";
310 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
311 if (i)
312 OS << ", ";
313 OS << Inst.getOperand(i);
314 }
315 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000316}
317
318void MCAsmStreamer::Finish() {
319 OS.flush();
320}
321
322MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
323 return new MCAsmStreamer(Context, OS);
324}