aasmcpu.pas 213 KB

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