cgx86.pas 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  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 = class(tcg)
  30. rgfpu : Trgx86fpu;
  31. procedure done_register_allocators;override;
  32. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  33. function getmmxregister(list:TAsmList):Tregister;
  34. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  35. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  36. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  37. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  38. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  39. function uses_registers(rt:Tregistertype):boolean;override;
  40. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  41. procedure dec_fpu_stack;
  42. procedure inc_fpu_stack;
  43. procedure a_call_name(list : TAsmList;const s : string);override;
  44. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  45. procedure a_call_ref(list : TAsmList;ref : treference);override;
  46. procedure a_call_name_static(list : TAsmList;const s : string);override;
  47. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  48. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference); override;
  49. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  50. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  51. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  52. { move instructions }
  53. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : aint;reg : tregister);override;
  54. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : aint;const ref : treference);override;
  55. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  56. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  57. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  58. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  59. { fpu move instructions }
  60. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  61. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  62. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  63. { vector register move instructions }
  64. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  65. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  66. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  67. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  68. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  69. { comparison operations }
  70. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  71. l : tasmlabel);override;
  72. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  73. l : tasmlabel);override;
  74. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  75. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  76. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  77. procedure a_jmp_name(list : TAsmList;const s : string);override;
  78. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  79. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  80. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  81. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference); override;
  82. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  83. { entry/exit code helpers }
  84. procedure g_profilecode(list : TAsmList);override;
  85. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  86. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  87. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);override;
  88. procedure g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string); override;
  89. procedure make_simple_ref(list:TAsmList;var ref: treference);
  90. protected
  91. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  92. procedure check_register_size(size:tcgsize;reg:tregister);
  93. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  94. function get_darwin_call_stub(const s: string): tasmsymbol;
  95. private
  96. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  97. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  98. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  99. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  100. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  101. end;
  102. const
  103. {$ifdef x86_64}
  104. TCGSize2OpSize: Array[tcgsize] of topsize =
  105. (S_NO,S_B,S_W,S_L,S_Q,S_T,S_B,S_W,S_L,S_Q,S_Q,
  106. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  107. S_NO,S_NO,S_NO,S_MD,S_T,
  108. S_NO,S_NO,S_NO,S_NO,S_T);
  109. {$else x86_64}
  110. TCGSize2OpSize: Array[tcgsize] of topsize =
  111. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  112. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  113. S_NO,S_NO,S_NO,S_MD,S_T,
  114. S_NO,S_NO,S_NO,S_NO,S_T);
  115. {$endif x86_64}
  116. {$ifndef NOTARGETWIN}
  117. winstackpagesize = 4096;
  118. {$endif NOTARGETWIN}
  119. implementation
  120. uses
  121. globals,verbose,systems,cutils,
  122. defutil,paramgr,procinfo,
  123. tgobj,ncgutil,
  124. fmodule;
  125. const
  126. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  127. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  128. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  129. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  130. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  131. procedure Tcgx86.done_register_allocators;
  132. begin
  133. rg[R_INTREGISTER].free;
  134. rg[R_MMREGISTER].free;
  135. rg[R_MMXREGISTER].free;
  136. rgfpu.free;
  137. inherited done_register_allocators;
  138. end;
  139. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  140. begin
  141. result:=rgfpu.getregisterfpu(list);
  142. end;
  143. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  144. begin
  145. if not assigned(rg[R_MMXREGISTER]) then
  146. internalerror(2003121214);
  147. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  148. end;
  149. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  150. begin
  151. if not assigned(rg[R_MMREGISTER]) then
  152. internalerror(2003121234);
  153. case size of
  154. OS_F64:
  155. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  156. OS_F32:
  157. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  158. OS_M128:
  159. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMWHOLE);
  160. else
  161. internalerror(200506041);
  162. end;
  163. end;
  164. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  165. begin
  166. if getregtype(r)=R_FPUREGISTER then
  167. internalerror(2003121210)
  168. else
  169. inherited getcpuregister(list,r);
  170. end;
  171. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  172. begin
  173. if getregtype(r)=R_FPUREGISTER then
  174. rgfpu.ungetregisterfpu(list,r)
  175. else
  176. inherited ungetcpuregister(list,r);
  177. end;
  178. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  179. begin
  180. if rt<>R_FPUREGISTER then
  181. inherited alloccpuregisters(list,rt,r);
  182. end;
  183. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  184. begin
  185. if rt<>R_FPUREGISTER then
  186. inherited dealloccpuregisters(list,rt,r);
  187. end;
  188. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  189. begin
  190. if rt=R_FPUREGISTER then
  191. result:=false
  192. else
  193. result:=inherited uses_registers(rt);
  194. end;
  195. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  196. begin
  197. if getregtype(r)<>R_FPUREGISTER then
  198. inherited add_reg_instruction(instr,r);
  199. end;
  200. procedure tcgx86.dec_fpu_stack;
  201. begin
  202. if rgfpu.fpuvaroffset<=0 then
  203. internalerror(200604201);
  204. dec(rgfpu.fpuvaroffset);
  205. end;
  206. procedure tcgx86.inc_fpu_stack;
  207. begin
  208. inc(rgfpu.fpuvaroffset);
  209. end;
  210. {****************************************************************************
  211. This is private property, keep out! :)
  212. ****************************************************************************}
  213. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  214. begin
  215. { ensure to have always valid sizes }
  216. if s1=OS_NO then
  217. s1:=s2;
  218. if s2=OS_NO then
  219. s2:=s1;
  220. case s2 of
  221. OS_8,OS_S8 :
  222. if S1 in [OS_8,OS_S8] then
  223. s3 := S_B
  224. else
  225. internalerror(200109221);
  226. OS_16,OS_S16:
  227. case s1 of
  228. OS_8,OS_S8:
  229. s3 := S_BW;
  230. OS_16,OS_S16:
  231. s3 := S_W;
  232. else
  233. internalerror(200109222);
  234. end;
  235. OS_32,OS_S32:
  236. case s1 of
  237. OS_8,OS_S8:
  238. s3 := S_BL;
  239. OS_16,OS_S16:
  240. s3 := S_WL;
  241. OS_32,OS_S32:
  242. s3 := S_L;
  243. else
  244. internalerror(200109223);
  245. end;
  246. {$ifdef x86_64}
  247. OS_64,OS_S64:
  248. case s1 of
  249. OS_8:
  250. s3 := S_BL;
  251. OS_S8:
  252. s3 := S_BQ;
  253. OS_16:
  254. s3 := S_WL;
  255. OS_S16:
  256. s3 := S_WQ;
  257. OS_32:
  258. s3 := S_L;
  259. OS_S32:
  260. s3 := S_LQ;
  261. OS_64,OS_S64:
  262. s3 := S_Q;
  263. else
  264. internalerror(200304302);
  265. end;
  266. {$endif x86_64}
  267. else
  268. internalerror(200109227);
  269. end;
  270. if s3 in [S_B,S_W,S_L,S_Q] then
  271. op := A_MOV
  272. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  273. op := A_MOVZX
  274. else
  275. {$ifdef x86_64}
  276. if s3 in [S_LQ] then
  277. op := A_MOVSXD
  278. else
  279. {$endif x86_64}
  280. op := A_MOVSX;
  281. end;
  282. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  283. var
  284. hreg : tregister;
  285. href : treference;
  286. {$ifndef x86_64}
  287. add_hreg: boolean;
  288. {$endif not x86_64}
  289. begin
  290. {$ifdef x86_64}
  291. { Only 32bit is allowed }
  292. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) then
  293. begin
  294. { Load constant value to register }
  295. hreg:=GetAddressRegister(list);
  296. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  297. ref.offset:=0;
  298. {if assigned(ref.symbol) then
  299. begin
  300. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  301. ref.symbol:=nil;
  302. end;}
  303. { Add register to reference }
  304. if ref.index=NR_NO then
  305. ref.index:=hreg
  306. else
  307. begin
  308. if ref.scalefactor<>0 then
  309. begin
  310. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  311. ref.base:=hreg;
  312. end
  313. else
  314. begin
  315. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  316. ref.index:=hreg;
  317. end;
  318. end;
  319. end;
  320. if (cs_create_pic in current_settings.moduleswitches) and
  321. assigned(ref.symbol) and not((ref.symbol.bind=AB_LOCAL) and (ref.symbol.typ in [AT_LABEL,AT_FUNCTION])) then
  322. begin
  323. reference_reset_symbol(href,ref.symbol,0);
  324. hreg:=getaddressregister(list);
  325. href.refaddr:=addr_pic;
  326. href.base:=NR_RIP;
  327. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  328. ref.symbol:=nil;
  329. if ref.base=NR_NO then
  330. ref.base:=hreg
  331. else if ref.index=NR_NO then
  332. begin
  333. ref.index:=hreg;
  334. ref.scalefactor:=1;
  335. end
  336. else
  337. begin
  338. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  339. ref.base:=hreg;
  340. end;
  341. end;
  342. {$else x86_64}
  343. add_hreg:=false;
  344. if (target_info.system=system_i386_darwin) then
  345. begin
  346. if assigned(ref.symbol) and
  347. not(assigned(ref.relsymbol)) and
  348. ((ref.symbol.bind = AB_EXTERNAL) or
  349. (cs_create_pic in current_settings.moduleswitches)) then
  350. begin
  351. if (ref.symbol.bind = AB_EXTERNAL) or
  352. ((cs_create_pic in current_settings.moduleswitches) and
  353. (ref.symbol.bind in [AB_COMMON,AB_GLOBAL])) then
  354. begin
  355. hreg:=g_indirect_sym_load(list,ref.symbol.name);
  356. ref.symbol:=nil;
  357. end
  358. else
  359. begin
  360. include(current_procinfo.flags,pi_needs_got);
  361. hreg:=current_procinfo.got;
  362. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  363. end;
  364. add_hreg:=true
  365. end
  366. end
  367. else if (cs_create_pic in current_settings.moduleswitches) and
  368. assigned(ref.symbol) and
  369. not((ref.symbol.bind=AB_LOCAL) and
  370. (ref.symbol.typ in [AT_LABEL,AT_FUNCTION])) then
  371. begin
  372. href.refaddr:=addr_pic;
  373. href.base:=current_procinfo.got;
  374. include(current_procinfo.flags,pi_needs_got);
  375. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  376. ref.symbol:=nil;
  377. add_hreg:=true;
  378. end;
  379. if add_hreg then
  380. begin
  381. if ref.base=NR_NO then
  382. ref.base:=hreg
  383. else if ref.index=NR_NO then
  384. begin
  385. ref.index:=hreg;
  386. ref.scalefactor:=1;
  387. end
  388. else
  389. begin
  390. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,hreg));
  391. ref.base:=hreg;
  392. end;
  393. end;
  394. {$endif x86_64}
  395. end;
  396. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  397. begin
  398. case t of
  399. OS_F32 :
  400. begin
  401. op:=A_FLD;
  402. s:=S_FS;
  403. end;
  404. OS_F64 :
  405. begin
  406. op:=A_FLD;
  407. s:=S_FL;
  408. end;
  409. OS_F80 :
  410. begin
  411. op:=A_FLD;
  412. s:=S_FX;
  413. end;
  414. OS_C64 :
  415. begin
  416. op:=A_FILD;
  417. s:=S_IQ;
  418. end;
  419. else
  420. internalerror(200204043);
  421. end;
  422. end;
  423. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  424. var
  425. op : tasmop;
  426. s : topsize;
  427. tmpref : treference;
  428. begin
  429. tmpref:=ref;
  430. make_simple_ref(list,tmpref);
  431. floatloadops(t,op,s);
  432. list.concat(Taicpu.Op_ref(op,s,tmpref));
  433. inc_fpu_stack;
  434. end;
  435. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  436. begin
  437. case t of
  438. OS_F32 :
  439. begin
  440. op:=A_FSTP;
  441. s:=S_FS;
  442. end;
  443. OS_F64 :
  444. begin
  445. op:=A_FSTP;
  446. s:=S_FL;
  447. end;
  448. OS_F80 :
  449. begin
  450. op:=A_FSTP;
  451. s:=S_FX;
  452. end;
  453. OS_C64 :
  454. begin
  455. op:=A_FISTP;
  456. s:=S_IQ;
  457. end;
  458. else
  459. internalerror(200204042);
  460. end;
  461. end;
  462. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  463. var
  464. op : tasmop;
  465. s : topsize;
  466. tmpref : treference;
  467. begin
  468. tmpref:=ref;
  469. make_simple_ref(list,tmpref);
  470. floatstoreops(t,op,s);
  471. list.concat(Taicpu.Op_ref(op,s,tmpref));
  472. { storing non extended floats can cause a floating point overflow }
  473. if (t<>OS_F80) and
  474. (cs_fpu_fwait in current_settings.localswitches) then
  475. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  476. dec_fpu_stack;
  477. end;
  478. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  479. begin
  480. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  481. internalerror(200306031);
  482. end;
  483. {****************************************************************************
  484. Assembler code
  485. ****************************************************************************}
  486. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  487. var
  488. r: treference;
  489. begin
  490. if (target_info.system<>system_i386_darwin) then
  491. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s)))
  492. else
  493. begin
  494. reference_reset_symbol(r,get_darwin_call_stub(s),0);
  495. r.refaddr:=addr_full;
  496. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  497. end;
  498. end;
  499. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  500. begin
  501. a_jmp_cond(list, OC_NONE, l);
  502. end;
  503. function tcgx86.get_darwin_call_stub(const s: string): tasmsymbol;
  504. var
  505. stubname: string;
  506. begin
  507. stubname := 'L'+s+'$stub';
  508. result := current_asmdata.getasmsymbol(stubname);
  509. if assigned(result) then
  510. exit;
  511. if current_asmdata.asmlists[al_imports]=nil then
  512. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  513. current_asmdata.asmlists[al_imports].concat(Tai_section.create(sec_stub,'',0));
  514. result := current_asmdata.RefAsmSymbol(stubname);
  515. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  516. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  517. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  518. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  519. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  520. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  521. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  522. end;
  523. procedure tcgx86.a_call_name(list : TAsmList;const s : string);
  524. var
  525. sym : tasmsymbol;
  526. r : treference;
  527. begin
  528. if (target_info.system <> system_i386_darwin) then
  529. begin
  530. sym:=current_asmdata.RefAsmSymbol(s);
  531. reference_reset_symbol(r,sym,0);
  532. if cs_create_pic in current_settings.moduleswitches then
  533. begin
  534. {$ifdef i386}
  535. include(current_procinfo.flags,pi_needs_got);
  536. {$endif i386}
  537. r.refaddr:=addr_pic
  538. end
  539. else
  540. r.refaddr:=addr_full;
  541. end
  542. else
  543. begin
  544. reference_reset_symbol(r,get_darwin_call_stub(s),0);
  545. r.refaddr:=addr_full;
  546. end;
  547. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  548. end;
  549. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  550. var
  551. sym : tasmsymbol;
  552. r : treference;
  553. begin
  554. sym:=current_asmdata.RefAsmSymbol(s);
  555. reference_reset_symbol(r,sym,0);
  556. r.refaddr:=addr_full;
  557. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  558. end;
  559. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  560. begin
  561. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  562. end;
  563. procedure tcgx86.a_call_ref(list : TAsmList;ref : treference);
  564. begin
  565. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  566. end;
  567. {********************** load instructions ********************}
  568. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : aint; reg : TRegister);
  569. begin
  570. check_register_size(tosize,reg);
  571. { the optimizer will change it to "xor reg,reg" when loading zero, }
  572. { no need to do it here too (JM) }
  573. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  574. end;
  575. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : aint;const ref : treference);
  576. var
  577. tmpref : treference;
  578. begin
  579. tmpref:=ref;
  580. make_simple_ref(list,tmpref);
  581. {$ifdef x86_64}
  582. { x86_64 only supports signed 32 bits constants directly }
  583. if (tosize in [OS_S64,OS_64]) and
  584. ((a<low(longint)) or (a>high(longint))) then
  585. begin
  586. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  587. inc(tmpref.offset,4);
  588. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  589. end
  590. else
  591. {$endif x86_64}
  592. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  593. end;
  594. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  595. var
  596. op: tasmop;
  597. s: topsize;
  598. tmpsize : tcgsize;
  599. tmpreg : tregister;
  600. tmpref : treference;
  601. begin
  602. tmpref:=ref;
  603. make_simple_ref(list,tmpref);
  604. check_register_size(fromsize,reg);
  605. sizes2load(fromsize,tosize,op,s);
  606. case s of
  607. {$ifdef x86_64}
  608. S_BQ,S_WQ,S_LQ,
  609. {$endif x86_64}
  610. S_BW,S_BL,S_WL :
  611. begin
  612. tmpreg:=getintregister(list,tosize);
  613. {$ifdef x86_64}
  614. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  615. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  616. 64 bit (FK) }
  617. if s in [S_BL,S_WL,S_L] then
  618. begin
  619. tmpreg:=makeregsize(list,tmpreg,OS_32);
  620. tmpsize:=OS_32;
  621. end
  622. else
  623. {$endif x86_64}
  624. tmpsize:=tosize;
  625. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  626. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  627. end;
  628. else
  629. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  630. end;
  631. end;
  632. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  633. var
  634. op: tasmop;
  635. s: topsize;
  636. tmpref : treference;
  637. begin
  638. tmpref:=ref;
  639. make_simple_ref(list,tmpref);
  640. check_register_size(tosize,reg);
  641. sizes2load(fromsize,tosize,op,s);
  642. {$ifdef x86_64}
  643. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  644. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  645. 64 bit (FK) }
  646. if s in [S_BL,S_WL,S_L] then
  647. reg:=makeregsize(list,reg,OS_32);
  648. {$endif x86_64}
  649. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  650. end;
  651. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  652. var
  653. op: tasmop;
  654. s: topsize;
  655. instr:Taicpu;
  656. begin
  657. check_register_size(fromsize,reg1);
  658. check_register_size(tosize,reg2);
  659. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  660. begin
  661. reg1:=makeregsize(list,reg1,tosize);
  662. s:=tcgsize2opsize[tosize];
  663. op:=A_MOV;
  664. end
  665. else
  666. sizes2load(fromsize,tosize,op,s);
  667. {$ifdef x86_64}
  668. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  669. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  670. 64 bit (FK)
  671. }
  672. if s in [S_BL,S_WL,S_L] then
  673. reg2:=makeregsize(list,reg2,OS_32);
  674. {$endif x86_64}
  675. if (reg1<>reg2) then
  676. begin
  677. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  678. { Notify the register allocator that we have written a move instruction so
  679. it can try to eliminate it. }
  680. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  681. add_move_instruction(instr);
  682. list.concat(instr);
  683. end;
  684. {$ifdef x86_64}
  685. { avoid merging of registers and killing the zero extensions (FK) }
  686. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  687. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  688. {$endif x86_64}
  689. end;
  690. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  691. var
  692. tmpref : treference;
  693. begin
  694. with ref do
  695. begin
  696. if (base=NR_NO) and (index=NR_NO) then
  697. begin
  698. if assigned(ref.symbol) then
  699. begin
  700. if (target_info.system=system_i386_darwin) and
  701. ((ref.symbol.bind = AB_EXTERNAL) or
  702. (cs_create_pic in current_settings.moduleswitches)) then
  703. begin
  704. if (ref.symbol.bind = AB_EXTERNAL) or
  705. ((cs_create_pic in current_settings.moduleswitches) and
  706. (ref.symbol.bind in [AB_COMMON,AB_GLOBAL])) then
  707. begin
  708. reference_reset_base(tmpref,
  709. g_indirect_sym_load(list,ref.symbol.name),
  710. offset);
  711. a_loadaddr_ref_reg(list,tmpref,r);
  712. end
  713. else
  714. begin
  715. include(current_procinfo.flags,pi_needs_got);
  716. reference_reset_base(tmpref,current_procinfo.got,offset);
  717. tmpref.symbol:=symbol;
  718. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  719. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  720. end;
  721. end
  722. else if (cs_create_pic in current_settings.moduleswitches) then
  723. begin
  724. {$ifdef x86_64}
  725. reference_reset_symbol(tmpref,ref.symbol,0);
  726. tmpref.refaddr:=addr_pic;
  727. tmpref.base:=NR_RIP;
  728. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  729. {$else x86_64}
  730. reference_reset_symbol(tmpref,ref.symbol,0);
  731. tmpref.refaddr:=addr_pic;
  732. tmpref.base:=current_procinfo.got;
  733. include(current_procinfo.flags,pi_needs_got);
  734. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  735. {$endif x86_64}
  736. if offset<>0 then
  737. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  738. end
  739. else
  740. begin
  741. tmpref:=ref;
  742. tmpref.refaddr:=ADDR_FULL;
  743. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  744. end
  745. end
  746. else
  747. a_load_const_reg(list,OS_ADDR,offset,r)
  748. end
  749. else if (base=NR_NO) and (index<>NR_NO) and
  750. (offset=0) and (scalefactor=0) and (symbol=nil) then
  751. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  752. else if (base<>NR_NO) and (index=NR_NO) and
  753. (offset=0) and (symbol=nil) then
  754. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  755. else
  756. begin
  757. tmpref:=ref;
  758. make_simple_ref(list,tmpref);
  759. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  760. end;
  761. if segment<>NR_NO then
  762. begin
  763. if (tf_section_threadvars in target_info.flags) then
  764. begin
  765. { Convert thread local address to a process global addres
  766. as we cannot handle far pointers.}
  767. case target_info.system of
  768. system_i386_linux:
  769. if segment=NR_GS then
  770. begin
  771. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0);
  772. tmpref.segment:=NR_GS;
  773. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  774. end
  775. else
  776. cgmessage(cg_e_cant_use_far_pointer_there);
  777. system_i386_win32:
  778. if segment=NR_FS then
  779. begin
  780. allocallcpuregisters(list);
  781. a_call_name(list,'GetTls');
  782. deallocallcpuregisters(list);
  783. list.concat(Taicpu.op_reg_reg(A_ADD,tcgsize2opsize[OS_ADDR],NR_EAX,r));
  784. end
  785. else
  786. cgmessage(cg_e_cant_use_far_pointer_there);
  787. else
  788. cgmessage(cg_e_cant_use_far_pointer_there);
  789. end;
  790. end
  791. else
  792. cgmessage(cg_e_cant_use_far_pointer_there);
  793. end;
  794. end;
  795. end;
  796. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  797. { R_ST means "the current value at the top of the fpu stack" (JM) }
  798. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  799. var
  800. href: treference;
  801. op: tasmop;
  802. s: topsize;
  803. begin
  804. if (reg1<>NR_ST) then
  805. begin
  806. floatloadops(tosize,op,s);
  807. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  808. inc_fpu_stack;
  809. end;
  810. if (reg2<>NR_ST) then
  811. begin
  812. floatstoreops(tosize,op,s);
  813. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  814. dec_fpu_stack;
  815. end;
  816. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  817. if (reg1=NR_ST) and
  818. (reg2=NR_ST) and
  819. (tosize<>OS_F80) and
  820. (tosize<fromsize) then
  821. begin
  822. { can't round down to lower precision in x87 :/ }
  823. tg.gettemp(list,tcgsize2size[tosize],tt_normal,href);
  824. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  825. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  826. tg.ungettemp(list,href);
  827. end;
  828. end;
  829. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  830. begin
  831. floatload(list,fromsize,ref);
  832. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  833. end;
  834. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  835. begin
  836. if reg<>NR_ST then
  837. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  838. floatstore(list,tosize,ref);
  839. end;
  840. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  841. const
  842. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  843. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  844. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  845. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  846. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  847. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  848. begin
  849. result:=convertop[fromsize,tosize];
  850. if result=A_NONE then
  851. internalerror(200312205);
  852. end;
  853. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  854. var
  855. instr : taicpu;
  856. begin
  857. if shuffle=nil then
  858. begin
  859. if fromsize=tosize then
  860. { needs correct size in case of spilling }
  861. case fromsize of
  862. OS_F32:
  863. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  864. OS_F64:
  865. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  866. else
  867. internalerror(2006091201);
  868. end
  869. else
  870. internalerror(200312202);
  871. end
  872. else if shufflescalar(shuffle) then
  873. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  874. else
  875. internalerror(200312201);
  876. case get_scalar_mm_op(fromsize,tosize) of
  877. A_MOVSS,
  878. A_MOVSD,
  879. A_MOVQ:
  880. add_move_instruction(instr);
  881. end;
  882. list.concat(instr);
  883. end;
  884. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  885. var
  886. tmpref : treference;
  887. begin
  888. tmpref:=ref;
  889. make_simple_ref(list,tmpref);
  890. if shuffle=nil then
  891. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  892. else if shufflescalar(shuffle) then
  893. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  894. else
  895. internalerror(200312252);
  896. end;
  897. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  898. var
  899. hreg : tregister;
  900. tmpref : treference;
  901. begin
  902. tmpref:=ref;
  903. make_simple_ref(list,tmpref);
  904. if shuffle=nil then
  905. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  906. else if shufflescalar(shuffle) then
  907. begin
  908. if tosize<>fromsize then
  909. begin
  910. hreg:=getmmregister(list,tosize);
  911. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  912. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  913. end
  914. else
  915. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  916. end
  917. else
  918. internalerror(200312252);
  919. end;
  920. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  921. var
  922. l : tlocation;
  923. begin
  924. l.loc:=LOC_REFERENCE;
  925. l.reference:=ref;
  926. l.size:=size;
  927. opmm_loc_reg(list,op,size,l,reg,shuffle);
  928. end;
  929. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  930. var
  931. l : tlocation;
  932. begin
  933. l.loc:=LOC_MMREGISTER;
  934. l.register:=src;
  935. l.size:=size;
  936. opmm_loc_reg(list,op,size,l,dst,shuffle);
  937. end;
  938. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  939. const
  940. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  941. ( { scalar }
  942. ( { OS_F32 }
  943. 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
  944. ),
  945. ( { OS_F64 }
  946. 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
  947. )
  948. ),
  949. ( { vectorized/packed }
  950. { because the logical packed single instructions have shorter op codes, we use always
  951. these
  952. }
  953. ( { OS_F32 }
  954. 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
  955. ),
  956. ( { OS_F64 }
  957. 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
  958. )
  959. )
  960. );
  961. var
  962. resultreg : tregister;
  963. asmop : tasmop;
  964. begin
  965. { this is an internally used procedure so the parameters have
  966. some constrains
  967. }
  968. if loc.size<>size then
  969. internalerror(200312213);
  970. resultreg:=dst;
  971. { deshuffle }
  972. //!!!
  973. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  974. begin
  975. end
  976. else if (shuffle=nil) then
  977. asmop:=opmm2asmop[1,size,op]
  978. else if shufflescalar(shuffle) then
  979. begin
  980. asmop:=opmm2asmop[0,size,op];
  981. { no scalar operation available? }
  982. if asmop=A_NOP then
  983. begin
  984. { do vectorized and shuffle finally }
  985. //!!!
  986. end;
  987. end
  988. else
  989. internalerror(200312211);
  990. if asmop=A_NOP then
  991. internalerror(200312216);
  992. case loc.loc of
  993. LOC_CREFERENCE,LOC_REFERENCE:
  994. begin
  995. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  996. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  997. end;
  998. LOC_CMMREGISTER,LOC_MMREGISTER:
  999. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1000. else
  1001. internalerror(200312214);
  1002. end;
  1003. { shuffle }
  1004. if resultreg<>dst then
  1005. begin
  1006. internalerror(200312212);
  1007. end;
  1008. end;
  1009. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  1010. var
  1011. opcode : tasmop;
  1012. power : longint;
  1013. {$ifdef x86_64}
  1014. tmpreg : tregister;
  1015. {$endif x86_64}
  1016. begin
  1017. optimize_op_const(op, a);
  1018. {$ifdef x86_64}
  1019. { x86_64 only supports signed 32 bits constants directly }
  1020. if not(op in [OP_NONE,OP_MOVE]) and
  1021. (size in [OS_S64,OS_64]) and
  1022. ((a<low(longint)) or (a>high(longint))) then
  1023. begin
  1024. tmpreg:=getintregister(list,size);
  1025. a_load_const_reg(list,size,a,tmpreg);
  1026. a_op_reg_reg(list,op,size,tmpreg,reg);
  1027. exit;
  1028. end;
  1029. {$endif x86_64}
  1030. check_register_size(size,reg);
  1031. case op of
  1032. OP_NONE :
  1033. begin
  1034. { Opcode is optimized away }
  1035. end;
  1036. OP_MOVE :
  1037. begin
  1038. { Optimized, replaced with a simple load }
  1039. a_load_const_reg(list,size,a,reg);
  1040. end;
  1041. OP_DIV, OP_IDIV:
  1042. begin
  1043. if ispowerof2(int64(a),power) then
  1044. begin
  1045. case op of
  1046. OP_DIV:
  1047. opcode := A_SHR;
  1048. OP_IDIV:
  1049. opcode := A_SAR;
  1050. end;
  1051. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  1052. exit;
  1053. end;
  1054. { the rest should be handled specifically in the code }
  1055. { generator because of the silly register usage restraints }
  1056. internalerror(200109224);
  1057. end;
  1058. OP_MUL,OP_IMUL:
  1059. begin
  1060. if not(cs_check_overflow in current_settings.localswitches) and
  1061. ispowerof2(int64(a),power) then
  1062. begin
  1063. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  1064. exit;
  1065. end;
  1066. if op = OP_IMUL then
  1067. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1068. else
  1069. { OP_MUL should be handled specifically in the code }
  1070. { generator because of the silly register usage restraints }
  1071. internalerror(200109225);
  1072. end;
  1073. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1074. if not(cs_check_overflow in current_settings.localswitches) and
  1075. (a = 1) and
  1076. (op in [OP_ADD,OP_SUB]) then
  1077. if op = OP_ADD then
  1078. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1079. else
  1080. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1081. else if (a = 0) then
  1082. if (op <> OP_AND) then
  1083. exit
  1084. else
  1085. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  1086. else if (aword(a) = high(aword)) and
  1087. (op in [OP_AND,OP_OR,OP_XOR]) then
  1088. begin
  1089. case op of
  1090. OP_AND:
  1091. exit;
  1092. OP_OR:
  1093. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1094. OP_XOR:
  1095. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1096. end
  1097. end
  1098. else
  1099. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1100. OP_SHL,OP_SHR,OP_SAR:
  1101. begin
  1102. {$ifdef x86_64}
  1103. if (a and 63) <> 0 Then
  1104. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1105. if (a shr 6) <> 0 Then
  1106. internalerror(200609073);
  1107. {$else x86_64}
  1108. if (a and 31) <> 0 Then
  1109. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1110. if (a shr 5) <> 0 Then
  1111. internalerror(200609071);
  1112. {$endif x86_64}
  1113. end
  1114. else internalerror(200609072);
  1115. end;
  1116. end;
  1117. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1118. var
  1119. opcode: tasmop;
  1120. power: longint;
  1121. {$ifdef x86_64}
  1122. tmpreg : tregister;
  1123. {$endif x86_64}
  1124. tmpref : treference;
  1125. begin
  1126. optimize_op_const(op, a);
  1127. tmpref:=ref;
  1128. make_simple_ref(list,tmpref);
  1129. {$ifdef x86_64}
  1130. { x86_64 only supports signed 32 bits constants directly }
  1131. if not(op in [OP_NONE,OP_MOVE]) and
  1132. (size in [OS_S64,OS_64]) and
  1133. ((a<low(longint)) or (a>high(longint))) then
  1134. begin
  1135. tmpreg:=getintregister(list,size);
  1136. a_load_const_reg(list,size,a,tmpreg);
  1137. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1138. exit;
  1139. end;
  1140. {$endif x86_64}
  1141. Case Op of
  1142. OP_NONE :
  1143. begin
  1144. { Opcode is optimized away }
  1145. end;
  1146. OP_MOVE :
  1147. begin
  1148. { Optimized, replaced with a simple load }
  1149. a_load_const_ref(list,size,a,ref);
  1150. end;
  1151. OP_DIV, OP_IDIV:
  1152. Begin
  1153. if ispowerof2(int64(a),power) then
  1154. begin
  1155. case op of
  1156. OP_DIV:
  1157. opcode := A_SHR;
  1158. OP_IDIV:
  1159. opcode := A_SAR;
  1160. end;
  1161. list.concat(taicpu.op_const_ref(opcode,
  1162. TCgSize2OpSize[size],power,tmpref));
  1163. exit;
  1164. end;
  1165. { the rest should be handled specifically in the code }
  1166. { generator because of the silly register usage restraints }
  1167. internalerror(200109231);
  1168. End;
  1169. OP_MUL,OP_IMUL:
  1170. begin
  1171. if not(cs_check_overflow in current_settings.localswitches) and
  1172. ispowerof2(int64(a),power) then
  1173. begin
  1174. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1175. power,tmpref));
  1176. exit;
  1177. end;
  1178. { can't multiply a memory location directly with a constant }
  1179. if op = OP_IMUL then
  1180. inherited a_op_const_ref(list,op,size,a,tmpref)
  1181. else
  1182. { OP_MUL should be handled specifically in the code }
  1183. { generator because of the silly register usage restraints }
  1184. internalerror(200109232);
  1185. end;
  1186. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1187. if not(cs_check_overflow in current_settings.localswitches) and
  1188. (a = 1) and
  1189. (op in [OP_ADD,OP_SUB]) then
  1190. if op = OP_ADD then
  1191. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1192. else
  1193. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1194. else if (a = 0) then
  1195. if (op <> OP_AND) then
  1196. exit
  1197. else
  1198. a_load_const_ref(list,size,0,tmpref)
  1199. else if (aword(a) = high(aword)) and
  1200. (op in [OP_AND,OP_OR,OP_XOR]) then
  1201. begin
  1202. case op of
  1203. OP_AND:
  1204. exit;
  1205. OP_OR:
  1206. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1207. OP_XOR:
  1208. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1209. end
  1210. end
  1211. else
  1212. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1213. TCgSize2OpSize[size],a,tmpref));
  1214. OP_SHL,OP_SHR,OP_SAR:
  1215. begin
  1216. if (a and 31) <> 0 then
  1217. list.concat(taicpu.op_const_ref(
  1218. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1219. if (a shr 5) <> 0 Then
  1220. internalerror(68991);
  1221. end
  1222. else internalerror(68992);
  1223. end;
  1224. end;
  1225. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1226. var
  1227. dstsize: topsize;
  1228. instr:Taicpu;
  1229. begin
  1230. check_register_size(size,src);
  1231. check_register_size(size,dst);
  1232. dstsize := tcgsize2opsize[size];
  1233. case op of
  1234. OP_NEG,OP_NOT:
  1235. begin
  1236. if src<>dst then
  1237. a_load_reg_reg(list,size,size,src,dst);
  1238. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1239. end;
  1240. OP_MUL,OP_DIV,OP_IDIV:
  1241. { special stuff, needs separate handling inside code }
  1242. { generator }
  1243. internalerror(200109233);
  1244. OP_SHR,OP_SHL,OP_SAR:
  1245. begin
  1246. { Use ecx to load the value, that allows beter coalescing }
  1247. getcpuregister(list,NR_ECX);
  1248. a_load_reg_reg(list,size,OS_32,src,NR_ECX);
  1249. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1250. ungetcpuregister(list,NR_ECX);
  1251. end;
  1252. else
  1253. begin
  1254. if reg2opsize(src) <> dstsize then
  1255. internalerror(200109226);
  1256. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1257. list.concat(instr);
  1258. end;
  1259. end;
  1260. end;
  1261. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1262. var
  1263. tmpref : treference;
  1264. begin
  1265. tmpref:=ref;
  1266. make_simple_ref(list,tmpref);
  1267. check_register_size(size,reg);
  1268. case op of
  1269. OP_NEG,OP_NOT,OP_IMUL:
  1270. begin
  1271. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1272. end;
  1273. OP_MUL,OP_DIV,OP_IDIV:
  1274. { special stuff, needs separate handling inside code }
  1275. { generator }
  1276. internalerror(200109239);
  1277. else
  1278. begin
  1279. reg := makeregsize(list,reg,size);
  1280. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1281. end;
  1282. end;
  1283. end;
  1284. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1285. var
  1286. tmpref : treference;
  1287. begin
  1288. tmpref:=ref;
  1289. make_simple_ref(list,tmpref);
  1290. check_register_size(size,reg);
  1291. case op of
  1292. OP_NEG,OP_NOT:
  1293. begin
  1294. if reg<>NR_NO then
  1295. internalerror(200109237);
  1296. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1297. end;
  1298. OP_IMUL:
  1299. begin
  1300. { this one needs a load/imul/store, which is the default }
  1301. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1302. end;
  1303. OP_MUL,OP_DIV,OP_IDIV:
  1304. { special stuff, needs separate handling inside code }
  1305. { generator }
  1306. internalerror(200109238);
  1307. else
  1308. begin
  1309. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1310. end;
  1311. end;
  1312. end;
  1313. {*************** compare instructructions ****************}
  1314. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1315. l : tasmlabel);
  1316. {$ifdef x86_64}
  1317. var
  1318. tmpreg : tregister;
  1319. {$endif x86_64}
  1320. begin
  1321. {$ifdef x86_64}
  1322. { x86_64 only supports signed 32 bits constants directly }
  1323. if (size in [OS_S64,OS_64]) and
  1324. ((a<low(longint)) or (a>high(longint))) then
  1325. begin
  1326. tmpreg:=getintregister(list,size);
  1327. a_load_const_reg(list,size,a,tmpreg);
  1328. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1329. exit;
  1330. end;
  1331. {$endif x86_64}
  1332. if (a = 0) then
  1333. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1334. else
  1335. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1336. a_jmp_cond(list,cmp_op,l);
  1337. end;
  1338. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1339. l : tasmlabel);
  1340. var
  1341. {$ifdef x86_64}
  1342. tmpreg : tregister;
  1343. {$endif x86_64}
  1344. tmpref : treference;
  1345. begin
  1346. tmpref:=ref;
  1347. make_simple_ref(list,tmpref);
  1348. {$ifdef x86_64}
  1349. { x86_64 only supports signed 32 bits constants directly }
  1350. if (size in [OS_S64,OS_64]) and
  1351. ((a<low(longint)) or (a>high(longint))) then
  1352. begin
  1353. tmpreg:=getintregister(list,size);
  1354. a_load_const_reg(list,size,a,tmpreg);
  1355. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1356. exit;
  1357. end;
  1358. {$endif x86_64}
  1359. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1360. a_jmp_cond(list,cmp_op,l);
  1361. end;
  1362. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1363. reg1,reg2 : tregister;l : tasmlabel);
  1364. begin
  1365. check_register_size(size,reg1);
  1366. check_register_size(size,reg2);
  1367. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1368. a_jmp_cond(list,cmp_op,l);
  1369. end;
  1370. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1371. var
  1372. tmpref : treference;
  1373. begin
  1374. tmpref:=ref;
  1375. make_simple_ref(list,tmpref);
  1376. check_register_size(size,reg);
  1377. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1378. a_jmp_cond(list,cmp_op,l);
  1379. end;
  1380. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1381. var
  1382. tmpref : treference;
  1383. begin
  1384. tmpref:=ref;
  1385. make_simple_ref(list,tmpref);
  1386. check_register_size(size,reg);
  1387. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1388. a_jmp_cond(list,cmp_op,l);
  1389. end;
  1390. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1391. var
  1392. ai : taicpu;
  1393. begin
  1394. if cond=OC_None then
  1395. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1396. else
  1397. begin
  1398. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1399. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1400. end;
  1401. ai.is_jmp:=true;
  1402. list.concat(ai);
  1403. end;
  1404. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1405. var
  1406. ai : taicpu;
  1407. begin
  1408. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1409. ai.SetCondition(flags_to_cond(f));
  1410. ai.is_jmp := true;
  1411. list.concat(ai);
  1412. end;
  1413. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1414. var
  1415. ai : taicpu;
  1416. hreg : tregister;
  1417. begin
  1418. hreg:=makeregsize(list,reg,OS_8);
  1419. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1420. ai.setcondition(flags_to_cond(f));
  1421. list.concat(ai);
  1422. if (reg<>hreg) then
  1423. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1424. end;
  1425. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1426. var
  1427. ai : taicpu;
  1428. tmpref : treference;
  1429. begin
  1430. tmpref:=ref;
  1431. make_simple_ref(list,tmpref);
  1432. if not(size in [OS_8,OS_S8]) then
  1433. a_load_const_ref(list,size,0,tmpref);
  1434. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1435. ai.setcondition(flags_to_cond(f));
  1436. list.concat(ai);
  1437. end;
  1438. { ************* concatcopy ************ }
  1439. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:aint);
  1440. const
  1441. {$ifdef cpu64bit}
  1442. REGCX=NR_RCX;
  1443. REGSI=NR_RSI;
  1444. REGDI=NR_RDI;
  1445. {$else cpu64bit}
  1446. REGCX=NR_ECX;
  1447. REGSI=NR_ESI;
  1448. REGDI=NR_EDI;
  1449. {$endif cpu64bit}
  1450. type copymode=(copy_move,copy_mmx,copy_string);
  1451. var srcref,dstref:Treference;
  1452. r,r0,r1,r2,r3:Tregister;
  1453. helpsize:aint;
  1454. copysize:byte;
  1455. cgsize:Tcgsize;
  1456. cm:copymode;
  1457. begin
  1458. cm:=copy_move;
  1459. helpsize:=3*sizeof(aword);
  1460. if cs_opt_size in current_settings.optimizerswitches then
  1461. helpsize:=2*sizeof(aword);
  1462. if (cs_mmx in current_settings.localswitches) and
  1463. not(pi_uses_fpu in current_procinfo.flags) and
  1464. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1465. cm:=copy_mmx;
  1466. if (len>helpsize) then
  1467. cm:=copy_string;
  1468. if (cs_opt_size in current_settings.optimizerswitches) and
  1469. not((len<=16) and (cm=copy_mmx)) then
  1470. cm:=copy_string;
  1471. if (source.segment<>NR_NO) or
  1472. (dest.segment<>NR_NO) then
  1473. cm:=copy_string;
  1474. case cm of
  1475. copy_move:
  1476. begin
  1477. dstref:=dest;
  1478. srcref:=source;
  1479. copysize:=sizeof(aint);
  1480. cgsize:=int_cgsize(copysize);
  1481. while len<>0 do
  1482. begin
  1483. if len<2 then
  1484. begin
  1485. copysize:=1;
  1486. cgsize:=OS_8;
  1487. end
  1488. else if len<4 then
  1489. begin
  1490. copysize:=2;
  1491. cgsize:=OS_16;
  1492. end
  1493. else if len<8 then
  1494. begin
  1495. copysize:=4;
  1496. cgsize:=OS_32;
  1497. end
  1498. {$ifdef cpu64bit}
  1499. else if len<16 then
  1500. begin
  1501. copysize:=8;
  1502. cgsize:=OS_64;
  1503. end
  1504. {$endif}
  1505. ;
  1506. dec(len,copysize);
  1507. r:=getintregister(list,cgsize);
  1508. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1509. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1510. inc(srcref.offset,copysize);
  1511. inc(dstref.offset,copysize);
  1512. end;
  1513. end;
  1514. copy_mmx:
  1515. begin
  1516. dstref:=dest;
  1517. srcref:=source;
  1518. r0:=getmmxregister(list);
  1519. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1520. if len>=16 then
  1521. begin
  1522. inc(srcref.offset,8);
  1523. r1:=getmmxregister(list);
  1524. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1525. end;
  1526. if len>=24 then
  1527. begin
  1528. inc(srcref.offset,8);
  1529. r2:=getmmxregister(list);
  1530. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1531. end;
  1532. if len>=32 then
  1533. begin
  1534. inc(srcref.offset,8);
  1535. r3:=getmmxregister(list);
  1536. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1537. end;
  1538. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1539. if len>=16 then
  1540. begin
  1541. inc(dstref.offset,8);
  1542. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1543. end;
  1544. if len>=24 then
  1545. begin
  1546. inc(dstref.offset,8);
  1547. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1548. end;
  1549. if len>=32 then
  1550. begin
  1551. inc(dstref.offset,8);
  1552. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1553. end;
  1554. end
  1555. else {copy_string, should be a good fallback in case of unhandled}
  1556. begin
  1557. getcpuregister(list,REGDI);
  1558. if (dest.segment=NR_NO) then
  1559. a_loadaddr_ref_reg(list,dest,REGDI)
  1560. else
  1561. begin
  1562. dstref:=dest;
  1563. dstref.segment:=NR_NO;
  1564. a_loadaddr_ref_reg(list,dstref,REGDI);
  1565. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_ES));
  1566. list.concat(taicpu.op_reg(A_PUSH,S_L,dest.segment));
  1567. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1568. end;
  1569. getcpuregister(list,REGSI);
  1570. if (source.segment=NR_NO) then
  1571. a_loadaddr_ref_reg(list,source,REGSI)
  1572. else
  1573. begin
  1574. srcref:=source;
  1575. srcref.segment:=NR_NO;
  1576. a_loadaddr_ref_reg(list,srcref,REGSI);
  1577. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_DS));
  1578. list.concat(taicpu.op_reg(A_PUSH,S_L,source.segment));
  1579. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1580. end;
  1581. getcpuregister(list,REGCX);
  1582. {$ifdef i386}
  1583. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1584. {$endif i386}
  1585. if (cs_opt_size in current_settings.optimizerswitches) and
  1586. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  1587. begin
  1588. a_load_const_reg(list,OS_INT,len,REGCX);
  1589. list.concat(Taicpu.op_none(A_REP,S_NO));
  1590. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1591. end
  1592. else
  1593. begin
  1594. helpsize:=len div sizeof(aint);
  1595. len:=len mod sizeof(aint);
  1596. if helpsize>1 then
  1597. begin
  1598. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1599. list.concat(Taicpu.op_none(A_REP,S_NO));
  1600. end;
  1601. if helpsize>0 then
  1602. begin
  1603. {$ifdef cpu64bit}
  1604. if sizeof(aint)=8 then
  1605. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1606. else
  1607. {$endif cpu64bit}
  1608. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1609. end;
  1610. if len>=4 then
  1611. begin
  1612. dec(len,4);
  1613. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1614. end;
  1615. if len>=2 then
  1616. begin
  1617. dec(len,2);
  1618. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1619. end;
  1620. if len=1 then
  1621. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1622. end;
  1623. ungetcpuregister(list,REGCX);
  1624. ungetcpuregister(list,REGSI);
  1625. ungetcpuregister(list,REGDI);
  1626. if (source.segment<>NR_NO) then
  1627. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1628. if (dest.segment<>NR_NO) then
  1629. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1630. end;
  1631. end;
  1632. end;
  1633. {****************************************************************************
  1634. Entry/Exit Code Helpers
  1635. ****************************************************************************}
  1636. procedure tcgx86.g_profilecode(list : TAsmList);
  1637. var
  1638. pl : tasmlabel;
  1639. mcountprefix : String[4];
  1640. begin
  1641. case target_info.system of
  1642. {$ifndef NOTARGETWIN}
  1643. system_i386_win32,
  1644. {$endif}
  1645. system_i386_freebsd,
  1646. system_i386_netbsd,
  1647. // system_i386_openbsd,
  1648. system_i386_wdosx :
  1649. begin
  1650. Case target_info.system Of
  1651. system_i386_freebsd : mcountprefix:='.';
  1652. system_i386_netbsd : mcountprefix:='__';
  1653. // system_i386_openbsd : mcountprefix:='.';
  1654. else
  1655. mcountPrefix:='';
  1656. end;
  1657. current_asmdata.getaddrlabel(pl);
  1658. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(aint));
  1659. list.concat(Tai_label.Create(pl));
  1660. list.concat(Tai_const.Create_32bit(0));
  1661. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1662. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1663. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1664. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount');
  1665. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1666. end;
  1667. system_i386_linux:
  1668. a_call_name(list,target_info.Cprefix+'mcount');
  1669. system_i386_go32v2,system_i386_watcom:
  1670. begin
  1671. a_call_name(list,'MCOUNT');
  1672. end;
  1673. system_x86_64_linux:
  1674. begin
  1675. a_call_name(list,'mcount');
  1676. end;
  1677. end;
  1678. end;
  1679. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1680. {$ifdef x86}
  1681. {$ifndef NOTARGETWIN}
  1682. var
  1683. href : treference;
  1684. i : integer;
  1685. again : tasmlabel;
  1686. {$endif NOTARGETWIN}
  1687. {$endif x86}
  1688. begin
  1689. if localsize>0 then
  1690. begin
  1691. {$ifdef i386}
  1692. {$ifndef NOTARGETWIN}
  1693. { windows guards only a few pages for stack growing,
  1694. so we have to access every page first }
  1695. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  1696. (localsize>=winstackpagesize) then
  1697. begin
  1698. if localsize div winstackpagesize<=5 then
  1699. begin
  1700. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1701. for i:=1 to localsize div winstackpagesize do
  1702. begin
  1703. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1704. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1705. end;
  1706. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1707. end
  1708. else
  1709. begin
  1710. current_asmdata.getjumplabel(again);
  1711. getcpuregister(list,NR_EDI);
  1712. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1713. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1714. a_label(list,again);
  1715. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1716. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1717. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1718. a_jmp_cond(list,OC_NE,again);
  1719. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize - 4,NR_ESP));
  1720. reference_reset_base(href,NR_ESP,localsize-4);
  1721. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  1722. ungetcpuregister(list,NR_EDI);
  1723. end
  1724. end
  1725. else
  1726. {$endif NOTARGETWIN}
  1727. {$endif i386}
  1728. {$ifdef x86_64}
  1729. {$ifndef NOTARGETWIN}
  1730. { windows guards only a few pages for stack growing,
  1731. so we have to access every page first }
  1732. if (target_info.system=system_x86_64_win64) and
  1733. (localsize>=winstackpagesize) then
  1734. begin
  1735. if localsize div winstackpagesize<=5 then
  1736. begin
  1737. list.concat(Taicpu.Op_const_reg(A_SUB,S_Q,localsize,NR_RSP));
  1738. for i:=1 to localsize div winstackpagesize do
  1739. begin
  1740. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4);
  1741. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1742. end;
  1743. reference_reset_base(href,NR_RSP,0);
  1744. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1745. end
  1746. else
  1747. begin
  1748. current_asmdata.getjumplabel(again);
  1749. getcpuregister(list,NR_R10);
  1750. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  1751. a_label(list,again);
  1752. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,winstackpagesize,NR_RSP));
  1753. reference_reset_base(href,NR_RSP,0);
  1754. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1755. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10));
  1756. a_jmp_cond(list,OC_NE,again);
  1757. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,localsize mod winstackpagesize,NR_RSP));
  1758. ungetcpuregister(list,NR_R10);
  1759. end
  1760. end
  1761. else
  1762. {$endif NOTARGETWIN}
  1763. {$endif x86_64}
  1764. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1765. end;
  1766. end;
  1767. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1768. var
  1769. stackmisalignment: longint;
  1770. begin
  1771. {$ifdef i386}
  1772. { interrupt support for i386 }
  1773. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1774. { this messes up stack alignment }
  1775. (target_info.system <> system_i386_darwin) then
  1776. begin
  1777. { .... also the segment registers }
  1778. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1779. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1780. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1781. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1782. { save the registers of an interrupt procedure }
  1783. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1784. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1785. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1786. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1787. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1788. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1789. end;
  1790. {$endif i386}
  1791. { save old framepointer }
  1792. if not nostackframe then
  1793. begin
  1794. { return address }
  1795. stackmisalignment := sizeof(aint);
  1796. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1797. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1798. CGmessage(cg_d_stackframe_omited)
  1799. else
  1800. begin
  1801. { push <frame_pointer> }
  1802. inc(stackmisalignment,sizeof(aint));
  1803. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1804. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1805. { Return address and FP are both on stack }
  1806. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(aint));
  1807. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(aint)));
  1808. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1809. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1810. end;
  1811. { allocate stackframe space }
  1812. if (localsize<>0) or
  1813. ((target_info.system in [system_i386_darwin,
  1814. system_x86_64_win64,system_x86_64_linux,system_x86_64_freebsd]) and
  1815. (stackmisalignment <> 0) and
  1816. ((pi_do_call in current_procinfo.flags) or
  1817. (po_assembler in current_procinfo.procdef.procoptions))) then
  1818. begin
  1819. if (target_info.system in [system_i386_darwin,
  1820. system_x86_64_win64,system_x86_64_linux,system_x86_64_freebsd]) then
  1821. localsize := align(localsize+stackmisalignment,16)-stackmisalignment;
  1822. cg.g_stackpointer_alloc(list,localsize);
  1823. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1824. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(aint));
  1825. end;
  1826. end;
  1827. end;
  1828. { produces if necessary overflowcode }
  1829. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  1830. var
  1831. hl : tasmlabel;
  1832. ai : taicpu;
  1833. cond : TAsmCond;
  1834. begin
  1835. if not(cs_check_overflow in current_settings.localswitches) then
  1836. exit;
  1837. current_asmdata.getjumplabel(hl);
  1838. if not ((def.typ=pointerdef) or
  1839. ((def.typ=orddef) and
  1840. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1841. bool8bit,bool16bit,bool32bit,bool64bit]))) then
  1842. cond:=C_NO
  1843. else
  1844. cond:=C_NB;
  1845. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1846. ai.SetCondition(cond);
  1847. ai.is_jmp:=true;
  1848. list.concat(ai);
  1849. a_call_name(list,'FPC_OVERFLOW');
  1850. a_label(list,hl);
  1851. end;
  1852. procedure tcgx86.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  1853. var
  1854. ref : treference;
  1855. sym : tasmsymbol;
  1856. begin
  1857. if (target_info.system=system_i386_darwin) then
  1858. begin
  1859. { a_jmp_name jumps to a stub which is always pic-safe on darwin }
  1860. inherited g_external_wrapper(list,procdef,externalname);
  1861. exit;
  1862. end;
  1863. sym:=current_asmdata.RefAsmSymbol(externalname);
  1864. reference_reset_symbol(ref,sym,0);
  1865. { create pic'ed? }
  1866. if cs_create_pic in current_settings.moduleswitches then
  1867. begin
  1868. { it could be that we're called from a procedure not having the
  1869. got loaded
  1870. }
  1871. g_maybe_got_init(list);
  1872. ref.refaddr:=addr_pic
  1873. end
  1874. else
  1875. ref.refaddr:=addr_full;
  1876. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1877. end;
  1878. end.