[llvm-commits] [llvm] r113691 - in /llvm/trunk: lib/MC/MCParser/AsmParser.cpp lib/MC/MCParser/ELFAsmParser.cpp test/MC/ELF/dg.exp test/MC/ELF/sleb.s test/MC/ELF/uleb.s

Rafael Espindola rafael.espindola at gmail.com
Sat Sep 11 09:45:15 PDT 2010


Author: rafael
Date: Sat Sep 11 11:45:15 2010
New Revision: 113691

URL: http://llvm.org/viewvc/llvm-project?rev=113691&view=rev
Log:
Add support for leb128 of absolute expressions.

Added:
    llvm/trunk/test/MC/ELF/sleb.s
    llvm/trunk/test/MC/ELF/uleb.s
Modified:
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp
    llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp
    llvm/trunk/test/MC/ELF/dg.exp

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=113691&r1=113690&r2=113691&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Sat Sep 11 11:45:15 2010
@@ -238,6 +238,9 @@
     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveMacro>(".macro");
     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endm");
     AddDirectiveHandler<&GenericAsmParser::ParseDirectiveEndMacro>(".endmacro");
+
+    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".sleb128");
+    AddDirectiveHandler<&GenericAsmParser::ParseDirectiveLEB128>(".uleb128");
   }
 
   bool ParseDirectiveFile(StringRef, SMLoc DirectiveLoc);
@@ -247,6 +250,10 @@
   bool ParseDirectiveMacrosOnOff(StringRef, SMLoc DirectiveLoc);
   bool ParseDirectiveMacro(StringRef, SMLoc DirectiveLoc);
   bool ParseDirectiveEndMacro(StringRef, SMLoc DirectiveLoc);
+
+  void ParseUleb128(uint64_t Value);
+  void ParseSleb128(int64_t Value);
+  bool ParseDirectiveLEB128(StringRef, SMLoc);
 };
 
 }
@@ -2049,6 +2056,48 @@
                   "no current macro definition");
 }
 
+void GenericAsmParser::ParseUleb128(uint64_t Value) {
+  const uint64_t Mask = (1 << 7) - 1;
+  do {
+    unsigned Byte = Value & Mask;
+    Value >>= 7;
+    if (Value) // Not the last one
+      Byte |= (1 << 7);
+    getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
+  } while (Value);
+}
+
+void GenericAsmParser::ParseSleb128(int64_t Value) {
+  const int64_t Mask = (1 << 7) - 1;
+  for(;;) {
+    unsigned Byte = Value & Mask;
+    Value >>= 7;
+    bool Done = ((Value ==  0 && (Byte & 0x40) == 0) ||
+                 (Value == -1 && (Byte & 0x40) != 0));
+    if (!Done)
+      Byte |= (1 << 7);
+    getStreamer().EmitIntValue(Byte, 1, DEFAULT_ADDRSPACE);
+    if (Done)
+      break;
+  }
+}
+
+bool GenericAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
+  int64_t Value;
+  if (getParser().ParseAbsoluteExpression(Value))
+    return true;
+
+  if (getLexer().isNot(AsmToken::EndOfStatement))
+    return TokError("unexpected token in directive");
+
+  if (DirName[1] == 's')
+    ParseSleb128(Value);
+  else
+    ParseUleb128(Value);
+  return false;
+}
+
+
 /// \brief Create an MCAsmParser instance.
 MCAsmParser *llvm::createMCAsmParser(const Target &T, SourceMgr &SM,
                                      MCContext &C, MCStreamer &Out,

Modified: llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp?rev=113691&r1=113690&r2=113691&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/ELFAsmParser.cpp Sat Sep 11 11:45:15 2010
@@ -48,8 +48,6 @@
     AddDirectiveHandler<&ELFAsmParser::ParseSectionDirectiveEhFrame>(".eh_frame");
     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSection>(".section");
     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
-    AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".sleb128");
-    AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".uleb128");
     AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
   }
 
@@ -109,7 +107,6 @@
                               MCSectionELF::SHF_WRITE,
                               SectionKind::getDataRel());
   }
-  bool ParseDirectiveLEB128(StringRef, SMLoc);
   bool ParseDirectiveSection(StringRef, SMLoc);
   bool ParseDirectiveSize(StringRef, SMLoc);
   bool ParseDirectivePrevious(StringRef, SMLoc);
@@ -254,26 +251,6 @@
   return false;
 }
 
-bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
-  int64_t Value;
-  if (getParser().ParseAbsoluteExpression(Value))
-    return true;
-
-  if (getLexer().isNot(AsmToken::EndOfStatement))
-    return TokError("unexpected token in directive");
-
-  // FIXME: Add proper MC support.
-  if (getContext().getAsmInfo().hasLEB128()) {
-    if (DirName[1] == 's')
-      getStreamer().EmitRawText("\t.sleb128\t" + Twine(Value));
-    else
-      getStreamer().EmitRawText("\t.uleb128\t" + Twine(Value));
-    return false;
-  }
-  // FIXME: This shouldn't be an error!
-  return TokError("LEB128 not supported yet");
-}
-
 bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
   const MCSection *PreviousSection = getStreamer().getPreviousSection();
   if (PreviousSection != NULL)

Modified: llvm/trunk/test/MC/ELF/dg.exp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/dg.exp?rev=113691&r1=113690&r2=113691&view=diff
==============================================================================
--- llvm/trunk/test/MC/ELF/dg.exp (original)
+++ llvm/trunk/test/MC/ELF/dg.exp Sat Sep 11 11:45:15 2010
@@ -1,5 +1,5 @@
 load_lib llvm.exp
 
 if { [llvm_supports_target X86] } {
-  RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll}]]
+  RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll,s}]]
 }

Added: llvm/trunk/test/MC/ELF/sleb.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/sleb.s?rev=113691&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/sleb.s (added)
+++ llvm/trunk/test/MC/ELF/sleb.s Sat Sep 11 11:45:15 2010
@@ -0,0 +1,29 @@
+// RUN: llvm-mc -filetype=obj -triple i686-pc-linux-gnu -n %s -o - | elf-dump  --dump-section-data | FileCheck -check-prefix=ELF_32 %s
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu -n %s -o - | elf-dump  --dump-section-data | FileCheck -check-prefix=ELF_64 %s
+// RUN: llvm-mc -filetype=obj -triple i386-apple-darwin9 %s -o - | macho-dump  --dump-section-data | FileCheck -check-prefix=MACHO_32 %s
+// RUN: llvm-mc -filetype=obj -triple x86_64-apple-darwin9 %s -o - | macho-dump  --dump-section-data | FileCheck -check-prefix=MACHO_64 %s
+
+	.text
+foo:
+	.sleb128	0
+	.sleb128	1
+	.sleb128	-1
+	.sleb128	63
+	.sleb128	-64
+
+	.sleb128	64
+	.sleb128	-65
+
+	.sleb128	8191
+	.sleb128        -8192
+
+	.sleb128        8193
+
+// ELF_32: ('sh_name', 1) # '.text'
+// ELF_32: ('_section_data', '00017f3f 40c000bf 7fff3f80 4081c000')
+// ELF_64: ('sh_name', 1) # '.text'
+// ELF_64: ('_section_data', '00017f3f 40c000bf 7fff3f80 4081c000')
+// MACHO_32: ('section_name', '__text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
+// MACHO_32: ('_section_data', '00017f3f 40c000bf 7fff3f80 4081c000')
+// MACHO_64: ('section_name', '__text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
+// MACHO_64: ('_section_data', '00017f3f 40c000bf 7fff3f80 4081c000')

Added: llvm/trunk/test/MC/ELF/uleb.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/uleb.s?rev=113691&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/uleb.s (added)
+++ llvm/trunk/test/MC/ELF/uleb.s Sat Sep 11 11:45:15 2010
@@ -0,0 +1,22 @@
+// RUN: llvm-mc -filetype=obj -triple i686-pc-linux-gnu -n %s -o - | elf-dump  --dump-section-data | FileCheck -check-prefix=ELF_32 %s
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu -n %s -o - | elf-dump  --dump-section-data | FileCheck -check-prefix=ELF_64 %s
+// RUN: llvm-mc -filetype=obj -triple i386-apple-darwin9 %s -o - | macho-dump  --dump-section-data | FileCheck -check-prefix=MACHO_32 %s
+// RUN: llvm-mc -filetype=obj -triple x86_64-apple-darwin9 %s -o - | macho-dump  --dump-section-data | FileCheck -check-prefix=MACHO_64 %s
+
+	.text
+foo:
+	.uleb128	0
+	.uleb128	1
+	.uleb128	127
+	.uleb128	128
+	.uleb128	16383
+	.uleb128	16384
+
+// ELF_32: ('sh_name', 1) # '.text'
+// ELF_32: ('_section_data', '00017f80 01ff7f80 8001')
+// ELF_64: ('sh_name', 1) # '.text'
+// ELF_64: ('_section_data', '00017f80 01ff7f80 8001')
+// MACHO_32: ('section_name', '__text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
+// MACHO_32: ('_section_data', '00017f80 01ff7f80 8001')
+// MACHO_64: ('section_name', '__text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
+// MACHO_64: ('_section_data', '00017f80 01ff7f80 8001')





More information about the llvm-commits mailing list