Change errs() to dbgs().
[oota-llvm.git] / lib / MC / MCExpr.cpp
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
16 using namespace llvm;
17
18 void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
19   switch (getKind()) {
20   case MCExpr::Constant:
21     OS << cast<MCConstantExpr>(*this).getValue();
22     return;
23
24   case MCExpr::SymbolRef: {
25     const MCSymbol &Sym = cast<MCSymbolRefExpr>(*this).getSymbol();
26     
27     // Parenthesize names that start with $ so that they don't look like
28     // absolute names.
29     if (Sym.getName()[0] == '$') {
30       OS << '(';
31       Sym.print(OS, MAI);
32       OS << ')';
33     } else {
34       Sym.print(OS, MAI);
35     }
36     return;
37   }
38
39   case MCExpr::Unary: {
40     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
41     switch (UE.getOpcode()) {
42     default: assert(0 && "Invalid opcode!");
43     case MCUnaryExpr::LNot:  OS << '!'; break;
44     case MCUnaryExpr::Minus: OS << '-'; break;
45     case MCUnaryExpr::Not:   OS << '~'; break;
46     case MCUnaryExpr::Plus:  OS << '+'; break;
47     }
48     UE.getSubExpr()->print(OS, MAI);
49     return;
50   }
51
52   case MCExpr::Binary: {
53     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
54     
55     // Only print parens around the LHS if it is non-trivial.
56     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
57       BE.getLHS()->print(OS, MAI);
58     } else {
59       OS << '(';
60       BE.getLHS()->print(OS, MAI);
61       OS << ')';
62     }
63     
64     switch (BE.getOpcode()) {
65     default: assert(0 && "Invalid opcode!");
66     case MCBinaryExpr::Add:
67       // Print "X-42" instead of "X+-42".
68       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
69         if (RHSC->getValue() < 0) {
70           OS << RHSC->getValue();
71           return;
72         }
73       }
74         
75       OS <<  '+';
76       break;
77     case MCBinaryExpr::And:  OS <<  '&'; break;
78     case MCBinaryExpr::Div:  OS <<  '/'; break;
79     case MCBinaryExpr::EQ:   OS << "=="; break;
80     case MCBinaryExpr::GT:   OS <<  '>'; break;
81     case MCBinaryExpr::GTE:  OS << ">="; break;
82     case MCBinaryExpr::LAnd: OS << "&&"; break;
83     case MCBinaryExpr::LOr:  OS << "||"; break;
84     case MCBinaryExpr::LT:   OS <<  '<'; break;
85     case MCBinaryExpr::LTE:  OS << "<="; break;
86     case MCBinaryExpr::Mod:  OS <<  '%'; break;
87     case MCBinaryExpr::Mul:  OS <<  '*'; break;
88     case MCBinaryExpr::NE:   OS << "!="; break;
89     case MCBinaryExpr::Or:   OS <<  '|'; break;
90     case MCBinaryExpr::Shl:  OS << "<<"; break;
91     case MCBinaryExpr::Shr:  OS << ">>"; break;
92     case MCBinaryExpr::Sub:  OS <<  '-'; break;
93     case MCBinaryExpr::Xor:  OS <<  '^'; break;
94     }
95     
96     // Only print parens around the LHS if it is non-trivial.
97     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
98       BE.getRHS()->print(OS, MAI);
99     } else {
100       OS << '(';
101       BE.getRHS()->print(OS, MAI);
102       OS << ')';
103     }
104     return;
105   }
106   }
107
108   assert(0 && "Invalid expression kind!");
109 }
110
111 void MCExpr::dump() const {
112   print(dbgs(), 0);
113   dbgs() << '\n';
114 }
115
116 /* *** */
117
118 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
119                                          const MCExpr *RHS, MCContext &Ctx) {
120   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
121 }
122
123 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
124                                        MCContext &Ctx) {
125   return new (Ctx) MCUnaryExpr(Opc, Expr);
126 }
127
128 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
129   return new (Ctx) MCConstantExpr(Value);
130 }
131
132 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
133                                                MCContext &Ctx) {
134   return new (Ctx) MCSymbolRefExpr(Sym);
135 }
136
137 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
138   return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
139 }
140
141
142 /* *** */
143
144 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
145   MCValue Value;
146   
147   if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
148     return false;
149
150   Res = Value.getConstant();
151   return true;
152 }
153
154 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, 
155                                 const MCSymbol *RHS_B, int64_t RHS_Cst,
156                                 MCValue &Res) {
157   // We can't add or subtract two symbols.
158   if ((LHS.getSymA() && RHS_A) ||
159       (LHS.getSymB() && RHS_B))
160     return false;
161
162   const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
163   const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
164   if (B) {
165     // If we have a negated symbol, then we must have also have a non-negated
166     // symbol in order to encode the expression. We can do this check later to
167     // permit expressions which eventually fold to a representable form -- such
168     // as (a + (0 - b)) -- if necessary.
169     if (!A)
170       return false;
171   }
172   Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
173   return true;
174 }
175
176 bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
177   switch (getKind()) {
178   case Constant:
179     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
180     return true;
181
182   case SymbolRef: {
183     const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
184
185     // Evaluate recursively if this is a variable.
186     if (Sym.isVariable())
187       return Sym.getValue()->EvaluateAsRelocatable(Res);
188
189     Res = MCValue::get(&Sym, 0, 0);
190     return true;
191   }
192
193   case Unary: {
194     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
195     MCValue Value;
196
197     if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
198       return false;
199
200     switch (AUE->getOpcode()) {
201     case MCUnaryExpr::LNot:
202       if (!Value.isAbsolute())
203         return false;
204       Res = MCValue::get(!Value.getConstant());
205       break;
206     case MCUnaryExpr::Minus:
207       /// -(a - b + const) ==> (b - a - const)
208       if (Value.getSymA() && !Value.getSymB())
209         return false;
210       Res = MCValue::get(Value.getSymB(), Value.getSymA(), 
211                          -Value.getConstant()); 
212       break;
213     case MCUnaryExpr::Not:
214       if (!Value.isAbsolute())
215         return false;
216       Res = MCValue::get(~Value.getConstant()); 
217       break;
218     case MCUnaryExpr::Plus:
219       Res = Value;
220       break;
221     }
222
223     return true;
224   }
225
226   case Binary: {
227     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
228     MCValue LHSValue, RHSValue;
229     
230     if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
231         !ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
232       return false;
233
234     // We only support a few operations on non-constant expressions, handle
235     // those first.
236     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
237       switch (ABE->getOpcode()) {
238       default:
239         return false;
240       case MCBinaryExpr::Sub:
241         // Negate RHS and add.
242         return EvaluateSymbolicAdd(LHSValue,
243                                    RHSValue.getSymB(), RHSValue.getSymA(),
244                                    -RHSValue.getConstant(),
245                                    Res);
246
247       case MCBinaryExpr::Add:
248         return EvaluateSymbolicAdd(LHSValue,
249                                    RHSValue.getSymA(), RHSValue.getSymB(),
250                                    RHSValue.getConstant(),
251                                    Res);
252       }
253     }
254
255     // FIXME: We need target hooks for the evaluation. It may be limited in
256     // width, and gas defines the result of comparisons differently from Apple
257     // as (the result is sign extended).
258     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
259     int64_t Result = 0;
260     switch (ABE->getOpcode()) {
261     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
262     case MCBinaryExpr::And:  Result = LHS & RHS; break;
263     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
264     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
265     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
266     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
267     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
268     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
269     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
270     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
271     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
272     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
273     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
274     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
275     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
276     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
277     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
278     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
279     }
280
281     Res = MCValue::get(Result);
282     return true;
283   }
284   }
285
286   assert(0 && "Invalid assembly expression kind!");
287   return false;
288 }