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 MCAsmInfo *MAI) const {
19 case MCExpr::Constant:
20 OS << cast<MCConstantExpr>(*this).getValue();
23 case MCExpr::SymbolRef:
24 cast<MCSymbolRefExpr>(*this).getSymbol().print(OS, MAI);
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, MAI);
40 case MCExpr::Binary: {
41 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
43 // Only print parens around the LHS if it is non-trivial.
44 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
45 BE.getLHS()->print(OS, MAI);
48 BE.getLHS()->print(OS, MAI);
52 switch (BE.getOpcode()) {
53 default: assert(0 && "Invalid opcode!");
54 case MCBinaryExpr::Add: OS << '+'; break;
55 case MCBinaryExpr::And: OS << '&'; break;
56 case MCBinaryExpr::Div: OS << '/'; break;
57 case MCBinaryExpr::EQ: OS << "=="; break;
58 case MCBinaryExpr::GT: OS << '>'; break;
59 case MCBinaryExpr::GTE: OS << ">="; break;
60 case MCBinaryExpr::LAnd: OS << "&&"; break;
61 case MCBinaryExpr::LOr: OS << "||"; break;
62 case MCBinaryExpr::LT: OS << '<'; break;
63 case MCBinaryExpr::LTE: OS << "<="; break;
64 case MCBinaryExpr::Mod: OS << '%'; break;
65 case MCBinaryExpr::Mul: OS << '*'; break;
66 case MCBinaryExpr::NE: OS << "!="; break;
67 case MCBinaryExpr::Or: OS << '|'; break;
68 case MCBinaryExpr::Shl: OS << "<<"; break;
69 case MCBinaryExpr::Shr: OS << ">>"; break;
70 case MCBinaryExpr::Sub: OS << '-'; break;
71 case MCBinaryExpr::Xor: OS << '^'; break;
74 // Only print parens around the LHS if it is non-trivial.
75 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
76 BE.getRHS()->print(OS, MAI);
79 BE.getRHS()->print(OS, MAI);
86 assert(0 && "Invalid expression kind!");
89 void MCExpr::dump() const {
96 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
97 const MCExpr *RHS, MCContext &Ctx) {
98 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
101 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
103 return new (Ctx) MCUnaryExpr(Opc, Expr);
106 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
107 return new (Ctx) MCConstantExpr(Value);
110 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
112 return new (Ctx) MCSymbolRefExpr(Sym);
117 bool MCExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const {
120 if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute())
123 Res = Value.getConstant();
127 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A,
128 const MCSymbol *RHS_B, int64_t RHS_Cst,
130 // We can't add or subtract two symbols.
131 if ((LHS.getSymA() && RHS_A) ||
132 (LHS.getSymB() && RHS_B))
135 const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
136 const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
138 // If we have a negated symbol, then we must have also have a non-negated
139 // symbol in order to encode the expression. We can do this check later to
140 // permit expressions which eventually fold to a representable form -- such
141 // as (a + (0 - b)) -- if necessary.
145 Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
149 bool MCExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const {
152 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
156 const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
157 if (const MCValue *Value = Ctx.GetSymbolValue(&Sym))
160 Res = MCValue::get(&Sym, 0, 0);
165 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
168 if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value))
171 switch (AUE->getOpcode()) {
172 case MCUnaryExpr::LNot:
173 if (!Value.isAbsolute())
175 Res = MCValue::get(!Value.getConstant());
177 case MCUnaryExpr::Minus:
178 /// -(a - b + const) ==> (b - a - const)
179 if (Value.getSymA() && !Value.getSymB())
181 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
182 -Value.getConstant());
184 case MCUnaryExpr::Not:
185 if (!Value.isAbsolute())
187 Res = MCValue::get(~Value.getConstant());
189 case MCUnaryExpr::Plus:
198 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
199 MCValue LHSValue, RHSValue;
201 if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) ||
202 !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue))
205 // We only support a few operations on non-constant expressions, handle
207 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
208 switch (ABE->getOpcode()) {
211 case MCBinaryExpr::Sub:
212 // Negate RHS and add.
213 return EvaluateSymbolicAdd(LHSValue,
214 RHSValue.getSymB(), RHSValue.getSymA(),
215 -RHSValue.getConstant(),
218 case MCBinaryExpr::Add:
219 return EvaluateSymbolicAdd(LHSValue,
220 RHSValue.getSymA(), RHSValue.getSymB(),
221 RHSValue.getConstant(),
226 // FIXME: We need target hooks for the evaluation. It may be limited in
227 // width, and gas defines the result of comparisons differently from Apple
228 // as (the result is sign extended).
229 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
231 switch (ABE->getOpcode()) {
232 case MCBinaryExpr::Add: Result = LHS + RHS; break;
233 case MCBinaryExpr::And: Result = LHS & RHS; break;
234 case MCBinaryExpr::Div: Result = LHS / RHS; break;
235 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
236 case MCBinaryExpr::GT: Result = LHS > RHS; break;
237 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
238 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
239 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
240 case MCBinaryExpr::LT: Result = LHS < RHS; break;
241 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
242 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
243 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
244 case MCBinaryExpr::NE: Result = LHS != RHS; break;
245 case MCBinaryExpr::Or: Result = LHS | RHS; break;
246 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
247 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
248 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
249 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
252 Res = MCValue::get(Result);
257 assert(0 && "Invalid assembly expression kind!");