cgcpu.pas 221 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the code generator for the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef,
  23. cgbase,cgutils,cgobj,
  24. aasmbase,aasmcpu,aasmtai,aasmdata,
  25. parabase,
  26. cpubase,cpuinfo,cg64f32,rgcpu;
  27. type
  28. { tbasecgarm is shared between all arm architectures }
  29. tbasecgarm = class(tcg)
  30. { true, if the next arithmetic operation should modify the flags }
  31. cgsetflags : boolean;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);override;
  33. protected
  34. procedure a_load_ref_cgparalocref(list: TAsmList; sourcesize: tcgsize; sizeleft: tcgint; const ref, paralocref: treference; const cgpara: tcgpara; const location: PCGParaLocation); override;
  35. public
  36. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  37. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  38. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  39. { move instructions }
  40. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  41. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  42. function a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  43. function a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  44. { fpu move instructions }
  45. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  46. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  47. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  48. procedure g_check_for_fpu_exception(list : TAsmList; force,clear : boolean); override;
  49. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  50. { comparison operations }
  51. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  52. l : tasmlabel);override;
  53. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  54. procedure a_jmp_name(list : TAsmList;const s : string); override;
  55. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  56. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  57. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  58. procedure g_profilecode(list : TAsmList); override;
  59. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  60. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  61. procedure g_maybe_got_init(list : TAsmList); override;
  62. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  63. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  64. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);override;
  65. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  66. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  67. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  68. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  69. procedure g_save_registers(list : TAsmList);override;
  70. procedure g_restore_registers(list : TAsmList);override;
  71. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  72. procedure fixref(list : TAsmList;var ref : treference);
  73. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; virtual;
  74. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  75. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  76. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  77. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  78. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  79. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); override;
  80. { Transform unsupported methods into Internal errors }
  81. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister); override;
  82. { try to generate optimized 32 Bit multiplication, returns true if successful generated }
  83. function try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  84. { clear out potential overflow bits from 8 or 16 bit operations
  85. the upper 24/16 bits of a register after an operation }
  86. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  87. { mla for thumb requires that none of the registers is equal to r13/r15, this method ensures this }
  88. procedure safe_mla(list: TAsmList;op1,op2,op3,op4 : TRegister);
  89. procedure g_maybe_tls_init(list : TAsmList); override;
  90. end;
  91. { tcgarm is shared between normal arm and thumb-2 }
  92. tcgarm = class(tbasecgarm)
  93. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  94. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  95. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  96. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  97. size: tcgsize; a: tcgint; src, dst: tregister); override;
  98. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  99. size: tcgsize; src1, src2, dst: tregister); override;
  100. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  101. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  102. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  103. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  104. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  105. {Multiply two 32-bit registers into lo and hi 32-bit registers}
  106. procedure a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister); override;
  107. end;
  108. { normal arm cg }
  109. tarmcgarm = class(tcgarm)
  110. procedure init_register_allocators;override;
  111. procedure done_register_allocators;override;
  112. end;
  113. { 64 bit cg for all arm flavours }
  114. tbasecg64farm = class(tcg64f32)
  115. end;
  116. { tcg64farm is shared between normal arm and thumb-2 }
  117. tcg64farm = class(tbasecg64farm)
  118. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  119. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  120. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  121. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  122. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  123. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  124. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);override;
  125. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);override;
  126. end;
  127. tarmcg64farm = class(tcg64farm)
  128. end;
  129. tthumbcgarm = class(tbasecgarm)
  130. procedure init_register_allocators;override;
  131. procedure done_register_allocators;override;
  132. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);override;
  133. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  134. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,dst: TRegister);override;
  135. procedure a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);override;
  136. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); override;
  137. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  138. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const Ref: treference; reg: tregister);override;
  139. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; reg: tregister);override;
  140. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  141. function handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference; override;
  142. end;
  143. tthumbcg64farm = class(tbasecg64farm)
  144. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  145. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  146. end;
  147. tthumb2cgarm = class(tcgarm)
  148. procedure init_register_allocators;override;
  149. procedure done_register_allocators;override;
  150. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  151. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  152. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  153. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  154. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  155. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  156. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  157. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  158. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  159. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; override;
  160. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  161. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  162. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  163. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  164. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  165. end;
  166. tthumb2cg64farm = class(tcg64farm)
  167. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  168. end;
  169. const
  170. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  171. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  172. winstackpagesize = 4096;
  173. function get_fpu_postfix(def : tdef) : toppostfix;
  174. procedure create_codegen;
  175. implementation
  176. uses
  177. globals,verbose,systems,cutils,
  178. aopt,aoptcpu,
  179. fmodule,
  180. symconst,symsym,symtable,
  181. tgobj,
  182. procinfo,cpupi,
  183. paramgr;
  184. { Range check must be disabled explicitly as conversions between signed and unsigned
  185. 32-bit values are done without explicit typecasts }
  186. {$R-}
  187. function get_fpu_postfix(def : tdef) : toppostfix;
  188. begin
  189. if def.typ=floatdef then
  190. begin
  191. case tfloatdef(def).floattype of
  192. s32real:
  193. result:=PF_S;
  194. s64real:
  195. result:=PF_D;
  196. s80real:
  197. result:=PF_E;
  198. else
  199. internalerror(200401272);
  200. end;
  201. end
  202. else
  203. internalerror(200401271);
  204. end;
  205. procedure tarmcgarm.init_register_allocators;
  206. begin
  207. inherited init_register_allocators;
  208. { currently, we always save R14, so we can use it }
  209. if (target_info.system<>system_arm_ios) then
  210. begin
  211. if assigned(current_procinfo) and (current_procinfo.framepointer<>NR_R11) then
  212. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  213. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  214. RS_R9,RS_R10,RS_R11,RS_R14],first_int_imreg,[])
  215. else
  216. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  217. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  218. RS_R9,RS_R10,RS_R14],first_int_imreg,[])
  219. end
  220. else
  221. { r7 is not available on Darwin, it's used as frame pointer (always,
  222. for backtrace support -- also in gcc/clang -> R11 can be used).
  223. r9 is volatile }
  224. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  225. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R9,RS_R12,RS_R4,RS_R5,RS_R6,RS_R8,
  226. RS_R10,RS_R11,RS_R14],first_int_imreg,[]);
  227. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  228. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  229. { The register allocator currently cannot deal with multiple
  230. non-overlapping subregs per register, so we can only use
  231. half the single precision registers for now (as sub registers of the
  232. double precision ones). }
  233. if FPUARM_HAS_32REGS in fpu_capabilities[current_settings.fputype] then
  234. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  235. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  236. RS_D16,RS_D17,RS_D18,RS_D19,RS_D20,RS_D21,RS_D22,RS_D23,RS_D24,RS_D25,RS_D26,RS_D27,RS_D28,RS_D29,RS_D30,RS_D31,
  237. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  238. ],first_mm_imreg,[])
  239. else
  240. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  241. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15],first_mm_imreg,[]);
  242. end;
  243. procedure tarmcgarm.done_register_allocators;
  244. begin
  245. rg[R_INTREGISTER].free;
  246. rg[R_FPUREGISTER].free;
  247. rg[R_MMREGISTER].free;
  248. inherited done_register_allocators;
  249. end;
  250. procedure tcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  251. var
  252. imm_shift : byte;
  253. l : tasmlabel;
  254. hr : treference;
  255. imm1, imm2: DWord;
  256. begin
  257. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  258. internalerror(2002090907);
  259. if is_shifter_const(a,imm_shift) then
  260. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  261. else if is_shifter_const(not(a),imm_shift) then
  262. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  263. { loading of constants with mov and orr }
  264. else if (split_into_shifter_const(a,imm1, imm2)) then
  265. begin
  266. list.concat(taicpu.op_reg_const(A_MOV,reg, imm1));
  267. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg, imm2));
  268. end
  269. { loading of constants with mvn and bic }
  270. else if (split_into_shifter_const(not(a), imm1, imm2)) then
  271. begin
  272. list.concat(taicpu.op_reg_const(A_MVN,reg, imm1));
  273. list.concat(taicpu.op_reg_reg_const(A_BIC,reg,reg, imm2));
  274. end
  275. else
  276. begin
  277. reference_reset(hr,4,[]);
  278. current_asmdata.getjumplabel(l);
  279. cg.a_label(current_procinfo.aktlocaldata,l);
  280. hr.symboldata:=current_procinfo.aktlocaldata.last;
  281. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  282. hr.symbol:=l;
  283. hr.base:=NR_PC;
  284. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  285. end;
  286. end;
  287. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  288. var
  289. oppostfix:toppostfix;
  290. usedtmpref: treference;
  291. tmpreg,tmpreg2 : tregister;
  292. so : tshifterop;
  293. dir : integer;
  294. begin
  295. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  296. FromSize := ToSize;
  297. case FromSize of
  298. { signed integer registers }
  299. OS_8:
  300. oppostfix:=PF_B;
  301. OS_S8:
  302. oppostfix:=PF_SB;
  303. OS_16:
  304. oppostfix:=PF_H;
  305. OS_S16:
  306. oppostfix:=PF_SH;
  307. OS_32,
  308. OS_S32:
  309. oppostfix:=PF_None;
  310. else
  311. InternalError(200308297);
  312. end;
  313. if (fromsize=OS_S8) and
  314. (not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) then
  315. oppostfix:=PF_B;
  316. if ((ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize])) or
  317. ((not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) and
  318. (oppostfix in [PF_SH,PF_H])) then
  319. begin
  320. if target_info.endian=endian_big then
  321. dir:=-1
  322. else
  323. dir:=1;
  324. case FromSize of
  325. OS_16,OS_S16:
  326. begin
  327. { only complicated references need an extra loadaddr }
  328. if assigned(ref.symbol) or
  329. (ref.index<>NR_NO) or
  330. (ref.offset<-4095) or
  331. (ref.offset>4094) or
  332. { sometimes the compiler reused registers }
  333. (reg=ref.index) or
  334. (reg=ref.base) then
  335. begin
  336. tmpreg2:=getintregister(list,OS_INT);
  337. a_loadaddr_ref_reg(list,ref,tmpreg2);
  338. reference_reset_base(usedtmpref,tmpreg2,0,ref.temppos,ref.alignment,ref.volatility);
  339. end
  340. else
  341. usedtmpref:=ref;
  342. if target_info.endian=endian_big then
  343. inc(usedtmpref.offset,1);
  344. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  345. tmpreg:=getintregister(list,OS_INT);
  346. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  347. inc(usedtmpref.offset,dir);
  348. if FromSize=OS_16 then
  349. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  350. else
  351. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  352. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  353. end;
  354. OS_32,OS_S32:
  355. begin
  356. tmpreg:=getintregister(list,OS_INT);
  357. { only complicated references need an extra loadaddr }
  358. if assigned(ref.symbol) or
  359. (ref.index<>NR_NO) or
  360. (ref.offset<-4095) or
  361. (ref.offset>4092) or
  362. { sometimes the compiler reused registers }
  363. (reg=ref.index) or
  364. (reg=ref.base) then
  365. begin
  366. tmpreg2:=getintregister(list,OS_INT);
  367. a_loadaddr_ref_reg(list,ref,tmpreg2);
  368. reference_reset_base(usedtmpref,tmpreg2,0,ref.temppos,ref.alignment,ref.volatility);
  369. end
  370. else
  371. usedtmpref:=ref;
  372. shifterop_reset(so);so.shiftmode:=SM_LSL;
  373. if ref.alignment=2 then
  374. begin
  375. if target_info.endian=endian_big then
  376. inc(usedtmpref.offset,2);
  377. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  378. inc(usedtmpref.offset,dir*2);
  379. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  380. so.shiftimm:=16;
  381. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  382. end
  383. else
  384. begin
  385. tmpreg2:=getintregister(list,OS_INT);
  386. if target_info.endian=endian_big then
  387. inc(usedtmpref.offset,3);
  388. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  389. inc(usedtmpref.offset,dir);
  390. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  391. inc(usedtmpref.offset,dir);
  392. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  393. so.shiftimm:=8;
  394. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  395. inc(usedtmpref.offset,dir);
  396. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  397. so.shiftimm:=16;
  398. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg2,so));
  399. so.shiftimm:=24;
  400. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  401. end;
  402. end
  403. else
  404. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  405. end;
  406. end
  407. else
  408. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  409. if (fromsize=OS_S8) and
  410. (not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) then
  411. a_load_reg_reg(list,OS_S8,OS_32,reg,reg)
  412. else if (fromsize=OS_S8) and (tosize = OS_16) then
  413. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  414. end;
  415. procedure tcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  416. var
  417. hsym : tsym;
  418. href : treference;
  419. paraloc : Pcgparalocation;
  420. shift : byte;
  421. begin
  422. { calculate the parameter info for the procdef }
  423. procdef.init_paraloc_info(callerside);
  424. hsym:=tsym(procdef.parast.Find('self'));
  425. if not(assigned(hsym) and
  426. (hsym.typ=paravarsym)) then
  427. internalerror(2003052503);
  428. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  429. while paraloc<>nil do
  430. with paraloc^ do
  431. begin
  432. case loc of
  433. LOC_REGISTER:
  434. begin
  435. if is_shifter_const(ioffset,shift) then
  436. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  437. else
  438. begin
  439. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  440. a_op_reg_reg(list,OP_SUB,size,NR_R12,register);
  441. end;
  442. end;
  443. LOC_REFERENCE:
  444. begin
  445. { offset in the wrapper needs to be adjusted for the stored
  446. return address }
  447. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),ctempposinvalid,sizeof(pint),[]);
  448. if is_shifter_const(ioffset,shift) then
  449. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  450. else
  451. begin
  452. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  453. a_op_reg_ref(list,OP_SUB,size,NR_R12,href);
  454. end;
  455. end
  456. else
  457. internalerror(2003091803);
  458. end;
  459. paraloc:=next;
  460. end;
  461. end;
  462. procedure tbasecgarm.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  463. var
  464. ref: treference;
  465. begin
  466. paraloc.check_simple_location;
  467. paramanager.allocparaloc(list,paraloc.location);
  468. case paraloc.location^.loc of
  469. LOC_REGISTER,LOC_CREGISTER:
  470. a_load_const_reg(list,size,a,paraloc.location^.register);
  471. LOC_REFERENCE:
  472. begin
  473. reference_reset(ref,paraloc.alignment,[]);
  474. ref.base:=paraloc.location^.reference.index;
  475. ref.offset:=paraloc.location^.reference.offset;
  476. a_load_const_ref(list,size,a,ref);
  477. end;
  478. else
  479. internalerror(2002081101);
  480. end;
  481. end;
  482. procedure tbasecgarm.a_load_ref_cgparalocref(list: TAsmList; sourcesize: tcgsize; sizeleft: tcgint; const ref, paralocref: treference; const cgpara: tcgpara; const location: PCGParaLocation);
  483. begin
  484. { doubles in softemu mode have a strange order of registers and references }
  485. if (cgpara.size=OS_F64) and
  486. (location^.size=OS_32) then
  487. begin
  488. g_concatcopy(list,ref,paralocref,4)
  489. end
  490. else
  491. inherited;
  492. end;
  493. procedure tbasecgarm.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  494. var
  495. ref: treference;
  496. tmpreg: tregister;
  497. begin
  498. paraloc.check_simple_location;
  499. paramanager.allocparaloc(list,paraloc.location);
  500. case paraloc.location^.loc of
  501. LOC_REGISTER,LOC_CREGISTER:
  502. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  503. LOC_REFERENCE:
  504. begin
  505. reference_reset(ref,paraloc.alignment,[]);
  506. ref.base := paraloc.location^.reference.index;
  507. ref.offset := paraloc.location^.reference.offset;
  508. tmpreg := getintregister(list,OS_ADDR);
  509. a_loadaddr_ref_reg(list,r,tmpreg);
  510. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  511. end;
  512. else
  513. internalerror(2002080701);
  514. end;
  515. end;
  516. procedure tbasecgarm.a_call_name(list : TAsmList;const s : string; weak: boolean);
  517. var
  518. branchopcode: tasmop;
  519. r : treference;
  520. sym : TAsmSymbol;
  521. begin
  522. { use always BL as newer binutils do not translate blx apparently
  523. generating BL is also what clang and gcc do by default }
  524. branchopcode:=A_BL;
  525. if not(weak) then
  526. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  527. else
  528. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  529. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  530. if (tf_pic_uses_got in target_info.flags) and
  531. (cs_create_pic in current_settings.moduleswitches) then
  532. begin
  533. r.refaddr:=addr_pic
  534. end
  535. else
  536. r.refaddr:=addr_full;
  537. list.concat(taicpu.op_ref(branchopcode,r));
  538. {
  539. the compiler does not properly set this flag anymore in pass 1, and
  540. for now we only need it after pass 2 (I hope) (JM)
  541. if not(pi_do_call in current_procinfo.flags) then
  542. internalerror(2003060703);
  543. }
  544. include(current_procinfo.flags,pi_do_call);
  545. end;
  546. procedure tbasecgarm.a_call_reg(list : TAsmList;reg: tregister);
  547. begin
  548. { check not really correct: should only be used for non-Thumb cpus }
  549. if not(CPUARM_HAS_BLX in cpu_capabilities[current_settings.cputype]) then
  550. begin
  551. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  552. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  553. end
  554. else
  555. list.concat(taicpu.op_reg(A_BLX, reg));
  556. {
  557. the compiler does not properly set this flag anymore in pass 1, and
  558. for now we only need it after pass 2 (I hope) (JM)
  559. if not(pi_do_call in current_procinfo.flags) then
  560. internalerror(2003060703);
  561. }
  562. include(current_procinfo.flags,pi_do_call);
  563. end;
  564. procedure tcgarm.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  565. begin
  566. a_op_const_reg_reg(list,op,size,a,reg,reg);
  567. end;
  568. procedure tcgarm.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  569. var
  570. tmpreg,tmpresreg : tregister;
  571. tmpref : treference;
  572. begin
  573. tmpreg:=getintregister(list,size);
  574. tmpresreg:=getintregister(list,size);
  575. tmpref:=a_internal_load_ref_reg(list,size,size,ref,tmpreg);
  576. a_op_const_reg_reg(list,op,size,a,tmpreg,tmpresreg);
  577. a_load_reg_ref(list,size,size,tmpresreg,tmpref);
  578. end;
  579. procedure tcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  580. var
  581. so : tshifterop;
  582. begin
  583. if op = OP_NEG then
  584. begin
  585. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0));
  586. maybeadjustresult(list,OP_NEG,size,dst);
  587. end
  588. else if op = OP_NOT then
  589. begin
  590. if size in [OS_8, OS_16, OS_S8, OS_S16] then
  591. begin
  592. shifterop_reset(so);
  593. so.shiftmode:=SM_LSL;
  594. if size in [OS_8, OS_S8] then
  595. so.shiftimm:=24
  596. else
  597. so.shiftimm:=16;
  598. list.concat(taicpu.op_reg_reg_shifterop(A_MVN,dst,src,so));
  599. {Using a shift here allows this to be folded into another instruction}
  600. if size in [OS_S8, OS_S16] then
  601. so.shiftmode:=SM_ASR
  602. else
  603. so.shiftmode:=SM_LSR;
  604. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  605. end
  606. else
  607. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  608. end
  609. else
  610. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  611. end;
  612. const
  613. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  614. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  615. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR,A_NONE,A_NONE);
  616. op_reg_opcg2asmop: array[TOpCG] of tasmop =
  617. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  618. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  619. op_reg_postfix: array[TOpCG] of TOpPostfix =
  620. (PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  621. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None);
  622. procedure tcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  623. size: tcgsize; a: tcgint; src, dst: tregister);
  624. var
  625. ovloc : tlocation;
  626. begin
  627. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,ovloc);
  628. end;
  629. procedure tcgarm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  630. size: tcgsize; src1, src2, dst: tregister);
  631. var
  632. ovloc : tlocation;
  633. begin
  634. a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  635. end;
  636. function opshift2shiftmode(op: TOpCg): tshiftmode;
  637. begin
  638. case op of
  639. OP_SHL: Result:=SM_LSL;
  640. OP_SHR: Result:=SM_LSR;
  641. OP_ROR: Result:=SM_ROR;
  642. OP_ROL: Result:=SM_ROR;
  643. OP_SAR: Result:=SM_ASR;
  644. else internalerror(2012070501);
  645. end
  646. end;
  647. function tbasecgarm.try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  648. var
  649. multiplier : dword;
  650. power : longint;
  651. shifterop : tshifterop;
  652. bitsset : byte;
  653. negative : boolean;
  654. first : boolean;
  655. b,
  656. cycles : byte;
  657. maxeffort : byte;
  658. begin
  659. result:=true;
  660. cycles:=0;
  661. negative:=a<0;
  662. shifterop.rs:=NR_NO;
  663. shifterop.shiftmode:=SM_LSL;
  664. if negative then
  665. inc(cycles);
  666. multiplier:=dword(abs(a));
  667. bitsset:=popcnt(multiplier and $fffffffe);
  668. { heuristics to estimate how much instructions are reasonable to replace the mul,
  669. this is currently based on XScale timings }
  670. { in the simplest case, we need a mov to load the constant and a mul to carry out the
  671. actual multiplication, this requires min. 1+4 cycles
  672. because the first shift imm. might cause a stall and because we need more instructions
  673. when replacing the mul we generate max. 3 instructions to replace this mul }
  674. maxeffort:=3;
  675. { if the constant is not a shifter op, we need either some mov/mvn/bic/or sequence or
  676. a ldr, so generating one more operation to replace this is beneficial }
  677. if not(is_shifter_const(dword(a),b)) and not(is_shifter_const(not(dword(a)),b)) then
  678. inc(maxeffort);
  679. { if the upper 5 bits are all set or clear, mul is one cycle faster }
  680. if ((dword(a) and $f8000000)=0) or ((dword(a) and $f8000000)=$f8000000) then
  681. dec(maxeffort);
  682. { if the upper 17 bits are all set or clear, mul is another cycle faster }
  683. if ((dword(a) and $ffff8000)=0) or ((dword(a) and $ffff8000)=$ffff8000) then
  684. dec(maxeffort);
  685. { most simple cases }
  686. if a=1 then
  687. a_load_reg_reg(list,OS_32,OS_32,src,dst)
  688. else if a=0 then
  689. a_load_const_reg(list,OS_32,0,dst)
  690. else if a=-1 then
  691. a_op_reg_reg(list,OP_NEG,OS_32,src,dst)
  692. { add up ?
  693. basically, one add is needed for each bit being set in the constant factor
  694. however, the least significant bit is for free, it can be hidden in the initial
  695. instruction
  696. }
  697. else if (bitsset+cycles<=maxeffort) and
  698. (bitsset<=popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)) then
  699. begin
  700. first:=true;
  701. while multiplier<>0 do
  702. begin
  703. shifterop.shiftimm:=BsrDWord(multiplier);
  704. if odd(multiplier) then
  705. begin
  706. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,shifterop));
  707. dec(multiplier);
  708. end
  709. else
  710. if first then
  711. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  712. else
  713. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,src,shifterop));
  714. first:=false;
  715. dec(multiplier,1 shl shifterop.shiftimm);
  716. end;
  717. if negative then
  718. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  719. end
  720. { subtract from the next greater power of two? }
  721. else if popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)+cycles+1<=maxeffort then
  722. begin
  723. first:=true;
  724. while multiplier<>0 do
  725. begin
  726. if first then
  727. begin
  728. multiplier:=(1 shl power)-multiplier;
  729. shifterop.shiftimm:=power;
  730. end
  731. else
  732. shifterop.shiftimm:=BsrDWord(multiplier);
  733. if odd(multiplier) then
  734. begin
  735. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,shifterop));
  736. dec(multiplier);
  737. end
  738. else
  739. if first then
  740. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  741. else
  742. begin
  743. list.concat(taicpu.op_reg_reg_reg_shifterop(A_SUB,dst,dst,src,shifterop));
  744. dec(multiplier,1 shl shifterop.shiftimm);
  745. end;
  746. first:=false;
  747. end;
  748. if negative then
  749. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  750. end
  751. else
  752. result:=false;
  753. end;
  754. procedure tcgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  755. var
  756. shift, lsb, width : byte;
  757. tmpreg : tregister;
  758. so : tshifterop;
  759. l1 : longint;
  760. imm1, imm2: DWord;
  761. begin
  762. optimize_op_const(size, op, a);
  763. case op of
  764. OP_NONE:
  765. begin
  766. if src <> dst then
  767. a_load_reg_reg(list, size, size, src, dst);
  768. exit;
  769. end;
  770. OP_MOVE:
  771. begin
  772. a_load_const_reg(list, size, a, dst);
  773. exit;
  774. end;
  775. else
  776. ;
  777. end;
  778. ovloc.loc:=LOC_VOID;
  779. if (a<>-2147483648) and not setflags and is_shifter_const(-a,shift) then
  780. case op of
  781. OP_ADD:
  782. begin
  783. op:=OP_SUB;
  784. a:=aint(dword(-a));
  785. end;
  786. OP_SUB:
  787. begin
  788. op:=OP_ADD;
  789. a:=aint(dword(-a));
  790. end
  791. else
  792. ;
  793. end;
  794. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  795. case op of
  796. OP_NEG,OP_NOT:
  797. internalerror(200308281);
  798. OP_SHL,
  799. OP_SHR,
  800. OP_ROL,
  801. OP_ROR,
  802. OP_SAR:
  803. begin
  804. if a>32 then
  805. internalerror(200308294);
  806. shifterop_reset(so);
  807. so.shiftmode:=opshift2shiftmode(op);
  808. if op = OP_ROL then
  809. so.shiftimm:=32-a
  810. else
  811. so.shiftimm:=a;
  812. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  813. end;
  814. else
  815. {if (op in [OP_SUB, OP_ADD]) and
  816. ((a < 0) or
  817. (a > 4095)) then
  818. begin
  819. tmpreg:=getintregister(list,size);
  820. list.concat(taicpu.op_reg_const(A_MOVT, tmpreg, (a shr 16) and $FFFF));
  821. list.concat(taicpu.op_reg_const(A_MOV, tmpreg, a and $FFFF));
  822. list.concat(setoppostfix(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  823. ));
  824. end
  825. else}
  826. begin
  827. if cgsetflags or setflags then
  828. a_reg_alloc(list,NR_DEFAULTFLAGS);
  829. list.concat(setoppostfix(
  830. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  831. end;
  832. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  833. begin
  834. ovloc.loc:=LOC_FLAGS;
  835. case op of
  836. OP_ADD:
  837. ovloc.resflags:=F_CS;
  838. OP_SUB:
  839. ovloc.resflags:=F_CC;
  840. else
  841. internalerror(2019050922);
  842. end;
  843. end;
  844. end
  845. else
  846. begin
  847. { there could be added some more sophisticated optimizations }
  848. if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  849. a_op_reg_reg(list,OP_NEG,size,src,dst)
  850. { we do this here instead in the peephole optimizer because
  851. it saves us a register }
  852. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  853. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  854. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  855. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  856. begin
  857. if l1>32 then{roozbeh does this ever happen?}
  858. internalerror(200308296);
  859. shifterop_reset(so);
  860. so.shiftmode:=SM_LSL;
  861. so.shiftimm:=l1;
  862. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  863. end
  864. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  865. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  866. begin
  867. if l1>32 then{does this ever happen?}
  868. internalerror(201205181);
  869. shifterop_reset(so);
  870. so.shiftmode:=SM_LSL;
  871. so.shiftimm:=l1;
  872. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  873. end
  874. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,src,dst) then
  875. begin
  876. { nothing to do on success }
  877. end
  878. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  879. broader range of shifterconstants.}
  880. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  881. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  882. { Doing two shifts instead of two bics might allow the peephole optimizer to fold the second shift
  883. into the following instruction}
  884. else if (op = OP_AND) and
  885. is_continuous_mask(aword(a), lsb, width) and
  886. ((lsb = 0) or ((lsb + width) = 32)) then
  887. begin
  888. shifterop_reset(so);
  889. if (width = 16) and
  890. (lsb = 0) and
  891. (current_settings.cputype >= cpu_armv6) then
  892. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  893. else if (width = 8) and
  894. (lsb = 0) and
  895. (current_settings.cputype >= cpu_armv6) then
  896. list.concat(taicpu.op_reg_reg(A_UXTB,dst,src))
  897. else if lsb = 0 then
  898. begin
  899. so.shiftmode:=SM_LSL;
  900. so.shiftimm:=32-width;
  901. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  902. so.shiftmode:=SM_LSR;
  903. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  904. end
  905. else
  906. begin
  907. so.shiftmode:=SM_LSR;
  908. so.shiftimm:=lsb;
  909. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  910. so.shiftmode:=SM_LSL;
  911. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  912. end;
  913. end
  914. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  915. begin
  916. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,imm1));
  917. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  918. end
  919. else if (op in [OP_ADD, OP_SUB, OP_OR, OP_XOR]) and
  920. not(cgsetflags or setflags) and
  921. split_into_shifter_const(a, imm1, imm2) then
  922. begin
  923. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,imm1));
  924. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  925. end
  926. else
  927. begin
  928. tmpreg:=getintregister(list,size);
  929. a_load_const_reg(list,size,a,tmpreg);
  930. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  931. end;
  932. end;
  933. maybeadjustresult(list,op,size,dst);
  934. end;
  935. procedure tcgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  936. var
  937. so : tshifterop;
  938. tmpreg,overflowreg : tregister;
  939. asmop : tasmop;
  940. begin
  941. ovloc.loc:=LOC_VOID;
  942. case op of
  943. OP_NEG,OP_NOT,
  944. OP_DIV,OP_IDIV:
  945. internalerror(200308283);
  946. OP_SHL,
  947. OP_SHR,
  948. OP_SAR,
  949. OP_ROR:
  950. begin
  951. if (op = OP_ROR) and not(size in [OS_32,OS_S32]) then
  952. internalerror(2008072801);
  953. shifterop_reset(so);
  954. so.rs:=src1;
  955. so.shiftmode:=opshift2shiftmode(op);
  956. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  957. end;
  958. OP_ROL:
  959. begin
  960. if not(size in [OS_32,OS_S32]) then
  961. internalerror(2008072804);
  962. { simulate ROL by ror'ing 32-value }
  963. tmpreg:=getintregister(list,OS_32);
  964. list.concat(taicpu.op_reg_reg_const(A_RSB,tmpreg,src1, 32));
  965. shifterop_reset(so);
  966. so.rs:=tmpreg;
  967. so.shiftmode:=SM_ROR;
  968. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  969. end;
  970. OP_IMUL,
  971. OP_MUL:
  972. begin
  973. if (cgsetflags or setflags) and
  974. (CPUARM_HAS_UMULL in cpu_capabilities[current_settings.cputype]) then
  975. begin
  976. overflowreg:=getintregister(list,size);
  977. if op=OP_IMUL then
  978. asmop:=A_SMULL
  979. else
  980. asmop:=A_UMULL;
  981. { the arm doesn't allow that rd and rm are the same }
  982. if dst=src2 then
  983. begin
  984. if dst<>src1 then
  985. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  986. else
  987. begin
  988. tmpreg:=getintregister(list,size);
  989. a_load_reg_reg(list,size,size,src2,dst);
  990. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  991. end;
  992. end
  993. else
  994. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  995. a_reg_alloc(list,NR_DEFAULTFLAGS);
  996. if op=OP_IMUL then
  997. begin
  998. shifterop_reset(so);
  999. so.shiftmode:=SM_ASR;
  1000. so.shiftimm:=31;
  1001. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  1002. end
  1003. else
  1004. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  1005. ovloc.loc:=LOC_FLAGS;
  1006. ovloc.resflags:=F_NE;
  1007. end
  1008. else
  1009. begin
  1010. { the arm doesn't allow that rd and rm are the same }
  1011. if dst=src2 then
  1012. begin
  1013. if dst<>src1 then
  1014. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  1015. else
  1016. begin
  1017. tmpreg:=getintregister(list,size);
  1018. a_load_reg_reg(list,size,size,src2,dst);
  1019. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  1020. end;
  1021. end
  1022. else
  1023. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  1024. end;
  1025. end;
  1026. else
  1027. begin
  1028. if cgsetflags or setflags then
  1029. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1030. list.concat(setoppostfix(
  1031. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  1032. end;
  1033. end;
  1034. maybeadjustresult(list,op,size,dst);
  1035. end;
  1036. procedure tcgarm.a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister);
  1037. var
  1038. asmop: tasmop;
  1039. begin
  1040. if CPUARM_HAS_UMULL in cpu_capabilities[current_settings.cputype] then
  1041. begin
  1042. list.concat(tai_comment.create(strpnew('tcgarm.a_mul_reg_reg_pair called')));
  1043. case size of
  1044. OS_32: asmop:=A_UMULL;
  1045. OS_S32: asmop:=A_SMULL;
  1046. else
  1047. InternalError(2014060802);
  1048. end;
  1049. { The caller might omit dstlo or dsthi, when he is not interested in it, we still
  1050. need valid registers everywhere. In case of dsthi = NR_NO we could fall back to
  1051. 32x32=32 bit multiplication}
  1052. if (dstlo = NR_NO) then
  1053. dstlo:=getintregister(list,size);
  1054. if (dsthi = NR_NO) then
  1055. dsthi:=getintregister(list,size);
  1056. list.concat(taicpu.op_reg_reg_reg_reg(asmop, dstlo, dsthi, src1,src2));
  1057. end
  1058. else if dsthi=NR_NO then
  1059. begin
  1060. if (dstlo = NR_NO) then
  1061. dstlo:=getintregister(list,size);
  1062. list.concat(taicpu.op_reg_reg_reg(A_MUL, dstlo, src1,src2));
  1063. end
  1064. else
  1065. begin
  1066. internalerror(2015083022);
  1067. end;
  1068. end;
  1069. function tbasecgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  1070. var
  1071. tmpreg1,tmpreg2 : tregister;
  1072. begin
  1073. tmpreg1:=NR_NO;
  1074. { Be sure to have a base register }
  1075. if (ref.base=NR_NO) then
  1076. begin
  1077. if ref.shiftmode<>SM_None then
  1078. internalerror(2014020707);
  1079. ref.base:=ref.index;
  1080. ref.index:=NR_NO;
  1081. end;
  1082. { absolute symbols can't be handled directly, we've to store the symbol reference
  1083. in the text segment and access it pc relative
  1084. For now, we assume that references where base or index equals to PC are already
  1085. relative, all other references are assumed to be absolute and thus they need
  1086. to be handled extra.
  1087. A proper solution would be to change refoptions to a set and store the information
  1088. if the symbol is absolute or relative there.
  1089. }
  1090. if (assigned(ref.symbol) and
  1091. not(is_pc(ref.base)) and
  1092. not(is_pc(ref.index))
  1093. ) or
  1094. { [#xxx] isn't a valid address operand }
  1095. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  1096. (ref.offset<-4095) or
  1097. (ref.offset>4095) or
  1098. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  1099. ((ref.offset<-255) or
  1100. (ref.offset>255)
  1101. )
  1102. ) or
  1103. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  1104. ((ref.offset<-1020) or
  1105. (ref.offset>1020) or
  1106. ((abs(ref.offset) mod 4)<>0)
  1107. )
  1108. ) or
  1109. ((GenerateThumbCode) and
  1110. (((oppostfix in [PF_SB,PF_SH]) and (ref.offset<>0)) or
  1111. ((oppostfix=PF_None) and ((ref.offset<0) or ((ref.base<>NR_STACK_POINTER_REG) and (ref.offset>124)) or
  1112. ((ref.base=NR_STACK_POINTER_REG) and (ref.offset>1020)) or ((ref.offset mod 4)<>0))) or
  1113. ((oppostfix=PF_H) and ((ref.offset<0) or (ref.offset>62) or ((ref.offset mod 2)<>0) or ((getsupreg(ref.base) in [RS_R8..RS_R15]) and (ref.offset<>0)))) or
  1114. ((oppostfix=PF_B) and ((ref.offset<0) or (ref.offset>31) or ((getsupreg(ref.base) in [RS_R8..RS_R15]) and (ref.offset<>0))))
  1115. )
  1116. ) then
  1117. begin
  1118. fixref(list,ref);
  1119. end;
  1120. if GenerateThumbCode then
  1121. begin
  1122. { certain thumb load require base and index }
  1123. if (oppostfix in [PF_SB,PF_SH]) and
  1124. (ref.base<>NR_NO) and (ref.index=NR_NO) then
  1125. begin
  1126. tmpreg1:=getintregister(list,OS_ADDR);
  1127. a_load_const_reg(list,OS_ADDR,0,tmpreg1);
  1128. ref.index:=tmpreg1;
  1129. end;
  1130. { "hi" registers cannot be used as base or index }
  1131. if (getsupreg(ref.base) in [RS_R8..RS_R12,RS_R14]) or
  1132. ((ref.base=NR_R13) and (ref.index<>NR_NO)) then
  1133. begin
  1134. tmpreg1:=getintregister(list,OS_ADDR);
  1135. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg1);
  1136. ref.base:=tmpreg1;
  1137. end;
  1138. if getsupreg(ref.index) in [RS_R8..RS_R14] then
  1139. begin
  1140. tmpreg1:=getintregister(list,OS_ADDR);
  1141. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.index,tmpreg1);
  1142. ref.index:=tmpreg1;
  1143. end;
  1144. end;
  1145. { fold if there is base, index and offset, however, don't fold
  1146. for vfp memory instructions because we later fold the index }
  1147. if not((op in [A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  1148. (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  1149. begin
  1150. if tmpreg1<>NR_NO then
  1151. begin
  1152. tmpreg2:=getintregister(list,OS_ADDR);
  1153. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg1,tmpreg2);
  1154. tmpreg1:=tmpreg2;
  1155. end
  1156. else
  1157. begin
  1158. tmpreg1:=getintregister(list,OS_ADDR);
  1159. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg1);
  1160. ref.base:=tmpreg1;
  1161. end;
  1162. ref.offset:=0;
  1163. end;
  1164. { floating point operations have only limited references
  1165. we expect here, that a base is already set }
  1166. if ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and (ref.index<>NR_NO) then
  1167. begin
  1168. if ref.shiftmode<>SM_none then
  1169. internalerror(200309121);
  1170. if tmpreg1<>NR_NO then
  1171. begin
  1172. if ref.base=tmpreg1 then
  1173. begin
  1174. if ref.signindex<0 then
  1175. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,tmpreg1,ref.index))
  1176. else
  1177. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,tmpreg1,ref.index));
  1178. ref.index:=NR_NO;
  1179. end
  1180. else
  1181. begin
  1182. if ref.index<>tmpreg1 then
  1183. internalerror(200403161);
  1184. if ref.signindex<0 then
  1185. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,ref.base,tmpreg1))
  1186. else
  1187. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,ref.base,tmpreg1));
  1188. ref.base:=tmpreg1;
  1189. ref.index:=NR_NO;
  1190. end;
  1191. end
  1192. else
  1193. begin
  1194. tmpreg1:=getintregister(list,OS_ADDR);
  1195. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,ref.base,ref.index));
  1196. ref.base:=tmpreg1;
  1197. ref.index:=NR_NO;
  1198. end;
  1199. end;
  1200. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  1201. Result := ref;
  1202. end;
  1203. procedure tbasecgarm.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  1204. var
  1205. oppostfix:toppostfix;
  1206. usedtmpref: treference;
  1207. tmpreg : tregister;
  1208. dir : integer;
  1209. begin
  1210. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  1211. FromSize := ToSize;
  1212. case ToSize of
  1213. { signed integer registers }
  1214. OS_8,
  1215. OS_S8:
  1216. oppostfix:=PF_B;
  1217. OS_16,
  1218. OS_S16:
  1219. oppostfix:=PF_H;
  1220. OS_32,
  1221. OS_S32,
  1222. { for vfp value stored in integer register }
  1223. OS_F32:
  1224. oppostfix:=PF_None;
  1225. else
  1226. InternalError(2003082912);
  1227. end;
  1228. if ((ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[tosize])) or
  1229. ((not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) and
  1230. (oppostfix =PF_H)) then
  1231. begin
  1232. if target_info.endian=endian_big then
  1233. dir:=-1
  1234. else
  1235. dir:=1;
  1236. case FromSize of
  1237. OS_16,OS_S16:
  1238. begin
  1239. tmpreg:=getintregister(list,OS_INT);
  1240. usedtmpref:=ref;
  1241. if target_info.endian=endian_big then
  1242. inc(usedtmpref.offset,1);
  1243. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1244. inc(usedtmpref.offset,dir);
  1245. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1246. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1247. end;
  1248. OS_32,OS_S32:
  1249. begin
  1250. tmpreg:=getintregister(list,OS_INT);
  1251. usedtmpref:=ref;
  1252. if ref.alignment=2 then
  1253. begin
  1254. if target_info.endian=endian_big then
  1255. inc(usedtmpref.offset,2);
  1256. usedtmpref:=a_internal_load_reg_ref(list,OS_16,OS_16,reg,usedtmpref);
  1257. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,reg,tmpreg);
  1258. inc(usedtmpref.offset,dir*2);
  1259. a_internal_load_reg_ref(list,OS_16,OS_16,tmpreg,usedtmpref);
  1260. end
  1261. else
  1262. begin
  1263. if target_info.endian=endian_big then
  1264. inc(usedtmpref.offset,3);
  1265. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1266. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1267. inc(usedtmpref.offset,dir);
  1268. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1269. a_op_const_reg(list,OP_SHR,OS_INT,8,tmpreg);
  1270. inc(usedtmpref.offset,dir);
  1271. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1272. a_op_const_reg(list,OP_SHR,OS_INT,8,tmpreg);
  1273. inc(usedtmpref.offset,dir);
  1274. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1275. end;
  1276. end
  1277. else
  1278. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1279. end;
  1280. end
  1281. else
  1282. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1283. end;
  1284. function tbasecgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  1285. var
  1286. oppostfix:toppostfix;
  1287. href: treference;
  1288. tmpreg: TRegister;
  1289. begin
  1290. case ToSize of
  1291. { signed integer registers }
  1292. OS_8,
  1293. OS_S8:
  1294. oppostfix:=PF_B;
  1295. OS_16,
  1296. OS_S16:
  1297. oppostfix:=PF_H;
  1298. OS_32,
  1299. OS_S32:
  1300. oppostfix:=PF_None;
  1301. else
  1302. InternalError(2003082910);
  1303. end;
  1304. if (tosize in [OS_S16,OS_16]) and
  1305. (not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) then
  1306. begin
  1307. result:=handle_load_store(list,A_STR,PF_B,reg,ref);
  1308. tmpreg:=getintregister(list,OS_INT);
  1309. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1310. href:=result;
  1311. inc(href.offset);
  1312. handle_load_store(list,A_STR,PF_B,tmpreg,href);
  1313. end
  1314. else
  1315. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  1316. end;
  1317. function tbasecgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  1318. var
  1319. oppostfix:toppostfix;
  1320. so: tshifterop;
  1321. tmpreg: TRegister;
  1322. href: treference;
  1323. begin
  1324. case FromSize of
  1325. { signed integer registers }
  1326. OS_8:
  1327. oppostfix:=PF_B;
  1328. OS_S8:
  1329. oppostfix:=PF_SB;
  1330. OS_16:
  1331. oppostfix:=PF_H;
  1332. OS_S16:
  1333. oppostfix:=PF_SH;
  1334. OS_32,
  1335. OS_S32:
  1336. oppostfix:=PF_None;
  1337. else
  1338. InternalError(200308291);
  1339. end;
  1340. if (tosize=OS_S8) and
  1341. (not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) then
  1342. begin
  1343. result:=handle_load_store(list,A_LDR,PF_B,reg,ref);
  1344. a_load_reg_reg(list,OS_S8,OS_32,reg,reg);
  1345. end
  1346. else if (tosize in [OS_S16,OS_16]) and
  1347. (not (CPUARM_HAS_ALL_MEM in cpu_capabilities[current_settings.cputype])) then
  1348. begin
  1349. result:=handle_load_store(list,A_LDR,PF_B,reg,ref);
  1350. tmpreg:=getintregister(list,OS_INT);
  1351. href:=result;
  1352. inc(href.offset);
  1353. handle_load_store(list,A_LDR,PF_B,tmpreg,href);
  1354. shifterop_reset(so);
  1355. so.shiftmode:=SM_LSL;
  1356. so.shiftimm:=8;
  1357. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  1358. end
  1359. else
  1360. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  1361. end;
  1362. procedure tbasecgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1363. var
  1364. so : tshifterop;
  1365. procedure do_shift(shiftmode : tshiftmode; shiftimm : byte; reg : tregister);
  1366. begin
  1367. if GenerateThumbCode then
  1368. begin
  1369. case shiftmode of
  1370. SM_ASR:
  1371. a_op_const_reg_reg(list,OP_SAR,OS_32,shiftimm,reg,reg2);
  1372. SM_LSR:
  1373. a_op_const_reg_reg(list,OP_SHR,OS_32,shiftimm,reg,reg2);
  1374. SM_LSL:
  1375. a_op_const_reg_reg(list,OP_SHL,OS_32,shiftimm,reg,reg2);
  1376. else
  1377. internalerror(2013090301);
  1378. end;
  1379. end
  1380. else
  1381. begin
  1382. so.shiftmode:=shiftmode;
  1383. so.shiftimm:=shiftimm;
  1384. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg,so));
  1385. end;
  1386. end;
  1387. var
  1388. instr: taicpu;
  1389. conv_done: boolean;
  1390. begin
  1391. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1392. internalerror(2002090901);
  1393. conv_done:=false;
  1394. if tosize<>fromsize then
  1395. begin
  1396. shifterop_reset(so);
  1397. conv_done:=true;
  1398. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1399. fromsize:=tosize;
  1400. if current_settings.cputype<cpu_armv6 then
  1401. case fromsize of
  1402. OS_8:
  1403. if GenerateThumbCode then
  1404. a_op_const_reg_reg(list,OP_AND,OS_32,$ff,reg1,reg2)
  1405. else
  1406. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1407. OS_S8:
  1408. begin
  1409. do_shift(SM_LSL,24,reg1);
  1410. if tosize=OS_16 then
  1411. begin
  1412. do_shift(SM_ASR,8,reg2);
  1413. do_shift(SM_LSR,16,reg2);
  1414. end
  1415. else
  1416. do_shift(SM_ASR,24,reg2);
  1417. end;
  1418. OS_16:
  1419. begin
  1420. do_shift(SM_LSL,16,reg1);
  1421. do_shift(SM_LSR,16,reg2);
  1422. end;
  1423. OS_S16:
  1424. begin
  1425. do_shift(SM_LSL,16,reg1);
  1426. do_shift(SM_ASR,16,reg2)
  1427. end;
  1428. else
  1429. conv_done:=false;
  1430. end
  1431. else
  1432. case fromsize of
  1433. OS_8:
  1434. if GenerateThumbCode then
  1435. list.concat(taicpu.op_reg_reg(A_UXTB,reg2,reg1))
  1436. else
  1437. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1438. OS_S8:
  1439. begin
  1440. if tosize=OS_16 then
  1441. begin
  1442. so.shiftmode:=SM_ROR;
  1443. so.shiftimm:=16;
  1444. list.concat(taicpu.op_reg_reg_shifterop(A_SXTB16,reg2,reg1,so));
  1445. do_shift(SM_LSR,16,reg2);
  1446. end
  1447. else
  1448. list.concat(taicpu.op_reg_reg(A_SXTB,reg2,reg1));
  1449. end;
  1450. OS_16:
  1451. list.concat(taicpu.op_reg_reg(A_UXTH,reg2,reg1));
  1452. OS_S16:
  1453. list.concat(taicpu.op_reg_reg(A_SXTH,reg2,reg1));
  1454. else
  1455. conv_done:=false;
  1456. end
  1457. end;
  1458. if not conv_done and (reg1<>reg2) then
  1459. begin
  1460. { same size, only a register mov required }
  1461. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  1462. list.Concat(instr);
  1463. { Notify the register allocator that we have written a move instruction so
  1464. it can try to eliminate it. }
  1465. add_move_instruction(instr);
  1466. end;
  1467. end;
  1468. procedure tbasecgarm.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  1469. var
  1470. href,href2 : treference;
  1471. hloc : pcgparalocation;
  1472. begin
  1473. href:=ref;
  1474. hloc:=paraloc.location;
  1475. while assigned(hloc) do
  1476. begin
  1477. case hloc^.loc of
  1478. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1479. begin
  1480. paramanager.allocparaloc(list,paraloc.location);
  1481. a_loadfpu_ref_reg(list,size,size,ref,hloc^.register);
  1482. end;
  1483. LOC_REGISTER :
  1484. case hloc^.size of
  1485. OS_32,
  1486. OS_F32:
  1487. begin
  1488. paramanager.allocparaloc(list,paraloc.location);
  1489. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  1490. end;
  1491. OS_64,
  1492. OS_F64:
  1493. cg64.a_load64_ref_cgpara(list,href,paraloc);
  1494. else
  1495. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  1496. end;
  1497. LOC_REFERENCE :
  1498. begin
  1499. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset,ctempposinvalid,paraloc.alignment,[]);
  1500. { concatcopy should choose the best way to copy the data }
  1501. g_concatcopy(list,href,href2,tcgsize2size[hloc^.size]);
  1502. end;
  1503. else
  1504. internalerror(200408241);
  1505. end;
  1506. inc(href.offset,tcgsize2size[hloc^.size]);
  1507. hloc:=hloc^.next;
  1508. end;
  1509. end;
  1510. procedure tbasecgarm.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1511. begin
  1512. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[tosize]));
  1513. end;
  1514. procedure tbasecgarm.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1515. var
  1516. oppostfix:toppostfix;
  1517. begin
  1518. case fromsize of
  1519. OS_32,
  1520. OS_F32:
  1521. oppostfix:=PF_S;
  1522. OS_64,
  1523. OS_F64:
  1524. oppostfix:=PF_D;
  1525. OS_F80:
  1526. oppostfix:=PF_E;
  1527. else
  1528. InternalError(200309021);
  1529. end;
  1530. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  1531. if fromsize<>tosize then
  1532. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  1533. end;
  1534. procedure tbasecgarm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1535. var
  1536. oppostfix:toppostfix;
  1537. begin
  1538. case tosize of
  1539. OS_F32:
  1540. oppostfix:=PF_S;
  1541. OS_F64:
  1542. oppostfix:=PF_D;
  1543. OS_F80:
  1544. oppostfix:=PF_E;
  1545. else
  1546. InternalError(200309022);
  1547. end;
  1548. handle_load_store(list,A_STF,oppostfix,reg,ref);
  1549. end;
  1550. procedure tbasecgarm.g_check_for_fpu_exception(list: TAsmList;force,clear : boolean);
  1551. var
  1552. r : TRegister;
  1553. ai: taicpu;
  1554. l: TAsmLabel;
  1555. begin
  1556. if ((cs_check_fpu_exceptions in current_settings.localswitches) and
  1557. not(FPUARM_HAS_EXCEPTION_TRAPPING in fpu_capabilities[current_settings.fputype]) and
  1558. (force or current_procinfo.FPUExceptionCheckNeeded)) then
  1559. begin
  1560. r:=getintregister(list,OS_INT);
  1561. list.concat(taicpu.op_reg_reg(A_FMRX,r,NR_FPSCR));
  1562. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_AND,r,r,$9f),PF_S));
  1563. current_asmdata.getjumplabel(l);
  1564. ai:=taicpu.op_sym(A_B,l);
  1565. ai.is_jmp:=true;
  1566. ai.condition:=C_EQ;
  1567. list.concat(ai);
  1568. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1569. cg.a_call_name(list,'FPC_THROWFPUEXCEPTION',false);
  1570. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1571. a_label(list,l);
  1572. if clear then
  1573. current_procinfo.FPUExceptionCheckNeeded:=false;
  1574. end;
  1575. end;
  1576. { comparison operations }
  1577. procedure tbasecgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1578. l : tasmlabel);
  1579. var
  1580. tmpreg : tregister;
  1581. b : byte;
  1582. begin
  1583. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1584. if (not(GenerateThumbCode) and is_shifter_const(a,b)) or
  1585. ((GenerateThumbCode) and is_thumb_imm(a)) then
  1586. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  1587. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  1588. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  1589. else if (a<>$7fffffff) and (a<>-1) and not(GenerateThumbCode) and is_shifter_const(-a,b) then
  1590. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  1591. else
  1592. begin
  1593. tmpreg:=getintregister(list,size);
  1594. a_load_const_reg(list,size,a,tmpreg);
  1595. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1596. end;
  1597. a_jmp_cond(list,cmp_op,l);
  1598. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1599. end;
  1600. procedure tbasecgarm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  1601. begin
  1602. if reverse then
  1603. begin
  1604. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,src));
  1605. list.Concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,31));
  1606. list.Concat(taicpu.op_reg_reg_const(A_AND,dst,dst,255));
  1607. end
  1608. { it is decided during the compilation of the system unit if this code is used or not
  1609. so no additional check for rbit is needed }
  1610. else
  1611. begin
  1612. list.Concat(taicpu.op_reg_reg(A_RBIT,dst,src));
  1613. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,dst));
  1614. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1615. list.Concat(taicpu.op_reg_const(A_CMP,dst,32));
  1616. if GenerateThumb2Code then
  1617. list.Concat(taicpu.op_cond(A_IT, C_EQ));
  1618. list.Concat(setcondition(taicpu.op_reg_const(A_MOV,dst,$ff),C_EQ));
  1619. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1620. end;
  1621. end;
  1622. procedure tbasecgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1623. begin
  1624. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1625. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1626. a_jmp_cond(list,cmp_op,l);
  1627. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1628. end;
  1629. procedure tbasecgarm.a_jmp_name(list : TAsmList;const s : string);
  1630. var
  1631. ai : taicpu;
  1632. begin
  1633. { generate far jump, leave it to the optimizer to get rid of it }
  1634. if GenerateThumbCode then
  1635. ai:=taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s,AT_FUNCTION))
  1636. else
  1637. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s,AT_FUNCTION));
  1638. ai.is_jmp:=true;
  1639. list.concat(ai);
  1640. end;
  1641. procedure tbasecgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1642. var
  1643. ai : taicpu;
  1644. begin
  1645. { generate far jump, leave it to the optimizer to get rid of it }
  1646. if GenerateThumbCode then
  1647. ai:=taicpu.op_sym(A_BL,l)
  1648. else
  1649. ai:=taicpu.op_sym(A_B,l);
  1650. ai.is_jmp:=true;
  1651. list.concat(ai);
  1652. end;
  1653. procedure tbasecgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1654. var
  1655. ai : taicpu;
  1656. inv_flags : TResFlags;
  1657. hlabel : TAsmLabel;
  1658. begin
  1659. if GenerateThumbCode then
  1660. begin
  1661. inv_flags:=f;
  1662. inverse_flags(inv_flags);
  1663. { the optimizer has to fix this if jump range is sufficient short }
  1664. current_asmdata.getjumplabel(hlabel);
  1665. ai:=setcondition(taicpu.op_sym(A_B,hlabel),flags_to_cond(inv_flags));
  1666. ai.is_jmp:=true;
  1667. list.concat(ai);
  1668. a_jmp_always(list,l);
  1669. a_label(list,hlabel);
  1670. end
  1671. else
  1672. begin
  1673. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1674. ai.is_jmp:=true;
  1675. list.concat(ai);
  1676. end;
  1677. end;
  1678. procedure tbasecgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1679. begin
  1680. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1681. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1682. end;
  1683. procedure tbasecgarm.g_profilecode(list : TAsmList);
  1684. begin
  1685. if target_info.system = system_arm_linux then
  1686. begin
  1687. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R14]));
  1688. a_call_name(list,'__gnu_mcount_nc',false);
  1689. end
  1690. else
  1691. internalerror(2014091201);
  1692. end;
  1693. procedure tbasecgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1694. var
  1695. ref : treference;
  1696. shift : byte;
  1697. firstfloatreg,lastfloatreg,
  1698. r : byte;
  1699. mmregs,
  1700. regs, saveregs : tcpuregisterset;
  1701. registerarea,
  1702. r7offset,
  1703. stackmisalignment : pint;
  1704. imm1, imm2: DWord;
  1705. stack_parameters : Boolean;
  1706. begin
  1707. LocalSize:=align(LocalSize,4);
  1708. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  1709. { call instruction does not put anything on the stack }
  1710. registerarea:=0;
  1711. tcpuprocinfo(current_procinfo).stackpaddingreg:=High(TSuperRegister);
  1712. lastfloatreg:=RS_NO;
  1713. if not(nostackframe) then
  1714. begin
  1715. firstfloatreg:=RS_NO;
  1716. mmregs:=[];
  1717. case current_settings.fputype of
  1718. fpu_none,
  1719. fpu_soft,
  1720. fpu_libgcc:
  1721. ;
  1722. fpu_fpa,
  1723. fpu_fpa10,
  1724. fpu_fpa11:
  1725. begin
  1726. { save floating point registers? }
  1727. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1728. for r:=RS_F0 to RS_F7 do
  1729. if r in regs then
  1730. begin
  1731. if firstfloatreg=RS_NO then
  1732. firstfloatreg:=r;
  1733. lastfloatreg:=r;
  1734. inc(registerarea,12);
  1735. end;
  1736. end;
  1737. else if FPUARM_HAS_32REGS in fpu_capabilities[current_settings.fputype] then
  1738. begin;
  1739. { the *[0..31] is a hack to prevent that the compiler tries to save odd single-type registers,
  1740. they have numbers>$1f which is not really correct as they should simply have the same numbers
  1741. as the even ones by with a different subtype as it is done on x86 with al/ah }
  1742. mmregs:=(rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall))*[0..31];
  1743. end
  1744. else if FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype] then
  1745. begin;
  1746. { the *[0..15] is a hack to prevent that the compiler tries to save odd single-type registers,
  1747. they have numbers>$1f which is not really correct as they should simply have the same numbers
  1748. as the even ones by with a different subtype as it is done on x86 with al/ah }
  1749. mmregs:=(rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall))*[0..15];
  1750. end
  1751. else
  1752. internalerror(2019050924);
  1753. end;
  1754. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1755. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1756. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1757. { save int registers }
  1758. reference_reset(ref,4,[]);
  1759. ref.index:=NR_STACK_POINTER_REG;
  1760. ref.addressmode:=AM_PREINDEXED;
  1761. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1762. if not(target_info.system in systems_darwin) then
  1763. begin
  1764. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1765. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1766. begin
  1767. a_reg_alloc(list,NR_R12);
  1768. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1769. end;
  1770. { the (old) ARM APCS requires saving both the stack pointer (to
  1771. crawl the stack) and the PC (to identify the function this
  1772. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  1773. and R15 -- still needs updating for EABI and Darwin, they don't
  1774. need that }
  1775. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1776. regs:=regs+[RS_FRAME_POINTER_REG,RS_R12,RS_R14,RS_R15]
  1777. else
  1778. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1779. include(regs,RS_R14);
  1780. if regs<>[] then
  1781. begin
  1782. for r:=RS_R0 to RS_R15 do
  1783. if r in regs then
  1784. inc(registerarea,4);
  1785. { if the stack is not 8 byte aligned, try to add an extra register,
  1786. so we can avoid the extra sub/add ...,#4 later (KB) }
  1787. if ((registerarea mod current_settings.alignment.localalignmax) <> 0) then
  1788. for r:=RS_R3 downto RS_R0 do
  1789. if not(r in regs) then
  1790. begin
  1791. regs:=regs+[r];
  1792. inc(registerarea,4);
  1793. tcpuprocinfo(current_procinfo).stackpaddingreg:=r;
  1794. break;
  1795. end;
  1796. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  1797. end;
  1798. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1799. begin
  1800. { the framepointer now points to the saved R15, so the saved
  1801. framepointer is at R11-12 (for get_caller_frame) }
  1802. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1803. a_reg_dealloc(list,NR_R12);
  1804. end;
  1805. end
  1806. else
  1807. begin
  1808. { always save r14 if we use r7 as the framepointer, because
  1809. the parameter offsets are hardcoded in advance and always
  1810. assume that r14 sits on the stack right behind the saved r7
  1811. }
  1812. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1813. include(regs,RS_FRAME_POINTER_REG);
  1814. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1815. include(regs,RS_R14);
  1816. if regs<>[] then
  1817. begin
  1818. { on Darwin, you first have to save [r4-r7,lr], and then
  1819. [r8,r10,r11] and make r7 point to the previously saved
  1820. r7 so that you can perform a stack crawl based on it
  1821. ([r7] is previous stack frame, [r7+4] is return address
  1822. }
  1823. include(regs,RS_FRAME_POINTER_REG);
  1824. saveregs:=regs-[RS_R8,RS_R10,RS_R11];
  1825. r7offset:=0;
  1826. for r:=RS_R0 to RS_R15 do
  1827. if r in saveregs then
  1828. begin
  1829. inc(registerarea,4);
  1830. if r<RS_FRAME_POINTER_REG then
  1831. inc(r7offset,4);
  1832. end;
  1833. { save the registers }
  1834. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1835. { make r7 point to the saved r7 (regardless of whether this
  1836. frame uses the framepointer, for backtrace purposes) }
  1837. if r7offset<>0 then
  1838. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_FRAME_POINTER_REG,NR_R13,r7offset))
  1839. else
  1840. list.concat(taicpu.op_reg_reg(A_MOV,NR_R7,NR_R13));
  1841. { now save the rest (if any) }
  1842. saveregs:=regs-saveregs;
  1843. if saveregs<>[] then
  1844. begin
  1845. for r:=RS_R8 to RS_R11 do
  1846. if r in saveregs then
  1847. inc(registerarea,4);
  1848. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1849. end;
  1850. end;
  1851. end;
  1852. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  1853. if (LocalSize<>0) or
  1854. ((stackmisalignment<>0) and
  1855. ((pi_do_call in current_procinfo.flags) or
  1856. (po_assembler in current_procinfo.procdef.procoptions))) then
  1857. begin
  1858. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  1859. if stack_parameters and (pi_estimatestacksize in current_procinfo.flags) then
  1860. begin
  1861. if localsize>tcpuprocinfo(current_procinfo).stackframesize then
  1862. internalerror(2014030901)
  1863. else
  1864. localsize:=tcpuprocinfo(current_procinfo).stackframesize-registerarea;
  1865. end;
  1866. if is_shifter_const(localsize,shift) then
  1867. begin
  1868. a_reg_dealloc(list,NR_R12);
  1869. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1870. end
  1871. else if split_into_shifter_const(localsize, imm1, imm2) then
  1872. begin
  1873. a_reg_dealloc(list,NR_R12);
  1874. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  1875. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  1876. end
  1877. else
  1878. begin
  1879. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1880. a_reg_alloc(list,NR_R12);
  1881. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1882. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1883. a_reg_dealloc(list,NR_R12);
  1884. end;
  1885. end;
  1886. if (mmregs<>[]) or
  1887. (firstfloatreg<>RS_NO) then
  1888. begin
  1889. reference_reset(ref,4,[]);
  1890. if (tg.direction*tcpuprocinfo(current_procinfo).floatregstart>=1023) or
  1891. (FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype]) then
  1892. begin
  1893. if not is_shifter_const(tcpuprocinfo(current_procinfo).floatregstart,shift) then
  1894. begin
  1895. a_reg_alloc(list,NR_R12);
  1896. a_load_const_reg(list,OS_ADDR,-tcpuprocinfo(current_procinfo).floatregstart,NR_R12);
  1897. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1898. a_reg_dealloc(list,NR_R12);
  1899. end
  1900. else
  1901. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tcpuprocinfo(current_procinfo).floatregstart));
  1902. ref.base:=NR_R12;
  1903. end
  1904. else
  1905. begin
  1906. ref.base:=current_procinfo.framepointer;
  1907. ref.offset:=tcpuprocinfo(current_procinfo).floatregstart;
  1908. end;
  1909. case current_settings.fputype of
  1910. fpu_fpa,
  1911. fpu_fpa10,
  1912. fpu_fpa11:
  1913. begin
  1914. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1915. lastfloatreg-firstfloatreg+1,ref));
  1916. end;
  1917. else if FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[current_settings.fputype] then
  1918. begin
  1919. ref.index:=ref.base;
  1920. ref.base:=NR_NO;
  1921. if mmregs<>[] then
  1922. list.concat(taicpu.op_ref_regset(A_VSTM,ref,R_MMREGISTER,R_SUBFD,mmregs));
  1923. end
  1924. else if FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype] then
  1925. begin
  1926. ref.index:=ref.base;
  1927. ref.base:=NR_NO;
  1928. if mmregs<>[] then
  1929. list.concat(taicpu.op_ref_regset(A_VSTM,ref,R_MMREGISTER,R_SUBFS,mmregs));
  1930. end
  1931. else
  1932. internalerror(2019050923);
  1933. end;
  1934. end;
  1935. end;
  1936. end;
  1937. procedure tbasecgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1938. var
  1939. ref : treference;
  1940. LocalSize : longint;
  1941. firstfloatreg,lastfloatreg,
  1942. r,
  1943. shift : byte;
  1944. mmregs,
  1945. saveregs,
  1946. regs : tcpuregisterset;
  1947. registerarea,
  1948. stackmisalignment: pint;
  1949. paddingreg: TSuperRegister;
  1950. imm1, imm2: DWord;
  1951. begin
  1952. if not(nostackframe) then
  1953. begin
  1954. registerarea:=0;
  1955. firstfloatreg:=RS_NO;
  1956. lastfloatreg:=RS_NO;
  1957. mmregs:=[];
  1958. saveregs:=[];
  1959. case current_settings.fputype of
  1960. fpu_none,
  1961. fpu_soft,
  1962. fpu_libgcc:
  1963. ;
  1964. fpu_fpa,
  1965. fpu_fpa10,
  1966. fpu_fpa11:
  1967. begin
  1968. { restore floating point registers? }
  1969. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1970. for r:=RS_F0 to RS_F7 do
  1971. if r in regs then
  1972. begin
  1973. if firstfloatreg=RS_NO then
  1974. firstfloatreg:=r;
  1975. lastfloatreg:=r;
  1976. { floating point register space is already included in
  1977. localsize below by calc_stackframe_size
  1978. inc(registerarea,12);
  1979. }
  1980. end;
  1981. end;
  1982. else if FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype] then
  1983. begin
  1984. { restore vfp registers? }
  1985. { the *[0..31] is a hack to prevent that the compiler tries to save odd single-type registers,
  1986. they have numbers>$1f which is not really correct as they should simply have the same numbers
  1987. as the even ones by with a different subtype as it is done on x86 with al/ah }
  1988. mmregs:=(rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall))*[0..31];
  1989. end
  1990. else
  1991. internalerror(2019050908);
  1992. end;
  1993. if (firstfloatreg<>RS_NO) or
  1994. (mmregs<>[]) then
  1995. begin
  1996. reference_reset(ref,4,[]);
  1997. if (tg.direction*tcpuprocinfo(current_procinfo).floatregstart>=1023) or
  1998. (FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype]) then
  1999. begin
  2000. if not is_shifter_const(tcpuprocinfo(current_procinfo).floatregstart,shift) then
  2001. begin
  2002. a_reg_alloc(list,NR_R12);
  2003. a_load_const_reg(list,OS_ADDR,-tcpuprocinfo(current_procinfo).floatregstart,NR_R12);
  2004. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  2005. a_reg_dealloc(list,NR_R12);
  2006. end
  2007. else
  2008. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tcpuprocinfo(current_procinfo).floatregstart));
  2009. ref.base:=NR_R12;
  2010. end
  2011. else
  2012. begin
  2013. ref.base:=current_procinfo.framepointer;
  2014. ref.offset:=tcpuprocinfo(current_procinfo).floatregstart;
  2015. end;
  2016. case current_settings.fputype of
  2017. fpu_fpa,
  2018. fpu_fpa10,
  2019. fpu_fpa11:
  2020. begin
  2021. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  2022. lastfloatreg-firstfloatreg+1,ref));
  2023. end;
  2024. else if FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[current_settings.fputype] then
  2025. begin
  2026. ref.index:=ref.base;
  2027. ref.base:=NR_NO;
  2028. if mmregs<>[] then
  2029. list.concat(taicpu.op_ref_regset(A_VLDM,ref,R_MMREGISTER,R_SUBFD,mmregs));
  2030. end
  2031. else if FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype] then
  2032. begin
  2033. ref.index:=ref.base;
  2034. ref.base:=NR_NO;
  2035. if mmregs<>[] then
  2036. list.concat(taicpu.op_ref_regset(A_VLDM,ref,R_MMREGISTER,R_SUBFS,mmregs));
  2037. end
  2038. else
  2039. internalerror(2019050921);
  2040. end;
  2041. end;
  2042. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  2043. if (pi_do_call in current_procinfo.flags) or
  2044. (regs<>[]) or
  2045. ((target_info.system in systems_darwin) and
  2046. (current_procinfo.framepointer<>NR_STACK_POINTER_REG)) then
  2047. begin
  2048. exclude(regs,RS_R14);
  2049. include(regs,RS_R15);
  2050. if (target_info.system in systems_darwin) then
  2051. include(regs,RS_FRAME_POINTER_REG);
  2052. end;
  2053. if not(target_info.system in systems_darwin) then
  2054. begin
  2055. { restore saved stack pointer to SP (R13) and saved lr to PC (R15).
  2056. The saved PC came after that but is discarded, since we restore
  2057. the stack pointer }
  2058. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  2059. regs:=regs+[RS_FRAME_POINTER_REG,RS_R13,RS_R15];
  2060. end
  2061. else
  2062. begin
  2063. { restore R8-R11 already if necessary (they've been stored
  2064. before the others) }
  2065. saveregs:=regs*[RS_R8,RS_R10,RS_R11];
  2066. if saveregs<>[] then
  2067. begin
  2068. reference_reset(ref,4,[]);
  2069. ref.index:=NR_STACK_POINTER_REG;
  2070. ref.addressmode:=AM_PREINDEXED;
  2071. for r:=RS_R8 to RS_R11 do
  2072. if r in saveregs then
  2073. inc(registerarea,4);
  2074. regs:=regs-saveregs;
  2075. end;
  2076. end;
  2077. for r:=RS_R0 to RS_R15 do
  2078. if r in regs then
  2079. inc(registerarea,4);
  2080. { reapply the stack padding reg, in case there was one, see the complimentary
  2081. comment in g_proc_entry() (KB) }
  2082. paddingreg:=tcpuprocinfo(current_procinfo).stackpaddingreg;
  2083. if paddingreg < RS_R4 then
  2084. if paddingreg in regs then
  2085. internalerror(201306190)
  2086. else
  2087. begin
  2088. regs:=regs+[paddingreg];
  2089. inc(registerarea,4);
  2090. end;
  2091. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  2092. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  2093. (target_info.system in systems_darwin) then
  2094. begin
  2095. LocalSize:=current_procinfo.calc_stackframe_size;
  2096. if (LocalSize<>0) or
  2097. ((stackmisalignment<>0) and
  2098. ((pi_do_call in current_procinfo.flags) or
  2099. (po_assembler in current_procinfo.procdef.procoptions))) then
  2100. begin
  2101. if pi_estimatestacksize in current_procinfo.flags then
  2102. LocalSize:=tcpuprocinfo(current_procinfo).stackframesize-registerarea
  2103. else
  2104. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  2105. if is_shifter_const(LocalSize,shift) then
  2106. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  2107. else if split_into_shifter_const(localsize, imm1, imm2) then
  2108. begin
  2109. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  2110. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  2111. end
  2112. else
  2113. begin
  2114. a_reg_alloc(list,NR_R12);
  2115. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  2116. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  2117. a_reg_dealloc(list,NR_R12);
  2118. end;
  2119. end;
  2120. if (target_info.system in systems_darwin) and
  2121. (saveregs<>[]) then
  2122. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  2123. if regs=[] then
  2124. begin
  2125. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2126. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2127. else
  2128. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2129. end
  2130. else
  2131. begin
  2132. reference_reset(ref,4,[]);
  2133. ref.index:=NR_STACK_POINTER_REG;
  2134. ref.addressmode:=AM_PREINDEXED;
  2135. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  2136. end;
  2137. end
  2138. else
  2139. begin
  2140. { restore int registers and return }
  2141. reference_reset(ref,4,[]);
  2142. ref.index:=NR_FRAME_POINTER_REG;
  2143. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_EA));
  2144. end;
  2145. end
  2146. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2147. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2148. else
  2149. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2150. end;
  2151. procedure tbasecgarm.g_maybe_got_init(list : TAsmList);
  2152. var
  2153. ref : treference;
  2154. l : TAsmLabel;
  2155. regs : tcpuregisterset;
  2156. r: byte;
  2157. begin
  2158. if (cs_create_pic in current_settings.moduleswitches) and
  2159. (pi_needs_got in current_procinfo.flags) and
  2160. (tf_pic_uses_got in target_info.flags) then
  2161. begin
  2162. { Procedure parametrs are not initialized at this stage.
  2163. Before GOT initialization code, allocate registers used for procedure parameters
  2164. to prevent usage of these registers for temp operations in later stages of code
  2165. generation. }
  2166. regs:=rg[R_INTREGISTER].used_in_proc;
  2167. for r:=RS_R0 to RS_R3 do
  2168. if r in regs then
  2169. a_reg_alloc(list, newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2170. { Allocate scratch register R12 and use it for GOT calculations directly.
  2171. Otherwise the init code can be distorted in later stages of code generation. }
  2172. a_reg_alloc(list,NR_R12);
  2173. reference_reset(ref,4,[]);
  2174. current_asmdata.getglobaldatalabel(l);
  2175. cg.a_label(current_procinfo.aktlocaldata,l);
  2176. ref.symbol:=l;
  2177. ref.base:=NR_PC;
  2178. ref.symboldata:=current_procinfo.aktlocaldata.last;
  2179. list.concat(Taicpu.op_reg_ref(A_LDR,NR_R12,ref));
  2180. current_asmdata.getaddrlabel(l);
  2181. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_32bit,l,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_',AT_DATA),-8));
  2182. cg.a_label(list,l);
  2183. list.concat(Taicpu.op_reg_reg_reg(A_ADD,NR_R12,NR_PC,NR_R12));
  2184. list.concat(Taicpu.op_reg_reg(A_MOV,current_procinfo.got,NR_R12));
  2185. { Deallocate registers }
  2186. a_reg_dealloc(list,NR_R12);
  2187. for r:=RS_R3 downto RS_R0 do
  2188. if r in regs then
  2189. a_reg_dealloc(list, newreg(R_INTREGISTER,r,R_SUBWHOLE));
  2190. end;
  2191. end;
  2192. procedure tbasecgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  2193. var
  2194. b : byte;
  2195. tmpref : treference;
  2196. instr : taicpu;
  2197. begin
  2198. if ref.addressmode<>AM_OFFSET then
  2199. internalerror(200309071);
  2200. tmpref:=ref;
  2201. { Be sure to have a base register }
  2202. if (tmpref.base=NR_NO) then
  2203. begin
  2204. if tmpref.shiftmode<>SM_None then
  2205. internalerror(2014020702);
  2206. if tmpref.signindex<0 then
  2207. internalerror(200312023);
  2208. tmpref.base:=tmpref.index;
  2209. tmpref.index:=NR_NO;
  2210. end;
  2211. if assigned(tmpref.symbol) or
  2212. not((is_shifter_const(tmpref.offset,b)) or
  2213. (is_shifter_const(-tmpref.offset,b))
  2214. ) then
  2215. fixref(list,tmpref);
  2216. { expect a base here if there is an index }
  2217. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  2218. internalerror(200312022);
  2219. if tmpref.index<>NR_NO then
  2220. begin
  2221. if tmpref.shiftmode<>SM_None then
  2222. internalerror(200312021);
  2223. if tmpref.signindex<0 then
  2224. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  2225. else
  2226. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  2227. if tmpref.offset<>0 then
  2228. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  2229. end
  2230. else
  2231. begin
  2232. if tmpref.base=NR_NO then
  2233. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  2234. else
  2235. if tmpref.offset<>0 then
  2236. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  2237. else
  2238. begin
  2239. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  2240. list.concat(instr);
  2241. add_move_instruction(instr);
  2242. end;
  2243. end;
  2244. end;
  2245. procedure tbasecgarm.fixref(list : TAsmList;var ref : treference);
  2246. var
  2247. tmpreg, tmpreg2 : tregister;
  2248. tmpref : treference;
  2249. l, piclabel : tasmlabel;
  2250. indirection_done : boolean;
  2251. begin
  2252. { absolute symbols can't be handled directly, we've to store the symbol reference
  2253. in the text segment and access it pc relative
  2254. For now, we assume that references where base or index equals to PC are already
  2255. relative, all other references are assumed to be absolute and thus they need
  2256. to be handled extra.
  2257. A proper solution would be to change refoptions to a set and store the information
  2258. if the symbol is absolute or relative there.
  2259. }
  2260. { create consts entry }
  2261. reference_reset(tmpref,4,[]);
  2262. current_asmdata.getjumplabel(l);
  2263. cg.a_label(current_procinfo.aktlocaldata,l);
  2264. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2265. piclabel:=nil;
  2266. tmpreg:=NR_NO;
  2267. indirection_done:=false;
  2268. if assigned(ref.symbol) then
  2269. begin
  2270. if (target_info.system=system_arm_ios) and
  2271. (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) then
  2272. begin
  2273. tmpreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  2274. if ref.offset<>0 then
  2275. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg);
  2276. indirection_done:=true;
  2277. end
  2278. else if ref.refaddr=addr_gottpoff then
  2279. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_gottpoff,ref.symbol,ref.relsymbol,ref.offset))
  2280. else if ref.refaddr=addr_tlsgd then
  2281. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_tlsgd,ref.symbol,ref.relsymbol,ref.offset))
  2282. else if ref.refaddr=addr_tlsdesc then
  2283. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_tlsdesc,ref.symbol,ref.relsymbol,ref.offset))
  2284. else if ref.refaddr=addr_tpoff then
  2285. begin
  2286. if assigned(ref.relsymbol) or (ref.offset<>0) then
  2287. Internalerror(2019092804);
  2288. current_procinfo.aktlocaldata.concat(tai_const.Create_type_sym(aitconst_tpoff,ref.symbol));
  2289. end
  2290. else if (cs_create_pic in current_settings.moduleswitches) then
  2291. if (tf_pic_uses_got in target_info.flags) then
  2292. current_procinfo.aktlocaldata.concat(tai_const.Create_type_sym(aitconst_got,ref.symbol))
  2293. else
  2294. begin
  2295. { ideally, we would want to generate
  2296. ldr r1, LPICConstPool
  2297. LPICLocal:
  2298. ldr/str r2,[pc,r1]
  2299. ...
  2300. LPICConstPool:
  2301. .long _globsym-(LPICLocal+8)
  2302. However, we cannot be sure that the ldr/str will follow
  2303. right after the call to fixref, so we have to load the
  2304. complete address already in a register.
  2305. }
  2306. current_asmdata.getaddrlabel(piclabel);
  2307. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,ref.symbol,ref.offset-8));
  2308. end
  2309. else
  2310. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  2311. end
  2312. else
  2313. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  2314. { load consts entry }
  2315. if not indirection_done then
  2316. begin
  2317. tmpreg:=getintregister(list,OS_INT);
  2318. tmpref.symbol:=l;
  2319. tmpref.base:=NR_PC;
  2320. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2321. if (cs_create_pic in current_settings.moduleswitches) and
  2322. (tf_pic_uses_got in target_info.flags) and
  2323. assigned(ref.symbol) then
  2324. begin
  2325. {$ifdef EXTDEBUG}
  2326. if not (pi_needs_got in current_procinfo.flags) then
  2327. Comment(V_warning,'pi_needs_got not included');
  2328. {$endif EXTDEBUG}
  2329. Include(current_procinfo.flags,pi_needs_got);
  2330. reference_reset(tmpref,4,[]);
  2331. tmpref.base:=current_procinfo.got;
  2332. tmpref.index:=tmpreg;
  2333. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2334. if ref.offset<>0 then
  2335. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg);
  2336. end;
  2337. end;
  2338. if assigned(piclabel) then
  2339. begin
  2340. cg.a_label(list,piclabel);
  2341. tmpreg2:=getaddressregister(list);
  2342. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpreg,NR_PC,tmpreg2);
  2343. tmpreg:=tmpreg2
  2344. end;
  2345. { This routine can be called with PC as base/index in case the offset
  2346. was too large to encode in a load/store. In that case, the entire
  2347. absolute expression has been re-encoded in a new constpool entry, and
  2348. we have to remove the use of PC from the original reference (the code
  2349. above made everything relative to the value loaded from the new
  2350. constpool entry) }
  2351. if is_pc(ref.base) then
  2352. ref.base:=NR_NO;
  2353. if is_pc(ref.index) then
  2354. ref.index:=NR_NO;
  2355. if (ref.base<>NR_NO) then
  2356. begin
  2357. if ref.index<>NR_NO then
  2358. begin
  2359. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  2360. ref.base:=tmpreg;
  2361. end
  2362. else
  2363. if ref.base<>NR_PC then
  2364. begin
  2365. ref.index:=tmpreg;
  2366. ref.shiftimm:=0;
  2367. ref.signindex:=1;
  2368. ref.shiftmode:=SM_None;
  2369. end
  2370. else
  2371. ref.base:=tmpreg;
  2372. end
  2373. else
  2374. ref.base:=tmpreg;
  2375. ref.offset:=0;
  2376. ref.symbol:=nil;
  2377. end;
  2378. procedure tbasecgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  2379. var
  2380. paraloc1,paraloc2,paraloc3 : TCGPara;
  2381. pd : tprocdef;
  2382. begin
  2383. pd:=search_system_proc('MOVE');
  2384. paraloc1.init;
  2385. paraloc2.init;
  2386. paraloc3.init;
  2387. paramanager.getcgtempparaloc(list,pd,1,paraloc1);
  2388. paramanager.getcgtempparaloc(list,pd,2,paraloc2);
  2389. paramanager.getcgtempparaloc(list,pd,3,paraloc3);
  2390. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  2391. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  2392. a_loadaddr_ref_cgpara(list,source,paraloc1);
  2393. paramanager.freecgpara(list,paraloc3);
  2394. paramanager.freecgpara(list,paraloc2);
  2395. paramanager.freecgpara(list,paraloc1);
  2396. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2397. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2398. a_call_name(list,'FPC_MOVE',false);
  2399. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2400. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2401. paraloc3.done;
  2402. paraloc2.done;
  2403. paraloc1.done;
  2404. end;
  2405. procedure tbasecgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  2406. const
  2407. maxtmpreg_arm = 10; {roozbeh: can be reduced to 8 or lower if might conflick with reserved ones,also +2 is used becouse of regs required for referencing}
  2408. maxtmpreg_thumb = 5;
  2409. var
  2410. srcref,dstref,usedtmpref,usedtmpref2:treference;
  2411. srcreg,destreg,countreg,r,tmpreg:tregister;
  2412. helpsize:aint;
  2413. copysize:byte;
  2414. cgsize:Tcgsize;
  2415. tmpregisters:array[1..maxtmpreg_arm] of tregister;
  2416. maxtmpreg,
  2417. tmpregi,tmpregi2:byte;
  2418. { will never be called with count<=4 }
  2419. procedure genloop(count : aword;size : byte);
  2420. const
  2421. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2422. var
  2423. l : tasmlabel;
  2424. begin
  2425. current_asmdata.getjumplabel(l);
  2426. if count<size then size:=1;
  2427. a_load_const_reg(list,OS_INT,count div size,countreg);
  2428. cg.a_label(list,l);
  2429. srcref.addressmode:=AM_POSTINDEXED;
  2430. dstref.addressmode:=AM_POSTINDEXED;
  2431. srcref.offset:=size;
  2432. dstref.offset:=size;
  2433. r:=getintregister(list,size2opsize[size]);
  2434. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2435. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2436. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  2437. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2438. a_jmp_flags(list,F_NE,l);
  2439. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2440. srcref.offset:=1;
  2441. dstref.offset:=1;
  2442. case count mod size of
  2443. 1:
  2444. begin
  2445. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2446. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2447. end;
  2448. 2:
  2449. if aligned then
  2450. begin
  2451. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2452. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2453. end
  2454. else
  2455. begin
  2456. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2457. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2458. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2459. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2460. end;
  2461. 3:
  2462. if aligned then
  2463. begin
  2464. srcref.offset:=2;
  2465. dstref.offset:=2;
  2466. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2467. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2468. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2469. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2470. end
  2471. else
  2472. begin
  2473. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2474. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2475. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2476. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2477. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2478. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2479. end;
  2480. end;
  2481. { keep the registers alive }
  2482. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2483. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2484. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2485. end;
  2486. { save estimation, if a creating a separate ref is needed or
  2487. if we can keep the original reference while copying }
  2488. function SimpleRef(const ref : treference) : boolean;
  2489. begin
  2490. result:=((ref.base=NR_PC) and (ref.addressmode=AM_OFFSET) and (ref.refaddr in [addr_full,addr_no])) or
  2491. ((ref.symbol=nil) and
  2492. (ref.addressmode=AM_OFFSET) and
  2493. (((ref.offset>=0) and (ref.offset+len<=31)) or
  2494. (not(GenerateThumbCode) and (ref.offset>=-255) and (ref.offset+len<=255)) or
  2495. { ldrh has a limited offset range }
  2496. (not(GenerateThumbCode) and ((len mod 4) in [0,1]) and (ref.offset>=-4095) and (ref.offset+len<=4095))
  2497. )
  2498. );
  2499. end;
  2500. { will never be called with count<=4 }
  2501. procedure genloop_thumb(count : aword;size : byte);
  2502. procedure refincofs(const ref : treference;const value : longint = 1);
  2503. begin
  2504. a_op_const_reg(list,OP_ADD,OS_ADDR,value,ref.base);
  2505. end;
  2506. const
  2507. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2508. var
  2509. l : tasmlabel;
  2510. begin
  2511. current_asmdata.getjumplabel(l);
  2512. if count<size then size:=1;
  2513. a_load_const_reg(list,OS_INT,count div size,countreg);
  2514. cg.a_label(list,l);
  2515. r:=getintregister(list,size2opsize[size]);
  2516. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2517. refincofs(srcref);
  2518. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2519. refincofs(dstref);
  2520. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2521. list.concat(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1));
  2522. a_jmp_flags(list,F_NE,l);
  2523. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2524. case count mod size of
  2525. 1:
  2526. begin
  2527. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2528. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2529. end;
  2530. 2:
  2531. if aligned then
  2532. begin
  2533. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2534. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2535. end
  2536. else
  2537. begin
  2538. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2539. refincofs(srcref);
  2540. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2541. refincofs(dstref);
  2542. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2543. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2544. end;
  2545. 3:
  2546. if aligned then
  2547. begin
  2548. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2549. refincofs(srcref,2);
  2550. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2551. refincofs(dstref,2);
  2552. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2553. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2554. end
  2555. else
  2556. begin
  2557. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2558. refincofs(srcref);
  2559. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2560. refincofs(dstref);
  2561. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2562. refincofs(srcref);
  2563. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2564. refincofs(dstref);
  2565. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2566. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2567. end;
  2568. end;
  2569. { keep the registers alive }
  2570. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2571. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2572. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2573. end;
  2574. begin
  2575. if len=0 then
  2576. exit;
  2577. if GenerateThumbCode then
  2578. maxtmpreg:=maxtmpreg_thumb
  2579. else
  2580. maxtmpreg:=maxtmpreg_arm;
  2581. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  2582. dstref:=dest;
  2583. srcref:=source;
  2584. if cs_opt_size in current_settings.optimizerswitches then
  2585. helpsize:=8;
  2586. if aligned and (len=4) then
  2587. begin
  2588. tmpreg:=getintregister(list,OS_32);
  2589. a_load_ref_reg(list,OS_32,OS_32,source,tmpreg);
  2590. a_load_reg_ref(list,OS_32,OS_32,tmpreg,dest);
  2591. end
  2592. else if aligned and (len=2) then
  2593. begin
  2594. tmpreg:=getintregister(list,OS_16);
  2595. a_load_ref_reg(list,OS_16,OS_16,source,tmpreg);
  2596. a_load_reg_ref(list,OS_16,OS_16,tmpreg,dest);
  2597. end
  2598. else if (len<=helpsize) and aligned then
  2599. begin
  2600. tmpregi:=0;
  2601. { loading address in a separate register needed? }
  2602. if SimpleRef(source) then
  2603. begin
  2604. { ... then we don't need a loadaddr }
  2605. srcref:=source;
  2606. end
  2607. else
  2608. begin
  2609. srcreg:=getintregister(list,OS_ADDR);
  2610. a_loadaddr_ref_reg(list,source,srcreg);
  2611. reference_reset_base(srcref,srcreg,0,source.temppos,source.alignment,source.volatility);
  2612. end;
  2613. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  2614. begin
  2615. inc(tmpregi);
  2616. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  2617. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  2618. inc(srcref.offset,4);
  2619. dec(len,4);
  2620. end;
  2621. { loading address in a separate register needed? }
  2622. if SimpleRef(dest) then
  2623. dstref:=dest
  2624. else
  2625. begin
  2626. destreg:=getintregister(list,OS_ADDR);
  2627. a_loadaddr_ref_reg(list,dest,destreg);
  2628. reference_reset_base(dstref,destreg,0,dest.temppos,dest.alignment,dest.volatility);
  2629. end;
  2630. tmpregi2:=1;
  2631. while (tmpregi2<=tmpregi) do
  2632. begin
  2633. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  2634. inc(dstref.offset,4);
  2635. inc(tmpregi2);
  2636. end;
  2637. copysize:=4;
  2638. cgsize:=OS_32;
  2639. while len<>0 do
  2640. begin
  2641. if len<2 then
  2642. begin
  2643. copysize:=1;
  2644. cgsize:=OS_8;
  2645. end
  2646. else if len<4 then
  2647. begin
  2648. copysize:=2;
  2649. cgsize:=OS_16;
  2650. end;
  2651. dec(len,copysize);
  2652. r:=getintregister(list,cgsize);
  2653. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2654. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2655. inc(srcref.offset,copysize);
  2656. inc(dstref.offset,copysize);
  2657. end;{end of while}
  2658. end
  2659. else
  2660. begin
  2661. cgsize:=OS_32;
  2662. if (len<=4) then{len<=4 and not aligned}
  2663. begin
  2664. r:=getintregister(list,cgsize);
  2665. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2666. if Len=1 then
  2667. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  2668. else
  2669. begin
  2670. tmpreg:=getintregister(list,cgsize);
  2671. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2672. inc(usedtmpref.offset,1);
  2673. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2674. inc(usedtmpref2.offset,1);
  2675. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2676. if len>2 then
  2677. begin
  2678. inc(usedtmpref.offset,1);
  2679. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2680. inc(usedtmpref2.offset,1);
  2681. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2682. if len>3 then
  2683. begin
  2684. inc(usedtmpref.offset,1);
  2685. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2686. inc(usedtmpref2.offset,1);
  2687. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2688. end;
  2689. end;
  2690. end;
  2691. end{end of if len<=4}
  2692. else
  2693. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  2694. destreg:=getintregister(list,OS_ADDR);
  2695. a_loadaddr_ref_reg(list,dest,destreg);
  2696. reference_reset_base(dstref,destreg,0,dest.temppos,dest.alignment,dest.volatility);
  2697. srcreg:=getintregister(list,OS_ADDR);
  2698. a_loadaddr_ref_reg(list,source,srcreg);
  2699. reference_reset_base(srcref,srcreg,0,dest.temppos,source.alignment,source.volatility);
  2700. countreg:=getintregister(list,OS_32);
  2701. // if cs_opt_size in current_settings.optimizerswitches then
  2702. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  2703. {if aligned then
  2704. genloop(len,4)
  2705. else}
  2706. if GenerateThumbCode then
  2707. genloop_thumb(len,1)
  2708. else
  2709. genloop(len,1);
  2710. end;
  2711. end;
  2712. end;
  2713. procedure tbasecgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2714. begin
  2715. g_concatcopy_internal(list,source,dest,len,false);
  2716. end;
  2717. procedure tbasecgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  2718. begin
  2719. if (source.alignment in [1,3]) or
  2720. (dest.alignment in [1,3]) then
  2721. g_concatcopy_internal(list,source,dest,len,false)
  2722. else
  2723. g_concatcopy_internal(list,source,dest,len,true);
  2724. end;
  2725. procedure tbasecgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  2726. var
  2727. ovloc : tlocation;
  2728. begin
  2729. ovloc.loc:=LOC_VOID;
  2730. g_overflowCheck_loc(list,l,def,ovloc);
  2731. end;
  2732. procedure tbasecgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2733. var
  2734. hl : tasmlabel;
  2735. ai:TAiCpu;
  2736. hflags : tresflags;
  2737. begin
  2738. if not(cs_check_overflow in current_settings.localswitches) then
  2739. exit;
  2740. current_asmdata.getjumplabel(hl);
  2741. case ovloc.loc of
  2742. LOC_VOID:
  2743. begin
  2744. ai:=taicpu.op_sym(A_B,hl);
  2745. ai.is_jmp:=true;
  2746. if not((def.typ=pointerdef) or
  2747. ((def.typ=orddef) and
  2748. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2749. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2750. ai.SetCondition(C_VC)
  2751. else
  2752. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  2753. ai.SetCondition(C_CS)
  2754. else
  2755. ai.SetCondition(C_CC);
  2756. list.concat(ai);
  2757. end;
  2758. LOC_FLAGS:
  2759. begin
  2760. hflags:=ovloc.resflags;
  2761. inverse_flags(hflags);
  2762. cg.a_jmp_flags(list,hflags,hl);
  2763. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2764. end;
  2765. else
  2766. internalerror(200409281);
  2767. end;
  2768. a_call_name(list,'FPC_OVERFLOW',false);
  2769. a_label(list,hl);
  2770. end;
  2771. procedure tbasecgarm.g_save_registers(list : TAsmList);
  2772. begin
  2773. { this work is done in g_proc_entry }
  2774. end;
  2775. procedure tbasecgarm.g_restore_registers(list : TAsmList);
  2776. begin
  2777. { this work is done in g_proc_exit }
  2778. end;
  2779. procedure tbasecgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2780. var
  2781. ai : taicpu;
  2782. hlabel : TAsmLabel;
  2783. begin
  2784. if GenerateThumbCode then
  2785. begin
  2786. { the optimizer has to fix this if jump range is sufficient short }
  2787. current_asmdata.getjumplabel(hlabel);
  2788. ai:=Taicpu.Op_sym(A_B,hlabel);
  2789. ai.SetCondition(inverse_cond(OpCmp2AsmCond[cond]));
  2790. ai.is_jmp:=true;
  2791. list.concat(ai);
  2792. a_jmp_always(list,l);
  2793. a_label(list,hlabel);
  2794. end
  2795. else
  2796. begin
  2797. ai:=Taicpu.Op_sym(A_B,l);
  2798. ai.SetCondition(OpCmp2AsmCond[cond]);
  2799. ai.is_jmp:=true;
  2800. list.concat(ai);
  2801. end;
  2802. end;
  2803. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  2804. const
  2805. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  2806. (A_VMOV,A_VCVT,A_NONE,A_NONE,A_NONE),
  2807. (A_VCVT,A_VMOV,A_NONE,A_NONE,A_NONE),
  2808. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2809. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2810. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  2811. begin
  2812. result:=convertop[fromsize,tosize];
  2813. if result=A_NONE then
  2814. internalerror(200312205);
  2815. end;
  2816. function get_scalar_mm_prefix(fromsize,tosize : tcgsize) : TOpPostfix;
  2817. const
  2818. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of TOpPostfix = (
  2819. (PF_F32, PF_F32F64,PF_None,PF_None,PF_None),
  2820. (PF_F64F32,PF_F64, PF_None,PF_None,PF_None),
  2821. (PF_None, PF_None, PF_None,PF_None,PF_None),
  2822. (PF_None, PF_None, PF_None,PF_None,PF_None),
  2823. (PF_None, PF_None, PF_None,PF_None,PF_None));
  2824. begin
  2825. result:=convertop[fromsize,tosize];
  2826. end;
  2827. procedure tbasecgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2828. var
  2829. instr: taicpu;
  2830. begin
  2831. if (shuffle=nil) or shufflescalar(shuffle) then
  2832. instr:=setoppostfix(taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1),get_scalar_mm_prefix(tosize,fromsize))
  2833. else
  2834. internalerror(2009112407);
  2835. list.concat(instr);
  2836. case instr.opcode of
  2837. A_VMOV:
  2838. { VMOV cannot generate an FPU exception, so we do not need a check here }
  2839. add_move_instruction(instr);
  2840. else
  2841. { VCVT can generate an exception }
  2842. maybe_check_for_fpu_exception(list);
  2843. end;
  2844. end;
  2845. procedure tbasecgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2846. var
  2847. intreg,
  2848. tmpmmreg : tregister;
  2849. reg64 : tregister64;
  2850. begin
  2851. if assigned(shuffle) and
  2852. not(shufflescalar(shuffle)) then
  2853. internalerror(2009112413);
  2854. case fromsize of
  2855. OS_32,OS_S32:
  2856. begin
  2857. fromsize:=OS_F32;
  2858. { since we are loading an integer, no conversion may be required }
  2859. if (fromsize<>tosize) then
  2860. internalerror(2009112801);
  2861. end;
  2862. OS_64,OS_S64:
  2863. begin
  2864. fromsize:=OS_F64;
  2865. { since we are loading an integer, no conversion may be required }
  2866. if (fromsize<>tosize) then
  2867. internalerror(2009112901);
  2868. end;
  2869. OS_F32,OS_F64:
  2870. ;
  2871. else
  2872. internalerror(2019050920);
  2873. end;
  2874. if (fromsize<>tosize) then
  2875. tmpmmreg:=getmmregister(list,fromsize)
  2876. else
  2877. tmpmmreg:=reg;
  2878. if (ref.alignment in [1,2]) then
  2879. begin
  2880. case fromsize of
  2881. OS_F32:
  2882. begin
  2883. intreg:=getintregister(list,OS_32);
  2884. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2885. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2886. end;
  2887. OS_F64:
  2888. begin
  2889. reg64.reglo:=getintregister(list,OS_32);
  2890. reg64.reghi:=getintregister(list,OS_32);
  2891. cg64.a_load64_ref_reg(list,ref,reg64);
  2892. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2893. end;
  2894. else
  2895. internalerror(2009112412);
  2896. end;
  2897. end
  2898. else
  2899. handle_load_store(list,A_VLDR,PF_None,tmpmmreg,ref);
  2900. if (tmpmmreg<>reg) then
  2901. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2902. end;
  2903. procedure tbasecgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2904. var
  2905. intreg,
  2906. tmpmmreg : tregister;
  2907. reg64 : tregister64;
  2908. begin
  2909. if assigned(shuffle) and
  2910. not(shufflescalar(shuffle)) then
  2911. internalerror(2009112416);
  2912. case tosize of
  2913. OS_32,OS_S32:
  2914. begin
  2915. tosize:=OS_F32;
  2916. { since we are loading an integer, no conversion may be required }
  2917. if (fromsize<>tosize) then
  2918. internalerror(2009112802);
  2919. end;
  2920. OS_64,OS_S64:
  2921. begin
  2922. tosize:=OS_F64;
  2923. { since we are loading an integer, no conversion may be required }
  2924. if (fromsize<>tosize) then
  2925. internalerror(2009112902);
  2926. end;
  2927. OS_F32,OS_F64:
  2928. ;
  2929. else
  2930. internalerror(2019050919);
  2931. end;
  2932. if (fromsize<>tosize) then
  2933. begin
  2934. tmpmmreg:=getmmregister(list,tosize);
  2935. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2936. end
  2937. else
  2938. tmpmmreg:=reg;
  2939. if (ref.alignment in [1,2]) then
  2940. begin
  2941. case tosize of
  2942. OS_F32:
  2943. begin
  2944. intreg:=getintregister(list,OS_32);
  2945. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2946. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2947. end;
  2948. OS_F64:
  2949. begin
  2950. reg64.reglo:=getintregister(list,OS_32);
  2951. reg64.reghi:=getintregister(list,OS_32);
  2952. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2953. cg64.a_load64_reg_ref(list,reg64,ref);
  2954. end;
  2955. else
  2956. internalerror(2009112417);
  2957. end;
  2958. end
  2959. else
  2960. handle_load_store(list,A_VSTR,PF_None,tmpmmreg,ref);
  2961. { VSTR cannot generate an FPU exception, VCVT is handled seperately, so we do not need a check here }
  2962. end;
  2963. procedure tbasecgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2964. begin
  2965. { this code can only be used to transfer raw data, not to perform
  2966. conversions }
  2967. if (tosize<>OS_F32) then
  2968. internalerror(2009112419);
  2969. if not(fromsize in [OS_32,OS_S32]) then
  2970. internalerror(2009112420);
  2971. if assigned(shuffle) and
  2972. not shufflescalar(shuffle) then
  2973. internalerror(2009112516);
  2974. list.concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg));
  2975. { VMOV cannot generate an FPU exception, so we do not need a check here }
  2976. end;
  2977. procedure tbasecgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2978. begin
  2979. { this code can only be used to transfer raw data, not to perform
  2980. conversions }
  2981. if (fromsize<>OS_F32) then
  2982. internalerror(2009112430);
  2983. if not(tosize in [OS_32,OS_S32]) then
  2984. internalerror(2009112409);
  2985. if assigned(shuffle) and
  2986. not shufflescalar(shuffle) then
  2987. internalerror(2009112514);
  2988. list.concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg));
  2989. { VMOV cannot generate an FPU exception, so we do not need a check here }
  2990. end;
  2991. procedure tbasecgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2992. var
  2993. tmpreg: tregister;
  2994. begin
  2995. { the vfp doesn't support xor nor any other logical operation, but
  2996. this routine is used to initialise global mm regvars. We can
  2997. easily initialise an mm reg with 0 though. }
  2998. case op of
  2999. OP_XOR:
  3000. begin
  3001. if (FPUARM_HAS_NEON in fpu_capabilities[current_settings.fputype]) and (size in [OS_F64]) then
  3002. begin
  3003. if (reg_cgsize(src)<>size) or
  3004. assigned(shuffle) then
  3005. internalerror(2019081301);
  3006. list.concat(taicpu.op_reg_reg_reg(A_VEOR,dst,dst,src));
  3007. end
  3008. else
  3009. begin
  3010. if (src<>dst) or
  3011. (reg_cgsize(src)<>size) or
  3012. assigned(shuffle) then
  3013. internalerror(2009112907);
  3014. tmpreg:=getintregister(list,OS_32);
  3015. a_load_const_reg(list,OS_32,0,tmpreg);
  3016. case size of
  3017. OS_F32:
  3018. list.concat(taicpu.op_reg_reg(A_VMOV,dst,tmpreg));
  3019. OS_F64:
  3020. list.concat(taicpu.op_reg_reg_reg(A_VMOV,dst,tmpreg,tmpreg));
  3021. else
  3022. internalerror(2009112908);
  3023. end;
  3024. end;
  3025. end
  3026. else
  3027. internalerror(2009112906);
  3028. end;
  3029. end;
  3030. procedure tbasecgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  3031. const
  3032. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  3033. begin
  3034. if (op in overflowops) and
  3035. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  3036. a_load_reg_reg(list,OS_32,size,dst,dst);
  3037. end;
  3038. procedure tbasecgarm.safe_mla(list : TAsmList; op1,op2,op3,op4 : TRegister);
  3039. procedure checkreg(var reg : TRegister);
  3040. var
  3041. tmpreg : TRegister;
  3042. begin
  3043. if ((GenerateThumbCode or GenerateThumb2Code) and (getsupreg(reg)=RS_R13)) or
  3044. (getsupreg(reg)=RS_R15) then
  3045. begin
  3046. tmpreg:=getintregister(list,OS_INT);
  3047. a_load_reg_reg(list,OS_INT,OS_INT,reg,tmpreg);
  3048. reg:=tmpreg;
  3049. end;
  3050. end;
  3051. begin
  3052. checkreg(op1);
  3053. checkreg(op2);
  3054. checkreg(op3);
  3055. checkreg(op4);
  3056. list.concat(taicpu.op_reg_reg_reg_reg(A_MLA,op1,op2,op3,op4));
  3057. end;
  3058. procedure tbasecgarm.g_maybe_tls_init(list : TAsmList);
  3059. begin
  3060. if pi_needs_tls in current_procinfo.flags then
  3061. begin
  3062. list.concat(tai_regalloc.alloc(NR_R0,nil));
  3063. a_call_name(list,'fpc_read_tp',false);
  3064. a_load_reg_reg(list,OS_ADDR,OS_ADDR,NR_R0,current_procinfo.tlsoffset);
  3065. list.concat(tai_regalloc.dealloc(NR_R0,nil));
  3066. end;
  3067. end;
  3068. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3069. begin
  3070. case op of
  3071. OP_NEG:
  3072. begin
  3073. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3074. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3075. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  3076. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3077. end;
  3078. OP_NOT:
  3079. begin
  3080. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  3081. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  3082. end;
  3083. else
  3084. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  3085. end;
  3086. end;
  3087. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  3088. begin
  3089. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  3090. end;
  3091. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  3092. var
  3093. ovloc : tlocation;
  3094. begin
  3095. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  3096. end;
  3097. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3098. var
  3099. ovloc : tlocation;
  3100. begin
  3101. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  3102. end;
  3103. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  3104. begin
  3105. { this code can only be used to transfer raw data, not to perform
  3106. conversions }
  3107. if (mmsize<>OS_F64) then
  3108. internalerror(2009112405);
  3109. list.concat(taicpu.op_reg_reg_reg(A_VMOV,mmreg,intreg.reglo,intreg.reghi));
  3110. { VMOV cannot generate an FPU exception, so we do not need a check here }
  3111. end;
  3112. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  3113. begin
  3114. { this code can only be used to transfer raw data, not to perform
  3115. conversions }
  3116. if (mmsize<>OS_F64) then
  3117. internalerror(2009112406);
  3118. list.concat(taicpu.op_reg_reg_reg(A_VMOV,intreg.reglo,intreg.reghi,mmreg));
  3119. { VMOV cannot generate an FPU exception, so we do not need a check here }
  3120. end;
  3121. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3122. var
  3123. tmpreg : tregister;
  3124. b : byte;
  3125. begin
  3126. ovloc.loc:=LOC_VOID;
  3127. case op of
  3128. OP_NEG,
  3129. OP_NOT :
  3130. internalerror(2012022501);
  3131. else
  3132. ;
  3133. end;
  3134. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3135. begin
  3136. case op of
  3137. OP_ADD:
  3138. begin
  3139. if is_shifter_const(lo(value),b) then
  3140. begin
  3141. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3142. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3143. end
  3144. else
  3145. begin
  3146. tmpreg:=cg.getintregister(list,OS_32);
  3147. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3148. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3149. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3150. end;
  3151. if is_shifter_const(hi(value),b) then
  3152. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  3153. else
  3154. begin
  3155. tmpreg:=cg.getintregister(list,OS_32);
  3156. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3157. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3158. end;
  3159. end;
  3160. OP_SUB:
  3161. begin
  3162. if is_shifter_const(lo(value),b) then
  3163. begin
  3164. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3165. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3166. end
  3167. else
  3168. begin
  3169. tmpreg:=cg.getintregister(list,OS_32);
  3170. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3171. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3172. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3173. end;
  3174. if is_shifter_const(hi(value),b) then
  3175. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  3176. else
  3177. begin
  3178. tmpreg:=cg.getintregister(list,OS_32);
  3179. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3180. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3181. end;
  3182. end;
  3183. else
  3184. internalerror(200502131);
  3185. end;
  3186. if size=OS_64 then
  3187. begin
  3188. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3189. ovloc.loc:=LOC_FLAGS;
  3190. case op of
  3191. OP_ADD:
  3192. ovloc.resflags:=F_CS;
  3193. OP_SUB:
  3194. ovloc.resflags:=F_CC;
  3195. else
  3196. internalerror(2019050918);
  3197. end;
  3198. end;
  3199. end
  3200. else
  3201. begin
  3202. case op of
  3203. OP_AND,OP_OR,OP_XOR:
  3204. begin
  3205. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  3206. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  3207. end;
  3208. OP_ADD:
  3209. begin
  3210. if is_shifter_const(aint(lo(value)),b) then
  3211. begin
  3212. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3213. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3214. end
  3215. else
  3216. begin
  3217. tmpreg:=cg.getintregister(list,OS_32);
  3218. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3219. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3220. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3221. end;
  3222. if is_shifter_const(aint(hi(value)),b) then
  3223. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3224. else
  3225. begin
  3226. tmpreg:=cg.getintregister(list,OS_32);
  3227. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  3228. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  3229. end;
  3230. end;
  3231. OP_SUB:
  3232. begin
  3233. if is_shifter_const(aint(lo(value)),b) then
  3234. begin
  3235. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3236. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3237. end
  3238. else
  3239. begin
  3240. tmpreg:=cg.getintregister(list,OS_32);
  3241. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3242. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3243. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3244. end;
  3245. if is_shifter_const(aint(hi(value)),b) then
  3246. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3247. else
  3248. begin
  3249. tmpreg:=cg.getintregister(list,OS_32);
  3250. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3251. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  3252. end;
  3253. end;
  3254. else
  3255. internalerror(2003083101);
  3256. end;
  3257. end;
  3258. end;
  3259. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3260. begin
  3261. ovloc.loc:=LOC_VOID;
  3262. case op of
  3263. OP_NEG,
  3264. OP_NOT :
  3265. internalerror(2012022502);
  3266. else
  3267. ;
  3268. end;
  3269. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3270. begin
  3271. case op of
  3272. OP_ADD:
  3273. begin
  3274. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3275. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3276. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  3277. end;
  3278. OP_SUB:
  3279. begin
  3280. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3281. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3282. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  3283. end;
  3284. else
  3285. internalerror(2003083102);
  3286. end;
  3287. if size=OS_64 then
  3288. begin
  3289. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3290. ovloc.loc:=LOC_FLAGS;
  3291. case op of
  3292. OP_ADD:
  3293. ovloc.resflags:=F_CS;
  3294. OP_SUB:
  3295. ovloc.resflags:=F_CC;
  3296. else
  3297. internalerror(2019050917);
  3298. end;
  3299. end;
  3300. end
  3301. else
  3302. begin
  3303. case op of
  3304. OP_AND,OP_OR,OP_XOR:
  3305. begin
  3306. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  3307. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  3308. end;
  3309. OP_ADD:
  3310. begin
  3311. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3312. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3313. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  3314. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3315. end;
  3316. OP_SUB:
  3317. begin
  3318. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3319. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3320. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  3321. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3322. end;
  3323. else
  3324. internalerror(2003083104);
  3325. end;
  3326. end;
  3327. end;
  3328. procedure tthumbcgarm.init_register_allocators;
  3329. begin
  3330. inherited init_register_allocators;
  3331. if assigned(current_procinfo) and (current_procinfo.framepointer=NR_R7) then
  3332. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3333. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6],first_int_imreg,[])
  3334. else
  3335. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3336. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7],first_int_imreg,[]);
  3337. end;
  3338. procedure tthumbcgarm.done_register_allocators;
  3339. begin
  3340. rg[R_INTREGISTER].free;
  3341. rg[R_FPUREGISTER].free;
  3342. rg[R_MMREGISTER].free;
  3343. inherited done_register_allocators;
  3344. end;
  3345. procedure tthumbcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3346. var
  3347. ref : treference;
  3348. r : byte;
  3349. regs : tcpuregisterset;
  3350. stackmisalignment : pint;
  3351. registerarea: DWord;
  3352. stack_parameters: Boolean;
  3353. begin
  3354. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3355. LocalSize:=align(LocalSize,4);
  3356. { call instruction does not put anything on the stack }
  3357. stackmisalignment:=0;
  3358. if not(nostackframe) then
  3359. begin
  3360. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3361. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3362. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3363. { save int registers }
  3364. reference_reset(ref,4,[]);
  3365. ref.index:=NR_STACK_POINTER_REG;
  3366. ref.addressmode:=AM_PREINDEXED;
  3367. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3368. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3369. begin
  3370. //!!!! a_reg_alloc(list,NR_R12);
  3371. //!!!! list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3372. end;
  3373. { the (old) ARM APCS requires saving both the stack pointer (to
  3374. crawl the stack) and the PC (to identify the function this
  3375. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  3376. and R15 -- still needs updating for EABI and Darwin, they don't
  3377. need that }
  3378. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3379. regs:=regs+[RS_R7,RS_R14]
  3380. else
  3381. // if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3382. include(regs,RS_R14);
  3383. { safely estimate stack size }
  3384. if localsize+current_settings.alignment.localalignmax+4>508 then
  3385. begin
  3386. include(rg[R_INTREGISTER].used_in_proc,RS_R4);
  3387. include(regs,RS_R4);
  3388. end;
  3389. registerarea:=0;
  3390. if regs<>[] then
  3391. begin
  3392. for r:=RS_R0 to RS_R15 do
  3393. if r in regs then
  3394. inc(registerarea,4);
  3395. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,regs));
  3396. end;
  3397. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3398. if stack_parameters or (LocalSize<>0) or
  3399. ((stackmisalignment<>0) and
  3400. ((pi_do_call in current_procinfo.flags) or
  3401. (po_assembler in current_procinfo.procdef.procoptions))) then
  3402. begin
  3403. { do we access stack parameters?
  3404. if yes, the previously estimated stacksize must be used }
  3405. if stack_parameters then
  3406. begin
  3407. if localsize>tcpuprocinfo(current_procinfo).stackframesize then
  3408. begin
  3409. writeln(localsize);
  3410. writeln(tcpuprocinfo(current_procinfo).stackframesize);
  3411. internalerror(2013040601);
  3412. end
  3413. else
  3414. localsize:=tcpuprocinfo(current_procinfo).stackframesize-registerarea;
  3415. end
  3416. else
  3417. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3418. if localsize<508 then
  3419. begin
  3420. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3421. end
  3422. else if localsize<=1016 then
  3423. begin
  3424. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3425. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize-508));
  3426. end
  3427. else
  3428. begin
  3429. a_load_const_reg(list,OS_ADDR,-localsize,NR_R4);
  3430. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R4));
  3431. include(regs,RS_R4);
  3432. //!!!! if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3433. //!!!! a_reg_alloc(list,NR_R12);
  3434. //!!!! a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3435. //!!!! list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3436. //!!!! a_reg_dealloc(list,NR_R12);
  3437. end;
  3438. end;
  3439. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3440. begin
  3441. list.concat(taicpu.op_reg_reg_const(A_ADD,current_procinfo.framepointer,NR_STACK_POINTER_REG,0));
  3442. end;
  3443. end;
  3444. end;
  3445. procedure tthumbcgarm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  3446. var
  3447. LocalSize : longint;
  3448. r: byte;
  3449. regs : tcpuregisterset;
  3450. registerarea : DWord;
  3451. stackmisalignment: pint;
  3452. stack_parameters : Boolean;
  3453. begin
  3454. if not(nostackframe) then
  3455. begin
  3456. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3457. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3458. include(regs,RS_R15);
  3459. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3460. include(regs,getsupreg(current_procinfo.framepointer));
  3461. registerarea:=0;
  3462. for r:=RS_R0 to RS_R15 do
  3463. if r in regs then
  3464. inc(registerarea,4);
  3465. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3466. LocalSize:=current_procinfo.calc_stackframe_size;
  3467. if stack_parameters then
  3468. localsize:=tcpuprocinfo(current_procinfo).stackframesize-registerarea
  3469. else
  3470. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3471. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  3472. (target_info.system in systems_darwin) then
  3473. begin
  3474. if (LocalSize<>0) or
  3475. ((stackmisalignment<>0) and
  3476. ((pi_do_call in current_procinfo.flags) or
  3477. (po_assembler in current_procinfo.procdef.procoptions))) then
  3478. begin
  3479. if LocalSize=0 then
  3480. else if LocalSize<=508 then
  3481. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  3482. else if LocalSize<=1016 then
  3483. begin
  3484. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3485. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize-508));
  3486. end
  3487. else
  3488. begin
  3489. a_reg_alloc(list,NR_R3);
  3490. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R3);
  3491. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R3));
  3492. a_reg_dealloc(list,NR_R3);
  3493. end;
  3494. end;
  3495. if regs=[] then
  3496. begin
  3497. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3498. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3499. else
  3500. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3501. end
  3502. else
  3503. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,regs));
  3504. end;
  3505. end
  3506. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3507. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3508. else
  3509. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3510. end;
  3511. procedure tthumbcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3512. var
  3513. oppostfix:toppostfix;
  3514. usedtmpref: treference;
  3515. tmpreg,tmpreg2 : tregister;
  3516. dir : integer;
  3517. begin
  3518. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  3519. FromSize := ToSize;
  3520. case FromSize of
  3521. { signed integer registers }
  3522. OS_8:
  3523. oppostfix:=PF_B;
  3524. OS_S8:
  3525. oppostfix:=PF_SB;
  3526. OS_16:
  3527. oppostfix:=PF_H;
  3528. OS_S16:
  3529. oppostfix:=PF_SH;
  3530. OS_32,
  3531. OS_S32:
  3532. oppostfix:=PF_None;
  3533. else
  3534. InternalError(200308298);
  3535. end;
  3536. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  3537. begin
  3538. if target_info.endian=endian_big then
  3539. dir:=-1
  3540. else
  3541. dir:=1;
  3542. case FromSize of
  3543. OS_16,OS_S16:
  3544. begin
  3545. { only complicated references need an extra loadaddr }
  3546. if assigned(ref.symbol) or
  3547. (ref.index<>NR_NO) or
  3548. (ref.offset<-124) or
  3549. (ref.offset>124) or
  3550. { sometimes the compiler reused registers }
  3551. (reg=ref.index) or
  3552. (reg=ref.base) then
  3553. begin
  3554. tmpreg2:=getintregister(list,OS_INT);
  3555. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3556. reference_reset_base(usedtmpref,tmpreg2,0,ref.temppos,ref.alignment,ref.volatility);
  3557. end
  3558. else
  3559. usedtmpref:=ref;
  3560. if target_info.endian=endian_big then
  3561. inc(usedtmpref.offset,1);
  3562. tmpreg:=getintregister(list,OS_INT);
  3563. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3564. inc(usedtmpref.offset,dir);
  3565. if FromSize=OS_16 then
  3566. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  3567. else
  3568. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  3569. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3570. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3571. end;
  3572. OS_32,OS_S32:
  3573. begin
  3574. tmpreg:=getintregister(list,OS_INT);
  3575. { only complicated references need an extra loadaddr }
  3576. if assigned(ref.symbol) or
  3577. (ref.index<>NR_NO) or
  3578. (ref.offset<-124) or
  3579. (ref.offset>124) or
  3580. { sometimes the compiler reused registers }
  3581. (reg=ref.index) or
  3582. (reg=ref.base) then
  3583. begin
  3584. tmpreg2:=getintregister(list,OS_INT);
  3585. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3586. reference_reset_base(usedtmpref,tmpreg2,0,ref.temppos,ref.alignment,ref.volatility);
  3587. end
  3588. else
  3589. usedtmpref:=ref;
  3590. if ref.alignment=2 then
  3591. begin
  3592. if target_info.endian=endian_big then
  3593. inc(usedtmpref.offset,2);
  3594. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3595. inc(usedtmpref.offset,dir*2);
  3596. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3597. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3598. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3599. end
  3600. else
  3601. begin
  3602. if target_info.endian=endian_big then
  3603. inc(usedtmpref.offset,3);
  3604. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3605. inc(usedtmpref.offset,dir);
  3606. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3607. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3608. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3609. inc(usedtmpref.offset,dir);
  3610. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3611. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3612. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3613. inc(usedtmpref.offset,dir);
  3614. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3615. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,24));
  3616. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3617. end;
  3618. end
  3619. else
  3620. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3621. end;
  3622. end
  3623. else
  3624. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3625. if (fromsize=OS_S8) and (tosize = OS_16) then
  3626. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3627. end;
  3628. procedure tthumbcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3629. var
  3630. l : tasmlabel;
  3631. hr : treference;
  3632. begin
  3633. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3634. internalerror(2002090908);
  3635. if is_thumb_imm(a) then
  3636. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3637. else
  3638. begin
  3639. reference_reset(hr,4,[]);
  3640. current_asmdata.getjumplabel(l);
  3641. cg.a_label(current_procinfo.aktlocaldata,l);
  3642. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3643. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3644. hr.symbol:=l;
  3645. hr.base:=NR_PC;
  3646. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3647. end;
  3648. end;
  3649. procedure tthumbcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3650. var
  3651. hsym : tsym;
  3652. href,
  3653. tmpref : treference;
  3654. paraloc : Pcgparalocation;
  3655. l : TAsmLabel;
  3656. begin
  3657. { calculate the parameter info for the procdef }
  3658. procdef.init_paraloc_info(callerside);
  3659. hsym:=tsym(procdef.parast.Find('self'));
  3660. if not(assigned(hsym) and
  3661. (hsym.typ=paravarsym)) then
  3662. internalerror(2003052504);
  3663. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3664. while paraloc<>nil do
  3665. with paraloc^ do
  3666. begin
  3667. case loc of
  3668. LOC_REGISTER:
  3669. begin
  3670. if is_thumb_imm(ioffset) then
  3671. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  3672. else
  3673. begin
  3674. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3675. reference_reset(tmpref,4,[]);
  3676. current_asmdata.getjumplabel(l);
  3677. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3678. cg.a_label(current_procinfo.aktlocaldata,l);
  3679. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3680. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3681. tmpref.symbol:=l;
  3682. tmpref.base:=NR_PC;
  3683. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3684. a_op_reg_reg(list,OP_SUB,size,NR_R4,register);
  3685. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3686. end;
  3687. end;
  3688. LOC_REFERENCE:
  3689. begin
  3690. { offset in the wrapper needs to be adjusted for the stored
  3691. return address }
  3692. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),ctempposinvalid,sizeof(pint),[]);
  3693. if is_thumb_imm(ioffset) then
  3694. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  3695. else
  3696. begin
  3697. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3698. reference_reset(tmpref,4,[]);
  3699. current_asmdata.getjumplabel(l);
  3700. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3701. cg.a_label(current_procinfo.aktlocaldata,l);
  3702. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3703. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3704. tmpref.symbol:=l;
  3705. tmpref.base:=NR_PC;
  3706. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3707. a_op_reg_ref(list,OP_SUB,size,NR_R4,href);
  3708. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3709. end;
  3710. end
  3711. else
  3712. internalerror(2003091804);
  3713. end;
  3714. paraloc:=next;
  3715. end;
  3716. end;
  3717. function tthumbcgarm.handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference;
  3718. var
  3719. href : treference;
  3720. tmpreg : TRegister;
  3721. begin
  3722. href:=ref;
  3723. if { LDR/STR limitations }
  3724. (
  3725. (((op=A_LDR) and (oppostfix=PF_None)) or
  3726. ((op=A_STR) and (oppostfix=PF_None))) and
  3727. (ref.base<>NR_STACK_POINTER_REG) and
  3728. (abs(ref.offset)>124)
  3729. ) or
  3730. { LDRB/STRB limitations }
  3731. (
  3732. (((op=A_LDR) and (oppostfix=PF_B)) or
  3733. ((op=A_LDRB) and (oppostfix=PF_None)) or
  3734. ((op=A_STR) and (oppostfix=PF_B)) or
  3735. ((op=A_STRB) and (oppostfix=PF_None))) and
  3736. ((ref.base=NR_STACK_POINTER_REG) or
  3737. (ref.index=NR_STACK_POINTER_REG) or
  3738. (abs(ref.offset)>31)
  3739. )
  3740. ) or
  3741. { LDRH/STRH limitations }
  3742. (
  3743. (((op=A_LDR) and (oppostfix=PF_H)) or
  3744. ((op=A_LDRH) and (oppostfix=PF_None)) or
  3745. ((op=A_STR) and (oppostfix=PF_H)) or
  3746. ((op=A_STRH) and (oppostfix=PF_None))) and
  3747. ((ref.base=NR_STACK_POINTER_REG) or
  3748. (ref.index=NR_STACK_POINTER_REG) or
  3749. (abs(ref.offset)>62) or
  3750. ((abs(ref.offset) mod 2)<>0)
  3751. )
  3752. ) then
  3753. begin
  3754. tmpreg:=getintregister(list,OS_ADDR);
  3755. a_loadaddr_ref_reg(list,ref,tmpreg);
  3756. reference_reset_base(href,tmpreg,0,ref.temppos,ref.alignment,ref.volatility);
  3757. end
  3758. else if (op=A_LDR) and
  3759. (oppostfix in [PF_None]) and
  3760. (ref.base=NR_STACK_POINTER_REG) and
  3761. (abs(ref.offset)>1020) then
  3762. begin
  3763. tmpreg:=getintregister(list,OS_ADDR);
  3764. a_loadaddr_ref_reg(list,ref,tmpreg);
  3765. reference_reset_base(href,tmpreg,0,ref.temppos,ref.alignment,ref.volatility);
  3766. end
  3767. else if (op=A_LDR) and
  3768. ((oppostfix in [PF_SH,PF_SB]) or
  3769. (abs(ref.offset)>124)) then
  3770. begin
  3771. tmpreg:=getintregister(list,OS_ADDR);
  3772. a_loadaddr_ref_reg(list,ref,tmpreg);
  3773. reference_reset_base(href,tmpreg,0,ref.temppos,ref.alignment,ref.volatility);
  3774. end;
  3775. Result:=inherited handle_load_store(list, op, oppostfix, reg, href);
  3776. end;
  3777. procedure tthumbcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  3778. var
  3779. tmpreg : tregister;
  3780. begin
  3781. case op of
  3782. OP_NEG:
  3783. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  3784. OP_NOT:
  3785. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  3786. OP_DIV,OP_IDIV:
  3787. internalerror(200308284);
  3788. OP_ROL:
  3789. begin
  3790. if not(size in [OS_32,OS_S32]) then
  3791. internalerror(2008072805);
  3792. { simulate ROL by ror'ing 32-value }
  3793. tmpreg:=getintregister(list,OS_32);
  3794. a_load_const_reg(list,OS_32,32,tmpreg);
  3795. list.concat(taicpu.op_reg_reg(A_SUB,tmpreg,src));
  3796. list.concat(taicpu.op_reg_reg(A_ROR,dst,src));
  3797. end;
  3798. else
  3799. begin
  3800. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3801. list.concat(setoppostfix(
  3802. taicpu.op_reg_reg(op_reg_opcg2asmop[op],dst,src),op_reg_postfix[op]));
  3803. end;
  3804. end;
  3805. maybeadjustresult(list,op,size,dst);
  3806. end;
  3807. procedure tthumbcgarm.a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);
  3808. var
  3809. tmpreg : tregister;
  3810. {$ifdef DUMMY}
  3811. l1 : longint;
  3812. {$endif DUMMY}
  3813. begin
  3814. //!!! ovloc.loc:=LOC_VOID;
  3815. if {$ifopt R+}(a<>-2147483648) and{$endif} {!!!!!! not setflags and } is_thumb_imm(-a) then
  3816. case op of
  3817. OP_ADD:
  3818. begin
  3819. op:=OP_SUB;
  3820. a:=aint(dword(-a));
  3821. end;
  3822. OP_SUB:
  3823. begin
  3824. op:=OP_ADD;
  3825. a:=aint(dword(-a));
  3826. end
  3827. else
  3828. ;
  3829. end;
  3830. if is_thumb_imm(a) and (op in [OP_ADD,OP_SUB]) then
  3831. begin
  3832. // if cgsetflags or setflags then
  3833. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3834. list.concat(setoppostfix(
  3835. taicpu.op_reg_const(op_reg_opcg2asmop[op],dst,a),op_reg_postfix[op]));
  3836. if (cgsetflags {!!! or setflags }) and (size in [OS_8,OS_16,OS_32]) then
  3837. begin
  3838. //!!! ovloc.loc:=LOC_FLAGS;
  3839. case op of
  3840. OP_ADD:
  3841. //!!! ovloc.resflags:=F_CS;
  3842. ;
  3843. OP_SUB:
  3844. //!!! ovloc.resflags:=F_CC;
  3845. ;
  3846. else
  3847. ;
  3848. end;
  3849. end;
  3850. end
  3851. else
  3852. begin
  3853. { there could be added some more sophisticated optimizations }
  3854. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  3855. a_load_reg_reg(list,size,size,dst,dst)
  3856. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3857. a_load_const_reg(list,size,0,dst)
  3858. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  3859. a_op_reg_reg(list,OP_NEG,size,dst,dst)
  3860. { we do this here instead in the peephole optimizer because
  3861. it saves us a register }
  3862. {$ifdef DUMMY}
  3863. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3864. a_op_const_reg_reg(list,OP_SHL,size,l1,dst,dst)
  3865. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3866. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3867. begin
  3868. if l1>32 then{roozbeh does this ever happen?}
  3869. internalerror(2003082903);
  3870. shifterop_reset(so);
  3871. so.shiftmode:=SM_LSL;
  3872. so.shiftimm:=l1;
  3873. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,dst,so));
  3874. end
  3875. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  3876. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  3877. begin
  3878. if l1>32 then{does this ever happen?}
  3879. internalerror(2012051802);
  3880. shifterop_reset(so);
  3881. so.shiftmode:=SM_LSL;
  3882. so.shiftimm:=l1;
  3883. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,dst,dst,so));
  3884. end
  3885. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,dst,dst) then
  3886. begin
  3887. { nothing to do on success }
  3888. end
  3889. {$endif DUMMY}
  3890. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  3891. Just using mov x, #0 might allow some easier optimizations down the line. }
  3892. else if (op = OP_AND) and (dword(a)=0) then
  3893. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  3894. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  3895. else if (op = OP_AND) and (not(dword(a))=0) then
  3896. // do nothing
  3897. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  3898. broader range of shifterconstants.}
  3899. {$ifdef DUMMY}
  3900. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  3901. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,not(dword(a))))
  3902. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  3903. begin
  3904. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm1));
  3905. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  3906. end
  3907. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  3908. not(cgsetflags or setflags) and
  3909. split_into_shifter_const(a, imm1, imm2) then
  3910. begin
  3911. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm1));
  3912. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  3913. end
  3914. {$endif DUMMY}
  3915. else if (op in [OP_SHL, OP_SHR, OP_SAR]) then
  3916. begin
  3917. list.concat(taicpu.op_reg_reg_const(op_reg_opcg2asmop[op],dst,dst,a));
  3918. end
  3919. else
  3920. begin
  3921. tmpreg:=getintregister(list,size);
  3922. a_load_const_reg(list,size,a,tmpreg);
  3923. a_op_reg_reg(list,op,size,tmpreg,dst);
  3924. end;
  3925. end;
  3926. maybeadjustresult(list,op,size,dst);
  3927. end;
  3928. procedure tthumbcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  3929. begin
  3930. if (op=OP_ADD) and (src=NR_R13) and (dst<>NR_R13) and ((a mod 4)=0) and (a>0) and (a<=1020) then
  3931. list.concat(taicpu.op_reg_reg_const(A_ADD,dst,src,a))
  3932. else
  3933. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  3934. end;
  3935. procedure tthumbcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3936. var
  3937. l1,l2 : tasmlabel;
  3938. ai : taicpu;
  3939. begin
  3940. current_asmdata.getjumplabel(l1);
  3941. current_asmdata.getjumplabel(l2);
  3942. ai:=setcondition(taicpu.op_sym(A_B,l1),flags_to_cond(f));
  3943. ai.is_jmp:=true;
  3944. list.concat(ai);
  3945. list.concat(taicpu.op_reg_const(A_MOV,reg,0));
  3946. list.concat(taicpu.op_sym(A_B,l2));
  3947. cg.a_label(list,l1);
  3948. list.concat(taicpu.op_reg_const(A_MOV,reg,1));
  3949. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3950. cg.a_label(list,l2);
  3951. end;
  3952. procedure tthumb2cgarm.init_register_allocators;
  3953. begin
  3954. inherited init_register_allocators;
  3955. { currently, we save R14 always, so we can use it }
  3956. if (target_info.system<>system_arm_ios) then
  3957. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3958. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3959. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  3960. else
  3961. { r9 is not available on Darwin according to the llvm code generator }
  3962. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3963. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3964. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  3965. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  3966. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  3967. if (FPUARM_HAS_32REGS in fpu_capabilities[current_settings.fputype]) and
  3968. (FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[current_settings.fputype]) then
  3969. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3970. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3971. RS_D16,RS_D17,RS_D18,RS_D19,RS_D20,RS_D21,RS_D22,RS_D23,RS_D24,RS_D25,RS_D26,RS_D27,RS_D28,RS_D29,RS_D30,RS_D31,
  3972. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3973. ],first_mm_imreg,[])
  3974. else if (FPUARM_HAS_32REGS in fpu_capabilities[current_settings.fputype]) then
  3975. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFS,
  3976. [RS_S0,RS_S1,RS_S2,RS_S3,RS_S4,RS_S5,RS_S6,RS_S7,
  3977. RS_S16,RS_S17,RS_S18,RS_S19,RS_S20,RS_S21,RS_S22,RS_S23,RS_S24,RS_S25,RS_S26,RS_S27,RS_S28,RS_S29,RS_S30,RS_S31,
  3978. RS_S8,RS_S9,RS_S10,RS_S11,RS_S12,RS_S13,RS_S14,RS_S15
  3979. ],first_mm_imreg,[])
  3980. else if FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype] then
  3981. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3982. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3983. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3984. ],first_mm_imreg,[])
  3985. else
  3986. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  3987. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  3988. end;
  3989. procedure tthumb2cgarm.done_register_allocators;
  3990. begin
  3991. rg[R_INTREGISTER].free;
  3992. rg[R_FPUREGISTER].free;
  3993. rg[R_MMREGISTER].free;
  3994. inherited done_register_allocators;
  3995. end;
  3996. procedure tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  3997. begin
  3998. list.concat(taicpu.op_reg(A_BLX, reg));
  3999. {
  4000. the compiler does not properly set this flag anymore in pass 1, and
  4001. for now we only need it after pass 2 (I hope) (JM)
  4002. if not(pi_do_call in current_procinfo.flags) then
  4003. internalerror(2003060703);
  4004. }
  4005. include(current_procinfo.flags,pi_do_call);
  4006. end;
  4007. procedure tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  4008. var
  4009. l : tasmlabel;
  4010. hr : treference;
  4011. begin
  4012. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  4013. internalerror(2002090909);
  4014. if is_thumb32_imm(a) then
  4015. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  4016. else if is_thumb32_imm(not(a)) then
  4017. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  4018. else if (a and $FFFF)=a then
  4019. list.concat(taicpu.op_reg_const(A_MOVW,reg,a))
  4020. else
  4021. begin
  4022. reference_reset(hr,4,[]);
  4023. current_asmdata.getjumplabel(l);
  4024. cg.a_label(current_procinfo.aktlocaldata,l);
  4025. hr.symboldata:=current_procinfo.aktlocaldata.last;
  4026. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  4027. hr.symbol:=l;
  4028. hr.base:=NR_PC;
  4029. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  4030. end;
  4031. end;
  4032. procedure tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  4033. var
  4034. oppostfix:toppostfix;
  4035. usedtmpref: treference;
  4036. tmpreg,tmpreg2 : tregister;
  4037. so : tshifterop;
  4038. dir : integer;
  4039. begin
  4040. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  4041. FromSize := ToSize;
  4042. case FromSize of
  4043. { signed integer registers }
  4044. OS_8:
  4045. oppostfix:=PF_B;
  4046. OS_S8:
  4047. oppostfix:=PF_SB;
  4048. OS_16:
  4049. oppostfix:=PF_H;
  4050. OS_S16:
  4051. oppostfix:=PF_SH;
  4052. OS_32,
  4053. OS_S32:
  4054. oppostfix:=PF_None;
  4055. else
  4056. InternalError(2003082913);
  4057. end;
  4058. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  4059. begin
  4060. if target_info.endian=endian_big then
  4061. dir:=-1
  4062. else
  4063. dir:=1;
  4064. case FromSize of
  4065. OS_16,OS_S16:
  4066. begin
  4067. { only complicated references need an extra loadaddr }
  4068. if assigned(ref.symbol) or
  4069. (ref.index<>NR_NO) or
  4070. (ref.offset<-255) or
  4071. (ref.offset>4094) or
  4072. { sometimes the compiler reused registers }
  4073. (reg=ref.index) or
  4074. (reg=ref.base) then
  4075. begin
  4076. tmpreg2:=getintregister(list,OS_INT);
  4077. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4078. reference_reset_base(usedtmpref,tmpreg2,0,ref.temppos,ref.alignment,ref.volatility);
  4079. end
  4080. else
  4081. usedtmpref:=ref;
  4082. if target_info.endian=endian_big then
  4083. inc(usedtmpref.offset,1);
  4084. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  4085. tmpreg:=getintregister(list,OS_INT);
  4086. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4087. inc(usedtmpref.offset,dir);
  4088. if FromSize=OS_16 then
  4089. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  4090. else
  4091. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  4092. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4093. end;
  4094. OS_32,OS_S32:
  4095. begin
  4096. tmpreg:=getintregister(list,OS_INT);
  4097. { only complicated references need an extra loadaddr }
  4098. if assigned(ref.symbol) or
  4099. (ref.index<>NR_NO) or
  4100. (ref.offset<-255) or
  4101. (ref.offset>4092) or
  4102. { sometimes the compiler reused registers }
  4103. (reg=ref.index) or
  4104. (reg=ref.base) then
  4105. begin
  4106. tmpreg2:=getintregister(list,OS_INT);
  4107. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4108. reference_reset_base(usedtmpref,tmpreg2,0,ref.temppos,ref.alignment,ref.volatility);
  4109. end
  4110. else
  4111. usedtmpref:=ref;
  4112. shifterop_reset(so);so.shiftmode:=SM_LSL;
  4113. if ref.alignment=2 then
  4114. begin
  4115. if target_info.endian=endian_big then
  4116. inc(usedtmpref.offset,2);
  4117. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  4118. inc(usedtmpref.offset,dir*2);
  4119. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  4120. so.shiftimm:=16;
  4121. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4122. end
  4123. else
  4124. begin
  4125. if target_info.endian=endian_big then
  4126. inc(usedtmpref.offset,3);
  4127. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4128. inc(usedtmpref.offset,dir);
  4129. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4130. so.shiftimm:=8;
  4131. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4132. inc(usedtmpref.offset,dir);
  4133. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4134. so.shiftimm:=16;
  4135. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4136. inc(usedtmpref.offset,dir);
  4137. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4138. so.shiftimm:=24;
  4139. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4140. end;
  4141. end
  4142. else
  4143. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4144. end;
  4145. end
  4146. else
  4147. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4148. if (fromsize=OS_S8) and (tosize = OS_16) then
  4149. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  4150. end;
  4151. procedure tthumb2cgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  4152. begin
  4153. if op = OP_NOT then
  4154. begin
  4155. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  4156. case size of
  4157. OS_8: list.concat(taicpu.op_reg_reg(A_UXTB,dst,dst));
  4158. OS_S8: list.concat(taicpu.op_reg_reg(A_SXTB,dst,dst));
  4159. OS_16: list.concat(taicpu.op_reg_reg(A_UXTH,dst,dst));
  4160. OS_S16: list.concat(taicpu.op_reg_reg(A_SXTH,dst,dst));
  4161. OS_32,
  4162. OS_S32:
  4163. ;
  4164. else
  4165. internalerror(2019050916);
  4166. end;
  4167. end
  4168. else
  4169. inherited a_op_reg_reg(list, op, size, src, dst);
  4170. end;
  4171. procedure tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4172. var
  4173. shift, width : byte;
  4174. tmpreg : tregister;
  4175. so : tshifterop;
  4176. l1 : longint;
  4177. begin
  4178. ovloc.loc:=LOC_VOID;
  4179. if (a<>-2147483648) and is_shifter_const(-a,shift) then
  4180. case op of
  4181. OP_ADD:
  4182. begin
  4183. op:=OP_SUB;
  4184. a:=aint(dword(-a));
  4185. end;
  4186. OP_SUB:
  4187. begin
  4188. op:=OP_ADD;
  4189. a:=aint(dword(-a));
  4190. end
  4191. else
  4192. ;
  4193. end;
  4194. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  4195. case op of
  4196. OP_NEG,OP_NOT,
  4197. OP_DIV,OP_IDIV:
  4198. internalerror(200308285);
  4199. OP_SHL:
  4200. begin
  4201. if a>32 then
  4202. internalerror(2014020703);
  4203. if a<>0 then
  4204. begin
  4205. shifterop_reset(so);
  4206. so.shiftmode:=SM_LSL;
  4207. so.shiftimm:=a;
  4208. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4209. end
  4210. else
  4211. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4212. end;
  4213. OP_ROL:
  4214. begin
  4215. if a>32 then
  4216. internalerror(2014020704);
  4217. if a<>0 then
  4218. begin
  4219. shifterop_reset(so);
  4220. so.shiftmode:=SM_ROR;
  4221. so.shiftimm:=32-a;
  4222. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4223. end
  4224. else
  4225. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4226. end;
  4227. OP_ROR:
  4228. begin
  4229. if a>32 then
  4230. internalerror(2014020705);
  4231. if a<>0 then
  4232. begin
  4233. shifterop_reset(so);
  4234. so.shiftmode:=SM_ROR;
  4235. so.shiftimm:=a;
  4236. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4237. end
  4238. else
  4239. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4240. end;
  4241. OP_SHR:
  4242. begin
  4243. if a>32 then
  4244. internalerror(200308292);
  4245. shifterop_reset(so);
  4246. if a<>0 then
  4247. begin
  4248. so.shiftmode:=SM_LSR;
  4249. so.shiftimm:=a;
  4250. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4251. end
  4252. else
  4253. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4254. end;
  4255. OP_SAR:
  4256. begin
  4257. if a>32 then
  4258. internalerror(200308295);
  4259. if a<>0 then
  4260. begin
  4261. shifterop_reset(so);
  4262. so.shiftmode:=SM_ASR;
  4263. so.shiftimm:=a;
  4264. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4265. end
  4266. else
  4267. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4268. end;
  4269. else
  4270. if (op in [OP_SUB, OP_ADD]) and
  4271. ((a < 0) or
  4272. (a > 4095)) then
  4273. begin
  4274. tmpreg:=getintregister(list,size);
  4275. a_load_const_reg(list, size, a, tmpreg);
  4276. if cgsetflags or setflags then
  4277. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4278. list.concat(setoppostfix(
  4279. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4280. end
  4281. else
  4282. begin
  4283. if cgsetflags or setflags then
  4284. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4285. list.concat(setoppostfix(
  4286. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4287. end;
  4288. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  4289. begin
  4290. ovloc.loc:=LOC_FLAGS;
  4291. case op of
  4292. OP_ADD:
  4293. ovloc.resflags:=F_CS;
  4294. OP_SUB:
  4295. ovloc.resflags:=F_CC;
  4296. else
  4297. ;
  4298. end;
  4299. end;
  4300. end
  4301. else
  4302. begin
  4303. { there could be added some more sophisticated optimizations }
  4304. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  4305. a_load_reg_reg(list,size,size,src,dst)
  4306. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  4307. a_load_const_reg(list,size,0,dst)
  4308. else if (op in [OP_IMUL]) and (a=-1) then
  4309. a_op_reg_reg(list,OP_NEG,size,src,dst)
  4310. { we do this here instead in the peephole optimizer because
  4311. it saves us a register }
  4312. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  4313. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  4314. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  4315. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  4316. begin
  4317. if l1>32 then{roozbeh does this ever happen?}
  4318. internalerror(2003082911);
  4319. shifterop_reset(so);
  4320. so.shiftmode:=SM_LSL;
  4321. so.shiftimm:=l1;
  4322. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  4323. end
  4324. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  4325. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  4326. begin
  4327. if l1>32 then{does this ever happen?}
  4328. internalerror(2012051803);
  4329. shifterop_reset(so);
  4330. so.shiftmode:=SM_LSL;
  4331. so.shiftimm:=l1;
  4332. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  4333. end
  4334. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,src,dst) then
  4335. begin
  4336. { nothing to do on success }
  4337. end
  4338. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  4339. Just using mov x, #0 might allow some easier optimizations down the line. }
  4340. else if (op = OP_AND) and (dword(a)=0) then
  4341. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  4342. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  4343. else if (op = OP_AND) and (not(dword(a))=0) then
  4344. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  4345. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  4346. broader range of shifterconstants.}
  4347. {else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  4348. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))}
  4349. else if (op = OP_AND) and is_thumb32_imm(a) then
  4350. list.concat(taicpu.op_reg_reg_const(A_AND,dst,src,dword(a)))
  4351. else if (op = OP_AND) and (a = $FFFF) then
  4352. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  4353. else if (op = OP_AND) and is_thumb32_imm(not(dword(a))) then
  4354. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  4355. else if (op = OP_AND) and is_continuous_mask(aword(not(a)), shift, width) then
  4356. begin
  4357. a_load_reg_reg(list,size,size,src,dst);
  4358. list.concat(taicpu.op_reg_const_const(A_BFC,dst,shift,width))
  4359. end
  4360. else
  4361. begin
  4362. tmpreg:=getintregister(list,size);
  4363. a_load_const_reg(list,size,a,tmpreg);
  4364. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  4365. end;
  4366. end;
  4367. maybeadjustresult(list,op,size,dst);
  4368. end;
  4369. const
  4370. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  4371. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  4372. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  4373. procedure tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4374. var
  4375. so : tshifterop;
  4376. tmpreg,overflowreg : tregister;
  4377. asmop : tasmop;
  4378. begin
  4379. ovloc.loc:=LOC_VOID;
  4380. case op of
  4381. OP_NEG,OP_NOT:
  4382. internalerror(200308286);
  4383. OP_ROL:
  4384. begin
  4385. if not(size in [OS_32,OS_S32]) then
  4386. internalerror(2008072806);
  4387. { simulate ROL by ror'ing 32-value }
  4388. tmpreg:=getintregister(list,OS_32);
  4389. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  4390. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  4391. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4392. end;
  4393. OP_ROR:
  4394. begin
  4395. if not(size in [OS_32,OS_S32]) then
  4396. internalerror(2008072802);
  4397. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4398. end;
  4399. OP_IMUL,
  4400. OP_MUL:
  4401. begin
  4402. if cgsetflags or setflags then
  4403. begin
  4404. overflowreg:=getintregister(list,size);
  4405. if op=OP_IMUL then
  4406. asmop:=A_SMULL
  4407. else
  4408. asmop:=A_UMULL;
  4409. { the arm doesn't allow that rd and rm are the same }
  4410. if dst=src2 then
  4411. begin
  4412. if dst<>src1 then
  4413. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  4414. else
  4415. begin
  4416. tmpreg:=getintregister(list,size);
  4417. a_load_reg_reg(list,size,size,src2,dst);
  4418. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  4419. end;
  4420. end
  4421. else
  4422. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  4423. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4424. if op=OP_IMUL then
  4425. begin
  4426. shifterop_reset(so);
  4427. so.shiftmode:=SM_ASR;
  4428. so.shiftimm:=31;
  4429. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  4430. end
  4431. else
  4432. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  4433. ovloc.loc:=LOC_FLAGS;
  4434. ovloc.resflags:=F_NE;
  4435. end
  4436. else
  4437. begin
  4438. { the arm doesn't allow that rd and rm are the same }
  4439. if dst=src2 then
  4440. begin
  4441. if dst<>src1 then
  4442. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  4443. else
  4444. begin
  4445. tmpreg:=getintregister(list,size);
  4446. a_load_reg_reg(list,size,size,src2,dst);
  4447. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  4448. end;
  4449. end
  4450. else
  4451. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  4452. end;
  4453. end;
  4454. else
  4455. begin
  4456. if cgsetflags or setflags then
  4457. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4458. {$ifdef dummy}
  4459. { R13 is not allowed for certain instruction operands }
  4460. if op_reg_reg_opcg2asmopThumb2[op] in [A_ADD,A_SUB,A_AND,A_BIC,A_EOR] then
  4461. begin
  4462. if getsupreg(dst)=RS_R13 then
  4463. begin
  4464. tmpreg:=getintregister(list,OS_INT);
  4465. a_load_reg_reg(list,OS_INT,OS_INT,dst,tmpreg);
  4466. dst:=tmpreg;
  4467. end;
  4468. if getsupreg(src1)=RS_R13 then
  4469. begin
  4470. tmpreg:=getintregister(list,OS_INT);
  4471. a_load_reg_reg(list,OS_INT,OS_INT,src1,tmpreg);
  4472. src1:=tmpreg;
  4473. end;
  4474. end;
  4475. {$endif}
  4476. list.concat(setoppostfix(
  4477. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4478. end;
  4479. end;
  4480. maybeadjustresult(list,op,size,dst);
  4481. end;
  4482. procedure tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  4483. begin
  4484. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  4485. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  4486. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  4487. end;
  4488. procedure tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  4489. var
  4490. ref : treference;
  4491. shift : byte;
  4492. firstfloatreg,lastfloatreg,
  4493. r : byte;
  4494. regs : tcpuregisterset;
  4495. stackmisalignment: pint;
  4496. begin
  4497. LocalSize:=align(LocalSize,4);
  4498. { call instruction does not put anything on the stack }
  4499. stackmisalignment:=0;
  4500. if not(nostackframe) then
  4501. begin
  4502. firstfloatreg:=RS_NO;
  4503. lastfloatreg:=RS_NO;
  4504. { save floating point registers? }
  4505. for r:=RS_F0 to RS_F7 do
  4506. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4507. begin
  4508. if firstfloatreg=RS_NO then
  4509. firstfloatreg:=r;
  4510. lastfloatreg:=r;
  4511. inc(stackmisalignment,12);
  4512. end;
  4513. a_reg_alloc(list,NR_STACK_POINTER_REG);
  4514. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4515. begin
  4516. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  4517. a_reg_alloc(list,NR_R12);
  4518. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  4519. end;
  4520. { save int registers }
  4521. reference_reset(ref,4,[]);
  4522. ref.index:=NR_STACK_POINTER_REG;
  4523. ref.addressmode:=AM_PREINDEXED;
  4524. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4525. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4526. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  4527. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  4528. include(regs,RS_R14);
  4529. if regs<>[] then
  4530. begin
  4531. for r:=RS_R0 to RS_R15 do
  4532. if (r in regs) then
  4533. inc(stackmisalignment,4);
  4534. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4535. end;
  4536. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4537. begin
  4538. { the framepointer now points to the saved R15, so the saved
  4539. framepointer is at R11-12 (for get_caller_frame) }
  4540. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  4541. a_reg_dealloc(list,NR_R12);
  4542. end;
  4543. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4544. if (LocalSize<>0) or
  4545. ((stackmisalignment<>0) and
  4546. ((pi_do_call in current_procinfo.flags) or
  4547. (po_assembler in current_procinfo.procdef.procoptions))) then
  4548. begin
  4549. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4550. if not(is_shifter_const(localsize,shift)) then
  4551. begin
  4552. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  4553. a_reg_alloc(list,NR_R12);
  4554. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4555. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  4556. a_reg_dealloc(list,NR_R12);
  4557. end
  4558. else
  4559. begin
  4560. a_reg_dealloc(list,NR_R12);
  4561. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  4562. end;
  4563. end;
  4564. if firstfloatreg<>RS_NO then
  4565. begin
  4566. reference_reset(ref,4,[]);
  4567. if tg.direction*tcpuprocinfo(current_procinfo).floatregstart>=1023 then
  4568. begin
  4569. a_load_const_reg(list,OS_ADDR,-tcpuprocinfo(current_procinfo).floatregstart,NR_R12);
  4570. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4571. ref.base:=NR_R12;
  4572. end
  4573. else
  4574. begin
  4575. ref.base:=current_procinfo.framepointer;
  4576. ref.offset:=tcpuprocinfo(current_procinfo).floatregstart;
  4577. end;
  4578. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4579. lastfloatreg-firstfloatreg+1,ref));
  4580. end;
  4581. end;
  4582. end;
  4583. procedure tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  4584. var
  4585. ref : treference;
  4586. firstfloatreg,lastfloatreg,
  4587. r : byte;
  4588. shift : byte;
  4589. regs : tcpuregisterset;
  4590. LocalSize : longint;
  4591. stackmisalignment: pint;
  4592. begin
  4593. if not(nostackframe) then
  4594. begin
  4595. stackmisalignment:=0;
  4596. { restore floating point register }
  4597. firstfloatreg:=RS_NO;
  4598. lastfloatreg:=RS_NO;
  4599. { save floating point registers? }
  4600. for r:=RS_F0 to RS_F7 do
  4601. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4602. begin
  4603. if firstfloatreg=RS_NO then
  4604. firstfloatreg:=r;
  4605. lastfloatreg:=r;
  4606. { floating point register space is already included in
  4607. localsize below by calc_stackframe_size
  4608. inc(stackmisalignment,12);
  4609. }
  4610. end;
  4611. if firstfloatreg<>RS_NO then
  4612. begin
  4613. reference_reset(ref,4,[]);
  4614. if tg.direction*tcpuprocinfo(current_procinfo).floatregstart>=1023 then
  4615. begin
  4616. a_load_const_reg(list,OS_ADDR,-tcpuprocinfo(current_procinfo).floatregstart,NR_R12);
  4617. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4618. ref.base:=NR_R12;
  4619. end
  4620. else
  4621. begin
  4622. ref.base:=current_procinfo.framepointer;
  4623. ref.offset:=tcpuprocinfo(current_procinfo).floatregstart;
  4624. end;
  4625. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4626. lastfloatreg-firstfloatreg+1,ref));
  4627. end;
  4628. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4629. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  4630. begin
  4631. exclude(regs,RS_R14);
  4632. include(regs,RS_R15);
  4633. end;
  4634. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  4635. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  4636. for r:=RS_R0 to RS_R15 do
  4637. if (r in regs) then
  4638. inc(stackmisalignment,4);
  4639. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4640. LocalSize:=current_procinfo.calc_stackframe_size;
  4641. if (LocalSize<>0) or
  4642. ((stackmisalignment<>0) and
  4643. ((pi_do_call in current_procinfo.flags) or
  4644. (po_assembler in current_procinfo.procdef.procoptions))) then
  4645. begin
  4646. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4647. if not(is_shifter_const(LocalSize,shift)) then
  4648. begin
  4649. a_reg_alloc(list,NR_R12);
  4650. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4651. list.concat(taicpu.op_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_R12));
  4652. a_reg_dealloc(list,NR_R12);
  4653. end
  4654. else
  4655. begin
  4656. a_reg_dealloc(list,NR_R12);
  4657. list.concat(taicpu.op_reg_const(A_ADD,NR_STACK_POINTER_REG,LocalSize));
  4658. end;
  4659. end;
  4660. if regs=[] then
  4661. list.concat(taicpu.op_reg(A_BX,NR_R14))
  4662. else
  4663. begin
  4664. reference_reset(ref,4,[]);
  4665. ref.index:=NR_STACK_POINTER_REG;
  4666. ref.addressmode:=AM_PREINDEXED;
  4667. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4668. end;
  4669. end
  4670. else
  4671. list.concat(taicpu.op_reg(A_BX,NR_R14));
  4672. end;
  4673. function tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  4674. var
  4675. tmpreg : tregister;
  4676. tmpref : treference;
  4677. l : tasmlabel;
  4678. begin
  4679. tmpreg:=NR_NO;
  4680. { Be sure to have a base register }
  4681. if (ref.base=NR_NO) then
  4682. begin
  4683. if ref.shiftmode<>SM_None then
  4684. internalerror(2014020706);
  4685. ref.base:=ref.index;
  4686. ref.index:=NR_NO;
  4687. end;
  4688. { absolute symbols can't be handled directly, we've to store the symbol reference
  4689. in the text segment and access it pc relative
  4690. For now, we assume that references where base or index equals to PC are already
  4691. relative, all other references are assumed to be absolute and thus they need
  4692. to be handled extra.
  4693. A proper solution would be to change refoptions to a set and store the information
  4694. if the symbol is absolute or relative there.
  4695. }
  4696. if (assigned(ref.symbol) and
  4697. not(is_pc(ref.base)) and
  4698. not(is_pc(ref.index))
  4699. ) or
  4700. { [#xxx] isn't a valid address operand }
  4701. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  4702. //(ref.offset<-4095) or
  4703. (ref.offset<-255) or
  4704. (ref.offset>4095) or
  4705. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  4706. ((ref.offset<-255) or
  4707. (ref.offset>255)
  4708. )
  4709. ) or
  4710. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  4711. ((ref.offset<-1020) or
  4712. (ref.offset>1020) or
  4713. ((abs(ref.offset) mod 4)<>0) or
  4714. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  4715. assigned(ref.symbol)
  4716. )
  4717. ) then
  4718. begin
  4719. reference_reset(tmpref,4,[]);
  4720. { load symbol }
  4721. tmpreg:=getintregister(list,OS_INT);
  4722. if assigned(ref.symbol) then
  4723. begin
  4724. current_asmdata.getjumplabel(l);
  4725. cg.a_label(current_procinfo.aktlocaldata,l);
  4726. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  4727. if ref.refaddr=addr_gottpoff then
  4728. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_gottpoff,ref.symbol,ref.relsymbol,ref.offset))
  4729. else if ref.refaddr=addr_tlsgd then
  4730. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_tlsgd,ref.symbol,ref.relsymbol,ref.offset))
  4731. else if ref.refaddr=addr_tlsdesc then
  4732. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_tlsdesc,ref.symbol,ref.relsymbol,ref.offset))
  4733. else if ref.refaddr=addr_tpoff then
  4734. begin
  4735. if assigned(ref.relsymbol) or (ref.offset<>0) then
  4736. Internalerror(2019092807);
  4737. current_procinfo.aktlocaldata.concat(tai_const.Create_type_sym(aitconst_tpoff,ref.symbol));
  4738. end
  4739. else
  4740. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  4741. { load consts entry }
  4742. tmpref.symbol:=l;
  4743. tmpref.base:=NR_R15;
  4744. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  4745. { in case of LDF/STF, we got rid of the NR_R15 }
  4746. if is_pc(ref.base) then
  4747. ref.base:=NR_NO;
  4748. if is_pc(ref.index) then
  4749. ref.index:=NR_NO;
  4750. end
  4751. else
  4752. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  4753. if (ref.base<>NR_NO) then
  4754. begin
  4755. if ref.index<>NR_NO then
  4756. begin
  4757. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4758. ref.base:=tmpreg;
  4759. end
  4760. else
  4761. begin
  4762. ref.index:=tmpreg;
  4763. ref.shiftimm:=0;
  4764. ref.signindex:=1;
  4765. ref.shiftmode:=SM_None;
  4766. end;
  4767. end
  4768. else
  4769. ref.base:=tmpreg;
  4770. ref.offset:=0;
  4771. ref.symbol:=nil;
  4772. end;
  4773. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  4774. begin
  4775. if tmpreg<>NR_NO then
  4776. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  4777. else
  4778. begin
  4779. tmpreg:=getintregister(list,OS_ADDR);
  4780. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  4781. ref.base:=tmpreg;
  4782. end;
  4783. ref.offset:=0;
  4784. end;
  4785. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  4786. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  4787. begin
  4788. tmpreg:=getintregister(list,OS_ADDR);
  4789. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  4790. ref.base := tmpreg;
  4791. end;
  4792. { floating point operations have only limited references
  4793. we expect here, that a base is already set }
  4794. if ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and (ref.index<>NR_NO) then
  4795. begin
  4796. if ref.shiftmode<>SM_none then
  4797. internalerror(2003091202);
  4798. if tmpreg<>NR_NO then
  4799. begin
  4800. if ref.base=tmpreg then
  4801. begin
  4802. if ref.signindex<0 then
  4803. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  4804. else
  4805. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  4806. ref.index:=NR_NO;
  4807. end
  4808. else
  4809. begin
  4810. if ref.index<>tmpreg then
  4811. internalerror(2004031602);
  4812. if ref.signindex<0 then
  4813. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  4814. else
  4815. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4816. ref.base:=tmpreg;
  4817. ref.index:=NR_NO;
  4818. end;
  4819. end
  4820. else
  4821. begin
  4822. tmpreg:=getintregister(list,OS_ADDR);
  4823. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  4824. ref.base:=tmpreg;
  4825. ref.index:=NR_NO;
  4826. end;
  4827. end;
  4828. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  4829. Result := ref;
  4830. end;
  4831. procedure tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  4832. var
  4833. instr: taicpu;
  4834. begin
  4835. if (fromsize=OS_F32) and
  4836. (tosize=OS_F32) then
  4837. begin
  4838. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  4839. list.Concat(instr);
  4840. add_move_instruction(instr);
  4841. { VMOV cannot generate an FPU exception, so we do not need a check here }
  4842. end
  4843. else if (fromsize=OS_F64) and
  4844. (tosize=OS_F64) then
  4845. begin
  4846. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  4847. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  4848. end
  4849. else if (fromsize=OS_F32) and
  4850. (tosize=OS_F64) then
  4851. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  4852. begin
  4853. //list.concat(nil);
  4854. end;
  4855. end;
  4856. procedure tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  4857. begin
  4858. handle_load_store(list,A_VLDR,PF_None,reg,ref);
  4859. end;
  4860. procedure tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  4861. begin
  4862. handle_load_store(list,A_VSTR,PF_None,reg,ref);
  4863. { VSTR cannot generate an FPU exception, so we do not need a check here }
  4864. end;
  4865. procedure tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  4866. begin
  4867. if //(shuffle=nil) and
  4868. (tosize=OS_F32) then
  4869. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  4870. else
  4871. internalerror(2012100813);
  4872. end;
  4873. procedure tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  4874. begin
  4875. if //(shuffle=nil) and
  4876. (fromsize=OS_F32) then
  4877. begin
  4878. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg));
  4879. { VMOV cannot generate an FPU exception, so we do not need a check here }
  4880. end
  4881. else
  4882. internalerror(2012100814);
  4883. end;
  4884. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  4885. var tmpreg: tregister;
  4886. begin
  4887. case op of
  4888. OP_NEG:
  4889. begin
  4890. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4891. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  4892. tmpreg:=cg.getintregister(list,OS_32);
  4893. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  4894. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  4895. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4896. end;
  4897. else
  4898. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  4899. end;
  4900. end;
  4901. procedure tthumbcg64farm.a_op64_reg_reg(list: TAsmList; op: TOpCG; size: tcgsize; regsrc, regdst: tregister64);
  4902. begin
  4903. case op of
  4904. OP_NEG:
  4905. begin
  4906. list.concat(taicpu.op_reg_const(A_MOV,regdst.reglo,0));
  4907. list.concat(taicpu.op_reg_const(A_MOV,regdst.reghi,0));
  4908. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4909. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4910. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4911. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4912. end;
  4913. OP_NOT:
  4914. begin
  4915. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  4916. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  4917. end;
  4918. OP_AND,OP_OR,OP_XOR:
  4919. begin
  4920. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  4921. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  4922. end;
  4923. OP_ADD:
  4924. begin
  4925. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4926. list.concat(taicpu.op_reg_reg(A_ADD,regdst.reglo,regsrc.reglo));
  4927. list.concat(taicpu.op_reg_reg(A_ADC,regdst.reghi,regsrc.reghi));
  4928. end;
  4929. OP_SUB:
  4930. begin
  4931. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4932. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4933. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4934. end;
  4935. else
  4936. internalerror(2003083105);
  4937. end;
  4938. end;
  4939. procedure tthumbcg64farm.a_op64_const_reg(list: TAsmList; op: TOpCG; size: tcgsize; value: int64; reg: tregister64);
  4940. var
  4941. tmpreg : tregister;
  4942. begin
  4943. case op of
  4944. OP_AND,OP_OR,OP_XOR:
  4945. begin
  4946. cg.a_op_const_reg(list,op,OS_32,aint(lo(value)),reg.reglo);
  4947. cg.a_op_const_reg(list,op,OS_32,aint(hi(value)),reg.reghi);
  4948. end;
  4949. OP_ADD:
  4950. begin
  4951. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4952. begin
  4953. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4954. list.concat(taicpu.op_reg_const(A_ADD,reg.reglo,aint(lo(value))));
  4955. end
  4956. else
  4957. begin
  4958. tmpreg:=cg.getintregister(list,OS_32);
  4959. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4960. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4961. list.concat(taicpu.op_reg_reg(A_ADD,reg.reglo,tmpreg));
  4962. end;
  4963. tmpreg:=cg.getintregister(list,OS_32);
  4964. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  4965. list.concat(taicpu.op_reg_reg(A_ADC,reg.reghi,tmpreg));
  4966. end;
  4967. OP_SUB:
  4968. begin
  4969. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4970. begin
  4971. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4972. list.concat(taicpu.op_reg_const(A_SUB,reg.reglo,aint(lo(value))))
  4973. end
  4974. else
  4975. begin
  4976. tmpreg:=cg.getintregister(list,OS_32);
  4977. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4978. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4979. list.concat(taicpu.op_reg_reg(A_SUB,reg.reglo,tmpreg));
  4980. end;
  4981. tmpreg:=cg.getintregister(list,OS_32);
  4982. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  4983. list.concat(taicpu.op_reg_reg(A_SBC,reg.reghi,tmpreg));
  4984. end;
  4985. else
  4986. internalerror(2003083106);
  4987. end;
  4988. end;
  4989. procedure create_codegen;
  4990. begin
  4991. if GenerateThumb2Code then
  4992. begin
  4993. cg:=tthumb2cgarm.create;
  4994. cg64:=tthumb2cg64farm.create;
  4995. casmoptimizer:=TCpuThumb2AsmOptimizer;
  4996. end
  4997. else if GenerateThumbCode then
  4998. begin
  4999. cg:=tthumbcgarm.create;
  5000. cg64:=tthumbcg64farm.create;
  5001. // casmoptimizer:=TCpuThumbAsmOptimizer;
  5002. end
  5003. else
  5004. begin
  5005. cg:=tarmcgarm.create;
  5006. cg64:=tarmcg64farm.create;
  5007. casmoptimizer:=TCpuAsmOptimizer;
  5008. end;
  5009. end;
  5010. end.