xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCExpr.cpp (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
1 //===- MCExpr.cpp - Assembly Level Expression Implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCExpr.h"
10 #include "llvm/ADT/ScopeExit.h"
11 #include "llvm/ADT/Statistic.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCValue.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include <cassert>
27 #include <cstdint>
28 
29 using namespace llvm;
30 
31 #define DEBUG_TYPE "mcexpr"
32 
33 namespace {
34 namespace stats {
35 
36 STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
37 
38 } // end namespace stats
39 } // end anonymous namespace
40 
getPrecedence(MCBinaryExpr::Opcode Op)41 static int getPrecedence(MCBinaryExpr::Opcode Op) {
42   switch (Op) {
43   case MCBinaryExpr::Add:
44   case MCBinaryExpr::Sub:
45     return 1;
46   default:
47     return 0;
48   }
49 }
50 
51 // VariantKind printing and formatting utilize MAI. operator<< (dump and some
52 // target code) specifies MAI as nullptr and should be avoided when MAI is
53 // needed.
print(raw_ostream & OS,const MCAsmInfo * MAI,int SurroundingPrec) const54 void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI,
55                    int SurroundingPrec) const {
56   constexpr int MaxPrec = 9;
57   switch (getKind()) {
58   case MCExpr::Target:
59     return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
60   case MCExpr::Constant: {
61     auto Value = cast<MCConstantExpr>(*this).getValue();
62     auto PrintInHex = cast<MCConstantExpr>(*this).useHexFormat();
63     auto SizeInBytes = cast<MCConstantExpr>(*this).getSizeInBytes();
64     if (Value < 0 && MAI && !MAI->supportsSignedData())
65       PrintInHex = true;
66     if (PrintInHex)
67       switch (SizeInBytes) {
68       default:
69         OS << "0x" << Twine::utohexstr(Value);
70         break;
71       case 1:
72         OS << format("0x%02" PRIx64, Value);
73         break;
74       case 2:
75         OS << format("0x%04" PRIx64, Value);
76         break;
77       case 4:
78         OS << format("0x%08" PRIx64, Value);
79         break;
80       case 8:
81         OS << format("0x%016" PRIx64, Value);
82         break;
83       }
84     else
85       OS << Value;
86     return;
87   }
88   case MCExpr::SymbolRef: {
89     const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
90     const MCSymbol &Sym = SRE.getSymbol();
91     Sym.print(OS, MAI);
92 
93     const MCSymbolRefExpr::VariantKind Kind = SRE.getKind();
94     if (Kind) {
95       if (!MAI) // should only be used by dump()
96         OS << "@<variant " << Kind << '>';
97       else if (MAI->useParensForSpecifier()) // ARM
98         OS << '(' << MAI->getSpecifierName(Kind) << ')';
99       else
100         OS << '@' << MAI->getSpecifierName(Kind);
101     }
102 
103     return;
104   }
105 
106   case MCExpr::Unary: {
107     const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
108     switch (UE.getOpcode()) {
109     case MCUnaryExpr::LNot:  OS << '!'; break;
110     case MCUnaryExpr::Minus: OS << '-'; break;
111     case MCUnaryExpr::Not:   OS << '~'; break;
112     case MCUnaryExpr::Plus:  OS << '+'; break;
113     }
114     UE.getSubExpr()->print(OS, MAI, MaxPrec);
115     return;
116   }
117 
118   case MCExpr::Binary: {
119     const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
120     // We want to avoid redundant parentheses for relocatable expressions like
121     // a-b+c.
122     //
123     // Print '(' if the current operator has lower precedence than the
124     // surrounding operator, or if the surrounding operator's precedence is
125     // unknown (set to HighPrecedence).
126     int Prec = getPrecedence(BE.getOpcode());
127     bool Paren = Prec < SurroundingPrec;
128     if (Paren)
129       OS << '(';
130     // Many operators' precedence is different from C. Set the precedence to
131     // HighPrecedence for unknown operators.
132     int SubPrec = Prec ? Prec : MaxPrec;
133     BE.getLHS()->print(OS, MAI, SubPrec);
134 
135     switch (BE.getOpcode()) {
136     case MCBinaryExpr::Add:
137       // Print "X-42" instead of "X+-42".
138       if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
139         if (RHSC->getValue() < 0) {
140           OS << RHSC->getValue();
141           if (Paren)
142             OS << ')';
143           return;
144         }
145       }
146 
147       OS <<  '+';
148       break;
149     case MCBinaryExpr::AShr: OS << ">>"; break;
150     case MCBinaryExpr::And:  OS <<  '&'; break;
151     case MCBinaryExpr::Div:  OS <<  '/'; break;
152     case MCBinaryExpr::EQ:   OS << "=="; break;
153     case MCBinaryExpr::GT:   OS <<  '>'; break;
154     case MCBinaryExpr::GTE:  OS << ">="; break;
155     case MCBinaryExpr::LAnd: OS << "&&"; break;
156     case MCBinaryExpr::LOr:  OS << "||"; break;
157     case MCBinaryExpr::LShr: OS << ">>"; break;
158     case MCBinaryExpr::LT:   OS <<  '<'; break;
159     case MCBinaryExpr::LTE:  OS << "<="; break;
160     case MCBinaryExpr::Mod:  OS <<  '%'; break;
161     case MCBinaryExpr::Mul:  OS <<  '*'; break;
162     case MCBinaryExpr::NE:   OS << "!="; break;
163     case MCBinaryExpr::Or:   OS <<  '|'; break;
164     case MCBinaryExpr::OrNot: OS << '!'; break;
165     case MCBinaryExpr::Shl:  OS << "<<"; break;
166     case MCBinaryExpr::Sub:  OS <<  '-'; break;
167     case MCBinaryExpr::Xor:  OS <<  '^'; break;
168     }
169 
170     BE.getRHS()->print(OS, MAI, SubPrec + 1);
171     if (Paren)
172       OS << ')';
173     return;
174   }
175 
176   case MCExpr::Specifier: {
177     auto &SE = cast<MCSpecifierExpr>(*this);
178     if (MAI)
179       return MAI->printSpecifierExpr(OS, SE);
180     // Used by dump features like -show-inst. Regular MCAsmStreamer output must
181     // set MAI.
182     OS << "specifier(" << SE.getSpecifier() << ',';
183     SE.getSubExpr()->print(OS, nullptr);
184     OS << ')';
185     return;
186   }
187   }
188 
189   llvm_unreachable("Invalid expression kind!");
190 }
191 
192 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const193 LLVM_DUMP_METHOD void MCExpr::dump() const {
194   print(dbgs(), nullptr);
195   dbgs() << '\n';
196 }
197 #endif
198 
199 /* *** */
200 
create(Opcode Opc,const MCExpr * LHS,const MCExpr * RHS,MCContext & Ctx,SMLoc Loc)201 const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
202                                          const MCExpr *RHS, MCContext &Ctx,
203                                          SMLoc Loc) {
204   return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
205 }
206 
create(Opcode Opc,const MCExpr * Expr,MCContext & Ctx,SMLoc Loc)207 const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
208                                        MCContext &Ctx, SMLoc Loc) {
209   return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
210 }
211 
create(int64_t Value,MCContext & Ctx,bool PrintInHex,unsigned SizeInBytes)212 const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx,
213                                              bool PrintInHex,
214                                              unsigned SizeInBytes) {
215   return new (Ctx) MCConstantExpr(Value, PrintInHex, SizeInBytes);
216 }
217 
218 /* *** */
219 
MCSymbolRefExpr(const MCSymbol * Symbol,Spec specifier,const MCAsmInfo * MAI,SMLoc Loc)220 MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, Spec specifier,
221                                  const MCAsmInfo *MAI, SMLoc Loc)
222     : MCExpr(MCExpr::SymbolRef, Loc, specifier), Symbol(Symbol) {
223   assert(Symbol);
224 }
225 
create(const MCSymbol * Sym,uint16_t specifier,MCContext & Ctx,SMLoc Loc)226 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
227                                                uint16_t specifier,
228                                                MCContext &Ctx, SMLoc Loc) {
229   return new (Ctx) MCSymbolRefExpr(Sym, specifier, Ctx.getAsmInfo(), Loc);
230 }
231 
232 /* *** */
233 
anchor()234 void MCTargetExpr::anchor() {}
235 
236 /* *** */
237 
evaluateAsAbsolute(int64_t & Res) const238 bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
239   return evaluateAsAbsolute(Res, nullptr, false);
240 }
241 
evaluateAsAbsolute(int64_t & Res,const MCAssembler & Asm) const242 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
243   return evaluateAsAbsolute(Res, &Asm, false);
244 }
245 
evaluateAsAbsolute(int64_t & Res,const MCAssembler * Asm) const246 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
247   return evaluateAsAbsolute(Res, Asm, false);
248 }
249 
evaluateKnownAbsolute(int64_t & Res,const MCAssembler & Asm) const250 bool MCExpr::evaluateKnownAbsolute(int64_t &Res, const MCAssembler &Asm) const {
251   return evaluateAsAbsolute(Res, &Asm, true);
252 }
253 
evaluateAsAbsolute(int64_t & Res,const MCAssembler * Asm,bool InSet) const254 bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
255                                 bool InSet) const {
256   MCValue Value;
257 
258   // Fast path constants.
259   if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
260     Res = CE->getValue();
261     return true;
262   }
263 
264   bool IsRelocatable = evaluateAsRelocatableImpl(Value, Asm, InSet);
265   Res = Value.getConstant();
266   // Value with RefKind (e.g. %hi(0xdeadbeef) in MIPS) is not considered
267   // absolute (the value is unknown at parse time), even if it might be resolved
268   // by evaluateFixup.
269   return IsRelocatable && Value.isAbsolute() && Value.getSpecifier() == 0;
270 }
271 
272 /// Helper method for \see EvaluateSymbolAdd().
attemptToFoldSymbolOffsetDifference(const MCAssembler * Asm,bool InSet,const MCSymbol * & A,const MCSymbol * & B,int64_t & Addend)273 static void attemptToFoldSymbolOffsetDifference(const MCAssembler *Asm,
274                                                 bool InSet, const MCSymbol *&A,
275                                                 const MCSymbol *&B,
276                                                 int64_t &Addend) {
277   if (!A || !B)
278     return;
279 
280   const MCSymbol &SA = *A, &SB = *B;
281   if (SA.isUndefined() || SB.isUndefined())
282     return;
283   if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(SA, SB, InSet))
284     return;
285 
286   auto FinalizeFolding = [&]() {
287     // Pointers to Thumb symbols need to have their low-bit set to allow
288     // for interworking.
289     if (Asm->isThumbFunc(&SA))
290       Addend |= 1;
291 
292     // Clear the symbol expr pointers to indicate we have folded these
293     // operands.
294     A = B = nullptr;
295   };
296 
297   const MCFragment *FA = SA.getFragment();
298   const MCFragment *FB = SB.getFragment();
299   const MCSection &SecA = *FA->getParent();
300   const MCSection &SecB = *FB->getParent();
301   if (&SecA != &SecB)
302     return;
303 
304   // When layout is available, we can generally compute the difference using the
305   // getSymbolOffset path, which also avoids the possible slow fragment walk.
306   // However, linker relaxation may cause incorrect fold of A-B if A and B are
307   // separated by a linker-relaxable fragment. If the section contains
308   // linker-relaxable instruction and InSet is false (not expressions in
309   // directive like .size/.fill), disable the fast path.
310   bool Layout = Asm->hasLayout();
311   if (Layout && (InSet || !SecA.isLinkerRelaxable())) {
312     // If both symbols are in the same fragment, return the difference of their
313     // offsets. canGetFragmentOffset(FA) may be false.
314     if (FA == FB && !SA.isVariable() && !SB.isVariable()) {
315       Addend += SA.getOffset() - SB.getOffset();
316       return FinalizeFolding();
317     }
318 
319     // Eagerly evaluate when layout is finalized.
320     Addend += Asm->getSymbolOffset(SA) - Asm->getSymbolOffset(SB);
321     FinalizeFolding();
322   } else {
323     // When layout is not finalized, our ability to resolve differences between
324     // symbols is limited to specific cases where the fragments between two
325     // symbols (including the fragments the symbols are defined in) are
326     // fixed-size fragments so the difference can be calculated. For example,
327     // this is important when the Subtarget is changed and a new MCDataFragment
328     // is created in the case of foo: instr; .arch_extension ext; instr .if . -
329     // foo.
330     if (SA.isVariable() || SB.isVariable())
331       return;
332 
333     // Try to find a constant displacement from FA to FB, add the displacement
334     // between the offset in FA of SA and the offset in FB of SB.
335     bool Reverse = false;
336     if (FA == FB)
337       Reverse = SA.getOffset() < SB.getOffset();
338     else
339       Reverse = FA->getLayoutOrder() < FB->getLayoutOrder();
340 
341     uint64_t SAOffset = SA.getOffset(), SBOffset = SB.getOffset();
342     int64_t Displacement = SA.getOffset() - SB.getOffset();
343     if (Reverse) {
344       std::swap(FA, FB);
345       std::swap(SAOffset, SBOffset);
346       Displacement *= -1;
347     }
348 
349     // Track whether B is before a relaxable instruction and whether A is after
350     // a relaxable instruction. If SA and SB are separated by a linker-relaxable
351     // instruction, the difference cannot be resolved as it may be changed by
352     // the linker.
353     bool BBeforeRelax = false, AAfterRelax = false;
354     for (auto F = FB; F; F = F->getNext()) {
355       auto DF = dyn_cast<MCDataFragment>(F);
356       if (DF && DF->isLinkerRelaxable()) {
357         if (&*F != FB || SBOffset != DF->getContents().size())
358           BBeforeRelax = true;
359         if (&*F != FA || SAOffset == DF->getContents().size())
360           AAfterRelax = true;
361         if (BBeforeRelax && AAfterRelax)
362           return;
363       }
364       const auto *RF = dyn_cast<MCRelaxableFragment>(F);
365       if (RF && RF->isLinkerRelaxable())
366         return;
367       if (&*F == FA) {
368         // If FA and FB belong to the same subsection, the loop will find FA and
369         // we can resolve the difference.
370         Addend += Reverse ? -Displacement : Displacement;
371         FinalizeFolding();
372         return;
373       }
374 
375       int64_t Num;
376       unsigned Count;
377       if (DF) {
378         Displacement += DF->getContents().size();
379       } else if (auto *RF = dyn_cast<MCRelaxableFragment>(F);
380                  RF && Asm->hasFinalLayout()) {
381         // Before finishLayout, a relaxable fragment's size is indeterminate.
382         // After layout, during relocation generation, it can be treated as a
383         // data fragment.
384         Displacement += RF->getContents().size();
385       } else if (auto *AF = dyn_cast<MCAlignFragment>(F);
386                  AF && Layout && AF->hasEmitNops() &&
387                  !Asm->getBackend().shouldInsertExtraNopBytesForCodeAlign(
388                      *AF, Count)) {
389         Displacement += Asm->computeFragmentSize(*AF);
390       } else if (auto *FF = dyn_cast<MCFillFragment>(F);
391                  FF && FF->getNumValues().evaluateAsAbsolute(Num)) {
392         Displacement += Num * FF->getValueSize();
393       } else {
394         return;
395       }
396     }
397   }
398 }
399 
400 // Evaluate the sum of two relocatable expressions.
401 //
402 //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
403 //
404 // This routine attempts to aggressively fold the operands such that the result
405 // is representable in an MCValue, but may not always succeed.
406 //
407 // LHS_A and RHS_A might have relocation specifiers while LHS_B and RHS_B
408 // cannot have specifiers.
409 //
410 // \returns True on success, false if the result is not representable in an
411 // MCValue.
412 
413 // NOTE: This function can be used before layout is done (see the object
414 // streamer for example) and having the Asm argument lets us avoid relaxations
415 // early.
evaluateSymbolicAdd(const MCAssembler * Asm,bool InSet,const MCValue & LHS,const MCValue & RHS,MCValue & Res)416 bool MCExpr::evaluateSymbolicAdd(const MCAssembler *Asm, bool InSet,
417                                  const MCValue &LHS, const MCValue &RHS,
418                                  MCValue &Res) {
419   const MCSymbol *LHS_A = LHS.getAddSym();
420   const MCSymbol *LHS_B = LHS.getSubSym();
421   int64_t LHS_Cst = LHS.getConstant();
422 
423   const MCSymbol *RHS_A = RHS.getAddSym();
424   const MCSymbol *RHS_B = RHS.getSubSym();
425   int64_t RHS_Cst = RHS.getConstant();
426 
427   // Fold the result constant immediately.
428   int64_t Result_Cst = LHS_Cst + RHS_Cst;
429 
430   // If we have a layout, we can fold resolved differences.
431   if (Asm && !LHS.getSpecifier() && !RHS.getSpecifier()) {
432     // While LHS_A-LHS_B and RHS_A-RHS_B from recursive calls have already been
433     // folded, reassociating terms in
434     //   Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
435     // might bring more opportunities.
436     if (LHS_A && RHS_B) {
437       attemptToFoldSymbolOffsetDifference(Asm, InSet, LHS_A, RHS_B, Result_Cst);
438     }
439     if (RHS_A && LHS_B) {
440       attemptToFoldSymbolOffsetDifference(Asm, InSet, RHS_A, LHS_B, Result_Cst);
441     }
442   }
443 
444   // We can't represent the addition or subtraction of two symbols.
445   if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
446     return false;
447 
448   // At this point, we have at most one additive symbol and one subtractive
449   // symbol -- find them.
450   auto *A = LHS_A ? LHS_A : RHS_A;
451   auto *B = LHS_B ? LHS_B : RHS_B;
452   auto Spec = LHS.getSpecifier();
453   if (!Spec)
454     Spec = RHS.getSpecifier();
455   Res = MCValue::get(A, B, Result_Cst, Spec);
456   return true;
457 }
458 
evaluateAsRelocatable(MCValue & Res,const MCAssembler * Asm) const459 bool MCExpr::evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm) const {
460   return evaluateAsRelocatableImpl(Res, Asm, false);
461 }
evaluateAsValue(MCValue & Res,const MCAssembler & Asm) const462 bool MCExpr::evaluateAsValue(MCValue &Res, const MCAssembler &Asm) const {
463   return evaluateAsRelocatableImpl(Res, &Asm, true);
464 }
465 
evaluateAsRelocatableImpl(MCValue & Res,const MCAssembler * Asm,bool InSet) const466 bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
467                                        bool InSet) const {
468   ++stats::MCExprEvaluate;
469   switch (getKind()) {
470   case Target:
471     return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Asm);
472   case Constant:
473     Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
474     return true;
475 
476   case SymbolRef: {
477     const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
478     MCSymbol &Sym = const_cast<MCSymbol &>(SRE->getSymbol());
479     const auto Kind = SRE->getKind();
480     bool Layout = Asm && Asm->hasLayout();
481 
482     // If the symbol is equated, resolve the inner expression.
483     // However, when two IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols reference
484     // each other, we retain the equated symbol to avoid a cyclic definition
485     // error.
486     if (Sym.isResolving()) {
487       if (Asm && Asm->hasFinalLayout()) {
488         Asm->getContext().reportError(
489             Sym.getVariableValue()->getLoc(),
490             "cyclic dependency detected for symbol '" + Sym.getName() + "'");
491         Sym.setVariableValue(MCConstantExpr::create(0, Asm->getContext()));
492       }
493       return false;
494     }
495     if (Sym.isVariable() && (Kind == 0 || Layout) && !Sym.isWeakExternal()) {
496       Sym.setIsResolving(true);
497       auto _ = make_scope_exit([&] { Sym.setIsResolving(false); });
498       bool IsMachO =
499           Asm && Asm->getContext().getAsmInfo()->hasSubsectionsViaSymbols();
500       if (!Sym.getVariableValue()->evaluateAsRelocatableImpl(Res, Asm,
501                                                              InSet || IsMachO))
502         return false;
503       // When generating relocations, if Sym resolves to a symbol relative to a
504       // section, relocations are generated against Sym. Treat label differences
505       // as constants.
506       auto *A = Res.getAddSym();
507       auto *B = Res.getSubSym();
508       if (InSet || !(A && !B && A->isInSection())) {
509         if (Kind) {
510           if (Res.isAbsolute()) {
511             Res = MCValue::get(&Sym, nullptr, 0, Kind);
512             return true;
513           }
514           // If the reference has a variant kind, we can only handle expressions
515           // which evaluate exactly to a single unadorned symbol. Attach the
516           // original VariantKind to SymA of the result.
517           if (Res.getSpecifier() || !Res.getAddSym() || Res.getSubSym() ||
518               Res.getConstant())
519             return false;
520           Res.Specifier = Kind;
521         }
522         if (!IsMachO)
523           return true;
524 
525         // FIXME: This is small hack. Given
526         // a = b + 4
527         // .long a
528         // the OS X assembler will completely drop the 4. We should probably
529         // include it in the relocation or produce an error if that is not
530         // possible.
531         // Allow constant expressions.
532         if (!A && !B)
533           return true;
534         // Allows aliases with zero offset.
535         if (Res.getConstant() == 0 && (!A || !B))
536           return true;
537       }
538     }
539 
540     Res = MCValue::get(&Sym, nullptr, 0, Kind);
541     return true;
542   }
543 
544   case Unary: {
545     const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
546     MCValue Value;
547 
548     if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, InSet))
549       return false;
550     switch (AUE->getOpcode()) {
551     case MCUnaryExpr::LNot:
552       if (!Value.isAbsolute())
553         return false;
554       Res = MCValue::get(!Value.getConstant());
555       break;
556     case MCUnaryExpr::Minus:
557       /// -(a - b + const) ==> (b - a - const)
558       if (Value.getAddSym() && !Value.getSubSym())
559         return false;
560 
561       // The cast avoids undefined behavior if the constant is INT64_MIN.
562       Res = MCValue::get(Value.getSubSym(), Value.getAddSym(),
563                          -(uint64_t)Value.getConstant());
564       break;
565     case MCUnaryExpr::Not:
566       if (!Value.isAbsolute())
567         return false;
568       Res = MCValue::get(~Value.getConstant());
569       break;
570     case MCUnaryExpr::Plus:
571       Res = Value;
572       break;
573     }
574 
575     return true;
576   }
577 
578   case Binary: {
579     const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
580     MCValue LHSValue, RHSValue;
581 
582     if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, InSet) ||
583         !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, InSet)) {
584       // Check if both are Target Expressions, see if we can compare them.
585       if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS())) {
586         if (const MCTargetExpr *R = dyn_cast<MCTargetExpr>(ABE->getRHS())) {
587           switch (ABE->getOpcode()) {
588           case MCBinaryExpr::EQ:
589             Res = MCValue::get(L->isEqualTo(R) ? -1 : 0);
590             return true;
591           case MCBinaryExpr::NE:
592             Res = MCValue::get(L->isEqualTo(R) ? 0 : -1);
593             return true;
594           default:
595             break;
596           }
597         }
598       }
599       return false;
600     }
601 
602     // We only support a few operations on non-constant expressions, handle
603     // those first.
604     auto Op = ABE->getOpcode();
605     int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
606     if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
607       switch (Op) {
608       default:
609         return false;
610       case MCBinaryExpr::Add:
611       case MCBinaryExpr::Sub:
612         if (Op == MCBinaryExpr::Sub) {
613           std::swap(RHSValue.SymA, RHSValue.SymB);
614           RHSValue.Cst = -(uint64_t)RHSValue.Cst;
615         }
616         if (RHSValue.isAbsolute()) {
617           LHSValue.Cst += RHSValue.Cst;
618           Res = LHSValue;
619           return true;
620         }
621         if (LHSValue.isAbsolute()) {
622           RHSValue.Cst += LHSValue.Cst;
623           Res = RHSValue;
624           return true;
625         }
626         if (LHSValue.SymB && LHSValue.Specifier)
627           return false;
628         if (RHSValue.SymB && RHSValue.Specifier)
629           return false;
630         return evaluateSymbolicAdd(Asm, InSet, LHSValue, RHSValue, Res);
631       }
632     }
633 
634     // FIXME: We need target hooks for the evaluation. It may be limited in
635     // width, and gas defines the result of comparisons differently from
636     // Apple as.
637     int64_t Result = 0;
638     switch (Op) {
639     case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
640     case MCBinaryExpr::Add:  Result = LHS + RHS; break;
641     case MCBinaryExpr::And:  Result = LHS & RHS; break;
642     case MCBinaryExpr::Div:
643     case MCBinaryExpr::Mod:
644       // Handle division by zero. gas just emits a warning and keeps going,
645       // we try to be stricter.
646       // FIXME: Currently the caller of this function has no way to understand
647       // we're bailing out because of 'division by zero'. Therefore, it will
648       // emit a 'expected relocatable expression' error. It would be nice to
649       // change this code to emit a better diagnostic.
650       if (RHS == 0)
651         return false;
652       if (ABE->getOpcode() == MCBinaryExpr::Div)
653         Result = LHS / RHS;
654       else
655         Result = LHS % RHS;
656       break;
657     case MCBinaryExpr::EQ:   Result = LHS == RHS; break;
658     case MCBinaryExpr::GT:   Result = LHS > RHS; break;
659     case MCBinaryExpr::GTE:  Result = LHS >= RHS; break;
660     case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
661     case MCBinaryExpr::LOr:  Result = LHS || RHS; break;
662     case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
663     case MCBinaryExpr::LT:   Result = LHS < RHS; break;
664     case MCBinaryExpr::LTE:  Result = LHS <= RHS; break;
665     case MCBinaryExpr::Mul:  Result = LHS * RHS; break;
666     case MCBinaryExpr::NE:   Result = LHS != RHS; break;
667     case MCBinaryExpr::Or:   Result = LHS | RHS; break;
668     case MCBinaryExpr::OrNot: Result = LHS | ~RHS; break;
669     case MCBinaryExpr::Shl:  Result = uint64_t(LHS) << uint64_t(RHS); break;
670     case MCBinaryExpr::Sub:  Result = LHS - RHS; break;
671     case MCBinaryExpr::Xor:  Result = LHS ^ RHS; break;
672     }
673 
674     switch (Op) {
675     default:
676       Res = MCValue::get(Result);
677       break;
678     case MCBinaryExpr::EQ:
679     case MCBinaryExpr::GT:
680     case MCBinaryExpr::GTE:
681     case MCBinaryExpr::LT:
682     case MCBinaryExpr::LTE:
683     case MCBinaryExpr::NE:
684       // A comparison operator returns a -1 if true and 0 if false.
685       Res = MCValue::get(Result ? -1 : 0);
686       break;
687     }
688 
689     return true;
690   }
691   case Specifier:
692     // Fold the expression during relocation generation. As parse time Asm might
693     // be null, and targets should not rely on the folding.
694     return Asm && Asm->getContext().getAsmInfo()->evaluateAsRelocatableImpl(
695                       cast<MCSpecifierExpr>(*this), Res, Asm);
696   }
697 
698   llvm_unreachable("Invalid assembly expression kind!");
699 }
700 
findAssociatedFragment() const701 MCFragment *MCExpr::findAssociatedFragment() const {
702   switch (getKind()) {
703   case Target:
704     // We never look through target specific expressions.
705     return cast<MCTargetExpr>(this)->findAssociatedFragment();
706 
707   case Constant:
708     return MCSymbol::AbsolutePseudoFragment;
709 
710   case SymbolRef: {
711     auto &Sym =
712         const_cast<MCSymbol &>(cast<MCSymbolRefExpr>(this)->getSymbol());
713     if (Sym.Fragment)
714       return Sym.Fragment;
715     if (Sym.isResolving())
716       return MCSymbol::AbsolutePseudoFragment;
717     Sym.setIsResolving(true);
718     auto *F = Sym.getFragment();
719     Sym.setIsResolving(false);
720     return F;
721   }
722 
723   case Unary:
724     return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
725 
726   case Binary: {
727     const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
728     MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
729     MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
730 
731     // If either is absolute, return the other.
732     if (LHS_F == MCSymbol::AbsolutePseudoFragment)
733       return RHS_F;
734     if (RHS_F == MCSymbol::AbsolutePseudoFragment)
735       return LHS_F;
736 
737     // Not always correct, but probably the best we can do without more context.
738     if (BE->getOpcode() == MCBinaryExpr::Sub)
739       return MCSymbol::AbsolutePseudoFragment;
740 
741     // Otherwise, return the first non-null fragment.
742     return LHS_F ? LHS_F : RHS_F;
743   }
744 
745   case Specifier:
746     return cast<MCSpecifierExpr>(this)->getSubExpr()->findAssociatedFragment();
747   }
748 
749   llvm_unreachable("Invalid assembly expression kind!");
750 }
751 
create(const MCExpr * Expr,Spec S,MCContext & Ctx,SMLoc Loc)752 const MCSpecifierExpr *MCSpecifierExpr::create(const MCExpr *Expr, Spec S,
753                                                MCContext &Ctx, SMLoc Loc) {
754   return new (Ctx) MCSpecifierExpr(Expr, S, Loc);
755 }
756 
create(const MCSymbol * Sym,Spec S,MCContext & Ctx,SMLoc Loc)757 const MCSpecifierExpr *MCSpecifierExpr::create(const MCSymbol *Sym, Spec S,
758                                                MCContext &Ctx, SMLoc Loc) {
759   return new (Ctx) MCSpecifierExpr(MCSymbolRefExpr::create(Sym, Ctx), S, Loc);
760 }
761