aasmcpu.pas 184 KB

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