MC: Add support for treating $ as a reference to the PC
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 25 Sep 2013 10:47:21 +0000 (10:47 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 25 Sep 2013 10:47:21 +0000 (10:47 +0000)
The binutils assembler supports a mode called DOLLAR_DOT which treats
the dollar sign token as a reference to the current program counter if
the dollar sign doesn't precede a constant or identifier.

This commit adds a new MCAsmInfo flag stating whether or not a given
target supports this interpretation of the dollar sign token; by
default, this flag is not enabled.

Further, enable this flag for PPC. The system assembler for AIX and
binutils both support using the dollar sign in this manner.

This fixes PR17353.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191368 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCAsmInfo.h
lib/MC/MCAsmInfo.cpp
lib/MC/MCParser/AsmParser.cpp
lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp
test/MC/PowerPC/ppc64-operands.s

index f5acd1716df44b69006396575ac1e6b98ea7eda6..3734d7e006b46c954fb400d077741737584936d0 100644 (file)
@@ -93,6 +93,10 @@ namespace llvm {
     /// this value.  Factored out in .debug_frame and .debug_line.
     unsigned MinInstAlignment;                  // Defaults to 1.
 
+    /// DollarIsPC - The '$' token, when not referencing an identifier or
+    /// constant, refers to the current PC.
+    bool DollarIsPC;                         // Defaults to false.
+
     /// SeparatorString - This string, if specified, is used to separate
     /// instructions from each other when on the same line.
     const char *SeparatorString;             // Defaults to ';'
@@ -421,6 +425,9 @@ namespace llvm {
     unsigned getMinInstAlignment() const {
       return MinInstAlignment;
     }
+    bool getDollarIsPC() const {
+      return DollarIsPC;
+    }
     const char *getSeparatorString() const {
       return SeparatorString;
     }
index 73dc74a4f20d82384808dcd50f0358ca1e331839..152aae15af1747705f78255d962adcb133707bbb 100644 (file)
@@ -35,6 +35,7 @@ MCAsmInfo::MCAsmInfo() {
   LinkerRequiresNonEmptyDwarfLines = false;
   MaxInstLength = 4;
   MinInstAlignment = 1;
+  DollarIsPC = false;
   SeparatorString = ";";
   CommentColumn = 40;
   CommentString = "#";
index 1267dc814cde2c61d11e981868ed3c4529f840b7..21cbd3416ca5183cafac42cc9d782b811f96df4a 100644 (file)
@@ -772,9 +772,19 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
   case AsmToken::Identifier: {
     StringRef Identifier;
     if (parseIdentifier(Identifier)) {
-      if (FirstTokenKind == AsmToken::Dollar)
-        return Error(FirstTokenLoc, "invalid token in expression");
-      return true;
+      if (FirstTokenKind == AsmToken::Dollar) {
+        if (Lexer.getMAI().getDollarIsPC()) {
+          // This is a '$' reference, which references the current PC.  Emit a
+          // temporary label to the streamer and refer to it.
+          MCSymbol *Sym = Ctx.CreateTempSymbol();
+          Out.EmitLabel(Sym);
+          Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
+          EndLoc = FirstTokenLoc;
+          return false;
+        } else
+          return Error(FirstTokenLoc, "invalid token in expression");
+        return true;
+      }
     }
 
     EndLoc = SMLoc::getFromPointer(Identifier.end());
index 1f3e5b49a644c6e6241c65d60d0e7331c3044a2e..91578a9b5043874102d701a3450ec63bfa8967d1 100644 (file)
@@ -54,6 +54,8 @@ PPCLinuxMCAsmInfo::PPCLinuxMCAsmInfo(bool is64Bit) {
   // Debug Information
   SupportsDebugInformation = true;
 
+  DollarIsPC = true;
+
   // Set up DWARF directives
   HasLEB128 = true;  // Target asm supports leb128 directives (little-endian)
   MinInstAlignment = 4;
index cb96fd4588765fb47e791da4d990ba3fbb9e943c..fc1cbeb94b94c7e9e84d16321c4b063a9e497dde 100644 (file)
 # CHECK: beqa 0, 1024                    # encoding: [0x41,0x82,0x04,0x02]
          beqa 1024
 
+# CHECK:                                 # encoding: [0x42,0x9f,A,0bAAAAAA01]
+         bcl 20, 31, $+4
+
+# CHECK:                                 # encoding: [0x42,0x00,A,0bAAAAAA00]
+         bdnz $-8