aasmcpu.pas 200 KB

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