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_reg(A_MOV,S_W,ref.segment,NR_ES));
  535. ref.segment:=NR_ES;
  536. end;
  537. {$endif}
  538. end;
  539. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  540. begin
  541. case t of
  542. OS_F32 :
  543. begin
  544. op:=A_FLD;
  545. s:=S_FS;
  546. end;
  547. OS_F64 :
  548. begin
  549. op:=A_FLD;
  550. s:=S_FL;
  551. end;
  552. OS_F80 :
  553. begin
  554. op:=A_FLD;
  555. s:=S_FX;
  556. end;
  557. OS_C64 :
  558. begin
  559. op:=A_FILD;
  560. s:=S_IQ;
  561. end;
  562. else
  563. internalerror(200204043);
  564. end;
  565. end;
  566. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  567. var
  568. op : tasmop;
  569. s : topsize;
  570. tmpref : treference;
  571. begin
  572. tmpref:=ref;
  573. make_simple_ref(list,tmpref);
  574. floatloadops(t,op,s);
  575. list.concat(Taicpu.Op_ref(op,s,tmpref));
  576. inc_fpu_stack;
  577. end;
  578. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  579. begin
  580. case t of
  581. OS_F32 :
  582. begin
  583. op:=A_FSTP;
  584. s:=S_FS;
  585. end;
  586. OS_F64 :
  587. begin
  588. op:=A_FSTP;
  589. s:=S_FL;
  590. end;
  591. OS_F80 :
  592. begin
  593. op:=A_FSTP;
  594. s:=S_FX;
  595. end;
  596. OS_C64 :
  597. begin
  598. op:=A_FISTP;
  599. s:=S_IQ;
  600. end;
  601. else
  602. internalerror(200204042);
  603. end;
  604. end;
  605. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  606. var
  607. op : tasmop;
  608. s : topsize;
  609. tmpref : treference;
  610. begin
  611. tmpref:=ref;
  612. make_simple_ref(list,tmpref);
  613. floatstoreops(t,op,s);
  614. list.concat(Taicpu.Op_ref(op,s,tmpref));
  615. { storing non extended floats can cause a floating point overflow }
  616. if (t<>OS_F80) and
  617. (cs_fpu_fwait in current_settings.localswitches) then
  618. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  619. dec_fpu_stack;
  620. end;
  621. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  622. begin
  623. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  624. internalerror(200306031);
  625. end;
  626. {****************************************************************************
  627. Assembler code
  628. ****************************************************************************}
  629. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  630. var
  631. r: treference;
  632. begin
  633. if (target_info.system <> system_i386_darwin) then
  634. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s)))
  635. else
  636. begin
  637. reference_reset_symbol(r,get_darwin_call_stub(s,false),0,sizeof(pint));
  638. r.refaddr:=addr_full;
  639. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  640. end;
  641. end;
  642. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  643. begin
  644. a_jmp_cond(list, OC_NONE, l);
  645. end;
  646. function tcgx86.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  647. var
  648. stubname: string;
  649. begin
  650. stubname := 'L'+s+'$stub';
  651. result := current_asmdata.getasmsymbol(stubname);
  652. if assigned(result) then
  653. exit;
  654. if current_asmdata.asmlists[al_imports]=nil then
  655. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  656. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',0);
  657. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION);
  658. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  659. { register as a weak symbol if necessary }
  660. if weak then
  661. current_asmdata.weakrefasmsymbol(s);
  662. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  663. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  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. end;
  669. procedure tcgx86.a_call_name(list : TAsmList;const s : string; weak: boolean);
  670. begin
  671. a_call_name_near(list,s,weak);
  672. end;
  673. procedure tcgx86.a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  674. var
  675. sym : tasmsymbol;
  676. r : treference;
  677. begin
  678. if (target_info.system <> system_i386_darwin) then
  679. begin
  680. if not(weak) then
  681. sym:=current_asmdata.RefAsmSymbol(s)
  682. else
  683. sym:=current_asmdata.WeakRefAsmSymbol(s);
  684. reference_reset_symbol(r,sym,0,sizeof(pint));
  685. if (cs_create_pic in current_settings.moduleswitches) and
  686. { darwin's assembler doesn't want @PLT after call symbols }
  687. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim]) then
  688. begin
  689. {$ifdef i386}
  690. include(current_procinfo.flags,pi_needs_got);
  691. {$endif i386}
  692. r.refaddr:=addr_pic
  693. end
  694. else
  695. r.refaddr:=addr_full;
  696. end
  697. else
  698. begin
  699. reference_reset_symbol(r,get_darwin_call_stub(s,weak),0,sizeof(pint));
  700. r.refaddr:=addr_full;
  701. end;
  702. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  703. end;
  704. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  705. begin
  706. a_call_name_static_near(list,s);
  707. end;
  708. procedure tcgx86.a_call_name_static_near(list : TAsmList;const s : string);
  709. var
  710. sym : tasmsymbol;
  711. r : treference;
  712. begin
  713. sym:=current_asmdata.RefAsmSymbol(s);
  714. reference_reset_symbol(r,sym,0,sizeof(pint));
  715. r.refaddr:=addr_full;
  716. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  717. end;
  718. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  719. begin
  720. a_call_reg_near(list,reg);
  721. end;
  722. procedure tcgx86.a_call_reg_near(list: TAsmList; reg: tregister);
  723. begin
  724. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  725. end;
  726. procedure tcgx86.a_call_ref(list : TAsmList;ref : treference);
  727. begin
  728. a_call_ref_near(list,ref);
  729. end;
  730. procedure tcgx86.a_call_ref_near(list: TAsmList; ref: treference);
  731. begin
  732. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  733. end;
  734. {********************** load instructions ********************}
  735. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : tcgint; reg : TRegister);
  736. begin
  737. check_register_size(tosize,reg);
  738. { the optimizer will change it to "xor reg,reg" when loading zero, }
  739. { no need to do it here too (JM) }
  740. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  741. end;
  742. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  743. var
  744. tmpref : treference;
  745. begin
  746. tmpref:=ref;
  747. make_simple_ref(list,tmpref);
  748. {$ifdef x86_64}
  749. { x86_64 only supports signed 32 bits constants directly }
  750. if (tosize in [OS_S64,OS_64]) and
  751. ((a<low(longint)) or (a>high(longint))) then
  752. begin
  753. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  754. inc(tmpref.offset,4);
  755. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  756. end
  757. else
  758. {$endif x86_64}
  759. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  760. end;
  761. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  762. var
  763. op: tasmop;
  764. s: topsize;
  765. tmpsize : tcgsize;
  766. tmpreg : tregister;
  767. tmpref : treference;
  768. begin
  769. tmpref:=ref;
  770. make_simple_ref(list,tmpref);
  771. check_register_size(fromsize,reg);
  772. sizes2load(fromsize,tosize,op,s);
  773. case s of
  774. {$ifdef x86_64}
  775. S_BQ,S_WQ,S_LQ,
  776. {$endif x86_64}
  777. S_BW,S_BL,S_WL :
  778. begin
  779. tmpreg:=getintregister(list,tosize);
  780. {$ifdef x86_64}
  781. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  782. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  783. 64 bit (FK) }
  784. if s in [S_BL,S_WL,S_L] then
  785. begin
  786. tmpreg:=makeregsize(list,tmpreg,OS_32);
  787. tmpsize:=OS_32;
  788. end
  789. else
  790. {$endif x86_64}
  791. tmpsize:=tosize;
  792. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  793. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  794. end;
  795. else
  796. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  797. end;
  798. end;
  799. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  800. var
  801. op: tasmop;
  802. s: topsize;
  803. tmpref : treference;
  804. begin
  805. tmpref:=ref;
  806. make_simple_ref(list,tmpref);
  807. check_register_size(tosize,reg);
  808. sizes2load(fromsize,tosize,op,s);
  809. {$ifdef x86_64}
  810. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  811. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  812. 64 bit (FK) }
  813. if s in [S_BL,S_WL,S_L] then
  814. reg:=makeregsize(list,reg,OS_32);
  815. {$endif x86_64}
  816. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  817. end;
  818. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  819. var
  820. op: tasmop;
  821. s: topsize;
  822. instr:Taicpu;
  823. begin
  824. check_register_size(fromsize,reg1);
  825. check_register_size(tosize,reg2);
  826. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  827. begin
  828. reg1:=makeregsize(list,reg1,tosize);
  829. s:=tcgsize2opsize[tosize];
  830. op:=A_MOV;
  831. end
  832. else
  833. sizes2load(fromsize,tosize,op,s);
  834. {$ifdef x86_64}
  835. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  836. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  837. 64 bit (FK)
  838. }
  839. if s in [S_BL,S_WL,S_L] then
  840. reg2:=makeregsize(list,reg2,OS_32);
  841. {$endif x86_64}
  842. if (reg1<>reg2) then
  843. begin
  844. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  845. { Notify the register allocator that we have written a move instruction so
  846. it can try to eliminate it. }
  847. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  848. add_move_instruction(instr);
  849. list.concat(instr);
  850. end;
  851. {$ifdef x86_64}
  852. { avoid merging of registers and killing the zero extensions (FK) }
  853. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  854. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  855. {$endif x86_64}
  856. end;
  857. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  858. var
  859. tmpref : treference;
  860. begin
  861. with ref do
  862. begin
  863. if (base=NR_NO) and (index=NR_NO) then
  864. begin
  865. if assigned(ref.symbol) then
  866. begin
  867. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  868. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  869. (cs_create_pic in current_settings.moduleswitches)) then
  870. begin
  871. if (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  872. ((cs_create_pic in current_settings.moduleswitches) and
  873. (ref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  874. begin
  875. reference_reset_base(tmpref,
  876. g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol)),
  877. offset,sizeof(pint));
  878. a_loadaddr_ref_reg(list,tmpref,r);
  879. end
  880. else
  881. begin
  882. include(current_procinfo.flags,pi_needs_got);
  883. reference_reset_base(tmpref,current_procinfo.got,offset,ref.alignment);
  884. tmpref.symbol:=symbol;
  885. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  886. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  887. end;
  888. end
  889. else if (cs_create_pic in current_settings.moduleswitches)
  890. {$ifdef x86_64}
  891. and not(ref.symbol.bind=AB_LOCAL)
  892. {$endif x86_64}
  893. then
  894. begin
  895. {$ifdef x86_64}
  896. reference_reset_symbol(tmpref,ref.symbol,0,ref.alignment);
  897. tmpref.refaddr:=addr_pic;
  898. tmpref.base:=NR_RIP;
  899. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  900. {$else x86_64}
  901. reference_reset_symbol(tmpref,ref.symbol,0,ref.alignment);
  902. tmpref.refaddr:=addr_pic;
  903. tmpref.base:=current_procinfo.got;
  904. include(current_procinfo.flags,pi_needs_got);
  905. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  906. {$endif x86_64}
  907. if offset<>0 then
  908. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  909. end
  910. {$ifdef x86_64}
  911. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin]))
  912. or (cs_create_pic in current_settings.moduleswitches)
  913. then
  914. begin
  915. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  916. tmpref:=ref;
  917. tmpref.base:=NR_RIP;
  918. tmpref.refaddr:=addr_pic_no_got;
  919. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  920. end
  921. {$endif x86_64}
  922. else
  923. begin
  924. tmpref:=ref;
  925. tmpref.refaddr:=ADDR_FULL;
  926. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  927. end
  928. end
  929. else
  930. a_load_const_reg(list,OS_ADDR,offset,r)
  931. end
  932. else if (base=NR_NO) and (index<>NR_NO) and
  933. (offset=0) and (scalefactor=0) and (symbol=nil) then
  934. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  935. else if (base<>NR_NO) and (index=NR_NO) and
  936. (offset=0) and (symbol=nil) then
  937. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  938. else
  939. begin
  940. tmpref:=ref;
  941. make_simple_ref(list,tmpref);
  942. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  943. end;
  944. if segment<>NR_NO then
  945. begin
  946. if (tf_section_threadvars in target_info.flags) then
  947. begin
  948. { Convert thread local address to a process global addres
  949. as we cannot handle far pointers.}
  950. case target_info.system of
  951. system_i386_linux,system_i386_android:
  952. if segment=NR_GS then
  953. begin
  954. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0,ref.alignment);
  955. tmpref.segment:=NR_GS;
  956. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  957. end
  958. else
  959. cgmessage(cg_e_cant_use_far_pointer_there);
  960. else
  961. cgmessage(cg_e_cant_use_far_pointer_there);
  962. end;
  963. end
  964. else
  965. cgmessage(cg_e_cant_use_far_pointer_there);
  966. end;
  967. end;
  968. end;
  969. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  970. { R_ST means "the current value at the top of the fpu stack" (JM) }
  971. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  972. var
  973. href: treference;
  974. op: tasmop;
  975. s: topsize;
  976. begin
  977. if (reg1<>NR_ST) then
  978. begin
  979. floatloadops(tosize,op,s);
  980. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  981. inc_fpu_stack;
  982. end;
  983. if (reg2<>NR_ST) then
  984. begin
  985. floatstoreops(tosize,op,s);
  986. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  987. dec_fpu_stack;
  988. end;
  989. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  990. if (reg1=NR_ST) and
  991. (reg2=NR_ST) and
  992. (tosize<>OS_F80) and
  993. (tosize<fromsize) then
  994. begin
  995. { can't round down to lower precision in x87 :/ }
  996. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  997. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  998. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  999. tg.ungettemp(list,href);
  1000. end;
  1001. end;
  1002. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1003. begin
  1004. floatload(list,fromsize,ref);
  1005. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1006. end;
  1007. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1008. begin
  1009. { in case a record returned in a floating point register
  1010. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1011. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1012. tosize }
  1013. if (fromsize in [OS_F32,OS_F64]) and
  1014. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1015. case tosize of
  1016. OS_32:
  1017. tosize:=OS_F32;
  1018. OS_64:
  1019. tosize:=OS_F64;
  1020. end;
  1021. if reg<>NR_ST then
  1022. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1023. floatstore(list,tosize,ref);
  1024. end;
  1025. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  1026. const
  1027. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1028. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1029. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1030. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1031. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1032. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1033. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1034. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1035. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1036. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1037. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1038. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  1039. begin
  1040. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1041. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1042. if (fromsize in [OS_F32,OS_F64]) and
  1043. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1044. case tosize of
  1045. OS_32:
  1046. tosize:=OS_F32;
  1047. OS_64:
  1048. tosize:=OS_F64;
  1049. end;
  1050. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1051. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1052. begin
  1053. if UseAVX then
  1054. result:=convertopavx[fromsize,tosize]
  1055. else
  1056. result:=convertopsse[fromsize,tosize];
  1057. end
  1058. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1059. OS_64 (record in memory/LOC_REFERENCE) }
  1060. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1061. (fromsize=OS_M64) then
  1062. begin
  1063. if UseAVX then
  1064. result:=A_VMOVQ
  1065. else
  1066. result:=A_MOVQ;
  1067. end
  1068. else
  1069. internalerror(2010060104);
  1070. if result=A_NONE then
  1071. internalerror(200312205);
  1072. end;
  1073. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1074. var
  1075. instr : taicpu;
  1076. op : TAsmOp;
  1077. begin
  1078. if shuffle=nil then
  1079. begin
  1080. if fromsize=tosize then
  1081. { needs correct size in case of spilling }
  1082. case fromsize of
  1083. OS_F32:
  1084. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1085. OS_F64:
  1086. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1087. OS_M64:
  1088. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1089. else
  1090. internalerror(2006091201);
  1091. end
  1092. else
  1093. internalerror(200312202);
  1094. add_move_instruction(instr);
  1095. end
  1096. else if shufflescalar(shuffle) then
  1097. begin
  1098. op:=get_scalar_mm_op(fromsize,tosize);
  1099. { MOVAPD/MOVAPS are normally faster }
  1100. if op=A_MOVSD then
  1101. op:=A_MOVAPD
  1102. else if op=A_MOVSS then
  1103. op:=A_MOVAPS
  1104. { VMOVSD/SS is not available with two register operands }
  1105. else if op=A_VMOVSD then
  1106. op:=A_VMOVAPD
  1107. else if op=A_VMOVSS then
  1108. op:=A_VMOVAPS;
  1109. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1110. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1111. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1112. else
  1113. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1114. case op of
  1115. A_VMOVAPD,
  1116. A_VMOVAPS,
  1117. A_VMOVSS,
  1118. A_VMOVSD,
  1119. A_VMOVQ,
  1120. A_MOVAPD,
  1121. A_MOVAPS,
  1122. A_MOVSS,
  1123. A_MOVSD,
  1124. A_MOVQ:
  1125. add_move_instruction(instr);
  1126. end;
  1127. end
  1128. else
  1129. internalerror(200312201);
  1130. list.concat(instr);
  1131. end;
  1132. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1133. var
  1134. tmpref : treference;
  1135. op : tasmop;
  1136. begin
  1137. tmpref:=ref;
  1138. make_simple_ref(list,tmpref);
  1139. if shuffle=nil then
  1140. begin
  1141. if fromsize=OS_M64 then
  1142. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  1143. else
  1144. {$ifdef x86_64}
  1145. { x86-64 has always properly aligned data }
  1146. list.concat(taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,reg));
  1147. {$else x86_64}
  1148. list.concat(taicpu.op_ref_reg(A_MOVDQU,S_NO,tmpref,reg));
  1149. {$endif x86_64}
  1150. end
  1151. else if shufflescalar(shuffle) then
  1152. begin
  1153. op:=get_scalar_mm_op(fromsize,tosize);
  1154. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1155. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1156. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1157. else
  1158. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1159. end
  1160. else
  1161. internalerror(200312252);
  1162. end;
  1163. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1164. var
  1165. hreg : tregister;
  1166. tmpref : treference;
  1167. op : tasmop;
  1168. begin
  1169. tmpref:=ref;
  1170. make_simple_ref(list,tmpref);
  1171. if shuffle=nil then
  1172. begin
  1173. if fromsize=OS_M64 then
  1174. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  1175. else
  1176. {$ifdef x86_64}
  1177. { x86-64 has always properly aligned data }
  1178. list.concat(taicpu.op_reg_ref(A_MOVDQA,S_NO,reg,tmpref))
  1179. {$else x86_64}
  1180. list.concat(taicpu.op_reg_ref(A_MOVDQU,S_NO,reg,tmpref))
  1181. {$endif x86_64}
  1182. end
  1183. else if shufflescalar(shuffle) then
  1184. begin
  1185. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1186. begin
  1187. hreg:=getmmregister(list,tosize);
  1188. op:=get_scalar_mm_op(fromsize,tosize);
  1189. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1190. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1191. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1192. else
  1193. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1194. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref))
  1195. end
  1196. else
  1197. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1198. end
  1199. else
  1200. internalerror(200312252);
  1201. end;
  1202. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1203. var
  1204. l : tlocation;
  1205. begin
  1206. l.loc:=LOC_REFERENCE;
  1207. l.reference:=ref;
  1208. l.size:=size;
  1209. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1210. end;
  1211. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1212. var
  1213. l : tlocation;
  1214. begin
  1215. l.loc:=LOC_MMREGISTER;
  1216. l.register:=src;
  1217. l.size:=size;
  1218. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1219. end;
  1220. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1221. const
  1222. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1223. ( { scalar }
  1224. ( { OS_F32 }
  1225. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_NOP,A_NOP,A_NOP
  1226. ),
  1227. ( { OS_F64 }
  1228. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_NOP,A_NOP,A_NOP
  1229. )
  1230. ),
  1231. ( { vectorized/packed }
  1232. { because the logical packed single instructions have shorter op codes, we use always
  1233. these
  1234. }
  1235. ( { OS_F32 }
  1236. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1237. ),
  1238. ( { OS_F64 }
  1239. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1240. )
  1241. )
  1242. );
  1243. var
  1244. resultreg : tregister;
  1245. asmop : tasmop;
  1246. begin
  1247. { this is an internally used procedure so the parameters have
  1248. some constrains
  1249. }
  1250. if loc.size<>size then
  1251. internalerror(2013061108);
  1252. resultreg:=dst;
  1253. { deshuffle }
  1254. //!!!
  1255. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1256. begin
  1257. internalerror(2013061107);
  1258. end
  1259. else if (shuffle=nil) then
  1260. asmop:=opmm2asmop[1,size,op]
  1261. else if shufflescalar(shuffle) then
  1262. begin
  1263. asmop:=opmm2asmop[0,size,op];
  1264. { no scalar operation available? }
  1265. if asmop=A_NOP then
  1266. begin
  1267. { do vectorized and shuffle finally }
  1268. internalerror(2010060102);
  1269. end;
  1270. end
  1271. else
  1272. internalerror(2013061106);
  1273. if asmop=A_NOP then
  1274. internalerror(2013061105);
  1275. case loc.loc of
  1276. LOC_CREFERENCE,LOC_REFERENCE:
  1277. begin
  1278. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1279. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1280. end;
  1281. LOC_CMMREGISTER,LOC_MMREGISTER:
  1282. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1283. else
  1284. internalerror(2013061104);
  1285. end;
  1286. { shuffle }
  1287. if resultreg<>dst then
  1288. begin
  1289. internalerror(2013061103);
  1290. end;
  1291. end;
  1292. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1293. var
  1294. l : tlocation;
  1295. begin
  1296. l.loc:=LOC_MMREGISTER;
  1297. l.register:=src1;
  1298. l.size:=size;
  1299. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1300. end;
  1301. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1302. var
  1303. l : tlocation;
  1304. begin
  1305. l.loc:=LOC_REFERENCE;
  1306. l.reference:=ref;
  1307. l.size:=size;
  1308. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1309. end;
  1310. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1311. const
  1312. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1313. ( { scalar }
  1314. ( { OS_F32 }
  1315. A_NOP,A_NOP,A_ADDSS,A_NOP,A_DIVSS,A_NOP,A_NOP,A_MULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSS,A_NOP,A_NOP,A_NOP
  1316. ),
  1317. ( { OS_F64 }
  1318. A_NOP,A_NOP,A_ADDSD,A_NOP,A_DIVSD,A_NOP,A_NOP,A_MULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSD,A_NOP,A_NOP,A_NOP
  1319. )
  1320. ),
  1321. ( { vectorized/packed }
  1322. { because the logical packed single instructions have shorter op codes, we use always
  1323. these
  1324. }
  1325. ( { OS_F32 }
  1326. A_NOP,A_NOP,A_ADDPS,A_NOP,A_DIVPS,A_NOP,A_NOP,A_MULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPS,A_XORPS,A_NOP,A_NOP
  1327. ),
  1328. ( { OS_F64 }
  1329. A_NOP,A_NOP,A_ADDPD,A_NOP,A_DIVPD,A_NOP,A_NOP,A_MULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPD,A_XORPD,A_NOP,A_NOP
  1330. )
  1331. )
  1332. );
  1333. var
  1334. resultreg : tregister;
  1335. asmop : tasmop;
  1336. begin
  1337. { this is an internally used procedure so the parameters have
  1338. some constrains
  1339. }
  1340. if loc.size<>size then
  1341. internalerror(200312213);
  1342. resultreg:=dst;
  1343. { deshuffle }
  1344. //!!!
  1345. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1346. begin
  1347. internalerror(2010060101);
  1348. end
  1349. else if (shuffle=nil) then
  1350. asmop:=opmm2asmop[1,size,op]
  1351. else if shufflescalar(shuffle) then
  1352. begin
  1353. asmop:=opmm2asmop[0,size,op];
  1354. { no scalar operation available? }
  1355. if asmop=A_NOP then
  1356. begin
  1357. { do vectorized and shuffle finally }
  1358. internalerror(2010060102);
  1359. end;
  1360. end
  1361. else
  1362. internalerror(200312211);
  1363. if asmop=A_NOP then
  1364. internalerror(200312216);
  1365. case loc.loc of
  1366. LOC_CREFERENCE,LOC_REFERENCE:
  1367. begin
  1368. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1369. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1370. end;
  1371. LOC_CMMREGISTER,LOC_MMREGISTER:
  1372. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1373. else
  1374. internalerror(200312214);
  1375. end;
  1376. { shuffle }
  1377. if resultreg<>dst then
  1378. begin
  1379. internalerror(200312212);
  1380. end;
  1381. end;
  1382. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1383. var
  1384. opcode : tasmop;
  1385. power : longint;
  1386. {$ifdef x86_64}
  1387. tmpreg : tregister;
  1388. {$endif x86_64}
  1389. begin
  1390. optimize_op_const(op, a);
  1391. {$ifdef x86_64}
  1392. { x86_64 only supports signed 32 bits constants directly }
  1393. if not(op in [OP_NONE,OP_MOVE]) and
  1394. (size in [OS_S64,OS_64]) and
  1395. ((a<low(longint)) or (a>high(longint))) then
  1396. begin
  1397. tmpreg:=getintregister(list,size);
  1398. a_load_const_reg(list,size,a,tmpreg);
  1399. a_op_reg_reg(list,op,size,tmpreg,reg);
  1400. exit;
  1401. end;
  1402. {$endif x86_64}
  1403. check_register_size(size,reg);
  1404. case op of
  1405. OP_NONE :
  1406. begin
  1407. { Opcode is optimized away }
  1408. end;
  1409. OP_MOVE :
  1410. begin
  1411. { Optimized, replaced with a simple load }
  1412. a_load_const_reg(list,size,a,reg);
  1413. end;
  1414. OP_DIV, OP_IDIV:
  1415. begin
  1416. if ispowerof2(int64(a),power) then
  1417. begin
  1418. case op of
  1419. OP_DIV:
  1420. opcode := A_SHR;
  1421. OP_IDIV:
  1422. opcode := A_SAR;
  1423. end;
  1424. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  1425. exit;
  1426. end;
  1427. { the rest should be handled specifically in the code }
  1428. { generator because of the silly register usage restraints }
  1429. internalerror(200109224);
  1430. end;
  1431. OP_MUL,OP_IMUL:
  1432. begin
  1433. if not(cs_check_overflow in current_settings.localswitches) and
  1434. ispowerof2(int64(a),power) then
  1435. begin
  1436. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  1437. exit;
  1438. end;
  1439. if op = OP_IMUL then
  1440. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1441. else
  1442. { OP_MUL should be handled specifically in the code }
  1443. { generator because of the silly register usage restraints }
  1444. internalerror(200109225);
  1445. end;
  1446. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1447. if not(cs_check_overflow in current_settings.localswitches) and
  1448. (a = 1) and
  1449. (op in [OP_ADD,OP_SUB]) and
  1450. UseIncDec then
  1451. begin
  1452. if op = OP_ADD then
  1453. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1454. else
  1455. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1456. end
  1457. else if (a = 0) then
  1458. if (op <> OP_AND) then
  1459. exit
  1460. else
  1461. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  1462. else if (aword(a) = high(aword)) and
  1463. (op in [OP_AND,OP_OR,OP_XOR]) then
  1464. begin
  1465. case op of
  1466. OP_AND:
  1467. exit;
  1468. OP_OR:
  1469. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1470. OP_XOR:
  1471. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1472. end
  1473. end
  1474. else
  1475. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1476. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1477. begin
  1478. {$if defined(x86_64)}
  1479. if (a and 63) <> 0 Then
  1480. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1481. if (a shr 6) <> 0 Then
  1482. internalerror(200609073);
  1483. {$elseif defined(i386)}
  1484. if (a and 31) <> 0 Then
  1485. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1486. if (a shr 5) <> 0 Then
  1487. internalerror(200609071);
  1488. {$elseif defined(i8086)}
  1489. if (a shr 5) <> 0 Then
  1490. internalerror(2013043002);
  1491. a := a and 31;
  1492. if a <> 0 Then
  1493. begin
  1494. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1495. begin
  1496. getcpuregister(list,NR_CL);
  1497. a_load_const_reg(list,OS_8,a,NR_CL);
  1498. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1499. ungetcpuregister(list,NR_CL);
  1500. end
  1501. else
  1502. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1503. end;
  1504. {$endif}
  1505. end
  1506. else internalerror(200609072);
  1507. end;
  1508. end;
  1509. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1510. var
  1511. opcode: tasmop;
  1512. power: longint;
  1513. {$ifdef x86_64}
  1514. tmpreg : tregister;
  1515. {$endif x86_64}
  1516. tmpref : treference;
  1517. begin
  1518. optimize_op_const(op, a);
  1519. tmpref:=ref;
  1520. make_simple_ref(list,tmpref);
  1521. {$ifdef x86_64}
  1522. { x86_64 only supports signed 32 bits constants directly }
  1523. if not(op in [OP_NONE,OP_MOVE]) and
  1524. (size in [OS_S64,OS_64]) and
  1525. ((a<low(longint)) or (a>high(longint))) then
  1526. begin
  1527. tmpreg:=getintregister(list,size);
  1528. a_load_const_reg(list,size,a,tmpreg);
  1529. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1530. exit;
  1531. end;
  1532. {$endif x86_64}
  1533. Case Op of
  1534. OP_NONE :
  1535. begin
  1536. { Opcode is optimized away }
  1537. end;
  1538. OP_MOVE :
  1539. begin
  1540. { Optimized, replaced with a simple load }
  1541. a_load_const_ref(list,size,a,ref);
  1542. end;
  1543. OP_DIV, OP_IDIV:
  1544. Begin
  1545. if ispowerof2(int64(a),power) then
  1546. begin
  1547. case op of
  1548. OP_DIV:
  1549. opcode := A_SHR;
  1550. OP_IDIV:
  1551. opcode := A_SAR;
  1552. end;
  1553. list.concat(taicpu.op_const_ref(opcode,
  1554. TCgSize2OpSize[size],power,tmpref));
  1555. exit;
  1556. end;
  1557. { the rest should be handled specifically in the code }
  1558. { generator because of the silly register usage restraints }
  1559. internalerror(200109231);
  1560. End;
  1561. OP_MUL,OP_IMUL:
  1562. begin
  1563. if not(cs_check_overflow in current_settings.localswitches) and
  1564. ispowerof2(int64(a),power) then
  1565. begin
  1566. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1567. power,tmpref));
  1568. exit;
  1569. end;
  1570. { can't multiply a memory location directly with a constant }
  1571. if op = OP_IMUL then
  1572. inherited a_op_const_ref(list,op,size,a,tmpref)
  1573. else
  1574. { OP_MUL should be handled specifically in the code }
  1575. { generator because of the silly register usage restraints }
  1576. internalerror(200109232);
  1577. end;
  1578. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1579. if not(cs_check_overflow in current_settings.localswitches) and
  1580. (a = 1) and
  1581. (op in [OP_ADD,OP_SUB]) and
  1582. UseIncDec then
  1583. begin
  1584. if op = OP_ADD then
  1585. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1586. else
  1587. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1588. end
  1589. else if (a = 0) then
  1590. if (op <> OP_AND) then
  1591. exit
  1592. else
  1593. a_load_const_ref(list,size,0,tmpref)
  1594. else if (aword(a) = high(aword)) and
  1595. (op in [OP_AND,OP_OR,OP_XOR]) then
  1596. begin
  1597. case op of
  1598. OP_AND:
  1599. exit;
  1600. OP_OR:
  1601. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1602. OP_XOR:
  1603. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1604. end
  1605. end
  1606. else
  1607. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1608. TCgSize2OpSize[size],a,tmpref));
  1609. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1610. begin
  1611. if (a and 31) <> 0 then
  1612. list.concat(taicpu.op_const_ref(
  1613. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1614. if (a shr 5) <> 0 Then
  1615. internalerror(68991);
  1616. end
  1617. else internalerror(68992);
  1618. end;
  1619. end;
  1620. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1621. const
  1622. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  1623. REGCX=NR_ECX;
  1624. REGCX_Size = OS_32;
  1625. {$elseif defined(cpu16bitalu)}
  1626. REGCX=NR_CX;
  1627. REGCX_Size = OS_16;
  1628. {$endif}
  1629. var
  1630. dstsize: topsize;
  1631. instr:Taicpu;
  1632. begin
  1633. check_register_size(size,src);
  1634. check_register_size(size,dst);
  1635. dstsize := tcgsize2opsize[size];
  1636. case op of
  1637. OP_NEG,OP_NOT:
  1638. begin
  1639. if src<>dst then
  1640. a_load_reg_reg(list,size,size,src,dst);
  1641. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1642. end;
  1643. OP_MUL,OP_DIV,OP_IDIV:
  1644. { special stuff, needs separate handling inside code }
  1645. { generator }
  1646. internalerror(200109233);
  1647. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1648. begin
  1649. { Use ecx to load the value, that allows better coalescing }
  1650. getcpuregister(list,REGCX);
  1651. a_load_reg_reg(list,size,REGCX_Size,src,REGCX);
  1652. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1653. ungetcpuregister(list,REGCX);
  1654. end;
  1655. else
  1656. begin
  1657. if reg2opsize(src) <> dstsize then
  1658. internalerror(200109226);
  1659. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1660. list.concat(instr);
  1661. end;
  1662. end;
  1663. end;
  1664. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1665. var
  1666. tmpref : treference;
  1667. begin
  1668. tmpref:=ref;
  1669. make_simple_ref(list,tmpref);
  1670. check_register_size(size,reg);
  1671. case op of
  1672. OP_NEG,OP_NOT,OP_IMUL:
  1673. begin
  1674. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1675. end;
  1676. OP_MUL,OP_DIV,OP_IDIV:
  1677. { special stuff, needs separate handling inside code }
  1678. { generator }
  1679. internalerror(200109239);
  1680. else
  1681. begin
  1682. reg := makeregsize(list,reg,size);
  1683. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1684. end;
  1685. end;
  1686. end;
  1687. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1688. var
  1689. tmpref : treference;
  1690. begin
  1691. tmpref:=ref;
  1692. make_simple_ref(list,tmpref);
  1693. check_register_size(size,reg);
  1694. case op of
  1695. OP_NEG,OP_NOT:
  1696. begin
  1697. if reg<>NR_NO then
  1698. internalerror(200109237);
  1699. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1700. end;
  1701. OP_IMUL:
  1702. begin
  1703. { this one needs a load/imul/store, which is the default }
  1704. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1705. end;
  1706. OP_MUL,OP_DIV,OP_IDIV:
  1707. { special stuff, needs separate handling inside code }
  1708. { generator }
  1709. internalerror(200109238);
  1710. else
  1711. begin
  1712. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1713. end;
  1714. end;
  1715. end;
  1716. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1717. var
  1718. opsize: topsize;
  1719. l : TAsmLabel;
  1720. begin
  1721. opsize:=tcgsize2opsize[size];
  1722. if not reverse then
  1723. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,dst))
  1724. else
  1725. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,dst));
  1726. current_asmdata.getjumplabel(l);
  1727. a_jmp_cond(list,OC_NE,l);
  1728. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,dst));
  1729. a_label(list,l);
  1730. end;
  1731. {*************** compare instructructions ****************}
  1732. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1733. l : tasmlabel);
  1734. {$ifdef x86_64}
  1735. var
  1736. tmpreg : tregister;
  1737. {$endif x86_64}
  1738. begin
  1739. {$ifdef x86_64}
  1740. { x86_64 only supports signed 32 bits constants directly }
  1741. if (size in [OS_S64,OS_64]) and
  1742. ((a<low(longint)) or (a>high(longint))) then
  1743. begin
  1744. tmpreg:=getintregister(list,size);
  1745. a_load_const_reg(list,size,a,tmpreg);
  1746. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1747. exit;
  1748. end;
  1749. {$endif x86_64}
  1750. if (a = 0) then
  1751. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1752. else
  1753. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1754. a_jmp_cond(list,cmp_op,l);
  1755. end;
  1756. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1757. l : tasmlabel);
  1758. var
  1759. {$ifdef x86_64}
  1760. tmpreg : tregister;
  1761. {$endif x86_64}
  1762. tmpref : treference;
  1763. begin
  1764. tmpref:=ref;
  1765. make_simple_ref(list,tmpref);
  1766. {$ifdef x86_64}
  1767. { x86_64 only supports signed 32 bits constants directly }
  1768. if (size in [OS_S64,OS_64]) and
  1769. ((a<low(longint)) or (a>high(longint))) then
  1770. begin
  1771. tmpreg:=getintregister(list,size);
  1772. a_load_const_reg(list,size,a,tmpreg);
  1773. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1774. exit;
  1775. end;
  1776. {$endif x86_64}
  1777. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1778. a_jmp_cond(list,cmp_op,l);
  1779. end;
  1780. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1781. reg1,reg2 : tregister;l : tasmlabel);
  1782. begin
  1783. check_register_size(size,reg1);
  1784. check_register_size(size,reg2);
  1785. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1786. a_jmp_cond(list,cmp_op,l);
  1787. end;
  1788. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1789. var
  1790. tmpref : treference;
  1791. begin
  1792. tmpref:=ref;
  1793. make_simple_ref(list,tmpref);
  1794. check_register_size(size,reg);
  1795. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1796. a_jmp_cond(list,cmp_op,l);
  1797. end;
  1798. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1799. var
  1800. tmpref : treference;
  1801. begin
  1802. tmpref:=ref;
  1803. make_simple_ref(list,tmpref);
  1804. check_register_size(size,reg);
  1805. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1806. a_jmp_cond(list,cmp_op,l);
  1807. end;
  1808. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1809. var
  1810. ai : taicpu;
  1811. begin
  1812. if cond=OC_None then
  1813. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1814. else
  1815. begin
  1816. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1817. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1818. end;
  1819. ai.is_jmp:=true;
  1820. list.concat(ai);
  1821. end;
  1822. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1823. var
  1824. ai : taicpu;
  1825. begin
  1826. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1827. ai.SetCondition(flags_to_cond(f));
  1828. ai.is_jmp := true;
  1829. list.concat(ai);
  1830. end;
  1831. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1832. var
  1833. ai : taicpu;
  1834. hreg : tregister;
  1835. begin
  1836. hreg:=makeregsize(list,reg,OS_8);
  1837. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1838. ai.setcondition(flags_to_cond(f));
  1839. list.concat(ai);
  1840. if reg<>hreg then
  1841. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1842. end;
  1843. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1844. var
  1845. ai : taicpu;
  1846. tmpref : treference;
  1847. begin
  1848. tmpref:=ref;
  1849. make_simple_ref(list,tmpref);
  1850. if not(size in [OS_8,OS_S8]) then
  1851. a_load_const_ref(list,size,0,tmpref);
  1852. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1853. ai.setcondition(flags_to_cond(f));
  1854. list.concat(ai);
  1855. {$ifndef cpu64bitalu}
  1856. if size in [OS_S64,OS_64] then
  1857. begin
  1858. inc(tmpref.offset,4);
  1859. a_load_const_ref(list,OS_32,0,tmpref);
  1860. end;
  1861. {$endif cpu64bitalu}
  1862. end;
  1863. { ************* concatcopy ************ }
  1864. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  1865. const
  1866. {$if defined(cpu64bitalu)}
  1867. REGCX=NR_RCX;
  1868. REGSI=NR_RSI;
  1869. REGDI=NR_RDI;
  1870. copy_len_sizes = [1, 2, 4, 8];
  1871. push_segment_size = S_L;
  1872. {$elseif defined(cpu32bitalu)}
  1873. REGCX=NR_ECX;
  1874. REGSI=NR_ESI;
  1875. REGDI=NR_EDI;
  1876. copy_len_sizes = [1, 2, 4];
  1877. push_segment_size = S_L;
  1878. {$elseif defined(cpu16bitalu)}
  1879. REGCX=NR_CX;
  1880. REGSI=NR_SI;
  1881. REGDI=NR_DI;
  1882. copy_len_sizes = [1, 2];
  1883. push_segment_size = S_W;
  1884. {$endif}
  1885. type copymode=(copy_move,copy_mmx,copy_string);
  1886. var srcref,dstref:Treference;
  1887. r,r0,r1,r2,r3:Tregister;
  1888. helpsize:tcgint;
  1889. copysize:byte;
  1890. cgsize:Tcgsize;
  1891. cm:copymode;
  1892. begin
  1893. cm:=copy_move;
  1894. helpsize:=3*sizeof(aword);
  1895. if cs_opt_size in current_settings.optimizerswitches then
  1896. helpsize:=2*sizeof(aword);
  1897. if (cs_mmx in current_settings.localswitches) and
  1898. not(pi_uses_fpu in current_procinfo.flags) and
  1899. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1900. cm:=copy_mmx;
  1901. if (len>helpsize) then
  1902. cm:=copy_string;
  1903. if (cs_opt_size in current_settings.optimizerswitches) and
  1904. not((len<=16) and (cm=copy_mmx)) and
  1905. not(len in copy_len_sizes) then
  1906. cm:=copy_string;
  1907. {$ifndef i8086}
  1908. if (source.segment<>NR_NO) or
  1909. (dest.segment<>NR_NO) then
  1910. cm:=copy_string;
  1911. {$endif not i8086}
  1912. case cm of
  1913. copy_move:
  1914. begin
  1915. dstref:=dest;
  1916. srcref:=source;
  1917. copysize:=sizeof(aint);
  1918. cgsize:=int_cgsize(copysize);
  1919. while len<>0 do
  1920. begin
  1921. if len<2 then
  1922. begin
  1923. copysize:=1;
  1924. cgsize:=OS_8;
  1925. end
  1926. else if len<4 then
  1927. begin
  1928. copysize:=2;
  1929. cgsize:=OS_16;
  1930. end
  1931. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  1932. else if len<8 then
  1933. begin
  1934. copysize:=4;
  1935. cgsize:=OS_32;
  1936. end
  1937. {$endif cpu32bitalu or cpu64bitalu}
  1938. {$ifdef cpu64bitalu}
  1939. else if len<16 then
  1940. begin
  1941. copysize:=8;
  1942. cgsize:=OS_64;
  1943. end
  1944. {$endif}
  1945. ;
  1946. dec(len,copysize);
  1947. r:=getintregister(list,cgsize);
  1948. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1949. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1950. inc(srcref.offset,copysize);
  1951. inc(dstref.offset,copysize);
  1952. end;
  1953. end;
  1954. copy_mmx:
  1955. begin
  1956. dstref:=dest;
  1957. srcref:=source;
  1958. r0:=getmmxregister(list);
  1959. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1960. if len>=16 then
  1961. begin
  1962. inc(srcref.offset,8);
  1963. r1:=getmmxregister(list);
  1964. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1965. end;
  1966. if len>=24 then
  1967. begin
  1968. inc(srcref.offset,8);
  1969. r2:=getmmxregister(list);
  1970. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1971. end;
  1972. if len>=32 then
  1973. begin
  1974. inc(srcref.offset,8);
  1975. r3:=getmmxregister(list);
  1976. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1977. end;
  1978. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1979. if len>=16 then
  1980. begin
  1981. inc(dstref.offset,8);
  1982. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1983. end;
  1984. if len>=24 then
  1985. begin
  1986. inc(dstref.offset,8);
  1987. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1988. end;
  1989. if len>=32 then
  1990. begin
  1991. inc(dstref.offset,8);
  1992. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1993. end;
  1994. end
  1995. else {copy_string, should be a good fallback in case of unhandled}
  1996. begin
  1997. getcpuregister(list,REGDI);
  1998. if (dest.segment=NR_NO) then
  1999. begin
  2000. a_loadaddr_ref_reg(list,dest,REGDI);
  2001. {$ifdef volatile_es}
  2002. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2003. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2004. {$endif volatile_es}
  2005. end
  2006. else
  2007. begin
  2008. dstref:=dest;
  2009. dstref.segment:=NR_NO;
  2010. a_loadaddr_ref_reg(list,dstref,REGDI);
  2011. {$ifndef volatile_es}
  2012. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2013. {$endif not volatile_es}
  2014. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment));
  2015. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2016. end;
  2017. getcpuregister(list,REGSI);
  2018. if (source.segment=NR_NO) then
  2019. a_loadaddr_ref_reg(list,source,REGSI)
  2020. else
  2021. begin
  2022. srcref:=source;
  2023. srcref.segment:=NR_NO;
  2024. a_loadaddr_ref_reg(list,srcref,REGSI);
  2025. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_DS));
  2026. list.concat(taicpu.op_reg(A_PUSH,S_L,source.segment));
  2027. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  2028. end;
  2029. getcpuregister(list,REGCX);
  2030. if ts_cld in current_settings.targetswitches then
  2031. list.concat(Taicpu.op_none(A_CLD,S_NO));
  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.