use both the Mangler and the CBE's mangler for identifiers,
[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 {
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 << '(' << Sym << ')';
31     else
32       OS << Sym;
33     return;
34   }
35
36   case MCExpr::Unary: {
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;
44     }
45     OS << *UE.getSubExpr();
46     return;
47   }
48
49   case MCExpr::Binary: {
50     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
51     
52     // Only print parens around the LHS if it is non-trivial.
53     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
54       OS << *BE.getLHS();
55     } else {
56       OS << '(' << *BE.getLHS() << ')';
57     }
58     
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();
66           return;
67         }
68       }
69         
70       OS <<  '+';
71       break;
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;
89     }
90     
91     // Only print parens around the LHS if it is non-trivial.
92     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
93       OS << *BE.getRHS();
94     } else {
95       OS << '(' << *BE.getRHS() << ')';
96     }
97     return;
98   }
99   }
100
101   assert(0 && "Invalid expression kind!");
102 }
103
104 void MCExpr::dump() const {
105   print(dbgs());
106   dbgs() << '\n';
107 }
108
109 /* *** */
110
111 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
112                                          const MCExpr *RHS, MCContext &Ctx) {
113   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
114 }
115
116 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
117                                        MCContext &Ctx) {
118   return new (Ctx) MCUnaryExpr(Opc, Expr);
119 }
120
121 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
122   return new (Ctx) MCConstantExpr(Value);
123 }
124
125 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
126                                                MCContext &Ctx) {
127   return new (Ctx) MCSymbolRefExpr(Sym);
128 }
129
130 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, MCContext &Ctx) {
131   return Create(Ctx.GetOrCreateSymbol(Name), Ctx);
132 }
133
134
135 /* *** */
136
137 bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
138   MCValue Value;
139   
140   if (!EvaluateAsRelocatable(Value) || !Value.isAbsolute())
141     return false;
142
143   Res = Value.getConstant();
144   return true;
145 }
146
147 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, 
148                                 const MCSymbol *RHS_B, int64_t RHS_Cst,
149                                 MCValue &Res) {
150   // We can't add or subtract two symbols.
151   if ((LHS.getSymA() && RHS_A) ||
152       (LHS.getSymB() && RHS_B))
153     return false;
154
155   const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
156   const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
157   if (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.
162     if (!A)
163       return false;
164   }
165   Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
166   return true;
167 }
168
169 bool MCExpr::EvaluateAsRelocatable(MCValue &Res) const {
170   switch (getKind()) {
171   case Constant:
172     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
173     return true;
174
175   case SymbolRef: {
176     const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
177
178     // Evaluate recursively if this is a variable.
179     if (Sym.isVariable())
180       return Sym.getValue()->EvaluateAsRelocatable(Res);
181
182     Res = MCValue::get(&Sym, 0, 0);
183     return true;
184   }
185
186   case Unary: {
187     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
188     MCValue Value;
189
190     if (!AUE->getSubExpr()->EvaluateAsRelocatable(Value))
191       return false;
192
193     switch (AUE->getOpcode()) {
194     case MCUnaryExpr::LNot:
195       if (!Value.isAbsolute())
196         return false;
197       Res = MCValue::get(!Value.getConstant());
198       break;
199     case MCUnaryExpr::Minus:
200       /// -(a - b + const) ==> (b - a - const)
201       if (Value.getSymA() && !Value.getSymB())
202         return false;
203       Res = MCValue::get(Value.getSymB(), Value.getSymA(), 
204                          -Value.getConstant()); 
205       break;
206     case MCUnaryExpr::Not:
207       if (!Value.isAbsolute())
208         return false;
209       Res = MCValue::get(~Value.getConstant()); 
210       break;
211     case MCUnaryExpr::Plus:
212       Res = Value;
213       break;
214     }
215
216     return true;
217   }
218
219   case Binary: {
220     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
221     MCValue LHSValue, RHSValue;
222     
223     if (!ABE->getLHS()->EvaluateAsRelocatable(LHSValue) ||
224         !ABE->getRHS()->EvaluateAsRelocatable(RHSValue))
225       return false;
226
227     // We only support a few operations on non-constant expressions, handle
228     // those first.
229     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
230       switch (ABE->getOpcode()) {
231       default:
232         return false;
233       case MCBinaryExpr::Sub:
234         // Negate RHS and add.
235         return EvaluateSymbolicAdd(LHSValue,
236                                    RHSValue.getSymB(), RHSValue.getSymA(),
237                                    -RHSValue.getConstant(),
238                                    Res);
239
240       case MCBinaryExpr::Add:
241         return EvaluateSymbolicAdd(LHSValue,
242                                    RHSValue.getSymA(), RHSValue.getSymB(),
243                                    RHSValue.getConstant(),
244                                    Res);
245       }
246     }
247
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();
252     int64_t Result = 0;
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;
272     }
273
274     Res = MCValue::get(Result);
275     return true;
276   }
277   }
278
279   assert(0 && "Invalid assembly expression kind!");
280   return false;
281 }