blob: d0a2ebbc309121b4f77cea8758075b45c411bdac [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 Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000014#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000015#include "llvm/MC/MCContext.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000017#include "llvm/MC/MCInst.h"
Chris Lattnerf9bdedd2009-08-10 18:15:01 +000018#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000019#include "llvm/MC/MCSymbol.h"
20#include "llvm/MC/MCValue.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner663c2d22009-08-19 06:12:02 +000022#include "llvm/Support/MathExtras.h"
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000023#include "llvm/Support/Format.h"
Daniel Dunbara11af532009-06-24 01:03:06 +000024#include "llvm/Support/raw_ostream.h"
25using namespace llvm;
26
27namespace {
28
Chris Lattner46a947d2009-08-17 04:17:34 +000029class MCAsmStreamer : public MCStreamer {
30 raw_ostream &OS;
Chris Lattner33adcfb2009-08-22 21:43:10 +000031 const MCAsmInfo &MAI;
Chris Lattner46a947d2009-08-17 04:17:34 +000032 AsmPrinter *Printer;
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000033 MCCodeEmitter *Emitter;
Chris Lattner46a947d2009-08-17 04:17:34 +000034public:
Chris Lattneraf76e592009-08-22 20:48:53 +000035 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000036 AsmPrinter *_Printer, MCCodeEmitter *_Emitter)
37 : MCStreamer(Context), OS(_OS), MAI(tai), Printer(_Printer),
38 Emitter(_Emitter) {}
Chris Lattner46a947d2009-08-17 04:17:34 +000039 ~MCAsmStreamer() {}
Daniel Dunbara11af532009-06-24 01:03:06 +000040
Chris Lattner46a947d2009-08-17 04:17:34 +000041 /// @name MCStreamer Interface
42 /// @{
Daniel Dunbara11af532009-06-24 01:03:06 +000043
Chris Lattner975780b2009-08-17 05:49:08 +000044 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara11af532009-06-24 01:03:06 +000045
Chris Lattner46a947d2009-08-17 04:17:34 +000046 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara11af532009-06-24 01:03:06 +000047
Chris Lattner46a947d2009-08-17 04:17:34 +000048 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara11af532009-06-24 01:03:06 +000049
Chris Lattner46a947d2009-08-17 04:17:34 +000050 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
51 bool MakeAbsolute = false);
Daniel Dunbara11af532009-06-24 01:03:06 +000052
Chris Lattner46a947d2009-08-17 04:17:34 +000053 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000054
Chris Lattner46a947d2009-08-17 04:17:34 +000055 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000056
Chris Lattner46a947d2009-08-17 04:17:34 +000057 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000058 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000059
Daniel Dunbar8751b942009-08-28 05:48:22 +000060 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000061 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000062
Chris Lattner46a947d2009-08-17 04:17:34 +000063 virtual void EmitBytes(const StringRef &Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000064
Chris Lattner46a947d2009-08-17 04:17:34 +000065 virtual void EmitValue(const MCValue &Value, unsigned Size);
Chris Lattner9be3fee2009-07-10 22:20:30 +000066
Chris Lattner46a947d2009-08-17 04:17:34 +000067 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
68 unsigned ValueSize = 1,
69 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000070
Chris Lattner46a947d2009-08-17 04:17:34 +000071 virtual void EmitValueToOffset(const MCValue &Offset,
72 unsigned char Value = 0);
73
74 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000075
Chris Lattner46a947d2009-08-17 04:17:34 +000076 virtual void Finish();
77
78 /// @}
79};
Daniel Dunbar84a29262009-06-24 19:25:34 +000080
Chris Lattner46a947d2009-08-17 04:17:34 +000081} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000082
Daniel Dunbarad4555c2009-07-31 23:04:32 +000083/// Allow printing symbols directly to a raw_ostream with proper quoting.
84static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000085 S->print(os);
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000086 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000087}
88
Daniel Dunbara11af532009-06-24 01:03:06 +000089/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000090static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000091 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000092 return os;
93}
94
Daniel Dunbar304f6a42009-06-25 21:03:18 +000095static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
96 assert(Bytes && "Invalid size!");
97 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
98}
99
100static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
101 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000102 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000103}
104
Chris Lattner975780b2009-08-17 05:49:08 +0000105void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000106 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000107 if (Section != CurSection) {
108 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000109 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000110 }
111}
112
113void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000114 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000115 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000116
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000117 OS << Symbol << ":\n";
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000118 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000119}
120
Kevin Enderbyf96db462009-07-16 17:56:39 +0000121void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
122 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000123 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000124 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
125 }
126 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000127}
128
Daniel Dunbara11af532009-06-24 01:03:06 +0000129void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
130 bool MakeAbsolute) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000131 // Only absolute symbols can be redefined.
132 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
133 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000134
Daniel Dunbara11af532009-06-24 01:03:06 +0000135 if (MakeAbsolute) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000136 OS << ".set " << Symbol << ", " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000137
138 // HACK: If the value isn't already absolute, set the symbol value to
139 // itself, we want to use the .set absolute value, not the actual
140 // expression.
141 if (!Value.isAbsolute())
142 getContext().SetSymbolValue(Symbol, MCValue::get(Symbol));
143 else
144 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000145 } else {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000146 OS << Symbol << " = " << Value << '\n';
Daniel Dunbarb2d0b6b2009-08-14 19:10:46 +0000147 getContext().SetSymbolValue(Symbol, Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000148 }
149}
150
151void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
152 SymbolAttr Attribute) {
153 switch (Attribute) {
154 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000155 case Hidden: OS << ".hidden"; break;
156 case IndirectSymbol: OS << ".indirect_symbol"; break;
157 case Internal: OS << ".internal"; break;
158 case LazyReference: OS << ".lazy_reference"; break;
159 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000160 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000161 case Protected: OS << ".protected"; break;
162 case Reference: OS << ".reference"; break;
163 case Weak: OS << ".weak"; break;
164 case WeakDefinition: OS << ".weak_definition"; break;
165 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000166 }
167
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000168 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000169}
170
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000171void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000172 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000173}
174
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000175void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000176 unsigned ByteAlignment) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +0000177 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000178 OS << ' ' << Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000179 if (ByteAlignment != 0)
180 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000181 OS << '\n';
182}
183
Daniel Dunbar8751b942009-08-28 05:48:22 +0000184void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000185 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000186 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000187 OS << ".zerofill ";
188
189 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000190 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000191 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000192
Chris Lattner9be3fee2009-07-10 22:20:30 +0000193 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000194 OS << ',' << Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000195 if (ByteAlignment != 0)
196 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000197 }
198 OS << '\n';
199}
200
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000201void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000202 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000203 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000204 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000205}
206
207void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000208 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000209 // Need target hooks to know how to print this.
210 switch (Size) {
211 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000212 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000213 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000214 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000215 case 4: OS << ".long"; break;
216 case 8: OS << ".quad"; break;
217 }
218
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000219 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000220}
221
Daniel Dunbar84a29262009-06-24 19:25:34 +0000222void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
223 unsigned ValueSize,
224 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000225 // Some assemblers don't support non-power of two alignments, so we always
226 // emit alignments as a power of two if possible.
227 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000228 switch (ValueSize) {
229 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000230 case 1: OS << MAI.getAlignDirective(); break;
231 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000232 case 2: OS << ".p2alignw "; break;
233 case 4: OS << ".p2alignl "; break;
234 case 8: llvm_unreachable("Unsupported alignment size!");
235 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000236
Chris Lattner33adcfb2009-08-22 21:43:10 +0000237 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000238 OS << ByteAlignment;
239 else
240 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000241
Chris Lattner663c2d22009-08-19 06:12:02 +0000242 if (Value || MaxBytesToEmit) {
243 OS << ", " << truncateToSize(Value, ValueSize);
244
245 if (MaxBytesToEmit)
246 OS << ", " << MaxBytesToEmit;
247 }
248 OS << '\n';
249 return;
250 }
251
252 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000253 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000254 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000255 default: llvm_unreachable("Invalid size for machine code value!");
256 case 1: OS << ".balign"; break;
257 case 2: OS << ".balignw"; break;
258 case 4: OS << ".balignl"; break;
259 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000260 }
261
Chris Lattner663c2d22009-08-19 06:12:02 +0000262 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000263 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000264 if (MaxBytesToEmit)
265 OS << ", " << MaxBytesToEmit;
266 OS << '\n';
267}
268
269void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
270 unsigned char Value) {
271 // FIXME: Verify that Offset is associated with the current section.
272 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
273}
274
Daniel Dunbara11af532009-06-24 01:03:06 +0000275void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000276 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000277
278 // If we have an AsmPrinter, use that to print.
279 if (Printer) {
280 Printer->printMCInst(&Inst);
Daniel Dunbara356aea2009-08-27 07:58:57 +0000281
282 // Show the encoding if we have a code emitter.
283 if (Emitter) {
284 SmallString<256> Code;
285 raw_svector_ostream VecOS(Code);
286 Emitter->EncodeInstruction(Inst, VecOS);
287 VecOS.flush();
288
289 OS.indent(20);
290 OS << " # encoding: [";
291 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
292 if (i)
293 OS << ',';
294 OS << format("%#04x", uint8_t(Code[i]));
295 }
296 OS << "]\n";
297 }
298
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000299 return;
300 }
301
302 // Otherwise fall back to a structural printing for now. Eventually we should
303 // always have access to the target specific printer.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000304 Inst.print(OS);
305 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000306}
307
308void MCAsmStreamer::Finish() {
309 OS.flush();
310}
311
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000312MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000313 const MCAsmInfo &MAI, AsmPrinter *AP,
314 MCCodeEmitter *CE) {
315 return new MCAsmStreamer(Context, OS, MAI, AP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000316}