Restore r125595 (reverted in r126336) with modifications:
authorJoerg Sonnenberger <joerg@bec.de>
Thu, 24 Feb 2011 21:59:22 +0000 (21:59 +0000)
committerJoerg Sonnenberger <joerg@bec.de>
Thu, 24 Feb 2011 21:59:22 +0000 (21:59 +0000)
Introduce a variable in the AsmParserExtension whether [] is valid in an
expression. If it is true, parse them like (). Enable this for ELF only.

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

include/llvm/MC/MCParser/MCAsmParserExtension.h
lib/MC/MCParser/AsmParser.cpp
lib/MC/MCParser/ELFAsmParser.cpp
lib/MC/MCParser/MCAsmParserExtension.cpp
test/MC/ARM/bracket-darwin.s [new file with mode: 0644]
test/MC/ELF/bracket-exprs.s [new file with mode: 0644]
test/MC/ELF/bracket.s [new file with mode: 0644]

index 95184cdfcf3288b270dc2479275df629e08bb834..ceb57f57e9e12ac2d49afe21951ff8114aa7fb6c 100644 (file)
@@ -38,6 +38,8 @@ protected:
     return (Obj->*Handler)(Directive, DirectiveLoc);
   }
 
+  bool BracketExpressionsSupported;
+
 public:
   virtual ~MCAsmParserExtension();
 
@@ -68,6 +70,8 @@ public:
 
   const AsmToken &getTok() { return getParser().getTok(); }
 
+  bool HasBracketExpressions() const { return BracketExpressionsSupported; }
+
   /// @}
 };
 
index ae072d883ba08da7279486e50272ca00a0b9c835..a84917ffb86a685d3ab6a07ced4b7597cfabf528 100644 (file)
@@ -173,6 +173,7 @@ private:
   bool ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc);
   bool ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
   bool ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
+  bool ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
 
   /// ParseIdentifier - Parse an identifier or string (as a quoted identifier)
   /// and set \arg Res to the identifier contents.
@@ -492,6 +493,20 @@ bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
   return false;
 }
 
+/// ParseBracketExpr - Parse a bracket expression and return it.
+/// NOTE: This assumes the leading '[' has already been consumed.
+///
+/// bracketexpr ::= expr]
+///
+bool AsmParser::ParseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
+  if (ParseExpression(Res)) return true;
+  if (Lexer.isNot(AsmToken::RBrac))
+    return TokError("expected ']' in brackets expression");
+  EndLoc = Lexer.getLoc();
+  Lex();
+  return false;
+}
+
 /// ParsePrimaryExpr - Parse a primary expression and return it.
 ///  primaryexpr ::= (parenexpr
 ///  primaryexpr ::= symbol
@@ -587,6 +602,11 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
   case AsmToken::LParen:
     Lex(); // Eat the '('.
     return ParseParenExpr(Res, EndLoc);
+  case AsmToken::LBrac:
+    if (!PlatformParser->HasBracketExpressions())
+      return TokError("brackets expression not supported on this target");
+    Lex(); // Eat the '['.
+    return ParseBracketExpr(Res, EndLoc);
   case AsmToken::Minus:
     Lex(); // Eat the operator.
     if (ParsePrimaryExpr(Res, EndLoc))
index 2285a63da117859cc68b76cdfe9f7090acec691f..dcf689a6f0e7fa0a443e1f4f1085f4529476a101 100644 (file)
@@ -33,7 +33,9 @@ class ELFAsmParser : public MCAsmParserExtension {
   bool SeenIdent;
 
 public:
-  ELFAsmParser() : SeenIdent(false) {}
+  ELFAsmParser() : SeenIdent(false) {
+    BracketExpressionsSupported = true;
+  }
 
   virtual void Initialize(MCAsmParser &Parser) {
     // Call the base implementation.
index c30d3067da590e6ad48f4a2abcac1e598aabe7f5..3f25a14926b6a4b82c3f1ff034dbbce1097a9646 100644 (file)
@@ -10,7 +10,8 @@
 #include "llvm/MC/MCParser/MCAsmParserExtension.h"
 using namespace llvm;
 
-MCAsmParserExtension::MCAsmParserExtension() {
+MCAsmParserExtension::MCAsmParserExtension() :
+  BracketExpressionsSupported(false) {
 }
 
 MCAsmParserExtension::~MCAsmParserExtension() {
diff --git a/test/MC/ARM/bracket-darwin.s b/test/MC/ARM/bracket-darwin.s
new file mode 100644 (file)
index 0000000..dc8b348
--- /dev/null
@@ -0,0 +1,5 @@
+// RUN: not llvm-mc -triple arm-apple-darwin %s 2> %t
+// RUN: FileCheck -input-file %t %s
+
+// CHECK: error: brackets expression not supported on this target
+.byte  [4-3]
diff --git a/test/MC/ELF/bracket-exprs.s b/test/MC/ELF/bracket-exprs.s
new file mode 100644 (file)
index 0000000..c428651
--- /dev/null
@@ -0,0 +1,16 @@
+// RUN: llvm-mc -triple i386-unknown-unknown %s | FileCheck %s
+// RUN: llvm-mc -triple arm-unknown-linux %s | FileCheck %s
+
+// CHECK: .byte 1
+.if [~0 >> 1] == -1
+.byte 1
+.else
+.byte 2
+.endif
+
+// CHECK: .byte 3
+.if 4 * [4 + (3 + [2 * 2] + 1)] == 48
+.byte 3
+.else
+.byte 4
+.endif
diff --git a/test/MC/ELF/bracket.s b/test/MC/ELF/bracket.s
new file mode 100644 (file)
index 0000000..702e309
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: not llvm-mc -triple i386-unknown-unknown %s 2> %t1 > %t2
+// RUN: FileCheck < %t1 %s
+
+// CHECK: error: expected ']' in brackets expression
+.size  x, [.-x)
+
+// CHECK: error: expected ')' in parentheses expression
+.size  y, (.-y]