aasmcpu.pas 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Contains the assembler object for the ARM
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,globals,verbose,
  22. aasmbase,aasmtai,aasmdata,aasmsym,
  23. ogbase,
  24. symtype,
  25. cpubase,cpuinfo,cgbase,cgutils;
  26. const
  27. { "mov reg,reg" source operand number }
  28. O_MOV_SOURCE = 1;
  29. { "mov reg,reg" source operand number }
  30. O_MOV_DEST = 0;
  31. { Operand types }
  32. OT_NONE = $00000000;
  33. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  34. OT_BITS16 = $00000002;
  35. OT_BITS32 = $00000004;
  36. OT_BITS64 = $00000008; { FPU only }
  37. OT_BITS80 = $00000010;
  38. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  39. OT_NEAR = $00000040;
  40. OT_SHORT = $00000080;
  41. OT_BITSTINY = $00000100; { fpu constant }
  42. OT_BITSSHIFTER =
  43. $00000200;
  44. OT_SIZE_MASK = $000003FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_SHIFTEROP = $00000800;
  51. OT_REGISTER = $00001000;
  52. OT_IMMEDIATE = $00002000;
  53. OT_REGLIST = $00008000;
  54. OT_IMM8 = $00002001;
  55. OT_IMM24 = $00002002;
  56. OT_IMM32 = $00002004;
  57. OT_IMM64 = $00002008;
  58. OT_IMM80 = $00002010;
  59. OT_IMMTINY = $00002100;
  60. OT_IMMSHIFTER= $00002200;
  61. OT_IMMEDIATE24 = OT_IMM24;
  62. OT_SHIFTIMM = OT_SHIFTEROP or OT_IMMSHIFTER;
  63. OT_SHIFTIMMEDIATE = OT_SHIFTIMM;
  64. OT_IMMEDIATESHIFTER = OT_IMMSHIFTER;
  65. OT_IMMEDIATEFPU = OT_IMMTINY;
  66. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  67. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  68. OT_REG8 = $00201001;
  69. OT_REG16 = $00201002;
  70. OT_REG32 = $00201004;
  71. OT_REG64 = $00201008;
  72. OT_VREG = $00201010; { vector register }
  73. OT_REGF = $00201020; { coproc register }
  74. OT_MEMORY = $00204000; { register number in 'basereg' }
  75. OT_MEM8 = $00204001;
  76. OT_MEM16 = $00204002;
  77. OT_MEM32 = $00204004;
  78. OT_MEM64 = $00204008;
  79. OT_MEM80 = $00204010;
  80. { word/byte load/store }
  81. OT_AM2 = $00010000;
  82. { misc ld/st operations }
  83. OT_AM3 = $00020000;
  84. { multiple ld/st operations }
  85. OT_AM4 = $00040000;
  86. { co proc. ld/st operations }
  87. OT_AM5 = $00080000;
  88. { exclusive ld/st operations }
  89. OT_AM6 = $00100000;
  90. OT_AMMASK = $001f0000;
  91. { IT instruction }
  92. OT_CONDITION = $00200000;
  93. OT_MEMORYAM2 = OT_MEMORY or OT_AM2;
  94. OT_MEMORYAM3 = OT_MEMORY or OT_AM3;
  95. OT_MEMORYAM4 = OT_MEMORY or OT_AM4;
  96. OT_MEMORYAM5 = OT_MEMORY or OT_AM5;
  97. OT_MEMORYAM6 = OT_MEMORY or OT_AM6;
  98. OT_FPUREG = $01000000; { floating point stack registers }
  99. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  100. { a mask for the following }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. instabentries = {$i armnop.inc}
  107. maxinfolen = 5;
  108. IF_NONE = $00000000;
  109. IF_ARMMASK = $000F0000;
  110. IF_ARM32 = $00010000;
  111. IF_THUMB = $00020000;
  112. IF_THUMB32 = $00040000;
  113. IF_ARMvMASK = $0FF00000;
  114. IF_ARMv4 = $00100000;
  115. IF_ARMv4T = $00200000;
  116. IF_ARMv5 = $00300000;
  117. IF_ARMv5T = $00400000;
  118. IF_ARMv5TE = $00500000;
  119. IF_ARMv5TEJ = $00600000;
  120. IF_ARMv6 = $00700000;
  121. IF_ARMv6K = $00800000;
  122. IF_ARMv6T2 = $00900000;
  123. IF_ARMv6Z = $00A00000;
  124. IF_ARMv6M = $00B00000;
  125. IF_ARMv7 = $00C00000;
  126. IF_ARMv7A = $00D00000;
  127. IF_ARMv7R = $00E00000;
  128. IF_ARMv7M = $00F00000;
  129. IF_ARMv7EM = $01000000;
  130. IF_FPMASK = $F0000000;
  131. IF_FPA = $10000000;
  132. IF_VFPv2 = $20000000;
  133. IF_VFPv3 = $40000000;
  134. { if the instruction can change in a second pass }
  135. IF_PASS2 = longint($80000000);
  136. type
  137. TInsTabCache=array[TasmOp] of longint;
  138. PInsTabCache=^TInsTabCache;
  139. tinsentry = record
  140. opcode : tasmop;
  141. ops : byte;
  142. optypes : array[0..5] of longint;
  143. code : array[0..maxinfolen] of char;
  144. flags : longint;
  145. end;
  146. pinsentry=^tinsentry;
  147. const
  148. InsTab : array[0..instabentries-1] of TInsEntry={$i armtab.inc}
  149. var
  150. InsTabCache : PInsTabCache;
  151. type
  152. taicpu = class(tai_cpu_abstract_sym)
  153. oppostfix : TOpPostfix;
  154. wideformat : boolean;
  155. roundingmode : troundingmode;
  156. procedure loadshifterop(opidx:longint;const so:tshifterop);
  157. procedure loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean=false);
  158. procedure loadconditioncode(opidx:longint;const cond:tasmcond);
  159. procedure loadmodeflags(opidx:longint;const flags:tcpumodeflags);
  160. procedure loadspecialreg(opidx:longint;const areg:tregister; const aflags:tspecialregflags);
  161. constructor op_none(op : tasmop);
  162. constructor op_reg(op : tasmop;_op1 : tregister);
  163. constructor op_ref(op : tasmop;const _op1 : treference);
  164. constructor op_const(op : tasmop;_op1 : longint);
  165. constructor op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  166. constructor op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  167. constructor op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  168. constructor op_regset(op:tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  169. constructor op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  170. constructor op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  171. constructor op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  172. constructor op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  173. constructor op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  174. constructor op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  175. constructor op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  176. constructor op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  177. { SFM/LFM }
  178. constructor op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  179. { ITxxx }
  180. constructor op_cond(op: tasmop; cond: tasmcond);
  181. { CPSxx }
  182. constructor op_modeflags(op: tasmop; flags: tcpumodeflags);
  183. constructor op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  184. { MSR }
  185. constructor op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  186. { *M*LL }
  187. constructor op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  188. { this is for Jmp instructions }
  189. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  190. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  191. constructor op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  192. constructor op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  193. constructor op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  194. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  195. function spilling_get_operation_type(opnr: longint): topertype;override;
  196. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;override;
  197. { assembler }
  198. public
  199. { the next will reset all instructions that can change in pass 2 }
  200. procedure ResetPass1;override;
  201. procedure ResetPass2;override;
  202. function CheckIfValid:boolean;
  203. function GetString:string;
  204. function Pass1(objdata:TObjData):longint;override;
  205. procedure Pass2(objdata:TObjData);override;
  206. protected
  207. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  208. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  209. procedure ppubuildderefimploper(var o:toper);override;
  210. procedure ppuderefoper(var o:toper);override;
  211. private
  212. { next fields are filled in pass1, so pass2 is faster }
  213. inssize : shortint;
  214. insoffset : longint;
  215. LastInsOffset : longint; { need to be public to be reset }
  216. insentry : PInsEntry;
  217. function InsEnd:longint;
  218. procedure create_ot(objdata:TObjData);
  219. function Matches(p:PInsEntry):longint;
  220. function calcsize(p:PInsEntry):shortint;
  221. procedure gencode(objdata:TObjData);
  222. function NeedAddrPrefix(opidx:byte):boolean;
  223. procedure Swapoperands;
  224. function FindInsentry(objdata:TObjData):boolean;
  225. end;
  226. tai_align = class(tai_align_abstract)
  227. { nothing to add }
  228. end;
  229. tai_thumb_func = class(tai)
  230. constructor create;
  231. end;
  232. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  233. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  234. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  235. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  236. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  237. { inserts pc relative symbols at places where they are reachable
  238. and transforms special instructions to valid instruction encodings }
  239. procedure finalizearmcode(list,listtoinsert : TAsmList);
  240. { inserts .pdata section and dummy function prolog needed for arm-wince exception handling }
  241. procedure InsertPData;
  242. procedure InitAsm;
  243. procedure DoneAsm;
  244. implementation
  245. uses
  246. itcpugas,aoptcpu;
  247. procedure taicpu.loadshifterop(opidx:longint;const so:tshifterop);
  248. begin
  249. allocate_oper(opidx+1);
  250. with oper[opidx]^ do
  251. begin
  252. if typ<>top_shifterop then
  253. begin
  254. clearop(opidx);
  255. new(shifterop);
  256. end;
  257. shifterop^:=so;
  258. typ:=top_shifterop;
  259. if assigned(add_reg_instruction_hook) then
  260. add_reg_instruction_hook(self,shifterop^.rs);
  261. end;
  262. end;
  263. procedure taicpu.loadregset(opidx:longint; regsetregtype: tregistertype; regsetsubregtype: tsubregister; const s:tcpuregisterset; ausermode: boolean);
  264. var
  265. i : byte;
  266. begin
  267. allocate_oper(opidx+1);
  268. with oper[opidx]^ do
  269. begin
  270. if typ<>top_regset then
  271. begin
  272. clearop(opidx);
  273. new(regset);
  274. end;
  275. regset^:=s;
  276. regtyp:=regsetregtype;
  277. subreg:=regsetsubregtype;
  278. usermode:=ausermode;
  279. typ:=top_regset;
  280. case regsetregtype of
  281. R_INTREGISTER:
  282. for i:=RS_R0 to RS_R15 do
  283. begin
  284. if assigned(add_reg_instruction_hook) and (i in regset^) then
  285. add_reg_instruction_hook(self,newreg(R_INTREGISTER,i,regsetsubregtype));
  286. end;
  287. R_MMREGISTER:
  288. { both RS_S0 and RS_D0 range from 0 to 31 }
  289. for i:=RS_D0 to RS_D31 do
  290. begin
  291. if assigned(add_reg_instruction_hook) and (i in regset^) then
  292. add_reg_instruction_hook(self,newreg(R_MMREGISTER,i,regsetsubregtype));
  293. end;
  294. end;
  295. end;
  296. end;
  297. procedure taicpu.loadconditioncode(opidx:longint;const cond:tasmcond);
  298. begin
  299. allocate_oper(opidx+1);
  300. with oper[opidx]^ do
  301. begin
  302. if typ<>top_conditioncode then
  303. clearop(opidx);
  304. cc:=cond;
  305. typ:=top_conditioncode;
  306. end;
  307. end;
  308. procedure taicpu.loadmodeflags(opidx: longint; const flags: tcpumodeflags);
  309. begin
  310. allocate_oper(opidx+1);
  311. with oper[opidx]^ do
  312. begin
  313. if typ<>top_modeflags then
  314. clearop(opidx);
  315. modeflags:=flags;
  316. typ:=top_modeflags;
  317. end;
  318. end;
  319. procedure taicpu.loadspecialreg(opidx: longint; const areg: tregister; const aflags: tspecialregflags);
  320. begin
  321. allocate_oper(opidx+1);
  322. with oper[opidx]^ do
  323. begin
  324. if typ<>top_specialreg then
  325. clearop(opidx);
  326. specialreg:=areg;
  327. specialflags:=aflags;
  328. typ:=top_specialreg;
  329. end;
  330. end;
  331. {*****************************************************************************
  332. taicpu Constructors
  333. *****************************************************************************}
  334. constructor taicpu.op_none(op : tasmop);
  335. begin
  336. inherited create(op);
  337. end;
  338. { for pld }
  339. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  340. begin
  341. inherited create(op);
  342. ops:=1;
  343. loadref(0,_op1);
  344. end;
  345. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  346. begin
  347. inherited create(op);
  348. ops:=1;
  349. loadreg(0,_op1);
  350. end;
  351. constructor taicpu.op_const(op : tasmop;_op1 : longint);
  352. begin
  353. inherited create(op);
  354. ops:=1;
  355. loadconst(0,aint(_op1));
  356. end;
  357. constructor taicpu.op_reg_reg(op : tasmop;_op1,_op2 : tregister);
  358. begin
  359. inherited create(op);
  360. ops:=2;
  361. loadreg(0,_op1);
  362. loadreg(1,_op2);
  363. end;
  364. constructor taicpu.op_reg_const(op:tasmop; _op1: tregister; _op2: aint);
  365. begin
  366. inherited create(op);
  367. ops:=2;
  368. loadreg(0,_op1);
  369. loadconst(1,aint(_op2));
  370. end;
  371. constructor taicpu.op_regset(op: tasmop; regtype: tregistertype; subreg: tsubregister; _op1: tcpuregisterset);
  372. begin
  373. inherited create(op);
  374. ops:=1;
  375. loadregset(0,regtype,subreg,_op1);
  376. end;
  377. constructor taicpu.op_ref_regset(op:tasmop; _op1: treference; regtype: tregistertype; subreg: tsubregister; _op2: tcpuregisterset);
  378. begin
  379. inherited create(op);
  380. ops:=2;
  381. loadref(0,_op1);
  382. loadregset(1,regtype,subreg,_op2);
  383. end;
  384. constructor taicpu.op_reg_ref(op : tasmop;_op1 : tregister;const _op2 : treference);
  385. begin
  386. inherited create(op);
  387. ops:=2;
  388. loadreg(0,_op1);
  389. loadref(1,_op2);
  390. end;
  391. constructor taicpu.op_reg_reg_reg(op : tasmop;_op1,_op2,_op3 : tregister);
  392. begin
  393. inherited create(op);
  394. ops:=3;
  395. loadreg(0,_op1);
  396. loadreg(1,_op2);
  397. loadreg(2,_op3);
  398. end;
  399. constructor taicpu.op_reg_reg_reg_reg(op : tasmop;_op1,_op2,_op3,_op4 : tregister);
  400. begin
  401. inherited create(op);
  402. ops:=4;
  403. loadreg(0,_op1);
  404. loadreg(1,_op2);
  405. loadreg(2,_op3);
  406. loadreg(3,_op4);
  407. end;
  408. constructor taicpu.op_reg_reg_const(op : tasmop;_op1,_op2 : tregister; _op3: aint);
  409. begin
  410. inherited create(op);
  411. ops:=3;
  412. loadreg(0,_op1);
  413. loadreg(1,_op2);
  414. loadconst(2,aint(_op3));
  415. end;
  416. constructor taicpu.op_reg_const_const(op : tasmop;_op1 : tregister; _op2,_op3: aint);
  417. begin
  418. inherited create(op);
  419. ops:=3;
  420. loadreg(0,_op1);
  421. loadconst(1,aint(_op2));
  422. loadconst(2,aint(_op3));
  423. end;
  424. constructor taicpu.op_reg_const_ref(op : tasmop;_op1 : tregister;_op2 : aint;_op3 : treference);
  425. begin
  426. inherited create(op);
  427. ops:=3;
  428. loadreg(0,_op1);
  429. loadconst(1,_op2);
  430. loadref(2,_op3);
  431. end;
  432. constructor taicpu.op_cond(op: tasmop; cond: tasmcond);
  433. begin
  434. inherited create(op);
  435. ops:=1;
  436. loadconditioncode(0, cond);
  437. end;
  438. constructor taicpu.op_modeflags(op: tasmop; flags: tcpumodeflags);
  439. begin
  440. inherited create(op);
  441. ops := 1;
  442. loadmodeflags(0,flags);
  443. end;
  444. constructor taicpu.op_modeflags_const(op: tasmop; flags: tcpumodeflags; a: aint);
  445. begin
  446. inherited create(op);
  447. ops := 2;
  448. loadmodeflags(0,flags);
  449. loadconst(1,a);
  450. end;
  451. constructor taicpu.op_specialreg_reg(op: tasmop; specialreg: tregister; specialregflags: tspecialregflags; _op2: tregister);
  452. begin
  453. inherited create(op);
  454. ops:=2;
  455. loadspecialreg(0,specialreg,specialregflags);
  456. loadreg(1,_op2);
  457. end;
  458. constructor taicpu.op_reg_reg_sym_ofs(op : tasmop;_op1,_op2 : tregister; _op3: tasmsymbol;_op3ofs: longint);
  459. begin
  460. inherited create(op);
  461. ops:=3;
  462. loadreg(0,_op1);
  463. loadreg(1,_op2);
  464. loadsymbol(0,_op3,_op3ofs);
  465. end;
  466. constructor taicpu.op_reg_reg_ref(op : tasmop;_op1,_op2 : tregister; const _op3: treference);
  467. begin
  468. inherited create(op);
  469. ops:=3;
  470. loadreg(0,_op1);
  471. loadreg(1,_op2);
  472. loadref(2,_op3);
  473. end;
  474. constructor taicpu.op_reg_reg_shifterop(op : tasmop;_op1,_op2 : tregister;_op3 : tshifterop);
  475. begin
  476. inherited create(op);
  477. ops:=3;
  478. loadreg(0,_op1);
  479. loadreg(1,_op2);
  480. loadshifterop(2,_op3);
  481. end;
  482. constructor taicpu.op_reg_reg_reg_shifterop(op : tasmop;_op1,_op2,_op3 : tregister;_op4 : tshifterop);
  483. begin
  484. inherited create(op);
  485. ops:=4;
  486. loadreg(0,_op1);
  487. loadreg(1,_op2);
  488. loadreg(2,_op3);
  489. loadshifterop(3,_op4);
  490. end;
  491. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_op1 : tasmsymbol);
  492. begin
  493. inherited create(op);
  494. condition:=cond;
  495. ops:=1;
  496. loadsymbol(0,_op1,0);
  497. end;
  498. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  499. begin
  500. inherited create(op);
  501. ops:=1;
  502. loadsymbol(0,_op1,0);
  503. end;
  504. constructor taicpu.op_sym_ofs(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint);
  505. begin
  506. inherited create(op);
  507. ops:=1;
  508. loadsymbol(0,_op1,_op1ofs);
  509. end;
  510. constructor taicpu.op_reg_sym_ofs(op : tasmop;_op1 : tregister;_op2:tasmsymbol;_op2ofs : longint);
  511. begin
  512. inherited create(op);
  513. ops:=2;
  514. loadreg(0,_op1);
  515. loadsymbol(1,_op2,_op2ofs);
  516. end;
  517. constructor taicpu.op_sym_ofs_ref(op : tasmop;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  518. begin
  519. inherited create(op);
  520. ops:=2;
  521. loadsymbol(0,_op1,_op1ofs);
  522. loadref(1,_op2);
  523. end;
  524. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  525. begin
  526. { allow the register allocator to remove unnecessary moves }
  527. result:=(
  528. ((opcode=A_MOV) and (regtype = R_INTREGISTER)) or
  529. ((opcode=A_MVF) and (regtype = R_FPUREGISTER)) or
  530. ((opcode in [A_FCPYS, A_FCPYD]) and (regtype = R_MMREGISTER)) or
  531. ((opcode in [A_VMOV]) and (regtype = R_MMREGISTER) and (oppostfix in [PF_F32,PF_F64]))
  532. ) and
  533. ((oppostfix in [PF_None,PF_D]) or (opcode = A_VMOV)) and
  534. (condition=C_None) and
  535. (ops=2) and
  536. (oper[0]^.typ=top_reg) and
  537. (oper[1]^.typ=top_reg) and
  538. (oper[0]^.reg=oper[1]^.reg);
  539. end;
  540. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  541. begin
  542. case getregtype(r) of
  543. R_INTREGISTER :
  544. result:=taicpu.op_reg_ref(A_LDR,r,ref);
  545. R_FPUREGISTER :
  546. { use lfm because we don't know the current internal format
  547. and avoid exceptions
  548. }
  549. result:=taicpu.op_reg_const_ref(A_LFM,r,1,ref);
  550. R_MMREGISTER :
  551. result:=taicpu.op_reg_ref(A_VLDR,r,ref);
  552. else
  553. internalerror(200401041);
  554. end;
  555. end;
  556. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  557. begin
  558. case getregtype(r) of
  559. R_INTREGISTER :
  560. result:=taicpu.op_reg_ref(A_STR,r,ref);
  561. R_FPUREGISTER :
  562. { use sfm because we don't know the current internal format
  563. and avoid exceptions
  564. }
  565. result:=taicpu.op_reg_const_ref(A_SFM,r,1,ref);
  566. R_MMREGISTER :
  567. result:=taicpu.op_reg_ref(A_VSTR,r,ref);
  568. else
  569. internalerror(200401041);
  570. end;
  571. end;
  572. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  573. begin
  574. case opcode of
  575. A_ADC,A_ADD,A_AND,A_BIC,
  576. A_EOR,A_CLZ,A_RBIT,
  577. A_LDR,A_LDRB,A_LDRBT,A_LDRH,A_LDRSB,
  578. A_LDRSH,A_LDRT,
  579. A_MOV,A_MVN,A_MLA,A_MUL,
  580. A_ORR,A_RSB,A_RSC,A_SBC,A_SUB,
  581. A_SWP,A_SWPB,
  582. A_LDF,A_FLT,A_FIX,
  583. A_ADF,A_DVF,A_FDV,A_FML,
  584. A_RFS,A_RFC,A_RDF,
  585. A_RMF,A_RPW,A_RSF,A_SUF,A_ABS,A_ACS,A_ASN,A_ATN,A_COS,
  586. A_EXP,A_LOG,A_LGN,A_MVF,A_MNF,A_FRD,A_MUF,A_POL,A_RND,A_SIN,A_SQT,A_TAN,
  587. A_LFM,
  588. A_FLDS,A_FLDD,
  589. A_FMRX,A_FMXR,A_FMSTAT,
  590. A_FMSR,A_FMRS,A_FMDRR,
  591. A_FCPYS,A_FCPYD,A_FCVTSD,A_FCVTDS,
  592. A_FABSS,A_FABSD,A_FSQRTS,A_FSQRTD,A_FMULS,A_FMULD,
  593. A_FADDS,A_FADDD,A_FSUBS,A_FSUBD,A_FDIVS,A_FDIVD,
  594. A_FMACS,A_FMACD,A_FMSCS,A_FMSCD,A_FNMACS,A_FNMACD,
  595. A_FNMSCS,A_FNMSCD,A_FNMULS,A_FNMULD,
  596. A_FMDHR,A_FMRDH,A_FMDLR,A_FMRDL,
  597. A_FNEGS,A_FNEGD,
  598. A_FSITOS,A_FSITOD,A_FTOSIS,A_FTOSID,
  599. A_FTOUIS,A_FTOUID,A_FUITOS,A_FUITOD,
  600. A_SXTB16,A_UXTB16,
  601. A_UXTB,A_UXTH,A_SXTB,A_SXTH,
  602. A_NEG,
  603. A_VABS,A_VADD,A_VCVT,A_VDIV,A_VLDR,A_VMOV,A_VMUL,A_VNEG,A_VSQRT,A_VSUB:
  604. if opnr=0 then
  605. result:=operand_write
  606. else
  607. result:=operand_read;
  608. A_BKPT,A_B,A_BL,A_BLX,A_BX,
  609. A_CMN,A_CMP,A_TEQ,A_TST,
  610. A_CMF,A_CMFE,A_WFS,A_CNF,
  611. A_FCMPS,A_FCMPD,A_FCMPES,A_FCMPED,A_FCMPEZS,A_FCMPEZD,
  612. A_FCMPZS,A_FCMPZD,
  613. A_VCMP,A_VCMPE:
  614. result:=operand_read;
  615. A_SMLAL,A_UMLAL:
  616. if opnr in [0,1] then
  617. result:=operand_readwrite
  618. else
  619. result:=operand_read;
  620. A_SMULL,A_UMULL,
  621. A_FMRRD:
  622. if opnr in [0,1] then
  623. result:=operand_write
  624. else
  625. result:=operand_read;
  626. A_STR,A_STRB,A_STRBT,
  627. A_STRH,A_STRT,A_STF,A_SFM,
  628. A_FSTS,A_FSTD,
  629. A_VSTR:
  630. { important is what happens with the involved registers }
  631. if opnr=0 then
  632. result := operand_read
  633. else
  634. { check for pre/post indexed }
  635. result := operand_read;
  636. //Thumb2
  637. A_LSL, A_LSR, A_ROR, A_ASR, A_SDIV, A_UDIV, A_MOVW, A_MOVT, A_MLS, A_BFI:
  638. if opnr in [0] then
  639. result:=operand_write
  640. else
  641. result:=operand_read;
  642. A_BFC:
  643. if opnr in [0] then
  644. result:=operand_readwrite
  645. else
  646. result:=operand_read;
  647. A_LDREX:
  648. if opnr in [0] then
  649. result:=operand_write
  650. else
  651. result:=operand_read;
  652. A_STREX:
  653. result:=operand_write;
  654. else
  655. internalerror(200403151);
  656. end;
  657. end;
  658. function taicpu.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  659. begin
  660. result := operand_read;
  661. if (oper[opnr]^.ref^.base = reg) and
  662. (oper[opnr]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  663. result := operand_readwrite;
  664. end;
  665. var
  666. IF_ArmInsVersion: longword;
  667. procedure BuildInsTabCache;
  668. var
  669. i : longint;
  670. begin
  671. if GenerateThumb2Code then
  672. IF_ArmInsVersion:=IF_THUMB32
  673. else if GenerateThumbCode then
  674. IF_ArmInsVersion:=IF_THUMB
  675. else
  676. IF_ArmInsVersion:=IF_ARM32;
  677. new(instabcache);
  678. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  679. i:=0;
  680. while (i<InsTabEntries) do
  681. begin
  682. if InsTabCache^[InsTab[i].Opcode]=-1 then
  683. InsTabCache^[InsTab[i].Opcode]:=i;
  684. inc(i);
  685. end;
  686. end;
  687. procedure InitAsm;
  688. begin
  689. if not assigned(instabcache) then
  690. BuildInsTabCache;
  691. end;
  692. procedure DoneAsm;
  693. begin
  694. if assigned(instabcache) then
  695. begin
  696. dispose(instabcache);
  697. instabcache:=nil;
  698. end;
  699. end;
  700. function setoppostfix(i : taicpu;pf : toppostfix) : taicpu;
  701. begin
  702. i.oppostfix:=pf;
  703. result:=i;
  704. end;
  705. function setroundingmode(i : taicpu;rm : troundingmode) : taicpu;
  706. begin
  707. i.roundingmode:=rm;
  708. result:=i;
  709. end;
  710. function setcondition(i : taicpu;c : tasmcond) : taicpu;
  711. begin
  712. i.condition:=c;
  713. result:=i;
  714. end;
  715. Function SimpleGetNextInstruction(Current: tai; Var Next: tai): Boolean;
  716. Begin
  717. Current:=tai(Current.Next);
  718. While Assigned(Current) And (Current.typ In SkipInstr) Do
  719. Current:=tai(Current.Next);
  720. Next:=Current;
  721. If Assigned(Next) And Not(Next.typ In SkipInstr) Then
  722. Result:=True
  723. Else
  724. Begin
  725. Next:=Nil;
  726. Result:=False;
  727. End;
  728. End;
  729. (*
  730. function armconstequal(hp1,hp2: tai): boolean;
  731. begin
  732. result:=false;
  733. if hp1.typ<>hp2.typ then
  734. exit;
  735. case hp1.typ of
  736. tai_const:
  737. result:=
  738. (tai_const(hp2).sym=tai_const(hp).sym) and
  739. (tai_const(hp2).value=tai_const(hp).value) and
  740. (tai(hp2.previous).typ=ait_label);
  741. tai_const:
  742. result:=
  743. (tai_const(hp2).sym=tai_const(hp).sym) and
  744. (tai_const(hp2).value=tai_const(hp).value) and
  745. (tai(hp2.previous).typ=ait_label);
  746. end;
  747. end;
  748. *)
  749. procedure insertpcrelativedata(list,listtoinsert : TAsmList);
  750. var
  751. limit: longint;
  752. { FLD/FST VFP instructions have a limit of +/- 1024, not 4096, this
  753. function checks the next count instructions if the limit must be
  754. decreased }
  755. procedure CheckLimit(hp : tai;count : integer);
  756. var
  757. i : Integer;
  758. begin
  759. for i:=1 to count do
  760. if SimpleGetNextInstruction(hp,hp) and
  761. (tai(hp).typ=ait_instruction) and
  762. ((taicpu(hp).opcode=A_FLDS) or
  763. (taicpu(hp).opcode=A_FLDD) or
  764. (taicpu(hp).opcode=A_VLDR)) then
  765. limit:=254;
  766. end;
  767. var
  768. curinspos,
  769. penalty,
  770. lastinspos,
  771. { increased for every data element > 4 bytes inserted }
  772. currentsize,
  773. extradataoffset,
  774. curop : longint;
  775. curtai : tai;
  776. ai_label : tai_label;
  777. curdatatai,hp,hp2 : tai;
  778. curdata : TAsmList;
  779. l : tasmlabel;
  780. doinsert,
  781. removeref : boolean;
  782. multiplier : byte;
  783. begin
  784. curdata:=TAsmList.create;
  785. lastinspos:=-1;
  786. curinspos:=0;
  787. extradataoffset:=0;
  788. if GenerateThumbCode then
  789. begin
  790. multiplier:=2;
  791. limit:=504;
  792. end
  793. else
  794. begin
  795. limit:=1016;
  796. multiplier:=1;
  797. end;
  798. curtai:=tai(list.first);
  799. doinsert:=false;
  800. while assigned(curtai) do
  801. begin
  802. { instruction? }
  803. case curtai.typ of
  804. ait_instruction:
  805. begin
  806. { walk through all operand of the instruction }
  807. for curop:=0 to taicpu(curtai).ops-1 do
  808. begin
  809. { reference? }
  810. if (taicpu(curtai).oper[curop]^.typ=top_ref) then
  811. begin
  812. { pc relative symbol? }
  813. curdatatai:=tai(taicpu(curtai).oper[curop]^.ref^.symboldata);
  814. if assigned(curdatatai) then
  815. begin
  816. { create a new copy of a data entry on arm thumb if the entry has been inserted already
  817. before because arm thumb does not allow pc relative negative offsets }
  818. if (GenerateThumbCode) and
  819. tai_label(curdatatai).inserted then
  820. begin
  821. current_asmdata.getjumplabel(l);
  822. hp:=tai_label.create(l);
  823. listtoinsert.Concat(hp);
  824. hp2:=tai(curdatatai.Next.GetCopy);
  825. hp2.Next:=nil;
  826. hp2.Previous:=nil;
  827. listtoinsert.Concat(hp2);
  828. taicpu(curtai).oper[curop]^.ref^.symboldata:=hp;
  829. taicpu(curtai).oper[curop]^.ref^.symbol:=l;
  830. curdatatai:=hp;
  831. end;
  832. { move only if we're at the first reference of a label }
  833. if not(tai_label(curdatatai).moved) then
  834. begin
  835. tai_label(curdatatai).moved:=true;
  836. { check if symbol already used. }
  837. { if yes, reuse the symbol }
  838. hp:=tai(curdatatai.next);
  839. removeref:=false;
  840. if assigned(hp) then
  841. begin
  842. case hp.typ of
  843. ait_const:
  844. begin
  845. if (tai_const(hp).consttype=aitconst_64bit) then
  846. inc(extradataoffset,multiplier);
  847. end;
  848. ait_comp_64bit,
  849. ait_real_64bit:
  850. begin
  851. inc(extradataoffset,multiplier);
  852. end;
  853. ait_real_80bit:
  854. begin
  855. inc(extradataoffset,2*multiplier);
  856. end;
  857. end;
  858. { check if the same constant has been already inserted into the currently handled list,
  859. if yes, reuse it }
  860. if (hp.typ=ait_const) then
  861. begin
  862. hp2:=tai(curdata.first);
  863. while assigned(hp2) do
  864. begin
  865. if (hp2.typ=ait_const) and (tai_const(hp2).sym=tai_const(hp).sym)
  866. and (tai_const(hp2).value=tai_const(hp).value) and (tai(hp2.previous).typ=ait_label)
  867. then
  868. begin
  869. with taicpu(curtai).oper[curop]^.ref^ do
  870. begin
  871. symboldata:=hp2.previous;
  872. symbol:=tai_label(hp2.previous).labsym;
  873. end;
  874. removeref:=true;
  875. break;
  876. end;
  877. hp2:=tai(hp2.next);
  878. end;
  879. end;
  880. end;
  881. { move or remove symbol reference }
  882. repeat
  883. hp:=tai(curdatatai.next);
  884. listtoinsert.remove(curdatatai);
  885. if removeref then
  886. curdatatai.free
  887. else
  888. curdata.concat(curdatatai);
  889. curdatatai:=hp;
  890. until (curdatatai=nil) or (curdatatai.typ=ait_label);
  891. if lastinspos=-1 then
  892. lastinspos:=curinspos;
  893. end;
  894. end;
  895. end;
  896. end;
  897. inc(curinspos,multiplier);
  898. end;
  899. ait_align:
  900. begin
  901. { code is always 4 byte aligned, so we don't have to take care of .align 2 which would
  902. requires also incrementing curinspos by 1 }
  903. inc(curinspos,(tai_align(curtai).aligntype div 4)*multiplier);
  904. end;
  905. ait_const:
  906. begin
  907. inc(curinspos,multiplier);
  908. if (tai_const(curtai).consttype=aitconst_64bit) then
  909. inc(curinspos,multiplier);
  910. end;
  911. ait_real_32bit:
  912. begin
  913. inc(curinspos,multiplier);
  914. end;
  915. ait_comp_64bit,
  916. ait_real_64bit:
  917. begin
  918. inc(curinspos,2*multiplier);
  919. end;
  920. ait_real_80bit:
  921. begin
  922. inc(curinspos,3*multiplier);
  923. end;
  924. end;
  925. { special case for case jump tables }
  926. penalty:=0;
  927. if SimpleGetNextInstruction(curtai,hp) and
  928. (tai(hp).typ=ait_instruction) then
  929. begin
  930. case taicpu(hp).opcode of
  931. A_MOV,
  932. A_LDR,
  933. A_ADD:
  934. { approximation if we hit a case jump table }
  935. if ((taicpu(hp).opcode in [A_ADD,A_LDR]) and not(GenerateThumbCode or GenerateThumb2Code) and
  936. (taicpu(hp).oper[0]^.typ=top_reg) and
  937. (taicpu(hp).oper[0]^.reg=NR_PC)) or
  938. ((taicpu(hp).opcode=A_MOV) and (GenerateThumbCode) and
  939. (taicpu(hp).oper[0]^.typ=top_reg) and
  940. (taicpu(hp).oper[0]^.reg=NR_PC))
  941. then
  942. begin
  943. penalty:=multiplier;
  944. hp:=tai(hp.next);
  945. { skip register allocations and comments inserted by the optimizer as well as a label
  946. as jump tables for thumb might have }
  947. while assigned(hp) and (hp.typ in [ait_comment,ait_regalloc,ait_label]) do
  948. hp:=tai(hp.next);
  949. while assigned(hp) and (hp.typ=ait_const) do
  950. begin
  951. inc(penalty,multiplier);
  952. hp:=tai(hp.next);
  953. end;
  954. end;
  955. A_IT:
  956. begin
  957. if GenerateThumb2Code then
  958. penalty:=multiplier;
  959. { check if the next instruction fits as well
  960. or if we splitted after the it so split before }
  961. CheckLimit(hp,1);
  962. end;
  963. A_ITE,
  964. A_ITT:
  965. begin
  966. if GenerateThumb2Code then
  967. penalty:=2*multiplier;
  968. { check if the next two instructions fit as well
  969. or if we splitted them so split before }
  970. CheckLimit(hp,2);
  971. end;
  972. A_ITEE,
  973. A_ITTE,
  974. A_ITET,
  975. A_ITTT:
  976. begin
  977. if GenerateThumb2Code then
  978. penalty:=3*multiplier;
  979. { check if the next three instructions fit as well
  980. or if we splitted them so split before }
  981. CheckLimit(hp,3);
  982. end;
  983. A_ITEEE,
  984. A_ITTEE,
  985. A_ITETE,
  986. A_ITTTE,
  987. A_ITEET,
  988. A_ITTET,
  989. A_ITETT,
  990. A_ITTTT:
  991. begin
  992. if GenerateThumb2Code then
  993. penalty:=4*multiplier;
  994. { check if the next three instructions fit as well
  995. or if we splitted them so split before }
  996. CheckLimit(hp,4);
  997. end;
  998. end;
  999. end;
  1000. CheckLimit(curtai,1);
  1001. { don't miss an insert }
  1002. doinsert:=doinsert or
  1003. (not(curdata.empty) and
  1004. (curinspos-lastinspos+penalty+extradataoffset>limit));
  1005. { split only at real instructions else the test below fails }
  1006. if doinsert and (curtai.typ=ait_instruction) and
  1007. (
  1008. { don't split loads of pc to lr and the following move }
  1009. not(
  1010. (taicpu(curtai).opcode=A_MOV) and
  1011. (taicpu(curtai).oper[0]^.typ=top_reg) and
  1012. (taicpu(curtai).oper[0]^.reg=NR_R14) and
  1013. (taicpu(curtai).oper[1]^.typ=top_reg) and
  1014. (taicpu(curtai).oper[1]^.reg=NR_PC)
  1015. )
  1016. ) and
  1017. (
  1018. { do not insert data after a B instruction due to their limited range }
  1019. not((GenerateThumbCode) and
  1020. (taicpu(curtai).opcode=A_B)
  1021. )
  1022. ) then
  1023. begin
  1024. lastinspos:=-1;
  1025. extradataoffset:=0;
  1026. if GenerateThumbCode then
  1027. limit:=502
  1028. else
  1029. limit:=1016;
  1030. { on arm thumb, insert the data always after all labels etc. following an instruction so it
  1031. is prevent that a bxx yyy; bl xxx; yyyy: sequence gets separated ( we never insert on arm thumb after
  1032. bxx) and the distance of bxx gets too long }
  1033. if GenerateThumbCode then
  1034. while assigned(tai(curtai.Next)) and (tai(curtai.Next).typ in SkipInstr+[ait_label]) do
  1035. curtai:=tai(curtai.next);
  1036. doinsert:=false;
  1037. current_asmdata.getjumplabel(l);
  1038. { align jump in thumb .text section to 4 bytes }
  1039. if not(curdata.empty) and (GenerateThumbCode) then
  1040. curdata.Insert(tai_align.Create(4));
  1041. curdata.insert(taicpu.op_sym(A_B,l));
  1042. curdata.concat(tai_label.create(l));
  1043. { mark all labels as inserted, arm thumb
  1044. needs this, so data referencing an already inserted label can be
  1045. duplicated because arm thumb does not allow negative pc relative offset }
  1046. hp2:=tai(curdata.first);
  1047. while assigned(hp2) do
  1048. begin
  1049. if hp2.typ=ait_label then
  1050. tai_label(hp2).inserted:=true;
  1051. hp2:=tai(hp2.next);
  1052. end;
  1053. { continue with the last inserted label because we use later
  1054. on SimpleGetNextInstruction, so if we used curtai.next (which
  1055. is then equal curdata.last.previous) we could over see one
  1056. instruction }
  1057. hp:=tai(curdata.Last);
  1058. list.insertlistafter(curtai,curdata);
  1059. curtai:=hp;
  1060. end
  1061. else
  1062. curtai:=tai(curtai.next);
  1063. end;
  1064. { align jump in thumb .text section to 4 bytes }
  1065. if not(curdata.empty) and (GenerateThumbCode or GenerateThumb2Code) then
  1066. curdata.Insert(tai_align.Create(4));
  1067. list.concatlist(curdata);
  1068. curdata.free;
  1069. end;
  1070. procedure ensurethumb2encodings(list: TAsmList);
  1071. var
  1072. curtai: tai;
  1073. op2reg: TRegister;
  1074. begin
  1075. { Do Thumb-2 16bit -> 32bit transformations }
  1076. curtai:=tai(list.first);
  1077. while assigned(curtai) do
  1078. begin
  1079. case curtai.typ of
  1080. ait_instruction:
  1081. begin
  1082. case taicpu(curtai).opcode of
  1083. A_ADD:
  1084. begin
  1085. { Set wide flag for ADD Rd,Rn,Rm where registers are over R7(high register set) }
  1086. if taicpu(curtai).ops = 3 then
  1087. begin
  1088. if taicpu(curtai).oper[2]^.typ in [top_reg,top_shifterop] then
  1089. begin
  1090. if taicpu(curtai).oper[2]^.typ = top_reg then
  1091. op2reg := taicpu(curtai).oper[2]^.reg
  1092. else if taicpu(curtai).oper[2]^.shifterop^.rs <> NR_NO then
  1093. op2reg := taicpu(curtai).oper[2]^.shifterop^.rs
  1094. else
  1095. op2reg := NR_NO;
  1096. if op2reg <> NR_NO then
  1097. begin
  1098. if (taicpu(curtai).oper[0]^.reg >= NR_R8) or
  1099. (taicpu(curtai).oper[1]^.reg >= NR_R8) or
  1100. (op2reg >= NR_R8) then
  1101. begin
  1102. taicpu(curtai).wideformat:=true;
  1103. { Handle special cases where register rules are violated by optimizer/user }
  1104. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  1105. { Transform ADD.W Rx, Ry, R13 into ADD.W Rx, R13, Ry }
  1106. if (op2reg = NR_R13) and (taicpu(curtai).oper[2]^.typ = top_reg) then
  1107. begin
  1108. taicpu(curtai).oper[2]^.reg := taicpu(curtai).oper[1]^.reg;
  1109. taicpu(curtai).oper[1]^.reg := op2reg;
  1110. end;
  1111. end;
  1112. end;
  1113. end;
  1114. end;
  1115. end;
  1116. end;
  1117. end;
  1118. end;
  1119. curtai:=tai(curtai.Next);
  1120. end;
  1121. end;
  1122. function getMergedInstruction(FirstOp,LastOp:TAsmOp;InvertLast:boolean) : TAsmOp;
  1123. const
  1124. opTable: array[A_IT..A_ITTTT] of string =
  1125. ('T','TE','TT','TEE','TTE','TET','TTT',
  1126. 'TEEE','TTEE','TETE','TTTE',
  1127. 'TEET','TTET','TETT','TTTT');
  1128. invertedOpTable: array[A_IT..A_ITTTT] of string =
  1129. ('E','ET','EE','ETT','EET','ETE','EEE',
  1130. 'ETTT','EETT','ETET','EEET',
  1131. 'ETTE','EETE','ETEE','EEEE');
  1132. var
  1133. resStr : string;
  1134. i : TAsmOp;
  1135. begin
  1136. if InvertLast then
  1137. resStr := opTable[FirstOp]+invertedOpTable[LastOp]
  1138. else
  1139. resStr := opTable[FirstOp]+opTable[LastOp];
  1140. if length(resStr) > 4 then
  1141. internalerror(2012100805);
  1142. for i := low(opTable) to high(opTable) do
  1143. if opTable[i] = resStr then
  1144. exit(i);
  1145. internalerror(2012100806);
  1146. end;
  1147. procedure foldITInstructions(list: TAsmList);
  1148. var
  1149. curtai,hp1 : tai;
  1150. levels,i : LongInt;
  1151. begin
  1152. curtai:=tai(list.First);
  1153. while assigned(curtai) do
  1154. begin
  1155. case curtai.typ of
  1156. ait_instruction:
  1157. if IsIT(taicpu(curtai).opcode) then
  1158. begin
  1159. levels := GetITLevels(taicpu(curtai).opcode);
  1160. if levels < 4 then
  1161. begin
  1162. i:=levels;
  1163. hp1:=tai(curtai.Next);
  1164. while assigned(hp1) and
  1165. (i > 0) do
  1166. begin
  1167. if hp1.typ=ait_instruction then
  1168. begin
  1169. dec(i);
  1170. if (i = 0) and
  1171. mustbelast(hp1) then
  1172. begin
  1173. hp1:=nil;
  1174. break;
  1175. end;
  1176. end;
  1177. hp1:=tai(hp1.Next);
  1178. end;
  1179. if assigned(hp1) then
  1180. begin
  1181. // We are pointing at the first instruction after the IT block
  1182. while assigned(hp1) and
  1183. (hp1.typ<>ait_instruction) do
  1184. hp1:=tai(hp1.Next);
  1185. if assigned(hp1) and
  1186. (hp1.typ=ait_instruction) and
  1187. IsIT(taicpu(hp1).opcode) then
  1188. begin
  1189. if (levels+GetITLevels(taicpu(hp1).opcode) <= 4) and
  1190. ((taicpu(curtai).oper[0]^.cc=taicpu(hp1).oper[0]^.cc) or
  1191. (taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc))) then
  1192. begin
  1193. taicpu(curtai).opcode:=getMergedInstruction(taicpu(curtai).opcode,
  1194. taicpu(hp1).opcode,
  1195. taicpu(curtai).oper[0]^.cc=inverse_cond(taicpu(hp1).oper[0]^.cc));
  1196. list.Remove(hp1);
  1197. hp1.Free;
  1198. end;
  1199. end;
  1200. end;
  1201. end;
  1202. end;
  1203. end;
  1204. curtai:=tai(curtai.Next);
  1205. end;
  1206. end;
  1207. procedure fix_invalid_imms(list: TAsmList);
  1208. var
  1209. curtai: tai;
  1210. sh: byte;
  1211. begin
  1212. curtai:=tai(list.First);
  1213. while assigned(curtai) do
  1214. begin
  1215. case curtai.typ of
  1216. ait_instruction:
  1217. begin
  1218. if (taicpu(curtai).opcode in [A_AND,A_BIC]) and
  1219. (taicpu(curtai).ops=3) and
  1220. (taicpu(curtai).oper[2]^.typ=top_const) and
  1221. (not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
  1222. is_shifter_const((not taicpu(curtai).oper[2]^.val) and $FFFFFFFF,sh) then
  1223. begin
  1224. case taicpu(curtai).opcode of
  1225. A_AND: taicpu(curtai).opcode:=A_BIC;
  1226. A_BIC: taicpu(curtai).opcode:=A_AND;
  1227. end;
  1228. taicpu(curtai).oper[2]^.val:=(not taicpu(curtai).oper[2]^.val) and $FFFFFFFF;
  1229. end
  1230. else if (taicpu(curtai).opcode in [A_SUB,A_ADD]) and
  1231. (taicpu(curtai).ops=3) and
  1232. (taicpu(curtai).oper[2]^.typ=top_const) and
  1233. (not is_shifter_const(taicpu(curtai).oper[2]^.val,sh)) and
  1234. is_shifter_const(-taicpu(curtai).oper[2]^.val,sh) then
  1235. begin
  1236. case taicpu(curtai).opcode of
  1237. A_ADD: taicpu(curtai).opcode:=A_SUB;
  1238. A_SUB: taicpu(curtai).opcode:=A_ADD;
  1239. end;
  1240. taicpu(curtai).oper[2]^.val:=-taicpu(curtai).oper[2]^.val;
  1241. end;
  1242. end;
  1243. end;
  1244. curtai:=tai(curtai.Next);
  1245. end;
  1246. end;
  1247. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1248. begin
  1249. { Do Thumb-2 16bit -> 32bit transformations }
  1250. if GenerateThumb2Code then
  1251. begin
  1252. ensurethumb2encodings(list);
  1253. foldITInstructions(list);
  1254. end;
  1255. fix_invalid_imms(list);
  1256. insertpcrelativedata(list, listtoinsert);
  1257. end;
  1258. procedure InsertPData;
  1259. var
  1260. prolog: TAsmList;
  1261. begin
  1262. prolog:=TAsmList.create;
  1263. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1264. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1265. prolog.concat(Tai_const.Create_32bit(0));
  1266. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1267. { dummy function }
  1268. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1269. current_asmdata.asmlists[al_start].insertList(prolog);
  1270. prolog.Free;
  1271. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1272. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1273. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1274. end;
  1275. (*
  1276. Floating point instruction format information, taken from the linux kernel
  1277. ARM Floating Point Instruction Classes
  1278. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1279. |c o n d|1 1 0 P|U|u|W|L| Rn |v| Fd |0|0|0|1| o f f s e t | CPDT
  1280. |c o n d|1 1 0 P|U|w|W|L| Rn |x| Fd |0|0|1|0| o f f s e t | CPDT (copro 2)
  1281. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1282. |c o n d|1 1 1 0|a|b|c|d|e| Fn |j| Fd |0|0|0|1|f|g|h|0|i| Fm | CPDO
  1283. |c o n d|1 1 1 0|a|b|c|L|e| Fn | Rd |0|0|0|1|f|g|h|1|i| Fm | CPRT
  1284. |c o n d|1 1 1 0|a|b|c|1|e| Fn |1|1|1|1|0|0|0|1|f|g|h|1|i| Fm | comparisons
  1285. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1286. CPDT data transfer instructions
  1287. LDF, STF, LFM (copro 2), SFM (copro 2)
  1288. CPDO dyadic arithmetic instructions
  1289. ADF, MUF, SUF, RSF, DVF, RDF,
  1290. POW, RPW, RMF, FML, FDV, FRD, POL
  1291. CPDO monadic arithmetic instructions
  1292. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1293. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1294. CPRT joint arithmetic/data transfer instructions
  1295. FIX (arithmetic followed by load/store)
  1296. FLT (load/store followed by arithmetic)
  1297. CMF, CNF CMFE, CNFE (comparisons)
  1298. WFS, RFS (write/read floating point status register)
  1299. WFC, RFC (write/read floating point control register)
  1300. cond condition codes
  1301. P pre/post index bit: 0 = postindex, 1 = preindex
  1302. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1303. W write back bit: 1 = update base register (Rn)
  1304. L load/store bit: 0 = store, 1 = load
  1305. Rn base register
  1306. Rd destination/source register
  1307. Fd floating point destination register
  1308. Fn floating point source register
  1309. Fm floating point source register or floating point constant
  1310. uv transfer length (TABLE 1)
  1311. wx register count (TABLE 2)
  1312. abcd arithmetic opcode (TABLES 3 & 4)
  1313. ef destination size (rounding precision) (TABLE 5)
  1314. gh rounding mode (TABLE 6)
  1315. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1316. i constant bit: 1 = constant (TABLE 6)
  1317. */
  1318. /*
  1319. TABLE 1
  1320. +-------------------------+---+---+---------+---------+
  1321. | Precision | u | v | FPSR.EP | length |
  1322. +-------------------------+---+---+---------+---------+
  1323. | Single | 0 | 0 | x | 1 words |
  1324. | Double | 1 | 1 | x | 2 words |
  1325. | Extended | 1 | 1 | x | 3 words |
  1326. | Packed decimal | 1 | 1 | 0 | 3 words |
  1327. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1328. +-------------------------+---+---+---------+---------+
  1329. Note: x = don't care
  1330. */
  1331. /*
  1332. TABLE 2
  1333. +---+---+---------------------------------+
  1334. | w | x | Number of registers to transfer |
  1335. +---+---+---------------------------------+
  1336. | 0 | 1 | 1 |
  1337. | 1 | 0 | 2 |
  1338. | 1 | 1 | 3 |
  1339. | 0 | 0 | 4 |
  1340. +---+---+---------------------------------+
  1341. */
  1342. /*
  1343. TABLE 3: Dyadic Floating Point Opcodes
  1344. +---+---+---+---+----------+-----------------------+-----------------------+
  1345. | a | b | c | d | Mnemonic | Description | Operation |
  1346. +---+---+---+---+----------+-----------------------+-----------------------+
  1347. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1348. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1349. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1350. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1351. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1352. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1353. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1354. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1355. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1356. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1357. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1358. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1359. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1360. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1361. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1362. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1363. +---+---+---+---+----------+-----------------------+-----------------------+
  1364. Note: POW, RPW, POL are deprecated, and are available for backwards
  1365. compatibility only.
  1366. */
  1367. /*
  1368. TABLE 4: Monadic Floating Point Opcodes
  1369. +---+---+---+---+----------+-----------------------+-----------------------+
  1370. | a | b | c | d | Mnemonic | Description | Operation |
  1371. +---+---+---+---+----------+-----------------------+-----------------------+
  1372. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1373. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1374. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1375. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1376. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1377. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1378. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1379. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1380. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1381. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1382. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1383. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1384. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1385. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1386. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1387. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1388. +---+---+---+---+----------+-----------------------+-----------------------+
  1389. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1390. available for backwards compatibility only.
  1391. */
  1392. /*
  1393. TABLE 5
  1394. +-------------------------+---+---+
  1395. | Rounding Precision | e | f |
  1396. +-------------------------+---+---+
  1397. | IEEE Single precision | 0 | 0 |
  1398. | IEEE Double precision | 0 | 1 |
  1399. | IEEE Extended precision | 1 | 0 |
  1400. | undefined (trap) | 1 | 1 |
  1401. +-------------------------+---+---+
  1402. */
  1403. /*
  1404. TABLE 5
  1405. +---------------------------------+---+---+
  1406. | Rounding Mode | g | h |
  1407. +---------------------------------+---+---+
  1408. | Round to nearest (default) | 0 | 0 |
  1409. | Round toward plus infinity | 0 | 1 |
  1410. | Round toward negative infinity | 1 | 0 |
  1411. | Round toward zero | 1 | 1 |
  1412. +---------------------------------+---+---+
  1413. *)
  1414. function taicpu.GetString:string;
  1415. var
  1416. i : longint;
  1417. s : string;
  1418. addsize : boolean;
  1419. begin
  1420. s:='['+gas_op2str[opcode];
  1421. for i:=0 to ops-1 do
  1422. begin
  1423. with oper[i]^ do
  1424. begin
  1425. if i=0 then
  1426. s:=s+' '
  1427. else
  1428. s:=s+',';
  1429. { type }
  1430. addsize:=false;
  1431. if (ot and OT_VREG)=OT_VREG then
  1432. s:=s+'vreg'
  1433. else
  1434. if (ot and OT_FPUREG)=OT_FPUREG then
  1435. s:=s+'fpureg'
  1436. else
  1437. if (ot and OT_REGF)=OT_REGF then
  1438. s:=s+'creg'
  1439. else
  1440. if (ot and OT_REGISTER)=OT_REGISTER then
  1441. begin
  1442. s:=s+'reg';
  1443. addsize:=true;
  1444. end
  1445. else
  1446. if (ot and OT_REGLIST)=OT_REGLIST then
  1447. begin
  1448. s:=s+'reglist';
  1449. addsize:=false;
  1450. end
  1451. else
  1452. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1453. begin
  1454. s:=s+'imm';
  1455. addsize:=true;
  1456. end
  1457. else
  1458. if (ot and OT_MEMORY)=OT_MEMORY then
  1459. begin
  1460. s:=s+'mem';
  1461. addsize:=true;
  1462. if (ot and OT_AM2)<>0 then
  1463. s:=s+' am2 '
  1464. else if (ot and OT_AM6)<>0 then
  1465. s:=s+' am2 ';
  1466. end
  1467. else
  1468. if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
  1469. begin
  1470. s:=s+'shifterop';
  1471. addsize:=false;
  1472. end
  1473. else
  1474. s:=s+'???';
  1475. { size }
  1476. if addsize then
  1477. begin
  1478. if (ot and OT_BITS8)<>0 then
  1479. s:=s+'8'
  1480. else
  1481. if (ot and OT_BITS16)<>0 then
  1482. s:=s+'24'
  1483. else
  1484. if (ot and OT_BITS32)<>0 then
  1485. s:=s+'32'
  1486. else
  1487. if (ot and OT_BITSSHIFTER)<>0 then
  1488. s:=s+'shifter'
  1489. else
  1490. s:=s+'??';
  1491. { signed }
  1492. if (ot and OT_SIGNED)<>0 then
  1493. s:=s+'s';
  1494. end;
  1495. end;
  1496. end;
  1497. GetString:=s+']';
  1498. end;
  1499. procedure taicpu.ResetPass1;
  1500. begin
  1501. { we need to reset everything here, because the choosen insentry
  1502. can be invalid for a new situation where the previously optimized
  1503. insentry is not correct }
  1504. InsEntry:=nil;
  1505. InsSize:=0;
  1506. LastInsOffset:=-1;
  1507. end;
  1508. procedure taicpu.ResetPass2;
  1509. begin
  1510. { we are here in a second pass, check if the instruction can be optimized }
  1511. if assigned(InsEntry) and
  1512. ((InsEntry^.flags and IF_PASS2)<>0) then
  1513. begin
  1514. InsEntry:=nil;
  1515. InsSize:=0;
  1516. end;
  1517. LastInsOffset:=-1;
  1518. end;
  1519. function taicpu.CheckIfValid:boolean;
  1520. begin
  1521. Result:=False; { unimplemented }
  1522. end;
  1523. function taicpu.Pass1(objdata:TObjData):longint;
  1524. var
  1525. ldr2op : array[PF_B..PF_T] of tasmop = (
  1526. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1527. str2op : array[PF_B..PF_T] of tasmop = (
  1528. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1529. begin
  1530. Pass1:=0;
  1531. { Save the old offset and set the new offset }
  1532. InsOffset:=ObjData.CurrObjSec.Size;
  1533. { Error? }
  1534. if (Insentry=nil) and (InsSize=-1) then
  1535. exit;
  1536. { set the file postion }
  1537. current_filepos:=fileinfo;
  1538. { tranlate LDR+postfix to complete opcode }
  1539. if (opcode=A_LDR) and (oppostfix=PF_D) then
  1540. begin
  1541. opcode:=A_LDRD;
  1542. oppostfix:=PF_None;
  1543. end
  1544. else if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1545. begin
  1546. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1547. opcode:=ldr2op[oppostfix]
  1548. else
  1549. internalerror(2005091001);
  1550. if opcode=A_None then
  1551. internalerror(2005091004);
  1552. { postfix has been added to opcode }
  1553. oppostfix:=PF_None;
  1554. end
  1555. else if (opcode=A_STR) and (oppostfix=PF_D) then
  1556. begin
  1557. opcode:=A_STRD;
  1558. oppostfix:=PF_None;
  1559. end
  1560. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1561. begin
  1562. if (oppostfix in [low(str2op)..high(str2op)]) then
  1563. opcode:=str2op[oppostfix]
  1564. else
  1565. internalerror(2005091002);
  1566. if opcode=A_None then
  1567. internalerror(2005091003);
  1568. { postfix has been added to opcode }
  1569. oppostfix:=PF_None;
  1570. end;
  1571. { Get InsEntry }
  1572. if FindInsEntry(objdata) then
  1573. begin
  1574. InsSize:=4;
  1575. LastInsOffset:=InsOffset;
  1576. Pass1:=InsSize;
  1577. exit;
  1578. end;
  1579. LastInsOffset:=-1;
  1580. end;
  1581. procedure taicpu.Pass2(objdata:TObjData);
  1582. begin
  1583. { error in pass1 ? }
  1584. if insentry=nil then
  1585. exit;
  1586. current_filepos:=fileinfo;
  1587. { Generate the instruction }
  1588. GenCode(objdata);
  1589. end;
  1590. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1591. begin
  1592. end;
  1593. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1594. begin
  1595. end;
  1596. procedure taicpu.ppubuildderefimploper(var o:toper);
  1597. begin
  1598. end;
  1599. procedure taicpu.ppuderefoper(var o:toper);
  1600. begin
  1601. end;
  1602. function taicpu.InsEnd:longint;
  1603. begin
  1604. Result:=0; { unimplemented }
  1605. end;
  1606. procedure taicpu.create_ot(objdata:TObjData);
  1607. var
  1608. i,l,relsize : longint;
  1609. dummy : byte;
  1610. currsym : TObjSymbol;
  1611. begin
  1612. if ops=0 then
  1613. exit;
  1614. { update oper[].ot field }
  1615. for i:=0 to ops-1 do
  1616. with oper[i]^ do
  1617. begin
  1618. case typ of
  1619. top_regset:
  1620. begin
  1621. ot:=OT_REGLIST;
  1622. end;
  1623. top_reg :
  1624. begin
  1625. case getregtype(reg) of
  1626. R_INTREGISTER:
  1627. ot:=OT_REG32 or OT_SHIFTEROP;
  1628. R_FPUREGISTER:
  1629. ot:=OT_FPUREG;
  1630. R_MMREGISTER:
  1631. ot:=OT_VREG;
  1632. R_SPECIALREGISTER:
  1633. ot:=OT_REGF;
  1634. else
  1635. internalerror(2005090901);
  1636. end;
  1637. end;
  1638. top_ref :
  1639. begin
  1640. if ref^.refaddr=addr_no then
  1641. begin
  1642. { create ot field }
  1643. { we should get the size here dependend on the
  1644. instruction }
  1645. if (ot and OT_SIZE_MASK)=0 then
  1646. ot:=OT_MEMORY or OT_BITS32
  1647. else
  1648. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1649. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1650. ot:=ot or OT_MEM_OFFS;
  1651. { if we need to fix a reference, we do it here }
  1652. { pc relative addressing }
  1653. if (ref^.base=NR_NO) and
  1654. (ref^.index=NR_NO) and
  1655. (ref^.shiftmode=SM_None)
  1656. { at least we should check if the destination symbol
  1657. is in a text section }
  1658. { and
  1659. (ref^.symbol^.owner="text") } then
  1660. ref^.base:=NR_PC;
  1661. { determine possible address modes }
  1662. if (ref^.base<>NR_NO) and
  1663. (opcode in [A_LDREX,A_LDREXB,A_LDREXH,A_LDREXD,
  1664. A_STREX,A_STREXB,A_STREXH,A_STREXD]) and
  1665. (
  1666. (ref^.addressmode=AM_OFFSET) and
  1667. (ref^.index=NR_NO) and
  1668. (ref^.shiftmode=SM_None) and
  1669. (ref^.offset=0)
  1670. ) then
  1671. ot:=ot or OT_AM6
  1672. else if (ref^.base<>NR_NO) and
  1673. (
  1674. (
  1675. (ref^.index=NR_NO) and
  1676. (ref^.shiftmode=SM_None) and
  1677. (ref^.offset>=-4097) and
  1678. (ref^.offset<=4097)
  1679. ) or
  1680. (
  1681. (ref^.shiftmode=SM_None) and
  1682. (ref^.offset=0)
  1683. ) or
  1684. (
  1685. (ref^.index<>NR_NO) and
  1686. (ref^.shiftmode<>SM_None) and
  1687. (ref^.shiftimm<=32) and
  1688. (ref^.offset=0)
  1689. )
  1690. ) then
  1691. ot:=ot or OT_AM2;
  1692. if (ref^.index<>NR_NO) and
  1693. (oppostfix in [PF_None,PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1694. (
  1695. (ref^.base=NR_NO) and
  1696. (ref^.shiftmode=SM_None) and
  1697. (ref^.offset=0)
  1698. ) then
  1699. ot:=ot or OT_AM4;
  1700. end
  1701. else
  1702. begin
  1703. l:=ref^.offset;
  1704. currsym:=ObjData.symbolref(ref^.symbol);
  1705. if assigned(currsym) then
  1706. inc(l,currsym.address);
  1707. relsize:=(InsOffset+2)-l;
  1708. if (relsize<-33554428) or (relsize>33554428) then
  1709. ot:=OT_IMM32
  1710. else
  1711. ot:=OT_IMM24;
  1712. end;
  1713. end;
  1714. top_local :
  1715. begin
  1716. { we should get the size here dependend on the
  1717. instruction }
  1718. if (ot and OT_SIZE_MASK)=0 then
  1719. ot:=OT_MEMORY or OT_BITS32
  1720. else
  1721. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1722. end;
  1723. top_const :
  1724. begin
  1725. ot:=OT_IMMEDIATE;
  1726. if is_shifter_const(val,dummy) then
  1727. ot:=OT_IMMSHIFTER
  1728. else
  1729. ot:=OT_IMM32
  1730. end;
  1731. top_none :
  1732. begin
  1733. { generated when there was an error in the
  1734. assembler reader. It never happends when generating
  1735. assembler }
  1736. end;
  1737. top_shifterop:
  1738. begin
  1739. ot:=OT_SHIFTEROP;
  1740. end;
  1741. top_conditioncode:
  1742. begin
  1743. ot:=OT_CONDITION;
  1744. end;
  1745. else
  1746. begin writeln(typ);
  1747. internalerror(200402261); end;
  1748. end;
  1749. end;
  1750. end;
  1751. function taicpu.Matches(p:PInsEntry):longint;
  1752. { * IF_SM stands for Size Match: any operand whose size is not
  1753. * explicitly specified by the template is `really' intended to be
  1754. * the same size as the first size-specified operand.
  1755. * Non-specification is tolerated in the input instruction, but
  1756. * _wrong_ specification is not.
  1757. *
  1758. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1759. * three-operand instructions such as SHLD: it implies that the
  1760. * first two operands must match in size, but that the third is
  1761. * required to be _unspecified_.
  1762. *
  1763. * IF_SB invokes Size Byte: operands with unspecified size in the
  1764. * template are really bytes, and so no non-byte specification in
  1765. * the input instruction will be tolerated. IF_SW similarly invokes
  1766. * Size Word, and IF_SD invokes Size Doubleword.
  1767. *
  1768. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1769. * that any operand with unspecified size in the template is
  1770. * required to have unspecified size in the instruction too...)
  1771. }
  1772. var
  1773. i{,j,asize,oprs} : longint;
  1774. {siz : array[0..3] of longint;}
  1775. begin
  1776. Matches:=100;
  1777. { Check the opcode and operands }
  1778. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1779. begin
  1780. Matches:=0;
  1781. exit;
  1782. end;
  1783. { Check that no spurious colons or TOs are present }
  1784. for i:=0 to p^.ops-1 do
  1785. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  1786. begin
  1787. Matches:=0;
  1788. exit;
  1789. end;
  1790. { Check that the operand flags all match up }
  1791. for i:=0 to p^.ops-1 do
  1792. begin
  1793. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  1794. ((p^.optypes[i] and OT_SIZE_MASK) and
  1795. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  1796. begin
  1797. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  1798. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  1799. begin
  1800. Matches:=0;
  1801. exit;
  1802. end
  1803. else
  1804. Matches:=1;
  1805. end;
  1806. end;
  1807. { check postfixes:
  1808. the existance of a certain postfix requires a
  1809. particular code }
  1810. { update condition flags
  1811. or floating point single }
  1812. if (oppostfix=PF_S) and
  1813. not(p^.code[0] in [#$04..#$0B,#$14..#$16,#$29,#$30]) then
  1814. begin
  1815. Matches:=0;
  1816. exit;
  1817. end;
  1818. { floating point size }
  1819. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  1820. not(p^.code[0] in []) then
  1821. begin
  1822. Matches:=0;
  1823. exit;
  1824. end;
  1825. { multiple load/store address modes }
  1826. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1827. not(p^.code[0] in [
  1828. // ldr,str,ldrb,strb
  1829. #$17,
  1830. // stm,ldm
  1831. #$26,
  1832. // vldm/vstm
  1833. #$44
  1834. ]) then
  1835. begin
  1836. Matches:=0;
  1837. exit;
  1838. end;
  1839. { we shouldn't see any opsize prefixes here }
  1840. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  1841. begin
  1842. Matches:=0;
  1843. exit;
  1844. end;
  1845. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  1846. begin
  1847. Matches:=0;
  1848. exit;
  1849. end;
  1850. { Check operand sizes }
  1851. { as default an untyped size can get all the sizes, this is different
  1852. from nasm, but else we need to do a lot checking which opcodes want
  1853. size or not with the automatic size generation }
  1854. (*
  1855. asize:=longint($ffffffff);
  1856. if (p^.flags and IF_SB)<>0 then
  1857. asize:=OT_BITS8
  1858. else if (p^.flags and IF_SW)<>0 then
  1859. asize:=OT_BITS16
  1860. else if (p^.flags and IF_SD)<>0 then
  1861. asize:=OT_BITS32;
  1862. if (p^.flags and IF_ARMASK)<>0 then
  1863. begin
  1864. siz[0]:=0;
  1865. siz[1]:=0;
  1866. siz[2]:=0;
  1867. if (p^.flags and IF_AR0)<>0 then
  1868. siz[0]:=asize
  1869. else if (p^.flags and IF_AR1)<>0 then
  1870. siz[1]:=asize
  1871. else if (p^.flags and IF_AR2)<>0 then
  1872. siz[2]:=asize;
  1873. end
  1874. else
  1875. begin
  1876. { we can leave because the size for all operands is forced to be
  1877. the same
  1878. but not if IF_SB IF_SW or IF_SD is set PM }
  1879. if asize=-1 then
  1880. exit;
  1881. siz[0]:=asize;
  1882. siz[1]:=asize;
  1883. siz[2]:=asize;
  1884. end;
  1885. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1886. begin
  1887. if (p^.flags and IF_SM2)<>0 then
  1888. oprs:=2
  1889. else
  1890. oprs:=p^.ops;
  1891. for i:=0 to oprs-1 do
  1892. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1893. begin
  1894. for j:=0 to oprs-1 do
  1895. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1896. break;
  1897. end;
  1898. end
  1899. else
  1900. oprs:=2;
  1901. { Check operand sizes }
  1902. for i:=0 to p^.ops-1 do
  1903. begin
  1904. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1905. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1906. { Immediates can always include smaller size }
  1907. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1908. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1909. Matches:=2;
  1910. end;
  1911. *)
  1912. end;
  1913. function taicpu.calcsize(p:PInsEntry):shortint;
  1914. begin
  1915. result:=4;
  1916. end;
  1917. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  1918. begin
  1919. Result:=False; { unimplemented }
  1920. end;
  1921. procedure taicpu.Swapoperands;
  1922. begin
  1923. end;
  1924. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1925. var
  1926. i : longint;
  1927. begin
  1928. result:=false;
  1929. { Things which may only be done once, not when a second pass is done to
  1930. optimize }
  1931. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1932. begin
  1933. { create the .ot fields }
  1934. create_ot(objdata);
  1935. { set the file postion }
  1936. current_filepos:=fileinfo;
  1937. end
  1938. else
  1939. begin
  1940. { we've already an insentry so it's valid }
  1941. result:=true;
  1942. exit;
  1943. end;
  1944. { Lookup opcode in the table }
  1945. InsSize:=-1;
  1946. i:=instabcache^[opcode];
  1947. if i=-1 then
  1948. begin
  1949. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1950. exit;
  1951. end;
  1952. insentry:=@instab[i];
  1953. while (insentry^.opcode=opcode) do
  1954. begin
  1955. if matches(insentry)=100 then
  1956. begin
  1957. result:=true;
  1958. exit;
  1959. end;
  1960. inc(i);
  1961. insentry:=@instab[i];
  1962. end;
  1963. if (ops=3) and (opcode=a_sub) then writeln(oppostfix,',',oper[2]^.val);
  1964. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1965. { No instruction found, set insentry to nil and inssize to -1 }
  1966. insentry:=nil;
  1967. inssize:=-1;
  1968. end;
  1969. procedure taicpu.gencode(objdata:TObjData);
  1970. const
  1971. CondVal : array[TAsmCond] of byte=(
  1972. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  1973. $B, $C, $D, $E, 0);
  1974. var
  1975. bytes, rd, rm, rn, d, m, n : dword;
  1976. bytelen : longint;
  1977. dp_operation : boolean;
  1978. i_field : byte;
  1979. currsym : TObjSymbol;
  1980. offset : longint;
  1981. refoper : poper;
  1982. msb : longint;
  1983. r: byte;
  1984. procedure setshifterop(op : byte);
  1985. var
  1986. r : byte;
  1987. imm : dword;
  1988. begin
  1989. case oper[op]^.typ of
  1990. top_const:
  1991. begin
  1992. i_field:=1;
  1993. if oper[op]^.val and $ff=oper[op]^.val then
  1994. bytes:=bytes or dword(oper[op]^.val)
  1995. else
  1996. begin
  1997. { calc rotate and adjust imm }
  1998. r:=0;
  1999. imm:=dword(oper[op]^.val);
  2000. repeat
  2001. imm:=RolDWord(imm, 2);
  2002. inc(r)
  2003. until (imm and $ff)=imm;
  2004. bytes:=bytes or (r shl 8) or imm;
  2005. end;
  2006. end;
  2007. top_reg:
  2008. begin
  2009. i_field:=0;
  2010. bytes:=bytes or getsupreg(oper[op]^.reg);
  2011. { does a real shifter op follow? }
  2012. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  2013. with oper[op+1]^.shifterop^ do
  2014. begin
  2015. bytes:=bytes or (shiftimm shl 7);
  2016. if shiftmode<>SM_RRX then
  2017. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2018. else
  2019. bytes:=bytes or (3 shl 5);
  2020. if getregtype(rs) <> R_INVALIDREGISTER then
  2021. begin
  2022. bytes:=bytes or (1 shl 4);
  2023. bytes:=bytes or (getsupreg(rs) shl 8);
  2024. end
  2025. end;
  2026. end;
  2027. else
  2028. internalerror(2005091103);
  2029. end;
  2030. end;
  2031. function MakeRegList(reglist: tcpuregisterset): word;
  2032. var
  2033. i, w: word;
  2034. begin
  2035. result:=0;
  2036. w:=1;
  2037. for i:=RS_R0 to RS_R15 do
  2038. begin
  2039. if i in reglist then
  2040. result:=result or w;
  2041. w:=w shl 1
  2042. end;
  2043. end;
  2044. function getcoproc(reg: tregister): byte;
  2045. begin
  2046. if reg=NR_p15 then
  2047. result:=15
  2048. else
  2049. begin
  2050. Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
  2051. result:=0;
  2052. end;
  2053. end;
  2054. function getcoprocreg(reg: tregister): byte;
  2055. begin
  2056. result:=getsupreg(reg)-getsupreg(NR_CR0);
  2057. end;
  2058. function getmmreg(reg: tregister): byte;
  2059. begin
  2060. case reg of
  2061. NR_D0: result:=0;
  2062. NR_D1: result:=1;
  2063. NR_D2: result:=2;
  2064. NR_D3: result:=3;
  2065. NR_D4: result:=4;
  2066. NR_D5: result:=5;
  2067. NR_D6: result:=6;
  2068. NR_D7: result:=7;
  2069. NR_D8: result:=8;
  2070. NR_D9: result:=9;
  2071. NR_D10: result:=10;
  2072. NR_D11: result:=11;
  2073. NR_D12: result:=12;
  2074. NR_D13: result:=13;
  2075. NR_D14: result:=14;
  2076. NR_D15: result:=15;
  2077. NR_D16: result:=16;
  2078. NR_D17: result:=17;
  2079. NR_D18: result:=18;
  2080. NR_D19: result:=19;
  2081. NR_D20: result:=20;
  2082. NR_D21: result:=21;
  2083. NR_D22: result:=22;
  2084. NR_D23: result:=23;
  2085. NR_D24: result:=24;
  2086. NR_D25: result:=25;
  2087. NR_D26: result:=26;
  2088. NR_D27: result:=27;
  2089. NR_D28: result:=28;
  2090. NR_D29: result:=29;
  2091. NR_D30: result:=30;
  2092. NR_D31: result:=31;
  2093. NR_S0: result:=0;
  2094. NR_S1: result:=1;
  2095. NR_S2: result:=2;
  2096. NR_S3: result:=3;
  2097. NR_S4: result:=4;
  2098. NR_S5: result:=5;
  2099. NR_S6: result:=6;
  2100. NR_S7: result:=7;
  2101. NR_S8: result:=8;
  2102. NR_S9: result:=9;
  2103. NR_S10: result:=10;
  2104. NR_S11: result:=11;
  2105. NR_S12: result:=12;
  2106. NR_S13: result:=13;
  2107. NR_S14: result:=14;
  2108. NR_S15: result:=15;
  2109. NR_S16: result:=16;
  2110. NR_S17: result:=17;
  2111. NR_S18: result:=18;
  2112. NR_S19: result:=19;
  2113. NR_S20: result:=20;
  2114. NR_S21: result:=21;
  2115. NR_S22: result:=22;
  2116. NR_S23: result:=23;
  2117. NR_S24: result:=24;
  2118. NR_S25: result:=25;
  2119. NR_S26: result:=26;
  2120. NR_S27: result:=27;
  2121. NR_S28: result:=28;
  2122. NR_S29: result:=29;
  2123. NR_S30: result:=30;
  2124. NR_S31: result:=31;
  2125. else
  2126. result:=0;
  2127. end;
  2128. end;
  2129. begin
  2130. bytes:=$0;
  2131. bytelen:=4;
  2132. i_field:=0;
  2133. { evaluate and set condition code }
  2134. bytes:=bytes or (CondVal[condition] shl 28);
  2135. { condition code allowed? }
  2136. { setup rest of the instruction }
  2137. case insentry^.code[0] of
  2138. #$01: // B/BL
  2139. begin
  2140. { set instruction code }
  2141. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2142. { set offset }
  2143. if oper[0]^.typ=top_const then
  2144. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2145. else
  2146. begin
  2147. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2148. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2149. begin
  2150. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  2151. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2152. end
  2153. else
  2154. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2155. end;
  2156. end;
  2157. #$02:
  2158. begin
  2159. { set instruction code }
  2160. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2161. { set code }
  2162. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2163. end;
  2164. #$03:
  2165. begin // BLX/BX
  2166. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2167. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2168. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2169. bytes:=bytes or ord(insentry^.code[4]);
  2170. bytes:=bytes or getsupreg(oper[0]^.reg);
  2171. end;
  2172. #$04..#$07: // SUB
  2173. begin
  2174. { set instruction code }
  2175. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2176. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2177. { set destination }
  2178. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2179. { set Rn }
  2180. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2181. { create shifter op }
  2182. setshifterop(2);
  2183. { set I field }
  2184. bytes:=bytes or (i_field shl 25);
  2185. { set S if necessary }
  2186. if oppostfix=PF_S then
  2187. bytes:=bytes or (1 shl 20);
  2188. end;
  2189. #$08,#$0A,#$0B: // MOV
  2190. begin
  2191. { set instruction code }
  2192. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2193. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2194. { set destination }
  2195. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2196. { create shifter op }
  2197. setshifterop(1);
  2198. { set I field }
  2199. bytes:=bytes or (i_field shl 25);
  2200. { set S if necessary }
  2201. if oppostfix=PF_S then
  2202. bytes:=bytes or (1 shl 20);
  2203. end;
  2204. #$0C,#$0E,#$0F: // CMP
  2205. begin
  2206. { set instruction code }
  2207. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2208. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2209. { set destination }
  2210. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2211. { create shifter op }
  2212. setshifterop(1);
  2213. { set I field }
  2214. bytes:=bytes or (i_field shl 25);
  2215. { always set S bit }
  2216. bytes:=bytes or (1 shl 20);
  2217. end;
  2218. #$14: // MUL/MLA r1,r2,r3
  2219. begin
  2220. { set instruction code }
  2221. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2222. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2223. bytes:=bytes or ord(insentry^.code[3]);
  2224. { set regs }
  2225. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2226. bytes:=bytes or getsupreg(oper[1]^.reg);
  2227. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2228. end;
  2229. #$15: // MUL/MLA r1,r2,r3,r4
  2230. begin
  2231. { set instruction code }
  2232. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2233. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2234. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2235. { set regs }
  2236. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2237. bytes:=bytes or getsupreg(oper[1]^.reg);
  2238. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2239. if ops>3 then
  2240. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2241. else
  2242. bytes:=bytes or ($F shl 12);
  2243. if oppostfix in [PF_R,PF_X] then
  2244. bytes:=bytes or (1 shl 5);
  2245. end;
  2246. #$16: // MULL r1,r2,r3,r4
  2247. begin
  2248. { set instruction code }
  2249. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2250. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2251. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2252. { set regs }
  2253. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2254. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2255. bytes:=bytes or getsupreg(oper[2]^.reg);
  2256. if ops=4 then
  2257. begin
  2258. if oper[3]^.typ=top_shifterop then
  2259. begin
  2260. if opcode in [A_PKHBT,A_PKHTB] then
  2261. begin
  2262. if ((opcode=A_PKHTB) and
  2263. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2264. ((opcode=A_PKHBT) and
  2265. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2266. (oper[3]^.shifterop^.rs<>NR_NO) then
  2267. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2268. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2269. end
  2270. else
  2271. begin
  2272. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2273. (oper[3]^.shifterop^.rs<>NR_NO) or
  2274. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2275. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2276. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2277. end;
  2278. end
  2279. else
  2280. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2281. end;
  2282. if PF_S=oppostfix then
  2283. bytes:=bytes or (1 shl 20);
  2284. if PF_X=oppostfix then
  2285. bytes:=bytes or (1 shl 5);
  2286. end;
  2287. #$17: // LDR/STR
  2288. begin
  2289. { set instruction code }
  2290. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2291. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2292. { set Rn and Rd }
  2293. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2294. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2295. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2296. begin
  2297. { set offset }
  2298. offset:=0;
  2299. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2300. if assigned(currsym) then
  2301. offset:=currsym.offset-insoffset-8;
  2302. offset:=offset+oper[1]^.ref^.offset;
  2303. if offset>=0 then
  2304. begin
  2305. { set U flag }
  2306. bytes:=bytes or (1 shl 23);
  2307. bytes:=bytes or offset
  2308. end
  2309. else
  2310. begin
  2311. offset:=-offset;
  2312. bytes:=bytes or offset
  2313. end;
  2314. end
  2315. else
  2316. begin
  2317. { set U flag }
  2318. if oper[1]^.ref^.signindex>=0 then
  2319. bytes:=bytes or (1 shl 23);
  2320. { set I flag }
  2321. bytes:=bytes or (1 shl 25);
  2322. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2323. { set shift }
  2324. with oper[1]^.ref^ do
  2325. if shiftmode<>SM_None then
  2326. begin
  2327. bytes:=bytes or (shiftimm shl 7);
  2328. if shiftmode<>SM_RRX then
  2329. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2330. else
  2331. bytes:=bytes or (3 shl 5);
  2332. end
  2333. end;
  2334. { set W bit }
  2335. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2336. bytes:=bytes or (1 shl 21);
  2337. { set P bit if necessary }
  2338. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2339. bytes:=bytes or (1 shl 24);
  2340. end;
  2341. #$18: // LDREX/STREX
  2342. begin
  2343. { set instruction code }
  2344. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2345. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2346. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2347. bytes:=bytes or ord(insentry^.code[4]);
  2348. { set Rn and Rd }
  2349. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2350. if (ops=3) then
  2351. begin
  2352. if opcode<>A_LDREXD then
  2353. bytes:=bytes or getsupreg(oper[1]^.reg);
  2354. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2355. end
  2356. else if (ops=4) then // STREXD
  2357. begin
  2358. if opcode<>A_LDREXD then
  2359. bytes:=bytes or getsupreg(oper[1]^.reg);
  2360. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  2361. end
  2362. else
  2363. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  2364. end;
  2365. #$19: // LDRD/STRD
  2366. begin
  2367. { set instruction code }
  2368. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2369. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2370. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2371. bytes:=bytes or ord(insentry^.code[4]);
  2372. { set Rn and Rd }
  2373. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2374. refoper:=oper[1];
  2375. if ops=3 then
  2376. refoper:=oper[2];
  2377. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2378. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2379. begin
  2380. bytes:=bytes or (1 shl 22);
  2381. { set offset }
  2382. offset:=0;
  2383. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2384. if assigned(currsym) then
  2385. offset:=currsym.offset-insoffset-8;
  2386. offset:=offset+refoper^.ref^.offset;
  2387. if offset>=0 then
  2388. begin
  2389. { set U flag }
  2390. bytes:=bytes or (1 shl 23);
  2391. bytes:=bytes or (offset and $F);
  2392. bytes:=bytes or ((offset and $F0) shl 4);
  2393. end
  2394. else
  2395. begin
  2396. offset:=-offset;
  2397. bytes:=bytes or (offset and $F);
  2398. bytes:=bytes or ((offset and $F0) shl 4);
  2399. end;
  2400. end
  2401. else
  2402. begin
  2403. { set U flag }
  2404. if refoper^.ref^.signindex>=0 then
  2405. bytes:=bytes or (1 shl 23);
  2406. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2407. end;
  2408. { set W bit }
  2409. if refoper^.ref^.addressmode=AM_PREINDEXED then
  2410. bytes:=bytes or (1 shl 21);
  2411. { set P bit if necessary }
  2412. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  2413. bytes:=bytes or (1 shl 24);
  2414. end;
  2415. #$1A: // QADD/QSUB
  2416. begin
  2417. { set instruction code }
  2418. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2419. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2420. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2421. { set regs }
  2422. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2423. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  2424. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2425. end;
  2426. #$1B:
  2427. begin
  2428. { set instruction code }
  2429. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2430. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2431. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2432. { set regs }
  2433. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2434. bytes:=bytes or getsupreg(oper[1]^.reg);
  2435. if ops=3 then
  2436. begin
  2437. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  2438. (oper[2]^.shifterop^.rs<>NR_NO) or
  2439. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  2440. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2441. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2442. end;
  2443. end;
  2444. #$1C: // MCR/MRC
  2445. begin
  2446. { set instruction code }
  2447. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2448. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2449. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2450. { set regs and operands }
  2451. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2452. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  2453. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2454. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  2455. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2456. if ops > 5 then
  2457. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  2458. end;
  2459. #$1D: // MCRR/MRRC
  2460. begin
  2461. { set instruction code }
  2462. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2463. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2464. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2465. { set regs and operands }
  2466. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2467. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  2468. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2469. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  2470. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2471. end;
  2472. #$22: // LDRH/STRH
  2473. begin
  2474. { set instruction code }
  2475. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2476. bytes:=bytes or ord(insentry^.code[2]);
  2477. { src/dest register (Rd) }
  2478. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2479. { base register (Rn) }
  2480. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2481. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2482. begin
  2483. bytes:=bytes or (1 shl 22); // with immediate offset
  2484. if oper[1]^.ref^.offset < 0 then
  2485. begin
  2486. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f0 shl 4);
  2487. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f);
  2488. end
  2489. else
  2490. begin
  2491. { set U bit }
  2492. bytes:=bytes or (1 shl 23);
  2493. bytes:=bytes or (oper[1]^.ref^.offset and $f0 shl 4);
  2494. bytes:=bytes or (oper[1]^.ref^.offset and $f);
  2495. end;
  2496. end
  2497. else
  2498. begin
  2499. { set U flag }
  2500. bytes:=bytes or (1 shl 23);
  2501. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2502. end;
  2503. { set W bit }
  2504. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2505. bytes:=bytes or (1 shl 21);
  2506. { set P bit if necessary }
  2507. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2508. bytes:=bytes or (1 shl 24);
  2509. end;
  2510. #$25: // PLD/PLI
  2511. begin
  2512. { set instruction code }
  2513. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2514. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2515. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2516. bytes:=bytes or ord(insentry^.code[4]);
  2517. { set Rn and Rd }
  2518. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  2519. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  2520. begin
  2521. { set offset }
  2522. offset:=0;
  2523. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2524. if assigned(currsym) then
  2525. offset:=currsym.offset-insoffset-8;
  2526. offset:=offset+oper[0]^.ref^.offset;
  2527. if offset>=0 then
  2528. begin
  2529. { set U flag }
  2530. bytes:=bytes or (1 shl 23);
  2531. bytes:=bytes or offset
  2532. end
  2533. else
  2534. begin
  2535. offset:=-offset;
  2536. bytes:=bytes or offset
  2537. end;
  2538. end
  2539. else
  2540. begin
  2541. bytes:=bytes or (1 shl 25);
  2542. { set U flag }
  2543. if oper[0]^.ref^.signindex>=0 then
  2544. bytes:=bytes or (1 shl 23);
  2545. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  2546. { set shift }
  2547. with oper[0]^.ref^ do
  2548. if shiftmode<>SM_None then
  2549. begin
  2550. bytes:=bytes or (shiftimm shl 7);
  2551. if shiftmode<>SM_RRX then
  2552. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2553. else
  2554. bytes:=bytes or (3 shl 5);
  2555. end
  2556. end;
  2557. end;
  2558. #$26: // LDM/STM
  2559. begin
  2560. { set instruction code }
  2561. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2562. if ops>1 then
  2563. begin
  2564. if oper[0]^.typ=top_ref then
  2565. begin
  2566. { set W bit }
  2567. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  2568. bytes:=bytes or (1 shl 21);
  2569. { set Rn }
  2570. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  2571. end
  2572. else { typ=top_reg }
  2573. begin
  2574. { set Rn }
  2575. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2576. end;
  2577. { reglist }
  2578. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  2579. end
  2580. else
  2581. begin
  2582. { push/pop }
  2583. { Set W and Rn to SP }
  2584. if opcode=A_PUSH then
  2585. bytes:=bytes or (1 shl 21);
  2586. bytes:=bytes or ($D shl 16);
  2587. { reglist }
  2588. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  2589. end;
  2590. { set P bit }
  2591. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  2592. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  2593. or (opcode=A_PUSH) then
  2594. bytes:=bytes or (1 shl 24);
  2595. { set U bit }
  2596. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  2597. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  2598. or (opcode=A_POP) then
  2599. bytes:=bytes or (1 shl 23);
  2600. end;
  2601. #$27: // SWP/SWPB
  2602. begin
  2603. { set instruction code }
  2604. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2605. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  2606. { set regs }
  2607. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2608. bytes:=bytes or getsupreg(oper[1]^.reg);
  2609. if ops=3 then
  2610. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2611. end;
  2612. #$28: // BX/BLX
  2613. begin
  2614. { set instruction code }
  2615. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2616. { set offset }
  2617. if oper[0]^.typ=top_const then
  2618. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2619. else
  2620. begin
  2621. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2622. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2623. begin
  2624. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2625. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  2626. end
  2627. else
  2628. begin
  2629. offset:=(((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2630. bytes:=bytes or ((offset shr 2) and $ffffff);
  2631. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  2632. end;
  2633. end;
  2634. end;
  2635. #$29: // SUB
  2636. begin
  2637. { set instruction code }
  2638. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2639. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2640. { set regs }
  2641. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2642. { set S if necessary }
  2643. if oppostfix=PF_S then
  2644. bytes:=bytes or (1 shl 20);
  2645. end;
  2646. #$2A:
  2647. begin
  2648. { set instruction code }
  2649. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2650. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2651. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2652. bytes:=bytes or ord(insentry^.code[4]);
  2653. { set opers }
  2654. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2655. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  2656. bytes:=bytes or getsupreg(oper[2]^.reg);
  2657. if (ops>3) and
  2658. (oper[3]^.typ=top_shifterop) and
  2659. (oper[3]^.shifterop^.rs=NR_NO) then
  2660. begin
  2661. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2662. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  2663. bytes:=bytes or (1 shl 6)
  2664. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  2665. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2666. end;
  2667. end;
  2668. #$2B: // SETEND
  2669. begin
  2670. { set instruction code }
  2671. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2672. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2673. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2674. bytes:=bytes or ord(insentry^.code[4]);
  2675. { set endian specifier }
  2676. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  2677. end;
  2678. #$2C: // MOVW
  2679. begin
  2680. { set instruction code }
  2681. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2682. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2683. { set destination }
  2684. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2685. { set imm }
  2686. bytes:=bytes or (oper[1]^.val and $FFF);
  2687. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  2688. end;
  2689. #$2D: // BFX
  2690. begin
  2691. { set instruction code }
  2692. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2693. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2694. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2695. bytes:=bytes or ord(insentry^.code[4]);
  2696. if ops=3 then
  2697. begin
  2698. msb:=(oper[1]^.val+oper[2]^.val-1);
  2699. { set destination }
  2700. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2701. { set immediates }
  2702. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  2703. bytes:=bytes or ((msb and $1F) shl 16);
  2704. end
  2705. else
  2706. begin
  2707. if opcode in [A_BFC,A_BFI] then
  2708. msb:=(oper[2]^.val+oper[3]^.val-1)
  2709. else
  2710. msb:=oper[3]^.val-1;
  2711. { set destination }
  2712. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2713. bytes:=bytes or getsupreg(oper[1]^.reg);
  2714. { set immediates }
  2715. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  2716. bytes:=bytes or ((msb and $1F) shl 16);
  2717. end;
  2718. end;
  2719. #$2E: // Cache stuff
  2720. begin
  2721. { set instruction code }
  2722. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2723. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2724. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2725. bytes:=bytes or ord(insentry^.code[4]);
  2726. { set code }
  2727. bytes:=bytes or (oper[0]^.val and $F);
  2728. end;
  2729. #$2F: // Nop
  2730. begin
  2731. { set instruction code }
  2732. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2733. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2734. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2735. bytes:=bytes or ord(insentry^.code[4]);
  2736. end;
  2737. #$30: // Shifts
  2738. begin
  2739. { set instruction code }
  2740. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2741. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2742. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2743. bytes:=bytes or ord(insentry^.code[4]);
  2744. { set destination }
  2745. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2746. bytes:=bytes or getsupreg(oper[1]^.reg);
  2747. if ops>2 then
  2748. begin
  2749. { set shift }
  2750. if oper[2]^.typ=top_reg then
  2751. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  2752. else
  2753. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  2754. end;
  2755. { set S if necessary }
  2756. if oppostfix=PF_S then
  2757. bytes:=bytes or (1 shl 20);
  2758. end;
  2759. #$31: // BKPT
  2760. begin
  2761. { set instruction code }
  2762. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2763. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2764. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  2765. { set imm }
  2766. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  2767. bytes:=bytes or (oper[0]^.val and $F);
  2768. end;
  2769. #$32: // CLZ/REV
  2770. begin
  2771. { set instruction code }
  2772. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2773. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2774. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2775. bytes:=bytes or ord(insentry^.code[4]);
  2776. { set regs }
  2777. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2778. bytes:=bytes or getsupreg(oper[1]^.reg);
  2779. end;
  2780. #$33:
  2781. begin
  2782. { set instruction code }
  2783. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2784. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2785. { set regs }
  2786. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2787. if oper[1]^.typ=top_ref then
  2788. begin
  2789. { set offset }
  2790. offset:=0;
  2791. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2792. if assigned(currsym) then
  2793. offset:=currsym.offset-insoffset-8;
  2794. offset:=offset+oper[1]^.ref^.offset;
  2795. if offset>=0 then
  2796. begin
  2797. { set U flag }
  2798. bytes:=bytes or (1 shl 23);
  2799. bytes:=bytes or offset
  2800. end
  2801. else
  2802. begin
  2803. bytes:=bytes or (1 shl 22);
  2804. offset:=-offset;
  2805. bytes:=bytes or offset
  2806. end;
  2807. end
  2808. else
  2809. begin
  2810. if is_shifter_const(oper[1]^.val,r) then
  2811. begin
  2812. setshifterop(1);
  2813. bytes:=bytes or (1 shl 23);
  2814. end
  2815. else
  2816. begin
  2817. bytes:=bytes or (1 shl 22);
  2818. oper[1]^.val:=-oper[1]^.val;
  2819. setshifterop(1);
  2820. end;
  2821. end;
  2822. end;
  2823. #$40: // VMOV
  2824. begin
  2825. { set instruction code }
  2826. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2827. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2828. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2829. bytes:=bytes or ord(insentry^.code[4]);
  2830. { set regs }
  2831. Rd:=0;
  2832. Rn:=0;
  2833. Rm:=0;
  2834. case oppostfix of
  2835. PF_None:
  2836. begin
  2837. if ops=4 then
  2838. begin
  2839. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  2840. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  2841. begin
  2842. Rd:=getmmreg(oper[0]^.reg);
  2843. Rm:=getsupreg(oper[2]^.reg);
  2844. Rn:=getsupreg(oper[3]^.reg);
  2845. end
  2846. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  2847. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  2848. begin
  2849. Rm:=getsupreg(oper[0]^.reg);
  2850. Rn:=getsupreg(oper[1]^.reg);
  2851. Rd:=getmmreg(oper[2]^.reg);
  2852. end
  2853. else
  2854. message(asmw_e_invalid_opcode_and_operands);
  2855. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  2856. bytes:=bytes or ((Rd and $1) shl 5);
  2857. bytes:=bytes or (Rm shl 12);
  2858. bytes:=bytes or (Rn shl 16);
  2859. end
  2860. else if ops=3 then
  2861. begin
  2862. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  2863. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  2864. begin
  2865. Rd:=getmmreg(oper[0]^.reg);
  2866. Rm:=getsupreg(oper[1]^.reg);
  2867. Rn:=getsupreg(oper[2]^.reg);
  2868. end
  2869. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  2870. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  2871. begin
  2872. Rm:=getsupreg(oper[0]^.reg);
  2873. Rn:=getsupreg(oper[1]^.reg);
  2874. Rd:=getmmreg(oper[2]^.reg);
  2875. end
  2876. else
  2877. message(asmw_e_invalid_opcode_and_operands);
  2878. bytes:=bytes or ((Rd and $F) shl 0);
  2879. bytes:=bytes or ((Rd and $10) shl 1);
  2880. bytes:=bytes or (Rm shl 12);
  2881. bytes:=bytes or (Rn shl 16);
  2882. end
  2883. else if ops=2 then
  2884. begin
  2885. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  2886. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  2887. begin
  2888. Rd:=getmmreg(oper[0]^.reg);
  2889. Rm:=getsupreg(oper[1]^.reg);
  2890. end
  2891. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  2892. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  2893. begin
  2894. Rm:=getsupreg(oper[0]^.reg);
  2895. Rd:=getmmreg(oper[1]^.reg);
  2896. end
  2897. else
  2898. message(asmw_e_invalid_opcode_and_operands);
  2899. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  2900. bytes:=bytes or ((Rd and $1) shl 7);
  2901. bytes:=bytes or (Rm shl 12);
  2902. end;
  2903. end;
  2904. PF_F32:
  2905. begin
  2906. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  2907. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  2908. Message(asmw_e_invalid_opcode_and_operands);
  2909. Rd:=getmmreg(oper[0]^.reg);
  2910. Rm:=getmmreg(oper[1]^.reg);
  2911. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  2912. bytes:=bytes or ((Rd and $1) shl 22);
  2913. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  2914. bytes:=bytes or ((Rm and $1) shl 5);
  2915. end;
  2916. PF_F64:
  2917. begin
  2918. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  2919. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  2920. Message(asmw_e_invalid_opcode_and_operands);
  2921. Rd:=getmmreg(oper[0]^.reg);
  2922. Rm:=getmmreg(oper[1]^.reg);
  2923. bytes:=bytes or ((Rd and $F) shl 12);
  2924. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  2925. bytes:=bytes or (Rm and $F);
  2926. bytes:=bytes or ((Rm and $10) shl 1);
  2927. end;
  2928. end;
  2929. end;
  2930. #$41: // VMRS/VMSR
  2931. begin
  2932. { set instruction code }
  2933. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2934. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2935. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2936. bytes:=bytes or ord(insentry^.code[4]);
  2937. { set regs }
  2938. if opcode=A_VMRS then
  2939. begin
  2940. case oper[1]^.reg of
  2941. NR_FPSID: Rn:=$0;
  2942. NR_FPSCR: Rn:=$1;
  2943. NR_MVFR1: Rn:=$6;
  2944. NR_MVFR0: Rn:=$7;
  2945. NR_FPEXC: Rn:=$8;
  2946. else
  2947. Rn:=0;
  2948. message(asmw_e_invalid_opcode_and_operands);
  2949. end;
  2950. bytes:=bytes or (Rn shl 16);
  2951. if oper[0]^.reg=NR_APSR_nzcv then
  2952. bytes:=bytes or ($F shl 12)
  2953. else
  2954. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2955. end
  2956. else
  2957. begin
  2958. case oper[0]^.reg of
  2959. NR_FPSID: Rn:=$0;
  2960. NR_FPSCR: Rn:=$1;
  2961. NR_FPEXC: Rn:=$8;
  2962. else
  2963. Rn:=0;
  2964. message(asmw_e_invalid_opcode_and_operands);
  2965. end;
  2966. bytes:=bytes or (Rn shl 16);
  2967. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  2968. end;
  2969. end;
  2970. #$42: // VMUL
  2971. begin
  2972. { set instruction code }
  2973. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2974. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2975. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2976. bytes:=bytes or ord(insentry^.code[4]);
  2977. { set regs }
  2978. if ops=3 then
  2979. begin
  2980. Rd:=getmmreg(oper[0]^.reg);
  2981. Rn:=getmmreg(oper[1]^.reg);
  2982. Rm:=getmmreg(oper[2]^.reg);
  2983. end
  2984. else if oper[1]^.typ=top_const then
  2985. begin
  2986. Rd:=getmmreg(oper[0]^.reg);
  2987. Rn:=0;
  2988. Rm:=0;
  2989. end
  2990. else
  2991. begin
  2992. Rd:=getmmreg(oper[0]^.reg);
  2993. Rn:=0;
  2994. Rm:=getmmreg(oper[1]^.reg);
  2995. end;
  2996. if oppostfix=PF_F32 then
  2997. begin
  2998. D:=rd and $1; Rd:=Rd shr 1;
  2999. N:=rn and $1; Rn:=Rn shr 1;
  3000. M:=rm and $1; Rm:=Rm shr 1;
  3001. end
  3002. else
  3003. begin
  3004. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3005. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3006. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3007. bytes:=bytes or (1 shl 8);
  3008. end;
  3009. bytes:=bytes or (Rd shl 12);
  3010. bytes:=bytes or (Rn shl 16);
  3011. bytes:=bytes or (Rm shl 0);
  3012. bytes:=bytes or (D shl 22);
  3013. bytes:=bytes or (N shl 7);
  3014. bytes:=bytes or (M shl 5);
  3015. end;
  3016. #$43: // VCVT
  3017. begin
  3018. { set instruction code }
  3019. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3020. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3021. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3022. bytes:=bytes or ord(insentry^.code[4]);
  3023. { set regs }
  3024. Rd:=getmmreg(oper[0]^.reg);
  3025. Rm:=getmmreg(oper[1]^.reg);
  3026. if (ops=2) and
  3027. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3028. begin
  3029. if oppostfix=PF_F32F64 then
  3030. begin
  3031. bytes:=bytes or (1 shl 8);
  3032. D:=rd and $1; Rd:=Rd shr 1;
  3033. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3034. end
  3035. else
  3036. begin
  3037. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3038. M:=rm and $1; Rm:=Rm shr 1;
  3039. end;
  3040. bytes:=bytes and $FFF0FFFF;
  3041. bytes:=bytes or ($7 shl 16);
  3042. bytes:=bytes or (Rd shl 12);
  3043. bytes:=bytes or (Rm shl 0);
  3044. bytes:=bytes or (D shl 22);
  3045. bytes:=bytes or (M shl 5);
  3046. end
  3047. else if ops=2 then
  3048. begin
  3049. case oppostfix of
  3050. PF_S32F64,
  3051. PF_U32F64,
  3052. PF_F64S32,
  3053. PF_F64U32:
  3054. bytes:=bytes or (1 shl 8);
  3055. end;
  3056. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3057. begin
  3058. case oppostfix of
  3059. PF_S32F64,
  3060. PF_S32F32:
  3061. bytes:=bytes or (1 shl 16);
  3062. end;
  3063. bytes:=bytes or (1 shl 18);
  3064. D:=rd and $1; Rd:=Rd shr 1;
  3065. if oppostfix in [PF_S32F64,PF_U32F64] then
  3066. begin
  3067. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3068. end
  3069. else
  3070. begin
  3071. M:=rm and $1; Rm:=Rm shr 1;
  3072. end;
  3073. end
  3074. else
  3075. begin
  3076. case oppostfix of
  3077. PF_F64S32,
  3078. PF_F32S32:
  3079. bytes:=bytes or (1 shl 7);
  3080. else
  3081. bytes:=bytes and $FFFFFF7F;
  3082. end;
  3083. M:=rm and $1; Rm:=Rm shr 1;
  3084. if oppostfix in [PF_F64S32,PF_F64U32] then
  3085. begin
  3086. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3087. end
  3088. else
  3089. begin
  3090. D:=rd and $1; Rd:=Rd shr 1;
  3091. end
  3092. end;
  3093. bytes:=bytes or (Rd shl 12);
  3094. bytes:=bytes or (Rm shl 0);
  3095. bytes:=bytes or (D shl 22);
  3096. bytes:=bytes or (M shl 5);
  3097. end
  3098. else
  3099. begin
  3100. if rd<>rm then
  3101. message(asmw_e_invalid_opcode_and_operands);
  3102. case oppostfix of
  3103. PF_S32F32,PF_U32F32,
  3104. PF_F32S32,PF_F32U32,
  3105. PF_S32F64,PF_U32F64,
  3106. PF_F64S32,PF_F64U32:
  3107. begin
  3108. if not (oper[2]^.val in [1..32]) then
  3109. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3110. bytes:=bytes or (1 shl 7);
  3111. rn:=32;
  3112. end;
  3113. PF_S16F64,PF_U16F64,
  3114. PF_F64S16,PF_F64U16,
  3115. PF_S16F32,PF_U16F32,
  3116. PF_F32S16,PF_F32U16:
  3117. begin
  3118. if not (oper[2]^.val in [0..16]) then
  3119. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3120. rn:=16;
  3121. end;
  3122. else
  3123. Rn:=0;
  3124. message(asmw_e_invalid_opcode_and_operands);
  3125. end;
  3126. case oppostfix of
  3127. PF_S16F64,PF_U16F64,
  3128. PF_S32F64,PF_U32F64,
  3129. PF_F64S16,PF_F64U16,
  3130. PF_F64S32,PF_F64U32:
  3131. begin
  3132. bytes:=bytes or (1 shl 8);
  3133. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3134. end;
  3135. else
  3136. begin
  3137. D:=rd and $1; Rd:=Rd shr 1;
  3138. end;
  3139. end;
  3140. case oppostfix of
  3141. PF_U16F64,PF_U16F32,
  3142. PF_U32F32,PF_U32F64,
  3143. PF_F64U16,PF_F32U16,
  3144. PF_F32U32,PF_F64U32:
  3145. bytes:=bytes or (1 shl 16);
  3146. end;
  3147. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3148. bytes:=bytes or (1 shl 18);
  3149. bytes:=bytes or (Rd shl 12);
  3150. bytes:=bytes or (D shl 22);
  3151. rn:=rn-oper[2]^.val;
  3152. bytes:=bytes or ((rn and $1) shl 5);
  3153. bytes:=bytes or ((rn and $1E) shr 1);
  3154. end;
  3155. end;
  3156. #$44: // VLDM/VSTM/VPUSH/VPOP
  3157. begin
  3158. { set instruction code }
  3159. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3160. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3161. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3162. { set regs }
  3163. if ops=2 then
  3164. begin
  3165. if oper[0]^.typ=top_ref then
  3166. begin
  3167. Rn:=getsupreg(oper[0]^.ref^.base);
  3168. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3169. begin
  3170. { set W }
  3171. bytes:=bytes or (1 shl 21);
  3172. end
  3173. else if oppostfix = PF_DB then
  3174. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3175. end
  3176. else
  3177. begin
  3178. Rn:=getsupreg(oper[0]^.reg);
  3179. if oppostfix = PF_DB then
  3180. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3181. end;
  3182. bytes:=bytes or (Rn shl 16);
  3183. { Set PU bits }
  3184. case oppostfix of
  3185. PF_None,
  3186. PF_IA:
  3187. bytes:=bytes or (1 shl 23);
  3188. PF_DB:
  3189. bytes:=bytes or (2 shl 23);
  3190. end;
  3191. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3192. if oper[1]^.regset^=[] then
  3193. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3194. rd:=0;
  3195. for r:=0 to 31 do
  3196. if r in oper[1]^.regset^ then
  3197. begin
  3198. rd:=r;
  3199. break;
  3200. end;
  3201. rn:=32-rd;
  3202. for r:=rd+1 to 31 do
  3203. if not(r in oper[1]^.regset^) then
  3204. begin
  3205. rn:=r-rd;
  3206. break;
  3207. end;
  3208. if dp_operation then
  3209. begin
  3210. bytes:=bytes or (1 shl 8);
  3211. bytes:=bytes or (rn*2);
  3212. bytes:=bytes or ((rd and $F) shl 12);
  3213. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3214. end
  3215. else
  3216. begin
  3217. bytes:=bytes or rn;
  3218. bytes:=bytes or ((rd and $1) shl 22);
  3219. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3220. end;
  3221. end
  3222. else { VPUSH/VPOP }
  3223. begin
  3224. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3225. if oper[0]^.regset^=[] then
  3226. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3227. rd:=0;
  3228. for r:=0 to 31 do
  3229. if r in oper[0]^.regset^ then
  3230. begin
  3231. rd:=r;
  3232. break;
  3233. end;
  3234. rn:=32-rd;
  3235. for r:=rd+1 to 31 do
  3236. if not(r in oper[0]^.regset^) then
  3237. begin
  3238. rn:=r-rd;
  3239. break;
  3240. end;
  3241. if dp_operation then
  3242. begin
  3243. bytes:=bytes or (1 shl 8);
  3244. bytes:=bytes or (rn*2);
  3245. bytes:=bytes or ((rd and $F) shl 12);
  3246. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3247. end
  3248. else
  3249. begin
  3250. bytes:=bytes or rn;
  3251. bytes:=bytes or ((rd and $1) shl 22);
  3252. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3253. end;
  3254. end;
  3255. end;
  3256. #$45: // VLDR/VSTR
  3257. begin
  3258. { set instruction code }
  3259. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3260. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3261. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3262. { set regs }
  3263. rd:=getmmreg(oper[0]^.reg);
  3264. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3265. begin
  3266. bytes:=bytes or (1 shl 8);
  3267. bytes:=bytes or ((rd and $F) shl 12);
  3268. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3269. end
  3270. else
  3271. begin
  3272. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3273. bytes:=bytes or ((rd and $1) shl 22);
  3274. end;
  3275. { set ref }
  3276. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3277. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3278. begin
  3279. { set offset }
  3280. offset:=0;
  3281. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3282. if assigned(currsym) then
  3283. offset:=currsym.offset-insoffset-8;
  3284. offset:=offset+oper[1]^.ref^.offset;
  3285. offset:=offset div 4;
  3286. if offset>=0 then
  3287. begin
  3288. { set U flag }
  3289. bytes:=bytes or (1 shl 23);
  3290. bytes:=bytes or offset
  3291. end
  3292. else
  3293. begin
  3294. offset:=-offset;
  3295. bytes:=bytes or offset
  3296. end;
  3297. end
  3298. else
  3299. message(asmw_e_invalid_opcode_and_operands);
  3300. end;
  3301. #$fe: // No written data
  3302. begin
  3303. exit;
  3304. end;
  3305. #$ff:
  3306. internalerror(2005091101);
  3307. else
  3308. begin
  3309. writeln(ord(insentry^.code[0]), ' - ', opcode);
  3310. internalerror(2005091102);
  3311. end;
  3312. end;
  3313. { we're finished, write code }
  3314. objdata.writebytes(bytes,bytelen);
  3315. end;
  3316. constructor tai_thumb_func.create;
  3317. begin
  3318. inherited create;
  3319. typ:=ait_thumb_func;
  3320. end;
  3321. begin
  3322. cai_align:=tai_align;
  3323. end.