make formatting of expressions more closely match the existing asmprinter.
authorChris Lattner <sabre@nondot.org>
Tue, 8 Sep 2009 06:34:07 +0000 (06:34 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 8 Sep 2009 06:34:07 +0000 (06:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@81202 91177308-0d34-0410-b5e6-96231b3b80d8

lib/MC/MCExpr.cpp

index 884211769a605acb40b8c9b60ec33a9b73ac60ac..2bb674ff3445fad8586aaf27d5a304300099c8c5 100644 (file)
@@ -39,9 +39,16 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
 
   case MCExpr::Binary: {
     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
-    OS << '(';
-    BE.getLHS()->print(OS, MAI);
-    OS << ' ';
+    
+    // Only print parens around the LHS if it is non-trivial.
+    if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
+      BE.getLHS()->print(OS, MAI);
+    } else {
+      OS << '(';
+      BE.getLHS()->print(OS, MAI);
+      OS << ')';
+    }
+    
     switch (BE.getOpcode()) {
     default: assert(0 && "Invalid opcode!");
     case MCBinaryExpr::Add:  OS <<  '+'; break;
@@ -63,9 +70,15 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
     case MCBinaryExpr::Sub:  OS <<  '-'; break;
     case MCBinaryExpr::Xor:  OS <<  '^'; break;
     }
-    OS << ' ';
-    BE.getRHS()->print(OS, MAI);
-    OS << ')';
+    
+    // Only print parens around the LHS if it is non-trivial.
+    if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
+      BE.getRHS()->print(OS, MAI);
+    } else {
+      OS << '(';
+      BE.getRHS()->print(OS, MAI);
+      OS << ')';
+    }
     return;
   }
   }