bc4241560e8ddb25977367b2aab80fee2fdf36b5
[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/raw_ostream.h"
15 using namespace llvm;
16
17 void MCExpr::print(raw_ostream &OS) const {
18   switch (getKind()) {
19   case MCExpr::Constant:
20     OS << cast<MCConstantExpr>(*this).getValue();
21     return;
22
23   case MCExpr::SymbolRef:
24     cast<MCSymbolRefExpr>(*this).getSymbol().print(OS);
25     return;
26
27   case MCExpr::Unary: {
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;
35     }
36     UE.getSubExpr()->print(OS);
37     return;
38   }
39
40   case MCExpr::Binary: {
41     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
42     OS << '(';
43     BE.getLHS()->print(OS);
44     OS << ' ';
45     switch (BE.getOpcode()) {
46     default: assert(0 && "Invalid opcode!");
47     case MCBinaryExpr::Add:  OS <<  '+'; break;
48     case MCBinaryExpr::And:  OS <<  '&'; break;
49     case MCBinaryExpr::Div:  OS <<  '/'; break;
50     case MCBinaryExpr::EQ:   OS << "=="; break;
51     case MCBinaryExpr::GT:   OS <<  '>'; break;
52     case MCBinaryExpr::GTE:  OS << ">="; break;
53     case MCBinaryExpr::LAnd: OS << "&&"; break;
54     case MCBinaryExpr::LOr:  OS << "||"; break;
55     case MCBinaryExpr::LT:   OS <<  '<'; break;
56     case MCBinaryExpr::LTE:  OS << "<="; break;
57     case MCBinaryExpr::Mod:  OS <<  '%'; break;
58     case MCBinaryExpr::Mul:  OS <<  '*'; break;
59     case MCBinaryExpr::NE:   OS << "!="; break;
60     case MCBinaryExpr::Or:   OS <<  '|'; break;
61     case MCBinaryExpr::Shl:  OS << "<<"; break;
62     case MCBinaryExpr::Shr:  OS << ">>"; break;
63     case MCBinaryExpr::Sub:  OS <<  '-'; break;
64     case MCBinaryExpr::Xor:  OS <<  '^'; break;
65     }
66     OS << ' ';
67     BE.getRHS()->print(OS);
68     OS << ')';
69     return;
70   }
71   }
72
73   assert(0 && "Invalid expression kind!");
74 }
75
76 void MCExpr::dump() const {
77   print(errs());
78   errs() << '\n';
79 }
80
81 /* *** */
82
83 const MCBinaryExpr * MCBinaryExpr::Create(Opcode Opc,
84                                           const MCExpr *LHS,
85                                           const MCExpr *RHS,
86                                           MCContext &Ctx) {
87   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
88 }
89
90 const MCUnaryExpr * MCUnaryExpr::Create(Opcode Opc,
91                                         const MCExpr *Expr,
92                                         MCContext &Ctx) {
93   return new (Ctx) MCUnaryExpr(Opc, Expr);
94 }
95
96 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
97   return new (Ctx) MCConstantExpr(Value);
98 }
99
100 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
101                                                MCContext &Ctx) {
102   return new (Ctx) MCSymbolRefExpr(Sym);
103 }
104
105 /* *** */
106
107 bool MCExpr::EvaluateAsAbsolute(MCContext &Ctx, int64_t &Res) const {
108   MCValue Value;
109   
110   if (!EvaluateAsRelocatable(Ctx, Value) || !Value.isAbsolute())
111     return false;
112
113   Res = Value.getConstant();
114   return true;
115 }
116
117 static bool EvaluateSymbolicAdd(const MCValue &LHS, const MCSymbol *RHS_A, 
118                                 const MCSymbol *RHS_B, int64_t RHS_Cst,
119                                 MCValue &Res) {
120   // We can't add or subtract two symbols.
121   if ((LHS.getSymA() && RHS_A) ||
122       (LHS.getSymB() && RHS_B))
123     return false;
124
125   const MCSymbol *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
126   const MCSymbol *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
127   if (B) {
128     // If we have a negated symbol, then we must have also have a non-negated
129     // symbol in order to encode the expression. We can do this check later to
130     // permit expressions which eventually fold to a representable form -- such
131     // as (a + (0 - b)) -- if necessary.
132     if (!A)
133       return false;
134   }
135   Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
136   return true;
137 }
138
139 bool MCExpr::EvaluateAsRelocatable(MCContext &Ctx, MCValue &Res) const {
140   switch (getKind()) {
141   case Constant:
142     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
143     return true;
144
145   case SymbolRef: {
146     const MCSymbol &Sym = cast<MCSymbolRefExpr>(this)->getSymbol();
147     if (const MCValue *Value = Ctx.GetSymbolValue(&Sym))
148       Res = *Value;
149     else
150       Res = MCValue::get(&Sym, 0, 0);
151     return true;
152   }
153
154   case Unary: {
155     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
156     MCValue Value;
157
158     if (!AUE->getSubExpr()->EvaluateAsRelocatable(Ctx, Value))
159       return false;
160
161     switch (AUE->getOpcode()) {
162     case MCUnaryExpr::LNot:
163       if (!Value.isAbsolute())
164         return false;
165       Res = MCValue::get(!Value.getConstant());
166       break;
167     case MCUnaryExpr::Minus:
168       /// -(a - b + const) ==> (b - a - const)
169       if (Value.getSymA() && !Value.getSymB())
170         return false;
171       Res = MCValue::get(Value.getSymB(), Value.getSymA(), 
172                          -Value.getConstant()); 
173       break;
174     case MCUnaryExpr::Not:
175       if (!Value.isAbsolute())
176         return false;
177       Res = MCValue::get(~Value.getConstant()); 
178       break;
179     case MCUnaryExpr::Plus:
180       Res = Value;
181       break;
182     }
183
184     return true;
185   }
186
187   case Binary: {
188     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
189     MCValue LHSValue, RHSValue;
190     
191     if (!ABE->getLHS()->EvaluateAsRelocatable(Ctx, LHSValue) ||
192         !ABE->getRHS()->EvaluateAsRelocatable(Ctx, RHSValue))
193       return false;
194
195     // We only support a few operations on non-constant expressions, handle
196     // those first.
197     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
198       switch (ABE->getOpcode()) {
199       default:
200         return false;
201       case MCBinaryExpr::Sub:
202         // Negate RHS and add.
203         return EvaluateSymbolicAdd(LHSValue,
204                                    RHSValue.getSymB(), RHSValue.getSymA(),
205                                    -RHSValue.getConstant(),
206                                    Res);
207
208       case MCBinaryExpr::Add:
209         return EvaluateSymbolicAdd(LHSValue,
210                                    RHSValue.getSymA(), RHSValue.getSymB(),
211                                    RHSValue.getConstant(),
212                                    Res);
213       }
214     }
215
216     // FIXME: We need target hooks for the evaluation. It may be limited in
217     // width, and gas defines the result of comparisons differently from Apple
218     // as (the result is sign extended).
219     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
220     int64_t Result = 0;
221     switch (ABE->getOpcode()) {
222     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
223     case MCBinaryExpr::And:  Result = LHS & RHS; break;
224     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
225     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
226     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
227     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
228     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
229     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
230     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
231     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
232     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
233     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
234     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
235     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
236     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
237     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
238     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
239     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
240     }
241
242     Res = MCValue::get(Result);
243     return true;
244   }
245   }
246
247   assert(0 && "Invalid assembly expression kind!");
248   return false;
249 }