aasmcpu.pas 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645
  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. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24)
  2150. else
  2151. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2152. end;
  2153. end;
  2154. #$02:
  2155. begin
  2156. { set instruction code }
  2157. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2158. { set code }
  2159. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2160. end;
  2161. #$03:
  2162. begin // BLX/BX
  2163. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2164. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2165. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2166. bytes:=bytes or ord(insentry^.code[4]);
  2167. bytes:=bytes or getsupreg(oper[0]^.reg);
  2168. end;
  2169. #$04..#$07: // SUB
  2170. begin
  2171. { set instruction code }
  2172. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2173. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2174. { set destination }
  2175. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2176. { set Rn }
  2177. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2178. { create shifter op }
  2179. setshifterop(2);
  2180. { set I field }
  2181. bytes:=bytes or (i_field shl 25);
  2182. { set S if necessary }
  2183. if oppostfix=PF_S then
  2184. bytes:=bytes or (1 shl 20);
  2185. end;
  2186. #$08,#$0A,#$0B: // MOV
  2187. begin
  2188. { set instruction code }
  2189. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2190. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2191. { set destination }
  2192. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2193. { create shifter op }
  2194. setshifterop(1);
  2195. { set I field }
  2196. bytes:=bytes or (i_field shl 25);
  2197. { set S if necessary }
  2198. if oppostfix=PF_S then
  2199. bytes:=bytes or (1 shl 20);
  2200. end;
  2201. #$0C,#$0E,#$0F: // CMP
  2202. begin
  2203. { set instruction code }
  2204. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2205. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2206. { set destination }
  2207. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2208. { create shifter op }
  2209. setshifterop(1);
  2210. { set I field }
  2211. bytes:=bytes or (i_field shl 25);
  2212. { always set S bit }
  2213. bytes:=bytes or (1 shl 20);
  2214. end;
  2215. #$14: // MUL/MLA r1,r2,r3
  2216. begin
  2217. { set instruction code }
  2218. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2219. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2220. bytes:=bytes or ord(insentry^.code[3]);
  2221. { set regs }
  2222. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2223. bytes:=bytes or getsupreg(oper[1]^.reg);
  2224. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2225. end;
  2226. #$15: // MUL/MLA r1,r2,r3,r4
  2227. begin
  2228. { set instruction code }
  2229. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2230. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2231. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2232. { set regs }
  2233. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2234. bytes:=bytes or getsupreg(oper[1]^.reg);
  2235. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2236. if ops>3 then
  2237. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2238. else
  2239. bytes:=bytes or ($F shl 12);
  2240. if oppostfix in [PF_R,PF_X] then
  2241. bytes:=bytes or (1 shl 5);
  2242. end;
  2243. #$16: // MULL r1,r2,r3,r4
  2244. begin
  2245. { set instruction code }
  2246. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2247. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2248. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2249. { set regs }
  2250. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2251. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2252. bytes:=bytes or getsupreg(oper[2]^.reg);
  2253. if ops=4 then
  2254. begin
  2255. if oper[3]^.typ=top_shifterop then
  2256. begin
  2257. if opcode in [A_PKHBT,A_PKHTB] then
  2258. begin
  2259. if ((opcode=A_PKHTB) and
  2260. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2261. ((opcode=A_PKHBT) and
  2262. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2263. (oper[3]^.shifterop^.rs<>NR_NO) then
  2264. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2265. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2266. end
  2267. else
  2268. begin
  2269. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2270. (oper[3]^.shifterop^.rs<>NR_NO) or
  2271. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2272. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2273. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2274. end;
  2275. end
  2276. else
  2277. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2278. end;
  2279. if PF_S=oppostfix then
  2280. bytes:=bytes or (1 shl 20);
  2281. if PF_X=oppostfix then
  2282. bytes:=bytes or (1 shl 5);
  2283. end;
  2284. #$17: // LDR/STR
  2285. begin
  2286. { set instruction code }
  2287. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2288. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2289. { set Rn and Rd }
  2290. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2291. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2292. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2293. begin
  2294. { set offset }
  2295. offset:=0;
  2296. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2297. if assigned(currsym) then
  2298. offset:=currsym.offset-insoffset-8;
  2299. offset:=offset+oper[1]^.ref^.offset;
  2300. if offset>=0 then
  2301. begin
  2302. { set U flag }
  2303. bytes:=bytes or (1 shl 23);
  2304. bytes:=bytes or offset
  2305. end
  2306. else
  2307. begin
  2308. offset:=-offset;
  2309. bytes:=bytes or offset
  2310. end;
  2311. end
  2312. else
  2313. begin
  2314. { set U flag }
  2315. if oper[1]^.ref^.signindex>=0 then
  2316. bytes:=bytes or (1 shl 23);
  2317. { set I flag }
  2318. bytes:=bytes or (1 shl 25);
  2319. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2320. { set shift }
  2321. with oper[1]^.ref^ do
  2322. if shiftmode<>SM_None then
  2323. begin
  2324. bytes:=bytes or (shiftimm shl 7);
  2325. if shiftmode<>SM_RRX then
  2326. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2327. else
  2328. bytes:=bytes or (3 shl 5);
  2329. end
  2330. end;
  2331. { set W bit }
  2332. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2333. bytes:=bytes or (1 shl 21);
  2334. { set P bit if necessary }
  2335. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2336. bytes:=bytes or (1 shl 24);
  2337. end;
  2338. #$18: // LDREX/STREX
  2339. begin
  2340. { set instruction code }
  2341. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2342. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2343. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2344. bytes:=bytes or ord(insentry^.code[4]);
  2345. { set Rn and Rd }
  2346. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2347. if (ops=3) then
  2348. begin
  2349. if opcode<>A_LDREXD then
  2350. bytes:=bytes or getsupreg(oper[1]^.reg);
  2351. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2352. end
  2353. else if (ops=4) then // STREXD
  2354. begin
  2355. if opcode<>A_LDREXD then
  2356. bytes:=bytes or getsupreg(oper[1]^.reg);
  2357. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  2358. end
  2359. else
  2360. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  2361. end;
  2362. #$19: // LDRD/STRD
  2363. begin
  2364. { set instruction code }
  2365. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2366. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2367. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2368. bytes:=bytes or ord(insentry^.code[4]);
  2369. { set Rn and Rd }
  2370. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2371. refoper:=oper[1];
  2372. if ops=3 then
  2373. refoper:=oper[2];
  2374. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2375. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2376. begin
  2377. bytes:=bytes or (1 shl 22);
  2378. { set offset }
  2379. offset:=0;
  2380. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2381. if assigned(currsym) then
  2382. offset:=currsym.offset-insoffset-8;
  2383. offset:=offset+refoper^.ref^.offset;
  2384. if offset>=0 then
  2385. begin
  2386. { set U flag }
  2387. bytes:=bytes or (1 shl 23);
  2388. bytes:=bytes or (offset and $F);
  2389. bytes:=bytes or ((offset and $F0) shl 4);
  2390. end
  2391. else
  2392. begin
  2393. offset:=-offset;
  2394. bytes:=bytes or (offset and $F);
  2395. bytes:=bytes or ((offset and $F0) shl 4);
  2396. end;
  2397. end
  2398. else
  2399. begin
  2400. { set U flag }
  2401. if refoper^.ref^.signindex>=0 then
  2402. bytes:=bytes or (1 shl 23);
  2403. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2404. end;
  2405. { set W bit }
  2406. if refoper^.ref^.addressmode=AM_PREINDEXED then
  2407. bytes:=bytes or (1 shl 21);
  2408. { set P bit if necessary }
  2409. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  2410. bytes:=bytes or (1 shl 24);
  2411. end;
  2412. #$1A: // QADD/QSUB
  2413. begin
  2414. { set instruction code }
  2415. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2416. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2417. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2418. { set regs }
  2419. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2420. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  2421. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2422. end;
  2423. #$1B:
  2424. begin
  2425. { set instruction code }
  2426. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2427. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2428. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2429. { set regs }
  2430. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2431. bytes:=bytes or getsupreg(oper[1]^.reg);
  2432. if ops=3 then
  2433. begin
  2434. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  2435. (oper[2]^.shifterop^.rs<>NR_NO) or
  2436. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  2437. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2438. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2439. end;
  2440. end;
  2441. #$1C: // MCR/MRC
  2442. begin
  2443. { set instruction code }
  2444. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2445. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2446. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2447. { set regs and operands }
  2448. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2449. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  2450. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2451. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  2452. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2453. if ops > 5 then
  2454. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  2455. end;
  2456. #$1D: // MCRR/MRRC
  2457. begin
  2458. { set instruction code }
  2459. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2460. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2461. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2462. { set regs and operands }
  2463. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2464. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  2465. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2466. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  2467. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2468. end;
  2469. #$22: // LDRH/STRH
  2470. begin
  2471. { set instruction code }
  2472. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2473. bytes:=bytes or ord(insentry^.code[2]);
  2474. { src/dest register (Rd) }
  2475. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2476. { base register (Rn) }
  2477. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2478. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2479. begin
  2480. bytes:=bytes or (1 shl 22); // with immediate offset
  2481. if oper[1]^.ref^.offset < 0 then
  2482. begin
  2483. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f0 shl 4);
  2484. bytes:=bytes or ((-oper[1]^.ref^.offset) and $f);
  2485. end
  2486. else
  2487. begin
  2488. { set U bit }
  2489. bytes:=bytes or (1 shl 23);
  2490. bytes:=bytes or (oper[1]^.ref^.offset and $f0 shl 4);
  2491. bytes:=bytes or (oper[1]^.ref^.offset and $f);
  2492. end;
  2493. end
  2494. else
  2495. begin
  2496. { set U flag }
  2497. bytes:=bytes or (1 shl 23);
  2498. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2499. end;
  2500. { set P bit if necessary }
  2501. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2502. bytes:=bytes or (1 shl 24);
  2503. end;
  2504. #$25: // PLD/PLI
  2505. begin
  2506. { set instruction code }
  2507. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2508. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2509. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2510. bytes:=bytes or ord(insentry^.code[4]);
  2511. { set Rn and Rd }
  2512. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  2513. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  2514. begin
  2515. { set offset }
  2516. offset:=0;
  2517. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2518. if assigned(currsym) then
  2519. offset:=currsym.offset-insoffset-8;
  2520. offset:=offset+oper[0]^.ref^.offset;
  2521. if offset>=0 then
  2522. begin
  2523. { set U flag }
  2524. bytes:=bytes or (1 shl 23);
  2525. bytes:=bytes or offset
  2526. end
  2527. else
  2528. begin
  2529. offset:=-offset;
  2530. bytes:=bytes or offset
  2531. end;
  2532. end
  2533. else
  2534. begin
  2535. { set U flag }
  2536. if oper[0]^.ref^.signindex>=0 then
  2537. bytes:=bytes or (1 shl 23);
  2538. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  2539. { set shift }
  2540. with oper[0]^.ref^ do
  2541. if shiftmode<>SM_None then
  2542. begin
  2543. bytes:=bytes or (shiftimm shl 7);
  2544. if shiftmode<>SM_RRX then
  2545. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2546. else
  2547. bytes:=bytes or (3 shl 5);
  2548. end
  2549. end;
  2550. end;
  2551. #$26: // LDM/STM
  2552. begin
  2553. { set instruction code }
  2554. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2555. if ops>1 then
  2556. begin
  2557. if oper[0]^.typ=top_ref then
  2558. begin
  2559. { set W bit }
  2560. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  2561. bytes:=bytes or (1 shl 21);
  2562. { set Rn }
  2563. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  2564. end
  2565. else { typ=top_reg }
  2566. begin
  2567. { set Rn }
  2568. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2569. end;
  2570. { reglist }
  2571. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  2572. end
  2573. else
  2574. begin
  2575. { push/pop }
  2576. { Set W and Rn to SP }
  2577. if opcode=A_PUSH then
  2578. bytes:=bytes or (1 shl 21);
  2579. bytes:=bytes or ($D shl 16);
  2580. { reglist }
  2581. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  2582. end;
  2583. { set P bit }
  2584. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  2585. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  2586. or (opcode=A_PUSH) then
  2587. bytes:=bytes or (1 shl 24);
  2588. { set U bit }
  2589. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  2590. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  2591. or (opcode=A_POP) then
  2592. bytes:=bytes or (1 shl 23);
  2593. end;
  2594. #$27: // SWP/SWPB
  2595. begin
  2596. { set instruction code }
  2597. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  2598. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  2599. { set regs }
  2600. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2601. bytes:=bytes or getsupreg(oper[1]^.reg);
  2602. if ops=3 then
  2603. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2604. end;
  2605. #$28: // BX/BLX
  2606. begin
  2607. { set instruction code }
  2608. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2609. { set offset }
  2610. if oper[0]^.typ=top_const then
  2611. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2612. else
  2613. begin
  2614. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2615. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2616. begin
  2617. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2618. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  2619. end
  2620. else
  2621. begin
  2622. offset:=(((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2623. bytes:=bytes or ((offset shr 2) and $ffffff);
  2624. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  2625. end;
  2626. end;
  2627. end;
  2628. #$29: // SUB
  2629. begin
  2630. { set instruction code }
  2631. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2632. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2633. { set regs }
  2634. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2635. { set S if necessary }
  2636. if oppostfix=PF_S then
  2637. bytes:=bytes or (1 shl 20);
  2638. end;
  2639. #$2A:
  2640. begin
  2641. { set instruction code }
  2642. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2643. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2644. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2645. bytes:=bytes or ord(insentry^.code[4]);
  2646. { set opers }
  2647. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2648. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  2649. bytes:=bytes or getsupreg(oper[2]^.reg);
  2650. if (ops>3) and
  2651. (oper[3]^.typ=top_shifterop) and
  2652. (oper[3]^.shifterop^.rs=NR_NO) then
  2653. begin
  2654. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2655. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  2656. bytes:=bytes or (1 shl 6)
  2657. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  2658. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2659. end;
  2660. end;
  2661. #$2B: // SETEND
  2662. begin
  2663. { set instruction code }
  2664. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2665. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2666. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2667. bytes:=bytes or ord(insentry^.code[4]);
  2668. { set endian specifier }
  2669. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  2670. end;
  2671. #$2C: // MOVW
  2672. begin
  2673. { set instruction code }
  2674. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2675. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2676. { set destination }
  2677. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2678. { set imm }
  2679. bytes:=bytes or (oper[1]^.val and $FFF);
  2680. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  2681. end;
  2682. #$2D: // BFX
  2683. begin
  2684. { set instruction code }
  2685. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2686. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2687. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2688. bytes:=bytes or ord(insentry^.code[4]);
  2689. if ops=3 then
  2690. begin
  2691. msb:=(oper[1]^.val+oper[2]^.val-1);
  2692. { set destination }
  2693. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2694. { set immediates }
  2695. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  2696. bytes:=bytes or ((msb and $1F) shl 16);
  2697. end
  2698. else
  2699. begin
  2700. if opcode in [A_BFC,A_BFI] then
  2701. msb:=(oper[2]^.val+oper[3]^.val-1)
  2702. else
  2703. msb:=oper[3]^.val-1;
  2704. { set destination }
  2705. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2706. bytes:=bytes or getsupreg(oper[1]^.reg);
  2707. { set immediates }
  2708. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  2709. bytes:=bytes or ((msb and $1F) shl 16);
  2710. end;
  2711. end;
  2712. #$2E: // Cache stuff
  2713. begin
  2714. { set instruction code }
  2715. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2716. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2717. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2718. bytes:=bytes or ord(insentry^.code[4]);
  2719. { set code }
  2720. bytes:=bytes or (oper[0]^.val and $F);
  2721. end;
  2722. #$2F: // Nop
  2723. begin
  2724. { set instruction code }
  2725. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2726. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2727. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2728. bytes:=bytes or ord(insentry^.code[4]);
  2729. end;
  2730. #$30: // Shifts
  2731. begin
  2732. { set instruction code }
  2733. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2734. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2735. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2736. bytes:=bytes or ord(insentry^.code[4]);
  2737. { set destination }
  2738. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2739. bytes:=bytes or getsupreg(oper[1]^.reg);
  2740. if ops>2 then
  2741. begin
  2742. { set shift }
  2743. if oper[2]^.typ=top_reg then
  2744. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  2745. else
  2746. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  2747. end;
  2748. { set S if necessary }
  2749. if oppostfix=PF_S then
  2750. bytes:=bytes or (1 shl 20);
  2751. end;
  2752. #$31: // BKPT
  2753. begin
  2754. { set instruction code }
  2755. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2756. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2757. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  2758. { set imm }
  2759. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  2760. bytes:=bytes or (oper[0]^.val and $F);
  2761. end;
  2762. #$32: // CLZ/REV
  2763. begin
  2764. { set instruction code }
  2765. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2766. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2767. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2768. bytes:=bytes or ord(insentry^.code[4]);
  2769. { set regs }
  2770. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2771. bytes:=bytes or getsupreg(oper[1]^.reg);
  2772. end;
  2773. #$33:
  2774. begin
  2775. { set instruction code }
  2776. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2777. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2778. { set regs }
  2779. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2780. if oper[1]^.typ=top_ref then
  2781. begin
  2782. { set offset }
  2783. offset:=0;
  2784. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2785. if assigned(currsym) then
  2786. offset:=currsym.offset-insoffset-8;
  2787. offset:=offset+oper[1]^.ref^.offset;
  2788. if offset>=0 then
  2789. begin
  2790. { set U flag }
  2791. bytes:=bytes or (1 shl 23);
  2792. bytes:=bytes or offset
  2793. end
  2794. else
  2795. begin
  2796. bytes:=bytes or (1 shl 22);
  2797. offset:=-offset;
  2798. bytes:=bytes or offset
  2799. end;
  2800. end
  2801. else
  2802. begin
  2803. if is_shifter_const(oper[1]^.val,r) then
  2804. begin
  2805. setshifterop(1);
  2806. bytes:=bytes or (1 shl 23);
  2807. end
  2808. else
  2809. begin
  2810. bytes:=bytes or (1 shl 22);
  2811. oper[1]^.val:=-oper[1]^.val;
  2812. setshifterop(1);
  2813. end;
  2814. end;
  2815. end;
  2816. #$40: // VMOV
  2817. begin
  2818. { set instruction code }
  2819. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2820. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2821. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2822. bytes:=bytes or ord(insentry^.code[4]);
  2823. { set regs }
  2824. case oppostfix of
  2825. PF_None:
  2826. begin
  2827. if ops=4 then
  2828. begin
  2829. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  2830. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  2831. begin
  2832. Rd:=getmmreg(oper[0]^.reg);
  2833. Rm:=getsupreg(oper[2]^.reg);
  2834. Rn:=getsupreg(oper[3]^.reg);
  2835. end
  2836. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  2837. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  2838. begin
  2839. Rm:=getsupreg(oper[0]^.reg);
  2840. Rn:=getsupreg(oper[1]^.reg);
  2841. Rd:=getmmreg(oper[2]^.reg);
  2842. end
  2843. else
  2844. message(asmw_e_invalid_opcode_and_operands);
  2845. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  2846. bytes:=bytes or ((Rd and $1) shl 5);
  2847. bytes:=bytes or (Rm shl 12);
  2848. bytes:=bytes or (Rn shl 16);
  2849. end
  2850. else if ops=3 then
  2851. begin
  2852. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  2853. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  2854. begin
  2855. Rd:=getmmreg(oper[0]^.reg);
  2856. Rm:=getsupreg(oper[1]^.reg);
  2857. Rn:=getsupreg(oper[2]^.reg);
  2858. end
  2859. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  2860. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  2861. begin
  2862. Rm:=getsupreg(oper[0]^.reg);
  2863. Rn:=getsupreg(oper[1]^.reg);
  2864. Rd:=getmmreg(oper[2]^.reg);
  2865. end
  2866. else
  2867. message(asmw_e_invalid_opcode_and_operands);
  2868. bytes:=bytes or ((Rd and $F) shl 0);
  2869. bytes:=bytes or ((Rd and $10) shl 1);
  2870. bytes:=bytes or (Rm shl 12);
  2871. bytes:=bytes or (Rn shl 16);
  2872. end
  2873. else if ops=2 then
  2874. begin
  2875. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  2876. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  2877. begin
  2878. Rd:=getmmreg(oper[0]^.reg);
  2879. Rm:=getsupreg(oper[1]^.reg);
  2880. end
  2881. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  2882. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  2883. begin
  2884. Rm:=getsupreg(oper[0]^.reg);
  2885. Rd:=getmmreg(oper[1]^.reg);
  2886. end
  2887. else
  2888. message(asmw_e_invalid_opcode_and_operands);
  2889. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  2890. bytes:=bytes or ((Rd and $1) shl 7);
  2891. bytes:=bytes or (Rm shl 12);
  2892. end;
  2893. end;
  2894. PF_F32:
  2895. begin
  2896. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  2897. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  2898. Message(asmw_e_invalid_opcode_and_operands);
  2899. Rd:=getmmreg(oper[0]^.reg);
  2900. Rm:=getmmreg(oper[1]^.reg);
  2901. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  2902. bytes:=bytes or ((Rd and $1) shl 22);
  2903. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  2904. bytes:=bytes or ((Rm and $1) shl 5);
  2905. end;
  2906. PF_F64:
  2907. begin
  2908. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  2909. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  2910. Message(asmw_e_invalid_opcode_and_operands);
  2911. Rd:=getmmreg(oper[0]^.reg);
  2912. Rm:=getmmreg(oper[1]^.reg);
  2913. bytes:=bytes or ((Rd and $F) shl 12);
  2914. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  2915. bytes:=bytes or (Rm and $F);
  2916. bytes:=bytes or ((Rm and $10) shl 1);
  2917. end;
  2918. end;
  2919. end;
  2920. #$41: // VMRS/VMSR
  2921. begin
  2922. { set instruction code }
  2923. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2924. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2925. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2926. bytes:=bytes or ord(insentry^.code[4]);
  2927. { set regs }
  2928. if opcode=A_VMRS then
  2929. begin
  2930. case oper[1]^.reg of
  2931. NR_FPSID: Rn:=$0;
  2932. NR_FPSCR: Rn:=$1;
  2933. NR_MVFR1: Rn:=$6;
  2934. NR_MVFR0: Rn:=$7;
  2935. NR_FPEXC: Rn:=$8;
  2936. else
  2937. message(asmw_e_invalid_opcode_and_operands);
  2938. end;
  2939. bytes:=bytes or (Rn shl 16);
  2940. if oper[0]^.reg=NR_APSR_nzcv then
  2941. bytes:=bytes or ($F shl 12)
  2942. else
  2943. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2944. end
  2945. else
  2946. begin
  2947. case oper[0]^.reg of
  2948. NR_FPSID: Rn:=$0;
  2949. NR_FPSCR: Rn:=$1;
  2950. NR_FPEXC: Rn:=$8;
  2951. else
  2952. message(asmw_e_invalid_opcode_and_operands);
  2953. end;
  2954. bytes:=bytes or (Rn shl 16);
  2955. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  2956. end;
  2957. end;
  2958. #$42: // VMUL
  2959. begin
  2960. { set instruction code }
  2961. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2962. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2963. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2964. bytes:=bytes or ord(insentry^.code[4]);
  2965. { set regs }
  2966. if ops=3 then
  2967. begin
  2968. Rd:=getmmreg(oper[0]^.reg);
  2969. Rn:=getmmreg(oper[1]^.reg);
  2970. Rm:=getmmreg(oper[2]^.reg);
  2971. end
  2972. else if oper[1]^.typ=top_const then
  2973. begin
  2974. Rd:=getmmreg(oper[0]^.reg);
  2975. Rn:=0;
  2976. Rm:=0;
  2977. end
  2978. else
  2979. begin
  2980. Rd:=getmmreg(oper[0]^.reg);
  2981. Rn:=0;
  2982. Rm:=getmmreg(oper[1]^.reg);
  2983. end;
  2984. if oppostfix=PF_F32 then
  2985. begin
  2986. D:=rd and $1; Rd:=Rd shr 1;
  2987. N:=rn and $1; Rn:=Rn shr 1;
  2988. M:=rm and $1; Rm:=Rm shr 1;
  2989. end
  2990. else
  2991. begin
  2992. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  2993. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  2994. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  2995. bytes:=bytes or (1 shl 8);
  2996. end;
  2997. bytes:=bytes or (Rd shl 12);
  2998. bytes:=bytes or (Rn shl 16);
  2999. bytes:=bytes or (Rm shl 0);
  3000. bytes:=bytes or (D shl 22);
  3001. bytes:=bytes or (N shl 7);
  3002. bytes:=bytes or (M shl 5);
  3003. end;
  3004. #$43: // VCVT
  3005. begin
  3006. { set instruction code }
  3007. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3008. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3009. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3010. bytes:=bytes or ord(insentry^.code[4]);
  3011. { set regs }
  3012. Rd:=getmmreg(oper[0]^.reg);
  3013. Rm:=getmmreg(oper[1]^.reg);
  3014. if (ops=2) and
  3015. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3016. begin
  3017. if oppostfix=PF_F32F64 then
  3018. begin
  3019. bytes:=bytes or (1 shl 8);
  3020. D:=rd and $1; Rd:=Rd shr 1;
  3021. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3022. end
  3023. else
  3024. begin
  3025. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3026. M:=rm and $1; Rm:=Rm shr 1;
  3027. end;
  3028. bytes:=bytes and $FFF0FFFF;
  3029. bytes:=bytes or ($7 shl 16);
  3030. bytes:=bytes or (Rd shl 12);
  3031. bytes:=bytes or (Rm shl 0);
  3032. bytes:=bytes or (D shl 22);
  3033. bytes:=bytes or (M shl 5);
  3034. end
  3035. else if ops=2 then
  3036. begin
  3037. case oppostfix of
  3038. PF_S32F64,
  3039. PF_U32F64,
  3040. PF_F64S32,
  3041. PF_F64U32:
  3042. bytes:=bytes or (1 shl 8);
  3043. end;
  3044. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3045. begin
  3046. case oppostfix of
  3047. PF_S32F64,
  3048. PF_S32F32:
  3049. bytes:=bytes or (1 shl 16);
  3050. end;
  3051. bytes:=bytes or (1 shl 18);
  3052. D:=rd and $1; Rd:=Rd shr 1;
  3053. if oppostfix in [PF_S32F64,PF_U32F64] then
  3054. begin
  3055. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3056. end
  3057. else
  3058. begin
  3059. M:=rm and $1; Rm:=Rm shr 1;
  3060. end;
  3061. end
  3062. else
  3063. begin
  3064. case oppostfix of
  3065. PF_F64S32,
  3066. PF_F32S32:
  3067. bytes:=bytes or (1 shl 7);
  3068. else
  3069. bytes:=bytes and $FFFFFF7F;
  3070. end;
  3071. M:=rm and $1; Rm:=Rm shr 1;
  3072. if oppostfix in [PF_F64S32,PF_F64U32] then
  3073. begin
  3074. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3075. end
  3076. else
  3077. begin
  3078. D:=rd and $1; Rd:=Rd shr 1;
  3079. end
  3080. end;
  3081. bytes:=bytes or (Rd shl 12);
  3082. bytes:=bytes or (Rm shl 0);
  3083. bytes:=bytes or (D shl 22);
  3084. bytes:=bytes or (M shl 5);
  3085. end
  3086. else
  3087. begin
  3088. if rd<>rm then
  3089. message(asmw_e_invalid_opcode_and_operands);
  3090. case oppostfix of
  3091. PF_S32F32,PF_U32F32,
  3092. PF_F32S32,PF_F32U32,
  3093. PF_S32F64,PF_U32F64,
  3094. PF_F64S32,PF_F64U32:
  3095. begin
  3096. if not (oper[2]^.val in [1..32]) then
  3097. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3098. bytes:=bytes or (1 shl 7);
  3099. rn:=32;
  3100. end;
  3101. PF_S16F64,PF_U16F64,
  3102. PF_F64S16,PF_F64U16,
  3103. PF_S16F32,PF_U16F32,
  3104. PF_F32S16,PF_F32U16:
  3105. begin
  3106. if not (oper[2]^.val in [0..16]) then
  3107. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3108. rn:=16;
  3109. end;
  3110. else
  3111. message(asmw_e_invalid_opcode_and_operands);
  3112. end;
  3113. case oppostfix of
  3114. PF_S16F64,PF_U16F64,
  3115. PF_S32F64,PF_U32F64,
  3116. PF_F64S16,PF_F64U16,
  3117. PF_F64S32,PF_F64U32:
  3118. begin
  3119. bytes:=bytes or (1 shl 8);
  3120. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3121. end;
  3122. else
  3123. begin
  3124. D:=rd and $1; Rd:=Rd shr 1;
  3125. end;
  3126. end;
  3127. case oppostfix of
  3128. PF_U16F64,PF_U16F32,
  3129. PF_U32F32,PF_U32F64,
  3130. PF_F64U16,PF_F32U16,
  3131. PF_F32U32,PF_F64U32:
  3132. bytes:=bytes or (1 shl 16);
  3133. end;
  3134. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3135. bytes:=bytes or (1 shl 18);
  3136. bytes:=bytes or (Rd shl 12);
  3137. bytes:=bytes or (D shl 22);
  3138. rn:=rn-oper[2]^.val;
  3139. bytes:=bytes or ((rn and $1) shl 5);
  3140. bytes:=bytes or ((rn and $1E) shr 1);
  3141. end;
  3142. end;
  3143. #$44: // VLDM/VSTM/VPUSH/VPOP
  3144. begin
  3145. { set instruction code }
  3146. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3147. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3148. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3149. { set regs }
  3150. if ops=2 then
  3151. begin
  3152. if oper[0]^.typ=top_ref then
  3153. begin
  3154. Rn:=getsupreg(oper[0]^.ref^.base);
  3155. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3156. begin
  3157. { set W }
  3158. bytes:=bytes or (1 shl 21);
  3159. end
  3160. else if oppostfix = PF_DB then
  3161. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3162. end
  3163. else
  3164. begin
  3165. Rn:=getsupreg(oper[0]^.reg);
  3166. if oppostfix = PF_DB then
  3167. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3168. end;
  3169. bytes:=bytes or (Rn shl 16);
  3170. { Set PU bits }
  3171. case oppostfix of
  3172. PF_None,
  3173. PF_IA:
  3174. bytes:=bytes or (1 shl 23);
  3175. PF_DB:
  3176. bytes:=bytes or (2 shl 23);
  3177. end;
  3178. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3179. if oper[1]^.regset^=[] then
  3180. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3181. for r:=0 to 31 do
  3182. if r in oper[1]^.regset^ then
  3183. begin
  3184. rd:=r;
  3185. break;
  3186. end;
  3187. rn:=32-rd;
  3188. for r:=rd+1 to 31 do
  3189. if not(r in oper[1]^.regset^) then
  3190. begin
  3191. rn:=r-rd;
  3192. break;
  3193. end;
  3194. if dp_operation then
  3195. begin
  3196. bytes:=bytes or (1 shl 8);
  3197. bytes:=bytes or (rn*2);
  3198. bytes:=bytes or ((rd and $F) shl 12);
  3199. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3200. end
  3201. else
  3202. begin
  3203. bytes:=bytes or rn;
  3204. bytes:=bytes or ((rd and $1) shl 22);
  3205. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3206. end;
  3207. end
  3208. else { VPUSH/VPOP }
  3209. begin
  3210. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3211. if oper[0]^.regset^=[] then
  3212. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3213. for r:=0 to 31 do
  3214. if r in oper[0]^.regset^ then
  3215. begin
  3216. rd:=r;
  3217. break;
  3218. end;
  3219. rn:=32-rd;
  3220. for r:=rd+1 to 31 do
  3221. if not(r in oper[0]^.regset^) then
  3222. begin
  3223. rn:=r-rd;
  3224. break;
  3225. end;
  3226. if dp_operation then
  3227. begin
  3228. bytes:=bytes or (1 shl 8);
  3229. bytes:=bytes or (rn*2);
  3230. bytes:=bytes or ((rd and $F) shl 12);
  3231. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3232. end
  3233. else
  3234. begin
  3235. bytes:=bytes or rn;
  3236. bytes:=bytes or ((rd and $1) shl 22);
  3237. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3238. end;
  3239. end;
  3240. end;
  3241. #$45: // VLDR/VSTR
  3242. begin
  3243. { set instruction code }
  3244. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3245. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3246. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3247. { set regs }
  3248. rd:=getmmreg(oper[0]^.reg);
  3249. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3250. begin
  3251. bytes:=bytes or (1 shl 8);
  3252. bytes:=bytes or ((rd and $F) shl 12);
  3253. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3254. end
  3255. else
  3256. begin
  3257. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3258. bytes:=bytes or ((rd and $1) shl 22);
  3259. end;
  3260. { set ref }
  3261. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3262. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3263. begin
  3264. { set offset }
  3265. offset:=0;
  3266. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3267. if assigned(currsym) then
  3268. offset:=currsym.offset-insoffset-8;
  3269. offset:=offset+oper[1]^.ref^.offset;
  3270. offset:=offset div 4;
  3271. if offset>=0 then
  3272. begin
  3273. { set U flag }
  3274. bytes:=bytes or (1 shl 23);
  3275. bytes:=bytes or offset
  3276. end
  3277. else
  3278. begin
  3279. offset:=-offset;
  3280. bytes:=bytes or offset
  3281. end;
  3282. end
  3283. else
  3284. message(asmw_e_invalid_opcode_and_operands);
  3285. end;
  3286. #$fe: // No written data
  3287. begin
  3288. exit;
  3289. end;
  3290. #$ff:
  3291. internalerror(2005091101);
  3292. else
  3293. begin
  3294. writeln(ord(insentry^.code[0]), ' - ', opcode);
  3295. internalerror(2005091102);
  3296. end;
  3297. end;
  3298. { we're finished, write code }
  3299. objdata.writebytes(bytes,bytelen);
  3300. end;
  3301. constructor tai_thumb_func.create;
  3302. begin
  3303. inherited create;
  3304. typ:=ait_thumb_func;
  3305. end;
  3306. begin
  3307. cai_align:=tai_align;
  3308. end.