aasmcpu.pas 198 KB

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