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 {
20 case MCExpr::Constant:
21 OS << cast<MCConstantExpr>(*this).getValue();
24 case MCExpr::SymbolRef: {
25 const MCSymbol &Sym = cast<MCSymbolRefExpr>(*this).getSymbol();
27 // Parenthesize names that start with $ so that they don't look like
29 if (Sym.getName()[0] == '$')
30 OS << '(' << Sym << ')';
37 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
38 switch (UE.getOpcode()) {
39 default: assert(0 && "Invalid opcode!");
40 case MCUnaryExpr::LNot: OS << '!'; break;
41 case MCUnaryExpr::Minus: OS << '-'; break;
42 case MCUnaryExpr::Not: OS << '~'; break;
43 case MCUnaryExpr::Plus: OS << '+'; break;
45 OS << *UE.getSubExpr();
49 case MCExpr::Binary: {
50 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
52 // Only print parens around the LHS if it is non-trivial.
53 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
56 OS << '(' << *BE.getLHS() << ')';
59 switch (BE.getOpcode()) {
60 default: assert(0 && "Invalid opcode!");
61 case MCBinaryExpr::Add:
62 // Print "X-42" instead of "X+-42".
63 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
64 if (RHSC->getValue() < 0) {
65 OS << RHSC->getValue();
72 case MCBinaryExpr::And: OS << '&'; break;
73 case MCBinaryExpr::Div: OS << '/'; break;
74 case MCBinaryExpr::EQ: OS << "=="; break;
75 case MCBinaryExpr::GT: OS << '>'; break;
76 case MCBinaryExpr::GTE: OS << ">="; break;
77 case MCBinaryExpr::LAnd: OS << "&&"; break;
78 case MCBinaryExpr::LOr: OS << "||"; break;
79 case MCBinaryExpr::LT: OS << '<'; break;
80 case MCBinaryExpr::LTE: OS << "<="; break;
81 case MCBinaryExpr::Mod: OS << '%'; break;
82 case MCBinaryExpr::Mul: OS << '*'; break;
83 case MCBinaryExpr::NE: OS << "!="; break;
84 case MCBinaryExpr::Or: OS << '|'; break;
85 case MCBinaryExpr::Shl: OS << "<<"; break;
86 case MCBinaryExpr::Shr: OS << ">>"; break;
87 case MCBinaryExpr::Sub: OS << '-'; break;
88 case MCBinaryExpr::Xor: OS << '^'; break;
91 // Only print parens around the LHS if it is non-trivial.
92 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
95 OS << '(' << *BE.getRHS() << ')';
101 assert(0 && "Invalid expression kind!");
104 void MCExpr::dump() const {
111 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
112 const MCExpr *RHS, MCContext &Ctx) {
113 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
116 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
118 return new (Ctx) MCUnaryExpr(Opc, Expr);
121 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
122 return new (Ctx) MCConstantExpr(Value);
125 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
127 return new (Ctx) MCSymbolRefExpr(Sym);
130 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
131 return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
137 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
140 if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
143 Res = Value.getConstant();
147 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
148 const MCSymbol *RHS_B, int64_t RHS_Cst,
150 // We can't add or subtract two symbols.
151 if ((LHS.getSymA() && RHS_A) ||
152 (LHS.getSymB() && RHS_B))
155 const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
156 const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
158 // If we have a negated symbol, then we must have also have a non-negated
159 // symbol in order to encode the expression. We can do this check later to
160 // permit expressions which eventually fold to a representable form -- such
161 // as (a + (0 - b)) -- if necessary.
165 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
169 bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
172 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
176 const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
178 // Evaluate recursively if this is a variable.
179 if (Sym.isVariable())
180 return Sym.getValue()->EvaluateAsRelocatable(Res);
182 Res = MCValue::get(&Sym, 0, 0);
187 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
190 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
193 switch (AUE->getOpcode()) {
194 case MCUnaryExpr::LNot:
195 if (!Value.isAbsolute())
197 Res = MCValue::get(!Value.getConstant());
199 case MCUnaryExpr::Minus:
200 /// -(a - b + const) ==> (b - a - const)
201 if (Value.getSymA() && !Value.getSymB())
203 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
204 -Value.getConstant());
206 case MCUnaryExpr::Not:
207 if (!Value.isAbsolute())
209 Res = MCValue::get(~Value.getConstant());
211 case MCUnaryExpr::Plus:
220 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
221 MCValue LHSValue, RHSValue;
223 if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
224 !ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
227 // We only support a few operations on non-constant expressions, handle
229 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
230 switch (ABE->getOpcode()) {
233 case MCBinaryExpr::Sub:
234 // Negate RHS and add.
235 return EvaluateSymbolicAdd(LHSValue,
236 RHSValue.getSymB(), RHSValue.getSymA(),
237 -RHSValue.getConstant(),
240 case MCBinaryExpr::Add:
241 return EvaluateSymbolicAdd(LHSValue,
242 RHSValue.getSymA(), RHSValue.getSymB(),
243 RHSValue.getConstant(),
248 // FIXME: We need target hooks for the evaluation. It may be limited in
249 // width, and gas defines the result of comparisons differently from Apple
250 // as (the result is sign extended).
251 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
253 switch (ABE->getOpcode()) {
254 case MCBinaryExpr::Add: Result = LHS + RHS; break;
255 case MCBinaryExpr::And: Result = LHS & RHS; break;
256 case MCBinaryExpr::Div: Result = LHS / RHS; break;
257 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
258 case MCBinaryExpr::GT: Result = LHS > RHS; break;
259 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
260 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
261 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
262 case MCBinaryExpr::LT: Result = LHS < RHS; break;
263 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
264 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
265 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
266 case MCBinaryExpr::NE: Result = LHS != RHS; break;
267 case MCBinaryExpr::Or: Result = LHS | RHS; break;
268 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
269 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
270 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
271 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
274 Res = MCValue::get(Result);
279 assert(0 && "Invalid assembly expression kind!");