MC: Tweak variable assignment diagnostics, and make reassignment of non-absolute
authorDaniel Dunbar <daniel@zuster.org>
Fri, 16 Oct 2009 01:57:39 +0000 (01:57 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 16 Oct 2009 01:57:39 +0000 (01:57 +0000)
variables and symbols invalid.

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

lib/MC/MCAsmStreamer.cpp
test/MC/AsmParser/variables-invalid.s [new file with mode: 0644]
tools/llvm-mc/AsmParser.cpp

index 1a6e8b6c8e1c654f428f4f124eb745454158d088..0e44ee19d4598f2e133566ab35d0445f3e29645b 100644 (file)
@@ -125,6 +125,7 @@ void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
   OS << '\n';
 
   // FIXME: Lift context changes into super class.
+  // FIXME: Set associated section.
   Symbol->setValue(Value);
 }
 
diff --git a/test/MC/AsmParser/variables-invalid.s b/test/MC/AsmParser/variables-invalid.s
new file mode 100644 (file)
index 0000000..d480670
--- /dev/null
@@ -0,0 +1,17 @@
+// RUN: not llvm-mc %s 2> %t
+// RUN: FileCheck --input-file %t %s
+
+        .data
+// CHECK: invalid assignment to 't0_v0'
+        t0_v0 = t0_v0 + 1
+
+        t1_v1 = 1
+        t1_v1 = 2
+
+t2_s0:
+// CHECK: redefinition of 't2_s0'
+        t2_s0 = 2
+
+        t3_s0 = t2_s0 + 1
+// CHECK: invalid reassignment of non-absolute variable 't3_s0'
+        t3_s0 = 1
index 7174fa8ee8e5587aa1c78780d21511e0022f43a4..02b7c396a4b814623a95a6943e853ed4d49c76b1 100644 (file)
@@ -741,14 +741,25 @@ bool AsmParser::ParseAssignment(const StringRef &Name) {
   // Eat the end of statement marker.
   Lexer.Lex();
 
-  // Diagnose assignment to a label.
-  //
-  // FIXME: Diagnostics. Note the location of the definition as a label.
+  // Validate that the LHS is allowed to be a variable (either it has not been
+  // used as a symbol, or it is an absolute symbol).
+  MCSymbol *Sym = getContext().LookupSymbol(Name);
+  if (Sym) {
+    // Diagnose assignment to a label.
+    //
+    // FIXME: Diagnostics. Note the location of the definition as a label.
+    // FIXME: Diagnose assignment to protected identifier (e.g., register name).
+    if (!Sym->isUndefined() && !Sym->isAbsolute())
+      return Error(EqualLoc, "redefinition of '" + Name + "'");
+    else if (!Sym->isVariable())
+      return Error(EqualLoc, "invalid assignment to '" + Name + "'");
+    else if (!isa<MCConstantExpr>(Sym->getValue()))
+      return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
+                   Name + "'");
+  } else
+    Sym = CreateSymbol(Name);
+
   // FIXME: Handle '.'.
-  // FIXME: Diagnose assignment to protected identifier (e.g., register name).
-  MCSymbol *Sym = CreateSymbol(Name);
-  if (!Sym->isUndefined() && !Sym->isAbsolute())
-    return Error(EqualLoc, "symbol has already been defined");
 
   // Do the assignment.
   Out.EmitAssignment(Sym, Value);