aasmcpu.pas 206 KB

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