llvm-mc: Switch MCInst to storing an MCExpr* instead of an MCValue.

Also, use MCInst::print instead of custom code in MCAsmPrinter.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@80575 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/X86/AsmParser/X86AsmParser.cpp b/lib/Target/X86/AsmParser/X86AsmParser.cpp
index 5232beb..d06350b 100644
--- a/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -12,8 +12,8 @@
 #include "llvm/ADT/Twine.h"
 #include "llvm/MC/MCAsmLexer.h"
 #include "llvm/MC/MCAsmParser.h"
+#include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCInst.h"
-#include "llvm/MC/MCValue.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Target/TargetRegistry.h"
 #include "llvm/Target/TargetAsmParser.h"
@@ -85,12 +85,12 @@
     } Reg;
 
     struct {
-      MCValue Val;
+      const MCExpr *Val;
     } Imm;
 
     struct {
       unsigned SegReg;
-      MCValue Disp;
+      const MCExpr *Disp;
       unsigned BaseReg;
       unsigned IndexReg;
       unsigned Scale;
@@ -107,12 +107,12 @@
     return Reg.RegNo;
   }
 
-  const MCValue &getImm() const {
+  const MCExpr *getImm() const {
     assert(Kind == Immediate && "Invalid access!");
     return Imm.Val;
   }
 
-  const MCValue &getMemDisp() const {
+  const MCExpr *getMemDisp() const {
     assert(Kind == Memory && "Invalid access!");
     return Mem.Disp;
   }
@@ -143,11 +143,12 @@
     if (!isImm())
       return false;
 
-    if (!getImm().isAbsolute())
-      return true;
+    if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
+      int64_t Value = CE->getValue();
+      return Value == (int64_t) (int8_t) Value;
+    }
 
-    int64_t Value = getImm().getConstant();
-    return Value == (int64_t) (int8_t) Value;
+    return true;
   }
   
   bool isMem() const { return Kind == Memory; }
@@ -161,13 +162,13 @@
 
   void addImmOperands(MCInst &Inst, unsigned N) const {
     assert(N == 1 && "Invalid number of operands!");
-    Inst.addOperand(MCOperand::CreateMCValue(getImm()));
+    Inst.addOperand(MCOperand::CreateExpr(getImm()));
   }
 
   void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
     // FIXME: Support user customization of the render method.
     assert(N == 1 && "Invalid number of operands!");
-    Inst.addOperand(MCOperand::CreateMCValue(getImm()));
+    Inst.addOperand(MCOperand::CreateExpr(getImm()));
   }
 
   void addMemOperands(MCInst &Inst, unsigned N) const {
@@ -176,7 +177,7 @@
     Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
     Inst.addOperand(MCOperand::CreateImm(getMemScale()));
     Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
-    Inst.addOperand(MCOperand::CreateMCValue(getMemDisp()));
+    Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
 
     // FIXME: What a hack.
     if (N == 5)
@@ -198,15 +199,16 @@
     return Res;
   }
 
