aasmcpu.pas 217 KB

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