cgx86.pas 69 KB

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