Add AsmParser support for the ELF .previous directive. Patch by Roman Divacky.
authorBenjamin Kramer <benny.kra@googlemail.com>
Thu, 2 Sep 2010 18:53:37 +0000 (18:53 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Thu, 2 Sep 2010 18:53:37 +0000 (18:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112849 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCStreamer.h
lib/MC/MCAsmStreamer.cpp
lib/MC/MCNullStreamer.cpp
lib/MC/MCObjectStreamer.cpp
lib/MC/MCParser/ELFAsmParser.cpp
lib/MC/MCStreamer.cpp
test/MC/AsmParser/ELF/directive_previous.s [new file with mode: 0644]

index e152c39e19d5f5dc26a60929a1a29d721604dcf0..1ce1b0e09d4acc0432415284527f4bd9dd9f8e4d 100644 (file)
@@ -54,6 +54,10 @@ namespace llvm {
     /// kept up to date by SwitchSection.
     const MCSection *CurSection;
 
+    /// PrevSection - This is the previous section code is being emitted to, it is
+    /// kept up to date by SwitchSection.
+    const MCSection *PrevSection;
+
   public:
     virtual ~MCStreamer();
 
@@ -96,6 +100,10 @@ namespace llvm {
     /// emitting code to.
     const MCSection *getCurrentSection() const { return CurSection; }
 
+    /// getPreviousSection - Return the previous section that the streamer is
+    /// emitting code to.
+    const MCSection *getPreviousSection() const { return PrevSection; }
+
     /// SwitchSection - Set the current section where code is being emitted to
     /// @p Section.  This is required to update CurSection.
     ///
index 4ededb24fdcfa9a68b3465fa81045abd78d19c8b..1cc8fb0b548672a6636b407d39121baeb2c43a80 100644 (file)
@@ -217,6 +217,7 @@ static inline int64_t truncateToSize(int64_t Value, unsigned Bytes) {
 void MCAsmStreamer::SwitchSection(const MCSection *Section) {
   assert(Section && "Cannot switch to a null section!");
   if (Section != CurSection) {
+    PrevSection = CurSection;
     CurSection = Section;
     Section->PrintSwitchToSection(MAI, OS);
   }
index 5332ade2115358170784f22a4d483f2095201b00..f7a2f20ca4bc3dcf8291cbb9174becaa5fce1b93 100644 (file)
@@ -26,6 +26,7 @@ namespace {
     /// @{
 
     virtual void SwitchSection(const MCSection *Section) {
+      PrevSection = CurSection;
       CurSection = Section;
     }
 
index eed4e7bd4f67db91af1400cef2cdf0d76bbc09d0..2b2385ef9156d8b89fb27f002f3630ec8c3f91e1 100644 (file)
@@ -77,6 +77,7 @@ void MCObjectStreamer::SwitchSection(const MCSection *Section) {
   // If already in this section, then this is a noop.
   if (Section == CurSection) return;
 
+  PrevSection = CurSection;
   CurSection = Section;
   CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
 }
index b0bc5c6114e3d4cb53bdf773e01b46f5d60cd9eb..f982fdaecb1232f59a927f7f74f97368ff5d59e6 100644 (file)
@@ -50,6 +50,7 @@ public:
     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveSize>(".size");
     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".sleb128");
     AddDirectiveHandler<&ELFAsmParser::ParseDirectiveLEB128>(".uleb128");
+    AddDirectiveHandler<&ELFAsmParser::ParseDirectivePrevious>(".previous");
   }
 
   bool ParseSectionDirectiveData(StringRef, SMLoc) {
@@ -111,6 +112,7 @@ public:
   bool ParseDirectiveLEB128(StringRef, SMLoc);
   bool ParseDirectiveSection(StringRef, SMLoc);
   bool ParseDirectiveSize(StringRef, SMLoc);
+  bool ParseDirectivePrevious(StringRef, SMLoc);
 };
 
 }
@@ -272,6 +274,14 @@ bool ELFAsmParser::ParseDirectiveLEB128(StringRef DirName, SMLoc) {
   return TokError("LEB128 not supported yet");
 }
 
+bool ELFAsmParser::ParseDirectivePrevious(StringRef DirName, SMLoc) {
+  const MCSection *PreviousSection = getStreamer().getPreviousSection();
+  if (PreviousSection != NULL)
+    getStreamer().SwitchSection(PreviousSection);
+
+  return false;
+}
+
 namespace llvm {
 
 MCAsmParserExtension *createELFAsmParser() {
index f682721b4a142d5abdbabff7836801e03a6df65f..3e9d02ea5ae73a1bc65c3e71e1a113e639953cba 100644 (file)
@@ -15,7 +15,8 @@
 #include <cstdlib>
 using namespace llvm;
 
-MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), CurSection(0) {
+MCStreamer::MCStreamer(MCContext &Ctx) : Context(Ctx), CurSection(0),
+                                         PrevSection(0) {
 }
 
 MCStreamer::~MCStreamer() {
diff --git a/test/MC/AsmParser/ELF/directive_previous.s b/test/MC/AsmParser/ELF/directive_previous.s
new file mode 100644 (file)
index 0000000..5db1eac
--- /dev/null
@@ -0,0 +1,13 @@
+# RUN: llvm-mc -triple i386-pc-linux-gnu %s | FileCheck %s
+
+.bss
+# CHECK: .bss
+
+.text
+# CHECK: .text
+
+.previous
+# CHECK: .bss
+
+.previous
+# CHECK: .text