2009-06-29 20:40:36 +00:00
|
|
|
//===- AsmExpr.cpp - Assembly file expressions ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AsmExpr.h"
|
|
|
|
#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;
|
|
|
|
|
|
|
|
AsmExpr::~AsmExpr() {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AsmExpr::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-06-30 01:49:52 +00:00
|
|
|
bool AsmExpr::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-06-30 01:49:52 +00:00
|
|
|
Res = MCValue::get(cast<AsmConstantExpr>(this)->getValue());
|
2009-06-29 20:40:36 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case SymbolRef: {
|
|
|
|
MCSymbol *Sym = cast<AsmSymbolRefExpr>(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: {
|
|
|
|
const AsmUnaryExpr *AUE = cast<AsmUnaryExpr>(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-06-30 01:49:52 +00:00
|
|
|
case AsmUnaryExpr::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;
|
|
|
|
case AsmUnaryExpr::Minus:
|
|
|
|
/// -(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;
|
|
|
|
case AsmUnaryExpr::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;
|
|
|
|
case AsmUnaryExpr::Plus:
|
|
|
|
Res = Value;
|
|
|
|
break;
|
2009-06-29 20:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Binary: {
|
|
|
|
const AsmBinaryExpr *ABE = cast<AsmBinaryExpr>(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;
|
|
|
|
case AsmBinaryExpr::Sub:
|
2009-06-30 02:08:27 +00:00
|
|
|
// Negate RHS and add.
|
|
|
|
return EvaluateSymbolicAdd(LHSValue,
|
|
|
|
RHSValue.getSymB(), RHSValue.getSymA(),
|
|
|
|
-RHSValue.getConstant(),
|
|
|
|
Res);
|
|
|
|
|
2009-06-30 01:49:52 +00:00
|
|
|
case AsmBinaryExpr::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-06-30 01:49:52 +00:00
|
|
|
case AsmBinaryExpr::Add: Result = LHS + RHS; break;
|
|
|
|
case AsmBinaryExpr::And: Result = LHS & RHS; break;
|
|
|
|
case AsmBinaryExpr::Div: Result = LHS / RHS; break;
|
|
|
|
case AsmBinaryExpr::EQ: Result = LHS == RHS; break;
|
|
|
|
case AsmBinaryExpr::GT: Result = LHS > RHS; break;
|
|
|
|
case AsmBinaryExpr::GTE: Result = LHS >= RHS; break;
|
|
|
|
case AsmBinaryExpr::LAnd: Result = LHS && RHS; break;
|
|
|
|
case AsmBinaryExpr::LOr: Result = LHS || RHS; break;
|
|
|
|
case AsmBinaryExpr::LT: Result = LHS < RHS; break;
|
|
|
|
case AsmBinaryExpr::LTE: Result = LHS <= RHS; break;
|
|
|
|
case AsmBinaryExpr::Mod: Result = LHS % RHS; break;
|
|
|
|
case AsmBinaryExpr::Mul: Result = LHS * RHS; break;
|
|
|
|
case AsmBinaryExpr::NE: Result = LHS != RHS; break;
|
|
|
|
case AsmBinaryExpr::Or: Result = LHS | RHS; break;
|
|
|
|
case AsmBinaryExpr::Shl: Result = LHS << RHS; break;
|
|
|
|
case AsmBinaryExpr::Shr: Result = LHS >> RHS; break;
|
|
|
|
case AsmBinaryExpr::Sub: Result = LHS - RHS; break;
|
|
|
|
case AsmBinaryExpr::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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|