cgx86.pas 142 KB

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