cgcpu.pas 144 KB

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