cgx86.pas 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927
  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. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2)
  778. else
  779. internalerror(200312202);
  780. end
  781. else if shufflescalar(shuffle) then
  782. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  783. else
  784. internalerror(200312201);
  785. case get_scalar_mm_op(fromsize,tosize) of
  786. A_MOVSS,
  787. A_MOVSD,
  788. A_MOVQ:
  789. add_move_instruction(instr);
  790. end;
  791. list.concat(instr);
  792. end;
  793. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  794. var
  795. tmpref : treference;
  796. begin
  797. tmpref:=ref;
  798. make_simple_ref(list,tmpref);
  799. if shuffle=nil then
  800. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  801. else if shufflescalar(shuffle) then
  802. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  803. else
  804. internalerror(200312252);
  805. end;
  806. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  807. var
  808. hreg : tregister;
  809. tmpref : treference;
  810. begin
  811. tmpref:=ref;
  812. make_simple_ref(list,tmpref);
  813. if shuffle=nil then
  814. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  815. else if shufflescalar(shuffle) then
  816. begin
  817. if tosize<>fromsize then
  818. begin
  819. hreg:=getmmregister(list,tosize);
  820. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  821. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  822. end
  823. else
  824. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  825. end
  826. else
  827. internalerror(200312252);
  828. end;
  829. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  830. var
  831. l : tlocation;
  832. begin
  833. l.loc:=LOC_REFERENCE;
  834. l.reference:=ref;
  835. l.size:=size;
  836. opmm_loc_reg(list,op,size,l,reg,shuffle);
  837. end;
  838. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  839. var
  840. l : tlocation;
  841. begin
  842. l.loc:=LOC_MMREGISTER;
  843. l.register:=src;
  844. l.size:=size;
  845. opmm_loc_reg(list,op,size,l,dst,shuffle);
  846. end;
  847. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  848. const
  849. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  850. ( { scalar }
  851. ( { OS_F32 }
  852. 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
  853. ),
  854. ( { OS_F64 }
  855. 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
  856. )
  857. ),
  858. ( { vectorized/packed }
  859. { because the logical packed single instructions have shorter op codes, we use always
  860. these
  861. }
  862. ( { OS_F32 }
  863. 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
  864. ),
  865. ( { OS_F64 }
  866. 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
  867. )
  868. )
  869. );
  870. var
  871. resultreg : tregister;
  872. asmop : tasmop;
  873. begin
  874. { this is an internally used procedure so the parameters have
  875. some constrains
  876. }
  877. if loc.size<>size then
  878. internalerror(200312213);
  879. resultreg:=dst;
  880. { deshuffle }
  881. //!!!
  882. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  883. begin
  884. end
  885. else if (shuffle=nil) then
  886. asmop:=opmm2asmop[1,size,op]
  887. else if shufflescalar(shuffle) then
  888. begin
  889. asmop:=opmm2asmop[0,size,op];
  890. { no scalar operation available? }
  891. if asmop=A_NOP then
  892. begin
  893. { do vectorized and shuffle finally }
  894. //!!!
  895. end;
  896. end
  897. else
  898. internalerror(200312211);
  899. if asmop=A_NOP then
  900. internalerror(200312215);
  901. case loc.loc of
  902. LOC_CREFERENCE,LOC_REFERENCE:
  903. begin
  904. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  905. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  906. end;
  907. LOC_CMMREGISTER,LOC_MMREGISTER:
  908. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  909. else
  910. internalerror(200312214);
  911. end;
  912. { shuffle }
  913. if resultreg<>dst then
  914. begin
  915. internalerror(200312212);
  916. end;
  917. end;
  918. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  919. var
  920. opcode : tasmop;
  921. power : longint;
  922. {$ifdef x86_64}
  923. tmpreg : tregister;
  924. {$endif x86_64}
  925. begin
  926. optimize_op_const(op, a);
  927. {$ifdef x86_64}
  928. { x86_64 only supports signed 32 bits constants directly }
  929. if not(op in [OP_NONE,OP_MOVE]) and
  930. (size in [OS_S64,OS_64]) and
  931. ((a<low(longint)) or (a>high(longint))) then
  932. begin
  933. tmpreg:=getintregister(list,size);
  934. a_load_const_reg(list,size,a,tmpreg);
  935. a_op_reg_reg(list,op,size,tmpreg,reg);
  936. exit;
  937. end;
  938. {$endif x86_64}
  939. check_register_size(size,reg);
  940. case op of
  941. OP_NONE :
  942. begin
  943. { Opcode is optimized away }
  944. end;
  945. OP_MOVE :
  946. begin
  947. { Optimized, replaced with a simple load }
  948. a_load_const_reg(list,size,a,reg);
  949. end;
  950. OP_DIV, OP_IDIV:
  951. begin
  952. if ispowerof2(int64(a),power) then
  953. begin
  954. case op of
  955. OP_DIV:
  956. opcode := A_SHR;
  957. OP_IDIV:
  958. opcode := A_SAR;
  959. end;
  960. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  961. exit;
  962. end;
  963. { the rest should be handled specifically in the code }
  964. { generator because of the silly register usage restraints }
  965. internalerror(200109224);
  966. end;
  967. OP_MUL,OP_IMUL:
  968. begin
  969. if not(cs_check_overflow in aktlocalswitches) and
  970. ispowerof2(int64(a),power) then
  971. begin
  972. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  973. exit;
  974. end;
  975. if op = OP_IMUL then
  976. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  977. else
  978. { OP_MUL should be handled specifically in the code }
  979. { generator because of the silly register usage restraints }
  980. internalerror(200109225);
  981. end;
  982. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  983. if not(cs_check_overflow in aktlocalswitches) and
  984. (a = 1) and
  985. (op in [OP_ADD,OP_SUB]) then
  986. if op = OP_ADD then
  987. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  988. else
  989. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  990. else if (a = 0) then
  991. if (op <> OP_AND) then
  992. exit
  993. else
  994. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  995. else if (aword(a) = high(aword)) and
  996. (op in [OP_AND,OP_OR,OP_XOR]) then
  997. begin
  998. case op of
  999. OP_AND:
  1000. exit;
  1001. OP_OR:
  1002. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1003. OP_XOR:
  1004. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1005. end
  1006. end
  1007. else
  1008. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1009. OP_SHL,OP_SHR,OP_SAR:
  1010. begin
  1011. {$ifdef x86_64}
  1012. if (a and 63) <> 0 Then
  1013. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1014. if (a shr 6) <> 0 Then
  1015. internalerror(200609073);
  1016. {$else x86_64}
  1017. if (a and 31) <> 0 Then
  1018. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1019. if (a shr 5) <> 0 Then
  1020. internalerror(200609071);
  1021. {$endif x86_64}
  1022. end
  1023. else internalerror(200609072);
  1024. end;
  1025. end;
  1026. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1027. var
  1028. opcode: tasmop;
  1029. power: longint;
  1030. {$ifdef x86_64}
  1031. tmpreg : tregister;
  1032. {$endif x86_64}
  1033. tmpref : treference;
  1034. begin
  1035. optimize_op_const(op, a);
  1036. tmpref:=ref;
  1037. make_simple_ref(list,tmpref);
  1038. {$ifdef x86_64}
  1039. { x86_64 only supports signed 32 bits constants directly }
  1040. if not(op in [OP_NONE,OP_MOVE]) and
  1041. (size in [OS_S64,OS_64]) and
  1042. ((a<low(longint)) or (a>high(longint))) then
  1043. begin
  1044. tmpreg:=getintregister(list,size);
  1045. a_load_const_reg(list,size,a,tmpreg);
  1046. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1047. exit;
  1048. end;
  1049. {$endif x86_64}
  1050. Case Op of
  1051. OP_NONE :
  1052. begin
  1053. { Opcode is optimized away }
  1054. end;
  1055. OP_MOVE :
  1056. begin
  1057. { Optimized, replaced with a simple load }
  1058. a_load_const_ref(list,size,a,ref);
  1059. end;
  1060. OP_DIV, OP_IDIV:
  1061. Begin
  1062. if ispowerof2(int64(a),power) then
  1063. begin
  1064. case op of
  1065. OP_DIV:
  1066. opcode := A_SHR;
  1067. OP_IDIV:
  1068. opcode := A_SAR;
  1069. end;
  1070. list.concat(taicpu.op_const_ref(opcode,
  1071. TCgSize2OpSize[size],power,tmpref));
  1072. exit;
  1073. end;
  1074. { the rest should be handled specifically in the code }
  1075. { generator because of the silly register usage restraints }
  1076. internalerror(200109231);
  1077. End;
  1078. OP_MUL,OP_IMUL:
  1079. begin
  1080. if not(cs_check_overflow in aktlocalswitches) and
  1081. ispowerof2(int64(a),power) then
  1082. begin
  1083. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1084. power,tmpref));
  1085. exit;
  1086. end;
  1087. { can't multiply a memory location directly with a constant }
  1088. if op = OP_IMUL then
  1089. inherited a_op_const_ref(list,op,size,a,tmpref)
  1090. else
  1091. { OP_MUL should be handled specifically in the code }
  1092. { generator because of the silly register usage restraints }
  1093. internalerror(200109232);
  1094. end;
  1095. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1096. if not(cs_check_overflow in aktlocalswitches) and
  1097. (a = 1) and
  1098. (op in [OP_ADD,OP_SUB]) then
  1099. if op = OP_ADD then
  1100. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1101. else
  1102. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1103. else if (a = 0) then
  1104. if (op <> OP_AND) then
  1105. exit
  1106. else
  1107. a_load_const_ref(list,size,0,tmpref)
  1108. else if (aword(a) = high(aword)) and
  1109. (op in [OP_AND,OP_OR,OP_XOR]) then
  1110. begin
  1111. case op of
  1112. OP_AND:
  1113. exit;
  1114. OP_OR:
  1115. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1116. OP_XOR:
  1117. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1118. end
  1119. end
  1120. else
  1121. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1122. TCgSize2OpSize[size],a,tmpref));
  1123. OP_SHL,OP_SHR,OP_SAR:
  1124. begin
  1125. if (a and 31) <> 0 then
  1126. list.concat(taicpu.op_const_ref(
  1127. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1128. if (a shr 5) <> 0 Then
  1129. internalerror(68991);
  1130. end
  1131. else internalerror(68992);
  1132. end;
  1133. end;
  1134. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1135. var
  1136. dstsize: topsize;
  1137. instr:Taicpu;
  1138. begin
  1139. check_register_size(size,src);
  1140. check_register_size(size,dst);
  1141. dstsize := tcgsize2opsize[size];
  1142. case op of
  1143. OP_NEG,OP_NOT:
  1144. begin
  1145. if src<>dst then
  1146. a_load_reg_reg(list,size,size,src,dst);
  1147. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1148. end;
  1149. OP_MUL,OP_DIV,OP_IDIV:
  1150. { special stuff, needs separate handling inside code }
  1151. { generator }
  1152. internalerror(200109233);
  1153. OP_SHR,OP_SHL,OP_SAR:
  1154. begin
  1155. { Use ecx to load the value, that allows beter coalescing }
  1156. getcpuregister(list,NR_ECX);
  1157. a_load_reg_reg(list,size,OS_32,src,NR_ECX);
  1158. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1159. ungetcpuregister(list,NR_ECX);
  1160. end;
  1161. else
  1162. begin
  1163. if reg2opsize(src) <> dstsize then
  1164. internalerror(200109226);
  1165. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1166. list.concat(instr);
  1167. end;
  1168. end;
  1169. end;
  1170. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1171. var
  1172. tmpref : treference;
  1173. begin
  1174. tmpref:=ref;
  1175. make_simple_ref(list,tmpref);
  1176. check_register_size(size,reg);
  1177. case op of
  1178. OP_NEG,OP_NOT,OP_IMUL:
  1179. begin
  1180. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1181. end;
  1182. OP_MUL,OP_DIV,OP_IDIV:
  1183. { special stuff, needs separate handling inside code }
  1184. { generator }
  1185. internalerror(200109239);
  1186. else
  1187. begin
  1188. reg := makeregsize(list,reg,size);
  1189. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1190. end;
  1191. end;
  1192. end;
  1193. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1194. var
  1195. tmpref : treference;
  1196. begin
  1197. tmpref:=ref;
  1198. make_simple_ref(list,tmpref);
  1199. check_register_size(size,reg);
  1200. case op of
  1201. OP_NEG,OP_NOT:
  1202. begin
  1203. if reg<>NR_NO then
  1204. internalerror(200109237);
  1205. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1206. end;
  1207. OP_IMUL:
  1208. begin
  1209. { this one needs a load/imul/store, which is the default }
  1210. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1211. end;
  1212. OP_MUL,OP_DIV,OP_IDIV:
  1213. { special stuff, needs separate handling inside code }
  1214. { generator }
  1215. internalerror(200109238);
  1216. else
  1217. begin
  1218. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1219. end;
  1220. end;
  1221. end;
  1222. {*************** compare instructructions ****************}
  1223. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1224. l : tasmlabel);
  1225. {$ifdef x86_64}
  1226. var
  1227. tmpreg : tregister;
  1228. {$endif x86_64}
  1229. begin
  1230. {$ifdef x86_64}
  1231. { x86_64 only supports signed 32 bits constants directly }
  1232. if (size in [OS_S64,OS_64]) and
  1233. ((a<low(longint)) or (a>high(longint))) then
  1234. begin
  1235. tmpreg:=getintregister(list,size);
  1236. a_load_const_reg(list,size,a,tmpreg);
  1237. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1238. exit;
  1239. end;
  1240. {$endif x86_64}
  1241. if (a = 0) then
  1242. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1243. else
  1244. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1245. a_jmp_cond(list,cmp_op,l);
  1246. end;
  1247. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1248. l : tasmlabel);
  1249. var
  1250. {$ifdef x86_64}
  1251. tmpreg : tregister;
  1252. {$endif x86_64}
  1253. tmpref : treference;
  1254. begin
  1255. tmpref:=ref;
  1256. make_simple_ref(list,tmpref);
  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_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1265. exit;
  1266. end;
  1267. {$endif x86_64}
  1268. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1269. a_jmp_cond(list,cmp_op,l);
  1270. end;
  1271. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1272. reg1,reg2 : tregister;l : tasmlabel);
  1273. begin
  1274. check_register_size(size,reg1);
  1275. check_register_size(size,reg2);
  1276. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1277. a_jmp_cond(list,cmp_op,l);
  1278. end;
  1279. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1280. var
  1281. tmpref : treference;
  1282. begin
  1283. tmpref:=ref;
  1284. make_simple_ref(list,tmpref);
  1285. check_register_size(size,reg);
  1286. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1287. a_jmp_cond(list,cmp_op,l);
  1288. end;
  1289. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1290. var
  1291. tmpref : treference;
  1292. begin
  1293. tmpref:=ref;
  1294. make_simple_ref(list,tmpref);
  1295. check_register_size(size,reg);
  1296. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1297. a_jmp_cond(list,cmp_op,l);
  1298. end;
  1299. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1300. var
  1301. ai : taicpu;
  1302. begin
  1303. if cond=OC_None then
  1304. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1305. else
  1306. begin
  1307. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1308. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1309. end;
  1310. ai.is_jmp:=true;
  1311. list.concat(ai);
  1312. end;
  1313. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1314. var
  1315. ai : taicpu;
  1316. begin
  1317. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1318. ai.SetCondition(flags_to_cond(f));
  1319. ai.is_jmp := true;
  1320. list.concat(ai);
  1321. end;
  1322. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1323. var
  1324. ai : taicpu;
  1325. hreg : tregister;
  1326. begin
  1327. hreg:=makeregsize(list,reg,OS_8);
  1328. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1329. ai.setcondition(flags_to_cond(f));
  1330. list.concat(ai);
  1331. if (reg<>hreg) then
  1332. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1333. end;
  1334. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1335. var
  1336. ai : taicpu;
  1337. tmpref : treference;
  1338. begin
  1339. tmpref:=ref;
  1340. make_simple_ref(list,tmpref);
  1341. if not(size in [OS_8,OS_S8]) then
  1342. a_load_const_ref(list,size,0,tmpref);
  1343. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1344. ai.setcondition(flags_to_cond(f));
  1345. list.concat(ai);
  1346. end;
  1347. { ************* concatcopy ************ }
  1348. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:aint);
  1349. const
  1350. {$ifdef cpu64bit}
  1351. REGCX=NR_RCX;
  1352. REGSI=NR_RSI;
  1353. REGDI=NR_RDI;
  1354. {$else cpu64bit}
  1355. REGCX=NR_ECX;
  1356. REGSI=NR_ESI;
  1357. REGDI=NR_EDI;
  1358. {$endif cpu64bit}
  1359. type copymode=(copy_move,copy_mmx,copy_string);
  1360. var srcref,dstref:Treference;
  1361. r,r0,r1,r2,r3:Tregister;
  1362. helpsize:aint;
  1363. copysize:byte;
  1364. cgsize:Tcgsize;
  1365. cm:copymode;
  1366. begin
  1367. cm:=copy_move;
  1368. helpsize:=12;
  1369. if cs_opt_size in aktoptimizerswitches then
  1370. helpsize:=8;
  1371. if (cs_mmx in aktlocalswitches) and
  1372. not(pi_uses_fpu in current_procinfo.flags) and
  1373. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1374. cm:=copy_mmx;
  1375. if (len>helpsize) then
  1376. cm:=copy_string;
  1377. if (cs_opt_size in aktoptimizerswitches) and
  1378. not((len<=16) and (cm=copy_mmx)) then
  1379. cm:=copy_string;
  1380. case cm of
  1381. copy_move:
  1382. begin
  1383. dstref:=dest;
  1384. srcref:=source;
  1385. copysize:=sizeof(aint);
  1386. cgsize:=int_cgsize(copysize);
  1387. while len<>0 do
  1388. begin
  1389. if len<2 then
  1390. begin
  1391. copysize:=1;
  1392. cgsize:=OS_8;
  1393. end
  1394. else if len<4 then
  1395. begin
  1396. copysize:=2;
  1397. cgsize:=OS_16;
  1398. end
  1399. else if len<8 then
  1400. begin
  1401. copysize:=4;
  1402. cgsize:=OS_32;
  1403. end;
  1404. dec(len,copysize);
  1405. r:=getintregister(list,cgsize);
  1406. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1407. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1408. inc(srcref.offset,copysize);
  1409. inc(dstref.offset,copysize);
  1410. end;
  1411. end;
  1412. copy_mmx:
  1413. begin
  1414. dstref:=dest;
  1415. srcref:=source;
  1416. r0:=getmmxregister(list);
  1417. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1418. if len>=16 then
  1419. begin
  1420. inc(srcref.offset,8);
  1421. r1:=getmmxregister(list);
  1422. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1423. end;
  1424. if len>=24 then
  1425. begin
  1426. inc(srcref.offset,8);
  1427. r2:=getmmxregister(list);
  1428. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1429. end;
  1430. if len>=32 then
  1431. begin
  1432. inc(srcref.offset,8);
  1433. r3:=getmmxregister(list);
  1434. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1435. end;
  1436. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1437. if len>=16 then
  1438. begin
  1439. inc(dstref.offset,8);
  1440. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1441. end;
  1442. if len>=24 then
  1443. begin
  1444. inc(dstref.offset,8);
  1445. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1446. end;
  1447. if len>=32 then
  1448. begin
  1449. inc(dstref.offset,8);
  1450. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1451. end;
  1452. end
  1453. else {copy_string, should be a good fallback in case of unhandled}
  1454. begin
  1455. getcpuregister(list,REGDI);
  1456. a_loadaddr_ref_reg(list,dest,REGDI);
  1457. getcpuregister(list,REGSI);
  1458. a_loadaddr_ref_reg(list,source,REGSI);
  1459. getcpuregister(list,REGCX);
  1460. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1461. if cs_opt_size in aktoptimizerswitches then
  1462. begin
  1463. a_load_const_reg(list,OS_INT,len,REGCX);
  1464. list.concat(Taicpu.op_none(A_REP,S_NO));
  1465. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1466. end
  1467. else
  1468. begin
  1469. helpsize:=len div sizeof(aint);
  1470. len:=len mod sizeof(aint);
  1471. if helpsize>1 then
  1472. begin
  1473. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1474. list.concat(Taicpu.op_none(A_REP,S_NO));
  1475. end;
  1476. if helpsize>0 then
  1477. begin
  1478. {$ifdef cpu64bit}
  1479. if sizeof(aint)=8 then
  1480. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1481. else
  1482. {$endif cpu64bit}
  1483. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1484. end;
  1485. if len>=4 then
  1486. begin
  1487. dec(len,4);
  1488. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1489. end;
  1490. if len>=2 then
  1491. begin
  1492. dec(len,2);
  1493. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1494. end;
  1495. if len=1 then
  1496. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1497. end;
  1498. ungetcpuregister(list,REGCX);
  1499. ungetcpuregister(list,REGSI);
  1500. ungetcpuregister(list,REGDI);
  1501. end;
  1502. end;
  1503. end;
  1504. {****************************************************************************
  1505. Entry/Exit Code Helpers
  1506. ****************************************************************************}
  1507. procedure tcgx86.g_releasevaluepara_openarray(list : TAsmList;const l:tlocation);
  1508. begin
  1509. if (use_fixed_stack) then
  1510. begin
  1511. inherited g_releasevaluepara_openarray(list,l);
  1512. exit;
  1513. end;
  1514. { Nothing to release }
  1515. end;
  1516. procedure tcgx86.g_profilecode(list : TAsmList);
  1517. var
  1518. pl : tasmlabel;
  1519. mcountprefix : String[4];
  1520. begin
  1521. case target_info.system of
  1522. {$ifndef NOTARGETWIN}
  1523. system_i386_win32,
  1524. {$endif}
  1525. system_i386_freebsd,
  1526. system_i386_netbsd,
  1527. // system_i386_openbsd,
  1528. system_i386_wdosx :
  1529. begin
  1530. Case target_info.system Of
  1531. system_i386_freebsd : mcountprefix:='.';
  1532. system_i386_netbsd : mcountprefix:='__';
  1533. // system_i386_openbsd : mcountprefix:='.';
  1534. else
  1535. mcountPrefix:='';
  1536. end;
  1537. current_asmdata.getaddrlabel(pl);
  1538. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(aint));
  1539. list.concat(Tai_label.Create(pl));
  1540. list.concat(Tai_const.Create_32bit(0));
  1541. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1542. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1543. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1544. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount');
  1545. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1546. end;
  1547. system_i386_linux:
  1548. a_call_name(list,target_info.Cprefix+'mcount');
  1549. system_i386_go32v2,system_i386_watcom:
  1550. begin
  1551. a_call_name(list,'MCOUNT');
  1552. end;
  1553. system_x86_64_linux:
  1554. begin
  1555. a_call_name(list,'mcount');
  1556. end;
  1557. end;
  1558. end;
  1559. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1560. {$ifdef x86}
  1561. {$ifndef NOTARGETWIN}
  1562. var
  1563. href : treference;
  1564. i : integer;
  1565. again : tasmlabel;
  1566. {$endif NOTARGETWIN}
  1567. {$endif x86}
  1568. begin
  1569. if localsize>0 then
  1570. begin
  1571. {$ifdef i386}
  1572. {$ifndef NOTARGETWIN}
  1573. { windows guards only a few pages for stack growing,
  1574. so we have to access every page first }
  1575. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  1576. (localsize>=winstackpagesize) then
  1577. begin
  1578. if localsize div winstackpagesize<=5 then
  1579. begin
  1580. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1581. for i:=1 to localsize div winstackpagesize do
  1582. begin
  1583. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1584. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1585. end;
  1586. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1587. end
  1588. else
  1589. begin
  1590. current_asmdata.getjumplabel(again);
  1591. getcpuregister(list,NR_EDI);
  1592. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1593. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1594. a_label(list,again);
  1595. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1596. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1597. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1598. a_jmp_cond(list,OC_NE,again);
  1599. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize - 4,NR_ESP));
  1600. reference_reset_base(href,NR_ESP,localsize-4);
  1601. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  1602. ungetcpuregister(list,NR_EDI);
  1603. end
  1604. end
  1605. else
  1606. {$endif NOTARGETWIN}
  1607. {$endif i386}
  1608. {$ifdef x86_64}
  1609. {$ifndef NOTARGETWIN}
  1610. { windows guards only a few pages for stack growing,
  1611. so we have to access every page first }
  1612. if (target_info.system=system_x86_64_win64) and
  1613. (localsize>=winstackpagesize) then
  1614. begin
  1615. if localsize div winstackpagesize<=5 then
  1616. begin
  1617. list.concat(Taicpu.Op_const_reg(A_SUB,S_Q,localsize,NR_RSP));
  1618. for i:=1 to localsize div winstackpagesize do
  1619. begin
  1620. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4);
  1621. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1622. end;
  1623. reference_reset_base(href,NR_RSP,0);
  1624. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1625. end
  1626. else
  1627. begin
  1628. current_asmdata.getjumplabel(again);
  1629. getcpuregister(list,NR_R10);
  1630. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  1631. a_label(list,again);
  1632. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,winstackpagesize,NR_RSP));
  1633. reference_reset_base(href,NR_RSP,0);
  1634. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1635. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10));
  1636. a_jmp_cond(list,OC_NE,again);
  1637. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,localsize mod winstackpagesize-4,NR_RSP));
  1638. ungetcpuregister(list,NR_R10);
  1639. end
  1640. end
  1641. else
  1642. {$endif NOTARGETWIN}
  1643. {$endif x86_64}
  1644. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1645. end;
  1646. end;
  1647. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1648. var
  1649. stackmisalignment: longint;
  1650. begin
  1651. {$ifdef i386}
  1652. { interrupt support for i386 }
  1653. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1654. { this messes up stack alignment }
  1655. (target_info.system <> system_i386_darwin) then
  1656. begin
  1657. { .... also the segment registers }
  1658. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1659. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1660. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1661. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1662. { save the registers of an interrupt procedure }
  1663. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1664. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1665. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1666. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1667. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1668. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1669. end;
  1670. {$endif i386}
  1671. { save old framepointer }
  1672. if not nostackframe then
  1673. begin
  1674. { return address }
  1675. stackmisalignment := sizeof(aint);
  1676. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1677. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1678. CGmessage(cg_d_stackframe_omited)
  1679. else
  1680. begin
  1681. { push <frame_pointer> }
  1682. inc(stackmisalignment,sizeof(aint));
  1683. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1684. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1685. { Return address and FP are both on stack }
  1686. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(aint));
  1687. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(aint)));
  1688. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1689. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1690. end;
  1691. { allocate stackframe space }
  1692. if (localsize<>0) or
  1693. ((target_info.system in [system_i386_darwin,system_x86_64_win64]) and
  1694. (stackmisalignment <> 0) and
  1695. ((pi_do_call in current_procinfo.flags) or
  1696. (po_assembler in current_procinfo.procdef.procoptions))) then
  1697. begin
  1698. if (target_info.system in [system_i386_darwin,system_x86_64_win64]) then
  1699. localsize := align(localsize+stackmisalignment,16)-stackmisalignment;
  1700. cg.g_stackpointer_alloc(list,localsize);
  1701. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1702. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(aint));
  1703. end;
  1704. end;
  1705. end;
  1706. { produces if necessary overflowcode }
  1707. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  1708. var
  1709. hl : tasmlabel;
  1710. ai : taicpu;
  1711. cond : TAsmCond;
  1712. begin
  1713. if not(cs_check_overflow in aktlocalswitches) then
  1714. exit;
  1715. current_asmdata.getjumplabel(hl);
  1716. if not ((def.deftype=pointerdef) or
  1717. ((def.deftype=orddef) and
  1718. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1719. bool8bit,bool16bit,bool32bit,bool64bit]))) then
  1720. cond:=C_NO
  1721. else
  1722. cond:=C_NB;
  1723. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1724. ai.SetCondition(cond);
  1725. ai.is_jmp:=true;
  1726. list.concat(ai);
  1727. a_call_name(list,'FPC_OVERFLOW');
  1728. a_label(list,hl);
  1729. end;
  1730. end.