aasmcpu.pas 202 KB

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