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/Debug.h"
15 #include "llvm/Support/raw_ostream.h"
18 void MCExpr::print(raw_ostream &OS) const {
21 return cast<MCTargetExpr>(this)->PrintImpl(OS);
22 case MCExpr::Constant:
23 OS << cast<MCConstantExpr>(*this).getValue();
26 case MCExpr::SymbolRef: {
27 const MCSymbol &Sym = cast<MCSymbolRefExpr>(*this).getSymbol();
29 // Parenthesize names that start with $ so that they don't look like
31 if (Sym.getName()[0] == '$')
32 OS << '(' << Sym << ')';
39 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
40 switch (UE.getOpcode()) {
41 default: assert(0 && "Invalid opcode!");
42 case MCUnaryExpr::LNot: OS << '!'; break;
43 case MCUnaryExpr::Minus: OS << '-'; break;
44 case MCUnaryExpr::Not: OS << '~'; break;
45 case MCUnaryExpr::Plus: OS << '+'; break;
47 OS << *UE.getSubExpr();
51 case MCExpr::Binary: {
52 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
54 // Only print parens around the LHS if it is non-trivial.
55 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
58 OS << '(' << *BE.getLHS() << ')';
61 switch (BE.getOpcode()) {
62 default: assert(0 && "Invalid opcode!");
63 case MCBinaryExpr::Add:
64 // Print "X-42" instead of "X+-42".
65 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
66 if (RHSC->getValue() < 0) {
67 OS << RHSC->getValue();
74 case MCBinaryExpr::And: OS << '&'; break;
75 case MCBinaryExpr::Div: OS << '/'; break;
76 case MCBinaryExpr::EQ: OS << "=="; break;
77 case MCBinaryExpr::GT: OS << '>'; break;
78 case MCBinaryExpr::GTE: OS << ">="; break;
79 case MCBinaryExpr::LAnd: OS << "&&"; break;
80 case MCBinaryExpr::LOr: OS << "||"; break;
81 case MCBinaryExpr::LT: OS << '<'; break;
82 case MCBinaryExpr::LTE: OS << "<="; break;
83 case MCBinaryExpr::Mod: OS << '%'; break;
84 case MCBinaryExpr::Mul: OS << '*'; break;
85 case MCBinaryExpr::NE: OS << "!="; break;
86 case MCBinaryExpr::Or: OS << '|'; break;
87 case MCBinaryExpr::Shl: OS << "<<"; break;
88 case MCBinaryExpr::Shr: OS << ">>"; break;
89 case MCBinaryExpr::Sub: OS << '-'; break;
90 case MCBinaryExpr::Xor: OS << '^'; break;
93 // Only print parens around the LHS if it is non-trivial.
94 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
97 OS << '(' << *BE.getRHS() << ')';
103 assert(0 && "Invalid expression kind!");
106 void MCExpr::dump() const {
113 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
114 const MCExpr *RHS, MCContext &Ctx) {
115 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
118 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
120 return new (Ctx) MCUnaryExpr(Opc, Expr);
123 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
124 return new (Ctx) MCConstantExpr(Value);
127 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
129 return new (Ctx) MCSymbolRefExpr(Sym);
132 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
133 return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
136 const MCSymbolRefExpr *MCSymbolRefExpr::CreateTemp(StringRef Name,
138 return Create(Ctx.GetOrCreateTemporarySymbol(Name), Ctx);
141 void MCTargetExpr::Anchor() {}
145 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
148 if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
151 Res = Value.getConstant();
155 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
156 const MCSymbol *RHS_B, int64_t RHS_Cst,
158 // We can't add or subtract two symbols.
159 if ((LHS.getSymA() && RHS_A) ||
160 (LHS.getSymB() && RHS_B))
163 const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
164 const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
166 // If we have a negated symbol, then we must have also have a non-negated
167 // symbol in order to encode the expression. We can do this check later to
168 // permit expressions which eventually fold to a representable form -- such
169 // as (a + (0 - b)) -- if necessary.
173 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
177 bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
180 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res);
183 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
187 const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
189 // Evaluate recursively if this is a variable.
190 if (Sym.isVariable())
191 return Sym.getValue()->EvaluateAsRelocatable(Res);
193 Res = MCValue::get(&Sym, 0, 0);
198 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
201 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
204 switch (AUE->getOpcode()) {
205 case MCUnaryExpr::LNot:
206 if (!Value.isAbsolute())
208 Res = MCValue::get(!Value.getConstant());
210 case MCUnaryExpr::Minus:
211 /// -(a - b + const) ==> (b - a - const)
212 if (Value.getSymA() && !Value.getSymB())
214 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
215 -Value.getConstant());
217 case MCUnaryExpr::Not:
218 if (!Value.isAbsolute())
220 Res = MCValue::get(~Value.getConstant());
222 case MCUnaryExpr::Plus:
231 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
232 MCValue LHSValue, RHSValue;
234 if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
235 !ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
238 // We only support a few operations on non-constant expressions, handle
240 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
241 switch (ABE->getOpcode()) {
244 case MCBinaryExpr::Sub:
245 // Negate RHS and add.
246 return EvaluateSymbolicAdd(LHSValue,
247 RHSValue.getSymB(), RHSValue.getSymA(),
248 -RHSValue.getConstant(),
251 case MCBinaryExpr::Add:
252 return EvaluateSymbolicAdd(LHSValue,
253 RHSValue.getSymA(), RHSValue.getSymB(),
254 RHSValue.getConstant(),
259 // FIXME: We need target hooks for the evaluation. It may be limited in
260 // width, and gas defines the result of comparisons and right shifts
261 // differently from Apple as.
262 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
264 switch (ABE->getOpcode()) {
265 case MCBinaryExpr::Add: Result = LHS + RHS; break;
266 case MCBinaryExpr::And: Result = LHS & RHS; break;
267 case MCBinaryExpr::Div: Result = LHS / RHS; break;
268 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
269 case MCBinaryExpr::GT: Result = LHS > RHS; break;
270 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
271 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
272 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
273 case MCBinaryExpr::LT: Result = LHS < RHS; break;
274 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
275 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
276 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
277 case MCBinaryExpr::NE: Result = LHS != RHS; break;
278 case MCBinaryExpr::Or: Result = LHS | RHS; break;
279 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
280 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
281 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
282 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
285 Res = MCValue::get(Result);
290 assert(0 && "Invalid assembly expression kind!");