blob: 3ed67a6a9b47b796d85f17beaf747e0ec39c2325 [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- PTXMCAsmStreamer.cpp - PTX Text Assembly Output -------------------===//
Che-Liang Chioud77f2a42010-11-08 02:58:44 +00002//
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
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000010#include "llvm/ADT/OwningPtr.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000013#include "llvm/MC/MCAsmInfo.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000014#include "llvm/MC/MCCodeEmitter.h"
Che-Liang Chioufc7072c2010-12-22 10:38:51 +000015#include "llvm/MC/MCContext.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000018#include "llvm/MC/MCInstPrinter.h"
19#include "llvm/MC/MCStreamer.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000020#include "llvm/MC/MCSymbol.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000021#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/MathExtras.h"
23#include "llvm/Support/Format.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000024#include "llvm/Support/FormattedStream.h"
Nick Lewycky44d798d2011-10-17 23:05:28 +000025#include "llvm/Support/PathV2.h"
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000026#include "llvm/Support/raw_ostream.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000027
28using namespace llvm;
29
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000030namespace {
31class PTXMCAsmStreamer : public MCStreamer {
32 formatted_raw_ostream &OS;
33 const MCAsmInfo &MAI;
34 OwningPtr<MCInstPrinter> InstPrinter;
35 OwningPtr<MCCodeEmitter> Emitter;
36
37 SmallString<128> CommentToEmit;
38 raw_svector_ostream CommentStream;
39
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000040 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
42
43public:
44 PTXMCAsmStreamer(MCContext &Context,
45 formatted_raw_ostream &os,
Rafael Espindola89b93722010-12-10 07:39:47 +000046 bool isVerboseAsm, bool useLoc,
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000047 MCInstPrinter *printer,
48 MCCodeEmitter *emitter,
49 bool showInst)
50 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
51 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
Rafael Espindola89b93722010-12-10 07:39:47 +000052 IsVerboseAsm(isVerboseAsm),
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000053 ShowInst(showInst) {
54 if (InstPrinter && IsVerboseAsm)
55 InstPrinter->setCommentStream(CommentStream);
56 }
57
58 ~PTXMCAsmStreamer() {}
59
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000060 inline void EmitEOL() {
61 // If we don't have any comments, just emit a \n.
62 if (!IsVerboseAsm) {
63 OS << '\n';
64 return;
65 }
66 EmitCommentsAndEOL();
67 }
68 void EmitCommentsAndEOL();
69
70 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
71 /// all.
72 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
73
74 /// hasRawTextSupport - We support EmitRawText.
75 virtual bool hasRawTextSupport() const { return true; }
76
77 /// AddComment - Add a comment that can be emitted to the generated .s
78 /// file if applicable as a QoI issue to make the output of the compiler
79 /// more readable. This only affects the MCAsmStreamer, and only when
80 /// verbose assembly output is enabled.
81 virtual void AddComment(const Twine &T);
82
83 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
84 virtual void AddEncodingComment(const MCInst &Inst);
85
86 /// GetCommentOS - Return a raw_ostream that comments can be written to.
87 /// Unlike AddComment, you are required to terminate comments with \n if you
88 /// use this method.
89 virtual raw_ostream &GetCommentOS() {
90 if (!IsVerboseAsm)
91 return nulls(); // Discard comments unless in verbose asm mode.
92 return CommentStream;
93 }
94
95 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
96 virtual void AddBlankLine() {
97 EmitEOL();
98 }
99
100 /// @name MCStreamer Interface
101 /// @{
102
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000103 virtual void ChangeSection(const MCSection *Section);
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000104 virtual void InitSections() { /* PTX does not use sections */ }
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000105
106 virtual void EmitLabel(MCSymbol *Symbol);
107
108 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
109
110 virtual void EmitThumbFunc(MCSymbol *Func);
111
112 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
113
114 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
115
Rafael Espindola32a006e2010-12-03 00:55:40 +0000116 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
117 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000118 const MCSymbol *Label,
119 unsigned PointerSize);
Rafael Espindola32a006e2010-12-03 00:55:40 +0000120
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000121 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
122
123 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
124 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
125 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
126 virtual void EmitCOFFSymbolType(int Type);
127 virtual void EndCOFFSymbolDef();
128 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
129 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
130 unsigned ByteAlignment);
131
132 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
133 ///
134 /// @param Symbol - The common symbol to emit.
135 /// @param Size - The size of the common symbol.
Benjamin Kramer36a16012011-09-01 23:04:27 +0000136 /// @param ByteAlignment - The alignment of the common symbol in bytes.
137 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
138 unsigned ByteAlignment);
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000139
140 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
141 unsigned Size = 0, unsigned ByteAlignment = 0);
142
143 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
144 uint64_t Size, unsigned ByteAlignment = 0);
145
146 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
147
Rafael Espindola89b93722010-12-10 07:39:47 +0000148 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000149 unsigned AddrSpace);
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000150 virtual void EmitULEB128Value(const MCExpr *Value);
151 virtual void EmitSLEB128Value(const MCExpr *Value);
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000152 virtual void EmitGPRel32Value(const MCExpr *Value);
153
154
155 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
156 unsigned AddrSpace);
157
158 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
159 unsigned ValueSize = 1,
160 unsigned MaxBytesToEmit = 0);
161
162 virtual void EmitCodeAlignment(unsigned ByteAlignment,
163 unsigned MaxBytesToEmit = 0);
164
Jim Grosbachebd4c052012-01-27 00:37:08 +0000165 virtual bool EmitValueToOffset(const MCExpr *Offset,
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000166 unsigned char Value = 0);
167
168 virtual void EmitFileDirective(StringRef Filename);
Nick Lewycky44d798d2011-10-17 23:05:28 +0000169 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
170 StringRef Filename);
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000171
172 virtual void EmitInstruction(const MCInst &Inst);
173
174 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
175 /// the specified string in the output .s file. This capability is
176 /// indicated by the hasRawTextSupport() predicate.
177 virtual void EmitRawText(StringRef String);
178
Rafael Espindola99b42372012-01-07 03:13:18 +0000179 virtual void FinishImpl();
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000180
181 /// @}
182
183}; // class PTXMCAsmStreamer
184
185}
186
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000187/// TODO: Add appropriate implementation of Emit*() methods when needed
188
189void PTXMCAsmStreamer::AddComment(const Twine &T) {
190 if (!IsVerboseAsm) return;
191
192 // Make sure that CommentStream is flushed.
193 CommentStream.flush();
194
195 T.toVector(CommentToEmit);
196 // Each comment goes on its own line.
197 CommentToEmit.push_back('\n');
198
199 // Tell the comment stream that the vector changed underneath it.
200 CommentStream.resync();
201}
202
203void PTXMCAsmStreamer::EmitCommentsAndEOL() {
204 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
205 OS << '\n';
206 return;
207 }
208
209 CommentStream.flush();
210 StringRef Comments = CommentToEmit.str();
211
212 assert(Comments.back() == '\n' &&
213 "Comment array not newline terminated");
214 do {
215 // Emit a line of comments.
216 OS.PadToColumn(MAI.getCommentColumn());
217 size_t Position = Comments.find('\n');
218 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
219
220 Comments = Comments.substr(Position+1);
221 } while (!Comments.empty());
222
223 CommentToEmit.clear();
224 // Tell the comment stream that the vector changed underneath it.
225 CommentStream.resync();
226}
227
228static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
229 assert(Bytes && "Invalid size!");
230 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
231}
232
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000233void PTXMCAsmStreamer::ChangeSection(const MCSection *Section) {
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000234 assert(Section && "Cannot switch to a null section!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000235}
236
237void PTXMCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
238 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
239 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000240 assert(getCurrentSection() && "Cannot emit before setting section!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000241
242 OS << *Symbol << MAI.getLabelSuffix();
243 EmitEOL();
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000244 Symbol->setSection(*getCurrentSection());
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000245}
246
247void PTXMCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
248
249void PTXMCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {}
250
251void PTXMCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
252 OS << *Symbol << " = " << *Value;
253 EmitEOL();
254
255 // FIXME: Lift context changes into super class.
256 Symbol->setVariableValue(Value);
257}
258
259void PTXMCAsmStreamer::EmitWeakReference(MCSymbol *Alias,
260 const MCSymbol *Symbol) {
261 OS << ".weakref " << *Alias << ", " << *Symbol;
262 EmitEOL();
263}
264
Rafael Espindola32a006e2010-12-03 00:55:40 +0000265void PTXMCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
266 const MCSymbol *LastLabel,
Evan Cheng672b93a2011-07-14 05:43:07 +0000267 const MCSymbol *Label,
268 unsigned PointerSize) {
Rafael Espindola32a006e2010-12-03 00:55:40 +0000269 report_fatal_error("Unimplemented.");
270}
271
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000272void PTXMCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
273 MCSymbolAttr Attribute) {}
274
275void PTXMCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
276
277void PTXMCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
278
279void PTXMCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {}
280
281void PTXMCAsmStreamer::EmitCOFFSymbolType (int Type) {}
282
283void PTXMCAsmStreamer::EndCOFFSymbolDef() {}
284
285void PTXMCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
286
287void PTXMCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
288 unsigned ByteAlignment) {}
289
Benjamin Kramer36a16012011-09-01 23:04:27 +0000290void PTXMCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
291 unsigned ByteAlignment) {}
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000292
293void PTXMCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
294 unsigned Size, unsigned ByteAlignment) {}
295
296void PTXMCAsmStreamer::EmitTBSSSymbol(const MCSection *Section,
297 MCSymbol *Symbol,
298 uint64_t Size, unsigned ByteAlignment) {}
299
300static inline char toOctal(int X) { return (X&7)+'0'; }
301
302static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
303 OS << '"';
304
305 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
306 unsigned char C = Data[i];
307 if (C == '"' || C == '\\') {
308 OS << '\\' << (char)C;
309 continue;
310 }
311
312 if (isprint((unsigned char)C)) {
313 OS << (char)C;
314 continue;
315 }
316
317 switch (C) {
318 case '\b': OS << "\\b"; break;
319 case '\f': OS << "\\f"; break;
320 case '\n': OS << "\\n"; break;
321 case '\r': OS << "\\r"; break;
322 case '\t': OS << "\\t"; break;
323 default:
324 OS << '\\';
325 OS << toOctal(C >> 6);
326 OS << toOctal(C >> 3);
327 OS << toOctal(C >> 0);
328 break;
329 }
330 }
331
332 OS << '"';
333}
334
335void PTXMCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000336 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000337 if (Data.empty()) return;
338
339 if (Data.size() == 1) {
340 OS << MAI.getData8bitsDirective(AddrSpace);
341 OS << (unsigned)(unsigned char)Data[0];
342 EmitEOL();
343 return;
344 }
345
346 // If the data ends with 0 and the target supports .asciz, use it, otherwise
347 // use .ascii
348 if (MAI.getAscizDirective() && Data.back() == 0) {
349 OS << MAI.getAscizDirective();
350 Data = Data.substr(0, Data.size()-1);
351 } else {
352 OS << MAI.getAsciiDirective();
353 }
354
355 OS << ' ';
356 PrintQuotedString(Data, OS);
357 EmitEOL();
358}
359
Rafael Espindola89b93722010-12-10 07:39:47 +0000360void PTXMCAsmStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
Rafael Espindoladebd7e42011-05-01 03:50:49 +0000361 unsigned AddrSpace) {
Rafael Espindola7768a9d2011-02-16 01:08:29 +0000362 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000363 const char *Directive = 0;
364 switch (Size) {
365 default: break;
366 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
367 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
368 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
369 case 8:
370 Directive = MAI.getData64bitsDirective(AddrSpace);
371 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
372 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000373 int64_t IntValue;
374 if (!Value->EvaluateAsAbsolute(IntValue))
375 report_fatal_error("Don't know how to emit this value.");
Evan Cheng1be0e272011-07-15 02:09:41 +0000376 if (getContext().getAsmInfo().isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000377 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
378 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000379 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000380 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
381 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000382 }
383 return;
384 }
385
386 assert(Directive && "Invalid size for machine code value!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000387 OS << Directive << *Value;
388 EmitEOL();
389}
390
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000391void PTXMCAsmStreamer::EmitULEB128Value(const MCExpr *Value) {
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000392 assert(MAI.hasLEB128() && "Cannot print a .uleb");
393 OS << ".uleb128 " << *Value;
394 EmitEOL();
395}
396
Rafael Espindolae8cfbd82011-04-21 23:39:26 +0000397void PTXMCAsmStreamer::EmitSLEB128Value(const MCExpr *Value) {
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000398 assert(MAI.hasLEB128() && "Cannot print a .sleb");
399 OS << ".sleb128 " << *Value;
400 EmitEOL();
401}
402
403void PTXMCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
404 assert(MAI.getGPRel32Directive() != 0);
405 OS << MAI.getGPRel32Directive() << *Value;
406 EmitEOL();
407}
408
409
410/// EmitFill - Emit NumBytes bytes worth of the value specified by
411/// FillValue. This implements directives such as '.space'.
412void PTXMCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
413 unsigned AddrSpace) {
414 if (NumBytes == 0) return;
415
416 if (AddrSpace == 0)
417 if (const char *ZeroDirective = MAI.getZeroDirective()) {
418 OS << ZeroDirective << NumBytes;
419 if (FillValue != 0)
420 OS << ',' << (int)FillValue;
421 EmitEOL();
422 return;
423 }
424
425 // Emit a byte at a time.
426 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
427}
428
Justin Holewinski12785e82011-03-03 13:34:29 +0000429void PTXMCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment,
430 int64_t Value,
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000431 unsigned ValueSize,
432 unsigned MaxBytesToEmit) {
433 // Some assemblers don't support non-power of two alignments, so we always
434 // emit alignments as a power of two if possible.
435 if (isPowerOf2_32(ByteAlignment)) {
436 switch (ValueSize) {
437 default: llvm_unreachable("Invalid size for machine code value!");
438 case 1: OS << MAI.getAlignDirective(); break;
439 // FIXME: use MAI for this!
440 case 2: OS << ".p2alignw "; break;
441 case 4: OS << ".p2alignl "; break;
442 case 8: llvm_unreachable("Unsupported alignment size!");
443 }
444
445 if (MAI.getAlignmentIsInBytes())
446 OS << ByteAlignment;
447 else
448 OS << Log2_32(ByteAlignment);
449
450 if (Value || MaxBytesToEmit) {
451 OS << ", 0x";
452 OS.write_hex(truncateToSize(Value, ValueSize));
453
454 if (MaxBytesToEmit)
455 OS << ", " << MaxBytesToEmit;
456 }
457 EmitEOL();
458 return;
459 }
460
461 // Non-power of two alignment. This is not widely supported by assemblers.
462 // FIXME: Parameterize this based on MAI.
463 switch (ValueSize) {
464 default: llvm_unreachable("Invalid size for machine code value!");
465 case 1: OS << ".balign"; break;
466 case 2: OS << ".balignw"; break;
467 case 4: OS << ".balignl"; break;
468 case 8: llvm_unreachable("Unsupported alignment size!");
469 }
470
471 OS << ' ' << ByteAlignment;
472 OS << ", " << truncateToSize(Value, ValueSize);
473 if (MaxBytesToEmit)
474 OS << ", " << MaxBytesToEmit;
475 EmitEOL();
476}
477
478void PTXMCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
479 unsigned MaxBytesToEmit) {}
480
Jim Grosbachebd4c052012-01-27 00:37:08 +0000481bool PTXMCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
482 unsigned char Value) {return false;}
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000483
484
485void PTXMCAsmStreamer::EmitFileDirective(StringRef Filename) {
486 assert(MAI.hasSingleParameterDotFile());
487 OS << "\t.file\t";
488 PrintQuotedString(Filename, OS);
489 EmitEOL();
490}
491
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000492// FIXME: should we inherit from MCAsmStreamer?
493bool PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
Nick Lewycky44d798d2011-10-17 23:05:28 +0000494 StringRef Directory,
495 StringRef Filename) {
496 if (!Directory.empty()) {
497 if (sys::path::is_absolute(Filename))
498 return EmitDwarfFileDirective(FileNo, "", Filename);
499 SmallString<128> FullPathName = Directory;
500 sys::path::append(FullPathName, Filename);
501 return EmitDwarfFileDirective(FileNo, "", FullPathName);
502 }
503
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000504 OS << "\t.file\t" << FileNo << ' ';
505 PrintQuotedString(Filename, OS);
506 EmitEOL();
Nick Lewycky44d798d2011-10-17 23:05:28 +0000507 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Directory, Filename);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000508}
509
510void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {}
511
512void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) {
Justin Holewinskif51b7e52011-09-30 14:36:36 +0000513 assert(getCurrentSection() && "Cannot emit contents before setting section!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000514
515 // Show the encoding in a comment if we have a code emitter.
516 if (Emitter)
517 AddEncodingComment(Inst);
518
519 // Show the MCInst if enabled.
520 if (ShowInst) {
521 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
522 GetCommentOS() << "\n";
523 }
524
525 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
526 if (InstPrinter)
Owen Anderson98c5dda2011-09-15 23:38:46 +0000527 InstPrinter->printInst(&Inst, OS, "");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000528 else
529 Inst.print(OS, &MAI);
530 EmitEOL();
531}
532
533/// EmitRawText - If this file is backed by an assembly streamer, this dumps
534/// the specified string in the output .s file. This capability is
535/// indicated by the hasRawTextSupport() predicate.
536void PTXMCAsmStreamer::EmitRawText(StringRef String) {
537 if (!String.empty() && String.back() == '\n')
538 String = String.substr(0, String.size()-1);
539 OS << String;
540 EmitEOL();
541}
542
Rafael Espindola99b42372012-01-07 03:13:18 +0000543void PTXMCAsmStreamer::FinishImpl() {}
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000544
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000545namespace llvm {
546 MCStreamer *createPTXAsmStreamer(MCContext &Context,
547 formatted_raw_ostream &OS,
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000548 bool isVerboseAsm, bool useLoc, bool useCFI,
Nick Lewycky44d798d2011-10-17 23:05:28 +0000549 bool useDwarfDirectory,
Rafael Espindola89b93722010-12-10 07:39:47 +0000550 MCInstPrinter *IP,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000551 MCCodeEmitter *CE, MCAsmBackend *MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +0000552 bool ShowInst) {
Rafael Espindola89b93722010-12-10 07:39:47 +0000553 return new PTXMCAsmStreamer(Context, OS, isVerboseAsm, useLoc,
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000554 IP, CE, ShowInst);
555 }
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000556}