cgx86.pas 118 KB

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