aasmcpu.pas 214 KB

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