MCExpr.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. #include "llvm/MC/MCExpr.h"
  9. #include "llvm/ADT/Statistic.h"
  10. #include "llvm/ADT/StringExtras.h"
  11. #include "llvm/ADT/StringSwitch.h"
  12. #include "llvm/Config/llvm-config.h"
  13. #include "llvm/MC/MCAsmBackend.h"
  14. #include "llvm/MC/MCAsmInfo.h"
  15. #include "llvm/MC/MCAsmLayout.h"
  16. #include "llvm/MC/MCAssembler.h"
  17. #include "llvm/MC/MCContext.h"
  18. #include "llvm/MC/MCObjectWriter.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. using namespace llvm;
  29. #define DEBUG_TYPE "mcexpr"
  30. namespace {
  31. namespace stats {
  32. STATISTIC(MCExprEvaluate, "Number of MCExpr evaluations");
  33. } // end namespace stats
  34. } // end anonymous namespace
  35. void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
  36. switch (getKind()) {
  37. case MCExpr::Target:
  38. return cast<MCTargetExpr>(this)->printImpl(OS, MAI);
  39. case MCExpr::Constant: {
  40. auto Value = cast<MCConstantExpr>(*this).getValue();
  41. auto PrintInHex = cast<MCConstantExpr>(*this).useHexFormat();
  42. auto SizeInBytes = cast<MCConstantExpr>(*this).getSizeInBytes();
  43. if (Value < 0 && MAI && !MAI->supportsSignedData())
  44. PrintInHex = true;
  45. if (PrintInHex)
  46. switch (SizeInBytes) {
  47. default:
  48. OS << "0x" << Twine::utohexstr(Value);
  49. break;
  50. case 1:
  51. OS << format("0x%02" PRIx64, Value);
  52. break;
  53. case 2:
  54. OS << format("0x%04" PRIx64, Value);
  55. break;
  56. case 4:
  57. OS << format("0x%08" PRIx64, Value);
  58. break;
  59. case 8:
  60. OS << format("0x%016" PRIx64, Value);
  61. break;
  62. }
  63. else
  64. OS << Value;
  65. return;
  66. }
  67. case MCExpr::SymbolRef: {
  68. const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*this);
  69. const MCSymbol &Sym = SRE.getSymbol();
  70. // Parenthesize names that start with $ so that they don't look like
  71. // absolute names.
  72. bool UseParens =
  73. !InParens && !Sym.getName().empty() && Sym.getName()[0] == '$';
  74. if (UseParens) {
  75. OS << '(';
  76. Sym.print(OS, MAI);
  77. OS << ')';
  78. } else
  79. Sym.print(OS, MAI);
  80. const MCSymbolRefExpr::VariantKind Kind = SRE.getKind();
  81. if (Kind != MCSymbolRefExpr::VK_None) {
  82. if (MAI && MAI->useParensForSymbolVariant()) // ARM
  83. OS << '(' << MCSymbolRefExpr::getVariantKindName(Kind) << ')';
  84. else
  85. OS << '@' << MCSymbolRefExpr::getVariantKindName(Kind);
  86. }
  87. return;
  88. }
  89. case MCExpr::Unary: {
  90. const MCUnaryExpr &UE = cast<MCUnaryExpr>(*this);
  91. switch (UE.getOpcode()) {
  92. case MCUnaryExpr::LNot: OS << '!'; break;
  93. case MCUnaryExpr::Minus: OS << '-'; break;
  94. case MCUnaryExpr::Not: OS << '~'; break;
  95. case MCUnaryExpr::Plus: OS << '+'; break;
  96. }
  97. bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
  98. if (Binary) OS << "(";
  99. UE.getSubExpr()->print(OS, MAI);
  100. if (Binary) OS << ")";
  101. return;
  102. }
  103. case MCExpr::Binary: {
  104. const MCBinaryExpr &BE = cast<MCBinaryExpr>(*this);
  105. // Only print parens around the LHS if it is non-trivial.
  106. if (isa<MCConstantExpr>(BE.getLHS()) || isa<MCSymbolRefExpr>(BE.getLHS())) {
  107. BE.getLHS()->print(OS, MAI);
  108. } else {
  109. OS << '(';
  110. BE.getLHS()->print(OS, MAI);
  111. OS << ')';
  112. }
  113. switch (BE.getOpcode()) {
  114. case MCBinaryExpr::Add:
  115. // Print "X-42" instead of "X+-42".
  116. if (const MCConstantExpr *RHSC = dyn_cast<MCConstantExpr>(BE.getRHS())) {
  117. if (RHSC->getValue() < 0) {
  118. OS << RHSC->getValue();
  119. return;
  120. }
  121. }
  122. OS << '+';
  123. break;
  124. case MCBinaryExpr::AShr: OS << ">>"; break;
  125. case MCBinaryExpr::And: OS << '&'; break;
  126. case MCBinaryExpr::Div: OS << '/'; break;
  127. case MCBinaryExpr::EQ: OS << "=="; break;
  128. case MCBinaryExpr::GT: OS << '>'; break;
  129. case MCBinaryExpr::GTE: OS << ">="; break;
  130. case MCBinaryExpr::LAnd: OS << "&&"; break;
  131. case MCBinaryExpr::LOr: OS << "||"; break;
  132. case MCBinaryExpr::LShr: OS << ">>"; break;
  133. case MCBinaryExpr::LT: OS << '<'; break;
  134. case MCBinaryExpr::LTE: OS << "<="; break;
  135. case MCBinaryExpr::Mod: OS << '%'; break;
  136. case MCBinaryExpr::Mul: OS << '*'; break;
  137. case MCBinaryExpr::NE: OS << "!="; break;
  138. case MCBinaryExpr::Or: OS << '|'; break;
  139. case MCBinaryExpr::OrNot: OS << '!'; break;
  140. case MCBinaryExpr::Shl: OS << "<<"; break;
  141. case MCBinaryExpr::Sub: OS << '-'; break;
  142. case MCBinaryExpr::Xor: OS << '^'; break;
  143. }
  144. // Only print parens around the LHS if it is non-trivial.
  145. if (isa<MCConstantExpr>(BE.getRHS()) || isa<MCSymbolRefExpr>(BE.getRHS())) {
  146. BE.getRHS()->print(OS, MAI);
  147. } else {
  148. OS << '(';
  149. BE.getRHS()->print(OS, MAI);
  150. OS << ')';
  151. }
  152. return;
  153. }
  154. }
  155. llvm_unreachable("Invalid expression kind!");
  156. }
  157. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  158. LLVM_DUMP_METHOD void MCExpr::dump() const {
  159. dbgs() << *this;
  160. dbgs() << '\n';
  161. }
  162. #endif
  163. /* *** */
  164. const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,
  165. const MCExpr *RHS, MCContext &Ctx,
  166. SMLoc Loc) {
  167. return new (Ctx) MCBinaryExpr(Opc, LHS, RHS, Loc);
  168. }
  169. const MCUnaryExpr *MCUnaryExpr::create(Opcode Opc, const MCExpr *Expr,
  170. MCContext &Ctx, SMLoc Loc) {
  171. return new (Ctx) MCUnaryExpr(Opc, Expr, Loc);
  172. }
  173. const MCConstantExpr *MCConstantExpr::create(int64_t Value, MCContext &Ctx,
  174. bool PrintInHex,
  175. unsigned SizeInBytes) {
  176. return new (Ctx) MCConstantExpr(Value, PrintInHex, SizeInBytes);
  177. }
  178. /* *** */
  179. MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
  180. const MCAsmInfo *MAI, SMLoc Loc)
  181. : MCExpr(MCExpr::SymbolRef, Loc,
  182. encodeSubclassData(Kind, MAI->hasSubsectionsViaSymbols())),
  183. Symbol(Symbol) {
  184. assert(Symbol);
  185. }
  186. const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
  187. VariantKind Kind,
  188. MCContext &Ctx, SMLoc Loc) {
  189. return new (Ctx) MCSymbolRefExpr(Sym, Kind, Ctx.getAsmInfo(), Loc);
  190. }
  191. const MCSymbolRefExpr *MCSymbolRefExpr::create(StringRef Name, VariantKind Kind,
  192. MCContext &Ctx) {
  193. return create(Ctx.getOrCreateSymbol(Name), Kind, Ctx);
  194. }
  195. StringRef MCSymbolRefExpr::getVariantKindName(VariantKind Kind) {
  196. switch (Kind) {
  197. case VK_Invalid: return "<<invalid>>";
  198. case VK_None: return "<<none>>";
  199. case VK_DTPOFF: return "DTPOFF";
  200. case VK_DTPREL: return "DTPREL";
  201. case VK_GOT: return "GOT";
  202. case VK_GOTOFF: return "GOTOFF";
  203. case VK_GOTREL: return "GOTREL";
  204. case VK_PCREL: return "PCREL";
  205. case VK_GOTPCREL: return "GOTPCREL";
  206. case VK_GOTPCREL_NORELAX: return "GOTPCREL_NORELAX";
  207. case VK_GOTTPOFF: return "GOTTPOFF";
  208. case VK_INDNTPOFF: return "INDNTPOFF";
  209. case VK_NTPOFF: return "NTPOFF";
  210. case VK_GOTNTPOFF: return "GOTNTPOFF";
  211. case VK_PLT: return "PLT";
  212. case VK_TLSGD: return "TLSGD";
  213. case VK_TLSLD: return "TLSLD";
  214. case VK_TLSLDM: return "TLSLDM";
  215. case VK_TPOFF: return "TPOFF";
  216. case VK_TPREL: return "TPREL";
  217. case VK_TLSCALL: return "tlscall";
  218. case VK_TLSDESC: return "tlsdesc";
  219. case VK_TLVP: return "TLVP";
  220. case VK_TLVPPAGE: return "TLVPPAGE";
  221. case VK_TLVPPAGEOFF: return "TLVPPAGEOFF";
  222. case VK_PAGE: return "PAGE";
  223. case VK_PAGEOFF: return "PAGEOFF";
  224. case VK_GOTPAGE: return "GOTPAGE";
  225. case VK_GOTPAGEOFF: return "GOTPAGEOFF";
  226. case VK_SECREL: return "SECREL32";
  227. case VK_SIZE: return "SIZE";
  228. case VK_WEAKREF: return "WEAKREF";
  229. case VK_X86_ABS8: return "ABS8";
  230. case VK_X86_PLTOFF: return "PLTOFF";
  231. case VK_ARM_NONE: return "none";
  232. case VK_ARM_GOT_PREL: return "GOT_PREL";
  233. case VK_ARM_TARGET1: return "target1";
  234. case VK_ARM_TARGET2: return "target2";
  235. case VK_ARM_PREL31: return "prel31";
  236. case VK_ARM_SBREL: return "sbrel";
  237. case VK_ARM_TLSLDO: return "tlsldo";
  238. case VK_ARM_TLSDESCSEQ: return "tlsdescseq";
  239. case VK_AVR_NONE: return "none";
  240. case VK_AVR_LO8: return "lo8";
  241. case VK_AVR_HI8: return "hi8";
  242. case VK_AVR_HLO8: return "hlo8";
  243. case VK_AVR_DIFF8: return "diff8";
  244. case VK_AVR_DIFF16: return "diff16";
  245. case VK_AVR_DIFF32: return "diff32";
  246. case VK_AVR_PM: return "pm";
  247. case VK_PPC_LO: return "l";
  248. case VK_PPC_HI: return "h";
  249. case VK_PPC_HA: return "ha";
  250. case VK_PPC_HIGH: return "high";
  251. case VK_PPC_HIGHA: return "higha";
  252. case VK_PPC_HIGHER: return "higher";
  253. case VK_PPC_HIGHERA: return "highera";
  254. case VK_PPC_HIGHEST: return "highest";
  255. case VK_PPC_HIGHESTA: return "highesta";
  256. case VK_PPC_GOT_LO: return "got@l";
  257. case VK_PPC_GOT_HI: return "got@h";
  258. case VK_PPC_GOT_HA: return "got@ha";
  259. case VK_PPC_TOCBASE: return "tocbase";
  260. case VK_PPC_TOC: return "toc";
  261. case VK_PPC_TOC_LO: return "toc@l";
  262. case VK_PPC_TOC_HI: return "toc@h";
  263. case VK_PPC_TOC_HA: return "toc@ha";
  264. case VK_PPC_U: return "u";
  265. case VK_PPC_L: return "l";
  266. case VK_PPC_DTPMOD: return "dtpmod";
  267. case VK_PPC_TPREL_LO: return "tprel@l";
  268. case VK_PPC_TPREL_HI: return "tprel@h";
  269. case VK_PPC_TPREL_HA: return "tprel@ha";
  270. case VK_PPC_TPREL_HIGH: return "tprel@high";
  271. case VK_PPC_TPREL_HIGHA: return "tprel@higha";
  272. case VK_PPC_TPREL_HIGHER: return "tprel@higher";
  273. case VK_PPC_TPREL_HIGHERA: return "tprel@highera";
  274. case VK_PPC_TPREL_HIGHEST: return "tprel@highest";
  275. case VK_PPC_TPREL_HIGHESTA: return "tprel@highesta";
  276. case VK_PPC_DTPREL_LO: return "dtprel@l";
  277. case VK_PPC_DTPREL_HI: return "dtprel@h";
  278. case VK_PPC_DTPREL_HA: return "dtprel@ha";
  279. case VK_PPC_DTPREL_HIGH: return "dtprel@high";
  280. case VK_PPC_DTPREL_HIGHA: return "dtprel@higha";
  281. case VK_PPC_DTPREL_HIGHER: return "dtprel@higher";
  282. case VK_PPC_DTPREL_HIGHERA: return "dtprel@highera";
  283. case VK_PPC_DTPREL_HIGHEST: return "dtprel@highest";
  284. case VK_PPC_DTPREL_HIGHESTA: return "dtprel@highesta";
  285. case VK_PPC_GOT_TPREL: return "got@tprel";
  286. case VK_PPC_GOT_TPREL_LO: return "got@tprel@l";
  287. case VK_PPC_GOT_TPREL_HI: return "got@tprel@h";
  288. case VK_PPC_GOT_TPREL_HA: return "got@tprel@ha";
  289. case VK_PPC_GOT_DTPREL: return "got@dtprel";
  290. case VK_PPC_GOT_DTPREL_LO: return "got@dtprel@l";
  291. case VK_PPC_GOT_DTPREL_HI: return "got@dtprel@h";
  292. case VK_PPC_GOT_DTPREL_HA: return "got@dtprel@ha";
  293. case VK_PPC_TLS: return "tls";
  294. case VK_PPC_GOT_TLSGD: return "got@tlsgd";
  295. case VK_PPC_GOT_TLSGD_LO: return "got@tlsgd@l";
  296. case VK_PPC_GOT_TLSGD_HI: return "got@tlsgd@h";
  297. case VK_PPC_GOT_TLSGD_HA: return "got@tlsgd@ha";
  298. case VK_PPC_TLSGD: return "tlsgd";
  299. case VK_PPC_AIX_TLSGD:
  300. return "gd";
  301. case VK_PPC_AIX_TLSGDM:
  302. return "m";
  303. case VK_PPC_GOT_TLSLD: return "got@tlsld";
  304. case VK_PPC_GOT_TLSLD_LO: return "got@tlsld@l";
  305. case VK_PPC_GOT_TLSLD_HI: return "got@tlsld@h";
  306. case VK_PPC_GOT_TLSLD_HA: return "got@tlsld@ha";
  307. case VK_PPC_GOT_PCREL:
  308. return "got@pcrel";
  309. case VK_PPC_GOT_TLSGD_PCREL:
  310. return "got@tlsgd@pcrel";
  311. case VK_PPC_GOT_TLSLD_PCREL:
  312. return "got@tlsld@pcrel";
  313. case VK_PPC_GOT_TPREL_PCREL:
  314. return "got@tprel@pcrel";
  315. case VK_PPC_TLS_PCREL:
  316. return "tls@pcrel";
  317. case VK_PPC_TLSLD: return "tlsld";
  318. case VK_PPC_LOCAL: return "local";
  319. case VK_PPC_NOTOC: return "notoc";
  320. case VK_PPC_PCREL_OPT: return "<<invalid>>";
  321. case VK_COFF_IMGREL32: return "IMGREL";
  322. case VK_Hexagon_LO16: return "LO16";
  323. case VK_Hexagon_HI16: return "HI16";
  324. case VK_Hexagon_GPREL: return "GPREL";
  325. case VK_Hexagon_GD_GOT: return "GDGOT";
  326. case VK_Hexagon_LD_GOT: return "LDGOT";
  327. case VK_Hexagon_GD_PLT: return "GDPLT";
  328. case VK_Hexagon_LD_PLT: return "LDPLT";
  329. case VK_Hexagon_IE: return "IE";
  330. case VK_Hexagon_IE_GOT: return "IEGOT";
  331. case VK_WASM_TYPEINDEX: return "TYPEINDEX";
  332. case VK_WASM_MBREL: return "MBREL";
  333. case VK_WASM_TLSREL: return "TLSREL";
  334. case VK_WASM_TBREL: return "TBREL";
  335. case VK_WASM_GOT_TLS: return "GOT@TLS";
  336. case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo";
  337. case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi";
  338. case VK_AMDGPU_REL32_LO: return "rel32@lo";
  339. case VK_AMDGPU_REL32_HI: return "rel32@hi";
  340. case VK_AMDGPU_REL64: return "rel64";
  341. case VK_AMDGPU_ABS32_LO: return "abs32@lo";
  342. case VK_AMDGPU_ABS32_HI: return "abs32@hi";
  343. case VK_VE_HI32: return "hi";
  344. case VK_VE_LO32: return "lo";
  345. case VK_VE_PC_HI32: return "pc_hi";
  346. case VK_VE_PC_LO32: return "pc_lo";
  347. case VK_VE_GOT_HI32: return "got_hi";
  348. case VK_VE_GOT_LO32: return "got_lo";
  349. case VK_VE_GOTOFF_HI32: return "gotoff_hi";
  350. case VK_VE_GOTOFF_LO32: return "gotoff_lo";
  351. case VK_VE_PLT_HI32: return "plt_hi";
  352. case VK_VE_PLT_LO32: return "plt_lo";
  353. case VK_VE_TLS_GD_HI32: return "tls_gd_hi";
  354. case VK_VE_TLS_GD_LO32: return "tls_gd_lo";
  355. case VK_VE_TPOFF_HI32: return "tpoff_hi";
  356. case VK_VE_TPOFF_LO32: return "tpoff_lo";
  357. }
  358. llvm_unreachable("Invalid variant kind");
  359. }
  360. MCSymbolRefExpr::VariantKind
  361. MCSymbolRefExpr::getVariantKindForName(StringRef Name) {
  362. return StringSwitch<VariantKind>(Name.lower())
  363. .Case("dtprel", VK_DTPREL)
  364. .Case("dtpoff", VK_DTPOFF)
  365. .Case("got", VK_GOT)
  366. .Case("gotoff", VK_GOTOFF)
  367. .Case("gotrel", VK_GOTREL)
  368. .Case("pcrel", VK_PCREL)
  369. .Case("gotpcrel", VK_GOTPCREL)
  370. .Case("gotpcrel_norelax", VK_GOTPCREL_NORELAX)
  371. .Case("gottpoff", VK_GOTTPOFF)
  372. .Case("indntpoff", VK_INDNTPOFF)
  373. .Case("ntpoff", VK_NTPOFF)
  374. .Case("gotntpoff", VK_GOTNTPOFF)
  375. .Case("plt", VK_PLT)
  376. .Case("tlscall", VK_TLSCALL)
  377. .Case("tlsdesc", VK_TLSDESC)
  378. .Case("tlsgd", VK_TLSGD)
  379. .Case("tlsld", VK_TLSLD)
  380. .Case("tlsldm", VK_TLSLDM)
  381. .Case("tpoff", VK_TPOFF)
  382. .Case("tprel", VK_TPREL)
  383. .Case("tlvp", VK_TLVP)
  384. .Case("tlvppage", VK_TLVPPAGE)
  385. .Case("tlvppageoff", VK_TLVPPAGEOFF)
  386. .Case("page", VK_PAGE)
  387. .Case("pageoff", VK_PAGEOFF)
  388. .Case("gotpage", VK_GOTPAGE)
  389. .Case("gotpageoff", VK_GOTPAGEOFF)
  390. .Case("imgrel", VK_COFF_IMGREL32)
  391. .Case("secrel32", VK_SECREL)
  392. .Case("size", VK_SIZE)
  393. .Case("abs8", VK_X86_ABS8)
  394. .Case("pltoff", VK_X86_PLTOFF)
  395. .Case("l", VK_PPC_LO)
  396. .Case("h", VK_PPC_HI)
  397. .Case("ha", VK_PPC_HA)
  398. .Case("high", VK_PPC_HIGH)
  399. .Case("higha", VK_PPC_HIGHA)
  400. .Case("higher", VK_PPC_HIGHER)
  401. .Case("highera", VK_PPC_HIGHERA)
  402. .Case("highest", VK_PPC_HIGHEST)
  403. .Case("highesta", VK_PPC_HIGHESTA)
  404. .Case("got@l", VK_PPC_GOT_LO)
  405. .Case("got@h", VK_PPC_GOT_HI)
  406. .Case("got@ha", VK_PPC_GOT_HA)
  407. .Case("local", VK_PPC_LOCAL)
  408. .Case("tocbase", VK_PPC_TOCBASE)
  409. .Case("toc", VK_PPC_TOC)
  410. .Case("toc@l", VK_PPC_TOC_LO)
  411. .Case("toc@h", VK_PPC_TOC_HI)
  412. .Case("toc@ha", VK_PPC_TOC_HA)
  413. .Case("u", VK_PPC_U)
  414. .Case("l", VK_PPC_L)
  415. .Case("tls", VK_PPC_TLS)
  416. .Case("dtpmod", VK_PPC_DTPMOD)
  417. .Case("tprel@l", VK_PPC_TPREL_LO)
  418. .Case("tprel@h", VK_PPC_TPREL_HI)
  419. .Case("tprel@ha", VK_PPC_TPREL_HA)
  420. .Case("tprel@high", VK_PPC_TPREL_HIGH)
  421. .Case("tprel@higha", VK_PPC_TPREL_HIGHA)
  422. .Case("tprel@higher", VK_PPC_TPREL_HIGHER)
  423. .Case("tprel@highera", VK_PPC_TPREL_HIGHERA)
  424. .Case("tprel@highest", VK_PPC_TPREL_HIGHEST)
  425. .Case("tprel@highesta", VK_PPC_TPREL_HIGHESTA)
  426. .Case("dtprel@l", VK_PPC_DTPREL_LO)
  427. .Case("dtprel@h", VK_PPC_DTPREL_HI)
  428. .Case("dtprel@ha", VK_PPC_DTPREL_HA)
  429. .Case("dtprel@high", VK_PPC_DTPREL_HIGH)
  430. .Case("dtprel@higha", VK_PPC_DTPREL_HIGHA)
  431. .Case("dtprel@higher", VK_PPC_DTPREL_HIGHER)
  432. .Case("dtprel@highera", VK_PPC_DTPREL_HIGHERA)
  433. .Case("dtprel@highest", VK_PPC_DTPREL_HIGHEST)
  434. .Case("dtprel@highesta", VK_PPC_DTPREL_HIGHESTA)
  435. .Case("got@tprel", VK_PPC_GOT_TPREL)
  436. .Case("got@tprel@l", VK_PPC_GOT_TPREL_LO)
  437. .Case("got@tprel@h", VK_PPC_GOT_TPREL_HI)
  438. .Case("got@tprel@ha", VK_PPC_GOT_TPREL_HA)
  439. .Case("got@dtprel", VK_PPC_GOT_DTPREL)
  440. .Case("got@dtprel@l", VK_PPC_GOT_DTPREL_LO)
  441. .Case("got@dtprel@h", VK_PPC_GOT_DTPREL_HI)
  442. .Case("got@dtprel@ha", VK_PPC_GOT_DTPREL_HA)
  443. .Case("got@tlsgd", VK_PPC_GOT_TLSGD)
  444. .Case("got@tlsgd@l", VK_PPC_GOT_TLSGD_LO)
  445. .Case("got@tlsgd@h", VK_PPC_GOT_TLSGD_HI)
  446. .Case("got@tlsgd@ha", VK_PPC_GOT_TLSGD_HA)
  447. .Case("got@tlsld", VK_PPC_GOT_TLSLD)
  448. .Case("got@tlsld@l", VK_PPC_GOT_TLSLD_LO)
  449. .Case("got@tlsld@h", VK_PPC_GOT_TLSLD_HI)
  450. .Case("got@tlsld@ha", VK_PPC_GOT_TLSLD_HA)
  451. .Case("got@pcrel", VK_PPC_GOT_PCREL)
  452. .Case("got@tlsgd@pcrel", VK_PPC_GOT_TLSGD_PCREL)
  453. .Case("got@tlsld@pcrel", VK_PPC_GOT_TLSLD_PCREL)
  454. .Case("got@tprel@pcrel", VK_PPC_GOT_TPREL_PCREL)
  455. .Case("tls@pcrel", VK_PPC_TLS_PCREL)
  456. .Case("notoc", VK_PPC_NOTOC)
  457. .Case("gdgot", VK_Hexagon_GD_GOT)
  458. .Case("gdplt", VK_Hexagon_GD_PLT)
  459. .Case("iegot", VK_Hexagon_IE_GOT)
  460. .Case("ie", VK_Hexagon_IE)
  461. .Case("ldgot", VK_Hexagon_LD_GOT)
  462. .Case("ldplt", VK_Hexagon_LD_PLT)
  463. .Case("none", VK_ARM_NONE)
  464. .Case("got_prel", VK_ARM_GOT_PREL)
  465. .Case("target1", VK_ARM_TARGET1)
  466. .Case("target2", VK_ARM_TARGET2)
  467. .Case("prel31", VK_ARM_PREL31)
  468. .Case("sbrel", VK_ARM_SBREL)
  469. .Case("tlsldo", VK_ARM_TLSLDO)
  470. .Case("lo8", VK_AVR_LO8)
  471. .Case("hi8", VK_AVR_HI8)
  472. .Case("hlo8", VK_AVR_HLO8)
  473. .Case("typeindex", VK_WASM_TYPEINDEX)
  474. .Case("tbrel", VK_WASM_TBREL)
  475. .Case("mbrel", VK_WASM_MBREL)
  476. .Case("tlsrel", VK_WASM_TLSREL)
  477. .Case("got@tls", VK_WASM_GOT_TLS)
  478. .Case("gotpcrel32@lo", VK_AMDGPU_GOTPCREL32_LO)
  479. .Case("gotpcrel32@hi", VK_AMDGPU_GOTPCREL32_HI)
  480. .Case("rel32@lo", VK_AMDGPU_REL32_LO)
  481. .Case("rel32@hi", VK_AMDGPU_REL32_HI)
  482. .Case("rel64", VK_AMDGPU_REL64)
  483. .Case("abs32@lo", VK_AMDGPU_ABS32_LO)
  484. .Case("abs32@hi", VK_AMDGPU_ABS32_HI)
  485. .Case("hi", VK_VE_HI32)
  486. .Case("lo", VK_VE_LO32)
  487. .Case("pc_hi", VK_VE_PC_HI32)
  488. .Case("pc_lo", VK_VE_PC_LO32)
  489. .Case("got_hi", VK_VE_GOT_HI32)
  490. .Case("got_lo", VK_VE_GOT_LO32)
  491. .Case("gotoff_hi", VK_VE_GOTOFF_HI32)
  492. .Case("gotoff_lo", VK_VE_GOTOFF_LO32)
  493. .Case("plt_hi", VK_VE_PLT_HI32)
  494. .Case("plt_lo", VK_VE_PLT_LO32)
  495. .Case("tls_gd_hi", VK_VE_TLS_GD_HI32)
  496. .Case("tls_gd_lo", VK_VE_TLS_GD_LO32)
  497. .Case("tpoff_hi", VK_VE_TPOFF_HI32)
  498. .Case("tpoff_lo", VK_VE_TPOFF_LO32)
  499. .Default(VK_Invalid);
  500. }
  501. /* *** */
  502. void MCTargetExpr::anchor() {}
  503. /* *** */
  504. bool MCExpr::evaluateAsAbsolute(int64_t &Res) const {
  505. return evaluateAsAbsolute(Res, nullptr, nullptr, nullptr, false);
  506. }
  507. bool MCExpr::evaluateAsAbsolute(int64_t &Res,
  508. const MCAsmLayout &Layout) const {
  509. return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr, false);
  510. }
  511. bool MCExpr::evaluateAsAbsolute(int64_t &Res,
  512. const MCAsmLayout &Layout,
  513. const SectionAddrMap &Addrs) const {
  514. // Setting InSet causes us to absolutize differences across sections and that
  515. // is what the MachO writer uses Addrs for.
  516. return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, &Addrs, true);
  517. }
  518. bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const {
  519. return evaluateAsAbsolute(Res, &Asm, nullptr, nullptr, false);
  520. }
  521. bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const {
  522. return evaluateAsAbsolute(Res, Asm, nullptr, nullptr, false);
  523. }
  524. bool MCExpr::evaluateKnownAbsolute(int64_t &Res,
  525. const MCAsmLayout &Layout) const {
  526. return evaluateAsAbsolute(Res, &Layout.getAssembler(), &Layout, nullptr,
  527. true);
  528. }
  529. bool MCExpr::evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
  530. const MCAsmLayout *Layout,
  531. const SectionAddrMap *Addrs, bool InSet) const {
  532. MCValue Value;
  533. // Fast path constants.
  534. if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(this)) {
  535. Res = CE->getValue();
  536. return true;
  537. }
  538. bool IsRelocatable =
  539. evaluateAsRelocatableImpl(Value, Asm, Layout, nullptr, Addrs, InSet);
  540. // Record the current value.
  541. Res = Value.getConstant();
  542. return IsRelocatable && Value.isAbsolute();
  543. }
  544. /// Helper method for \see EvaluateSymbolAdd().
  545. static void AttemptToFoldSymbolOffsetDifference(
  546. const MCAssembler *Asm, const MCAsmLayout *Layout,
  547. const SectionAddrMap *Addrs, bool InSet, const MCSymbolRefExpr *&A,
  548. const MCSymbolRefExpr *&B, int64_t &Addend) {
  549. if (!A || !B)
  550. return;
  551. const MCSymbol &SA = A->getSymbol();
  552. const MCSymbol &SB = B->getSymbol();
  553. if (SA.isUndefined() || SB.isUndefined())
  554. return;
  555. if (!Asm->getWriter().isSymbolRefDifferenceFullyResolved(*Asm, A, B, InSet))
  556. return;
  557. auto FinalizeFolding = [&]() {
  558. // Pointers to Thumb symbols need to have their low-bit set to allow
  559. // for interworking.
  560. if (Asm->isThumbFunc(&SA))
  561. Addend |= 1;
  562. // If symbol is labeled as micromips, we set low-bit to ensure
  563. // correct offset in .gcc_except_table
  564. if (Asm->getBackend().isMicroMips(&SA))
  565. Addend |= 1;
  566. // Clear the symbol expr pointers to indicate we have folded these
  567. // operands.
  568. A = B = nullptr;
  569. };
  570. const MCFragment *FA = SA.getFragment();
  571. const MCFragment *FB = SB.getFragment();
  572. // If both symbols are in the same fragment, return the difference of their
  573. // offsets
  574. if (FA == FB && !SA.isVariable() && !SA.isUnset() && !SB.isVariable() &&
  575. !SB.isUnset()) {
  576. Addend += SA.getOffset() - SB.getOffset();
  577. return FinalizeFolding();
  578. }
  579. const MCSection &SecA = *FA->getParent();
  580. const MCSection &SecB = *FB->getParent();
  581. if ((&SecA != &SecB) && !Addrs)
  582. return;
  583. if (Layout) {
  584. // One of the symbol involved is part of a fragment being laid out. Quit now
  585. // to avoid a self loop.
  586. if (!Layout->canGetFragmentOffset(FA) || !Layout->canGetFragmentOffset(FB))
  587. return;
  588. // Eagerly evaluate when layout is finalized.
  589. Addend += Layout->getSymbolOffset(A->getSymbol()) -
  590. Layout->getSymbolOffset(B->getSymbol());
  591. if (Addrs && (&SecA != &SecB))
  592. Addend += (Addrs->lookup(&SecA) - Addrs->lookup(&SecB));
  593. FinalizeFolding();
  594. } else {
  595. // When layout is not finalized, our ability to resolve differences between
  596. // symbols is limited to specific cases where the fragments between two
  597. // symbols (including the fragments the symbols are defined in) are
  598. // fixed-size fragments so the difference can be calculated. For example,
  599. // this is important when the Subtarget is changed and a new MCDataFragment
  600. // is created in the case of foo: instr; .arch_extension ext; instr .if . -
  601. // foo.
  602. if (SA.isVariable() || SA.isUnset() || SB.isVariable() || SB.isUnset() ||
  603. FA->getKind() != MCFragment::FT_Data ||
  604. FB->getKind() != MCFragment::FT_Data ||
  605. FA->getSubsectionNumber() != FB->getSubsectionNumber())
  606. return;
  607. // Try to find a constant displacement from FA to FB, add the displacement
  608. // between the offset in FA of SA and the offset in FB of SB.
  609. int64_t Displacement = SA.getOffset() - SB.getOffset();
  610. for (auto FI = FB->getIterator(), FE = SecA.end(); FI != FE; ++FI) {
  611. if (&*FI == FA) {
  612. Addend += Displacement;
  613. return FinalizeFolding();
  614. }
  615. if (FI->getKind() != MCFragment::FT_Data)
  616. return;
  617. Displacement += cast<MCDataFragment>(FI)->getContents().size();
  618. }
  619. }
  620. }
  621. /// Evaluate the result of an add between (conceptually) two MCValues.
  622. ///
  623. /// This routine conceptually attempts to construct an MCValue:
  624. /// Result = (Result_A - Result_B + Result_Cst)
  625. /// from two MCValue's LHS and RHS where
  626. /// Result = LHS + RHS
  627. /// and
  628. /// Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
  629. ///
  630. /// This routine attempts to aggressively fold the operands such that the result
  631. /// is representable in an MCValue, but may not always succeed.
  632. ///
  633. /// \returns True on success, false if the result is not representable in an
  634. /// MCValue.
  635. /// NOTE: It is really important to have both the Asm and Layout arguments.
  636. /// They might look redundant, but this function can be used before layout
  637. /// is done (see the object streamer for example) and having the Asm argument
  638. /// lets us avoid relaxations early.
  639. static bool
  640. EvaluateSymbolicAdd(const MCAssembler *Asm, const MCAsmLayout *Layout,
  641. const SectionAddrMap *Addrs, bool InSet, const MCValue &LHS,
  642. const MCSymbolRefExpr *RHS_A, const MCSymbolRefExpr *RHS_B,
  643. int64_t RHS_Cst, MCValue &Res) {
  644. // FIXME: This routine (and other evaluation parts) are *incredibly* sloppy
  645. // about dealing with modifiers. This will ultimately bite us, one day.
  646. const MCSymbolRefExpr *LHS_A = LHS.getSymA();
  647. const MCSymbolRefExpr *LHS_B = LHS.getSymB();
  648. int64_t LHS_Cst = LHS.getConstant();
  649. // Fold the result constant immediately.
  650. int64_t Result_Cst = LHS_Cst + RHS_Cst;
  651. assert((!Layout || Asm) &&
  652. "Must have an assembler object if layout is given!");
  653. // If we have a layout, we can fold resolved differences.
  654. if (Asm) {
  655. // First, fold out any differences which are fully resolved. By
  656. // reassociating terms in
  657. // Result = (LHS_A - LHS_B + LHS_Cst) + (RHS_A - RHS_B + RHS_Cst).
  658. // we have the four possible differences:
  659. // (LHS_A - LHS_B),
  660. // (LHS_A - RHS_B),
  661. // (RHS_A - LHS_B),
  662. // (RHS_A - RHS_B).
  663. // Since we are attempting to be as aggressive as possible about folding, we
  664. // attempt to evaluate each possible alternative.
  665. AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, LHS_B,
  666. Result_Cst);
  667. AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, LHS_A, RHS_B,
  668. Result_Cst);
  669. AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, LHS_B,
  670. Result_Cst);
  671. AttemptToFoldSymbolOffsetDifference(Asm, Layout, Addrs, InSet, RHS_A, RHS_B,
  672. Result_Cst);
  673. }
  674. // We can't represent the addition or subtraction of two symbols.
  675. if ((LHS_A && RHS_A) || (LHS_B && RHS_B))
  676. return false;
  677. // At this point, we have at most one additive symbol and one subtractive
  678. // symbol -- find them.
  679. const MCSymbolRefExpr *A = LHS_A ? LHS_A : RHS_A;
  680. const MCSymbolRefExpr *B = LHS_B ? LHS_B : RHS_B;
  681. Res = MCValue::get(A, B, Result_Cst);
  682. return true;
  683. }
  684. bool MCExpr::evaluateAsRelocatable(MCValue &Res,
  685. const MCAsmLayout *Layout,
  686. const MCFixup *Fixup) const {
  687. MCAssembler *Assembler = Layout ? &Layout->getAssembler() : nullptr;
  688. return evaluateAsRelocatableImpl(Res, Assembler, Layout, Fixup, nullptr,
  689. false);
  690. }
  691. bool MCExpr::evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const {
  692. MCAssembler *Assembler = &Layout.getAssembler();
  693. return evaluateAsRelocatableImpl(Res, Assembler, &Layout, nullptr, nullptr,
  694. true);
  695. }
  696. static bool canExpand(const MCSymbol &Sym, bool InSet) {
  697. const MCExpr *Expr = Sym.getVariableValue();
  698. const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr);
  699. if (Inner) {
  700. if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
  701. return false;
  702. }
  703. if (InSet)
  704. return true;
  705. return !Sym.isInSection();
  706. }
  707. bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
  708. const MCAsmLayout *Layout,
  709. const MCFixup *Fixup,
  710. const SectionAddrMap *Addrs,
  711. bool InSet) const {
  712. ++stats::MCExprEvaluate;
  713. switch (getKind()) {
  714. case Target:
  715. return cast<MCTargetExpr>(this)->evaluateAsRelocatableImpl(Res, Layout,
  716. Fixup);
  717. case Constant:
  718. Res = MCValue::get(cast<MCConstantExpr>(this)->getValue());
  719. return true;
  720. case SymbolRef: {
  721. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
  722. const MCSymbol &Sym = SRE->getSymbol();
  723. const auto Kind = SRE->getKind();
  724. // Evaluate recursively if this is a variable.
  725. if (Sym.isVariable() && (Kind == MCSymbolRefExpr::VK_None || Layout) &&
  726. canExpand(Sym, InSet)) {
  727. bool IsMachO = SRE->hasSubsectionsViaSymbols();
  728. if (Sym.getVariableValue()->evaluateAsRelocatableImpl(
  729. Res, Asm, Layout, Fixup, Addrs, InSet || IsMachO)) {
  730. if (Kind != MCSymbolRefExpr::VK_None) {
  731. if (Res.isAbsolute()) {
  732. Res = MCValue::get(SRE, nullptr, 0);
  733. return true;
  734. }
  735. // If the reference has a variant kind, we can only handle expressions
  736. // which evaluate exactly to a single unadorned symbol. Attach the
  737. // original VariantKind to SymA of the result.
  738. if (Res.getRefKind() != MCSymbolRefExpr::VK_None || !Res.getSymA() ||
  739. Res.getSymB() || Res.getConstant())
  740. return false;
  741. Res =
  742. MCValue::get(MCSymbolRefExpr::create(&Res.getSymA()->getSymbol(),
  743. Kind, Asm->getContext()),
  744. Res.getSymB(), Res.getConstant(), Res.getRefKind());
  745. }
  746. if (!IsMachO)
  747. return true;
  748. const MCSymbolRefExpr *A = Res.getSymA();
  749. const MCSymbolRefExpr *B = Res.getSymB();
  750. // FIXME: This is small hack. Given
  751. // a = b + 4
  752. // .long a
  753. // the OS X assembler will completely drop the 4. We should probably
  754. // include it in the relocation or produce an error if that is not
  755. // possible.
  756. // Allow constant expressions.
  757. if (!A && !B)
  758. return true;
  759. // Allows aliases with zero offset.
  760. if (Res.getConstant() == 0 && (!A || !B))
  761. return true;
  762. }
  763. }
  764. Res = MCValue::get(SRE, nullptr, 0);
  765. return true;
  766. }
  767. case Unary: {
  768. const MCUnaryExpr *AUE = cast<MCUnaryExpr>(this);
  769. MCValue Value;
  770. if (!AUE->getSubExpr()->evaluateAsRelocatableImpl(Value, Asm, Layout, Fixup,
  771. Addrs, InSet))
  772. return false;
  773. switch (AUE->getOpcode()) {
  774. case MCUnaryExpr::LNot:
  775. if (!Value.isAbsolute())
  776. return false;
  777. Res = MCValue::get(!Value.getConstant());
  778. break;
  779. case MCUnaryExpr::Minus:
  780. /// -(a - b + const) ==> (b - a - const)
  781. if (Value.getSymA() && !Value.getSymB())
  782. return false;
  783. // The cast avoids undefined behavior if the constant is INT64_MIN.
  784. Res = MCValue::get(Value.getSymB(), Value.getSymA(),
  785. -(uint64_t)Value.getConstant());
  786. break;
  787. case MCUnaryExpr::Not:
  788. if (!Value.isAbsolute())
  789. return false;
  790. Res = MCValue::get(~Value.getConstant());
  791. break;
  792. case MCUnaryExpr::Plus:
  793. Res = Value;
  794. break;
  795. }
  796. return true;
  797. }
  798. case Binary: {
  799. const MCBinaryExpr *ABE = cast<MCBinaryExpr>(this);
  800. MCValue LHSValue, RHSValue;
  801. if (!ABE->getLHS()->evaluateAsRelocatableImpl(LHSValue, Asm, Layout, Fixup,
  802. Addrs, InSet) ||
  803. !ABE->getRHS()->evaluateAsRelocatableImpl(RHSValue, Asm, Layout, Fixup,
  804. Addrs, InSet)) {
  805. // Check if both are Target Expressions, see if we can compare them.
  806. if (const MCTargetExpr *L = dyn_cast<MCTargetExpr>(ABE->getLHS()))
  807. if (const MCTargetExpr *R = cast<MCTargetExpr>(ABE->getRHS())) {
  808. switch (ABE->getOpcode()) {
  809. case MCBinaryExpr::EQ:
  810. Res = MCValue::get((L->isEqualTo(R)) ? -1 : 0);
  811. return true;
  812. case MCBinaryExpr::NE:
  813. Res = MCValue::get((R->isEqualTo(R)) ? 0 : -1);
  814. return true;
  815. default: break;
  816. }
  817. }
  818. return false;
  819. }
  820. // We only support a few operations on non-constant expressions, handle
  821. // those first.
  822. if (!LHSValue.isAbsolute() || !RHSValue.isAbsolute()) {
  823. switch (ABE->getOpcode()) {
  824. default:
  825. return false;
  826. case MCBinaryExpr::Sub:
  827. // Negate RHS and add.
  828. // The cast avoids undefined behavior if the constant is INT64_MIN.
  829. return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
  830. RHSValue.getSymB(), RHSValue.getSymA(),
  831. -(uint64_t)RHSValue.getConstant(), Res);
  832. case MCBinaryExpr::Add:
  833. return EvaluateSymbolicAdd(Asm, Layout, Addrs, InSet, LHSValue,
  834. RHSValue.getSymA(), RHSValue.getSymB(),
  835. RHSValue.getConstant(), Res);
  836. }
  837. }
  838. // FIXME: We need target hooks for the evaluation. It may be limited in
  839. // width, and gas defines the result of comparisons differently from
  840. // Apple as.
  841. int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
  842. int64_t Result = 0;
  843. auto Op = ABE->getOpcode();
  844. switch (Op) {
  845. case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
  846. case MCBinaryExpr::Add: Result = LHS + RHS; break;
  847. case MCBinaryExpr::And: Result = LHS & RHS; break;
  848. case MCBinaryExpr::Div:
  849. case MCBinaryExpr::Mod:
  850. // Handle division by zero. gas just emits a warning and keeps going,
  851. // we try to be stricter.
  852. // FIXME: Currently the caller of this function has no way to understand
  853. // we're bailing out because of 'division by zero'. Therefore, it will
  854. // emit a 'expected relocatable expression' error. It would be nice to
  855. // change this code to emit a better diagnostic.
  856. if (RHS == 0)
  857. return false;
  858. if (ABE->getOpcode() == MCBinaryExpr::Div)
  859. Result = LHS / RHS;
  860. else
  861. Result = LHS % RHS;
  862. break;
  863. case MCBinaryExpr::EQ: Result = LHS == RHS; break;
  864. case MCBinaryExpr::GT: Result = LHS > RHS; break;
  865. case MCBinaryExpr::GTE: Result = LHS >= RHS; break;
  866. case MCBinaryExpr::LAnd: Result = LHS && RHS; break;
  867. case MCBinaryExpr::LOr: Result = LHS || RHS; break;
  868. case MCBinaryExpr::LShr: Result = uint64_t(LHS) >> uint64_t(RHS); break;
  869. case MCBinaryExpr::LT: Result = LHS < RHS; break;
  870. case MCBinaryExpr::LTE: Result = LHS <= RHS; break;
  871. case MCBinaryExpr::Mul: Result = LHS * RHS; break;
  872. case MCBinaryExpr::NE: Result = LHS != RHS; break;
  873. case MCBinaryExpr::Or: Result = LHS | RHS; break;
  874. case MCBinaryExpr::OrNot: Result = LHS | ~RHS; break;
  875. case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
  876. case MCBinaryExpr::Sub: Result = LHS - RHS; break;
  877. case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
  878. }
  879. switch (Op) {
  880. default:
  881. Res = MCValue::get(Result);
  882. break;
  883. case MCBinaryExpr::EQ:
  884. case MCBinaryExpr::GT:
  885. case MCBinaryExpr::GTE:
  886. case MCBinaryExpr::LT:
  887. case MCBinaryExpr::LTE:
  888. case MCBinaryExpr::NE:
  889. // A comparison operator returns a -1 if true and 0 if false.
  890. Res = MCValue::get(Result ? -1 : 0);
  891. break;
  892. }
  893. return true;
  894. }
  895. }
  896. llvm_unreachable("Invalid assembly expression kind!");
  897. }
  898. MCFragment *MCExpr::findAssociatedFragment() const {
  899. switch (getKind()) {
  900. case Target:
  901. // We never look through target specific expressions.
  902. return cast<MCTargetExpr>(this)->findAssociatedFragment();
  903. case Constant:
  904. return MCSymbol::AbsolutePseudoFragment;
  905. case SymbolRef: {
  906. const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(this);
  907. const MCSymbol &Sym = SRE->getSymbol();
  908. return Sym.getFragment();
  909. }
  910. case Unary:
  911. return cast<MCUnaryExpr>(this)->getSubExpr()->findAssociatedFragment();
  912. case Binary: {
  913. const MCBinaryExpr *BE = cast<MCBinaryExpr>(this);
  914. MCFragment *LHS_F = BE->getLHS()->findAssociatedFragment();
  915. MCFragment *RHS_F = BE->getRHS()->findAssociatedFragment();
  916. // If either is absolute, return the other.
  917. if (LHS_F == MCSymbol::AbsolutePseudoFragment)
  918. return RHS_F;
  919. if (RHS_F == MCSymbol::AbsolutePseudoFragment)
  920. return LHS_F;
  921. // Not always correct, but probably the best we can do without more context.
  922. if (BE->getOpcode() == MCBinaryExpr::Sub)
  923. return MCSymbol::AbsolutePseudoFragment;
  924. // Otherwise, return the first non-null fragment.
  925. return LHS_F ? LHS_F : RHS_F;
  926. }
  927. }
  928. llvm_unreachable("Invalid assembly expression kind!");
  929. }