Implement DTPOFF.
[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 #define DEBUG_TYPE "mcexpr"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/MC/MCAsmLayout.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectFormat.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Target/TargetAsmBackend.h"
23 using namespace llvm;
24
25 namespace {
26 namespace stats {
27 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
28 }
29 }
30
31 void MCExpr::print(raw_ostream &OS) const {
32   switch (getKind()) {
33   case MCExpr::Target:
34     return cast<MCTargetExpr>(this)->PrintImpl(OS);
35   case MCExpr::Constant:
36     OS << cast<MCConstantExpr>(*this).getValue();
37     return;
38
39   case MCExpr::SymbolRef: {
40     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
41     const MCSymbol &Sym = SRE.getSymbol();
42
43     if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_HI16 ||
44         SRE.getKind() == MCSymbolRefExpr::VK_ARM_LO16)
45       OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
46
47     // Parenthesize names that start with $ so that they don't look like
48     // absolute names.
49     if (Sym.getName()[0] == '$')
50       OS << '(' << Sym << ')';
51     else
52       OS << Sym;
53
54     if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT)
55       OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
56     else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
57              SRE.getKind() != MCSymbolRefExpr::VK_ARM_HI16 &&
58              SRE.getKind() != MCSymbolRefExpr::VK_ARM_LO16)
59       OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
60
61     return;
62   }
63
64   case MCExpr::Unary: {
65     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
66     switch (UE.getOpcode()) {
67     default: assert(0 && "Invalid opcode!");
68     case MCUnaryExpr::LNot:  OS << '!'; break;
69     case MCUnaryExpr::Minus: OS << '-'; break;
70     case MCUnaryExpr::Not:   OS << '~'; break;
71     case MCUnaryExpr::Plus:  OS << '+'; break;
72     }
73     OS << *UE.getSubExpr();
74     return;
75   }
76
77   case MCExpr::Binary: {
78     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
79
80     // Only print parens around the LHS if it is non-trivial.
81     if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
82       OS << *BE.getLHS();
83     } else {
84       OS << '(' << *BE.getLHS() << ')';
85     }
86
87     switch (BE.getOpcode()) {
88     default: assert(0 && "Invalid opcode!");
89     case MCBinaryExpr::Add:
90       // Print "X-42" instead of "X+-42".
91       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
92         if (RHSC->getValue() < 0) {
93           OS << RHSC->getValue();
94           return;
95         }
96       }
97
98       OS <<  '+';
99       break;
100     case MCBinaryExpr::And:  OS <<  '&'; break;
101     case MCBinaryExpr::Div:  OS <<  '/'; break;
102     case MCBinaryExpr::EQ:   OS << "=="; break;
103     case MCBinaryExpr::GT:   OS <<  '>'; break;
104     case MCBinaryExpr::GTE:  OS << ">="; break;
105     case MCBinaryExpr::LAnd: OS << "&&"; break;
106     case MCBinaryExpr::LOr:  OS << "||"; break;
107     case MCBinaryExpr::LT:   OS <<  '<'; break;
108     case MCBinaryExpr::LTE:  OS << "<="; break;
109     case MCBinaryExpr::Mod:  OS <<  '%'; break;
110     case MCBinaryExpr::Mul:  OS <<  '*'; break;
111     case MCBinaryExpr::NE:   OS << "!="; break;
112     case MCBinaryExpr::Or:   OS <<  '|'; break;
113     case MCBinaryExpr::Shl:  OS << "<<"; break;
114     case MCBinaryExpr::Shr:  OS << ">>"; break;
115     case MCBinaryExpr::Sub:  OS <<  '-'; break;
116     case MCBinaryExpr::Xor:  OS <<  '^'; break;
117     }
118
119     // Only print parens around the LHS if it is non-trivial.
120     if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
121       OS << *BE.getRHS();
122     } else {
123       OS << '(' << *BE.getRHS() << ')';
124     }
125     return;
126   }
127   }
128
129   assert(0 && "Invalid expression kind!");
130 }
131
132 void MCExpr::dump() const {
133   print(dbgs());
134   dbgs() << '\n';
135 }
136
137 /* *** */
138
139 const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
140                                          const MCExpr *RHS, MCContext &Ctx) {
141   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
142 }
143
144 const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
145                                        MCContext &Ctx) {
146   return new (Ctx) MCUnaryExpr(Opc, Expr);
147 }
148
149 const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
150   return new (Ctx) MCConstantExpr(Value);
151 }
152
153 /* *** */
154
155 const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
156                                                VariantKind Kind,
157                                                MCContext &Ctx) {
158   return new (Ctx) MCSymbolRefExpr(Sym, Kind);
159 }
160
161 const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
162                                                MCContext &Ctx) {
163   return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
164 }
165
166 StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
167   switch (Kind) {
168   default:
169   case VK_Invalid: return "<<invalid>>";
170   case VK_None: return "<<none>>";
171
172   case VK_GOT: return "GOT";
173   case VK_GOTOFF: return "GOTOFF";
174   case VK_GOTPCREL: return "GOTPCREL";
175   case VK_GOTTPOFF: return "GOTTPOFF";
176   case VK_INDNTPOFF: return "INDNTPOFF";
177   case VK_NTPOFF: return "NTPOFF";
178   case VK_GOTNTPOFF: return "GOTNTPOFF";
179   case VK_PLT: return "PLT";
180   case VK_TLSGD: return "TLSGD";
181   case VK_TLSLDM: return "TLSLDM";
182   case VK_TPOFF: return "TPOFF";
183   case VK_DTPOFF: return "DTPOFF";
184   case VK_ARM_HI16: return ":upper16:";
185   case VK_ARM_LO16: return ":lower16:";
186   case VK_ARM_PLT: return "(PLT)";
187   case VK_TLVP: return "TLVP";
188   }
189 }
190
191 MCSymbolRefExpr::VariantKind
192 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
193   return StringSwitch<VariantKind>(Name)
194     .Case("GOT", VK_GOT)
195     .Case("GOTOFF", VK_GOTOFF)
196     .Case("GOTPCREL", VK_GOTPCREL)
197     .Case("GOTTPOFF", VK_GOTTPOFF)
198     .Case("INDNTPOFF", VK_INDNTPOFF)
199     .Case("NTPOFF", VK_NTPOFF)
200     .Case("GOTNTPOFF", VK_GOTNTPOFF)
201     .Case("PLT", VK_PLT)
202     .Case("TLSGD", VK_TLSGD)
203     .Case("TLSLDM", VK_TLSLDM)
204     .Case("TPOFF", VK_TPOFF)
205     .Case("DTPOFF", VK_DTPOFF)
206     .Case("TLVP", VK_TLVP)
207     .Default(VK_Invalid);
208 }
209
210 /* *** */
211
212 void MCTargetExpr::Anchor() {}
213
214 /* *** */
215
216 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout) const {
217   MCValue Value;
218
219   // Fast path constants.
220   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
221     Res = CE->getValue();
222     return true;
223   }
224
225   if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute()) {
226     // EvaluateAsAbsolute is defined to return the "current value" of
227     // the expression if we are given a Layout object, even in cases
228     // when the value is not fixed.
229     if (Layout) {
230       Res = Value.getConstant();
231       if (Value.getSymA()) {
232         Res += Layout->getSymbolAddress(
233           &Layout->getAssembler().getSymbolData(Value.getSymA()->getSymbol()));
234       }
235       if (Value.getSymB()) {
236         Res -= Layout->getSymbolAddress(
237           &Layout->getAssembler().getSymbolData(Value.getSymB()->getSymbol()));
238       }
239     }
240     return false;
241   }
242
243   Res = Value.getConstant();
244   return true;
245 }
246
247 static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, bool InSet,
248                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
249                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
250                                 MCValue &Res) {
251   // We can't add or subtract two symbols.
252   if ((LHS.getSymA() && RHS_A) ||
253       (LHS.getSymB() && RHS_B))
254     return false;
255
256   const MCSymbolRefExpr *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
257   const MCSymbolRefExpr *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
258   if (B) {
259     // If we have a negated symbol, then we must have also have a non-negated
260     // symbol in order to encode the expression. We can do this check later to
261     // permit expressions which eventually fold to a representable form -- such
262     // as (a + (0 - b)) -- if necessary.
263     if (!A)
264       return false;
265   }
266
267   // Absolutize symbol differences between defined symbols when we have a
268   // layout object and the target requests it.
269
270   if (Layout && A && B) {
271     const MCSymbol &SA = A->getSymbol();
272     const MCSymbol &SB = B->getSymbol();
273     const MCObjectFormat &F =
274       Layout->getAssembler().getBackend().getObjectFormat();
275     if (SA.isDefined() && SB.isDefined() && F.isAbsolute(InSet, SA, SB)) {
276       const MCAssembler &Asm = Layout->getAssembler();
277       MCSymbolData &AD = Asm.getSymbolData(A->getSymbol());
278       MCSymbolData &BD = Asm.getSymbolData(B->getSymbol());
279       Res = MCValue::get(+ Layout->getSymbolAddress(&AD)
280                          - Layout->getSymbolAddress(&BD)
281                          + LHS.getConstant()
282                          + RHS_Cst);
283       return true;
284     }
285   }
286
287
288   Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
289   return true;
290 }
291
292 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
293                                    const MCAsmLayout *Layout) const {
294   return EvaluateAsRelocatableImpl(Res, Layout, false);
295 }
296
297 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
298                                        const MCAsmLayout *Layout,
299                                        bool InSet) const {
300   ++stats::MCExprEvaluate;
301
302   switch (getKind()) {
303   case Target:
304     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
305
306   case Constant:
307     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
308     return true;
309
310   case SymbolRef: {
311     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
312     const MCSymbol &Sym = SRE->getSymbol();
313
314     // Evaluate recursively if this is a variable.
315     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None)
316       return Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Layout,
317                                                                true);
318
319     Res = MCValue::get(SRE, 0, 0);
320     return true;
321   }
322
323   case Unary: {
324     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
325     MCValue Value;
326
327     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Layout, InSet))
328       return false;
329
330     switch (AUE->getOpcode()) {
331     case MCUnaryExpr::LNot:
332       if (!Value.isAbsolute())
333         return false;
334       Res = MCValue::get(!Value.getConstant());
335       break;
336     case MCUnaryExpr::Minus:
337       /// -(a - b + const) ==> (b - a - const)
338       if (Value.getSymA() && !Value.getSymB())
339         return false;
340       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
341                          -Value.getConstant());
342       break;
343     case MCUnaryExpr::Not:
344       if (!Value.isAbsolute())
345         return false;
346       Res = MCValue::get(~Value.getConstant());
347       break;
348     case MCUnaryExpr::Plus:
349       Res = Value;
350       break;
351     }
352
353     return true;
354   }
355
356   case Binary: {
357     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
358     MCValue LHSValue, RHSValue;
359
360     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Layout, InSet) ||
361         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Layout, InSet))
362       return false;
363
364     // We only support a few operations on non-constant expressions, handle
365     // those first.
366     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
367       switch (ABE->getOpcode()) {
368       default:
369         return false;
370       case MCBinaryExpr::Sub:
371         // Negate RHS and add.
372         return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
373                                    RHSValue.getSymB(), RHSValue.getSymA(),
374                                    -RHSValue.getConstant(),
375                                    Res);
376
377       case MCBinaryExpr::Add:
378         return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
379                                    RHSValue.getSymA(), RHSValue.getSymB(),
380                                    RHSValue.getConstant(),
381                                    Res);
382       }
383     }
384
385     // FIXME: We need target hooks for the evaluation. It may be limited in
386     // width, and gas defines the result of comparisons and right shifts
387     // differently from Apple as.
388     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
389     int64_t Result = 0;
390     switch (ABE->getOpcode()) {
391     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
392     case MCBinaryExpr::And:  Result = LHS & RHS; break;
393     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
394     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
395     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
396     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
397     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
398     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
399     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
400     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
401     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
402     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
403     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
404     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
405     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
406     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
407     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
408     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
409     }
410
411     Res = MCValue::get(Result);
412     return true;
413   }
414   }
415
416   assert(0 && "Invalid assembly expression kind!");
417   return false;
418 }