cgx86.pas 67 KB

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