cgx86.pas 65 KB

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