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