aasmcpu.pas 214 KB

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