aasmcpu.pas 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234
  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. A_SWI:
  1373. begin
  1374. taicpu(curtai).opcode:=A_SVC;
  1375. end;
  1376. end;
  1377. end;
  1378. end;
  1379. curtai:=tai(curtai.Next);
  1380. end;
  1381. end;
  1382. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1383. begin
  1384. expand_instructions(list);
  1385. { Do Thumb-2 16bit -> 32bit transformations }
  1386. if GenerateThumb2Code then
  1387. begin
  1388. ensurethumbencodings(list);
  1389. ensurethumb2encodings(list);
  1390. foldITInstructions(list);
  1391. end
  1392. else if GenerateThumbCode then
  1393. ensurethumbencodings(list);
  1394. gather_it_info(list);
  1395. fix_invalid_imms(list);
  1396. insertpcrelativedata(list, listtoinsert);
  1397. end;
  1398. procedure InsertPData;
  1399. var
  1400. prolog: TAsmList;
  1401. begin
  1402. prolog:=TAsmList.create;
  1403. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1404. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1405. prolog.concat(Tai_const.Create_32bit(0));
  1406. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1407. { dummy function }
  1408. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1409. current_asmdata.asmlists[al_start].insertList(prolog);
  1410. prolog.Free;
  1411. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1412. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1413. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1414. end;
  1415. (*
  1416. Floating point instruction format information, taken from the linux kernel
  1417. ARM Floating Point Instruction Classes
  1418. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1419. |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
  1420. |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)
  1421. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1422. |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
  1423. |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
  1424. |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
  1425. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1426. CPDT data transfer instructions
  1427. LDF, STF, LFM (copro 2), SFM (copro 2)
  1428. CPDO dyadic arithmetic instructions
  1429. ADF, MUF, SUF, RSF, DVF, RDF,
  1430. POW, RPW, RMF, FML, FDV, FRD, POL
  1431. CPDO monadic arithmetic instructions
  1432. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1433. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1434. CPRT joint arithmetic/data transfer instructions
  1435. FIX (arithmetic followed by load/store)
  1436. FLT (load/store followed by arithmetic)
  1437. CMF, CNF CMFE, CNFE (comparisons)
  1438. WFS, RFS (write/read floating point status register)
  1439. WFC, RFC (write/read floating point control register)
  1440. cond condition codes
  1441. P pre/post index bit: 0 = postindex, 1 = preindex
  1442. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1443. W write back bit: 1 = update base register (Rn)
  1444. L load/store bit: 0 = store, 1 = load
  1445. Rn base register
  1446. Rd destination/source register
  1447. Fd floating point destination register
  1448. Fn floating point source register
  1449. Fm floating point source register or floating point constant
  1450. uv transfer length (TABLE 1)
  1451. wx register count (TABLE 2)
  1452. abcd arithmetic opcode (TABLES 3 & 4)
  1453. ef destination size (rounding precision) (TABLE 5)
  1454. gh rounding mode (TABLE 6)
  1455. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1456. i constant bit: 1 = constant (TABLE 6)
  1457. */
  1458. /*
  1459. TABLE 1
  1460. +-------------------------+---+---+---------+---------+
  1461. | Precision | u | v | FPSR.EP | length |
  1462. +-------------------------+---+---+---------+---------+
  1463. | Single | 0 | 0 | x | 1 words |
  1464. | Double | 1 | 1 | x | 2 words |
  1465. | Extended | 1 | 1 | x | 3 words |
  1466. | Packed decimal | 1 | 1 | 0 | 3 words |
  1467. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1468. +-------------------------+---+---+---------+---------+
  1469. Note: x = don't care
  1470. */
  1471. /*
  1472. TABLE 2
  1473. +---+---+---------------------------------+
  1474. | w | x | Number of registers to transfer |
  1475. +---+---+---------------------------------+
  1476. | 0 | 1 | 1 |
  1477. | 1 | 0 | 2 |
  1478. | 1 | 1 | 3 |
  1479. | 0 | 0 | 4 |
  1480. +---+---+---------------------------------+
  1481. */
  1482. /*
  1483. TABLE 3: Dyadic Floating Point Opcodes
  1484. +---+---+---+---+----------+-----------------------+-----------------------+
  1485. | a | b | c | d | Mnemonic | Description | Operation |
  1486. +---+---+---+---+----------+-----------------------+-----------------------+
  1487. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1488. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1489. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1490. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1491. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1492. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1493. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1494. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1495. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1496. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1497. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1498. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1499. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1500. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1501. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1502. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1503. +---+---+---+---+----------+-----------------------+-----------------------+
  1504. Note: POW, RPW, POL are deprecated, and are available for backwards
  1505. compatibility only.
  1506. */
  1507. /*
  1508. TABLE 4: Monadic Floating Point Opcodes
  1509. +---+---+---+---+----------+-----------------------+-----------------------+
  1510. | a | b | c | d | Mnemonic | Description | Operation |
  1511. +---+---+---+---+----------+-----------------------+-----------------------+
  1512. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1513. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1514. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1515. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1516. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1517. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1518. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1519. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1520. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1521. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1522. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1523. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1524. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1525. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1526. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1527. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1528. +---+---+---+---+----------+-----------------------+-----------------------+
  1529. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1530. available for backwards compatibility only.
  1531. */
  1532. /*
  1533. TABLE 5
  1534. +-------------------------+---+---+
  1535. | Rounding Precision | e | f |
  1536. +-------------------------+---+---+
  1537. | IEEE Single precision | 0 | 0 |
  1538. | IEEE Double precision | 0 | 1 |
  1539. | IEEE Extended precision | 1 | 0 |
  1540. | undefined (trap) | 1 | 1 |
  1541. +-------------------------+---+---+
  1542. */
  1543. /*
  1544. TABLE 5
  1545. +---------------------------------+---+---+
  1546. | Rounding Mode | g | h |
  1547. +---------------------------------+---+---+
  1548. | Round to nearest (default) | 0 | 0 |
  1549. | Round toward plus infinity | 0 | 1 |
  1550. | Round toward negative infinity | 1 | 0 |
  1551. | Round toward zero | 1 | 1 |
  1552. +---------------------------------+---+---+
  1553. *)
  1554. function taicpu.GetString:string;
  1555. var
  1556. i : longint;
  1557. s : string;
  1558. addsize : boolean;
  1559. begin
  1560. s:='['+gas_op2str[opcode];
  1561. for i:=0 to ops-1 do
  1562. begin
  1563. with oper[i]^ do
  1564. begin
  1565. if i=0 then
  1566. s:=s+' '
  1567. else
  1568. s:=s+',';
  1569. { type }
  1570. addsize:=false;
  1571. if (ot and OT_VREG)=OT_VREG then
  1572. s:=s+'vreg'
  1573. else
  1574. if (ot and OT_FPUREG)=OT_FPUREG then
  1575. s:=s+'fpureg'
  1576. else
  1577. if (ot and OT_REGS)=OT_REGS then
  1578. s:=s+'sreg'
  1579. else
  1580. if (ot and OT_REGF)=OT_REGF then
  1581. s:=s+'creg'
  1582. else
  1583. if (ot and OT_REGISTER)=OT_REGISTER then
  1584. begin
  1585. s:=s+'reg';
  1586. addsize:=true;
  1587. end
  1588. else
  1589. if (ot and OT_REGLIST)=OT_REGLIST then
  1590. begin
  1591. s:=s+'reglist';
  1592. addsize:=false;
  1593. end
  1594. else
  1595. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1596. begin
  1597. s:=s+'imm';
  1598. addsize:=true;
  1599. end
  1600. else
  1601. if (ot and OT_MEMORY)=OT_MEMORY then
  1602. begin
  1603. s:=s+'mem';
  1604. addsize:=true;
  1605. if (ot and OT_AM2)<>0 then
  1606. s:=s+' am2 '
  1607. else if (ot and OT_AM6)<>0 then
  1608. s:=s+' am2 ';
  1609. end
  1610. else
  1611. if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
  1612. begin
  1613. s:=s+'shifterop';
  1614. addsize:=false;
  1615. end
  1616. else
  1617. s:=s+'???';
  1618. { size }
  1619. if addsize then
  1620. begin
  1621. if (ot and OT_BITS8)<>0 then
  1622. s:=s+'8'
  1623. else
  1624. if (ot and OT_BITS16)<>0 then
  1625. s:=s+'24'
  1626. else
  1627. if (ot and OT_BITS32)<>0 then
  1628. s:=s+'32'
  1629. else
  1630. if (ot and OT_BITSSHIFTER)<>0 then
  1631. s:=s+'shifter'
  1632. else
  1633. s:=s+'??';
  1634. { signed }
  1635. if (ot and OT_SIGNED)<>0 then
  1636. s:=s+'s';
  1637. end;
  1638. end;
  1639. end;
  1640. GetString:=s+']';
  1641. end;
  1642. procedure taicpu.ResetPass1;
  1643. begin
  1644. { we need to reset everything here, because the choosen insentry
  1645. can be invalid for a new situation where the previously optimized
  1646. insentry is not correct }
  1647. InsEntry:=nil;
  1648. InsSize:=0;
  1649. LastInsOffset:=-1;
  1650. end;
  1651. procedure taicpu.ResetPass2;
  1652. begin
  1653. { we are here in a second pass, check if the instruction can be optimized }
  1654. if assigned(InsEntry) and
  1655. ((InsEntry^.flags and IF_PASS2)<>0) then
  1656. begin
  1657. InsEntry:=nil;
  1658. InsSize:=0;
  1659. end;
  1660. LastInsOffset:=-1;
  1661. end;
  1662. function taicpu.CheckIfValid:boolean;
  1663. begin
  1664. Result:=False; { unimplemented }
  1665. end;
  1666. function taicpu.Pass1(objdata:TObjData):longint;
  1667. var
  1668. ldr2op : array[PF_B..PF_T] of tasmop = (
  1669. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1670. str2op : array[PF_B..PF_T] of tasmop = (
  1671. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1672. begin
  1673. Pass1:=0;
  1674. { Save the old offset and set the new offset }
  1675. InsOffset:=ObjData.CurrObjSec.Size;
  1676. { Error? }
  1677. if (Insentry=nil) and (InsSize=-1) then
  1678. exit;
  1679. { set the file postion }
  1680. current_filepos:=fileinfo;
  1681. { tranlate LDR+postfix to complete opcode }
  1682. if (opcode=A_LDR) and (oppostfix=PF_D) then
  1683. begin
  1684. opcode:=A_LDRD;
  1685. oppostfix:=PF_None;
  1686. end
  1687. else if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1688. begin
  1689. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1690. opcode:=ldr2op[oppostfix]
  1691. else
  1692. internalerror(2005091001);
  1693. if opcode=A_None then
  1694. internalerror(2005091004);
  1695. { postfix has been added to opcode }
  1696. oppostfix:=PF_None;
  1697. end
  1698. else if (opcode=A_STR) and (oppostfix=PF_D) then
  1699. begin
  1700. opcode:=A_STRD;
  1701. oppostfix:=PF_None;
  1702. end
  1703. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1704. begin
  1705. if (oppostfix in [low(str2op)..high(str2op)]) then
  1706. opcode:=str2op[oppostfix]
  1707. else
  1708. internalerror(2005091002);
  1709. if opcode=A_None then
  1710. internalerror(2005091003);
  1711. { postfix has been added to opcode }
  1712. oppostfix:=PF_None;
  1713. end;
  1714. { Get InsEntry }
  1715. if FindInsEntry(objdata) then
  1716. begin
  1717. InsSize:=4;
  1718. LastInsOffset:=InsOffset;
  1719. Pass1:=InsSize;
  1720. exit;
  1721. end;
  1722. LastInsOffset:=-1;
  1723. end;
  1724. procedure taicpu.Pass2(objdata:TObjData);
  1725. begin
  1726. { error in pass1 ? }
  1727. if insentry=nil then
  1728. exit;
  1729. current_filepos:=fileinfo;
  1730. { Generate the instruction }
  1731. GenCode(objdata);
  1732. end;
  1733. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1734. begin
  1735. end;
  1736. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1737. begin
  1738. end;
  1739. procedure taicpu.ppubuildderefimploper(var o:toper);
  1740. begin
  1741. end;
  1742. procedure taicpu.ppuderefoper(var o:toper);
  1743. begin
  1744. end;
  1745. procedure taicpu.BuildArmMasks;
  1746. const
  1747. Masks: array[tcputype] of longint =
  1748. (
  1749. IF_NONE,
  1750. IF_ARMv4,
  1751. IF_ARMv4,
  1752. IF_ARMv4T or IF_ARMv4,
  1753. IF_ARMv4T or IF_ARMv4 or IF_ARMv5,
  1754. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T,
  1755. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE,
  1756. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ,
  1757. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6,
  1758. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K,
  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,
  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,
  1761. IF_ARMv4T or IF_ARMv5T or IF_ARMv6M,
  1762. 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,
  1763. 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,
  1764. 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,
  1765. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M,
  1766. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M or IF_ARMv7EM
  1767. );
  1768. FPUMasks: array[tfputype] of longint =
  1769. (
  1770. IF_NONE,
  1771. IF_NONE,
  1772. IF_NONE,
  1773. IF_FPA,
  1774. IF_FPA,
  1775. IF_FPA,
  1776. IF_VFPv2,
  1777. IF_VFPv2 or IF_VFPv3,
  1778. IF_VFPv2 or IF_VFPv3,
  1779. IF_NONE
  1780. );
  1781. begin
  1782. fArmVMask:=Masks[current_settings.cputype] or FPUMasks[current_settings.fputype];
  1783. if current_settings.instructionset=is_thumb then
  1784. begin
  1785. fArmMask:=IF_THUMB;
  1786. if CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype] then
  1787. fArmMask:=fArmMask or IF_THUMB32;
  1788. end
  1789. else
  1790. fArmMask:=IF_ARM32;
  1791. end;
  1792. function taicpu.InsEnd:longint;
  1793. begin
  1794. Result:=0; { unimplemented }
  1795. end;
  1796. procedure taicpu.create_ot(objdata:TObjData);
  1797. var
  1798. i,l,relsize : longint;
  1799. dummy : byte;
  1800. currsym : TObjSymbol;
  1801. begin
  1802. if ops=0 then
  1803. exit;
  1804. { update oper[].ot field }
  1805. for i:=0 to ops-1 do
  1806. with oper[i]^ do
  1807. begin
  1808. case typ of
  1809. top_regset:
  1810. begin
  1811. ot:=OT_REGLIST;
  1812. end;
  1813. top_reg :
  1814. begin
  1815. case getregtype(reg) of
  1816. R_INTREGISTER:
  1817. begin
  1818. ot:=OT_REG32 or OT_SHIFTEROP;
  1819. if getsupreg(reg)<8 then
  1820. ot:=ot or OT_REGLO
  1821. else if reg=NR_STACK_POINTER_REG then
  1822. ot:=ot or OT_REGSP;
  1823. end;
  1824. R_FPUREGISTER:
  1825. ot:=OT_FPUREG;
  1826. R_MMREGISTER:
  1827. ot:=OT_VREG;
  1828. R_SPECIALREGISTER:
  1829. ot:=OT_REGF;
  1830. else
  1831. internalerror(2005090901);
  1832. end;
  1833. end;
  1834. top_ref :
  1835. begin
  1836. if ref^.refaddr=addr_no then
  1837. begin
  1838. { create ot field }
  1839. { we should get the size here dependend on the
  1840. instruction }
  1841. if (ot and OT_SIZE_MASK)=0 then
  1842. ot:=OT_MEMORY or OT_BITS32
  1843. else
  1844. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1845. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1846. ot:=ot or OT_MEM_OFFS;
  1847. { if we need to fix a reference, we do it here }
  1848. { pc relative addressing }
  1849. if (ref^.base=NR_NO) and
  1850. (ref^.index=NR_NO) and
  1851. (ref^.shiftmode=SM_None)
  1852. { at least we should check if the destination symbol
  1853. is in a text section }
  1854. { and
  1855. (ref^.symbol^.owner="text") } then
  1856. ref^.base:=NR_PC;
  1857. { determine possible address modes }
  1858. if GenerateThumbCode or
  1859. GenerateThumb2Code then
  1860. begin
  1861. if (ref^.base=NR_PC) then
  1862. ot:=ot or OT_AM6
  1863. else if (ref^.base=NR_STACK_POINTER_REG) then
  1864. ot:=ot or OT_AM5
  1865. else if ref^.index=NR_NO then
  1866. ot:=ot or OT_AM4
  1867. else
  1868. ot:=ot or OT_AM3;
  1869. end;
  1870. if (ref^.base<>NR_NO) and
  1871. (opcode in [A_LDREX,A_LDREXB,A_LDREXH,A_LDREXD,
  1872. A_STREX,A_STREXB,A_STREXH,A_STREXD]) and
  1873. (
  1874. (ref^.addressmode=AM_OFFSET) and
  1875. (ref^.index=NR_NO) and
  1876. (ref^.shiftmode=SM_None) and
  1877. (ref^.offset=0)
  1878. ) then
  1879. ot:=ot or OT_AM6
  1880. else if (ref^.base<>NR_NO) and
  1881. (
  1882. (
  1883. (ref^.index=NR_NO) and
  1884. (ref^.shiftmode=SM_None) and
  1885. (ref^.offset>=-4097) and
  1886. (ref^.offset<=4097)
  1887. ) or
  1888. (
  1889. (ref^.shiftmode=SM_None) and
  1890. (ref^.offset=0)
  1891. ) or
  1892. (
  1893. (ref^.index<>NR_NO) and
  1894. (ref^.shiftmode<>SM_None) and
  1895. (ref^.shiftimm<=32) and
  1896. (ref^.offset=0)
  1897. )
  1898. ) then
  1899. ot:=ot or OT_AM2;
  1900. if (ref^.index<>NR_NO) and
  1901. (oppostfix in [PF_None,PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1902. (
  1903. (ref^.base=NR_NO) and
  1904. (ref^.shiftmode=SM_None) and
  1905. (ref^.offset=0)
  1906. ) then
  1907. ot:=ot or OT_AM4;
  1908. end
  1909. else
  1910. begin
  1911. l:=ref^.offset;
  1912. currsym:=ObjData.symbolref(ref^.symbol);
  1913. if assigned(currsym) then
  1914. inc(l,currsym.address);
  1915. relsize:=(InsOffset+2)-l;
  1916. if (relsize<-33554428) or (relsize>33554428) then
  1917. ot:=OT_IMM32
  1918. else
  1919. ot:=OT_IMM24;
  1920. end;
  1921. end;
  1922. top_local :
  1923. begin
  1924. { we should get the size here dependend on the
  1925. instruction }
  1926. if (ot and OT_SIZE_MASK)=0 then
  1927. ot:=OT_MEMORY or OT_BITS32
  1928. else
  1929. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1930. end;
  1931. top_const :
  1932. begin
  1933. ot:=OT_IMMEDIATE;
  1934. if (val=0) then
  1935. ot:=ot_immediatezero
  1936. else if is_shifter_const(val,dummy) then
  1937. ot:=OT_IMMSHIFTER
  1938. else if GenerateThumb2Code and is_thumb32_imm(val) then
  1939. ot:=OT_IMMSHIFTER
  1940. else
  1941. ot:=OT_IMM32
  1942. end;
  1943. top_none :
  1944. begin
  1945. { generated when there was an error in the
  1946. assembler reader. It never happends when generating
  1947. assembler }
  1948. end;
  1949. top_shifterop:
  1950. begin
  1951. ot:=OT_SHIFTEROP;
  1952. end;
  1953. top_conditioncode:
  1954. begin
  1955. ot:=OT_CONDITION;
  1956. end;
  1957. top_specialreg:
  1958. begin
  1959. ot:=OT_REGS;
  1960. end;
  1961. top_modeflags:
  1962. begin
  1963. ot:=OT_MODEFLAGS;
  1964. end;
  1965. else
  1966. begin writeln(typ);
  1967. internalerror(200402261); end;
  1968. end;
  1969. end;
  1970. end;
  1971. function taicpu.Matches(p:PInsEntry):longint;
  1972. { * IF_SM stands for Size Match: any operand whose size is not
  1973. * explicitly specified by the template is `really' intended to be
  1974. * the same size as the first size-specified operand.
  1975. * Non-specification is tolerated in the input instruction, but
  1976. * _wrong_ specification is not.
  1977. *
  1978. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1979. * three-operand instructions such as SHLD: it implies that the
  1980. * first two operands must match in size, but that the third is
  1981. * required to be _unspecified_.
  1982. *
  1983. * IF_SB invokes Size Byte: operands with unspecified size in the
  1984. * template are really bytes, and so no non-byte specification in
  1985. * the input instruction will be tolerated. IF_SW similarly invokes
  1986. * Size Word, and IF_SD invokes Size Doubleword.
  1987. *
  1988. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1989. * that any operand with unspecified size in the template is
  1990. * required to have unspecified size in the instruction too...)
  1991. }
  1992. var
  1993. i{,j,asize,oprs} : longint;
  1994. {siz : array[0..3] of longint;}
  1995. begin
  1996. Matches:=100;
  1997. { Check the opcode and operands }
  1998. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1999. begin
  2000. Matches:=0;
  2001. exit;
  2002. end;
  2003. { check ARM instruction version }
  2004. if (p^.flags and fArmVMask)=0 then
  2005. begin
  2006. Matches:=0;
  2007. exit;
  2008. end;
  2009. { check ARM instruction type }
  2010. if (p^.flags and fArmMask)=0 then
  2011. begin
  2012. Matches:=0;
  2013. exit;
  2014. end;
  2015. { Check wideformat flag }
  2016. if wideformat and ((p^.flags and IF_WIDE)=0) then
  2017. begin
  2018. matches:=0;
  2019. exit;
  2020. end;
  2021. { Check that no spurious colons or TOs are present }
  2022. for i:=0 to p^.ops-1 do
  2023. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  2024. begin
  2025. Matches:=0;
  2026. exit;
  2027. end;
  2028. { Check that the operand flags all match up }
  2029. for i:=0 to p^.ops-1 do
  2030. begin
  2031. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  2032. ((p^.optypes[i] and OT_SIZE_MASK) and
  2033. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  2034. begin
  2035. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  2036. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  2037. begin
  2038. Matches:=0;
  2039. exit;
  2040. end
  2041. else if ((p^.optypes[i] and OT_OPT_SIZE)<>0) and
  2042. ((p^.optypes[i] and OT_OPT_SIZE)<>(oper[i]^.ot and OT_OPT_SIZE)) then
  2043. begin
  2044. Matches:=0;
  2045. exit;
  2046. end
  2047. else
  2048. Matches:=1;
  2049. end;
  2050. end;
  2051. { check postfixes:
  2052. the existance of a certain postfix requires a
  2053. particular code }
  2054. { update condition flags
  2055. or floating point single }
  2056. if (oppostfix=PF_S) and
  2057. not(p^.code[0] in [#$04..#$0F,#$14..#$16,#$29,#$30,#$60..#$6B,#$80..#$82,#$A0..#$A2]) then
  2058. begin
  2059. Matches:=0;
  2060. exit;
  2061. end;
  2062. { floating point size }
  2063. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  2064. not(p^.code[0] in [#$A0..#$A2]) then
  2065. begin
  2066. Matches:=0;
  2067. exit;
  2068. end;
  2069. { multiple load/store address modes }
  2070. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  2071. not(p^.code[0] in [
  2072. // ldr,str,ldrb,strb
  2073. #$17,
  2074. // stm,ldm
  2075. #$26,#$69,#$8C,
  2076. // vldm/vstm
  2077. #$44,#$94
  2078. ]) then
  2079. begin
  2080. Matches:=0;
  2081. exit;
  2082. end;
  2083. { we shouldn't see any opsize prefixes here }
  2084. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  2085. begin
  2086. Matches:=0;
  2087. exit;
  2088. end;
  2089. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  2090. begin
  2091. Matches:=0;
  2092. exit;
  2093. end;
  2094. { Check thumb flags }
  2095. if p^.code[0] in [#$60..#$61] then
  2096. begin
  2097. if (p^.code[0]=#$60) and
  2098. (GenerateThumb2Code and
  2099. ((not inIT) and (oppostfix<>PF_S)) or
  2100. (inIT and (condition=C_None))) then
  2101. begin
  2102. Matches:=0;
  2103. exit;
  2104. end
  2105. else if (p^.code[0]=#$61) and
  2106. (oppostfix=PF_S) then
  2107. begin
  2108. Matches:=0;
  2109. exit;
  2110. end;
  2111. end
  2112. else if p^.code[0]=#$62 then
  2113. begin
  2114. if (GenerateThumb2Code and
  2115. (condition<>C_None) and
  2116. (not inIT) and
  2117. (not lastinIT)) then
  2118. begin
  2119. Matches:=0;
  2120. exit;
  2121. end;
  2122. end
  2123. else if p^.code[0]=#$63 then
  2124. begin
  2125. if inIT then
  2126. begin
  2127. Matches:=0;
  2128. exit;
  2129. end;
  2130. end
  2131. else if p^.code[0]=#$64 then
  2132. begin
  2133. if (opcode=A_MUL) then
  2134. begin
  2135. if (ops=3) and
  2136. ((oper[2]^.typ<>top_reg) or
  2137. (oper[0]^.reg<>oper[2]^.reg)) then
  2138. begin
  2139. matches:=0;
  2140. exit;
  2141. end;
  2142. end;
  2143. end;
  2144. { Check operand sizes }
  2145. { as default an untyped size can get all the sizes, this is different
  2146. from nasm, but else we need to do a lot checking which opcodes want
  2147. size or not with the automatic size generation }
  2148. (*
  2149. asize:=longint($ffffffff);
  2150. if (p^.flags and IF_SB)<>0 then
  2151. asize:=OT_BITS8
  2152. else if (p^.flags and IF_SW)<>0 then
  2153. asize:=OT_BITS16
  2154. else if (p^.flags and IF_SD)<>0 then
  2155. asize:=OT_BITS32;
  2156. if (p^.flags and IF_ARMASK)<>0 then
  2157. begin
  2158. siz[0]:=0;
  2159. siz[1]:=0;
  2160. siz[2]:=0;
  2161. if (p^.flags and IF_AR0)<>0 then
  2162. siz[0]:=asize
  2163. else if (p^.flags and IF_AR1)<>0 then
  2164. siz[1]:=asize
  2165. else if (p^.flags and IF_AR2)<>0 then
  2166. siz[2]:=asize;
  2167. end
  2168. else
  2169. begin
  2170. { we can leave because the size for all operands is forced to be
  2171. the same
  2172. but not if IF_SB IF_SW or IF_SD is set PM }
  2173. if asize=-1 then
  2174. exit;
  2175. siz[0]:=asize;
  2176. siz[1]:=asize;
  2177. siz[2]:=asize;
  2178. end;
  2179. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  2180. begin
  2181. if (p^.flags and IF_SM2)<>0 then
  2182. oprs:=2
  2183. else
  2184. oprs:=p^.ops;
  2185. for i:=0 to oprs-1 do
  2186. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  2187. begin
  2188. for j:=0 to oprs-1 do
  2189. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  2190. break;
  2191. end;
  2192. end
  2193. else
  2194. oprs:=2;
  2195. { Check operand sizes }
  2196. for i:=0 to p^.ops-1 do
  2197. begin
  2198. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  2199. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  2200. { Immediates can always include smaller size }
  2201. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  2202. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  2203. Matches:=2;
  2204. end;
  2205. *)
  2206. end;
  2207. function taicpu.calcsize(p:PInsEntry):shortint;
  2208. begin
  2209. result:=4;
  2210. end;
  2211. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  2212. begin
  2213. Result:=False; { unimplemented }
  2214. end;
  2215. procedure taicpu.Swapoperands;
  2216. begin
  2217. end;
  2218. function taicpu.FindInsentry(objdata:TObjData):boolean;
  2219. var
  2220. i : longint;
  2221. begin
  2222. result:=false;
  2223. { Things which may only be done once, not when a second pass is done to
  2224. optimize }
  2225. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  2226. begin
  2227. { create the .ot fields }
  2228. create_ot(objdata);
  2229. BuildArmMasks;
  2230. { set the file postion }
  2231. current_filepos:=fileinfo;
  2232. end
  2233. else
  2234. begin
  2235. { we've already an insentry so it's valid }
  2236. result:=true;
  2237. exit;
  2238. end;
  2239. { Lookup opcode in the table }
  2240. InsSize:=-1;
  2241. i:=instabcache^[opcode];
  2242. if i=-1 then
  2243. begin
  2244. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  2245. exit;
  2246. end;
  2247. insentry:=@instab[i];
  2248. while (insentry^.opcode=opcode) do
  2249. begin
  2250. if matches(insentry)=100 then
  2251. begin
  2252. result:=true;
  2253. exit;
  2254. end;
  2255. inc(i);
  2256. insentry:=@instab[i];
  2257. end;
  2258. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2259. { No instruction found, set insentry to nil and inssize to -1 }
  2260. insentry:=nil;
  2261. inssize:=-1;
  2262. end;
  2263. procedure taicpu.gencode(objdata:TObjData);
  2264. const
  2265. CondVal : array[TAsmCond] of byte=(
  2266. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  2267. $B, $C, $D, $E, 0);
  2268. var
  2269. bytes, rd, rm, rn, d, m, n : dword;
  2270. bytelen : longint;
  2271. dp_operation : boolean;
  2272. i_field : byte;
  2273. currsym : TObjSymbol;
  2274. offset : longint;
  2275. refoper : poper;
  2276. msb : longint;
  2277. r: byte;
  2278. procedure setshifterop(op : byte);
  2279. var
  2280. r : byte;
  2281. imm : dword;
  2282. count : integer;
  2283. begin
  2284. case oper[op]^.typ of
  2285. top_const:
  2286. begin
  2287. i_field:=1;
  2288. if oper[op]^.val and $ff=oper[op]^.val then
  2289. bytes:=bytes or dword(oper[op]^.val)
  2290. else
  2291. begin
  2292. { calc rotate and adjust imm }
  2293. count:=0;
  2294. r:=0;
  2295. imm:=dword(oper[op]^.val);
  2296. repeat
  2297. imm:=RolDWord(imm, 2);
  2298. inc(r);
  2299. inc(count);
  2300. if count > 32 then
  2301. begin
  2302. message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm');
  2303. exit;
  2304. end;
  2305. until (imm and $ff)=imm;
  2306. bytes:=bytes or (r shl 8) or imm;
  2307. end;
  2308. end;
  2309. top_reg:
  2310. begin
  2311. i_field:=0;
  2312. bytes:=bytes or getsupreg(oper[op]^.reg);
  2313. { does a real shifter op follow? }
  2314. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  2315. with oper[op+1]^.shifterop^ do
  2316. begin
  2317. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2318. if shiftmode<>SM_RRX then
  2319. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2320. else
  2321. bytes:=bytes or (3 shl 5);
  2322. if getregtype(rs) <> R_INVALIDREGISTER then
  2323. begin
  2324. bytes:=bytes or (1 shl 4);
  2325. bytes:=bytes or (getsupreg(rs) shl 8);
  2326. end
  2327. end;
  2328. end;
  2329. else
  2330. internalerror(2005091103);
  2331. end;
  2332. end;
  2333. function MakeRegList(reglist: tcpuregisterset): word;
  2334. var
  2335. i, w: word;
  2336. begin
  2337. result:=0;
  2338. w:=1;
  2339. for i:=RS_R0 to RS_R15 do
  2340. begin
  2341. if i in reglist then
  2342. result:=result or w;
  2343. w:=w shl 1
  2344. end;
  2345. end;
  2346. function getcoproc(reg: tregister): byte;
  2347. begin
  2348. if reg=NR_p15 then
  2349. result:=15
  2350. else
  2351. begin
  2352. Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
  2353. result:=0;
  2354. end;
  2355. end;
  2356. function getcoprocreg(reg: tregister): byte;
  2357. begin
  2358. result:=getsupreg(reg)-getsupreg(NR_CR0);
  2359. end;
  2360. function getmmreg(reg: tregister): byte;
  2361. begin
  2362. case reg of
  2363. NR_D0: result:=0;
  2364. NR_D1: result:=1;
  2365. NR_D2: result:=2;
  2366. NR_D3: result:=3;
  2367. NR_D4: result:=4;
  2368. NR_D5: result:=5;
  2369. NR_D6: result:=6;
  2370. NR_D7: result:=7;
  2371. NR_D8: result:=8;
  2372. NR_D9: result:=9;
  2373. NR_D10: result:=10;
  2374. NR_D11: result:=11;
  2375. NR_D12: result:=12;
  2376. NR_D13: result:=13;
  2377. NR_D14: result:=14;
  2378. NR_D15: result:=15;
  2379. NR_D16: result:=16;
  2380. NR_D17: result:=17;
  2381. NR_D18: result:=18;
  2382. NR_D19: result:=19;
  2383. NR_D20: result:=20;
  2384. NR_D21: result:=21;
  2385. NR_D22: result:=22;
  2386. NR_D23: result:=23;
  2387. NR_D24: result:=24;
  2388. NR_D25: result:=25;
  2389. NR_D26: result:=26;
  2390. NR_D27: result:=27;
  2391. NR_D28: result:=28;
  2392. NR_D29: result:=29;
  2393. NR_D30: result:=30;
  2394. NR_D31: result:=31;
  2395. NR_S0: result:=0;
  2396. NR_S1: result:=1;
  2397. NR_S2: result:=2;
  2398. NR_S3: result:=3;
  2399. NR_S4: result:=4;
  2400. NR_S5: result:=5;
  2401. NR_S6: result:=6;
  2402. NR_S7: result:=7;
  2403. NR_S8: result:=8;
  2404. NR_S9: result:=9;
  2405. NR_S10: result:=10;
  2406. NR_S11: result:=11;
  2407. NR_S12: result:=12;
  2408. NR_S13: result:=13;
  2409. NR_S14: result:=14;
  2410. NR_S15: result:=15;
  2411. NR_S16: result:=16;
  2412. NR_S17: result:=17;
  2413. NR_S18: result:=18;
  2414. NR_S19: result:=19;
  2415. NR_S20: result:=20;
  2416. NR_S21: result:=21;
  2417. NR_S22: result:=22;
  2418. NR_S23: result:=23;
  2419. NR_S24: result:=24;
  2420. NR_S25: result:=25;
  2421. NR_S26: result:=26;
  2422. NR_S27: result:=27;
  2423. NR_S28: result:=28;
  2424. NR_S29: result:=29;
  2425. NR_S30: result:=30;
  2426. NR_S31: result:=31;
  2427. else
  2428. result:=0;
  2429. end;
  2430. end;
  2431. procedure encodethumbimm(imm: longword);
  2432. var
  2433. imm12, tmp: tcgint;
  2434. shift: integer;
  2435. found: boolean;
  2436. begin
  2437. found:=true;
  2438. if (imm and $FF) = imm then
  2439. imm12:=imm
  2440. else if ((imm shr 16)=(imm and $FFFF)) and
  2441. ((imm and $FF00FF00) = 0) then
  2442. imm12:=(imm and $ff) or ($1 shl 8)
  2443. else if ((imm shr 16)=(imm and $FFFF)) and
  2444. ((imm and $00FF00FF) = 0) then
  2445. imm12:=((imm shr 8) and $ff) or ($2 shl 8)
  2446. else if ((imm shr 16)=(imm and $FFFF)) and
  2447. (((imm shr 8) and $FF)=(imm and $FF)) then
  2448. imm12:=(imm and $ff) or ($3 shl 8)
  2449. else
  2450. begin
  2451. found:=false;
  2452. imm12:=0;
  2453. for shift:=1 to 31 do
  2454. begin
  2455. tmp:=RolDWord(imm,shift);
  2456. if ((tmp and $FF)=tmp) and
  2457. ((tmp and $80)=$80) then
  2458. begin
  2459. imm12:=(tmp and $7F) or (shift shl 7);
  2460. found:=true;
  2461. break;
  2462. end;
  2463. end;
  2464. end;
  2465. if found then
  2466. begin
  2467. bytes:=bytes or (imm12 and $FF);
  2468. bytes:=bytes or (((imm12 shr 8) and $7) shl 12);
  2469. bytes:=bytes or (((imm12 shr 11) and $1) shl 26);
  2470. end
  2471. else
  2472. Message1(asmw_e_value_exceeds_bounds, IntToStr(imm));
  2473. end;
  2474. procedure setthumbshift(op: byte; is_sat: boolean = false);
  2475. var
  2476. shift,typ: byte;
  2477. begin
  2478. shift:=0;
  2479. typ:=0;
  2480. case oper[op]^.shifterop^.shiftmode of
  2481. SM_LSL: begin typ:=0; shift:=oper[op]^.shifterop^.shiftimm; end;
  2482. SM_LSR: begin typ:=1; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2483. SM_ASR: begin typ:=2; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2484. SM_ROR: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; if shift=0 then message(asmw_e_invalid_opcode_and_operands); end;
  2485. SM_RRX: begin typ:=3; shift:=0; end;
  2486. end;
  2487. if is_sat then
  2488. begin
  2489. bytes:=bytes or ((typ and 1) shl 5);
  2490. bytes:=bytes or ((typ shr 1) shl 21);
  2491. end
  2492. else
  2493. bytes:=bytes or (typ shl 4);
  2494. bytes:=bytes or (shift and $3) shl 6;
  2495. bytes:=bytes or ((shift and $1C) shr 2) shl 12;
  2496. end;
  2497. begin
  2498. bytes:=$0;
  2499. bytelen:=4;
  2500. i_field:=0;
  2501. { evaluate and set condition code }
  2502. bytes:=bytes or (CondVal[condition] shl 28);
  2503. { condition code allowed? }
  2504. { setup rest of the instruction }
  2505. case insentry^.code[0] of
  2506. #$01: // B/BL
  2507. begin
  2508. { set instruction code }
  2509. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2510. { set offset }
  2511. if oper[0]^.typ=top_const then
  2512. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2513. else
  2514. begin
  2515. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2516. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2517. begin
  2518. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  2519. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2520. end
  2521. else
  2522. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2523. end;
  2524. end;
  2525. #$02:
  2526. begin
  2527. { set instruction code }
  2528. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2529. { set code }
  2530. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2531. end;
  2532. #$03:
  2533. begin // BLX/BX
  2534. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2535. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2536. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2537. bytes:=bytes or ord(insentry^.code[4]);
  2538. bytes:=bytes or getsupreg(oper[0]^.reg);
  2539. end;
  2540. #$04..#$07: // SUB
  2541. begin
  2542. { set instruction code }
  2543. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2544. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2545. { set destination }
  2546. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2547. { set Rn }
  2548. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2549. { create shifter op }
  2550. setshifterop(2);
  2551. { set I field }
  2552. bytes:=bytes or (i_field shl 25);
  2553. { set S if necessary }
  2554. if oppostfix=PF_S then
  2555. bytes:=bytes or (1 shl 20);
  2556. end;
  2557. #$08,#$0A,#$0B: // MOV
  2558. begin
  2559. { set instruction code }
  2560. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2561. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2562. { set destination }
  2563. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2564. { create shifter op }
  2565. setshifterop(1);
  2566. { set I field }
  2567. bytes:=bytes or (i_field shl 25);
  2568. { set S if necessary }
  2569. if oppostfix=PF_S then
  2570. bytes:=bytes or (1 shl 20);
  2571. end;
  2572. #$0C,#$0E,#$0F: // CMP
  2573. begin
  2574. { set instruction code }
  2575. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2576. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2577. { set destination }
  2578. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2579. { create shifter op }
  2580. setshifterop(1);
  2581. { set I field }
  2582. bytes:=bytes or (i_field shl 25);
  2583. { always set S bit }
  2584. bytes:=bytes or (1 shl 20);
  2585. end;
  2586. #$10: // MRS
  2587. begin
  2588. { set instruction code }
  2589. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2590. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2591. { set destination }
  2592. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2593. case oper[1]^.reg of
  2594. NR_APSR,NR_CPSR:;
  2595. NR_SPSR:
  2596. begin
  2597. bytes:=bytes or (1 shl 22);
  2598. end;
  2599. else
  2600. Message(asmw_e_invalid_opcode_and_operands);
  2601. end;
  2602. end;
  2603. #$12,#$13: // MSR
  2604. begin
  2605. { set instruction code }
  2606. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2607. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2608. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2609. { set destination }
  2610. if oper[0]^.typ=top_specialreg then
  2611. begin
  2612. if (oper[0]^.specialreg<>NR_CPSR) and
  2613. (oper[0]^.specialreg<>NR_SPSR) then
  2614. Message1(asmw_e_invalid_opcode_and_operands, '"Invalid special reg"');
  2615. if srC in oper[0]^.specialflags then
  2616. bytes:=bytes or (1 shl 16);
  2617. if srX in oper[0]^.specialflags then
  2618. bytes:=bytes or (1 shl 17);
  2619. if srS in oper[0]^.specialflags then
  2620. bytes:=bytes or (1 shl 18);
  2621. if srF in oper[0]^.specialflags then
  2622. bytes:=bytes or (1 shl 19);
  2623. { Set R bit }
  2624. if oper[0]^.specialreg=NR_SPSR then
  2625. bytes:=bytes or (1 shl 22);
  2626. end
  2627. else
  2628. case oper[0]^.reg of
  2629. NR_APSR_nzcvq: bytes:=bytes or (2 shl 18);
  2630. NR_APSR_g: bytes:=bytes or (1 shl 18);
  2631. NR_APSR_nzcvqg: bytes:=bytes or (3 shl 18);
  2632. else
  2633. Message1(asmw_e_invalid_opcode_and_operands, 'Invalid combination APSR bits used');
  2634. end;
  2635. setshifterop(1);
  2636. end;
  2637. #$14: // MUL/MLA r1,r2,r3
  2638. begin
  2639. { set instruction code }
  2640. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2641. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2642. bytes:=bytes or ord(insentry^.code[3]);
  2643. { set regs }
  2644. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2645. bytes:=bytes or getsupreg(oper[1]^.reg);
  2646. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2647. if oppostfix in [PF_S] then
  2648. bytes:=bytes or (1 shl 20);
  2649. end;
  2650. #$15: // MUL/MLA r1,r2,r3,r4
  2651. begin
  2652. { set instruction code }
  2653. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2654. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2655. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2656. { set regs }
  2657. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2658. bytes:=bytes or getsupreg(oper[1]^.reg);
  2659. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2660. if ops>3 then
  2661. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2662. else
  2663. bytes:=bytes or ord(insentry^.code[4]) shl 12;
  2664. if oppostfix in [PF_R,PF_X] then
  2665. bytes:=bytes or (1 shl 5);
  2666. if oppostfix in [PF_S] then
  2667. bytes:=bytes or (1 shl 20);
  2668. end;
  2669. #$16: // MULL r1,r2,r3,r4
  2670. begin
  2671. { set instruction code }
  2672. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2673. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2674. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2675. { set regs }
  2676. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2677. if (ops=3) and (opcode=A_PKHTB) then
  2678. begin
  2679. bytes:=bytes or getsupreg(oper[1]^.reg);
  2680. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2681. end
  2682. else
  2683. begin
  2684. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2685. bytes:=bytes or getsupreg(oper[2]^.reg);
  2686. end;
  2687. if ops=4 then
  2688. begin
  2689. if oper[3]^.typ=top_shifterop then
  2690. begin
  2691. if opcode in [A_PKHBT,A_PKHTB] then
  2692. begin
  2693. if ((opcode=A_PKHTB) and
  2694. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2695. ((opcode=A_PKHBT) and
  2696. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2697. (oper[3]^.shifterop^.rs<>NR_NO) then
  2698. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2699. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2700. end
  2701. else
  2702. begin
  2703. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2704. (oper[3]^.shifterop^.rs<>NR_NO) or
  2705. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2706. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2707. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2708. end;
  2709. end
  2710. else
  2711. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2712. end;
  2713. if PF_S=oppostfix then
  2714. bytes:=bytes or (1 shl 20);
  2715. if PF_X=oppostfix then
  2716. bytes:=bytes or (1 shl 5);
  2717. end;
  2718. #$17: // LDR/STR
  2719. begin
  2720. { set instruction code }
  2721. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2722. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2723. { set Rn and Rd }
  2724. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2725. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2726. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2727. begin
  2728. { set offset }
  2729. offset:=0;
  2730. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2731. if assigned(currsym) then
  2732. offset:=currsym.offset-insoffset-8;
  2733. offset:=offset+oper[1]^.ref^.offset;
  2734. if offset>=0 then
  2735. { set U flag }
  2736. bytes:=bytes or (1 shl 23)
  2737. else
  2738. offset:=-offset;
  2739. bytes:=bytes or (offset and $FFF);
  2740. end
  2741. else
  2742. begin
  2743. { set U flag }
  2744. if oper[1]^.ref^.signindex>=0 then
  2745. bytes:=bytes or (1 shl 23);
  2746. { set I flag }
  2747. bytes:=bytes or (1 shl 25);
  2748. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2749. { set shift }
  2750. with oper[1]^.ref^ do
  2751. if shiftmode<>SM_None then
  2752. begin
  2753. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2754. if shiftmode<>SM_RRX then
  2755. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2756. else
  2757. bytes:=bytes or (3 shl 5);
  2758. end
  2759. end;
  2760. { set W bit }
  2761. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2762. bytes:=bytes or (1 shl 21);
  2763. { set P bit if necessary }
  2764. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2765. bytes:=bytes or (1 shl 24);
  2766. end;
  2767. #$18: // LDREX/STREX
  2768. begin
  2769. { set instruction code }
  2770. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2771. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2772. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2773. bytes:=bytes or ord(insentry^.code[4]);
  2774. { set Rn and Rd }
  2775. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2776. if (ops=3) then
  2777. begin
  2778. if opcode<>A_LDREXD then
  2779. bytes:=bytes or getsupreg(oper[1]^.reg);
  2780. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2781. end
  2782. else if (ops=4) then // STREXD
  2783. begin
  2784. if opcode<>A_LDREXD then
  2785. bytes:=bytes or getsupreg(oper[1]^.reg);
  2786. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  2787. end
  2788. else
  2789. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  2790. end;
  2791. #$19: // LDRD/STRD
  2792. begin
  2793. { set instruction code }
  2794. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2795. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2796. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2797. bytes:=bytes or ord(insentry^.code[4]);
  2798. { set Rn and Rd }
  2799. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2800. refoper:=oper[1];
  2801. if ops=3 then
  2802. refoper:=oper[2];
  2803. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2804. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2805. begin
  2806. bytes:=bytes or (1 shl 22);
  2807. { set offset }
  2808. offset:=0;
  2809. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2810. if assigned(currsym) then
  2811. offset:=currsym.offset-insoffset-8;
  2812. offset:=offset+refoper^.ref^.offset;
  2813. if offset>=0 then
  2814. { set U flag }
  2815. bytes:=bytes or (1 shl 23)
  2816. else
  2817. offset:=-offset;
  2818. bytes:=bytes or (offset and $F);
  2819. bytes:=bytes or ((offset and $F0) shl 4);
  2820. end
  2821. else
  2822. begin
  2823. { set U flag }
  2824. if refoper^.ref^.signindex>=0 then
  2825. bytes:=bytes or (1 shl 23);
  2826. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2827. end;
  2828. { set W bit }
  2829. if refoper^.ref^.addressmode=AM_PREINDEXED then
  2830. bytes:=bytes or (1 shl 21);
  2831. { set P bit if necessary }
  2832. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  2833. bytes:=bytes or (1 shl 24);
  2834. end;
  2835. #$1A: // QADD/QSUB
  2836. begin
  2837. { set instruction code }
  2838. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2839. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2840. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2841. { set regs }
  2842. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2843. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  2844. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2845. end;
  2846. #$1B:
  2847. begin
  2848. { set instruction code }
  2849. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2850. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2851. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2852. { set regs }
  2853. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2854. bytes:=bytes or getsupreg(oper[1]^.reg);
  2855. if ops=3 then
  2856. begin
  2857. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  2858. (oper[2]^.shifterop^.rs<>NR_NO) or
  2859. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  2860. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2861. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2862. end;
  2863. end;
  2864. #$1C: // MCR/MRC
  2865. begin
  2866. { set instruction code }
  2867. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2868. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2869. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2870. { set regs and operands }
  2871. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2872. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  2873. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2874. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  2875. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2876. if ops > 5 then
  2877. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  2878. end;
  2879. #$1D: // MCRR/MRRC
  2880. begin
  2881. { set instruction code }
  2882. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2883. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2884. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2885. { set regs and operands }
  2886. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2887. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  2888. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2889. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  2890. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2891. end;
  2892. #$1E: // LDRHT/STRHT
  2893. begin
  2894. { set instruction code }
  2895. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2896. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2897. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2898. bytes:=bytes or ord(insentry^.code[4]);
  2899. { set Rn and Rd }
  2900. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2901. refoper:=oper[1];
  2902. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2903. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2904. begin
  2905. bytes:=bytes or (1 shl 22);
  2906. { set offset }
  2907. offset:=0;
  2908. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2909. if assigned(currsym) then
  2910. offset:=currsym.offset-insoffset-8;
  2911. offset:=offset+refoper^.ref^.offset;
  2912. if offset>=0 then
  2913. { set U flag }
  2914. bytes:=bytes or (1 shl 23)
  2915. else
  2916. offset:=-offset;
  2917. bytes:=bytes or (offset and $F);
  2918. bytes:=bytes or ((offset and $F0) shl 4);
  2919. end
  2920. else
  2921. begin
  2922. { set U flag }
  2923. if refoper^.ref^.signindex>=0 then
  2924. bytes:=bytes or (1 shl 23);
  2925. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2926. end;
  2927. end;
  2928. #$22: // LDRH/STRH
  2929. begin
  2930. { set instruction code }
  2931. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2932. bytes:=bytes or ord(insentry^.code[2]);
  2933. { src/dest register (Rd) }
  2934. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2935. { base register (Rn) }
  2936. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2937. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2938. begin
  2939. bytes:=bytes or (1 shl 22); // with immediate offset
  2940. offset:=oper[1]^.ref^.offset;
  2941. if offset>=0 then
  2942. { set U flag }
  2943. bytes:=bytes or (1 shl 23)
  2944. else
  2945. offset:=-offset;
  2946. bytes:=bytes or (offset and $F);
  2947. bytes:=bytes or ((offset and $F0) shl 4);
  2948. end
  2949. else
  2950. begin
  2951. { set U flag }
  2952. if oper[1]^.ref^.signindex>=0 then
  2953. bytes:=bytes or (1 shl 23);
  2954. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2955. end;
  2956. { set W bit }
  2957. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2958. bytes:=bytes or (1 shl 21);
  2959. { set P bit if necessary }
  2960. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2961. bytes:=bytes or (1 shl 24);
  2962. end;
  2963. #$25: // PLD/PLI
  2964. begin
  2965. { set instruction code }
  2966. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2967. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2968. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2969. bytes:=bytes or ord(insentry^.code[4]);
  2970. { set Rn and Rd }
  2971. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  2972. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  2973. begin
  2974. { set offset }
  2975. offset:=0;
  2976. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2977. if assigned(currsym) then
  2978. offset:=currsym.offset-insoffset-8;
  2979. offset:=offset+oper[0]^.ref^.offset;
  2980. if offset>=0 then
  2981. begin
  2982. { set U flag }
  2983. bytes:=bytes or (1 shl 23);
  2984. bytes:=bytes or offset
  2985. end
  2986. else
  2987. begin
  2988. offset:=-offset;
  2989. bytes:=bytes or offset
  2990. end;
  2991. end
  2992. else
  2993. begin
  2994. bytes:=bytes or (1 shl 25);
  2995. { set U flag }
  2996. if oper[0]^.ref^.signindex>=0 then
  2997. bytes:=bytes or (1 shl 23);
  2998. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  2999. { set shift }
  3000. with oper[0]^.ref^ do
  3001. if shiftmode<>SM_None then
  3002. begin
  3003. bytes:=bytes or ((shiftimm and $1F) shl 7);
  3004. if shiftmode<>SM_RRX then
  3005. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  3006. else
  3007. bytes:=bytes or (3 shl 5);
  3008. end
  3009. end;
  3010. end;
  3011. #$26: // LDM/STM
  3012. begin
  3013. { set instruction code }
  3014. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3015. if ops>1 then
  3016. begin
  3017. if oper[0]^.typ=top_ref then
  3018. begin
  3019. { set W bit }
  3020. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  3021. bytes:=bytes or (1 shl 21);
  3022. { set Rn }
  3023. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  3024. end
  3025. else { typ=top_reg }
  3026. begin
  3027. { set Rn }
  3028. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3029. end;
  3030. { reglist }
  3031. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  3032. end
  3033. else
  3034. begin
  3035. { push/pop }
  3036. { Set W and Rn to SP }
  3037. if opcode=A_PUSH then
  3038. bytes:=bytes or (1 shl 21);
  3039. bytes:=bytes or ($D shl 16);
  3040. { reglist }
  3041. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  3042. end;
  3043. { set P bit }
  3044. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  3045. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  3046. or (opcode=A_PUSH) then
  3047. bytes:=bytes or (1 shl 24);
  3048. { set U bit }
  3049. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  3050. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  3051. or (opcode=A_POP) then
  3052. bytes:=bytes or (1 shl 23);
  3053. end;
  3054. #$27: // SWP/SWPB
  3055. begin
  3056. { set instruction code }
  3057. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3058. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  3059. { set regs }
  3060. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3061. bytes:=bytes or getsupreg(oper[1]^.reg);
  3062. if ops=3 then
  3063. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3064. end;
  3065. #$28: // BX/BLX
  3066. begin
  3067. { set instruction code }
  3068. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3069. { set offset }
  3070. if oper[0]^.typ=top_const then
  3071. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  3072. else
  3073. begin
  3074. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3075. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  3076. begin
  3077. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  3078. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  3079. end
  3080. else
  3081. begin
  3082. offset:=((currsym.offset-insoffset-8) and $3fffffe);
  3083. { Turn BLX into BL if the destination isn't odd, could happen with recursion }
  3084. if not odd(offset shr 1) then
  3085. bytes:=(bytes and $EB000000) or $EB000000;
  3086. bytes:=bytes or ((offset shr 2) and $ffffff);
  3087. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  3088. end;
  3089. end;
  3090. end;
  3091. #$29: // SUB
  3092. begin
  3093. { set instruction code }
  3094. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3095. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3096. { set regs }
  3097. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3098. { set S if necessary }
  3099. if oppostfix=PF_S then
  3100. bytes:=bytes or (1 shl 20);
  3101. end;
  3102. #$2A:
  3103. begin
  3104. { set instruction code }
  3105. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3106. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3107. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3108. bytes:=bytes or ord(insentry^.code[4]);
  3109. { set opers }
  3110. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3111. if opcode in [A_SSAT, A_SSAT16] then
  3112. bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
  3113. else
  3114. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  3115. bytes:=bytes or getsupreg(oper[2]^.reg);
  3116. if (ops>3) and
  3117. (oper[3]^.typ=top_shifterop) and
  3118. (oper[3]^.shifterop^.rs=NR_NO) then
  3119. begin
  3120. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  3121. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  3122. bytes:=bytes or (1 shl 6)
  3123. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  3124. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3125. end;
  3126. end;
  3127. #$2B: // SETEND
  3128. begin
  3129. { set instruction code }
  3130. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3131. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3132. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3133. bytes:=bytes or ord(insentry^.code[4]);
  3134. { set endian specifier }
  3135. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  3136. end;
  3137. #$2C: // MOVW
  3138. begin
  3139. { set instruction code }
  3140. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3141. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3142. { set destination }
  3143. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3144. { set imm }
  3145. bytes:=bytes or (oper[1]^.val and $FFF);
  3146. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  3147. end;
  3148. #$2D: // BFX
  3149. begin
  3150. { set instruction code }
  3151. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3152. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3153. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3154. bytes:=bytes or ord(insentry^.code[4]);
  3155. if ops=3 then
  3156. begin
  3157. msb:=(oper[1]^.val+oper[2]^.val-1);
  3158. { set destination }
  3159. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3160. { set immediates }
  3161. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  3162. bytes:=bytes or ((msb and $1F) shl 16);
  3163. end
  3164. else
  3165. begin
  3166. if opcode in [A_BFC,A_BFI] then
  3167. msb:=(oper[2]^.val+oper[3]^.val-1)
  3168. else
  3169. msb:=oper[3]^.val-1;
  3170. { set destination }
  3171. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3172. bytes:=bytes or getsupreg(oper[1]^.reg);
  3173. { set immediates }
  3174. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3175. bytes:=bytes or ((msb and $1F) shl 16);
  3176. end;
  3177. end;
  3178. #$2E: // Cache stuff
  3179. begin
  3180. { set instruction code }
  3181. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3182. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3183. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3184. bytes:=bytes or ord(insentry^.code[4]);
  3185. { set code }
  3186. bytes:=bytes or (oper[0]^.val and $F);
  3187. end;
  3188. #$2F: // Nop
  3189. begin
  3190. { set instruction code }
  3191. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3192. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3193. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3194. bytes:=bytes or ord(insentry^.code[4]);
  3195. end;
  3196. #$30: // Shifts
  3197. begin
  3198. { set instruction code }
  3199. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3200. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3201. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3202. bytes:=bytes or ord(insentry^.code[4]);
  3203. { set destination }
  3204. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3205. bytes:=bytes or getsupreg(oper[1]^.reg);
  3206. if ops>2 then
  3207. begin
  3208. { set shift }
  3209. if oper[2]^.typ=top_reg then
  3210. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  3211. else
  3212. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3213. end;
  3214. { set S if necessary }
  3215. if oppostfix=PF_S then
  3216. bytes:=bytes or (1 shl 20);
  3217. end;
  3218. #$31: // BKPT
  3219. begin
  3220. { set instruction code }
  3221. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3222. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3223. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  3224. { set imm }
  3225. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  3226. bytes:=bytes or (oper[0]^.val and $F);
  3227. end;
  3228. #$32: // CLZ/REV
  3229. begin
  3230. { set instruction code }
  3231. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3232. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3233. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3234. bytes:=bytes or ord(insentry^.code[4]);
  3235. { set regs }
  3236. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3237. bytes:=bytes or getsupreg(oper[1]^.reg);
  3238. end;
  3239. #$33:
  3240. begin
  3241. { set instruction code }
  3242. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3243. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3244. { set regs }
  3245. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3246. if oper[1]^.typ=top_ref then
  3247. begin
  3248. { set offset }
  3249. offset:=0;
  3250. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3251. if assigned(currsym) then
  3252. offset:=currsym.offset-insoffset-8;
  3253. offset:=offset+oper[1]^.ref^.offset;
  3254. if offset>=0 then
  3255. begin
  3256. { set U flag }
  3257. bytes:=bytes or (1 shl 23);
  3258. bytes:=bytes or offset
  3259. end
  3260. else
  3261. begin
  3262. bytes:=bytes or (1 shl 22);
  3263. offset:=-offset;
  3264. bytes:=bytes or offset
  3265. end;
  3266. end
  3267. else
  3268. begin
  3269. if is_shifter_const(oper[1]^.val,r) then
  3270. begin
  3271. setshifterop(1);
  3272. bytes:=bytes or (1 shl 23);
  3273. end
  3274. else
  3275. begin
  3276. bytes:=bytes or (1 shl 22);
  3277. oper[1]^.val:=-oper[1]^.val;
  3278. setshifterop(1);
  3279. end;
  3280. end;
  3281. end;
  3282. #$40,#$90: // VMOV
  3283. begin
  3284. { set instruction code }
  3285. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3286. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3287. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3288. bytes:=bytes or ord(insentry^.code[4]);
  3289. { set regs }
  3290. Rd:=0;
  3291. Rn:=0;
  3292. Rm:=0;
  3293. case oppostfix of
  3294. PF_None:
  3295. begin
  3296. if ops=4 then
  3297. begin
  3298. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3299. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  3300. begin
  3301. Rd:=getmmreg(oper[0]^.reg);
  3302. Rm:=getsupreg(oper[2]^.reg);
  3303. Rn:=getsupreg(oper[3]^.reg);
  3304. end
  3305. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3306. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3307. begin
  3308. Rm:=getsupreg(oper[0]^.reg);
  3309. Rn:=getsupreg(oper[1]^.reg);
  3310. Rd:=getmmreg(oper[2]^.reg);
  3311. end
  3312. else
  3313. message(asmw_e_invalid_opcode_and_operands);
  3314. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  3315. bytes:=bytes or ((Rd and $1) shl 5);
  3316. bytes:=bytes or (Rm shl 12);
  3317. bytes:=bytes or (Rn shl 16);
  3318. end
  3319. else if ops=3 then
  3320. begin
  3321. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3322. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3323. begin
  3324. Rd:=getmmreg(oper[0]^.reg);
  3325. Rm:=getsupreg(oper[1]^.reg);
  3326. Rn:=getsupreg(oper[2]^.reg);
  3327. end
  3328. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3329. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3330. begin
  3331. Rm:=getsupreg(oper[0]^.reg);
  3332. Rn:=getsupreg(oper[1]^.reg);
  3333. Rd:=getmmreg(oper[2]^.reg);
  3334. end
  3335. else
  3336. message(asmw_e_invalid_opcode_and_operands);
  3337. bytes:=bytes or ((Rd and $F) shl 0);
  3338. bytes:=bytes or ((Rd and $10) shl 1);
  3339. bytes:=bytes or (Rm shl 12);
  3340. bytes:=bytes or (Rn shl 16);
  3341. end
  3342. else if ops=2 then
  3343. begin
  3344. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3345. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3346. begin
  3347. Rd:=getmmreg(oper[0]^.reg);
  3348. Rm:=getsupreg(oper[1]^.reg);
  3349. end
  3350. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3351. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  3352. begin
  3353. Rm:=getsupreg(oper[0]^.reg);
  3354. Rd:=getmmreg(oper[1]^.reg);
  3355. end
  3356. else
  3357. message(asmw_e_invalid_opcode_and_operands);
  3358. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  3359. bytes:=bytes or ((Rd and $1) shl 7);
  3360. bytes:=bytes or (Rm shl 12);
  3361. end;
  3362. end;
  3363. PF_F32:
  3364. begin
  3365. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3366. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3367. Message(asmw_e_invalid_opcode_and_operands);
  3368. Rd:=getmmreg(oper[0]^.reg);
  3369. Rm:=getmmreg(oper[1]^.reg);
  3370. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  3371. bytes:=bytes or ((Rd and $1) shl 22);
  3372. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  3373. bytes:=bytes or ((Rm and $1) shl 5);
  3374. end;
  3375. PF_F64:
  3376. begin
  3377. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3378. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3379. Message(asmw_e_invalid_opcode_and_operands);
  3380. Rd:=getmmreg(oper[0]^.reg);
  3381. Rm:=getmmreg(oper[1]^.reg);
  3382. bytes:=bytes or (1 shl 8);
  3383. bytes:=bytes or ((Rd and $F) shl 12);
  3384. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  3385. bytes:=bytes or (Rm and $F);
  3386. bytes:=bytes or ((Rm and $10) shl 1);
  3387. end;
  3388. end;
  3389. end;
  3390. #$41,#$91: // VMRS/VMSR
  3391. begin
  3392. { set instruction code }
  3393. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3394. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3395. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3396. bytes:=bytes or ord(insentry^.code[4]);
  3397. { set regs }
  3398. if opcode=A_VMRS then
  3399. begin
  3400. case oper[1]^.reg of
  3401. NR_FPSID: Rn:=$0;
  3402. NR_FPSCR: Rn:=$1;
  3403. NR_MVFR1: Rn:=$6;
  3404. NR_MVFR0: Rn:=$7;
  3405. NR_FPEXC: Rn:=$8;
  3406. else
  3407. Rn:=0;
  3408. message(asmw_e_invalid_opcode_and_operands);
  3409. end;
  3410. bytes:=bytes or (Rn shl 16);
  3411. if oper[0]^.reg=NR_APSR_nzcv then
  3412. bytes:=bytes or ($F shl 12)
  3413. else
  3414. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3415. end
  3416. else
  3417. begin
  3418. case oper[0]^.reg of
  3419. NR_FPSID: Rn:=$0;
  3420. NR_FPSCR: Rn:=$1;
  3421. NR_FPEXC: Rn:=$8;
  3422. else
  3423. Rn:=0;
  3424. message(asmw_e_invalid_opcode_and_operands);
  3425. end;
  3426. bytes:=bytes or (Rn shl 16);
  3427. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  3428. end;
  3429. end;
  3430. #$42,#$92: // VMUL
  3431. begin
  3432. { set instruction code }
  3433. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3434. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3435. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3436. bytes:=bytes or ord(insentry^.code[4]);
  3437. { set regs }
  3438. if ops=3 then
  3439. begin
  3440. Rd:=getmmreg(oper[0]^.reg);
  3441. Rn:=getmmreg(oper[1]^.reg);
  3442. Rm:=getmmreg(oper[2]^.reg);
  3443. end
  3444. else if oper[1]^.typ=top_const then
  3445. begin
  3446. Rd:=getmmreg(oper[0]^.reg);
  3447. Rn:=0;
  3448. Rm:=0;
  3449. end
  3450. else
  3451. begin
  3452. Rd:=getmmreg(oper[0]^.reg);
  3453. Rn:=0;
  3454. Rm:=getmmreg(oper[1]^.reg);
  3455. end;
  3456. if oppostfix=PF_F32 then
  3457. begin
  3458. D:=rd and $1; Rd:=Rd shr 1;
  3459. N:=rn and $1; Rn:=Rn shr 1;
  3460. M:=rm and $1; Rm:=Rm shr 1;
  3461. end
  3462. else
  3463. begin
  3464. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3465. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3466. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3467. bytes:=bytes or (1 shl 8);
  3468. end;
  3469. bytes:=bytes or (Rd shl 12);
  3470. bytes:=bytes or (Rn shl 16);
  3471. bytes:=bytes or (Rm shl 0);
  3472. bytes:=bytes or (D shl 22);
  3473. bytes:=bytes or (N shl 7);
  3474. bytes:=bytes or (M shl 5);
  3475. end;
  3476. #$43,#$93: // VCVT
  3477. begin
  3478. { set instruction code }
  3479. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3480. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3481. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3482. bytes:=bytes or ord(insentry^.code[4]);
  3483. { set regs }
  3484. Rd:=getmmreg(oper[0]^.reg);
  3485. Rm:=getmmreg(oper[1]^.reg);
  3486. if (ops=2) and
  3487. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3488. begin
  3489. if oppostfix=PF_F32F64 then
  3490. begin
  3491. bytes:=bytes or (1 shl 8);
  3492. D:=rd and $1; Rd:=Rd shr 1;
  3493. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3494. end
  3495. else
  3496. begin
  3497. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3498. M:=rm and $1; Rm:=Rm shr 1;
  3499. end;
  3500. bytes:=bytes and $FFF0FFFF;
  3501. bytes:=bytes or ($7 shl 16);
  3502. bytes:=bytes or (Rd shl 12);
  3503. bytes:=bytes or (Rm shl 0);
  3504. bytes:=bytes or (D shl 22);
  3505. bytes:=bytes or (M shl 5);
  3506. end
  3507. else if ops=2 then
  3508. begin
  3509. case oppostfix of
  3510. PF_S32F64,
  3511. PF_U32F64,
  3512. PF_F64S32,
  3513. PF_F64U32:
  3514. bytes:=bytes or (1 shl 8);
  3515. end;
  3516. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3517. begin
  3518. case oppostfix of
  3519. PF_S32F64,
  3520. PF_S32F32:
  3521. bytes:=bytes or (1 shl 16);
  3522. end;
  3523. bytes:=bytes or (1 shl 18);
  3524. D:=rd and $1; Rd:=Rd shr 1;
  3525. if oppostfix in [PF_S32F64,PF_U32F64] then
  3526. begin
  3527. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3528. end
  3529. else
  3530. begin
  3531. M:=rm and $1; Rm:=Rm shr 1;
  3532. end;
  3533. end
  3534. else
  3535. begin
  3536. case oppostfix of
  3537. PF_F64S32,
  3538. PF_F32S32:
  3539. bytes:=bytes or (1 shl 7);
  3540. else
  3541. bytes:=bytes and $FFFFFF7F;
  3542. end;
  3543. M:=rm and $1; Rm:=Rm shr 1;
  3544. if oppostfix in [PF_F64S32,PF_F64U32] then
  3545. begin
  3546. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3547. end
  3548. else
  3549. begin
  3550. D:=rd and $1; Rd:=Rd shr 1;
  3551. end
  3552. end;
  3553. bytes:=bytes or (Rd shl 12);
  3554. bytes:=bytes or (Rm shl 0);
  3555. bytes:=bytes or (D shl 22);
  3556. bytes:=bytes or (M shl 5);
  3557. end
  3558. else
  3559. begin
  3560. if rd<>rm then
  3561. message(asmw_e_invalid_opcode_and_operands);
  3562. case oppostfix of
  3563. PF_S32F32,PF_U32F32,
  3564. PF_F32S32,PF_F32U32,
  3565. PF_S32F64,PF_U32F64,
  3566. PF_F64S32,PF_F64U32:
  3567. begin
  3568. if not (oper[2]^.val in [1..32]) then
  3569. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3570. bytes:=bytes or (1 shl 7);
  3571. rn:=32;
  3572. end;
  3573. PF_S16F64,PF_U16F64,
  3574. PF_F64S16,PF_F64U16,
  3575. PF_S16F32,PF_U16F32,
  3576. PF_F32S16,PF_F32U16:
  3577. begin
  3578. if not (oper[2]^.val in [0..16]) then
  3579. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3580. rn:=16;
  3581. end;
  3582. else
  3583. Rn:=0;
  3584. message(asmw_e_invalid_opcode_and_operands);
  3585. end;
  3586. case oppostfix of
  3587. PF_S16F64,PF_U16F64,
  3588. PF_S32F64,PF_U32F64,
  3589. PF_F64S16,PF_F64U16,
  3590. PF_F64S32,PF_F64U32:
  3591. begin
  3592. bytes:=bytes or (1 shl 8);
  3593. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3594. end;
  3595. else
  3596. begin
  3597. D:=rd and $1; Rd:=Rd shr 1;
  3598. end;
  3599. end;
  3600. case oppostfix of
  3601. PF_U16F64,PF_U16F32,
  3602. PF_U32F32,PF_U32F64,
  3603. PF_F64U16,PF_F32U16,
  3604. PF_F32U32,PF_F64U32:
  3605. bytes:=bytes or (1 shl 16);
  3606. end;
  3607. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3608. bytes:=bytes or (1 shl 18);
  3609. bytes:=bytes or (Rd shl 12);
  3610. bytes:=bytes or (D shl 22);
  3611. rn:=rn-oper[2]^.val;
  3612. bytes:=bytes or ((rn and $1) shl 5);
  3613. bytes:=bytes or ((rn and $1E) shr 1);
  3614. end;
  3615. end;
  3616. #$44,#$94: // VLDM/VSTM/VPUSH/VPOP
  3617. begin
  3618. { set instruction code }
  3619. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3620. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3621. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3622. { set regs }
  3623. if ops=2 then
  3624. begin
  3625. if oper[0]^.typ=top_ref then
  3626. begin
  3627. Rn:=getsupreg(oper[0]^.ref^.index);
  3628. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3629. begin
  3630. { set W }
  3631. bytes:=bytes or (1 shl 21);
  3632. end
  3633. else if oppostfix = PF_DB then
  3634. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3635. end
  3636. else
  3637. begin
  3638. Rn:=getsupreg(oper[0]^.reg);
  3639. if oppostfix = PF_DB then
  3640. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3641. end;
  3642. bytes:=bytes or (Rn shl 16);
  3643. { Set PU bits }
  3644. case oppostfix of
  3645. PF_None,
  3646. PF_IA:
  3647. bytes:=bytes or (1 shl 23);
  3648. PF_DB:
  3649. bytes:=bytes or (2 shl 23);
  3650. end;
  3651. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3652. if oper[1]^.regset^=[] then
  3653. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3654. rd:=0;
  3655. for r:=0 to 31 do
  3656. if r in oper[1]^.regset^ then
  3657. begin
  3658. rd:=r;
  3659. break;
  3660. end;
  3661. rn:=32-rd;
  3662. for r:=rd+1 to 31 do
  3663. if not(r in oper[1]^.regset^) then
  3664. begin
  3665. rn:=r-rd;
  3666. break;
  3667. end;
  3668. if dp_operation then
  3669. begin
  3670. bytes:=bytes or (1 shl 8);
  3671. bytes:=bytes or (rn*2);
  3672. bytes:=bytes or ((rd and $F) shl 12);
  3673. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3674. end
  3675. else
  3676. begin
  3677. bytes:=bytes or rn;
  3678. bytes:=bytes or ((rd and $1) shl 22);
  3679. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3680. end;
  3681. end
  3682. else { VPUSH/VPOP }
  3683. begin
  3684. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3685. if oper[0]^.regset^=[] then
  3686. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3687. rd:=0;
  3688. for r:=0 to 31 do
  3689. if r in oper[0]^.regset^ then
  3690. begin
  3691. rd:=r;
  3692. break;
  3693. end;
  3694. rn:=32-rd;
  3695. for r:=rd+1 to 31 do
  3696. if not(r in oper[0]^.regset^) then
  3697. begin
  3698. rn:=r-rd;
  3699. break;
  3700. end;
  3701. if dp_operation then
  3702. begin
  3703. bytes:=bytes or (1 shl 8);
  3704. bytes:=bytes or (rn*2);
  3705. bytes:=bytes or ((rd and $F) shl 12);
  3706. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3707. end
  3708. else
  3709. begin
  3710. bytes:=bytes or rn;
  3711. bytes:=bytes or ((rd and $1) shl 22);
  3712. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3713. end;
  3714. end;
  3715. end;
  3716. #$45,#$95: // VLDR/VSTR
  3717. begin
  3718. { set instruction code }
  3719. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3720. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3721. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3722. { set regs }
  3723. rd:=getmmreg(oper[0]^.reg);
  3724. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3725. begin
  3726. bytes:=bytes or (1 shl 8);
  3727. bytes:=bytes or ((rd and $F) shl 12);
  3728. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3729. end
  3730. else
  3731. begin
  3732. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3733. bytes:=bytes or ((rd and $1) shl 22);
  3734. end;
  3735. { set ref }
  3736. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3737. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3738. begin
  3739. { set offset }
  3740. offset:=0;
  3741. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3742. if assigned(currsym) then
  3743. offset:=currsym.offset-insoffset-8;
  3744. offset:=offset+oper[1]^.ref^.offset;
  3745. offset:=offset div 4;
  3746. if offset>=0 then
  3747. begin
  3748. { set U flag }
  3749. bytes:=bytes or (1 shl 23);
  3750. bytes:=bytes or offset
  3751. end
  3752. else
  3753. begin
  3754. offset:=-offset;
  3755. bytes:=bytes or offset
  3756. end;
  3757. end
  3758. else
  3759. message(asmw_e_invalid_opcode_and_operands);
  3760. end;
  3761. #$60: { Thumb }
  3762. begin
  3763. bytelen:=2;
  3764. bytes:=0;
  3765. { set opcode }
  3766. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3767. bytes:=bytes or ord(insentry^.code[2]);
  3768. { set regs }
  3769. if ops=2 then
  3770. begin
  3771. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3772. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3773. if (oper[1]^.typ=top_reg) then
  3774. bytes:=bytes or ((getsupreg(oper[1]^.reg) and $7) shl 6)
  3775. else
  3776. bytes:=bytes or ((oper[1]^.val and $1F) shl 6);
  3777. end
  3778. else if ops=3 then
  3779. begin
  3780. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3781. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3782. if (oper[2]^.typ=top_reg) then
  3783. bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6)
  3784. else
  3785. bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
  3786. end
  3787. else if ops=1 then
  3788. begin
  3789. if oper[0]^.typ=top_const then
  3790. bytes:=bytes or (oper[0]^.val and $FF);
  3791. end;
  3792. end;
  3793. #$61: { Thumb }
  3794. begin
  3795. bytelen:=2;
  3796. bytes:=0;
  3797. { set opcode }
  3798. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3799. bytes:=bytes or ord(insentry^.code[2]);
  3800. { set regs }
  3801. if ops=2 then
  3802. begin
  3803. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3804. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3805. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3806. end
  3807. else if ops=1 then
  3808. begin
  3809. if oper[0]^.typ=top_const then
  3810. bytes:=bytes or (oper[0]^.val and $FF);
  3811. end;
  3812. end;
  3813. #$62..#$63: { Thumb branches }
  3814. begin
  3815. bytelen:=2;
  3816. bytes:=0;
  3817. { set opcode }
  3818. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3819. bytes:=bytes or ord(insentry^.code[2]);
  3820. if insentry^.code[0]=#$63 then
  3821. bytes:=bytes or (CondVal[condition] shl 8);
  3822. if oper[0]^.typ=top_const then
  3823. begin
  3824. if insentry^.code[0]=#$63 then
  3825. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
  3826. else
  3827. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
  3828. end
  3829. else if oper[0]^.typ=top_reg then
  3830. begin
  3831. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3832. end
  3833. else if oper[0]^.typ=top_ref then
  3834. begin
  3835. offset:=0;
  3836. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3837. if assigned(currsym) then
  3838. offset:=currsym.offset-insoffset-8;
  3839. offset:=offset+oper[0]^.ref^.offset;
  3840. if insentry^.code[0]=#$63 then
  3841. bytes:=bytes or (((offset+4) shr 1) and $FF)
  3842. else
  3843. bytes:=bytes or (((offset+4) shr 1) and $7FF);
  3844. end
  3845. end;
  3846. #$64: { Thumb: Special encodings }
  3847. begin
  3848. bytelen:=2;
  3849. bytes:=0;
  3850. { set opcode }
  3851. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3852. bytes:=bytes or ord(insentry^.code[2]);
  3853. case opcode of
  3854. A_SUB:
  3855. begin
  3856. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3857. if (ops=3) and
  3858. (oper[2]^.typ=top_const) then
  3859. bytes:=bytes or ((oper[2]^.val shr 2) and $7F)
  3860. else if (ops=2) and
  3861. (oper[1]^.typ=top_const) then
  3862. bytes:=bytes or ((oper[1]^.val shr 2) and $7F);
  3863. end;
  3864. A_MUL:
  3865. if (ops in [2,3]) 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. A_ADD:
  3871. begin
  3872. if ops=2 then
  3873. begin
  3874. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3875. bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
  3876. end
  3877. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3878. (oper[2]^.typ=top_const) then
  3879. begin
  3880. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
  3881. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3882. end
  3883. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3884. (oper[2]^.typ=top_reg) then
  3885. begin
  3886. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3887. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3888. end
  3889. else
  3890. begin
  3891. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3892. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3893. end;
  3894. end;
  3895. end;
  3896. end;
  3897. #$65: { Thumb load/store }
  3898. begin
  3899. bytelen:=2;
  3900. bytes:=0;
  3901. { set opcode }
  3902. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3903. bytes:=bytes or ord(insentry^.code[2]);
  3904. { set regs }
  3905. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3906. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3907. bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
  3908. end;
  3909. #$66: { Thumb load/store }
  3910. begin
  3911. bytelen:=2;
  3912. bytes:=0;
  3913. { set opcode }
  3914. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3915. bytes:=bytes or ord(insentry^.code[2]);
  3916. { set regs }
  3917. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3918. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3919. bytes:=bytes or (((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $1F) shl 6);
  3920. end;
  3921. #$67: { Thumb load/store }
  3922. begin
  3923. bytelen:=2;
  3924. bytes:=0;
  3925. { set opcode }
  3926. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3927. bytes:=bytes or ord(insentry^.code[2]);
  3928. { set regs }
  3929. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3930. if oper[1]^.typ=top_ref then
  3931. bytes:=bytes or ((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $FF)
  3932. else
  3933. bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
  3934. end;
  3935. #$68: { Thumb CB[N]Z }
  3936. begin
  3937. bytelen:=2;
  3938. bytes:=0;
  3939. { set opcode }
  3940. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3941. { set opers }
  3942. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3943. if oper[1]^.typ=top_ref then
  3944. begin
  3945. offset:=0;
  3946. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3947. if assigned(currsym) then
  3948. offset:=currsym.offset-insoffset-8;
  3949. offset:=offset+oper[1]^.ref^.offset;
  3950. offset:=offset div 2;
  3951. end
  3952. else
  3953. offset:=oper[1]^.val div 2;
  3954. bytes:=bytes or ((offset) and $1F) shl 3;
  3955. bytes:=bytes or ((offset shr 5) and 1) shl 9;
  3956. end;
  3957. #$69: { Thumb: Push/Pop/Stm/Ldm }
  3958. begin
  3959. bytelen:=2;
  3960. bytes:=0;
  3961. { set opcode }
  3962. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3963. case opcode of
  3964. A_PUSH:
  3965. begin
  3966. for r:=0 to 7 do
  3967. if r in oper[0]^.regset^ then
  3968. bytes:=bytes or (1 shl r);
  3969. if RS_R14 in oper[0]^.regset^ then
  3970. bytes:=bytes or (1 shl 8);
  3971. end;
  3972. A_POP:
  3973. begin
  3974. for r:=0 to 7 do
  3975. if r in oper[0]^.regset^ then
  3976. bytes:=bytes or (1 shl r);
  3977. if RS_R15 in oper[0]^.regset^ then
  3978. bytes:=bytes or (1 shl 8);
  3979. end;
  3980. A_STM:
  3981. begin
  3982. for r:=0 to 7 do
  3983. if r in oper[1]^.regset^ then
  3984. bytes:=bytes or (1 shl r);
  3985. if oper[0]^.typ=top_ref then
  3986. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  3987. else
  3988. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3989. end;
  3990. A_LDM:
  3991. begin
  3992. for r:=0 to 7 do
  3993. if r in oper[1]^.regset^ then
  3994. bytes:=bytes or (1 shl r);
  3995. if oper[0]^.typ=top_ref then
  3996. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  3997. else
  3998. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3999. end;
  4000. end;
  4001. end;
  4002. #$6A: { Thumb: IT }
  4003. begin
  4004. bytelen:=2;
  4005. bytes:=0;
  4006. { set opcode }
  4007. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4008. bytes:=bytes or (ord(insentry^.code[2]) shl 0);
  4009. bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
  4010. i_field:=(bytes shr 4) and 1;
  4011. i_field:=(i_field shl 1) or i_field;
  4012. i_field:=(i_field shl 2) or i_field;
  4013. bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
  4014. end;
  4015. #$6B: { Thumb: Data processing (misc) }
  4016. begin
  4017. bytelen:=2;
  4018. bytes:=0;
  4019. { set opcode }
  4020. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4021. bytes:=bytes or ord(insentry^.code[2]);
  4022. { set regs }
  4023. if ops>=2 then
  4024. begin
  4025. if oper[1]^.typ=top_const then
  4026. begin
  4027. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
  4028. bytes:=bytes or (oper[1]^.val and $FF);
  4029. end
  4030. else if oper[1]^.typ=top_reg then
  4031. begin
  4032. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4033. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4034. end;
  4035. end
  4036. else if ops=1 then
  4037. begin
  4038. if oper[0]^.typ=top_const then
  4039. bytes:=bytes or (oper[0]^.val and $FF);
  4040. end;
  4041. end;
  4042. #$80: { Thumb-2: Dataprocessing }
  4043. begin
  4044. bytes:=0;
  4045. { set instruction code }
  4046. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4047. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4048. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4049. bytes:=bytes or ord(insentry^.code[4]);
  4050. if ops=1 then
  4051. begin
  4052. if oper[0]^.typ=top_reg then
  4053. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4054. else if oper[0]^.typ=top_const then
  4055. bytes:=bytes or (oper[0]^.val and $F);
  4056. end
  4057. else if (ops=2) and
  4058. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4059. begin
  4060. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4061. if oper[1]^.typ=top_const then
  4062. encodethumbimm(oper[1]^.val)
  4063. else if oper[1]^.typ=top_reg then
  4064. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4065. end
  4066. else if (ops=3) and
  4067. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4068. begin
  4069. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4070. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4071. if oper[2]^.typ=top_shifterop then
  4072. setthumbshift(2)
  4073. else if oper[2]^.typ=top_reg then
  4074. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
  4075. end
  4076. else if (ops=2) and
  4077. (opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
  4078. begin
  4079. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4080. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4081. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4082. end
  4083. else if ops=2 then
  4084. begin
  4085. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4086. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4087. if oper[1]^.typ=top_const then
  4088. encodethumbimm(oper[1]^.val)
  4089. else if oper[1]^.typ=top_reg then
  4090. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4091. end
  4092. else if ops=3 then
  4093. begin
  4094. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4095. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4096. if oper[2]^.typ=top_const then
  4097. encodethumbimm(oper[2]^.val)
  4098. else if oper[2]^.typ=top_reg then
  4099. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4100. end
  4101. else if ops=4 then
  4102. begin
  4103. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4104. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4105. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4106. if oper[3]^.typ=top_shifterop then
  4107. setthumbshift(3)
  4108. else if oper[3]^.typ=top_reg then
  4109. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
  4110. end;
  4111. if oppostfix=PF_S then
  4112. bytes:=bytes or (1 shl 20)
  4113. else if oppostfix=PF_X then
  4114. bytes:=bytes or (1 shl 4)
  4115. else if oppostfix=PF_R then
  4116. bytes:=bytes or (1 shl 4);
  4117. end;
  4118. #$81: { Thumb-2: Dataprocessing misc }
  4119. begin
  4120. bytes:=0;
  4121. { set instruction code }
  4122. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4123. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4124. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4125. bytes:=bytes or ord(insentry^.code[4]);
  4126. if ops=3 then
  4127. begin
  4128. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4129. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4130. if oper[2]^.typ=top_const then
  4131. begin
  4132. bytes:=bytes or (oper[2]^.val and $FF);
  4133. bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
  4134. bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
  4135. end;
  4136. end
  4137. else if ops=2 then
  4138. begin
  4139. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4140. offset:=0;
  4141. if oper[1]^.typ=top_const then
  4142. begin
  4143. offset:=oper[1]^.val;
  4144. end
  4145. else if oper[1]^.typ=top_ref then
  4146. begin
  4147. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4148. if assigned(currsym) then
  4149. offset:=currsym.offset-insoffset-8;
  4150. offset:=offset+oper[1]^.ref^.offset;
  4151. offset:=offset;
  4152. end;
  4153. bytes:=bytes or (offset and $FF);
  4154. bytes:=bytes or ((offset and $700) shr 8) shl 12;
  4155. bytes:=bytes or ((offset and $800) shr 11) shl 26;
  4156. bytes:=bytes or ((offset and $F000) shr 12) shl 16;
  4157. end;
  4158. if oppostfix=PF_S then
  4159. bytes:=bytes or (1 shl 20);
  4160. end;
  4161. #$82: { Thumb-2: Shifts }
  4162. begin
  4163. bytes:=0;
  4164. { set instruction code }
  4165. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4166. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4167. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4168. bytes:=bytes or ord(insentry^.code[4]);
  4169. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4170. if oper[1]^.typ=top_reg then
  4171. begin
  4172. offset:=2;
  4173. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4174. end
  4175. else
  4176. begin
  4177. offset:=1;
  4178. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
  4179. end;
  4180. if oper[offset]^.typ=top_const then
  4181. begin
  4182. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4183. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4184. end
  4185. else if oper[offset]^.typ=top_reg then
  4186. bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
  4187. if (ops>=(offset+2)) and
  4188. (oper[offset+1]^.typ=top_const) then
  4189. bytes:=bytes or (oper[offset+1]^.val and $1F);
  4190. if oppostfix=PF_S then
  4191. bytes:=bytes or (1 shl 20);
  4192. end;
  4193. #$84: { Thumb-2: Shifts(width-1) }
  4194. begin
  4195. bytes:=0;
  4196. { set instruction code }
  4197. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4198. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4199. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4200. bytes:=bytes or ord(insentry^.code[4]);
  4201. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4202. if oper[1]^.typ=top_reg then
  4203. begin
  4204. offset:=2;
  4205. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4206. end
  4207. else
  4208. offset:=1;
  4209. if oper[offset]^.typ=top_const then
  4210. begin
  4211. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4212. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4213. end;
  4214. if (ops>=(offset+2)) and
  4215. (oper[offset+1]^.typ=top_const) then
  4216. begin
  4217. if opcode in [A_BFI,A_BFC] then
  4218. i_field:=oper[offset+1]^.val+oper[offset]^.val-1
  4219. else
  4220. i_field:=oper[offset+1]^.val-1;
  4221. bytes:=bytes or (i_field and $1F);
  4222. end;
  4223. if oppostfix=PF_S then
  4224. bytes:=bytes or (1 shl 20);
  4225. end;
  4226. #$83: { Thumb-2: Saturation }
  4227. begin
  4228. bytes:=0;
  4229. { set instruction code }
  4230. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4231. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4232. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4233. bytes:=bytes or ord(insentry^.code[4]);
  4234. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4235. bytes:=bytes or (oper[1]^.val and $1F);
  4236. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4237. if ops=4 then
  4238. setthumbshift(3,true);
  4239. end;
  4240. #$85: { Thumb-2: Long multiplications }
  4241. begin
  4242. bytes:=0;
  4243. { set instruction code }
  4244. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4245. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4246. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4247. bytes:=bytes or ord(insentry^.code[4]);
  4248. if ops=4 then
  4249. begin
  4250. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4251. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
  4252. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4253. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
  4254. end;
  4255. if oppostfix=PF_S then
  4256. bytes:=bytes or (1 shl 20)
  4257. else if oppostfix=PF_X then
  4258. bytes:=bytes or (1 shl 4);
  4259. end;
  4260. #$86: { Thumb-2: Extension ops }
  4261. begin
  4262. bytes:=0;
  4263. { set instruction code }
  4264. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4265. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4266. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4267. bytes:=bytes or ord(insentry^.code[4]);
  4268. if ops=2 then
  4269. begin
  4270. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4271. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4272. end
  4273. else if ops=3 then
  4274. begin
  4275. if oper[2]^.typ=top_shifterop then
  4276. begin
  4277. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4278. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4279. bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
  4280. end
  4281. else
  4282. begin
  4283. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4284. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4285. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4286. end;
  4287. end
  4288. else if ops=4 then
  4289. begin
  4290. if oper[3]^.typ=top_shifterop then
  4291. begin
  4292. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4293. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4294. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4295. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
  4296. end;
  4297. end;
  4298. end;
  4299. #$87: { Thumb-2: PLD/PLI }
  4300. begin
  4301. { set instruction code }
  4302. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4303. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4304. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4305. bytes:=bytes or ord(insentry^.code[4]);
  4306. { set Rn and Rd }
  4307. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4308. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4309. begin
  4310. { set offset }
  4311. offset:=0;
  4312. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4313. if assigned(currsym) then
  4314. offset:=currsym.offset-insoffset-8;
  4315. offset:=offset+oper[0]^.ref^.offset;
  4316. if offset>=0 then
  4317. begin
  4318. { set U flag }
  4319. bytes:=bytes or (1 shl 23);
  4320. bytes:=bytes or (offset and $FFF);
  4321. end
  4322. else
  4323. begin
  4324. bytes:=bytes or ($3 shl 10);
  4325. offset:=-offset;
  4326. bytes:=bytes or (offset and $FF);
  4327. end;
  4328. end
  4329. else
  4330. begin
  4331. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4332. { set shift }
  4333. with oper[0]^.ref^ do
  4334. if shiftmode=SM_LSL then
  4335. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4336. end;
  4337. end;
  4338. #$88: { Thumb-2: LDR/STR }
  4339. begin
  4340. { set instruction code }
  4341. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4342. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4343. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4344. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4345. { set Rn and Rd }
  4346. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4347. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4348. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4349. begin
  4350. { set offset }
  4351. offset:=0;
  4352. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4353. if assigned(currsym) then
  4354. offset:=currsym.offset-insoffset-8;
  4355. offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
  4356. if offset>=0 then
  4357. begin
  4358. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4359. bytes:=bytes or (1 shl 23);
  4360. { set U flag }
  4361. if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
  4362. bytes:=bytes or (1 shl 9);
  4363. bytes:=bytes or offset
  4364. end
  4365. else
  4366. begin
  4367. bytes:=bytes or (1 shl 11);
  4368. offset:=-offset;
  4369. bytes:=bytes or offset
  4370. end;
  4371. end
  4372. else
  4373. begin
  4374. { set I flag }
  4375. bytes:=bytes or (1 shl 25);
  4376. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  4377. { set shift }
  4378. with oper[1]^.ref^ do
  4379. if shiftmode<>SM_None then
  4380. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4381. end;
  4382. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4383. begin
  4384. { set W bit }
  4385. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4386. bytes:=bytes or (1 shl 8);
  4387. { set P bit if necessary }
  4388. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  4389. bytes:=bytes or (1 shl 10);
  4390. end;
  4391. end;
  4392. #$89: { Thumb-2: LDRD/STRD }
  4393. begin
  4394. { set instruction code }
  4395. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4396. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4397. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4398. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4399. { set Rn and Rd }
  4400. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4401. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4402. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4403. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4404. begin
  4405. { set offset }
  4406. offset:=0;
  4407. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4408. if assigned(currsym) then
  4409. offset:=currsym.offset-insoffset-8;
  4410. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4411. if offset>=0 then
  4412. begin
  4413. { set U flag }
  4414. bytes:=bytes or (1 shl 23);
  4415. bytes:=bytes or offset
  4416. end
  4417. else
  4418. begin
  4419. offset:=-offset;
  4420. bytes:=bytes or offset
  4421. end;
  4422. end
  4423. else
  4424. begin
  4425. message(asmw_e_invalid_opcode_and_operands);
  4426. end;
  4427. { set W bit }
  4428. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4429. bytes:=bytes or (1 shl 21);
  4430. { set P bit if necessary }
  4431. if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
  4432. bytes:=bytes or (1 shl 24);
  4433. end;
  4434. #$8A: { Thumb-2: LDREX }
  4435. begin
  4436. { set instruction code }
  4437. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4438. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4439. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4440. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4441. { set Rn and Rd }
  4442. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4443. if (ops=2) and (opcode in [A_LDREX]) then
  4444. begin
  4445. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4446. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4447. begin
  4448. { set offset }
  4449. offset:=0;
  4450. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4451. if assigned(currsym) then
  4452. offset:=currsym.offset-insoffset-8;
  4453. offset:=(offset+oper[1]^.ref^.offset) div 4;
  4454. if offset>=0 then
  4455. begin
  4456. bytes:=bytes or offset
  4457. end
  4458. else
  4459. begin
  4460. message(asmw_e_invalid_opcode_and_operands);
  4461. end;
  4462. end
  4463. else
  4464. begin
  4465. message(asmw_e_invalid_opcode_and_operands);
  4466. end;
  4467. end
  4468. else if (ops=2) then
  4469. begin
  4470. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4471. end
  4472. else
  4473. begin
  4474. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4475. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4476. end;
  4477. end;
  4478. #$8B: { Thumb-2: STREX }
  4479. begin
  4480. { set instruction code }
  4481. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4482. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4483. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4484. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4485. { set Rn and Rd }
  4486. if (ops=3) and (opcode in [A_STREX]) then
  4487. begin
  4488. bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
  4489. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4490. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4491. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4492. begin
  4493. { set offset }
  4494. offset:=0;
  4495. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4496. if assigned(currsym) then
  4497. offset:=currsym.offset-insoffset-8;
  4498. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4499. if offset>=0 then
  4500. begin
  4501. bytes:=bytes or offset
  4502. end
  4503. else
  4504. begin
  4505. message(asmw_e_invalid_opcode_and_operands);
  4506. end;
  4507. end
  4508. else
  4509. begin
  4510. message(asmw_e_invalid_opcode_and_operands);
  4511. end;
  4512. end
  4513. else if (ops=3) then
  4514. begin
  4515. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4516. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4517. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4518. end
  4519. else
  4520. begin
  4521. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4522. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4523. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  4524. bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
  4525. end;
  4526. end;
  4527. #$8C: { Thumb-2: LDM/STM }
  4528. begin
  4529. { set instruction code }
  4530. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4531. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4532. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4533. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4534. if oper[0]^.typ=top_reg then
  4535. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4536. else
  4537. begin
  4538. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
  4539. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  4540. bytes:=bytes or (1 shl 21);
  4541. end;
  4542. for r:=0 to 15 do
  4543. if r in oper[1]^.regset^ then
  4544. bytes:=bytes or (1 shl r);
  4545. case oppostfix of
  4546. PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
  4547. PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
  4548. end;
  4549. end;
  4550. #$8D: { Thumb-2: BL/BLX }
  4551. begin
  4552. { set instruction code }
  4553. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4554. bytes:=bytes or (ord(insentry^.code[2]) shl 8);
  4555. { set offset }
  4556. if oper[0]^.typ=top_const then
  4557. offset:=(oper[0]^.val shr 1) and $FFFFFF
  4558. else
  4559. begin
  4560. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4561. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  4562. begin
  4563. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  4564. offset:=$FFFFFE
  4565. end
  4566. else
  4567. offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
  4568. end;
  4569. bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
  4570. bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
  4571. bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
  4572. bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
  4573. bytes:=bytes or ((offset shr 23) and $1) shl 26;
  4574. end;
  4575. #$8E: { Thumb-2: TBB/TBH }
  4576. begin
  4577. { set instruction code }
  4578. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4579. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4580. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4581. bytes:=bytes or ord(insentry^.code[4]);
  4582. { set Rn and Rm }
  4583. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4584. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4585. message(asmw_e_invalid_effective_address)
  4586. else
  4587. begin
  4588. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4589. if (opcode=A_TBH) and
  4590. (oper[0]^.ref^.shiftmode<>SM_LSL) and
  4591. (oper[0]^.ref^.shiftimm<>1) then
  4592. message(asmw_e_invalid_effective_address);
  4593. end;
  4594. end;
  4595. #$A0: { FPA: CPDT(LDF/STF) }
  4596. begin
  4597. { set instruction code }
  4598. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4599. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4600. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4601. bytes:=bytes or ord(insentry^.code[4]);
  4602. if ops=2 then
  4603. begin
  4604. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4605. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4606. bytes:=bytes or ((oper[1]^.ref^.offset shr 2) and $FF);
  4607. if oper[1]^.ref^.offset>=0 then
  4608. bytes:=bytes or (1 shl 23);
  4609. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4610. bytes:=bytes or (1 shl 21);
  4611. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  4612. bytes:=bytes or (1 shl 24);
  4613. case oppostfix of
  4614. PF_D: bytes:=bytes or (0 shl 22) or (1 shl 15);
  4615. PF_E: bytes:=bytes or (1 shl 22) or (0 shl 15);
  4616. PF_P: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4617. end;
  4618. end
  4619. else
  4620. begin
  4621. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4622. case oper[1]^.val of
  4623. 1: bytes:=bytes or (1 shl 15);
  4624. 2: bytes:=bytes or (1 shl 22);
  4625. 3: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4626. 4: ;
  4627. else
  4628. message1(asmw_e_invalid_opcode_and_operands, 'Invalid count for LFM/SFM');
  4629. end;
  4630. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4631. bytes:=bytes or ((oper[2]^.ref^.offset shr 2) and $FF);
  4632. if oper[2]^.ref^.offset>=0 then
  4633. bytes:=bytes or (2 shl 23);
  4634. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4635. bytes:=bytes or (1 shl 21);
  4636. if oper[2]^.ref^.addressmode=AM_PREINDEXED then
  4637. bytes:=bytes or (1 shl 24);
  4638. end;
  4639. end;
  4640. #$A1: { FPA: CPDO }
  4641. begin
  4642. { set instruction code }
  4643. bytes:=bytes or ($E shl 24);
  4644. bytes:=bytes or (ord(insentry^.code[1]) shl 15);
  4645. bytes:=bytes or ((ord(insentry^.code[2]) shr 1) shl 20);
  4646. bytes:=bytes or (1 shl 8);
  4647. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4648. if ops=2 then
  4649. begin
  4650. if oper[1]^.typ=top_reg then
  4651. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4652. else
  4653. case oper[1]^.val of
  4654. 0: bytes:=bytes or $8;
  4655. 1: bytes:=bytes or $9;
  4656. 2: bytes:=bytes or $A;
  4657. 3: bytes:=bytes or $B;
  4658. 4: bytes:=bytes or $C;
  4659. 5: bytes:=bytes or $D;
  4660. //0.5: bytes:=bytes or $E;
  4661. 10: bytes:=bytes or $F;
  4662. else
  4663. Message(asmw_e_invalid_opcode_and_operands);
  4664. end;
  4665. end
  4666. else
  4667. begin
  4668. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  4669. if oper[2]^.typ=top_reg then
  4670. bytes:=bytes or getsupreg(oper[2]^.reg) shl 0
  4671. else
  4672. case oper[2]^.val of
  4673. 0: bytes:=bytes or $8;
  4674. 1: bytes:=bytes or $9;
  4675. 2: bytes:=bytes or $A;
  4676. 3: bytes:=bytes or $B;
  4677. 4: bytes:=bytes or $C;
  4678. 5: bytes:=bytes or $D;
  4679. //0.5: bytes:=bytes or $E;
  4680. 10: bytes:=bytes or $F;
  4681. else
  4682. Message(asmw_e_invalid_opcode_and_operands);
  4683. end;
  4684. end;
  4685. case roundingmode of
  4686. RM_P: bytes:=bytes or (1 shl 5);
  4687. RM_M: bytes:=bytes or (2 shl 5);
  4688. RM_Z: bytes:=bytes or (3 shl 5);
  4689. end;
  4690. case oppostfix of
  4691. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4692. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4693. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4694. else
  4695. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4696. end;
  4697. end;
  4698. #$A2: { FPA: CPDO }
  4699. begin
  4700. { set instruction code }
  4701. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4702. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4703. bytes:=bytes or ($11 shl 4);
  4704. case opcode of
  4705. A_FLT:
  4706. begin
  4707. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4708. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  4709. case roundingmode of
  4710. RM_P: bytes:=bytes or (1 shl 5);
  4711. RM_M: bytes:=bytes or (2 shl 5);
  4712. RM_Z: bytes:=bytes or (3 shl 5);
  4713. end;
  4714. case oppostfix of
  4715. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4716. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4717. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4718. else
  4719. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4720. end;
  4721. end;
  4722. A_FIX:
  4723. begin
  4724. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4725. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4726. case roundingmode of
  4727. RM_P: bytes:=bytes or (1 shl 5);
  4728. RM_M: bytes:=bytes or (2 shl 5);
  4729. RM_Z: bytes:=bytes or (3 shl 5);
  4730. end;
  4731. end;
  4732. A_WFS,A_RFS,A_WFC,A_RFC:
  4733. begin
  4734. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4735. end;
  4736. A_CMF,A_CNF,A_CMFE,A_CNFE:
  4737. begin
  4738. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4739. if oper[1]^.typ=top_reg then
  4740. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4741. else
  4742. case oper[1]^.val of
  4743. 0: bytes:=bytes or $8;
  4744. 1: bytes:=bytes or $9;
  4745. 2: bytes:=bytes or $A;
  4746. 3: bytes:=bytes or $B;
  4747. 4: bytes:=bytes or $C;
  4748. 5: bytes:=bytes or $D;
  4749. //0.5: bytes:=bytes or $E;
  4750. 10: bytes:=bytes or $F;
  4751. else
  4752. Message(asmw_e_invalid_opcode_and_operands);
  4753. end;
  4754. end;
  4755. end;
  4756. end;
  4757. #$fe: // No written data
  4758. begin
  4759. exit;
  4760. end;
  4761. #$ff:
  4762. internalerror(2005091101);
  4763. else
  4764. begin
  4765. writeln(ord(insentry^.code[0]), ' - ', opcode);
  4766. internalerror(2005091102);
  4767. end;
  4768. end;
  4769. { Todo: Decide whether the code above should take care of writing data in an order that makes senes }
  4770. if (insentry^.code[0] in [#$80..#$95]) and (bytelen=4) then
  4771. bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
  4772. { we're finished, write code }
  4773. objdata.writebytes(bytes,bytelen);
  4774. end;
  4775. begin
  4776. cai_align:=tai_align;
  4777. end.