aasmcpu.pas 149 KB

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