aasmcpu.pas 214 KB

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