1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
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 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCExpr.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCSymbol.h"
13 #include "llvm/MC/MCValue.h"
14 #include "llvm/Support/raw_ostream.h"
17 void MCExpr::print(raw_ostream &OS) const {
19 case MCExpr::Constant:
20 OS << cast<MCConstantExpr>(*this).getValue();
23 case MCExpr::SymbolRef:
24 cast<MCSymbolRefExpr>(*this).getSymbol().print(OS);
28 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
29 switch (UE.getOpcode()) {
30 default: assert(0 && "Invalid opcode!");
31 case MCUnaryExpr::LNot: OS << '!'; break;
32 case MCUnaryExpr::Minus: OS << '-'; break;
33 case MCUnaryExpr::Not: OS << '~'; break;
34 case MCUnaryExpr::Plus: OS << '+'; break;
36 UE.getSubExpr()->print(OS);
40 case MCExpr::Binary: {
41 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
43 BE.getLHS()->print(OS);
45 switch (BE.getOpcode()) {
46 default: assert(0 && "Invalid opcode!");
47 case MCBinaryExpr::Add: OS << '+'; break;
48 case MCBinaryExpr::And: OS << '&'; break;
49 case MCBinaryExpr::Div: OS << '/'; break;
50 case MCBinaryExpr::EQ: OS << "=="; break;
51 case MCBinaryExpr::GT: OS << '>'; break;
52 case MCBinaryExpr::GTE: OS << ">="; break;
53 case MCBinaryExpr::LAnd: OS << "&&"; break;
54 case MCBinaryExpr::LOr: OS << "||"; break;
55 case MCBinaryExpr::LT: OS << '<'; break;
56 case MCBinaryExpr::LTE: OS << "<="; break;
57 case MCBinaryExpr::Mod: OS << '%'; break;
58 case MCBinaryExpr::Mul: OS << '*'; break;
59 case MCBinaryExpr::NE: OS << "!="; break;
60 case MCBinaryExpr::Or: OS << '|'; break;
61 case MCBinaryExpr::Shl: OS << "<<"; break;
62 case MCBinaryExpr::Shr: OS << ">>"; break;
63 case MCBinaryExpr::Sub: OS << '-'; break;
64 case MCBinaryExpr::Xor: OS << '^'; break;
67 BE.getRHS()->print(OS);
73 assert(0 && "Invalid expression kind!");
76 void MCExpr::dump() const {
83 const MCBinaryExpr * MCBinaryExpr::Create(Opcode Opc,
87 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
90 const MCUnaryExpr * MCUnaryExpr::Create(Opcode Opc,
93 return new (Ctx) MCUnaryExpr(Opc, Expr);
96 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
97 return new (Ctx) MCConstantExpr(Value);
100 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
102 return new (Ctx) MCSymbolRefExpr(Sym);
107 bool MCExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const {
110 if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute())
113 Res = Value.getConstant();
117 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
118 const MCSymbol *RHS_B, int64_t RHS_Cst,
120 // We can't add or subtract two symbols.
121 if ((LHS.getSymA() && RHS_A) ||
122 (LHS.getSymB() && RHS_B))
125 const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
126 const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
128 // If we have a negated symbol, then we must have also have a non-negated
129 // symbol in order to encode the expression. We can do this check later to
130 // permit expressions which eventually fold to a representable form -- such
131 // as (a + (0 - b)) -- if necessary.
135 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
139 bool MCExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const {
142 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
146 const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
147 if (const MCValue *Value = Ctx.GetSymbolValue(&Sym))
150 Res = MCValue::get(&Sym, 0, 0);
155 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
158 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value))
161 switch (AUE->getOpcode()) {
162 case MCUnaryExpr::LNot:
163 if (!Value.isAbsolute())
165 Res = MCValue::get(!Value.getConstant());
167 case MCUnaryExpr::Minus:
168 /// -(a - b + const) ==> (b - a - const)
169 if (Value.getSymA() && !Value.getSymB())
171 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
172 -Value.getConstant());
174 case MCUnaryExpr::Not:
175 if (!Value.isAbsolute())
177 Res = MCValue::get(~Value.getConstant());
179 case MCUnaryExpr::Plus:
188 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
189 MCValue LHSValue, RHSValue;
191 if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) ||
192 !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue))
195 // We only support a few operations on non-constant expressions, handle
197 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
198 switch (ABE->getOpcode()) {
201 case MCBinaryExpr::Sub:
202 // Negate RHS and add.
203 return EvaluateSymbolicAdd(LHSValue,
204 RHSValue.getSymB(), RHSValue.getSymA(),
205 -RHSValue.getConstant(),
208 case MCBinaryExpr::Add:
209 return EvaluateSymbolicAdd(LHSValue,
210 RHSValue.getSymA(), RHSValue.getSymB(),
211 RHSValue.getConstant(),
216 // FIXME: We need target hooks for the evaluation. It may be limited in
217 // width, and gas defines the result of comparisons differently from Apple
218 // as (the result is sign extended).
219 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
221 switch (ABE->getOpcode()) {
222 case MCBinaryExpr::Add: Result = LHS + RHS; break;
223 case MCBinaryExpr::And: Result = LHS & RHS; break;
224 case MCBinaryExpr::Div: Result = LHS / RHS; break;
225 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
226 case MCBinaryExpr::GT: Result = LHS > RHS; break;
227 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
228 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
229 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
230 case MCBinaryExpr::LT: Result = LHS < RHS; break;
231 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
232 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
233 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
234 case MCBinaryExpr::NE: Result = LHS != RHS; break;
235 case MCBinaryExpr::Or: Result = LHS | RHS; break;
236 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
237 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
238 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
239 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
242 Res = MCValue::get(Result);
247 assert(0 && "Invalid assembly expression kind!");