cgx86.pas 106 KB

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