blob: 1a6e8b6c8e1c654f428f4f124eb745454158d088 [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
Chris Lattner46a947d2009-08-17 04:17:34 +000061 virtual void EmitBytes(const 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 Lattner9be3fee2009-07-10 22:20:30 +000064
Chris Lattner46a947d2009-08-17 04:17:34 +000065 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
66 unsigned ValueSize = 1,
67 unsigned MaxBytesToEmit = 0);
Daniel Dunbara11af532009-06-24 01:03:06 +000068
Daniel Dunbar821e3332009-08-31 08:09:28 +000069 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner46a947d2009-08-17 04:17:34 +000070 unsigned char Value = 0);
71
72 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara11af532009-06-24 01:03:06 +000073
Chris Lattner46a947d2009-08-17 04:17:34 +000074 virtual void Finish();
75
76 /// @}
77};
Daniel Dunbar84a29262009-06-24 19:25:34 +000078
Chris Lattner46a947d2009-08-17 04:17:34 +000079} // end anonymous namespace.
Daniel Dunbara11af532009-06-24 01:03:06 +000080
Daniel Dunbar304f6a42009-06-25 21:03:18 +000081static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
82 assert(Bytes && "Invalid size!");
83 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
84}
85
Daniel Dunbar821e3332009-08-31 08:09:28 +000086static inline const MCExpr *truncateToSize(const MCExpr *Value,
87 unsigned Bytes) {
88 // FIXME: Do we really need this routine?
89 return Value;
Daniel Dunbar304f6a42009-06-25 21:03:18 +000090}
91
Chris Lattner975780b2009-08-17 05:49:08 +000092void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +000093 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara11af532009-06-24 01:03:06 +000094 if (Section != CurSection) {
95 CurSection = Section;
Chris Lattner33adcfb2009-08-22 21:43:10 +000096 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara11af532009-06-24 01:03:06 +000097 }
98}
99
100void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar8906ff12009-08-22 07:22:36 +0000101 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000102 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000103
Chris Lattner684c593d2009-09-03 05:46:51 +0000104 Symbol->print(OS, &MAI);
105 OS << ":\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 Lattner684c593d2009-09-03 05:46:51 +0000122 Symbol->print(OS, &MAI);
123 OS << " = ";
124 Value->print(OS, &MAI);
Chris Lattnerd87eb642009-09-03 05:33:01 +0000125 OS << '\n';
Daniel Dunbarfffff912009-10-16 01:34:54 +0000126
127 // FIXME: Lift context changes into super class.
128 Symbol->setValue(Value);
Daniel Dunbara11af532009-06-24 01:03:06 +0000129}
130
Daniel Dunbarfffff912009-10-16 01:34:54 +0000131void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Daniel Dunbara11af532009-06-24 01:03:06 +0000132 SymbolAttr Attribute) {
133 switch (Attribute) {
134 case Global: OS << ".globl"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000135 case Hidden: OS << ".hidden"; break;
136 case IndirectSymbol: OS << ".indirect_symbol"; break;
137 case Internal: OS << ".internal"; break;
138 case LazyReference: OS << ".lazy_reference"; break;
139 case NoDeadStrip: OS << ".no_dead_strip"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000140 case PrivateExtern: OS << ".private_extern"; break;
Daniel Dunbard814b212009-06-24 16:36:52 +0000141 case Protected: OS << ".protected"; break;
142 case Reference: OS << ".reference"; break;
143 case Weak: OS << ".weak"; break;
144 case WeakDefinition: OS << ".weak_definition"; break;
145 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000146 }
147
Chris Lattner684c593d2009-09-03 05:46:51 +0000148 OS << ' ';
149 Symbol->print(OS, &MAI);
150 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000151}
152
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000153void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattner684c593d2009-09-03 05:46:51 +0000154 OS << ".desc" << ' ';
155 Symbol->print(OS, &MAI);
156 OS << ',' << DescValue << '\n';
Kevin Enderby95cf30c2009-07-14 18:17:10 +0000157}
158
Chris Lattner4e4db7a2009-07-07 20:30:46 +0000159void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000160 unsigned ByteAlignment) {
Chris Lattner684c593d2009-09-03 05:46:51 +0000161 OS << ".comm ";
162 Symbol->print(OS, &MAI);
163 OS << ',' << 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) {
Chris Lattner684c593d2009-09-03 05:46:51 +0000179 OS << ',';
180 Symbol->print(OS, &MAI);
181 OS << ',' << Size;
Daniel Dunbar7092c7e2009-08-30 06:17:16 +0000182 if (ByteAlignment != 0)
183 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner9be3fee2009-07-10 22:20:30 +0000184 }
185 OS << '\n';
186}
187
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000188void MCAsmStreamer::EmitBytes(const StringRef &Data) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000189 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +0000190 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Daniel Dunbare44313e2009-08-14 19:59:24 +0000191 OS << ".byte " << (unsigned) (unsigned char) Data[i] << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000192}
193
Daniel Dunbar821e3332009-08-31 08:09:28 +0000194void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000195 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000196 // Need target hooks to know how to print this.
197 switch (Size) {
198 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000199 llvm_unreachable("Invalid size for machine code value!");
Daniel Dunbara11af532009-06-24 01:03:06 +0000200 case 1: OS << ".byte"; break;
Daniel Dunbarf5e75a12009-06-24 16:05:35 +0000201 case 2: OS << ".short"; break;
Daniel Dunbara11af532009-06-24 01:03:06 +0000202 case 4: OS << ".long"; break;
203 case 8: OS << ".quad"; break;
204 }
205
Chris Lattnerd87eb642009-09-03 05:33:01 +0000206 OS << ' ';
Chris Lattner684c593d2009-09-03 05:46:51 +0000207 truncateToSize(Value, Size)->print(OS, &MAI);
Chris Lattnerd87eb642009-09-03 05:33:01 +0000208 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000209}
210
Daniel Dunbar84a29262009-06-24 19:25:34 +0000211void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
212 unsigned ValueSize,
213 unsigned MaxBytesToEmit) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000214 // Some assemblers don't support non-power of two alignments, so we always
215 // emit alignments as a power of two if possible.
216 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6e579c62009-08-19 06:35:36 +0000217 switch (ValueSize) {
218 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattner33adcfb2009-08-22 21:43:10 +0000219 case 1: OS << MAI.getAlignDirective(); break;
220 // FIXME: use MAI for this!
Chris Lattner6e579c62009-08-19 06:35:36 +0000221 case 2: OS << ".p2alignw "; break;
222 case 4: OS << ".p2alignl "; break;
223 case 8: llvm_unreachable("Unsupported alignment size!");
224 }
Chris Lattner663c2d22009-08-19 06:12:02 +0000225
Chris Lattner33adcfb2009-08-22 21:43:10 +0000226 if (MAI.getAlignmentIsInBytes())
Chris Lattner663c2d22009-08-19 06:12:02 +0000227 OS << ByteAlignment;
228 else
229 OS << Log2_32(ByteAlignment);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000230
Chris Lattner663c2d22009-08-19 06:12:02 +0000231 if (Value || MaxBytesToEmit) {
Chris Lattner579531c2009-09-03 04:01:10 +0000232 OS << ", 0x";
233 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner663c2d22009-08-19 06:12:02 +0000234
235 if (MaxBytesToEmit)
236 OS << ", " << MaxBytesToEmit;
237 }
238 OS << '\n';
239 return;
240 }
241
242 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000243 // FIXME: Parameterize this based on MAI.
Daniel Dunbar84a29262009-06-24 19:25:34 +0000244 switch (ValueSize) {
Chris Lattner663c2d22009-08-19 06:12:02 +0000245 default: llvm_unreachable("Invalid size for machine code value!");
246 case 1: OS << ".balign"; break;
247 case 2: OS << ".balignw"; break;
248 case 4: OS << ".balignl"; break;
249 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbar84a29262009-06-24 19:25:34 +0000250 }
251
Chris Lattner663c2d22009-08-19 06:12:02 +0000252 OS << ' ' << ByteAlignment;
Daniel Dunbar304f6a42009-06-25 21:03:18 +0000253 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbar84a29262009-06-24 19:25:34 +0000254 if (MaxBytesToEmit)
255 OS << ", " << MaxBytesToEmit;
256 OS << '\n';
257}
258
Daniel Dunbar821e3332009-08-31 08:09:28 +0000259void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbar84a29262009-06-24 19:25:34 +0000260 unsigned char Value) {
261 // FIXME: Verify that Offset is associated with the current section.
Chris Lattnerd87eb642009-09-03 05:33:01 +0000262 OS << ".org ";
Chris Lattner684c593d2009-09-03 05:46:51 +0000263 Offset->print(OS, &MAI);
Chris Lattnerd87eb642009-09-03 05:33:01 +0000264 OS << ", " << (unsigned) Value << '\n';
Daniel Dunbar84a29262009-06-24 19:25:34 +0000265}
266
Daniel Dunbara11af532009-06-24 01:03:06 +0000267void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar71d259b2009-06-24 17:00:42 +0000268 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000269
270 // If we have an AsmPrinter, use that to print.
Chris Lattner90edac02009-09-14 03:02:37 +0000271 if (InstPrinter) {
272 InstPrinter->printInst(&Inst);
Chris Lattner1b6570e2009-09-13 22:24:34 +0000273 OS << '\n';
Daniel Dunbara356aea2009-08-27 07:58:57 +0000274
275 // Show the encoding if we have a code emitter.
276 if (Emitter) {
277 SmallString<256> Code;
278 raw_svector_ostream VecOS(Code);
279 Emitter->EncodeInstruction(Inst, VecOS);
280 VecOS.flush();
281
282 OS.indent(20);
283 OS << " # encoding: [";
284 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
285 if (i)
286 OS << ',';
287 OS << format("%#04x", uint8_t(Code[i]));
288 }
289 OS << "]\n";
290 }
291
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000292 return;
293 }
294
295 // Otherwise fall back to a structural printing for now. Eventually we should
296 // always have access to the target specific printer.
Chris Lattner684c593d2009-09-03 05:46:51 +0000297 Inst.print(OS, &MAI);
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000298 OS << '\n';
Daniel Dunbara11af532009-06-24 01:03:06 +0000299}
300
301void MCAsmStreamer::Finish() {
302 OS.flush();
303}
304
Daniel Dunbarc22e0b22009-08-14 03:48:55 +0000305MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Chris Lattner90edac02009-09-14 03:02:37 +0000306 const MCAsmInfo &MAI, MCInstPrinter *IP,
Daniel Dunbar4a0abd82009-08-27 00:51:57 +0000307 MCCodeEmitter *CE) {
Chris Lattner90edac02009-09-14 03:02:37 +0000308 return new MCAsmStreamer(Context, OS, MAI, IP, CE);
Daniel Dunbara11af532009-06-24 01:03:06 +0000309}