aasmcpu.pas 205 KB

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