aasmcpu.pas 184 KB

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