aasmcpu.pas 207 KB

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