cgx86.pas 67 KB

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