aasmcpu.pas 184 KB

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