2009-08-31 08:06:59 +00:00
|
|
|
//===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
|
2009-06-29 20:40:36 +00: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 08:06:59 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2009-06-29 20:40:36 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2009-06-30 01:49:52 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-06-29 20:40:36 +00:00
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-31 08:06:59 +00:00
|
|
|
MCExpr::~MCExpr() {
|
2009-06-29 20:40:36 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 08:06:59 +00:00
|
|
|
bool MCExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const {
|
2009-06-30 01:49:52 +00:00
|
|
|
MCValue Value;
|
|
|
|
|
2009-07-01 06:48:00 +00:00
|
|
|
if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute())
|
2009-06-30 01:49:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Res = Value.getConstant();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-26 09:16:46 +00:00
|
|
|
static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
|
|
|
|
const MCSymbol *RHS_B, int64_t RHS_Cst,
|
2009-06-30 02:08:27 +00: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 09:16:46 +00:00
|
|
|
const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
|
|
|
|
const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
|
2009-06-30 02:08:27 +00:00
|
|
|
if (B) {
|
2009-06-30 22:49:27 +00: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 02:08:27 +00:00
|
|
|
// as (a + (0 - b)) -- if necessary.
|
2009-06-30 22:49:27 +00:00
|
|
|
if (!A)
|
2009-06-30 02:08:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-31 08:06:59 +00:00
|
|
|
bool MCExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const {
|
2009-06-29 20:40:36 +00:00
|
|
|
switch (getKind()) {
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid assembly expression kind!");
|
|
|
|
|
|
|
|
case Constant:
|
2009-08-31 08:06:59 +00:00
|
|
|
Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
|
2009-06-29 20:40:36 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case SymbolRef: {
|
2009-08-31 08:06:59 +00:00
|
|
|
MCSymbol *Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
|
2009-06-30 01:49:52 +00:00
|
|
|
if (const MCValue *Value = Ctx.GetSymbolValue(Sym))
|
|
|
|
Res = *Value;
|
|
|
|
else
|
|
|
|
Res = MCValue::get(Sym, 0, 0);
|
2009-06-29 20:40:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Unary: {
|
2009-08-31 08:06:59 +00:00
|
|
|
const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
|
2009-06-30 01:49:52 +00:00
|
|
|
MCValue Value;
|
2009-06-29 20:40:36 +00:00
|
|
|
|
2009-06-30 01:49:52 +00:00
|
|
|
if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value))
|
2009-06-29 20:40:36 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (AUE->getOpcode()) {
|
2009-08-31 08:06:59 +00:00
|
|
|
case MCUnaryExpr::LNot:
|
2009-07-01 06:48:00 +00:00
|
|
|
if (!Value.isAbsolute())
|
2009-06-30 01:49:52 +00:00
|
|
|
return false;
|
|
|
|
Res = MCValue::get(!Value.getConstant());
|
|
|
|
break;
|
2009-08-31 08:06:59 +00:00
|
|
|
case MCUnaryExpr::Minus:
|
2009-06-30 01:49:52 +00:00
|
|
|
/// -(a - b + const) ==> (b - a - const)
|
2009-08-11 17:47:52 +00:00
|
|
|
if (Value.getSymA() && !Value.getSymB())
|
2009-06-30 01:49:52 +00:00
|
|
|
return false;
|
|
|
|
Res = MCValue::get(Value.getSymB(), Value.getSymA(),
|
|
|
|
-Value.getConstant());
|
|
|
|
break;
|
2009-08-31 08:06:59 +00:00
|
|
|
case MCUnaryExpr::Not:
|
2009-07-01 06:48:00 +00:00
|
|
|
if (!Value.isAbsolute())
|
2009-06-30 01:49:52 +00:00
|
|
|
return false;
|
|
|
|
Res = MCValue::get(~Value.getConstant());
|
|
|
|
break;
|
2009-08-31 08:06:59 +00:00
|
|
|
case MCUnaryExpr::Plus:
|
2009-06-30 01:49:52 +00:00
|
|
|
Res = Value;
|
|
|
|
break;
|
2009-06-29 20:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Binary: {
|
2009-08-31 08:06:59 +00:00
|
|
|
const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
|
2009-06-30 01:49:52 +00:00
|
|
|
MCValue LHSValue, RHSValue;
|
2009-06-29 20:40:36 +00:00
|
|
|
|
2009-06-30 01:49:52 +00:00
|
|
|
if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) ||
|
|
|
|
!ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue))
|
2009-06-29 20:40:36 +00:00
|
|
|
return false;
|
|
|
|
|
2009-06-30 01:49:52 +00:00
|
|
|
// We only support a few operations on non-constant expressions, handle
|
|
|
|
// those first.
|
2009-07-01 06:48:00 +00:00
|
|
|
if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
|
2009-06-30 01:49:52 +00:00
|
|
|
switch (ABE->getOpcode()) {
|
|
|
|
default:
|
|
|
|
return false;
|
2009-08-31 08:06:59 +00:00
|
|
|
case MCBinaryExpr::Sub:
|
2009-06-30 02:08:27 +00:00
|
|
|
// Negate RHS and add.
|
|
|
|
return EvaluateSymbolicAdd(LHSValue,
|
|
|
|
RHSValue.getSymB(), RHSValue.getSymA(),
|
|
|
|
-RHSValue.getConstant(),
|
|
|
|
Res);
|
|
|
|
|
2009-08-31 08:06:59 +00:00
|
|
|
case MCBinaryExpr::Add:
|
2009-06-30 02:08:27 +00:00
|
|
|
return EvaluateSymbolicAdd(LHSValue,
|
|
|
|
RHSValue.getSymA(), RHSValue.getSymB(),
|
|
|
|
RHSValue.getConstant(),
|
|
|
|
Res);
|
2009-06-30 01:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 20:40:36 +00: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 16:02:47 +00:00
|
|
|
int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
|
|
|
|
int64_t Result = 0;
|
2009-06-29 20:40:36 +00:00
|
|
|
switch (ABE->getOpcode()) {
|
2009-08-31 08:06:59 +00: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 20:40:36 +00:00
|
|
|
}
|
|
|
|
|
2009-06-30 01:49:52 +00:00
|
|
|
Res = MCValue::get(Result);
|
2009-06-29 20:40:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|