blob: fa7c7cf09a42c36f894b161160e420a750e2b834 [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"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000011#include "llvm/ADT/SmallString.h"
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000012#include "llvm/CodeGen/AsmPrinter.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000013#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000015#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000016#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000017#include "llvm/MC/MCSymbol.h"
18#include "llvm/MC/MCValue.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000019#include "llvm/MC/MCAsmInfo.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000021#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000022#include "llvm/Support/Format.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26namespace {
27
Chris Lattner46a947d2009-08-17 04:17:34 +000028class MCAsmStreamer : public MCStreamer {
29 raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000030 const MCAsmInfo &MAI;
Chris Lattner46a947d2009-08-17 04:17:34 +000031 AsmPrinter *Printer;
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000032 MCCodeEmitter *Emitter;
Chris Lattner46a947d2009-08-17 04:17:34 +000033public:
Chris Lattneraf76e592009-08-22 20:48:53 +000034 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000035 AsmPrinter *_Printer, MCCodeEmitter *_Emitter)
36 : MCStreamer(Context), OS(_OS), MAI(tai), Printer(_Printer),
37 Emitter(_Emitter) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000038 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000039
Chris Lattner46a947d2009-08-17 04:17:34 +000040 /// @name MCStreamer Interface
41 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000042
Chris Lattner975780b2009-08-17 05:49:08 +000043 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000044
Chris Lattner46a947d2009-08-17 04:17:34 +000045 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000046
Chris Lattner46a947d2009-08-17 04:17:34 +000047 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000048
Chris Lattner46a947d2009-08-17 04:17:34 +000049 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
50 bool MakeAbsolute = false);
Daniel Dunbara11af532009-06-24 01:03:06 +000051
Chris Lattner46a947d2009-08-17 04:17:34 +000052 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000053
Chris Lattner46a947d2009-08-17 04:17:34 +000054 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000055
Chris Lattner46a947d2009-08-17 04:17:34 +000056 virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000057
Chris Lattner46a947d2009-08-17 04:17:34 +000058 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbare6cdbf22009-08-28 05:48:46 +000059 unsigned Pow2Alignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000060
Daniel Dunbar8751b942009-08-28 05:48:22 +000061 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Chris Lattner46a947d2009-08-17 04:17:34 +000062 unsigned Size = 0, unsigned Pow2Alignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000063
Chris Lattner46a947d2009-08-17 04:17:34 +000064 virtual void EmitBytes(const StringRef &Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000065
Chris Lattner46a947d2009-08-17 04:17:34 +000066 virtual void EmitValue(const MCValue &Value, unsigned Size);
Chris Lattner9be3fee2009-07-10 22:20:30 +000067
Chris Lattner46a947d2009-08-17 04:17:34 +000068 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
69 unsigned ValueSize = 1,
70 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000071
Chris Lattner46a947d2009-08-17 04:17:34 +000072 virtual void EmitValueToOffset(const MCValue &Offset,
73 unsigned char Value = 0);
74
75 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000076
Chris Lattner46a947d2009-08-17 04:17:34 +000077 virtual void Finish();
78
79 /// @}
80};
Daniel Dunbar84a29262009-06-24 19:25:34 +000081
Chris Lattner46a947d2009-08-17 04:17:34 +000082} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000083
Daniel Dunbarad4555c2009-07-31 23:04:32 +000084/// Allow printing symbols directly to a raw_ostream with proper quoting.
85static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000086 S->print(os);
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000087 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000088}
89
Daniel Dunbara11af532009-06-24 01:03:06 +000090/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000091static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000092 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000093 return os;
94}
95
Daniel Dunbar304f6a42009-06-25 21:03:18 +000096static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
97 assert(Bytes && "Invalid size!");
98 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
99}
100
101static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
102 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000103 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000104}
105
Chris Lattner975780b2009-08-17 05:49:08 +0000106void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000107 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000108 if (Section != CurSection) {
109 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000110 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000111 }
112}
113
114void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000115 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000116 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000117
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000118 OS << Symbol << ":\n";
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000119 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000120}
121
Kevin Enderbyf96db462009-07-16 17:56:39 +0000122void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
123 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000124 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000125 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
126 }
127 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000128}
129
Daniel Dunbara11af532009-06-24 01:03:06 +0000130void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
131 bool MakeAbsolute) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000132 // Only absolute symbols can be redefined.
133 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
134 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000135
Daniel Dunbara11af532009-06-24 01:03:06 +0000136 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000137 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000138
139 // HACK: If the value isn't already absolute, set the symbol value to
140 // itself, we want to use the .set absolute value, not the actual
141 // expression.
142 if (!Value.isAbsolute())
143 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
144 else
145 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000146 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000147 OS << Symbol << " = " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000148 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000149 }
150}
151
152void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
153 SymbolAttr Attribute) {
154 switch (Attribute) {
155 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000156 case Hidden: OS << ".hidden"; break;
157 case IndirectSymbol: OS << ".indirect_symbol"; break;
158 case Internal: OS << ".internal"; break;
159 case LazyReference: OS << ".lazy_reference"; break;
160 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000161 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000162 case Protected: OS << ".protected"; break;
163 case Reference: OS << ".reference"; break;
164 case Weak: OS << ".weak"; break;
165 case WeakDefinition: OS << ".weak_definition"; break;
166 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000167 }
168
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000169 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000170}
171
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000172void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000173 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000174}
175
Kevin Enderby71148242009-07-14 21:35:03 +0000176void MCAsmStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000177 OS << ".lsym" << ' ' << Symbol << ',' << Value << '\n';
Kevin Enderby71148242009-07-14 21:35:03 +0000178}
179
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000180void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbare6cdbf22009-08-28 05:48:46 +0000181 unsigned Pow2Alignment) {
182 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000183 OS << ' ' << Symbol << ',' << Size;
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000184 if (Pow2Alignment != 0)
185 OS << ',' << Pow2Alignment;
186 OS << '\n';
187}
188
Daniel Dunbar8751b942009-08-28 05:48:22 +0000189void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Chris Lattner9be3fee2009-07-10 22:20:30 +0000190 unsigned Size, unsigned Pow2Alignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000191 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000192 OS << ".zerofill ";
193
194 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000195 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000196 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000197
Chris Lattner9be3fee2009-07-10 22:20:30 +0000198 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000199 OS << ',' << Symbol << ',' << Size;
Chris Lattner9be3fee2009-07-10 22:20:30 +0000200 if (Pow2Alignment != 0)
201 OS << ',' << Pow2Alignment;
202 }
203 OS << '\n';
204}
205
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000206void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000207 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000208 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000209 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000210}
211
212void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000213 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000214 // Need target hooks to know how to print this.
215 switch (Size) {
216 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000217 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000218 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000219 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000220 case 4: OS << ".long"; break;
221 case 8: OS << ".quad"; break;
222 }
223
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000224 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000225}
226
Daniel Dunbar84a29262009-06-24 19:25:34 +0000227void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
228 unsigned ValueSize,
229 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000230 // Some assemblers don't support non-power of two alignments, so we always
231 // emit alignments as a power of two if possible.
232 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000233 switch (ValueSize) {
234 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000235 case 1: OS << MAI.getAlignDirective(); break;
236 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000237 case 2: OS << ".p2alignw "; break;
238 case 4: OS << ".p2alignl "; break;
239 case 8: llvm_unreachable("Unsupported alignment size!");
240 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000241
Chris Lattner33adcfb2009-08-22 21:43:10 +0000242 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000243 OS << ByteAlignment;
244 else
245 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000246
Chris Lattner663c2d22009-08-19 06:12:02 +0000247 if (Value || MaxBytesToEmit) {
248 OS << ", " << truncateToSize(Value, ValueSize);
249
250 if (MaxBytesToEmit)
251 OS << ", " << MaxBytesToEmit;
252 }
253 OS << '\n';
254 return;
255 }
256
257 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000258 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000259 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000260 default: llvm_unreachable("Invalid size for machine code value!");
261 case 1: OS << ".balign"; break;
262 case 2: OS << ".balignw"; break;
263 case 4: OS << ".balignl"; break;
264 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000265 }
266
Chris Lattner663c2d22009-08-19 06:12:02 +0000267 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000268 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000269 if (MaxBytesToEmit)
270 OS << ", " << MaxBytesToEmit;
271 OS << '\n';
272}
273
274void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
275 unsigned char Value) {
276 // FIXME: Verify that Offset is associated with the current section.
277 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
278}
279
Daniel Dunbarabde2982009-07-01 06:35:03 +0000280static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
281 if (Op.isReg())
282 return OS << "reg:" << Op.getReg();
283 if (Op.isImm())
284 return OS << "imm:" << Op.getImm();
285 if (Op.isMBBLabel())
286 return OS << "mbblabel:("
287 << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
288 assert(Op.isMCValue() && "Invalid operand!");
289 return OS << "val:" << Op.getMCValue();
290}
291
Daniel Dunbara11af532009-06-24 01:03:06 +0000292void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000293 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000294
295 // If we have an AsmPrinter, use that to print.
296 if (Printer) {
297 Printer->printMCInst(&Inst);
Daniel Dunbara356aea2009-08-27 07:58:57 +0000298
299 // Show the encoding if we have a code emitter.
300 if (Emitter) {
301 SmallString<256> Code;
302 raw_svector_ostream VecOS(Code);
303 Emitter->EncodeInstruction(Inst, VecOS);
304 VecOS.flush();
305
306 OS.indent(20);
307 OS << " # encoding: [";
308 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
309 if (i)
310 OS << ',';
311 OS << format("%#04x", uint8_t(Code[i]));
312 }
313 OS << "]\n";
314 }
315
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000316 return;
317 }
318
319 // Otherwise fall back to a structural printing for now. Eventually we should
320 // always have access to the target specific printer.
Daniel Dunbarabde2982009-07-01 06:35:03 +0000321 OS << "MCInst("
322 << "opcode=" << Inst.getOpcode() << ", "
323 << "operands=[";
324 for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
325 if (i)
326 OS << ", ";
327 OS << Inst.getOperand(i);
328 }
329 OS << "])\n";
Daniel Dunbara11af532009-06-24 01:03:06 +0000330}
331
332void MCAsmStreamer::Finish() {
333 OS.flush();
334}
335
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000336MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000337 const MCAsmInfo &MAI, AsmPrinter *AP,
338 MCCodeEmitter *CE) {
339 return new MCAsmStreamer(Context, OS, MAI, AP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000340}