cgx86.pas 140 KB

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