blob: fcf1aabb5a8629c5c58ba25a83dae24ca6c200d9 [file] [log] [blame]
Daniel Dunbar28c251b2009-08-31 08:06:59 +00001//===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
Daniel Dunbarfc6877a2009-06-29 20:40:36 +00002//
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
Daniel Dunbarff547842010-03-23 23:47:14 +000010#define DEBUG_TYPE "mcexpr"
Daniel Dunbar28c251b2009-08-31 08:06:59 +000011#include "llvm/MC/MCExpr.h"
Daniel Dunbarff547842010-03-23 23:47:14 +000012#include "llvm/ADT/Statistic.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000013#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar979ba5b2010-03-11 05:53:37 +000014#include "llvm/MC/MCAsmLayout.h"
15#include "llvm/MC/MCAssembler.h"
Daniel Dunbarfc6877a2009-06-29 20:40:36 +000016#include "llvm/MC/MCContext.h"
Daniel Dunbar15d17072009-06-30 01:49:52 +000017#include "llvm/MC/MCSymbol.h"
Daniel Dunbarfc6877a2009-06-29 20:40:36 +000018#include "llvm/MC/MCValue.h"
David Greene5d2bcb12010-01-05 01:28:07 +000019#include "llvm/Support/Debug.h"
Daniel Dunbar87392fd2009-08-31 08:07:33 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbar979ba5b2010-03-11 05:53:37 +000021#include "llvm/Target/TargetAsmBackend.h"
Daniel Dunbarfc6877a2009-06-29 20:40:36 +000022using namespace llvm;
23
Daniel Dunbarff547842010-03-23 23:47:14 +000024namespace {
25namespace stats {
26STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
27}
28}
29
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000030void MCExpr::print(raw_ostream &OS) const {
Daniel Dunbar87392fd2009-08-31 08:07:33 +000031 switch (getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +000032 case MCExpr::Target:
33 return cast<MCTargetExpr>(this)->PrintImpl(OS);
Daniel Dunbar87392fd2009-08-31 08:07:33 +000034 case MCExpr::Constant:
35 OS << cast<MCConstantExpr>(*this).getValue();
36 return;
37
Chris Lattnerd50c2b92009-09-08 23:20:50 +000038 case MCExpr::SymbolRef: {
Daniel Dunbar4e815f82010-03-15 23:51:06 +000039 const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
40 const MCSymbol &Sym = SRE.getSymbol();
Chris Lattner1e61e692010-11-15 02:46:57 +000041 // Parenthesize names that start with $ so that they don't look like
42 // absolute names.
43 bool UseParens = Sym.getName()[0] == '$';
Daniel Dunbar9a1d2002010-03-18 00:59:10 +000044
Roman Divackya1000742011-06-09 20:25:38 +000045 if (SRE.getKind() == MCSymbolRefExpr::VK_PPC_DARWIN_HA16 ||
46 SRE.getKind() == MCSymbolRefExpr::VK_PPC_DARWIN_LO16) {
Chris Lattner1e61e692010-11-15 02:46:57 +000047 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
48 UseParens = true;
49 }
50
51 if (UseParens)
Chris Lattner10b318b2010-01-17 21:43:43 +000052 OS << '(' << Sym << ')';
53 else
54 OS << Sym;
Daniel Dunbar4e815f82010-03-15 23:51:06 +000055
Jim Grosbach2c4d5122010-11-10 03:26:07 +000056 if (SRE.getKind() == MCSymbolRefExpr::VK_ARM_PLT ||
57 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TLSGD ||
58 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOT ||
59 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTOFF ||
60 SRE.getKind() == MCSymbolRefExpr::VK_ARM_TPOFF ||
61 SRE.getKind() == MCSymbolRefExpr::VK_ARM_GOTTPOFF)
Jim Grosbach637d89f2010-09-22 23:27:36 +000062 OS << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
63 else if (SRE.getKind() != MCSymbolRefExpr::VK_None &&
Roman Divackya1000742011-06-09 20:25:38 +000064 SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_HA16 &&
65 SRE.getKind() != MCSymbolRefExpr::VK_PPC_DARWIN_LO16)
Daniel Dunbar4e815f82010-03-15 23:51:06 +000066 OS << '@' << MCSymbolRefExpr::getVariantKindName(SRE.getKind());
67
Daniel Dunbar87392fd2009-08-31 08:07:33 +000068 return;
Chris Lattnerd50c2b92009-09-08 23:20:50 +000069 }
Daniel Dunbar87392fd2009-08-31 08:07:33 +000070
71 case MCExpr::Unary: {
72 const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
73 switch (UE.getOpcode()) {
74 default: assert(0 && "Invalid opcode!");
75 case MCUnaryExpr::LNot: OS << '!'; break;
76 case MCUnaryExpr::Minus: OS << '-'; break;
77 case MCUnaryExpr::Not: OS << '~'; break;
78 case MCUnaryExpr::Plus: OS << '+'; break;
79 }
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000080 OS << *UE.getSubExpr();
Daniel Dunbar87392fd2009-08-31 08:07:33 +000081 return;
82 }
83
84 case MCExpr::Binary: {
85 const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
Daniel Dunbar9a1d2002010-03-18 00:59:10 +000086
Chris Lattnerbe73e8c2009-09-08 06:34:07 +000087 // Only print parens around the LHS if it is non-trivial.
88 if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000089 OS << *BE.getLHS();
Chris Lattnerbe73e8c2009-09-08 06:34:07 +000090 } else {
Chris Lattner8cb9a3b2010-01-18 00:37:40 +000091 OS << '(' << *BE.getLHS() << ')';
Chris Lattnerbe73e8c2009-09-08 06:34:07 +000092 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +000093
Daniel Dunbar87392fd2009-08-31 08:07:33 +000094 switch (BE.getOpcode()) {
95 default: assert(0 && "Invalid opcode!");
Chris Lattnerd19ceb92009-09-08 06:37:35 +000096 case MCBinaryExpr::Add:
97 // Print "X-42" instead of "X+-42".
98 if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
99 if (RHSC->getValue() < 0) {
100 OS << RHSC->getValue();
101 return;
102 }
103 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000104
Chris Lattnerd19ceb92009-09-08 06:37:35 +0000105 OS << '+';
106 break;
Daniel Dunbar87392fd2009-08-31 08:07:33 +0000107 case MCBinaryExpr::And: OS << '&'; break;
108 case MCBinaryExpr::Div: OS << '/'; break;
109 case MCBinaryExpr::EQ: OS << "=="; break;
110 case MCBinaryExpr::GT: OS << '>'; break;
111 case MCBinaryExpr::GTE: OS << ">="; break;
112 case MCBinaryExpr::LAnd: OS << "&&"; break;
113 case MCBinaryExpr::LOr: OS << "||"; break;
114 case MCBinaryExpr::LT: OS << '<'; break;
115 case MCBinaryExpr::LTE: OS << "<="; break;
116 case MCBinaryExpr::Mod: OS << '%'; break;
117 case MCBinaryExpr::Mul: OS << '*'; break;
118 case MCBinaryExpr::NE: OS << "!="; break;
119 case MCBinaryExpr::Or: OS << '|'; break;
120 case MCBinaryExpr::Shl: OS << "<<"; break;
121 case MCBinaryExpr::Shr: OS << ">>"; break;
122 case MCBinaryExpr::Sub: OS << '-'; break;
123 case MCBinaryExpr::Xor: OS << '^'; break;
124 }
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000125
Chris Lattnerbe73e8c2009-09-08 06:34:07 +0000126 // Only print parens around the LHS if it is non-trivial.
127 if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000128 OS << *BE.getRHS();
Chris Lattnerbe73e8c2009-09-08 06:34:07 +0000129 } else {
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000130 OS << '(' << *BE.getRHS() << ')';
Chris Lattnerbe73e8c2009-09-08 06:34:07 +0000131 }
Daniel Dunbar87392fd2009-08-31 08:07:33 +0000132 return;
133 }
134 }
135
136 assert(0 && "Invalid expression kind!");
137}
138
139void MCExpr::dump() const {
Chris Lattner8cb9a3b2010-01-18 00:37:40 +0000140 print(dbgs());
David Greene5d2bcb12010-01-05 01:28:07 +0000141 dbgs() << '\n';
Daniel Dunbar87392fd2009-08-31 08:07:33 +0000142}
143
144/* *** */
145
Chris Lattner8b4ada22009-09-08 06:27:48 +0000146const MCBinaryExpr *MCBinaryExpr::Create(Opcode Opc, const MCExpr *LHS,
147 const MCExpr *RHS, MCContext &Ctx) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000148 return new (Ctx) MCBinaryExpr(Opc, LHS, RHS);
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000149}
150
Chris Lattner8b4ada22009-09-08 06:27:48 +0000151const MCUnaryExpr *MCUnaryExpr::Create(Opcode Opc, const MCExpr *Expr,
152 MCContext &Ctx) {
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000153 return new (Ctx) MCUnaryExpr(Opc, Expr);
154}
155
156const MCConstantExpr *MCConstantExpr::Create(int64_t Value, MCContext &Ctx) {
157 return new (Ctx) MCConstantExpr(Value);
158}
159
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000160/* *** */
161
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000162const MCSymbolRefExpr *MCSymbolRefExpr::Create(const MCSymbol *Sym,
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000163 VariantKind Kind,
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000164 MCContext &Ctx) {
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000165 return new (Ctx) MCSymbolRefExpr(Sym, Kind);
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000166}
167
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000168const MCSymbolRefExpr *MCSymbolRefExpr::Create(StringRef Name, VariantKind Kind,
169 MCContext &Ctx) {
170 return Create(Ctx.GetOrCreateSymbol(Name), Kind, Ctx);
Chris Lattner4f3e7aa2009-09-16 01:26:31 +0000171}
172
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000173StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
174 switch (Kind) {
175 default:
176 case VK_Invalid: return "<<invalid>>";
177 case VK_None: return "<<none>>";
178
179 case VK_GOT: return "GOT";
180 case VK_GOTOFF: return "GOTOFF";
181 case VK_GOTPCREL: return "GOTPCREL";
182 case VK_GOTTPOFF: return "GOTTPOFF";
183 case VK_INDNTPOFF: return "INDNTPOFF";
184 case VK_NTPOFF: return "NTPOFF";
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000185 case VK_GOTNTPOFF: return "GOTNTPOFF";
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000186 case VK_PLT: return "PLT";
187 case VK_TLSGD: return "TLSGD";
Rafael Espindolab4d17212010-10-28 15:02:40 +0000188 case VK_TLSLD: return "TLSLD";
Rafael Espindolaa264f722010-10-28 14:37:09 +0000189 case VK_TLSLDM: return "TLSLDM";
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000190 case VK_TPOFF: return "TPOFF";
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000191 case VK_DTPOFF: return "DTPOFF";
Chris Lattner6135a962010-11-14 22:22:59 +0000192 case VK_TLVP: return "TLVP";
Jim Grosbach637d89f2010-09-22 23:27:36 +0000193 case VK_ARM_PLT: return "(PLT)";
Jim Grosbach2c4d5122010-11-10 03:26:07 +0000194 case VK_ARM_GOT: return "(GOT)";
195 case VK_ARM_GOTOFF: return "(GOTOFF)";
196 case VK_ARM_TPOFF: return "(tpoff)";
197 case VK_ARM_GOTTPOFF: return "(gottpoff)";
Jim Grosbach4d088632010-11-17 19:30:11 +0000198 case VK_ARM_TLSGD: return "(tlsgd)";
Chris Lattner6135a962010-11-14 22:22:59 +0000199 case VK_PPC_TOC: return "toc";
Roman Divackya1000742011-06-09 20:25:38 +0000200 case VK_PPC_DARWIN_HA16: return "ha16";
201 case VK_PPC_DARWIN_LO16: return "lo16";
202 case VK_PPC_GAS_HA16: return "ha";
203 case VK_PPC_GAS_LO16: return "l";
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000204 }
205}
206
207MCSymbolRefExpr::VariantKind
208MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
209 return StringSwitch<VariantKind>(Name)
210 .Case("GOT", VK_GOT)
Rafael Espindola54104db2011-01-23 16:11:25 +0000211 .Case("got", VK_GOT)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000212 .Case("GOTOFF", VK_GOTOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000213 .Case("gotoff", VK_GOTOFF)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000214 .Case("GOTPCREL", VK_GOTPCREL)
Rafael Espindola54104db2011-01-23 16:11:25 +0000215 .Case("gotpcrel", VK_GOTPCREL)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000216 .Case("GOTTPOFF", VK_GOTTPOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000217 .Case("gottpoff", VK_GOTTPOFF)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000218 .Case("INDNTPOFF", VK_INDNTPOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000219 .Case("indntpoff", VK_INDNTPOFF)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000220 .Case("NTPOFF", VK_NTPOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000221 .Case("ntpoff", VK_NTPOFF)
Rafael Espindolaa0a2f872010-10-28 14:22:44 +0000222 .Case("GOTNTPOFF", VK_GOTNTPOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000223 .Case("gotntpoff", VK_GOTNTPOFF)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000224 .Case("PLT", VK_PLT)
Rafael Espindola54104db2011-01-23 16:11:25 +0000225 .Case("plt", VK_PLT)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000226 .Case("TLSGD", VK_TLSGD)
Rafael Espindola54104db2011-01-23 16:11:25 +0000227 .Case("tlsgd", VK_TLSGD)
Rafael Espindolab4d17212010-10-28 15:02:40 +0000228 .Case("TLSLD", VK_TLSLD)
Rafael Espindola54104db2011-01-23 16:11:25 +0000229 .Case("tlsld", VK_TLSLD)
Rafael Espindolaa264f722010-10-28 14:37:09 +0000230 .Case("TLSLDM", VK_TLSLDM)
Rafael Espindola54104db2011-01-23 16:11:25 +0000231 .Case("tlsldm", VK_TLSLDM)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000232 .Case("TPOFF", VK_TPOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000233 .Case("tpoff", VK_TPOFF)
Rafael Espindola0cf15d62010-10-28 14:48:59 +0000234 .Case("DTPOFF", VK_DTPOFF)
Rafael Espindola54104db2011-01-23 16:11:25 +0000235 .Case("dtpoff", VK_DTPOFF)
Eric Christopher96ac5152010-05-26 00:02:12 +0000236 .Case("TLVP", VK_TLVP)
Rafael Espindola54104db2011-01-23 16:11:25 +0000237 .Case("tlvp", VK_TLVP)
Daniel Dunbar4e815f82010-03-15 23:51:06 +0000238 .Default(VK_Invalid);
239}
240
241/* *** */
242
Chris Lattnerdf9c4382010-02-08 22:07:36 +0000243void MCTargetExpr::Anchor() {}
Chris Lattner4f3e7aa2009-09-16 01:26:31 +0000244
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000245/* *** */
246
Rafael Espindolad0764822010-12-18 03:57:21 +0000247bool MCExpr::EvaluateAsAbsolute(int64_t &Res) const {
248 return EvaluateAsAbsolute(Res, 0, 0, 0);
249}
250
251bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
252 const MCAsmLayout &Layout) const {
253 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, 0);
254}
255
256bool MCExpr::EvaluateAsAbsolute(int64_t &Res,
257 const MCAsmLayout &Layout,
258 const SectionAddrMap &Addrs) const {
259 return EvaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs);
260}
261
262bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
263 return EvaluateAsAbsolute(Res, &Asm, 0, 0);
264}
265
266bool MCExpr::EvaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
267 const MCAsmLayout *Layout,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000268 const SectionAddrMap *Addrs) const {
Daniel Dunbar15d17072009-06-30 01:49:52 +0000269 MCValue Value;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000270
Daniel Dunbaref6e96f2010-03-23 23:47:07 +0000271 // Fast path constants.
272 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
273 Res = CE->getValue();
274 return true;
275 }
276
Rafael Espindola3f037ef2010-12-18 04:19:20 +0000277 // FIXME: The use if InSet = Addrs is a hack. Setting InSet causes us
278 // absolutize differences across sections and that is what the MachO writer
279 // uses Addrs for.
Rafael Espindola10b6d332010-12-22 22:16:24 +0000280 bool IsRelocatable =
281 EvaluateAsRelocatableImpl(Value, Asm, Layout, Addrs, /*InSet*/ Addrs);
Daniel Dunbar15d17072009-06-30 01:49:52 +0000282
Rafael Espindola10b6d332010-12-22 22:16:24 +0000283 // Record the current value.
Daniel Dunbar15d17072009-06-30 01:49:52 +0000284 Res = Value.getConstant();
Rafael Espindola10b6d332010-12-22 22:16:24 +0000285
286 return IsRelocatable && Value.isAbsolute();
Daniel Dunbar15d17072009-06-30 01:49:52 +0000287}
288
Daniel Dunbar17304b32010-12-17 05:50:33 +0000289/// \brief Helper method for \see EvaluateSymbolAdd().
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000290static void AttemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
291 const MCAsmLayout *Layout,
292 const SectionAddrMap *Addrs,
293 bool InSet,
Daniel Dunbar17304b32010-12-17 05:50:33 +0000294 const MCSymbolRefExpr *&A,
295 const MCSymbolRefExpr *&B,
296 int64_t &Addend) {
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000297 if (!A || !B)
298 return;
Daniel Dunbar17304b32010-12-17 05:50:33 +0000299
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000300 const MCSymbol &SA = A->getSymbol();
301 const MCSymbol &SB = B->getSymbol();
302
303 if (SA.isUndefined() || SB.isUndefined())
304 return;
305
306 if (!Asm->getWriter().IsSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
307 return;
308
309 MCSymbolData &AD = Asm->getSymbolData(SA);
310 MCSymbolData &BD = Asm->getSymbolData(SB);
311
312 if (AD.getFragment() == BD.getFragment()) {
313 Addend += (AD.getOffset() - BD.getOffset());
Daniel Dunbar17304b32010-12-17 05:50:33 +0000314
Owen Andersonce3a1ba2011-03-21 23:13:43 +0000315 // Pointers to Thumb symbols need to have their low-bit set to allow
316 // for interworking.
317 if (Asm->isThumbFunc(&SA))
318 Addend |= 1;
319
Daniel Dunbar17304b32010-12-17 05:50:33 +0000320 // Clear the symbol expr pointers to indicate we have folded these
321 // operands.
322 A = B = 0;
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000323 return;
Daniel Dunbar17304b32010-12-17 05:50:33 +0000324 }
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000325
326 if (!Layout)
327 return;
328
329 const MCSectionData &SecA = *AD.getFragment()->getParent();
330 const MCSectionData &SecB = *BD.getFragment()->getParent();
331
332 if ((&SecA != &SecB) && !Addrs)
333 return;
334
335 // Eagerly evaluate.
336 Addend += (Layout->getSymbolOffset(&Asm->getSymbolData(A->getSymbol())) -
337 Layout->getSymbolOffset(&Asm->getSymbolData(B->getSymbol())));
338 if (Addrs && (&SecA != &SecB))
339 Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
340
341 // Clear the symbol expr pointers to indicate we have folded these
342 // operands.
343 A = B = 0;
Daniel Dunbar17304b32010-12-17 05:50:33 +0000344}
345
Daniel Dunbardd18e282010-12-16 18:36:25 +0000346/// \brief Evaluate the result of an add between (conceptually) two MCValues.
347///
348/// This routine conceptually attempts to construct an MCValue:
349/// Result = (Result_A - Result_B + Result_Cst)
350/// from two MCValue's LHS and RHS where
351/// Result = LHS + RHS
352/// and
353/// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
354///
355/// This routine attempts to aggresively fold the operands such that the result
356/// is representable in an MCValue, but may not always succeed.
357///
358/// \returns True on success, false if the result is not representable in an
359/// MCValue.
Rafael Espindolad0764822010-12-18 03:57:21 +0000360
361/// NOTE: It is really important to have both the Asm and Layout arguments.
362/// They might look redundant, but this function can be used before layout
363/// is done (see the object streamer for example) and having the Asm argument
Rafael Espindolaf705a7e2010-12-18 04:01:45 +0000364/// lets us avoid relaxations early.
Rafael Espindolad0764822010-12-18 03:57:21 +0000365static bool EvaluateSymbolicAdd(const MCAssembler *Asm,
366 const MCAsmLayout *Layout,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000367 const SectionAddrMap *Addrs,
Rafael Espindola32a006e2010-12-03 00:55:40 +0000368 bool InSet,
Rafael Espindolaf230df92010-10-16 18:23:53 +0000369 const MCValue &LHS,const MCSymbolRefExpr *RHS_A,
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000370 const MCSymbolRefExpr *RHS_B, int64_t RHS_Cst,
Daniel Dunbar35976042009-06-30 02:08:27 +0000371 MCValue &Res) {
Daniel Dunbarf2ed62d2010-12-17 01:07:20 +0000372 // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
373 // about dealing with modifiers. This will ultimately bite us, one day.
374 const MCSymbolRefExpr *LHS_A = LHS.getSymA();
375 const MCSymbolRefExpr *LHS_B = LHS.getSymB();
376 int64_t LHS_Cst = LHS.getConstant();
377
378 // Fold the result constant immediately.
379 int64_t Result_Cst = LHS_Cst + RHS_Cst;
380
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000381 assert((!Layout || Asm) &&
382 "Must have an assembler object if layout is given!");
383
Daniel Dunbar17304b32010-12-17 05:50:33 +0000384 // If we have a layout, we can fold resolved differences.
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000385 if (Asm) {
Daniel Dunbar17304b32010-12-17 05:50:33 +0000386 // First, fold out any differences which are fully resolved. By
387 // reassociating terms in
388 // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
389 // we have the four possible differences:
390 // (LHS_A - LHS_B),
391 // (LHS_A - RHS_B),
392 // (RHS_A - LHS_B),
393 // (RHS_A - RHS_B).
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000394 // Since we are attempting to be as aggressive as possible about folding, we
Daniel Dunbar17304b32010-12-17 05:50:33 +0000395 // attempt to evaluate each possible alternative.
Rafael Espindolac5b0e442010-12-19 04:18:56 +0000396 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
397 Result_Cst);
398 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
399 Result_Cst);
400 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
401 Result_Cst);
402 AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
403 Result_Cst);
Daniel Dunbar17304b32010-12-17 05:50:33 +0000404 }
405
Daniel Dunbarf2ed62d2010-12-17 01:07:20 +0000406 // We can't represent the addition or subtraction of two symbols.
407 if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
Daniel Dunbar35976042009-06-30 02:08:27 +0000408 return false;
409
Daniel Dunbar17304b32010-12-17 05:50:33 +0000410 // At this point, we have at most one additive symbol and one subtractive
411 // symbol -- find them.
Daniel Dunbarf2ed62d2010-12-17 01:07:20 +0000412 const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
413 const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
Daniel Dunbar17304b32010-12-17 05:50:33 +0000414
415 // If we have a negated symbol, then we must have also have a non-negated
416 // symbol in order to encode the expression.
417 if (B && !A)
418 return false;
Rafael Espindolaf230df92010-10-16 18:23:53 +0000419
Daniel Dunbarf2ed62d2010-12-17 01:07:20 +0000420 Res = MCValue::get(A, B, Result_Cst);
Daniel Dunbar35976042009-06-30 02:08:27 +0000421 return true;
422}
423
Rafael Espindolad0764822010-12-18 03:57:21 +0000424bool MCExpr::EvaluateAsRelocatable(MCValue &Res,
Rafael Espindola33a38a12010-12-22 16:11:57 +0000425 const MCAsmLayout &Layout) const {
426 return EvaluateAsRelocatableImpl(Res, &Layout.getAssembler(), &Layout,
427 0, false);
Rafael Espindolad0764822010-12-18 03:57:21 +0000428}
429
Rafael Espindolaf230df92010-10-16 18:23:53 +0000430bool MCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
Rafael Espindolad0764822010-12-18 03:57:21 +0000431 const MCAssembler *Asm,
Rafael Espindolaf230df92010-10-16 18:23:53 +0000432 const MCAsmLayout *Layout,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000433 const SectionAddrMap *Addrs,
Rafael Espindolaf230df92010-10-16 18:23:53 +0000434 bool InSet) const {
Daniel Dunbarff547842010-03-23 23:47:14 +0000435 ++stats::MCExprEvaluate;
436
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000437 switch (getKind()) {
Chris Lattner5d917a82010-02-08 19:41:07 +0000438 case Target:
Daniel Dunbarf82f4492010-03-11 02:28:59 +0000439 return cast<MCTargetExpr>(this)->EvaluateAsRelocatableImpl(Res, Layout);
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000440
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000441 case Constant:
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000442 Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000443 return true;
444
445 case SymbolRef: {
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000446 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
447 const MCSymbol &Sym = SRE->getSymbol();
Daniel Dunbar7c3600d2009-10-16 01:33:57 +0000448
449 // Evaluate recursively if this is a variable.
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000450 if (Sym.isVariable() && SRE->getKind() == MCSymbolRefExpr::VK_None) {
Rafael Espindolad0764822010-12-18 03:57:21 +0000451 bool Ret = Sym.getVariableValue()->EvaluateAsRelocatableImpl(Res, Asm,
452 Layout,
453 Addrs,
454 true);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000455 // If we failed to simplify this to a constant, let the target
456 // handle it.
457 if (Ret && !Res.getSymA() && !Res.getSymB())
458 return true;
459 }
Daniel Dunbar7c3600d2009-10-16 01:33:57 +0000460
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000461 Res = MCValue::get(SRE, 0, 0);
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000462 return true;
463 }
464
465 case Unary: {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000466 const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
Daniel Dunbar15d17072009-06-30 01:49:52 +0000467 MCValue Value;
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000468
Rafael Espindolad0764822010-12-18 03:57:21 +0000469 if (!AUE->getSubExpr()->EvaluateAsRelocatableImpl(Value, Asm, Layout,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000470 Addrs, InSet))
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000471 return false;
472
473 switch (AUE->getOpcode()) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000474 case MCUnaryExpr::LNot:
Daniel Dunbar80f62d02009-07-01 06:48:00 +0000475 if (!Value.isAbsolute())
Daniel Dunbar15d17072009-06-30 01:49:52 +0000476 return false;
477 Res = MCValue::get(!Value.getConstant());
478 break;
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000479 case MCUnaryExpr::Minus:
Daniel Dunbar15d17072009-06-30 01:49:52 +0000480 /// -(a - b + const) ==> (b - a - const)
Daniel Dunbarb27a41b2009-08-11 17:47:52 +0000481 if (Value.getSymA() && !Value.getSymB())
Daniel Dunbar15d17072009-06-30 01:49:52 +0000482 return false;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000483 Res = MCValue::get(Value.getSymB(), Value.getSymA(),
484 -Value.getConstant());
Daniel Dunbar15d17072009-06-30 01:49:52 +0000485 break;
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000486 case MCUnaryExpr::Not:
Daniel Dunbar80f62d02009-07-01 06:48:00 +0000487 if (!Value.isAbsolute())
Daniel Dunbar15d17072009-06-30 01:49:52 +0000488 return false;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000489 Res = MCValue::get(~Value.getConstant());
Daniel Dunbar15d17072009-06-30 01:49:52 +0000490 break;
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000491 case MCUnaryExpr::Plus:
Daniel Dunbar15d17072009-06-30 01:49:52 +0000492 Res = Value;
493 break;
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000494 }
495
496 return true;
497 }
498
499 case Binary: {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000500 const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
Daniel Dunbar15d17072009-06-30 01:49:52 +0000501 MCValue LHSValue, RHSValue;
Daniel Dunbar9a1d2002010-03-18 00:59:10 +0000502
Rafael Espindolad0764822010-12-18 03:57:21 +0000503 if (!ABE->getLHS()->EvaluateAsRelocatableImpl(LHSValue, Asm, Layout,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000504 Addrs, InSet) ||
Rafael Espindolad0764822010-12-18 03:57:21 +0000505 !ABE->getRHS()->EvaluateAsRelocatableImpl(RHSValue, Asm, Layout,
Rafael Espindola85f2ecc2010-12-07 00:27:36 +0000506 Addrs, InSet))
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000507 return false;
508
Daniel Dunbar15d17072009-06-30 01:49:52 +0000509 // We only support a few operations on non-constant expressions, handle
510 // those first.
Daniel Dunbar80f62d02009-07-01 06:48:00 +0000511 if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
Daniel Dunbar15d17072009-06-30 01:49:52 +0000512 switch (ABE->getOpcode()) {
513 default:
514 return false;
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000515 case MCBinaryExpr::Sub:
Daniel Dunbar35976042009-06-30 02:08:27 +0000516 // Negate RHS and add.
Rafael Espindolad0764822010-12-18 03:57:21 +0000517 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
Daniel Dunbar35976042009-06-30 02:08:27 +0000518 RHSValue.getSymB(), RHSValue.getSymA(),
519 -RHSValue.getConstant(),
520 Res);
521
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000522 case MCBinaryExpr::Add:
Rafael Espindolad0764822010-12-18 03:57:21 +0000523 return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
Daniel Dunbar35976042009-06-30 02:08:27 +0000524 RHSValue.getSymA(), RHSValue.getSymB(),
525 RHSValue.getConstant(),
526 Res);
Daniel Dunbar15d17072009-06-30 01:49:52 +0000527 }
528 }
529
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000530 // FIXME: We need target hooks for the evaluation. It may be limited in
Dan Gohman036c1302010-02-08 23:58:47 +0000531 // width, and gas defines the result of comparisons and right shifts
532 // differently from Apple as.
Daniel Dunbarb79742c2009-06-30 16:02:47 +0000533 int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
534 int64_t Result = 0;
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000535 switch (ABE->getOpcode()) {
Daniel Dunbar28c251b2009-08-31 08:06:59 +0000536 case MCBinaryExpr::Add: Result = LHS + RHS; break;
537 case MCBinaryExpr::And: Result = LHS & RHS; break;
538 case MCBinaryExpr::Div: Result = LHS / RHS; break;
539 case MCBinaryExpr::EQ: Result = LHS == RHS; break;
540 case MCBinaryExpr::GT: Result = LHS > RHS; break;
541 case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
542 case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
543 case MCBinaryExpr::LOr: Result = LHS || RHS; break;
544 case MCBinaryExpr::LT: Result = LHS < RHS; break;
545 case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
546 case MCBinaryExpr::Mod: Result = LHS % RHS; break;
547 case MCBinaryExpr::Mul: Result = LHS * RHS; break;
548 case MCBinaryExpr::NE: Result = LHS != RHS; break;
549 case MCBinaryExpr::Or: Result = LHS | RHS; break;
550 case MCBinaryExpr::Shl: Result = LHS << RHS; break;
551 case MCBinaryExpr::Shr: Result = LHS >> RHS; break;
552 case MCBinaryExpr::Sub: Result = LHS - RHS; break;
553 case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000554 }
555
Daniel Dunbar15d17072009-06-30 01:49:52 +0000556 Res = MCValue::get(Result);
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000557 return true;
558 }
559 }
Daniel Dunbarfc6877a2009-06-29 20:40:36 +0000560
Daniel Dunbar9643ac52009-08-31 08:07:22 +0000561 assert(0 && "Invalid assembly expression kind!");
562 return false;
563}
Daniel Dunbar0eab5c42011-04-29 18:00:03 +0000564
565const MCSection *MCExpr::FindAssociatedSection() const {
566 switch (getKind()) {
567 case Target:
568 // We never look through target specific expressions.
569 return cast<MCTargetExpr>(this)->FindAssociatedSection();
570
571 case Constant:
572 return MCSymbol::AbsolutePseudoSection;
573
574 case SymbolRef: {
575 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
576 const MCSymbol &Sym = SRE->getSymbol();
577
578 if (Sym.isDefined())
579 return &Sym.getSection();
580
581 return 0;
582 }
583
584 case Unary:
585 return cast<MCUnaryExpr>(this)->getSubExpr()->FindAssociatedSection();
586
587 case Binary: {
588 const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
589 const MCSection *LHS_S = BE->getLHS()->FindAssociatedSection();
590 const MCSection *RHS_S = BE->getRHS()->FindAssociatedSection();
591
592 // If either section is absolute, return the other.
593 if (LHS_S == MCSymbol::AbsolutePseudoSection)
594 return RHS_S;
595 if (RHS_S == MCSymbol::AbsolutePseudoSection)
596 return LHS_S;
597
598 // Otherwise, return the first non-null section.
599 return LHS_S ? LHS_S : RHS_S;
600 }
601 }
602
603 assert(0 && "Invalid assembly expression kind!");
604 return 0;
605}