cgx86.pas 100 KB

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