aasmcpu.pas 184 KB

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