cgcpu.pas 201 KB

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