aasmcpu.pas 212 KB

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