cgx86.pas 138 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. This unit implements the common parts of the code generator for the i386 and the x86-64.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  18. }
  19. unit cgx86;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cgbase,cgutils,cgobj,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. cpubase,cpuinfo,rgx86,
  27. symconst,symtype,symdef,
  28. parabase;
  29. type
  30. { tcgx86 }
  31. tcgx86 = class(tcg)
  32. rgfpu : Trgx86fpu;
  33. procedure done_register_allocators;override;
  34. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  35. function getmmxregister(list:TAsmList):Tregister;
  36. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  37. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  38. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  39. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  40. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  41. function uses_registers(rt:Tregistertype):boolean;override;
  42. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  43. procedure dec_fpu_stack;
  44. procedure inc_fpu_stack;
  45. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  46. procedure a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  47. procedure a_call_name_static(list : TAsmList;const s : string);override;
  48. procedure a_call_name_static_near(list : TAsmList;const s : string);
  49. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  50. procedure a_call_reg_near(list : TAsmList;reg : tregister);
  51. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  52. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  53. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  54. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  55. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  56. procedure a_op_ref(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference); override;
  57. {$ifndef i8086}
  58. procedure a_op_const_reg_reg(list : TAsmList; op : Topcg; size : Tcgsize; a : tcgint; src,dst : Tregister); override;
  59. procedure a_op_reg_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; src1,src2,dst : tregister); override;
  60. {$endif not i8086}
  61. { move instructions }
  62. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  63. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  64. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  65. { final as a_load_ref_reg_internal() should be overridden instead }
  66. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;final;
  67. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  68. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  69. { bit scan instructions }
  70. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister); override;
  71. { fpu move instructions }
  72. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  73. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  74. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  75. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara); override;
  76. { vector register move instructions }
  77. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  78. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  79. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  80. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  81. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  82. procedure a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;const ref : treference;src,dst : tregister;shuffle : pmmshuffle);override;
  83. procedure a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;src1,src2,dst : tregister;shuffle : pmmshuffle);override;
  84. { comparison operations }
  85. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  86. l : tasmlabel);override;
  87. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  88. l : tasmlabel);override;
  89. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  90. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  91. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  92. procedure a_jmp_name(list : TAsmList;const s : string);override;
  93. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  94. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  95. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  96. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference); override;
  97. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  98. { entry/exit code helpers }
  99. procedure g_profilecode(list : TAsmList);override;
  100. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  101. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  102. procedure g_save_registers(list: TAsmList); override;
  103. procedure g_restore_registers(list: TAsmList); override;
  104. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);override;
  105. procedure make_simple_ref(list:TAsmList;var ref: treference);inline;
  106. procedure make_direct_ref(list:TAsmList;var ref: treference);
  107. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  108. procedure generate_leave(list : TAsmList);
  109. protected
  110. procedure a_load_ref_reg_internal(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister;isdirect:boolean);virtual;
  111. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  112. procedure check_register_size(size:tcgsize;reg:tregister);
  113. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  114. procedure opmm_loc_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;loc : tlocation;src,dst : tregister;shuffle : pmmshuffle);
  115. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  116. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  117. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  118. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  119. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  120. procedure internal_restore_regs(list: TAsmList; use_pop: boolean);
  121. procedure make_simple_ref(list:TAsmList;var ref: treference;isdirect:boolean);
  122. end;
  123. const
  124. {$if defined(x86_64)}
  125. TCGSize2OpSize: Array[tcgsize] of topsize =
  126. (S_NO,S_B,S_W,S_L,S_Q,S_XMM,S_B,S_W,S_L,S_Q,S_XMM,
  127. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  128. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM);
  129. {$elseif defined(i386)}
  130. TCGSize2OpSize: Array[tcgsize] of topsize =
  131. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  132. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  133. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM);
  134. {$elseif defined(i8086)}
  135. TCGSize2OpSize: Array[tcgsize] of topsize =
  136. (S_NO,S_B,S_W,S_W,S_W,S_T,S_B,S_W,S_W,S_W,S_W,
  137. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  138. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM);
  139. {$endif}
  140. {$ifndef NOTARGETWIN}
  141. winstackpagesize = 4096;
  142. {$endif NOTARGETWIN}
  143. function UseIncDec: boolean;
  144. { returns true, if the compiler should use leave instead of mov/pop }
  145. function UseLeave: boolean;
  146. { Gets the byte alignment of a reference }
  147. function GetRefAlignment(ref: treference): Byte;
  148. implementation
  149. uses
  150. globals,verbose,systems,cutils,
  151. symcpu,
  152. paramgr,procinfo,
  153. tgobj,ncgutil;
  154. { modern CPUs prefer add/sub over inc/dec because add/sub break instructions dependencies on flags
  155. because they modify all flags }
  156. function UseIncDec: boolean;
  157. begin
  158. {$if defined(x86_64)}
  159. Result:=cs_opt_size in current_settings.optimizerswitches;
  160. {$elseif defined(i386)}
  161. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_386]);
  162. {$elseif defined(i8086)}
  163. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_8086..cpu_386]);
  164. {$endif}
  165. end;
  166. function UseLeave: boolean;
  167. begin
  168. {$if defined(x86_64)}
  169. { Modern processors should be happy with mov;pop, maybe except older AMDs }
  170. Result:=cs_opt_size in current_settings.optimizerswitches;
  171. {$elseif defined(i386)}
  172. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.optimizecputype<cpu_Pentium2);
  173. {$elseif defined(i8086)}
  174. Result:=current_settings.cputype>=cpu_186;
  175. {$endif}
  176. end;
  177. function GetRefAlignment(ref: treference): Byte; {$IFDEF USEINLINE}inline;{$ENDIF}
  178. begin
  179. {$ifdef x86_64}
  180. { The stack pointer and base pointer will be aligned to 16-byte boundaries if the machine code is well-behaved }
  181. if (ref.base = NR_RSP) or (ref.base = NR_RBP) then
  182. begin
  183. if (ref.index = NR_NO) and ((ref.offset mod 16) = 0) then
  184. Result := 16
  185. else
  186. Result := ref.alignment;
  187. end
  188. else
  189. {$endif x86_64}
  190. Result := ref.alignment;
  191. end;
  192. const
  193. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  194. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  195. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR,A_ROL,A_ROR);
  196. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  197. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  198. procedure Tcgx86.done_register_allocators;
  199. begin
  200. rg[R_INTREGISTER].free;
  201. rg[R_MMREGISTER].free;
  202. rg[R_MMXREGISTER].free;
  203. rgfpu.free;
  204. inherited done_register_allocators;
  205. end;
  206. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  207. begin
  208. result:=rgfpu.getregisterfpu(list);
  209. end;
  210. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  211. begin
  212. if not assigned(rg[R_MMXREGISTER]) then
  213. internalerror(2003121204);
  214. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  215. end;
  216. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  217. begin
  218. if not assigned(rg[R_MMREGISTER]) then
  219. internalerror(2003121234);
  220. case size of
  221. OS_F64:
  222. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  223. OS_F32:
  224. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  225. OS_M64:
  226. result:=rg[R_MMREGISTER].getregister(list,R_SUBQ);
  227. OS_128,
  228. OS_M128,
  229. OS_F128:
  230. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMX); { R_SUBMMWHOLE seems a bit dangerous and ambiguous, so changed to R_SUBMMX. [Kit] }
  231. OS_M256:
  232. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMY);
  233. OS_M512:
  234. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMZ);
  235. else
  236. internalerror(200506041);
  237. end;
  238. end;
  239. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  240. begin
  241. if getregtype(r)=R_FPUREGISTER then
  242. internalerror(2003121210)
  243. else
  244. inherited getcpuregister(list,r);
  245. end;
  246. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  247. begin
  248. if getregtype(r)=R_FPUREGISTER then
  249. rgfpu.ungetregisterfpu(list,r)
  250. else
  251. inherited ungetcpuregister(list,r);
  252. end;
  253. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  254. begin
  255. if rt<>R_FPUREGISTER then
  256. inherited alloccpuregisters(list,rt,r);
  257. end;
  258. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  259. begin
  260. if rt<>R_FPUREGISTER then
  261. inherited dealloccpuregisters(list,rt,r);
  262. end;
  263. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  264. begin
  265. if rt=R_FPUREGISTER then
  266. result:=false
  267. else
  268. result:=inherited uses_registers(rt);
  269. end;
  270. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  271. begin
  272. if getregtype(r)<>R_FPUREGISTER then
  273. inherited add_reg_instruction(instr,r);
  274. end;
  275. procedure tcgx86.dec_fpu_stack;
  276. begin
  277. if rgfpu.fpuvaroffset<=0 then
  278. internalerror(200604201);
  279. dec(rgfpu.fpuvaroffset);
  280. end;
  281. procedure tcgx86.inc_fpu_stack;
  282. begin
  283. if rgfpu.fpuvaroffset>=7 then
  284. internalerror(2012062901);
  285. inc(rgfpu.fpuvaroffset);
  286. end;
  287. { Range check must be disabled explicitly as the code serves
  288. on three different architecture sizes }
  289. {$R-}
  290. {****************************************************************************
  291. This is private property, keep out! :)
  292. ****************************************************************************}
  293. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  294. begin
  295. { ensure to have always valid sizes }
  296. if s1=OS_NO then
  297. s1:=s2;
  298. if s2=OS_NO then
  299. s2:=s1;
  300. case s2 of
  301. OS_8,OS_S8 :
  302. if S1 in [OS_8,OS_S8] then
  303. s3 := S_B
  304. else
  305. internalerror(200109221);
  306. OS_16,OS_S16:
  307. case s1 of
  308. OS_8,OS_S8:
  309. s3 := S_BW;
  310. OS_16,OS_S16:
  311. s3 := S_W;
  312. else
  313. internalerror(200109222);
  314. end;
  315. OS_32,OS_S32:
  316. case s1 of
  317. OS_8,OS_S8:
  318. s3 := S_BL;
  319. OS_16,OS_S16:
  320. s3 := S_WL;
  321. OS_32,OS_S32:
  322. s3 := S_L;
  323. else
  324. internalerror(200109223);
  325. end;
  326. {$ifdef x86_64}
  327. OS_64,OS_S64:
  328. case s1 of
  329. OS_8:
  330. s3 := S_BL;
  331. OS_S8:
  332. s3 := S_BQ;
  333. OS_16:
  334. s3 := S_WL;
  335. OS_S16:
  336. s3 := S_WQ;
  337. OS_32:
  338. s3 := S_L;
  339. OS_S32:
  340. s3 := S_LQ;
  341. OS_64,OS_S64:
  342. s3 := S_Q;
  343. else
  344. internalerror(200304302);
  345. end;
  346. {$endif x86_64}
  347. else
  348. internalerror(200109227);
  349. end;
  350. if s3 in [S_B,S_W,S_L,S_Q] then
  351. op := A_MOV
  352. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  353. op := A_MOVZX
  354. else
  355. {$ifdef x86_64}
  356. if s3 in [S_LQ] then
  357. op := A_MOVSXD
  358. else
  359. {$endif x86_64}
  360. op := A_MOVSX;
  361. end;
  362. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  363. begin
  364. make_simple_ref(list,ref,false);
  365. end;
  366. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference;isdirect:boolean);
  367. var
  368. hreg : tregister;
  369. href : treference;
  370. {$ifndef x86_64}
  371. add_hreg: boolean;
  372. {$endif not x86_64}
  373. begin
  374. hreg:=NR_NO;
  375. { make_simple_ref() may have already been called earlier, and in that
  376. case make sure we don't perform the PIC-simplifications twice }
  377. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  378. exit;
  379. { handle indirect symbols first }
  380. if not isdirect then
  381. make_direct_ref(list,ref);
  382. {$if defined(x86_64)}
  383. { Only 32bit is allowed }
  384. { Note that this isn't entirely correct: for RIP-relative targets/memory models,
  385. it is actually (offset+@symbol-RIP) that should fit into 32 bits. Since two last
  386. members aren't known until link time, ABIs place very pessimistic limits
  387. on offset values, e.g. SysV AMD64 allows +/-$1000000 (16 megabytes) }
  388. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) or
  389. { absolute address is not a common thing in x64, but nevertheless a possible one }
  390. ((ref.base=NR_NO) and (ref.index=NR_NO) and (ref.symbol=nil)) then
  391. begin
  392. { Load constant value to register }
  393. hreg:=GetAddressRegister(list);
  394. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  395. ref.offset:=0;
  396. {if assigned(ref.symbol) then
  397. begin
  398. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  399. ref.symbol:=nil;
  400. end;}
  401. { Add register to reference }
  402. if ref.base=NR_NO then
  403. ref.base:=hreg
  404. else if ref.index=NR_NO then
  405. ref.index:=hreg
  406. else
  407. begin
  408. { don't use add, as the flags may contain a value }
  409. reference_reset_base(href,hreg,0,ref.temppos,ref.alignment,[]);
  410. href.index:=ref.index;
  411. href.scalefactor:=ref.scalefactor;
  412. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  413. ref.index:=hreg;
  414. ref.scalefactor:=1;
  415. end;
  416. end;
  417. if assigned(ref.symbol) then
  418. begin
  419. if cs_create_pic in current_settings.moduleswitches then
  420. begin
  421. { Local symbols must not be accessed via the GOT }
  422. if (ref.symbol.bind=AB_LOCAL) then
  423. begin
  424. { unfortunately, RIP-based addresses don't support an index }
  425. if (ref.base<>NR_NO) or
  426. (ref.index<>NR_NO) then
  427. begin
  428. reference_reset_symbol(href,ref.symbol,0,ref.alignment,[]);
  429. hreg:=getaddressregister(list);
  430. href.refaddr:=addr_pic_no_got;
  431. href.base:=NR_RIP;
  432. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  433. ref.symbol:=nil;
  434. end
  435. else
  436. begin
  437. ref.refaddr:=addr_pic_no_got;
  438. hreg:=NR_NO;
  439. ref.base:=NR_RIP;
  440. end;
  441. end
  442. else
  443. begin
  444. reference_reset_symbol(href,ref.symbol,0,ref.alignment,[]);
  445. hreg:=getaddressregister(list);
  446. href.refaddr:=addr_pic;
  447. href.base:=NR_RIP;
  448. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  449. ref.symbol:=nil;
  450. end;
  451. if ref.base=NR_NO then
  452. ref.base:=hreg
  453. else if ref.index=NR_NO then
  454. begin
  455. ref.index:=hreg;
  456. ref.scalefactor:=1;
  457. end
  458. else
  459. begin
  460. { don't use add, as the flags may contain a value }
  461. reference_reset_base(href,ref.base,0,ref.temppos,ref.alignment,[]);
  462. href.index:=hreg;
  463. ref.base:=getaddressregister(list);
  464. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  465. end;
  466. end
  467. else
  468. { Always use RIP relative symbol addressing for Windows and Darwin targets. }
  469. if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim])) and (ref.base<>NR_RIP) then
  470. begin
  471. if (ref.refaddr=addr_no) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  472. begin
  473. { Set RIP relative addressing for simple symbol references }
  474. ref.base:=NR_RIP;
  475. ref.refaddr:=addr_pic_no_got
  476. end
  477. else
  478. begin
  479. { Use temp register to load calculated 64-bit symbol address for complex references }
  480. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  481. href.base:=NR_RIP;
  482. href.refaddr:=addr_pic_no_got;
  483. hreg:=GetAddressRegister(list);
  484. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  485. ref.symbol:=nil;
  486. if ref.base=NR_NO then
  487. ref.base:=hreg
  488. else if ref.index=NR_NO then
  489. begin
  490. ref.index:=hreg;
  491. ref.scalefactor:=0;
  492. end
  493. else
  494. begin
  495. { don't use add, as the flags may contain a value }
  496. reference_reset_base(href,ref.base,0,ref.temppos,ref.alignment,[]);
  497. href.index:=hreg;
  498. ref.base:=getaddressregister(list);
  499. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  500. end;
  501. end;
  502. end;
  503. end;
  504. {$elseif defined(i386)}
  505. add_hreg:=false;
  506. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  507. begin
  508. if assigned(ref.symbol) and
  509. not(assigned(ref.relsymbol)) and
  510. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN]) or
  511. (cs_create_pic in current_settings.moduleswitches)) then
  512. begin
  513. if ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN] then
  514. begin
  515. hreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  516. ref.symbol:=nil;
  517. end
  518. else
  519. begin
  520. include(current_procinfo.flags,pi_needs_got);
  521. { make a copy of the got register, hreg can get modified }
  522. hreg:=getaddressregister(list);
  523. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,hreg);
  524. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  525. end;
  526. add_hreg:=true
  527. end
  528. end
  529. else if (cs_create_pic in current_settings.moduleswitches) and
  530. assigned(ref.symbol) then
  531. begin
  532. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  533. href.base:=current_procinfo.got;
  534. href.refaddr:=addr_pic;
  535. include(current_procinfo.flags,pi_needs_got);
  536. hreg:=getaddressregister(list);
  537. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  538. ref.symbol:=nil;
  539. add_hreg:=true;
  540. end;
  541. if add_hreg then
  542. begin
  543. if ref.base=NR_NO then
  544. ref.base:=hreg
  545. else if ref.index=NR_NO then
  546. begin
  547. ref.index:=hreg;
  548. ref.scalefactor:=1;
  549. end
  550. else
  551. begin
  552. { don't use add, as the flags may contain a value }
  553. reference_reset_base(href,ref.base,0,ref.temppos,ref.alignment,[]);
  554. href.index:=hreg;
  555. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  556. ref.base:=hreg;
  557. end;
  558. end;
  559. {$elseif defined(i8086)}
  560. { i8086 does not support stack relative addressing }
  561. if ref.base = NR_STACK_POINTER_REG then
  562. begin
  563. href:=ref;
  564. href.base:=getaddressregister(list);
  565. { let the register allocator find a suitable register for the reference }
  566. list.Concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_SP, href.base));
  567. { if DS<>SS in the current memory model, we need to add an SS: segment override as well }
  568. if (ref.segment=NR_NO) and not segment_regs_equal(NR_DS,NR_SS) then
  569. href.segment:=NR_SS;
  570. ref:=href;
  571. end;
  572. { if there is a segment in an int register, move it to ES }
  573. if (ref.segment<>NR_NO) and (not is_segment_reg(ref.segment)) then
  574. begin
  575. list.concat(taicpu.op_reg_reg(A_MOV,S_W,ref.segment,NR_ES));
  576. ref.segment:=NR_ES;
  577. end;
  578. { can the segment override be dropped? }
  579. if ref.segment<>NR_NO then
  580. begin
  581. if (ref.base=NR_BP) and segment_regs_equal(ref.segment,NR_SS) then
  582. ref.segment:=NR_NO;
  583. if (ref.base<>NR_BP) and segment_regs_equal(ref.segment,NR_DS) then
  584. ref.segment:=NR_NO;
  585. end;
  586. {$endif}
  587. end;
  588. procedure tcgx86.make_direct_ref(list:tasmlist;var ref:treference);
  589. var
  590. href : treference;
  591. hreg : tregister;
  592. begin
  593. if assigned(ref.symbol) and (ref.symbol.bind in asmsymbindindirect) then
  594. begin
  595. { load the symbol into a register }
  596. hreg:=getaddressregister(list);
  597. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  598. { tell make_simple_ref that we are loading the symbol address via an indirect
  599. symbol and that hence it should not call make_direct_ref() again }
  600. a_load_ref_reg_internal(list,OS_ADDR,OS_ADDR,href,hreg,true);
  601. if ref.base<>NR_NO then
  602. begin
  603. { fold symbol register into base register }
  604. reference_reset_base(href,hreg,0,ctempposinvalid,ref.alignment,[]);
  605. href.index:=ref.base;
  606. hreg:=getaddressregister(list);
  607. a_loadaddr_ref_reg(list,href,hreg);
  608. end;
  609. { we're done }
  610. ref.symbol:=nil;
  611. ref.base:=hreg;
  612. end;
  613. end;
  614. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  615. begin
  616. case t of
  617. OS_F32 :
  618. begin
  619. op:=A_FLD;
  620. s:=S_FS;
  621. end;
  622. OS_F64 :
  623. begin
  624. op:=A_FLD;
  625. s:=S_FL;
  626. end;
  627. OS_F80 :
  628. begin
  629. op:=A_FLD;
  630. s:=S_FX;
  631. end;
  632. OS_C64 :
  633. begin
  634. op:=A_FILD;
  635. s:=S_IQ;
  636. end;
  637. else
  638. internalerror(200204043);
  639. end;
  640. end;
  641. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  642. var
  643. op : tasmop;
  644. s : topsize;
  645. tmpref : treference;
  646. begin
  647. tmpref:=ref;
  648. make_simple_ref(list,tmpref);
  649. floatloadops(t,op,s);
  650. list.concat(Taicpu.Op_ref(op,s,tmpref));
  651. inc_fpu_stack;
  652. end;
  653. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  654. begin
  655. case t of
  656. OS_F32 :
  657. begin
  658. op:=A_FSTP;
  659. s:=S_FS;
  660. end;
  661. OS_F64 :
  662. begin
  663. op:=A_FSTP;
  664. s:=S_FL;
  665. end;
  666. OS_F80 :
  667. begin
  668. op:=A_FSTP;
  669. s:=S_FX;
  670. end;
  671. OS_C64 :
  672. begin
  673. op:=A_FISTP;
  674. s:=S_IQ;
  675. end;
  676. else
  677. internalerror(200204042);
  678. end;
  679. end;
  680. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  681. var
  682. op : tasmop;
  683. s : topsize;
  684. tmpref : treference;
  685. begin
  686. tmpref:=ref;
  687. make_simple_ref(list,tmpref);
  688. floatstoreops(t,op,s);
  689. list.concat(Taicpu.Op_ref(op,s,tmpref));
  690. { storing non extended floats can cause a floating point overflow }
  691. if ((t<>OS_F80) and (cs_fpu_fwait in current_settings.localswitches))
  692. {$ifdef i8086}
  693. { 8087 and 80287 need a FWAIT after a memory store, before it can be
  694. read with the integer unit }
  695. or (current_settings.cputype<=cpu_286)
  696. {$endif i8086}
  697. then
  698. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  699. dec_fpu_stack;
  700. end;
  701. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  702. begin
  703. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  704. internalerror(200306031);
  705. end;
  706. {****************************************************************************
  707. Assembler code
  708. ****************************************************************************}
  709. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  710. var
  711. r: treference;
  712. begin
  713. if (target_info.system <> system_i386_darwin) then
  714. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  715. else
  716. begin
  717. reference_reset_symbol(r,get_darwin_call_stub(s,false),0,sizeof(pint),[]);
  718. r.refaddr:=addr_full;
  719. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  720. end;
  721. end;
  722. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  723. begin
  724. a_jmp_cond(list, OC_NONE, l);
  725. end;
  726. function tcgx86.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  727. var
  728. stubname: string;
  729. begin
  730. stubname := 'L'+s+'$stub';
  731. result := current_asmdata.getasmsymbol(stubname);
  732. if assigned(result) then
  733. exit;
  734. if current_asmdata.asmlists[al_imports]=nil then
  735. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  736. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',0);
  737. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION,voidcodepointertype);
  738. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  739. { register as a weak symbol if necessary }
  740. if weak then
  741. current_asmdata.weakrefasmsymbol(s,AT_FUNCTION);
  742. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  743. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  744. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  745. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  746. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  747. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  748. end;
  749. procedure tcgx86.a_call_name(list : TAsmList;const s : string; weak: boolean);
  750. begin
  751. a_call_name_near(list,s,weak);
  752. end;
  753. procedure tcgx86.a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  754. var
  755. sym : tasmsymbol;
  756. r : treference;
  757. begin
  758. if (target_info.system <> system_i386_darwin) then
  759. begin
  760. if not(weak) then
  761. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  762. else
  763. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  764. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  765. if (cs_create_pic in current_settings.moduleswitches) and
  766. { darwin's assembler doesn't want @PLT after call symbols }
  767. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim,system_x86_64_iphonesim]) then
  768. begin
  769. r.refaddr:=addr_pic;
  770. end
  771. else
  772. r.refaddr:=addr_full;
  773. end
  774. else
  775. begin
  776. reference_reset_symbol(r,get_darwin_call_stub(s,weak),0,sizeof(pint),[]);
  777. r.refaddr:=addr_full;
  778. end;
  779. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  780. end;
  781. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  782. begin
  783. a_call_name_static_near(list,s);
  784. end;
  785. procedure tcgx86.a_call_name_static_near(list : TAsmList;const s : string);
  786. var
  787. sym : tasmsymbol;
  788. r : treference;
  789. begin
  790. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  791. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  792. r.refaddr:=addr_full;
  793. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  794. end;
  795. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  796. begin
  797. a_call_reg_near(list,reg);
  798. end;
  799. procedure tcgx86.a_call_reg_near(list: TAsmList; reg: tregister);
  800. begin
  801. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  802. end;
  803. {********************** load instructions ********************}
  804. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : tcgint; reg : TRegister);
  805. begin
  806. check_register_size(tosize,reg);
  807. { the optimizer will change it to "xor reg,reg" when loading zero, }
  808. { no need to do it here too (JM) }
  809. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  810. end;
  811. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  812. var
  813. tmpref : treference;
  814. begin
  815. tmpref:=ref;
  816. make_simple_ref(list,tmpref);
  817. {$ifdef x86_64}
  818. { x86_64 only supports signed 32 bits constants directly }
  819. if (tosize in [OS_S64,OS_64]) and
  820. ((a<low(longint)) or (a>high(longint))) then
  821. begin
  822. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  823. inc(tmpref.offset,4);
  824. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  825. end
  826. else
  827. {$endif x86_64}
  828. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  829. end;
  830. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  831. var
  832. op: tasmop;
  833. s: topsize;
  834. tmpsize : tcgsize;
  835. tmpreg : tregister;
  836. tmpref : treference;
  837. begin
  838. tmpref:=ref;
  839. make_simple_ref(list,tmpref);
  840. if TCGSize2Size[fromsize]>TCGSize2Size[tosize] then
  841. begin
  842. fromsize:=tosize;
  843. reg:=makeregsize(list,reg,fromsize);
  844. end;
  845. check_register_size(fromsize,reg);
  846. sizes2load(fromsize,tosize,op,s);
  847. case s of
  848. {$ifdef x86_64}
  849. S_BQ,S_WQ,S_LQ,
  850. {$endif x86_64}
  851. S_BW,S_BL,S_WL :
  852. begin
  853. tmpreg:=getintregister(list,tosize);
  854. {$ifdef x86_64}
  855. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  856. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  857. 64 bit (FK) }
  858. if s in [S_BL,S_WL,S_L] then
  859. begin
  860. tmpreg:=makeregsize(list,tmpreg,OS_32);
  861. tmpsize:=OS_32;
  862. end
  863. else
  864. {$endif x86_64}
  865. tmpsize:=tosize;
  866. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  867. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  868. end;
  869. else
  870. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  871. end;
  872. end;
  873. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  874. begin
  875. a_load_ref_reg_internal(list,fromsize,tosize,ref,reg,false);
  876. end;
  877. procedure tcgx86.a_load_ref_reg_internal(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister;isdirect:boolean);
  878. var
  879. op: tasmop;
  880. s: topsize;
  881. tmpref : treference;
  882. begin
  883. tmpref:=ref;
  884. make_simple_ref(list,tmpref,isdirect);
  885. check_register_size(tosize,reg);
  886. sizes2load(fromsize,tosize,op,s);
  887. {$ifdef x86_64}
  888. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  889. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  890. 64 bit (FK) }
  891. if s in [S_BL,S_WL,S_L] then
  892. reg:=makeregsize(list,reg,OS_32);
  893. {$endif x86_64}
  894. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  895. end;
  896. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  897. var
  898. op: tasmop;
  899. s: topsize;
  900. instr:Taicpu;
  901. begin
  902. check_register_size(fromsize,reg1);
  903. check_register_size(tosize,reg2);
  904. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  905. begin
  906. reg1:=makeregsize(list,reg1,tosize);
  907. s:=tcgsize2opsize[tosize];
  908. op:=A_MOV;
  909. end
  910. else
  911. sizes2load(fromsize,tosize,op,s);
  912. {$ifdef x86_64}
  913. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  914. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  915. 64 bit (FK)
  916. }
  917. if s in [S_BL,S_WL,S_L] then
  918. reg2:=makeregsize(list,reg2,OS_32);
  919. {$endif x86_64}
  920. if (reg1<>reg2) then
  921. begin
  922. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  923. { Notify the register allocator that we have written a move instruction so
  924. it can try to eliminate it. }
  925. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  926. add_move_instruction(instr);
  927. list.concat(instr);
  928. end;
  929. {$ifdef x86_64}
  930. { avoid merging of registers and killing the zero extensions (FK) }
  931. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  932. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  933. {$endif x86_64}
  934. end;
  935. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  936. var
  937. dirref,tmpref : treference;
  938. tmpreg : TRegister;
  939. begin
  940. dirref:=ref;
  941. { this could probably done in a more optimized way, but for now this
  942. is sufficent }
  943. make_direct_ref(list,dirref);
  944. with dirref do
  945. begin
  946. {$ifdef i386}
  947. if refaddr=addr_ntpoff then
  948. begin
  949. { Convert thread local address to a process global addres
  950. as we cannot handle far pointers.}
  951. case target_info.system of
  952. system_i386_linux,system_i386_android:
  953. if segment=NR_GS then
  954. begin
  955. reference_reset(tmpref,1,[]);
  956. tmpref.segment:=NR_GS;
  957. tmpreg:=getaddressregister(list);
  958. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,tmpreg);
  959. reference_reset(tmpref,1,[]);
  960. tmpref.symbol:=symbol;
  961. tmpref.refaddr:=refaddr;
  962. tmpref.base:=tmpreg;
  963. if base<>NR_NO then
  964. tmpref.index:=base;
  965. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,tmpreg));
  966. segment:=NR_NO;
  967. base:=tmpreg;
  968. symbol:=nil;
  969. refaddr:=addr_no;
  970. end
  971. else
  972. Internalerror(2018110402);
  973. else
  974. Internalerror(2018110403);
  975. end;
  976. end;
  977. {$endif i386}
  978. {$ifdef x86_64}
  979. if refaddr=addr_tpoff then
  980. begin
  981. { Convert thread local address to a process global addres
  982. as we cannot handle far pointers.}
  983. case target_info.system of
  984. system_x86_64_linux:
  985. if segment=NR_FS then
  986. begin
  987. reference_reset(tmpref,1,[]);
  988. tmpref.segment:=NR_FS;
  989. tmpreg:=getaddressregister(list);
  990. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,tmpreg);
  991. reference_reset(tmpref,1,[]);
  992. tmpref.symbol:=symbol;
  993. tmpref.refaddr:=refaddr;
  994. tmpref.base:=tmpreg;
  995. if base<>NR_NO then
  996. tmpref.index:=base;
  997. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,tmpreg));
  998. segment:=NR_NO;
  999. base:=tmpreg;
  1000. symbol:=nil;
  1001. refaddr:=addr_no;
  1002. end
  1003. else
  1004. Internalerror(2019012003);
  1005. else
  1006. Internalerror(2019012004);
  1007. end;
  1008. end;
  1009. {$endif x86_64}
  1010. if (base=NR_NO) and (index=NR_NO) then
  1011. begin
  1012. if assigned(dirref.symbol) then
  1013. begin
  1014. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  1015. ((dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  1016. (cs_create_pic in current_settings.moduleswitches)) then
  1017. begin
  1018. if (dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  1019. ((cs_create_pic in current_settings.moduleswitches) and
  1020. (dirref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  1021. begin
  1022. reference_reset_base(tmpref,
  1023. g_indirect_sym_load(list,dirref.symbol.name,asmsym2indsymflags(dirref.symbol)),
  1024. offset,ctempposinvalid,sizeof(pint),[]);
  1025. a_loadaddr_ref_reg(list,tmpref,r);
  1026. end
  1027. else
  1028. begin
  1029. include(current_procinfo.flags,pi_needs_got);
  1030. reference_reset_base(tmpref,current_procinfo.got,offset,dirref.temppos,dirref.alignment,[]);
  1031. tmpref.symbol:=symbol;
  1032. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  1033. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  1034. end;
  1035. end
  1036. else if (cs_create_pic in current_settings.moduleswitches)
  1037. {$ifdef x86_64}
  1038. and not(dirref.symbol.bind=AB_LOCAL)
  1039. {$endif x86_64}
  1040. then
  1041. begin
  1042. {$ifdef x86_64}
  1043. reference_reset_symbol(tmpref,dirref.symbol,0,sizeof(pint),[]);
  1044. tmpref.refaddr:=addr_pic;
  1045. tmpref.base:=NR_RIP;
  1046. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  1047. {$else x86_64}
  1048. reference_reset_symbol(tmpref,dirref.symbol,0,sizeof(pint),[]);
  1049. tmpref.refaddr:=addr_pic;
  1050. tmpref.base:=current_procinfo.got;
  1051. include(current_procinfo.flags,pi_needs_got);
  1052. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  1053. {$endif x86_64}
  1054. if offset<>0 then
  1055. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  1056. end
  1057. {$ifdef x86_64}
  1058. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim]))
  1059. or (cs_create_pic in current_settings.moduleswitches)
  1060. then
  1061. begin
  1062. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  1063. tmpref:=dirref;
  1064. tmpref.base:=NR_RIP;
  1065. tmpref.refaddr:=addr_pic_no_got;
  1066. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  1067. end
  1068. {$endif x86_64}
  1069. else
  1070. begin
  1071. tmpref:=dirref;
  1072. tmpref.refaddr:=ADDR_FULL;
  1073. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  1074. end
  1075. end
  1076. else
  1077. a_load_const_reg(list,OS_ADDR,offset,r)
  1078. end
  1079. else if (base=NR_NO) and (index<>NR_NO) and
  1080. (offset=0) and (scalefactor=0) and (symbol=nil) then
  1081. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  1082. else if (base<>NR_NO) and (index=NR_NO) and
  1083. (offset=0) and (symbol=nil) then
  1084. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  1085. else
  1086. begin
  1087. tmpref:=dirref;
  1088. make_simple_ref(list,tmpref);
  1089. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  1090. end;
  1091. if segment<>NR_NO then
  1092. begin
  1093. {$ifdef i8086}
  1094. if is_segment_reg(segment) then
  1095. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,segment,GetNextReg(r)))
  1096. else
  1097. a_load_reg_reg(list,OS_16,OS_16,segment,GetNextReg(r));
  1098. {$else i8086}
  1099. cgmessage(cg_e_cant_use_far_pointer_there);
  1100. {$endif i8086}
  1101. end;
  1102. end;
  1103. end;
  1104. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  1105. { R_ST means "the current value at the top of the fpu stack" (JM) }
  1106. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  1107. var
  1108. href: treference;
  1109. op: tasmop;
  1110. s: topsize;
  1111. begin
  1112. if (reg1<>NR_ST) then
  1113. begin
  1114. floatloadops(tosize,op,s);
  1115. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  1116. inc_fpu_stack;
  1117. end;
  1118. if (reg2<>NR_ST) then
  1119. begin
  1120. floatstoreops(tosize,op,s);
  1121. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  1122. dec_fpu_stack;
  1123. end;
  1124. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  1125. if (reg1=NR_ST) and
  1126. (reg2=NR_ST) and
  1127. (tosize<>OS_F80) and
  1128. (tosize<fromsize) then
  1129. begin
  1130. { can't round down to lower precision in x87 :/ }
  1131. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  1132. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  1133. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  1134. tg.ungettemp(list,href);
  1135. end;
  1136. end;
  1137. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1138. var
  1139. tmpref : treference;
  1140. begin
  1141. tmpref:=ref;
  1142. make_simple_ref(list,tmpref);
  1143. floatload(list,fromsize,tmpref);
  1144. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1145. end;
  1146. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1147. var
  1148. tmpref : treference;
  1149. begin
  1150. tmpref:=ref;
  1151. make_simple_ref(list,tmpref);
  1152. { in case a record returned in a floating point register
  1153. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1154. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1155. tosize }
  1156. if (fromsize in [OS_F32,OS_F64]) and
  1157. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1158. case tosize of
  1159. OS_32:
  1160. tosize:=OS_F32;
  1161. OS_64:
  1162. tosize:=OS_F64;
  1163. else
  1164. ;
  1165. end;
  1166. if reg<>NR_ST then
  1167. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1168. floatstore(list,tosize,tmpref);
  1169. end;
  1170. procedure tcgx86.a_loadfpu_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference; const cgpara: TCGPara);
  1171. var
  1172. href: treference;
  1173. begin
  1174. if cgpara.location^.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  1175. begin
  1176. cgpara.check_simple_location;
  1177. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1178. floatload(list,size,ref);
  1179. floatstore(list,size,href);
  1180. end
  1181. else
  1182. inherited a_loadfpu_ref_cgpara(list, size, ref, cgpara);
  1183. end;
  1184. function get_scalar_mm_op(fromsize,tosize : tcgsize;aligned : boolean) : tasmop;
  1185. const
  1186. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1187. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1188. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1189. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1190. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1191. (A_NONE,A_NONE,A_NONE,A_NONE,A_MOVAPS));
  1192. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1193. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1194. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1195. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1196. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1197. (A_NONE,A_NONE,A_NONE,A_NONE,A_VMOVAPS));
  1198. begin
  1199. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1200. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1201. if (fromsize in [OS_F32,OS_F64]) and
  1202. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1203. case tosize of
  1204. OS_32:
  1205. tosize:=OS_F32;
  1206. OS_64:
  1207. tosize:=OS_F64;
  1208. else
  1209. ;
  1210. end;
  1211. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1212. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1213. begin
  1214. if UseAVX then
  1215. result:=convertopavx[fromsize,tosize]
  1216. else
  1217. result:=convertopsse[fromsize,tosize];
  1218. end
  1219. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1220. OS_64 (record in memory/LOC_REFERENCE) }
  1221. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1222. begin
  1223. case fromsize of
  1224. OS_M64:
  1225. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1226. OS_64 (record in memory/LOC_REFERENCE) }
  1227. if UseAVX then
  1228. result:=A_VMOVQ
  1229. else
  1230. result:=A_MOVQ;
  1231. OS_M128:
  1232. { 128-bit aligned vector }
  1233. if UseAVX then
  1234. begin
  1235. if aligned then
  1236. result:=A_VMOVAPS
  1237. else
  1238. result:=A_VMOVUPS;
  1239. end
  1240. else if aligned then
  1241. result:=A_MOVAPS
  1242. else
  1243. result:=A_MOVUPS;
  1244. OS_M256,
  1245. OS_M512:
  1246. { 256-bit aligned vector }
  1247. if UseAVX then
  1248. result:=A_VMOVAPS
  1249. else
  1250. { SSE does not support 256-bit or 512-bit vectors }
  1251. InternalError(2018012930);
  1252. else
  1253. InternalError(2018012920);
  1254. end;
  1255. end
  1256. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1257. (fromsize=OS_M128) then
  1258. begin
  1259. if UseAVX then
  1260. result:=A_VMOVDQU
  1261. else
  1262. result:=A_MOVDQU;
  1263. end
  1264. else
  1265. internalerror(2010060104);
  1266. if result=A_NONE then
  1267. internalerror(200312205);
  1268. end;
  1269. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1270. var
  1271. instr : taicpu;
  1272. op : TAsmOp;
  1273. begin
  1274. if shuffle=nil then
  1275. begin
  1276. if fromsize=tosize then
  1277. { needs correct size in case of spilling }
  1278. case fromsize of
  1279. OS_F32:
  1280. if UseAVX then
  1281. instr:=taicpu.op_reg_reg(A_VMOVAPS,S_NO,reg1,reg2)
  1282. else
  1283. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1284. OS_F64:
  1285. if UseAVX then
  1286. instr:=taicpu.op_reg_reg(A_VMOVAPD,S_NO,reg1,reg2)
  1287. else
  1288. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1289. OS_M64:
  1290. if UseAVX then
  1291. instr:=taicpu.op_reg_reg(A_VMOVQ,S_NO,reg1,reg2)
  1292. else
  1293. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1294. OS_M128:
  1295. if UseAVX then
  1296. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1297. else
  1298. instr:=taicpu.op_reg_reg(A_MOVDQA,S_NO,reg1,reg2);
  1299. OS_M256,
  1300. OS_M512:
  1301. if UseAVX then
  1302. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1303. else
  1304. { SSE doesn't support 512-bit vectors }
  1305. InternalError(2018012933);
  1306. else
  1307. internalerror(2006091201);
  1308. end
  1309. else
  1310. internalerror(200312202);
  1311. add_move_instruction(instr);
  1312. end
  1313. else if shufflescalar(shuffle) then
  1314. begin
  1315. op:=get_scalar_mm_op(fromsize,tosize,true);
  1316. { MOVAPD/MOVAPS are normally faster }
  1317. if op=A_MOVSD then
  1318. op:=A_MOVAPD
  1319. else if op=A_MOVSS then
  1320. op:=A_MOVAPS
  1321. { VMOVSD/SS is not available with two register operands }
  1322. else if op=A_VMOVSD then
  1323. op:=A_VMOVAPD
  1324. else if op=A_VMOVSS then
  1325. op:=A_VMOVAPS;
  1326. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1327. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1328. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1329. else
  1330. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1331. case op of
  1332. A_VMOVAPD,
  1333. A_VMOVAPS,
  1334. A_VMOVSS,
  1335. A_VMOVSD,
  1336. A_VMOVQ,
  1337. A_MOVAPD,
  1338. A_MOVAPS,
  1339. A_MOVSS,
  1340. A_MOVSD,
  1341. A_MOVQ:
  1342. add_move_instruction(instr);
  1343. else
  1344. ;
  1345. end;
  1346. end
  1347. else
  1348. internalerror(200312201);
  1349. list.concat(instr);
  1350. end;
  1351. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1352. var
  1353. tmpref : treference;
  1354. op : tasmop;
  1355. begin
  1356. tmpref:=ref;
  1357. make_simple_ref(list,tmpref);
  1358. if shuffle=nil then
  1359. begin
  1360. case fromsize of
  1361. OS_F32:
  1362. if UseAVX then
  1363. op := A_VMOVSS
  1364. else
  1365. op := A_MOVSS;
  1366. OS_F64:
  1367. if UseAVX then
  1368. op := A_VMOVSD
  1369. else
  1370. op := A_MOVSD;
  1371. OS_M32, OS_32, OS_S32:
  1372. if UseAVX then
  1373. op := A_VMOVD
  1374. else
  1375. op := A_MOVD;
  1376. OS_M64, OS_64, OS_S64:
  1377. { there is no VMOVQ for MMX registers }
  1378. if UseAVX and (getregtype(reg)<>R_MMXREGISTER) then
  1379. op := A_VMOVQ
  1380. else
  1381. op := A_MOVQ;
  1382. OS_128,
  1383. OS_M128:
  1384. { Use XMM integer transfer }
  1385. if UseAVX then
  1386. begin
  1387. if GetRefAlignment(tmpref) = 16 then
  1388. op := A_VMOVDQA
  1389. else
  1390. op := A_VMOVDQU;
  1391. end
  1392. else
  1393. begin
  1394. if GetRefAlignment(tmpref) = 16 then
  1395. op := A_MOVDQA
  1396. else
  1397. op := A_MOVDQU;
  1398. end;
  1399. OS_M256:
  1400. { Use YMM integer transfer }
  1401. if UseAVX then
  1402. begin
  1403. if GetRefAlignment(tmpref) = 32 then
  1404. op := A_VMOVDQA
  1405. else
  1406. op := A_VMOVDQU;
  1407. end
  1408. else
  1409. { SSE doesn't support 256-bit vectors }
  1410. Internalerror(2020010401);
  1411. OS_M512:
  1412. { Use ZMM integer transfer }
  1413. if UseAVX then
  1414. begin
  1415. if GetRefAlignment(tmpref) = 64 then
  1416. op := A_VMOVDQA64
  1417. else
  1418. op := A_VMOVDQU64;
  1419. end
  1420. else
  1421. { SSE doesn't support 512-bit vectors }
  1422. InternalError(2018012939);
  1423. else
  1424. { No valid transfer command available }
  1425. internalerror(2017121410);
  1426. end;
  1427. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg));
  1428. end
  1429. else if shufflescalar(shuffle) then
  1430. begin
  1431. op:=get_scalar_mm_op(fromsize,tosize,tcgsize2size[fromsize]=ref.alignment);
  1432. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1433. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1434. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1435. else
  1436. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1437. end
  1438. else
  1439. internalerror(200312252);
  1440. end;
  1441. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1442. var
  1443. hreg : tregister;
  1444. tmpref : treference;
  1445. op : tasmop;
  1446. begin
  1447. tmpref:=ref;
  1448. make_simple_ref(list,tmpref);
  1449. if shuffle=nil then
  1450. begin
  1451. case fromsize of
  1452. OS_F32:
  1453. if UseAVX then
  1454. op := A_VMOVSS
  1455. else
  1456. op := A_MOVSS;
  1457. OS_F64:
  1458. if UseAVX then
  1459. op := A_VMOVSD
  1460. else
  1461. op := A_MOVSD;
  1462. OS_M32, OS_32, OS_S32:
  1463. if UseAVX then
  1464. op := A_VMOVD
  1465. else
  1466. op := A_MOVD;
  1467. OS_M64, OS_64, OS_S64:
  1468. { there is no VMOVQ for MMX registers }
  1469. if UseAVX and (getregtype(reg)<>R_MMXREGISTER) then
  1470. op := A_VMOVQ
  1471. else
  1472. op := A_MOVQ;
  1473. OS_M128:
  1474. { Use XMM integer transfer }
  1475. if UseAVX then
  1476. begin
  1477. if GetRefAlignment(tmpref) = 16 then
  1478. op := A_VMOVDQA
  1479. else
  1480. op := A_VMOVDQU;
  1481. end else
  1482. begin
  1483. if GetRefAlignment(tmpref) = 16 then
  1484. op := A_MOVDQA
  1485. else
  1486. op := A_MOVDQU;
  1487. end;
  1488. OS_M256:
  1489. { Use XMM integer transfer }
  1490. if UseAVX then
  1491. begin
  1492. if GetRefAlignment(tmpref) = 32 then
  1493. op := A_VMOVDQA
  1494. else
  1495. op := A_VMOVDQU;
  1496. end else
  1497. { SSE doesn't support 256-bit vectors }
  1498. InternalError(2018012942);
  1499. OS_M512:
  1500. { Use XMM integer transfer }
  1501. if UseAVX then
  1502. begin
  1503. if GetRefAlignment(tmpref) = 64 then
  1504. op := A_VMOVDQA64
  1505. else
  1506. op := A_VMOVDQU64;
  1507. end else
  1508. { SSE doesn't support 512-bit vectors }
  1509. InternalError(2018012945);
  1510. else
  1511. { No valid transfer command available }
  1512. internalerror(2017121411);
  1513. end;
  1514. list.concat(taicpu.op_reg_ref(op,S_NO,reg,tmpref));
  1515. end
  1516. else if shufflescalar(shuffle) then
  1517. begin
  1518. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1519. begin
  1520. hreg:=getmmregister(list,tosize);
  1521. op:=get_scalar_mm_op(fromsize,tosize,tcgsize2size[tosize]=ref.alignment);
  1522. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1523. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1524. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1525. else
  1526. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1527. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize,tcgsize2size[tosize]=tmpref.alignment),S_NO,hreg,tmpref))
  1528. end
  1529. else
  1530. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize,tcgsize2size[tosize]=tmpref.alignment),S_NO,reg,tmpref));
  1531. end
  1532. else
  1533. internalerror(2003122501);
  1534. end;
  1535. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1536. var
  1537. l : tlocation;
  1538. begin
  1539. l.loc:=LOC_REFERENCE;
  1540. l.reference:=ref;
  1541. l.size:=size;
  1542. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1543. end;
  1544. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1545. var
  1546. l : tlocation;
  1547. begin
  1548. l.loc:=LOC_MMREGISTER;
  1549. l.register:=src;
  1550. l.size:=size;
  1551. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1552. end;
  1553. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1554. const
  1555. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1556. ( { scalar }
  1557. ( { OS_F32 }
  1558. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_NOP,A_NOP,A_NOP
  1559. ),
  1560. ( { OS_F64 }
  1561. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_NOP,A_NOP,A_NOP
  1562. )
  1563. ),
  1564. ( { vectorized/packed }
  1565. { because the logical packed single instructions have shorter op codes, we use always
  1566. these
  1567. }
  1568. ( { OS_F32 }
  1569. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1570. ),
  1571. ( { OS_F64 }
  1572. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1573. )
  1574. )
  1575. );
  1576. var
  1577. resultreg : tregister;
  1578. asmop : tasmop;
  1579. begin
  1580. { this is an internally used procedure so the parameters have
  1581. some constrains
  1582. }
  1583. if loc.size<>size then
  1584. internalerror(2013061108);
  1585. resultreg:=dst;
  1586. { deshuffle }
  1587. //!!!
  1588. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1589. begin
  1590. internalerror(2013061107);
  1591. end
  1592. else if (shuffle=nil) then
  1593. asmop:=opmm2asmop[1,size,op]
  1594. else if shufflescalar(shuffle) then
  1595. begin
  1596. asmop:=opmm2asmop[0,size,op];
  1597. { no scalar operation available? }
  1598. if asmop=A_NOP then
  1599. begin
  1600. { do vectorized and shuffle finally }
  1601. internalerror(2010060103);
  1602. end;
  1603. end
  1604. else
  1605. internalerror(2013061106);
  1606. if asmop=A_NOP then
  1607. internalerror(2013061105);
  1608. case loc.loc of
  1609. LOC_CREFERENCE,LOC_REFERENCE:
  1610. begin
  1611. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1612. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1613. end;
  1614. LOC_CMMREGISTER,LOC_MMREGISTER:
  1615. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1616. else
  1617. internalerror(2013061104);
  1618. end;
  1619. { shuffle }
  1620. if resultreg<>dst then
  1621. begin
  1622. internalerror(2013061103);
  1623. end;
  1624. end;
  1625. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1626. var
  1627. l : tlocation;
  1628. begin
  1629. l.loc:=LOC_MMREGISTER;
  1630. l.register:=src1;
  1631. l.size:=size;
  1632. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1633. end;
  1634. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1635. var
  1636. l : tlocation;
  1637. begin
  1638. l.loc:=LOC_REFERENCE;
  1639. l.reference:=ref;
  1640. l.size:=size;
  1641. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1642. end;
  1643. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1644. const
  1645. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1646. ( { scalar }
  1647. ( { OS_F32 }
  1648. A_NOP,A_NOP,A_ADDSS,A_NOP,A_DIVSS,A_NOP,A_NOP,A_MULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSS,A_XORPS,A_NOP,A_NOP
  1649. ),
  1650. ( { OS_F64 }
  1651. A_NOP,A_NOP,A_ADDSD,A_NOP,A_DIVSD,A_NOP,A_NOP,A_MULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSD,A_XORPD,A_NOP,A_NOP
  1652. )
  1653. ),
  1654. ( { vectorized/packed }
  1655. { because the logical packed single instructions have shorter op codes, we use always
  1656. these
  1657. }
  1658. ( { OS_F32 }
  1659. A_NOP,A_NOP,A_ADDPS,A_NOP,A_DIVPS,A_NOP,A_NOP,A_MULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPS,A_XORPS,A_NOP,A_NOP
  1660. ),
  1661. ( { OS_F64 }
  1662. A_NOP,A_NOP,A_ADDPD,A_NOP,A_DIVPD,A_NOP,A_NOP,A_MULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPD,A_XORPD,A_NOP,A_NOP
  1663. )
  1664. )
  1665. );
  1666. opmm2asmop_avx : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1667. ( { scalar }
  1668. ( { OS_F32 }
  1669. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_VXORPS,A_NOP,A_NOP
  1670. ),
  1671. ( { OS_F64 }
  1672. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_VXORPD,A_NOP,A_NOP
  1673. )
  1674. ),
  1675. ( { vectorized/packed }
  1676. { because the logical packed single instructions have shorter op codes, we use always
  1677. these
  1678. }
  1679. ( { OS_F32 }
  1680. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1681. ),
  1682. ( { OS_F64 }
  1683. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1684. )
  1685. )
  1686. );
  1687. opmm2asmop_full : array[topcg] of tasmop = (
  1688. A_NOP,A_NOP,A_NOP,A_PAND,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_POR,A_NOP,A_NOP,A_NOP,A_NOP,A_PXOR,A_NOP,A_NOP
  1689. );
  1690. opmm2asmop_full_avx : array[topcg] of tasmop = (
  1691. A_NOP,A_NOP,A_NOP,A_VPAND,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VPOR,A_NOP,A_NOP,A_NOP,A_NOP,A_VPXOR,A_NOP,A_NOP
  1692. );
  1693. var
  1694. resultreg : tregister;
  1695. asmop : tasmop;
  1696. begin
  1697. { this is an internally used procedure so the parameters have
  1698. some constrains
  1699. }
  1700. if loc.size<>size then
  1701. internalerror(200312213);
  1702. resultreg:=dst;
  1703. { deshuffle }
  1704. //!!!
  1705. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1706. begin
  1707. internalerror(2010060101);
  1708. end
  1709. else if shuffle=nil then
  1710. begin
  1711. if UseAVX then
  1712. begin
  1713. asmop:=opmm2asmop_full_avx[op];
  1714. if size in [OS_M256,OS_M512] then
  1715. Include(current_procinfo.flags,pi_uses_ymm);
  1716. end
  1717. else
  1718. asmop:=opmm2asmop_full[op];
  1719. end
  1720. else if shufflescalar(shuffle) then
  1721. begin
  1722. if UseAVX then
  1723. begin
  1724. asmop:=opmm2asmop_avx[0,size,op];
  1725. if size in [OS_M256,OS_M512] then
  1726. Include(current_procinfo.flags,pi_uses_ymm);
  1727. end
  1728. else
  1729. asmop:=opmm2asmop[0,size,op];
  1730. end
  1731. else
  1732. internalerror(200312211);
  1733. if asmop=A_NOP then
  1734. internalerror(200312216);
  1735. case loc.loc of
  1736. LOC_CREFERENCE,LOC_REFERENCE:
  1737. begin
  1738. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1739. if UseAVX then
  1740. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,resultreg,resultreg))
  1741. else
  1742. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1743. end;
  1744. LOC_CMMREGISTER,LOC_MMREGISTER:
  1745. if UseAVX then
  1746. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,resultreg,resultreg))
  1747. else
  1748. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1749. else
  1750. internalerror(200312214);
  1751. end;
  1752. { shuffle }
  1753. if resultreg<>dst then
  1754. begin
  1755. internalerror(200312212);
  1756. end;
  1757. end;
  1758. {$ifndef i8086}
  1759. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1760. a:tcgint;src,dst:Tregister);
  1761. var
  1762. power,al : longint;
  1763. href : treference;
  1764. begin
  1765. power:=0;
  1766. optimize_op_const(size,op,a);
  1767. case op of
  1768. OP_NONE:
  1769. begin
  1770. a_load_reg_reg(list,size,size,src,dst);
  1771. exit;
  1772. end;
  1773. OP_MOVE:
  1774. begin
  1775. a_load_const_reg(list,size,a,dst);
  1776. exit;
  1777. end;
  1778. else
  1779. ;
  1780. end;
  1781. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1782. not(cs_check_overflow in current_settings.localswitches) and
  1783. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1784. begin
  1785. reference_reset_base(href,src,0,ctempposinvalid,0,[]);
  1786. href.index:=src;
  1787. href.scalefactor:=a-1;
  1788. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1789. end
  1790. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1791. not(cs_check_overflow in current_settings.localswitches) and
  1792. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1793. begin
  1794. reference_reset_base(href,NR_NO,0,ctempposinvalid,0,[]);
  1795. href.index:=src;
  1796. href.scalefactor:=a;
  1797. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1798. end
  1799. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1800. (a>1) and (a<=maxLongint) and not ispowerof2(int64(a),power) then
  1801. begin
  1802. { MUL with overflow checking should be handled specifically in the code generator }
  1803. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1804. internalerror(2014011801);
  1805. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1806. end
  1807. else if (op=OP_ADD) and
  1808. ((size in [OS_32,OS_S32]) or
  1809. { lea supports only 32 bit signed displacments }
  1810. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1811. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1812. ) and
  1813. not(cs_check_overflow in current_settings.localswitches) then
  1814. begin
  1815. { a might still be in the range 0x80000000 to 0xffffffff
  1816. which might trigger a range check error as
  1817. reference_reset_base expects a longint value. }
  1818. {$push} {$R-}{$Q-}
  1819. al := longint (a);
  1820. {$pop}
  1821. reference_reset_base(href,src,al,ctempposinvalid,0,[]);
  1822. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1823. end
  1824. else if (op=OP_SHL) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1825. (int64(a)>=1) and (int64(a)<=3) then
  1826. begin
  1827. reference_reset_base(href,NR_NO,0,ctempposinvalid,0,[]);
  1828. href.index:=src;
  1829. href.scalefactor:=1 shl longint(a);
  1830. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1831. end
  1832. else if (op=OP_SUB) and
  1833. ((size in [OS_32,OS_S32]) or
  1834. { lea supports only 32 bit signed displacments }
  1835. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1836. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1837. ) and
  1838. not(cs_check_overflow in current_settings.localswitches) then
  1839. begin
  1840. reference_reset_base(href,src,-a,ctempposinvalid,0,[]);
  1841. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1842. end
  1843. else if (op in [OP_ROR,OP_ROL]) and
  1844. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1845. (size in [OS_32,OS_S32
  1846. {$ifdef x86_64}
  1847. ,OS_64,OS_S64
  1848. {$endif x86_64}
  1849. ]) then
  1850. begin
  1851. if op=OP_ROR then
  1852. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1853. else
  1854. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1855. end
  1856. else
  1857. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1858. end;
  1859. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1860. size: tcgsize; src1, src2, dst: tregister);
  1861. var
  1862. href : treference;
  1863. begin
  1864. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1865. not(cs_check_overflow in current_settings.localswitches) then
  1866. begin
  1867. reference_reset_base(href,src1,0,ctempposinvalid,0,[]);
  1868. href.index:=src2;
  1869. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1870. end
  1871. else if (op in [OP_SHR,OP_SHL]) and
  1872. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1873. (size in [OS_32,OS_S32
  1874. {$ifdef x86_64}
  1875. ,OS_64,OS_S64
  1876. {$endif x86_64}
  1877. ]) then
  1878. begin
  1879. if op=OP_SHL then
  1880. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1881. else
  1882. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1883. end
  1884. else
  1885. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1886. end;
  1887. {$endif not i8086}
  1888. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1889. {$ifdef x86_64}
  1890. var
  1891. tmpreg : tregister;
  1892. {$endif x86_64}
  1893. begin
  1894. optimize_op_const(size, op, a);
  1895. {$ifdef x86_64}
  1896. { x86_64 only supports signed 32 bits constants directly }
  1897. if not(op in [OP_NONE,OP_MOVE]) and
  1898. (size in [OS_S64,OS_64]) and
  1899. ((a<low(longint)) or (a>high(longint))) then
  1900. begin
  1901. tmpreg:=getintregister(list,size);
  1902. a_load_const_reg(list,size,a,tmpreg);
  1903. a_op_reg_reg(list,op,size,tmpreg,reg);
  1904. exit;
  1905. end;
  1906. {$endif x86_64}
  1907. check_register_size(size,reg);
  1908. case op of
  1909. OP_NONE :
  1910. begin
  1911. { Opcode is optimized away }
  1912. end;
  1913. OP_MOVE :
  1914. begin
  1915. { Optimized, replaced with a simple load }
  1916. a_load_const_reg(list,size,a,reg);
  1917. end;
  1918. OP_DIV, OP_IDIV:
  1919. begin
  1920. { should be handled specifically in the code }
  1921. { generator because of the silly register usage restraints }
  1922. internalerror(200109224);
  1923. end;
  1924. OP_MUL,OP_IMUL:
  1925. begin
  1926. if not (cs_check_overflow in current_settings.localswitches) then
  1927. op:=OP_IMUL;
  1928. if op = OP_IMUL then
  1929. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1930. else
  1931. { OP_MUL should be handled specifically in the code }
  1932. { generator because of the silly register usage restraints }
  1933. internalerror(200109225);
  1934. end;
  1935. OP_ADD, OP_SUB:
  1936. if not(cs_check_overflow in current_settings.localswitches) and
  1937. (a = 1) and
  1938. UseIncDec then
  1939. begin
  1940. if op = OP_ADD then
  1941. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1942. else
  1943. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1944. end
  1945. else
  1946. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1947. OP_AND,OP_OR:
  1948. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1949. OP_XOR:
  1950. if (aword(a)=high(aword)) then
  1951. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  1952. else
  1953. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1954. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1955. begin
  1956. {$if defined(x86_64)}
  1957. if (a and 63) <> 0 Then
  1958. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1959. if (a shr 6) <> 0 Then
  1960. internalerror(200609073);
  1961. {$elseif defined(i386)}
  1962. if (a and 31) <> 0 Then
  1963. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1964. if (a shr 5) <> 0 Then
  1965. internalerror(200609071);
  1966. {$elseif defined(i8086)}
  1967. if (a shr 5) <> 0 Then
  1968. internalerror(2013043002);
  1969. a := a and 31;
  1970. if a <> 0 Then
  1971. begin
  1972. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1973. begin
  1974. getcpuregister(list,NR_CL);
  1975. a_load_const_reg(list,OS_8,a,NR_CL);
  1976. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1977. ungetcpuregister(list,NR_CL);
  1978. end
  1979. else
  1980. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1981. end;
  1982. {$endif}
  1983. end
  1984. else internalerror(200609072);
  1985. end;
  1986. end;
  1987. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1988. var
  1989. {$ifdef x86_64}
  1990. tmpreg : tregister;
  1991. {$endif x86_64}
  1992. tmpref : treference;
  1993. begin
  1994. optimize_op_const(size, op, a);
  1995. if op in [OP_NONE,OP_MOVE] then
  1996. begin
  1997. if (op=OP_MOVE) then
  1998. a_load_const_ref(list,size,a,ref);
  1999. exit;
  2000. end;
  2001. {$ifdef x86_64}
  2002. { x86_64 only supports signed 32 bits constants directly }
  2003. if (size in [OS_S64,OS_64]) and
  2004. ((a<low(longint)) or (a>high(longint))) then
  2005. begin
  2006. tmpreg:=getintregister(list,size);
  2007. a_load_const_reg(list,size,a,tmpreg);
  2008. a_op_reg_ref(list,op,size,tmpreg,ref);
  2009. exit;
  2010. end;
  2011. {$endif x86_64}
  2012. tmpref:=ref;
  2013. make_simple_ref(list,tmpref);
  2014. Case Op of
  2015. OP_DIV, OP_IDIV:
  2016. Begin
  2017. { should be handled specifically in the code }
  2018. { generator because of the silly register usage restraints }
  2019. internalerror(200109231);
  2020. End;
  2021. OP_MUL,OP_IMUL:
  2022. begin
  2023. if not (cs_check_overflow in current_settings.localswitches) then
  2024. op:=OP_IMUL;
  2025. { can't multiply a memory location directly with a constant }
  2026. if op = OP_IMUL then
  2027. inherited a_op_const_ref(list,op,size,a,tmpref)
  2028. else
  2029. { OP_MUL should be handled specifically in the code }
  2030. { generator because of the silly register usage restraints }
  2031. internalerror(200109232);
  2032. end;
  2033. OP_ADD, OP_SUB:
  2034. if not(cs_check_overflow in current_settings.localswitches) and
  2035. (a = 1) and
  2036. UseIncDec then
  2037. begin
  2038. if op = OP_ADD then
  2039. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  2040. else
  2041. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  2042. end
  2043. else
  2044. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2045. OP_AND,OP_OR:
  2046. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2047. OP_XOR:
  2048. if (aword(a)=high(aword)) then
  2049. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  2050. else
  2051. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2052. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  2053. begin
  2054. {$if defined(x86_64)}
  2055. if (a and 63) <> 0 Then
  2056. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  2057. if (a shr 6) <> 0 Then
  2058. internalerror(2013111003);
  2059. {$elseif defined(i386)}
  2060. if (a and 31) <> 0 Then
  2061. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  2062. if (a shr 5) <> 0 Then
  2063. internalerror(2013111002);
  2064. {$elseif defined(i8086)}
  2065. if (a shr 5) <> 0 Then
  2066. internalerror(2013111001);
  2067. a := a and 31;
  2068. if a <> 0 Then
  2069. begin
  2070. if (current_settings.cputype < cpu_186) and (a <> 1) then
  2071. begin
  2072. getcpuregister(list,NR_CL);
  2073. a_load_const_reg(list,OS_8,a,NR_CL);
  2074. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  2075. ungetcpuregister(list,NR_CL);
  2076. end
  2077. else
  2078. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2079. end;
  2080. {$endif}
  2081. end
  2082. else internalerror(68992);
  2083. end;
  2084. end;
  2085. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  2086. const
  2087. {$if defined(cpu64bitalu)}
  2088. REGCX=NR_RCX;
  2089. REGCX_Size = OS_64;
  2090. {$elseif defined(cpu32bitalu)}
  2091. REGCX=NR_ECX;
  2092. REGCX_Size = OS_32;
  2093. {$elseif defined(cpu16bitalu)}
  2094. REGCX=NR_CX;
  2095. REGCX_Size = OS_16;
  2096. {$endif}
  2097. var
  2098. dstsize: topsize;
  2099. instr:Taicpu;
  2100. begin
  2101. if not(Op in [OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2102. check_register_size(size,src);
  2103. check_register_size(size,dst);
  2104. dstsize := tcgsize2opsize[size];
  2105. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2106. op:=OP_IMUL;
  2107. case op of
  2108. OP_NEG,OP_NOT:
  2109. begin
  2110. if src<>dst then
  2111. a_load_reg_reg(list,size,size,src,dst);
  2112. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  2113. end;
  2114. OP_MUL,OP_DIV,OP_IDIV:
  2115. { special stuff, needs separate handling inside code }
  2116. { generator }
  2117. internalerror(200109233);
  2118. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2119. begin
  2120. { Use ecx to load the value, that allows better coalescing }
  2121. getcpuregister(list,REGCX);
  2122. a_load_reg_reg(list,reg_cgsize(src),REGCX_Size,src,REGCX);
  2123. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  2124. ungetcpuregister(list,REGCX);
  2125. end;
  2126. else
  2127. begin
  2128. if reg2opsize(src) <> dstsize then
  2129. internalerror(200109226);
  2130. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  2131. list.concat(instr);
  2132. end;
  2133. end;
  2134. end;
  2135. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  2136. var
  2137. tmpref : treference;
  2138. begin
  2139. tmpref:=ref;
  2140. make_simple_ref(list,tmpref);
  2141. check_register_size(size,reg);
  2142. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2143. op:=OP_IMUL;
  2144. case op of
  2145. OP_NEG,OP_NOT:
  2146. begin
  2147. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2148. end;
  2149. OP_MUL,OP_DIV,OP_IDIV:
  2150. { special stuff, needs separate handling inside code }
  2151. { generator }
  2152. internalerror(200109239);
  2153. else
  2154. begin
  2155. reg := makeregsize(list,reg,size);
  2156. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  2157. end;
  2158. end;
  2159. end;
  2160. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  2161. const
  2162. {$if defined(cpu64bitalu)}
  2163. REGCX=NR_RCX;
  2164. REGCX_Size = OS_64;
  2165. {$elseif defined(cpu32bitalu)}
  2166. REGCX=NR_ECX;
  2167. REGCX_Size = OS_32;
  2168. {$elseif defined(cpu16bitalu)}
  2169. REGCX=NR_CX;
  2170. REGCX_Size = OS_16;
  2171. {$endif}
  2172. var
  2173. tmpref : treference;
  2174. begin
  2175. tmpref:=ref;
  2176. make_simple_ref(list,tmpref);
  2177. { we don't check the register size for some operations, for the following reasons:
  2178. SHR,SHL,SAR,ROL,ROR:
  2179. We allow the register size to differ from the destination size.
  2180. This allows generating better code when performing, for example, a
  2181. shift/rotate in place (x:=x shl y) of a byte variable. In this case,
  2182. we allow the shift count (y) to be located in a 32-bit register,
  2183. even though x is a byte. This:
  2184. - reduces register pressure on i386 (because only EAX,EBX,ECX and
  2185. EDX have 8-bit subregisters)
  2186. - avoids partial register writes, which can cause various
  2187. performance issues on modern out-of-order execution x86 CPUs }
  2188. if not (op in [OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2189. check_register_size(size,reg);
  2190. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2191. op:=OP_IMUL;
  2192. case op of
  2193. OP_NEG,OP_NOT:
  2194. inherited;
  2195. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2196. begin
  2197. { Use ecx to load the value, that allows better coalescing }
  2198. getcpuregister(list,REGCX);
  2199. a_load_reg_reg(list,reg_cgsize(reg),REGCX_Size,reg,REGCX);
  2200. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],NR_CL,tmpref));
  2201. ungetcpuregister(list,REGCX);
  2202. end;
  2203. OP_IMUL:
  2204. begin
  2205. { this one needs a load/imul/store, which is the default }
  2206. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2207. end;
  2208. OP_MUL,OP_DIV,OP_IDIV:
  2209. { special stuff, needs separate handling inside code }
  2210. { generator }
  2211. internalerror(200109238);
  2212. else
  2213. begin
  2214. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  2215. end;
  2216. end;
  2217. end;
  2218. procedure tcgx86.a_op_ref(list: TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference);
  2219. var
  2220. tmpref: treference;
  2221. begin
  2222. if not (Op in [OP_NOT,OP_NEG]) then
  2223. internalerror(2020050705);
  2224. tmpref:=ref;
  2225. make_simple_ref(list,tmpref);
  2226. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  2227. end;
  2228. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  2229. var
  2230. tmpreg: tregister;
  2231. opsize: topsize;
  2232. l : TAsmLabel;
  2233. begin
  2234. { no bsf/bsr for byte }
  2235. if srcsize in [OS_8,OS_S8] then
  2236. begin
  2237. tmpreg:=getintregister(list,OS_INT);
  2238. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  2239. src:=tmpreg;
  2240. srcsize:=OS_INT;
  2241. end;
  2242. { source and destination register must have the same size }
  2243. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  2244. tmpreg:=getintregister(list,srcsize)
  2245. else
  2246. tmpreg:=dst;
  2247. opsize:=tcgsize2opsize[srcsize];
  2248. if not reverse then
  2249. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  2250. else
  2251. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  2252. current_asmdata.getjumplabel(l);
  2253. a_jmp_cond(list,OC_NE,l);
  2254. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  2255. a_label(list,l);
  2256. if tmpreg<>dst then
  2257. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  2258. end;
  2259. {*************** compare instructructions ****************}
  2260. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  2261. l : tasmlabel);
  2262. {$ifdef x86_64}
  2263. var
  2264. tmpreg : tregister;
  2265. {$endif x86_64}
  2266. begin
  2267. {$ifdef x86_64}
  2268. { x86_64 only supports signed 32 bits constants directly }
  2269. if (size in [OS_S64,OS_64]) and
  2270. ((a<low(longint)) or (a>high(longint))) then
  2271. begin
  2272. tmpreg:=getintregister(list,size);
  2273. a_load_const_reg(list,size,a,tmpreg);
  2274. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2275. exit;
  2276. end;
  2277. {$endif x86_64}
  2278. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2279. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  2280. a_jmp_cond(list,cmp_op,l);
  2281. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2282. end;
  2283. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2284. l : tasmlabel);
  2285. var
  2286. {$ifdef x86_64}
  2287. tmpreg : tregister;
  2288. {$endif x86_64}
  2289. tmpref : treference;
  2290. begin
  2291. tmpref:=ref;
  2292. make_simple_ref(list,tmpref);
  2293. {$ifdef x86_64}
  2294. { x86_64 only supports signed 32 bits constants directly }
  2295. if (size in [OS_S64,OS_64]) and
  2296. ((a<low(longint)) or (a>high(longint))) then
  2297. begin
  2298. tmpreg:=getintregister(list,size);
  2299. a_load_const_reg(list,size,a,tmpreg);
  2300. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  2301. exit;
  2302. end;
  2303. {$endif x86_64}
  2304. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  2305. a_jmp_cond(list,cmp_op,l);
  2306. end;
  2307. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  2308. reg1,reg2 : tregister;l : tasmlabel);
  2309. begin
  2310. check_register_size(size,reg1);
  2311. check_register_size(size,reg2);
  2312. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  2313. a_jmp_cond(list,cmp_op,l);
  2314. end;
  2315. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  2316. var
  2317. tmpref : treference;
  2318. begin
  2319. tmpref:=ref;
  2320. make_simple_ref(list,tmpref);
  2321. check_register_size(size,reg);
  2322. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  2323. a_jmp_cond(list,cmp_op,l);
  2324. end;
  2325. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  2326. var
  2327. tmpref : treference;
  2328. begin
  2329. tmpref:=ref;
  2330. make_simple_ref(list,tmpref);
  2331. check_register_size(size,reg);
  2332. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  2333. a_jmp_cond(list,cmp_op,l);
  2334. end;
  2335. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2336. var
  2337. ai : taicpu;
  2338. begin
  2339. if cond=OC_None then
  2340. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2341. else
  2342. begin
  2343. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  2344. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2345. end;
  2346. ai.is_jmp:=true;
  2347. list.concat(ai);
  2348. end;
  2349. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2350. var
  2351. ai : taicpu;
  2352. hl : tasmlabel;
  2353. f2 : tresflags;
  2354. begin
  2355. hl:=nil;
  2356. f2:=f;
  2357. case f of
  2358. F_FNE:
  2359. begin
  2360. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2361. ai.SetCondition(C_P);
  2362. ai.is_jmp:=true;
  2363. list.concat(ai);
  2364. f2:=F_NE;
  2365. end;
  2366. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2367. begin
  2368. { JP before JA/JAE is redundant, but it must be generated here
  2369. and left for peephole optimizer to remove. }
  2370. current_asmdata.getjumplabel(hl);
  2371. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2372. ai.SetCondition(C_P);
  2373. ai.is_jmp:=true;
  2374. list.concat(ai);
  2375. f2:=FPUFlags2Flags[f];
  2376. end;
  2377. else
  2378. ;
  2379. end;
  2380. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2381. ai.SetCondition(flags_to_cond(f2));
  2382. ai.is_jmp := true;
  2383. list.concat(ai);
  2384. if assigned(hl) then
  2385. a_label(list,hl);
  2386. end;
  2387. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2388. var
  2389. ai : taicpu;
  2390. f2 : tresflags;
  2391. hreg,hreg2 : tregister;
  2392. op: tasmop;
  2393. begin
  2394. hreg2:=NR_NO;
  2395. op:=A_AND;
  2396. f2:=f;
  2397. case f of
  2398. F_FE,F_FNE,F_FB,F_FBE:
  2399. begin
  2400. hreg2:=getintregister(list,OS_8);
  2401. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2402. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2403. begin
  2404. ai.setcondition(C_P);
  2405. op:=A_OR;
  2406. end
  2407. else
  2408. ai.setcondition(C_NP);
  2409. list.concat(ai);
  2410. f2:=FPUFlags2Flags[f];
  2411. end;
  2412. F_FA,F_FAE: { These do not need PF check }
  2413. f2:=FPUFlags2Flags[f];
  2414. else
  2415. ;
  2416. end;
  2417. hreg:=makeregsize(list,reg,OS_8);
  2418. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2419. ai.setcondition(flags_to_cond(f2));
  2420. list.concat(ai);
  2421. if (hreg2<>NR_NO) then
  2422. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2423. if reg<>hreg then
  2424. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2425. end;
  2426. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2427. var
  2428. ai : taicpu;
  2429. tmpref : treference;
  2430. f2 : tresflags;
  2431. begin
  2432. f2:=f;
  2433. case f of
  2434. F_FE,F_FNE,F_FB,F_FBE:
  2435. begin
  2436. inherited g_flags2ref(list,size,f,ref);
  2437. exit;
  2438. end;
  2439. F_FA,F_FAE:
  2440. f2:=FPUFlags2Flags[f];
  2441. else
  2442. ;
  2443. end;
  2444. tmpref:=ref;
  2445. make_simple_ref(list,tmpref);
  2446. if not(size in [OS_8,OS_S8]) then
  2447. a_load_const_ref(list,size,0,tmpref);
  2448. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2449. ai.setcondition(flags_to_cond(f2));
  2450. list.concat(ai);
  2451. {$ifndef cpu64bitalu}
  2452. if size in [OS_S64,OS_64] then
  2453. begin
  2454. inc(tmpref.offset,4);
  2455. a_load_const_ref(list,OS_32,0,tmpref);
  2456. end;
  2457. {$endif cpu64bitalu}
  2458. end;
  2459. { ************* concatcopy ************ }
  2460. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2461. const
  2462. {$if defined(cpu64bitalu)}
  2463. REGCX=NR_RCX;
  2464. REGSI=NR_RSI;
  2465. REGDI=NR_RDI;
  2466. copy_len_sizes = [1, 2, 4, 8];
  2467. push_segment_size = S_L;
  2468. {$elseif defined(cpu32bitalu)}
  2469. REGCX=NR_ECX;
  2470. REGSI=NR_ESI;
  2471. REGDI=NR_EDI;
  2472. copy_len_sizes = [1, 2, 4];
  2473. push_segment_size = S_L;
  2474. {$elseif defined(cpu16bitalu)}
  2475. REGCX=NR_CX;
  2476. REGSI=NR_SI;
  2477. REGDI=NR_DI;
  2478. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2479. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2480. push_segment_size = S_W;
  2481. {$endif}
  2482. type
  2483. copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx,copy_avx512);
  2484. var srcref,dstref,tmpref:Treference;
  2485. r,r0,r1,r2,r3:Tregister;
  2486. helpsize:tcgint;
  2487. copysize:byte;
  2488. cgsize:Tcgsize;
  2489. cm:copymode;
  2490. saved_ds,saved_es: Boolean;
  2491. hlist: TAsmList;
  2492. begin
  2493. srcref:=source;
  2494. dstref:=dest;
  2495. {$ifndef i8086}
  2496. make_simple_ref(list,srcref);
  2497. make_simple_ref(list,dstref);
  2498. {$endif not i8086}
  2499. {$ifdef i386}
  2500. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2501. than just resolving the tls segment }
  2502. if (srcref.refaddr=addr_ntpoff) and (srcref.segment=NR_GS) then
  2503. begin
  2504. r:=getaddressregister(list);
  2505. a_loadaddr_ref_reg(list,srcref,r);
  2506. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2507. srcref.base:=r;
  2508. end;
  2509. if (dstref.refaddr=addr_ntpoff) and (dstref.segment=NR_GS) then
  2510. begin
  2511. r:=getaddressregister(list);
  2512. a_loadaddr_ref_reg(list,dstref,r);
  2513. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2514. dstref.base:=r;
  2515. end;
  2516. {$endif i386}
  2517. {$ifdef x86_64}
  2518. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2519. than just resolving the tls segment }
  2520. if (srcref.refaddr=addr_tpoff) and (srcref.segment=NR_FS) then
  2521. begin
  2522. r:=getaddressregister(list);
  2523. a_loadaddr_ref_reg(list,srcref,r);
  2524. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2525. srcref.base:=r;
  2526. end;
  2527. if (dstref.refaddr=addr_tpoff) and (dstref.segment=NR_FS) then
  2528. begin
  2529. r:=getaddressregister(list);
  2530. a_loadaddr_ref_reg(list,dstref,r);
  2531. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2532. dstref.base:=r;
  2533. end;
  2534. {$endif x86_64}
  2535. cm:=copy_move;
  2536. helpsize:=3*sizeof(aword);
  2537. if cs_opt_size in current_settings.optimizerswitches then
  2538. helpsize:=2*sizeof(aword);
  2539. {$ifndef i8086}
  2540. { avx helps only to reduce size, using it in general does at least not help on
  2541. an i7-4770
  2542. but using the xmm registers reduces register pressure (FK) }
  2543. if (FPUX86_HAS_AVXUNIT in fpu_capabilities[current_settings.fputype]) and
  2544. ((len mod 4)=0) and (len<=48) {$ifndef i386}and (len>=16){$endif i386} then
  2545. cm:=copy_avx
  2546. else if (FPUX86_HAS_AVX512F in fpu_capabilities[current_settings.fputype]) and
  2547. ((len mod 4)=0) and (len<=128) {$ifndef i386}and (len>=16){$endif i386} then
  2548. cm:=copy_avx512
  2549. else
  2550. { I'am not sure what CPUs would benefit from using sse instructions for moves
  2551. but using the xmm registers reduces register pressure (FK) }
  2552. if
  2553. {$ifdef x86_64}
  2554. ((current_settings.fputype>=fpu_sse64)
  2555. {$else x86_64}
  2556. ((current_settings.fputype>=fpu_sse)
  2557. {$endif x86_64}
  2558. or (CPUX86_HAS_SSE2 in cpu_capabilities[current_settings.cputype])) and
  2559. ({$ifdef i386}(len=8) or {$endif i386}(len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2560. cm:=copy_mm
  2561. else
  2562. {$endif i8086}
  2563. if (cs_mmx in current_settings.localswitches) and
  2564. not(pi_uses_fpu in current_procinfo.flags) and
  2565. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2566. cm:=copy_mmx
  2567. else
  2568. if len>helpsize then
  2569. cm:=copy_string;
  2570. if (cs_opt_size in current_settings.optimizerswitches) and
  2571. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2572. not(len in copy_len_sizes) then
  2573. cm:=copy_string;
  2574. {$ifndef i8086}
  2575. { using %fs and %gs as segment prefixes is perfectly valid }
  2576. if ((srcref.segment<>NR_NO) and (srcref.segment<>NR_FS) and (srcref.segment<>NR_GS)) or
  2577. ((dstref.segment<>NR_NO) and (dstref.segment<>NR_FS) and (dstref.segment<>NR_GS)) then
  2578. cm:=copy_string;
  2579. {$endif not i8086}
  2580. case cm of
  2581. copy_move:
  2582. begin
  2583. copysize:=sizeof(aint);
  2584. cgsize:=int_cgsize(copysize);
  2585. while len<>0 do
  2586. begin
  2587. if len<2 then
  2588. begin
  2589. copysize:=1;
  2590. cgsize:=OS_8;
  2591. end
  2592. else if len<4 then
  2593. begin
  2594. copysize:=2;
  2595. cgsize:=OS_16;
  2596. end
  2597. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2598. else if len<8 then
  2599. begin
  2600. copysize:=4;
  2601. cgsize:=OS_32;
  2602. end
  2603. {$endif cpu32bitalu or cpu64bitalu}
  2604. {$ifdef cpu64bitalu}
  2605. else if len<16 then
  2606. begin
  2607. copysize:=8;
  2608. cgsize:=OS_64;
  2609. end
  2610. {$endif}
  2611. ;
  2612. dec(len,copysize);
  2613. r:=getintregister(list,cgsize);
  2614. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2615. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2616. inc(srcref.offset,copysize);
  2617. inc(dstref.offset,copysize);
  2618. end;
  2619. end;
  2620. copy_mmx:
  2621. begin
  2622. r0:=getmmxregister(list);
  2623. r1:=NR_NO;
  2624. r2:=NR_NO;
  2625. r3:=NR_NO;
  2626. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2627. if len>=16 then
  2628. begin
  2629. inc(srcref.offset,8);
  2630. r1:=getmmxregister(list);
  2631. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2632. end;
  2633. if len>=24 then
  2634. begin
  2635. inc(srcref.offset,8);
  2636. r2:=getmmxregister(list);
  2637. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2638. end;
  2639. if len>=32 then
  2640. begin
  2641. inc(srcref.offset,8);
  2642. r3:=getmmxregister(list);
  2643. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2644. end;
  2645. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2646. if len>=16 then
  2647. begin
  2648. inc(dstref.offset,8);
  2649. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2650. end;
  2651. if len>=24 then
  2652. begin
  2653. inc(dstref.offset,8);
  2654. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2655. end;
  2656. if len>=32 then
  2657. begin
  2658. inc(dstref.offset,8);
  2659. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2660. end;
  2661. end;
  2662. copy_mm:
  2663. begin
  2664. r0:=NR_NO;
  2665. r1:=NR_NO;
  2666. r2:=NR_NO;
  2667. r3:=NR_NO;
  2668. if len>=16 then
  2669. begin
  2670. r0:=getmmregister(list,OS_M128);
  2671. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2672. inc(srcref.offset,16);
  2673. end;
  2674. if len>=32 then
  2675. begin
  2676. r1:=getmmregister(list,OS_M128);
  2677. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2678. inc(srcref.offset,16);
  2679. end;
  2680. if len>=48 then
  2681. begin
  2682. r2:=getmmregister(list,OS_M128);
  2683. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2684. inc(srcref.offset,16);
  2685. end;
  2686. if (len=8) or (len=24) or (len=40) then
  2687. begin
  2688. r3:=getmmregister(list,OS_M64);
  2689. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2690. end;
  2691. if len>=16 then
  2692. begin
  2693. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2694. inc(dstref.offset,16);
  2695. end;
  2696. if len>=32 then
  2697. begin
  2698. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2699. inc(dstref.offset,16);
  2700. end;
  2701. if len>=48 then
  2702. begin
  2703. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2704. inc(dstref.offset,16);
  2705. end;
  2706. if (len=8) or (len=24) or (len=40) then
  2707. begin
  2708. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2709. end;
  2710. end;
  2711. copy_avx512,
  2712. copy_avx:
  2713. begin
  2714. hlist:=TAsmList.create;
  2715. if cm=copy_avx512 then
  2716. while len>=64 do
  2717. begin
  2718. r0:=getmmregister(list,OS_M512);
  2719. a_loadmm_ref_reg(list,OS_M512,OS_M512,srcref,r0,nil);
  2720. a_loadmm_reg_ref(hlist,OS_M512,OS_M512,r0,dstref,nil);
  2721. inc(srcref.offset,64);
  2722. inc(dstref.offset,64);
  2723. dec(len,64);
  2724. Include(current_procinfo.flags,pi_uses_ymm);
  2725. end;
  2726. while len>=32 do
  2727. begin
  2728. r0:=getmmregister(list,OS_M256);
  2729. a_loadmm_ref_reg(list,OS_M256,OS_M256,srcref,r0,nil);
  2730. a_loadmm_reg_ref(hlist,OS_M256,OS_M256,r0,dstref,nil);
  2731. inc(srcref.offset,32);
  2732. inc(dstref.offset,32);
  2733. dec(len,32);
  2734. Include(current_procinfo.flags,pi_uses_ymm);
  2735. end;
  2736. while len>=16 do
  2737. begin
  2738. r0:=getmmregister(list,OS_M128);
  2739. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2740. a_loadmm_reg_ref(hlist,OS_M128,OS_M128,r0,dstref,nil);
  2741. inc(srcref.offset,16);
  2742. inc(dstref.offset,16);
  2743. dec(len,16);
  2744. end;
  2745. if len>=8 then
  2746. begin
  2747. r0:=getmmregister(list,OS_M64);
  2748. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2749. a_loadmm_reg_ref(hlist,OS_M64,OS_M64,r0,dstref,nil);
  2750. inc(srcref.offset,8);
  2751. inc(dstref.offset,8);
  2752. dec(len,8);
  2753. end;
  2754. if len>=4 then
  2755. begin
  2756. r0:=getintregister(list,OS_32);
  2757. a_load_ref_reg(list,OS_32,OS_32,srcref,r0);
  2758. a_load_reg_ref(hlist,OS_32,OS_32,r0,dstref);
  2759. inc(srcref.offset,4);
  2760. inc(dstref.offset,4);
  2761. dec(len,4);
  2762. end;
  2763. list.concatList(hlist);
  2764. hlist.free;
  2765. end
  2766. else {copy_string, should be a good fallback in case of unhandled}
  2767. begin
  2768. getcpuregister(list,REGDI);
  2769. if (dstref.segment=NR_NO) and
  2770. (segment_regs_equal(NR_SS,NR_DS) or ((dstref.base<>NR_BP) and (dstref.base<>NR_SP))) then
  2771. begin
  2772. a_loadaddr_ref_reg(list,dstref,REGDI);
  2773. saved_es:=false;
  2774. {$ifdef volatile_es}
  2775. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2776. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2777. {$endif volatile_es}
  2778. end
  2779. else
  2780. begin
  2781. { load offset of dest. reference }
  2782. tmpref:=dstref;
  2783. tmpref.segment:=NR_NO;
  2784. a_loadaddr_ref_reg(list,tmpref,REGDI);
  2785. {$ifdef volatile_es}
  2786. saved_es:=false;
  2787. {$else volatile_es}
  2788. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2789. saved_es:=true;
  2790. {$endif volatile_es}
  2791. if dstref.segment<>NR_NO then
  2792. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dstref.segment))
  2793. else if (dstref.base=NR_BP) or (dstref.base=NR_SP) then
  2794. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2795. else
  2796. internalerror(2014040401);
  2797. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2798. end;
  2799. getcpuregister(list,REGSI);
  2800. {$ifdef i8086}
  2801. { at this point, si and di are allocated, so no register is available as index =>
  2802. compiler will hang/ie during spilling, so avoid that srcref has base and index, see also tests/tbs/tb0637.pp }
  2803. if (srcref.base<>NR_NO) and (srcref.index<>NR_NO) then
  2804. begin
  2805. r:=getaddressregister(list);
  2806. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,srcref.base,srcref.index,r);
  2807. srcref.base:=r;
  2808. srcref.index:=NR_NO;
  2809. end;
  2810. {$endif i8086}
  2811. if ((srcref.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((srcref.base<>NR_BP) and (srcref.base<>NR_SP)))) or
  2812. (is_segment_reg(srcref.segment) and segment_regs_equal(srcref.segment,NR_DS)) then
  2813. begin
  2814. srcref.segment:=NR_NO;
  2815. a_loadaddr_ref_reg(list,srcref,REGSI);
  2816. saved_ds:=false;
  2817. end
  2818. else
  2819. begin
  2820. { load offset of source reference }
  2821. tmpref:=srcref;
  2822. tmpref.segment:=NR_NO;
  2823. a_loadaddr_ref_reg(list,tmpref,REGSI);
  2824. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2825. saved_ds:=true;
  2826. if srcref.segment<>NR_NO then
  2827. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,srcref.segment))
  2828. else if (srcref.base=NR_BP) or (srcref.base=NR_SP) then
  2829. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2830. else
  2831. internalerror(2014040402);
  2832. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2833. end;
  2834. getcpuregister(list,REGCX);
  2835. if ts_cld in current_settings.targetswitches then
  2836. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2837. if (cs_opt_size in current_settings.optimizerswitches) and
  2838. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2839. begin
  2840. a_load_const_reg(list,OS_INT,len,REGCX);
  2841. list.concat(Taicpu.op_none(A_REP,S_NO));
  2842. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2843. end
  2844. else
  2845. begin
  2846. helpsize:=len div sizeof(aint);
  2847. len:=len mod sizeof(aint);
  2848. if helpsize>1 then
  2849. begin
  2850. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2851. list.concat(Taicpu.op_none(A_REP,S_NO));
  2852. end;
  2853. if helpsize>0 then
  2854. begin
  2855. {$if defined(cpu64bitalu)}
  2856. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2857. {$elseif defined(cpu32bitalu)}
  2858. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2859. {$elseif defined(cpu16bitalu)}
  2860. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2861. {$endif}
  2862. end;
  2863. if len>=4 then
  2864. begin
  2865. dec(len,4);
  2866. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2867. end;
  2868. if len>=2 then
  2869. begin
  2870. dec(len,2);
  2871. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2872. end;
  2873. if len=1 then
  2874. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2875. end;
  2876. ungetcpuregister(list,REGCX);
  2877. ungetcpuregister(list,REGSI);
  2878. ungetcpuregister(list,REGDI);
  2879. if saved_ds then
  2880. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2881. if saved_es then
  2882. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2883. end;
  2884. end;
  2885. end;
  2886. {****************************************************************************
  2887. Entry/Exit Code Helpers
  2888. ****************************************************************************}
  2889. procedure tcgx86.g_profilecode(list : TAsmList);
  2890. var
  2891. pl : tasmlabel;
  2892. mcountprefix : String[4];
  2893. begin
  2894. case target_info.system of
  2895. {$ifndef NOTARGETWIN}
  2896. system_i386_win32,
  2897. {$endif}
  2898. system_i386_freebsd,
  2899. system_i386_netbsd,
  2900. system_i386_wdosx :
  2901. begin
  2902. Case target_info.system Of
  2903. system_i386_freebsd : mcountprefix:='.';
  2904. system_i386_netbsd : mcountprefix:='__';
  2905. else
  2906. mcountPrefix:='';
  2907. end;
  2908. current_asmdata.getaddrlabel(pl);
  2909. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2910. list.concat(Tai_label.Create(pl));
  2911. list.concat(Tai_const.Create_32bit(0));
  2912. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2913. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2914. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2915. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2916. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2917. end;
  2918. system_i386_linux:
  2919. a_call_name(list,target_info.Cprefix+'mcount',false);
  2920. system_i386_go32v2,system_i386_watcom:
  2921. begin
  2922. a_call_name(list,'MCOUNT',false);
  2923. end;
  2924. system_x86_64_linux,
  2925. system_x86_64_darwin,
  2926. system_x86_64_iphonesim:
  2927. begin
  2928. a_call_name(list,'mcount',false);
  2929. end;
  2930. system_i386_openbsd,
  2931. system_x86_64_openbsd:
  2932. begin
  2933. a_call_name(list,'__mcount',false);
  2934. end;
  2935. else
  2936. internalerror(2019050701);
  2937. end;
  2938. end;
  2939. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2940. procedure decrease_sp(a : tcgint);
  2941. var
  2942. href : treference;
  2943. begin
  2944. {$ifdef x86_64}
  2945. if localsize=8 then
  2946. list.concat(Taicpu.op_reg(A_PUSH,TCGSize2OpSize[OS_ADDR],NR_RAX))
  2947. else
  2948. {$endif x86_64}
  2949. begin
  2950. reference_reset_base(href,NR_STACK_POINTER_REG,-a,ctempposinvalid,0,[]);
  2951. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2952. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2953. end;
  2954. end;
  2955. {$ifdef x86}
  2956. {$ifndef NOTARGETWIN}
  2957. var
  2958. href : treference;
  2959. i : integer;
  2960. again : tasmlabel;
  2961. {$endif NOTARGETWIN}
  2962. {$endif x86}
  2963. begin
  2964. if localsize>0 then
  2965. begin
  2966. {$ifdef i386}
  2967. {$ifndef NOTARGETWIN}
  2968. { windows guards only a few pages for stack growing,
  2969. so we have to access every page first }
  2970. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2971. (localsize>=winstackpagesize) then
  2972. begin
  2973. if localsize div winstackpagesize<=5 then
  2974. begin
  2975. decrease_sp(localsize-4);
  2976. for i:=1 to localsize div winstackpagesize do
  2977. begin
  2978. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,ctempposinvalid,4,[]);
  2979. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2980. end;
  2981. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2982. end
  2983. else
  2984. begin
  2985. current_asmdata.getjumplabel(again);
  2986. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2987. does not change "used_in_proc" state of EDI and therefore can be
  2988. called after saving registers with "push" instruction
  2989. without creating an unbalanced "pop edi" in epilogue }
  2990. a_reg_alloc(list,NR_EDI);
  2991. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2992. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2993. a_label(list,again);
  2994. decrease_sp(winstackpagesize-4);
  2995. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2996. if UseIncDec then
  2997. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2998. else
  2999. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  3000. a_jmp_cond(list,OC_NE,again);
  3001. decrease_sp(localsize mod winstackpagesize-4);
  3002. reference_reset_base(href,NR_ESP,localsize-4,ctempposinvalid,4,[]);
  3003. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  3004. a_reg_dealloc(list,NR_EDI);
  3005. end
  3006. end
  3007. else
  3008. {$endif NOTARGETWIN}
  3009. {$endif i386}
  3010. {$ifdef x86_64}
  3011. {$ifndef NOTARGETWIN}
  3012. { windows guards only a few pages for stack growing,
  3013. so we have to access every page first }
  3014. if (target_info.system=system_x86_64_win64) and
  3015. (localsize>=winstackpagesize) then
  3016. begin
  3017. if localsize div winstackpagesize<=5 then
  3018. begin
  3019. decrease_sp(localsize);
  3020. for i:=1 to localsize div winstackpagesize do
  3021. begin
  3022. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,ctempposinvalid,4,[]);
  3023. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3024. end;
  3025. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  3026. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3027. end
  3028. else
  3029. begin
  3030. current_asmdata.getjumplabel(again);
  3031. getcpuregister(list,NR_R10);
  3032. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  3033. a_label(list,again);
  3034. decrease_sp(winstackpagesize);
  3035. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  3036. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3037. if UseIncDec then
  3038. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  3039. else
  3040. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  3041. a_jmp_cond(list,OC_NE,again);
  3042. decrease_sp(localsize mod winstackpagesize);
  3043. ungetcpuregister(list,NR_R10);
  3044. end
  3045. end
  3046. else
  3047. {$endif NOTARGETWIN}
  3048. {$endif x86_64}
  3049. decrease_sp(localsize);
  3050. end;
  3051. end;
  3052. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3053. var
  3054. stackmisalignment: longint;
  3055. regsize: longint;
  3056. {$ifdef i8086}
  3057. dgroup: treference;
  3058. fardataseg: treference;
  3059. {$endif i8086}
  3060. procedure push_regs;
  3061. var
  3062. r: longint;
  3063. usedregs: tcpuregisterset;
  3064. regs_to_save_int: tcpuregisterarray;
  3065. hreg: TRegister;
  3066. begin
  3067. regsize:=0;
  3068. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3069. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3070. for r := low(regs_to_save_int) to high(regs_to_save_int) do
  3071. if regs_to_save_int[r] in usedregs then
  3072. begin
  3073. inc(regsize,sizeof(aint));
  3074. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3075. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],hreg));
  3076. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3077. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)*2+localsize))
  3078. else
  3079. begin
  3080. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)+localsize));
  3081. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3082. end;
  3083. end;
  3084. end;
  3085. begin
  3086. regsize:=0;
  3087. stackmisalignment:=0;
  3088. {$ifdef i8086}
  3089. { Win16 callback/exported proc prologue support.
  3090. Since callbacks can be called from different modules, DS on entry may be
  3091. initialized with the data segment of a different module, so we need to
  3092. get ours. But we can't do
  3093. push ds
  3094. mov ax, dgroup
  3095. mov ds, ax
  3096. because code segments are shared between different instances of the same
  3097. module (which have different instances of the current program's data segment),
  3098. so the same 'mov ax, dgroup' instruction will be used for all instances
  3099. of the program and it will load the same segment into ax.
  3100. So, the standard win16 prologue looks like this:
  3101. mov ax, ds
  3102. nop
  3103. inc bp
  3104. push bp
  3105. mov bp, sp
  3106. push ds
  3107. mov ds, ax
  3108. By default, this does nothing, except wasting a few extra machine cycles and
  3109. destroying ax in the process. However, Windows checks the first three bytes
  3110. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  3111. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  3112. a thunk that loads ds for the current program instance in ax before calling
  3113. the routine.
  3114. And now the fun part comes: somebody (Michael Geary) figured out that all this
  3115. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  3116. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  3117. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  3118. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  3119. another solution for dlls - since win16 dlls only have a single instance of their
  3120. data segment, we can initialize ds from dgroup. However, there's not a single
  3121. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  3122. that's why there's still an option to turn smart callbacks off and go the
  3123. MakeProcInstance way.
  3124. Additional details here: http://www.geary.com/fixds.html }
  3125. if (current_settings.x86memorymodel<>mm_huge) and
  3126. (po_exports in current_procinfo.procdef.procoptions) and
  3127. (target_info.system=system_i8086_win16) then
  3128. begin
  3129. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  3130. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  3131. else
  3132. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  3133. list.concat(Taicpu.op_none(A_NOP));
  3134. end
  3135. { interrupt support for i8086 }
  3136. else if po_interrupt in current_procinfo.procdef.procoptions then
  3137. begin
  3138. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  3139. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  3140. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  3141. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  3142. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3143. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3144. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3145. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3146. if current_settings.x86memorymodel=mm_tiny then
  3147. begin
  3148. { in the tiny memory model, we can't use dgroup, because that
  3149. adds a relocation entry to the .exe and we can't produce a
  3150. .com file (because they don't support relactions), so instead
  3151. we initialize DS from CS. }
  3152. if cs_opt_size in current_settings.optimizerswitches then
  3153. begin
  3154. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  3155. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  3156. end
  3157. else
  3158. begin
  3159. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  3160. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3161. end;
  3162. end
  3163. else if current_settings.x86memorymodel=mm_huge then
  3164. begin
  3165. reference_reset(fardataseg,0,[]);
  3166. fardataseg.refaddr:=addr_fardataseg;
  3167. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3168. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3169. end
  3170. else
  3171. begin
  3172. reference_reset(dgroup,0,[]);
  3173. dgroup.refaddr:=addr_dgroup;
  3174. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  3175. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3176. end;
  3177. end;
  3178. {$endif i8086}
  3179. {$ifdef i386}
  3180. { interrupt support for i386 }
  3181. if (po_interrupt in current_procinfo.procdef.procoptions) then
  3182. begin
  3183. { .... also the segment registers }
  3184. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  3185. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  3186. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3187. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3188. { save the registers of an interrupt procedure }
  3189. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  3190. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  3191. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  3192. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  3193. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  3194. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  3195. { pushf, push %cs, 4*selector registers, 6*general purpose registers }
  3196. inc(stackmisalignment,4+4+4*2+6*4);
  3197. end;
  3198. {$endif i386}
  3199. { save old framepointer }
  3200. if not nostackframe then
  3201. begin
  3202. { return address }
  3203. inc(stackmisalignment,sizeof(pint));
  3204. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  3205. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3206. begin
  3207. {$ifdef i386}
  3208. if (not paramanager.use_fixed_stack) then
  3209. push_regs;
  3210. {$endif i386}
  3211. CGmessage(cg_d_stackframe_omited);
  3212. end
  3213. else
  3214. begin
  3215. {$ifdef i8086}
  3216. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  3217. ((po_exports in current_procinfo.procdef.procoptions) and
  3218. (target_info.system=system_i8086_win16))) and
  3219. is_proc_far(current_procinfo.procdef) then
  3220. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  3221. {$endif i8086}
  3222. { push <frame_pointer> }
  3223. inc(stackmisalignment,sizeof(pint));
  3224. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  3225. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  3226. { Return address and FP are both on stack }
  3227. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  3228. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  3229. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  3230. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  3231. else
  3232. begin
  3233. push_regs;
  3234. gen_load_frame_for_exceptfilter(list);
  3235. { Need only as much stack space as necessary to do the calls.
  3236. Exception filters don't have own local vars, and temps are 'mapped'
  3237. to the parent procedure.
  3238. maxpushedparasize is already aligned at least on x86_64. }
  3239. localsize:=current_procinfo.maxpushedparasize;
  3240. end;
  3241. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  3242. end;
  3243. { allocate stackframe space }
  3244. if (localsize<>0) or
  3245. ((target_info.stackalign>sizeof(pint)) and
  3246. (stackmisalignment <> 0) and
  3247. ((pi_do_call in current_procinfo.flags) or
  3248. (po_assembler in current_procinfo.procdef.procoptions))) then
  3249. begin
  3250. if target_info.stackalign>sizeof(pint) then
  3251. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  3252. g_stackpointer_alloc(list,localsize);
  3253. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3254. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3255. current_procinfo.final_localsize:=localsize;
  3256. end
  3257. {$ifdef i8086}
  3258. else
  3259. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  3260. because it will generate code for stack checking, if stack checking is on }
  3261. g_stackpointer_alloc(list,0)
  3262. {$endif i8086}
  3263. ;
  3264. {$ifdef i8086}
  3265. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  3266. if (current_settings.x86memorymodel<>mm_huge) and
  3267. (po_exports in current_procinfo.procdef.procoptions) and
  3268. (target_info.system=system_i8086_win16) then
  3269. begin
  3270. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3271. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3272. end
  3273. else if (current_settings.x86memorymodel=mm_huge) and
  3274. not (po_interrupt in current_procinfo.procdef.procoptions) then
  3275. begin
  3276. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3277. reference_reset(fardataseg,0,[]);
  3278. fardataseg.refaddr:=addr_fardataseg;
  3279. if current_procinfo.procdef.proccalloption=pocall_register then
  3280. begin
  3281. { Use CX register if using register convention
  3282. as it is not a register used to store parameters }
  3283. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_CX));
  3284. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CX,NR_DS));
  3285. end
  3286. else
  3287. begin
  3288. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3289. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3290. end;
  3291. end;
  3292. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  3293. but must be preserved in Microsoft C's pascal calling convention, and
  3294. since Windows is compiled with Microsoft compilers, these registers
  3295. must be saved for exported procedures (BP7 for Win16 also does this). }
  3296. if (po_exports in current_procinfo.procdef.procoptions) and
  3297. (target_info.system=system_i8086_win16) then
  3298. begin
  3299. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3300. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3301. end;
  3302. {$endif i8086}
  3303. {$ifdef i386}
  3304. if (not paramanager.use_fixed_stack) and
  3305. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  3306. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3307. begin
  3308. regsize:=0;
  3309. push_regs;
  3310. reference_reset_base(current_procinfo.save_regs_ref,
  3311. current_procinfo.framepointer,
  3312. -(localsize+regsize),ctempposinvalid,sizeof(aint),[]);
  3313. end;
  3314. {$endif i386}
  3315. end;
  3316. end;
  3317. procedure tcgx86.g_save_registers(list: TAsmList);
  3318. begin
  3319. {$ifdef i386}
  3320. if paramanager.use_fixed_stack then
  3321. {$endif i386}
  3322. inherited g_save_registers(list);
  3323. end;
  3324. procedure tcgx86.g_restore_registers(list: TAsmList);
  3325. begin
  3326. {$ifdef i386}
  3327. if paramanager.use_fixed_stack then
  3328. {$endif i386}
  3329. inherited g_restore_registers(list);
  3330. end;
  3331. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  3332. var
  3333. r: longint;
  3334. hreg: tregister;
  3335. href: treference;
  3336. usedregs: tcpuregisterset;
  3337. regs_to_save_int: tcpuregisterarray;
  3338. begin
  3339. href:=current_procinfo.save_regs_ref;
  3340. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3341. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3342. for r:=high(regs_to_save_int) downto low(regs_to_save_int) do
  3343. if regs_to_save_int[r] in usedregs then
  3344. begin
  3345. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3346. { Allocate register so the optimizer does not remove the load }
  3347. a_reg_alloc(list,hreg);
  3348. if use_pop then
  3349. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  3350. else
  3351. begin
  3352. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3353. inc(href.offset,sizeof(aint));
  3354. end;
  3355. current_asmdata.asmcfi.cfa_restore(list,hreg);
  3356. end;
  3357. end;
  3358. procedure tcgx86.generate_leave(list: TAsmList);
  3359. begin
  3360. if UseLeave then
  3361. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  3362. else
  3363. begin
  3364. {$if defined(x86_64)}
  3365. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_RSP);
  3366. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  3367. current_asmdata.asmcfi.cfa_restore(list,NR_RBP);
  3368. current_asmdata.asmcfi.cfa_def_cfa_offset(list,8);
  3369. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  3370. {$elseif defined(i386)}
  3371. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_ESP);
  3372. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  3373. current_asmdata.asmcfi.cfa_restore(list,NR_EBP);
  3374. current_asmdata.asmcfi.cfa_def_cfa_offset(list,4);
  3375. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  3376. {$elseif defined(i8086)}
  3377. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  3378. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  3379. {$endif}
  3380. end;
  3381. end;
  3382. { produces if necessary overflowcode }
  3383. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  3384. var
  3385. hl : tasmlabel;
  3386. ai : taicpu;
  3387. cond : TAsmCond;
  3388. begin
  3389. if not(cs_check_overflow in current_settings.localswitches) then
  3390. exit;
  3391. current_asmdata.getjumplabel(hl);
  3392. if not ((def.typ=pointerdef) or
  3393. ((def.typ=orddef) and
  3394. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  3395. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  3396. cond:=C_NO
  3397. else
  3398. cond:=C_NB;
  3399. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  3400. ai.SetCondition(cond);
  3401. ai.is_jmp:=true;
  3402. list.concat(ai);
  3403. a_call_name(list,'FPC_OVERFLOW',false);
  3404. a_label(list,hl);
  3405. end;
  3406. end.