#ifndef LLVM_MC_MCINST_H
#define LLVM_MC_MCINST_H
-#include "llvm/MC/MCValue.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/DebugLoc.h"
namespace llvm {
class raw_ostream;
+class MCExpr;
/// MCOperand - Instances of this class represent operands of the MCInst class.
/// This is a simple discriminated union.
kRegister, ///< Register operand.
kImmediate, ///< Immediate operand.
kMBBLabel, ///< Basic block label.
- kMCValue ///< Relocatable immediate operand.
+ kExpr ///< Relocatable immediate operand.
};
unsigned char Kind;
union {
unsigned RegVal;
int64_t ImmVal;
- MCValue MCValueVal;
+ const MCExpr *ExprVal;
struct {
unsigned FunctionNo;
unsigned BlockNo;
bool isReg() const { return Kind == kRegister; }
bool isImm() const { return Kind == kImmediate; }
bool isMBBLabel() const { return Kind == kMBBLabel; }
- bool isMCValue() const { return Kind == kMCValue; }
+ bool isExpr() const { return Kind == kExpr; }
/// getReg - Returns the register number.
unsigned getReg() const {
}
unsigned getMBBLabelFunction() const {
- assert(isMBBLabel() && "Wrong accessor");
+ assert(isMBBLabel() && "This is not a machine basic block");
return MBBLabel.FunctionNo;
}
unsigned getMBBLabelBlock() const {
- assert(isMBBLabel() && "Wrong accessor");
+ assert(isMBBLabel() && "This is not a machine basic block");
return MBBLabel.BlockNo;
}
- const MCValue &getMCValue() const {
- assert(isMCValue() && "This is not an MCValue");
- return MCValueVal;
+ const MCExpr *getExpr() const {
+ assert(isExpr() && "This is not an expression");
+ return ExprVal;
}
- void setMCValue(const MCValue &Val) {
- assert(isMCValue() && "This is not an MCValue");
- MCValueVal = Val;
+ void setExpr(const MCExpr *Val) {
+ assert(isExpr() && "This is not an expression");
+ ExprVal = Val;
}
static MCOperand CreateReg(unsigned Reg) {
Op.MBBLabel.BlockNo = MBB;
return Op;
}
- static MCOperand CreateMCValue(const MCValue &Val) {
+ static MCOperand CreateExpr(const MCExpr *Val) {
MCOperand Op;
- Op.Kind = kMCValue;
- Op.MCValueVal = Val;
+ Op.Kind = kExpr;
+ Op.ExprVal = Val;
return Op;
}
#include "llvm/MC/MCStreamer.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/CodeGen/AsmPrinter.h"
+#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
-#include "llvm/MC/MCAsmInfo.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Format.h"
OS << ".org " << Offset << ", " << (unsigned) Value << '\n';
}
-static raw_ostream &operator<<(raw_ostream &OS, const MCOperand &Op) {
- if (Op.isReg())
- return OS << "reg:" << Op.getReg();
- if (Op.isImm())
- return OS << "imm:" << Op.getImm();
- if (Op.isMBBLabel())
- return OS << "mbblabel:("
- << Op.getMBBLabelFunction() << ", " << Op.getMBBLabelBlock();
- assert(Op.isMCValue() && "Invalid operand!");
- return OS << "val:" << Op.getMCValue();
-}
-
void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
assert(CurSection && "Cannot emit contents before setting section!");
// Otherwise fall back to a structural printing for now. Eventually we should
// always have access to the target specific printer.
- OS << "MCInst("
- << "opcode=" << Inst.getOpcode() << ", "
- << "operands=[";
- for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) {
- if (i)
- OS << ", ";
- OS << Inst.getOperand(i);
- }
- OS << "])\n";
+ Inst.print(OS);
+ OS << '\n';
}
void MCAsmStreamer::Finish() {
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCExpr.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
else if (isMBBLabel())
OS << "MBB:(" << getMBBLabelFunction() << ","
<< getMBBLabelBlock() << ")";
- else if (isMCValue()) {
- OS << "Value:(";
- getMCValue().print(OS);
+ else if (isExpr()) {
+ OS << "Expr:(";
+ getExpr()->print(OS);
OS << ")";
} else
OS << "UNDEFINED";
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCCodeEmitter.h"
+#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSymbol.h"
return Value;
}
+ const MCExpr *AddValueSymbols(const MCExpr *Value) {
+ switch (Value->getKind()) {
+ case MCExpr::Constant:
+ break;
+
+ case MCExpr::Binary: {
+ const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
+ AddValueSymbols(BE->getLHS());
+ AddValueSymbols(BE->getRHS());
+ break;
+ }
+
+ case MCExpr::SymbolRef:
+ getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
+ break;
+
+ case MCExpr::Unary:
+ AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
+ break;
+ }
+
+ return Value;
+ }
+
/// @name MCStreamer Interface
/// @{
void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
// Scan for values.
for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
- if (Inst.getOperand(i).isMCValue())
- AddValueSymbols(Inst.getOperand(i).getMCValue());
+ if (Inst.getOperand(i).isExpr())
+ AddValueSymbols(Inst.getOperand(i).getExpr());
if (!Emitter)
llvm_unreachable("no code emitter available!");
#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"
} Reg;
struct {
- MCValue Val;
+ const MCExpr *Val;
} Imm;
struct {
unsigned SegReg;
- MCValue Disp;
+ const MCExpr *Disp;
unsigned BaseReg;
unsigned IndexReg;
unsigned Scale;
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;
}
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; }
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 {
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)
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!");
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;
// 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 (.
// 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
#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"
// 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::
}
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);
}
// 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.
#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;
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.
O << '$';
O << Op.getImm();
return;
- } else if (Op.isMCValue()) {
+ } else if (Op.isExpr()) {
O << '$';
- Op.getMCValue().print(O);
+ Op.getExpr()->print(O);
return;
}
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() ||
#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"
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;
}
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);
foo:
// CHECK: addl $24, "a$b"(%eax)
addl $24, "a$b"(%eax)
-// CHECK: addl $24, "a$b" + 10(%eax)
+// CHECK: addl $24, ("a$b" + 10)(%eax)
addl $24, ("a$b" + 10)(%eax)
// CHECK: "b$c" = 10
"b$c" = 10
-// CHECK: addl $10, %eax
+// CHECK: addl $"b$c", %eax
addl "b$c", %eax
// CHECK: set "a 0", 11
# Immediates
# CHECK: addl $1, %eax
addl $1, %eax
-# CHECK: addl $3, %eax
+# CHECK: addl $(1 + 2), %eax
addl $(1+2), %eax
# CHECK: addl $a, %eax
addl $a, %eax
-# CHECK: addl $3, %eax
+# CHECK: addl $(1 + 2), %eax
addl $1 + 2, %eax
# Disambiguation
#addl $1, 4+4
# FIXME: Add back when we can match this.
#addl $1, (4+4)
-# CHECK: addl $1, 8(%eax)
+# CHECK: addl $1, (4 + 4)(%eax)
addl $1, 4+4(%eax)
-# CHECK: addl $1, 8(%eax)
+# CHECK: addl $1, (4 + 4)(%eax)
addl $1, (4+4)(%eax)
# CHECK: addl $1, 8(%eax)
addl $1, 8(%eax)
# CHECK: addl $1, 0(%eax)
addl $1, (%eax)
-# CHECK: addl $1, 8(,%eax)
+# CHECK: addl $1, (4 + 4)(,%eax)
addl $1, (4+4)(,%eax)
# Indirect Memory Operands