cgcpu.pas 219 KB

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