blob: 317e74d5b320a1d59b2b93b82a754073e51c972d [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 Dunbar8c2eebe2009-08-31 08:08:38 +000012#include "llvm/MC/MCAsmInfo.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 Dunbar8c2eebe2009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbarabde2982009-07-01 06:35:03 +000016#include "llvm/MC/MCInst.h"
Chris Lattner90edac02009-09-14 03:02:37 +000017#include "llvm/MC/MCInstPrinter.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"
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 Lattner90edac02009-09-14 03:02:37 +000031 MCInstPrinter *InstPrinter;
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,
Chris Lattner90edac02009-09-14 03:02:37 +000035 MCInstPrinter *_Printer, MCCodeEmitter *_Emitter)
36 : MCStreamer(Context), OS(_OS), MAI(tai), InstPrinter(_Printer),
Daniel Dunbar4a0abd82009-08-27 00:51:57 +000037 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
Daniel Dunbar821e3332009-08-31 08:09:28 +000049 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara11af532009-06-24 01:03:06 +000050
Chris Lattner46a947d2009-08-17 04:17:34 +000051 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderbya5c78322009-07-13 21:03:15 +000052
Chris Lattner46a947d2009-08-17 04:17:34 +000053 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara11af532009-06-24 01:03:06 +000054
Chris Lattner46a947d2009-08-17 04:17:34 +000055 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000056 unsigned ByteAlignment);
Kevin Enderby95cf30c2009-07-14 18:17:10 +000057
Daniel Dunbar8751b942009-08-28 05:48:22 +000058 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +000059 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderby71148242009-07-14 21:35:03 +000060
Daniel Dunbar2928c832009-11-06 10:58:06 +000061 virtual void EmitBytes(StringRef Data);
Chris Lattner4e4db7a2009-07-07 20:30:46 +000062
Daniel Dunbar821e3332009-08-31 08:09:28 +000063 virtual void EmitValue(const MCExpr *Value, unsigned Size);
Chris Lattner6113b3d2010-01-19 18:52:28 +000064 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue);
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
Daniel Dunbar821e3332009-08-31 08:09:28 +000070 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +000071 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 Dunbar304f6a42009-06-25 21:03:18 +000082static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
83 assert(Bytes && "Invalid size!");
84 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
85}
86
Daniel Dunbar821e3332009-08-31 08:09:28 +000087static inline const MCExpr *truncateToSize(const MCExpr *Value,
88 unsigned Bytes) {
89 // FIXME: Do we really need this routine?
90 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +000091}
92
Chris Lattner975780b2009-08-17 05:49:08 +000093void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +000094 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +000095 if (Section != CurSection) {
96 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +000097 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +000098 }
99}
100
101void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000102 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000103 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000104
Chris Lattner10b318b2010-01-17 21:43:43 +0000105 OS << *Symbol << ":\n";
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000106 Symbol->setSection(*CurSection);
Daniel Dunbara11af532009-06-24 01:03:06 +0000107}
108
Kevin Enderbyf96db462009-07-16 17:56:39 +0000109void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
110 switch (Flag) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000111 default: assert(0 && "Invalid flag!");
Kevin Enderbyf96db462009-07-16 17:56:39 +0000112 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
113 }
114 OS << '\n';
Kevin Enderbya5c78322009-07-13 21:03:15 +0000115}
116
Daniel Dunbar821e3332009-08-31 08:09:28 +0000117void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000118 // Only absolute symbols can be redefined.
119 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
120 "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000121
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000122 OS << *Symbol << " = " << *Value << '\n';
Daniel Dunbarfffff912009-10-16 01:34:54 +0000123
124 // FIXME: Lift context changes into super class.
Daniel Dunbar75773ff2009-10-16 01:57:39 +0000125 // FIXME: Set associated section.
Daniel Dunbarfffff912009-10-16 01:34:54 +0000126 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000127}
128
Daniel Dunbarfffff912009-10-16 01:34:54 +0000129void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Daniel Dunbara11af532009-06-24 01:03:06 +0000130 SymbolAttr Attribute) {
131 switch (Attribute) {
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000132 case Global: OS << ".globl"; break;
133 case Hidden: OS << ".hidden"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000134 case IndirectSymbol: OS << ".indirect_symbol"; break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000135 case Internal: OS << ".internal"; break;
136 case LazyReference: OS << ".lazy_reference"; break;
137 case NoDeadStrip: OS << ".no_dead_strip"; break;
138 case PrivateExtern: OS << ".private_extern"; break;
139 case Protected: OS << ".protected"; break;
140 case Reference: OS << ".reference"; break;
141 case Weak: OS << ".weak"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000142 case WeakDefinition: OS << ".weak_definition"; break;
Daniel Dunbar7eb85192009-10-16 01:58:15 +0000143 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000144 }
145
Chris Lattner10b318b2010-01-17 21:43:43 +0000146 OS << ' ' << *Symbol << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000147}
148
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000149void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000150 OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000151}
152
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000153void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000154 unsigned ByteAlignment) {
Chris Lattner4ed54382010-01-19 06:01:04 +0000155 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
156 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
157 if (MAI.getAlignmentIsInBytes())
158 OS << ',' << ByteAlignment;
159 else
160 OS << ',' << Log2_32(ByteAlignment);
161 }
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000162 OS << '\n';
163}
164
Daniel Dunbar8751b942009-08-28 05:48:22 +0000165void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000166 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar011e4db2009-08-13 23:36:34 +0000167 // Note: a .zerofill directive does not switch sections.
Chris Lattner93b6db32009-08-08 23:39:42 +0000168 OS << ".zerofill ";
169
170 // This is a mach-o specific directive.
Chris Lattnerff4bc462009-08-10 01:39:42 +0000171 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar12de0df2009-08-14 18:51:45 +0000172 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner93b6db32009-08-08 23:39:42 +0000173
Chris Lattner9be3fee2009-07-10 22:20:30 +0000174 if (Symbol != NULL) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000175 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000176 if (ByteAlignment != 0)
177 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000178 }
179 OS << '\n';
180}
181
Daniel Dunbar2928c832009-11-06 10:58:06 +0000182void MCAsmStreamer::EmitBytes(StringRef Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000183 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000184 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000185 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000186}
187
Daniel Dunbar821e3332009-08-31 08:09:28 +0000188void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000189 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000190 // Need target hooks to know how to print this.
191 switch (Size) {
192 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000193 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000194 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000195 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000196 case 4: OS << ".long"; break;
197 case 8: OS << ".quad"; break;
198 }
199
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000200 OS << ' ' << *truncateToSize(Value, Size) << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000201}
202
Chris Lattner6113b3d2010-01-19 18:52:28 +0000203/// EmitFill - Emit NumBytes bytes worth of the value specified by
204/// FillValue. This implements directives such as '.space'.
205void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue) {
206 if (const char *ZeroDirective = MAI.getZeroDirective()) {
207 OS << ZeroDirective << NumBytes;
208 if (FillValue != 0)
209 OS << ',' << (int)FillValue;
210 OS << '\n';
211 } else {
212 // Emit a byte at a time.
213 MCStreamer::EmitFill(NumBytes, FillValue);
214 }
215}
216
Daniel Dunbar84a29262009-06-24 19:25:34 +0000217void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
218 unsigned ValueSize,
219 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000220 // Some assemblers don't support non-power of two alignments, so we always
221 // emit alignments as a power of two if possible.
222 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000223 switch (ValueSize) {
224 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000225 case 1: OS << MAI.getAlignDirective(); break;
226 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000227 case 2: OS << ".p2alignw "; break;
228 case 4: OS << ".p2alignl "; break;
229 case 8: llvm_unreachable("Unsupported alignment size!");
230 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000231
Chris Lattner33adcfb2009-08-22 21:43:10 +0000232 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000233 OS << ByteAlignment;
234 else
235 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000236
Chris Lattner663c2d22009-08-19 06:12:02 +0000237 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000238 OS << ", 0x";
239 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000240
241 if (MaxBytesToEmit)
242 OS << ", " << MaxBytesToEmit;
243 }
244 OS << '\n';
245 return;
246 }
247
248 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000249 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000250 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000251 default: llvm_unreachable("Invalid size for machine code value!");
252 case 1: OS << ".balign"; break;
253 case 2: OS << ".balignw"; break;
254 case 4: OS << ".balignl"; break;
255 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000256 }
257
Chris Lattner663c2d22009-08-19 06:12:02 +0000258 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000259 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000260 if (MaxBytesToEmit)
261 OS << ", " << MaxBytesToEmit;
262 OS << '\n';
263}
264
Daniel Dunbar821e3332009-08-31 08:09:28 +0000265void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000266 unsigned char Value) {
267 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000268 OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
Daniel Dunbar84a29262009-06-24 19:25:34 +0000269}
270
Daniel Dunbara11af532009-06-24 01:03:06 +0000271void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000272 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000273
274 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000275 if (InstPrinter) {
276 InstPrinter->printInst(&Inst);
Chris Lattner1b6570e2009-09-13 22:24:34 +0000277 OS << '\n';
Daniel Dunbara356aea2009-08-27 07:58:57 +0000278
279 // Show the encoding if we have a code emitter.
280 if (Emitter) {
281 SmallString<256> Code;
282 raw_svector_ostream VecOS(Code);
283 Emitter->EncodeInstruction(Inst, VecOS);
284 VecOS.flush();
285
286 OS.indent(20);
287 OS << " # encoding: [";
288 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
289 if (i)
290 OS << ',';
291 OS << format("%#04x", uint8_t(Code[i]));
292 }
293 OS << "]\n";
294 }
295
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000296 return;
297 }
298
299 // Otherwise fall back to a structural printing for now. Eventually we should
300 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000301 Inst.print(OS, &MAI);
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000302 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000303}
304
305void MCAsmStreamer::Finish() {
306 OS.flush();
307}
308
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000309MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Chris Lattner90edac02009-09-14 03:02:37 +0000310 const MCAsmInfo &MAI, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000311 MCCodeEmitter *CE) {
Chris Lattner90edac02009-09-14 03:02:37 +0000312 return new MCAsmStreamer(Context, OS, MAI, IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000313}