cgx86.pas 96 KB

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