aasmcpu.pas 202 KB

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