blob: 8043baf8cfc88d28e3419ae21260f1019d44a79c [file] [log] [blame]
Che-Liang Chioud77f2a42010-11-08 02:58:44 +00001//===- lib/Target/PTX/PTXMCAsmStreamer.cpp - PTX 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
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/MCContext.h"
15#include "llvm/MC/MCCodeEmitter.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"
25#include "llvm/Support/raw_ostream.h"
Che-Liang Chioud77f2a42010-11-08 02:58:44 +000026
27using namespace llvm;
28
Rafael Espindolaa484f2c2010-11-28 14:48:34 +000029namespace {
30class PTXMCAsmStreamer : public MCStreamer {
31 formatted_raw_ostream &OS;
32 const MCAsmInfo &MAI;
33 OwningPtr<MCInstPrinter> InstPrinter;
34 OwningPtr<MCCodeEmitter> Emitter;
35
36 SmallString<128> CommentToEmit;
37 raw_svector_ostream CommentStream;
38
39 unsigned IsLittleEndian : 1;
40 unsigned IsVerboseAsm : 1;
41 unsigned ShowInst : 1;
42
43public:
44 PTXMCAsmStreamer(MCContext &Context,
45 formatted_raw_ostream &os,
46 bool isLittleEndian,
47 bool isVerboseAsm,
48 MCInstPrinter *printer,
49 MCCodeEmitter *emitter,
50 bool showInst)
51 : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()),
52 InstPrinter(printer), Emitter(emitter), CommentStream(CommentToEmit),
53 IsLittleEndian(isLittleEndian), IsVerboseAsm(isVerboseAsm),
54 ShowInst(showInst) {
55 if (InstPrinter && IsVerboseAsm)
56 InstPrinter->setCommentStream(CommentStream);
57 }
58
59 ~PTXMCAsmStreamer() {}
60
61 bool isLittleEndian() const { return IsLittleEndian; }
62
63 inline void EmitEOL() {
64 // If we don't have any comments, just emit a \n.
65 if (!IsVerboseAsm) {
66 OS << '\n';
67 return;
68 }
69 EmitCommentsAndEOL();
70 }
71 void EmitCommentsAndEOL();
72
73 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
74 /// all.
75 virtual bool isVerboseAsm() const { return IsVerboseAsm; }
76
77 /// hasRawTextSupport - We support EmitRawText.
78 virtual bool hasRawTextSupport() const { return true; }
79
80 /// AddComment - Add a comment that can be emitted to the generated .s
81 /// file if applicable as a QoI issue to make the output of the compiler
82 /// more readable. This only affects the MCAsmStreamer, and only when
83 /// verbose assembly output is enabled.
84 virtual void AddComment(const Twine &T);
85
86 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
87 virtual void AddEncodingComment(const MCInst &Inst);
88
89 /// GetCommentOS - Return a raw_ostream that comments can be written to.
90 /// Unlike AddComment, you are required to terminate comments with \n if you
91 /// use this method.
92 virtual raw_ostream &GetCommentOS() {
93 if (!IsVerboseAsm)
94 return nulls(); // Discard comments unless in verbose asm mode.
95 return CommentStream;
96 }
97
98 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
99 virtual void AddBlankLine() {
100 EmitEOL();
101 }
102
103 /// @name MCStreamer Interface
104 /// @{
105
106 virtual void SwitchSection(const MCSection *Section);
107
108 virtual void InitSections() {
109 }
110
111 virtual void EmitLabel(MCSymbol *Symbol);
112
113 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
114
115 virtual void EmitThumbFunc(MCSymbol *Func);
116
117 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
118
119 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
120
Rafael Espindola32a006e2010-12-03 00:55:40 +0000121 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
122 const MCSymbol *LastLabel,
123 const MCSymbol *Label);
124
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000125 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
126
127 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
128 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol);
129 virtual void EmitCOFFSymbolStorageClass(int StorageClass);
130 virtual void EmitCOFFSymbolType(int Type);
131 virtual void EndCOFFSymbolDef();
132 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
133 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
134 unsigned ByteAlignment);
135
136 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
137 ///
138 /// @param Symbol - The common symbol to emit.
139 /// @param Size - The size of the common symbol.
140 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size);
141
142 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
143 unsigned Size = 0, unsigned ByteAlignment = 0);
144
145 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
146 uint64_t Size, unsigned ByteAlignment = 0);
147
148 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
149
Devang Patelee4854f2010-12-02 21:32:30 +0000150 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace,
151 bool UseSet = false);
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000152 virtual void EmitULEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
153 virtual void EmitSLEB128Value(const MCExpr *Value, unsigned AddrSpace = 0);
154 virtual void EmitGPRel32Value(const MCExpr *Value);
155
156
157 virtual void EmitFill(uint64_t NumBytes, uint8_t FillValue,
158 unsigned AddrSpace);
159
160 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
161 unsigned ValueSize = 1,
162 unsigned MaxBytesToEmit = 0);
163
164 virtual void EmitCodeAlignment(unsigned ByteAlignment,
165 unsigned MaxBytesToEmit = 0);
166
167 virtual void EmitValueToOffset(const MCExpr *Offset,
168 unsigned char Value = 0);
169
170 virtual void EmitFileDirective(StringRef Filename);
171 virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Filename);
172
173 virtual void EmitInstruction(const MCInst &Inst);
174
175 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
176 /// the specified string in the output .s file. This capability is
177 /// indicated by the hasRawTextSupport() predicate.
178 virtual void EmitRawText(StringRef String);
179
180 virtual void Finish();
181
182 /// @}
183
184}; // class PTXMCAsmStreamer
185
186}
187
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000188/// TODO: Add appropriate implementation of Emit*() methods when needed
189
190void PTXMCAsmStreamer::AddComment(const Twine &T) {
191 if (!IsVerboseAsm) return;
192
193 // Make sure that CommentStream is flushed.
194 CommentStream.flush();
195
196 T.toVector(CommentToEmit);
197 // Each comment goes on its own line.
198 CommentToEmit.push_back('\n');
199
200 // Tell the comment stream that the vector changed underneath it.
201 CommentStream.resync();
202}
203
204void PTXMCAsmStreamer::EmitCommentsAndEOL() {
205 if (CommentToEmit.empty() && CommentStream.GetNumBytesInBuffer() == 0) {
206 OS << '\n';
207 return;
208 }
209
210 CommentStream.flush();
211 StringRef Comments = CommentToEmit.str();
212
213 assert(Comments.back() == '\n' &&
214 "Comment array not newline terminated");
215 do {
216 // Emit a line of comments.
217 OS.PadToColumn(MAI.getCommentColumn());
218 size_t Position = Comments.find('\n');
219 OS << MAI.getCommentString() << ' ' << Comments.substr(0, Position) << '\n';
220
221 Comments = Comments.substr(Position+1);
222 } while (!Comments.empty());
223
224 CommentToEmit.clear();
225 // Tell the comment stream that the vector changed underneath it.
226 CommentStream.resync();
227}
228
229static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
230 assert(Bytes && "Invalid size!");
231 return Value & ((uint64_t) (int64_t) -1 >> (64 - Bytes * 8));
232}
233
234void PTXMCAsmStreamer::SwitchSection(const MCSection *Section) {
235 assert(Section && "Cannot switch to a null section!");
236 if (Section != CurSection) {
237 PrevSection = CurSection;
238 CurSection = Section;
239 }
240}
241
242void PTXMCAsmStreamer::EmitLabel(MCSymbol *Symbol) {
243 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
244 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
245 assert(CurSection && "Cannot emit before setting section!");
246
247 OS << *Symbol << MAI.getLabelSuffix();
248 EmitEOL();
249 Symbol->setSection(*CurSection);
250}
251
252void PTXMCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {}
253
254void PTXMCAsmStreamer::EmitThumbFunc(MCSymbol *Func) {}
255
256void PTXMCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
257 OS << *Symbol << " = " << *Value;
258 EmitEOL();
259
260 // FIXME: Lift context changes into super class.
261 Symbol->setVariableValue(Value);
262}
263
264void PTXMCAsmStreamer::EmitWeakReference(MCSymbol *Alias,
265 const MCSymbol *Symbol) {
266 OS << ".weakref " << *Alias << ", " << *Symbol;
267 EmitEOL();
268}
269
Rafael Espindola32a006e2010-12-03 00:55:40 +0000270void PTXMCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta,
271 const MCSymbol *LastLabel,
272 const MCSymbol *Label) {
273 report_fatal_error("Unimplemented.");
274}
275
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000276void PTXMCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
277 MCSymbolAttr Attribute) {}
278
279void PTXMCAsmStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
280
281void PTXMCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
282
283void PTXMCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass) {}
284
285void PTXMCAsmStreamer::EmitCOFFSymbolType (int Type) {}
286
287void PTXMCAsmStreamer::EndCOFFSymbolDef() {}
288
289void PTXMCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
290
291void PTXMCAsmStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
292 unsigned ByteAlignment) {}
293
294void PTXMCAsmStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {}
295
296void PTXMCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
297 unsigned Size, unsigned ByteAlignment) {}
298
299void PTXMCAsmStreamer::EmitTBSSSymbol(const MCSection *Section,
300 MCSymbol *Symbol,
301 uint64_t Size, unsigned ByteAlignment) {}
302
303static inline char toOctal(int X) { return (X&7)+'0'; }
304
305static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
306 OS << '"';
307
308 for (unsigned i = 0, e = Data.size(); i != e; ++i) {
309 unsigned char C = Data[i];
310 if (C == '"' || C == '\\') {
311 OS << '\\' << (char)C;
312 continue;
313 }
314
315 if (isprint((unsigned char)C)) {
316 OS << (char)C;
317 continue;
318 }
319
320 switch (C) {
321 case '\b': OS << "\\b"; break;
322 case '\f': OS << "\\f"; break;
323 case '\n': OS << "\\n"; break;
324 case '\r': OS << "\\r"; break;
325 case '\t': OS << "\\t"; break;
326 default:
327 OS << '\\';
328 OS << toOctal(C >> 6);
329 OS << toOctal(C >> 3);
330 OS << toOctal(C >> 0);
331 break;
332 }
333 }
334
335 OS << '"';
336}
337
338void PTXMCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
339 assert(CurSection && "Cannot emit contents before setting section!");
340 if (Data.empty()) return;
341
342 if (Data.size() == 1) {
343 OS << MAI.getData8bitsDirective(AddrSpace);
344 OS << (unsigned)(unsigned char)Data[0];
345 EmitEOL();
346 return;
347 }
348
349 // If the data ends with 0 and the target supports .asciz, use it, otherwise
350 // use .ascii
351 if (MAI.getAscizDirective() && Data.back() == 0) {
352 OS << MAI.getAscizDirective();
353 Data = Data.substr(0, Data.size()-1);
354 } else {
355 OS << MAI.getAsciiDirective();
356 }
357
358 OS << ' ';
359 PrintQuotedString(Data, OS);
360 EmitEOL();
361}
362
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000363void PTXMCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
Devang Patelee4854f2010-12-02 21:32:30 +0000364 unsigned AddrSpace, bool UseSet) {
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000365 assert(CurSection && "Cannot emit contents before setting section!");
366 const char *Directive = 0;
367 switch (Size) {
368 default: break;
369 case 1: Directive = MAI.getData8bitsDirective(AddrSpace); break;
370 case 2: Directive = MAI.getData16bitsDirective(AddrSpace); break;
371 case 4: Directive = MAI.getData32bitsDirective(AddrSpace); break;
372 case 8:
373 Directive = MAI.getData64bitsDirective(AddrSpace);
374 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
375 if (Directive) break;
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000376 int64_t IntValue;
377 if (!Value->EvaluateAsAbsolute(IntValue))
378 report_fatal_error("Don't know how to emit this value.");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000379 if (isLittleEndian()) {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000380 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
381 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000382 } else {
Rafael Espindolaec0b4282010-11-28 23:22:44 +0000383 EmitIntValue((uint32_t)(IntValue >> 32), 4, AddrSpace);
384 EmitIntValue((uint32_t)(IntValue >> 0 ), 4, AddrSpace);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000385 }
386 return;
387 }
388
389 assert(Directive && "Invalid size for machine code value!");
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000390 OS << Directive << *Value;
391 EmitEOL();
392}
393
394void PTXMCAsmStreamer::EmitULEB128Value(const MCExpr *Value,
395 unsigned AddrSpace) {
396 assert(MAI.hasLEB128() && "Cannot print a .uleb");
397 OS << ".uleb128 " << *Value;
398 EmitEOL();
399}
400
401void PTXMCAsmStreamer::EmitSLEB128Value(const MCExpr *Value,
402 unsigned AddrSpace) {
403 assert(MAI.hasLEB128() && "Cannot print a .sleb");
404 OS << ".sleb128 " << *Value;
405 EmitEOL();
406}
407
408void PTXMCAsmStreamer::EmitGPRel32Value(const MCExpr *Value) {
409 assert(MAI.getGPRel32Directive() != 0);
410 OS << MAI.getGPRel32Directive() << *Value;
411 EmitEOL();
412}
413
414
415/// EmitFill - Emit NumBytes bytes worth of the value specified by
416/// FillValue. This implements directives such as '.space'.
417void PTXMCAsmStreamer::EmitFill(uint64_t NumBytes, uint8_t FillValue,
418 unsigned AddrSpace) {
419 if (NumBytes == 0) return;
420
421 if (AddrSpace == 0)
422 if (const char *ZeroDirective = MAI.getZeroDirective()) {
423 OS << ZeroDirective << NumBytes;
424 if (FillValue != 0)
425 OS << ',' << (int)FillValue;
426 EmitEOL();
427 return;
428 }
429
430 // Emit a byte at a time.
431 MCStreamer::EmitFill(NumBytes, FillValue, AddrSpace);
432}
433
434void PTXMCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
435 unsigned ValueSize,
436 unsigned MaxBytesToEmit) {
437 // Some assemblers don't support non-power of two alignments, so we always
438 // emit alignments as a power of two if possible.
439 if (isPowerOf2_32(ByteAlignment)) {
440 switch (ValueSize) {
441 default: llvm_unreachable("Invalid size for machine code value!");
442 case 1: OS << MAI.getAlignDirective(); break;
443 // FIXME: use MAI for this!
444 case 2: OS << ".p2alignw "; break;
445 case 4: OS << ".p2alignl "; break;
446 case 8: llvm_unreachable("Unsupported alignment size!");
447 }
448
449 if (MAI.getAlignmentIsInBytes())
450 OS << ByteAlignment;
451 else
452 OS << Log2_32(ByteAlignment);
453
454 if (Value || MaxBytesToEmit) {
455 OS << ", 0x";
456 OS.write_hex(truncateToSize(Value, ValueSize));
457
458 if (MaxBytesToEmit)
459 OS << ", " << MaxBytesToEmit;
460 }
461 EmitEOL();
462 return;
463 }
464
465 // Non-power of two alignment. This is not widely supported by assemblers.
466 // FIXME: Parameterize this based on MAI.
467 switch (ValueSize) {
468 default: llvm_unreachable("Invalid size for machine code value!");
469 case 1: OS << ".balign"; break;
470 case 2: OS << ".balignw"; break;
471 case 4: OS << ".balignl"; break;
472 case 8: llvm_unreachable("Unsupported alignment size!");
473 }
474
475 OS << ' ' << ByteAlignment;
476 OS << ", " << truncateToSize(Value, ValueSize);
477 if (MaxBytesToEmit)
478 OS << ", " << MaxBytesToEmit;
479 EmitEOL();
480}
481
482void PTXMCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment,
483 unsigned MaxBytesToEmit) {}
484
485void PTXMCAsmStreamer::EmitValueToOffset(const MCExpr *Offset,
486 unsigned char Value) {}
487
488
489void PTXMCAsmStreamer::EmitFileDirective(StringRef Filename) {
490 assert(MAI.hasSingleParameterDotFile());
491 OS << "\t.file\t";
492 PrintQuotedString(Filename, OS);
493 EmitEOL();
494}
495
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000496// FIXME: should we inherit from MCAsmStreamer?
497bool PTXMCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo,
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000498 StringRef Filename){
499 OS << "\t.file\t" << FileNo << ' ';
500 PrintQuotedString(Filename, OS);
501 EmitEOL();
Rafael Espindolaaf6b58082010-11-16 21:20:32 +0000502 return this->MCStreamer::EmitDwarfFileDirective(FileNo, Filename);
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000503}
504
505void PTXMCAsmStreamer::AddEncodingComment(const MCInst &Inst) {}
506
507void PTXMCAsmStreamer::EmitInstruction(const MCInst &Inst) {
508 assert(CurSection && "Cannot emit contents before setting section!");
509
510 // Show the encoding in a comment if we have a code emitter.
511 if (Emitter)
512 AddEncodingComment(Inst);
513
514 // Show the MCInst if enabled.
515 if (ShowInst) {
516 Inst.dump_pretty(GetCommentOS(), &MAI, InstPrinter.get(), "\n ");
517 GetCommentOS() << "\n";
518 }
519
520 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
521 if (InstPrinter)
522 InstPrinter->printInst(&Inst, OS);
523 else
524 Inst.print(OS, &MAI);
525 EmitEOL();
526}
527
528/// EmitRawText - If this file is backed by an assembly streamer, this dumps
529/// the specified string in the output .s file. This capability is
530/// indicated by the hasRawTextSupport() predicate.
531void PTXMCAsmStreamer::EmitRawText(StringRef String) {
532 if (!String.empty() && String.back() == '\n')
533 String = String.substr(0, String.size()-1);
534 OS << String;
535 EmitEOL();
536}
537
538void PTXMCAsmStreamer::Finish() {}
539
Rafael Espindolaa484f2c2010-11-28 14:48:34 +0000540namespace llvm {
541 MCStreamer *createPTXAsmStreamer(MCContext &Context,
542 formatted_raw_ostream &OS,
543 bool isLittleEndian,
544 bool isVerboseAsm, MCInstPrinter *IP,
545 MCCodeEmitter *CE, bool ShowInst) {
546 return new PTXMCAsmStreamer(Context, OS, isLittleEndian, isVerboseAsm,
547 IP, CE, ShowInst);
548 }
Che-Liang Chioud77f2a42010-11-08 02:58:44 +0000549}