cgx86.pas 118 KB

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