2009-08-31 10:06:59 +02:00
|
|
|
//===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
|
2009-06-29 22:40:36 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-08-31 10:06:59 +02:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2009-06-29 22:40:36 +02:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-06-30 03:49:52 +02:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-06-29 22:40:36 +02:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2010-01-05 02:28:07 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-31 10:07:33 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-06-29 22:40:36 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-09-03 07:46:51 +02:00
|
|
|
void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
|
2009-08-31 10:07:33 +02:00
|
|
|
switch (getKind()) {
|
|
|
|
case MCExpr::Constant:
|
|
|
|
OS << cast<MCConstantExpr>(*this).getValue();
|
|
|
|
return;
|
|
|
|
|
2009-09-09 01:20:50 +02:00
|
|
|
case MCExpr::SymbolRef: {
|
|
|
|
const MCSymbol &Sym = cast<MCSymbolRefExpr>(*this).getSymbol();
|
|
|
|
|
|
|
|
// Parenthesize names that start with $ so that they don't look like
|
|
|
|
// absolute names.
|
|
|
|
if (Sym.getName()[0] == '$') {
|
|
|
|
OS << '(';
|
|
|
|
Sym.print(OS, MAI);
|
|
|
|
OS << ')';
|
|
|
|
} else {
|
|
|
|
Sym.print(OS, MAI);
|
|
|
|
}
|
2009-08-31 10:07:33 +02:00
|
|
|
return;
|
2009-09-09 01:20:50 +02:00
|
|
|
}
|
2009-08-31 10:07:33 +02:00
|
|
|
|
|
|
|
case MCExpr::Unary: {
|
|
|
|
const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
|
|
|
|
switch (UE.getOpcode()) {
|
|
|
|
default: assert(0 && "Invalid opcode!");
|
|
|
|
case MCUnaryExpr::LNot: OS << '!'; break;
|
|
|
|
case MCUnaryExpr::Minus: OS << '-'; break;
|
|
|
|
case MCUnaryExpr::Not: OS << '~'; break;
|
|
|
|
case MCUnaryExpr::Plus: OS << '+'; break;
|
|
|
|
}
|
2009-09-03 07:46:51 +02:00
|
|
|
UE.getSubExpr()->print(OS, MAI);
|
2009-08-31 10:07:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MCExpr::Binary: {
|
|
|
|
const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
|
2009-09-08 08:34:07 +02:00
|
|
|
|
|
|
|
// 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 << ')';
|
|
|
|
}
|
|
|
|
|
2009-08-31 10:07:33 +02:00
|
|
|
switch (BE.getOpcode()) {
|
|
|
|
default: assert(0 && "Invalid opcode!");
|
2009-09-08 08:37:35 +02:00
|
|
|
case MCBinaryExpr::Add:
|
|
|
|
// Print "X-42" instead of "X+-42".
|
|
|
|
if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
|
|
|
|
if (RHSC->getValue() < 0) {
|
|
|
|
OS << RHSC->getValue();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << '+';
|
|
|
|
break;
|
2009-08-31 10:07:33 +02:00
|
|
|
case MCBinaryExpr::And: OS << '&'; break;
|
|
|
|
case MCBinaryExpr::Div: OS << '/'; break;
|
|
|
|
case MCBinaryExpr::EQ: OS << "=="; break;
|
|
|
|
case MCBinaryExpr::GT: OS << '>'; break;
|
|
|
|
case MCBinaryExpr::GTE: OS << ">="; break;
|
|
|
|
case MCBinaryExpr::LAnd: OS << "&&"; break;
|
|
|
|
case MCBinaryExpr::LOr: OS << "||"; break;
|
|
|
|
case MCBinaryExpr::LT: OS << '<'; break;
|
|
|
|
case MCBinaryExpr::LTE: OS << "<="; break;
|
|
|
|
case MCBinaryExpr::Mod: OS << '%'; break;
|
|
|
|
case MCBinaryExpr::Mul: OS << '*'; break;
|
|
|
|
case MCBinaryExpr::NE: OS << "!="; break;
|
|
|
|
case MCBinaryExpr::Or: OS << '|'; break;
|
|
|
|
case MCBinaryExpr::Shl: OS << "<<"; break;
|
|
|
|
case MCBinaryExpr::Shr: OS << ">>"; break;
|
|
|
|
case MCBinaryExpr::Sub: OS << '-'; break;
|
|
|
|
case MCBinaryExpr::Xor: OS << '^'; break;
|
|
|
|
}
|
2009-09-08 08:34:07 +02:00
|
|
|
|
|
|
|
// 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 << ')';
|
|
|
|
}
|
2009-08-31 10:07:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0 && "Invalid expression kind!");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCExpr::dump() const {
|
2010-01-05 02:28:07 +01:00
|
|
|
print(dbgs(), 0);
|
|
|
|
dbgs() << '\n';
|
2009-08-31 10:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* *** */
|
|
|
|
|
2009-09-08 08:27:48 +02:00
|
|
|
const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
|
|
|
|
const MCExpr *RHS, MCContext &Ctx) {
|
2009-08-31 10:07:22 +02:00
|
|
|
return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
|
2009-06-29 22:40:36 +02:00
|
|
|
}
|
|
|
|
|
2009-09-08 08:27:48 +02:00
|
|
|
const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
|
|
|
|
MCContext &Ctx) {
|
2009-08-31 10:07:22 +02:00
|
|
|
return new (Ctx) MCUnaryExpr(Opc, Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
|
|
|
|
return new (Ctx) MCConstantExpr(Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
|
|
|
|
MCContext &Ctx) {
|
|
|
|
return new (Ctx) MCSymbolRefExpr(Sym);
|
|
|
|
}
|
|
|
|
|
2009-11-06 11:58:06 +01:00
|
|
|
const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
|
2009-09-16 03:26:31 +02:00
|
|
|
return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-31 10:07:22 +02:00
|
|
|
/* *** */
|
|
|
|
|
2009-10-16 03:57:52 +02:00
|
|
|
bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
|
2009-06-30 03:49:52 +02:00
|
|
|
MCValue Value;
|
|
|
|
|
2009-10-16 03:57:52 +02:00
|
|
|
if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
|
2009-06-30 03:49:52 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Res = Value.getConstant();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-26 11:16:46 +02:00
|
|
|
static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
|
|
|
|
const MCSymbol *RHS_B, int64_t RHS_Cst,
|
2009-06-30 04:08:27 +02:00
|
|
|
MCValue &Res) {
|
|
|
|
// We can't add or subtract two symbols.
|
|
|
|
if ((LHS.getSymA() && RHS_A) ||
|
|
|
|
(LHS.getSymB() && RHS_B))
|
|
|
|
return false;
|
|
|
|
|
2009-08-26 11:16:46 +02:00
|
|
|
const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
|
|
|
|
const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
|
2009-06-30 04:08:27 +02:00
|
|
|
if (B) {
|
2009-07-01 00:49:27 +02:00
|
|
|
// If we have a negated symbol, then we must have also have a non-negated
|
|
|
|
// symbol in order to encode the expression. We can do this check later to
|
|
|
|
// permit expressions which eventually fold to a representable form -- such
|
2009-06-30 04:08:27 +02:00
|
|
|
// as (a + (0 - b)) -- if necessary.
|
2009-07-01 00:49:27 +02:00
|
|
|
if (!A)
|
2009-06-30 04:08:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-16 03:57:52 +02:00
|
|
|
bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
|
2009-06-29 22:40:36 +02:00
|
|
|
switch (getKind()) {
|
|
|
|
case Constant:
|
2009-08-31 10:06:59 +02:00
|
|
|
Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
|
2009-06-29 22:40:36 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case SymbolRef: {
|
2009-08-31 10:07:22 +02:00
|
|
|
const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
|
2009-10-16 03:33:57 +02:00
|
|
|
|
|
|
|
// Evaluate recursively if this is a variable.
|
|
|
|
if (Sym.isVariable())
|
2009-10-16 03:57:52 +02:00
|
|
|
return Sym.getValue()->EvaluateAsRelocatable(Res);
|
2009-10-16 03:33:57 +02:00
|
|
|
|
2009-10-16 03:33:11 +02:00
|
|
|
Res = MCValue::get(&Sym, 0, 0);
|
2009-06-29 22:40:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Unary: {
|
2009-08-31 10:06:59 +02:00
|
|
|
const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
|
2009-06-30 03:49:52 +02:00
|
|
|
MCValue Value;
|
2009-06-29 22:40:36 +02:00
|
|
|
|
2009-10-16 03:57:52 +02:00
|
|
|
if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
|
2009-06-29 22:40:36 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (AUE->getOpcode()) {
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCUnaryExpr::LNot:
|
2009-07-01 08:48:00 +02:00
|
|
|
if (!Value.isAbsolute())
|
2009-06-30 03:49:52 +02:00
|
|
|
return false;
|
|
|
|
Res = MCValue::get(!Value.getConstant());
|
|
|
|
break;
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCUnaryExpr::Minus:
|
2009-06-30 03:49:52 +02:00
|
|
|
/// -(a - b + const) ==> (b - a - const)
|
2009-08-11 19:47:52 +02:00
|
|
|
if (Value.getSymA() && !Value.getSymB())
|
2009-06-30 03:49:52 +02:00
|
|
|
return false;
|
|
|
|
Res = MCValue::get(Value.getSymB(), Value.getSymA(),
|
|
|
|
-Value.getConstant());
|
|
|
|
break;
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCUnaryExpr::Not:
|
2009-07-01 08:48:00 +02:00
|
|
|
if (!Value.isAbsolute())
|
2009-06-30 03:49:52 +02:00
|
|
|
return false;
|
|
|
|
Res = MCValue::get(~Value.getConstant());
|
|
|
|
break;
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCUnaryExpr::Plus:
|
2009-06-30 03:49:52 +02:00
|
|
|
Res = Value;
|
|
|
|
break;
|
2009-06-29 22:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Binary: {
|
2009-08-31 10:06:59 +02:00
|
|
|
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
|
2009-06-30 03:49:52 +02:00
|
|
|
MCValue LHSValue, RHSValue;
|
2009-06-29 22:40:36 +02:00
|
|
|
|
2009-10-16 03:57:52 +02:00
|
|
|
if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
|
|
|
|
!ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
|
2009-06-29 22:40:36 +02:00
|
|
|
return false;
|
|
|
|
|
2009-06-30 03:49:52 +02:00
|
|
|
// We only support a few operations on non-constant expressions, handle
|
|
|
|
// those first.
|
2009-07-01 08:48:00 +02:00
|
|
|
if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
|
2009-06-30 03:49:52 +02:00
|
|
|
switch (ABE->getOpcode()) {
|
|
|
|
default:
|
|
|
|
return false;
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCBinaryExpr::Sub:
|
2009-06-30 04:08:27 +02:00
|
|
|
// Negate RHS and add.
|
|
|
|
return EvaluateSymbolicAdd(LHSValue,
|
|
|
|
RHSValue.getSymB(), RHSValue.getSymA(),
|
|
|
|
-RHSValue.getConstant(),
|
|
|
|
Res);
|
|
|
|
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCBinaryExpr::Add:
|
2009-06-30 04:08:27 +02:00
|
|
|
return EvaluateSymbolicAdd(LHSValue,
|
|
|
|
RHSValue.getSymA(), RHSValue.getSymB(),
|
|
|
|
RHSValue.getConstant(),
|
|
|
|
Res);
|
2009-06-30 03:49:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 22:40:36 +02:00
|
|
|
// FIXME: We need target hooks for the evaluation. It may be limited in
|
|
|
|
// width, and gas defines the result of comparisons differently from Apple
|
|
|
|
// as (the result is sign extended).
|
2009-06-30 18:02:47 +02:00
|
|
|
int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
|
|
|
|
int64_t Result = 0;
|
2009-06-29 22:40:36 +02:00
|
|
|
switch (ABE->getOpcode()) {
|
2009-08-31 10:06:59 +02:00
|
|
|
case MCBinaryExpr::Add: Result = LHS + RHS; break;
|
|
|
|
case MCBinaryExpr::And: Result = LHS & RHS; break;
|
|
|
|
case MCBinaryExpr::Div: Result = LHS / RHS; break;
|
|
|
|
case MCBinaryExpr::EQ: Result = LHS == RHS; break;
|
|
|
|
case MCBinaryExpr::GT: Result = LHS > RHS; break;
|
|
|
|
case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
|
|
|
|
case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
|
|
|
|
case MCBinaryExpr::LOr: Result = LHS || RHS; break;
|
|
|
|
case MCBinaryExpr::LT: Result = LHS < RHS; break;
|
|
|
|
case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
|
|
|
|
case MCBinaryExpr::Mod: Result = LHS % RHS; break;
|
|
|
|
case MCBinaryExpr::Mul: Result = LHS * RHS; break;
|
|
|
|
case MCBinaryExpr::NE: Result = LHS != RHS; break;
|
|
|
|
case MCBinaryExpr::Or: Result = LHS | RHS; break;
|
|
|
|
case MCBinaryExpr::Shl: Result = LHS << RHS; break;
|
|
|
|
case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
|
|
|
|
case MCBinaryExpr::Sub: Result = LHS - RHS; break;
|
|
|
|
case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
|
2009-06-29 22:40:36 +02:00
|
|
|
}
|
|
|
|
|
2009-06-30 03:49:52 +02:00
|
|
|
Res = MCValue::get(Result);
|
2009-06-29 22:40:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-31 10:07:22 +02:00
|
|
|
assert(0 && "Invalid assembly expression kind!");
|
|
|
|
return false;
|
|
|
|
}
|