ARMv7Assembler.h 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. /*
  2. * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
  3. * Copyright (C) 2010 University of Szeged
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef ARMAssembler_h
  27. #define ARMAssembler_h
  28. #if ENABLE(ASSEMBLER) && CPU(ARM_THUMB2)
  29. #include "AssemblerBuffer.h"
  30. #include <wtf/Assertions.h>
  31. #include <wtf/Vector.h>
  32. #include <stdint.h>
  33. #if ENABLE(JIT) && PLATFORM(MANX)
  34. #include <manx/Memblock.h>
  35. #endif
  36. namespace JSC {
  37. namespace ARMRegisters {
  38. typedef enum {
  39. r0,
  40. r1,
  41. r2,
  42. r3,
  43. r4,
  44. r5,
  45. r6,
  46. r7, wr = r7, // thumb work register
  47. r8,
  48. r9, sb = r9, // static base
  49. r10, sl = r10, // stack limit
  50. r11, fp = r11, // frame pointer
  51. r12, ip = r12,
  52. r13, sp = r13,
  53. r14, lr = r14,
  54. r15, pc = r15,
  55. } RegisterID;
  56. typedef enum {
  57. s0,
  58. s1,
  59. s2,
  60. s3,
  61. s4,
  62. s5,
  63. s6,
  64. s7,
  65. s8,
  66. s9,
  67. s10,
  68. s11,
  69. s12,
  70. s13,
  71. s14,
  72. s15,
  73. s16,
  74. s17,
  75. s18,
  76. s19,
  77. s20,
  78. s21,
  79. s22,
  80. s23,
  81. s24,
  82. s25,
  83. s26,
  84. s27,
  85. s28,
  86. s29,
  87. s30,
  88. s31,
  89. } FPSingleRegisterID;
  90. typedef enum {
  91. d0,
  92. d1,
  93. d2,
  94. d3,
  95. d4,
  96. d5,
  97. d6,
  98. d7,
  99. d8,
  100. d9,
  101. d10,
  102. d11,
  103. d12,
  104. d13,
  105. d14,
  106. d15,
  107. d16,
  108. d17,
  109. d18,
  110. d19,
  111. d20,
  112. d21,
  113. d22,
  114. d23,
  115. d24,
  116. d25,
  117. d26,
  118. d27,
  119. d28,
  120. d29,
  121. d30,
  122. d31,
  123. } FPDoubleRegisterID;
  124. typedef enum {
  125. q0,
  126. q1,
  127. q2,
  128. q3,
  129. q4,
  130. q5,
  131. q6,
  132. q7,
  133. q8,
  134. q9,
  135. q10,
  136. q11,
  137. q12,
  138. q13,
  139. q14,
  140. q15,
  141. q16,
  142. q17,
  143. q18,
  144. q19,
  145. q20,
  146. q21,
  147. q22,
  148. q23,
  149. q24,
  150. q25,
  151. q26,
  152. q27,
  153. q28,
  154. q29,
  155. q30,
  156. q31,
  157. } FPQuadRegisterID;
  158. inline FPSingleRegisterID asSingle(FPDoubleRegisterID reg)
  159. {
  160. ASSERT(reg < d16);
  161. return (FPSingleRegisterID)(reg << 1);
  162. }
  163. inline FPDoubleRegisterID asDouble(FPSingleRegisterID reg)
  164. {
  165. ASSERT(!(reg & 1));
  166. return (FPDoubleRegisterID)(reg >> 1);
  167. }
  168. }
  169. class ARMv7Assembler;
  170. class ARMThumbImmediate {
  171. friend class ARMv7Assembler;
  172. typedef uint8_t ThumbImmediateType;
  173. static const ThumbImmediateType TypeInvalid = 0;
  174. static const ThumbImmediateType TypeEncoded = 1;
  175. static const ThumbImmediateType TypeUInt16 = 2;
  176. typedef union {
  177. int16_t asInt;
  178. struct {
  179. unsigned imm8 : 8;
  180. unsigned imm3 : 3;
  181. unsigned i : 1;
  182. unsigned imm4 : 4;
  183. };
  184. // If this is an encoded immediate, then it may describe a shift, or a pattern.
  185. struct {
  186. unsigned shiftValue7 : 7;
  187. unsigned shiftAmount : 5;
  188. };
  189. struct {
  190. unsigned immediate : 8;
  191. unsigned pattern : 4;
  192. };
  193. } ThumbImmediateValue;
  194. // byte0 contains least significant bit; not using an array to make client code endian agnostic.
  195. typedef union {
  196. int32_t asInt;
  197. struct {
  198. uint8_t byte0;
  199. uint8_t byte1;
  200. uint8_t byte2;
  201. uint8_t byte3;
  202. };
  203. } PatternBytes;
  204. ALWAYS_INLINE static void countLeadingZerosPartial(uint32_t& value, int32_t& zeros, const int N)
  205. {
  206. if (value & ~((1 << N) - 1)) /* check for any of the top N bits (of 2N bits) are set */
  207. value >>= N; /* if any were set, lose the bottom N */
  208. else /* if none of the top N bits are set, */
  209. zeros += N; /* then we have identified N leading zeros */
  210. }
  211. static int32_t countLeadingZeros(uint32_t value)
  212. {
  213. if (!value)
  214. return 32;
  215. int32_t zeros = 0;
  216. countLeadingZerosPartial(value, zeros, 16);
  217. countLeadingZerosPartial(value, zeros, 8);
  218. countLeadingZerosPartial(value, zeros, 4);
  219. countLeadingZerosPartial(value, zeros, 2);
  220. countLeadingZerosPartial(value, zeros, 1);
  221. return zeros;
  222. }
  223. ARMThumbImmediate()
  224. : m_type(TypeInvalid)
  225. {
  226. m_value.asInt = 0;
  227. }
  228. ARMThumbImmediate(ThumbImmediateType type, ThumbImmediateValue value)
  229. : m_type(type)
  230. , m_value(value)
  231. {
  232. }
  233. ARMThumbImmediate(ThumbImmediateType type, uint16_t value)
  234. : m_type(TypeUInt16)
  235. {
  236. // Make sure this constructor is only reached with type TypeUInt16;
  237. // this extra parameter makes the code a little clearer by making it
  238. // explicit at call sites which type is being constructed
  239. ASSERT_UNUSED(type, type == TypeUInt16);
  240. m_value.asInt = value;
  241. }
  242. public:
  243. static ARMThumbImmediate makeEncodedImm(uint32_t value)
  244. {
  245. ThumbImmediateValue encoding;
  246. encoding.asInt = 0;
  247. // okay, these are easy.
  248. if (value < 256) {
  249. encoding.immediate = value;
  250. encoding.pattern = 0;
  251. return ARMThumbImmediate(TypeEncoded, encoding);
  252. }
  253. int32_t leadingZeros = countLeadingZeros(value);
  254. // if there were 24 or more leading zeros, then we'd have hit the (value < 256) case.
  255. ASSERT(leadingZeros < 24);
  256. // Given a number with bit fields Z:B:C, where count(Z)+count(B)+count(C) == 32,
  257. // Z are the bits known zero, B is the 8-bit immediate, C are the bits to check for
  258. // zero. count(B) == 8, so the count of bits to be checked is 24 - count(Z).
  259. int32_t rightShiftAmount = 24 - leadingZeros;
  260. if (value == ((value >> rightShiftAmount) << rightShiftAmount)) {
  261. // Shift the value down to the low byte position. The assign to
  262. // shiftValue7 drops the implicit top bit.
  263. encoding.shiftValue7 = value >> rightShiftAmount;
  264. // The endoded shift amount is the magnitude of a right rotate.
  265. encoding.shiftAmount = 8 + leadingZeros;
  266. return ARMThumbImmediate(TypeEncoded, encoding);
  267. }
  268. PatternBytes bytes;
  269. bytes.asInt = value;
  270. if ((bytes.byte0 == bytes.byte1) && (bytes.byte0 == bytes.byte2) && (bytes.byte0 == bytes.byte3)) {
  271. encoding.immediate = bytes.byte0;
  272. encoding.pattern = 3;
  273. return ARMThumbImmediate(TypeEncoded, encoding);
  274. }
  275. if ((bytes.byte0 == bytes.byte2) && !(bytes.byte1 | bytes.byte3)) {
  276. encoding.immediate = bytes.byte0;
  277. encoding.pattern = 1;
  278. return ARMThumbImmediate(TypeEncoded, encoding);
  279. }
  280. if ((bytes.byte1 == bytes.byte3) && !(bytes.byte0 | bytes.byte2)) {
  281. encoding.immediate = bytes.byte1;
  282. encoding.pattern = 2;
  283. return ARMThumbImmediate(TypeEncoded, encoding);
  284. }
  285. return ARMThumbImmediate();
  286. }
  287. static ARMThumbImmediate makeUInt12(int32_t value)
  288. {
  289. return (!(value & 0xfffff000))
  290. ? ARMThumbImmediate(TypeUInt16, (uint16_t)value)
  291. : ARMThumbImmediate();
  292. }
  293. static ARMThumbImmediate makeUInt12OrEncodedImm(int32_t value)
  294. {
  295. // If this is not a 12-bit unsigned it, try making an encoded immediate.
  296. return (!(value & 0xfffff000))
  297. ? ARMThumbImmediate(TypeUInt16, (uint16_t)value)
  298. : makeEncodedImm(value);
  299. }
  300. // The 'make' methods, above, return a !isValid() value if the argument
  301. // cannot be represented as the requested type. This methods is called
  302. // 'get' since the argument can always be represented.
  303. static ARMThumbImmediate makeUInt16(uint16_t value)
  304. {
  305. return ARMThumbImmediate(TypeUInt16, value);
  306. }
  307. bool isValid()
  308. {
  309. return m_type != TypeInvalid;
  310. }
  311. uint16_t asUInt16() const { return m_value.asInt; }
  312. // These methods rely on the format of encoded byte values.
  313. bool isUInt3() { return !(m_value.asInt & 0xfff8); }
  314. bool isUInt4() { return !(m_value.asInt & 0xfff0); }
  315. bool isUInt5() { return !(m_value.asInt & 0xffe0); }
  316. bool isUInt6() { return !(m_value.asInt & 0xffc0); }
  317. bool isUInt7() { return !(m_value.asInt & 0xff80); }
  318. bool isUInt8() { return !(m_value.asInt & 0xff00); }
  319. bool isUInt9() { return (m_type == TypeUInt16) && !(m_value.asInt & 0xfe00); }
  320. bool isUInt10() { return (m_type == TypeUInt16) && !(m_value.asInt & 0xfc00); }
  321. bool isUInt12() { return (m_type == TypeUInt16) && !(m_value.asInt & 0xf000); }
  322. bool isUInt16() { return m_type == TypeUInt16; }
  323. uint8_t getUInt3() { ASSERT(isUInt3()); return m_value.asInt; }
  324. uint8_t getUInt4() { ASSERT(isUInt4()); return m_value.asInt; }
  325. uint8_t getUInt5() { ASSERT(isUInt5()); return m_value.asInt; }
  326. uint8_t getUInt6() { ASSERT(isUInt6()); return m_value.asInt; }
  327. uint8_t getUInt7() { ASSERT(isUInt7()); return m_value.asInt; }
  328. uint8_t getUInt8() { ASSERT(isUInt8()); return m_value.asInt; }
  329. uint16_t getUInt9() { ASSERT(isUInt9()); return m_value.asInt; }
  330. uint16_t getUInt10() { ASSERT(isUInt10()); return m_value.asInt; }
  331. uint16_t getUInt12() { ASSERT(isUInt12()); return m_value.asInt; }
  332. uint16_t getUInt16() { ASSERT(isUInt16()); return m_value.asInt; }
  333. bool isEncodedImm() { return m_type == TypeEncoded; }
  334. private:
  335. ThumbImmediateType m_type;
  336. ThumbImmediateValue m_value;
  337. };
  338. typedef enum {
  339. SRType_LSL,
  340. SRType_LSR,
  341. SRType_ASR,
  342. SRType_ROR,
  343. SRType_RRX = SRType_ROR
  344. } ARMShiftType;
  345. class ShiftTypeAndAmount {
  346. friend class ARMv7Assembler;
  347. public:
  348. ShiftTypeAndAmount()
  349. {
  350. m_u.type = (ARMShiftType)0;
  351. m_u.amount = 0;
  352. }
  353. ShiftTypeAndAmount(ARMShiftType type, unsigned amount)
  354. {
  355. m_u.type = type;
  356. m_u.amount = amount & 31;
  357. }
  358. unsigned lo4() { return m_u.lo4; }
  359. unsigned hi4() { return m_u.hi4; }
  360. private:
  361. union {
  362. struct {
  363. unsigned lo4 : 4;
  364. unsigned hi4 : 4;
  365. };
  366. struct {
  367. unsigned type : 2;
  368. unsigned amount : 6;
  369. };
  370. } m_u;
  371. };
  372. class ARMv7Assembler {
  373. public:
  374. typedef ARMRegisters::RegisterID RegisterID;
  375. typedef ARMRegisters::FPSingleRegisterID FPSingleRegisterID;
  376. typedef ARMRegisters::FPDoubleRegisterID FPDoubleRegisterID;
  377. typedef ARMRegisters::FPQuadRegisterID FPQuadRegisterID;
  378. // (HS, LO, HI, LS) -> (AE, B, A, BE)
  379. // (VS, VC) -> (O, NO)
  380. typedef enum {
  381. ConditionEQ, // Zero / Equal.
  382. ConditionNE, // Non-zero / Not equal.
  383. ConditionHS, ConditionCS = ConditionHS, // Unsigned higher or same.
  384. ConditionLO, ConditionCC = ConditionLO, // Unsigned lower.
  385. ConditionMI, // Negative.
  386. ConditionPL, // Positive or zero.
  387. ConditionVS, // Overflowed.
  388. ConditionVC, // Not overflowed.
  389. ConditionHI, // Unsigned higher.
  390. ConditionLS, // Unsigned lower or same.
  391. ConditionGE, // Signed greater than or equal.
  392. ConditionLT, // Signed less than.
  393. ConditionGT, // Signed greater than.
  394. ConditionLE, // Signed less than or equal.
  395. ConditionAL, // Unconditional / Always execute.
  396. ConditionInvalid
  397. } Condition;
  398. #define JUMP_ENUM_WITH_SIZE(index, value) (((value) << 3) | (index))
  399. #define JUMP_ENUM_SIZE(jump) ((jump) >> 3)
  400. enum JumpType { JumpFixed = JUMP_ENUM_WITH_SIZE(0, 0),
  401. JumpNoCondition = JUMP_ENUM_WITH_SIZE(1, 5 * sizeof(uint16_t)),
  402. JumpCondition = JUMP_ENUM_WITH_SIZE(2, 6 * sizeof(uint16_t)),
  403. JumpNoConditionFixedSize = JUMP_ENUM_WITH_SIZE(3, 5 * sizeof(uint16_t)),
  404. JumpConditionFixedSize = JUMP_ENUM_WITH_SIZE(4, 6 * sizeof(uint16_t))
  405. };
  406. enum JumpLinkType {
  407. LinkInvalid = JUMP_ENUM_WITH_SIZE(0, 0),
  408. LinkJumpT1 = JUMP_ENUM_WITH_SIZE(1, sizeof(uint16_t)),
  409. LinkJumpT2 = JUMP_ENUM_WITH_SIZE(2, sizeof(uint16_t)),
  410. LinkJumpT3 = JUMP_ENUM_WITH_SIZE(3, 2 * sizeof(uint16_t)),
  411. LinkJumpT4 = JUMP_ENUM_WITH_SIZE(4, 2 * sizeof(uint16_t)),
  412. LinkConditionalJumpT4 = JUMP_ENUM_WITH_SIZE(5, 3 * sizeof(uint16_t)),
  413. LinkBX = JUMP_ENUM_WITH_SIZE(6, 5 * sizeof(uint16_t)),
  414. LinkConditionalBX = JUMP_ENUM_WITH_SIZE(7, 6 * sizeof(uint16_t))
  415. };
  416. class LinkRecord {
  417. public:
  418. LinkRecord(intptr_t from, intptr_t to, JumpType type, Condition condition)
  419. {
  420. data.realTypes.m_from = from;
  421. data.realTypes.m_to = to;
  422. data.realTypes.m_type = type;
  423. data.realTypes.m_linkType = LinkInvalid;
  424. data.realTypes.m_condition = condition;
  425. }
  426. void operator=(const LinkRecord& other)
  427. {
  428. data.copyTypes.content[0] = other.data.copyTypes.content[0];
  429. data.copyTypes.content[1] = other.data.copyTypes.content[1];
  430. data.copyTypes.content[2] = other.data.copyTypes.content[2];
  431. }
  432. intptr_t from() const { return data.realTypes.m_from; }
  433. void setFrom(intptr_t from) { data.realTypes.m_from = from; }
  434. intptr_t to() const { return data.realTypes.m_to; }
  435. JumpType type() const { return data.realTypes.m_type; }
  436. JumpLinkType linkType() const { return data.realTypes.m_linkType; }
  437. void setLinkType(JumpLinkType linkType) { ASSERT(data.realTypes.m_linkType == LinkInvalid); data.realTypes.m_linkType = linkType; }
  438. Condition condition() const { return data.realTypes.m_condition; }
  439. private:
  440. union {
  441. struct RealTypes {
  442. intptr_t m_from : 31;
  443. intptr_t m_to : 31;
  444. JumpType m_type : 8;
  445. JumpLinkType m_linkType : 8;
  446. Condition m_condition : 16;
  447. } realTypes;
  448. struct CopyTypes {
  449. uint32_t content[3];
  450. } copyTypes;
  451. COMPILE_ASSERT(sizeof(RealTypes) == sizeof(CopyTypes), LinkRecordCopyStructSizeEqualsRealStruct);
  452. } data;
  453. };
  454. ARMv7Assembler()
  455. : m_indexOfLastWatchpoint(INT_MIN)
  456. , m_indexOfTailOfLastWatchpoint(INT_MIN)
  457. {
  458. }
  459. private:
  460. // ARMv7, Appx-A.6.3
  461. static bool BadReg(RegisterID reg)
  462. {
  463. return (reg == ARMRegisters::sp) || (reg == ARMRegisters::pc);
  464. }
  465. uint32_t singleRegisterMask(FPSingleRegisterID rdNum, int highBitsShift, int lowBitShift)
  466. {
  467. uint32_t rdMask = (rdNum >> 1) << highBitsShift;
  468. if (rdNum & 1)
  469. rdMask |= 1 << lowBitShift;
  470. return rdMask;
  471. }
  472. uint32_t doubleRegisterMask(FPDoubleRegisterID rdNum, int highBitShift, int lowBitsShift)
  473. {
  474. uint32_t rdMask = (rdNum & 0xf) << lowBitsShift;
  475. if (rdNum & 16)
  476. rdMask |= 1 << highBitShift;
  477. return rdMask;
  478. }
  479. typedef enum {
  480. OP_ADD_reg_T1 = 0x1800,
  481. OP_SUB_reg_T1 = 0x1A00,
  482. OP_ADD_imm_T1 = 0x1C00,
  483. OP_SUB_imm_T1 = 0x1E00,
  484. OP_MOV_imm_T1 = 0x2000,
  485. OP_CMP_imm_T1 = 0x2800,
  486. OP_ADD_imm_T2 = 0x3000,
  487. OP_SUB_imm_T2 = 0x3800,
  488. OP_AND_reg_T1 = 0x4000,
  489. OP_EOR_reg_T1 = 0x4040,
  490. OP_TST_reg_T1 = 0x4200,
  491. OP_RSB_imm_T1 = 0x4240,
  492. OP_CMP_reg_T1 = 0x4280,
  493. OP_ORR_reg_T1 = 0x4300,
  494. OP_MVN_reg_T1 = 0x43C0,
  495. OP_ADD_reg_T2 = 0x4400,
  496. OP_MOV_reg_T1 = 0x4600,
  497. OP_BLX = 0x4700,
  498. OP_BX = 0x4700,
  499. OP_STR_reg_T1 = 0x5000,
  500. OP_STRH_reg_T1 = 0x5200,
  501. OP_STRB_reg_T1 = 0x5400,
  502. OP_LDRSB_reg_T1 = 0x5600,
  503. OP_LDR_reg_T1 = 0x5800,
  504. OP_LDRH_reg_T1 = 0x5A00,
  505. OP_LDRB_reg_T1 = 0x5C00,
  506. OP_LDRSH_reg_T1 = 0x5E00,
  507. OP_STR_imm_T1 = 0x6000,
  508. OP_LDR_imm_T1 = 0x6800,
  509. OP_STRB_imm_T1 = 0x7000,
  510. OP_LDRB_imm_T1 = 0x7800,
  511. OP_STRH_imm_T1 = 0x8000,
  512. OP_LDRH_imm_T1 = 0x8800,
  513. OP_STR_imm_T2 = 0x9000,
  514. OP_LDR_imm_T2 = 0x9800,
  515. OP_ADD_SP_imm_T1 = 0xA800,
  516. OP_ADD_SP_imm_T2 = 0xB000,
  517. OP_SUB_SP_imm_T1 = 0xB080,
  518. OP_BKPT = 0xBE00,
  519. OP_IT = 0xBF00,
  520. OP_NOP_T1 = 0xBF00,
  521. #if OS(PSP2)
  522. OP_HLT = 0xB7B7,
  523. #endif
  524. } OpcodeID;
  525. typedef enum {
  526. OP_B_T1 = 0xD000,
  527. OP_B_T2 = 0xE000,
  528. OP_AND_reg_T2 = 0xEA00,
  529. OP_TST_reg_T2 = 0xEA10,
  530. OP_ORR_reg_T2 = 0xEA40,
  531. OP_ORR_S_reg_T2 = 0xEA50,
  532. OP_ASR_imm_T1 = 0xEA4F,
  533. OP_LSL_imm_T1 = 0xEA4F,
  534. OP_LSR_imm_T1 = 0xEA4F,
  535. OP_ROR_imm_T1 = 0xEA4F,
  536. OP_MVN_reg_T2 = 0xEA6F,
  537. OP_EOR_reg_T2 = 0xEA80,
  538. OP_ADD_reg_T3 = 0xEB00,
  539. OP_ADD_S_reg_T3 = 0xEB10,
  540. OP_SUB_reg_T2 = 0xEBA0,
  541. OP_SUB_S_reg_T2 = 0xEBB0,
  542. OP_CMP_reg_T2 = 0xEBB0,
  543. OP_VMOV_CtoD = 0xEC00,
  544. OP_VMOV_DtoC = 0xEC10,
  545. OP_FSTS = 0xED00,
  546. OP_VSTR = 0xED00,
  547. OP_FLDS = 0xED10,
  548. OP_VLDR = 0xED10,
  549. OP_VMOV_CtoS = 0xEE00,
  550. OP_VMOV_StoC = 0xEE10,
  551. OP_VMUL_T2 = 0xEE20,
  552. OP_VADD_T2 = 0xEE30,
  553. OP_VSUB_T2 = 0xEE30,
  554. OP_VDIV = 0xEE80,
  555. OP_VABS_T2 = 0xEEB0,
  556. OP_VCMP = 0xEEB0,
  557. OP_VCVT_FPIVFP = 0xEEB0,
  558. OP_VMOV_T2 = 0xEEB0,
  559. OP_VMOV_IMM_T2 = 0xEEB0,
  560. OP_VMRS = 0xEEB0,
  561. OP_VNEG_T2 = 0xEEB0,
  562. OP_VSQRT_T1 = 0xEEB0,
  563. OP_VCVTSD_T1 = 0xEEB0,
  564. OP_VCVTDS_T1 = 0xEEB0,
  565. OP_B_T3a = 0xF000,
  566. OP_B_T4a = 0xF000,
  567. OP_AND_imm_T1 = 0xF000,
  568. OP_TST_imm = 0xF010,
  569. OP_ORR_imm_T1 = 0xF040,
  570. OP_MOV_imm_T2 = 0xF040,
  571. OP_MVN_imm = 0xF060,
  572. OP_EOR_imm_T1 = 0xF080,
  573. OP_ADD_imm_T3 = 0xF100,
  574. OP_ADD_S_imm_T3 = 0xF110,
  575. OP_CMN_imm = 0xF110,
  576. OP_ADC_imm = 0xF140,
  577. OP_SUB_imm_T3 = 0xF1A0,
  578. OP_SUB_S_imm_T3 = 0xF1B0,
  579. OP_CMP_imm_T2 = 0xF1B0,
  580. OP_RSB_imm_T2 = 0xF1C0,
  581. OP_RSB_S_imm_T2 = 0xF1D0,
  582. OP_ADD_imm_T4 = 0xF200,
  583. OP_MOV_imm_T3 = 0xF240,
  584. OP_SUB_imm_T4 = 0xF2A0,
  585. OP_MOVT = 0xF2C0,
  586. OP_UBFX_T1 = 0xF3C0,
  587. OP_NOP_T2a = 0xF3AF,
  588. OP_STRB_imm_T3 = 0xF800,
  589. OP_STRB_reg_T2 = 0xF800,
  590. OP_LDRB_imm_T3 = 0xF810,
  591. OP_LDRB_reg_T2 = 0xF810,
  592. OP_STRH_imm_T3 = 0xF820,
  593. OP_STRH_reg_T2 = 0xF820,
  594. OP_LDRH_reg_T2 = 0xF830,
  595. OP_LDRH_imm_T3 = 0xF830,
  596. OP_STR_imm_T4 = 0xF840,
  597. OP_STR_reg_T2 = 0xF840,
  598. OP_LDR_imm_T4 = 0xF850,
  599. OP_LDR_reg_T2 = 0xF850,
  600. OP_STRB_imm_T2 = 0xF880,
  601. OP_LDRB_imm_T2 = 0xF890,
  602. OP_STRH_imm_T2 = 0xF8A0,
  603. OP_LDRH_imm_T2 = 0xF8B0,
  604. OP_STR_imm_T3 = 0xF8C0,
  605. OP_LDR_imm_T3 = 0xF8D0,
  606. OP_LDRSB_reg_T2 = 0xF910,
  607. OP_LDRSH_reg_T2 = 0xF930,
  608. OP_LSL_reg_T2 = 0xFA00,
  609. OP_LSR_reg_T2 = 0xFA20,
  610. OP_ASR_reg_T2 = 0xFA40,
  611. OP_ROR_reg_T2 = 0xFA60,
  612. OP_CLZ = 0xFAB0,
  613. OP_SMULL_T1 = 0xFB80,
  614. #if CPU(APPLE_ARMV7S)
  615. OP_SDIV_T1 = 0xFB90,
  616. OP_UDIV_T1 = 0xFBB0,
  617. #endif
  618. } OpcodeID1;
  619. typedef enum {
  620. OP_VADD_T2b = 0x0A00,
  621. OP_VDIVb = 0x0A00,
  622. OP_FLDSb = 0x0A00,
  623. OP_VLDRb = 0x0A00,
  624. OP_VMOV_IMM_T2b = 0x0A00,
  625. OP_VMOV_T2b = 0x0A40,
  626. OP_VMUL_T2b = 0x0A00,
  627. OP_FSTSb = 0x0A00,
  628. OP_VSTRb = 0x0A00,
  629. OP_VMOV_StoCb = 0x0A10,
  630. OP_VMOV_CtoSb = 0x0A10,
  631. OP_VMOV_DtoCb = 0x0A10,
  632. OP_VMOV_CtoDb = 0x0A10,
  633. OP_VMRSb = 0x0A10,
  634. OP_VABS_T2b = 0x0A40,
  635. OP_VCMPb = 0x0A40,
  636. OP_VCVT_FPIVFPb = 0x0A40,
  637. OP_VNEG_T2b = 0x0A40,
  638. OP_VSUB_T2b = 0x0A40,
  639. OP_VSQRT_T1b = 0x0A40,
  640. OP_VCVTSD_T1b = 0x0A40,
  641. OP_VCVTDS_T1b = 0x0A40,
  642. OP_NOP_T2b = 0x8000,
  643. OP_B_T3b = 0x8000,
  644. OP_B_T4b = 0x9000,
  645. } OpcodeID2;
  646. struct FourFours {
  647. FourFours(unsigned f3, unsigned f2, unsigned f1, unsigned f0)
  648. {
  649. m_u.f0 = f0;
  650. m_u.f1 = f1;
  651. m_u.f2 = f2;
  652. m_u.f3 = f3;
  653. }
  654. union {
  655. unsigned value;
  656. struct {
  657. unsigned f0 : 4;
  658. unsigned f1 : 4;
  659. unsigned f2 : 4;
  660. unsigned f3 : 4;
  661. };
  662. } m_u;
  663. };
  664. class ARMInstructionFormatter;
  665. // false means else!
  666. bool ifThenElseConditionBit(Condition condition, bool isIf)
  667. {
  668. return isIf ? (condition & 1) : !(condition & 1);
  669. }
  670. uint8_t ifThenElse(Condition condition, bool inst2if, bool inst3if, bool inst4if)
  671. {
  672. int mask = (ifThenElseConditionBit(condition, inst2if) << 3)
  673. | (ifThenElseConditionBit(condition, inst3if) << 2)
  674. | (ifThenElseConditionBit(condition, inst4if) << 1)
  675. | 1;
  676. ASSERT((condition != ConditionAL) || !(mask & (mask - 1)));
  677. return (condition << 4) | mask;
  678. }
  679. uint8_t ifThenElse(Condition condition, bool inst2if, bool inst3if)
  680. {
  681. int mask = (ifThenElseConditionBit(condition, inst2if) << 3)
  682. | (ifThenElseConditionBit(condition, inst3if) << 2)
  683. | 2;
  684. ASSERT((condition != ConditionAL) || !(mask & (mask - 1)));
  685. return (condition << 4) | mask;
  686. }
  687. uint8_t ifThenElse(Condition condition, bool inst2if)
  688. {
  689. int mask = (ifThenElseConditionBit(condition, inst2if) << 3)
  690. | 4;
  691. ASSERT((condition != ConditionAL) || !(mask & (mask - 1)));
  692. return (condition << 4) | mask;
  693. }
  694. uint8_t ifThenElse(Condition condition)
  695. {
  696. int mask = 8;
  697. return (condition << 4) | mask;
  698. }
  699. public:
  700. void adc(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  701. {
  702. // Rd can only be SP if Rn is also SP.
  703. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  704. ASSERT(rd != ARMRegisters::pc);
  705. ASSERT(rn != ARMRegisters::pc);
  706. ASSERT(imm.isEncodedImm());
  707. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_ADC_imm, rn, rd, imm);
  708. }
  709. void add(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  710. {
  711. // Rd can only be SP if Rn is also SP.
  712. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  713. ASSERT(rd != ARMRegisters::pc);
  714. ASSERT(rn != ARMRegisters::pc);
  715. ASSERT(imm.isValid());
  716. if (rn == ARMRegisters::sp) {
  717. ASSERT(!(imm.getUInt16() & 3));
  718. if (!(rd & 8) && imm.isUInt10()) {
  719. m_formatter.oneWordOp5Reg3Imm8(OP_ADD_SP_imm_T1, rd, static_cast<uint8_t>(imm.getUInt10() >> 2));
  720. return;
  721. } else if ((rd == ARMRegisters::sp) && imm.isUInt9()) {
  722. m_formatter.oneWordOp9Imm7(OP_ADD_SP_imm_T2, static_cast<uint8_t>(imm.getUInt9() >> 2));
  723. return;
  724. }
  725. } else if (!((rd | rn) & 8)) {
  726. if (imm.isUInt3()) {
  727. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_ADD_imm_T1, (RegisterID)imm.getUInt3(), rn, rd);
  728. return;
  729. } else if ((rd == rn) && imm.isUInt8()) {
  730. m_formatter.oneWordOp5Reg3Imm8(OP_ADD_imm_T2, rd, imm.getUInt8());
  731. return;
  732. }
  733. }
  734. if (imm.isEncodedImm())
  735. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_ADD_imm_T3, rn, rd, imm);
  736. else {
  737. ASSERT(imm.isUInt12());
  738. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_ADD_imm_T4, rn, rd, imm);
  739. }
  740. }
  741. ALWAYS_INLINE void add(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  742. {
  743. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  744. ASSERT(rd != ARMRegisters::pc);
  745. ASSERT(rn != ARMRegisters::pc);
  746. ASSERT(!BadReg(rm));
  747. m_formatter.twoWordOp12Reg4FourFours(OP_ADD_reg_T3, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  748. }
  749. // NOTE: In an IT block, add doesn't modify the flags register.
  750. ALWAYS_INLINE void add(RegisterID rd, RegisterID rn, RegisterID rm)
  751. {
  752. if (rd == rn)
  753. m_formatter.oneWordOp8RegReg143(OP_ADD_reg_T2, rm, rd);
  754. else if (rd == rm)
  755. m_formatter.oneWordOp8RegReg143(OP_ADD_reg_T2, rn, rd);
  756. else if (!((rd | rn | rm) & 8))
  757. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_ADD_reg_T1, rm, rn, rd);
  758. else
  759. add(rd, rn, rm, ShiftTypeAndAmount());
  760. }
  761. // Not allowed in an IT (if then) block.
  762. ALWAYS_INLINE void add_S(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  763. {
  764. // Rd can only be SP if Rn is also SP.
  765. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  766. ASSERT(rd != ARMRegisters::pc);
  767. ASSERT(rn != ARMRegisters::pc);
  768. ASSERT(imm.isEncodedImm());
  769. if (!((rd | rn) & 8)) {
  770. if (imm.isUInt3()) {
  771. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_ADD_imm_T1, (RegisterID)imm.getUInt3(), rn, rd);
  772. return;
  773. } else if ((rd == rn) && imm.isUInt8()) {
  774. m_formatter.oneWordOp5Reg3Imm8(OP_ADD_imm_T2, rd, imm.getUInt8());
  775. return;
  776. }
  777. }
  778. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_ADD_S_imm_T3, rn, rd, imm);
  779. }
  780. // Not allowed in an IT (if then) block?
  781. ALWAYS_INLINE void add_S(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  782. {
  783. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  784. ASSERT(rd != ARMRegisters::pc);
  785. ASSERT(rn != ARMRegisters::pc);
  786. ASSERT(!BadReg(rm));
  787. m_formatter.twoWordOp12Reg4FourFours(OP_ADD_S_reg_T3, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  788. }
  789. // Not allowed in an IT (if then) block.
  790. ALWAYS_INLINE void add_S(RegisterID rd, RegisterID rn, RegisterID rm)
  791. {
  792. if (!((rd | rn | rm) & 8))
  793. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_ADD_reg_T1, rm, rn, rd);
  794. else
  795. add_S(rd, rn, rm, ShiftTypeAndAmount());
  796. }
  797. ALWAYS_INLINE void ARM_and(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  798. {
  799. ASSERT(!BadReg(rd));
  800. ASSERT(!BadReg(rn));
  801. ASSERT(imm.isEncodedImm());
  802. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_AND_imm_T1, rn, rd, imm);
  803. }
  804. ALWAYS_INLINE void ARM_and(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  805. {
  806. ASSERT(!BadReg(rd));
  807. ASSERT(!BadReg(rn));
  808. ASSERT(!BadReg(rm));
  809. m_formatter.twoWordOp12Reg4FourFours(OP_AND_reg_T2, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  810. }
  811. ALWAYS_INLINE void ARM_and(RegisterID rd, RegisterID rn, RegisterID rm)
  812. {
  813. if ((rd == rn) && !((rd | rm) & 8))
  814. m_formatter.oneWordOp10Reg3Reg3(OP_AND_reg_T1, rm, rd);
  815. else if ((rd == rm) && !((rd | rn) & 8))
  816. m_formatter.oneWordOp10Reg3Reg3(OP_AND_reg_T1, rn, rd);
  817. else
  818. ARM_and(rd, rn, rm, ShiftTypeAndAmount());
  819. }
  820. ALWAYS_INLINE void asr(RegisterID rd, RegisterID rm, int32_t shiftAmount)
  821. {
  822. ASSERT(!BadReg(rd));
  823. ASSERT(!BadReg(rm));
  824. ShiftTypeAndAmount shift(SRType_ASR, shiftAmount);
  825. m_formatter.twoWordOp16FourFours(OP_ASR_imm_T1, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  826. }
  827. ALWAYS_INLINE void asr(RegisterID rd, RegisterID rn, RegisterID rm)
  828. {
  829. ASSERT(!BadReg(rd));
  830. ASSERT(!BadReg(rn));
  831. ASSERT(!BadReg(rm));
  832. m_formatter.twoWordOp12Reg4FourFours(OP_ASR_reg_T2, rn, FourFours(0xf, rd, 0, rm));
  833. }
  834. // Only allowed in IT (if then) block if last instruction.
  835. ALWAYS_INLINE AssemblerLabel b()
  836. {
  837. m_formatter.twoWordOp16Op16(OP_B_T4a, OP_B_T4b);
  838. return m_formatter.label();
  839. }
  840. // Only allowed in IT (if then) block if last instruction.
  841. ALWAYS_INLINE AssemblerLabel blx(RegisterID rm)
  842. {
  843. ASSERT(rm != ARMRegisters::pc);
  844. m_formatter.oneWordOp8RegReg143(OP_BLX, rm, (RegisterID)8);
  845. return m_formatter.label();
  846. }
  847. // Only allowed in IT (if then) block if last instruction.
  848. ALWAYS_INLINE AssemblerLabel bx(RegisterID rm)
  849. {
  850. m_formatter.oneWordOp8RegReg143(OP_BX, rm, (RegisterID)0);
  851. return m_formatter.label();
  852. }
  853. void bkpt(uint8_t imm = 0)
  854. {
  855. m_formatter.oneWordOp8Imm8(OP_BKPT, imm);
  856. }
  857. ALWAYS_INLINE void clz(RegisterID rd, RegisterID rm)
  858. {
  859. ASSERT(!BadReg(rd));
  860. ASSERT(!BadReg(rm));
  861. m_formatter.twoWordOp12Reg4FourFours(OP_CLZ, rm, FourFours(0xf, rd, 8, rm));
  862. }
  863. ALWAYS_INLINE void cmn(RegisterID rn, ARMThumbImmediate imm)
  864. {
  865. ASSERT(rn != ARMRegisters::pc);
  866. ASSERT(imm.isEncodedImm());
  867. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_CMN_imm, rn, (RegisterID)0xf, imm);
  868. }
  869. ALWAYS_INLINE void cmp(RegisterID rn, ARMThumbImmediate imm)
  870. {
  871. ASSERT(rn != ARMRegisters::pc);
  872. ASSERT(imm.isEncodedImm());
  873. if (!(rn & 8) && imm.isUInt8())
  874. m_formatter.oneWordOp5Reg3Imm8(OP_CMP_imm_T1, rn, imm.getUInt8());
  875. else
  876. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_CMP_imm_T2, rn, (RegisterID)0xf, imm);
  877. }
  878. ALWAYS_INLINE void cmp(RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  879. {
  880. ASSERT(rn != ARMRegisters::pc);
  881. ASSERT(!BadReg(rm));
  882. m_formatter.twoWordOp12Reg4FourFours(OP_CMP_reg_T2, rn, FourFours(shift.hi4(), 0xf, shift.lo4(), rm));
  883. }
  884. ALWAYS_INLINE void cmp(RegisterID rn, RegisterID rm)
  885. {
  886. if ((rn | rm) & 8)
  887. cmp(rn, rm, ShiftTypeAndAmount());
  888. else
  889. m_formatter.oneWordOp10Reg3Reg3(OP_CMP_reg_T1, rm, rn);
  890. }
  891. // xor is not spelled with an 'e'. :-(
  892. ALWAYS_INLINE void eor(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  893. {
  894. ASSERT(!BadReg(rd));
  895. ASSERT(!BadReg(rn));
  896. ASSERT(imm.isEncodedImm());
  897. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_EOR_imm_T1, rn, rd, imm);
  898. }
  899. // xor is not spelled with an 'e'. :-(
  900. ALWAYS_INLINE void eor(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  901. {
  902. ASSERT(!BadReg(rd));
  903. ASSERT(!BadReg(rn));
  904. ASSERT(!BadReg(rm));
  905. m_formatter.twoWordOp12Reg4FourFours(OP_EOR_reg_T2, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  906. }
  907. // xor is not spelled with an 'e'. :-(
  908. void eor(RegisterID rd, RegisterID rn, RegisterID rm)
  909. {
  910. if ((rd == rn) && !((rd | rm) & 8))
  911. m_formatter.oneWordOp10Reg3Reg3(OP_EOR_reg_T1, rm, rd);
  912. else if ((rd == rm) && !((rd | rn) & 8))
  913. m_formatter.oneWordOp10Reg3Reg3(OP_EOR_reg_T1, rn, rd);
  914. else
  915. eor(rd, rn, rm, ShiftTypeAndAmount());
  916. }
  917. #if OS(PSP2)
  918. ALWAYS_INLINE void hlt()
  919. {
  920. m_formatter.oneWordOp8Imm8(OP_HLT, 0xb7);
  921. }
  922. #endif
  923. ALWAYS_INLINE void it(Condition cond)
  924. {
  925. m_formatter.oneWordOp8Imm8(OP_IT, ifThenElse(cond));
  926. }
  927. ALWAYS_INLINE void it(Condition cond, bool inst2if)
  928. {
  929. m_formatter.oneWordOp8Imm8(OP_IT, ifThenElse(cond, inst2if));
  930. }
  931. ALWAYS_INLINE void it(Condition cond, bool inst2if, bool inst3if)
  932. {
  933. m_formatter.oneWordOp8Imm8(OP_IT, ifThenElse(cond, inst2if, inst3if));
  934. }
  935. ALWAYS_INLINE void it(Condition cond, bool inst2if, bool inst3if, bool inst4if)
  936. {
  937. m_formatter.oneWordOp8Imm8(OP_IT, ifThenElse(cond, inst2if, inst3if, inst4if));
  938. }
  939. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  940. ALWAYS_INLINE void ldr(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  941. {
  942. ASSERT(rn != ARMRegisters::pc); // LDR (literal)
  943. ASSERT(imm.isUInt12());
  944. if (!((rt | rn) & 8) && imm.isUInt7())
  945. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_LDR_imm_T1, imm.getUInt7() >> 2, rn, rt);
  946. else if ((rn == ARMRegisters::sp) && !(rt & 8) && imm.isUInt10())
  947. m_formatter.oneWordOp5Reg3Imm8(OP_LDR_imm_T2, rt, static_cast<uint8_t>(imm.getUInt10() >> 2));
  948. else
  949. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDR_imm_T3, rn, rt, imm.getUInt12());
  950. }
  951. ALWAYS_INLINE void ldrWide8BitImmediate(RegisterID rt, RegisterID rn, uint8_t immediate)
  952. {
  953. ASSERT(rn != ARMRegisters::pc);
  954. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDR_imm_T3, rn, rt, immediate);
  955. }
  956. ALWAYS_INLINE void ldrCompact(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  957. {
  958. ASSERT(rn != ARMRegisters::pc); // LDR (literal)
  959. ASSERT(imm.isUInt7());
  960. ASSERT(!((rt | rn) & 8));
  961. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_LDR_imm_T1, imm.getUInt7() >> 2, rn, rt);
  962. }
  963. // If index is set, this is a regular offset or a pre-indexed load;
  964. // if index is not set then is is a post-index load.
  965. //
  966. // If wback is set rn is updated - this is a pre or post index load,
  967. // if wback is not set this is a regular offset memory access.
  968. //
  969. // (-255 <= offset <= 255)
  970. // _reg = REG[rn]
  971. // _tmp = _reg + offset
  972. // MEM[index ? _tmp : _reg] = REG[rt]
  973. // if (wback) REG[rn] = _tmp
  974. ALWAYS_INLINE void ldr(RegisterID rt, RegisterID rn, int offset, bool index, bool wback)
  975. {
  976. ASSERT(rt != ARMRegisters::pc);
  977. ASSERT(rn != ARMRegisters::pc);
  978. ASSERT(index || wback);
  979. ASSERT(!wback | (rt != rn));
  980. bool add = true;
  981. if (offset < 0) {
  982. add = false;
  983. offset = -offset;
  984. }
  985. ASSERT((offset & ~0xff) == 0);
  986. offset |= (wback << 8);
  987. offset |= (add << 9);
  988. offset |= (index << 10);
  989. offset |= (1 << 11);
  990. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDR_imm_T4, rn, rt, offset);
  991. }
  992. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  993. ALWAYS_INLINE void ldr(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  994. {
  995. ASSERT(rn != ARMRegisters::pc); // LDR (literal)
  996. ASSERT(!BadReg(rm));
  997. ASSERT(shift <= 3);
  998. if (!shift && !((rt | rn | rm) & 8))
  999. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_LDR_reg_T1, rm, rn, rt);
  1000. else
  1001. m_formatter.twoWordOp12Reg4FourFours(OP_LDR_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1002. }
  1003. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1004. ALWAYS_INLINE void ldrh(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  1005. {
  1006. ASSERT(rn != ARMRegisters::pc); // LDR (literal)
  1007. ASSERT(imm.isUInt12());
  1008. if (!((rt | rn) & 8) && imm.isUInt6())
  1009. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_LDRH_imm_T1, imm.getUInt6() >> 2, rn, rt);
  1010. else
  1011. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDRH_imm_T2, rn, rt, imm.getUInt12());
  1012. }
  1013. // If index is set, this is a regular offset or a pre-indexed load;
  1014. // if index is not set then is is a post-index load.
  1015. //
  1016. // If wback is set rn is updated - this is a pre or post index load,
  1017. // if wback is not set this is a regular offset memory access.
  1018. //
  1019. // (-255 <= offset <= 255)
  1020. // _reg = REG[rn]
  1021. // _tmp = _reg + offset
  1022. // MEM[index ? _tmp : _reg] = REG[rt]
  1023. // if (wback) REG[rn] = _tmp
  1024. ALWAYS_INLINE void ldrh(RegisterID rt, RegisterID rn, int offset, bool index, bool wback)
  1025. {
  1026. ASSERT(rt != ARMRegisters::pc);
  1027. ASSERT(rn != ARMRegisters::pc);
  1028. ASSERT(index || wback);
  1029. ASSERT(!wback | (rt != rn));
  1030. bool add = true;
  1031. if (offset < 0) {
  1032. add = false;
  1033. offset = -offset;
  1034. }
  1035. ASSERT((offset & ~0xff) == 0);
  1036. offset |= (wback << 8);
  1037. offset |= (add << 9);
  1038. offset |= (index << 10);
  1039. offset |= (1 << 11);
  1040. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDRH_imm_T3, rn, rt, offset);
  1041. }
  1042. ALWAYS_INLINE void ldrh(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1043. {
  1044. ASSERT(!BadReg(rt)); // Memory hint
  1045. ASSERT(rn != ARMRegisters::pc); // LDRH (literal)
  1046. ASSERT(!BadReg(rm));
  1047. ASSERT(shift <= 3);
  1048. if (!shift && !((rt | rn | rm) & 8))
  1049. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_LDRH_reg_T1, rm, rn, rt);
  1050. else
  1051. m_formatter.twoWordOp12Reg4FourFours(OP_LDRH_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1052. }
  1053. void ldrb(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  1054. {
  1055. ASSERT(rn != ARMRegisters::pc); // LDR (literal)
  1056. ASSERT(imm.isUInt12());
  1057. if (!((rt | rn) & 8) && imm.isUInt5())
  1058. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_LDRB_imm_T1, imm.getUInt5(), rn, rt);
  1059. else
  1060. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDRB_imm_T2, rn, rt, imm.getUInt12());
  1061. }
  1062. void ldrb(RegisterID rt, RegisterID rn, int offset, bool index, bool wback)
  1063. {
  1064. ASSERT(rt != ARMRegisters::pc);
  1065. ASSERT(rn != ARMRegisters::pc);
  1066. ASSERT(index || wback);
  1067. ASSERT(!wback | (rt != rn));
  1068. bool add = true;
  1069. if (offset < 0) {
  1070. add = false;
  1071. offset = -offset;
  1072. }
  1073. ASSERT(!(offset & ~0xff));
  1074. offset |= (wback << 8);
  1075. offset |= (add << 9);
  1076. offset |= (index << 10);
  1077. offset |= (1 << 11);
  1078. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_LDRB_imm_T3, rn, rt, offset);
  1079. }
  1080. ALWAYS_INLINE void ldrb(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1081. {
  1082. ASSERT(rn != ARMRegisters::pc); // LDR (literal)
  1083. ASSERT(!BadReg(rm));
  1084. ASSERT(shift <= 3);
  1085. if (!shift && !((rt | rn | rm) & 8))
  1086. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_LDRB_reg_T1, rm, rn, rt);
  1087. else
  1088. m_formatter.twoWordOp12Reg4FourFours(OP_LDRB_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1089. }
  1090. void ldrsb(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1091. {
  1092. ASSERT(rn != ARMRegisters::pc);
  1093. ASSERT(!BadReg(rm));
  1094. ASSERT(shift <= 3);
  1095. if (!shift && !((rt | rn | rm) & 8))
  1096. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_LDRSB_reg_T1, rm, rn, rt);
  1097. else
  1098. m_formatter.twoWordOp12Reg4FourFours(OP_LDRSB_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1099. }
  1100. void ldrsh(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1101. {
  1102. ASSERT(rn != ARMRegisters::pc);
  1103. ASSERT(!BadReg(rm));
  1104. ASSERT(shift <= 3);
  1105. if (!shift && !((rt | rn | rm) & 8))
  1106. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_LDRSH_reg_T1, rm, rn, rt);
  1107. else
  1108. m_formatter.twoWordOp12Reg4FourFours(OP_LDRSH_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1109. }
  1110. void lsl(RegisterID rd, RegisterID rm, int32_t shiftAmount)
  1111. {
  1112. ASSERT(!BadReg(rd));
  1113. ASSERT(!BadReg(rm));
  1114. ShiftTypeAndAmount shift(SRType_LSL, shiftAmount);
  1115. m_formatter.twoWordOp16FourFours(OP_LSL_imm_T1, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1116. }
  1117. ALWAYS_INLINE void lsl(RegisterID rd, RegisterID rn, RegisterID rm)
  1118. {
  1119. ASSERT(!BadReg(rd));
  1120. ASSERT(!BadReg(rn));
  1121. ASSERT(!BadReg(rm));
  1122. m_formatter.twoWordOp12Reg4FourFours(OP_LSL_reg_T2, rn, FourFours(0xf, rd, 0, rm));
  1123. }
  1124. ALWAYS_INLINE void lsr(RegisterID rd, RegisterID rm, int32_t shiftAmount)
  1125. {
  1126. ASSERT(!BadReg(rd));
  1127. ASSERT(!BadReg(rm));
  1128. ShiftTypeAndAmount shift(SRType_LSR, shiftAmount);
  1129. m_formatter.twoWordOp16FourFours(OP_LSR_imm_T1, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1130. }
  1131. ALWAYS_INLINE void lsr(RegisterID rd, RegisterID rn, RegisterID rm)
  1132. {
  1133. ASSERT(!BadReg(rd));
  1134. ASSERT(!BadReg(rn));
  1135. ASSERT(!BadReg(rm));
  1136. m_formatter.twoWordOp12Reg4FourFours(OP_LSR_reg_T2, rn, FourFours(0xf, rd, 0, rm));
  1137. }
  1138. ALWAYS_INLINE void movT3(RegisterID rd, ARMThumbImmediate imm)
  1139. {
  1140. ASSERT(imm.isValid());
  1141. ASSERT(!imm.isEncodedImm());
  1142. ASSERT(!BadReg(rd));
  1143. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_MOV_imm_T3, imm.m_value.imm4, rd, imm);
  1144. }
  1145. #if OS(LINUX) || OS(QNX) || OS(PSP2)
  1146. static void revertJumpTo_movT3movtcmpT2(void* instructionStart, RegisterID left, RegisterID right, uintptr_t imm)
  1147. {
  1148. uint16_t* address = static_cast<uint16_t*>(instructionStart);
  1149. ARMThumbImmediate lo16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(imm));
  1150. ARMThumbImmediate hi16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(imm >> 16));
  1151. address[0] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOV_imm_T3, lo16);
  1152. address[1] = twoWordOp5i6Imm4Reg4EncodedImmSecond(right, lo16);
  1153. address[2] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOVT, hi16);
  1154. address[3] = twoWordOp5i6Imm4Reg4EncodedImmSecond(right, hi16);
  1155. address[4] = OP_CMP_reg_T2 | left;
  1156. cacheFlush(address, sizeof(uint16_t) * 5);
  1157. }
  1158. #else
  1159. static void revertJumpTo_movT3(void* instructionStart, RegisterID rd, ARMThumbImmediate imm)
  1160. {
  1161. ASSERT(imm.isValid());
  1162. ASSERT(!imm.isEncodedImm());
  1163. ASSERT(!BadReg(rd));
  1164. uint16_t* address = static_cast<uint16_t*>(instructionStart);
  1165. address[0] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOV_imm_T3, imm);
  1166. address[1] = twoWordOp5i6Imm4Reg4EncodedImmSecond(rd, imm);
  1167. cacheFlush(address, sizeof(uint16_t) * 2);
  1168. }
  1169. #endif
  1170. ALWAYS_INLINE void mov(RegisterID rd, ARMThumbImmediate imm)
  1171. {
  1172. ASSERT(imm.isValid());
  1173. ASSERT(!BadReg(rd));
  1174. if ((rd < 8) && imm.isUInt8())
  1175. m_formatter.oneWordOp5Reg3Imm8(OP_MOV_imm_T1, rd, imm.getUInt8());
  1176. else if (imm.isEncodedImm())
  1177. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_MOV_imm_T2, 0xf, rd, imm);
  1178. else
  1179. movT3(rd, imm);
  1180. }
  1181. ALWAYS_INLINE void mov(RegisterID rd, RegisterID rm)
  1182. {
  1183. m_formatter.oneWordOp8RegReg143(OP_MOV_reg_T1, rm, rd);
  1184. }
  1185. ALWAYS_INLINE void movt(RegisterID rd, ARMThumbImmediate imm)
  1186. {
  1187. ASSERT(imm.isUInt16());
  1188. ASSERT(!BadReg(rd));
  1189. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_MOVT, imm.m_value.imm4, rd, imm);
  1190. }
  1191. ALWAYS_INLINE void mvn(RegisterID rd, ARMThumbImmediate imm)
  1192. {
  1193. ASSERT(imm.isEncodedImm());
  1194. ASSERT(!BadReg(rd));
  1195. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_MVN_imm, 0xf, rd, imm);
  1196. }
  1197. ALWAYS_INLINE void mvn(RegisterID rd, RegisterID rm, ShiftTypeAndAmount shift)
  1198. {
  1199. ASSERT(!BadReg(rd));
  1200. ASSERT(!BadReg(rm));
  1201. m_formatter.twoWordOp16FourFours(OP_MVN_reg_T2, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1202. }
  1203. ALWAYS_INLINE void mvn(RegisterID rd, RegisterID rm)
  1204. {
  1205. if (!((rd | rm) & 8))
  1206. m_formatter.oneWordOp10Reg3Reg3(OP_MVN_reg_T1, rm, rd);
  1207. else
  1208. mvn(rd, rm, ShiftTypeAndAmount());
  1209. }
  1210. ALWAYS_INLINE void neg(RegisterID rd, RegisterID rm)
  1211. {
  1212. ARMThumbImmediate zero = ARMThumbImmediate::makeUInt12(0);
  1213. sub(rd, zero, rm);
  1214. }
  1215. ALWAYS_INLINE void orr(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  1216. {
  1217. ASSERT(!BadReg(rd));
  1218. ASSERT(!BadReg(rn));
  1219. ASSERT(imm.isEncodedImm());
  1220. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_ORR_imm_T1, rn, rd, imm);
  1221. }
  1222. ALWAYS_INLINE void orr(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  1223. {
  1224. ASSERT(!BadReg(rd));
  1225. ASSERT(!BadReg(rn));
  1226. ASSERT(!BadReg(rm));
  1227. m_formatter.twoWordOp12Reg4FourFours(OP_ORR_reg_T2, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1228. }
  1229. void orr(RegisterID rd, RegisterID rn, RegisterID rm)
  1230. {
  1231. if ((rd == rn) && !((rd | rm) & 8))
  1232. m_formatter.oneWordOp10Reg3Reg3(OP_ORR_reg_T1, rm, rd);
  1233. else if ((rd == rm) && !((rd | rn) & 8))
  1234. m_formatter.oneWordOp10Reg3Reg3(OP_ORR_reg_T1, rn, rd);
  1235. else
  1236. orr(rd, rn, rm, ShiftTypeAndAmount());
  1237. }
  1238. ALWAYS_INLINE void orr_S(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  1239. {
  1240. ASSERT(!BadReg(rd));
  1241. ASSERT(!BadReg(rn));
  1242. ASSERT(!BadReg(rm));
  1243. m_formatter.twoWordOp12Reg4FourFours(OP_ORR_S_reg_T2, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1244. }
  1245. void orr_S(RegisterID rd, RegisterID rn, RegisterID rm)
  1246. {
  1247. if ((rd == rn) && !((rd | rm) & 8))
  1248. m_formatter.oneWordOp10Reg3Reg3(OP_ORR_reg_T1, rm, rd);
  1249. else if ((rd == rm) && !((rd | rn) & 8))
  1250. m_formatter.oneWordOp10Reg3Reg3(OP_ORR_reg_T1, rn, rd);
  1251. else
  1252. orr_S(rd, rn, rm, ShiftTypeAndAmount());
  1253. }
  1254. ALWAYS_INLINE void ror(RegisterID rd, RegisterID rm, int32_t shiftAmount)
  1255. {
  1256. ASSERT(!BadReg(rd));
  1257. ASSERT(!BadReg(rm));
  1258. ShiftTypeAndAmount shift(SRType_ROR, shiftAmount);
  1259. m_formatter.twoWordOp16FourFours(OP_ROR_imm_T1, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1260. }
  1261. ALWAYS_INLINE void ror(RegisterID rd, RegisterID rn, RegisterID rm)
  1262. {
  1263. ASSERT(!BadReg(rd));
  1264. ASSERT(!BadReg(rn));
  1265. ASSERT(!BadReg(rm));
  1266. m_formatter.twoWordOp12Reg4FourFours(OP_ROR_reg_T2, rn, FourFours(0xf, rd, 0, rm));
  1267. }
  1268. #if CPU(APPLE_ARMV7S)
  1269. ALWAYS_INLINE void sdiv(RegisterID rd, RegisterID rn, RegisterID rm)
  1270. {
  1271. ASSERT(!BadReg(rd));
  1272. ASSERT(!BadReg(rn));
  1273. ASSERT(!BadReg(rm));
  1274. m_formatter.twoWordOp12Reg4FourFours(OP_SDIV_T1, rn, FourFours(0xf, rd, 0xf, rm));
  1275. }
  1276. #endif
  1277. ALWAYS_INLINE void smull(RegisterID rdLo, RegisterID rdHi, RegisterID rn, RegisterID rm)
  1278. {
  1279. ASSERT(!BadReg(rdLo));
  1280. ASSERT(!BadReg(rdHi));
  1281. ASSERT(!BadReg(rn));
  1282. ASSERT(!BadReg(rm));
  1283. ASSERT(rdLo != rdHi);
  1284. m_formatter.twoWordOp12Reg4FourFours(OP_SMULL_T1, rn, FourFours(rdLo, rdHi, 0, rm));
  1285. }
  1286. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1287. ALWAYS_INLINE void str(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  1288. {
  1289. ASSERT(rt != ARMRegisters::pc);
  1290. ASSERT(rn != ARMRegisters::pc);
  1291. ASSERT(imm.isUInt12());
  1292. if (!((rt | rn) & 8) && imm.isUInt7())
  1293. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_STR_imm_T1, imm.getUInt7() >> 2, rn, rt);
  1294. else if ((rn == ARMRegisters::sp) && !(rt & 8) && imm.isUInt10())
  1295. m_formatter.oneWordOp5Reg3Imm8(OP_STR_imm_T2, rt, static_cast<uint8_t>(imm.getUInt10() >> 2));
  1296. else
  1297. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_STR_imm_T3, rn, rt, imm.getUInt12());
  1298. }
  1299. // If index is set, this is a regular offset or a pre-indexed store;
  1300. // if index is not set then is is a post-index store.
  1301. //
  1302. // If wback is set rn is updated - this is a pre or post index store,
  1303. // if wback is not set this is a regular offset memory access.
  1304. //
  1305. // (-255 <= offset <= 255)
  1306. // _reg = REG[rn]
  1307. // _tmp = _reg + offset
  1308. // MEM[index ? _tmp : _reg] = REG[rt]
  1309. // if (wback) REG[rn] = _tmp
  1310. ALWAYS_INLINE void str(RegisterID rt, RegisterID rn, int offset, bool index, bool wback)
  1311. {
  1312. ASSERT(rt != ARMRegisters::pc);
  1313. ASSERT(rn != ARMRegisters::pc);
  1314. ASSERT(index || wback);
  1315. ASSERT(!wback | (rt != rn));
  1316. bool add = true;
  1317. if (offset < 0) {
  1318. add = false;
  1319. offset = -offset;
  1320. }
  1321. ASSERT((offset & ~0xff) == 0);
  1322. offset |= (wback << 8);
  1323. offset |= (add << 9);
  1324. offset |= (index << 10);
  1325. offset |= (1 << 11);
  1326. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_STR_imm_T4, rn, rt, offset);
  1327. }
  1328. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1329. ALWAYS_INLINE void str(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1330. {
  1331. ASSERT(rn != ARMRegisters::pc);
  1332. ASSERT(!BadReg(rm));
  1333. ASSERT(shift <= 3);
  1334. if (!shift && !((rt | rn | rm) & 8))
  1335. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_STR_reg_T1, rm, rn, rt);
  1336. else
  1337. m_formatter.twoWordOp12Reg4FourFours(OP_STR_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1338. }
  1339. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1340. ALWAYS_INLINE void strb(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  1341. {
  1342. ASSERT(rt != ARMRegisters::pc);
  1343. ASSERT(rn != ARMRegisters::pc);
  1344. ASSERT(imm.isUInt12());
  1345. if (!((rt | rn) & 8) && imm.isUInt7())
  1346. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_STRB_imm_T1, imm.getUInt7() >> 2, rn, rt);
  1347. else
  1348. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_STRB_imm_T2, rn, rt, imm.getUInt12());
  1349. }
  1350. // If index is set, this is a regular offset or a pre-indexed store;
  1351. // if index is not set then is is a post-index store.
  1352. //
  1353. // If wback is set rn is updated - this is a pre or post index store,
  1354. // if wback is not set this is a regular offset memory access.
  1355. //
  1356. // (-255 <= offset <= 255)
  1357. // _reg = REG[rn]
  1358. // _tmp = _reg + offset
  1359. // MEM[index ? _tmp : _reg] = REG[rt]
  1360. // if (wback) REG[rn] = _tmp
  1361. ALWAYS_INLINE void strb(RegisterID rt, RegisterID rn, int offset, bool index, bool wback)
  1362. {
  1363. ASSERT(rt != ARMRegisters::pc);
  1364. ASSERT(rn != ARMRegisters::pc);
  1365. ASSERT(index || wback);
  1366. ASSERT(!wback | (rt != rn));
  1367. bool add = true;
  1368. if (offset < 0) {
  1369. add = false;
  1370. offset = -offset;
  1371. }
  1372. ASSERT((offset & ~0xff) == 0);
  1373. offset |= (wback << 8);
  1374. offset |= (add << 9);
  1375. offset |= (index << 10);
  1376. offset |= (1 << 11);
  1377. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_STRB_imm_T3, rn, rt, offset);
  1378. }
  1379. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1380. ALWAYS_INLINE void strb(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1381. {
  1382. ASSERT(rn != ARMRegisters::pc);
  1383. ASSERT(!BadReg(rm));
  1384. ASSERT(shift <= 3);
  1385. if (!shift && !((rt | rn | rm) & 8))
  1386. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_STRB_reg_T1, rm, rn, rt);
  1387. else
  1388. m_formatter.twoWordOp12Reg4FourFours(OP_STRB_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1389. }
  1390. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1391. ALWAYS_INLINE void strh(RegisterID rt, RegisterID rn, ARMThumbImmediate imm)
  1392. {
  1393. ASSERT(rt != ARMRegisters::pc);
  1394. ASSERT(rn != ARMRegisters::pc);
  1395. ASSERT(imm.isUInt12());
  1396. if (!((rt | rn) & 8) && imm.isUInt7())
  1397. m_formatter.oneWordOp5Imm5Reg3Reg3(OP_STRH_imm_T1, imm.getUInt7() >> 2, rn, rt);
  1398. else
  1399. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_STRH_imm_T2, rn, rt, imm.getUInt12());
  1400. }
  1401. // If index is set, this is a regular offset or a pre-indexed store;
  1402. // if index is not set then is is a post-index store.
  1403. //
  1404. // If wback is set rn is updated - this is a pre or post index store,
  1405. // if wback is not set this is a regular offset memory access.
  1406. //
  1407. // (-255 <= offset <= 255)
  1408. // _reg = REG[rn]
  1409. // _tmp = _reg + offset
  1410. // MEM[index ? _tmp : _reg] = REG[rt]
  1411. // if (wback) REG[rn] = _tmp
  1412. ALWAYS_INLINE void strh(RegisterID rt, RegisterID rn, int offset, bool index, bool wback)
  1413. {
  1414. ASSERT(rt != ARMRegisters::pc);
  1415. ASSERT(rn != ARMRegisters::pc);
  1416. ASSERT(index || wback);
  1417. ASSERT(!wback | (rt != rn));
  1418. bool add = true;
  1419. if (offset < 0) {
  1420. add = false;
  1421. offset = -offset;
  1422. }
  1423. ASSERT(!(offset & ~0xff));
  1424. offset |= (wback << 8);
  1425. offset |= (add << 9);
  1426. offset |= (index << 10);
  1427. offset |= (1 << 11);
  1428. m_formatter.twoWordOp12Reg4Reg4Imm12(OP_STRH_imm_T3, rn, rt, offset);
  1429. }
  1430. // rt == ARMRegisters::pc only allowed if last instruction in IT (if then) block.
  1431. ALWAYS_INLINE void strh(RegisterID rt, RegisterID rn, RegisterID rm, unsigned shift = 0)
  1432. {
  1433. ASSERT(rn != ARMRegisters::pc);
  1434. ASSERT(!BadReg(rm));
  1435. ASSERT(shift <= 3);
  1436. if (!shift && !((rt | rn | rm) & 8))
  1437. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_STRH_reg_T1, rm, rn, rt);
  1438. else
  1439. m_formatter.twoWordOp12Reg4FourFours(OP_STRH_reg_T2, rn, FourFours(rt, 0, shift, rm));
  1440. }
  1441. ALWAYS_INLINE void sub(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  1442. {
  1443. // Rd can only be SP if Rn is also SP.
  1444. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  1445. ASSERT(rd != ARMRegisters::pc);
  1446. ASSERT(rn != ARMRegisters::pc);
  1447. ASSERT(imm.isValid());
  1448. if ((rn == ARMRegisters::sp) && (rd == ARMRegisters::sp) && imm.isUInt9()) {
  1449. ASSERT(!(imm.getUInt16() & 3));
  1450. m_formatter.oneWordOp9Imm7(OP_SUB_SP_imm_T1, static_cast<uint8_t>(imm.getUInt9() >> 2));
  1451. return;
  1452. } else if (!((rd | rn) & 8)) {
  1453. if (imm.isUInt3()) {
  1454. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_SUB_imm_T1, (RegisterID)imm.getUInt3(), rn, rd);
  1455. return;
  1456. } else if ((rd == rn) && imm.isUInt8()) {
  1457. m_formatter.oneWordOp5Reg3Imm8(OP_SUB_imm_T2, rd, imm.getUInt8());
  1458. return;
  1459. }
  1460. }
  1461. if (imm.isEncodedImm())
  1462. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_SUB_imm_T3, rn, rd, imm);
  1463. else {
  1464. ASSERT(imm.isUInt12());
  1465. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_SUB_imm_T4, rn, rd, imm);
  1466. }
  1467. }
  1468. ALWAYS_INLINE void sub(RegisterID rd, ARMThumbImmediate imm, RegisterID rn)
  1469. {
  1470. ASSERT(rd != ARMRegisters::pc);
  1471. ASSERT(rn != ARMRegisters::pc);
  1472. ASSERT(imm.isValid());
  1473. ASSERT(imm.isUInt12());
  1474. if (!((rd | rn) & 8) && !imm.getUInt12())
  1475. m_formatter.oneWordOp10Reg3Reg3(OP_RSB_imm_T1, rn, rd);
  1476. else
  1477. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_RSB_imm_T2, rn, rd, imm);
  1478. }
  1479. ALWAYS_INLINE void sub(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  1480. {
  1481. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  1482. ASSERT(rd != ARMRegisters::pc);
  1483. ASSERT(rn != ARMRegisters::pc);
  1484. ASSERT(!BadReg(rm));
  1485. m_formatter.twoWordOp12Reg4FourFours(OP_SUB_reg_T2, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1486. }
  1487. // NOTE: In an IT block, add doesn't modify the flags register.
  1488. ALWAYS_INLINE void sub(RegisterID rd, RegisterID rn, RegisterID rm)
  1489. {
  1490. if (!((rd | rn | rm) & 8))
  1491. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_SUB_reg_T1, rm, rn, rd);
  1492. else
  1493. sub(rd, rn, rm, ShiftTypeAndAmount());
  1494. }
  1495. // Not allowed in an IT (if then) block.
  1496. void sub_S(RegisterID rd, RegisterID rn, ARMThumbImmediate imm)
  1497. {
  1498. // Rd can only be SP if Rn is also SP.
  1499. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  1500. ASSERT(rd != ARMRegisters::pc);
  1501. ASSERT(rn != ARMRegisters::pc);
  1502. ASSERT(imm.isValid());
  1503. if ((rn == ARMRegisters::sp) && (rd == ARMRegisters::sp) && imm.isUInt9()) {
  1504. ASSERT(!(imm.getUInt16() & 3));
  1505. m_formatter.oneWordOp9Imm7(OP_SUB_SP_imm_T1, static_cast<uint8_t>(imm.getUInt9() >> 2));
  1506. return;
  1507. } else if (!((rd | rn) & 8)) {
  1508. if (imm.isUInt3()) {
  1509. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_SUB_imm_T1, (RegisterID)imm.getUInt3(), rn, rd);
  1510. return;
  1511. } else if ((rd == rn) && imm.isUInt8()) {
  1512. m_formatter.oneWordOp5Reg3Imm8(OP_SUB_imm_T2, rd, imm.getUInt8());
  1513. return;
  1514. }
  1515. }
  1516. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_SUB_S_imm_T3, rn, rd, imm);
  1517. }
  1518. ALWAYS_INLINE void sub_S(RegisterID rd, ARMThumbImmediate imm, RegisterID rn)
  1519. {
  1520. ASSERT(rd != ARMRegisters::pc);
  1521. ASSERT(rn != ARMRegisters::pc);
  1522. ASSERT(imm.isValid());
  1523. ASSERT(imm.isUInt12());
  1524. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_RSB_S_imm_T2, rn, rd, imm);
  1525. }
  1526. // Not allowed in an IT (if then) block?
  1527. ALWAYS_INLINE void sub_S(RegisterID rd, RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  1528. {
  1529. ASSERT((rd != ARMRegisters::sp) || (rn == ARMRegisters::sp));
  1530. ASSERT(rd != ARMRegisters::pc);
  1531. ASSERT(rn != ARMRegisters::pc);
  1532. ASSERT(!BadReg(rm));
  1533. m_formatter.twoWordOp12Reg4FourFours(OP_SUB_S_reg_T2, rn, FourFours(shift.hi4(), rd, shift.lo4(), rm));
  1534. }
  1535. // Not allowed in an IT (if then) block.
  1536. ALWAYS_INLINE void sub_S(RegisterID rd, RegisterID rn, RegisterID rm)
  1537. {
  1538. if (!((rd | rn | rm) & 8))
  1539. m_formatter.oneWordOp7Reg3Reg3Reg3(OP_SUB_reg_T1, rm, rn, rd);
  1540. else
  1541. sub_S(rd, rn, rm, ShiftTypeAndAmount());
  1542. }
  1543. ALWAYS_INLINE void tst(RegisterID rn, ARMThumbImmediate imm)
  1544. {
  1545. ASSERT(!BadReg(rn));
  1546. ASSERT(imm.isEncodedImm());
  1547. m_formatter.twoWordOp5i6Imm4Reg4EncodedImm(OP_TST_imm, rn, (RegisterID)0xf, imm);
  1548. }
  1549. ALWAYS_INLINE void tst(RegisterID rn, RegisterID rm, ShiftTypeAndAmount shift)
  1550. {
  1551. ASSERT(!BadReg(rn));
  1552. ASSERT(!BadReg(rm));
  1553. m_formatter.twoWordOp12Reg4FourFours(OP_TST_reg_T2, rn, FourFours(shift.hi4(), 0xf, shift.lo4(), rm));
  1554. }
  1555. ALWAYS_INLINE void tst(RegisterID rn, RegisterID rm)
  1556. {
  1557. if ((rn | rm) & 8)
  1558. tst(rn, rm, ShiftTypeAndAmount());
  1559. else
  1560. m_formatter.oneWordOp10Reg3Reg3(OP_TST_reg_T1, rm, rn);
  1561. }
  1562. ALWAYS_INLINE void ubfx(RegisterID rd, RegisterID rn, unsigned lsb, unsigned width)
  1563. {
  1564. ASSERT(lsb < 32);
  1565. ASSERT((width >= 1) && (width <= 32));
  1566. ASSERT((lsb + width) <= 32);
  1567. m_formatter.twoWordOp12Reg40Imm3Reg4Imm20Imm5(OP_UBFX_T1, rd, rn, (lsb & 0x1c) << 10, (lsb & 0x3) << 6, (width - 1) & 0x1f);
  1568. }
  1569. #if CPU(APPLE_ARMV7S)
  1570. ALWAYS_INLINE void udiv(RegisterID rd, RegisterID rn, RegisterID rm)
  1571. {
  1572. ASSERT(!BadReg(rd));
  1573. ASSERT(!BadReg(rn));
  1574. ASSERT(!BadReg(rm));
  1575. m_formatter.twoWordOp12Reg4FourFours(OP_UDIV_T1, rn, FourFours(0xf, rd, 0xf, rm));
  1576. }
  1577. #endif
  1578. void vadd(FPDoubleRegisterID rd, FPDoubleRegisterID rn, FPDoubleRegisterID rm)
  1579. {
  1580. m_formatter.vfpOp(OP_VADD_T2, OP_VADD_T2b, true, rn, rd, rm);
  1581. }
  1582. void vcmp(FPDoubleRegisterID rd, FPDoubleRegisterID rm)
  1583. {
  1584. m_formatter.vfpOp(OP_VCMP, OP_VCMPb, true, VFPOperand(4), rd, rm);
  1585. }
  1586. void vcmpz(FPDoubleRegisterID rd)
  1587. {
  1588. m_formatter.vfpOp(OP_VCMP, OP_VCMPb, true, VFPOperand(5), rd, VFPOperand(0));
  1589. }
  1590. void vcvt_signedToFloatingPoint(FPDoubleRegisterID rd, FPSingleRegisterID rm)
  1591. {
  1592. // boolean values are 64bit (toInt, unsigned, roundZero)
  1593. m_formatter.vfpOp(OP_VCVT_FPIVFP, OP_VCVT_FPIVFPb, true, vcvtOp(false, false, false), rd, rm);
  1594. }
  1595. void vcvt_floatingPointToSigned(FPSingleRegisterID rd, FPDoubleRegisterID rm)
  1596. {
  1597. // boolean values are 64bit (toInt, unsigned, roundZero)
  1598. m_formatter.vfpOp(OP_VCVT_FPIVFP, OP_VCVT_FPIVFPb, true, vcvtOp(true, false, true), rd, rm);
  1599. }
  1600. void vcvt_floatingPointToUnsigned(FPSingleRegisterID rd, FPDoubleRegisterID rm)
  1601. {
  1602. // boolean values are 64bit (toInt, unsigned, roundZero)
  1603. m_formatter.vfpOp(OP_VCVT_FPIVFP, OP_VCVT_FPIVFPb, true, vcvtOp(true, true, true), rd, rm);
  1604. }
  1605. void vdiv(FPDoubleRegisterID rd, FPDoubleRegisterID rn, FPDoubleRegisterID rm)
  1606. {
  1607. m_formatter.vfpOp(OP_VDIV, OP_VDIVb, true, rn, rd, rm);
  1608. }
  1609. void vldr(FPDoubleRegisterID rd, RegisterID rn, int32_t imm)
  1610. {
  1611. m_formatter.vfpMemOp(OP_VLDR, OP_VLDRb, true, rn, rd, imm);
  1612. }
  1613. void flds(FPSingleRegisterID rd, RegisterID rn, int32_t imm)
  1614. {
  1615. m_formatter.vfpMemOp(OP_FLDS, OP_FLDSb, false, rn, rd, imm);
  1616. }
  1617. void vmov(RegisterID rd, FPSingleRegisterID rn)
  1618. {
  1619. ASSERT(!BadReg(rd));
  1620. m_formatter.vfpOp(OP_VMOV_StoC, OP_VMOV_StoCb, false, rn, rd, VFPOperand(0));
  1621. }
  1622. void vmov(FPSingleRegisterID rd, RegisterID rn)
  1623. {
  1624. ASSERT(!BadReg(rn));
  1625. m_formatter.vfpOp(OP_VMOV_CtoS, OP_VMOV_CtoSb, false, rd, rn, VFPOperand(0));
  1626. }
  1627. void vmov(RegisterID rd1, RegisterID rd2, FPDoubleRegisterID rn)
  1628. {
  1629. ASSERT(!BadReg(rd1));
  1630. ASSERT(!BadReg(rd2));
  1631. m_formatter.vfpOp(OP_VMOV_DtoC, OP_VMOV_DtoCb, true, rd2, VFPOperand(rd1 | 16), rn);
  1632. }
  1633. void vmov(FPDoubleRegisterID rd, RegisterID rn1, RegisterID rn2)
  1634. {
  1635. ASSERT(!BadReg(rn1));
  1636. ASSERT(!BadReg(rn2));
  1637. m_formatter.vfpOp(OP_VMOV_CtoD, OP_VMOV_CtoDb, true, rn2, VFPOperand(rn1 | 16), rd);
  1638. }
  1639. void vmov(FPDoubleRegisterID rd, FPDoubleRegisterID rn)
  1640. {
  1641. m_formatter.vfpOp(OP_VMOV_T2, OP_VMOV_T2b, true, VFPOperand(0), rd, rn);
  1642. }
  1643. void vmrs(RegisterID reg = ARMRegisters::pc)
  1644. {
  1645. ASSERT(reg != ARMRegisters::sp);
  1646. m_formatter.vfpOp(OP_VMRS, OP_VMRSb, false, VFPOperand(1), VFPOperand(0x10 | reg), VFPOperand(0));
  1647. }
  1648. void vmul(FPDoubleRegisterID rd, FPDoubleRegisterID rn, FPDoubleRegisterID rm)
  1649. {
  1650. m_formatter.vfpOp(OP_VMUL_T2, OP_VMUL_T2b, true, rn, rd, rm);
  1651. }
  1652. void vstr(FPDoubleRegisterID rd, RegisterID rn, int32_t imm)
  1653. {
  1654. m_formatter.vfpMemOp(OP_VSTR, OP_VSTRb, true, rn, rd, imm);
  1655. }
  1656. void fsts(FPSingleRegisterID rd, RegisterID rn, int32_t imm)
  1657. {
  1658. m_formatter.vfpMemOp(OP_FSTS, OP_FSTSb, false, rn, rd, imm);
  1659. }
  1660. void vsub(FPDoubleRegisterID rd, FPDoubleRegisterID rn, FPDoubleRegisterID rm)
  1661. {
  1662. m_formatter.vfpOp(OP_VSUB_T2, OP_VSUB_T2b, true, rn, rd, rm);
  1663. }
  1664. void vabs(FPDoubleRegisterID rd, FPDoubleRegisterID rm)
  1665. {
  1666. m_formatter.vfpOp(OP_VABS_T2, OP_VABS_T2b, true, VFPOperand(16), rd, rm);
  1667. }
  1668. void vneg(FPDoubleRegisterID rd, FPDoubleRegisterID rm)
  1669. {
  1670. m_formatter.vfpOp(OP_VNEG_T2, OP_VNEG_T2b, true, VFPOperand(1), rd, rm);
  1671. }
  1672. void vsqrt(FPDoubleRegisterID rd, FPDoubleRegisterID rm)
  1673. {
  1674. m_formatter.vfpOp(OP_VSQRT_T1, OP_VSQRT_T1b, true, VFPOperand(17), rd, rm);
  1675. }
  1676. void vcvtds(FPDoubleRegisterID rd, FPSingleRegisterID rm)
  1677. {
  1678. m_formatter.vfpOp(OP_VCVTDS_T1, OP_VCVTDS_T1b, false, VFPOperand(23), rd, rm);
  1679. }
  1680. void vcvtsd(FPSingleRegisterID rd, FPDoubleRegisterID rm)
  1681. {
  1682. m_formatter.vfpOp(OP_VCVTSD_T1, OP_VCVTSD_T1b, true, VFPOperand(23), rd, rm);
  1683. }
  1684. void nop()
  1685. {
  1686. m_formatter.oneWordOp8Imm8(OP_NOP_T1, 0);
  1687. }
  1688. void nopw()
  1689. {
  1690. m_formatter.twoWordOp16Op16(OP_NOP_T2a, OP_NOP_T2b);
  1691. }
  1692. AssemblerLabel labelIgnoringWatchpoints()
  1693. {
  1694. return m_formatter.label();
  1695. }
  1696. AssemblerLabel labelForWatchpoint()
  1697. {
  1698. AssemblerLabel result = m_formatter.label();
  1699. if (static_cast<int>(result.m_offset) != m_indexOfLastWatchpoint)
  1700. result = label();
  1701. m_indexOfLastWatchpoint = result.m_offset;
  1702. m_indexOfTailOfLastWatchpoint = result.m_offset + maxJumpReplacementSize();
  1703. return result;
  1704. }
  1705. AssemblerLabel label()
  1706. {
  1707. AssemblerLabel result = m_formatter.label();
  1708. while (UNLIKELY(static_cast<int>(result.m_offset) < m_indexOfTailOfLastWatchpoint)) {
  1709. if (UNLIKELY(static_cast<int>(result.m_offset) + 4 <= m_indexOfTailOfLastWatchpoint))
  1710. nopw();
  1711. else
  1712. nop();
  1713. result = m_formatter.label();
  1714. }
  1715. return result;
  1716. }
  1717. AssemblerLabel align(int alignment)
  1718. {
  1719. while (!m_formatter.isAligned(alignment))
  1720. bkpt();
  1721. return label();
  1722. }
  1723. static void* getRelocatedAddress(void* code, AssemblerLabel label)
  1724. {
  1725. ASSERT(label.isSet());
  1726. return reinterpret_cast<void*>(reinterpret_cast<ptrdiff_t>(code) + label.m_offset);
  1727. }
  1728. static int getDifferenceBetweenLabels(AssemblerLabel a, AssemblerLabel b)
  1729. {
  1730. return b.m_offset - a.m_offset;
  1731. }
  1732. int executableOffsetFor(int location)
  1733. {
  1734. if (!location)
  1735. return 0;
  1736. return static_cast<int32_t*>(m_formatter.data())[location / sizeof(int32_t) - 1];
  1737. }
  1738. int jumpSizeDelta(JumpType jumpType, JumpLinkType jumpLinkType) { return JUMP_ENUM_SIZE(jumpType) - JUMP_ENUM_SIZE(jumpLinkType); }
  1739. // Assembler admin methods:
  1740. static ALWAYS_INLINE bool linkRecordSourceComparator(const LinkRecord& a, const LinkRecord& b)
  1741. {
  1742. return a.from() < b.from();
  1743. }
  1744. bool canCompact(JumpType jumpType)
  1745. {
  1746. // The following cannot be compacted:
  1747. // JumpFixed: represents custom jump sequence
  1748. // JumpNoConditionFixedSize: represents unconditional jump that must remain a fixed size
  1749. // JumpConditionFixedSize: represents conditional jump that must remain a fixed size
  1750. return (jumpType == JumpNoCondition) || (jumpType == JumpCondition);
  1751. }
  1752. JumpLinkType computeJumpType(JumpType jumpType, const uint8_t* from, const uint8_t* to)
  1753. {
  1754. if (jumpType == JumpFixed)
  1755. return LinkInvalid;
  1756. // for patchable jump we must leave space for the longest code sequence
  1757. if (jumpType == JumpNoConditionFixedSize)
  1758. return LinkBX;
  1759. if (jumpType == JumpConditionFixedSize)
  1760. return LinkConditionalBX;
  1761. const int paddingSize = JUMP_ENUM_SIZE(jumpType);
  1762. if (jumpType == JumpCondition) {
  1763. // 2-byte conditional T1
  1764. const uint16_t* jumpT1Location = reinterpret_cast_ptr<const uint16_t*>(from - (paddingSize - JUMP_ENUM_SIZE(LinkJumpT1)));
  1765. if (canBeJumpT1(jumpT1Location, to))
  1766. return LinkJumpT1;
  1767. // 4-byte conditional T3
  1768. const uint16_t* jumpT3Location = reinterpret_cast_ptr<const uint16_t*>(from - (paddingSize - JUMP_ENUM_SIZE(LinkJumpT3)));
  1769. if (canBeJumpT3(jumpT3Location, to))
  1770. return LinkJumpT3;
  1771. // 4-byte conditional T4 with IT
  1772. const uint16_t* conditionalJumpT4Location =
  1773. reinterpret_cast_ptr<const uint16_t*>(from - (paddingSize - JUMP_ENUM_SIZE(LinkConditionalJumpT4)));
  1774. if (canBeJumpT4(conditionalJumpT4Location, to))
  1775. return LinkConditionalJumpT4;
  1776. } else {
  1777. // 2-byte unconditional T2
  1778. const uint16_t* jumpT2Location = reinterpret_cast_ptr<const uint16_t*>(from - (paddingSize - JUMP_ENUM_SIZE(LinkJumpT2)));
  1779. if (canBeJumpT2(jumpT2Location, to))
  1780. return LinkJumpT2;
  1781. // 4-byte unconditional T4
  1782. const uint16_t* jumpT4Location = reinterpret_cast_ptr<const uint16_t*>(from - (paddingSize - JUMP_ENUM_SIZE(LinkJumpT4)));
  1783. if (canBeJumpT4(jumpT4Location, to))
  1784. return LinkJumpT4;
  1785. // use long jump sequence
  1786. return LinkBX;
  1787. }
  1788. ASSERT(jumpType == JumpCondition);
  1789. return LinkConditionalBX;
  1790. }
  1791. JumpLinkType computeJumpType(LinkRecord& record, const uint8_t* from, const uint8_t* to)
  1792. {
  1793. JumpLinkType linkType = computeJumpType(record.type(), from, to);
  1794. record.setLinkType(linkType);
  1795. return linkType;
  1796. }
  1797. void recordLinkOffsets(int32_t regionStart, int32_t regionEnd, int32_t offset)
  1798. {
  1799. int32_t ptr = regionStart / sizeof(int32_t);
  1800. const int32_t end = regionEnd / sizeof(int32_t);
  1801. int32_t* offsets = static_cast<int32_t*>(m_formatter.data());
  1802. while (ptr < end)
  1803. offsets[ptr++] = offset;
  1804. }
  1805. Vector<LinkRecord, 0, UnsafeVectorOverflow>& jumpsToLink()
  1806. {
  1807. std::sort(m_jumpsToLink.begin(), m_jumpsToLink.end(), linkRecordSourceComparator);
  1808. return m_jumpsToLink;
  1809. }
  1810. void ALWAYS_INLINE link(LinkRecord& record, uint8_t* from, uint8_t* to)
  1811. {
  1812. switch (record.linkType()) {
  1813. case LinkJumpT1:
  1814. linkJumpT1(record.condition(), reinterpret_cast_ptr<uint16_t*>(from), to);
  1815. break;
  1816. case LinkJumpT2:
  1817. linkJumpT2(reinterpret_cast_ptr<uint16_t*>(from), to);
  1818. break;
  1819. case LinkJumpT3:
  1820. linkJumpT3(record.condition(), reinterpret_cast_ptr<uint16_t*>(from), to);
  1821. break;
  1822. case LinkJumpT4:
  1823. linkJumpT4(reinterpret_cast_ptr<uint16_t*>(from), to);
  1824. break;
  1825. case LinkConditionalJumpT4:
  1826. linkConditionalJumpT4(record.condition(), reinterpret_cast_ptr<uint16_t*>(from), to);
  1827. break;
  1828. case LinkConditionalBX:
  1829. linkConditionalBX(record.condition(), reinterpret_cast_ptr<uint16_t*>(from), to);
  1830. break;
  1831. case LinkBX:
  1832. linkBX(reinterpret_cast_ptr<uint16_t*>(from), to);
  1833. break;
  1834. default:
  1835. RELEASE_ASSERT_NOT_REACHED();
  1836. break;
  1837. }
  1838. }
  1839. void* unlinkedCode() { return m_formatter.data(); }
  1840. size_t codeSize() const { return m_formatter.codeSize(); }
  1841. static unsigned getCallReturnOffset(AssemblerLabel call)
  1842. {
  1843. ASSERT(call.isSet());
  1844. return call.m_offset;
  1845. }
  1846. // Linking & patching:
  1847. //
  1848. // 'link' and 'patch' methods are for use on unprotected code - such as the code
  1849. // within the AssemblerBuffer, and code being patched by the patch buffer. Once
  1850. // code has been finalized it is (platform support permitting) within a non-
  1851. // writable region of memory; to modify the code in an execute-only execuable
  1852. // pool the 'repatch' and 'relink' methods should be used.
  1853. void linkJump(AssemblerLabel from, AssemblerLabel to, JumpType type, Condition condition)
  1854. {
  1855. ASSERT(to.isSet());
  1856. ASSERT(from.isSet());
  1857. m_jumpsToLink.append(LinkRecord(from.m_offset, to.m_offset, type, condition));
  1858. }
  1859. static void linkJump(void* code, AssemblerLabel from, void* to)
  1860. {
  1861. ASSERT(from.isSet());
  1862. uint16_t* location = reinterpret_cast<uint16_t*>(reinterpret_cast<intptr_t>(code) + from.m_offset);
  1863. linkJumpAbsolute(location, to);
  1864. }
  1865. static void linkCall(void* code, AssemblerLabel from, void* to)
  1866. {
  1867. ASSERT(!(reinterpret_cast<intptr_t>(code) & 1));
  1868. ASSERT(from.isSet());
  1869. ASSERT(reinterpret_cast<intptr_t>(to) & 1);
  1870. setPointer(reinterpret_cast<uint16_t*>(reinterpret_cast<intptr_t>(code) + from.m_offset) - 1, to, false);
  1871. }
  1872. static void linkPointer(void* code, AssemblerLabel where, void* value)
  1873. {
  1874. setPointer(reinterpret_cast<char*>(code) + where.m_offset, value, false);
  1875. }
  1876. static void relinkJump(void* from, void* to)
  1877. {
  1878. ASSERT(!(reinterpret_cast<intptr_t>(from) & 1));
  1879. ASSERT(!(reinterpret_cast<intptr_t>(to) & 1));
  1880. linkJumpAbsolute(reinterpret_cast<uint16_t*>(from), to);
  1881. cacheFlush(reinterpret_cast<uint16_t*>(from) - 5, 5 * sizeof(uint16_t));
  1882. }
  1883. static void relinkCall(void* from, void* to)
  1884. {
  1885. ASSERT(!(reinterpret_cast<intptr_t>(from) & 1));
  1886. ASSERT(reinterpret_cast<intptr_t>(to) & 1);
  1887. setPointer(reinterpret_cast<uint16_t*>(from) - 1, to, true);
  1888. }
  1889. static void* readCallTarget(void* from)
  1890. {
  1891. return readPointer(reinterpret_cast<uint16_t*>(from) - 1);
  1892. }
  1893. static void repatchInt32(void* where, int32_t value)
  1894. {
  1895. ASSERT(!(reinterpret_cast<intptr_t>(where) & 1));
  1896. setInt32(where, value, true);
  1897. }
  1898. static void repatchCompact(void* where, int32_t offset)
  1899. {
  1900. ASSERT(offset >= -255 && offset <= 255);
  1901. bool add = true;
  1902. if (offset < 0) {
  1903. add = false;
  1904. offset = -offset;
  1905. }
  1906. offset |= (add << 9);
  1907. offset |= (1 << 10);
  1908. offset |= (1 << 11);
  1909. uint16_t* location = reinterpret_cast<uint16_t*>(where);
  1910. location[1] &= ~((1 << 12) - 1);
  1911. location[1] |= offset;
  1912. cacheFlush(location, sizeof(uint16_t) * 2);
  1913. }
  1914. static void repatchPointer(void* where, void* value)
  1915. {
  1916. ASSERT(!(reinterpret_cast<intptr_t>(where) & 1));
  1917. setPointer(where, value, true);
  1918. }
  1919. static void* readPointer(void* where)
  1920. {
  1921. return reinterpret_cast<void*>(readInt32(where));
  1922. }
  1923. static void replaceWithJump(void* instructionStart, void* to)
  1924. {
  1925. ASSERT(!(bitwise_cast<uintptr_t>(instructionStart) & 1));
  1926. ASSERT(!(bitwise_cast<uintptr_t>(to) & 1));
  1927. #if OS(LINUX) || OS(QNX) || OS(PSP2)
  1928. if (canBeJumpT4(reinterpret_cast<uint16_t*>(instructionStart), to)) {
  1929. uint16_t* ptr = reinterpret_cast<uint16_t*>(instructionStart) + 2;
  1930. linkJumpT4(ptr, to);
  1931. cacheFlush(ptr - 2, sizeof(uint16_t) * 2);
  1932. } else {
  1933. uint16_t* ptr = reinterpret_cast<uint16_t*>(instructionStart) + 5;
  1934. linkBX(ptr, to);
  1935. cacheFlush(ptr - 5, sizeof(uint16_t) * 5);
  1936. }
  1937. #else
  1938. uint16_t* ptr = reinterpret_cast<uint16_t*>(instructionStart) + 2;
  1939. linkJumpT4(ptr, to);
  1940. cacheFlush(ptr - 2, sizeof(uint16_t) * 2);
  1941. #endif
  1942. }
  1943. static ptrdiff_t maxJumpReplacementSize()
  1944. {
  1945. #if OS(LINUX) || OS(QNX) || OS(PSP2)
  1946. return 10;
  1947. #else
  1948. return 4;
  1949. #endif
  1950. }
  1951. static void replaceWithLoad(void* instructionStart)
  1952. {
  1953. ASSERT(!(bitwise_cast<uintptr_t>(instructionStart) & 1));
  1954. uint16_t* ptr = reinterpret_cast<uint16_t*>(instructionStart);
  1955. switch (ptr[0] & 0xFFF0) {
  1956. case OP_LDR_imm_T3:
  1957. break;
  1958. case OP_ADD_imm_T3:
  1959. ASSERT(!(ptr[1] & 0xF000));
  1960. ptr[0] &= 0x000F;
  1961. ptr[0] |= OP_LDR_imm_T3;
  1962. ptr[1] |= (ptr[1] & 0x0F00) << 4;
  1963. ptr[1] &= 0xF0FF;
  1964. cacheFlush(ptr, sizeof(uint16_t) * 2);
  1965. break;
  1966. default:
  1967. RELEASE_ASSERT_NOT_REACHED();
  1968. }
  1969. }
  1970. static void replaceWithAddressComputation(void* instructionStart)
  1971. {
  1972. ASSERT(!(bitwise_cast<uintptr_t>(instructionStart) & 1));
  1973. uint16_t* ptr = reinterpret_cast<uint16_t*>(instructionStart);
  1974. switch (ptr[0] & 0xFFF0) {
  1975. case OP_LDR_imm_T3:
  1976. ASSERT(!(ptr[1] & 0x0F00));
  1977. ptr[0] &= 0x000F;
  1978. ptr[0] |= OP_ADD_imm_T3;
  1979. ptr[1] |= (ptr[1] & 0xF000) >> 4;
  1980. ptr[1] &= 0x0FFF;
  1981. cacheFlush(ptr, sizeof(uint16_t) * 2);
  1982. break;
  1983. case OP_ADD_imm_T3:
  1984. break;
  1985. default:
  1986. RELEASE_ASSERT_NOT_REACHED();
  1987. }
  1988. }
  1989. unsigned debugOffset() { return m_formatter.debugOffset(); }
  1990. #if OS(LINUX)
  1991. static inline void linuxPageFlush(uintptr_t begin, uintptr_t end)
  1992. {
  1993. asm volatile(
  1994. "push {r7}\n"
  1995. "mov r0, %0\n"
  1996. "mov r1, %1\n"
  1997. "movw r7, #0x2\n"
  1998. "movt r7, #0xf\n"
  1999. "movs r2, #0x0\n"
  2000. "svc 0x0\n"
  2001. "pop {r7}\n"
  2002. :
  2003. : "r" (begin), "r" (end)
  2004. : "r0", "r1", "r2");
  2005. }
  2006. #endif
  2007. static void cacheFlush(void* code, size_t size)
  2008. {
  2009. #if OS(IOS)
  2010. sys_cache_control(kCacheFunctionPrepareForExecution, code, size);
  2011. #elif OS(LINUX)
  2012. size_t page = pageSize();
  2013. uintptr_t current = reinterpret_cast<uintptr_t>(code);
  2014. uintptr_t end = current + size;
  2015. uintptr_t firstPageEnd = (current & ~(page - 1)) + page;
  2016. if (end <= firstPageEnd) {
  2017. linuxPageFlush(current, end);
  2018. return;
  2019. }
  2020. linuxPageFlush(current, firstPageEnd);
  2021. for (current = firstPageEnd; current + page < end; current += page)
  2022. linuxPageFlush(current, current + page);
  2023. linuxPageFlush(current, end);
  2024. #elif OS(WINCE)
  2025. CacheRangeFlush(code, size, CACHE_SYNC_ALL);
  2026. #elif OS(QNX)
  2027. #if !ENABLE(ASSEMBLER_WX_EXCLUSIVE)
  2028. msync(code, size, MS_INVALIDATE_ICACHE);
  2029. #else
  2030. UNUSED_PARAM(code);
  2031. UNUSED_PARAM(size);
  2032. #endif
  2033. #elif PLATFORM(MANX)
  2034. Manx::Memblock::cacheFlush(code, size);
  2035. #else
  2036. #error "The cacheFlush support is missing on this platform."
  2037. #endif
  2038. }
  2039. private:
  2040. // VFP operations commonly take one or more 5-bit operands, typically representing a
  2041. // floating point register number. This will commonly be encoded in the instruction
  2042. // in two parts, with one single bit field, and one 4-bit field. In the case of
  2043. // double precision operands the high bit of the register number will be encoded
  2044. // separately, and for single precision operands the high bit of the register number
  2045. // will be encoded individually.
  2046. // VFPOperand encapsulates a 5-bit VFP operand, with bits 0..3 containing the 4-bit
  2047. // field to be encoded together in the instruction (the low 4-bits of a double
  2048. // register number, or the high 4-bits of a single register number), and bit 4
  2049. // contains the bit value to be encoded individually.
  2050. struct VFPOperand {
  2051. explicit VFPOperand(uint32_t value)
  2052. : m_value(value)
  2053. {
  2054. ASSERT(!(m_value & ~0x1f));
  2055. }
  2056. VFPOperand(FPDoubleRegisterID reg)
  2057. : m_value(reg)
  2058. {
  2059. }
  2060. VFPOperand(RegisterID reg)
  2061. : m_value(reg)
  2062. {
  2063. }
  2064. VFPOperand(FPSingleRegisterID reg)
  2065. : m_value(((reg & 1) << 4) | (reg >> 1)) // rotate the lowest bit of 'reg' to the top.
  2066. {
  2067. }
  2068. uint32_t bits1()
  2069. {
  2070. return m_value >> 4;
  2071. }
  2072. uint32_t bits4()
  2073. {
  2074. return m_value & 0xf;
  2075. }
  2076. uint32_t m_value;
  2077. };
  2078. VFPOperand vcvtOp(bool toInteger, bool isUnsigned, bool isRoundZero)
  2079. {
  2080. // Cannot specify rounding when converting to float.
  2081. ASSERT(toInteger || !isRoundZero);
  2082. uint32_t op = 0x8;
  2083. if (toInteger) {
  2084. // opc2 indicates both toInteger & isUnsigned.
  2085. op |= isUnsigned ? 0x4 : 0x5;
  2086. // 'op' field in instruction is isRoundZero
  2087. if (isRoundZero)
  2088. op |= 0x10;
  2089. } else {
  2090. ASSERT(!isRoundZero);
  2091. // 'op' field in instruction is isUnsigned
  2092. if (!isUnsigned)
  2093. op |= 0x10;
  2094. }
  2095. return VFPOperand(op);
  2096. }
  2097. static void setInt32(void* code, uint32_t value, bool flush)
  2098. {
  2099. uint16_t* location = reinterpret_cast<uint16_t*>(code);
  2100. ASSERT(isMOV_imm_T3(location - 4) && isMOVT(location - 2));
  2101. ARMThumbImmediate lo16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(value));
  2102. ARMThumbImmediate hi16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(value >> 16));
  2103. location[-4] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOV_imm_T3, lo16);
  2104. location[-3] = twoWordOp5i6Imm4Reg4EncodedImmSecond((location[-3] >> 8) & 0xf, lo16);
  2105. location[-2] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOVT, hi16);
  2106. location[-1] = twoWordOp5i6Imm4Reg4EncodedImmSecond((location[-1] >> 8) & 0xf, hi16);
  2107. if (flush)
  2108. cacheFlush(location - 4, 4 * sizeof(uint16_t));
  2109. }
  2110. static int32_t readInt32(void* code)
  2111. {
  2112. uint16_t* location = reinterpret_cast<uint16_t*>(code);
  2113. ASSERT(isMOV_imm_T3(location - 4) && isMOVT(location - 2));
  2114. ARMThumbImmediate lo16;
  2115. ARMThumbImmediate hi16;
  2116. decodeTwoWordOp5i6Imm4Reg4EncodedImmFirst(lo16, location[-4]);
  2117. decodeTwoWordOp5i6Imm4Reg4EncodedImmSecond(lo16, location[-3]);
  2118. decodeTwoWordOp5i6Imm4Reg4EncodedImmFirst(hi16, location[-2]);
  2119. decodeTwoWordOp5i6Imm4Reg4EncodedImmSecond(hi16, location[-1]);
  2120. uint32_t result = hi16.asUInt16();
  2121. result <<= 16;
  2122. result |= lo16.asUInt16();
  2123. return static_cast<int32_t>(result);
  2124. }
  2125. static void setUInt7ForLoad(void* code, ARMThumbImmediate imm)
  2126. {
  2127. // Requires us to have planted a LDR_imm_T1
  2128. ASSERT(imm.isValid());
  2129. ASSERT(imm.isUInt7());
  2130. uint16_t* location = reinterpret_cast<uint16_t*>(code);
  2131. location[0] &= ~((static_cast<uint16_t>(0x7f) >> 2) << 6);
  2132. location[0] |= (imm.getUInt7() >> 2) << 6;
  2133. cacheFlush(location, sizeof(uint16_t));
  2134. }
  2135. static void setPointer(void* code, void* value, bool flush)
  2136. {
  2137. setInt32(code, reinterpret_cast<uint32_t>(value), flush);
  2138. }
  2139. static bool isB(void* address)
  2140. {
  2141. uint16_t* instruction = static_cast<uint16_t*>(address);
  2142. return ((instruction[0] & 0xf800) == OP_B_T4a) && ((instruction[1] & 0xd000) == OP_B_T4b);
  2143. }
  2144. static bool isBX(void* address)
  2145. {
  2146. uint16_t* instruction = static_cast<uint16_t*>(address);
  2147. return (instruction[0] & 0xff87) == OP_BX;
  2148. }
  2149. static bool isMOV_imm_T3(void* address)
  2150. {
  2151. uint16_t* instruction = static_cast<uint16_t*>(address);
  2152. return ((instruction[0] & 0xFBF0) == OP_MOV_imm_T3) && ((instruction[1] & 0x8000) == 0);
  2153. }
  2154. static bool isMOVT(void* address)
  2155. {
  2156. uint16_t* instruction = static_cast<uint16_t*>(address);
  2157. return ((instruction[0] & 0xFBF0) == OP_MOVT) && ((instruction[1] & 0x8000) == 0);
  2158. }
  2159. static bool isNOP_T1(void* address)
  2160. {
  2161. uint16_t* instruction = static_cast<uint16_t*>(address);
  2162. return instruction[0] == OP_NOP_T1;
  2163. }
  2164. static bool isNOP_T2(void* address)
  2165. {
  2166. uint16_t* instruction = static_cast<uint16_t*>(address);
  2167. return (instruction[0] == OP_NOP_T2a) && (instruction[1] == OP_NOP_T2b);
  2168. }
  2169. static bool canBeJumpT1(const uint16_t* instruction, const void* target)
  2170. {
  2171. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2172. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2173. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2174. // It does not appear to be documented in the ARM ARM (big surprise), but
  2175. // for OP_B_T1 the branch displacement encoded in the instruction is 2
  2176. // less than the actual displacement.
  2177. relative -= 2;
  2178. return ((relative << 23) >> 23) == relative;
  2179. }
  2180. static bool canBeJumpT2(const uint16_t* instruction, const void* target)
  2181. {
  2182. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2183. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2184. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2185. // It does not appear to be documented in the ARM ARM (big surprise), but
  2186. // for OP_B_T2 the branch displacement encoded in the instruction is 2
  2187. // less than the actual displacement.
  2188. relative -= 2;
  2189. return ((relative << 20) >> 20) == relative;
  2190. }
  2191. static bool canBeJumpT3(const uint16_t* instruction, const void* target)
  2192. {
  2193. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2194. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2195. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2196. return ((relative << 11) >> 11) == relative;
  2197. }
  2198. static bool canBeJumpT4(const uint16_t* instruction, const void* target)
  2199. {
  2200. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2201. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2202. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2203. return ((relative << 7) >> 7) == relative;
  2204. }
  2205. void linkJumpT1(Condition cond, uint16_t* instruction, void* target)
  2206. {
  2207. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2208. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2209. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2210. ASSERT(canBeJumpT1(instruction, target));
  2211. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2212. // It does not appear to be documented in the ARM ARM (big surprise), but
  2213. // for OP_B_T1 the branch displacement encoded in the instruction is 2
  2214. // less than the actual displacement.
  2215. relative -= 2;
  2216. // All branch offsets should be an even distance.
  2217. ASSERT(!(relative & 1));
  2218. instruction[-1] = OP_B_T1 | ((cond & 0xf) << 8) | ((relative & 0x1fe) >> 1);
  2219. }
  2220. static void linkJumpT2(uint16_t* instruction, void* target)
  2221. {
  2222. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2223. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2224. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2225. ASSERT(canBeJumpT2(instruction, target));
  2226. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2227. // It does not appear to be documented in the ARM ARM (big surprise), but
  2228. // for OP_B_T2 the branch displacement encoded in the instruction is 2
  2229. // less than the actual displacement.
  2230. relative -= 2;
  2231. // All branch offsets should be an even distance.
  2232. ASSERT(!(relative & 1));
  2233. instruction[-1] = OP_B_T2 | ((relative & 0xffe) >> 1);
  2234. }
  2235. void linkJumpT3(Condition cond, uint16_t* instruction, void* target)
  2236. {
  2237. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2238. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2239. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2240. ASSERT(canBeJumpT3(instruction, target));
  2241. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2242. // All branch offsets should be an even distance.
  2243. ASSERT(!(relative & 1));
  2244. instruction[-2] = OP_B_T3a | ((relative & 0x100000) >> 10) | ((cond & 0xf) << 6) | ((relative & 0x3f000) >> 12);
  2245. instruction[-1] = OP_B_T3b | ((relative & 0x80000) >> 8) | ((relative & 0x40000) >> 5) | ((relative & 0xffe) >> 1);
  2246. }
  2247. static void linkJumpT4(uint16_t* instruction, void* target)
  2248. {
  2249. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2250. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2251. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2252. ASSERT(canBeJumpT4(instruction, target));
  2253. intptr_t relative = reinterpret_cast<intptr_t>(target) - (reinterpret_cast<intptr_t>(instruction));
  2254. // ARM encoding for the top two bits below the sign bit is 'peculiar'.
  2255. if (relative >= 0)
  2256. relative ^= 0xC00000;
  2257. // All branch offsets should be an even distance.
  2258. ASSERT(!(relative & 1));
  2259. instruction[-2] = OP_B_T4a | ((relative & 0x1000000) >> 14) | ((relative & 0x3ff000) >> 12);
  2260. instruction[-1] = OP_B_T4b | ((relative & 0x800000) >> 10) | ((relative & 0x400000) >> 11) | ((relative & 0xffe) >> 1);
  2261. }
  2262. void linkConditionalJumpT4(Condition cond, uint16_t* instruction, void* target)
  2263. {
  2264. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2265. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2266. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2267. instruction[-3] = ifThenElse(cond) | OP_IT;
  2268. linkJumpT4(instruction, target);
  2269. }
  2270. static void linkBX(uint16_t* instruction, void* target)
  2271. {
  2272. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2273. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2274. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2275. const uint16_t JUMP_TEMPORARY_REGISTER = ARMRegisters::ip;
  2276. ARMThumbImmediate lo16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(reinterpret_cast<uint32_t>(target) + 1));
  2277. ARMThumbImmediate hi16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(reinterpret_cast<uint32_t>(target) >> 16));
  2278. instruction[-5] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOV_imm_T3, lo16);
  2279. instruction[-4] = twoWordOp5i6Imm4Reg4EncodedImmSecond(JUMP_TEMPORARY_REGISTER, lo16);
  2280. instruction[-3] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOVT, hi16);
  2281. instruction[-2] = twoWordOp5i6Imm4Reg4EncodedImmSecond(JUMP_TEMPORARY_REGISTER, hi16);
  2282. instruction[-1] = OP_BX | (JUMP_TEMPORARY_REGISTER << 3);
  2283. }
  2284. void linkConditionalBX(Condition cond, uint16_t* instruction, void* target)
  2285. {
  2286. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2287. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2288. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2289. linkBX(instruction, target);
  2290. instruction[-6] = ifThenElse(cond, true, true) | OP_IT;
  2291. }
  2292. static void linkJumpAbsolute(uint16_t* instruction, void* target)
  2293. {
  2294. // FIMXE: this should be up in the MacroAssembler layer. :-(
  2295. ASSERT(!(reinterpret_cast<intptr_t>(instruction) & 1));
  2296. ASSERT(!(reinterpret_cast<intptr_t>(target) & 1));
  2297. ASSERT((isMOV_imm_T3(instruction - 5) && isMOVT(instruction - 3) && isBX(instruction - 1))
  2298. || (isNOP_T1(instruction - 5) && isNOP_T2(instruction - 4) && isB(instruction - 2)));
  2299. if (canBeJumpT4(instruction, target)) {
  2300. // There may be a better way to fix this, but right now put the NOPs first, since in the
  2301. // case of an conditional branch this will be coming after an ITTT predicating *three*
  2302. // instructions! Looking backwards to modify the ITTT to an IT is not easy, due to
  2303. // variable wdith encoding - the previous instruction might *look* like an ITTT but
  2304. // actually be the second half of a 2-word op.
  2305. instruction[-5] = OP_NOP_T1;
  2306. instruction[-4] = OP_NOP_T2a;
  2307. instruction[-3] = OP_NOP_T2b;
  2308. linkJumpT4(instruction, target);
  2309. } else {
  2310. const uint16_t JUMP_TEMPORARY_REGISTER = ARMRegisters::ip;
  2311. ARMThumbImmediate lo16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(reinterpret_cast<uint32_t>(target) + 1));
  2312. ARMThumbImmediate hi16 = ARMThumbImmediate::makeUInt16(static_cast<uint16_t>(reinterpret_cast<uint32_t>(target) >> 16));
  2313. instruction[-5] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOV_imm_T3, lo16);
  2314. instruction[-4] = twoWordOp5i6Imm4Reg4EncodedImmSecond(JUMP_TEMPORARY_REGISTER, lo16);
  2315. instruction[-3] = twoWordOp5i6Imm4Reg4EncodedImmFirst(OP_MOVT, hi16);
  2316. instruction[-2] = twoWordOp5i6Imm4Reg4EncodedImmSecond(JUMP_TEMPORARY_REGISTER, hi16);
  2317. instruction[-1] = OP_BX | (JUMP_TEMPORARY_REGISTER << 3);
  2318. }
  2319. }
  2320. static uint16_t twoWordOp5i6Imm4Reg4EncodedImmFirst(uint16_t op, ARMThumbImmediate imm)
  2321. {
  2322. return op | (imm.m_value.i << 10) | imm.m_value.imm4;
  2323. }
  2324. static void decodeTwoWordOp5i6Imm4Reg4EncodedImmFirst(ARMThumbImmediate& result, uint16_t value)
  2325. {
  2326. result.m_value.i = (value >> 10) & 1;
  2327. result.m_value.imm4 = value & 15;
  2328. }
  2329. static uint16_t twoWordOp5i6Imm4Reg4EncodedImmSecond(uint16_t rd, ARMThumbImmediate imm)
  2330. {
  2331. return (imm.m_value.imm3 << 12) | (rd << 8) | imm.m_value.imm8;
  2332. }
  2333. static void decodeTwoWordOp5i6Imm4Reg4EncodedImmSecond(ARMThumbImmediate& result, uint16_t value)
  2334. {
  2335. result.m_value.imm3 = (value >> 12) & 7;
  2336. result.m_value.imm8 = value & 255;
  2337. }
  2338. class ARMInstructionFormatter {
  2339. public:
  2340. ALWAYS_INLINE void oneWordOp5Reg3Imm8(OpcodeID op, RegisterID rd, uint8_t imm)
  2341. {
  2342. m_buffer.putShort(op | (rd << 8) | imm);
  2343. }
  2344. ALWAYS_INLINE void oneWordOp5Imm5Reg3Reg3(OpcodeID op, uint8_t imm, RegisterID reg1, RegisterID reg2)
  2345. {
  2346. m_buffer.putShort(op | (imm << 6) | (reg1 << 3) | reg2);
  2347. }
  2348. ALWAYS_INLINE void oneWordOp7Reg3Reg3Reg3(OpcodeID op, RegisterID reg1, RegisterID reg2, RegisterID reg3)
  2349. {
  2350. m_buffer.putShort(op | (reg1 << 6) | (reg2 << 3) | reg3);
  2351. }
  2352. ALWAYS_INLINE void oneWordOp8Imm8(OpcodeID op, uint8_t imm)
  2353. {
  2354. m_buffer.putShort(op | imm);
  2355. }
  2356. ALWAYS_INLINE void oneWordOp8RegReg143(OpcodeID op, RegisterID reg1, RegisterID reg2)
  2357. {
  2358. m_buffer.putShort(op | ((reg2 & 8) << 4) | (reg1 << 3) | (reg2 & 7));
  2359. }
  2360. ALWAYS_INLINE void oneWordOp9Imm7(OpcodeID op, uint8_t imm)
  2361. {
  2362. m_buffer.putShort(op | imm);
  2363. }
  2364. ALWAYS_INLINE void oneWordOp10Reg3Reg3(OpcodeID op, RegisterID reg1, RegisterID reg2)
  2365. {
  2366. m_buffer.putShort(op | (reg1 << 3) | reg2);
  2367. }
  2368. ALWAYS_INLINE void twoWordOp12Reg4FourFours(OpcodeID1 op, RegisterID reg, FourFours ff)
  2369. {
  2370. m_buffer.putShort(op | reg);
  2371. m_buffer.putShort(ff.m_u.value);
  2372. }
  2373. ALWAYS_INLINE void twoWordOp16FourFours(OpcodeID1 op, FourFours ff)
  2374. {
  2375. m_buffer.putShort(op);
  2376. m_buffer.putShort(ff.m_u.value);
  2377. }
  2378. ALWAYS_INLINE void twoWordOp16Op16(OpcodeID1 op1, OpcodeID2 op2)
  2379. {
  2380. m_buffer.putShort(op1);
  2381. m_buffer.putShort(op2);
  2382. }
  2383. ALWAYS_INLINE void twoWordOp5i6Imm4Reg4EncodedImm(OpcodeID1 op, int imm4, RegisterID rd, ARMThumbImmediate imm)
  2384. {
  2385. ARMThumbImmediate newImm = imm;
  2386. newImm.m_value.imm4 = imm4;
  2387. m_buffer.putShort(ARMv7Assembler::twoWordOp5i6Imm4Reg4EncodedImmFirst(op, newImm));
  2388. m_buffer.putShort(ARMv7Assembler::twoWordOp5i6Imm4Reg4EncodedImmSecond(rd, newImm));
  2389. }
  2390. ALWAYS_INLINE void twoWordOp12Reg4Reg4Imm12(OpcodeID1 op, RegisterID reg1, RegisterID reg2, uint16_t imm)
  2391. {
  2392. m_buffer.putShort(op | reg1);
  2393. m_buffer.putShort((reg2 << 12) | imm);
  2394. }
  2395. ALWAYS_INLINE void twoWordOp12Reg40Imm3Reg4Imm20Imm5(OpcodeID1 op, RegisterID reg1, RegisterID reg2, uint16_t imm1, uint16_t imm2, uint16_t imm3)
  2396. {
  2397. m_buffer.putShort(op | reg1);
  2398. m_buffer.putShort((imm1 << 12) | (reg2 << 8) | (imm2 << 6) | imm3);
  2399. }
  2400. // Formats up instructions of the pattern:
  2401. // 111111111B11aaaa:bbbb222SA2C2cccc
  2402. // Where 1s in the pattern come from op1, 2s in the pattern come from op2, S is the provided size bit.
  2403. // Operands provide 5 bit values of the form Aaaaa, Bbbbb, Ccccc.
  2404. ALWAYS_INLINE void vfpOp(OpcodeID1 op1, OpcodeID2 op2, bool size, VFPOperand a, VFPOperand b, VFPOperand c)
  2405. {
  2406. ASSERT(!(op1 & 0x004f));
  2407. ASSERT(!(op2 & 0xf1af));
  2408. m_buffer.putShort(op1 | b.bits1() << 6 | a.bits4());
  2409. m_buffer.putShort(op2 | b.bits4() << 12 | size << 8 | a.bits1() << 7 | c.bits1() << 5 | c.bits4());
  2410. }
  2411. // Arm vfp addresses can be offset by a 9-bit ones-comp immediate, left shifted by 2.
  2412. // (i.e. +/-(0..255) 32-bit words)
  2413. ALWAYS_INLINE void vfpMemOp(OpcodeID1 op1, OpcodeID2 op2, bool size, RegisterID rn, VFPOperand rd, int32_t imm)
  2414. {
  2415. bool up = true;
  2416. if (imm < 0) {
  2417. imm = -imm;
  2418. up = false;
  2419. }
  2420. uint32_t offset = imm;
  2421. ASSERT(!(offset & ~0x3fc));
  2422. offset >>= 2;
  2423. m_buffer.putShort(op1 | (up << 7) | rd.bits1() << 6 | rn);
  2424. m_buffer.putShort(op2 | rd.bits4() << 12 | size << 8 | offset);
  2425. }
  2426. // Administrative methods:
  2427. size_t codeSize() const { return m_buffer.codeSize(); }
  2428. AssemblerLabel label() const { return m_buffer.label(); }
  2429. bool isAligned(int alignment) const { return m_buffer.isAligned(alignment); }
  2430. void* data() const { return m_buffer.data(); }
  2431. unsigned debugOffset() { return m_buffer.debugOffset(); }
  2432. private:
  2433. AssemblerBuffer m_buffer;
  2434. } m_formatter;
  2435. Vector<LinkRecord, 0, UnsafeVectorOverflow> m_jumpsToLink;
  2436. int m_indexOfLastWatchpoint;
  2437. int m_indexOfTailOfLastWatchpoint;
  2438. };
  2439. } // namespace JSC
  2440. #endif // ENABLE(ASSEMBLER) && CPU(ARM_THUMB2)
  2441. #endif // ARMAssembler_h