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