cgcpu.pas 164 KB

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