aasmcpu.pas 194 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287
  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. { reglist }
  3030. bytes:=bytes or MakeRegList(oper[1]^.regset^);
  3031. end
  3032. else
  3033. begin
  3034. { push/pop }
  3035. { Set W and Rn to SP }
  3036. if opcode=A_PUSH then
  3037. bytes:=bytes or (1 shl 21);
  3038. bytes:=bytes or ($D shl 16);
  3039. { reglist }
  3040. bytes:=bytes or MakeRegList(oper[0]^.regset^);
  3041. end;
  3042. { set P bit }
  3043. if (opcode=A_LDM) and (oppostfix in [PF_ED,PF_EA,PF_IB,PF_DB])
  3044. or (opcode=A_STM) and (oppostfix in [PF_FA,PF_FD,PF_IB,PF_DB])
  3045. or (opcode=A_PUSH) then
  3046. bytes:=bytes or (1 shl 24);
  3047. { set U bit }
  3048. if (opcode=A_LDM) and (oppostfix in [PF_None,PF_ED,PF_FD,PF_IB,PF_IA])
  3049. or (opcode=A_STM) and (oppostfix in [PF_None,PF_FA,PF_EA,PF_IB,PF_IA])
  3050. or (opcode=A_POP) then
  3051. bytes:=bytes or (1 shl 23);
  3052. end;
  3053. #$27: // SWP/SWPB
  3054. begin
  3055. { set instruction code }
  3056. bytes:=bytes or (ord(insentry^.code[1]) shl 20);
  3057. bytes:=bytes or (ord(insentry^.code[2]) shl 4);
  3058. { set regs }
  3059. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3060. bytes:=bytes or getsupreg(oper[1]^.reg);
  3061. if ops=3 then
  3062. bytes:=bytes or (getsupreg(oper[2]^.ref^.base) shl 16);
  3063. end;
  3064. #$28: // BX/BLX
  3065. begin
  3066. { set instruction code }
  3067. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3068. { set offset }
  3069. if oper[0]^.typ=top_const then
  3070. bytes:=bytes or ((oper[0]^.val shr 2) and $ffffff)
  3071. else
  3072. begin
  3073. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3074. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  3075. begin
  3076. bytes:=bytes or $fffffe; // TODO: Not sure this is right, but it matches the output of gas
  3077. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24_THUMB);
  3078. end
  3079. else
  3080. begin
  3081. offset:=((currsym.offset-insoffset-8) and $3fffffe);
  3082. { Turn BLX into BL if the destination isn't odd, could happen with recursion }
  3083. if not odd(offset shr 1) then
  3084. bytes:=(bytes and $EB000000) or $EB000000;
  3085. bytes:=bytes or ((offset shr 2) and $ffffff);
  3086. bytes:=bytes or ((offset shr 1) and $1) shl 24;
  3087. end;
  3088. end;
  3089. end;
  3090. #$29: // SUB
  3091. begin
  3092. { set instruction code }
  3093. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3094. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3095. { set regs }
  3096. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3097. { set S if necessary }
  3098. if oppostfix=PF_S then
  3099. bytes:=bytes or (1 shl 20);
  3100. end;
  3101. #$2A:
  3102. begin
  3103. { set instruction code }
  3104. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3105. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3106. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3107. bytes:=bytes or ord(insentry^.code[4]);
  3108. { set opers }
  3109. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3110. if opcode in [A_SSAT, A_SSAT16] then
  3111. bytes:=bytes or (((oper[1]^.val-1) and $1F) shl 16)
  3112. else
  3113. bytes:=bytes or ((oper[1]^.val and $1F) shl 16);
  3114. bytes:=bytes or getsupreg(oper[2]^.reg);
  3115. if (ops>3) and
  3116. (oper[3]^.typ=top_shifterop) and
  3117. (oper[3]^.shifterop^.rs=NR_NO) then
  3118. begin
  3119. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm and $1F) shl 7);
  3120. if oper[3]^.shifterop^.shiftmode=SM_ASR then
  3121. bytes:=bytes or (1 shl 6)
  3122. else if oper[3]^.shifterop^.shiftmode<>SM_LSL then
  3123. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  3124. end;
  3125. end;
  3126. #$2B: // SETEND
  3127. begin
  3128. { set instruction code }
  3129. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3130. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3131. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3132. bytes:=bytes or ord(insentry^.code[4]);
  3133. { set endian specifier }
  3134. bytes:=bytes or ((oper[0]^.val and 1) shl 9);
  3135. end;
  3136. #$2C: // MOVW
  3137. begin
  3138. { set instruction code }
  3139. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3140. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3141. { set destination }
  3142. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3143. { set imm }
  3144. bytes:=bytes or (oper[1]^.val and $FFF);
  3145. bytes:=bytes or ((oper[1]^.val and $F000) shl 4);
  3146. end;
  3147. #$2D: // BFX
  3148. begin
  3149. { set instruction code }
  3150. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3151. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3152. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3153. bytes:=bytes or ord(insentry^.code[4]);
  3154. if ops=3 then
  3155. begin
  3156. msb:=(oper[1]^.val+oper[2]^.val-1);
  3157. { set destination }
  3158. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3159. { set immediates }
  3160. bytes:=bytes or ((oper[1]^.val and $1F) shl 7);
  3161. bytes:=bytes or ((msb and $1F) shl 16);
  3162. end
  3163. else
  3164. begin
  3165. if opcode in [A_BFC,A_BFI] then
  3166. msb:=(oper[2]^.val+oper[3]^.val-1)
  3167. else
  3168. msb:=oper[3]^.val-1;
  3169. { set destination }
  3170. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3171. bytes:=bytes or getsupreg(oper[1]^.reg);
  3172. { set immediates }
  3173. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3174. bytes:=bytes or ((msb and $1F) shl 16);
  3175. end;
  3176. end;
  3177. #$2E: // Cache stuff
  3178. begin
  3179. { set instruction code }
  3180. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3181. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3182. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3183. bytes:=bytes or ord(insentry^.code[4]);
  3184. { set code }
  3185. bytes:=bytes or (oper[0]^.val and $F);
  3186. end;
  3187. #$2F: // Nop
  3188. begin
  3189. { set instruction code }
  3190. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3191. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3192. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3193. bytes:=bytes or ord(insentry^.code[4]);
  3194. end;
  3195. #$30: // Shifts
  3196. begin
  3197. { set instruction code }
  3198. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3199. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3200. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3201. bytes:=bytes or ord(insentry^.code[4]);
  3202. { set destination }
  3203. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3204. bytes:=bytes or getsupreg(oper[1]^.reg);
  3205. if ops>2 then
  3206. begin
  3207. { set shift }
  3208. if oper[2]^.typ=top_reg then
  3209. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 8)
  3210. else
  3211. bytes:=bytes or ((oper[2]^.val and $1F) shl 7);
  3212. end;
  3213. { set S if necessary }
  3214. if oppostfix=PF_S then
  3215. bytes:=bytes or (1 shl 20);
  3216. end;
  3217. #$31: // BKPT
  3218. begin
  3219. { set instruction code }
  3220. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3221. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3222. bytes:=bytes or (ord(insentry^.code[3]) shl 0);
  3223. { set imm }
  3224. bytes:=bytes or (oper[0]^.val and $FFF0) shl 4;
  3225. bytes:=bytes or (oper[0]^.val and $F);
  3226. end;
  3227. #$32: // CLZ/REV
  3228. begin
  3229. { set instruction code }
  3230. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3231. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3232. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3233. bytes:=bytes or ord(insentry^.code[4]);
  3234. { set regs }
  3235. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3236. bytes:=bytes or getsupreg(oper[1]^.reg);
  3237. end;
  3238. #$33:
  3239. begin
  3240. { set instruction code }
  3241. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3242. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3243. { set regs }
  3244. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3245. if oper[1]^.typ=top_ref then
  3246. begin
  3247. { set offset }
  3248. offset:=0;
  3249. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3250. if assigned(currsym) then
  3251. offset:=currsym.offset-insoffset-8;
  3252. offset:=offset+oper[1]^.ref^.offset;
  3253. if offset>=0 then
  3254. begin
  3255. { set U flag }
  3256. bytes:=bytes or (1 shl 23);
  3257. bytes:=bytes or offset
  3258. end
  3259. else
  3260. begin
  3261. bytes:=bytes or (1 shl 22);
  3262. offset:=-offset;
  3263. bytes:=bytes or offset
  3264. end;
  3265. end
  3266. else
  3267. begin
  3268. if is_shifter_const(oper[1]^.val,r) then
  3269. begin
  3270. setshifterop(1);
  3271. bytes:=bytes or (1 shl 23);
  3272. end
  3273. else
  3274. begin
  3275. bytes:=bytes or (1 shl 22);
  3276. oper[1]^.val:=-oper[1]^.val;
  3277. setshifterop(1);
  3278. end;
  3279. end;
  3280. end;
  3281. #$40,#$90: // VMOV
  3282. begin
  3283. { set instruction code }
  3284. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3285. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3286. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3287. bytes:=bytes or ord(insentry^.code[4]);
  3288. { set regs }
  3289. Rd:=0;
  3290. Rn:=0;
  3291. Rm:=0;
  3292. case oppostfix of
  3293. PF_None:
  3294. begin
  3295. if ops=4 then
  3296. begin
  3297. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3298. (getregtype(oper[2]^.reg)=R_INTREGISTER) then
  3299. begin
  3300. Rd:=getmmreg(oper[0]^.reg);
  3301. Rm:=getsupreg(oper[2]^.reg);
  3302. Rn:=getsupreg(oper[3]^.reg);
  3303. end
  3304. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3305. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3306. begin
  3307. Rm:=getsupreg(oper[0]^.reg);
  3308. Rn:=getsupreg(oper[1]^.reg);
  3309. Rd:=getmmreg(oper[2]^.reg);
  3310. end
  3311. else
  3312. message(asmw_e_invalid_opcode_and_operands);
  3313. bytes:=bytes or (((Rd and $1E) shr 1) shl 0);
  3314. bytes:=bytes or ((Rd and $1) shl 5);
  3315. bytes:=bytes or (Rm shl 12);
  3316. bytes:=bytes or (Rn shl 16);
  3317. end
  3318. else if ops=3 then
  3319. begin
  3320. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3321. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3322. begin
  3323. Rd:=getmmreg(oper[0]^.reg);
  3324. Rm:=getsupreg(oper[1]^.reg);
  3325. Rn:=getsupreg(oper[2]^.reg);
  3326. end
  3327. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3328. (getregtype(oper[2]^.reg)=R_MMREGISTER) then
  3329. begin
  3330. Rm:=getsupreg(oper[0]^.reg);
  3331. Rn:=getsupreg(oper[1]^.reg);
  3332. Rd:=getmmreg(oper[2]^.reg);
  3333. end
  3334. else
  3335. message(asmw_e_invalid_opcode_and_operands);
  3336. bytes:=bytes or ((Rd and $F) shl 0);
  3337. bytes:=bytes or ((Rd and $10) shl 1);
  3338. bytes:=bytes or (Rm shl 12);
  3339. bytes:=bytes or (Rn shl 16);
  3340. end
  3341. else if ops=2 then
  3342. begin
  3343. if (getregtype(oper[0]^.reg)=R_MMREGISTER) and
  3344. (getregtype(oper[1]^.reg)=R_INTREGISTER) then
  3345. begin
  3346. Rd:=getmmreg(oper[0]^.reg);
  3347. Rm:=getsupreg(oper[1]^.reg);
  3348. end
  3349. else if (getregtype(oper[0]^.reg)=R_INTREGISTER) and
  3350. (getregtype(oper[1]^.reg)=R_MMREGISTER) then
  3351. begin
  3352. Rm:=getsupreg(oper[0]^.reg);
  3353. Rd:=getmmreg(oper[1]^.reg);
  3354. end
  3355. else
  3356. message(asmw_e_invalid_opcode_and_operands);
  3357. bytes:=bytes or (((Rd and $1E) shr 1) shl 16);
  3358. bytes:=bytes or ((Rd and $1) shl 7);
  3359. bytes:=bytes or (Rm shl 12);
  3360. end;
  3361. end;
  3362. PF_F32:
  3363. begin
  3364. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3365. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3366. Message(asmw_e_invalid_opcode_and_operands);
  3367. Rd:=getmmreg(oper[0]^.reg);
  3368. Rm:=getmmreg(oper[1]^.reg);
  3369. bytes:=bytes or (((Rd and $1E) shr 1) shl 12);
  3370. bytes:=bytes or ((Rd and $1) shl 22);
  3371. bytes:=bytes or (((Rm and $1E) shr 1) shl 0);
  3372. bytes:=bytes or ((Rm and $1) shl 5);
  3373. end;
  3374. PF_F64:
  3375. begin
  3376. if (getregtype(oper[0]^.reg)<>R_MMREGISTER) or
  3377. (getregtype(oper[1]^.reg)<>R_MMREGISTER) then
  3378. Message(asmw_e_invalid_opcode_and_operands);
  3379. Rd:=getmmreg(oper[0]^.reg);
  3380. Rm:=getmmreg(oper[1]^.reg);
  3381. bytes:=bytes or (1 shl 8);
  3382. bytes:=bytes or ((Rd and $F) shl 12);
  3383. bytes:=bytes or (((Rd and $10) shr 4) shl 22);
  3384. bytes:=bytes or (Rm and $F);
  3385. bytes:=bytes or ((Rm and $10) shl 1);
  3386. end;
  3387. end;
  3388. end;
  3389. #$41,#$91: // VMRS/VMSR
  3390. begin
  3391. { set instruction code }
  3392. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3393. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3394. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3395. bytes:=bytes or ord(insentry^.code[4]);
  3396. { set regs }
  3397. if opcode=A_VMRS then
  3398. begin
  3399. case oper[1]^.reg of
  3400. NR_FPSID: Rn:=$0;
  3401. NR_FPSCR: Rn:=$1;
  3402. NR_MVFR1: Rn:=$6;
  3403. NR_MVFR0: Rn:=$7;
  3404. NR_FPEXC: Rn:=$8;
  3405. else
  3406. Rn:=0;
  3407. message(asmw_e_invalid_opcode_and_operands);
  3408. end;
  3409. bytes:=bytes or (Rn shl 16);
  3410. if oper[0]^.reg=NR_APSR_nzcv then
  3411. bytes:=bytes or ($F shl 12)
  3412. else
  3413. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  3414. end
  3415. else
  3416. begin
  3417. case oper[0]^.reg of
  3418. NR_FPSID: Rn:=$0;
  3419. NR_FPSCR: Rn:=$1;
  3420. NR_FPEXC: Rn:=$8;
  3421. else
  3422. Rn:=0;
  3423. message(asmw_e_invalid_opcode_and_operands);
  3424. end;
  3425. bytes:=bytes or (Rn shl 16);
  3426. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  3427. end;
  3428. end;
  3429. #$42,#$92: // VMUL
  3430. begin
  3431. { set instruction code }
  3432. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3433. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3434. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3435. bytes:=bytes or ord(insentry^.code[4]);
  3436. { set regs }
  3437. if ops=3 then
  3438. begin
  3439. Rd:=getmmreg(oper[0]^.reg);
  3440. Rn:=getmmreg(oper[1]^.reg);
  3441. Rm:=getmmreg(oper[2]^.reg);
  3442. end
  3443. else if oper[1]^.typ=top_const then
  3444. begin
  3445. Rd:=getmmreg(oper[0]^.reg);
  3446. Rn:=0;
  3447. Rm:=0;
  3448. end
  3449. else
  3450. begin
  3451. Rd:=getmmreg(oper[0]^.reg);
  3452. Rn:=0;
  3453. Rm:=getmmreg(oper[1]^.reg);
  3454. end;
  3455. if oppostfix=PF_F32 then
  3456. begin
  3457. D:=rd and $1; Rd:=Rd shr 1;
  3458. N:=rn and $1; Rn:=Rn shr 1;
  3459. M:=rm and $1; Rm:=Rm shr 1;
  3460. end
  3461. else
  3462. begin
  3463. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3464. N:=(rn shr 4) and $1; Rn:=Rn and $F;
  3465. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3466. bytes:=bytes or (1 shl 8);
  3467. end;
  3468. bytes:=bytes or (Rd shl 12);
  3469. bytes:=bytes or (Rn shl 16);
  3470. bytes:=bytes or (Rm shl 0);
  3471. bytes:=bytes or (D shl 22);
  3472. bytes:=bytes or (N shl 7);
  3473. bytes:=bytes or (M shl 5);
  3474. end;
  3475. #$43,#$93: // VCVT
  3476. begin
  3477. { set instruction code }
  3478. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3479. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3480. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3481. bytes:=bytes or ord(insentry^.code[4]);
  3482. { set regs }
  3483. Rd:=getmmreg(oper[0]^.reg);
  3484. Rm:=getmmreg(oper[1]^.reg);
  3485. if (ops=2) and
  3486. (oppostfix in [PF_F32F64,PF_F64F32]) then
  3487. begin
  3488. if oppostfix=PF_F32F64 then
  3489. begin
  3490. bytes:=bytes or (1 shl 8);
  3491. D:=rd and $1; Rd:=Rd shr 1;
  3492. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3493. end
  3494. else
  3495. begin
  3496. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3497. M:=rm and $1; Rm:=Rm shr 1;
  3498. end;
  3499. bytes:=bytes and $FFF0FFFF;
  3500. bytes:=bytes or ($7 shl 16);
  3501. bytes:=bytes or (Rd shl 12);
  3502. bytes:=bytes or (Rm shl 0);
  3503. bytes:=bytes or (D shl 22);
  3504. bytes:=bytes or (M shl 5);
  3505. end
  3506. else if ops=2 then
  3507. begin
  3508. case oppostfix of
  3509. PF_S32F64,
  3510. PF_U32F64,
  3511. PF_F64S32,
  3512. PF_F64U32:
  3513. bytes:=bytes or (1 shl 8);
  3514. end;
  3515. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64] then
  3516. begin
  3517. case oppostfix of
  3518. PF_S32F64,
  3519. PF_S32F32:
  3520. bytes:=bytes or (1 shl 16);
  3521. end;
  3522. bytes:=bytes or (1 shl 18);
  3523. D:=rd and $1; Rd:=Rd shr 1;
  3524. if oppostfix in [PF_S32F64,PF_U32F64] then
  3525. begin
  3526. M:=(rm shr 4) and $1; Rm:=Rm and $F;
  3527. end
  3528. else
  3529. begin
  3530. M:=rm and $1; Rm:=Rm shr 1;
  3531. end;
  3532. end
  3533. else
  3534. begin
  3535. case oppostfix of
  3536. PF_F64S32,
  3537. PF_F32S32:
  3538. bytes:=bytes or (1 shl 7);
  3539. else
  3540. bytes:=bytes and $FFFFFF7F;
  3541. end;
  3542. M:=rm and $1; Rm:=Rm shr 1;
  3543. if oppostfix in [PF_F64S32,PF_F64U32] then
  3544. begin
  3545. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3546. end
  3547. else
  3548. begin
  3549. D:=rd and $1; Rd:=Rd shr 1;
  3550. end
  3551. end;
  3552. bytes:=bytes or (Rd shl 12);
  3553. bytes:=bytes or (Rm shl 0);
  3554. bytes:=bytes or (D shl 22);
  3555. bytes:=bytes or (M shl 5);
  3556. end
  3557. else
  3558. begin
  3559. if rd<>rm then
  3560. message(asmw_e_invalid_opcode_and_operands);
  3561. case oppostfix of
  3562. PF_S32F32,PF_U32F32,
  3563. PF_F32S32,PF_F32U32,
  3564. PF_S32F64,PF_U32F64,
  3565. PF_F64S32,PF_F64U32:
  3566. begin
  3567. if not (oper[2]^.val in [1..32]) then
  3568. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 1-32');
  3569. bytes:=bytes or (1 shl 7);
  3570. rn:=32;
  3571. end;
  3572. PF_S16F64,PF_U16F64,
  3573. PF_F64S16,PF_F64U16,
  3574. PF_S16F32,PF_U16F32,
  3575. PF_F32S16,PF_F32U16:
  3576. begin
  3577. if not (oper[2]^.val in [0..16]) then
  3578. message1(asmw_e_invalid_opcode_and_operands, 'fbits not within 0-16');
  3579. rn:=16;
  3580. end;
  3581. else
  3582. Rn:=0;
  3583. message(asmw_e_invalid_opcode_and_operands);
  3584. end;
  3585. case oppostfix of
  3586. PF_S16F64,PF_U16F64,
  3587. PF_S32F64,PF_U32F64,
  3588. PF_F64S16,PF_F64U16,
  3589. PF_F64S32,PF_F64U32:
  3590. begin
  3591. bytes:=bytes or (1 shl 8);
  3592. D:=(rd shr 4) and $1; Rd:=Rd and $F;
  3593. end;
  3594. else
  3595. begin
  3596. D:=rd and $1; Rd:=Rd shr 1;
  3597. end;
  3598. end;
  3599. case oppostfix of
  3600. PF_U16F64,PF_U16F32,
  3601. PF_U32F32,PF_U32F64,
  3602. PF_F64U16,PF_F32U16,
  3603. PF_F32U32,PF_F64U32:
  3604. bytes:=bytes or (1 shl 16);
  3605. end;
  3606. if oppostfix in [PF_S32F32,PF_S32F64,PF_U32F32,PF_U32F64,PF_S16F32,PF_S16F64,PF_U16F32,PF_U16F64] then
  3607. bytes:=bytes or (1 shl 18);
  3608. bytes:=bytes or (Rd shl 12);
  3609. bytes:=bytes or (D shl 22);
  3610. rn:=rn-oper[2]^.val;
  3611. bytes:=bytes or ((rn and $1) shl 5);
  3612. bytes:=bytes or ((rn and $1E) shr 1);
  3613. end;
  3614. end;
  3615. #$44,#$94: // VLDM/VSTM/VPUSH/VPOP
  3616. begin
  3617. { set instruction code }
  3618. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3619. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3620. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3621. { set regs }
  3622. if ops=2 then
  3623. begin
  3624. if oper[0]^.typ=top_ref then
  3625. begin
  3626. Rn:=getsupreg(oper[0]^.ref^.index);
  3627. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  3628. begin
  3629. { set W }
  3630. bytes:=bytes or (1 shl 21);
  3631. end
  3632. else if oppostfix = PF_DB then
  3633. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3634. end
  3635. else
  3636. begin
  3637. Rn:=getsupreg(oper[0]^.reg);
  3638. if oppostfix = PF_DB then
  3639. message1(asmw_e_invalid_opcode_and_operands, 'Invalid postfix without writeback');
  3640. end;
  3641. bytes:=bytes or (Rn shl 16);
  3642. { Set PU bits }
  3643. case oppostfix of
  3644. PF_None,
  3645. PF_IA:
  3646. bytes:=bytes or (1 shl 23);
  3647. PF_DB:
  3648. bytes:=bytes or (2 shl 23);
  3649. end;
  3650. dp_operation:=(oper[1]^.subreg=R_SUBFD);
  3651. if oper[1]^.regset^=[] then
  3652. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3653. rd:=0;
  3654. for r:=0 to 31 do
  3655. if r in oper[1]^.regset^ then
  3656. begin
  3657. rd:=r;
  3658. break;
  3659. end;
  3660. rn:=32-rd;
  3661. for r:=rd+1 to 31 do
  3662. if not(r in oper[1]^.regset^) then
  3663. begin
  3664. rn:=r-rd;
  3665. break;
  3666. end;
  3667. if dp_operation then
  3668. begin
  3669. bytes:=bytes or (1 shl 8);
  3670. bytes:=bytes or (rn*2);
  3671. bytes:=bytes or ((rd and $F) shl 12);
  3672. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3673. end
  3674. else
  3675. begin
  3676. bytes:=bytes or rn;
  3677. bytes:=bytes or ((rd and $1) shl 22);
  3678. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3679. end;
  3680. end
  3681. else { VPUSH/VPOP }
  3682. begin
  3683. dp_operation:=(oper[0]^.subreg=R_SUBFD);
  3684. if oper[0]^.regset^=[] then
  3685. message1(asmw_e_invalid_opcode_and_operands, 'Regset cannot be empty');
  3686. rd:=0;
  3687. for r:=0 to 31 do
  3688. if r in oper[0]^.regset^ then
  3689. begin
  3690. rd:=r;
  3691. break;
  3692. end;
  3693. rn:=32-rd;
  3694. for r:=rd+1 to 31 do
  3695. if not(r in oper[0]^.regset^) then
  3696. begin
  3697. rn:=r-rd;
  3698. break;
  3699. end;
  3700. if dp_operation then
  3701. begin
  3702. bytes:=bytes or (1 shl 8);
  3703. bytes:=bytes or (rn*2);
  3704. bytes:=bytes or ((rd and $F) shl 12);
  3705. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3706. end
  3707. else
  3708. begin
  3709. bytes:=bytes or rn;
  3710. bytes:=bytes or ((rd and $1) shl 22);
  3711. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3712. end;
  3713. end;
  3714. end;
  3715. #$45,#$95: // VLDR/VSTR
  3716. begin
  3717. { set instruction code }
  3718. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3719. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3720. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3721. { set regs }
  3722. rd:=getmmreg(oper[0]^.reg);
  3723. if getsubreg(oper[0]^.reg)=R_SUBFD then
  3724. begin
  3725. bytes:=bytes or (1 shl 8);
  3726. bytes:=bytes or ((rd and $F) shl 12);
  3727. bytes:=bytes or (((rd and $10) shr 4) shl 22);
  3728. end
  3729. else
  3730. begin
  3731. bytes:=bytes or (((rd and $1E) shr 1) shl 12);
  3732. bytes:=bytes or ((rd and $1) shl 22);
  3733. end;
  3734. { set ref }
  3735. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  3736. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  3737. begin
  3738. { set offset }
  3739. offset:=0;
  3740. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3741. if assigned(currsym) then
  3742. offset:=currsym.offset-insoffset-8;
  3743. offset:=offset+oper[1]^.ref^.offset;
  3744. offset:=offset div 4;
  3745. if offset>=0 then
  3746. begin
  3747. { set U flag }
  3748. bytes:=bytes or (1 shl 23);
  3749. bytes:=bytes or offset
  3750. end
  3751. else
  3752. begin
  3753. offset:=-offset;
  3754. bytes:=bytes or offset
  3755. end;
  3756. end
  3757. else
  3758. message(asmw_e_invalid_opcode_and_operands);
  3759. end;
  3760. #$46: { System instructions }
  3761. begin
  3762. { set instruction code }
  3763. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  3764. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  3765. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  3766. { set regs }
  3767. if (oper[0]^.typ=top_modeflags) then
  3768. begin
  3769. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 8);
  3770. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  3771. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  3772. end;
  3773. if (ops=2) then
  3774. bytes:=bytes or (oper[1]^.val and $1F)
  3775. else if (ops=1) and
  3776. (oper[0]^.typ=top_const) then
  3777. bytes:=bytes or (oper[0]^.val and $1F);
  3778. end;
  3779. #$60: { Thumb }
  3780. begin
  3781. bytelen:=2;
  3782. bytes:=0;
  3783. { set opcode }
  3784. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3785. bytes:=bytes or ord(insentry^.code[2]);
  3786. { set regs }
  3787. if ops=2 then
  3788. begin
  3789. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3790. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3791. if (oper[1]^.typ=top_reg) then
  3792. bytes:=bytes or ((getsupreg(oper[1]^.reg) and $7) shl 6)
  3793. else
  3794. bytes:=bytes or ((oper[1]^.val and $1F) shl 6);
  3795. end
  3796. else if ops=3 then
  3797. begin
  3798. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3799. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3800. if (oper[2]^.typ=top_reg) then
  3801. bytes:=bytes or ((getsupreg(oper[2]^.reg) and $7) shl 6)
  3802. else
  3803. bytes:=bytes or ((oper[2]^.val and $1F) shl 6);
  3804. end
  3805. else if ops=1 then
  3806. begin
  3807. if oper[0]^.typ=top_const then
  3808. bytes:=bytes or (oper[0]^.val and $FF);
  3809. end;
  3810. end;
  3811. #$61: { Thumb }
  3812. begin
  3813. bytelen:=2;
  3814. bytes:=0;
  3815. { set opcode }
  3816. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3817. bytes:=bytes or ord(insentry^.code[2]);
  3818. { set regs }
  3819. if ops=2 then
  3820. begin
  3821. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3822. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3823. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3824. end
  3825. else if ops=1 then
  3826. begin
  3827. if oper[0]^.typ=top_const then
  3828. bytes:=bytes or (oper[0]^.val and $FF);
  3829. end;
  3830. end;
  3831. #$62..#$63: { Thumb branches }
  3832. begin
  3833. bytelen:=2;
  3834. bytes:=0;
  3835. { set opcode }
  3836. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3837. bytes:=bytes or ord(insentry^.code[2]);
  3838. if insentry^.code[0]=#$63 then
  3839. bytes:=bytes or (CondVal[condition] shl 8);
  3840. if oper[0]^.typ=top_const then
  3841. begin
  3842. if insentry^.code[0]=#$63 then
  3843. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $FF)
  3844. else
  3845. bytes:=bytes or (((oper[0]^.val shr 1)-1) and $3FF);
  3846. end
  3847. else if oper[0]^.typ=top_reg then
  3848. begin
  3849. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 3);
  3850. end
  3851. else if oper[0]^.typ=top_ref then
  3852. begin
  3853. offset:=0;
  3854. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  3855. if assigned(currsym) then
  3856. offset:=currsym.offset-insoffset-8;
  3857. offset:=offset+oper[0]^.ref^.offset;
  3858. if insentry^.code[0]=#$63 then
  3859. bytes:=bytes or (((offset+4) shr 1) and $FF)
  3860. else
  3861. bytes:=bytes or (((offset+4) shr 1) and $7FF);
  3862. end
  3863. end;
  3864. #$64: { Thumb: Special encodings }
  3865. begin
  3866. bytelen:=2;
  3867. bytes:=0;
  3868. { set opcode }
  3869. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3870. bytes:=bytes or ord(insentry^.code[2]);
  3871. case opcode of
  3872. A_SUB:
  3873. begin
  3874. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3875. if (ops=3) and
  3876. (oper[2]^.typ=top_const) then
  3877. bytes:=bytes or ((oper[2]^.val shr 2) and $7F)
  3878. else if (ops=2) and
  3879. (oper[1]^.typ=top_const) then
  3880. bytes:=bytes or ((oper[1]^.val shr 2) and $7F);
  3881. end;
  3882. A_MUL:
  3883. if (ops in [2,3]) then
  3884. begin
  3885. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3886. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  3887. end;
  3888. A_ADD:
  3889. begin
  3890. if ops=2 then
  3891. begin
  3892. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3893. bytes:=bytes or (getsupreg(oper[1]^.reg) shl $3);
  3894. end
  3895. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3896. (oper[2]^.typ=top_const) then
  3897. begin
  3898. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7) shl 8;
  3899. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3900. end
  3901. else if (oper[0]^.reg<>NR_STACK_POINTER_REG) and
  3902. (oper[2]^.typ=top_reg) then
  3903. begin
  3904. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3905. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $8) shr 3) shl 7;
  3906. end
  3907. else
  3908. begin
  3909. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3910. bytes:=bytes or ((oper[2]^.val shr 2) and $7F);
  3911. end;
  3912. end;
  3913. end;
  3914. end;
  3915. #$65: { Thumb load/store }
  3916. begin
  3917. bytelen:=2;
  3918. bytes:=0;
  3919. { set opcode }
  3920. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3921. bytes:=bytes or ord(insentry^.code[2]);
  3922. { set regs }
  3923. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3924. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3925. bytes:=bytes or (getsupreg(oper[1]^.ref^.index) shl 6);
  3926. end;
  3927. #$66: { Thumb load/store }
  3928. begin
  3929. bytelen:=2;
  3930. bytes:=0;
  3931. { set opcode }
  3932. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3933. bytes:=bytes or ord(insentry^.code[2]);
  3934. { set regs }
  3935. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3936. bytes:=bytes or (getsupreg(oper[1]^.ref^.base) shl 3);
  3937. bytes:=bytes or (((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $1F) shl 6);
  3938. end;
  3939. #$67: { Thumb load/store }
  3940. begin
  3941. bytelen:=2;
  3942. bytes:=0;
  3943. { set opcode }
  3944. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3945. bytes:=bytes or ord(insentry^.code[2]);
  3946. { set regs }
  3947. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  3948. if oper[1]^.typ=top_ref then
  3949. bytes:=bytes or ((oper[1]^.ref^.offset shr ord(insentry^.code[3])) and $FF)
  3950. else
  3951. bytes:=bytes or ((oper[1]^.val shr ord(insentry^.code[3])) and $FF);
  3952. end;
  3953. #$68: { Thumb CB[N]Z }
  3954. begin
  3955. bytelen:=2;
  3956. bytes:=0;
  3957. { set opcode }
  3958. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3959. { set opers }
  3960. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  3961. if oper[1]^.typ=top_ref then
  3962. begin
  3963. offset:=0;
  3964. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  3965. if assigned(currsym) then
  3966. offset:=currsym.offset-insoffset-8;
  3967. offset:=offset+oper[1]^.ref^.offset;
  3968. offset:=offset div 2;
  3969. end
  3970. else
  3971. offset:=oper[1]^.val div 2;
  3972. bytes:=bytes or ((offset) and $1F) shl 3;
  3973. bytes:=bytes or ((offset shr 5) and 1) shl 9;
  3974. end;
  3975. #$69: { Thumb: Push/Pop/Stm/Ldm }
  3976. begin
  3977. bytelen:=2;
  3978. bytes:=0;
  3979. { set opcode }
  3980. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  3981. case opcode of
  3982. A_PUSH:
  3983. begin
  3984. for r:=0 to 7 do
  3985. if r in oper[0]^.regset^ then
  3986. bytes:=bytes or (1 shl r);
  3987. if RS_R14 in oper[0]^.regset^ then
  3988. bytes:=bytes or (1 shl 8);
  3989. end;
  3990. A_POP:
  3991. begin
  3992. for r:=0 to 7 do
  3993. if r in oper[0]^.regset^ then
  3994. bytes:=bytes or (1 shl r);
  3995. if RS_R15 in oper[0]^.regset^ then
  3996. bytes:=bytes or (1 shl 8);
  3997. end;
  3998. A_STM:
  3999. begin
  4000. for r:=0 to 7 do
  4001. if r in oper[1]^.regset^ then
  4002. bytes:=bytes or (1 shl r);
  4003. if oper[0]^.typ=top_ref then
  4004. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  4005. else
  4006. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4007. end;
  4008. A_LDM:
  4009. begin
  4010. for r:=0 to 7 do
  4011. if r in oper[1]^.regset^ then
  4012. bytes:=bytes or (1 shl r);
  4013. if oper[0]^.typ=top_ref then
  4014. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 8)
  4015. else
  4016. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4017. end;
  4018. end;
  4019. end;
  4020. #$6A: { Thumb: IT }
  4021. begin
  4022. bytelen:=2;
  4023. bytes:=0;
  4024. { set opcode }
  4025. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4026. bytes:=bytes or (ord(insentry^.code[2]) shl 0);
  4027. bytes:=bytes or (CondVal[oper[0]^.cc] shl 4);
  4028. i_field:=(bytes shr 4) and 1;
  4029. i_field:=(i_field shl 1) or i_field;
  4030. i_field:=(i_field shl 2) or i_field;
  4031. bytes:=bytes or ((i_field and ord(insentry^.code[3])) xor (ord(insentry^.code[3]) shr 4));
  4032. end;
  4033. #$6B: { Thumb: Data processing (misc) }
  4034. begin
  4035. bytelen:=2;
  4036. bytes:=0;
  4037. { set opcode }
  4038. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4039. bytes:=bytes or ord(insentry^.code[2]);
  4040. { set regs }
  4041. if ops>=2 then
  4042. begin
  4043. if oper[1]^.typ=top_const then
  4044. begin
  4045. bytes:=bytes or ((getsupreg(oper[0]^.reg) and $7) shl 8);
  4046. bytes:=bytes or (oper[1]^.val and $FF);
  4047. end
  4048. else if oper[1]^.typ=top_reg then
  4049. begin
  4050. bytes:=bytes or (getsupreg(oper[0]^.reg) and $7);
  4051. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 3);
  4052. end;
  4053. end
  4054. else if ops=1 then
  4055. begin
  4056. if oper[0]^.typ=top_const then
  4057. bytes:=bytes or (oper[0]^.val and $FF);
  4058. end;
  4059. end;
  4060. #$6C: { Thumb: CPS }
  4061. begin
  4062. bytelen:=2;
  4063. bytes:=0;
  4064. { set opcode }
  4065. bytes:=bytes or (ord(insentry^.code[1]) shl 8);
  4066. bytes:=bytes or ord(insentry^.code[2]);
  4067. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 2);
  4068. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 1);
  4069. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 0);
  4070. end;
  4071. #$80: { Thumb-2: Dataprocessing }
  4072. begin
  4073. bytes:=0;
  4074. { set instruction code }
  4075. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4076. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4077. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4078. bytes:=bytes or ord(insentry^.code[4]);
  4079. if ops=1 then
  4080. begin
  4081. if oper[0]^.typ=top_reg then
  4082. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4083. else if oper[0]^.typ=top_const then
  4084. bytes:=bytes or (oper[0]^.val and $F);
  4085. end
  4086. else if (ops=2) and
  4087. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4088. begin
  4089. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4090. if oper[1]^.typ=top_const then
  4091. encodethumbimm(oper[1]^.val)
  4092. else if oper[1]^.typ=top_reg then
  4093. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4094. end
  4095. else if (ops=3) and
  4096. (opcode in [A_CMP,A_CMN,A_TEQ,A_TST]) then
  4097. begin
  4098. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4099. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4100. if oper[2]^.typ=top_shifterop then
  4101. setthumbshift(2)
  4102. else if oper[2]^.typ=top_reg then
  4103. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 12);
  4104. end
  4105. else if (ops=2) and
  4106. (opcode in [A_REV,A_RBIT,A_REV16,A_REVSH,A_CLZ]) then
  4107. begin
  4108. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4109. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4110. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4111. end
  4112. else if ops=2 then
  4113. begin
  4114. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4115. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4116. if oper[1]^.typ=top_const then
  4117. encodethumbimm(oper[1]^.val)
  4118. else if oper[1]^.typ=top_reg then
  4119. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4120. end
  4121. else if ops=3 then
  4122. begin
  4123. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4124. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4125. if oper[2]^.typ=top_const then
  4126. encodethumbimm(oper[2]^.val)
  4127. else if oper[2]^.typ=top_reg then
  4128. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4129. end
  4130. else if ops=4 then
  4131. begin
  4132. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4133. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4134. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4135. if oper[3]^.typ=top_shifterop then
  4136. setthumbshift(3)
  4137. else if oper[3]^.typ=top_reg then
  4138. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 12);
  4139. end;
  4140. if oppostfix=PF_S then
  4141. bytes:=bytes or (1 shl 20)
  4142. else if oppostfix=PF_X then
  4143. bytes:=bytes or (1 shl 4)
  4144. else if oppostfix=PF_R then
  4145. bytes:=bytes or (1 shl 4);
  4146. end;
  4147. #$81: { Thumb-2: Dataprocessing misc }
  4148. begin
  4149. bytes:=0;
  4150. { set instruction code }
  4151. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4152. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4153. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4154. bytes:=bytes or ord(insentry^.code[4]);
  4155. if ops=3 then
  4156. begin
  4157. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4158. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4159. if oper[2]^.typ=top_const then
  4160. begin
  4161. bytes:=bytes or (oper[2]^.val and $FF);
  4162. bytes:=bytes or ((oper[2]^.val and $700) shr 8) shl 12;
  4163. bytes:=bytes or ((oper[2]^.val and $800) shr 11) shl 26;
  4164. end;
  4165. end
  4166. else if ops=2 then
  4167. begin
  4168. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4169. offset:=0;
  4170. if oper[1]^.typ=top_const then
  4171. begin
  4172. offset:=oper[1]^.val;
  4173. end
  4174. else if oper[1]^.typ=top_ref then
  4175. begin
  4176. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4177. if assigned(currsym) then
  4178. offset:=currsym.offset-insoffset-8;
  4179. offset:=offset+oper[1]^.ref^.offset;
  4180. offset:=offset;
  4181. end;
  4182. bytes:=bytes or (offset and $FF);
  4183. bytes:=bytes or ((offset and $700) shr 8) shl 12;
  4184. bytes:=bytes or ((offset and $800) shr 11) shl 26;
  4185. bytes:=bytes or ((offset and $F000) shr 12) shl 16;
  4186. end;
  4187. if oppostfix=PF_S then
  4188. bytes:=bytes or (1 shl 20);
  4189. end;
  4190. #$82: { Thumb-2: Shifts }
  4191. begin
  4192. bytes:=0;
  4193. { set instruction code }
  4194. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4195. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4196. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4197. bytes:=bytes or ord(insentry^.code[4]);
  4198. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4199. if oper[1]^.typ=top_reg then
  4200. begin
  4201. offset:=2;
  4202. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4203. end
  4204. else
  4205. begin
  4206. offset:=1;
  4207. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 0);
  4208. end;
  4209. if oper[offset]^.typ=top_const then
  4210. begin
  4211. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4212. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4213. end
  4214. else if oper[offset]^.typ=top_reg then
  4215. bytes:=bytes or (getsupreg(oper[offset]^.reg) shl 16);
  4216. if (ops>=(offset+2)) and
  4217. (oper[offset+1]^.typ=top_const) then
  4218. bytes:=bytes or (oper[offset+1]^.val and $1F);
  4219. if oppostfix=PF_S then
  4220. bytes:=bytes or (1 shl 20);
  4221. end;
  4222. #$84: { Thumb-2: Shifts(width-1) }
  4223. begin
  4224. bytes:=0;
  4225. { set instruction code }
  4226. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4227. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4228. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4229. bytes:=bytes or ord(insentry^.code[4]);
  4230. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4231. if oper[1]^.typ=top_reg then
  4232. begin
  4233. offset:=2;
  4234. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4235. end
  4236. else
  4237. offset:=1;
  4238. if oper[offset]^.typ=top_const then
  4239. begin
  4240. bytes:=bytes or (oper[offset]^.val and $3) shl 6;
  4241. bytes:=bytes or (oper[offset]^.val and $1C) shl 10;
  4242. end;
  4243. if (ops>=(offset+2)) and
  4244. (oper[offset+1]^.typ=top_const) then
  4245. begin
  4246. if opcode in [A_BFI,A_BFC] then
  4247. i_field:=oper[offset+1]^.val+oper[offset]^.val-1
  4248. else
  4249. i_field:=oper[offset+1]^.val-1;
  4250. bytes:=bytes or (i_field and $1F);
  4251. end;
  4252. if oppostfix=PF_S then
  4253. bytes:=bytes or (1 shl 20);
  4254. end;
  4255. #$83: { Thumb-2: Saturation }
  4256. begin
  4257. bytes:=0;
  4258. { set instruction code }
  4259. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4260. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4261. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4262. bytes:=bytes or ord(insentry^.code[4]);
  4263. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4264. bytes:=bytes or (oper[1]^.val and $1F);
  4265. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4266. if ops=4 then
  4267. setthumbshift(3,true);
  4268. end;
  4269. #$85: { Thumb-2: Long multiplications }
  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. if ops=4 then
  4278. begin
  4279. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4280. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 8);
  4281. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 16);
  4282. bytes:=bytes or (getsupreg(oper[3]^.reg) shl 0);
  4283. end;
  4284. if oppostfix=PF_S then
  4285. bytes:=bytes or (1 shl 20)
  4286. else if oppostfix=PF_X then
  4287. bytes:=bytes or (1 shl 4);
  4288. end;
  4289. #$86: { Thumb-2: Extension ops }
  4290. begin
  4291. bytes:=0;
  4292. { set instruction code }
  4293. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4294. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4295. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4296. bytes:=bytes or ord(insentry^.code[4]);
  4297. if ops=2 then
  4298. begin
  4299. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4300. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4301. end
  4302. else if ops=3 then
  4303. begin
  4304. if oper[2]^.typ=top_shifterop then
  4305. begin
  4306. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4307. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4308. bytes:=bytes or ((oper[2]^.shifterop^.shiftimm shr 3) shl 4);
  4309. end
  4310. else
  4311. begin
  4312. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4313. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4314. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4315. end;
  4316. end
  4317. else if ops=4 then
  4318. begin
  4319. if oper[3]^.typ=top_shifterop then
  4320. begin
  4321. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 8);
  4322. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 16);
  4323. bytes:=bytes or (getsupreg(oper[2]^.reg) shl 0);
  4324. bytes:=bytes or ((oper[3]^.shifterop^.shiftimm shr 3) shl 4);
  4325. end;
  4326. end;
  4327. end;
  4328. #$87: { Thumb-2: PLD/PLI }
  4329. begin
  4330. { set instruction code }
  4331. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4332. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4333. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4334. bytes:=bytes or ord(insentry^.code[4]);
  4335. { set Rn and Rd }
  4336. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4337. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4338. begin
  4339. { set offset }
  4340. offset:=0;
  4341. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4342. if assigned(currsym) then
  4343. offset:=currsym.offset-insoffset-8;
  4344. offset:=offset+oper[0]^.ref^.offset;
  4345. if offset>=0 then
  4346. begin
  4347. { set U flag }
  4348. bytes:=bytes or (1 shl 23);
  4349. bytes:=bytes or (offset and $FFF);
  4350. end
  4351. else
  4352. begin
  4353. bytes:=bytes or ($3 shl 10);
  4354. offset:=-offset;
  4355. bytes:=bytes or (offset and $FF);
  4356. end;
  4357. end
  4358. else
  4359. begin
  4360. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4361. { set shift }
  4362. with oper[0]^.ref^ do
  4363. if shiftmode=SM_LSL then
  4364. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4365. end;
  4366. end;
  4367. #$88: { Thumb-2: LDR/STR }
  4368. begin
  4369. { set instruction code }
  4370. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4371. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4372. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4373. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4374. { set Rn and Rd }
  4375. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4376. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4377. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4378. begin
  4379. { set offset }
  4380. offset:=0;
  4381. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4382. if assigned(currsym) then
  4383. offset:=currsym.offset-insoffset-8;
  4384. offset:=(offset+oper[1]^.ref^.offset) shr ord(insentry^.code[5]);
  4385. if offset>=0 then
  4386. begin
  4387. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4388. bytes:=bytes or (1 shl 23);
  4389. { set U flag }
  4390. if (oper[1]^.ref^.addressmode<>AM_OFFSET) then
  4391. bytes:=bytes or (1 shl 9);
  4392. bytes:=bytes or offset
  4393. end
  4394. else
  4395. begin
  4396. bytes:=bytes or (1 shl 11);
  4397. offset:=-offset;
  4398. bytes:=bytes or offset
  4399. end;
  4400. end
  4401. else
  4402. begin
  4403. { set I flag }
  4404. bytes:=bytes or (1 shl 25);
  4405. bytes:=bytes or getsupreg(oper[1]^.ref^.index);
  4406. { set shift }
  4407. with oper[1]^.ref^ do
  4408. if shiftmode<>SM_None then
  4409. bytes:=bytes or ((shiftimm and $1F) shl 4);
  4410. end;
  4411. if not (opcode in [A_LDRT,A_LDRSBT,A_LDRSHT,A_LDRBT,A_LDRHT]) then
  4412. begin
  4413. { set W bit }
  4414. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4415. bytes:=bytes or (1 shl 8);
  4416. { set P bit if necessary }
  4417. if oper[1]^.ref^.addressmode<>AM_POSTINDEXED then
  4418. bytes:=bytes or (1 shl 10);
  4419. end;
  4420. end;
  4421. #$89: { Thumb-2: LDRD/STRD }
  4422. begin
  4423. { set instruction code }
  4424. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4425. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4426. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4427. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4428. { set Rn and Rd }
  4429. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4430. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4431. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4432. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4433. begin
  4434. { set offset }
  4435. offset:=0;
  4436. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4437. if assigned(currsym) then
  4438. offset:=currsym.offset-insoffset-8;
  4439. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4440. if offset>=0 then
  4441. begin
  4442. { set U flag }
  4443. bytes:=bytes or (1 shl 23);
  4444. bytes:=bytes or offset
  4445. end
  4446. else
  4447. begin
  4448. offset:=-offset;
  4449. bytes:=bytes or offset
  4450. end;
  4451. end
  4452. else
  4453. begin
  4454. message(asmw_e_invalid_opcode_and_operands);
  4455. end;
  4456. { set W bit }
  4457. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4458. bytes:=bytes or (1 shl 21);
  4459. { set P bit if necessary }
  4460. if oper[2]^.ref^.addressmode<>AM_POSTINDEXED then
  4461. bytes:=bytes or (1 shl 24);
  4462. end;
  4463. #$8A: { Thumb-2: LDREX }
  4464. begin
  4465. { set instruction code }
  4466. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4467. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4468. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4469. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4470. { set Rn and Rd }
  4471. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4472. if (ops=2) and (opcode in [A_LDREX]) then
  4473. begin
  4474. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4475. if getregtype(oper[1]^.ref^.index)=R_INVALIDREGISTER then
  4476. begin
  4477. { set offset }
  4478. offset:=0;
  4479. currsym:=objdata.symbolref(oper[1]^.ref^.symbol);
  4480. if assigned(currsym) then
  4481. offset:=currsym.offset-insoffset-8;
  4482. offset:=(offset+oper[1]^.ref^.offset) div 4;
  4483. if offset>=0 then
  4484. begin
  4485. bytes:=bytes or offset
  4486. end
  4487. else
  4488. begin
  4489. message(asmw_e_invalid_opcode_and_operands);
  4490. end;
  4491. end
  4492. else
  4493. begin
  4494. message(asmw_e_invalid_opcode_and_operands);
  4495. end;
  4496. end
  4497. else if (ops=2) then
  4498. begin
  4499. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4500. end
  4501. else
  4502. begin
  4503. bytes:=bytes or getsupreg(oper[1]^.reg) shl 8;
  4504. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4505. end;
  4506. end;
  4507. #$8B: { Thumb-2: STREX }
  4508. begin
  4509. { set instruction code }
  4510. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4511. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4512. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4513. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4514. { set Rn and Rd }
  4515. if (ops=3) and (opcode in [A_STREX]) then
  4516. begin
  4517. bytes:=bytes or getsupreg(oper[0]^.reg) shl 8;
  4518. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4519. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4520. if getregtype(oper[2]^.ref^.index)=R_INVALIDREGISTER then
  4521. begin
  4522. { set offset }
  4523. offset:=0;
  4524. currsym:=objdata.symbolref(oper[2]^.ref^.symbol);
  4525. if assigned(currsym) then
  4526. offset:=currsym.offset-insoffset-8;
  4527. offset:=(offset+oper[2]^.ref^.offset) div 4;
  4528. if offset>=0 then
  4529. begin
  4530. bytes:=bytes or offset
  4531. end
  4532. else
  4533. begin
  4534. message(asmw_e_invalid_opcode_and_operands);
  4535. end;
  4536. end
  4537. else
  4538. begin
  4539. message(asmw_e_invalid_opcode_and_operands);
  4540. end;
  4541. end
  4542. else if (ops=3) then
  4543. begin
  4544. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4545. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4546. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4547. end
  4548. else
  4549. begin
  4550. bytes:=bytes or getsupreg(oper[0]^.reg) shl 0;
  4551. bytes:=bytes or getsupreg(oper[1]^.reg) shl 12;
  4552. bytes:=bytes or getsupreg(oper[2]^.reg) shl 8;
  4553. bytes:=bytes or getsupreg(oper[3]^.ref^.base) shl 16;
  4554. end;
  4555. end;
  4556. #$8C: { Thumb-2: LDM/STM }
  4557. begin
  4558. { set instruction code }
  4559. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4560. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4561. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4562. bytes:=bytes or (ord(insentry^.code[4]) shl 0);
  4563. if oper[0]^.typ=top_reg then
  4564. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16)
  4565. else
  4566. begin
  4567. bytes:=bytes or (getsupreg(oper[0]^.ref^.base) shl 16);
  4568. if oper[0]^.ref^.addressmode<>AM_OFFSET then
  4569. bytes:=bytes or (1 shl 21);
  4570. end;
  4571. for r:=0 to 15 do
  4572. if r in oper[1]^.regset^ then
  4573. bytes:=bytes or (1 shl r);
  4574. case oppostfix of
  4575. PF_None,PF_IA,PF_FD: bytes:=bytes or ($1 shl 23);
  4576. PF_DB,PF_EA: bytes:=bytes or ($2 shl 23);
  4577. end;
  4578. end;
  4579. #$8D: { Thumb-2: BL/BLX }
  4580. begin
  4581. { set instruction code }
  4582. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4583. bytes:=bytes or (ord(insentry^.code[2]) shl 8);
  4584. { set offset }
  4585. if oper[0]^.typ=top_const then
  4586. offset:=(oper[0]^.val shr 1) and $FFFFFF
  4587. else
  4588. begin
  4589. currsym:=objdata.symbolref(oper[0]^.ref^.symbol);
  4590. if (currsym.bind<>AB_LOCAL) and (currsym.objsection<>objdata.CurrObjSec) then
  4591. begin
  4592. objdata.writereloc(oper[0]^.ref^.offset,0,currsym,RELOC_RELATIVE_24);
  4593. offset:=$FFFFFE
  4594. end
  4595. else
  4596. offset:=((currsym.offset-insoffset-8) shr 1) and $FFFFFF;
  4597. end;
  4598. bytes:=bytes or ((offset shr 00) and $7FF) shl 0;
  4599. bytes:=bytes or ((offset shr 11) and $3FF) shl 16;
  4600. bytes:=bytes or (((offset shr 21) xor (offset shr 23) xor 1) and $1) shl 11;
  4601. bytes:=bytes or (((offset shr 22) xor (offset shr 23) xor 1) and $1) shl 13;
  4602. bytes:=bytes or ((offset shr 23) and $1) shl 26;
  4603. end;
  4604. #$8E: { Thumb-2: TBB/TBH }
  4605. begin
  4606. { set instruction code }
  4607. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4608. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4609. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4610. bytes:=bytes or ord(insentry^.code[4]);
  4611. { set Rn and Rm }
  4612. bytes:=bytes or getsupreg(oper[0]^.ref^.base) shl 16;
  4613. if getregtype(oper[0]^.ref^.index)=R_INVALIDREGISTER then
  4614. message(asmw_e_invalid_effective_address)
  4615. else
  4616. begin
  4617. bytes:=bytes or getsupreg(oper[0]^.ref^.index);
  4618. if (opcode=A_TBH) and
  4619. (oper[0]^.ref^.shiftmode<>SM_LSL) and
  4620. (oper[0]^.ref^.shiftimm<>1) then
  4621. message(asmw_e_invalid_effective_address);
  4622. end;
  4623. end;
  4624. #$8F: { Thumb-2: CPSxx }
  4625. begin
  4626. { set opcode }
  4627. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4628. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4629. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4630. bytes:=bytes or ord(insentry^.code[4]);
  4631. if (oper[0]^.typ=top_modeflags) then
  4632. begin
  4633. if mfA in oper[0]^.modeflags then bytes:=bytes or (1 shl 7);
  4634. if mfI in oper[0]^.modeflags then bytes:=bytes or (1 shl 6);
  4635. if mfF in oper[0]^.modeflags then bytes:=bytes or (1 shl 5);
  4636. end;
  4637. if (ops=2) then
  4638. bytes:=bytes or (oper[1]^.val and $1F)
  4639. else if (ops=1) and
  4640. (oper[0]^.typ=top_const) then
  4641. bytes:=bytes or (oper[0]^.val and $1F);
  4642. end;
  4643. #$A0: { FPA: CPDT(LDF/STF) }
  4644. begin
  4645. { set instruction code }
  4646. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4647. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4648. bytes:=bytes or (ord(insentry^.code[3]) shl 8);
  4649. bytes:=bytes or ord(insentry^.code[4]);
  4650. if ops=2 then
  4651. begin
  4652. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4653. bytes:=bytes or getsupreg(oper[1]^.ref^.base) shl 16;
  4654. bytes:=bytes or ((oper[1]^.ref^.offset shr 2) and $FF);
  4655. if oper[1]^.ref^.offset>=0 then
  4656. bytes:=bytes or (1 shl 23);
  4657. if oper[1]^.ref^.addressmode<>AM_OFFSET then
  4658. bytes:=bytes or (1 shl 21);
  4659. if oper[1]^.ref^.addressmode=AM_PREINDEXED then
  4660. bytes:=bytes or (1 shl 24);
  4661. case oppostfix of
  4662. PF_D: bytes:=bytes or (0 shl 22) or (1 shl 15);
  4663. PF_E: bytes:=bytes or (1 shl 22) or (0 shl 15);
  4664. PF_P: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4665. end;
  4666. end
  4667. else
  4668. begin
  4669. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4670. case oper[1]^.val of
  4671. 1: bytes:=bytes or (1 shl 15);
  4672. 2: bytes:=bytes or (1 shl 22);
  4673. 3: bytes:=bytes or (1 shl 22) or (1 shl 15);
  4674. 4: ;
  4675. else
  4676. message1(asmw_e_invalid_opcode_and_operands, 'Invalid count for LFM/SFM');
  4677. end;
  4678. bytes:=bytes or getsupreg(oper[2]^.ref^.base) shl 16;
  4679. bytes:=bytes or ((oper[2]^.ref^.offset shr 2) and $FF);
  4680. if oper[2]^.ref^.offset>=0 then
  4681. bytes:=bytes or (2 shl 23);
  4682. if oper[2]^.ref^.addressmode<>AM_OFFSET then
  4683. bytes:=bytes or (1 shl 21);
  4684. if oper[2]^.ref^.addressmode=AM_PREINDEXED then
  4685. bytes:=bytes or (1 shl 24);
  4686. end;
  4687. end;
  4688. #$A1: { FPA: CPDO }
  4689. begin
  4690. { set instruction code }
  4691. bytes:=bytes or ($E shl 24);
  4692. bytes:=bytes or (ord(insentry^.code[1]) shl 15);
  4693. bytes:=bytes or ((ord(insentry^.code[2]) shr 1) shl 20);
  4694. bytes:=bytes or (1 shl 8);
  4695. bytes:=bytes or getsupreg(oper[0]^.reg) shl 12;
  4696. if ops=2 then
  4697. begin
  4698. if oper[1]^.typ=top_reg then
  4699. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4700. else
  4701. case oper[1]^.val of
  4702. 0: bytes:=bytes or $8;
  4703. 1: bytes:=bytes or $9;
  4704. 2: bytes:=bytes or $A;
  4705. 3: bytes:=bytes or $B;
  4706. 4: bytes:=bytes or $C;
  4707. 5: bytes:=bytes or $D;
  4708. //0.5: bytes:=bytes or $E;
  4709. 10: bytes:=bytes or $F;
  4710. else
  4711. Message(asmw_e_invalid_opcode_and_operands);
  4712. end;
  4713. end
  4714. else
  4715. begin
  4716. bytes:=bytes or getsupreg(oper[1]^.reg) shl 16;
  4717. if oper[2]^.typ=top_reg then
  4718. bytes:=bytes or getsupreg(oper[2]^.reg) shl 0
  4719. else
  4720. case oper[2]^.val of
  4721. 0: bytes:=bytes or $8;
  4722. 1: bytes:=bytes or $9;
  4723. 2: bytes:=bytes or $A;
  4724. 3: bytes:=bytes or $B;
  4725. 4: bytes:=bytes or $C;
  4726. 5: bytes:=bytes or $D;
  4727. //0.5: bytes:=bytes or $E;
  4728. 10: bytes:=bytes or $F;
  4729. else
  4730. Message(asmw_e_invalid_opcode_and_operands);
  4731. end;
  4732. end;
  4733. case roundingmode of
  4734. RM_P: bytes:=bytes or (1 shl 5);
  4735. RM_M: bytes:=bytes or (2 shl 5);
  4736. RM_Z: bytes:=bytes or (3 shl 5);
  4737. end;
  4738. case oppostfix of
  4739. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4740. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4741. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4742. else
  4743. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4744. end;
  4745. end;
  4746. #$A2: { FPA: CPDO }
  4747. begin
  4748. { set instruction code }
  4749. bytes:=bytes or (ord(insentry^.code[1]) shl 24);
  4750. bytes:=bytes or (ord(insentry^.code[2]) shl 16);
  4751. bytes:=bytes or ($11 shl 4);
  4752. case opcode of
  4753. A_FLT:
  4754. begin
  4755. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4756. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 12);
  4757. case roundingmode of
  4758. RM_P: bytes:=bytes or (1 shl 5);
  4759. RM_M: bytes:=bytes or (2 shl 5);
  4760. RM_Z: bytes:=bytes or (3 shl 5);
  4761. end;
  4762. case oppostfix of
  4763. PF_S: bytes:=bytes or (0 shl 19) or (0 shl 7);
  4764. PF_D: bytes:=bytes or (0 shl 19) or (1 shl 7);
  4765. PF_E: bytes:=bytes or (1 shl 19) or (0 shl 7);
  4766. else
  4767. message1(asmw_e_invalid_opcode_and_operands, 'Precision cannot be undefined');
  4768. end;
  4769. end;
  4770. A_FIX:
  4771. begin
  4772. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4773. bytes:=bytes or (getsupreg(oper[1]^.reg) shl 0);
  4774. case roundingmode of
  4775. RM_P: bytes:=bytes or (1 shl 5);
  4776. RM_M: bytes:=bytes or (2 shl 5);
  4777. RM_Z: bytes:=bytes or (3 shl 5);
  4778. end;
  4779. end;
  4780. A_WFS,A_RFS,A_WFC,A_RFC:
  4781. begin
  4782. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 12);
  4783. end;
  4784. A_CMF,A_CNF,A_CMFE,A_CNFE:
  4785. begin
  4786. bytes:=bytes or (getsupreg(oper[0]^.reg) shl 16);
  4787. if oper[1]^.typ=top_reg then
  4788. bytes:=bytes or getsupreg(oper[1]^.reg) shl 0
  4789. else
  4790. case oper[1]^.val of
  4791. 0: bytes:=bytes or $8;
  4792. 1: bytes:=bytes or $9;
  4793. 2: bytes:=bytes or $A;
  4794. 3: bytes:=bytes or $B;
  4795. 4: bytes:=bytes or $C;
  4796. 5: bytes:=bytes or $D;
  4797. //0.5: bytes:=bytes or $E;
  4798. 10: bytes:=bytes or $F;
  4799. else
  4800. Message(asmw_e_invalid_opcode_and_operands);
  4801. end;
  4802. end;
  4803. end;
  4804. end;
  4805. #$fe: // No written data
  4806. begin
  4807. exit;
  4808. end;
  4809. #$ff:
  4810. internalerror(2005091101);
  4811. else
  4812. begin
  4813. writeln(ord(insentry^.code[0]), ' - ', opcode);
  4814. internalerror(2005091102);
  4815. end;
  4816. end;
  4817. { Todo: Decide whether the code above should take care of writing data in an order that makes senes }
  4818. if (insentry^.code[0] in [#$80..#$95]) and (bytelen=4) then
  4819. bytes:=((bytes shr 16) and $FFFF) or ((bytes and $FFFF) shl 16);
  4820. { we're finished, write code }
  4821. objdata.writebytes(bytes,bytelen);
  4822. end;
  4823. begin
  4824. cai_align:=tai_align;
  4825. end.