teach MCAsmStreamer::EmitBytes to use .ascii and .asciz
authorChris Lattner <sabre@nondot.org>
Sat, 23 Jan 2010 00:15:00 +0000 (00:15 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 23 Jan 2010 00:15:00 +0000 (00:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94259 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/AsmPrinter/DIE.h
lib/MC/MCAsmStreamer.cpp
test/MC/AsmParser/directive_ascii.s

index 323286ded18eba7bb29418cdcc493f390a8a842d..af90289e5f835dda236df5a08871a02aaa219390 100644 (file)
@@ -101,7 +101,7 @@ namespace llvm {
 
     /// Emit - Print the abbreviation using the specified asm printer.
     ///
-    void Emit(const DwarfPrinter *Asm) const;
+    void Emit(const DwarfPrinter *DP) const;
 
 #ifndef NDEBUG
     void print(raw_ostream &O);
index d4ef3ca826cad5f1ad8009c5f551bc656c1fd5e8..75903aa8449d2fb885850680210365f70b0f88a7 100644 (file)
@@ -271,13 +271,57 @@ void MCAsmStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
   EmitEOL();
 }
 
+static inline char toOctal(int X) { return (X&7)+'0'; }
+
 void MCAsmStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
   assert(CurSection && "Cannot emit contents before setting section!");
-  const char *Directive = MAI.getData8bitsDirective(AddrSpace);
-  for (unsigned i = 0, e = Data.size(); i != e; ++i) {
-    OS << Directive << (unsigned)(unsigned char)Data[i];
+  if (Data.empty()) return;
+  
+  if (Data.size() == 1) {
+    OS << MAI.getData8bitsDirective(AddrSpace);
+    OS << (unsigned)(unsigned char)Data[0];
     EmitEOL();
+    return;
   }
+
+  // If the data ends with 0 and the target supports .asciz, use it, otherwise
+  // use .ascii
+  if (MAI.getAscizDirective() && Data.back() == 0) {
+    OS << MAI.getAscizDirective();
+    Data = Data.substr(0, Data.size()-1);
+  } else {
+    OS << MAI.getAsciiDirective();
+  }
+
+  OS << " \"";
+  for (unsigned i = 0, e = Data.size(); i != e; ++i) {
+    unsigned char C = Data[i];
+    if (C == '"' || C == '\\') {
+      OS << '\\' << (char)C;
+      continue;
+    }
+    
+    if (isprint((unsigned char)C)) {
+      OS << (char)C;
+      continue;
+    }
+    
+    switch (C) {
+    case '\b': OS << "\\b"; break;
+    case '\f': OS << "\\f"; break;
+    case '\n': OS << "\\n"; break;
+    case '\r': OS << "\\r"; break;
+    case '\t': OS << "\\t"; break;
+    default:
+      OS << '\\';
+      OS << toOctal(C >> 6);
+      OS << toOctal(C >> 3);
+      OS << toOctal(C >> 0);
+      break;
+    }
+  }
+  OS << '"';
+  EmitEOL();
 }
 
 /// EmitIntValue - Special case of EmitValue that avoids the client having
index cc6d23b751d31e8970eb7b5f74ac3ecb95392471..5bfc1e9d6eacaf76631a6b913bfe55e1a763a28a 100644 (file)
@@ -23,27 +23,12 @@ TEST3:
         .asciz "B", "C"
         
 # CHECK: TEST4:
-# CHECK: .byte 1
-# CHECK: .byte 1
-# CHECK: .byte 7
-# CHECK: .byte 0
-# CHECK: .byte 56
-# CHECK: .byte 1
-# CHECK: .byte 0
-# CHECK: .byte 49
-# CHECK: .byte 128
-# CHECK: .byte 0
+# CHECK: .asciz "\001\001\007\0008\001\0001\200"
 TEST4:  
         .ascii "\1\01\07\08\001\0001\200\0"
         
 # CHECK: TEST5:
-# CHECK: .byte 8
-# CHECK: .byte 12
-# CHECK: .byte 10
-# CHECK: .byte 13
-# CHECK: .byte 9
-# CHECK: .byte 92
-# CHECK: .byte 34
+# CHECK: .ascii "\b\f\n\r\t\\\""
 TEST5:
         .ascii "\b\f\n\r\t\\\""