cgx86.pas 113 KB

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