MC: When parsing a variable reference, substitute absolute variables immediately
authorDaniel Dunbar <daniel@zuster.org>
Fri, 16 Oct 2009 01:34:54 +0000 (01:34 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Fri, 16 Oct 2009 01:34:54 +0000 (01:34 +0000)
since they are allowed to be redefined.

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

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

index e56e968380f4072bf21fc390a8e897ab0867fc64..1a6e8b6c8e1c654f428f4f124eb745454158d088 100644 (file)
@@ -123,9 +123,12 @@ void MCAsmStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
   OS << " = ";
   Value->print(OS, &MAI);
   OS << '\n';
+
+  // FIXME: Lift context changes into super class.
+  Symbol->setValue(Value);
 }
 
-void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 
+void MCAsmStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
                                         SymbolAttr Attribute) {
   switch (Attribute) {
   case Global: OS << ".globl"; break;
index 53da7edf97cb64ac33f6914d9b3d820c4489af07..456d61f06044c6f719a8b0da953ae95e892d1e59 100644 (file)
@@ -12,21 +12,21 @@ a:
         .long 0
 
         .text
-foo:    
+foo:
 // CHECK: addl $24, a$b(%eax)
-        addl $24, "a$b"(%eax)    
+        addl $24, "a$b"(%eax)
 // CHECK: addl $24, a$b+10(%eax)
         addl $24, ("a$b" + 10)(%eax)
-        
+
 // CHECK: b$c = 10
 "b$c" = 10
-// CHECK: addl $b$c, %eax
+// CHECK: addl $10, %eax
         addl "b$c", %eax
-        
+
 // CHECK: "a 0" = 11
         .set "a 0", 11
-        
-// CHECK: .long "a 0"
+
+// CHECK: .long 11
         .long "a 0"
 
 // XXCHCK: .section "a 1,a 2"
@@ -48,12 +48,12 @@ foo:
         .lcomm "a 7", 1
 
 // FIXME: We don't bother to support .lsym.
-        
+
 // CHECX: .lsym "a 8",1
 //        .lsym "a 8", 1
 
 // CHECK: "a 9" = a-b
         .set "a 9", a - b
-        
+
 // CHECK: .long "a 9"
         .long "a 9"
diff --git a/test/MC/AsmParser/variables.s b/test/MC/AsmParser/variables.s
new file mode 100644 (file)
index 0000000..86fea27
--- /dev/null
@@ -0,0 +1,15 @@
+// RUN: llvm-mc %s
+
+        .data
+        t0_v0 = 1
+        t0_v1 = t0_v0
+        .if t0_v1 != 1
+        .abort "invalid value"
+        .endif
+
+        t1_v0 = 1
+        t1_v1 = t0_v0
+        t1_v0 = 2
+        .if t0_v1 != 1
+        .abort "invalid value"
+        .endif
index aae27f5d7649a614af8f92d87c549459bb372938..7174fa8ee8e5587aa1c78780d21511e0022f43a4 100644 (file)
@@ -21,6 +21,7 @@
 #include "llvm/MC/MCSectionMachO.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCSymbol.h"
+#include "llvm/MC/MCValue.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Target/TargetAsmParser.h"
@@ -220,12 +221,22 @@ bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
     Res = MCUnaryExpr::CreateLNot(Res, getContext());
     return false;
   case AsmToken::String:
-  case AsmToken::Identifier:
-    // This is a label, this should be parsed as part of an expression, to
-    // handle things like LFOO+4.
-    Res = MCSymbolRefExpr::Create(Lexer.getTok().getIdentifier(), getContext());
+  case AsmToken::Identifier: {
+    // This is a symbol reference.
+    MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
     Lexer.Lex(); // Eat identifier.
+
+    // If this is an absolute variable reference, substitute it now to preserve
+    // semantics in the face of reassignment.
+    if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
+      Res = Sym->getValue();
+      return false;
+    }
+
+    // Otherwise create a symbol ref.
+    Res = MCSymbolRefExpr::Create(Sym, getContext());
     return false;
+  }
   case AsmToken::Integer:
     Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
     Lexer.Lex(); // Eat token.