ARMAsmPrinter.cpp 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  1. //===-- ARMAsmPrinter.cpp - Print machine code to an ARM .s file ----------===//
  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. // This file contains a printer that converts from our internal representation
  10. // of machine-dependent LLVM code to GAS-format ARM assembly language.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "ARMAsmPrinter.h"
  14. #include "ARM.h"
  15. #include "ARMConstantPoolValue.h"
  16. #include "ARMMachineFunctionInfo.h"
  17. #include "ARMTargetMachine.h"
  18. #include "ARMTargetObjectFile.h"
  19. #include "MCTargetDesc/ARMAddressingModes.h"
  20. #include "MCTargetDesc/ARMInstPrinter.h"
  21. #include "MCTargetDesc/ARMMCExpr.h"
  22. #include "TargetInfo/ARMTargetInfo.h"
  23. #include "llvm/ADT/SetVector.h"
  24. #include "llvm/ADT/SmallString.h"
  25. #include "llvm/BinaryFormat/COFF.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  28. #include "llvm/CodeGen/MachineModuleInfoImpls.h"
  29. #include "llvm/IR/Constants.h"
  30. #include "llvm/IR/DataLayout.h"
  31. #include "llvm/IR/Mangler.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/IR/Type.h"
  34. #include "llvm/MC/MCAsmInfo.h"
  35. #include "llvm/MC/MCAssembler.h"
  36. #include "llvm/MC/MCContext.h"
  37. #include "llvm/MC/MCELFStreamer.h"
  38. #include "llvm/MC/MCInst.h"
  39. #include "llvm/MC/MCInstBuilder.h"
  40. #include "llvm/MC/MCObjectStreamer.h"
  41. #include "llvm/MC/MCStreamer.h"
  42. #include "llvm/MC/MCSymbol.h"
  43. #include "llvm/MC/TargetRegistry.h"
  44. #include "llvm/Support/ARMBuildAttributes.h"
  45. #include "llvm/Support/Debug.h"
  46. #include "llvm/Support/ErrorHandling.h"
  47. #include "llvm/Support/TargetParser.h"
  48. #include "llvm/Support/raw_ostream.h"
  49. #include "llvm/Target/TargetMachine.h"
  50. using namespace llvm;
  51. #define DEBUG_TYPE "asm-printer"
  52. ARMAsmPrinter::ARMAsmPrinter(TargetMachine &TM,
  53. std::unique_ptr<MCStreamer> Streamer)
  54. : AsmPrinter(TM, std::move(Streamer)), Subtarget(nullptr), AFI(nullptr),
  55. MCP(nullptr), InConstantPool(false), OptimizationGoals(-1) {}
  56. void ARMAsmPrinter::emitFunctionBodyEnd() {
  57. // Make sure to terminate any constant pools that were at the end
  58. // of the function.
  59. if (!InConstantPool)
  60. return;
  61. InConstantPool = false;
  62. OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
  63. }
  64. void ARMAsmPrinter::emitFunctionEntryLabel() {
  65. if (AFI->isThumbFunction()) {
  66. OutStreamer->emitAssemblerFlag(MCAF_Code16);
  67. OutStreamer->emitThumbFunc(CurrentFnSym);
  68. } else {
  69. OutStreamer->emitAssemblerFlag(MCAF_Code32);
  70. }
  71. // Emit symbol for CMSE non-secure entry point
  72. if (AFI->isCmseNSEntryFunction()) {
  73. MCSymbol *S =
  74. OutContext.getOrCreateSymbol("__acle_se_" + CurrentFnSym->getName());
  75. emitLinkage(&MF->getFunction(), S);
  76. OutStreamer->emitSymbolAttribute(S, MCSA_ELF_TypeFunction);
  77. OutStreamer->emitLabel(S);
  78. }
  79. OutStreamer->emitLabel(CurrentFnSym);
  80. }
  81. void ARMAsmPrinter::emitXXStructor(const DataLayout &DL, const Constant *CV) {
  82. uint64_t Size = getDataLayout().getTypeAllocSize(CV->getType());
  83. assert(Size && "C++ constructor pointer had zero size!");
  84. const GlobalValue *GV = dyn_cast<GlobalValue>(CV->stripPointerCasts());
  85. assert(GV && "C++ constructor pointer was not a GlobalValue!");
  86. const MCExpr *E = MCSymbolRefExpr::create(GetARMGVSymbol(GV,
  87. ARMII::MO_NO_FLAG),
  88. (Subtarget->isTargetELF()
  89. ? MCSymbolRefExpr::VK_ARM_TARGET1
  90. : MCSymbolRefExpr::VK_None),
  91. OutContext);
  92. OutStreamer->emitValue(E, Size);
  93. }
  94. void ARMAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
  95. if (PromotedGlobals.count(GV))
  96. // The global was promoted into a constant pool. It should not be emitted.
  97. return;
  98. AsmPrinter::emitGlobalVariable(GV);
  99. }
  100. /// runOnMachineFunction - This uses the emitInstruction()
  101. /// method to print assembly for each instruction.
  102. ///
  103. bool ARMAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
  104. AFI = MF.getInfo<ARMFunctionInfo>();
  105. MCP = MF.getConstantPool();
  106. Subtarget = &MF.getSubtarget<ARMSubtarget>();
  107. SetupMachineFunction(MF);
  108. const Function &F = MF.getFunction();
  109. const TargetMachine& TM = MF.getTarget();
  110. // Collect all globals that had their storage promoted to a constant pool.
  111. // Functions are emitted before variables, so this accumulates promoted
  112. // globals from all functions in PromotedGlobals.
  113. for (auto *GV : AFI->getGlobalsPromotedToConstantPool())
  114. PromotedGlobals.insert(GV);
  115. // Calculate this function's optimization goal.
  116. unsigned OptimizationGoal;
  117. if (F.hasOptNone())
  118. // For best debugging illusion, speed and small size sacrificed
  119. OptimizationGoal = 6;
  120. else if (F.hasMinSize())
  121. // Aggressively for small size, speed and debug illusion sacrificed
  122. OptimizationGoal = 4;
  123. else if (F.hasOptSize())
  124. // For small size, but speed and debugging illusion preserved
  125. OptimizationGoal = 3;
  126. else if (TM.getOptLevel() == CodeGenOpt::Aggressive)
  127. // Aggressively for speed, small size and debug illusion sacrificed
  128. OptimizationGoal = 2;
  129. else if (TM.getOptLevel() > CodeGenOpt::None)
  130. // For speed, but small size and good debug illusion preserved
  131. OptimizationGoal = 1;
  132. else // TM.getOptLevel() == CodeGenOpt::None
  133. // For good debugging, but speed and small size preserved
  134. OptimizationGoal = 5;
  135. // Combine a new optimization goal with existing ones.
  136. if (OptimizationGoals == -1) // uninitialized goals
  137. OptimizationGoals = OptimizationGoal;
  138. else if (OptimizationGoals != (int)OptimizationGoal) // conflicting goals
  139. OptimizationGoals = 0;
  140. if (Subtarget->isTargetCOFF()) {
  141. bool Internal = F.hasInternalLinkage();
  142. COFF::SymbolStorageClass Scl = Internal ? COFF::IMAGE_SYM_CLASS_STATIC
  143. : COFF::IMAGE_SYM_CLASS_EXTERNAL;
  144. int Type = COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT;
  145. OutStreamer->BeginCOFFSymbolDef(CurrentFnSym);
  146. OutStreamer->EmitCOFFSymbolStorageClass(Scl);
  147. OutStreamer->EmitCOFFSymbolType(Type);
  148. OutStreamer->EndCOFFSymbolDef();
  149. }
  150. // Emit the rest of the function body.
  151. emitFunctionBody();
  152. // Emit the XRay table for this function.
  153. emitXRayTable();
  154. // If we need V4T thumb mode Register Indirect Jump pads, emit them.
  155. // These are created per function, rather than per TU, since it's
  156. // relatively easy to exceed the thumb branch range within a TU.
  157. if (! ThumbIndirectPads.empty()) {
  158. OutStreamer->emitAssemblerFlag(MCAF_Code16);
  159. emitAlignment(Align(2));
  160. for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) {
  161. OutStreamer->emitLabel(TIP.second);
  162. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX)
  163. .addReg(TIP.first)
  164. // Add predicate operands.
  165. .addImm(ARMCC::AL)
  166. .addReg(0));
  167. }
  168. ThumbIndirectPads.clear();
  169. }
  170. // We didn't modify anything.
  171. return false;
  172. }
  173. void ARMAsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
  174. raw_ostream &O) {
  175. assert(MO.isGlobal() && "caller should check MO.isGlobal");
  176. unsigned TF = MO.getTargetFlags();
  177. if (TF & ARMII::MO_LO16)
  178. O << ":lower16:";
  179. else if (TF & ARMII::MO_HI16)
  180. O << ":upper16:";
  181. GetARMGVSymbol(MO.getGlobal(), TF)->print(O, MAI);
  182. printOffset(MO.getOffset(), O);
  183. }
  184. void ARMAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
  185. raw_ostream &O) {
  186. const MachineOperand &MO = MI->getOperand(OpNum);
  187. switch (MO.getType()) {
  188. default: llvm_unreachable("<unknown operand type>");
  189. case MachineOperand::MO_Register: {
  190. Register Reg = MO.getReg();
  191. assert(Register::isPhysicalRegister(Reg));
  192. assert(!MO.getSubReg() && "Subregs should be eliminated!");
  193. if(ARM::GPRPairRegClass.contains(Reg)) {
  194. const MachineFunction &MF = *MI->getParent()->getParent();
  195. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  196. Reg = TRI->getSubReg(Reg, ARM::gsub_0);
  197. }
  198. O << ARMInstPrinter::getRegisterName(Reg);
  199. break;
  200. }
  201. case MachineOperand::MO_Immediate: {
  202. O << '#';
  203. unsigned TF = MO.getTargetFlags();
  204. if (TF == ARMII::MO_LO16)
  205. O << ":lower16:";
  206. else if (TF == ARMII::MO_HI16)
  207. O << ":upper16:";
  208. O << MO.getImm();
  209. break;
  210. }
  211. case MachineOperand::MO_MachineBasicBlock:
  212. MO.getMBB()->getSymbol()->print(O, MAI);
  213. return;
  214. case MachineOperand::MO_GlobalAddress: {
  215. PrintSymbolOperand(MO, O);
  216. break;
  217. }
  218. case MachineOperand::MO_ConstantPoolIndex:
  219. if (Subtarget->genExecuteOnly())
  220. llvm_unreachable("execute-only should not generate constant pools");
  221. GetCPISymbol(MO.getIndex())->print(O, MAI);
  222. break;
  223. }
  224. }
  225. MCSymbol *ARMAsmPrinter::GetCPISymbol(unsigned CPID) const {
  226. // The AsmPrinter::GetCPISymbol superclass method tries to use CPID as
  227. // indexes in MachineConstantPool, which isn't in sync with indexes used here.
  228. const DataLayout &DL = getDataLayout();
  229. return OutContext.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
  230. "CPI" + Twine(getFunctionNumber()) + "_" +
  231. Twine(CPID));
  232. }
  233. //===--------------------------------------------------------------------===//
  234. MCSymbol *ARMAsmPrinter::
  235. GetARMJTIPICJumpTableLabel(unsigned uid) const {
  236. const DataLayout &DL = getDataLayout();
  237. SmallString<60> Name;
  238. raw_svector_ostream(Name) << DL.getPrivateGlobalPrefix() << "JTI"
  239. << getFunctionNumber() << '_' << uid;
  240. return OutContext.getOrCreateSymbol(Name);
  241. }
  242. bool ARMAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
  243. const char *ExtraCode, raw_ostream &O) {
  244. // Does this asm operand have a single letter operand modifier?
  245. if (ExtraCode && ExtraCode[0]) {
  246. if (ExtraCode[1] != 0) return true; // Unknown modifier.
  247. switch (ExtraCode[0]) {
  248. default:
  249. // See if this is a generic print operand
  250. return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
  251. case 'P': // Print a VFP double precision register.
  252. case 'q': // Print a NEON quad precision register.
  253. printOperand(MI, OpNum, O);
  254. return false;
  255. case 'y': // Print a VFP single precision register as indexed double.
  256. if (MI->getOperand(OpNum).isReg()) {
  257. MCRegister Reg = MI->getOperand(OpNum).getReg().asMCReg();
  258. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  259. // Find the 'd' register that has this 's' register as a sub-register,
  260. // and determine the lane number.
  261. for (MCSuperRegIterator SR(Reg, TRI); SR.isValid(); ++SR) {
  262. if (!ARM::DPRRegClass.contains(*SR))
  263. continue;
  264. bool Lane0 = TRI->getSubReg(*SR, ARM::ssub_0) == Reg;
  265. O << ARMInstPrinter::getRegisterName(*SR) << (Lane0 ? "[0]" : "[1]");
  266. return false;
  267. }
  268. }
  269. return true;
  270. case 'B': // Bitwise inverse of integer or symbol without a preceding #.
  271. if (!MI->getOperand(OpNum).isImm())
  272. return true;
  273. O << ~(MI->getOperand(OpNum).getImm());
  274. return false;
  275. case 'L': // The low 16 bits of an immediate constant.
  276. if (!MI->getOperand(OpNum).isImm())
  277. return true;
  278. O << (MI->getOperand(OpNum).getImm() & 0xffff);
  279. return false;
  280. case 'M': { // A register range suitable for LDM/STM.
  281. if (!MI->getOperand(OpNum).isReg())
  282. return true;
  283. const MachineOperand &MO = MI->getOperand(OpNum);
  284. Register RegBegin = MO.getReg();
  285. // This takes advantage of the 2 operand-ness of ldm/stm and that we've
  286. // already got the operands in registers that are operands to the
  287. // inline asm statement.
  288. O << "{";
  289. if (ARM::GPRPairRegClass.contains(RegBegin)) {
  290. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  291. Register Reg0 = TRI->getSubReg(RegBegin, ARM::gsub_0);
  292. O << ARMInstPrinter::getRegisterName(Reg0) << ", ";
  293. RegBegin = TRI->getSubReg(RegBegin, ARM::gsub_1);
  294. }
  295. O << ARMInstPrinter::getRegisterName(RegBegin);
  296. // FIXME: The register allocator not only may not have given us the
  297. // registers in sequence, but may not be in ascending registers. This
  298. // will require changes in the register allocator that'll need to be
  299. // propagated down here if the operands change.
  300. unsigned RegOps = OpNum + 1;
  301. while (MI->getOperand(RegOps).isReg()) {
  302. O << ", "
  303. << ARMInstPrinter::getRegisterName(MI->getOperand(RegOps).getReg());
  304. RegOps++;
  305. }
  306. O << "}";
  307. return false;
  308. }
  309. case 'R': // The most significant register of a pair.
  310. case 'Q': { // The least significant register of a pair.
  311. if (OpNum == 0)
  312. return true;
  313. const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
  314. if (!FlagsOP.isImm())
  315. return true;
  316. unsigned Flags = FlagsOP.getImm();
  317. // This operand may not be the one that actually provides the register. If
  318. // it's tied to a previous one then we should refer instead to that one
  319. // for registers and their classes.
  320. unsigned TiedIdx;
  321. if (InlineAsm::isUseOperandTiedToDef(Flags, TiedIdx)) {
  322. for (OpNum = InlineAsm::MIOp_FirstOperand; TiedIdx; --TiedIdx) {
  323. unsigned OpFlags = MI->getOperand(OpNum).getImm();
  324. OpNum += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
  325. }
  326. Flags = MI->getOperand(OpNum).getImm();
  327. // Later code expects OpNum to be pointing at the register rather than
  328. // the flags.
  329. OpNum += 1;
  330. }
  331. unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
  332. unsigned RC;
  333. bool FirstHalf;
  334. const ARMBaseTargetMachine &ATM =
  335. static_cast<const ARMBaseTargetMachine &>(TM);
  336. // 'Q' should correspond to the low order register and 'R' to the high
  337. // order register. Whether this corresponds to the upper or lower half
  338. // depends on the endianess mode.
  339. if (ExtraCode[0] == 'Q')
  340. FirstHalf = ATM.isLittleEndian();
  341. else
  342. // ExtraCode[0] == 'R'.
  343. FirstHalf = !ATM.isLittleEndian();
  344. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  345. if (InlineAsm::hasRegClassConstraint(Flags, RC) &&
  346. ARM::GPRPairRegClass.hasSubClassEq(TRI->getRegClass(RC))) {
  347. if (NumVals != 1)
  348. return true;
  349. const MachineOperand &MO = MI->getOperand(OpNum);
  350. if (!MO.isReg())
  351. return true;
  352. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  353. Register Reg =
  354. TRI->getSubReg(MO.getReg(), FirstHalf ? ARM::gsub_0 : ARM::gsub_1);
  355. O << ARMInstPrinter::getRegisterName(Reg);
  356. return false;
  357. }
  358. if (NumVals != 2)
  359. return true;
  360. unsigned RegOp = FirstHalf ? OpNum : OpNum + 1;
  361. if (RegOp >= MI->getNumOperands())
  362. return true;
  363. const MachineOperand &MO = MI->getOperand(RegOp);
  364. if (!MO.isReg())
  365. return true;
  366. Register Reg = MO.getReg();
  367. O << ARMInstPrinter::getRegisterName(Reg);
  368. return false;
  369. }
  370. case 'e': // The low doubleword register of a NEON quad register.
  371. case 'f': { // The high doubleword register of a NEON quad register.
  372. if (!MI->getOperand(OpNum).isReg())
  373. return true;
  374. Register Reg = MI->getOperand(OpNum).getReg();
  375. if (!ARM::QPRRegClass.contains(Reg))
  376. return true;
  377. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  378. Register SubReg =
  379. TRI->getSubReg(Reg, ExtraCode[0] == 'e' ? ARM::dsub_0 : ARM::dsub_1);
  380. O << ARMInstPrinter::getRegisterName(SubReg);
  381. return false;
  382. }
  383. // This modifier is not yet supported.
  384. case 'h': // A range of VFP/NEON registers suitable for VLD1/VST1.
  385. return true;
  386. case 'H': { // The highest-numbered register of a pair.
  387. const MachineOperand &MO = MI->getOperand(OpNum);
  388. if (!MO.isReg())
  389. return true;
  390. const MachineFunction &MF = *MI->getParent()->getParent();
  391. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  392. Register Reg = MO.getReg();
  393. if(!ARM::GPRPairRegClass.contains(Reg))
  394. return false;
  395. Reg = TRI->getSubReg(Reg, ARM::gsub_1);
  396. O << ARMInstPrinter::getRegisterName(Reg);
  397. return false;
  398. }
  399. }
  400. }
  401. printOperand(MI, OpNum, O);
  402. return false;
  403. }
  404. bool ARMAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
  405. unsigned OpNum, const char *ExtraCode,
  406. raw_ostream &O) {
  407. // Does this asm operand have a single letter operand modifier?
  408. if (ExtraCode && ExtraCode[0]) {
  409. if (ExtraCode[1] != 0) return true; // Unknown modifier.
  410. switch (ExtraCode[0]) {
  411. case 'A': // A memory operand for a VLD1/VST1 instruction.
  412. default: return true; // Unknown modifier.
  413. case 'm': // The base register of a memory operand.
  414. if (!MI->getOperand(OpNum).isReg())
  415. return true;
  416. O << ARMInstPrinter::getRegisterName(MI->getOperand(OpNum).getReg());
  417. return false;
  418. }
  419. }
  420. const MachineOperand &MO = MI->getOperand(OpNum);
  421. assert(MO.isReg() && "unexpected inline asm memory operand");
  422. O << "[" << ARMInstPrinter::getRegisterName(MO.getReg()) << "]";
  423. return false;
  424. }
  425. static bool isThumb(const MCSubtargetInfo& STI) {
  426. return STI.getFeatureBits()[ARM::ModeThumb];
  427. }
  428. void ARMAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
  429. const MCSubtargetInfo *EndInfo) const {
  430. // If either end mode is unknown (EndInfo == NULL) or different than
  431. // the start mode, then restore the start mode.
  432. const bool WasThumb = isThumb(StartInfo);
  433. if (!EndInfo || WasThumb != isThumb(*EndInfo)) {
  434. OutStreamer->emitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
  435. }
  436. }
  437. void ARMAsmPrinter::emitStartOfAsmFile(Module &M) {
  438. const Triple &TT = TM.getTargetTriple();
  439. // Use unified assembler syntax.
  440. OutStreamer->emitAssemblerFlag(MCAF_SyntaxUnified);
  441. // Emit ARM Build Attributes
  442. if (TT.isOSBinFormatELF())
  443. emitAttributes();
  444. // Use the triple's architecture and subarchitecture to determine
  445. // if we're thumb for the purposes of the top level code16 assembler
  446. // flag.
  447. if (!M.getModuleInlineAsm().empty() && TT.isThumb())
  448. OutStreamer->emitAssemblerFlag(MCAF_Code16);
  449. }
  450. static void
  451. emitNonLazySymbolPointer(MCStreamer &OutStreamer, MCSymbol *StubLabel,
  452. MachineModuleInfoImpl::StubValueTy &MCSym) {
  453. // L_foo$stub:
  454. OutStreamer.emitLabel(StubLabel);
  455. // .indirect_symbol _foo
  456. OutStreamer.emitSymbolAttribute(MCSym.getPointer(), MCSA_IndirectSymbol);
  457. if (MCSym.getInt())
  458. // External to current translation unit.
  459. OutStreamer.emitIntValue(0, 4/*size*/);
  460. else
  461. // Internal to current translation unit.
  462. //
  463. // When we place the LSDA into the TEXT section, the type info
  464. // pointers need to be indirect and pc-rel. We accomplish this by
  465. // using NLPs; however, sometimes the types are local to the file.
  466. // We need to fill in the value for the NLP in those cases.
  467. OutStreamer.emitValue(
  468. MCSymbolRefExpr::create(MCSym.getPointer(), OutStreamer.getContext()),
  469. 4 /*size*/);
  470. }
  471. void ARMAsmPrinter::emitEndOfAsmFile(Module &M) {
  472. const Triple &TT = TM.getTargetTriple();
  473. if (TT.isOSBinFormatMachO()) {
  474. // All darwin targets use mach-o.
  475. const TargetLoweringObjectFileMachO &TLOFMacho =
  476. static_cast<const TargetLoweringObjectFileMachO &>(getObjFileLowering());
  477. MachineModuleInfoMachO &MMIMacho =
  478. MMI->getObjFileInfo<MachineModuleInfoMachO>();
  479. // Output non-lazy-pointers for external and common global variables.
  480. MachineModuleInfoMachO::SymbolListTy Stubs = MMIMacho.GetGVStubList();
  481. if (!Stubs.empty()) {
  482. // Switch with ".non_lazy_symbol_pointer" directive.
  483. OutStreamer->SwitchSection(TLOFMacho.getNonLazySymbolPointerSection());
  484. emitAlignment(Align(4));
  485. for (auto &Stub : Stubs)
  486. emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
  487. Stubs.clear();
  488. OutStreamer->AddBlankLine();
  489. }
  490. Stubs = MMIMacho.GetThreadLocalGVStubList();
  491. if (!Stubs.empty()) {
  492. // Switch with ".non_lazy_symbol_pointer" directive.
  493. OutStreamer->SwitchSection(TLOFMacho.getThreadLocalPointerSection());
  494. emitAlignment(Align(4));
  495. for (auto &Stub : Stubs)
  496. emitNonLazySymbolPointer(*OutStreamer, Stub.first, Stub.second);
  497. Stubs.clear();
  498. OutStreamer->AddBlankLine();
  499. }
  500. // Funny Darwin hack: This flag tells the linker that no global symbols
  501. // contain code that falls through to other global symbols (e.g. the obvious
  502. // implementation of multiple entry points). If this doesn't occur, the
  503. // linker can safely perform dead code stripping. Since LLVM never
  504. // generates code that does this, it is always safe to set.
  505. OutStreamer->emitAssemblerFlag(MCAF_SubsectionsViaSymbols);
  506. }
  507. // The last attribute to be emitted is ABI_optimization_goals
  508. MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
  509. ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
  510. if (OptimizationGoals > 0 &&
  511. (Subtarget->isTargetAEABI() || Subtarget->isTargetGNUAEABI() ||
  512. Subtarget->isTargetMuslAEABI()))
  513. ATS.emitAttribute(ARMBuildAttrs::ABI_optimization_goals, OptimizationGoals);
  514. OptimizationGoals = -1;
  515. ATS.finishAttributeSection();
  516. }
  517. //===----------------------------------------------------------------------===//
  518. // Helper routines for emitStartOfAsmFile() and emitEndOfAsmFile()
  519. // FIXME:
  520. // The following seem like one-off assembler flags, but they actually need
  521. // to appear in the .ARM.attributes section in ELF.
  522. // Instead of subclassing the MCELFStreamer, we do the work here.
  523. // Returns true if all functions have the same function attribute value.
  524. // It also returns true when the module has no functions.
  525. static bool checkFunctionsAttributeConsistency(const Module &M, StringRef Attr,
  526. StringRef Value) {
  527. return !any_of(M, [&](const Function &F) {
  528. return F.getFnAttribute(Attr).getValueAsString() != Value;
  529. });
  530. }
  531. // Returns true if all functions have the same denormal mode.
  532. // It also returns true when the module has no functions.
  533. static bool checkDenormalAttributeConsistency(const Module &M,
  534. StringRef Attr,
  535. DenormalMode Value) {
  536. return !any_of(M, [&](const Function &F) {
  537. StringRef AttrVal = F.getFnAttribute(Attr).getValueAsString();
  538. return parseDenormalFPAttribute(AttrVal) != Value;
  539. });
  540. }
  541. void ARMAsmPrinter::emitAttributes() {
  542. MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
  543. ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
  544. ATS.emitTextAttribute(ARMBuildAttrs::conformance, "2.09");
  545. ATS.switchVendor("aeabi");
  546. // Compute ARM ELF Attributes based on the default subtarget that
  547. // we'd have constructed. The existing ARM behavior isn't LTO clean
  548. // anyhow.
  549. // FIXME: For ifunc related functions we could iterate over and look
  550. // for a feature string that doesn't match the default one.
  551. const Triple &TT = TM.getTargetTriple();
  552. StringRef CPU = TM.getTargetCPU();
  553. StringRef FS = TM.getTargetFeatureString();
  554. std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
  555. if (!FS.empty()) {
  556. if (!ArchFS.empty())
  557. ArchFS = (Twine(ArchFS) + "," + FS).str();
  558. else
  559. ArchFS = std::string(FS);
  560. }
  561. const ARMBaseTargetMachine &ATM =
  562. static_cast<const ARMBaseTargetMachine &>(TM);
  563. const ARMSubtarget STI(TT, std::string(CPU), ArchFS, ATM,
  564. ATM.isLittleEndian());
  565. // Emit build attributes for the available hardware.
  566. ATS.emitTargetAttributes(STI);
  567. // RW data addressing.
  568. if (isPositionIndependent()) {
  569. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
  570. ARMBuildAttrs::AddressRWPCRel);
  571. } else if (STI.isRWPI()) {
  572. // RWPI specific attributes.
  573. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RW_data,
  574. ARMBuildAttrs::AddressRWSBRel);
  575. }
  576. // RO data addressing.
  577. if (isPositionIndependent() || STI.isROPI()) {
  578. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_RO_data,
  579. ARMBuildAttrs::AddressROPCRel);
  580. }
  581. // GOT use.
  582. if (isPositionIndependent()) {
  583. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
  584. ARMBuildAttrs::AddressGOT);
  585. } else {
  586. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_GOT_use,
  587. ARMBuildAttrs::AddressDirect);
  588. }
  589. // Set FP Denormals.
  590. if (checkDenormalAttributeConsistency(*MMI->getModule(), "denormal-fp-math",
  591. DenormalMode::getPreserveSign()))
  592. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
  593. ARMBuildAttrs::PreserveFPSign);
  594. else if (checkDenormalAttributeConsistency(*MMI->getModule(),
  595. "denormal-fp-math",
  596. DenormalMode::getPositiveZero()))
  597. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
  598. ARMBuildAttrs::PositiveZero);
  599. else if (!TM.Options.UnsafeFPMath)
  600. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
  601. ARMBuildAttrs::IEEEDenormals);
  602. else {
  603. if (!STI.hasVFP2Base()) {
  604. // When the target doesn't have an FPU (by design or
  605. // intention), the assumptions made on the software support
  606. // mirror that of the equivalent hardware support *if it
  607. // existed*. For v7 and better we indicate that denormals are
  608. // flushed preserving sign, and for V6 we indicate that
  609. // denormals are flushed to positive zero.
  610. if (STI.hasV7Ops())
  611. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
  612. ARMBuildAttrs::PreserveFPSign);
  613. } else if (STI.hasVFP3Base()) {
  614. // In VFPv4, VFPv4U, VFPv3, or VFPv3U, it is preserved. That is,
  615. // the sign bit of the zero matches the sign bit of the input or
  616. // result that is being flushed to zero.
  617. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_denormal,
  618. ARMBuildAttrs::PreserveFPSign);
  619. }
  620. // For VFPv2 implementations it is implementation defined as
  621. // to whether denormals are flushed to positive zero or to
  622. // whatever the sign of zero is (ARM v7AR ARM 2.7.5). Historically
  623. // LLVM has chosen to flush this to positive zero (most likely for
  624. // GCC compatibility), so that's the chosen value here (the
  625. // absence of its emission implies zero).
  626. }
  627. // Set FP exceptions and rounding
  628. if (checkFunctionsAttributeConsistency(*MMI->getModule(),
  629. "no-trapping-math", "true") ||
  630. TM.Options.NoTrappingFPMath)
  631. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions,
  632. ARMBuildAttrs::Not_Allowed);
  633. else if (!TM.Options.UnsafeFPMath) {
  634. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_exceptions, ARMBuildAttrs::Allowed);
  635. // If the user has permitted this code to choose the IEEE 754
  636. // rounding at run-time, emit the rounding attribute.
  637. if (TM.Options.HonorSignDependentRoundingFPMathOption)
  638. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_rounding, ARMBuildAttrs::Allowed);
  639. }
  640. // TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath is the
  641. // equivalent of GCC's -ffinite-math-only flag.
  642. if (TM.Options.NoInfsFPMath && TM.Options.NoNaNsFPMath)
  643. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
  644. ARMBuildAttrs::Allowed);
  645. else
  646. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_number_model,
  647. ARMBuildAttrs::AllowIEEE754);
  648. // FIXME: add more flags to ARMBuildAttributes.h
  649. // 8-bytes alignment stuff.
  650. ATS.emitAttribute(ARMBuildAttrs::ABI_align_needed, 1);
  651. ATS.emitAttribute(ARMBuildAttrs::ABI_align_preserved, 1);
  652. // Hard float. Use both S and D registers and conform to AAPCS-VFP.
  653. if (STI.isAAPCS_ABI() && TM.Options.FloatABIType == FloatABI::Hard)
  654. ATS.emitAttribute(ARMBuildAttrs::ABI_VFP_args, ARMBuildAttrs::HardFPAAPCS);
  655. // FIXME: To support emitting this build attribute as GCC does, the
  656. // -mfp16-format option and associated plumbing must be
  657. // supported. For now the __fp16 type is exposed by default, so this
  658. // attribute should be emitted with value 1.
  659. ATS.emitAttribute(ARMBuildAttrs::ABI_FP_16bit_format,
  660. ARMBuildAttrs::FP16FormatIEEE);
  661. if (MMI) {
  662. if (const Module *SourceModule = MMI->getModule()) {
  663. // ABI_PCS_wchar_t to indicate wchar_t width
  664. // FIXME: There is no way to emit value 0 (wchar_t prohibited).
  665. if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>(
  666. SourceModule->getModuleFlag("wchar_size"))) {
  667. int WCharWidth = WCharWidthValue->getZExtValue();
  668. assert((WCharWidth == 2 || WCharWidth == 4) &&
  669. "wchar_t width must be 2 or 4 bytes");
  670. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_wchar_t, WCharWidth);
  671. }
  672. // ABI_enum_size to indicate enum width
  673. // FIXME: There is no way to emit value 0 (enums prohibited) or value 3
  674. // (all enums contain a value needing 32 bits to encode).
  675. if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>(
  676. SourceModule->getModuleFlag("min_enum_size"))) {
  677. int EnumWidth = EnumWidthValue->getZExtValue();
  678. assert((EnumWidth == 1 || EnumWidth == 4) &&
  679. "Minimum enum width must be 1 or 4 bytes");
  680. int EnumBuildAttr = EnumWidth == 1 ? 1 : 2;
  681. ATS.emitAttribute(ARMBuildAttrs::ABI_enum_size, EnumBuildAttr);
  682. }
  683. auto *PACValue = mdconst::extract_or_null<ConstantInt>(
  684. SourceModule->getModuleFlag("sign-return-address"));
  685. if (PACValue && PACValue->getZExtValue() == 1) {
  686. // If "+pacbti" is used as an architecture extension,
  687. // Tag_PAC_extension is emitted in
  688. // ARMTargetStreamer::emitTargetAttributes().
  689. if (!STI.hasPACBTI()) {
  690. ATS.emitAttribute(ARMBuildAttrs::PAC_extension,
  691. ARMBuildAttrs::AllowPACInNOPSpace);
  692. }
  693. ATS.emitAttribute(ARMBuildAttrs::PACRET_use, ARMBuildAttrs::PACRETUsed);
  694. }
  695. auto *BTIValue = mdconst::extract_or_null<ConstantInt>(
  696. SourceModule->getModuleFlag("branch-target-enforcement"));
  697. if (BTIValue && BTIValue->getZExtValue() == 1) {
  698. // If "+pacbti" is used as an architecture extension,
  699. // Tag_BTI_extension is emitted in
  700. // ARMTargetStreamer::emitTargetAttributes().
  701. if (!STI.hasPACBTI()) {
  702. ATS.emitAttribute(ARMBuildAttrs::BTI_extension,
  703. ARMBuildAttrs::AllowBTIInNOPSpace);
  704. }
  705. ATS.emitAttribute(ARMBuildAttrs::BTI_use, ARMBuildAttrs::BTIUsed);
  706. }
  707. }
  708. }
  709. // We currently do not support using R9 as the TLS pointer.
  710. if (STI.isRWPI())
  711. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
  712. ARMBuildAttrs::R9IsSB);
  713. else if (STI.isR9Reserved())
  714. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
  715. ARMBuildAttrs::R9Reserved);
  716. else
  717. ATS.emitAttribute(ARMBuildAttrs::ABI_PCS_R9_use,
  718. ARMBuildAttrs::R9IsGPR);
  719. }
  720. //===----------------------------------------------------------------------===//
  721. static MCSymbol *getBFLabel(StringRef Prefix, unsigned FunctionNumber,
  722. unsigned LabelId, MCContext &Ctx) {
  723. MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
  724. + "BF" + Twine(FunctionNumber) + "_" + Twine(LabelId));
  725. return Label;
  726. }
  727. static MCSymbol *getPICLabel(StringRef Prefix, unsigned FunctionNumber,
  728. unsigned LabelId, MCContext &Ctx) {
  729. MCSymbol *Label = Ctx.getOrCreateSymbol(Twine(Prefix)
  730. + "PC" + Twine(FunctionNumber) + "_" + Twine(LabelId));
  731. return Label;
  732. }
  733. static MCSymbolRefExpr::VariantKind
  734. getModifierVariantKind(ARMCP::ARMCPModifier Modifier) {
  735. switch (Modifier) {
  736. case ARMCP::no_modifier:
  737. return MCSymbolRefExpr::VK_None;
  738. case ARMCP::TLSGD:
  739. return MCSymbolRefExpr::VK_TLSGD;
  740. case ARMCP::TPOFF:
  741. return MCSymbolRefExpr::VK_TPOFF;
  742. case ARMCP::GOTTPOFF:
  743. return MCSymbolRefExpr::VK_GOTTPOFF;
  744. case ARMCP::SBREL:
  745. return MCSymbolRefExpr::VK_ARM_SBREL;
  746. case ARMCP::GOT_PREL:
  747. return MCSymbolRefExpr::VK_ARM_GOT_PREL;
  748. case ARMCP::SECREL:
  749. return MCSymbolRefExpr::VK_SECREL;
  750. }
  751. llvm_unreachable("Invalid ARMCPModifier!");
  752. }
  753. MCSymbol *ARMAsmPrinter::GetARMGVSymbol(const GlobalValue *GV,
  754. unsigned char TargetFlags) {
  755. if (Subtarget->isTargetMachO()) {
  756. bool IsIndirect =
  757. (TargetFlags & ARMII::MO_NONLAZY) && Subtarget->isGVIndirectSymbol(GV);
  758. if (!IsIndirect)
  759. return getSymbol(GV);
  760. // FIXME: Remove this when Darwin transition to @GOT like syntax.
  761. MCSymbol *MCSym = getSymbolWithGlobalValueBase(GV, "$non_lazy_ptr");
  762. MachineModuleInfoMachO &MMIMachO =
  763. MMI->getObjFileInfo<MachineModuleInfoMachO>();
  764. MachineModuleInfoImpl::StubValueTy &StubSym =
  765. GV->isThreadLocal() ? MMIMachO.getThreadLocalGVStubEntry(MCSym)
  766. : MMIMachO.getGVStubEntry(MCSym);
  767. if (!StubSym.getPointer())
  768. StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV),
  769. !GV->hasInternalLinkage());
  770. return MCSym;
  771. } else if (Subtarget->isTargetCOFF()) {
  772. assert(Subtarget->isTargetWindows() &&
  773. "Windows is the only supported COFF target");
  774. bool IsIndirect =
  775. (TargetFlags & (ARMII::MO_DLLIMPORT | ARMII::MO_COFFSTUB));
  776. if (!IsIndirect)
  777. return getSymbol(GV);
  778. SmallString<128> Name;
  779. if (TargetFlags & ARMII::MO_DLLIMPORT)
  780. Name = "__imp_";
  781. else if (TargetFlags & ARMII::MO_COFFSTUB)
  782. Name = ".refptr.";
  783. getNameWithPrefix(Name, GV);
  784. MCSymbol *MCSym = OutContext.getOrCreateSymbol(Name);
  785. if (TargetFlags & ARMII::MO_COFFSTUB) {
  786. MachineModuleInfoCOFF &MMICOFF =
  787. MMI->getObjFileInfo<MachineModuleInfoCOFF>();
  788. MachineModuleInfoImpl::StubValueTy &StubSym =
  789. MMICOFF.getGVStubEntry(MCSym);
  790. if (!StubSym.getPointer())
  791. StubSym = MachineModuleInfoImpl::StubValueTy(getSymbol(GV), true);
  792. }
  793. return MCSym;
  794. } else if (Subtarget->isTargetELF()) {
  795. return getSymbol(GV);
  796. }
  797. llvm_unreachable("unexpected target");
  798. }
  799. void ARMAsmPrinter::emitMachineConstantPoolValue(
  800. MachineConstantPoolValue *MCPV) {
  801. const DataLayout &DL = getDataLayout();
  802. int Size = DL.getTypeAllocSize(MCPV->getType());
  803. ARMConstantPoolValue *ACPV = static_cast<ARMConstantPoolValue*>(MCPV);
  804. if (ACPV->isPromotedGlobal()) {
  805. // This constant pool entry is actually a global whose storage has been
  806. // promoted into the constant pool. This global may be referenced still
  807. // by debug information, and due to the way AsmPrinter is set up, the debug
  808. // info is immutable by the time we decide to promote globals to constant
  809. // pools. Because of this, we need to ensure we emit a symbol for the global
  810. // with private linkage (the default) so debug info can refer to it.
  811. //
  812. // However, if this global is promoted into several functions we must ensure
  813. // we don't try and emit duplicate symbols!
  814. auto *ACPC = cast<ARMConstantPoolConstant>(ACPV);
  815. for (const auto *GV : ACPC->promotedGlobals()) {
  816. if (!EmittedPromotedGlobalLabels.count(GV)) {
  817. MCSymbol *GVSym = getSymbol(GV);
  818. OutStreamer->emitLabel(GVSym);
  819. EmittedPromotedGlobalLabels.insert(GV);
  820. }
  821. }
  822. return emitGlobalConstant(DL, ACPC->getPromotedGlobalInit());
  823. }
  824. MCSymbol *MCSym;
  825. if (ACPV->isLSDA()) {
  826. MCSym = getMBBExceptionSym(MF->front());
  827. } else if (ACPV->isBlockAddress()) {
  828. const BlockAddress *BA =
  829. cast<ARMConstantPoolConstant>(ACPV)->getBlockAddress();
  830. MCSym = GetBlockAddressSymbol(BA);
  831. } else if (ACPV->isGlobalValue()) {
  832. const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV();
  833. // On Darwin, const-pool entries may get the "FOO$non_lazy_ptr" mangling, so
  834. // flag the global as MO_NONLAZY.
  835. unsigned char TF = Subtarget->isTargetMachO() ? ARMII::MO_NONLAZY : 0;
  836. MCSym = GetARMGVSymbol(GV, TF);
  837. } else if (ACPV->isMachineBasicBlock()) {
  838. const MachineBasicBlock *MBB = cast<ARMConstantPoolMBB>(ACPV)->getMBB();
  839. MCSym = MBB->getSymbol();
  840. } else {
  841. assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
  842. auto Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol();
  843. MCSym = GetExternalSymbolSymbol(Sym);
  844. }
  845. // Create an MCSymbol for the reference.
  846. const MCExpr *Expr =
  847. MCSymbolRefExpr::create(MCSym, getModifierVariantKind(ACPV->getModifier()),
  848. OutContext);
  849. if (ACPV->getPCAdjustment()) {
  850. MCSymbol *PCLabel =
  851. getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
  852. ACPV->getLabelId(), OutContext);
  853. const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
  854. PCRelExpr =
  855. MCBinaryExpr::createAdd(PCRelExpr,
  856. MCConstantExpr::create(ACPV->getPCAdjustment(),
  857. OutContext),
  858. OutContext);
  859. if (ACPV->mustAddCurrentAddress()) {
  860. // We want "(<expr> - .)", but MC doesn't have a concept of the '.'
  861. // label, so just emit a local label end reference that instead.
  862. MCSymbol *DotSym = OutContext.createTempSymbol();
  863. OutStreamer->emitLabel(DotSym);
  864. const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
  865. PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
  866. }
  867. Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
  868. }
  869. OutStreamer->emitValue(Expr, Size);
  870. }
  871. void ARMAsmPrinter::emitJumpTableAddrs(const MachineInstr *MI) {
  872. const MachineOperand &MO1 = MI->getOperand(1);
  873. unsigned JTI = MO1.getIndex();
  874. // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
  875. // ARM mode tables.
  876. emitAlignment(Align(4));
  877. // Emit a label for the jump table.
  878. MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
  879. OutStreamer->emitLabel(JTISymbol);
  880. // Mark the jump table as data-in-code.
  881. OutStreamer->emitDataRegion(MCDR_DataRegionJT32);
  882. // Emit each entry of the table.
  883. const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
  884. const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
  885. const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
  886. for (MachineBasicBlock *MBB : JTBBs) {
  887. // Construct an MCExpr for the entry. We want a value of the form:
  888. // (BasicBlockAddr - TableBeginAddr)
  889. //
  890. // For example, a table with entries jumping to basic blocks BB0 and BB1
  891. // would look like:
  892. // LJTI_0_0:
  893. // .word (LBB0 - LJTI_0_0)
  894. // .word (LBB1 - LJTI_0_0)
  895. const MCExpr *Expr = MCSymbolRefExpr::create(MBB->getSymbol(), OutContext);
  896. if (isPositionIndependent() || Subtarget->isROPI())
  897. Expr = MCBinaryExpr::createSub(Expr, MCSymbolRefExpr::create(JTISymbol,
  898. OutContext),
  899. OutContext);
  900. // If we're generating a table of Thumb addresses in static relocation
  901. // model, we need to add one to keep interworking correctly.
  902. else if (AFI->isThumbFunction())
  903. Expr = MCBinaryExpr::createAdd(Expr, MCConstantExpr::create(1,OutContext),
  904. OutContext);
  905. OutStreamer->emitValue(Expr, 4);
  906. }
  907. // Mark the end of jump table data-in-code region.
  908. OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
  909. }
  910. void ARMAsmPrinter::emitJumpTableInsts(const MachineInstr *MI) {
  911. const MachineOperand &MO1 = MI->getOperand(1);
  912. unsigned JTI = MO1.getIndex();
  913. // Make sure the Thumb jump table is 4-byte aligned. This will be a nop for
  914. // ARM mode tables.
  915. emitAlignment(Align(4));
  916. // Emit a label for the jump table.
  917. MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
  918. OutStreamer->emitLabel(JTISymbol);
  919. // Emit each entry of the table.
  920. const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
  921. const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
  922. const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
  923. for (MachineBasicBlock *MBB : JTBBs) {
  924. const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
  925. OutContext);
  926. // If this isn't a TBB or TBH, the entries are direct branch instructions.
  927. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2B)
  928. .addExpr(MBBSymbolExpr)
  929. .addImm(ARMCC::AL)
  930. .addReg(0));
  931. }
  932. }
  933. void ARMAsmPrinter::emitJumpTableTBInst(const MachineInstr *MI,
  934. unsigned OffsetWidth) {
  935. assert((OffsetWidth == 1 || OffsetWidth == 2) && "invalid tbb/tbh width");
  936. const MachineOperand &MO1 = MI->getOperand(1);
  937. unsigned JTI = MO1.getIndex();
  938. if (Subtarget->isThumb1Only())
  939. emitAlignment(Align(4));
  940. MCSymbol *JTISymbol = GetARMJTIPICJumpTableLabel(JTI);
  941. OutStreamer->emitLabel(JTISymbol);
  942. // Emit each entry of the table.
  943. const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
  944. const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
  945. const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
  946. // Mark the jump table as data-in-code.
  947. OutStreamer->emitDataRegion(OffsetWidth == 1 ? MCDR_DataRegionJT8
  948. : MCDR_DataRegionJT16);
  949. for (auto MBB : JTBBs) {
  950. const MCExpr *MBBSymbolExpr = MCSymbolRefExpr::create(MBB->getSymbol(),
  951. OutContext);
  952. // Otherwise it's an offset from the dispatch instruction. Construct an
  953. // MCExpr for the entry. We want a value of the form:
  954. // (BasicBlockAddr - TBBInstAddr + 4) / 2
  955. //
  956. // For example, a TBB table with entries jumping to basic blocks BB0 and BB1
  957. // would look like:
  958. // LJTI_0_0:
  959. // .byte (LBB0 - (LCPI0_0 + 4)) / 2
  960. // .byte (LBB1 - (LCPI0_0 + 4)) / 2
  961. // where LCPI0_0 is a label defined just before the TBB instruction using
  962. // this table.
  963. MCSymbol *TBInstPC = GetCPISymbol(MI->getOperand(0).getImm());
  964. const MCExpr *Expr = MCBinaryExpr::createAdd(
  965. MCSymbolRefExpr::create(TBInstPC, OutContext),
  966. MCConstantExpr::create(4, OutContext), OutContext);
  967. Expr = MCBinaryExpr::createSub(MBBSymbolExpr, Expr, OutContext);
  968. Expr = MCBinaryExpr::createDiv(Expr, MCConstantExpr::create(2, OutContext),
  969. OutContext);
  970. OutStreamer->emitValue(Expr, OffsetWidth);
  971. }
  972. // Mark the end of jump table data-in-code region. 32-bit offsets use
  973. // actual branch instructions here, so we don't mark those as a data-region
  974. // at all.
  975. OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
  976. // Make sure the next instruction is 2-byte aligned.
  977. emitAlignment(Align(2));
  978. }
  979. void ARMAsmPrinter::EmitUnwindingInstruction(const MachineInstr *MI) {
  980. assert(MI->getFlag(MachineInstr::FrameSetup) &&
  981. "Only instruction which are involved into frame setup code are allowed");
  982. MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
  983. ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
  984. const MachineFunction &MF = *MI->getParent()->getParent();
  985. const TargetRegisterInfo *TargetRegInfo =
  986. MF.getSubtarget().getRegisterInfo();
  987. const MachineRegisterInfo &MachineRegInfo = MF.getRegInfo();
  988. Register FramePtr = TargetRegInfo->getFrameRegister(MF);
  989. unsigned Opc = MI->getOpcode();
  990. unsigned SrcReg, DstReg;
  991. switch (Opc) {
  992. case ARM::tPUSH:
  993. // special case: tPUSH does not have src/dst regs.
  994. SrcReg = DstReg = ARM::SP;
  995. break;
  996. case ARM::tLDRpci:
  997. case ARM::t2MOVi16:
  998. case ARM::t2MOVTi16:
  999. // special cases:
  1000. // 1) for Thumb1 code we sometimes materialize the constant via constpool
  1001. // load.
  1002. // 2) for Thumb2 execute only code we materialize the constant via
  1003. // immediate constants in 2 separate instructions (MOVW/MOVT).
  1004. SrcReg = ~0U;
  1005. DstReg = MI->getOperand(0).getReg();
  1006. break;
  1007. default:
  1008. SrcReg = MI->getOperand(1).getReg();
  1009. DstReg = MI->getOperand(0).getReg();
  1010. break;
  1011. }
  1012. // Try to figure out the unwinding opcode out of src / dst regs.
  1013. if (MI->mayStore()) {
  1014. // Register saves.
  1015. assert(DstReg == ARM::SP &&
  1016. "Only stack pointer as a destination reg is supported");
  1017. SmallVector<unsigned, 4> RegList;
  1018. // Skip src & dst reg, and pred ops.
  1019. unsigned StartOp = 2 + 2;
  1020. // Use all the operands.
  1021. unsigned NumOffset = 0;
  1022. // Amount of SP adjustment folded into a push, before the
  1023. // registers are stored (pad at higher addresses).
  1024. unsigned PadBefore = 0;
  1025. // Amount of SP adjustment folded into a push, after the
  1026. // registers are stored (pad at lower addresses).
  1027. unsigned PadAfter = 0;
  1028. switch (Opc) {
  1029. default:
  1030. MI->print(errs());
  1031. llvm_unreachable("Unsupported opcode for unwinding information");
  1032. case ARM::tPUSH:
  1033. // Special case here: no src & dst reg, but two extra imp ops.
  1034. StartOp = 2; NumOffset = 2;
  1035. LLVM_FALLTHROUGH;
  1036. case ARM::STMDB_UPD:
  1037. case ARM::t2STMDB_UPD:
  1038. case ARM::VSTMDDB_UPD:
  1039. assert(SrcReg == ARM::SP &&
  1040. "Only stack pointer as a source reg is supported");
  1041. for (unsigned i = StartOp, NumOps = MI->getNumOperands() - NumOffset;
  1042. i != NumOps; ++i) {
  1043. const MachineOperand &MO = MI->getOperand(i);
  1044. // Actually, there should never be any impdef stuff here. Skip it
  1045. // temporary to workaround PR11902.
  1046. if (MO.isImplicit())
  1047. continue;
  1048. // Registers, pushed as a part of folding an SP update into the
  1049. // push instruction are marked as undef and should not be
  1050. // restored when unwinding, because the function can modify the
  1051. // corresponding stack slots.
  1052. if (MO.isUndef()) {
  1053. assert(RegList.empty() &&
  1054. "Pad registers must come before restored ones");
  1055. unsigned Width =
  1056. TargetRegInfo->getRegSizeInBits(MO.getReg(), MachineRegInfo) / 8;
  1057. PadAfter += Width;
  1058. continue;
  1059. }
  1060. // Check for registers that are remapped (for a Thumb1 prologue that
  1061. // saves high registers).
  1062. Register Reg = MO.getReg();
  1063. if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(Reg))
  1064. Reg = RemappedReg;
  1065. RegList.push_back(Reg);
  1066. }
  1067. break;
  1068. case ARM::STR_PRE_IMM:
  1069. case ARM::STR_PRE_REG:
  1070. case ARM::t2STR_PRE:
  1071. assert(MI->getOperand(2).getReg() == ARM::SP &&
  1072. "Only stack pointer as a source reg is supported");
  1073. if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg))
  1074. SrcReg = RemappedReg;
  1075. RegList.push_back(SrcReg);
  1076. break;
  1077. case ARM::t2STRD_PRE:
  1078. assert(MI->getOperand(3).getReg() == ARM::SP &&
  1079. "Only stack pointer as a source reg is supported");
  1080. SrcReg = MI->getOperand(1).getReg();
  1081. if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg))
  1082. SrcReg = RemappedReg;
  1083. RegList.push_back(SrcReg);
  1084. SrcReg = MI->getOperand(2).getReg();
  1085. if (unsigned RemappedReg = AFI->EHPrologueRemappedRegs.lookup(SrcReg))
  1086. SrcReg = RemappedReg;
  1087. RegList.push_back(SrcReg);
  1088. PadBefore = -MI->getOperand(4).getImm() - 8;
  1089. break;
  1090. }
  1091. if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
  1092. if (PadBefore)
  1093. ATS.emitPad(PadBefore);
  1094. ATS.emitRegSave(RegList, Opc == ARM::VSTMDDB_UPD);
  1095. // Account for the SP adjustment, folded into the push.
  1096. if (PadAfter)
  1097. ATS.emitPad(PadAfter);
  1098. }
  1099. } else {
  1100. // Changes of stack / frame pointer.
  1101. if (SrcReg == ARM::SP) {
  1102. int64_t Offset = 0;
  1103. switch (Opc) {
  1104. default:
  1105. MI->print(errs());
  1106. llvm_unreachable("Unsupported opcode for unwinding information");
  1107. case ARM::MOVr:
  1108. case ARM::tMOVr:
  1109. Offset = 0;
  1110. break;
  1111. case ARM::ADDri:
  1112. case ARM::t2ADDri:
  1113. case ARM::t2ADDri12:
  1114. case ARM::t2ADDspImm:
  1115. case ARM::t2ADDspImm12:
  1116. Offset = -MI->getOperand(2).getImm();
  1117. break;
  1118. case ARM::SUBri:
  1119. case ARM::t2SUBri:
  1120. case ARM::t2SUBri12:
  1121. case ARM::t2SUBspImm:
  1122. case ARM::t2SUBspImm12:
  1123. Offset = MI->getOperand(2).getImm();
  1124. break;
  1125. case ARM::tSUBspi:
  1126. Offset = MI->getOperand(2).getImm()*4;
  1127. break;
  1128. case ARM::tADDspi:
  1129. case ARM::tADDrSPi:
  1130. Offset = -MI->getOperand(2).getImm()*4;
  1131. break;
  1132. case ARM::tADDhirr:
  1133. Offset =
  1134. -AFI->EHPrologueOffsetInRegs.lookup(MI->getOperand(2).getReg());
  1135. break;
  1136. }
  1137. if (MAI->getExceptionHandlingType() == ExceptionHandling::ARM) {
  1138. if (DstReg == FramePtr && FramePtr != ARM::SP)
  1139. // Set-up of the frame pointer. Positive values correspond to "add"
  1140. // instruction.
  1141. ATS.emitSetFP(FramePtr, ARM::SP, -Offset);
  1142. else if (DstReg == ARM::SP) {
  1143. // Change of SP by an offset. Positive values correspond to "sub"
  1144. // instruction.
  1145. ATS.emitPad(Offset);
  1146. } else {
  1147. // Move of SP to a register. Positive values correspond to an "add"
  1148. // instruction.
  1149. ATS.emitMovSP(DstReg, -Offset);
  1150. }
  1151. }
  1152. } else if (DstReg == ARM::SP) {
  1153. MI->print(errs());
  1154. llvm_unreachable("Unsupported opcode for unwinding information");
  1155. } else {
  1156. int64_t Offset = 0;
  1157. switch (Opc) {
  1158. case ARM::tMOVr:
  1159. // If a Thumb1 function spills r8-r11, we copy the values to low
  1160. // registers before pushing them. Record the copy so we can emit the
  1161. // correct ".save" later.
  1162. AFI->EHPrologueRemappedRegs[DstReg] = SrcReg;
  1163. break;
  1164. case ARM::tLDRpci: {
  1165. // Grab the constpool index and check, whether it corresponds to
  1166. // original or cloned constpool entry.
  1167. unsigned CPI = MI->getOperand(1).getIndex();
  1168. const MachineConstantPool *MCP = MF.getConstantPool();
  1169. if (CPI >= MCP->getConstants().size())
  1170. CPI = AFI->getOriginalCPIdx(CPI);
  1171. assert(CPI != -1U && "Invalid constpool index");
  1172. // Derive the actual offset.
  1173. const MachineConstantPoolEntry &CPE = MCP->getConstants()[CPI];
  1174. assert(!CPE.isMachineConstantPoolEntry() && "Invalid constpool entry");
  1175. Offset = cast<ConstantInt>(CPE.Val.ConstVal)->getSExtValue();
  1176. AFI->EHPrologueOffsetInRegs[DstReg] = Offset;
  1177. break;
  1178. }
  1179. case ARM::t2MOVi16:
  1180. Offset = MI->getOperand(1).getImm();
  1181. AFI->EHPrologueOffsetInRegs[DstReg] = Offset;
  1182. break;
  1183. case ARM::t2MOVTi16:
  1184. Offset = MI->getOperand(2).getImm();
  1185. AFI->EHPrologueOffsetInRegs[DstReg] |= (Offset << 16);
  1186. break;
  1187. case ARM::t2PAC:
  1188. case ARM::t2PACBTI:
  1189. AFI->EHPrologueRemappedRegs[ARM::R12] = ARM::RA_AUTH_CODE;
  1190. break;
  1191. default:
  1192. MI->print(errs());
  1193. llvm_unreachable("Unsupported opcode for unwinding information");
  1194. }
  1195. }
  1196. }
  1197. }
  1198. // Simple pseudo-instructions have their lowering (with expansion to real
  1199. // instructions) auto-generated.
  1200. #include "ARMGenMCPseudoLowering.inc"
  1201. void ARMAsmPrinter::emitInstruction(const MachineInstr *MI) {
  1202. const DataLayout &DL = getDataLayout();
  1203. MCTargetStreamer &TS = *OutStreamer->getTargetStreamer();
  1204. ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
  1205. // If we just ended a constant pool, mark it as such.
  1206. if (InConstantPool && MI->getOpcode() != ARM::CONSTPOOL_ENTRY) {
  1207. OutStreamer->emitDataRegion(MCDR_DataRegionEnd);
  1208. InConstantPool = false;
  1209. }
  1210. // Emit unwinding stuff for frame-related instructions
  1211. if (Subtarget->isTargetEHABICompatible() &&
  1212. MI->getFlag(MachineInstr::FrameSetup))
  1213. EmitUnwindingInstruction(MI);
  1214. // Do any auto-generated pseudo lowerings.
  1215. if (emitPseudoExpansionLowering(*OutStreamer, MI))
  1216. return;
  1217. assert(!convertAddSubFlagsOpcode(MI->getOpcode()) &&
  1218. "Pseudo flag setting opcode should be expanded early");
  1219. // Check for manual lowerings.
  1220. unsigned Opc = MI->getOpcode();
  1221. switch (Opc) {
  1222. case ARM::t2MOVi32imm: llvm_unreachable("Should be lowered by thumb2it pass");
  1223. case ARM::DBG_VALUE: llvm_unreachable("Should be handled by generic printing");
  1224. case ARM::LEApcrel:
  1225. case ARM::tLEApcrel:
  1226. case ARM::t2LEApcrel: {
  1227. // FIXME: Need to also handle globals and externals
  1228. MCSymbol *CPISymbol = GetCPISymbol(MI->getOperand(1).getIndex());
  1229. EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
  1230. ARM::t2LEApcrel ? ARM::t2ADR
  1231. : (MI->getOpcode() == ARM::tLEApcrel ? ARM::tADR
  1232. : ARM::ADR))
  1233. .addReg(MI->getOperand(0).getReg())
  1234. .addExpr(MCSymbolRefExpr::create(CPISymbol, OutContext))
  1235. // Add predicate operands.
  1236. .addImm(MI->getOperand(2).getImm())
  1237. .addReg(MI->getOperand(3).getReg()));
  1238. return;
  1239. }
  1240. case ARM::LEApcrelJT:
  1241. case ARM::tLEApcrelJT:
  1242. case ARM::t2LEApcrelJT: {
  1243. MCSymbol *JTIPICSymbol =
  1244. GetARMJTIPICJumpTableLabel(MI->getOperand(1).getIndex());
  1245. EmitToStreamer(*OutStreamer, MCInstBuilder(MI->getOpcode() ==
  1246. ARM::t2LEApcrelJT ? ARM::t2ADR
  1247. : (MI->getOpcode() == ARM::tLEApcrelJT ? ARM::tADR
  1248. : ARM::ADR))
  1249. .addReg(MI->getOperand(0).getReg())
  1250. .addExpr(MCSymbolRefExpr::create(JTIPICSymbol, OutContext))
  1251. // Add predicate operands.
  1252. .addImm(MI->getOperand(2).getImm())
  1253. .addReg(MI->getOperand(3).getReg()));
  1254. return;
  1255. }
  1256. // Darwin call instructions are just normal call instructions with different
  1257. // clobber semantics (they clobber R9).
  1258. case ARM::BX_CALL: {
  1259. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
  1260. .addReg(ARM::LR)
  1261. .addReg(ARM::PC)
  1262. // Add predicate operands.
  1263. .addImm(ARMCC::AL)
  1264. .addReg(0)
  1265. // Add 's' bit operand (always reg0 for this)
  1266. .addReg(0));
  1267. assert(Subtarget->hasV4TOps());
  1268. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX)
  1269. .addReg(MI->getOperand(0).getReg()));
  1270. return;
  1271. }
  1272. case ARM::tBX_CALL: {
  1273. if (Subtarget->hasV5TOps())
  1274. llvm_unreachable("Expected BLX to be selected for v5t+");
  1275. // On ARM v4t, when doing a call from thumb mode, we need to ensure
  1276. // that the saved lr has its LSB set correctly (the arch doesn't
  1277. // have blx).
  1278. // So here we generate a bl to a small jump pad that does bx rN.
  1279. // The jump pads are emitted after the function body.
  1280. Register TReg = MI->getOperand(0).getReg();
  1281. MCSymbol *TRegSym = nullptr;
  1282. for (std::pair<unsigned, MCSymbol *> &TIP : ThumbIndirectPads) {
  1283. if (TIP.first == TReg) {
  1284. TRegSym = TIP.second;
  1285. break;
  1286. }
  1287. }
  1288. if (!TRegSym) {
  1289. TRegSym = OutContext.createTempSymbol();
  1290. ThumbIndirectPads.push_back(std::make_pair(TReg, TRegSym));
  1291. }
  1292. // Create a link-saving branch to the Reg Indirect Jump Pad.
  1293. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBL)
  1294. // Predicate comes first here.
  1295. .addImm(ARMCC::AL).addReg(0)
  1296. .addExpr(MCSymbolRefExpr::create(TRegSym, OutContext)));
  1297. return;
  1298. }
  1299. case ARM::BMOVPCRX_CALL: {
  1300. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
  1301. .addReg(ARM::LR)
  1302. .addReg(ARM::PC)
  1303. // Add predicate operands.
  1304. .addImm(ARMCC::AL)
  1305. .addReg(0)
  1306. // Add 's' bit operand (always reg0 for this)
  1307. .addReg(0));
  1308. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
  1309. .addReg(ARM::PC)
  1310. .addReg(MI->getOperand(0).getReg())
  1311. // Add predicate operands.
  1312. .addImm(ARMCC::AL)
  1313. .addReg(0)
  1314. // Add 's' bit operand (always reg0 for this)
  1315. .addReg(0));
  1316. return;
  1317. }
  1318. case ARM::BMOVPCB_CALL: {
  1319. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVr)
  1320. .addReg(ARM::LR)
  1321. .addReg(ARM::PC)
  1322. // Add predicate operands.
  1323. .addImm(ARMCC::AL)
  1324. .addReg(0)
  1325. // Add 's' bit operand (always reg0 for this)
  1326. .addReg(0));
  1327. const MachineOperand &Op = MI->getOperand(0);
  1328. const GlobalValue *GV = Op.getGlobal();
  1329. const unsigned TF = Op.getTargetFlags();
  1330. MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
  1331. const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
  1332. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::Bcc)
  1333. .addExpr(GVSymExpr)
  1334. // Add predicate operands.
  1335. .addImm(ARMCC::AL)
  1336. .addReg(0));
  1337. return;
  1338. }
  1339. case ARM::MOVi16_ga_pcrel:
  1340. case ARM::t2MOVi16_ga_pcrel: {
  1341. MCInst TmpInst;
  1342. TmpInst.setOpcode(Opc == ARM::MOVi16_ga_pcrel? ARM::MOVi16 : ARM::t2MOVi16);
  1343. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
  1344. unsigned TF = MI->getOperand(1).getTargetFlags();
  1345. const GlobalValue *GV = MI->getOperand(1).getGlobal();
  1346. MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
  1347. const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
  1348. MCSymbol *LabelSym =
  1349. getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
  1350. MI->getOperand(2).getImm(), OutContext);
  1351. const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
  1352. unsigned PCAdj = (Opc == ARM::MOVi16_ga_pcrel) ? 8 : 4;
  1353. const MCExpr *PCRelExpr =
  1354. ARMMCExpr::createLower16(MCBinaryExpr::createSub(GVSymExpr,
  1355. MCBinaryExpr::createAdd(LabelSymExpr,
  1356. MCConstantExpr::create(PCAdj, OutContext),
  1357. OutContext), OutContext), OutContext);
  1358. TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
  1359. // Add predicate operands.
  1360. TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
  1361. TmpInst.addOperand(MCOperand::createReg(0));
  1362. // Add 's' bit operand (always reg0 for this)
  1363. TmpInst.addOperand(MCOperand::createReg(0));
  1364. EmitToStreamer(*OutStreamer, TmpInst);
  1365. return;
  1366. }
  1367. case ARM::MOVTi16_ga_pcrel:
  1368. case ARM::t2MOVTi16_ga_pcrel: {
  1369. MCInst TmpInst;
  1370. TmpInst.setOpcode(Opc == ARM::MOVTi16_ga_pcrel
  1371. ? ARM::MOVTi16 : ARM::t2MOVTi16);
  1372. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
  1373. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
  1374. unsigned TF = MI->getOperand(2).getTargetFlags();
  1375. const GlobalValue *GV = MI->getOperand(2).getGlobal();
  1376. MCSymbol *GVSym = GetARMGVSymbol(GV, TF);
  1377. const MCExpr *GVSymExpr = MCSymbolRefExpr::create(GVSym, OutContext);
  1378. MCSymbol *LabelSym =
  1379. getPICLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
  1380. MI->getOperand(3).getImm(), OutContext);
  1381. const MCExpr *LabelSymExpr= MCSymbolRefExpr::create(LabelSym, OutContext);
  1382. unsigned PCAdj = (Opc == ARM::MOVTi16_ga_pcrel) ? 8 : 4;
  1383. const MCExpr *PCRelExpr =
  1384. ARMMCExpr::createUpper16(MCBinaryExpr::createSub(GVSymExpr,
  1385. MCBinaryExpr::createAdd(LabelSymExpr,
  1386. MCConstantExpr::create(PCAdj, OutContext),
  1387. OutContext), OutContext), OutContext);
  1388. TmpInst.addOperand(MCOperand::createExpr(PCRelExpr));
  1389. // Add predicate operands.
  1390. TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
  1391. TmpInst.addOperand(MCOperand::createReg(0));
  1392. // Add 's' bit operand (always reg0 for this)
  1393. TmpInst.addOperand(MCOperand::createReg(0));
  1394. EmitToStreamer(*OutStreamer, TmpInst);
  1395. return;
  1396. }
  1397. case ARM::t2BFi:
  1398. case ARM::t2BFic:
  1399. case ARM::t2BFLi:
  1400. case ARM::t2BFr:
  1401. case ARM::t2BFLr: {
  1402. // This is a Branch Future instruction.
  1403. const MCExpr *BranchLabel = MCSymbolRefExpr::create(
  1404. getBFLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
  1405. MI->getOperand(0).getIndex(), OutContext),
  1406. OutContext);
  1407. auto MCInst = MCInstBuilder(Opc).addExpr(BranchLabel);
  1408. if (MI->getOperand(1).isReg()) {
  1409. // For BFr/BFLr
  1410. MCInst.addReg(MI->getOperand(1).getReg());
  1411. } else {
  1412. // For BFi/BFLi/BFic
  1413. const MCExpr *BranchTarget;
  1414. if (MI->getOperand(1).isMBB())
  1415. BranchTarget = MCSymbolRefExpr::create(
  1416. MI->getOperand(1).getMBB()->getSymbol(), OutContext);
  1417. else if (MI->getOperand(1).isGlobal()) {
  1418. const GlobalValue *GV = MI->getOperand(1).getGlobal();
  1419. BranchTarget = MCSymbolRefExpr::create(
  1420. GetARMGVSymbol(GV, MI->getOperand(1).getTargetFlags()), OutContext);
  1421. } else if (MI->getOperand(1).isSymbol()) {
  1422. BranchTarget = MCSymbolRefExpr::create(
  1423. GetExternalSymbolSymbol(MI->getOperand(1).getSymbolName()),
  1424. OutContext);
  1425. } else
  1426. llvm_unreachable("Unhandled operand kind in Branch Future instruction");
  1427. MCInst.addExpr(BranchTarget);
  1428. }
  1429. if (Opc == ARM::t2BFic) {
  1430. const MCExpr *ElseLabel = MCSymbolRefExpr::create(
  1431. getBFLabel(DL.getPrivateGlobalPrefix(), getFunctionNumber(),
  1432. MI->getOperand(2).getIndex(), OutContext),
  1433. OutContext);
  1434. MCInst.addExpr(ElseLabel);
  1435. MCInst.addImm(MI->getOperand(3).getImm());
  1436. } else {
  1437. MCInst.addImm(MI->getOperand(2).getImm())
  1438. .addReg(MI->getOperand(3).getReg());
  1439. }
  1440. EmitToStreamer(*OutStreamer, MCInst);
  1441. return;
  1442. }
  1443. case ARM::t2BF_LabelPseudo: {
  1444. // This is a pseudo op for a label used by a branch future instruction
  1445. // Emit the label.
  1446. OutStreamer->emitLabel(getBFLabel(DL.getPrivateGlobalPrefix(),
  1447. getFunctionNumber(),
  1448. MI->getOperand(0).getIndex(), OutContext));
  1449. return;
  1450. }
  1451. case ARM::tPICADD: {
  1452. // This is a pseudo op for a label + instruction sequence, which looks like:
  1453. // LPC0:
  1454. // add r0, pc
  1455. // This adds the address of LPC0 to r0.
  1456. // Emit the label.
  1457. OutStreamer->emitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
  1458. getFunctionNumber(),
  1459. MI->getOperand(2).getImm(), OutContext));
  1460. // Form and emit the add.
  1461. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
  1462. .addReg(MI->getOperand(0).getReg())
  1463. .addReg(MI->getOperand(0).getReg())
  1464. .addReg(ARM::PC)
  1465. // Add predicate operands.
  1466. .addImm(ARMCC::AL)
  1467. .addReg(0));
  1468. return;
  1469. }
  1470. case ARM::PICADD: {
  1471. // This is a pseudo op for a label + instruction sequence, which looks like:
  1472. // LPC0:
  1473. // add r0, pc, r0
  1474. // This adds the address of LPC0 to r0.
  1475. // Emit the label.
  1476. OutStreamer->emitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
  1477. getFunctionNumber(),
  1478. MI->getOperand(2).getImm(), OutContext));
  1479. // Form and emit the add.
  1480. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr)
  1481. .addReg(MI->getOperand(0).getReg())
  1482. .addReg(ARM::PC)
  1483. .addReg(MI->getOperand(1).getReg())
  1484. // Add predicate operands.
  1485. .addImm(MI->getOperand(3).getImm())
  1486. .addReg(MI->getOperand(4).getReg())
  1487. // Add 's' bit operand (always reg0 for this)
  1488. .addReg(0));
  1489. return;
  1490. }
  1491. case ARM::PICSTR:
  1492. case ARM::PICSTRB:
  1493. case ARM::PICSTRH:
  1494. case ARM::PICLDR:
  1495. case ARM::PICLDRB:
  1496. case ARM::PICLDRH:
  1497. case ARM::PICLDRSB:
  1498. case ARM::PICLDRSH: {
  1499. // This is a pseudo op for a label + instruction sequence, which looks like:
  1500. // LPC0:
  1501. // OP r0, [pc, r0]
  1502. // The LCP0 label is referenced by a constant pool entry in order to get
  1503. // a PC-relative address at the ldr instruction.
  1504. // Emit the label.
  1505. OutStreamer->emitLabel(getPICLabel(DL.getPrivateGlobalPrefix(),
  1506. getFunctionNumber(),
  1507. MI->getOperand(2).getImm(), OutContext));
  1508. // Form and emit the load
  1509. unsigned Opcode;
  1510. switch (MI->getOpcode()) {
  1511. default:
  1512. llvm_unreachable("Unexpected opcode!");
  1513. case ARM::PICSTR: Opcode = ARM::STRrs; break;
  1514. case ARM::PICSTRB: Opcode = ARM::STRBrs; break;
  1515. case ARM::PICSTRH: Opcode = ARM::STRH; break;
  1516. case ARM::PICLDR: Opcode = ARM::LDRrs; break;
  1517. case ARM::PICLDRB: Opcode = ARM::LDRBrs; break;
  1518. case ARM::PICLDRH: Opcode = ARM::LDRH; break;
  1519. case ARM::PICLDRSB: Opcode = ARM::LDRSB; break;
  1520. case ARM::PICLDRSH: Opcode = ARM::LDRSH; break;
  1521. }
  1522. EmitToStreamer(*OutStreamer, MCInstBuilder(Opcode)
  1523. .addReg(MI->getOperand(0).getReg())
  1524. .addReg(ARM::PC)
  1525. .addReg(MI->getOperand(1).getReg())
  1526. .addImm(0)
  1527. // Add predicate operands.
  1528. .addImm(MI->getOperand(3).getImm())
  1529. .addReg(MI->getOperand(4).getReg()));
  1530. return;
  1531. }
  1532. case ARM::CONSTPOOL_ENTRY: {
  1533. if (Subtarget->genExecuteOnly())
  1534. llvm_unreachable("execute-only should not generate constant pools");
  1535. /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool
  1536. /// in the function. The first operand is the ID# for this instruction, the
  1537. /// second is the index into the MachineConstantPool that this is, the third
  1538. /// is the size in bytes of this constant pool entry.
  1539. /// The required alignment is specified on the basic block holding this MI.
  1540. unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
  1541. unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
  1542. // If this is the first entry of the pool, mark it.
  1543. if (!InConstantPool) {
  1544. OutStreamer->emitDataRegion(MCDR_DataRegion);
  1545. InConstantPool = true;
  1546. }
  1547. OutStreamer->emitLabel(GetCPISymbol(LabelId));
  1548. const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
  1549. if (MCPE.isMachineConstantPoolEntry())
  1550. emitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
  1551. else
  1552. emitGlobalConstant(DL, MCPE.Val.ConstVal);
  1553. return;
  1554. }
  1555. case ARM::JUMPTABLE_ADDRS:
  1556. emitJumpTableAddrs(MI);
  1557. return;
  1558. case ARM::JUMPTABLE_INSTS:
  1559. emitJumpTableInsts(MI);
  1560. return;
  1561. case ARM::JUMPTABLE_TBB:
  1562. case ARM::JUMPTABLE_TBH:
  1563. emitJumpTableTBInst(MI, MI->getOpcode() == ARM::JUMPTABLE_TBB ? 1 : 2);
  1564. return;
  1565. case ARM::t2BR_JT: {
  1566. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
  1567. .addReg(ARM::PC)
  1568. .addReg(MI->getOperand(0).getReg())
  1569. // Add predicate operands.
  1570. .addImm(ARMCC::AL)
  1571. .addReg(0));
  1572. return;
  1573. }
  1574. case ARM::t2TBB_JT:
  1575. case ARM::t2TBH_JT: {
  1576. unsigned Opc = MI->getOpcode() == ARM::t2TBB_JT ? ARM::t2TBB : ARM::t2TBH;
  1577. // Lower and emit the PC label, then the instruction itself.
  1578. OutStreamer->emitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
  1579. EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
  1580. .addReg(MI->getOperand(0).getReg())
  1581. .addReg(MI->getOperand(1).getReg())
  1582. // Add predicate operands.
  1583. .addImm(ARMCC::AL)
  1584. .addReg(0));
  1585. return;
  1586. }
  1587. case ARM::tTBB_JT:
  1588. case ARM::tTBH_JT: {
  1589. bool Is8Bit = MI->getOpcode() == ARM::tTBB_JT;
  1590. Register Base = MI->getOperand(0).getReg();
  1591. Register Idx = MI->getOperand(1).getReg();
  1592. assert(MI->getOperand(1).isKill() && "We need the index register as scratch!");
  1593. // Multiply up idx if necessary.
  1594. if (!Is8Bit)
  1595. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
  1596. .addReg(Idx)
  1597. .addReg(ARM::CPSR)
  1598. .addReg(Idx)
  1599. .addImm(1)
  1600. // Add predicate operands.
  1601. .addImm(ARMCC::AL)
  1602. .addReg(0));
  1603. if (Base == ARM::PC) {
  1604. // TBB [base, idx] =
  1605. // ADDS idx, idx, base
  1606. // LDRB idx, [idx, #4] ; or LDRH if TBH
  1607. // LSLS idx, #1
  1608. // ADDS pc, pc, idx
  1609. // When using PC as the base, it's important that there is no padding
  1610. // between the last ADDS and the start of the jump table. The jump table
  1611. // is 4-byte aligned, so we ensure we're 4 byte aligned here too.
  1612. //
  1613. // FIXME: Ideally we could vary the LDRB index based on the padding
  1614. // between the sequence and jump table, however that relies on MCExprs
  1615. // for load indexes which are currently not supported.
  1616. OutStreamer->emitCodeAlignment(4, &getSubtargetInfo());
  1617. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
  1618. .addReg(Idx)
  1619. .addReg(Idx)
  1620. .addReg(Base)
  1621. // Add predicate operands.
  1622. .addImm(ARMCC::AL)
  1623. .addReg(0));
  1624. unsigned Opc = Is8Bit ? ARM::tLDRBi : ARM::tLDRHi;
  1625. EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
  1626. .addReg(Idx)
  1627. .addReg(Idx)
  1628. .addImm(Is8Bit ? 4 : 2)
  1629. // Add predicate operands.
  1630. .addImm(ARMCC::AL)
  1631. .addReg(0));
  1632. } else {
  1633. // TBB [base, idx] =
  1634. // LDRB idx, [base, idx] ; or LDRH if TBH
  1635. // LSLS idx, #1
  1636. // ADDS pc, pc, idx
  1637. unsigned Opc = Is8Bit ? ARM::tLDRBr : ARM::tLDRHr;
  1638. EmitToStreamer(*OutStreamer, MCInstBuilder(Opc)
  1639. .addReg(Idx)
  1640. .addReg(Base)
  1641. .addReg(Idx)
  1642. // Add predicate operands.
  1643. .addImm(ARMCC::AL)
  1644. .addReg(0));
  1645. }
  1646. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLSLri)
  1647. .addReg(Idx)
  1648. .addReg(ARM::CPSR)
  1649. .addReg(Idx)
  1650. .addImm(1)
  1651. // Add predicate operands.
  1652. .addImm(ARMCC::AL)
  1653. .addReg(0));
  1654. OutStreamer->emitLabel(GetCPISymbol(MI->getOperand(3).getImm()));
  1655. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDhirr)
  1656. .addReg(ARM::PC)
  1657. .addReg(ARM::PC)
  1658. .addReg(Idx)
  1659. // Add predicate operands.
  1660. .addImm(ARMCC::AL)
  1661. .addReg(0));
  1662. return;
  1663. }
  1664. case ARM::tBR_JTr:
  1665. case ARM::BR_JTr: {
  1666. // mov pc, target
  1667. MCInst TmpInst;
  1668. unsigned Opc = MI->getOpcode() == ARM::BR_JTr ?
  1669. ARM::MOVr : ARM::tMOVr;
  1670. TmpInst.setOpcode(Opc);
  1671. TmpInst.addOperand(MCOperand::createReg(ARM::PC));
  1672. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
  1673. // Add predicate operands.
  1674. TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
  1675. TmpInst.addOperand(MCOperand::createReg(0));
  1676. // Add 's' bit operand (always reg0 for this)
  1677. if (Opc == ARM::MOVr)
  1678. TmpInst.addOperand(MCOperand::createReg(0));
  1679. EmitToStreamer(*OutStreamer, TmpInst);
  1680. return;
  1681. }
  1682. case ARM::BR_JTm_i12: {
  1683. // ldr pc, target
  1684. MCInst TmpInst;
  1685. TmpInst.setOpcode(ARM::LDRi12);
  1686. TmpInst.addOperand(MCOperand::createReg(ARM::PC));
  1687. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
  1688. TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
  1689. // Add predicate operands.
  1690. TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
  1691. TmpInst.addOperand(MCOperand::createReg(0));
  1692. EmitToStreamer(*OutStreamer, TmpInst);
  1693. return;
  1694. }
  1695. case ARM::BR_JTm_rs: {
  1696. // ldr pc, target
  1697. MCInst TmpInst;
  1698. TmpInst.setOpcode(ARM::LDRrs);
  1699. TmpInst.addOperand(MCOperand::createReg(ARM::PC));
  1700. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
  1701. TmpInst.addOperand(MCOperand::createReg(MI->getOperand(1).getReg()));
  1702. TmpInst.addOperand(MCOperand::createImm(MI->getOperand(2).getImm()));
  1703. // Add predicate operands.
  1704. TmpInst.addOperand(MCOperand::createImm(ARMCC::AL));
  1705. TmpInst.addOperand(MCOperand::createReg(0));
  1706. EmitToStreamer(*OutStreamer, TmpInst);
  1707. return;
  1708. }
  1709. case ARM::BR_JTadd: {
  1710. // add pc, target, idx
  1711. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDrr)
  1712. .addReg(ARM::PC)
  1713. .addReg(MI->getOperand(0).getReg())
  1714. .addReg(MI->getOperand(1).getReg())
  1715. // Add predicate operands.
  1716. .addImm(ARMCC::AL)
  1717. .addReg(0)
  1718. // Add 's' bit operand (always reg0 for this)
  1719. .addReg(0));
  1720. return;
  1721. }
  1722. case ARM::SPACE:
  1723. OutStreamer->emitZeros(MI->getOperand(1).getImm());
  1724. return;
  1725. case ARM::TRAP: {
  1726. // Non-Darwin binutils don't yet support the "trap" mnemonic.
  1727. // FIXME: Remove this special case when they do.
  1728. if (!Subtarget->isTargetMachO()) {
  1729. uint32_t Val = 0xe7ffdefeUL;
  1730. OutStreamer->AddComment("trap");
  1731. ATS.emitInst(Val);
  1732. return;
  1733. }
  1734. break;
  1735. }
  1736. case ARM::TRAPNaCl: {
  1737. uint32_t Val = 0xe7fedef0UL;
  1738. OutStreamer->AddComment("trap");
  1739. ATS.emitInst(Val);
  1740. return;
  1741. }
  1742. case ARM::tTRAP: {
  1743. // Non-Darwin binutils don't yet support the "trap" mnemonic.
  1744. // FIXME: Remove this special case when they do.
  1745. if (!Subtarget->isTargetMachO()) {
  1746. uint16_t Val = 0xdefe;
  1747. OutStreamer->AddComment("trap");
  1748. ATS.emitInst(Val, 'n');
  1749. return;
  1750. }
  1751. break;
  1752. }
  1753. case ARM::t2Int_eh_sjlj_setjmp:
  1754. case ARM::t2Int_eh_sjlj_setjmp_nofp:
  1755. case ARM::tInt_eh_sjlj_setjmp: {
  1756. // Two incoming args: GPR:$src, GPR:$val
  1757. // mov $val, pc
  1758. // adds $val, #7
  1759. // str $val, [$src, #4]
  1760. // movs r0, #0
  1761. // b LSJLJEH
  1762. // movs r0, #1
  1763. // LSJLJEH:
  1764. Register SrcReg = MI->getOperand(0).getReg();
  1765. Register ValReg = MI->getOperand(1).getReg();
  1766. MCSymbol *Label = OutContext.createTempSymbol("SJLJEH");
  1767. OutStreamer->AddComment("eh_setjmp begin");
  1768. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
  1769. .addReg(ValReg)
  1770. .addReg(ARM::PC)
  1771. // Predicate.
  1772. .addImm(ARMCC::AL)
  1773. .addReg(0));
  1774. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tADDi3)
  1775. .addReg(ValReg)
  1776. // 's' bit operand
  1777. .addReg(ARM::CPSR)
  1778. .addReg(ValReg)
  1779. .addImm(7)
  1780. // Predicate.
  1781. .addImm(ARMCC::AL)
  1782. .addReg(0));
  1783. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tSTRi)
  1784. .addReg(ValReg)
  1785. .addReg(SrcReg)
  1786. // The offset immediate is #4. The operand value is scaled by 4 for the
  1787. // tSTR instruction.
  1788. .addImm(1)
  1789. // Predicate.
  1790. .addImm(ARMCC::AL)
  1791. .addReg(0));
  1792. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
  1793. .addReg(ARM::R0)
  1794. .addReg(ARM::CPSR)
  1795. .addImm(0)
  1796. // Predicate.
  1797. .addImm(ARMCC::AL)
  1798. .addReg(0));
  1799. const MCExpr *SymbolExpr = MCSymbolRefExpr::create(Label, OutContext);
  1800. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tB)
  1801. .addExpr(SymbolExpr)
  1802. .addImm(ARMCC::AL)
  1803. .addReg(0));
  1804. OutStreamer->AddComment("eh_setjmp end");
  1805. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVi8)
  1806. .addReg(ARM::R0)
  1807. .addReg(ARM::CPSR)
  1808. .addImm(1)
  1809. // Predicate.
  1810. .addImm(ARMCC::AL)
  1811. .addReg(0));
  1812. OutStreamer->emitLabel(Label);
  1813. return;
  1814. }
  1815. case ARM::Int_eh_sjlj_setjmp_nofp:
  1816. case ARM::Int_eh_sjlj_setjmp: {
  1817. // Two incoming args: GPR:$src, GPR:$val
  1818. // add $val, pc, #8
  1819. // str $val, [$src, #+4]
  1820. // mov r0, #0
  1821. // add pc, pc, #0
  1822. // mov r0, #1
  1823. Register SrcReg = MI->getOperand(0).getReg();
  1824. Register ValReg = MI->getOperand(1).getReg();
  1825. OutStreamer->AddComment("eh_setjmp begin");
  1826. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri)
  1827. .addReg(ValReg)
  1828. .addReg(ARM::PC)
  1829. .addImm(8)
  1830. // Predicate.
  1831. .addImm(ARMCC::AL)
  1832. .addReg(0)
  1833. // 's' bit operand (always reg0 for this).
  1834. .addReg(0));
  1835. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::STRi12)
  1836. .addReg(ValReg)
  1837. .addReg(SrcReg)
  1838. .addImm(4)
  1839. // Predicate.
  1840. .addImm(ARMCC::AL)
  1841. .addReg(0));
  1842. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi)
  1843. .addReg(ARM::R0)
  1844. .addImm(0)
  1845. // Predicate.
  1846. .addImm(ARMCC::AL)
  1847. .addReg(0)
  1848. // 's' bit operand (always reg0 for this).
  1849. .addReg(0));
  1850. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::ADDri)
  1851. .addReg(ARM::PC)
  1852. .addReg(ARM::PC)
  1853. .addImm(0)
  1854. // Predicate.
  1855. .addImm(ARMCC::AL)
  1856. .addReg(0)
  1857. // 's' bit operand (always reg0 for this).
  1858. .addReg(0));
  1859. OutStreamer->AddComment("eh_setjmp end");
  1860. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::MOVi)
  1861. .addReg(ARM::R0)
  1862. .addImm(1)
  1863. // Predicate.
  1864. .addImm(ARMCC::AL)
  1865. .addReg(0)
  1866. // 's' bit operand (always reg0 for this).
  1867. .addReg(0));
  1868. return;
  1869. }
  1870. case ARM::Int_eh_sjlj_longjmp: {
  1871. // ldr sp, [$src, #8]
  1872. // ldr $scratch, [$src, #4]
  1873. // ldr r7, [$src]
  1874. // bx $scratch
  1875. Register SrcReg = MI->getOperand(0).getReg();
  1876. Register ScratchReg = MI->getOperand(1).getReg();
  1877. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
  1878. .addReg(ARM::SP)
  1879. .addReg(SrcReg)
  1880. .addImm(8)
  1881. // Predicate.
  1882. .addImm(ARMCC::AL)
  1883. .addReg(0));
  1884. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
  1885. .addReg(ScratchReg)
  1886. .addReg(SrcReg)
  1887. .addImm(4)
  1888. // Predicate.
  1889. .addImm(ARMCC::AL)
  1890. .addReg(0));
  1891. const MachineFunction &MF = *MI->getParent()->getParent();
  1892. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  1893. if (STI.isTargetDarwin() || STI.isTargetWindows()) {
  1894. // These platforms always use the same frame register
  1895. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
  1896. .addReg(STI.getFramePointerReg())
  1897. .addReg(SrcReg)
  1898. .addImm(0)
  1899. // Predicate.
  1900. .addImm(ARMCC::AL)
  1901. .addReg(0));
  1902. } else {
  1903. // If the calling code might use either R7 or R11 as
  1904. // frame pointer register, restore it into both.
  1905. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
  1906. .addReg(ARM::R7)
  1907. .addReg(SrcReg)
  1908. .addImm(0)
  1909. // Predicate.
  1910. .addImm(ARMCC::AL)
  1911. .addReg(0));
  1912. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::LDRi12)
  1913. .addReg(ARM::R11)
  1914. .addReg(SrcReg)
  1915. .addImm(0)
  1916. // Predicate.
  1917. .addImm(ARMCC::AL)
  1918. .addReg(0));
  1919. }
  1920. assert(Subtarget->hasV4TOps());
  1921. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::BX)
  1922. .addReg(ScratchReg)
  1923. // Predicate.
  1924. .addImm(ARMCC::AL)
  1925. .addReg(0));
  1926. return;
  1927. }
  1928. case ARM::tInt_eh_sjlj_longjmp: {
  1929. // ldr $scratch, [$src, #8]
  1930. // mov sp, $scratch
  1931. // ldr $scratch, [$src, #4]
  1932. // ldr r7, [$src]
  1933. // bx $scratch
  1934. Register SrcReg = MI->getOperand(0).getReg();
  1935. Register ScratchReg = MI->getOperand(1).getReg();
  1936. const MachineFunction &MF = *MI->getParent()->getParent();
  1937. const ARMSubtarget &STI = MF.getSubtarget<ARMSubtarget>();
  1938. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
  1939. .addReg(ScratchReg)
  1940. .addReg(SrcReg)
  1941. // The offset immediate is #8. The operand value is scaled by 4 for the
  1942. // tLDR instruction.
  1943. .addImm(2)
  1944. // Predicate.
  1945. .addImm(ARMCC::AL)
  1946. .addReg(0));
  1947. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tMOVr)
  1948. .addReg(ARM::SP)
  1949. .addReg(ScratchReg)
  1950. // Predicate.
  1951. .addImm(ARMCC::AL)
  1952. .addReg(0));
  1953. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
  1954. .addReg(ScratchReg)
  1955. .addReg(SrcReg)
  1956. .addImm(1)
  1957. // Predicate.
  1958. .addImm(ARMCC::AL)
  1959. .addReg(0));
  1960. if (STI.isTargetDarwin() || STI.isTargetWindows()) {
  1961. // These platforms always use the same frame register
  1962. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
  1963. .addReg(STI.getFramePointerReg())
  1964. .addReg(SrcReg)
  1965. .addImm(0)
  1966. // Predicate.
  1967. .addImm(ARMCC::AL)
  1968. .addReg(0));
  1969. } else {
  1970. // If the calling code might use either R7 or R11 as
  1971. // frame pointer register, restore it into both.
  1972. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
  1973. .addReg(ARM::R7)
  1974. .addReg(SrcReg)
  1975. .addImm(0)
  1976. // Predicate.
  1977. .addImm(ARMCC::AL)
  1978. .addReg(0));
  1979. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tLDRi)
  1980. .addReg(ARM::R11)
  1981. .addReg(SrcReg)
  1982. .addImm(0)
  1983. // Predicate.
  1984. .addImm(ARMCC::AL)
  1985. .addReg(0));
  1986. }
  1987. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::tBX)
  1988. .addReg(ScratchReg)
  1989. // Predicate.
  1990. .addImm(ARMCC::AL)
  1991. .addReg(0));
  1992. return;
  1993. }
  1994. case ARM::tInt_WIN_eh_sjlj_longjmp: {
  1995. // ldr.w r11, [$src, #0]
  1996. // ldr.w sp, [$src, #8]
  1997. // ldr.w pc, [$src, #4]
  1998. Register SrcReg = MI->getOperand(0).getReg();
  1999. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
  2000. .addReg(ARM::R11)
  2001. .addReg(SrcReg)
  2002. .addImm(0)
  2003. // Predicate
  2004. .addImm(ARMCC::AL)
  2005. .addReg(0));
  2006. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
  2007. .addReg(ARM::SP)
  2008. .addReg(SrcReg)
  2009. .addImm(8)
  2010. // Predicate
  2011. .addImm(ARMCC::AL)
  2012. .addReg(0));
  2013. EmitToStreamer(*OutStreamer, MCInstBuilder(ARM::t2LDRi12)
  2014. .addReg(ARM::PC)
  2015. .addReg(SrcReg)
  2016. .addImm(4)
  2017. // Predicate
  2018. .addImm(ARMCC::AL)
  2019. .addReg(0));
  2020. return;
  2021. }
  2022. case ARM::PATCHABLE_FUNCTION_ENTER:
  2023. LowerPATCHABLE_FUNCTION_ENTER(*MI);
  2024. return;
  2025. case ARM::PATCHABLE_FUNCTION_EXIT:
  2026. LowerPATCHABLE_FUNCTION_EXIT(*MI);
  2027. return;
  2028. case ARM::PATCHABLE_TAIL_CALL:
  2029. LowerPATCHABLE_TAIL_CALL(*MI);
  2030. return;
  2031. case ARM::SpeculationBarrierISBDSBEndBB: {
  2032. // Print DSB SYS + ISB
  2033. MCInst TmpInstDSB;
  2034. TmpInstDSB.setOpcode(ARM::DSB);
  2035. TmpInstDSB.addOperand(MCOperand::createImm(0xf));
  2036. EmitToStreamer(*OutStreamer, TmpInstDSB);
  2037. MCInst TmpInstISB;
  2038. TmpInstISB.setOpcode(ARM::ISB);
  2039. TmpInstISB.addOperand(MCOperand::createImm(0xf));
  2040. EmitToStreamer(*OutStreamer, TmpInstISB);
  2041. return;
  2042. }
  2043. case ARM::t2SpeculationBarrierISBDSBEndBB: {
  2044. // Print DSB SYS + ISB
  2045. MCInst TmpInstDSB;
  2046. TmpInstDSB.setOpcode(ARM::t2DSB);
  2047. TmpInstDSB.addOperand(MCOperand::createImm(0xf));
  2048. TmpInstDSB.addOperand(MCOperand::createImm(ARMCC::AL));
  2049. TmpInstDSB.addOperand(MCOperand::createReg(0));
  2050. EmitToStreamer(*OutStreamer, TmpInstDSB);
  2051. MCInst TmpInstISB;
  2052. TmpInstISB.setOpcode(ARM::t2ISB);
  2053. TmpInstISB.addOperand(MCOperand::createImm(0xf));
  2054. TmpInstISB.addOperand(MCOperand::createImm(ARMCC::AL));
  2055. TmpInstISB.addOperand(MCOperand::createReg(0));
  2056. EmitToStreamer(*OutStreamer, TmpInstISB);
  2057. return;
  2058. }
  2059. case ARM::SpeculationBarrierSBEndBB: {
  2060. // Print SB
  2061. MCInst TmpInstSB;
  2062. TmpInstSB.setOpcode(ARM::SB);
  2063. EmitToStreamer(*OutStreamer, TmpInstSB);
  2064. return;
  2065. }
  2066. case ARM::t2SpeculationBarrierSBEndBB: {
  2067. // Print SB
  2068. MCInst TmpInstSB;
  2069. TmpInstSB.setOpcode(ARM::t2SB);
  2070. EmitToStreamer(*OutStreamer, TmpInstSB);
  2071. return;
  2072. }
  2073. }
  2074. MCInst TmpInst;
  2075. LowerARMMachineInstrToMCInst(MI, TmpInst, *this);
  2076. EmitToStreamer(*OutStreamer, TmpInst);
  2077. }
  2078. //===----------------------------------------------------------------------===//
  2079. // Target Registry Stuff
  2080. //===----------------------------------------------------------------------===//
  2081. // Force static initialization.
  2082. extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMAsmPrinter() {
  2083. RegisterAsmPrinter<ARMAsmPrinter> X(getTheARMLETarget());
  2084. RegisterAsmPrinter<ARMAsmPrinter> Y(getTheARMBETarget());
  2085. RegisterAsmPrinter<ARMAsmPrinter> A(getTheThumbLETarget());
  2086. RegisterAsmPrinter<ARMAsmPrinter> B(getTheThumbBETarget());
  2087. }