aasmcpu.pas 202 KB

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