-  static X86Operand CreateImm(MCValue Val) {
+  static X86Operand CreateImm(const MCExpr *Val) {
     X86Operand Res;
     Res.Kind = Immediate;
     Res.Imm.Val = Val;
     return Res;
   }
 
-  static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
-                              unsigned IndexReg, unsigned Scale) {
+  static X86Operand CreateMem(unsigned SegReg, const MCExpr *Disp,
+                              unsigned BaseReg, unsigned IndexReg,
+                              unsigned Scale) {
     // We should never just have a displacement, that would be an immediate.
     assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
 
@@ -257,8 +259,8 @@
   case AsmToken::Dollar: {
     // $42 -> immediate.
     getLexer().Lex();
-    MCValue Val;
-    if (getParser().ParseRelocatableExpression(Val))
+    const MCExpr *Val;
+    if (getParser().ParseExpression(Val))
       return true;
     Op = X86Operand::CreateImm(Val);
     return false;
@@ -275,9 +277,9 @@
   // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)".  The
   // only way to do this without lookahead is to eat the ( and see what is after
   // it.
-  MCValue Disp = MCValue::get(0, 0, 0);
+  const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
   if (getLexer().isNot(AsmToken::LParen)) {
-    if (getParser().ParseRelocatableExpression(Disp)) return true;
+    if (getParser().ParseExpression(Disp)) return true;
     
     // After parsing the base expression we could either have a parenthesized
     // memory address or not.  If not, return now.  If so, eat the (.
@@ -302,7 +304,7 @@
       // memory operand consumed.
     } else {
       // It must be an parenthesized expression, parse it now.
-      if (getParser().ParseParenRelocatableExpression(Disp))
+      if (getParser().ParseParenExpression(Disp))
         return true;
       
       // After parsing the base expression we could either have a parenthesized
diff --git a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp
index e1b6263..2058d7d 100644
--- a/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp
+++ b/lib/Target/X86/AsmPrinter/X86ATTAsmPrinter.cpp
@@ -28,6 +28,7 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCSectionMachO.h"
 #include "llvm/MC/MCStreamer.h"
@@ -794,8 +795,17 @@
   
   // Create a symbol for the name.
   MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
-  return MCOperand::CreateMCValue(MCValue::get(Sym, NegatedSymbol,
-                                               MO.getOffset()));
+  // FIXME: We would like an efficient form for this, so we don't have to do a
+  // lot of extra uniquing.
+  const MCExpr *Expr = MCSymbolRefExpr::Create(Sym, OutContext);
+  if (NegatedSymbol)
+    Expr = MCBinaryExpr::CreateSub(Expr, MCSymbolRefExpr::Create(NegatedSymbol,
+                                                                 OutContext),
+                                   OutContext);
+  Expr = MCBinaryExpr::CreateAdd(Expr, MCConstantExpr::Create(MO.getOffset(),
+                                                              OutContext),
+                                 OutContext);
+  return MCOperand::CreateExpr(Expr);
 }
 
 MCOperand X86ATTAsmPrinter::
@@ -807,7 +817,13 @@
   }
 
   MCSymbol *Sym = OutContext.GetOrCreateSymbol(Name);
-  return MCOperand::CreateMCValue(MCValue::get(Sym, 0, MO.getOffset()));
+  // FIXME: We would like an efficient form for this, so we don't have to do a
+  // lot of extra uniquing.
+  const MCExpr *Expr =
+    MCBinaryExpr::CreateAdd(MCSymbolRefExpr::Create(Sym, OutContext),
+                            MCConstantExpr::Create(MO.getOffset(),OutContext),
+                            OutContext);
+  return MCOperand::CreateExpr(Expr);
 }
 
 
@@ -848,7 +864,10 @@
     // Emit the call.
     MCSymbol *PICBase = GetPICBaseSymbol();
     TmpInst.setOpcode(X86::CALLpcrel32);
-    TmpInst.addOperand(MCOperand::CreateMCValue(MCValue::get(PICBase)));
+    // FIXME: We would like an efficient form for this, so we don't have to do a
+    // lot of extra uniquing.
+    TmpInst.addOperand(MCOperand::CreateExpr(MCSymbolRefExpr::Create(PICBase,
+                                                                  OutContext)));
     printInstruction(&TmpInst);
 
     // Emit the label.
diff --git a/lib/Target/X86/AsmPrinter/X86ATTInstPrinter.cpp b/lib/Target/X86/AsmPrinter/X86ATTInstPrinter.cpp
index f45df0c..ee79791 100644
--- a/lib/Target/X86/AsmPrinter/X86ATTInstPrinter.cpp
+++ b/lib/Target/X86/AsmPrinter/X86ATTInstPrinter.cpp
@@ -16,6 +16,7 @@
 #include "llvm/MC/MCInst.h"
 #include "X86ATTAsmPrinter.h"
 #include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCExpr.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormattedStream.h"
 using namespace llvm;
@@ -55,8 +56,8 @@
   
   if (Op.isImm())
     O << Op.getImm();
-  else if (Op.isMCValue())
-    Op.getMCValue().print(O);
+  else if (Op.isExpr())
+    Op.getExpr()->print(O);
   else if (Op.isMBBLabel())
     // FIXME: Keep in sync with printBasicBlockLabel.  printBasicBlockLabel
     // should eventually call into this code, not the other way around.
@@ -90,9 +91,9 @@
     O << '$';
     O << Op.getImm();
     return;
-  } else if (Op.isMCValue()) {
+  } else if (Op.isExpr()) {
     O << '$';
-    Op.getMCValue().print(O);
+    Op.getExpr()->print(O);
     return;
   }
   
@@ -109,8 +110,8 @@
     int64_t DispVal = DispSpec.getImm();
     if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
       O << DispVal;
-  } else if (DispSpec.isMCValue()) {
-    DispSpec.getMCValue().print(O);
+  } else if (DispSpec.isExpr()) {
+    DispSpec.getExpr()->print(O);
   } else {
     llvm_unreachable("non-immediate displacement for LEA?");
     //assert(DispSpec.isGlobal() || DispSpec.isCPI() ||
diff --git a/lib/Target/X86/X86CodeEmitter.cpp b/lib/Target/X86/X86CodeEmitter.cpp
index 0c472a7..7e2fd97 100644
--- a/lib/Target/X86/X86CodeEmitter.cpp
+++ b/lib/Target/X86/X86CodeEmitter.cpp
@@ -30,6 +30,7 @@
 #include "llvm/Function.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
@@ -968,12 +969,12 @@
       Instr->addOperand(MachineOperand::CreateImm(Op.getImm()));
       return true;
     }
-    if (!Op.isMCValue())
+    if (!Op.isExpr())
       return false;
 
-    const MCValue &Val = Op.getMCValue();
-    if (Val.isAbsolute()) {
-      Instr->addOperand(MachineOperand::CreateImm(Val.getConstant()));
+    const MCExpr *Expr = Op.getExpr();
+    if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) {
+      Instr->addOperand(MachineOperand::CreateImm(CE->getValue()));
       return true;
     }
 
@@ -1036,9 +1037,8 @@
       if (CurOp < NumOps) {
         // Hack to make branches work.
         if (!(Desc.TSFlags & X86II::ImmMask) &&
-            MI.getOperand(0).isMCValue() && 
-            MI.getOperand(0).getMCValue().getSymA() &&
-            !MI.getOperand(0).getMCValue().getSymB())
+            MI.getOperand(0).isExpr() &&
+            isa<MCSymbolRefExpr>(MI.getOperand(0).getExpr()))
           Instr->addOperand(MachineOperand::CreateMBB(DummyMBB));
         else
           OK &= AddImmToInstr(MI, Instr, CurOp);