Implement TLSLDM.
[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_ARM_HI16: return ":upper16:";
184   case VK_ARM_LO16: return ":lower16:";
185   case VK_ARM_PLT: return "(PLT)";
186   case VK_TLVP: return "TLVP";
187   }
188 }
189
190 MCSymbolRefExpr::VariantKind
191 MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
192   return StringSwitch<VariantKind>(Name)
193     .Case("GOT", VK_GOT)
194     .Case("GOTOFF", VK_GOTOFF)
195     .Case("GOTPCREL", VK_GOTPCREL)
196     .Case("GOTTPOFF", VK_GOTTPOFF)
197     .Case("INDNTPOFF", VK_INDNTPOFF)
198     .Case("NTPOFF", VK_NTPOFF)
199     .Case("GOTNTPOFF", VK_GOTNTPOFF)
200     .Case("PLT", VK_PLT)
201     .Case("TLSGD", VK_TLSGD)
202     .Case("TLSLDM", VK_TLSLDM)
203     .Case("TPOFF", VK_TPOFF)
204     .Case("TLVP", VK_TLVP)
205     .Default(VK_Invalid);
206 }
207
208 /* *** */
209
210 void MCTargetExpr::Anchor() {}
211
212 /* *** */
213
214 bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAsmLayout *Layout) const {
215   MCValue Value;
216
217   // Fast path constants.
218   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
219     Res = CE->getValue();
220     return true;
221   }
222
223   if (!EvaluateAsRelocatable(Value, Layout) || !Value.isAbsolute()) {
224     // EvaluateAsAbsolute is defined to return the "current value" of
225     // the expression if we are given a Layout object, even in cases
226     // when the value is not fixed.
227     if (Layout) {
228       Res = Value.getConstant();
229       if (Value.getSymA()) {
230         Res += Layout->getSymbolAddress(
231           &Layout->getAssembler().getSymbolData(Value.getSymA()->getSymbol()));
232       }
233       if (Value.getSymB()) {
234         Res -= Layout->getSymbolAddress(
235           &Layout->getAssembler().getSymbolData(Value.getSymB()->getSymbol()));
236       }
237     }
238     return false;
239   }
240
241   Res = Value.getConstant();
242   return true;
243 }
244
245 static bool EvaluateSymbolicAdd(const MCAsmLayout *Layout, bool InSet,
246                                 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
247                                 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
248                                 MCValue &Res) {
249   // We can't add or subtract two symbols.
250   if ((LHS.getSymA() && RHS_A) ||
251       (LHS.getSymB() && RHS_B))
252     return false;
253
254   const MCSymbolRefExpr *A = LHS.getSymA() ? LHS.getSymA() : RHS_A;
255   const MCSymbolRefExpr *B = LHS.getSymB() ? LHS.getSymB() : RHS_B;
256   if (B) {
257     // If we have a negated symbol, then we must have also have a non-negated
258     // symbol in order to encode the expression. We can do this check later to
259     // permit expressions which eventually fold to a representable form -- such
260     // as (a + (0 - b)) -- if necessary.
261     if (!A)
262       return false;
263   }
264
265   // Absolutize symbol differences between defined symbols when we have a
266   // layout object and the target requests it.
267
268   if (Layout && A && B) {
269     const MCSymbol &SA = A->getSymbol();
270     const MCSymbol &SB = B->getSymbol();
271     const MCObjectFormat &F =
272       Layout->getAssembler().getBackend().getObjectFormat();
273     if (SA.isDefined() && SB.isDefined() && F.isAbsolute(InSet, SA, SB)) {
274       const MCAssembler &Asm = Layout->getAssembler();
275       MCSymbolData &AD = Asm.getSymbolData(A->getSymbol());
276       MCSymbolData &BD = Asm.getSymbolData(B->getSymbol());
277       Res = MCValue::get(+ Layout->getSymbolAddress(&AD)
278                          - Layout->getSymbolAddress(&BD)
279                          + LHS.getConstant()
280                          + RHS_Cst);
281       return true;
282     }
283   }
284
285
286   Res = MCValue::get(A, B, LHS.getConstant() + RHS_Cst);
287   return true;
288 }
289
290 bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
291                                    const MCAsmLayout *Layout) const {
292   return EvaluateAsRelocatableImpl(Res, Layout, false);
293 }
294
295 bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
296                                        const MCAsmLayout *Layout,
297                                        bool InSet) const {
298   ++stats::MCExprEvaluate;
299
300   switch (getKind()) {
301   case Target:
302     return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
303
304   case Constant:
305     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
306     return true;
307
308   case SymbolRef: {
309     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
310     const MCSymbol &Sym = SRE->getSymbol();
311
312     // Evaluate recursively if this is a variable.
313     if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None)
314       return Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Layout,
315                                                                true);
316
317     Res = MCValue::get(SRE, 0, 0);
318     return true;
319   }
320
321   case Unary: {
322     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
323     MCValue Value;
324
325     if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Layout, InSet))
326       return false;
327
328     switch (AUE->getOpcode()) {
329     case MCUnaryExpr::LNot:
330       if (!Value.isAbsolute())
331         return false;
332       Res = MCValue::get(!Value.getConstant());
333       break;
334     case MCUnaryExpr::Minus:
335       /// -(a - b + const) ==> (b - a - const)
336       if (Value.getSymA() && !Value.getSymB())
337         return false;
338       Res = MCValue::get(Value.getSymB(), Value.getSymA(),
339                          -Value.getConstant());
340       break;
341     case MCUnaryExpr::Not:
342       if (!Value.isAbsolute())
343         return false;
344       Res = MCValue::get(~Value.getConstant());
345       break;
346     case MCUnaryExpr::Plus:
347       Res = Value;
348       break;
349     }
350
351     return true;
352   }
353
354   case Binary: {
355     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
356     MCValue LHSValue, RHSValue;
357
358     if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Layout, InSet) ||
359         !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Layout, InSet))
360       return false;
361
362     // We only support a few operations on non-constant expressions, handle
363     // those first.
364     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
365       switch (ABE->getOpcode()) {
366       default:
367         return false;
368       case MCBinaryExpr::Sub:
369         // Negate RHS and add.
370         return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
371                                    RHSValue.getSymB(), RHSValue.getSymA(),
372                                    -RHSValue.getConstant(),
373                                    Res);
374
375       case MCBinaryExpr::Add:
376         return EvaluateSymbolicAdd(Layout, InSet, LHSValue,
377                                    RHSValue.getSymA(), RHSValue.getSymB(),
378                                    RHSValue.getConstant(),
379                                    Res);
380       }
381     }
382
383     // FIXME: We need target hooks for the evaluation. It may be limited in
384     // width, and gas defines the result of comparisons and right shifts
385     // differently from Apple as.
386     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
387     int64_t Result = 0;
388     switch (ABE->getOpcode()) {
389     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
390     case MCBinaryExpr::And:  Result = LHS & RHS; break;
391     case MCBinaryExpr::Div:  Result = LHS / RHS; break;
392     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
393     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
394     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
395     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
396     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
397     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
398     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
399     case MCBinaryExpr::Mod:  Result = LHS % RHS; break;
400     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
401     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
402     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
403     case MCBinaryExpr::Shl:  Result = LHS << RHS; break;
404     case MCBinaryExpr::Shr:  Result = LHS >> RHS; break;
405     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
406     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
407     }
408
409     Res = MCValue::get(Result);
410     return true;
411   }
412   }
413
414   assert(0 && "Invalid assembly expression kind!");
415   return false;
416 }