1 //===- AsmExpr.cpp - Assembly file expressions ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCSymbol.h"
13 #include "llvm/MC/MCValue.h"
19 bool AsmExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const {
22 if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isConstant())
25 Res = Value.getConstant();
29 bool AsmExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const {
32 assert(0 && "Invalid assembly expression kind!");
35 Res = MCValue::get(cast<AsmConstantExpr>(this)->getValue());
39 MCSymbol *Sym = cast<AsmSymbolRefExpr>(this)->getSymbol();
40 if (const MCValue *Value = Ctx.GetSymbolValue(Sym))
43 Res = MCValue::get(Sym, 0, 0);
48 const AsmUnaryExpr *AUE = cast<AsmUnaryExpr>(this);
51 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value))
54 switch (AUE->getOpcode()) {
55 case AsmUnaryExpr::LNot:
56 if (!Value.isConstant())
58 Res = MCValue::get(!Value.getConstant());
60 case AsmUnaryExpr::Minus:
61 /// -(a - b + const) ==> (b - a - const)
62 if (Value.getSymA() && !Value.getSymA())
64 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
65 -Value.getConstant());
67 case AsmUnaryExpr::Not:
68 if (!Value.isConstant())
70 Res = MCValue::get(~Value.getConstant());
72 case AsmUnaryExpr::Plus:
81 const AsmBinaryExpr *ABE = cast<AsmBinaryExpr>(this);
82 MCValue LHSValue, RHSValue;
84 if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) ||
85 !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue))
88 // We only support a few operations on non-constant expressions, handle
90 if (!LHSValue.isConstant() || !RHSValue.isConstant()) {
91 switch (ABE->getOpcode()) {
94 case AsmBinaryExpr::Sub:
95 // Negate RHS and fall through.
96 RHSValue = MCValue::get(RHSValue.getSymB(), RHSValue.getSymA(),
97 -RHSValue.getConstant());
98 case AsmBinaryExpr::Add:
99 // (a_0 - b_0 + cst_0) + (a_1 - b_1 + cst_1)
101 // We can't add or subtract two symbols.
102 if ((LHSValue.getSymA() && RHSValue.getSymB()) ||
103 (LHSValue.getSymB() && RHSValue.getSymB()))
107 LHSValue.getSymA() ? LHSValue.getSymA() : RHSValue.getSymA();
109 LHSValue.getSymB() ? LHSValue.getSymB() : RHSValue.getSymB();
111 // If we have a negated symbol, then we must have also have a
112 // non-negated symbol, and both symbols must be in the same
113 // non-external section. We can do this check later to permit
114 // expressions which eventually fold to a representable form -- such
115 // as (a + (0 - b)) -- if necessary.
116 if (!A || !A->getSection() || A->getSection() != B->getSection())
119 Res = MCValue::get(A, B,
120 LHSValue.getConstant() + RHSValue.getConstant());
125 // FIXME: We need target hooks for the evaluation. It may be limited in
126 // width, and gas defines the result of comparisons differently from Apple
127 // as (the result is sign extended).
128 int64_t Result, LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
129 switch (ABE->getOpcode()) {
130 case AsmBinaryExpr::Add: Result = LHS + RHS; break;
131 case AsmBinaryExpr::And: Result = LHS & RHS; break;
132 case AsmBinaryExpr::Div: Result = LHS / RHS; break;
133 case AsmBinaryExpr::EQ: Result = LHS == RHS; break;
134 case AsmBinaryExpr::GT: Result = LHS > RHS; break;
135 case AsmBinaryExpr::GTE: Result = LHS >= RHS; break;
136 case AsmBinaryExpr::LAnd: Result = LHS && RHS; break;
137 case AsmBinaryExpr::LOr: Result = LHS || RHS; break;
138 case AsmBinaryExpr::LT: Result = LHS < RHS; break;
139 case AsmBinaryExpr::LTE: Result = LHS <= RHS; break;
140 case AsmBinaryExpr::Mod: Result = LHS % RHS; break;
141 case AsmBinaryExpr::Mul: Result = LHS * RHS; break;
142 case AsmBinaryExpr::NE: Result = LHS != RHS; break;
143 case AsmBinaryExpr::Or: Result = LHS | RHS; break;
144 case AsmBinaryExpr::Shl: Result = LHS << RHS; break;
145 case AsmBinaryExpr::Shr: Result = LHS >> RHS; break;
146 case AsmBinaryExpr::Sub: Result = LHS - RHS; break;
147 case AsmBinaryExpr::Xor: Result = LHS ^ RHS; break;
150 Res = MCValue::get(Result);