cgx86.pas 106 KB

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