aasmcpu.pas 196 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356
  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. A_SWI:
  1373. begin
  1374. taicpu(curtai).opcode:=A_SVC;
  1375. end;
  1376. end;
  1377. end;
  1378. end;
  1379. curtai:=tai(curtai.Next);
  1380. end;
  1381. end;
  1382. procedure finalizearmcode(list, listtoinsert: TAsmList);
  1383. begin
  1384. expand_instructions(list);
  1385. { Do Thumb-2 16bit -> 32bit transformations }
  1386. if GenerateThumb2Code then
  1387. begin
  1388. ensurethumbencodings(list);
  1389. ensurethumb2encodings(list);
  1390. foldITInstructions(list);
  1391. end
  1392. else if GenerateThumbCode then
  1393. ensurethumbencodings(list);
  1394. gather_it_info(list);
  1395. fix_invalid_imms(list);
  1396. insertpcrelativedata(list, listtoinsert);
  1397. end;
  1398. procedure InsertPData;
  1399. var
  1400. prolog: TAsmList;
  1401. begin
  1402. prolog:=TAsmList.create;
  1403. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(pint),secorder_begin);
  1404. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  1405. prolog.concat(Tai_const.Create_32bit(0));
  1406. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  1407. { dummy function }
  1408. prolog.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14));
  1409. current_asmdata.asmlists[al_start].insertList(prolog);
  1410. prolog.Free;
  1411. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(pint));
  1412. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  1413. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit(longint($ffffff01)));
  1414. end;
  1415. (*
  1416. Floating point instruction format information, taken from the linux kernel
  1417. ARM Floating Point Instruction Classes
  1418. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1419. |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
  1420. |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)
  1421. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1422. |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
  1423. |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
  1424. |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
  1425. | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
  1426. CPDT data transfer instructions
  1427. LDF, STF, LFM (copro 2), SFM (copro 2)
  1428. CPDO dyadic arithmetic instructions
  1429. ADF, MUF, SUF, RSF, DVF, RDF,
  1430. POW, RPW, RMF, FML, FDV, FRD, POL
  1431. CPDO monadic arithmetic instructions
  1432. MVF, MNF, ABS, RND, SQT, LOG, LGN, EXP,
  1433. SIN, COS, TAN, ASN, ACS, ATN, URD, NRM
  1434. CPRT joint arithmetic/data transfer instructions
  1435. FIX (arithmetic followed by load/store)
  1436. FLT (load/store followed by arithmetic)
  1437. CMF, CNF CMFE, CNFE (comparisons)
  1438. WFS, RFS (write/read floating point status register)
  1439. WFC, RFC (write/read floating point control register)
  1440. cond condition codes
  1441. P pre/post index bit: 0 = postindex, 1 = preindex
  1442. U up/down bit: 0 = stack grows down, 1 = stack grows up
  1443. W write back bit: 1 = update base register (Rn)
  1444. L load/store bit: 0 = store, 1 = load
  1445. Rn base register
  1446. Rd destination/source register
  1447. Fd floating point destination register
  1448. Fn floating point source register
  1449. Fm floating point source register or floating point constant
  1450. uv transfer length (TABLE 1)
  1451. wx register count (TABLE 2)
  1452. abcd arithmetic opcode (TABLES 3 & 4)
  1453. ef destination size (rounding precision) (TABLE 5)
  1454. gh rounding mode (TABLE 6)
  1455. j dyadic/monadic bit: 0 = dyadic, 1 = monadic
  1456. i constant bit: 1 = constant (TABLE 6)
  1457. */
  1458. /*
  1459. TABLE 1
  1460. +-------------------------+---+---+---------+---------+
  1461. | Precision | u | v | FPSR.EP | length |
  1462. +-------------------------+---+---+---------+---------+
  1463. | Single | 0 | 0 | x | 1 words |
  1464. | Double | 1 | 1 | x | 2 words |
  1465. | Extended | 1 | 1 | x | 3 words |
  1466. | Packed decimal | 1 | 1 | 0 | 3 words |
  1467. | Expanded packed decimal | 1 | 1 | 1 | 4 words |
  1468. +-------------------------+---+---+---------+---------+
  1469. Note: x = don't care
  1470. */
  1471. /*
  1472. TABLE 2
  1473. +---+---+---------------------------------+
  1474. | w | x | Number of registers to transfer |
  1475. +---+---+---------------------------------+
  1476. | 0 | 1 | 1 |
  1477. | 1 | 0 | 2 |
  1478. | 1 | 1 | 3 |
  1479. | 0 | 0 | 4 |
  1480. +---+---+---------------------------------+
  1481. */
  1482. /*
  1483. TABLE 3: Dyadic Floating Point Opcodes
  1484. +---+---+---+---+----------+-----------------------+-----------------------+
  1485. | a | b | c | d | Mnemonic | Description | Operation |
  1486. +---+---+---+---+----------+-----------------------+-----------------------+
  1487. | 0 | 0 | 0 | 0 | ADF | Add | Fd := Fn + Fm |
  1488. | 0 | 0 | 0 | 1 | MUF | Multiply | Fd := Fn * Fm |
  1489. | 0 | 0 | 1 | 0 | SUF | Subtract | Fd := Fn - Fm |
  1490. | 0 | 0 | 1 | 1 | RSF | Reverse subtract | Fd := Fm - Fn |
  1491. | 0 | 1 | 0 | 0 | DVF | Divide | Fd := Fn / Fm |
  1492. | 0 | 1 | 0 | 1 | RDF | Reverse divide | Fd := Fm / Fn |
  1493. | 0 | 1 | 1 | 0 | POW | Power | Fd := Fn ^ Fm |
  1494. | 0 | 1 | 1 | 1 | RPW | Reverse power | Fd := Fm ^ Fn |
  1495. | 1 | 0 | 0 | 0 | RMF | Remainder | Fd := IEEE rem(Fn/Fm) |
  1496. | 1 | 0 | 0 | 1 | FML | Fast Multiply | Fd := Fn * Fm |
  1497. | 1 | 0 | 1 | 0 | FDV | Fast Divide | Fd := Fn / Fm |
  1498. | 1 | 0 | 1 | 1 | FRD | Fast reverse divide | Fd := Fm / Fn |
  1499. | 1 | 1 | 0 | 0 | POL | Polar angle (ArcTan2) | Fd := arctan2(Fn,Fm) |
  1500. | 1 | 1 | 0 | 1 | | undefined instruction | trap |
  1501. | 1 | 1 | 1 | 0 | | undefined instruction | trap |
  1502. | 1 | 1 | 1 | 1 | | undefined instruction | trap |
  1503. +---+---+---+---+----------+-----------------------+-----------------------+
  1504. Note: POW, RPW, POL are deprecated, and are available for backwards
  1505. compatibility only.
  1506. */
  1507. /*
  1508. TABLE 4: Monadic Floating Point Opcodes
  1509. +---+---+---+---+----------+-----------------------+-----------------------+
  1510. | a | b | c | d | Mnemonic | Description | Operation |
  1511. +---+---+---+---+----------+-----------------------+-----------------------+
  1512. | 0 | 0 | 0 | 0 | MVF | Move | Fd := Fm |
  1513. | 0 | 0 | 0 | 1 | MNF | Move negated | Fd := - Fm |
  1514. | 0 | 0 | 1 | 0 | ABS | Absolute value | Fd := abs(Fm) |
  1515. | 0 | 0 | 1 | 1 | RND | Round to integer | Fd := int(Fm) |
  1516. | 0 | 1 | 0 | 0 | SQT | Square root | Fd := sqrt(Fm) |
  1517. | 0 | 1 | 0 | 1 | LOG | Log base 10 | Fd := log10(Fm) |
  1518. | 0 | 1 | 1 | 0 | LGN | Log base e | Fd := ln(Fm) |
  1519. | 0 | 1 | 1 | 1 | EXP | Exponent | Fd := e ^ Fm |
  1520. | 1 | 0 | 0 | 0 | SIN | Sine | Fd := sin(Fm) |
  1521. | 1 | 0 | 0 | 1 | COS | Cosine | Fd := cos(Fm) |
  1522. | 1 | 0 | 1 | 0 | TAN | Tangent | Fd := tan(Fm) |
  1523. | 1 | 0 | 1 | 1 | ASN | Arc Sine | Fd := arcsin(Fm) |
  1524. | 1 | 1 | 0 | 0 | ACS | Arc Cosine | Fd := arccos(Fm) |
  1525. | 1 | 1 | 0 | 1 | ATN | Arc Tangent | Fd := arctan(Fm) |
  1526. | 1 | 1 | 1 | 0 | URD | Unnormalized round | Fd := int(Fm) |
  1527. | 1 | 1 | 1 | 1 | NRM | Normalize | Fd := norm(Fm) |
  1528. +---+---+---+---+----------+-----------------------+-----------------------+
  1529. Note: LOG, LGN, EXP, SIN, COS, TAN, ASN, ACS, ATN are deprecated, and are
  1530. available for backwards compatibility only.
  1531. */
  1532. /*
  1533. TABLE 5
  1534. +-------------------------+---+---+
  1535. | Rounding Precision | e | f |
  1536. +-------------------------+---+---+
  1537. | IEEE Single precision | 0 | 0 |
  1538. | IEEE Double precision | 0 | 1 |
  1539. | IEEE Extended precision | 1 | 0 |
  1540. | undefined (trap) | 1 | 1 |
  1541. +-------------------------+---+---+
  1542. */
  1543. /*
  1544. TABLE 5
  1545. +---------------------------------+---+---+
  1546. | Rounding Mode | g | h |
  1547. +---------------------------------+---+---+
  1548. | Round to nearest (default) | 0 | 0 |
  1549. | Round toward plus infinity | 0 | 1 |
  1550. | Round toward negative infinity | 1 | 0 |
  1551. | Round toward zero | 1 | 1 |
  1552. +---------------------------------+---+---+
  1553. *)
  1554. function taicpu.GetString:string;
  1555. var
  1556. i : longint;
  1557. s : string;
  1558. addsize : boolean;
  1559. begin
  1560. s:='['+gas_op2str[opcode];
  1561. for i:=0 to ops-1 do
  1562. begin
  1563. with oper[i]^ do
  1564. begin
  1565. if i=0 then
  1566. s:=s+' '
  1567. else
  1568. s:=s+',';
  1569. { type }
  1570. addsize:=false;
  1571. if (ot and OT_VREG)=OT_VREG then
  1572. s:=s+'vreg'
  1573. else
  1574. if (ot and OT_FPUREG)=OT_FPUREG then
  1575. s:=s+'fpureg'
  1576. else
  1577. if (ot and OT_REGS)=OT_REGS then
  1578. s:=s+'sreg'
  1579. else
  1580. if (ot and OT_REGF)=OT_REGF then
  1581. s:=s+'creg'
  1582. else
  1583. if (ot and OT_REGISTER)=OT_REGISTER then
  1584. begin
  1585. s:=s+'reg';
  1586. addsize:=true;
  1587. end
  1588. else
  1589. if (ot and OT_REGLIST)=OT_REGLIST then
  1590. begin
  1591. s:=s+'reglist';
  1592. addsize:=false;
  1593. end
  1594. else
  1595. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  1596. begin
  1597. s:=s+'imm';
  1598. addsize:=true;
  1599. end
  1600. else
  1601. if (ot and OT_MEMORY)=OT_MEMORY then
  1602. begin
  1603. s:=s+'mem';
  1604. addsize:=true;
  1605. if (ot and OT_AM2)<>0 then
  1606. s:=s+' am2 '
  1607. else if (ot and OT_AM6)<>0 then
  1608. s:=s+' am2 ';
  1609. end
  1610. else
  1611. if (ot and OT_SHIFTEROP)=OT_SHIFTEROP then
  1612. begin
  1613. s:=s+'shifterop';
  1614. addsize:=false;
  1615. end
  1616. else
  1617. s:=s+'???';
  1618. { size }
  1619. if addsize then
  1620. begin
  1621. if (ot and OT_BITS8)<>0 then
  1622. s:=s+'8'
  1623. else
  1624. if (ot and OT_BITS16)<>0 then
  1625. s:=s+'24'
  1626. else
  1627. if (ot and OT_BITS32)<>0 then
  1628. s:=s+'32'
  1629. else
  1630. if (ot and OT_BITSSHIFTER)<>0 then
  1631. s:=s+'shifter'
  1632. else
  1633. s:=s+'??';
  1634. { signed }
  1635. if (ot and OT_SIGNED)<>0 then
  1636. s:=s+'s';
  1637. end;
  1638. end;
  1639. end;
  1640. GetString:=s+']';
  1641. end;
  1642. procedure taicpu.ResetPass1;
  1643. begin
  1644. { we need to reset everything here, because the choosen insentry
  1645. can be invalid for a new situation where the previously optimized
  1646. insentry is not correct }
  1647. InsEntry:=nil;
  1648. InsSize:=0;
  1649. LastInsOffset:=-1;
  1650. end;
  1651. procedure taicpu.ResetPass2;
  1652. begin
  1653. { we are here in a second pass, check if the instruction can be optimized }
  1654. if assigned(InsEntry) and
  1655. ((InsEntry^.flags and IF_PASS2)<>0) then
  1656. begin
  1657. InsEntry:=nil;
  1658. InsSize:=0;
  1659. end;
  1660. LastInsOffset:=-1;
  1661. end;
  1662. function taicpu.CheckIfValid:boolean;
  1663. begin
  1664. Result:=False; { unimplemented }
  1665. end;
  1666. function taicpu.Pass1(objdata:TObjData):longint;
  1667. var
  1668. ldr2op : array[PF_B..PF_T] of tasmop = (
  1669. A_LDRB,A_LDRSB,A_LDRBT,A_LDRH,A_LDRSH,A_LDRT);
  1670. str2op : array[PF_B..PF_T] of tasmop = (
  1671. A_STRB,A_None,A_STRBT,A_STRH,A_None,A_STRT);
  1672. begin
  1673. Pass1:=0;
  1674. { Save the old offset and set the new offset }
  1675. InsOffset:=ObjData.CurrObjSec.Size;
  1676. { Error? }
  1677. if (Insentry=nil) and (InsSize=-1) then
  1678. exit;
  1679. { set the file postion }
  1680. current_filepos:=fileinfo;
  1681. { tranlate LDR+postfix to complete opcode }
  1682. if (opcode=A_LDR) and (oppostfix=PF_D) then
  1683. begin
  1684. opcode:=A_LDRD;
  1685. oppostfix:=PF_None;
  1686. end
  1687. else if (opcode=A_LDR) and (oppostfix<>PF_None) then
  1688. begin
  1689. if (oppostfix in [low(ldr2op)..high(ldr2op)]) then
  1690. opcode:=ldr2op[oppostfix]
  1691. else
  1692. internalerror(2005091001);
  1693. if opcode=A_None then
  1694. internalerror(2005091004);
  1695. { postfix has been added to opcode }
  1696. oppostfix:=PF_None;
  1697. end
  1698. else if (opcode=A_STR) and (oppostfix=PF_D) then
  1699. begin
  1700. opcode:=A_STRD;
  1701. oppostfix:=PF_None;
  1702. end
  1703. else if (opcode=A_STR) and (oppostfix<>PF_None) then
  1704. begin
  1705. if (oppostfix in [low(str2op)..high(str2op)]) then
  1706. opcode:=str2op[oppostfix]
  1707. else
  1708. internalerror(2005091002);
  1709. if opcode=A_None then
  1710. internalerror(2005091003);
  1711. { postfix has been added to opcode }
  1712. oppostfix:=PF_None;
  1713. end;
  1714. { Get InsEntry }
  1715. if FindInsEntry(objdata) then
  1716. begin
  1717. InsSize:=4;
  1718. LastInsOffset:=InsOffset;
  1719. Pass1:=InsSize;
  1720. exit;
  1721. end;
  1722. LastInsOffset:=-1;
  1723. end;
  1724. procedure taicpu.Pass2(objdata:TObjData);
  1725. begin
  1726. { error in pass1 ? }
  1727. if insentry=nil then
  1728. exit;
  1729. current_filepos:=fileinfo;
  1730. { Generate the instruction }
  1731. GenCode(objdata);
  1732. end;
  1733. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1734. begin
  1735. end;
  1736. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1737. begin
  1738. end;
  1739. procedure taicpu.ppubuildderefimploper(var o:toper);
  1740. begin
  1741. end;
  1742. procedure taicpu.ppuderefoper(var o:toper);
  1743. begin
  1744. end;
  1745. procedure taicpu.BuildArmMasks;
  1746. const
  1747. Masks: array[tcputype] of longint =
  1748. (
  1749. IF_NONE,
  1750. IF_ARMv4,
  1751. IF_ARMv4,
  1752. IF_ARMv4T or IF_ARMv4,
  1753. IF_ARMv4T or IF_ARMv4 or IF_ARMv5,
  1754. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T,
  1755. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE,
  1756. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ,
  1757. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6,
  1758. IF_ARMv4T or IF_ARMv4 or IF_ARMv5 or IF_ARMv5T or IF_ARMv5TE or IF_ARMv5TEJ or IF_armv6 or IF_ARMv6K,
  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,
  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,
  1761. IF_ARMv4T or IF_ARMv5T or IF_ARMv6M,
  1762. 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,
  1763. 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,
  1764. 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,
  1765. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M,
  1766. IF_ARMv4T or IF_ARMv5T or IF_ARMv6T2 or IF_ARMv7M or IF_ARMv7EM
  1767. );
  1768. FPUMasks: array[tfputype] of longint =
  1769. (
  1770. IF_NONE,
  1771. IF_NONE,
  1772. IF_NONE,
  1773. IF_FPA,
  1774. IF_FPA,
  1775. IF_FPA,
  1776. IF_VFPv2,
  1777. IF_VFPv2 or IF_VFPv3,
  1778. IF_VFPv2 or IF_VFPv3,
  1779. IF_NONE
  1780. );
  1781. begin
  1782. fArmVMask:=Masks[current_settings.cputype] or FPUMasks[current_settings.fputype];
  1783. if current_settings.instructionset=is_thumb then
  1784. begin
  1785. fArmMask:=IF_THUMB;
  1786. if CPUARM_HAS_THUMB2 in cpu_capabilities[current_settings.cputype] then
  1787. fArmMask:=fArmMask or IF_THUMB32;
  1788. end
  1789. else
  1790. fArmMask:=IF_ARM32;
  1791. end;
  1792. function taicpu.InsEnd:longint;
  1793. begin
  1794. Result:=0; { unimplemented }
  1795. end;
  1796. procedure taicpu.create_ot(objdata:TObjData);
  1797. var
  1798. i,l,relsize : longint;
  1799. dummy : byte;
  1800. currsym : TObjSymbol;
  1801. begin
  1802. if ops=0 then
  1803. exit;
  1804. { update oper[].ot field }
  1805. for i:=0 to ops-1 do
  1806. with oper[i]^ do
  1807. begin
  1808. case typ of
  1809. top_regset:
  1810. begin
  1811. ot:=OT_REGLIST;
  1812. end;
  1813. top_reg :
  1814. begin
  1815. case getregtype(reg) of
  1816. R_INTREGISTER:
  1817. begin
  1818. ot:=OT_REG32 or OT_SHIFTEROP;
  1819. if getsupreg(reg)<8 then
  1820. ot:=ot or OT_REGLO
  1821. else if reg=NR_STACK_POINTER_REG then
  1822. ot:=ot or OT_REGSP;
  1823. end;
  1824. R_FPUREGISTER:
  1825. ot:=OT_FPUREG;
  1826. R_MMREGISTER:
  1827. ot:=OT_VREG;
  1828. R_SPECIALREGISTER:
  1829. ot:=OT_REGF;
  1830. else
  1831. internalerror(2005090901);
  1832. end;
  1833. end;
  1834. top_ref :
  1835. begin
  1836. if ref^.refaddr=addr_no then
  1837. begin
  1838. { create ot field }
  1839. { we should get the size here dependend on the
  1840. instruction }
  1841. if (ot and OT_SIZE_MASK)=0 then
  1842. ot:=OT_MEMORY or OT_BITS32
  1843. else
  1844. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1845. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1846. ot:=ot or OT_MEM_OFFS;
  1847. { if we need to fix a reference, we do it here }
  1848. { pc relative addressing }
  1849. if (ref^.base=NR_NO) and
  1850. (ref^.index=NR_NO) and
  1851. (ref^.shiftmode=SM_None)
  1852. { at least we should check if the destination symbol
  1853. is in a text section }
  1854. { and
  1855. (ref^.symbol^.owner="text") } then
  1856. ref^.base:=NR_PC;
  1857. { determine possible address modes }
  1858. if GenerateThumbCode or
  1859. GenerateThumb2Code then
  1860. begin
  1861. if (ref^.base=NR_PC) then
  1862. ot:=ot or OT_AM6
  1863. else if (ref^.base=NR_STACK_POINTER_REG) then
  1864. ot:=ot or OT_AM5
  1865. else if ref^.index=NR_NO then
  1866. ot:=ot or OT_AM4
  1867. else
  1868. ot:=ot or OT_AM3;
  1869. end;
  1870. if (ref^.base<>NR_NO) and
  1871. (opcode in [A_LDREX,A_LDREXB,A_LDREXH,A_LDREXD,
  1872. A_STREX,A_STREXB,A_STREXH,A_STREXD]) and
  1873. (
  1874. (ref^.addressmode=AM_OFFSET) and
  1875. (ref^.index=NR_NO) and
  1876. (ref^.shiftmode=SM_None) and
  1877. (ref^.offset=0)
  1878. ) then
  1879. ot:=ot or OT_AM6
  1880. else if (ref^.base<>NR_NO) and
  1881. (
  1882. (
  1883. (ref^.index=NR_NO) and
  1884. (ref^.shiftmode=SM_None) and
  1885. (ref^.offset>=-4097) and
  1886. (ref^.offset<=4097)
  1887. ) or
  1888. (
  1889. (ref^.shiftmode=SM_None) and
  1890. (ref^.offset=0)
  1891. ) or
  1892. (
  1893. (ref^.index<>NR_NO) and
  1894. (ref^.shiftmode<>SM_None) and
  1895. (ref^.shiftimm<=32) and
  1896. (ref^.offset=0)
  1897. )
  1898. ) then
  1899. ot:=ot or OT_AM2;
  1900. if (ref^.index<>NR_NO) and
  1901. (oppostfix in [PF_None,PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  1902. (
  1903. (ref^.base=NR_NO) and
  1904. (ref^.shiftmode=SM_None) and
  1905. (ref^.offset=0)
  1906. ) then
  1907. ot:=ot or OT_AM4;
  1908. end
  1909. else
  1910. begin
  1911. l:=ref^.offset;
  1912. currsym:=ObjData.symbolref(ref^.symbol);
  1913. if assigned(currsym) then
  1914. inc(l,currsym.address);
  1915. relsize:=(InsOffset+2)-l;
  1916. if (relsize<-33554428) or (relsize>33554428) then
  1917. ot:=OT_IMM32
  1918. else
  1919. ot:=OT_IMM24;
  1920. end;
  1921. end;
  1922. top_local :
  1923. begin
  1924. { we should get the size here dependend on the
  1925. instruction }
  1926. if (ot and OT_SIZE_MASK)=0 then
  1927. ot:=OT_MEMORY or OT_BITS32
  1928. else
  1929. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1930. end;
  1931. top_const :
  1932. begin
  1933. ot:=OT_IMMEDIATE;
  1934. if (val=0) then
  1935. ot:=ot_immediatezero
  1936. else if is_shifter_const(val,dummy) then
  1937. ot:=OT_IMMSHIFTER
  1938. else if GenerateThumb2Code and is_thumb32_imm(val) then
  1939. ot:=OT_IMMSHIFTER
  1940. else
  1941. ot:=OT_IMM32
  1942. end;
  1943. top_none :
  1944. begin
  1945. { generated when there was an error in the
  1946. assembler reader. It never happends when generating
  1947. assembler }
  1948. end;
  1949. top_shifterop:
  1950. begin
  1951. ot:=OT_SHIFTEROP;
  1952. end;
  1953. top_conditioncode:
  1954. begin
  1955. ot:=OT_CONDITION;
  1956. end;
  1957. top_specialreg:
  1958. begin
  1959. ot:=OT_REGS;
  1960. end;
  1961. top_modeflags:
  1962. begin
  1963. ot:=OT_MODEFLAGS;
  1964. end;
  1965. else
  1966. internalerror(2004022623);
  1967. end;
  1968. end;
  1969. end;
  1970. function taicpu.Matches(p:PInsEntry):longint;
  1971. { * IF_SM stands for Size Match: any operand whose size is not
  1972. * explicitly specified by the template is `really' intended to be
  1973. * the same size as the first size-specified operand.
  1974. * Non-specification is tolerated in the input instruction, but
  1975. * _wrong_ specification is not.
  1976. *
  1977. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1978. * three-operand instructions such as SHLD: it implies that the
  1979. * first two operands must match in size, but that the third is
  1980. * required to be _unspecified_.
  1981. *
  1982. * IF_SB invokes Size Byte: operands with unspecified size in the
  1983. * template are really bytes, and so no non-byte specification in
  1984. * the input instruction will be tolerated. IF_SW similarly invokes
  1985. * Size Word, and IF_SD invokes Size Doubleword.
  1986. *
  1987. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1988. * that any operand with unspecified size in the template is
  1989. * required to have unspecified size in the instruction too...)
  1990. }
  1991. var
  1992. i{,j,asize,oprs} : longint;
  1993. {siz : array[0..3] of longint;}
  1994. begin
  1995. Matches:=100;
  1996. { Check the opcode and operands }
  1997. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1998. begin
  1999. Matches:=0;
  2000. exit;
  2001. end;
  2002. { check ARM instruction version }
  2003. if (p^.flags and fArmVMask)=0 then
  2004. begin
  2005. Matches:=0;
  2006. exit;
  2007. end;
  2008. { check ARM instruction type }
  2009. if (p^.flags and fArmMask)=0 then
  2010. begin
  2011. Matches:=0;
  2012. exit;
  2013. end;
  2014. { Check wideformat flag }
  2015. if wideformat and ((p^.flags and IF_WIDE)=0) then
  2016. begin
  2017. matches:=0;
  2018. exit;
  2019. end;
  2020. { Check that no spurious colons or TOs are present }
  2021. for i:=0 to p^.ops-1 do
  2022. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  2023. begin
  2024. Matches:=0;
  2025. exit;
  2026. end;
  2027. { Check that the operand flags all match up }
  2028. for i:=0 to p^.ops-1 do
  2029. begin
  2030. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  2031. ((p^.optypes[i] and OT_SIZE_MASK) and
  2032. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  2033. begin
  2034. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  2035. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  2036. begin
  2037. Matches:=0;
  2038. exit;
  2039. end
  2040. else if ((p^.optypes[i] and OT_OPT_SIZE)<>0) and
  2041. ((p^.optypes[i] and OT_OPT_SIZE)<>(oper[i]^.ot and OT_OPT_SIZE)) then
  2042. begin
  2043. Matches:=0;
  2044. exit;
  2045. end
  2046. else
  2047. Matches:=1;
  2048. end;
  2049. end;
  2050. { check postfixes:
  2051. the existance of a certain postfix requires a
  2052. particular code }
  2053. { update condition flags
  2054. or floating point single }
  2055. if (oppostfix=PF_S) and
  2056. not(p^.code[0] in [#$04..#$0F,#$14..#$16,#$29,#$30,#$60..#$6B,#$80..#$82,#$A0..#$A2]) then
  2057. begin
  2058. Matches:=0;
  2059. exit;
  2060. end;
  2061. { floating point size }
  2062. if (oppostfix in [PF_D,PF_E,PF_P,PF_EP]) and
  2063. not(p^.code[0] in [#$A0..#$A2]) then
  2064. begin
  2065. Matches:=0;
  2066. exit;
  2067. end;
  2068. { multiple load/store address modes }
  2069. if (oppostfix in [PF_IA,PF_IB,PF_DA,PF_DB,PF_FD,PF_FA,PF_ED,PF_EA]) and
  2070. not(p^.code[0] in [
  2071. // ldr,str,ldrb,strb
  2072. #$17,
  2073. // stm,ldm
  2074. #$26,#$69,#$8C,
  2075. // vldm/vstm
  2076. #$44,#$94
  2077. ]) then
  2078. begin
  2079. Matches:=0;
  2080. exit;
  2081. end;
  2082. { we shouldn't see any opsize prefixes here }
  2083. if (oppostfix in [PF_B,PF_SB,PF_BT,PF_H,PF_SH,PF_T]) then
  2084. begin
  2085. Matches:=0;
  2086. exit;
  2087. end;
  2088. if (roundingmode<>RM_None) and not(p^.code[0] in []) then
  2089. begin
  2090. Matches:=0;
  2091. exit;
  2092. end;
  2093. { Check thumb flags }
  2094. if p^.code[0] in [#$60..#$61] then
  2095. begin
  2096. if (p^.code[0]=#$60) and
  2097. (GenerateThumb2Code and
  2098. ((not inIT) and (oppostfix<>PF_S)) or
  2099. (inIT and (condition=C_None))) then
  2100. begin
  2101. Matches:=0;
  2102. exit;
  2103. end
  2104. else if (p^.code[0]=#$61) and
  2105. (oppostfix=PF_S) then
  2106. begin
  2107. Matches:=0;
  2108. exit;
  2109. end;
  2110. end
  2111. else if p^.code[0]=#$62 then
  2112. begin
  2113. if (GenerateThumb2Code and
  2114. (condition<>C_None) and
  2115. (not inIT) and
  2116. (not lastinIT)) then
  2117. begin
  2118. Matches:=0;
  2119. exit;
  2120. end;
  2121. end
  2122. else if p^.code[0]=#$63 then
  2123. begin
  2124. if inIT then
  2125. begin
  2126. Matches:=0;
  2127. exit;
  2128. end;
  2129. end
  2130. else if p^.code[0]=#$64 then
  2131. begin
  2132. if (opcode=A_MUL) then
  2133. begin
  2134. if (ops=3) and
  2135. ((oper[2]^.typ<>top_reg) or
  2136. (oper[0]^.reg<>oper[2]^.reg)) then
  2137. begin
  2138. matches:=0;
  2139. exit;
  2140. end;
  2141. end;
  2142. end;
  2143. { Check operand sizes }
  2144. { as default an untyped size can get all the sizes, this is different
  2145. from nasm, but else we need to do a lot checking which opcodes want
  2146. size or not with the automatic size generation }
  2147. (*
  2148. asize:=longint($ffffffff);
  2149. if (p^.flags and IF_SB)<>0 then
  2150. asize:=OT_BITS8
  2151. else if (p^.flags and IF_SW)<>0 then
  2152. asize:=OT_BITS16
  2153. else if (p^.flags and IF_SD)<>0 then
  2154. asize:=OT_BITS32;
  2155. if (p^.flags and IF_ARMASK)<>0 then
  2156. begin
  2157. siz[0]:=0;
  2158. siz[1]:=0;
  2159. siz[2]:=0;
  2160. if (p^.flags and IF_AR0)<>0 then
  2161. siz[0]:=asize
  2162. else if (p^.flags and IF_AR1)<>0 then
  2163. siz[1]:=asize
  2164. else if (p^.flags and IF_AR2)<>0 then
  2165. siz[2]:=asize;
  2166. end
  2167. else
  2168. begin
  2169. { we can leave because the size for all operands is forced to be
  2170. the same
  2171. but not if IF_SB IF_SW or IF_SD is set PM }
  2172. if asize=-1 then
  2173. exit;
  2174. siz[0]:=asize;
  2175. siz[1]:=asize;
  2176. siz[2]:=asize;
  2177. end;
  2178. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  2179. begin
  2180. if (p^.flags and IF_SM2)<>0 then
  2181. oprs:=2
  2182. else
  2183. oprs:=p^.ops;
  2184. for i:=0 to oprs-1 do
  2185. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  2186. begin
  2187. for j:=0 to oprs-1 do
  2188. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  2189. break;
  2190. end;
  2191. end
  2192. else
  2193. oprs:=2;
  2194. { Check operand sizes }
  2195. for i:=0 to p^.ops-1 do
  2196. begin
  2197. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  2198. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  2199. { Immediates can always include smaller size }
  2200. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  2201. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  2202. Matches:=2;
  2203. end;
  2204. *)
  2205. end;
  2206. function taicpu.calcsize(p:PInsEntry):shortint;
  2207. begin
  2208. result:=4;
  2209. end;
  2210. function taicpu.NeedAddrPrefix(opidx:byte):boolean;
  2211. begin
  2212. Result:=False; { unimplemented }
  2213. end;
  2214. procedure taicpu.Swapoperands;
  2215. begin
  2216. end;
  2217. function taicpu.FindInsentry(objdata:TObjData):boolean;
  2218. var
  2219. i : longint;
  2220. begin
  2221. result:=false;
  2222. { Things which may only be done once, not when a second pass is done to
  2223. optimize }
  2224. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  2225. begin
  2226. { create the .ot fields }
  2227. create_ot(objdata);
  2228. BuildArmMasks;
  2229. { set the file postion }
  2230. current_filepos:=fileinfo;
  2231. end
  2232. else
  2233. begin
  2234. { we've already an insentry so it's valid }
  2235. result:=true;
  2236. exit;
  2237. end;
  2238. { Lookup opcode in the table }
  2239. InsSize:=-1;
  2240. i:=instabcache^[opcode];
  2241. if i=-1 then
  2242. begin
  2243. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  2244. exit;
  2245. end;
  2246. insentry:=@instab[i];
  2247. while (insentry^.opcode=opcode) do
  2248. begin
  2249. if matches(insentry)=100 then
  2250. begin
  2251. result:=true;
  2252. exit;
  2253. end;
  2254. inc(i);
  2255. insentry:=@instab[i];
  2256. end;
  2257. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2258. { No instruction found, set insentry to nil and inssize to -1 }
  2259. insentry:=nil;
  2260. inssize:=-1;
  2261. end;
  2262. procedure taicpu.gencode(objdata:TObjData);
  2263. const
  2264. CondVal : array[TAsmCond] of byte=(
  2265. $E, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $A,
  2266. $B, $C, $D, $E, 0);
  2267. var
  2268. bytes, rd, rm, rn, d, m, n : dword;
  2269. bytelen : longint;
  2270. dp_operation : boolean;
  2271. i_field : byte;
  2272. currsym : TObjSymbol;
  2273. offset : longint;
  2274. refoper : poper;
  2275. msb : longint;
  2276. r: byte;
  2277. procedure setshifterop(op : byte);
  2278. var
  2279. r : byte;
  2280. imm : dword;
  2281. count : integer;
  2282. begin
  2283. case oper[op]^.typ of
  2284. top_const:
  2285. begin
  2286. i_field:=1;
  2287. if oper[op]^.val and $ff=oper[op]^.val then
  2288. bytes:=bytes or dword(oper[op]^.val)
  2289. else
  2290. begin
  2291. { calc rotate and adjust imm }
  2292. count:=0;
  2293. r:=0;
  2294. imm:=dword(oper[op]^.val);
  2295. repeat
  2296. imm:=RolDWord(imm, 2);
  2297. inc(r);
  2298. inc(count);
  2299. if count > 32 then
  2300. begin
  2301. message1(asmw_e_invalid_opcode_and_operands, 'invalid shifter imm');
  2302. exit;
  2303. end;
  2304. until (imm and $ff)=imm;
  2305. bytes:=bytes or (r shl 8) or imm;
  2306. end;
  2307. end;
  2308. top_reg:
  2309. begin
  2310. i_field:=0;
  2311. bytes:=bytes or getsupreg(oper[op]^.reg);
  2312. { does a real shifter op follow? }
  2313. if (op+1<opercnt) and (oper[op+1]^.typ=top_shifterop) then
  2314. with oper[op+1]^.shifterop^ do
  2315. begin
  2316. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2317. if shiftmode<>SM_RRX then
  2318. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2319. else
  2320. bytes:=bytes or (3 shl 5);
  2321. if getregtype(rs) <> R_INVALIDREGISTER then
  2322. begin
  2323. bytes:=bytes or (1 shl 4);
  2324. bytes:=bytes or (getsupreg(rs) shl 8);
  2325. end
  2326. end;
  2327. end;
  2328. else
  2329. internalerror(2005091103);
  2330. end;
  2331. end;
  2332. function MakeRegList(reglist: tcpuregisterset): word;
  2333. var
  2334. i, w: word;
  2335. begin
  2336. result:=0;
  2337. w:=1;
  2338. for i:=RS_R0 to RS_R15 do
  2339. begin
  2340. if i in reglist then
  2341. result:=result or w;
  2342. w:=w shl 1
  2343. end;
  2344. end;
  2345. function getcoproc(reg: tregister): byte;
  2346. begin
  2347. if reg=NR_p15 then
  2348. result:=15
  2349. else
  2350. begin
  2351. Message1(asmw_e_invalid_opcode_and_operands,'Invalid coprocessor port');
  2352. result:=0;
  2353. end;
  2354. end;
  2355. function getcoprocreg(reg: tregister): byte;
  2356. begin
  2357. result:=getsupreg(reg)-getsupreg(NR_CR0);
  2358. end;
  2359. function getmmreg(reg: tregister): byte;
  2360. begin
  2361. case reg of
  2362. NR_D0: result:=0;
  2363. NR_D1: result:=1;
  2364. NR_D2: result:=2;
  2365. NR_D3: result:=3;
  2366. NR_D4: result:=4;
  2367. NR_D5: result:=5;
  2368. NR_D6: result:=6;
  2369. NR_D7: result:=7;
  2370. NR_D8: result:=8;
  2371. NR_D9: result:=9;
  2372. NR_D10: result:=10;
  2373. NR_D11: result:=11;
  2374. NR_D12: result:=12;
  2375. NR_D13: result:=13;
  2376. NR_D14: result:=14;
  2377. NR_D15: result:=15;
  2378. NR_D16: result:=16;
  2379. NR_D17: result:=17;
  2380. NR_D18: result:=18;
  2381. NR_D19: result:=19;
  2382. NR_D20: result:=20;
  2383. NR_D21: result:=21;
  2384. NR_D22: result:=22;
  2385. NR_D23: result:=23;
  2386. NR_D24: result:=24;
  2387. NR_D25: result:=25;
  2388. NR_D26: result:=26;
  2389. NR_D27: result:=27;
  2390. NR_D28: result:=28;
  2391. NR_D29: result:=29;
  2392. NR_D30: result:=30;
  2393. NR_D31: result:=31;
  2394. NR_S0: result:=0;
  2395. NR_S1: result:=1;
  2396. NR_S2: result:=2;
  2397. NR_S3: result:=3;
  2398. NR_S4: result:=4;
  2399. NR_S5: result:=5;
  2400. NR_S6: result:=6;
  2401. NR_S7: result:=7;
  2402. NR_S8: result:=8;
  2403. NR_S9: result:=9;
  2404. NR_S10: result:=10;
  2405. NR_S11: result:=11;
  2406. NR_S12: result:=12;
  2407. NR_S13: result:=13;
  2408. NR_S14: result:=14;
  2409. NR_S15: result:=15;
  2410. NR_S16: result:=16;
  2411. NR_S17: result:=17;
  2412. NR_S18: result:=18;
  2413. NR_S19: result:=19;
  2414. NR_S20: result:=20;
  2415. NR_S21: result:=21;
  2416. NR_S22: result:=22;
  2417. NR_S23: result:=23;
  2418. NR_S24: result:=24;
  2419. NR_S25: result:=25;
  2420. NR_S26: result:=26;
  2421. NR_S27: result:=27;
  2422. NR_S28: result:=28;
  2423. NR_S29: result:=29;
  2424. NR_S30: result:=30;
  2425. NR_S31: result:=31;
  2426. else
  2427. result:=0;
  2428. end;
  2429. end;
  2430. procedure encodethumbimm(imm: longword);
  2431. var
  2432. imm12, tmp: tcgint;
  2433. shift: integer;
  2434. found: boolean;
  2435. begin
  2436. found:=true;
  2437. if (imm and $FF) = imm then
  2438. imm12:=imm
  2439. else if ((imm shr 16)=(imm and $FFFF)) and
  2440. ((imm and $FF00FF00) = 0) then
  2441. imm12:=(imm and $ff) or ($1 shl 8)
  2442. else if ((imm shr 16)=(imm and $FFFF)) and
  2443. ((imm and $00FF00FF) = 0) then
  2444. imm12:=((imm shr 8) and $ff) or ($2 shl 8)
  2445. else if ((imm shr 16)=(imm and $FFFF)) and
  2446. (((imm shr 8) and $FF)=(imm and $FF)) then
  2447. imm12:=(imm and $ff) or ($3 shl 8)
  2448. else
  2449. begin
  2450. found:=false;
  2451. imm12:=0;
  2452. for shift:=1 to 31 do
  2453. begin
  2454. tmp:=RolDWord(imm,shift);
  2455. if ((tmp and $FF)=tmp) and
  2456. ((tmp and $80)=$80) then
  2457. begin
  2458. imm12:=(tmp and $7F) or (shift shl 7);
  2459. found:=true;
  2460. break;
  2461. end;
  2462. end;
  2463. end;
  2464. if found then
  2465. begin
  2466. bytes:=bytes or (imm12 and $FF);
  2467. bytes:=bytes or (((imm12 shr 8) and $7) shl 12);
  2468. bytes:=bytes or (((imm12 shr 11) and $1) shl 26);
  2469. end
  2470. else
  2471. Message1(asmw_e_value_exceeds_bounds, IntToStr(imm));
  2472. end;
  2473. procedure setthumbshift(op: byte; is_sat: boolean = false);
  2474. var
  2475. shift,typ: byte;
  2476. begin
  2477. shift:=0;
  2478. typ:=0;
  2479. case oper[op]^.shifterop^.shiftmode of
  2480. SM_LSL: begin typ:=0; shift:=oper[op]^.shifterop^.shiftimm; end;
  2481. SM_LSR: begin typ:=1; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2482. SM_ASR: begin typ:=2; shift:=oper[op]^.shifterop^.shiftimm; if shift=32 then shift:=0; end;
  2483. SM_ROR: begin typ:=3; shift:=oper[op]^.shifterop^.shiftimm; if shift=0 then message(asmw_e_invalid_opcode_and_operands); end;
  2484. SM_RRX: begin typ:=3; shift:=0; end;
  2485. end;
  2486. if is_sat then
  2487. begin
  2488. bytes:=bytes or ((typ and 1) shl 5);
  2489. bytes:=bytes or ((typ shr 1) shl 21);
  2490. end
  2491. else
  2492. bytes:=bytes or (typ shl 4);
  2493. bytes:=bytes or (shift and $3) shl 6;
  2494. bytes:=bytes or ((shift and $1C) shr 2) shl 12;
  2495. end;
  2496. begin
  2497. bytes:=$0;
  2498. bytelen:=4;
  2499. i_field:=0;
  2500. { evaluate and set condition code }
  2501. bytes:=bytes or (CondVal[condition] shl 28);
  2502. { condition code allowed? }
  2503. { setup rest of the instruction }
  2504. case insentry^.code[0] of
  2505. #$01: // B/BL
  2506. begin
  2507. { set instruction code }
  2508. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2509. { set offset }
  2510. if oper[0]^.typ=top_const then
  2511. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  2512. else
  2513. begin
  2514. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2515. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  2516. begin
  2517. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  2518. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  2519. end
  2520. else
  2521. bytes:=bytes or (((currsym.offset-insoffset-8) shr 2) and $ffffff);
  2522. end;
  2523. end;
  2524. #$02:
  2525. begin
  2526. { set instruction code }
  2527. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2528. { set code }
  2529. bytes:=bytes or (oper[0]^.val and $FFFFFF);
  2530. end;
  2531. #$03:
  2532. begin // BLX/BX
  2533. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2534. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2535. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2536. bytes:=bytes or ord(insentry^.code[4]);
  2537. bytes:=bytes or getsupreg(oper[0]^.reg);
  2538. end;
  2539. #$04..#$07: // SUB
  2540. begin
  2541. { set instruction code }
  2542. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2543. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2544. { set destination }
  2545. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2546. { set Rn }
  2547. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  2548. { create shifter op }
  2549. setshifterop(2);
  2550. { set I field }
  2551. bytes:=bytes or (i_field shl 25);
  2552. { set S if necessary }
  2553. if oppostfix=PF_S then
  2554. bytes:=bytes or (1 shl 20);
  2555. end;
  2556. #$08,#$0A,#$0B: // MOV
  2557. begin
  2558. { set instruction code }
  2559. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2560. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2561. { set destination }
  2562. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2563. { create shifter op }
  2564. setshifterop(1);
  2565. { set I field }
  2566. bytes:=bytes or (i_field shl 25);
  2567. { set S if necessary }
  2568. if oppostfix=PF_S then
  2569. bytes:=bytes or (1 shl 20);
  2570. end;
  2571. #$0C,#$0E,#$0F: // CMP
  2572. begin
  2573. { set instruction code }
  2574. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2575. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2576. { set destination }
  2577. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  2578. { create shifter op }
  2579. setshifterop(1);
  2580. { set I field }
  2581. bytes:=bytes or (i_field shl 25);
  2582. { always set S bit }
  2583. bytes:=bytes or (1 shl 20);
  2584. end;
  2585. #$10: // MRS
  2586. begin
  2587. { set instruction code }
  2588. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2589. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2590. { set destination }
  2591. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  2592. case oper[1]^.reg of
  2593. NR_APSR,NR_CPSR:;
  2594. NR_SPSR:
  2595. begin
  2596. bytes:=bytes or (1 shl 22);
  2597. end;
  2598. else
  2599. Message(asmw_e_invalid_opcode_and_operands);
  2600. end;
  2601. end;
  2602. #$12,#$13: // MSR
  2603. begin
  2604. { set instruction code }
  2605. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2606. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2607. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2608. { set destination }
  2609. if oper[0]^.typ=top_specialreg then
  2610. begin
  2611. if (oper[0]^.specialreg<>NR_CPSR) and
  2612. (oper[0]^.specialreg<>NR_SPSR) then
  2613. Message1(asmw_e_invalid_opcode_and_operands, '"Invalid special reg"');
  2614. if srC in oper[0]^.specialflags then
  2615. bytes:=bytes or (1 shl 16);
  2616. if srX in oper[0]^.specialflags then
  2617. bytes:=bytes or (1 shl 17);
  2618. if srS in oper[0]^.specialflags then
  2619. bytes:=bytes or (1 shl 18);
  2620. if srF in oper[0]^.specialflags then
  2621. bytes:=bytes or (1 shl 19);
  2622. { Set R bit }
  2623. if oper[0]^.specialreg=NR_SPSR then
  2624. bytes:=bytes or (1 shl 22);
  2625. end
  2626. else
  2627. case oper[0]^.reg of
  2628. NR_APSR_nzcvq: bytes:=bytes or (2 shl 18);
  2629. NR_APSR_g: bytes:=bytes or (1 shl 18);
  2630. NR_APSR_nzcvqg: bytes:=bytes or (3 shl 18);
  2631. else
  2632. Message1(asmw_e_invalid_opcode_and_operands, 'Invalid combination APSR bits used');
  2633. end;
  2634. setshifterop(1);
  2635. end;
  2636. #$14: // MUL/MLA r1,r2,r3
  2637. begin
  2638. { set instruction code }
  2639. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2640. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2641. bytes:=bytes or ord(insentry^.code[3]);
  2642. { set regs }
  2643. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2644. bytes:=bytes or getsupreg(oper[1]^.reg);
  2645. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2646. if oppostfix in [PF_S] then
  2647. bytes:=bytes or (1 shl 20);
  2648. end;
  2649. #$15: // MUL/MLA r1,r2,r3,r4
  2650. begin
  2651. { set instruction code }
  2652. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2653. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2654. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2655. { set regs }
  2656. bytes:=bytes or getsupreg(oper[0]^.reg) shl 16;
  2657. bytes:=bytes or getsupreg(oper[1]^.reg);
  2658. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  2659. if ops>3 then
  2660. bytes:=bytes or getsupreg(oper[3]^.reg) shl 12
  2661. else
  2662. bytes:=bytes or ord(insentry^.code[4]) shl 12;
  2663. if oppostfix in [PF_R,PF_X] then
  2664. bytes:=bytes or (1 shl 5);
  2665. if oppostfix in [PF_S] then
  2666. bytes:=bytes or (1 shl 20);
  2667. end;
  2668. #$16: // MULL r1,r2,r3,r4
  2669. begin
  2670. { set instruction code }
  2671. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2672. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2673. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2674. { set regs }
  2675. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2676. if (ops=3) and (opcode=A_PKHTB) then
  2677. begin
  2678. bytes:=bytes or getsupreg(oper[1]^.reg);
  2679. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2680. end
  2681. else
  2682. begin
  2683. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  2684. bytes:=bytes or getsupreg(oper[2]^.reg);
  2685. end;
  2686. if ops=4 then
  2687. begin
  2688. if oper[3]^.typ=top_shifterop then
  2689. begin
  2690. if opcode in [A_PKHBT,A_PKHTB] then
  2691. begin
  2692. if ((opcode=A_PKHTB) and
  2693. (oper[3]^.shifterop^.shiftmode <> SM_ASR)) or
  2694. ((opcode=A_PKHBT) and
  2695. (oper[3]^.shifterop^.shiftmode <> SM_LSL)) or
  2696. (oper[3]^.shifterop^.rs<>NR_NO) then
  2697. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2698. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  2699. end
  2700. else
  2701. begin
  2702. if (oper[3]^.shifterop^.shiftmode<>sm_ror) or
  2703. (oper[3]^.shifterop^.rs<>NR_NO) or
  2704. (not (oper[3]^.shifterop^.shiftimm in [0,8,16,24])) then
  2705. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2706. bytes:=bytes or (((oper[3]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2707. end;
  2708. end
  2709. else
  2710. bytes:=bytes or getsupreg(oper[3]^.reg) shl 8;
  2711. end;
  2712. if PF_S=oppostfix then
  2713. bytes:=bytes or (1 shl 20);
  2714. if PF_X=oppostfix then
  2715. bytes:=bytes or (1 shl 5);
  2716. end;
  2717. #$17: // LDR/STR
  2718. begin
  2719. { set instruction code }
  2720. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2721. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2722. { set Rn and Rd }
  2723. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2724. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2725. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2726. begin
  2727. { set offset }
  2728. offset:=0;
  2729. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  2730. if assigned(currsym) then
  2731. offset:=currsym.offset-insoffset-8;
  2732. offset:=offset+oper[1]^.ref^.offset;
  2733. if offset>=0 then
  2734. { set U flag }
  2735. bytes:=bytes or (1 shl 23)
  2736. else
  2737. offset:=-offset;
  2738. bytes:=bytes or (offset and $FFF);
  2739. end
  2740. else
  2741. begin
  2742. { set U flag }
  2743. if oper[1]^.ref^.signindex>=0 then
  2744. bytes:=bytes or (1 shl 23);
  2745. { set I flag }
  2746. bytes:=bytes or (1 shl 25);
  2747. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2748. { set shift }
  2749. with oper[1]^.ref^ do
  2750. if shiftmode<>SM_None then
  2751. begin
  2752. bytes:=bytes or ((shiftimm and $1F) shl 7);
  2753. if shiftmode<>SM_RRX then
  2754. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  2755. else
  2756. bytes:=bytes or (3 shl 5);
  2757. end
  2758. end;
  2759. { set W bit }
  2760. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2761. bytes:=bytes or (1 shl 21);
  2762. { set P bit if necessary }
  2763. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2764. bytes:=bytes or (1 shl 24);
  2765. end;
  2766. #$18: // LDREX/STREX
  2767. begin
  2768. { set instruction code }
  2769. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2770. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2771. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2772. bytes:=bytes or ord(insentry^.code[4]);
  2773. { set Rn and Rd }
  2774. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2775. if (ops=3) then
  2776. begin
  2777. if opcode<>A_LDREXD then
  2778. bytes:=bytes or getsupreg(oper[1]^.reg);
  2779. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  2780. end
  2781. else if (ops=4) then // STREXD
  2782. begin
  2783. if opcode<>A_LDREXD then
  2784. bytes:=bytes or getsupreg(oper[1]^.reg);
  2785. bytes:=bytes or (getsupreg(oper[3]^.ref^.base) shl 16);
  2786. end
  2787. else
  2788. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 16);
  2789. end;
  2790. #$19: // LDRD/STRD
  2791. begin
  2792. { set instruction code }
  2793. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2794. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2795. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2796. bytes:=bytes or ord(insentry^.code[4]);
  2797. { set Rn and Rd }
  2798. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2799. refoper:=oper[1];
  2800. if ops=3 then
  2801. refoper:=oper[2];
  2802. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2803. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2804. begin
  2805. bytes:=bytes or (1 shl 22);
  2806. { set offset }
  2807. offset:=0;
  2808. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2809. if assigned(currsym) then
  2810. offset:=currsym.offset-insoffset-8;
  2811. offset:=offset+refoper^.ref^.offset;
  2812. if offset>=0 then
  2813. { set U flag }
  2814. bytes:=bytes or (1 shl 23)
  2815. else
  2816. offset:=-offset;
  2817. bytes:=bytes or (offset and $F);
  2818. bytes:=bytes or ((offset and $F0) shl 4);
  2819. end
  2820. else
  2821. begin
  2822. { set U flag }
  2823. if refoper^.ref^.signindex>=0 then
  2824. bytes:=bytes or (1 shl 23);
  2825. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2826. end;
  2827. { set W bit }
  2828. if refoper^.ref^.addressmode=AM_PREINDEXED then
  2829. bytes:=bytes or (1 shl 21);
  2830. { set P bit if necessary }
  2831. if refoper^.ref^.addressmode<>AM_POSTINDEXED then
  2832. bytes:=bytes or (1 shl 24);
  2833. end;
  2834. #$1A: // QADD/QSUB
  2835. begin
  2836. { set instruction code }
  2837. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2838. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2839. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2840. { set regs }
  2841. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2842. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0;
  2843. bytes:=bytes or getsupreg(oper[2]^.reg) shl 16;
  2844. end;
  2845. #$1B:
  2846. begin
  2847. { set instruction code }
  2848. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2849. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2850. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2851. { set regs }
  2852. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2853. bytes:=bytes or getsupreg(oper[1]^.reg);
  2854. if ops=3 then
  2855. begin
  2856. if (oper[2]^.shifterop^.shiftmode<>sm_ror) or
  2857. (oper[2]^.shifterop^.rs<>NR_NO) or
  2858. (not (oper[2]^.shifterop^.shiftimm in [0,8,16,24])) then
  2859. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2860. bytes:=bytes or (((oper[2]^.shifterop^.shiftimm shr 3) and $3) shl 10);
  2861. end;
  2862. end;
  2863. #$1C: // MCR/MRC
  2864. begin
  2865. { set instruction code }
  2866. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2867. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2868. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2869. { set regs and operands }
  2870. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2871. bytes:=bytes or ((oper[1]^.val and $7) shl 21);
  2872. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2873. bytes:=bytes or getcoprocreg(oper[3]^.reg) shl 16;
  2874. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2875. if ops > 5 then
  2876. bytes:=bytes or ((oper[5]^.val and $7) shl 5);
  2877. end;
  2878. #$1D: // MCRR/MRRC
  2879. begin
  2880. { set instruction code }
  2881. bytes:=bytes or ord(insentry^.code[1]) shl 24;
  2882. bytes:=bytes or ord(insentry^.code[2]) shl 16;
  2883. bytes:=bytes or ord(insentry^.code[3]) shl 4;
  2884. { set regs and operands }
  2885. bytes:=bytes or getcoproc(oper[0]^.reg) shl 8;
  2886. bytes:=bytes or ((oper[1]^.val and $7) shl 4);
  2887. bytes:=bytes or getsupreg(oper[2]^.reg) shl 12;
  2888. bytes:=bytes or getsupreg(oper[3]^.reg) shl 16;
  2889. bytes:=bytes or getcoprocreg(oper[4]^.reg);
  2890. end;
  2891. #$1E: // LDRHT/STRHT
  2892. begin
  2893. { set instruction code }
  2894. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2895. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2896. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2897. bytes:=bytes or ord(insentry^.code[4]);
  2898. { set Rn and Rd }
  2899. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2900. refoper:=oper[1];
  2901. bytes:=bytes or getsupreg(refoper^.ref^.base) shl 16;
  2902. if getregtype(refoper^.ref^.index)=R_INVALIDREGISTER then
  2903. begin
  2904. bytes:=bytes or (1 shl 22);
  2905. { set offset }
  2906. offset:=0;
  2907. currsym:=objdata.symbolref(refoper^.ref^.symbol);
  2908. if assigned(currsym) then
  2909. offset:=currsym.offset-insoffset-8;
  2910. offset:=offset+refoper^.ref^.offset;
  2911. if offset>=0 then
  2912. { set U flag }
  2913. bytes:=bytes or (1 shl 23)
  2914. else
  2915. offset:=-offset;
  2916. bytes:=bytes or (offset and $F);
  2917. bytes:=bytes or ((offset and $F0) shl 4);
  2918. end
  2919. else
  2920. begin
  2921. { set U flag }
  2922. if refoper^.ref^.signindex>=0 then
  2923. bytes:=bytes or (1 shl 23);
  2924. bytes:=bytes or getsupreg(refoper^.ref^.index);
  2925. end;
  2926. end;
  2927. #$22: // LDRH/STRH
  2928. begin
  2929. { set instruction code }
  2930. bytes:=bytes or (ord(insentry^.code[1]) shl 16);
  2931. bytes:=bytes or ord(insentry^.code[2]);
  2932. { src/dest register (Rd) }
  2933. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  2934. { base register (Rn) }
  2935. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  2936. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  2937. begin
  2938. bytes:=bytes or (1 shl 22); // with immediate offset
  2939. offset:=oper[1]^.ref^.offset;
  2940. if offset>=0 then
  2941. { set U flag }
  2942. bytes:=bytes or (1 shl 23)
  2943. else
  2944. offset:=-offset;
  2945. bytes:=bytes or (offset and $F);
  2946. bytes:=bytes or ((offset and $F0) shl 4);
  2947. end
  2948. else
  2949. begin
  2950. { set U flag }
  2951. if oper[1]^.ref^.signindex>=0 then
  2952. bytes:=bytes or (1 shl 23);
  2953. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  2954. end;
  2955. { set W bit }
  2956. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  2957. bytes:=bytes or (1 shl 21);
  2958. { set P bit if necessary }
  2959. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  2960. bytes:=bytes or (1 shl 24);
  2961. end;
  2962. #$25: // PLD/PLI
  2963. begin
  2964. { set instruction code }
  2965. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  2966. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  2967. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  2968. bytes:=bytes or ord(insentry^.code[4]);
  2969. { set Rn and Rd }
  2970. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  2971. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  2972. begin
  2973. { set offset }
  2974. offset:=0;
  2975. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  2976. if assigned(currsym) then
  2977. offset:=currsym.offset-insoffset-8;
  2978. offset:=offset+oper[0]^.ref^.offset;
  2979. if offset>=0 then
  2980. begin
  2981. { set U flag }
  2982. bytes:=bytes or (1 shl 23);
  2983. bytes:=bytes or offset
  2984. end
  2985. else
  2986. begin
  2987. offset:=-offset;
  2988. bytes:=bytes or offset
  2989. end;
  2990. end
  2991. else
  2992. begin
  2993. bytes:=bytes or (1 shl 25);
  2994. { set U flag }
  2995. if oper[0]^.ref^.signindex>=0 then
  2996. bytes:=bytes or (1 shl 23);
  2997. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  2998. { set shift }
  2999. with oper[0]^.ref^ do
  3000. if shiftmode<>SM_None then
  3001. begin
  3002. bytes:=bytes or ((shiftimm and $1F) shl 7);
  3003. if shiftmode<>SM_RRX then
  3004. bytes:=bytes or (ord(shiftmode) - ord(SM_LSL)) shl 5
  3005. else
  3006. bytes:=bytes or (3 shl 5);
  3007. end
  3008. end;
  3009. end;
  3010. #$26: // LDM/STM
  3011. begin
  3012. { set instruction code }
  3013. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3014. if ops>1 then
  3015. begin
  3016. if oper[0]^.typ=top_ref then
  3017. begin
  3018. { set W bit }
  3019. if oper[0]^.ref^.addressmode=AM_PREINDEXED then
  3020. bytes:=bytes or (1 shl 21);
  3021. { set Rn }
  3022. bytes:=bytes or (getsupreg(oper[0]^.ref^.index) shl 16);
  3023. end
  3024. else { typ=top_reg }
  3025. begin
  3026. { set Rn }
  3027. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  3028. end;
  3029. if oper[1]^.usermode then
  3030. begin
  3031. if (oper[0]^.typ=top_ref) then
  3032. begin
  3033. if (opcode=A_LDM) and
  3034. (RS_PC in oper[1]^.regset^) then
  3035. begin
  3036. // Valid exception return
  3037. end
  3038. else
  3039. Message(asmw_e_invalid_opcode_and_operands);
  3040. end;
  3041. bytes:=bytes or (1 shl 22);
  3042. end;
  3043. { reglist }
  3044. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  3045. end
  3046. else
  3047. begin
  3048. { push/pop }
  3049. { Set W and Rn to SP }
  3050. if opcode=A_PUSH then
  3051. bytes:=bytes or (1 shl 21);
  3052. bytes:=bytes or ($D shl 16);
  3053. { reglist }
  3054. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  3055. end;
  3056. { set P bit }
  3057. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  3058. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  3059. or (opcode=A_PUSH) then
  3060. bytes:=bytes or (1 shl 24);
  3061. { set U bit }
  3062. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  3063. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  3064. or (opcode=A_POP) then
  3065. bytes:=bytes or (1 shl 23);
  3066. end;
  3067. #$27: // SWP/SWPB
  3068. begin
  3069. { set instruction code }
  3070. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3071. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  3072. { set regs }
  3073. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3074. bytes:=bytes or getsupreg(oper[1]^.reg);
  3075. if ops=3 then
  3076. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3077. end;
  3078. #$28: // BX/BLX
  3079. begin
  3080. { set instruction code }
  3081. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3082. { set offset }
  3083. if oper[0]^.typ=top_const then
  3084. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  3085. else
  3086. begin
  3087. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3088. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  3089. begin
  3090. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  3091. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  3092. end
  3093. else
  3094. begin
  3095. offset:=((currsym.offset-insoffset-8) and $3fffffe);
  3096. { Turn BLX into BL if the destination isn't odd, could happen with recursion }
  3097. if not odd(offset shr 1) then
  3098. bytes:=(bytes and $EB000000) or $EB000000;
  3099. bytes:=bytes or ((offset shr 2) and $ffffff);
  3100. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  3101. end;
  3102. end;
  3103. end;
  3104. #$29: // SUB
  3105. begin
  3106. { set instruction code }
  3107. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3108. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3109. { set regs }
  3110. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3111. { set S if necessary }
  3112. if oppostfix=PF_S then
  3113. bytes:=bytes or (1 shl 20);
  3114. end;
  3115. #$2A:
  3116. begin
  3117. { set instruction code }
  3118. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3119. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3120. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3121. bytes:=bytes or ord(insentry^.code[4]);
  3122. { set opers }
  3123. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3124. if opcode in [A_SSAT, A_SSAT16] then
  3125. bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
  3126. else
  3127. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  3128. bytes:=bytes or getsupreg(oper[2]^.reg);
  3129. if (ops>3) and
  3130. (oper[3]^.typ=top_shifterop) and
  3131. (oper[3]^.shifterop^.rs=NR_NO) then
  3132. begin
  3133. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  3134. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  3135. bytes:=bytes or (1 shl 6)
  3136. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  3137. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3138. end;
  3139. end;
  3140. #$2B: // SETEND
  3141. begin
  3142. { set instruction code }
  3143. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3144. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3145. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3146. bytes:=bytes or ord(insentry^.code[4]);
  3147. { set endian specifier }
  3148. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  3149. end;
  3150. #$2C: // MOVW
  3151. begin
  3152. { set instruction code }
  3153. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3154. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3155. { set destination }
  3156. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3157. { set imm }
  3158. bytes:=bytes or (oper[1]^.val and $FFF);
  3159. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  3160. end;
  3161. #$2D: // BFX
  3162. begin
  3163. { set instruction code }
  3164. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3165. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3166. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3167. bytes:=bytes or ord(insentry^.code[4]);
  3168. if ops=3 then
  3169. begin
  3170. msb:=(oper[1]^.val+oper[2]^.val-1);
  3171. { set destination }
  3172. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3173. { set immediates }
  3174. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  3175. bytes:=bytes or ((msb and $1F) shl 16);
  3176. end
  3177. else
  3178. begin
  3179. if opcode in [A_BFC,A_BFI] then
  3180. msb:=(oper[2]^.val+oper[3]^.val-1)
  3181. else
  3182. msb:=oper[3]^.val-1;
  3183. { set destination }
  3184. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3185. bytes:=bytes or getsupreg(oper[1]^.reg);
  3186. { set immediates }
  3187. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3188. bytes:=bytes or ((msb and $1F) shl 16);
  3189. end;
  3190. end;
  3191. #$2E: // Cache stuff
  3192. begin
  3193. { set instruction code }
  3194. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3195. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3196. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3197. bytes:=bytes or ord(insentry^.code[4]);
  3198. { set code }
  3199. bytes:=bytes or (oper[0]^.val and $F);
  3200. end;
  3201. #$2F: // Nop
  3202. begin
  3203. { set instruction code }
  3204. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3205. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3206. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3207. bytes:=bytes or ord(insentry^.code[4]);
  3208. end;
  3209. #$30: // Shifts
  3210. begin
  3211. { set instruction code }
  3212. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3213. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3214. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3215. bytes:=bytes or ord(insentry^.code[4]);
  3216. { set destination }
  3217. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3218. bytes:=bytes or getsupreg(oper[1]^.reg);
  3219. if ops>2 then
  3220. begin
  3221. { set shift }
  3222. if oper[2]^.typ=top_reg then
  3223. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  3224. else
  3225. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3226. end;
  3227. { set S if necessary }
  3228. if oppostfix=PF_S then
  3229. bytes:=bytes or (1 shl 20);
  3230. end;
  3231. #$31: // BKPT
  3232. begin
  3233. { set instruction code }
  3234. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3235. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3236. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  3237. { set imm }
  3238. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  3239. bytes:=bytes or (oper[0]^.val and $F);
  3240. end;
  3241. #$32: // CLZ/REV
  3242. begin
  3243. { set instruction code }
  3244. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3245. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3246. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3247. bytes:=bytes or ord(insentry^.code[4]);
  3248. { set regs }
  3249. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3250. bytes:=bytes or getsupreg(oper[1]^.reg);
  3251. end;
  3252. #$33:
  3253. begin
  3254. { set instruction code }
  3255. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3256. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3257. { set regs }
  3258. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3259. if oper[1]^.typ=top_ref then
  3260. begin
  3261. { set offset }
  3262. offset:=0;
  3263. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3264. if assigned(currsym) then
  3265. offset:=currsym.offset-insoffset-8;
  3266. offset:=offset+oper[1]^.ref^.offset;
  3267. if offset>=0 then
  3268. begin
  3269. { set U flag }
  3270. bytes:=bytes or (1 shl 23);
  3271. bytes:=bytes or offset
  3272. end
  3273. else
  3274. begin
  3275. bytes:=bytes or (1 shl 22);
  3276. offset:=-offset;
  3277. bytes:=bytes or offset
  3278. end;
  3279. end
  3280. else
  3281. begin
  3282. if is_shifter_const(oper[1]^.val,r) then
  3283. begin
  3284. setshifterop(1);
  3285. bytes:=bytes or (1 shl 23);
  3286. end
  3287. else
  3288. begin
  3289. bytes:=bytes or (1 shl 22);
  3290. oper[1]^.val:=-oper[1]^.val;
  3291. setshifterop(1);
  3292. end;
  3293. end;
  3294. end;
  3295. #$40,#$90: // VMOV
  3296. begin
  3297. { set instruction code }
  3298. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3299. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3300. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3301. bytes:=bytes or ord(insentry^.code[4]);
  3302. { set regs }
  3303. Rd:=0;
  3304. Rn:=0;
  3305. Rm:=0;
  3306. case oppostfix of
  3307. PF_None:
  3308. begin
  3309. if ops=4 then
  3310. begin
  3311. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3312. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  3313. begin
  3314. Rd:=getmmreg(oper[0]^.reg);
  3315. Rm:=getsupreg(oper[2]^.reg);
  3316. Rn:=getsupreg(oper[3]^.reg);
  3317. end
  3318. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3319. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3320. begin
  3321. Rm:=getsupreg(oper[0]^.reg);
  3322. Rn:=getsupreg(oper[1]^.reg);
  3323. Rd:=getmmreg(oper[2]^.reg);
  3324. end
  3325. else
  3326. message(asmw_e_invalid_opcode_and_operands);
  3327. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  3328. bytes:=bytes or ((Rd and $1) shl 5);
  3329. bytes:=bytes or (Rm shl 12);
  3330. bytes:=bytes or (Rn shl 16);
  3331. end
  3332. else if ops=3 then
  3333. begin
  3334. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3335. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3336. begin
  3337. Rd:=getmmreg(oper[0]^.reg);
  3338. Rm:=getsupreg(oper[1]^.reg);
  3339. Rn:=getsupreg(oper[2]^.reg);
  3340. end
  3341. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3342. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3343. begin
  3344. Rm:=getsupreg(oper[0]^.reg);
  3345. Rn:=getsupreg(oper[1]^.reg);
  3346. Rd:=getmmreg(oper[2]^.reg);
  3347. end
  3348. else
  3349. message(asmw_e_invalid_opcode_and_operands);
  3350. bytes:=bytes or ((Rd and $F) shl 0);
  3351. bytes:=bytes or ((Rd and $10) shl 1);
  3352. bytes:=bytes or (Rm shl 12);
  3353. bytes:=bytes or (Rn shl 16);
  3354. end
  3355. else if ops=2 then
  3356. begin
  3357. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3358. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3359. begin
  3360. Rd:=getmmreg(oper[0]^.reg);
  3361. Rm:=getsupreg(oper[1]^.reg);
  3362. end
  3363. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3364. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  3365. begin
  3366. Rm:=getsupreg(oper[0]^.reg);
  3367. Rd:=getmmreg(oper[1]^.reg);
  3368. end
  3369. else
  3370. message(asmw_e_invalid_opcode_and_operands);
  3371. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  3372. bytes:=bytes or ((Rd and $1) shl 7);
  3373. bytes:=bytes or (Rm shl 12);
  3374. end;
  3375. end;
  3376. PF_F32:
  3377. begin
  3378. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3379. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3380. Message(asmw_e_invalid_opcode_and_operands);
  3381. Rd:=getmmreg(oper[0]^.reg);
  3382. Rm:=getmmreg(oper[1]^.reg);
  3383. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  3384. bytes:=bytes or ((Rd and $1) shl 22);
  3385. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  3386. bytes:=bytes or ((Rm and $1) shl 5);
  3387. end;
  3388. PF_F64:
  3389. begin
  3390. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3391. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3392. Message(asmw_e_invalid_opcode_and_operands);
  3393. Rd:=getmmreg(oper[0]^.reg);
  3394. Rm:=getmmreg(oper[1]^.reg);
  3395. bytes:=bytes or (1 shl 8);
  3396. bytes:=bytes or ((Rd and $F) shl 12);
  3397. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  3398. bytes:=bytes or (Rm and $F);
  3399. bytes:=bytes or ((Rm and $10) shl 1);
  3400. end;
  3401. end;
  3402. end;
  3403. #$41,#$91: // VMRS/VMSR
  3404. begin
  3405. { set instruction code }
  3406. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3407. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3408. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3409. bytes:=bytes or ord(insentry^.code[4]);
  3410. { set regs }
  3411. if opcode=A_VMRS then
  3412. begin
  3413. case oper[1]^.reg of
  3414. NR_FPSID: Rn:=$0;
  3415. NR_FPSCR: Rn:=$1;
  3416. NR_MVFR1: Rn:=$6;
  3417. NR_MVFR0: Rn:=$7;
  3418. NR_FPEXC: Rn:=$8;
  3419. else
  3420. Rn:=0;
  3421. message(asmw_e_invalid_opcode_and_operands);
  3422. end;
  3423. bytes:=bytes or (Rn shl 16);
  3424. if oper[0]^.reg=NR_APSR_nzcv then
  3425. bytes:=bytes or ($F shl 12)
  3426. else
  3427. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3428. end
  3429. else
  3430. begin
  3431. case oper[0]^.reg of
  3432. NR_FPSID: Rn:=$0;
  3433. NR_FPSCR: Rn:=$1;
  3434. NR_FPEXC: Rn:=$8;
  3435. else
  3436. Rn:=0;
  3437. message(asmw_e_invalid_opcode_and_operands);
  3438. end;
  3439. bytes:=bytes or (Rn shl 16);
  3440. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  3441. end;
  3442. end;
  3443. #$42,#$92: // VMUL
  3444. begin
  3445. { set instruction code }
  3446. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3447. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3448. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3449. bytes:=bytes or ord(insentry^.code[4]);
  3450. { set regs }
  3451. if ops=3 then
  3452. begin
  3453. Rd:=getmmreg(oper[0]^.reg);
  3454. Rn:=getmmreg(oper[1]^.reg);
  3455. Rm:=getmmreg(oper[2]^.reg);
  3456. end
  3457. else if oper[1]^.typ=top_const then
  3458. begin
  3459. Rd:=getmmreg(oper[0]^.reg);
  3460. Rn:=0;
  3461. Rm:=0;
  3462. end
  3463. else
  3464. begin
  3465. Rd:=getmmreg(oper[0]^.reg);
  3466. Rn:=0;
  3467. Rm:=getmmreg(oper[1]^.reg);
  3468. end;
  3469. if oppostfix=PF_F32 then
  3470. begin
  3471. D:=rd and $1; Rd:=Rd shr 1;
  3472. N:=rn and $1; Rn:=Rn shr 1;
  3473. M:=rm and $1; Rm:=Rm shr 1;
  3474. end
  3475. else
  3476. begin
  3477. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3478. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3479. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3480. bytes:=bytes or (1 shl 8);
  3481. end;
  3482. bytes:=bytes or (Rd shl 12);
  3483. bytes:=bytes or (Rn shl 16);
  3484. bytes:=bytes or (Rm shl 0);
  3485. bytes:=bytes or (D shl 22);
  3486. bytes:=bytes or (N shl 7);
  3487. bytes:=bytes or (M shl 5);
  3488. end;
  3489. #$43,#$93: // VCVT
  3490. begin
  3491. { set instruction code }
  3492. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3493. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3494. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3495. bytes:=bytes or ord(insentry^.code[4]);
  3496. { set regs }
  3497. Rd:=getmmreg(oper[0]^.reg);
  3498. Rm:=getmmreg(oper[1]^.reg);
  3499. if (ops=2) and
  3500. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3501. begin
  3502. if oppostfix=PF_F32F64 then
  3503. begin
  3504. bytes:=bytes or (1 shl 8);
  3505. D:=rd and $1; Rd:=Rd shr 1;
  3506. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3507. end
  3508. else
  3509. begin
  3510. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3511. M:=rm and $1; Rm:=Rm shr 1;
  3512. end;
  3513. bytes:=bytes and $FFF0FFFF;
  3514. bytes:=bytes or ($7 shl 16);
  3515. bytes:=bytes or (Rd shl 12);
  3516. bytes:=bytes or (Rm shl 0);
  3517. bytes:=bytes or (D shl 22);
  3518. bytes:=bytes or (M shl 5);
  3519. end
  3520. else if ops=2 then
  3521. begin
  3522. case oppostfix of
  3523. PF_S32F64,
  3524. PF_U32F64,
  3525. PF_F64S32,
  3526. PF_F64U32:
  3527. bytes:=bytes or (1 shl 8);
  3528. end;
  3529. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3530. begin
  3531. case oppostfix of
  3532. PF_S32F64,
  3533. PF_S32F32:
  3534. bytes:=bytes or (1 shl 16);
  3535. end;
  3536. bytes:=bytes or (1 shl 18);
  3537. D:=rd and $1; Rd:=Rd shr 1;
  3538. if oppostfix in [PF_S32F64,PF_U32F64] then
  3539. begin
  3540. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3541. end
  3542. else
  3543. begin
  3544. M:=rm and $1; Rm:=Rm shr 1;
  3545. end;
  3546. end
  3547. else
  3548. begin
  3549. case oppostfix of
  3550. PF_F64S32,
  3551. PF_F32S32:
  3552. bytes:=bytes or (1 shl 7);
  3553. else
  3554. bytes:=bytes and $FFFFFF7F;
  3555. end;
  3556. M:=rm and $1; Rm:=Rm shr 1;
  3557. if oppostfix in [PF_F64S32,PF_F64U32] then
  3558. begin
  3559. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3560. end
  3561. else
  3562. begin
  3563. D:=rd and $1; Rd:=Rd shr 1;
  3564. end
  3565. end;
  3566. bytes:=bytes or (Rd shl 12);
  3567. bytes:=bytes or (Rm shl 0);
  3568. bytes:=bytes or (D shl 22);
  3569. bytes:=bytes or (M shl 5);
  3570. end
  3571. else
  3572. begin
  3573. if rd<>rm then
  3574. message(asmw_e_invalid_opcode_and_operands);
  3575. case oppostfix of
  3576. PF_S32F32,PF_U32F32,
  3577. PF_F32S32,PF_F32U32,
  3578. PF_S32F64,PF_U32F64,
  3579. PF_F64S32,PF_F64U32:
  3580. begin
  3581. if not (oper[2]^.val in [1..32]) then
  3582. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3583. bytes:=bytes or (1 shl 7);
  3584. rn:=32;
  3585. end;
  3586. PF_S16F64,PF_U16F64,
  3587. PF_F64S16,PF_F64U16,
  3588. PF_S16F32,PF_U16F32,
  3589. PF_F32S16,PF_F32U16:
  3590. begin
  3591. if not (oper[2]^.val in [0..16]) then
  3592. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3593. rn:=16;
  3594. end;
  3595. else
  3596. Rn:=0;
  3597. message(asmw_e_invalid_opcode_and_operands);
  3598. end;
  3599. case oppostfix of
  3600. PF_S16F64,PF_U16F64,
  3601. PF_S32F64,PF_U32F64,
  3602. PF_F64S16,PF_F64U16,
  3603. PF_F64S32,PF_F64U32:
  3604. begin
  3605. bytes:=bytes or (1 shl 8);
  3606. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3607. end;
  3608. else
  3609. begin
  3610. D:=rd and $1; Rd:=Rd shr 1;
  3611. end;
  3612. end;
  3613. case oppostfix of
  3614. PF_U16F64,PF_U16F32,
  3615. PF_U32F32,PF_U32F64,
  3616. PF_F64U16,PF_F32U16,
  3617. PF_F32U32,PF_F64U32:
  3618. bytes:=bytes or (1 shl 16);
  3619. end;
  3620. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3621. bytes:=bytes or (1 shl 18);
  3622. bytes:=bytes or (Rd shl 12);
  3623. bytes:=bytes or (D shl 22);
  3624. rn:=rn-oper[2]^.val;
  3625. bytes:=bytes or ((rn and $1) shl 5);
  3626. bytes:=bytes or ((rn and $1E) shr 1);
  3627. end;
  3628. end;
  3629. #$44,#$94: // VLDM/VSTM/VPUSH/VPOP
  3630. begin
  3631. { set instruction code }
  3632. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3633. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3634. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3635. { set regs }
  3636. if ops=2 then
  3637. begin
  3638. if oper[0]^.typ=top_ref then
  3639. begin
  3640. Rn:=getsupreg(oper[0]^.ref^.index);
  3641. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3642. begin
  3643. { set W }
  3644. bytes:=bytes or (1 shl 21);
  3645. end
  3646. else if oppostfix = PF_DB then
  3647. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3648. end
  3649. else
  3650. begin
  3651. Rn:=getsupreg(oper[0]^.reg);
  3652. if oppostfix = PF_DB then
  3653. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3654. end;
  3655. bytes:=bytes or (Rn shl 16);
  3656. { Set PU bits }
  3657. case oppostfix of
  3658. PF_None,
  3659. PF_IA:
  3660. bytes:=bytes or (1 shl 23);
  3661. PF_DB:
  3662. bytes:=bytes or (2 shl 23);
  3663. end;
  3664. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3665. if oper[1]^.regset^=[] then
  3666. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3667. rd:=0;
  3668. for r:=0 to 31 do
  3669. if r in oper[1]^.regset^ then
  3670. begin
  3671. rd:=r;
  3672. break;
  3673. end;
  3674. rn:=32-rd;
  3675. for r:=rd+1 to 31 do
  3676. if not(r in oper[1]^.regset^) then
  3677. begin
  3678. rn:=r-rd;
  3679. break;
  3680. end;
  3681. if dp_operation then
  3682. begin
  3683. bytes:=bytes or (1 shl 8);
  3684. bytes:=bytes or (rn*2);
  3685. bytes:=bytes or ((rd and $F) shl 12);
  3686. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3687. end
  3688. else
  3689. begin
  3690. bytes:=bytes or rn;
  3691. bytes:=bytes or ((rd and $1) shl 22);
  3692. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3693. end;
  3694. end
  3695. else { VPUSH/VPOP }
  3696. begin
  3697. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3698. if oper[0]^.regset^=[] then
  3699. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3700. rd:=0;
  3701. for r:=0 to 31 do
  3702. if r in oper[0]^.regset^ then
  3703. begin
  3704. rd:=r;
  3705. break;
  3706. end;
  3707. rn:=32-rd;
  3708. for r:=rd+1 to 31 do
  3709. if not(r in oper[0]^.regset^) then
  3710. begin
  3711. rn:=r-rd;
  3712. break;
  3713. end;
  3714. if dp_operation then
  3715. begin
  3716. bytes:=bytes or (1 shl 8);
  3717. bytes:=bytes or (rn*2);
  3718. bytes:=bytes or ((rd and $F) shl 12);
  3719. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3720. end
  3721. else
  3722. begin
  3723. bytes:=bytes or rn;
  3724. bytes:=bytes or ((rd and $1) shl 22);
  3725. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3726. end;
  3727. end;
  3728. end;
  3729. #$45,#$95: // VLDR/VSTR
  3730. begin
  3731. { set instruction code }
  3732. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3733. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3734. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3735. { set regs }
  3736. rd:=getmmreg(oper[0]^.reg);
  3737. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3738. begin
  3739. bytes:=bytes or (1 shl 8);
  3740. bytes:=bytes or ((rd and $F) shl 12);
  3741. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3742. end
  3743. else
  3744. begin
  3745. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3746. bytes:=bytes or ((rd and $1) shl 22);
  3747. end;
  3748. { set ref }
  3749. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3750. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3751. begin
  3752. { set offset }
  3753. offset:=0;
  3754. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3755. if assigned(currsym) then
  3756. offset:=currsym.offset-insoffset-8;
  3757. offset:=offset+oper[1]^.ref^.offset;
  3758. offset:=offset div 4;
  3759. if offset>=0 then
  3760. begin
  3761. { set U flag }
  3762. bytes:=bytes or (1 shl 23);
  3763. bytes:=bytes or offset
  3764. end
  3765. else
  3766. begin
  3767. offset:=-offset;
  3768. bytes:=bytes or offset
  3769. end;
  3770. end
  3771. else
  3772. message(asmw_e_invalid_opcode_and_operands);
  3773. end;
  3774. #$46: { System instructions }
  3775. begin
  3776. { set instruction code }
  3777. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3778. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3779. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3780. { set regs }
  3781. if (oper[0]^.typ=top_modeflags) then
  3782. begin
  3783. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 8);
  3784. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  3785. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  3786. end;
  3787. if (ops=2) then
  3788. bytes:=bytes or (oper[1]^.val and $1F)
  3789. else if (ops=1) and
  3790. (oper[0]^.typ=top_const) then
  3791. bytes:=bytes or (oper[0]^.val and $1F);
  3792. end;
  3793. #$60: { Thumb }
  3794. begin
  3795. bytelen:=2;
  3796. bytes:=0;
  3797. { set opcode }
  3798. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3799. bytes:=bytes or ord(insentry^.code[2]);
  3800. { set regs }
  3801. if ops=2 then
  3802. begin
  3803. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3804. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3805. if (oper[1]^.typ=top_reg) then
  3806. bytes:=bytes or ((getsupreg(oper[1]^.reg) and $7) shl 6)
  3807. else
  3808. bytes:=bytes or ((oper[1]^.val and $1F) shl 6);
  3809. end
  3810. else if ops=3 then
  3811. begin
  3812. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3813. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3814. if (oper[2]^.typ=top_reg) then
  3815. bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6)
  3816. else
  3817. bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
  3818. end
  3819. else if ops=1 then
  3820. begin
  3821. if oper[0]^.typ=top_const then
  3822. bytes:=bytes or (oper[0]^.val and $FF);
  3823. end;
  3824. end;
  3825. #$61: { Thumb }
  3826. begin
  3827. bytelen:=2;
  3828. bytes:=0;
  3829. { set opcode }
  3830. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3831. bytes:=bytes or ord(insentry^.code[2]);
  3832. { set regs }
  3833. if ops=2 then
  3834. begin
  3835. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3836. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3837. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3838. end
  3839. else if ops=1 then
  3840. begin
  3841. if oper[0]^.typ=top_const then
  3842. bytes:=bytes or (oper[0]^.val and $FF);
  3843. end;
  3844. end;
  3845. #$62..#$63: { Thumb branches }
  3846. begin
  3847. bytelen:=2;
  3848. bytes:=0;
  3849. { set opcode }
  3850. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3851. bytes:=bytes or ord(insentry^.code[2]);
  3852. if insentry^.code[0]=#$63 then
  3853. bytes:=bytes or (CondVal[condition] shl 8);
  3854. if oper[0]^.typ=top_const then
  3855. begin
  3856. if insentry^.code[0]=#$63 then
  3857. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
  3858. else
  3859. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
  3860. end
  3861. else if oper[0]^.typ=top_reg then
  3862. begin
  3863. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3864. end
  3865. else if oper[0]^.typ=top_ref then
  3866. begin
  3867. offset:=0;
  3868. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3869. if assigned(currsym) then
  3870. offset:=currsym.offset-insoffset-8;
  3871. offset:=offset+oper[0]^.ref^.offset;
  3872. if insentry^.code[0]=#$63 then
  3873. bytes:=bytes or (((offset+4) shr 1) and $FF)
  3874. else
  3875. bytes:=bytes or (((offset+4) shr 1) and $7FF);
  3876. end
  3877. end;
  3878. #$64: { Thumb: Special encodings }
  3879. begin
  3880. bytelen:=2;
  3881. bytes:=0;
  3882. { set opcode }
  3883. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3884. bytes:=bytes or ord(insentry^.code[2]);
  3885. case opcode of
  3886. A_SUB:
  3887. begin
  3888. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3889. if (ops=3) and
  3890. (oper[2]^.typ=top_const) then
  3891. bytes:=bytes or ((oper[2]^.val shr 2) and $7F)
  3892. else if (ops=2) and
  3893. (oper[1]^.typ=top_const) then
  3894. bytes:=bytes or ((oper[1]^.val shr 2) and $7F);
  3895. end;
  3896. A_MUL:
  3897. if (ops in [2,3]) then
  3898. begin
  3899. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3900. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3901. end;
  3902. A_ADD:
  3903. begin
  3904. if ops=2 then
  3905. begin
  3906. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3907. bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
  3908. end
  3909. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3910. (oper[2]^.typ=top_const) then
  3911. begin
  3912. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
  3913. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3914. end
  3915. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3916. (oper[2]^.typ=top_reg) then
  3917. begin
  3918. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3919. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3920. end
  3921. else
  3922. begin
  3923. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3924. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3925. end;
  3926. end;
  3927. end;
  3928. end;
  3929. #$65: { Thumb load/store }
  3930. begin
  3931. bytelen:=2;
  3932. bytes:=0;
  3933. { set opcode }
  3934. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3935. bytes:=bytes or ord(insentry^.code[2]);
  3936. { set regs }
  3937. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3938. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3939. bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
  3940. end;
  3941. #$66: { Thumb load/store }
  3942. begin
  3943. bytelen:=2;
  3944. bytes:=0;
  3945. { set opcode }
  3946. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3947. bytes:=bytes or ord(insentry^.code[2]);
  3948. { set regs }
  3949. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3950. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3951. bytes:=bytes or (((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $1F) shl 6);
  3952. end;
  3953. #$67: { Thumb load/store }
  3954. begin
  3955. bytelen:=2;
  3956. bytes:=0;
  3957. { set opcode }
  3958. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3959. bytes:=bytes or ord(insentry^.code[2]);
  3960. { set regs }
  3961. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3962. if oper[1]^.typ=top_ref then
  3963. bytes:=bytes or ((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $FF)
  3964. else
  3965. bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
  3966. end;
  3967. #$68: { Thumb CB[N]Z }
  3968. begin
  3969. bytelen:=2;
  3970. bytes:=0;
  3971. { set opcode }
  3972. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3973. { set opers }
  3974. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3975. if oper[1]^.typ=top_ref then
  3976. begin
  3977. offset:=0;
  3978. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3979. if assigned(currsym) then
  3980. offset:=currsym.offset-insoffset-8;
  3981. offset:=offset+oper[1]^.ref^.offset;
  3982. offset:=offset div 2;
  3983. end
  3984. else
  3985. offset:=oper[1]^.val div 2;
  3986. bytes:=bytes or ((offset) and $1F) shl 3;
  3987. bytes:=bytes or ((offset shr 5) and 1) shl 9;
  3988. end;
  3989. #$69: { Thumb: Push/Pop/Stm/Ldm }
  3990. begin
  3991. bytelen:=2;
  3992. bytes:=0;
  3993. { set opcode }
  3994. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3995. case opcode of
  3996. A_PUSH:
  3997. begin
  3998. for r:=0 to 7 do
  3999. if r in oper[0]^.regset^ then
  4000. bytes:=bytes or (1 shl r);
  4001. if RS_R14 in oper[0]^.regset^ then
  4002. bytes:=bytes or (1 shl 8);
  4003. end;
  4004. A_POP:
  4005. begin
  4006. for r:=0 to 7 do
  4007. if r in oper[0]^.regset^ then
  4008. bytes:=bytes or (1 shl r);
  4009. if RS_R15 in oper[0]^.regset^ then
  4010. bytes:=bytes or (1 shl 8);
  4011. end;
  4012. A_STM:
  4013. begin
  4014. for r:=0 to 7 do
  4015. if r in oper[1]^.regset^ then
  4016. bytes:=bytes or (1 shl r);
  4017. if oper[0]^.typ=top_ref then
  4018. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  4019. else
  4020. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4021. end;
  4022. A_LDM:
  4023. begin
  4024. for r:=0 to 7 do
  4025. if r in oper[1]^.regset^ then
  4026. bytes:=bytes or (1 shl r);
  4027. if oper[0]^.typ=top_ref then
  4028. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  4029. else
  4030. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4031. end;
  4032. end;
  4033. end;
  4034. #$6A: { Thumb: IT }
  4035. begin
  4036. bytelen:=2;
  4037. bytes:=0;
  4038. { set opcode }
  4039. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4040. bytes:=bytes or (ord(insentry^.code[2]) shl 0);
  4041. bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
  4042. i_field:=(bytes shr 4) and 1;
  4043. i_field:=(i_field shl 1) or i_field;
  4044. i_field:=(i_field shl 2) or i_field;
  4045. bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
  4046. end;
  4047. #$6B: { Thumb: Data processing (misc) }
  4048. begin
  4049. bytelen:=2;
  4050. bytes:=0;
  4051. { set opcode }
  4052. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4053. bytes:=bytes or ord(insentry^.code[2]);
  4054. { set regs }
  4055. if ops>=2 then
  4056. begin
  4057. if oper[1]^.typ=top_const then
  4058. begin
  4059. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
  4060. bytes:=bytes or (oper[1]^.val and $FF);
  4061. end
  4062. else if oper[1]^.typ=top_reg then
  4063. begin
  4064. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4065. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4066. end;
  4067. end
  4068. else if ops=1 then
  4069. begin
  4070. if oper[0]^.typ=top_const then
  4071. bytes:=bytes or (oper[0]^.val and $FF);
  4072. end;
  4073. end;
  4074. #$6C: { Thumb: CPS }
  4075. begin
  4076. bytelen:=2;
  4077. bytes:=0;
  4078. { set opcode }
  4079. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4080. bytes:=bytes or ord(insentry^.code[2]);
  4081. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 2);
  4082. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 1);
  4083. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 0);
  4084. end;
  4085. #$80: { Thumb-2: Dataprocessing }
  4086. begin
  4087. bytes:=0;
  4088. { set instruction code }
  4089. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4090. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4091. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4092. bytes:=bytes or ord(insentry^.code[4]);
  4093. if ops=1 then
  4094. begin
  4095. if oper[0]^.typ=top_reg then
  4096. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4097. else if oper[0]^.typ=top_const then
  4098. bytes:=bytes or (oper[0]^.val and $F);
  4099. end
  4100. else if (ops=2) and
  4101. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4102. begin
  4103. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4104. if oper[1]^.typ=top_const then
  4105. encodethumbimm(oper[1]^.val)
  4106. else if oper[1]^.typ=top_reg then
  4107. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4108. end
  4109. else if (ops=3) and
  4110. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4111. begin
  4112. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4113. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4114. if oper[2]^.typ=top_shifterop then
  4115. setthumbshift(2)
  4116. else if oper[2]^.typ=top_reg then
  4117. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
  4118. end
  4119. else if (ops=2) and
  4120. (opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
  4121. begin
  4122. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4123. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4124. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4125. end
  4126. else if ops=2 then
  4127. begin
  4128. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4129. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4130. if oper[1]^.typ=top_const then
  4131. encodethumbimm(oper[1]^.val)
  4132. else if oper[1]^.typ=top_reg then
  4133. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4134. end
  4135. else if ops=3 then
  4136. begin
  4137. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4138. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4139. if oper[2]^.typ=top_const then
  4140. encodethumbimm(oper[2]^.val)
  4141. else if oper[2]^.typ=top_reg then
  4142. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4143. end
  4144. else if ops=4 then
  4145. begin
  4146. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4147. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4148. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4149. if oper[3]^.typ=top_shifterop then
  4150. setthumbshift(3)
  4151. else if oper[3]^.typ=top_reg then
  4152. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
  4153. end;
  4154. if oppostfix=PF_S then
  4155. bytes:=bytes or (1 shl 20)
  4156. else if oppostfix=PF_X then
  4157. bytes:=bytes or (1 shl 4)
  4158. else if oppostfix=PF_R then
  4159. bytes:=bytes or (1 shl 4);
  4160. end;
  4161. #$81: { Thumb-2: Dataprocessing misc }
  4162. begin
  4163. bytes:=0;
  4164. { set instruction code }
  4165. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4166. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4167. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4168. bytes:=bytes or ord(insentry^.code[4]);
  4169. if ops=3 then
  4170. begin
  4171. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4172. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4173. if oper[2]^.typ=top_const then
  4174. begin
  4175. bytes:=bytes or (oper[2]^.val and $FF);
  4176. bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
  4177. bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
  4178. end;
  4179. end
  4180. else if ops=2 then
  4181. begin
  4182. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4183. offset:=0;
  4184. if oper[1]^.typ=top_const then
  4185. begin
  4186. offset:=oper[1]^.val;
  4187. end
  4188. else if oper[1]^.typ=top_ref then
  4189. begin
  4190. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4191. if assigned(currsym) then
  4192. offset:=currsym.offset-insoffset-8;
  4193. offset:=offset+oper[1]^.ref^.offset;
  4194. offset:=offset;
  4195. end;
  4196. bytes:=bytes or (offset and $FF);
  4197. bytes:=bytes or ((offset and $700) shr 8) shl 12;
  4198. bytes:=bytes or ((offset and $800) shr 11) shl 26;
  4199. bytes:=bytes or ((offset and $F000) shr 12) shl 16;
  4200. end;
  4201. if oppostfix=PF_S then
  4202. bytes:=bytes or (1 shl 20);
  4203. end;
  4204. #$82: { Thumb-2: Shifts }
  4205. begin
  4206. bytes:=0;
  4207. { set instruction code }
  4208. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4209. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4210. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4211. bytes:=bytes or ord(insentry^.code[4]);
  4212. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4213. if oper[1]^.typ=top_reg then
  4214. begin
  4215. offset:=2;
  4216. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4217. end
  4218. else
  4219. begin
  4220. offset:=1;
  4221. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
  4222. end;
  4223. if oper[offset]^.typ=top_const then
  4224. begin
  4225. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4226. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4227. end
  4228. else if oper[offset]^.typ=top_reg then
  4229. bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
  4230. if (ops>=(offset+2)) and
  4231. (oper[offset+1]^.typ=top_const) then
  4232. bytes:=bytes or (oper[offset+1]^.val and $1F);
  4233. if oppostfix=PF_S then
  4234. bytes:=bytes or (1 shl 20);
  4235. end;
  4236. #$84: { Thumb-2: Shifts(width-1) }
  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. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4245. if oper[1]^.typ=top_reg then
  4246. begin
  4247. offset:=2;
  4248. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4249. end
  4250. else
  4251. offset:=1;
  4252. if oper[offset]^.typ=top_const then
  4253. begin
  4254. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4255. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4256. end;
  4257. if (ops>=(offset+2)) and
  4258. (oper[offset+1]^.typ=top_const) then
  4259. begin
  4260. if opcode in [A_BFI,A_BFC] then
  4261. i_field:=oper[offset+1]^.val+oper[offset]^.val-1
  4262. else
  4263. i_field:=oper[offset+1]^.val-1;
  4264. bytes:=bytes or (i_field and $1F);
  4265. end;
  4266. if oppostfix=PF_S then
  4267. bytes:=bytes or (1 shl 20);
  4268. end;
  4269. #$83: { Thumb-2: Saturation }
  4270. begin
  4271. bytes:=0;
  4272. { set instruction code }
  4273. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4274. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4275. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4276. bytes:=bytes or ord(insentry^.code[4]);
  4277. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4278. bytes:=bytes or (oper[1]^.val and $1F);
  4279. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4280. if ops=4 then
  4281. setthumbshift(3,true);
  4282. end;
  4283. #$85: { Thumb-2: Long multiplications }
  4284. begin
  4285. bytes:=0;
  4286. { set instruction code }
  4287. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4288. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4289. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4290. bytes:=bytes or ord(insentry^.code[4]);
  4291. if ops=4 then
  4292. begin
  4293. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4294. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
  4295. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4296. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
  4297. end;
  4298. if oppostfix=PF_S then
  4299. bytes:=bytes or (1 shl 20)
  4300. else if oppostfix=PF_X then
  4301. bytes:=bytes or (1 shl 4);
  4302. end;
  4303. #$86: { Thumb-2: Extension ops }
  4304. begin
  4305. bytes:=0;
  4306. { set instruction code }
  4307. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4308. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4309. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4310. bytes:=bytes or ord(insentry^.code[4]);
  4311. if ops=2 then
  4312. begin
  4313. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4314. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4315. end
  4316. else if ops=3 then
  4317. begin
  4318. if oper[2]^.typ=top_shifterop then
  4319. begin
  4320. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4321. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4322. bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
  4323. end
  4324. else
  4325. begin
  4326. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4327. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4328. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4329. end;
  4330. end
  4331. else if ops=4 then
  4332. begin
  4333. if oper[3]^.typ=top_shifterop then
  4334. begin
  4335. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4336. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4337. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4338. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
  4339. end;
  4340. end;
  4341. end;
  4342. #$87: { Thumb-2: PLD/PLI }
  4343. begin
  4344. { set instruction code }
  4345. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4346. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4347. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4348. bytes:=bytes or ord(insentry^.code[4]);
  4349. { set Rn and Rd }
  4350. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4351. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4352. begin
  4353. { set offset }
  4354. offset:=0;
  4355. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4356. if assigned(currsym) then
  4357. offset:=currsym.offset-insoffset-8;
  4358. offset:=offset+oper[0]^.ref^.offset;
  4359. if offset>=0 then
  4360. begin
  4361. { set U flag }
  4362. bytes:=bytes or (1 shl 23);
  4363. bytes:=bytes or (offset and $FFF);
  4364. end
  4365. else
  4366. begin
  4367. bytes:=bytes or ($3 shl 10);
  4368. offset:=-offset;
  4369. bytes:=bytes or (offset and $FF);
  4370. end;
  4371. end
  4372. else
  4373. begin
  4374. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4375. { set shift }
  4376. with oper[0]^.ref^ do
  4377. if shiftmode=SM_LSL then
  4378. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4379. end;
  4380. end;
  4381. #$88: { Thumb-2: LDR/STR }
  4382. begin
  4383. { set instruction code }
  4384. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4385. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4386. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4387. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4388. { set Rn and Rd }
  4389. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4390. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4391. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4392. begin
  4393. { set offset }
  4394. offset:=0;
  4395. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4396. if assigned(currsym) then
  4397. offset:=currsym.offset-insoffset-8;
  4398. offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
  4399. if offset>=0 then
  4400. begin
  4401. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4402. bytes:=bytes or (1 shl 23);
  4403. { set U flag }
  4404. if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
  4405. bytes:=bytes or (1 shl 9);
  4406. bytes:=bytes or offset
  4407. end
  4408. else
  4409. begin
  4410. bytes:=bytes or (1 shl 11);
  4411. offset:=-offset;
  4412. bytes:=bytes or offset
  4413. end;
  4414. end
  4415. else
  4416. begin
  4417. { set I flag }
  4418. bytes:=bytes or (1 shl 25);
  4419. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  4420. { set shift }
  4421. with oper[1]^.ref^ do
  4422. if shiftmode<>SM_None then
  4423. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4424. end;
  4425. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4426. begin
  4427. { set W bit }
  4428. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4429. bytes:=bytes or (1 shl 8);
  4430. { set P bit if necessary }
  4431. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  4432. bytes:=bytes or (1 shl 10);
  4433. end;
  4434. end;
  4435. #$89: { Thumb-2: LDRD/STRD }
  4436. begin
  4437. { set instruction code }
  4438. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4439. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4440. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4441. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4442. { set Rn and Rd }
  4443. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4444. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4445. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4446. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4447. begin
  4448. { set offset }
  4449. offset:=0;
  4450. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4451. if assigned(currsym) then
  4452. offset:=currsym.offset-insoffset-8;
  4453. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4454. if offset>=0 then
  4455. begin
  4456. { set U flag }
  4457. bytes:=bytes or (1 shl 23);
  4458. bytes:=bytes or offset
  4459. end
  4460. else
  4461. begin
  4462. offset:=-offset;
  4463. bytes:=bytes or offset
  4464. end;
  4465. end
  4466. else
  4467. begin
  4468. message(asmw_e_invalid_opcode_and_operands);
  4469. end;
  4470. { set W bit }
  4471. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4472. bytes:=bytes or (1 shl 21);
  4473. { set P bit if necessary }
  4474. if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
  4475. bytes:=bytes or (1 shl 24);
  4476. end;
  4477. #$8A: { Thumb-2: LDREX }
  4478. begin
  4479. { set instruction code }
  4480. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4481. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4482. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4483. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4484. { set Rn and Rd }
  4485. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4486. if (ops=2) and (opcode in [A_LDREX]) then
  4487. begin
  4488. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4489. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4490. begin
  4491. { set offset }
  4492. offset:=0;
  4493. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4494. if assigned(currsym) then
  4495. offset:=currsym.offset-insoffset-8;
  4496. offset:=(offset+oper[1]^.ref^.offset) div 4;
  4497. if offset>=0 then
  4498. begin
  4499. bytes:=bytes or offset
  4500. end
  4501. else
  4502. begin
  4503. message(asmw_e_invalid_opcode_and_operands);
  4504. end;
  4505. end
  4506. else
  4507. begin
  4508. message(asmw_e_invalid_opcode_and_operands);
  4509. end;
  4510. end
  4511. else if (ops=2) then
  4512. begin
  4513. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4514. end
  4515. else
  4516. begin
  4517. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4518. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4519. end;
  4520. end;
  4521. #$8B: { Thumb-2: STREX }
  4522. begin
  4523. { set instruction code }
  4524. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4525. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4526. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4527. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4528. { set Rn and Rd }
  4529. if (ops=3) and (opcode in [A_STREX]) then
  4530. begin
  4531. bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
  4532. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4533. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4534. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4535. begin
  4536. { set offset }
  4537. offset:=0;
  4538. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4539. if assigned(currsym) then
  4540. offset:=currsym.offset-insoffset-8;
  4541. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4542. if offset>=0 then
  4543. begin
  4544. bytes:=bytes or offset
  4545. end
  4546. else
  4547. begin
  4548. message(asmw_e_invalid_opcode_and_operands);
  4549. end;
  4550. end
  4551. else
  4552. begin
  4553. message(asmw_e_invalid_opcode_and_operands);
  4554. end;
  4555. end
  4556. else if (ops=3) then
  4557. begin
  4558. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4559. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4560. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4561. end
  4562. else
  4563. begin
  4564. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4565. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4566. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  4567. bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
  4568. end;
  4569. end;
  4570. #$8C: { Thumb-2: LDM/STM }
  4571. begin
  4572. { set instruction code }
  4573. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4574. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4575. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4576. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4577. if oper[0]^.typ=top_reg then
  4578. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4579. else
  4580. begin
  4581. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
  4582. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  4583. bytes:=bytes or (1 shl 21);
  4584. end;
  4585. for r:=0 to 15 do
  4586. if r in oper[1]^.regset^ then
  4587. bytes:=bytes or (1 shl r);
  4588. case oppostfix of
  4589. PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
  4590. PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
  4591. end;
  4592. end;
  4593. #$8D: { Thumb-2: BL/BLX }
  4594. begin
  4595. { set instruction code }
  4596. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4597. bytes:=bytes or (ord(insentry^.code[2]) shl 8);
  4598. { set offset }
  4599. if oper[0]^.typ=top_const then
  4600. offset:=(oper[0]^.val shr 1) and $FFFFFF
  4601. else
  4602. begin
  4603. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4604. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  4605. begin
  4606. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  4607. offset:=$FFFFFE
  4608. end
  4609. else
  4610. offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
  4611. end;
  4612. bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
  4613. bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
  4614. bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
  4615. bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
  4616. bytes:=bytes or ((offset shr 23) and $1) shl 26;
  4617. end;
  4618. #$8E: { Thumb-2: TBB/TBH }
  4619. begin
  4620. { set instruction code }
  4621. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4622. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4623. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4624. bytes:=bytes or ord(insentry^.code[4]);
  4625. { set Rn and Rm }
  4626. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4627. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4628. message(asmw_e_invalid_effective_address)
  4629. else
  4630. begin
  4631. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4632. if (opcode=A_TBH) and
  4633. (oper[0]^.ref^.shiftmode<>SM_LSL) and
  4634. (oper[0]^.ref^.shiftimm<>1) then
  4635. message(asmw_e_invalid_effective_address);
  4636. end;
  4637. end;
  4638. #$8F: { Thumb-2: CPSxx }
  4639. begin
  4640. { set opcode }
  4641. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4642. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4643. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4644. bytes:=bytes or ord(insentry^.code[4]);
  4645. if (oper[0]^.typ=top_modeflags) then
  4646. begin
  4647. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  4648. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  4649. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 5);
  4650. end;
  4651. if (ops=2) then
  4652. bytes:=bytes or (oper[1]^.val and $1F)
  4653. else if (ops=1) and
  4654. (oper[0]^.typ=top_const) then
  4655. bytes:=bytes or (oper[0]^.val and $1F);
  4656. end;
  4657. #$96: { Thumb-2: MSR/MRS }
  4658. begin
  4659. { set instruction code }
  4660. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4661. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4662. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4663. bytes:=bytes or ord(insentry^.code[4]);
  4664. if opcode=A_MRS then
  4665. begin
  4666. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4667. case oper[1]^.reg of
  4668. NR_MSP: bytes:=bytes or $08;
  4669. NR_PSP: bytes:=bytes or $09;
  4670. NR_IPSR: bytes:=bytes or $05;
  4671. NR_EPSR: bytes:=bytes or $06;
  4672. NR_APSR: bytes:=bytes or $00;
  4673. NR_PRIMASK: bytes:=bytes or $10;
  4674. NR_BASEPRI: bytes:=bytes or $11;
  4675. NR_BASEPRI_MAX: bytes:=bytes or $12;
  4676. NR_FAULTMASK: bytes:=bytes or $13;
  4677. NR_CONTROL: bytes:=bytes or $14;
  4678. else
  4679. Message(asmw_e_invalid_opcode_and_operands);
  4680. end;
  4681. end
  4682. else
  4683. begin
  4684. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4685. case oper[0]^.reg of
  4686. NR_APSR,
  4687. NR_APSR_nzcvqg: bytes:=bytes or $C00;
  4688. NR_APSR_g: bytes:=bytes or $400;
  4689. NR_APSR_nzcvq: bytes:=bytes or $800;
  4690. NR_MSP: bytes:=bytes or $08;
  4691. NR_PSP: bytes:=bytes or $09;
  4692. NR_PRIMASK: bytes:=bytes or $10;
  4693. NR_BASEPRI: bytes:=bytes or $11;
  4694. NR_BASEPRI_MAX: bytes:=bytes or $12;
  4695. NR_FAULTMASK: bytes:=bytes or $13;
  4696. NR_CONTROL: bytes:=bytes or $14;
  4697. else
  4698. Message(asmw_e_invalid_opcode_and_operands);
  4699. end;
  4700. end;
  4701. end;
  4702. #$A0: { FPA: CPDT(LDF/STF) }
  4703. begin
  4704. { set instruction code }
  4705. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4706. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4707. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4708. bytes:=bytes or ord(insentry^.code[4]);
  4709. if ops=2 then
  4710. begin
  4711. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4712. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4713. bytes:=bytes or ((oper[1]^.ref^.offset shr 2) and $FF);
  4714. if oper[1]^.ref^.offset>=0 then
  4715. bytes:=bytes or (1 shl 23);
  4716. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4717. bytes:=bytes or (1 shl 21);
  4718. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  4719. bytes:=bytes or (1 shl 24);
  4720. case oppostfix of
  4721. PF_D: bytes:=bytes or (0 shl 22) or (1 shl 15);
  4722. PF_E: bytes:=bytes or (1 shl 22) or (0 shl 15);
  4723. PF_P: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4724. end;
  4725. end
  4726. else
  4727. begin
  4728. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4729. case oper[1]^.val of
  4730. 1: bytes:=bytes or (1 shl 15);
  4731. 2: bytes:=bytes or (1 shl 22);
  4732. 3: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4733. 4: ;
  4734. else
  4735. message1(asmw_e_invalid_opcode_and_operands, 'Invalid count for LFM/SFM');
  4736. end;
  4737. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4738. bytes:=bytes or ((oper[2]^.ref^.offset shr 2) and $FF);
  4739. if oper[2]^.ref^.offset>=0 then
  4740. bytes:=bytes or (1 shl 23);
  4741. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4742. bytes:=bytes or (1 shl 21);
  4743. if oper[2]^.ref^.addressmode=AM_PREINDEXED then
  4744. bytes:=bytes or (1 shl 24);
  4745. end;
  4746. end;
  4747. #$A1: { FPA: CPDO }
  4748. begin
  4749. { set instruction code }
  4750. bytes:=bytes or ($E shl 24);
  4751. bytes:=bytes or (ord(insentry^.code[1]) shl 15);
  4752. bytes:=bytes or ((ord(insentry^.code[2]) shr 1) shl 20);
  4753. bytes:=bytes or (1 shl 8);
  4754. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4755. if ops=2 then
  4756. begin
  4757. if oper[1]^.typ=top_reg then
  4758. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4759. else
  4760. case oper[1]^.val of
  4761. 0: bytes:=bytes or $8;
  4762. 1: bytes:=bytes or $9;
  4763. 2: bytes:=bytes or $A;
  4764. 3: bytes:=bytes or $B;
  4765. 4: bytes:=bytes or $C;
  4766. 5: bytes:=bytes or $D;
  4767. //0.5: bytes:=bytes or $E;
  4768. 10: bytes:=bytes or $F;
  4769. else
  4770. Message(asmw_e_invalid_opcode_and_operands);
  4771. end;
  4772. end
  4773. else
  4774. begin
  4775. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  4776. if oper[2]^.typ=top_reg then
  4777. bytes:=bytes or getsupreg(oper[2]^.reg) shl 0
  4778. else
  4779. case oper[2]^.val of
  4780. 0: bytes:=bytes or $8;
  4781. 1: bytes:=bytes or $9;
  4782. 2: bytes:=bytes or $A;
  4783. 3: bytes:=bytes or $B;
  4784. 4: bytes:=bytes or $C;
  4785. 5: bytes:=bytes or $D;
  4786. //0.5: bytes:=bytes or $E;
  4787. 10: bytes:=bytes or $F;
  4788. else
  4789. Message(asmw_e_invalid_opcode_and_operands);
  4790. end;
  4791. end;
  4792. case roundingmode of
  4793. RM_P: bytes:=bytes or (1 shl 5);
  4794. RM_M: bytes:=bytes or (2 shl 5);
  4795. RM_Z: bytes:=bytes or (3 shl 5);
  4796. end;
  4797. case oppostfix of
  4798. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4799. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4800. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4801. else
  4802. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4803. end;
  4804. end;
  4805. #$A2: { FPA: CPDO }
  4806. begin
  4807. { set instruction code }
  4808. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4809. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4810. bytes:=bytes or ($11 shl 4);
  4811. case opcode of
  4812. A_FLT:
  4813. begin
  4814. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4815. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  4816. case roundingmode of
  4817. RM_P: bytes:=bytes or (1 shl 5);
  4818. RM_M: bytes:=bytes or (2 shl 5);
  4819. RM_Z: bytes:=bytes or (3 shl 5);
  4820. end;
  4821. case oppostfix of
  4822. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4823. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4824. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4825. else
  4826. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4827. end;
  4828. end;
  4829. A_FIX:
  4830. begin
  4831. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4832. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4833. case roundingmode of
  4834. RM_P: bytes:=bytes or (1 shl 5);
  4835. RM_M: bytes:=bytes or (2 shl 5);
  4836. RM_Z: bytes:=bytes or (3 shl 5);
  4837. end;
  4838. end;
  4839. A_WFS,A_RFS,A_WFC,A_RFC:
  4840. begin
  4841. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4842. end;
  4843. A_CMF,A_CNF,A_CMFE,A_CNFE:
  4844. begin
  4845. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4846. if oper[1]^.typ=top_reg then
  4847. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4848. else
  4849. case oper[1]^.val of
  4850. 0: bytes:=bytes or $8;
  4851. 1: bytes:=bytes or $9;
  4852. 2: bytes:=bytes or $A;
  4853. 3: bytes:=bytes or $B;
  4854. 4: bytes:=bytes or $C;
  4855. 5: bytes:=bytes or $D;
  4856. //0.5: bytes:=bytes or $E;
  4857. 10: bytes:=bytes or $F;
  4858. else
  4859. Message(asmw_e_invalid_opcode_and_operands);
  4860. end;
  4861. end;
  4862. end;
  4863. end;
  4864. #$fe: // No written data
  4865. begin
  4866. exit;
  4867. end;
  4868. #$ff:
  4869. internalerror(2005091101);
  4870. else
  4871. begin
  4872. writeln(ord(insentry^.code[0]), ' - ', opcode);
  4873. internalerror(2005091102);
  4874. end;
  4875. end;
  4876. { Todo: Decide whether the code above should take care of writing data in an order that makes senes }
  4877. if (insentry^.code[0] in [#$80..#$96]) and (bytelen=4) then
  4878. bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
  4879. { we're finished, write code }
  4880. objdata.writebytes(bytes,bytelen);
  4881. end;
  4882. begin
  4883. cai_align:=tai_align;
  4884. end.