Fix PR1339 and CodeGen/X86/dollar-name.ll
authorDan Gohman <gohman@apple.com>
Thu, 26 Apr 2007 21:07:05 +0000 (21:07 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 26 Apr 2007 21:07:05 +0000 (21:07 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@36495 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/X86/X86ATTAsmPrinter.cpp

index e595750a65be0d79e716ead496f7c1f6f5a872fa..03a22865e2ce8378a6689229b20ab715f23181cf 100755 (executable)
@@ -272,16 +272,24 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
   case MachineOperand::MO_GlobalAddress: {
     bool isCallOp = Modifier && !strcmp(Modifier, "call");
     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
+    bool needCloseParen = false;
 
     GlobalValue *GV = MO.getGlobal();
     GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
     bool isThreadLocal = GVar && GVar->isThreadLocal();
 
-    if (!isMemOp && !isCallOp) O << '$';
-
     std::string Name = Mang->getValueName(GV);
     X86SharedAsmPrinter::decorateName(Name, GV);
     
+    if (!isMemOp && !isCallOp)
+      O << '$';
+    else if (Name[0] == '$') {
+      // The name begins with a dollar-sign. In order to avoid having it look
+      // like an integer immediate to the assembler, enclose it in parens.
+      O << '(';
+      needCloseParen = true;
+    }
+
     if (printStub(TM, Subtarget)) {
       // Link-once, External, or Weakly-linked global variables need
       // non-lazily-resolved stubs
@@ -352,6 +360,12 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
              GV->hasLinkOnceLinkage()) &&
             TM.getRelocationModel() != Reloc::Static)
           O << "@GOTPCREL";
+
+        if (needCloseParen) {
+          needCloseParen = false;
+          O << ')';
+        }
+
         // Use rip when possible to reduce code size, except when
         // index or base register are also part of the address. e.g.
         // foo(%rip)(%rcx,%rax,4) is not legal
@@ -359,10 +373,14 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
       }
     }
 
+    if (needCloseParen)
+      O << ')';
+
     return;
   }
   case MachineOperand::MO_ExternalSymbol: {
     bool isCallOp = Modifier && !strcmp(Modifier, "call");
+    bool needCloseParen = false;
     std::string Name(TAI->getGlobalPrefix());
     Name += MO.getSymbolName();
     if (isCallOp && printStub(TM, Subtarget)) {
@@ -370,7 +388,15 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
       O << TAI->getPrivateGlobalPrefix() << Name << "$stub";
       return;
     }
-    if (!isCallOp) O << '$';
+    if (!isCallOp)
+      O << '$';
+    else if (Name[0] == '$') {
+      // The name begins with a dollar-sign. In order to avoid having it look
+      // like an integer immediate to the assembler, enclose it in parens.
+      O << '(';
+      needCloseParen = true;
+    }
+
     O << Name;
 
     if (printGOT(TM, Subtarget)) {
@@ -391,6 +417,9 @@ void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
         O << "@PLT";
     }
 
+    if (needCloseParen)
+      O << ')';
+
     if (!isCallOp && Subtarget->isPICStyleRIPRel())
       O << "(%rip)";