blob: f334600643642170066577b336535081e00bdea1 [file] [log] [blame]
Daniel Dunbara5cc0032009-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 Dunbar12266ad2009-08-27 00:51:57 +000011#include "llvm/ADT/SmallString.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000012#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar12266ad2009-08-27 00:51:57 +000013#include "llvm/MC/MCCodeEmitter.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbarfc387ba2009-07-01 06:35:03 +000016#include "llvm/MC/MCInst.h"
Chris Lattnera835afd2009-09-14 03:02:37 +000017#include "llvm/MC/MCInstPrinter.h"
Chris Lattner7f1ac7f2009-08-10 18:15:01 +000018#include "llvm/MC/MCSectionMachO.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000019#include "llvm/MC/MCSymbol.h"
Edwin Török675d5622009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner4891d142009-08-19 06:12:02 +000021#include "llvm/Support/MathExtras.h"
Daniel Dunbar12266ad2009-08-27 00:51:57 +000022#include "llvm/Support/Format.h"
Daniel Dunbara5cc0032009-06-24 01:03:06 +000023#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26namespace {
27
Chris Lattner241c5f82009-08-17 04:17:34 +000028class MCAsmStreamer : public MCStreamer {
29 raw_ostream &OS;
Chris Lattnera5ef4d32009-08-22 21:43:10 +000030 const MCAsmInfo &MAI;
Chris Lattnera835afd2009-09-14 03:02:37 +000031 MCInstPrinter *InstPrinter;
Daniel Dunbar12266ad2009-08-27 00:51:57 +000032 MCCodeEmitter *Emitter;
Chris Lattner241c5f82009-08-17 04:17:34 +000033public:
Chris Lattner621c44d2009-08-22 20:48:53 +000034 MCAsmStreamer(MCContext &Context, raw_ostream &_OS, const MCAsmInfo &tai,
Chris Lattnera835afd2009-09-14 03:02:37 +000035 MCInstPrinter *_Printer, MCCodeEmitter *_Emitter)
36 : MCStreamer(Context), OS(_OS), MAI(tai), InstPrinter(_Printer),
Daniel Dunbar12266ad2009-08-27 00:51:57 +000037 Emitter(_Emitter) {}
Chris Lattner241c5f82009-08-17 04:17:34 +000038 ~MCAsmStreamer() {}
Daniel Dunbara5cc0032009-06-24 01:03:06 +000039
Chris Lattner241c5f82009-08-17 04:17:34 +000040 /// @name MCStreamer Interface
41 /// @{
Daniel Dunbara5cc0032009-06-24 01:03:06 +000042
Chris Lattner2f579bd2009-08-17 05:49:08 +000043 virtual void SwitchSection(const MCSection *Section);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000044
Chris Lattner241c5f82009-08-17 04:17:34 +000045 virtual void EmitLabel(MCSymbol *Symbol);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000046
Chris Lattner241c5f82009-08-17 04:17:34 +000047 virtual void EmitAssemblerFlag(AssemblerFlag Flag);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000048
Daniel Dunbard79f4322009-08-31 08:09:28 +000049 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000050
Chris Lattner241c5f82009-08-17 04:17:34 +000051 virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
Kevin Enderby3694db22009-07-13 21:03:15 +000052
Chris Lattner241c5f82009-08-17 04:17:34 +000053 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000054
Chris Lattner241c5f82009-08-17 04:17:34 +000055 virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbarb03d1172009-08-30 06:17:16 +000056 unsigned ByteAlignment);
Kevin Enderby8ae702b2009-07-14 18:17:10 +000057
Daniel Dunbar15f1a5c2009-08-28 05:48:22 +000058 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
Daniel Dunbarb03d1172009-08-30 06:17:16 +000059 unsigned Size = 0, unsigned ByteAlignment = 0);
Kevin Enderbyd7895a12009-07-14 21:35:03 +000060
Chris Lattnera71dc602010-01-19 19:46:13 +000061 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
Chris Lattnere29dbe32009-07-07 20:30:46 +000062
Chris Lattnera71dc602010-01-19 19:46:13 +000063 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
64 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
65 unsigned AddrSpace);
Chris Lattner349007a2009-07-10 22:20:30 +000066
Chris Lattner241c5f82009-08-17 04:17:34 +000067 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
68 unsigned ValueSize = 1,
69 unsigned MaxBytesToEmit = 0);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000070
Daniel Dunbard79f4322009-08-31 08:09:28 +000071 virtual void EmitValueToOffset(const MCExpr *Offset,
Chris Lattner241c5f82009-08-17 04:17:34 +000072 unsigned char Value = 0);
73
74 virtual void EmitInstruction(const MCInst &Inst);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000075
Chris Lattner241c5f82009-08-17 04:17:34 +000076 virtual void Finish();
77
78 /// @}
79};
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +000080
Chris Lattner241c5f82009-08-17 04:17:34 +000081} // end anonymous namespace.
Daniel Dunbara5cc0032009-06-24 01:03:06 +000082
Daniel Dunbar7c48d622009-06-25 21:03:18 +000083static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
84 assert(Bytes && "Invalid size!");
85 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
86}
87
Daniel Dunbard79f4322009-08-31 08:09:28 +000088static inline const MCExpr *truncateToSize(const MCExpr *Value,
89 unsigned Bytes) {
90 // FIXME: Do we really need this routine?
91 return Value;
Daniel Dunbar7c48d622009-06-25 21:03:18 +000092}
93
Chris Lattner2f579bd2009-08-17 05:49:08 +000094void MCAsmStreamer::SwitchSection(const MCSection *Section) {
Chris Lattner73266f92009-08-19 05:49:37 +000095 assert(Section && "Cannot switch to a null section!");
Daniel Dunbara5cc0032009-06-24 01:03:06 +000096 if (Section != CurSection) {
97 CurSection = Section;
Chris Lattnera5ef4d32009-08-22 21:43:10 +000098 Section->PrintSwitchToSection(MAI, OS);
Daniel Dunbara5cc0032009-06-24 01:03:06 +000099 }
100}
101
102void MCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000103 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
Daniel Dunbar75a52922009-06-24 17:00:42 +0000104 assert(CurSection && "Cannot emit before setting section!");
Daniel Dunbar75a52922009-06-24 17:00:42 +0000105
Chris Lattnerce409842010-01-17 21:43:43 +0000106 OS << *Symbol << ":\n";
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000107 Symbol->setSection(*CurSection);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000108}
109
Kevin Enderby68c81be2009-07-16 17:56:39 +0000110void MCAsmStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
111 switch (Flag) {
Daniel Dunbar6a3d1732009-08-13 23:36:34 +0000112 default: assert(0 && "Invalid flag!");
Kevin Enderby68c81be2009-07-16 17:56:39 +0000113 case SubsectionsViaSymbols: OS << ".subsections_via_symbols"; break;
114 }
115 OS << '\n';
Kevin Enderby3694db22009-07-13 21:03:15 +0000116}
117
Daniel Dunbard79f4322009-08-31 08:09:28 +0000118void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
Daniel Dunbar1c2ee9f2009-08-22 07:22:36 +0000119 // Only absolute symbols can be redefined.
120 assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
121 "Cannot define a symbol twice!");
Daniel Dunbar75a52922009-06-24 17:00:42 +0000122
Chris Lattner1e9a11b2010-01-18 00:37:40 +0000123 OS << *Symbol << " = " << *Value << '\n';
Daniel Dunbar86cd1e92009-10-16 01:34:54 +0000124
125 // FIXME: Lift context changes into super class.
Daniel Dunbar9ce249c2009-10-16 01:57:39 +0000126 // FIXME: Set associated section.
Daniel Dunbar86cd1e92009-10-16 01:34:54 +0000127 Symbol->setValue(Value);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000128}
129
Daniel Dunbar86cd1e92009-10-16 01:34:54 +0000130void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000131 SymbolAttr Attribute) {
132 switch (Attribute) {
Daniel Dunbarf9a37ec2009-10-16 01:58:15 +0000133 case Global: OS << ".globl"; break;
134 case Hidden: OS << ".hidden"; break;
Daniel Dunbarf7dafd32009-06-24 16:36:52 +0000135 case IndirectSymbol: OS << ".indirect_symbol"; break;
Daniel Dunbarf9a37ec2009-10-16 01:58:15 +0000136 case Internal: OS << ".internal"; break;
137 case LazyReference: OS << ".lazy_reference"; break;
138 case NoDeadStrip: OS << ".no_dead_strip"; break;
139 case PrivateExtern: OS << ".private_extern"; break;
140 case Protected: OS << ".protected"; break;
141 case Reference: OS << ".reference"; break;
142 case Weak: OS << ".weak"; break;
Daniel Dunbarf7dafd32009-06-24 16:36:52 +0000143 case WeakDefinition: OS << ".weak_definition"; break;
Daniel Dunbarf9a37ec2009-10-16 01:58:15 +0000144 case WeakReference: OS << ".weak_reference"; break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000145 }
146
Chris Lattnerce409842010-01-17 21:43:43 +0000147 OS << ' ' << *Symbol << '\n';
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000148}
149
Kevin Enderby8ae702b2009-07-14 18:17:10 +0000150void MCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
Chris Lattnerce409842010-01-17 21:43:43 +0000151 OS << ".desc" << ' ' << *Symbol << ',' << DescValue << '\n';
Kevin Enderby8ae702b2009-07-14 18:17:10 +0000152}
153
Chris Lattnere29dbe32009-07-07 20:30:46 +0000154void MCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000155 unsigned ByteAlignment) {
Chris Lattnerecf1cdc2010-01-19 06:01:04 +0000156 OS << MAI.getCOMMDirective() << *Symbol << ',' << Size;
157 if (ByteAlignment != 0 && MAI.getCOMMDirectiveTakesAlignment()) {
158 if (MAI.getAlignmentIsInBytes())
159 OS << ',' << ByteAlignment;
160 else
161 OS << ',' << Log2_32(ByteAlignment);
162 }
Chris Lattnere29dbe32009-07-07 20:30:46 +0000163 OS << '\n';
164}
165
Daniel Dunbar15f1a5c2009-08-28 05:48:22 +0000166void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000167 unsigned Size, unsigned ByteAlignment) {
Daniel Dunbar6a3d1732009-08-13 23:36:34 +0000168 // Note: a .zerofill directive does not switch sections.
Chris Lattner26aabb92009-08-08 23:39:42 +0000169 OS << ".zerofill ";
170
171 // This is a mach-o specific directive.
Chris Lattner72a676a2009-08-10 01:39:42 +0000172 const MCSectionMachO *MOSection = ((const MCSectionMachO*)Section);
Daniel Dunbar0305b2d2009-08-14 18:51:45 +0000173 OS << MOSection->getSegmentName() << "," << MOSection->getSectionName();
Chris Lattner26aabb92009-08-08 23:39:42 +0000174
Chris Lattner349007a2009-07-10 22:20:30 +0000175 if (Symbol != NULL) {
Chris Lattnerce409842010-01-17 21:43:43 +0000176 OS << ',' << *Symbol << ',' << Size;
Daniel Dunbarb03d1172009-08-30 06:17:16 +0000177 if (ByteAlignment != 0)
178 OS << ',' << Log2_32(ByteAlignment);
Chris Lattner349007a2009-07-10 22:20:30 +0000179 }
180 OS << '\n';
181}
182
Chris Lattnera71dc602010-01-19 19:46:13 +0000183void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000184 assert(CurSection && "Cannot emit contents before setting section!");
Chris Lattnera71dc602010-01-19 19:46:13 +0000185 const char *Directive = MAI.getData8bitsDirective(AddrSpace);
Daniel Dunbar9a7b61d2009-07-27 21:49:56 +0000186 for (unsigned i = 0, e = Data.size(); i != e; ++i)
Chris Lattnera71dc602010-01-19 19:46:13 +0000187 OS << Directive << (unsigned)(unsigned char)Data[i] << '\n';
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000188}
189
Chris Lattnera71dc602010-01-19 19:46:13 +0000190void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
191 unsigned AddrSpace) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000192 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000193 // Need target hooks to know how to print this.
194 switch (Size) {
Chris Lattnera71dc602010-01-19 19:46:13 +0000195 default: assert(0 && "Invalid size for machine code value!");
196 case 1: OS << MAI.getData8bitsDirective(AddrSpace); break;
197 case 2: OS << MAI.getData16bitsDirective(AddrSpace); break;
198 case 4: OS << MAI.getData32bitsDirective(AddrSpace); break;
199 case 8: OS << MAI.getData64bitsDirective(AddrSpace); break;
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000200 }
Chris Lattnera71dc602010-01-19 19:46:13 +0000201
202 OS << *truncateToSize(Value, Size) << '\n';
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000203}
204
Chris Lattner7a787a22010-01-19 18:52:28 +0000205/// EmitFill - Emit NumBytes bytes worth of the value specified by
206/// FillValue. This implements directives such as '.space'.
Chris Lattnera71dc602010-01-19 19:46:13 +0000207void MCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
208 unsigned AddrSpace) {
Chris Lattner3b296362010-01-19 18:58:52 +0000209 if (NumBytes == 0) return;
210
Chris Lattnera71dc602010-01-19 19:46:13 +0000211 if (AddrSpace == 0)
212 if (const char *ZeroDirective = MAI.getZeroDirective()) {
213 OS << ZeroDirective << NumBytes;
214 if (FillValue != 0)
215 OS << ',' << (int)FillValue;
216 OS << '\n';
217 return;
218 }
219
220 // Emit a byte at a time.
221 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
Chris Lattner7a787a22010-01-19 18:52:28 +0000222}
223
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000224void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
225 unsigned ValueSize,
226 unsigned MaxBytesToEmit) {
Chris Lattner4891d142009-08-19 06:12:02 +0000227 // Some assemblers don't support non-power of two alignments, so we always
228 // emit alignments as a power of two if possible.
229 if (isPowerOf2_32(ByteAlignment)) {
Chris Lattner6ad99b22009-08-19 06:35:36 +0000230 switch (ValueSize) {
231 default: llvm_unreachable("Invalid size for machine code value!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000232 case 1: OS << MAI.getAlignDirective(); break;
233 // FIXME: use MAI for this!
Chris Lattner6ad99b22009-08-19 06:35:36 +0000234 case 2: OS << ".p2alignw "; break;
235 case 4: OS << ".p2alignl "; break;
236 case 8: llvm_unreachable("Unsupported alignment size!");
237 }
Chris Lattner4891d142009-08-19 06:12:02 +0000238
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000239 if (MAI.getAlignmentIsInBytes())
Chris Lattner4891d142009-08-19 06:12:02 +0000240 OS << ByteAlignment;
241 else
242 OS << Log2_32(ByteAlignment);
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000243
Chris Lattner4891d142009-08-19 06:12:02 +0000244 if (Value || MaxBytesToEmit) {
Chris Lattner27f44ad2009-09-03 04:01:10 +0000245 OS << ", 0x";
246 OS.write_hex(truncateToSize(Value, ValueSize));
Chris Lattner4891d142009-08-19 06:12:02 +0000247
248 if (MaxBytesToEmit)
249 OS << ", " << MaxBytesToEmit;
250 }
251 OS << '\n';
252 return;
253 }
254
255 // Non-power of two alignment. This is not widely supported by assemblers.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000256 // FIXME: Parameterize this based on MAI.
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000257 switch (ValueSize) {
Chris Lattner4891d142009-08-19 06:12:02 +0000258 default: llvm_unreachable("Invalid size for machine code value!");
259 case 1: OS << ".balign"; break;
260 case 2: OS << ".balignw"; break;
261 case 4: OS << ".balignl"; break;
262 case 8: llvm_unreachable("Unsupported alignment size!");
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000263 }
264
Chris Lattner4891d142009-08-19 06:12:02 +0000265 OS << ' ' << ByteAlignment;
Daniel Dunbar7c48d622009-06-25 21:03:18 +0000266 OS << ", " << truncateToSize(Value, ValueSize);
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000267 if (MaxBytesToEmit)
268 OS << ", " << MaxBytesToEmit;
269 OS << '\n';
270}
271
Daniel Dunbard79f4322009-08-31 08:09:28 +0000272void MCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000273 unsigned char Value) {
274 // FIXME: Verify that Offset is associated with the current section.
Chris Lattner1e9a11b2010-01-18 00:37:40 +0000275 OS << ".org " << *Offset << ", " << (unsigned) Value << '\n';
Daniel Dunbarf9e2b0c2009-06-24 19:25:34 +0000276}
277
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000278void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Daniel Dunbar75a52922009-06-24 17:00:42 +0000279 assert(CurSection && "Cannot emit contents before setting section!");
Daniel Dunbar575db962009-08-14 03:48:55 +0000280
281 // If we have an AsmPrinter, use that to print.
Chris Lattnera835afd2009-09-14 03:02:37 +0000282 if (InstPrinter) {
283 InstPrinter->printInst(&Inst);
Chris Lattner98544f72009-09-13 22:24:34 +0000284 OS << '\n';
Daniel Dunbar1f328162009-08-27 07:58:57 +0000285
286 // Show the encoding if we have a code emitter.
287 if (Emitter) {
288 SmallString<256> Code;
289 raw_svector_ostream VecOS(Code);
290 Emitter->EncodeInstruction(Inst, VecOS);
291 VecOS.flush();
292
293 OS.indent(20);
294 OS << " # encoding: [";
295 for (unsigned i = 0, e = Code.size(); i != e; ++i) {
296 if (i)
297 OS << ',';
298 OS << format("%#04x", uint8_t(Code[i]));
299 }
300 OS << "]\n";
301 }
302
Daniel Dunbar575db962009-08-14 03:48:55 +0000303 return;
304 }
305
306 // Otherwise fall back to a structural printing for now. Eventually we should
307 // always have access to the target specific printer.
Chris Lattner0fe3a1e2009-09-03 05:46:51 +0000308 Inst.print(OS, &MAI);
Daniel Dunbar6e966212009-08-31 08:08:38 +0000309 OS << '\n';
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000310}
311
312void MCAsmStreamer::Finish() {
313 OS.flush();
314}
315
Daniel Dunbar575db962009-08-14 03:48:55 +0000316MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
Chris Lattnera835afd2009-09-14 03:02:37 +0000317 const MCAsmInfo &MAI, MCInstPrinter *IP,
Daniel Dunbar12266ad2009-08-27 00:51:57 +0000318 MCCodeEmitter *CE) {
Chris Lattnera835afd2009-09-14 03:02:37 +0000319 return new MCAsmStreamer(Context, OS, MAI, IP, CE);
Daniel Dunbara5cc0032009-06-24 01:03:06 +0000320}