blob: c220b8a9024adbfbf07c483af607424c7ecbae95 [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
Daniel Dunbare2ace502009-08-31 08:09:09 +000050 virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value);
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 EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000057 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000058
Daniel Dunbar8751b942009-08-28 05:48:22 +000059 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000060 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000061
Chris Lattner46a947d2009-08-17 04:17:34 +000062 virtual void EmitBytes(const StringRef &Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000063
Chris Lattner46a947d2009-08-17 04:17:34 +000064 virtual void EmitValue(const MCValue &Value, unsigned Size);
Chris Lattner9be3fee2009-07-10 22:20:30 +000065
Chris Lattner46a947d2009-08-17 04:17:34 +000066 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
67 unsigned ValueSize = 1,
68 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000069
Chris Lattner46a947d2009-08-17 04:17:34 +000070 virtual void EmitValueToOffset(const MCValue &Offset,
71 unsigned char Value = 0);
72
73 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000074
Chris Lattner46a947d2009-08-17 04:17:34 +000075 virtual void Finish();
76
77 /// @}
78};
Daniel Dunbar84a29262009-06-24 19:25:34 +000079
Chris Lattner46a947d2009-08-17 04:17:34 +000080} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000081
Daniel Dunbarad4555c2009-07-31 23:04:32 +000082/// Allow printing symbols directly to a raw_ostream with proper quoting.
83static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000084 S->print(os);
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000085 return os;
Daniel Dunbarad4555c2009-07-31 23:04:32 +000086}
87
Daniel Dunbara11af532009-06-24 01:03:06 +000088/// Allow printing values directly to a raw_ostream.
Daniel Dunbar304f6a42009-06-25 21:03:18 +000089static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
Daniel Dunbarc22e0b22009-08-14 03:48:55 +000090 Value.print(os);
Daniel Dunbara11af532009-06-24 01:03:06 +000091 return os;
92}
93
Daniel Dunbar304f6a42009-06-25 21:03:18 +000094static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
95 assert(Bytes && "Invalid size!");
96 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
97}
98
99static inline MCValue truncateToSize(const MCValue &Value, unsigned Bytes) {
100 return MCValue::get(Value.getSymA(), Value.getSymB(),
Daniel Dunbar7908a6a2009-06-29 19:51:00 +0000101 truncateToSize(Value.getConstant(), Bytes));
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000102}
103
Chris Lattner975780b2009-08-17 05:49:08 +0000104void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000105 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000106 if (Section != CurSection) {
107 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000108 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +0000109 }
110}
111
112void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000113 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000114 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000115
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000116 OS << Symbol << ":\n";
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000117 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000118}
119
Kevin Enderbyf96db462009-07-16 17:56:39 +0000120void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
121 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000122 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000123 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
124 }
125 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000126}
127
Daniel Dunbare2ace502009-08-31 08:09:09 +0000128void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCValue &Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000129 // Only absolute symbols can be redefined.
130 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
131 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000132
Daniel Dunbare2ace502009-08-31 08:09:09 +0000133 OS << Symbol << " = " << Value << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000134}
135
136void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
137 SymbolAttr Attribute) {
138 switch (Attribute) {
139 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000140 case Hidden: OS << ".hidden"; break;
141 case IndirectSymbol: OS << ".indirect_symbol"; break;
142 case Internal: OS << ".internal"; break;
143 case LazyReference: OS << ".lazy_reference"; break;
144 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000145 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000146 case Protected: OS << ".protected"; break;
147 case Reference: OS << ".reference"; break;
148 case Weak: OS << ".weak"; break;
149 case WeakDefinition: OS << ".weak_definition"; break;
150 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000151 }
152
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000153 OS << ' ' << Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000154}
155
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000156void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000157 OS << ".desc" << ' ' << Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000158}
159
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000160void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000161 unsigned ByteAlignment) {
Daniel Dunbare6cdbf22009-08-28 05:48:46 +0000162 OS << ".comm";
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000163 OS << ' ' << Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000164 if (ByteAlignment != 0)
165 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000166 OS << '\n';
167}
168
Daniel Dunbar8751b942009-08-28 05:48:22 +0000169void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000170 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000171 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000172 OS << ".zerofill ";
173
174 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000175 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000176 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000177
Chris Lattner9be3fee2009-07-10 22:20:30 +0000178 if (Symbol != NULL) {
Daniel Dunbarad4555c2009-07-31 23:04:32 +0000179 OS << ',' << Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000180 if (ByteAlignment != 0)
181 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000182 }
183 OS << '\n';
184}
185
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000186void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000187 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000188 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000189 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000190}
191
192void MCAsmStreamer::EmitValue(const MCValue &Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000193 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000194 // Need target hooks to know how to print this.
195 switch (Size) {
196 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000197 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000198 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000199 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000200 case 4: OS << ".long"; break;
201 case 8: OS << ".quad"; break;
202 }
203
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000204 OS << ' ' << truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000205}
206
Daniel Dunbar84a29262009-06-24 19:25:34 +0000207void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
208 unsigned ValueSize,
209 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000210 // Some assemblers don't support non-power of two alignments, so we always
211 // emit alignments as a power of two if possible.
212 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000213 switch (ValueSize) {
214 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000215 case 1: OS << MAI.getAlignDirective(); break;
216 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000217 case 2: OS << ".p2alignw "; break;
218 case 4: OS << ".p2alignl "; break;
219 case 8: llvm_unreachable("Unsupported alignment size!");
220 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000221
Chris Lattner33adcfb2009-08-22 21:43:10 +0000222 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000223 OS << ByteAlignment;
224 else
225 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000226
Chris Lattner663c2d22009-08-19 06:12:02 +0000227 if (Value || MaxBytesToEmit) {
228 OS << ", " << truncateToSize(Value, ValueSize);
229
230 if (MaxBytesToEmit)
231 OS << ", " << MaxBytesToEmit;
232 }
233 OS << '\n';
234 return;
235 }
236
237 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000238 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000239 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000240 default: llvm_unreachable("Invalid size for machine code value!");
241 case 1: OS << ".balign"; break;
242 case 2: OS << ".balignw"; break;
243 case 4: OS << ".balignl"; break;
244 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000245 }
246
Chris Lattner663c2d22009-08-19 06:12:02 +0000247 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000248 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000249 if (MaxBytesToEmit)
250 OS << ", " << MaxBytesToEmit;
251 OS << '\n';
252}
253
254void MCAsmStreamer::EmitValueToOffset(const MCValue &Offset,
255 unsigned char Value) {
256 // FIXME: Verify that Offset is associated with the current section.
257 OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
258}
259
Daniel Dunbara11af532009-06-24 01:03:06 +0000260void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000261 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000262
263 // If we have an AsmPrinter, use that to print.
264 if (Printer) {
265 Printer->printMCInst(&Inst);
Daniel Dunbara356aea2009-08-27 07:58:57 +0000266
267 // Show the encoding if we have a code emitter.
268 if (Emitter) {
269 SmallString<256> Code;
270 raw_svector_ostream VecOS(Code);
271 Emitter->EncodeInstruction(Inst, VecOS);
272 VecOS.flush();
273
274 OS.indent(20);
275 OS << " # encoding: [";
276 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
277 if (i)
278 OS << ',';
279 OS << format("%#04x", uint8_t(Code[i]));
280 }
281 OS << "]\n";
282 }
283
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000284 return;
285 }
286
287 // Otherwise fall back to a structural printing for now. Eventually we should
288 // always have access to the target specific printer.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000289 Inst.print(OS);
290 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000291}
292
293void MCAsmStreamer::Finish() {
294 OS.flush();
295}
296
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000297MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000298 const MCAsmInfo &MAI, AsmPrinter *AP,
299 MCCodeEmitter *CE) {
300 return new MCAsmStreamer(Context, OS, MAI, AP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000301}