cgcpu.pas 216 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398
  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_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  56. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  57. procedure g_maybe_got_init(list : TAsmList); override;
  58. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  59. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  60. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);override;
  61. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  62. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  63. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  64. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  65. procedure g_save_registers(list : TAsmList);override;
  66. procedure g_restore_registers(list : TAsmList);override;
  67. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  68. procedure fixref(list : TAsmList;var ref : treference);
  69. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; virtual;
  70. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  71. procedure g_stackpointer_alloc(list : TAsmList;size : 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. end;
  140. tthumbcg64farm = class(tbasecg64farm)
  141. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  142. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  143. end;
  144. tthumb2cgarm = class(tcgarm)
  145. procedure init_register_allocators;override;
  146. procedure done_register_allocators;override;
  147. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  148. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  149. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  150. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  151. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  152. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  153. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  154. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  155. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  156. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; override;
  157. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  158. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  159. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  160. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  161. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  162. end;
  163. tthumb2cg64farm = class(tcg64farm)
  164. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  165. end;
  166. const
  167. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  168. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  169. winstackpagesize = 4096;
  170. function get_fpu_postfix(def : tdef) : toppostfix;
  171. procedure create_codegen;
  172. implementation
  173. uses
  174. globals,verbose,systems,cutils,
  175. aopt,aoptcpu,
  176. fmodule,
  177. symconst,symsym,symtable,
  178. tgobj,
  179. procinfo,cpupi,
  180. paramgr;
  181. function get_fpu_postfix(def : tdef) : toppostfix;
  182. begin
  183. if def.typ=floatdef then
  184. begin
  185. case tfloatdef(def).floattype of
  186. s32real:
  187. result:=PF_S;
  188. s64real:
  189. result:=PF_D;
  190. s80real:
  191. result:=PF_E;
  192. else
  193. internalerror(200401272);
  194. end;
  195. end
  196. else
  197. internalerror(200401271);
  198. end;
  199. procedure tarmcgarm.init_register_allocators;
  200. begin
  201. inherited init_register_allocators;
  202. { currently, we always save R14, so we can use it }
  203. if (target_info.system<>system_arm_darwin) then
  204. begin
  205. if assigned(current_procinfo) and (current_procinfo.framepointer<>NR_R11) then
  206. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  207. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  208. RS_R9,RS_R10,RS_R11,RS_R14],first_int_imreg,[])
  209. else
  210. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  211. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  212. RS_R9,RS_R10,RS_R14],first_int_imreg,[])
  213. end
  214. else
  215. { r7 is not available on Darwin, it's used as frame pointer (always,
  216. for backtrace support -- also in gcc/clang -> R11 can be used).
  217. r9 is volatile }
  218. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  219. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R9,RS_R12,RS_R4,RS_R5,RS_R6,RS_R8,
  220. RS_R10,RS_R11,RS_R14],first_int_imreg,[]);
  221. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  222. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  223. { The register allocator currently cannot deal with multiple
  224. non-overlapping subregs per register, so we can only use
  225. half the single precision registers for now (as sub registers of the
  226. double precision ones). }
  227. if current_settings.fputype=fpu_vfpv3 then
  228. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  229. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  230. 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,
  231. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  232. ],first_mm_imreg,[])
  233. else
  234. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  235. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15],first_mm_imreg,[]);
  236. end;
  237. procedure tarmcgarm.done_register_allocators;
  238. begin
  239. rg[R_INTREGISTER].free;
  240. rg[R_FPUREGISTER].free;
  241. rg[R_MMREGISTER].free;
  242. inherited done_register_allocators;
  243. end;
  244. procedure tcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  245. var
  246. imm_shift : byte;
  247. l : tasmlabel;
  248. hr : treference;
  249. imm1, imm2: DWord;
  250. begin
  251. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  252. internalerror(2002090902);
  253. if is_shifter_const(a,imm_shift) then
  254. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  255. else if is_shifter_const(not(a),imm_shift) then
  256. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  257. { loading of constants with mov and orr }
  258. else if (split_into_shifter_const(a,imm1, imm2)) then
  259. begin
  260. list.concat(taicpu.op_reg_const(A_MOV,reg, imm1));
  261. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg, imm2));
  262. end
  263. { loading of constants with mvn and bic }
  264. else if (split_into_shifter_const(not(a), imm1, imm2)) then
  265. begin
  266. list.concat(taicpu.op_reg_const(A_MVN,reg, imm1));
  267. list.concat(taicpu.op_reg_reg_const(A_BIC,reg,reg, imm2));
  268. end
  269. else
  270. begin
  271. reference_reset(hr,4);
  272. current_asmdata.getjumplabel(l);
  273. cg.a_label(current_procinfo.aktlocaldata,l);
  274. hr.symboldata:=current_procinfo.aktlocaldata.last;
  275. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  276. hr.symbol:=l;
  277. hr.base:=NR_PC;
  278. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  279. end;
  280. end;
  281. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  282. var
  283. oppostfix:toppostfix;
  284. usedtmpref: treference;
  285. tmpreg,tmpreg2 : tregister;
  286. so : tshifterop;
  287. dir : integer;
  288. begin
  289. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  290. FromSize := ToSize;
  291. case FromSize of
  292. { signed integer registers }
  293. OS_8:
  294. oppostfix:=PF_B;
  295. OS_S8:
  296. oppostfix:=PF_SB;
  297. OS_16:
  298. oppostfix:=PF_H;
  299. OS_S16:
  300. oppostfix:=PF_SH;
  301. OS_32,
  302. OS_S32:
  303. oppostfix:=PF_None;
  304. else
  305. InternalError(200308297);
  306. end;
  307. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  308. begin
  309. if target_info.endian=endian_big then
  310. dir:=-1
  311. else
  312. dir:=1;
  313. case FromSize of
  314. OS_16,OS_S16:
  315. begin
  316. { only complicated references need an extra loadaddr }
  317. if assigned(ref.symbol) or
  318. (ref.index<>NR_NO) or
  319. (ref.offset<-4095) or
  320. (ref.offset>4094) or
  321. { sometimes the compiler reused registers }
  322. (reg=ref.index) or
  323. (reg=ref.base) then
  324. begin
  325. tmpreg2:=getintregister(list,OS_INT);
  326. a_loadaddr_ref_reg(list,ref,tmpreg2);
  327. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  328. end
  329. else
  330. usedtmpref:=ref;
  331. if target_info.endian=endian_big then
  332. inc(usedtmpref.offset,1);
  333. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  334. tmpreg:=getintregister(list,OS_INT);
  335. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  336. inc(usedtmpref.offset,dir);
  337. if FromSize=OS_16 then
  338. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  339. else
  340. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  341. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  342. end;
  343. OS_32,OS_S32:
  344. begin
  345. tmpreg:=getintregister(list,OS_INT);
  346. { only complicated references need an extra loadaddr }
  347. if assigned(ref.symbol) or
  348. (ref.index<>NR_NO) or
  349. (ref.offset<-4095) or
  350. (ref.offset>4092) or
  351. { sometimes the compiler reused registers }
  352. (reg=ref.index) or
  353. (reg=ref.base) then
  354. begin
  355. tmpreg2:=getintregister(list,OS_INT);
  356. a_loadaddr_ref_reg(list,ref,tmpreg2);
  357. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  358. end
  359. else
  360. usedtmpref:=ref;
  361. shifterop_reset(so);so.shiftmode:=SM_LSL;
  362. if ref.alignment=2 then
  363. begin
  364. if target_info.endian=endian_big then
  365. inc(usedtmpref.offset,2);
  366. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  367. inc(usedtmpref.offset,dir*2);
  368. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  369. so.shiftimm:=16;
  370. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  371. end
  372. else
  373. begin
  374. tmpreg2:=getintregister(list,OS_INT);
  375. if target_info.endian=endian_big then
  376. inc(usedtmpref.offset,3);
  377. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  378. inc(usedtmpref.offset,dir);
  379. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  380. inc(usedtmpref.offset,dir);
  381. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  382. so.shiftimm:=8;
  383. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  384. inc(usedtmpref.offset,dir);
  385. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  386. so.shiftimm:=16;
  387. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg2,so));
  388. so.shiftimm:=24;
  389. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  390. end;
  391. end
  392. else
  393. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  394. end;
  395. end
  396. else
  397. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  398. if (fromsize=OS_S8) and (tosize = OS_16) then
  399. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  400. end;
  401. procedure tcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  402. var
  403. hsym : tsym;
  404. href : treference;
  405. paraloc : Pcgparalocation;
  406. shift : byte;
  407. begin
  408. { calculate the parameter info for the procdef }
  409. procdef.init_paraloc_info(callerside);
  410. hsym:=tsym(procdef.parast.Find('self'));
  411. if not(assigned(hsym) and
  412. (hsym.typ=paravarsym)) then
  413. internalerror(200305251);
  414. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  415. while paraloc<>nil do
  416. with paraloc^ do
  417. begin
  418. case loc of
  419. LOC_REGISTER:
  420. begin
  421. if is_shifter_const(ioffset,shift) then
  422. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  423. else
  424. begin
  425. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  426. a_op_reg_reg(list,OP_SUB,size,NR_R12,register);
  427. end;
  428. end;
  429. LOC_REFERENCE:
  430. begin
  431. { offset in the wrapper needs to be adjusted for the stored
  432. return address }
  433. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  434. if is_shifter_const(ioffset,shift) then
  435. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  436. else
  437. begin
  438. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  439. a_op_reg_ref(list,OP_SUB,size,NR_R12,href);
  440. end;
  441. end
  442. else
  443. internalerror(200309189);
  444. end;
  445. paraloc:=next;
  446. end;
  447. end;
  448. procedure tbasecgarm.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  449. var
  450. ref: treference;
  451. begin
  452. paraloc.check_simple_location;
  453. paramanager.allocparaloc(list,paraloc.location);
  454. case paraloc.location^.loc of
  455. LOC_REGISTER,LOC_CREGISTER:
  456. a_load_const_reg(list,size,a,paraloc.location^.register);
  457. LOC_REFERENCE:
  458. begin
  459. reference_reset(ref,paraloc.alignment);
  460. ref.base:=paraloc.location^.reference.index;
  461. ref.offset:=paraloc.location^.reference.offset;
  462. a_load_const_ref(list,size,a,ref);
  463. end;
  464. else
  465. internalerror(2002081101);
  466. end;
  467. end;
  468. procedure tbasecgarm.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  469. var
  470. tmpref, ref: treference;
  471. location: pcgparalocation;
  472. sizeleft: aint;
  473. begin
  474. location := paraloc.location;
  475. tmpref := r;
  476. sizeleft := paraloc.intsize;
  477. while assigned(location) do
  478. begin
  479. paramanager.allocparaloc(list,location);
  480. case location^.loc of
  481. LOC_REGISTER,LOC_CREGISTER:
  482. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  483. LOC_REFERENCE:
  484. begin
  485. reference_reset_base(ref,location^.reference.index,location^.reference.offset,paraloc.alignment);
  486. { doubles in softemu mode have a strange order of registers and references }
  487. if location^.size=OS_32 then
  488. g_concatcopy(list,tmpref,ref,4)
  489. else
  490. begin
  491. g_concatcopy(list,tmpref,ref,sizeleft);
  492. if assigned(location^.next) then
  493. internalerror(2005010710);
  494. end;
  495. end;
  496. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  497. case location^.size of
  498. OS_F32, OS_F64:
  499. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  500. else
  501. internalerror(2002072801);
  502. end;
  503. LOC_VOID:
  504. begin
  505. // nothing to do
  506. end;
  507. else
  508. internalerror(2002081103);
  509. end;
  510. inc(tmpref.offset,tcgsize2size[location^.size]);
  511. dec(sizeleft,tcgsize2size[location^.size]);
  512. location := location^.next;
  513. end;
  514. end;
  515. procedure tbasecgarm.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  516. var
  517. ref: treference;
  518. tmpreg: tregister;
  519. begin
  520. paraloc.check_simple_location;
  521. paramanager.allocparaloc(list,paraloc.location);
  522. case paraloc.location^.loc of
  523. LOC_REGISTER,LOC_CREGISTER:
  524. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  525. LOC_REFERENCE:
  526. begin
  527. reference_reset(ref,paraloc.alignment);
  528. ref.base := paraloc.location^.reference.index;
  529. ref.offset := paraloc.location^.reference.offset;
  530. tmpreg := getintregister(list,OS_ADDR);
  531. a_loadaddr_ref_reg(list,r,tmpreg);
  532. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  533. end;
  534. else
  535. internalerror(2002080701);
  536. end;
  537. end;
  538. procedure tbasecgarm.a_call_name(list : TAsmList;const s : string; weak: boolean);
  539. var
  540. branchopcode: tasmop;
  541. r : treference;
  542. sym : TAsmSymbol;
  543. begin
  544. { check not really correct: should only be used for non-Thumb cpus }
  545. if CPUARM_HAS_BLX_LABEL in cpu_capabilities[current_settings.cputype] then
  546. branchopcode:=A_BLX
  547. else
  548. branchopcode:=A_BL;
  549. if not(weak) then
  550. sym:=current_asmdata.RefAsmSymbol(s)
  551. else
  552. sym:=current_asmdata.WeakRefAsmSymbol(s);
  553. reference_reset_symbol(r,sym,0,sizeof(pint));
  554. if (tf_pic_uses_got in target_info.flags) and
  555. (cs_create_pic in current_settings.moduleswitches) then
  556. begin
  557. include(current_procinfo.flags,pi_needs_got);
  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_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1627. var
  1628. ref : treference;
  1629. shift : byte;
  1630. firstfloatreg,lastfloatreg,
  1631. r : byte;
  1632. mmregs,
  1633. regs, saveregs : tcpuregisterset;
  1634. registerarea,
  1635. r7offset,
  1636. stackmisalignment : pint;
  1637. postfix: toppostfix;
  1638. imm1, imm2: DWord;
  1639. stack_parameters : Boolean;
  1640. begin
  1641. LocalSize:=align(LocalSize,4);
  1642. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  1643. { call instruction does not put anything on the stack }
  1644. registerarea:=0;
  1645. tarmprocinfo(current_procinfo).stackpaddingreg:=High(TSuperRegister);
  1646. lastfloatreg:=RS_NO;
  1647. if not(nostackframe) then
  1648. begin
  1649. firstfloatreg:=RS_NO;
  1650. mmregs:=[];
  1651. case current_settings.fputype of
  1652. fpu_fpa,
  1653. fpu_fpa10,
  1654. fpu_fpa11:
  1655. begin
  1656. { save floating point registers? }
  1657. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1658. for r:=RS_F0 to RS_F7 do
  1659. if r in regs then
  1660. begin
  1661. if firstfloatreg=RS_NO then
  1662. firstfloatreg:=r;
  1663. lastfloatreg:=r;
  1664. inc(registerarea,12);
  1665. end;
  1666. end;
  1667. fpu_vfpv2,
  1668. fpu_vfpv3,
  1669. fpu_vfpv3_d16:
  1670. begin;
  1671. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1672. end;
  1673. end;
  1674. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1675. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1676. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1677. { save int registers }
  1678. reference_reset(ref,4);
  1679. ref.index:=NR_STACK_POINTER_REG;
  1680. ref.addressmode:=AM_PREINDEXED;
  1681. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1682. if not(target_info.system in systems_darwin) then
  1683. begin
  1684. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1685. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1686. begin
  1687. a_reg_alloc(list,NR_R12);
  1688. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1689. end;
  1690. { the (old) ARM APCS requires saving both the stack pointer (to
  1691. crawl the stack) and the PC (to identify the function this
  1692. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  1693. and R15 -- still needs updating for EABI and Darwin, they don't
  1694. need that }
  1695. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1696. regs:=regs+[RS_FRAME_POINTER_REG,RS_R12,RS_R14,RS_R15]
  1697. else
  1698. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1699. include(regs,RS_R14);
  1700. if regs<>[] then
  1701. begin
  1702. for r:=RS_R0 to RS_R15 do
  1703. if r in regs then
  1704. inc(registerarea,4);
  1705. { if the stack is not 8 byte aligned, try to add an extra register,
  1706. so we can avoid the extra sub/add ...,#4 later (KB) }
  1707. if ((registerarea mod current_settings.alignment.localalignmax) <> 0) then
  1708. for r:=RS_R3 downto RS_R0 do
  1709. if not(r in regs) then
  1710. begin
  1711. regs:=regs+[r];
  1712. inc(registerarea,4);
  1713. tarmprocinfo(current_procinfo).stackpaddingreg:=r;
  1714. break;
  1715. end;
  1716. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  1717. end;
  1718. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1719. begin
  1720. { the framepointer now points to the saved R15, so the saved
  1721. framepointer is at R11-12 (for get_caller_frame) }
  1722. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1723. a_reg_dealloc(list,NR_R12);
  1724. end;
  1725. end
  1726. else
  1727. begin
  1728. { always save r14 if we use r7 as the framepointer, because
  1729. the parameter offsets are hardcoded in advance and always
  1730. assume that r14 sits on the stack right behind the saved r7
  1731. }
  1732. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1733. include(regs,RS_FRAME_POINTER_REG);
  1734. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1735. include(regs,RS_R14);
  1736. if regs<>[] then
  1737. begin
  1738. { on Darwin, you first have to save [r4-r7,lr], and then
  1739. [r8,r10,r11] and make r7 point to the previously saved
  1740. r7 so that you can perform a stack crawl based on it
  1741. ([r7] is previous stack frame, [r7+4] is return address
  1742. }
  1743. include(regs,RS_FRAME_POINTER_REG);
  1744. saveregs:=regs-[RS_R8,RS_R10,RS_R11];
  1745. r7offset:=0;
  1746. for r:=RS_R0 to RS_R15 do
  1747. if r in saveregs then
  1748. begin
  1749. inc(registerarea,4);
  1750. if r<RS_FRAME_POINTER_REG then
  1751. inc(r7offset,4);
  1752. end;
  1753. { save the registers }
  1754. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1755. { make r7 point to the saved r7 (regardless of whether this
  1756. frame uses the framepointer, for backtrace purposes) }
  1757. if r7offset<>0 then
  1758. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_FRAME_POINTER_REG,NR_R13,r7offset))
  1759. else
  1760. list.concat(taicpu.op_reg_reg(A_MOV,NR_R7,NR_R13));
  1761. { now save the rest (if any) }
  1762. saveregs:=regs-saveregs;
  1763. if saveregs<>[] then
  1764. begin
  1765. for r:=RS_R8 to RS_R11 do
  1766. if r in saveregs then
  1767. inc(registerarea,4);
  1768. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1769. end;
  1770. end;
  1771. end;
  1772. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  1773. if (LocalSize<>0) or
  1774. ((stackmisalignment<>0) and
  1775. ((pi_do_call in current_procinfo.flags) or
  1776. (po_assembler in current_procinfo.procdef.procoptions))) then
  1777. begin
  1778. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  1779. if stack_parameters and (pi_estimatestacksize in current_procinfo.flags) then
  1780. begin
  1781. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  1782. internalerror(2014030901)
  1783. else
  1784. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  1785. end;
  1786. if is_shifter_const(localsize,shift) then
  1787. begin
  1788. a_reg_dealloc(list,NR_R12);
  1789. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1790. end
  1791. else if split_into_shifter_const(localsize, imm1, imm2) then
  1792. begin
  1793. a_reg_dealloc(list,NR_R12);
  1794. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  1795. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  1796. end
  1797. else
  1798. begin
  1799. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1800. a_reg_alloc(list,NR_R12);
  1801. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1802. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1803. a_reg_dealloc(list,NR_R12);
  1804. end;
  1805. end;
  1806. if (mmregs<>[]) or
  1807. (firstfloatreg<>RS_NO) then
  1808. begin
  1809. reference_reset(ref,4);
  1810. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1811. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1812. begin
  1813. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1814. begin
  1815. a_reg_alloc(list,NR_R12);
  1816. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1817. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1818. a_reg_dealloc(list,NR_R12);
  1819. end
  1820. else
  1821. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1822. ref.base:=NR_R12;
  1823. end
  1824. else
  1825. begin
  1826. ref.base:=current_procinfo.framepointer;
  1827. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1828. end;
  1829. case current_settings.fputype of
  1830. fpu_fpa,
  1831. fpu_fpa10,
  1832. fpu_fpa11:
  1833. begin
  1834. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1835. lastfloatreg-firstfloatreg+1,ref));
  1836. end;
  1837. fpu_vfpv2,
  1838. fpu_vfpv3,
  1839. fpu_vfpv3_d16:
  1840. begin
  1841. ref.index:=ref.base;
  1842. ref.base:=NR_NO;
  1843. { FSTMX is deprecated on ARMv6 and later }
  1844. if (current_settings.cputype<cpu_armv6) then
  1845. postfix:=PF_IAX
  1846. else
  1847. postfix:=PF_IAD;
  1848. list.concat(setoppostfix(taicpu.op_ref_regset(A_FSTM,ref,R_MMREGISTER,R_SUBFD,mmregs),postfix));
  1849. end;
  1850. end;
  1851. end;
  1852. end;
  1853. end;
  1854. procedure tbasecgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1855. var
  1856. ref : treference;
  1857. LocalSize : longint;
  1858. firstfloatreg,lastfloatreg,
  1859. r,
  1860. shift : byte;
  1861. mmregs,
  1862. saveregs,
  1863. regs : tcpuregisterset;
  1864. registerarea,
  1865. stackmisalignment: pint;
  1866. paddingreg: TSuperRegister;
  1867. mmpostfix: toppostfix;
  1868. imm1, imm2: DWord;
  1869. begin
  1870. if not(nostackframe) then
  1871. begin
  1872. registerarea:=0;
  1873. firstfloatreg:=RS_NO;
  1874. lastfloatreg:=RS_NO;
  1875. mmregs:=[];
  1876. saveregs:=[];
  1877. case current_settings.fputype of
  1878. fpu_fpa,
  1879. fpu_fpa10,
  1880. fpu_fpa11:
  1881. begin
  1882. { restore floating point registers? }
  1883. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1884. for r:=RS_F0 to RS_F7 do
  1885. if r in regs then
  1886. begin
  1887. if firstfloatreg=RS_NO then
  1888. firstfloatreg:=r;
  1889. lastfloatreg:=r;
  1890. { floating point register space is already included in
  1891. localsize below by calc_stackframe_size
  1892. inc(registerarea,12);
  1893. }
  1894. end;
  1895. end;
  1896. fpu_vfpv2,
  1897. fpu_vfpv3,
  1898. fpu_vfpv3_d16:
  1899. begin;
  1900. { restore vfp registers? }
  1901. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1902. end;
  1903. end;
  1904. if (firstfloatreg<>RS_NO) or
  1905. (mmregs<>[]) then
  1906. begin
  1907. reference_reset(ref,4);
  1908. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1909. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1910. begin
  1911. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1912. begin
  1913. a_reg_alloc(list,NR_R12);
  1914. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1915. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1916. a_reg_dealloc(list,NR_R12);
  1917. end
  1918. else
  1919. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1920. ref.base:=NR_R12;
  1921. end
  1922. else
  1923. begin
  1924. ref.base:=current_procinfo.framepointer;
  1925. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1926. end;
  1927. case current_settings.fputype of
  1928. fpu_fpa,
  1929. fpu_fpa10,
  1930. fpu_fpa11:
  1931. begin
  1932. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1933. lastfloatreg-firstfloatreg+1,ref));
  1934. end;
  1935. fpu_vfpv2,
  1936. fpu_vfpv3,
  1937. fpu_vfpv3_d16:
  1938. begin
  1939. ref.index:=ref.base;
  1940. ref.base:=NR_NO;
  1941. { FLDMX is deprecated on ARMv6 and later }
  1942. if (current_settings.cputype<cpu_armv6) then
  1943. mmpostfix:=PF_IAX
  1944. else
  1945. mmpostfix:=PF_IAD;
  1946. list.concat(setoppostfix(taicpu.op_ref_regset(A_FLDM,ref,R_MMREGISTER,R_SUBFD,mmregs),mmpostfix));
  1947. end;
  1948. end;
  1949. end;
  1950. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1951. if (pi_do_call in current_procinfo.flags) or
  1952. (regs<>[]) or
  1953. ((target_info.system in systems_darwin) and
  1954. (current_procinfo.framepointer<>NR_STACK_POINTER_REG)) then
  1955. begin
  1956. exclude(regs,RS_R14);
  1957. include(regs,RS_R15);
  1958. if (target_info.system in systems_darwin) then
  1959. include(regs,RS_FRAME_POINTER_REG);
  1960. end;
  1961. if not(target_info.system in systems_darwin) then
  1962. begin
  1963. { restore saved stack pointer to SP (R13) and saved lr to PC (R15).
  1964. The saved PC came after that but is discarded, since we restore
  1965. the stack pointer }
  1966. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  1967. regs:=regs+[RS_FRAME_POINTER_REG,RS_R13,RS_R15];
  1968. end
  1969. else
  1970. begin
  1971. { restore R8-R11 already if necessary (they've been stored
  1972. before the others) }
  1973. saveregs:=regs*[RS_R8,RS_R10,RS_R11];
  1974. if saveregs<>[] then
  1975. begin
  1976. reference_reset(ref,4);
  1977. ref.index:=NR_STACK_POINTER_REG;
  1978. ref.addressmode:=AM_PREINDEXED;
  1979. for r:=RS_R8 to RS_R11 do
  1980. if r in saveregs then
  1981. inc(registerarea,4);
  1982. regs:=regs-saveregs;
  1983. end;
  1984. end;
  1985. for r:=RS_R0 to RS_R15 do
  1986. if r in regs then
  1987. inc(registerarea,4);
  1988. { reapply the stack padding reg, in case there was one, see the complimentary
  1989. comment in g_proc_entry() (KB) }
  1990. paddingreg:=tarmprocinfo(current_procinfo).stackpaddingreg;
  1991. if paddingreg < RS_R4 then
  1992. if paddingreg in regs then
  1993. internalerror(201306190)
  1994. else
  1995. begin
  1996. regs:=regs+[paddingreg];
  1997. inc(registerarea,4);
  1998. end;
  1999. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  2000. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  2001. (target_info.system in systems_darwin) then
  2002. begin
  2003. LocalSize:=current_procinfo.calc_stackframe_size;
  2004. if (LocalSize<>0) or
  2005. ((stackmisalignment<>0) and
  2006. ((pi_do_call in current_procinfo.flags) or
  2007. (po_assembler in current_procinfo.procdef.procoptions))) then
  2008. begin
  2009. if pi_estimatestacksize in current_procinfo.flags then
  2010. LocalSize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  2011. else
  2012. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  2013. if is_shifter_const(LocalSize,shift) then
  2014. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  2015. else if split_into_shifter_const(localsize, imm1, imm2) then
  2016. begin
  2017. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  2018. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  2019. end
  2020. else
  2021. begin
  2022. a_reg_alloc(list,NR_R12);
  2023. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  2024. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  2025. a_reg_dealloc(list,NR_R12);
  2026. end;
  2027. end;
  2028. if (target_info.system in systems_darwin) and
  2029. (saveregs<>[]) then
  2030. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  2031. if regs=[] then
  2032. begin
  2033. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2034. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2035. else
  2036. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2037. end
  2038. else
  2039. begin
  2040. reference_reset(ref,4);
  2041. ref.index:=NR_STACK_POINTER_REG;
  2042. ref.addressmode:=AM_PREINDEXED;
  2043. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  2044. end;
  2045. end
  2046. else
  2047. begin
  2048. { restore int registers and return }
  2049. reference_reset(ref,4);
  2050. ref.index:=NR_FRAME_POINTER_REG;
  2051. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_EA));
  2052. end;
  2053. end
  2054. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2055. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2056. else
  2057. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2058. end;
  2059. procedure tbasecgarm.g_maybe_got_init(list : TAsmList);
  2060. var
  2061. ref : treference;
  2062. l : TAsmLabel;
  2063. begin
  2064. if (cs_create_pic in current_settings.moduleswitches) and
  2065. (pi_needs_got in current_procinfo.flags) and
  2066. (tf_pic_uses_got in target_info.flags) then
  2067. begin
  2068. reference_reset(ref,4);
  2069. current_asmdata.getdatalabel(l);
  2070. cg.a_label(current_procinfo.aktlocaldata,l);
  2071. ref.symbol:=l;
  2072. ref.base:=NR_PC;
  2073. ref.symboldata:=current_procinfo.aktlocaldata.last;
  2074. list.concat(Taicpu.op_reg_ref(A_LDR,current_procinfo.got,ref));
  2075. current_asmdata.getaddrlabel(l);
  2076. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_32bit,l,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),-8));
  2077. cg.a_label(list,l);
  2078. list.concat(Taicpu.op_reg_reg_reg(A_ADD,current_procinfo.got,NR_PC,current_procinfo.got));
  2079. end;
  2080. end;
  2081. procedure tbasecgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  2082. var
  2083. b : byte;
  2084. tmpref : treference;
  2085. instr : taicpu;
  2086. begin
  2087. if ref.addressmode<>AM_OFFSET then
  2088. internalerror(200309071);
  2089. tmpref:=ref;
  2090. { Be sure to have a base register }
  2091. if (tmpref.base=NR_NO) then
  2092. begin
  2093. if tmpref.shiftmode<>SM_None then
  2094. internalerror(2014020702);
  2095. if tmpref.signindex<0 then
  2096. internalerror(200312023);
  2097. tmpref.base:=tmpref.index;
  2098. tmpref.index:=NR_NO;
  2099. end;
  2100. if assigned(tmpref.symbol) or
  2101. not((is_shifter_const(tmpref.offset,b)) or
  2102. (is_shifter_const(-tmpref.offset,b))
  2103. ) then
  2104. fixref(list,tmpref);
  2105. { expect a base here if there is an index }
  2106. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  2107. internalerror(200312022);
  2108. if tmpref.index<>NR_NO then
  2109. begin
  2110. if tmpref.shiftmode<>SM_None then
  2111. internalerror(200312021);
  2112. if tmpref.signindex<0 then
  2113. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  2114. else
  2115. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  2116. if tmpref.offset<>0 then
  2117. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  2118. end
  2119. else
  2120. begin
  2121. if tmpref.base=NR_NO then
  2122. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  2123. else
  2124. if tmpref.offset<>0 then
  2125. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  2126. else
  2127. begin
  2128. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  2129. list.concat(instr);
  2130. add_move_instruction(instr);
  2131. end;
  2132. end;
  2133. end;
  2134. procedure tbasecgarm.fixref(list : TAsmList;var ref : treference);
  2135. var
  2136. tmpreg, tmpreg2 : tregister;
  2137. tmpref : treference;
  2138. l, piclabel : tasmlabel;
  2139. indirection_done : boolean;
  2140. begin
  2141. { absolute symbols can't be handled directly, we've to store the symbol reference
  2142. in the text segment and access it pc relative
  2143. For now, we assume that references where base or index equals to PC are already
  2144. relative, all other references are assumed to be absolute and thus they need
  2145. to be handled extra.
  2146. A proper solution would be to change refoptions to a set and store the information
  2147. if the symbol is absolute or relative there.
  2148. }
  2149. { create consts entry }
  2150. reference_reset(tmpref,4);
  2151. current_asmdata.getjumplabel(l);
  2152. cg.a_label(current_procinfo.aktlocaldata,l);
  2153. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2154. piclabel:=nil;
  2155. tmpreg:=NR_NO;
  2156. indirection_done:=false;
  2157. if assigned(ref.symbol) then
  2158. begin
  2159. if (target_info.system=system_arm_darwin) and
  2160. (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) then
  2161. begin
  2162. tmpreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  2163. if ref.offset<>0 then
  2164. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg);
  2165. indirection_done:=true;
  2166. end
  2167. else if (cs_create_pic in current_settings.moduleswitches) then
  2168. if (tf_pic_uses_got in target_info.flags) then
  2169. current_procinfo.aktlocaldata.concat(tai_const.Create_type_sym_offset(aitconst_got,ref.symbol,ref.offset))
  2170. else
  2171. begin
  2172. { ideally, we would want to generate
  2173. ldr r1, LPICConstPool
  2174. LPICLocal:
  2175. ldr/str r2,[pc,r1]
  2176. ...
  2177. LPICConstPool:
  2178. .long _globsym-(LPICLocal+8)
  2179. However, we cannot be sure that the ldr/str will follow
  2180. right after the call to fixref, so we have to load the
  2181. complete address already in a register.
  2182. }
  2183. current_asmdata.getaddrlabel(piclabel);
  2184. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,ref.symbol,ref.offset-8));
  2185. end
  2186. else
  2187. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  2188. end
  2189. else
  2190. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  2191. { load consts entry }
  2192. if not indirection_done then
  2193. begin
  2194. tmpreg:=getintregister(list,OS_INT);
  2195. tmpref.symbol:=l;
  2196. tmpref.base:=NR_PC;
  2197. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2198. if (cs_create_pic in current_settings.moduleswitches) and
  2199. (tf_pic_uses_got in target_info.flags) and
  2200. assigned(ref.symbol) then
  2201. begin
  2202. reference_reset(tmpref,4);
  2203. tmpref.base:=current_procinfo.got;
  2204. tmpref.index:=tmpreg;
  2205. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2206. end;
  2207. end;
  2208. if assigned(piclabel) then
  2209. begin
  2210. cg.a_label(list,piclabel);
  2211. tmpreg2:=getaddressregister(list);
  2212. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpreg,NR_PC,tmpreg2);
  2213. tmpreg:=tmpreg2
  2214. end;
  2215. { This routine can be called with PC as base/index in case the offset
  2216. was too large to encode in a load/store. In that case, the entire
  2217. absolute expression has been re-encoded in a new constpool entry, and
  2218. we have to remove the use of PC from the original reference (the code
  2219. above made everything relative to the value loaded from the new
  2220. constpool entry) }
  2221. if is_pc(ref.base) then
  2222. ref.base:=NR_NO;
  2223. if is_pc(ref.index) then
  2224. ref.index:=NR_NO;
  2225. if (ref.base<>NR_NO) then
  2226. begin
  2227. if ref.index<>NR_NO then
  2228. begin
  2229. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  2230. ref.base:=tmpreg;
  2231. end
  2232. else
  2233. if ref.base<>NR_PC then
  2234. begin
  2235. ref.index:=tmpreg;
  2236. ref.shiftimm:=0;
  2237. ref.signindex:=1;
  2238. ref.shiftmode:=SM_None;
  2239. end
  2240. else
  2241. ref.base:=tmpreg;
  2242. end
  2243. else
  2244. ref.base:=tmpreg;
  2245. ref.offset:=0;
  2246. ref.symbol:=nil;
  2247. end;
  2248. procedure tbasecgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  2249. var
  2250. paraloc1,paraloc2,paraloc3 : TCGPara;
  2251. pd : tprocdef;
  2252. begin
  2253. pd:=search_system_proc('MOVE');
  2254. paraloc1.init;
  2255. paraloc2.init;
  2256. paraloc3.init;
  2257. paramanager.getintparaloc(pd,1,paraloc1);
  2258. paramanager.getintparaloc(pd,2,paraloc2);
  2259. paramanager.getintparaloc(pd,3,paraloc3);
  2260. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  2261. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  2262. a_loadaddr_ref_cgpara(list,source,paraloc1);
  2263. paramanager.freecgpara(list,paraloc3);
  2264. paramanager.freecgpara(list,paraloc2);
  2265. paramanager.freecgpara(list,paraloc1);
  2266. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2267. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2268. a_call_name(list,'FPC_MOVE',false);
  2269. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2270. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2271. paraloc3.done;
  2272. paraloc2.done;
  2273. paraloc1.done;
  2274. end;
  2275. procedure tbasecgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  2276. const
  2277. 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}
  2278. maxtmpreg_thumb = 5;
  2279. var
  2280. srcref,dstref,usedtmpref,usedtmpref2:treference;
  2281. srcreg,destreg,countreg,r,tmpreg:tregister;
  2282. helpsize:aint;
  2283. copysize:byte;
  2284. cgsize:Tcgsize;
  2285. tmpregisters:array[1..maxtmpreg_arm] of tregister;
  2286. maxtmpreg,
  2287. tmpregi,tmpregi2:byte;
  2288. { will never be called with count<=4 }
  2289. procedure genloop(count : aword;size : byte);
  2290. const
  2291. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2292. var
  2293. l : tasmlabel;
  2294. begin
  2295. current_asmdata.getjumplabel(l);
  2296. if count<size then size:=1;
  2297. a_load_const_reg(list,OS_INT,count div size,countreg);
  2298. cg.a_label(list,l);
  2299. srcref.addressmode:=AM_POSTINDEXED;
  2300. dstref.addressmode:=AM_POSTINDEXED;
  2301. srcref.offset:=size;
  2302. dstref.offset:=size;
  2303. r:=getintregister(list,size2opsize[size]);
  2304. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2305. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2306. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  2307. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2308. a_jmp_flags(list,F_NE,l);
  2309. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2310. srcref.offset:=1;
  2311. dstref.offset:=1;
  2312. case count mod size of
  2313. 1:
  2314. begin
  2315. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2316. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2317. end;
  2318. 2:
  2319. if aligned then
  2320. begin
  2321. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2322. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2323. end
  2324. else
  2325. begin
  2326. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2327. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2328. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2329. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2330. end;
  2331. 3:
  2332. if aligned then
  2333. begin
  2334. srcref.offset:=2;
  2335. dstref.offset:=2;
  2336. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2337. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2338. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2339. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2340. end
  2341. else
  2342. begin
  2343. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2344. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2345. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2346. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2347. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2348. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2349. end;
  2350. end;
  2351. { keep the registers alive }
  2352. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2353. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2354. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2355. end;
  2356. { will never be called with count<=4 }
  2357. procedure genloop_thumb(count : aword;size : byte);
  2358. procedure refincofs(const ref : treference;const value : longint = 1);
  2359. begin
  2360. a_op_const_reg(list,OP_ADD,OS_ADDR,value,ref.base);
  2361. end;
  2362. const
  2363. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2364. var
  2365. l : tasmlabel;
  2366. begin
  2367. current_asmdata.getjumplabel(l);
  2368. if count<size then size:=1;
  2369. a_load_const_reg(list,OS_INT,count div size,countreg);
  2370. cg.a_label(list,l);
  2371. r:=getintregister(list,size2opsize[size]);
  2372. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2373. refincofs(srcref);
  2374. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2375. refincofs(dstref);
  2376. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2377. list.concat(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1));
  2378. a_jmp_flags(list,F_NE,l);
  2379. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2380. case count mod size of
  2381. 1:
  2382. begin
  2383. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2384. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2385. end;
  2386. 2:
  2387. if aligned then
  2388. begin
  2389. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2390. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2391. end
  2392. else
  2393. begin
  2394. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2395. refincofs(srcref);
  2396. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2397. refincofs(dstref);
  2398. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2399. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2400. end;
  2401. 3:
  2402. if aligned then
  2403. begin
  2404. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2405. refincofs(srcref,2);
  2406. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2407. refincofs(dstref,2);
  2408. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2409. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2410. end
  2411. else
  2412. begin
  2413. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2414. refincofs(srcref);
  2415. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2416. refincofs(dstref);
  2417. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2418. refincofs(srcref);
  2419. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2420. refincofs(dstref);
  2421. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2422. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2423. end;
  2424. end;
  2425. { keep the registers alive }
  2426. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2427. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2428. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2429. end;
  2430. begin
  2431. if len=0 then
  2432. exit;
  2433. if GenerateThumbCode then
  2434. maxtmpreg:=maxtmpreg_thumb
  2435. else
  2436. maxtmpreg:=maxtmpreg_arm;
  2437. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  2438. dstref:=dest;
  2439. srcref:=source;
  2440. if cs_opt_size in current_settings.optimizerswitches then
  2441. helpsize:=8;
  2442. if aligned and (len=4) then
  2443. begin
  2444. tmpreg:=getintregister(list,OS_32);
  2445. a_load_ref_reg(list,OS_32,OS_32,source,tmpreg);
  2446. a_load_reg_ref(list,OS_32,OS_32,tmpreg,dest);
  2447. end
  2448. else if aligned and (len=2) then
  2449. begin
  2450. tmpreg:=getintregister(list,OS_16);
  2451. a_load_ref_reg(list,OS_16,OS_16,source,tmpreg);
  2452. a_load_reg_ref(list,OS_16,OS_16,tmpreg,dest);
  2453. end
  2454. else if (len<=helpsize) and aligned then
  2455. begin
  2456. tmpregi:=0;
  2457. srcreg:=getintregister(list,OS_ADDR);
  2458. { explicit pc relative addressing, could be
  2459. e.g. a floating point constant }
  2460. if source.base=NR_PC then
  2461. begin
  2462. { ... then we don't need a loadaddr }
  2463. srcref:=source;
  2464. end
  2465. else
  2466. begin
  2467. a_loadaddr_ref_reg(list,source,srcreg);
  2468. reference_reset_base(srcref,srcreg,0,source.alignment);
  2469. end;
  2470. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  2471. begin
  2472. inc(tmpregi);
  2473. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  2474. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  2475. inc(srcref.offset,4);
  2476. dec(len,4);
  2477. end;
  2478. destreg:=getintregister(list,OS_ADDR);
  2479. a_loadaddr_ref_reg(list,dest,destreg);
  2480. reference_reset_base(dstref,destreg,0,dest.alignment);
  2481. tmpregi2:=1;
  2482. while (tmpregi2<=tmpregi) do
  2483. begin
  2484. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  2485. inc(dstref.offset,4);
  2486. inc(tmpregi2);
  2487. end;
  2488. copysize:=4;
  2489. cgsize:=OS_32;
  2490. while len<>0 do
  2491. begin
  2492. if len<2 then
  2493. begin
  2494. copysize:=1;
  2495. cgsize:=OS_8;
  2496. end
  2497. else if len<4 then
  2498. begin
  2499. copysize:=2;
  2500. cgsize:=OS_16;
  2501. end;
  2502. dec(len,copysize);
  2503. r:=getintregister(list,cgsize);
  2504. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2505. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2506. inc(srcref.offset,copysize);
  2507. inc(dstref.offset,copysize);
  2508. end;{end of while}
  2509. end
  2510. else
  2511. begin
  2512. cgsize:=OS_32;
  2513. if (len<=4) then{len<=4 and not aligned}
  2514. begin
  2515. r:=getintregister(list,cgsize);
  2516. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2517. if Len=1 then
  2518. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  2519. else
  2520. begin
  2521. tmpreg:=getintregister(list,cgsize);
  2522. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2523. inc(usedtmpref.offset,1);
  2524. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2525. inc(usedtmpref2.offset,1);
  2526. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2527. if len>2 then
  2528. begin
  2529. inc(usedtmpref.offset,1);
  2530. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2531. inc(usedtmpref2.offset,1);
  2532. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2533. if len>3 then
  2534. begin
  2535. inc(usedtmpref.offset,1);
  2536. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2537. inc(usedtmpref2.offset,1);
  2538. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2539. end;
  2540. end;
  2541. end;
  2542. end{end of if len<=4}
  2543. else
  2544. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  2545. destreg:=getintregister(list,OS_ADDR);
  2546. a_loadaddr_ref_reg(list,dest,destreg);
  2547. reference_reset_base(dstref,destreg,0,dest.alignment);
  2548. srcreg:=getintregister(list,OS_ADDR);
  2549. a_loadaddr_ref_reg(list,source,srcreg);
  2550. reference_reset_base(srcref,srcreg,0,source.alignment);
  2551. countreg:=getintregister(list,OS_32);
  2552. // if cs_opt_size in current_settings.optimizerswitches then
  2553. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  2554. {if aligned then
  2555. genloop(len,4)
  2556. else}
  2557. if GenerateThumbCode then
  2558. genloop_thumb(len,1)
  2559. else
  2560. genloop(len,1);
  2561. end;
  2562. end;
  2563. end;
  2564. procedure tbasecgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2565. begin
  2566. g_concatcopy_internal(list,source,dest,len,false);
  2567. end;
  2568. procedure tbasecgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  2569. begin
  2570. if (source.alignment in [1,3]) or
  2571. (dest.alignment in [1,3]) then
  2572. g_concatcopy_internal(list,source,dest,len,false)
  2573. else
  2574. g_concatcopy_internal(list,source,dest,len,true);
  2575. end;
  2576. procedure tbasecgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  2577. var
  2578. ovloc : tlocation;
  2579. begin
  2580. ovloc.loc:=LOC_VOID;
  2581. g_overflowCheck_loc(list,l,def,ovloc);
  2582. end;
  2583. procedure tbasecgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2584. var
  2585. hl : tasmlabel;
  2586. ai:TAiCpu;
  2587. hflags : tresflags;
  2588. begin
  2589. if not(cs_check_overflow in current_settings.localswitches) then
  2590. exit;
  2591. current_asmdata.getjumplabel(hl);
  2592. case ovloc.loc of
  2593. LOC_VOID:
  2594. begin
  2595. ai:=taicpu.op_sym(A_B,hl);
  2596. ai.is_jmp:=true;
  2597. if not((def.typ=pointerdef) or
  2598. ((def.typ=orddef) and
  2599. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2600. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2601. ai.SetCondition(C_VC)
  2602. else
  2603. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  2604. ai.SetCondition(C_CS)
  2605. else
  2606. ai.SetCondition(C_CC);
  2607. list.concat(ai);
  2608. end;
  2609. LOC_FLAGS:
  2610. begin
  2611. hflags:=ovloc.resflags;
  2612. inverse_flags(hflags);
  2613. cg.a_jmp_flags(list,hflags,hl);
  2614. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2615. end;
  2616. else
  2617. internalerror(200409281);
  2618. end;
  2619. a_call_name(list,'FPC_OVERFLOW',false);
  2620. a_label(list,hl);
  2621. end;
  2622. procedure tbasecgarm.g_save_registers(list : TAsmList);
  2623. begin
  2624. { this work is done in g_proc_entry }
  2625. end;
  2626. procedure tbasecgarm.g_restore_registers(list : TAsmList);
  2627. begin
  2628. { this work is done in g_proc_exit }
  2629. end;
  2630. procedure tbasecgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2631. var
  2632. ai : taicpu;
  2633. hlabel : TAsmLabel;
  2634. begin
  2635. if GenerateThumbCode then
  2636. begin
  2637. { the optimizer has to fix this if jump range is sufficient short }
  2638. current_asmdata.getjumplabel(hlabel);
  2639. ai:=Taicpu.Op_sym(A_B,hlabel);
  2640. ai.SetCondition(inverse_cond(OpCmp2AsmCond[cond]));
  2641. ai.is_jmp:=true;
  2642. list.concat(ai);
  2643. a_jmp_always(list,l);
  2644. a_label(list,hlabel);
  2645. end
  2646. else
  2647. begin
  2648. ai:=Taicpu.Op_sym(A_B,l);
  2649. ai.SetCondition(OpCmp2AsmCond[cond]);
  2650. ai.is_jmp:=true;
  2651. list.concat(ai);
  2652. end;
  2653. end;
  2654. procedure tbasecgarm.g_stackpointer_alloc(list: TAsmList; size: longint);
  2655. begin
  2656. internalerror(200807237);
  2657. end;
  2658. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  2659. const
  2660. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  2661. (A_FCPYS,A_FCVTSD,A_NONE,A_NONE,A_NONE),
  2662. (A_FCVTDS,A_FCPYD,A_NONE,A_NONE,A_NONE),
  2663. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2664. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2665. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  2666. begin
  2667. result:=convertop[fromsize,tosize];
  2668. if result=A_NONE then
  2669. internalerror(200312205);
  2670. end;
  2671. procedure tbasecgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2672. var
  2673. instr: taicpu;
  2674. begin
  2675. if shuffle=nil then
  2676. begin
  2677. if fromsize=tosize then
  2678. { needs correct size in case of spilling }
  2679. case fromsize of
  2680. OS_F32:
  2681. instr:=taicpu.op_reg_reg(A_FCPYS,reg2,reg1);
  2682. OS_F64:
  2683. instr:=taicpu.op_reg_reg(A_FCPYD,reg2,reg1);
  2684. else
  2685. internalerror(2009112405);
  2686. end
  2687. else
  2688. internalerror(2009112406);
  2689. end
  2690. else if shufflescalar(shuffle) then
  2691. instr:=taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1)
  2692. else
  2693. internalerror(2009112407);
  2694. list.concat(instr);
  2695. case instr.opcode of
  2696. A_FCPYS,
  2697. A_FCPYD:
  2698. add_move_instruction(instr);
  2699. end;
  2700. end;
  2701. procedure tbasecgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2702. var
  2703. intreg,
  2704. tmpmmreg : tregister;
  2705. reg64 : tregister64;
  2706. op : tasmop;
  2707. begin
  2708. if assigned(shuffle) and
  2709. not(shufflescalar(shuffle)) then
  2710. internalerror(2009112413);
  2711. case fromsize of
  2712. OS_32,OS_S32:
  2713. begin
  2714. fromsize:=OS_F32;
  2715. { since we are loading an integer, no conversion may be required }
  2716. if (fromsize<>tosize) then
  2717. internalerror(2009112801);
  2718. end;
  2719. OS_64,OS_S64:
  2720. begin
  2721. fromsize:=OS_F64;
  2722. { since we are loading an integer, no conversion may be required }
  2723. if (fromsize<>tosize) then
  2724. internalerror(2009112901);
  2725. end;
  2726. end;
  2727. if (fromsize<>tosize) then
  2728. tmpmmreg:=getmmregister(list,fromsize)
  2729. else
  2730. tmpmmreg:=reg;
  2731. if (ref.alignment in [1,2]) then
  2732. begin
  2733. case fromsize of
  2734. OS_F32:
  2735. begin
  2736. intreg:=getintregister(list,OS_32);
  2737. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2738. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2739. end;
  2740. OS_F64:
  2741. begin
  2742. reg64.reglo:=getintregister(list,OS_32);
  2743. reg64.reghi:=getintregister(list,OS_32);
  2744. cg64.a_load64_ref_reg(list,ref,reg64);
  2745. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2746. end;
  2747. else
  2748. internalerror(2009112412);
  2749. end;
  2750. end
  2751. else
  2752. begin
  2753. case fromsize of
  2754. OS_F32:
  2755. op:=A_FLDS;
  2756. OS_F64:
  2757. op:=A_FLDD;
  2758. else
  2759. internalerror(2009112415);
  2760. end;
  2761. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2762. end;
  2763. if (tmpmmreg<>reg) then
  2764. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2765. end;
  2766. procedure tbasecgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2767. var
  2768. intreg,
  2769. tmpmmreg : tregister;
  2770. reg64 : tregister64;
  2771. op : tasmop;
  2772. begin
  2773. if assigned(shuffle) and
  2774. not(shufflescalar(shuffle)) then
  2775. internalerror(2009112416);
  2776. case tosize of
  2777. OS_32,OS_S32:
  2778. begin
  2779. tosize:=OS_F32;
  2780. { since we are loading an integer, no conversion may be required }
  2781. if (fromsize<>tosize) then
  2782. internalerror(2009112801);
  2783. end;
  2784. OS_64,OS_S64:
  2785. begin
  2786. tosize:=OS_F64;
  2787. { since we are loading an integer, no conversion may be required }
  2788. if (fromsize<>tosize) then
  2789. internalerror(2009112901);
  2790. end;
  2791. end;
  2792. if (fromsize<>tosize) then
  2793. begin
  2794. tmpmmreg:=getmmregister(list,tosize);
  2795. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2796. end
  2797. else
  2798. tmpmmreg:=reg;
  2799. if (ref.alignment in [1,2]) then
  2800. begin
  2801. case tosize of
  2802. OS_F32:
  2803. begin
  2804. intreg:=getintregister(list,OS_32);
  2805. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2806. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2807. end;
  2808. OS_F64:
  2809. begin
  2810. reg64.reglo:=getintregister(list,OS_32);
  2811. reg64.reghi:=getintregister(list,OS_32);
  2812. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2813. cg64.a_load64_reg_ref(list,reg64,ref);
  2814. end;
  2815. else
  2816. internalerror(2009112417);
  2817. end;
  2818. end
  2819. else
  2820. begin
  2821. case fromsize of
  2822. OS_F32:
  2823. op:=A_FSTS;
  2824. OS_F64:
  2825. op:=A_FSTD;
  2826. else
  2827. internalerror(2009112418);
  2828. end;
  2829. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2830. end;
  2831. end;
  2832. procedure tbasecgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2833. begin
  2834. { this code can only be used to transfer raw data, not to perform
  2835. conversions }
  2836. if (tosize<>OS_F32) then
  2837. internalerror(2009112419);
  2838. if not(fromsize in [OS_32,OS_S32]) then
  2839. internalerror(2009112420);
  2840. if assigned(shuffle) and
  2841. not shufflescalar(shuffle) then
  2842. internalerror(2009112516);
  2843. list.concat(taicpu.op_reg_reg(A_FMSR,mmreg,intreg));
  2844. end;
  2845. procedure tbasecgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2846. begin
  2847. { this code can only be used to transfer raw data, not to perform
  2848. conversions }
  2849. if (fromsize<>OS_F32) then
  2850. internalerror(2009112430);
  2851. if not(tosize in [OS_32,OS_S32]) then
  2852. internalerror(2009112420);
  2853. if assigned(shuffle) and
  2854. not shufflescalar(shuffle) then
  2855. internalerror(2009112514);
  2856. list.concat(taicpu.op_reg_reg(A_FMRS,intreg,mmreg));
  2857. end;
  2858. procedure tbasecgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2859. var
  2860. tmpreg: tregister;
  2861. begin
  2862. { the vfp doesn't support xor nor any other logical operation, but
  2863. this routine is used to initialise global mm regvars. We can
  2864. easily initialise an mm reg with 0 though. }
  2865. case op of
  2866. OP_XOR:
  2867. begin
  2868. if (src<>dst) or
  2869. (reg_cgsize(src)<>size) or
  2870. assigned(shuffle) then
  2871. internalerror(2009112907);
  2872. tmpreg:=getintregister(list,OS_32);
  2873. a_load_const_reg(list,OS_32,0,tmpreg);
  2874. case size of
  2875. OS_F32:
  2876. list.concat(taicpu.op_reg_reg(A_FMSR,dst,tmpreg));
  2877. OS_F64:
  2878. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,dst,tmpreg,tmpreg));
  2879. else
  2880. internalerror(2009112908);
  2881. end;
  2882. end
  2883. else
  2884. internalerror(2009112906);
  2885. end;
  2886. end;
  2887. procedure tbasecgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2888. procedure loadvmttor12;
  2889. var
  2890. tmpref,
  2891. href : treference;
  2892. extrareg : boolean;
  2893. l : TAsmLabel;
  2894. begin
  2895. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2896. if GenerateThumbCode then
  2897. begin
  2898. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2899. begin
  2900. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2901. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2902. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2903. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2904. end
  2905. else
  2906. begin
  2907. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2908. { create consts entry }
  2909. reference_reset(tmpref,4);
  2910. current_asmdata.getjumplabel(l);
  2911. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2912. cg.a_label(current_procinfo.aktlocaldata,l);
  2913. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2914. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2915. tmpref.symbol:=l;
  2916. tmpref.base:=NR_PC;
  2917. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2918. href.offset:=0;
  2919. href.index:=NR_R1;
  2920. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2921. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2922. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2923. end;
  2924. end
  2925. else
  2926. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2927. end;
  2928. procedure op_onr12methodaddr;
  2929. var
  2930. tmpref,
  2931. href : treference;
  2932. l : TAsmLabel;
  2933. begin
  2934. if (procdef.extnumber=$ffff) then
  2935. Internalerror(200006139);
  2936. if GenerateThumbCode then
  2937. begin
  2938. reference_reset_base(href,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2939. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2940. begin
  2941. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2942. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2943. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2944. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2945. end
  2946. else
  2947. begin
  2948. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2949. { create consts entry }
  2950. reference_reset(tmpref,4);
  2951. current_asmdata.getjumplabel(l);
  2952. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2953. cg.a_label(current_procinfo.aktlocaldata,l);
  2954. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2955. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2956. tmpref.symbol:=l;
  2957. tmpref.base:=NR_PC;
  2958. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2959. href.offset:=0;
  2960. href.index:=NR_R1;
  2961. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2962. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2963. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2964. end;
  2965. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  2966. end
  2967. else
  2968. begin
  2969. reference_reset_base(href,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2970. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2971. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  2972. end;
  2973. end;
  2974. var
  2975. make_global : boolean;
  2976. tmpref : treference;
  2977. l : TAsmLabel;
  2978. begin
  2979. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2980. Internalerror(200006137);
  2981. if not assigned(procdef.struct) or
  2982. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2983. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2984. Internalerror(200006138);
  2985. if procdef.owner.symtabletype<>ObjectSymtable then
  2986. Internalerror(200109191);
  2987. make_global:=false;
  2988. if (not current_module.is_unit) or
  2989. create_smartlink or
  2990. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  2991. make_global:=true;
  2992. if make_global then
  2993. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  2994. else
  2995. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  2996. { the wrapper might need aktlocaldata for the additional data to
  2997. load the constant }
  2998. current_procinfo:=cprocinfo.create(nil);
  2999. { set param1 interface to self }
  3000. g_adjust_self_value(list,procdef,ioffset);
  3001. { case 4 }
  3002. if (po_virtualmethod in procdef.procoptions) and
  3003. not is_objectpascal_helper(procdef.struct) then
  3004. begin
  3005. loadvmttor12;
  3006. op_onr12methodaddr;
  3007. end
  3008. { case 0 }
  3009. else if GenerateThumbCode then
  3010. begin
  3011. { bl cannot be used here because it destroys lr }
  3012. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3013. { create consts entry }
  3014. reference_reset(tmpref,4);
  3015. current_asmdata.getjumplabel(l);
  3016. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3017. cg.a_label(current_procinfo.aktlocaldata,l);
  3018. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3019. current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3020. tmpref.symbol:=l;
  3021. tmpref.base:=NR_PC;
  3022. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  3023. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3024. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3025. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  3026. end
  3027. else
  3028. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3029. list.concatlist(current_procinfo.aktlocaldata);
  3030. current_procinfo.Free;
  3031. current_procinfo:=nil;
  3032. list.concat(Tai_symbol_end.Createname(labelname));
  3033. end;
  3034. procedure tbasecgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  3035. const
  3036. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  3037. begin
  3038. if (op in overflowops) and
  3039. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  3040. a_load_reg_reg(list,OS_32,size,dst,dst);
  3041. end;
  3042. procedure tbasecgarm.safe_mla(list : TAsmList; op1,op2,op3,op4 : TRegister);
  3043. procedure checkreg(var reg : TRegister);
  3044. var
  3045. tmpreg : TRegister;
  3046. begin
  3047. if ((GenerateThumbCode or GenerateThumb2Code) and (getsupreg(reg)=RS_R13)) or
  3048. (getsupreg(reg)=RS_R15) then
  3049. begin
  3050. tmpreg:=getintregister(list,OS_INT);
  3051. a_load_reg_reg(list,OS_INT,OS_INT,reg,tmpreg);
  3052. reg:=tmpreg;
  3053. end;
  3054. end;
  3055. begin
  3056. checkreg(op1);
  3057. checkreg(op2);
  3058. checkreg(op3);
  3059. checkreg(op4);
  3060. list.concat(taicpu.op_reg_reg_reg_reg(A_MLA,op1,op2,op3,op4));
  3061. end;
  3062. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3063. begin
  3064. case op of
  3065. OP_NEG:
  3066. begin
  3067. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3068. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3069. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  3070. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3071. end;
  3072. OP_NOT:
  3073. begin
  3074. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  3075. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  3076. end;
  3077. else
  3078. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  3079. end;
  3080. end;
  3081. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  3082. begin
  3083. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  3084. end;
  3085. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  3086. var
  3087. ovloc : tlocation;
  3088. begin
  3089. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  3090. end;
  3091. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3092. var
  3093. ovloc : tlocation;
  3094. begin
  3095. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  3096. end;
  3097. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  3098. begin
  3099. { this code can only be used to transfer raw data, not to perform
  3100. conversions }
  3101. if (mmsize<>OS_F64) then
  3102. internalerror(2009112405);
  3103. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,mmreg,intreg.reglo,intreg.reghi));
  3104. end;
  3105. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  3106. begin
  3107. { this code can only be used to transfer raw data, not to perform
  3108. conversions }
  3109. if (mmsize<>OS_F64) then
  3110. internalerror(2009112406);
  3111. list.concat(taicpu.op_reg_reg_reg(A_FMRRD,intreg.reglo,intreg.reghi,mmreg));
  3112. end;
  3113. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3114. var
  3115. tmpreg : tregister;
  3116. b : byte;
  3117. begin
  3118. ovloc.loc:=LOC_VOID;
  3119. case op of
  3120. OP_NEG,
  3121. OP_NOT :
  3122. internalerror(2012022501);
  3123. end;
  3124. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3125. begin
  3126. case op of
  3127. OP_ADD:
  3128. begin
  3129. if is_shifter_const(lo(value),b) then
  3130. begin
  3131. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3132. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3133. end
  3134. else
  3135. begin
  3136. tmpreg:=cg.getintregister(list,OS_32);
  3137. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3138. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3139. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3140. end;
  3141. if is_shifter_const(hi(value),b) then
  3142. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  3143. else
  3144. begin
  3145. tmpreg:=cg.getintregister(list,OS_32);
  3146. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3147. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3148. end;
  3149. end;
  3150. OP_SUB:
  3151. begin
  3152. if is_shifter_const(lo(value),b) then
  3153. begin
  3154. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3155. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3156. end
  3157. else
  3158. begin
  3159. tmpreg:=cg.getintregister(list,OS_32);
  3160. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3161. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3162. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3163. end;
  3164. if is_shifter_const(hi(value),b) then
  3165. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  3166. else
  3167. begin
  3168. tmpreg:=cg.getintregister(list,OS_32);
  3169. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3170. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3171. end;
  3172. end;
  3173. else
  3174. internalerror(200502131);
  3175. end;
  3176. if size=OS_64 then
  3177. begin
  3178. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3179. ovloc.loc:=LOC_FLAGS;
  3180. case op of
  3181. OP_ADD:
  3182. ovloc.resflags:=F_CS;
  3183. OP_SUB:
  3184. ovloc.resflags:=F_CC;
  3185. end;
  3186. end;
  3187. end
  3188. else
  3189. begin
  3190. case op of
  3191. OP_AND,OP_OR,OP_XOR:
  3192. begin
  3193. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  3194. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  3195. end;
  3196. OP_ADD:
  3197. begin
  3198. if is_shifter_const(aint(lo(value)),b) then
  3199. begin
  3200. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3201. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3202. end
  3203. else
  3204. begin
  3205. tmpreg:=cg.getintregister(list,OS_32);
  3206. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3207. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3208. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3209. end;
  3210. if is_shifter_const(aint(hi(value)),b) then
  3211. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3212. else
  3213. begin
  3214. tmpreg:=cg.getintregister(list,OS_32);
  3215. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  3216. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  3217. end;
  3218. end;
  3219. OP_SUB:
  3220. begin
  3221. if is_shifter_const(aint(lo(value)),b) then
  3222. begin
  3223. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3224. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3225. end
  3226. else
  3227. begin
  3228. tmpreg:=cg.getintregister(list,OS_32);
  3229. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3230. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3231. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3232. end;
  3233. if is_shifter_const(aint(hi(value)),b) then
  3234. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3235. else
  3236. begin
  3237. tmpreg:=cg.getintregister(list,OS_32);
  3238. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3239. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  3240. end;
  3241. end;
  3242. else
  3243. internalerror(2003083101);
  3244. end;
  3245. end;
  3246. end;
  3247. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3248. begin
  3249. ovloc.loc:=LOC_VOID;
  3250. case op of
  3251. OP_NEG,
  3252. OP_NOT :
  3253. internalerror(2012022502);
  3254. end;
  3255. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3256. begin
  3257. case op of
  3258. OP_ADD:
  3259. begin
  3260. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3261. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3262. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  3263. end;
  3264. OP_SUB:
  3265. begin
  3266. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3267. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3268. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  3269. end;
  3270. else
  3271. internalerror(2003083101);
  3272. end;
  3273. if size=OS_64 then
  3274. begin
  3275. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3276. ovloc.loc:=LOC_FLAGS;
  3277. case op of
  3278. OP_ADD:
  3279. ovloc.resflags:=F_CS;
  3280. OP_SUB:
  3281. ovloc.resflags:=F_CC;
  3282. end;
  3283. end;
  3284. end
  3285. else
  3286. begin
  3287. case op of
  3288. OP_AND,OP_OR,OP_XOR:
  3289. begin
  3290. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  3291. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  3292. end;
  3293. OP_ADD:
  3294. begin
  3295. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3296. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3297. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  3298. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3299. end;
  3300. OP_SUB:
  3301. begin
  3302. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3303. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3304. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  3305. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3306. end;
  3307. else
  3308. internalerror(2003083101);
  3309. end;
  3310. end;
  3311. end;
  3312. procedure tthumbcgarm.init_register_allocators;
  3313. begin
  3314. inherited init_register_allocators;
  3315. if assigned(current_procinfo) and (current_procinfo.framepointer=NR_R7) then
  3316. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3317. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6],first_int_imreg,[])
  3318. else
  3319. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3320. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7],first_int_imreg,[]);
  3321. end;
  3322. procedure tthumbcgarm.done_register_allocators;
  3323. begin
  3324. rg[R_INTREGISTER].free;
  3325. rg[R_FPUREGISTER].free;
  3326. rg[R_MMREGISTER].free;
  3327. inherited done_register_allocators;
  3328. end;
  3329. procedure tthumbcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3330. var
  3331. ref : treference;
  3332. shift : byte;
  3333. r : byte;
  3334. regs, saveregs : tcpuregisterset;
  3335. r7offset,
  3336. stackmisalignment : pint;
  3337. postfix: toppostfix;
  3338. registerarea,
  3339. imm1, imm2: DWord;
  3340. stack_parameters: Boolean;
  3341. begin
  3342. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3343. LocalSize:=align(LocalSize,4);
  3344. { call instruction does not put anything on the stack }
  3345. stackmisalignment:=0;
  3346. if not(nostackframe) then
  3347. begin
  3348. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3349. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3350. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3351. { save int registers }
  3352. reference_reset(ref,4);
  3353. ref.index:=NR_STACK_POINTER_REG;
  3354. ref.addressmode:=AM_PREINDEXED;
  3355. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3356. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3357. begin
  3358. //!!!! a_reg_alloc(list,NR_R12);
  3359. //!!!! list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3360. end;
  3361. { the (old) ARM APCS requires saving both the stack pointer (to
  3362. crawl the stack) and the PC (to identify the function this
  3363. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  3364. and R15 -- still needs updating for EABI and Darwin, they don't
  3365. need that }
  3366. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3367. regs:=regs+[RS_R7,RS_R14]
  3368. else
  3369. // if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3370. include(regs,RS_R14);
  3371. { safely estimate stack size }
  3372. if localsize+current_settings.alignment.localalignmax+4>508 then
  3373. begin
  3374. include(rg[R_INTREGISTER].used_in_proc,RS_R4);
  3375. include(regs,RS_R4);
  3376. end;
  3377. registerarea:=0;
  3378. if regs<>[] then
  3379. begin
  3380. for r:=RS_R0 to RS_R15 do
  3381. if r in regs then
  3382. inc(registerarea,4);
  3383. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,regs));
  3384. end;
  3385. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3386. if stack_parameters or (LocalSize<>0) or
  3387. ((stackmisalignment<>0) and
  3388. ((pi_do_call in current_procinfo.flags) or
  3389. (po_assembler in current_procinfo.procdef.procoptions))) then
  3390. begin
  3391. { do we access stack parameters?
  3392. if yes, the previously estimated stacksize must be used }
  3393. if stack_parameters then
  3394. begin
  3395. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  3396. begin
  3397. writeln(localsize);
  3398. writeln(tarmprocinfo(current_procinfo).stackframesize);
  3399. internalerror(2013040601);
  3400. end
  3401. else
  3402. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  3403. end
  3404. else
  3405. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3406. if localsize<508 then
  3407. begin
  3408. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3409. end
  3410. else if localsize<=1016 then
  3411. begin
  3412. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3413. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize-508));
  3414. end
  3415. else
  3416. begin
  3417. a_load_const_reg(list,OS_ADDR,-localsize,NR_R4);
  3418. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R4));
  3419. include(regs,RS_R4);
  3420. //!!!! if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3421. //!!!! a_reg_alloc(list,NR_R12);
  3422. //!!!! a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3423. //!!!! list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3424. //!!!! a_reg_dealloc(list,NR_R12);
  3425. end;
  3426. end;
  3427. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3428. begin
  3429. list.concat(taicpu.op_reg_reg_const(A_ADD,current_procinfo.framepointer,NR_STACK_POINTER_REG,0));
  3430. end;
  3431. end;
  3432. end;
  3433. procedure tthumbcgarm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  3434. var
  3435. ref : treference;
  3436. LocalSize : longint;
  3437. r,
  3438. shift : byte;
  3439. saveregs,
  3440. regs : tcpuregisterset;
  3441. registerarea : DWord;
  3442. stackmisalignment: pint;
  3443. imm1, imm2: DWord;
  3444. stack_parameters : Boolean;
  3445. begin
  3446. if not(nostackframe) then
  3447. begin
  3448. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3449. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3450. include(regs,RS_R15);
  3451. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3452. include(regs,getsupreg(current_procinfo.framepointer));
  3453. registerarea:=0;
  3454. for r:=RS_R0 to RS_R15 do
  3455. if r in regs then
  3456. inc(registerarea,4);
  3457. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3458. LocalSize:=current_procinfo.calc_stackframe_size;
  3459. if stack_parameters then
  3460. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  3461. else
  3462. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3463. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  3464. (target_info.system in systems_darwin) then
  3465. begin
  3466. if (LocalSize<>0) or
  3467. ((stackmisalignment<>0) and
  3468. ((pi_do_call in current_procinfo.flags) or
  3469. (po_assembler in current_procinfo.procdef.procoptions))) then
  3470. begin
  3471. if LocalSize=0 then
  3472. else if LocalSize<=508 then
  3473. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  3474. else if LocalSize<=1016 then
  3475. begin
  3476. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3477. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize-508));
  3478. end
  3479. else
  3480. begin
  3481. a_reg_alloc(list,NR_R3);
  3482. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R3);
  3483. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R3));
  3484. a_reg_dealloc(list,NR_R3);
  3485. end;
  3486. end;
  3487. if regs=[] then
  3488. begin
  3489. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3490. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3491. else
  3492. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3493. end
  3494. else
  3495. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,regs));
  3496. end;
  3497. end
  3498. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3499. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3500. else
  3501. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3502. end;
  3503. procedure tthumbcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3504. var
  3505. oppostfix:toppostfix;
  3506. usedtmpref: treference;
  3507. tmpreg,tmpreg2 : tregister;
  3508. dir : integer;
  3509. begin
  3510. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  3511. FromSize := ToSize;
  3512. case FromSize of
  3513. { signed integer registers }
  3514. OS_8:
  3515. oppostfix:=PF_B;
  3516. OS_S8:
  3517. oppostfix:=PF_SB;
  3518. OS_16:
  3519. oppostfix:=PF_H;
  3520. OS_S16:
  3521. oppostfix:=PF_SH;
  3522. OS_32,
  3523. OS_S32:
  3524. oppostfix:=PF_None;
  3525. else
  3526. InternalError(200308298);
  3527. end;
  3528. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  3529. begin
  3530. if target_info.endian=endian_big then
  3531. dir:=-1
  3532. else
  3533. dir:=1;
  3534. case FromSize of
  3535. OS_16,OS_S16:
  3536. begin
  3537. { only complicated references need an extra loadaddr }
  3538. if assigned(ref.symbol) or
  3539. (ref.index<>NR_NO) or
  3540. (ref.offset<-124) or
  3541. (ref.offset>124) or
  3542. { sometimes the compiler reused registers }
  3543. (reg=ref.index) or
  3544. (reg=ref.base) then
  3545. begin
  3546. tmpreg2:=getintregister(list,OS_INT);
  3547. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3548. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3549. end
  3550. else
  3551. usedtmpref:=ref;
  3552. if target_info.endian=endian_big then
  3553. inc(usedtmpref.offset,1);
  3554. tmpreg:=getintregister(list,OS_INT);
  3555. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3556. inc(usedtmpref.offset,dir);
  3557. if FromSize=OS_16 then
  3558. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  3559. else
  3560. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  3561. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3562. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3563. end;
  3564. OS_32,OS_S32:
  3565. begin
  3566. tmpreg:=getintregister(list,OS_INT);
  3567. { only complicated references need an extra loadaddr }
  3568. if assigned(ref.symbol) or
  3569. (ref.index<>NR_NO) or
  3570. (ref.offset<-124) or
  3571. (ref.offset>124) or
  3572. { sometimes the compiler reused registers }
  3573. (reg=ref.index) or
  3574. (reg=ref.base) then
  3575. begin
  3576. tmpreg2:=getintregister(list,OS_INT);
  3577. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3578. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3579. end
  3580. else
  3581. usedtmpref:=ref;
  3582. if ref.alignment=2 then
  3583. begin
  3584. if target_info.endian=endian_big then
  3585. inc(usedtmpref.offset,2);
  3586. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3587. inc(usedtmpref.offset,dir*2);
  3588. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3589. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3590. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3591. end
  3592. else
  3593. begin
  3594. if target_info.endian=endian_big then
  3595. inc(usedtmpref.offset,3);
  3596. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3597. inc(usedtmpref.offset,dir);
  3598. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3599. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3600. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3601. inc(usedtmpref.offset,dir);
  3602. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3603. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3604. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3605. inc(usedtmpref.offset,dir);
  3606. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3607. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,24));
  3608. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3609. end;
  3610. end
  3611. else
  3612. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3613. end;
  3614. end
  3615. else
  3616. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3617. if (fromsize=OS_S8) and (tosize = OS_16) then
  3618. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3619. end;
  3620. procedure tthumbcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3621. var
  3622. imm_shift : byte;
  3623. l : tasmlabel;
  3624. hr : treference;
  3625. begin
  3626. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3627. internalerror(2002090902);
  3628. if is_thumb_imm(a) then
  3629. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3630. else
  3631. begin
  3632. reference_reset(hr,4);
  3633. current_asmdata.getjumplabel(l);
  3634. cg.a_label(current_procinfo.aktlocaldata,l);
  3635. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3636. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3637. hr.symbol:=l;
  3638. hr.base:=NR_PC;
  3639. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3640. end;
  3641. end;
  3642. procedure tthumbcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3643. var
  3644. hsym : tsym;
  3645. href,
  3646. tmpref : treference;
  3647. paraloc : Pcgparalocation;
  3648. l : TAsmLabel;
  3649. begin
  3650. { calculate the parameter info for the procdef }
  3651. procdef.init_paraloc_info(callerside);
  3652. hsym:=tsym(procdef.parast.Find('self'));
  3653. if not(assigned(hsym) and
  3654. (hsym.typ=paravarsym)) then
  3655. internalerror(200305251);
  3656. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3657. while paraloc<>nil do
  3658. with paraloc^ do
  3659. begin
  3660. case loc of
  3661. LOC_REGISTER:
  3662. begin
  3663. if is_thumb_imm(ioffset) then
  3664. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  3665. else
  3666. begin
  3667. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3668. reference_reset(tmpref,4);
  3669. current_asmdata.getjumplabel(l);
  3670. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3671. cg.a_label(current_procinfo.aktlocaldata,l);
  3672. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3673. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3674. tmpref.symbol:=l;
  3675. tmpref.base:=NR_PC;
  3676. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3677. a_op_reg_reg(list,OP_SUB,size,NR_R4,register);
  3678. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3679. end;
  3680. end;
  3681. LOC_REFERENCE:
  3682. begin
  3683. { offset in the wrapper needs to be adjusted for the stored
  3684. return address }
  3685. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  3686. if is_thumb_imm(ioffset) then
  3687. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  3688. else
  3689. begin
  3690. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3691. reference_reset(tmpref,4);
  3692. current_asmdata.getjumplabel(l);
  3693. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3694. cg.a_label(current_procinfo.aktlocaldata,l);
  3695. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3696. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3697. tmpref.symbol:=l;
  3698. tmpref.base:=NR_PC;
  3699. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3700. a_op_reg_ref(list,OP_SUB,size,NR_R4,href);
  3701. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3702. end;
  3703. end
  3704. else
  3705. internalerror(200309189);
  3706. end;
  3707. paraloc:=next;
  3708. end;
  3709. end;
  3710. function tthumbcgarm.handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference;
  3711. var
  3712. href : treference;
  3713. tmpreg : TRegister;
  3714. begin
  3715. href:=ref;
  3716. if (op in [A_STR,A_STRB,A_STRH]) and
  3717. (abs(ref.offset)>124) then
  3718. begin
  3719. tmpreg:=getintregister(list,OS_ADDR);
  3720. a_loadaddr_ref_reg(list,ref,tmpreg);
  3721. reference_reset_base(href,tmpreg,0,ref.alignment);
  3722. end
  3723. else if (op=A_LDR) and
  3724. (oppostfix in [PF_None]) and
  3725. (ref.base<>NR_STACK_POINTER_REG) and
  3726. (abs(ref.offset)>124) then
  3727. begin
  3728. tmpreg:=getintregister(list,OS_ADDR);
  3729. a_loadaddr_ref_reg(list,ref,tmpreg);
  3730. reference_reset_base(href,tmpreg,0,ref.alignment);
  3731. end
  3732. else if (op=A_LDR) and
  3733. (oppostfix in [PF_None]) and
  3734. (ref.base=NR_STACK_POINTER_REG) and
  3735. (abs(ref.offset)>1020) then
  3736. begin
  3737. tmpreg:=getintregister(list,OS_ADDR);
  3738. a_loadaddr_ref_reg(list,ref,tmpreg);
  3739. reference_reset_base(href,tmpreg,0,ref.alignment);
  3740. end
  3741. else if (op=A_LDR) and
  3742. ((oppostfix in [PF_SH,PF_SB]) or
  3743. (abs(ref.offset)>124)) then
  3744. begin
  3745. tmpreg:=getintregister(list,OS_ADDR);
  3746. a_loadaddr_ref_reg(list,ref,tmpreg);
  3747. reference_reset_base(href,tmpreg,0,ref.alignment);
  3748. end;
  3749. Result:=inherited handle_load_store(list, op, oppostfix, reg, href);
  3750. end;
  3751. procedure tthumbcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  3752. var
  3753. tmpreg,overflowreg : tregister;
  3754. asmop : tasmop;
  3755. begin
  3756. case op of
  3757. OP_NEG:
  3758. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  3759. OP_NOT:
  3760. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  3761. OP_DIV,OP_IDIV:
  3762. internalerror(200308284);
  3763. OP_ROL:
  3764. begin
  3765. if not(size in [OS_32,OS_S32]) then
  3766. internalerror(2008072801);
  3767. { simulate ROL by ror'ing 32-value }
  3768. tmpreg:=getintregister(list,OS_32);
  3769. a_load_const_reg(list,OS_32,32,tmpreg);
  3770. list.concat(taicpu.op_reg_reg(A_SUB,tmpreg,src));
  3771. list.concat(taicpu.op_reg_reg(A_ROR,dst,src));
  3772. end;
  3773. else
  3774. begin
  3775. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3776. list.concat(setoppostfix(
  3777. taicpu.op_reg_reg(op_reg_opcg2asmop[op],dst,src),op_reg_postfix[op]));
  3778. end;
  3779. end;
  3780. maybeadjustresult(list,op,size,dst);
  3781. end;
  3782. procedure tthumbcgarm.a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);
  3783. var
  3784. tmpreg : tregister;
  3785. so : tshifterop;
  3786. l1 : longint;
  3787. imm1, imm2: DWord;
  3788. begin
  3789. //!!! ovloc.loc:=LOC_VOID;
  3790. if {$ifopt R+}(a<>-2147483648) and{$endif} {!!!!!! not setflags and } is_thumb_imm(-a) then
  3791. case op of
  3792. OP_ADD:
  3793. begin
  3794. op:=OP_SUB;
  3795. a:=aint(dword(-a));
  3796. end;
  3797. OP_SUB:
  3798. begin
  3799. op:=OP_ADD;
  3800. a:=aint(dword(-a));
  3801. end
  3802. end;
  3803. if is_thumb_imm(a) and (op in [OP_ADD,OP_SUB]) then
  3804. begin
  3805. // if cgsetflags or setflags then
  3806. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3807. list.concat(setoppostfix(
  3808. taicpu.op_reg_const(op_reg_opcg2asmop[op],dst,a),op_reg_postfix[op]));
  3809. if (cgsetflags {!!! or setflags }) and (size in [OS_8,OS_16,OS_32]) then
  3810. begin
  3811. //!!! ovloc.loc:=LOC_FLAGS;
  3812. case op of
  3813. OP_ADD:
  3814. //!!! ovloc.resflags:=F_CS;
  3815. ;
  3816. OP_SUB:
  3817. //!!! ovloc.resflags:=F_CC;
  3818. ;
  3819. end;
  3820. end;
  3821. end
  3822. else
  3823. begin
  3824. { there could be added some more sophisticated optimizations }
  3825. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  3826. a_load_reg_reg(list,size,size,dst,dst)
  3827. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3828. a_load_const_reg(list,size,0,dst)
  3829. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  3830. a_op_reg_reg(list,OP_NEG,size,dst,dst)
  3831. { we do this here instead in the peephole optimizer because
  3832. it saves us a register }
  3833. {$ifdef DUMMY}
  3834. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3835. a_op_const_reg_reg(list,OP_SHL,size,l1,dst,dst)
  3836. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3837. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3838. begin
  3839. if l1>32 then{roozbeh does this ever happen?}
  3840. internalerror(200308296);
  3841. shifterop_reset(so);
  3842. so.shiftmode:=SM_LSL;
  3843. so.shiftimm:=l1;
  3844. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,dst,so));
  3845. end
  3846. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  3847. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  3848. begin
  3849. if l1>32 then{does this ever happen?}
  3850. internalerror(201205181);
  3851. shifterop_reset(so);
  3852. so.shiftmode:=SM_LSL;
  3853. so.shiftimm:=l1;
  3854. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,dst,dst,so));
  3855. end
  3856. 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
  3857. begin
  3858. { nothing to do on success }
  3859. end
  3860. {$endif DUMMY}
  3861. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  3862. Just using mov x, #0 might allow some easier optimizations down the line. }
  3863. else if (op = OP_AND) and (dword(a)=0) then
  3864. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  3865. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  3866. else if (op = OP_AND) and (not(dword(a))=0) then
  3867. // do nothing
  3868. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  3869. broader range of shifterconstants.}
  3870. {$ifdef DUMMY}
  3871. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  3872. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,not(dword(a))))
  3873. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  3874. begin
  3875. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm1));
  3876. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  3877. end
  3878. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  3879. not(cgsetflags or setflags) and
  3880. split_into_shifter_const(a, imm1, imm2) then
  3881. begin
  3882. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm1));
  3883. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  3884. end
  3885. {$endif DUMMY}
  3886. else if (op in [OP_SHL, OP_SHR, OP_SAR]) then
  3887. begin
  3888. list.concat(taicpu.op_reg_reg_const(op_reg_opcg2asmop[op],dst,dst,a));
  3889. end
  3890. else
  3891. begin
  3892. tmpreg:=getintregister(list,size);
  3893. a_load_const_reg(list,size,a,tmpreg);
  3894. a_op_reg_reg(list,op,size,tmpreg,dst);
  3895. end;
  3896. end;
  3897. maybeadjustresult(list,op,size,dst);
  3898. end;
  3899. procedure tthumbcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  3900. begin
  3901. if (op=OP_ADD) and (src=NR_R13) and (dst<>NR_R13) and ((a mod 4)=0) and (a>0) and (a<=1020) then
  3902. list.concat(taicpu.op_reg_reg_const(A_ADD,dst,src,a))
  3903. else
  3904. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  3905. end;
  3906. procedure tthumbcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3907. var
  3908. l1,l2 : tasmlabel;
  3909. ai : taicpu;
  3910. begin
  3911. current_asmdata.getjumplabel(l1);
  3912. current_asmdata.getjumplabel(l2);
  3913. ai:=setcondition(taicpu.op_sym(A_B,l1),flags_to_cond(f));
  3914. ai.is_jmp:=true;
  3915. list.concat(ai);
  3916. list.concat(taicpu.op_reg_const(A_MOV,reg,0));
  3917. list.concat(taicpu.op_sym(A_B,l2));
  3918. cg.a_label(list,l1);
  3919. list.concat(taicpu.op_reg_const(A_MOV,reg,1));
  3920. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3921. cg.a_label(list,l2);
  3922. end;
  3923. procedure tthumb2cgarm.init_register_allocators;
  3924. begin
  3925. inherited init_register_allocators;
  3926. { currently, we save R14 always, so we can use it }
  3927. if (target_info.system<>system_arm_darwin) then
  3928. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3929. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3930. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  3931. else
  3932. { r9 is not available on Darwin according to the llvm code generator }
  3933. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3934. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3935. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  3936. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  3937. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  3938. if current_settings.fputype in [fpu_fpv4_s16,fpu_vfpv3_d16] then
  3939. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3940. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3941. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3942. ],first_mm_imreg,[])
  3943. else
  3944. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  3945. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  3946. end;
  3947. procedure tthumb2cgarm.done_register_allocators;
  3948. begin
  3949. rg[R_INTREGISTER].free;
  3950. rg[R_FPUREGISTER].free;
  3951. rg[R_MMREGISTER].free;
  3952. inherited done_register_allocators;
  3953. end;
  3954. procedure tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  3955. begin
  3956. list.concat(taicpu.op_reg(A_BLX, reg));
  3957. {
  3958. the compiler does not properly set this flag anymore in pass 1, and
  3959. for now we only need it after pass 2 (I hope) (JM)
  3960. if not(pi_do_call in current_procinfo.flags) then
  3961. internalerror(2003060703);
  3962. }
  3963. include(current_procinfo.flags,pi_do_call);
  3964. end;
  3965. procedure tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3966. var
  3967. imm_shift : byte;
  3968. l : tasmlabel;
  3969. hr : treference;
  3970. begin
  3971. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3972. internalerror(2002090902);
  3973. if is_thumb32_imm(a) then
  3974. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3975. else if is_thumb32_imm(not(a)) then
  3976. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  3977. else if (a and $FFFF)=a then
  3978. list.concat(taicpu.op_reg_const(A_MOVW,reg,a))
  3979. else
  3980. begin
  3981. reference_reset(hr,4);
  3982. current_asmdata.getjumplabel(l);
  3983. cg.a_label(current_procinfo.aktlocaldata,l);
  3984. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3985. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3986. hr.symbol:=l;
  3987. hr.base:=NR_PC;
  3988. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3989. end;
  3990. end;
  3991. procedure tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3992. var
  3993. oppostfix:toppostfix;
  3994. usedtmpref: treference;
  3995. tmpreg,tmpreg2 : tregister;
  3996. so : tshifterop;
  3997. dir : integer;
  3998. begin
  3999. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  4000. FromSize := ToSize;
  4001. case FromSize of
  4002. { signed integer registers }
  4003. OS_8:
  4004. oppostfix:=PF_B;
  4005. OS_S8:
  4006. oppostfix:=PF_SB;
  4007. OS_16:
  4008. oppostfix:=PF_H;
  4009. OS_S16:
  4010. oppostfix:=PF_SH;
  4011. OS_32,
  4012. OS_S32:
  4013. oppostfix:=PF_None;
  4014. else
  4015. InternalError(200308299);
  4016. end;
  4017. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  4018. begin
  4019. if target_info.endian=endian_big then
  4020. dir:=-1
  4021. else
  4022. dir:=1;
  4023. case FromSize of
  4024. OS_16,OS_S16:
  4025. begin
  4026. { only complicated references need an extra loadaddr }
  4027. if assigned(ref.symbol) or
  4028. (ref.index<>NR_NO) or
  4029. (ref.offset<-255) or
  4030. (ref.offset>4094) or
  4031. { sometimes the compiler reused registers }
  4032. (reg=ref.index) or
  4033. (reg=ref.base) then
  4034. begin
  4035. tmpreg2:=getintregister(list,OS_INT);
  4036. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4037. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4038. end
  4039. else
  4040. usedtmpref:=ref;
  4041. if target_info.endian=endian_big then
  4042. inc(usedtmpref.offset,1);
  4043. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  4044. tmpreg:=getintregister(list,OS_INT);
  4045. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4046. inc(usedtmpref.offset,dir);
  4047. if FromSize=OS_16 then
  4048. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  4049. else
  4050. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  4051. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4052. end;
  4053. OS_32,OS_S32:
  4054. begin
  4055. tmpreg:=getintregister(list,OS_INT);
  4056. { only complicated references need an extra loadaddr }
  4057. if assigned(ref.symbol) or
  4058. (ref.index<>NR_NO) or
  4059. (ref.offset<-255) or
  4060. (ref.offset>4092) or
  4061. { sometimes the compiler reused registers }
  4062. (reg=ref.index) or
  4063. (reg=ref.base) then
  4064. begin
  4065. tmpreg2:=getintregister(list,OS_INT);
  4066. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4067. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4068. end
  4069. else
  4070. usedtmpref:=ref;
  4071. shifterop_reset(so);so.shiftmode:=SM_LSL;
  4072. if ref.alignment=2 then
  4073. begin
  4074. if target_info.endian=endian_big then
  4075. inc(usedtmpref.offset,2);
  4076. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  4077. inc(usedtmpref.offset,dir*2);
  4078. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  4079. so.shiftimm:=16;
  4080. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4081. end
  4082. else
  4083. begin
  4084. if target_info.endian=endian_big then
  4085. inc(usedtmpref.offset,3);
  4086. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4087. inc(usedtmpref.offset,dir);
  4088. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4089. so.shiftimm:=8;
  4090. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4091. inc(usedtmpref.offset,dir);
  4092. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4093. so.shiftimm:=16;
  4094. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4095. inc(usedtmpref.offset,dir);
  4096. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4097. so.shiftimm:=24;
  4098. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4099. end;
  4100. end
  4101. else
  4102. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4103. end;
  4104. end
  4105. else
  4106. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4107. if (fromsize=OS_S8) and (tosize = OS_16) then
  4108. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  4109. end;
  4110. procedure tthumb2cgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  4111. begin
  4112. if op = OP_NOT then
  4113. begin
  4114. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  4115. case size of
  4116. OS_8: list.concat(taicpu.op_reg_reg(A_UXTB,dst,dst));
  4117. OS_S8: list.concat(taicpu.op_reg_reg(A_SXTB,dst,dst));
  4118. OS_16: list.concat(taicpu.op_reg_reg(A_UXTH,dst,dst));
  4119. OS_S16: list.concat(taicpu.op_reg_reg(A_SXTH,dst,dst));
  4120. end;
  4121. end
  4122. else
  4123. inherited a_op_reg_reg(list, op, size, src, dst);
  4124. end;
  4125. procedure tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4126. var
  4127. shift, width : byte;
  4128. tmpreg : tregister;
  4129. so : tshifterop;
  4130. l1 : longint;
  4131. begin
  4132. ovloc.loc:=LOC_VOID;
  4133. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  4134. case op of
  4135. OP_ADD:
  4136. begin
  4137. op:=OP_SUB;
  4138. a:=aint(dword(-a));
  4139. end;
  4140. OP_SUB:
  4141. begin
  4142. op:=OP_ADD;
  4143. a:=aint(dword(-a));
  4144. end
  4145. end;
  4146. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  4147. case op of
  4148. OP_NEG,OP_NOT,
  4149. OP_DIV,OP_IDIV:
  4150. internalerror(200308285);
  4151. OP_SHL:
  4152. begin
  4153. if a>32 then
  4154. internalerror(2014020703);
  4155. if a<>0 then
  4156. begin
  4157. shifterop_reset(so);
  4158. so.shiftmode:=SM_LSL;
  4159. so.shiftimm:=a;
  4160. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4161. end
  4162. else
  4163. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4164. end;
  4165. OP_ROL:
  4166. begin
  4167. if a>32 then
  4168. internalerror(2014020704);
  4169. if a<>0 then
  4170. begin
  4171. shifterop_reset(so);
  4172. so.shiftmode:=SM_ROR;
  4173. so.shiftimm:=32-a;
  4174. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4175. end
  4176. else
  4177. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4178. end;
  4179. OP_ROR:
  4180. begin
  4181. if a>32 then
  4182. internalerror(2014020705);
  4183. if a<>0 then
  4184. begin
  4185. shifterop_reset(so);
  4186. so.shiftmode:=SM_ROR;
  4187. so.shiftimm:=a;
  4188. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4189. end
  4190. else
  4191. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4192. end;
  4193. OP_SHR:
  4194. begin
  4195. if a>32 then
  4196. internalerror(200308292);
  4197. shifterop_reset(so);
  4198. if a<>0 then
  4199. begin
  4200. so.shiftmode:=SM_LSR;
  4201. so.shiftimm:=a;
  4202. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4203. end
  4204. else
  4205. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4206. end;
  4207. OP_SAR:
  4208. begin
  4209. if a>32 then
  4210. internalerror(200308295);
  4211. if a<>0 then
  4212. begin
  4213. shifterop_reset(so);
  4214. so.shiftmode:=SM_ASR;
  4215. so.shiftimm:=a;
  4216. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4217. end
  4218. else
  4219. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4220. end;
  4221. else
  4222. if (op in [OP_SUB, OP_ADD]) and
  4223. ((a < 0) or
  4224. (a > 4095)) then
  4225. begin
  4226. tmpreg:=getintregister(list,size);
  4227. a_load_const_reg(list, size, a, tmpreg);
  4228. if cgsetflags or setflags then
  4229. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4230. list.concat(setoppostfix(
  4231. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4232. end
  4233. else
  4234. begin
  4235. if cgsetflags or setflags then
  4236. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4237. list.concat(setoppostfix(
  4238. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4239. end;
  4240. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  4241. begin
  4242. ovloc.loc:=LOC_FLAGS;
  4243. case op of
  4244. OP_ADD:
  4245. ovloc.resflags:=F_CS;
  4246. OP_SUB:
  4247. ovloc.resflags:=F_CC;
  4248. end;
  4249. end;
  4250. end
  4251. else
  4252. begin
  4253. { there could be added some more sophisticated optimizations }
  4254. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  4255. a_load_reg_reg(list,size,size,src,dst)
  4256. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  4257. a_load_const_reg(list,size,0,dst)
  4258. else if (op in [OP_IMUL]) and (a=-1) then
  4259. a_op_reg_reg(list,OP_NEG,size,src,dst)
  4260. { we do this here instead in the peephole optimizer because
  4261. it saves us a register }
  4262. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  4263. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  4264. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  4265. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  4266. begin
  4267. if l1>32 then{roozbeh does this ever happen?}
  4268. internalerror(200308296);
  4269. shifterop_reset(so);
  4270. so.shiftmode:=SM_LSL;
  4271. so.shiftimm:=l1;
  4272. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  4273. end
  4274. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  4275. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  4276. begin
  4277. if l1>32 then{does this ever happen?}
  4278. internalerror(201205181);
  4279. shifterop_reset(so);
  4280. so.shiftmode:=SM_LSL;
  4281. so.shiftimm:=l1;
  4282. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  4283. end
  4284. 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
  4285. begin
  4286. { nothing to do on success }
  4287. end
  4288. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  4289. Just using mov x, #0 might allow some easier optimizations down the line. }
  4290. else if (op = OP_AND) and (dword(a)=0) then
  4291. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  4292. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  4293. else if (op = OP_AND) and (not(dword(a))=0) then
  4294. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  4295. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  4296. broader range of shifterconstants.}
  4297. {else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  4298. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))}
  4299. else if (op = OP_AND) and is_thumb32_imm(a) then
  4300. list.concat(taicpu.op_reg_reg_const(A_AND,dst,src,dword(a)))
  4301. else if (op = OP_AND) and (a = $FFFF) then
  4302. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  4303. else if (op = OP_AND) and is_thumb32_imm(not(dword(a))) then
  4304. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  4305. else if (op = OP_AND) and is_continuous_mask(not(a), shift, width) then
  4306. begin
  4307. a_load_reg_reg(list,size,size,src,dst);
  4308. list.concat(taicpu.op_reg_const_const(A_BFC,dst,shift,width))
  4309. end
  4310. else
  4311. begin
  4312. tmpreg:=getintregister(list,size);
  4313. a_load_const_reg(list,size,a,tmpreg);
  4314. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  4315. end;
  4316. end;
  4317. maybeadjustresult(list,op,size,dst);
  4318. end;
  4319. const
  4320. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  4321. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  4322. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  4323. procedure tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4324. var
  4325. so : tshifterop;
  4326. tmpreg,overflowreg : tregister;
  4327. asmop : tasmop;
  4328. begin
  4329. ovloc.loc:=LOC_VOID;
  4330. case op of
  4331. OP_NEG,OP_NOT:
  4332. internalerror(200308286);
  4333. OP_ROL:
  4334. begin
  4335. if not(size in [OS_32,OS_S32]) then
  4336. internalerror(2008072801);
  4337. { simulate ROL by ror'ing 32-value }
  4338. tmpreg:=getintregister(list,OS_32);
  4339. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  4340. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  4341. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4342. end;
  4343. OP_ROR:
  4344. begin
  4345. if not(size in [OS_32,OS_S32]) then
  4346. internalerror(2008072802);
  4347. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4348. end;
  4349. OP_IMUL,
  4350. OP_MUL:
  4351. begin
  4352. if cgsetflags or setflags then
  4353. begin
  4354. overflowreg:=getintregister(list,size);
  4355. if op=OP_IMUL then
  4356. asmop:=A_SMULL
  4357. else
  4358. asmop:=A_UMULL;
  4359. { the arm doesn't allow that rd and rm are the same }
  4360. if dst=src2 then
  4361. begin
  4362. if dst<>src1 then
  4363. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  4364. else
  4365. begin
  4366. tmpreg:=getintregister(list,size);
  4367. a_load_reg_reg(list,size,size,src2,dst);
  4368. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  4369. end;
  4370. end
  4371. else
  4372. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  4373. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4374. if op=OP_IMUL then
  4375. begin
  4376. shifterop_reset(so);
  4377. so.shiftmode:=SM_ASR;
  4378. so.shiftimm:=31;
  4379. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  4380. end
  4381. else
  4382. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  4383. ovloc.loc:=LOC_FLAGS;
  4384. ovloc.resflags:=F_NE;
  4385. end
  4386. else
  4387. begin
  4388. { the arm doesn't allow that rd and rm are the same }
  4389. if dst=src2 then
  4390. begin
  4391. if dst<>src1 then
  4392. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  4393. else
  4394. begin
  4395. tmpreg:=getintregister(list,size);
  4396. a_load_reg_reg(list,size,size,src2,dst);
  4397. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  4398. end;
  4399. end
  4400. else
  4401. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  4402. end;
  4403. end;
  4404. else
  4405. begin
  4406. if cgsetflags or setflags then
  4407. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4408. {$ifdef dummy}
  4409. { R13 is not allowed for certain instruction operands }
  4410. if op_reg_reg_opcg2asmopThumb2[op] in [A_ADD,A_SUB,A_AND,A_BIC,A_EOR] then
  4411. begin
  4412. if getsupreg(dst)=RS_R13 then
  4413. begin
  4414. tmpreg:=getintregister(list,OS_INT);
  4415. a_load_reg_reg(list,OS_INT,OS_INT,dst,tmpreg);
  4416. dst:=tmpreg;
  4417. end;
  4418. if getsupreg(src1)=RS_R13 then
  4419. begin
  4420. tmpreg:=getintregister(list,OS_INT);
  4421. a_load_reg_reg(list,OS_INT,OS_INT,src1,tmpreg);
  4422. src1:=tmpreg;
  4423. end;
  4424. end;
  4425. {$endif}
  4426. list.concat(setoppostfix(
  4427. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4428. end;
  4429. end;
  4430. maybeadjustresult(list,op,size,dst);
  4431. end;
  4432. procedure tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  4433. var item: taicpu;
  4434. begin
  4435. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  4436. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  4437. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  4438. end;
  4439. procedure tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  4440. var
  4441. ref : treference;
  4442. shift : byte;
  4443. firstfloatreg,lastfloatreg,
  4444. r : byte;
  4445. regs : tcpuregisterset;
  4446. stackmisalignment: pint;
  4447. begin
  4448. LocalSize:=align(LocalSize,4);
  4449. { call instruction does not put anything on the stack }
  4450. stackmisalignment:=0;
  4451. if not(nostackframe) then
  4452. begin
  4453. firstfloatreg:=RS_NO;
  4454. lastfloatreg:=RS_NO;
  4455. { save floating point registers? }
  4456. for r:=RS_F0 to RS_F7 do
  4457. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4458. begin
  4459. if firstfloatreg=RS_NO then
  4460. firstfloatreg:=r;
  4461. lastfloatreg:=r;
  4462. inc(stackmisalignment,12);
  4463. end;
  4464. a_reg_alloc(list,NR_STACK_POINTER_REG);
  4465. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4466. begin
  4467. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  4468. a_reg_alloc(list,NR_R12);
  4469. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  4470. end;
  4471. { save int registers }
  4472. reference_reset(ref,4);
  4473. ref.index:=NR_STACK_POINTER_REG;
  4474. ref.addressmode:=AM_PREINDEXED;
  4475. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4476. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4477. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  4478. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  4479. include(regs,RS_R14);
  4480. if regs<>[] then
  4481. begin
  4482. for r:=RS_R0 to RS_R15 do
  4483. if (r in regs) then
  4484. inc(stackmisalignment,4);
  4485. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4486. end;
  4487. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4488. begin
  4489. { the framepointer now points to the saved R15, so the saved
  4490. framepointer is at R11-12 (for get_caller_frame) }
  4491. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  4492. a_reg_dealloc(list,NR_R12);
  4493. end;
  4494. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4495. if (LocalSize<>0) or
  4496. ((stackmisalignment<>0) and
  4497. ((pi_do_call in current_procinfo.flags) or
  4498. (po_assembler in current_procinfo.procdef.procoptions))) then
  4499. begin
  4500. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4501. if not(is_shifter_const(localsize,shift)) then
  4502. begin
  4503. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  4504. a_reg_alloc(list,NR_R12);
  4505. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4506. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  4507. a_reg_dealloc(list,NR_R12);
  4508. end
  4509. else
  4510. begin
  4511. a_reg_dealloc(list,NR_R12);
  4512. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  4513. end;
  4514. end;
  4515. if firstfloatreg<>RS_NO then
  4516. begin
  4517. reference_reset(ref,4);
  4518. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4519. begin
  4520. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4521. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4522. ref.base:=NR_R12;
  4523. end
  4524. else
  4525. begin
  4526. ref.base:=current_procinfo.framepointer;
  4527. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4528. end;
  4529. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4530. lastfloatreg-firstfloatreg+1,ref));
  4531. end;
  4532. end;
  4533. end;
  4534. procedure tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  4535. var
  4536. ref : treference;
  4537. firstfloatreg,lastfloatreg,
  4538. r : byte;
  4539. shift : byte;
  4540. regs : tcpuregisterset;
  4541. LocalSize : longint;
  4542. stackmisalignment: pint;
  4543. begin
  4544. if not(nostackframe) then
  4545. begin
  4546. stackmisalignment:=0;
  4547. { restore floating point register }
  4548. firstfloatreg:=RS_NO;
  4549. lastfloatreg:=RS_NO;
  4550. { save floating point registers? }
  4551. for r:=RS_F0 to RS_F7 do
  4552. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4553. begin
  4554. if firstfloatreg=RS_NO then
  4555. firstfloatreg:=r;
  4556. lastfloatreg:=r;
  4557. { floating point register space is already included in
  4558. localsize below by calc_stackframe_size
  4559. inc(stackmisalignment,12);
  4560. }
  4561. end;
  4562. if firstfloatreg<>RS_NO then
  4563. begin
  4564. reference_reset(ref,4);
  4565. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4566. begin
  4567. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4568. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4569. ref.base:=NR_R12;
  4570. end
  4571. else
  4572. begin
  4573. ref.base:=current_procinfo.framepointer;
  4574. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4575. end;
  4576. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4577. lastfloatreg-firstfloatreg+1,ref));
  4578. end;
  4579. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4580. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  4581. begin
  4582. exclude(regs,RS_R14);
  4583. include(regs,RS_R15);
  4584. end;
  4585. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  4586. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  4587. for r:=RS_R0 to RS_R15 do
  4588. if (r in regs) then
  4589. inc(stackmisalignment,4);
  4590. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4591. LocalSize:=current_procinfo.calc_stackframe_size;
  4592. if (LocalSize<>0) or
  4593. ((stackmisalignment<>0) and
  4594. ((pi_do_call in current_procinfo.flags) or
  4595. (po_assembler in current_procinfo.procdef.procoptions))) then
  4596. begin
  4597. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4598. if not(is_shifter_const(LocalSize,shift)) then
  4599. begin
  4600. a_reg_alloc(list,NR_R12);
  4601. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4602. list.concat(taicpu.op_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_R12));
  4603. a_reg_dealloc(list,NR_R12);
  4604. end
  4605. else
  4606. begin
  4607. a_reg_dealloc(list,NR_R12);
  4608. list.concat(taicpu.op_reg_const(A_ADD,NR_STACK_POINTER_REG,LocalSize));
  4609. end;
  4610. end;
  4611. if regs=[] then
  4612. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  4613. else
  4614. begin
  4615. reference_reset(ref,4);
  4616. ref.index:=NR_STACK_POINTER_REG;
  4617. ref.addressmode:=AM_PREINDEXED;
  4618. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4619. end;
  4620. end
  4621. else
  4622. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  4623. end;
  4624. function tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  4625. var
  4626. tmpreg : tregister;
  4627. tmpref : treference;
  4628. l : tasmlabel;
  4629. so: tshifterop;
  4630. begin
  4631. tmpreg:=NR_NO;
  4632. { Be sure to have a base register }
  4633. if (ref.base=NR_NO) then
  4634. begin
  4635. if ref.shiftmode<>SM_None then
  4636. internalerror(2014020706);
  4637. ref.base:=ref.index;
  4638. ref.index:=NR_NO;
  4639. end;
  4640. { absolute symbols can't be handled directly, we've to store the symbol reference
  4641. in the text segment and access it pc relative
  4642. For now, we assume that references where base or index equals to PC are already
  4643. relative, all other references are assumed to be absolute and thus they need
  4644. to be handled extra.
  4645. A proper solution would be to change refoptions to a set and store the information
  4646. if the symbol is absolute or relative there.
  4647. }
  4648. if (assigned(ref.symbol) and
  4649. not(is_pc(ref.base)) and
  4650. not(is_pc(ref.index))
  4651. ) or
  4652. { [#xxx] isn't a valid address operand }
  4653. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  4654. //(ref.offset<-4095) or
  4655. (ref.offset<-255) or
  4656. (ref.offset>4095) or
  4657. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  4658. ((ref.offset<-255) or
  4659. (ref.offset>255)
  4660. )
  4661. ) or
  4662. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  4663. ((ref.offset<-1020) or
  4664. (ref.offset>1020) or
  4665. ((abs(ref.offset) mod 4)<>0) or
  4666. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  4667. assigned(ref.symbol)
  4668. )
  4669. ) then
  4670. begin
  4671. reference_reset(tmpref,4);
  4672. { load symbol }
  4673. tmpreg:=getintregister(list,OS_INT);
  4674. if assigned(ref.symbol) then
  4675. begin
  4676. current_asmdata.getjumplabel(l);
  4677. cg.a_label(current_procinfo.aktlocaldata,l);
  4678. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  4679. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  4680. { load consts entry }
  4681. tmpref.symbol:=l;
  4682. tmpref.base:=NR_R15;
  4683. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  4684. { in case of LDF/STF, we got rid of the NR_R15 }
  4685. if is_pc(ref.base) then
  4686. ref.base:=NR_NO;
  4687. if is_pc(ref.index) then
  4688. ref.index:=NR_NO;
  4689. end
  4690. else
  4691. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  4692. if (ref.base<>NR_NO) then
  4693. begin
  4694. if ref.index<>NR_NO then
  4695. begin
  4696. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4697. ref.base:=tmpreg;
  4698. end
  4699. else
  4700. begin
  4701. ref.index:=tmpreg;
  4702. ref.shiftimm:=0;
  4703. ref.signindex:=1;
  4704. ref.shiftmode:=SM_None;
  4705. end;
  4706. end
  4707. else
  4708. ref.base:=tmpreg;
  4709. ref.offset:=0;
  4710. ref.symbol:=nil;
  4711. end;
  4712. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  4713. begin
  4714. if tmpreg<>NR_NO then
  4715. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  4716. else
  4717. begin
  4718. tmpreg:=getintregister(list,OS_ADDR);
  4719. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  4720. ref.base:=tmpreg;
  4721. end;
  4722. ref.offset:=0;
  4723. end;
  4724. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  4725. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  4726. begin
  4727. tmpreg:=getintregister(list,OS_ADDR);
  4728. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  4729. ref.base := tmpreg;
  4730. end;
  4731. { floating point operations have only limited references
  4732. we expect here, that a base is already set }
  4733. 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
  4734. begin
  4735. if ref.shiftmode<>SM_none then
  4736. internalerror(200309121);
  4737. if tmpreg<>NR_NO then
  4738. begin
  4739. if ref.base=tmpreg then
  4740. begin
  4741. if ref.signindex<0 then
  4742. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  4743. else
  4744. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  4745. ref.index:=NR_NO;
  4746. end
  4747. else
  4748. begin
  4749. if ref.index<>tmpreg then
  4750. internalerror(200403161);
  4751. if ref.signindex<0 then
  4752. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  4753. else
  4754. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4755. ref.base:=tmpreg;
  4756. ref.index:=NR_NO;
  4757. end;
  4758. end
  4759. else
  4760. begin
  4761. tmpreg:=getintregister(list,OS_ADDR);
  4762. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  4763. ref.base:=tmpreg;
  4764. ref.index:=NR_NO;
  4765. end;
  4766. end;
  4767. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  4768. Result := ref;
  4769. end;
  4770. procedure tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  4771. var
  4772. instr: taicpu;
  4773. begin
  4774. if (fromsize=OS_F32) and
  4775. (tosize=OS_F32) then
  4776. begin
  4777. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  4778. list.Concat(instr);
  4779. add_move_instruction(instr);
  4780. end
  4781. else if (fromsize=OS_F64) and
  4782. (tosize=OS_F64) then
  4783. begin
  4784. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  4785. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  4786. end
  4787. else if (fromsize=OS_F32) and
  4788. (tosize=OS_F64) then
  4789. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  4790. begin
  4791. //list.concat(nil);
  4792. end;
  4793. end;
  4794. procedure tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  4795. begin
  4796. if fromsize=OS_F32 then
  4797. handle_load_store(list,A_VLDR,PF_F32,reg,ref)
  4798. else
  4799. handle_load_store(list,A_VLDR,PF_F64,reg,ref);
  4800. end;
  4801. procedure tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  4802. begin
  4803. if fromsize=OS_F32 then
  4804. handle_load_store(list,A_VSTR,PF_F32,reg,ref)
  4805. else
  4806. handle_load_store(list,A_VSTR,PF_F64,reg,ref);
  4807. end;
  4808. procedure tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  4809. begin
  4810. if //(shuffle=nil) and
  4811. (tosize=OS_F32) then
  4812. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  4813. else
  4814. internalerror(2012100813);
  4815. end;
  4816. procedure tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  4817. begin
  4818. if //(shuffle=nil) and
  4819. (fromsize=OS_F32) then
  4820. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg))
  4821. else
  4822. internalerror(2012100814);
  4823. end;
  4824. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  4825. var tmpreg: tregister;
  4826. begin
  4827. case op of
  4828. OP_NEG:
  4829. begin
  4830. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4831. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  4832. tmpreg:=cg.getintregister(list,OS_32);
  4833. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  4834. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  4835. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4836. end;
  4837. else
  4838. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  4839. end;
  4840. end;
  4841. procedure tthumbcg64farm.a_op64_reg_reg(list: TAsmList; op: TOpCG; size: tcgsize; regsrc, regdst: tregister64);
  4842. begin
  4843. case op of
  4844. OP_NEG:
  4845. begin
  4846. list.concat(taicpu.op_reg_const(A_MOV,regdst.reglo,0));
  4847. list.concat(taicpu.op_reg_const(A_MOV,regdst.reghi,0));
  4848. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4849. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4850. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4851. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4852. end;
  4853. OP_NOT:
  4854. begin
  4855. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  4856. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  4857. end;
  4858. OP_AND,OP_OR,OP_XOR:
  4859. begin
  4860. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  4861. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  4862. end;
  4863. OP_ADD:
  4864. begin
  4865. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4866. list.concat(taicpu.op_reg_reg(A_ADD,regdst.reglo,regsrc.reglo));
  4867. list.concat(taicpu.op_reg_reg(A_ADC,regdst.reghi,regsrc.reghi));
  4868. end;
  4869. OP_SUB:
  4870. begin
  4871. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4872. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4873. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4874. end;
  4875. else
  4876. internalerror(2003083101);
  4877. end;
  4878. end;
  4879. procedure tthumbcg64farm.a_op64_const_reg(list: TAsmList; op: TOpCG; size: tcgsize; value: int64; reg: tregister64);
  4880. var
  4881. tmpreg : tregister;
  4882. b : byte;
  4883. begin
  4884. case op of
  4885. OP_AND,OP_OR,OP_XOR:
  4886. begin
  4887. cg.a_op_const_reg(list,op,OS_32,aint(lo(value)),reg.reglo);
  4888. cg.a_op_const_reg(list,op,OS_32,aint(hi(value)),reg.reghi);
  4889. end;
  4890. OP_ADD:
  4891. begin
  4892. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4893. begin
  4894. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4895. list.concat(taicpu.op_reg_const(A_ADD,reg.reglo,aint(lo(value))));
  4896. end
  4897. else
  4898. begin
  4899. tmpreg:=cg.getintregister(list,OS_32);
  4900. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4901. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4902. list.concat(taicpu.op_reg_reg(A_ADD,reg.reglo,tmpreg));
  4903. end;
  4904. tmpreg:=cg.getintregister(list,OS_32);
  4905. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  4906. list.concat(taicpu.op_reg_reg(A_ADC,reg.reghi,tmpreg));
  4907. end;
  4908. OP_SUB:
  4909. begin
  4910. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4911. begin
  4912. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4913. list.concat(taicpu.op_reg_const(A_SUB,reg.reglo,aint(lo(value))))
  4914. end
  4915. else
  4916. begin
  4917. tmpreg:=cg.getintregister(list,OS_32);
  4918. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4919. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4920. list.concat(taicpu.op_reg_reg(A_SUB,reg.reglo,tmpreg));
  4921. end;
  4922. tmpreg:=cg.getintregister(list,OS_32);
  4923. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  4924. list.concat(taicpu.op_reg_reg(A_SBC,reg.reghi,tmpreg));
  4925. end;
  4926. else
  4927. internalerror(2003083101);
  4928. end;
  4929. end;
  4930. procedure create_codegen;
  4931. begin
  4932. if GenerateThumb2Code then
  4933. begin
  4934. cg:=tthumb2cgarm.create;
  4935. cg64:=tthumb2cg64farm.create;
  4936. casmoptimizer:=TCpuThumb2AsmOptimizer;
  4937. end
  4938. else if GenerateThumbCode then
  4939. begin
  4940. cg:=tthumbcgarm.create;
  4941. cg64:=tthumbcg64farm.create;
  4942. // casmoptimizer:=TCpuThumbAsmOptimizer;
  4943. end
  4944. else
  4945. begin
  4946. cg:=tarmcgarm.create;
  4947. cg64:=tarmcg64farm.create;
  4948. casmoptimizer:=TCpuAsmOptimizer;
  4949. end;
  4950. end;
  4951. end.