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 MCAsmInfo *MAI) 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 UE.getSubExpr()->print(OS, MAI);
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())) {
54 BE.getLHS()->print(OS, MAI);
57 BE.getLHS()->print(OS, MAI);
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())) {
95 BE.getRHS()->print(OS, MAI);
98 BE.getRHS()->print(OS, MAI);
105 assert(0 && "Invalid expression kind!");
108 void MCExpr::dump() const {
115 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
116 const MCExpr *RHS, MCContext &Ctx) {
117 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
120 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
122 return new (Ctx) MCUnaryExpr(Opc, Expr);
125 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
126 return new (Ctx) MCConstantExpr(Value);
129 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
131 return new (Ctx) MCSymbolRefExpr(Sym);
134 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
135 return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
141 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
144 if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
147 Res = Value.getConstant();
151 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
152 const MCSymbol *RHS_B, int64_t RHS_Cst,
154 // We can't add or subtract two symbols.
155 if ((LHS.getSymA() && RHS_A) ||
156 (LHS.getSymB() && RHS_B))
159 const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
160 const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
162 // If we have a negated symbol, then we must have also have a non-negated
163 // symbol in order to encode the expression. We can do this check later to
164 // permit expressions which eventually fold to a representable form -- such
165 // as (a + (0 - b)) -- if necessary.
169 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
173 bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
176 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
180 const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
182 // Evaluate recursively if this is a variable.
183 if (Sym.isVariable())
184 return Sym.getValue()->EvaluateAsRelocatable(Res);
186 Res = MCValue::get(&Sym, 0, 0);
191 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
194 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
197 switch (AUE->getOpcode()) {
198 case MCUnaryExpr::LNot:
199 if (!Value.isAbsolute())
201 Res = MCValue::get(!Value.getConstant());
203 case MCUnaryExpr::Minus:
204 /// -(a - b + const) ==> (b - a - const)
205 if (Value.getSymA() && !Value.getSymB())
207 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
208 -Value.getConstant());
210 case MCUnaryExpr::Not:
211 if (!Value.isAbsolute())
213 Res = MCValue::get(~Value.getConstant());
215 case MCUnaryExpr::Plus:
224 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
225 MCValue LHSValue, RHSValue;
227 if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
228 !ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
231 // We only support a few operations on non-constant expressions, handle
233 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
234 switch (ABE->getOpcode()) {
237 case MCBinaryExpr::Sub:
238 // Negate RHS and add.
239 return EvaluateSymbolicAdd(LHSValue,
240 RHSValue.getSymB(), RHSValue.getSymA(),
241 -RHSValue.getConstant(),
244 case MCBinaryExpr::Add:
245 return EvaluateSymbolicAdd(LHSValue,
246 RHSValue.getSymA(), RHSValue.getSymB(),
247 RHSValue.getConstant(),
252 // FIXME: We need target hooks for the evaluation. It may be limited in
253 // width, and gas defines the result of comparisons differently from Apple
254 // as (the result is sign extended).
255 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
257 switch (ABE->getOpcode()) {
258 case MCBinaryExpr::Add: Result = LHS + RHS; break;
259 case MCBinaryExpr::And: Result = LHS & RHS; break;
260 case MCBinaryExpr::Div: Result = LHS / RHS; break;
261 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
262 case MCBinaryExpr::GT: Result = LHS > RHS; break;
263 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
264 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
265 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
266 case MCBinaryExpr::LT: Result = LHS < RHS; break;
267 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
268 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
269 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
270 case MCBinaryExpr::NE: Result = LHS != RHS; break;
271 case MCBinaryExpr::Or: Result = LHS | RHS; break;
272 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
273 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
274 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
275 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
278 Res = MCValue::get(Result);
283 assert(0 && "Invalid assembly expression kind!");