cgx86.pas 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the common parts of the code generator for the i386 and the x86-64.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  19. }
  20. unit cgx86;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. cgbase,cgutils,cgobj,
  26. aasmbase,aasmtai,aasmcpu,
  27. cpubase,cpuinfo,rgobj,rgx86,rgcpu,
  28. symconst,symtype;
  29. type
  30. tcgx86 = class(tcg)
  31. rgfpu : Trgx86fpu;
  32. procedure done_register_allocators;override;
  33. function getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;override;
  34. function getmmxregister(list:Taasmoutput):Tregister;
  35. procedure getcpuregister(list:Taasmoutput;r:Tregister);override;
  36. procedure ungetcpuregister(list:Taasmoutput;r:Tregister);override;
  37. procedure alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);override;
  38. procedure dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;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 : taasmoutput;const s : string);override;
  44. procedure a_call_reg(list : taasmoutput;reg : tregister);override;
  45. procedure a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  46. procedure a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference); override;
  47. procedure a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  48. procedure a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  49. procedure a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  50. procedure a_op_const_reg_reg(list: taasmoutput; op: TOpCg;
  51. size: tcgsize; a: aint; src, dst: tregister); override;
  52. procedure a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;
  53. size: tcgsize; src1, src2, dst: tregister); override;
  54. { move instructions }
  55. procedure a_load_const_reg(list : taasmoutput; tosize: tcgsize; a : aint;reg : tregister);override;
  56. procedure a_load_const_ref(list : taasmoutput; tosize: tcgsize; a : aint;const ref : treference);override;
  57. procedure a_load_reg_ref(list : taasmoutput;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  58. procedure a_load_ref_reg(list : taasmoutput;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  59. procedure a_load_reg_reg(list : taasmoutput;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  60. procedure a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);override;
  61. { fpu move instructions }
  62. procedure a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister); override;
  63. procedure a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister); override;
  64. procedure a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference); override;
  65. { vector register move instructions }
  66. procedure a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  67. procedure a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  68. procedure a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  69. procedure a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  70. procedure a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  71. { comparison operations }
  72. procedure a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  73. l : tasmlabel);override;
  74. procedure a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  75. l : tasmlabel);override;
  76. procedure a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  77. procedure a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  78. procedure a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  79. procedure a_jmp_name(list : taasmoutput;const s : string);override;
  80. procedure a_jmp_always(list : taasmoutput;l: tasmlabel); override;
  81. procedure a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel); override;
  82. procedure g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister); override;
  83. procedure g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference); override;
  84. procedure g_concatcopy(list : taasmoutput;const source,dest : treference;len : aint);override;
  85. { entry/exit code helpers }
  86. procedure g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);override;
  87. procedure g_profilecode(list : taasmoutput);override;
  88. procedure g_stackpointer_alloc(list : taasmoutput;localsize : longint);override;
  89. procedure g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);override;
  90. procedure g_overflowcheck(list: taasmoutput; const l:tlocation;def:tdef);override;
  91. protected
  92. procedure a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  93. procedure check_register_size(size:tcgsize;reg:tregister);
  94. procedure opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  95. private
  96. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  97. procedure make_simple_ref(list:taasmoutput;var ref: treference);
  98. procedure floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  99. procedure floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  100. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  101. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  102. end;
  103. function use_sse(def : tdef) : boolean;
  104. const
  105. {$ifdef x86_64}
  106. TCGSize2OpSize: Array[tcgsize] of topsize =
  107. (S_NO,S_B,S_W,S_L,S_Q,S_Q,S_B,S_W,S_L,S_Q,S_Q,
  108. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  109. S_NO,S_NO,S_NO,S_MD,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  110. {$else x86_64}
  111. TCGSize2OpSize: Array[tcgsize] of topsize =
  112. (S_NO,S_B,S_W,S_L,S_L,S_L,S_B,S_W,S_L,S_L,S_L,
  113. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  114. S_NO,S_NO,S_NO,S_MD,S_NO,S_NO,S_NO,S_NO,S_NO,S_NO);
  115. {$endif x86_64}
  116. {$ifndef NOTARGETWIN32}
  117. winstackpagesize = 4096;
  118. {$endif NOTARGETWIN32}
  119. implementation
  120. uses
  121. globals,verbose,systems,cutils,
  122. dwarf,
  123. symdef,defutil,paramgr,procinfo;
  124. const
  125. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_ADD,A_AND,A_DIV,
  126. A_IDIV,A_MUL, A_IMUL, 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. function use_sse(def : tdef) : boolean;
  131. begin
  132. use_sse:=(is_single(def) and (aktfputype in sse_singlescalar)) or
  133. (is_double(def) and (aktfputype in sse_doublescalar));
  134. end;
  135. procedure Tcgx86.done_register_allocators;
  136. begin
  137. rg[R_INTREGISTER].free;
  138. rg[R_MMREGISTER].free;
  139. rg[R_MMXREGISTER].free;
  140. rgfpu.free;
  141. inherited done_register_allocators;
  142. end;
  143. function Tcgx86.getfpuregister(list:Taasmoutput;size:Tcgsize):Tregister;
  144. begin
  145. result:=rgfpu.getregisterfpu(list);
  146. end;
  147. function Tcgx86.getmmxregister(list:Taasmoutput):Tregister;
  148. begin
  149. if not assigned(rg[R_MMXREGISTER]) then
  150. internalerror(200312124);
  151. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  152. end;
  153. procedure Tcgx86.getcpuregister(list:Taasmoutput;r:Tregister);
  154. begin
  155. if getregtype(r)=R_FPUREGISTER then
  156. internalerror(2003121210)
  157. else
  158. inherited getcpuregister(list,r);
  159. end;
  160. procedure tcgx86.ungetcpuregister(list:Taasmoutput;r:Tregister);
  161. begin
  162. if getregtype(r)=R_FPUREGISTER then
  163. rgfpu.ungetregisterfpu(list,r)
  164. else
  165. inherited ungetcpuregister(list,r);
  166. end;
  167. procedure Tcgx86.alloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  168. begin
  169. if rt<>R_FPUREGISTER then
  170. inherited alloccpuregisters(list,rt,r);
  171. end;
  172. procedure Tcgx86.dealloccpuregisters(list:Taasmoutput;rt:Tregistertype;r:Tcpuregisterset);
  173. begin
  174. if rt<>R_FPUREGISTER then
  175. inherited dealloccpuregisters(list,rt,r);
  176. end;
  177. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  178. begin
  179. if rt=R_FPUREGISTER then
  180. result:=false
  181. else
  182. result:=inherited uses_registers(rt);
  183. end;
  184. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  185. begin
  186. if getregtype(r)<>R_FPUREGISTER then
  187. inherited add_reg_instruction(instr,r);
  188. end;
  189. procedure tcgx86.dec_fpu_stack;
  190. begin
  191. dec(rgfpu.fpuvaroffset);
  192. end;
  193. procedure tcgx86.inc_fpu_stack;
  194. begin
  195. inc(rgfpu.fpuvaroffset);
  196. end;
  197. {****************************************************************************
  198. This is private property, keep out! :)
  199. ****************************************************************************}
  200. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  201. begin
  202. case s2 of
  203. OS_8,OS_S8 :
  204. if S1 in [OS_8,OS_S8] then
  205. s3 := S_B
  206. else
  207. internalerror(200109221);
  208. OS_16,OS_S16:
  209. case s1 of
  210. OS_8,OS_S8:
  211. s3 := S_BW;
  212. OS_16,OS_S16:
  213. s3 := S_W;
  214. else
  215. internalerror(200109222);
  216. end;
  217. OS_32,OS_S32:
  218. case s1 of
  219. OS_8,OS_S8:
  220. s3 := S_BL;
  221. OS_16,OS_S16:
  222. s3 := S_WL;
  223. OS_32,OS_S32:
  224. s3 := S_L;
  225. else
  226. internalerror(200109223);
  227. end;
  228. {$ifdef x86_64}
  229. OS_64,OS_S64:
  230. case s1 of
  231. OS_8:
  232. s3 := S_BL;
  233. OS_S8:
  234. s3 := S_BQ;
  235. OS_16:
  236. s3 := S_WL;
  237. OS_S16:
  238. s3 := S_WQ;
  239. OS_32:
  240. s3 := S_L;
  241. OS_S32:
  242. s3 := S_LQ;
  243. OS_64,OS_S64:
  244. s3 := S_Q;
  245. else
  246. internalerror(200304302);
  247. end;
  248. {$endif x86_64}
  249. else
  250. internalerror(200109227);
  251. end;
  252. if s3 in [S_B,S_W,S_L,S_Q] then
  253. op := A_MOV
  254. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  255. op := A_MOVZX
  256. else
  257. {$ifdef x86_64}
  258. if s3 in [S_LQ] then
  259. op := A_MOVSXD
  260. else
  261. {$endif x86_64}
  262. op := A_MOVSX;
  263. end;
  264. procedure tcgx86.make_simple_ref(list:taasmoutput;var ref: treference);
  265. {$ifdef x86_64}
  266. var
  267. hreg : tregister;
  268. {$endif x86_64}
  269. begin
  270. {$ifdef x86_64}
  271. { Only 32bit is allowed }
  272. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) then
  273. begin
  274. { Load constant value to register }
  275. hreg:=GetAddressRegister(list);
  276. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  277. ref.offset:=0;
  278. {if assigned(ref.symbol) then
  279. begin
  280. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  281. ref.symbol:=nil;
  282. end;}
  283. { Add register to reference }
  284. if ref.index=NR_NO then
  285. ref.index:=hreg
  286. else
  287. begin
  288. if ref.scalefactor<>0 then
  289. begin
  290. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  291. ref.base:=hreg;
  292. end
  293. else
  294. begin
  295. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  296. ref.index:=hreg;
  297. end;
  298. end;
  299. end;
  300. {$endif x86_64}
  301. end;
  302. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  303. begin
  304. case t of
  305. OS_F32 :
  306. begin
  307. op:=A_FLD;
  308. s:=S_FS;
  309. end;
  310. OS_F64 :
  311. begin
  312. op:=A_FLD;
  313. s:=S_FL;
  314. end;
  315. OS_F80 :
  316. begin
  317. op:=A_FLD;
  318. s:=S_FX;
  319. end;
  320. OS_C64 :
  321. begin
  322. op:=A_FILD;
  323. s:=S_IQ;
  324. end;
  325. else
  326. internalerror(200204041);
  327. end;
  328. end;
  329. procedure tcgx86.floatload(list: taasmoutput; t : tcgsize;const ref : treference);
  330. var
  331. op : tasmop;
  332. s : topsize;
  333. tmpref : treference;
  334. begin
  335. tmpref:=ref;
  336. make_simple_ref(list,tmpref);
  337. floatloadops(t,op,s);
  338. list.concat(Taicpu.Op_ref(op,s,tmpref));
  339. inc_fpu_stack;
  340. end;
  341. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  342. begin
  343. case t of
  344. OS_F32 :
  345. begin
  346. op:=A_FSTP;
  347. s:=S_FS;
  348. end;
  349. OS_F64 :
  350. begin
  351. op:=A_FSTP;
  352. s:=S_FL;
  353. end;
  354. OS_F80 :
  355. begin
  356. op:=A_FSTP;
  357. s:=S_FX;
  358. end;
  359. OS_C64 :
  360. begin
  361. op:=A_FISTP;
  362. s:=S_IQ;
  363. end;
  364. else
  365. internalerror(200204042);
  366. end;
  367. end;
  368. procedure tcgx86.floatstore(list: taasmoutput; t : tcgsize;const ref : treference);
  369. var
  370. op : tasmop;
  371. s : topsize;
  372. tmpref : treference;
  373. begin
  374. tmpref:=ref;
  375. make_simple_ref(list,tmpref);
  376. floatstoreops(t,op,s);
  377. list.concat(Taicpu.Op_ref(op,s,tmpref));
  378. dec_fpu_stack;
  379. end;
  380. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  381. begin
  382. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  383. internalerror(200306031);
  384. end;
  385. {****************************************************************************
  386. Assembler code
  387. ****************************************************************************}
  388. procedure tcgx86.a_jmp_name(list : taasmoutput;const s : string);
  389. begin
  390. list.concat(taicpu.op_sym(A_JMP,S_NO,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  391. end;
  392. procedure tcgx86.a_jmp_always(list : taasmoutput;l: tasmlabel);
  393. begin
  394. a_jmp_cond(list, OC_NONE, l);
  395. end;
  396. procedure tcgx86.a_call_name(list : taasmoutput;const s : string);
  397. begin
  398. list.concat(taicpu.op_sym(A_CALL,S_NO,objectlibrary.newasmsymbol(s,AB_EXTERNAL,AT_FUNCTION)));
  399. end;
  400. procedure tcgx86.a_call_reg(list : taasmoutput;reg : tregister);
  401. begin
  402. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  403. end;
  404. {********************** load instructions ********************}
  405. procedure tcgx86.a_load_const_reg(list : taasmoutput; tosize: TCGSize; a : aint; reg : TRegister);
  406. begin
  407. check_register_size(tosize,reg);
  408. { the optimizer will change it to "xor reg,reg" when loading zero, }
  409. { no need to do it here too (JM) }
  410. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  411. end;
  412. procedure tcgx86.a_load_const_ref(list : taasmoutput; tosize: tcgsize; a : aint;const ref : treference);
  413. var
  414. tmpref : treference;
  415. begin
  416. tmpref:=ref;
  417. make_simple_ref(list,tmpref);
  418. {$ifdef x86_64}
  419. { x86_64 only supports signed 32 bits constants directly }
  420. if (tosize in [OS_S64,OS_64]) and
  421. ((a<low(longint)) or (a>high(longint))) then
  422. begin
  423. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  424. inc(tmpref.offset,4);
  425. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  426. end
  427. else
  428. {$endif x86_64}
  429. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  430. end;
  431. procedure tcgx86.a_load_reg_ref(list : taasmoutput; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  432. var
  433. op: tasmop;
  434. s: topsize;
  435. tmpsize : tcgsize;
  436. tmpreg : tregister;
  437. tmpref : treference;
  438. begin
  439. tmpref:=ref;
  440. make_simple_ref(list,tmpref);
  441. check_register_size(fromsize,reg);
  442. sizes2load(fromsize,tosize,op,s);
  443. case s of
  444. {$ifdef x86_64}
  445. S_BQ,S_WQ,S_LQ,
  446. {$endif x86_64}
  447. S_BW,S_BL,S_WL :
  448. begin
  449. tmpreg:=getintregister(list,tosize);
  450. {$ifdef x86_64}
  451. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  452. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  453. 64 bit (FK) }
  454. if s in [S_BL,S_WL,S_L] then
  455. begin
  456. tmpreg:=makeregsize(list,tmpreg,OS_32);
  457. tmpsize:=OS_32;
  458. end
  459. else
  460. {$endif x86_64}
  461. tmpsize:=tosize;
  462. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  463. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  464. end;
  465. else
  466. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  467. end;
  468. end;
  469. procedure tcgx86.a_load_ref_reg(list : taasmoutput;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  470. var
  471. op: tasmop;
  472. s: topsize;
  473. tmpref : treference;
  474. begin
  475. tmpref:=ref;
  476. make_simple_ref(list,tmpref);
  477. check_register_size(tosize,reg);
  478. sizes2load(fromsize,tosize,op,s);
  479. {$ifdef x86_64}
  480. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  481. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  482. 64 bit (FK) }
  483. if s in [S_BL,S_WL,S_L] then
  484. reg:=makeregsize(list,reg,OS_32);
  485. {$endif x86_64}
  486. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  487. end;
  488. procedure tcgx86.a_load_reg_reg(list : taasmoutput;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  489. var
  490. op: tasmop;
  491. s: topsize;
  492. instr:Taicpu;
  493. begin
  494. check_register_size(fromsize,reg1);
  495. check_register_size(tosize,reg2);
  496. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  497. begin
  498. reg1:=makeregsize(list,reg1,tosize);
  499. s:=tcgsize2opsize[tosize];
  500. op:=A_MOV;
  501. end
  502. else
  503. sizes2load(fromsize,tosize,op,s);
  504. {$ifdef x86_64}
  505. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  506. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  507. 64 bit (FK) }
  508. if s in [S_BL,S_WL,S_L] then
  509. reg2:=makeregsize(list,reg2,OS_32);
  510. {$endif x86_64}
  511. if (reg1<>reg2) then
  512. begin
  513. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  514. { Notify the register allocator that we have written a move instruction so
  515. it can try to eliminate it. }
  516. add_move_instruction(instr);
  517. list.concat(instr);
  518. end;
  519. end;
  520. procedure tcgx86.a_loadaddr_ref_reg(list : taasmoutput;const ref : treference;r : tregister);
  521. var
  522. tmpref : treference;
  523. begin
  524. with ref do
  525. if (base=NR_NO) and (index=NR_NO) then
  526. begin
  527. if assigned(ref.symbol) then
  528. list.concat(Taicpu.op_sym_ofs_reg(A_MOV,tcgsize2opsize[OS_ADDR],symbol,offset,r))
  529. else
  530. a_load_const_reg(list,OS_ADDR,offset,r);
  531. end
  532. else if (base=NR_NO) and (index<>NR_NO) and
  533. (offset=0) and (scalefactor=0) and (symbol=nil) then
  534. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  535. else if (base<>NR_NO) and (index=NR_NO) and
  536. (offset=0) and (symbol=nil) then
  537. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  538. else
  539. begin
  540. tmpref:=ref;
  541. make_simple_ref(list,tmpref);
  542. list.concat(taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  543. end;
  544. end;
  545. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  546. { R_ST means "the current value at the top of the fpu stack" (JM) }
  547. procedure tcgx86.a_loadfpu_reg_reg(list: taasmoutput; size: tcgsize; reg1, reg2: tregister);
  548. begin
  549. if (reg1<>NR_ST) then
  550. begin
  551. list.concat(taicpu.op_reg(A_FLD,S_NO,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  552. inc_fpu_stack;
  553. end;
  554. if (reg2<>NR_ST) then
  555. begin
  556. list.concat(taicpu.op_reg(A_FSTP,S_NO,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  557. dec_fpu_stack;
  558. end;
  559. end;
  560. procedure tcgx86.a_loadfpu_ref_reg(list: taasmoutput; size: tcgsize; const ref: treference; reg: tregister);
  561. begin
  562. floatload(list,size,ref);
  563. if (reg<>NR_ST) then
  564. a_loadfpu_reg_reg(list,size,NR_ST,reg);
  565. end;
  566. procedure tcgx86.a_loadfpu_reg_ref(list: taasmoutput; size: tcgsize; reg: tregister; const ref: treference);
  567. begin
  568. if reg<>NR_ST then
  569. a_loadfpu_reg_reg(list,size,reg,NR_ST);
  570. floatstore(list,size,ref);
  571. end;
  572. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  573. const
  574. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  575. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  576. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  577. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  578. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  579. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  580. begin
  581. result:=convertop[fromsize,tosize];
  582. if result=A_NONE then
  583. internalerror(200312205);
  584. end;
  585. procedure tcgx86.a_loadmm_reg_reg(list: taasmoutput; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  586. var
  587. instr : taicpu;
  588. begin
  589. if shuffle=nil then
  590. begin
  591. if fromsize=tosize then
  592. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2)
  593. else
  594. internalerror(200312202);
  595. end
  596. else if shufflescalar(shuffle) then
  597. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  598. else
  599. internalerror(200312201);
  600. case get_scalar_mm_op(fromsize,tosize) of
  601. A_MOVSS,
  602. A_MOVSD,
  603. A_MOVQ:
  604. add_move_instruction(instr);
  605. end;
  606. list.concat(instr);
  607. end;
  608. procedure tcgx86.a_loadmm_ref_reg(list: taasmoutput; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  609. var
  610. tmpref : treference;
  611. begin
  612. tmpref:=ref;
  613. make_simple_ref(list,tmpref);
  614. if shuffle=nil then
  615. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  616. else if shufflescalar(shuffle) then
  617. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  618. else
  619. internalerror(200312252);
  620. end;
  621. procedure tcgx86.a_loadmm_reg_ref(list: taasmoutput; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  622. var
  623. hreg : tregister;
  624. tmpref : treference;
  625. begin
  626. tmpref:=ref;
  627. make_simple_ref(list,tmpref);
  628. if shuffle=nil then
  629. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  630. else if shufflescalar(shuffle) then
  631. begin
  632. if tosize<>fromsize then
  633. begin
  634. hreg:=getmmregister(list,tosize);
  635. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  636. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  637. end
  638. else
  639. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  640. end
  641. else
  642. internalerror(200312252);
  643. end;
  644. procedure tcgx86.a_opmm_ref_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  645. var
  646. l : tlocation;
  647. begin
  648. l.loc:=LOC_REFERENCE;
  649. l.reference:=ref;
  650. l.size:=size;
  651. opmm_loc_reg(list,op,size,l,reg,shuffle);
  652. end;
  653. procedure tcgx86.a_opmm_reg_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  654. var
  655. l : tlocation;
  656. begin
  657. l.loc:=LOC_MMREGISTER;
  658. l.register:=src;
  659. l.size:=size;
  660. opmm_loc_reg(list,op,size,l,dst,shuffle);
  661. end;
  662. procedure tcgx86.opmm_loc_reg(list: taasmoutput; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  663. const
  664. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  665. ( { scalar }
  666. ( { OS_F32 }
  667. 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
  668. ),
  669. ( { OS_F64 }
  670. 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
  671. )
  672. ),
  673. ( { vectorized/packed }
  674. { because the logical packed single instructions have shorter op codes, we use always
  675. these
  676. }
  677. ( { OS_F32 }
  678. 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
  679. ),
  680. ( { OS_F64 }
  681. 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
  682. )
  683. )
  684. );
  685. var
  686. resultreg : tregister;
  687. asmop : tasmop;
  688. begin
  689. { this is an internally used procedure so the parameters have
  690. some constrains
  691. }
  692. if loc.size<>size then
  693. internalerror(200312213);
  694. resultreg:=dst;
  695. { deshuffle }
  696. //!!!
  697. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  698. begin
  699. end
  700. else if (shuffle=nil) then
  701. asmop:=opmm2asmop[1,size,op]
  702. else if shufflescalar(shuffle) then
  703. begin
  704. asmop:=opmm2asmop[0,size,op];
  705. { no scalar operation available? }
  706. if asmop=A_NOP then
  707. begin
  708. { do vectorized and shuffle finally }
  709. //!!!
  710. end;
  711. end
  712. else
  713. internalerror(200312211);
  714. if asmop=A_NOP then
  715. internalerror(200312215);
  716. case loc.loc of
  717. LOC_CREFERENCE,LOC_REFERENCE:
  718. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  719. LOC_CMMREGISTER,LOC_MMREGISTER:
  720. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  721. else
  722. internalerror(200312214);
  723. end;
  724. { shuffle }
  725. if resultreg<>dst then
  726. begin
  727. internalerror(200312212);
  728. end;
  729. end;
  730. procedure tcgx86.a_op_const_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  731. var
  732. opcode : tasmop;
  733. power : longint;
  734. {$ifdef x86_64}
  735. tmpreg : tregister;
  736. {$endif x86_64}
  737. begin
  738. {$ifdef x86_64}
  739. { x86_64 only supports signed 32 bits constants directly }
  740. if (size in [OS_S64,OS_64]) and
  741. ((a<low(longint)) or (a>high(longint))) then
  742. begin
  743. tmpreg:=getintregister(list,size);
  744. a_load_const_reg(list,size,a,tmpreg);
  745. a_op_reg_reg(list,op,size,tmpreg,reg);
  746. exit;
  747. end;
  748. {$endif x86_64}
  749. check_register_size(size,reg);
  750. case op of
  751. OP_DIV, OP_IDIV:
  752. begin
  753. if ispowerof2(int64(a),power) then
  754. begin
  755. case op of
  756. OP_DIV:
  757. opcode := A_SHR;
  758. OP_IDIV:
  759. opcode := A_SAR;
  760. end;
  761. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  762. exit;
  763. end;
  764. { the rest should be handled specifically in the code }
  765. { generator because of the silly register usage restraints }
  766. internalerror(200109224);
  767. end;
  768. OP_MUL,OP_IMUL:
  769. begin
  770. if not(cs_check_overflow in aktlocalswitches) and
  771. ispowerof2(int64(a),power) then
  772. begin
  773. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  774. exit;
  775. end;
  776. if op = OP_IMUL then
  777. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  778. else
  779. { OP_MUL should be handled specifically in the code }
  780. { generator because of the silly register usage restraints }
  781. internalerror(200109225);
  782. end;
  783. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  784. if not(cs_check_overflow in aktlocalswitches) and
  785. (a = 1) and
  786. (op in [OP_ADD,OP_SUB]) then
  787. if op = OP_ADD then
  788. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  789. else
  790. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  791. else if (a = 0) then
  792. if (op <> OP_AND) then
  793. exit
  794. else
  795. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  796. else if (aword(a) = high(aword)) and
  797. (op in [OP_AND,OP_OR,OP_XOR]) then
  798. begin
  799. case op of
  800. OP_AND:
  801. exit;
  802. OP_OR:
  803. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  804. OP_XOR:
  805. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  806. end
  807. end
  808. else
  809. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  810. OP_SHL,OP_SHR,OP_SAR:
  811. begin
  812. if (a and 31) <> 0 Then
  813. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  814. if (a shr 5) <> 0 Then
  815. internalerror(68991);
  816. end
  817. else internalerror(68992);
  818. end;
  819. end;
  820. procedure tcgx86.a_op_const_ref(list : taasmoutput; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  821. var
  822. opcode: tasmop;
  823. power: longint;
  824. {$ifdef x86_64}
  825. tmpreg : tregister;
  826. {$endif x86_64}
  827. tmpref : treference;
  828. begin
  829. tmpref:=ref;
  830. make_simple_ref(list,tmpref);
  831. {$ifdef x86_64}
  832. { x86_64 only supports signed 32 bits constants directly }
  833. if (size in [OS_S64,OS_64]) and
  834. ((a<low(longint)) or (a>high(longint))) then
  835. begin
  836. tmpreg:=getintregister(list,size);
  837. a_load_const_reg(list,size,a,tmpreg);
  838. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  839. exit;
  840. end;
  841. {$endif x86_64}
  842. Case Op of
  843. OP_DIV, OP_IDIV:
  844. Begin
  845. if ispowerof2(int64(a),power) then
  846. begin
  847. case op of
  848. OP_DIV:
  849. opcode := A_SHR;
  850. OP_IDIV:
  851. opcode := A_SAR;
  852. end;
  853. list.concat(taicpu.op_const_ref(opcode,
  854. TCgSize2OpSize[size],power,tmpref));
  855. exit;
  856. end;
  857. { the rest should be handled specifically in the code }
  858. { generator because of the silly register usage restraints }
  859. internalerror(200109231);
  860. End;
  861. OP_MUL,OP_IMUL:
  862. begin
  863. if not(cs_check_overflow in aktlocalswitches) and
  864. ispowerof2(int64(a),power) then
  865. begin
  866. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  867. power,tmpref));
  868. exit;
  869. end;
  870. { can't multiply a memory location directly with a constant }
  871. if op = OP_IMUL then
  872. inherited a_op_const_ref(list,op,size,a,tmpref)
  873. else
  874. { OP_MUL should be handled specifically in the code }
  875. { generator because of the silly register usage restraints }
  876. internalerror(200109232);
  877. end;
  878. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  879. if not(cs_check_overflow in aktlocalswitches) and
  880. (a = 1) and
  881. (op in [OP_ADD,OP_SUB]) then
  882. if op = OP_ADD then
  883. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  884. else
  885. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  886. else if (a = 0) then
  887. if (op <> OP_AND) then
  888. exit
  889. else
  890. a_load_const_ref(list,size,0,tmpref)
  891. else if (aword(a) = high(aword)) and
  892. (op in [OP_AND,OP_OR,OP_XOR]) then
  893. begin
  894. case op of
  895. OP_AND:
  896. exit;
  897. OP_OR:
  898. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  899. OP_XOR:
  900. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  901. end
  902. end
  903. else
  904. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  905. TCgSize2OpSize[size],a,tmpref));
  906. OP_SHL,OP_SHR,OP_SAR:
  907. begin
  908. if (a and 31) <> 0 then
  909. list.concat(taicpu.op_const_ref(
  910. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  911. if (a shr 5) <> 0 Then
  912. internalerror(68991);
  913. end
  914. else internalerror(68992);
  915. end;
  916. end;
  917. procedure tcgx86.a_op_reg_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  918. var
  919. dstsize: topsize;
  920. instr:Taicpu;
  921. begin
  922. check_register_size(size,src);
  923. check_register_size(size,dst);
  924. dstsize := tcgsize2opsize[size];
  925. case op of
  926. OP_NEG,OP_NOT:
  927. begin
  928. if src<>dst then
  929. a_load_reg_reg(list,size,size,src,dst);
  930. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  931. end;
  932. OP_MUL,OP_DIV,OP_IDIV:
  933. { special stuff, needs separate handling inside code }
  934. { generator }
  935. internalerror(200109233);
  936. OP_SHR,OP_SHL,OP_SAR:
  937. begin
  938. getcpuregister(list,NR_CL);
  939. a_load_reg_reg(list,OS_8,OS_8,makeregsize(list,src,OS_8),NR_CL);
  940. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,src));
  941. ungetcpuregister(list,NR_CL);
  942. end;
  943. else
  944. begin
  945. if reg2opsize(src) <> dstsize then
  946. internalerror(200109226);
  947. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  948. list.concat(instr);
  949. end;
  950. end;
  951. end;
  952. procedure tcgx86.a_op_ref_reg(list : taasmoutput; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  953. var
  954. tmpref : treference;
  955. begin
  956. tmpref:=ref;
  957. make_simple_ref(list,tmpref);
  958. check_register_size(size,reg);
  959. case op of
  960. OP_NEG,OP_NOT,OP_IMUL:
  961. begin
  962. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  963. end;
  964. OP_MUL,OP_DIV,OP_IDIV:
  965. { special stuff, needs separate handling inside code }
  966. { generator }
  967. internalerror(200109239);
  968. else
  969. begin
  970. reg := makeregsize(list,reg,size);
  971. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  972. end;
  973. end;
  974. end;
  975. procedure tcgx86.a_op_reg_ref(list : taasmoutput; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  976. var
  977. tmpref : treference;
  978. begin
  979. tmpref:=ref;
  980. make_simple_ref(list,tmpref);
  981. check_register_size(size,reg);
  982. case op of
  983. OP_NEG,OP_NOT:
  984. begin
  985. if reg<>NR_NO then
  986. internalerror(200109237);
  987. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  988. end;
  989. OP_IMUL:
  990. begin
  991. { this one needs a load/imul/store, which is the default }
  992. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  993. end;
  994. OP_MUL,OP_DIV,OP_IDIV:
  995. { special stuff, needs separate handling inside code }
  996. { generator }
  997. internalerror(200109238);
  998. else
  999. begin
  1000. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1001. end;
  1002. end;
  1003. end;
  1004. procedure tcgx86.a_op_const_reg_reg(list: taasmoutput; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister);
  1005. var
  1006. tmpref: treference;
  1007. power: longint;
  1008. {$ifdef x86_64}
  1009. tmpreg : tregister;
  1010. {$endif x86_64}
  1011. begin
  1012. {$ifdef x86_64}
  1013. { x86_64 only supports signed 32 bits constants directly }
  1014. if (size in [OS_S64,OS_64]) and
  1015. ((a<low(longint)) or (a>high(longint))) then
  1016. begin
  1017. tmpreg:=getintregister(list,size);
  1018. a_load_const_reg(list,size,a,tmpreg);
  1019. a_op_reg_reg_reg(list,op,size,tmpreg,src,dst);
  1020. exit;
  1021. end;
  1022. {$endif x86_64}
  1023. check_register_size(size,src);
  1024. check_register_size(size,dst);
  1025. if tcgsize2size[size]<>tcgsize2size[OS_INT] then
  1026. begin
  1027. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1028. exit;
  1029. end;
  1030. { if we get here, we have to do a 32 bit calculation, guaranteed }
  1031. case op of
  1032. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  1033. OP_SAR:
  1034. { can't do anything special for these }
  1035. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1036. OP_IMUL:
  1037. begin
  1038. if not(cs_check_overflow in aktlocalswitches) and
  1039. ispowerof2(int64(a),power) then
  1040. { can be done with a shift }
  1041. begin
  1042. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1043. exit;
  1044. end;
  1045. list.concat(taicpu.op_const_reg_reg(A_IMUL,tcgsize2opsize[size],a,src,dst));
  1046. end;
  1047. OP_ADD, OP_SUB:
  1048. if (a = 0) then
  1049. a_load_reg_reg(list,size,size,src,dst)
  1050. else
  1051. begin
  1052. reference_reset(tmpref);
  1053. tmpref.base := src;
  1054. tmpref.offset := longint(a);
  1055. if op = OP_SUB then
  1056. tmpref.offset := -tmpref.offset;
  1057. list.concat(taicpu.op_ref_reg(A_LEA,tcgsize2opsize[size],tmpref,dst));
  1058. end
  1059. else internalerror(200112302);
  1060. end;
  1061. end;
  1062. procedure tcgx86.a_op_reg_reg_reg(list: taasmoutput; op: TOpCg;size: tcgsize; src1, src2, dst: tregister);
  1063. var
  1064. tmpref: treference;
  1065. begin
  1066. check_register_size(size,src1);
  1067. check_register_size(size,src2);
  1068. check_register_size(size,dst);
  1069. if tcgsize2size[size]<>tcgsize2size[OS_INT] then
  1070. begin
  1071. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1072. exit;
  1073. end;
  1074. { if we get here, we have to do a 32 bit calculation, guaranteed }
  1075. Case Op of
  1076. OP_DIV, OP_IDIV, OP_MUL, OP_AND, OP_OR, OP_XOR, OP_SHL, OP_SHR,
  1077. OP_SAR,OP_SUB,OP_NOT,OP_NEG:
  1078. { can't do anything special for these }
  1079. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1080. OP_IMUL:
  1081. list.concat(taicpu.op_reg_reg_reg(A_IMUL,tcgsize2opsize[size],src1,src2,dst));
  1082. OP_ADD:
  1083. begin
  1084. reference_reset(tmpref);
  1085. tmpref.base := src1;
  1086. tmpref.index := src2;
  1087. tmpref.scalefactor := 1;
  1088. list.concat(taicpu.op_ref_reg(A_LEA,tcgsize2opsize[size],tmpref,dst));
  1089. end
  1090. else internalerror(200112303);
  1091. end;
  1092. end;
  1093. {*************** compare instructructions ****************}
  1094. procedure tcgx86.a_cmp_const_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1095. l : tasmlabel);
  1096. {$ifdef x86_64}
  1097. var
  1098. tmpreg : tregister;
  1099. {$endif x86_64}
  1100. begin
  1101. {$ifdef x86_64}
  1102. { x86_64 only supports signed 32 bits constants directly }
  1103. if (size in [OS_S64,OS_64]) and
  1104. ((a<low(longint)) or (a>high(longint))) then
  1105. begin
  1106. tmpreg:=getintregister(list,size);
  1107. a_load_const_reg(list,size,a,tmpreg);
  1108. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1109. exit;
  1110. end;
  1111. {$endif x86_64}
  1112. if (a = 0) then
  1113. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1114. else
  1115. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1116. a_jmp_cond(list,cmp_op,l);
  1117. end;
  1118. procedure tcgx86.a_cmp_const_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1119. l : tasmlabel);
  1120. var
  1121. {$ifdef x86_64}
  1122. tmpreg : tregister;
  1123. {$endif x86_64}
  1124. tmpref : treference;
  1125. begin
  1126. tmpref:=ref;
  1127. make_simple_ref(list,tmpref);
  1128. {$ifdef x86_64}
  1129. { x86_64 only supports signed 32 bits constants directly }
  1130. if (size in [OS_S64,OS_64]) and
  1131. ((a<low(longint)) or (a>high(longint))) then
  1132. begin
  1133. tmpreg:=getintregister(list,size);
  1134. a_load_const_reg(list,size,a,tmpreg);
  1135. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1136. exit;
  1137. end;
  1138. {$endif x86_64}
  1139. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1140. a_jmp_cond(list,cmp_op,l);
  1141. end;
  1142. procedure tcgx86.a_cmp_reg_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;
  1143. reg1,reg2 : tregister;l : tasmlabel);
  1144. begin
  1145. check_register_size(size,reg1);
  1146. check_register_size(size,reg2);
  1147. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1148. a_jmp_cond(list,cmp_op,l);
  1149. end;
  1150. procedure tcgx86.a_cmp_ref_reg_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1151. var
  1152. tmpref : treference;
  1153. begin
  1154. tmpref:=ref;
  1155. make_simple_ref(list,tmpref);
  1156. check_register_size(size,reg);
  1157. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1158. a_jmp_cond(list,cmp_op,l);
  1159. end;
  1160. procedure tcgx86.a_cmp_reg_ref_label(list : taasmoutput;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1161. var
  1162. tmpref : treference;
  1163. begin
  1164. tmpref:=ref;
  1165. make_simple_ref(list,tmpref);
  1166. check_register_size(size,reg);
  1167. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1168. a_jmp_cond(list,cmp_op,l);
  1169. end;
  1170. procedure tcgx86.a_jmp_cond(list : taasmoutput;cond : TOpCmp;l: tasmlabel);
  1171. var
  1172. ai : taicpu;
  1173. begin
  1174. if cond=OC_None then
  1175. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1176. else
  1177. begin
  1178. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1179. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1180. end;
  1181. ai.is_jmp:=true;
  1182. list.concat(ai);
  1183. end;
  1184. procedure tcgx86.a_jmp_flags(list : taasmoutput;const f : TResFlags;l: tasmlabel);
  1185. var
  1186. ai : taicpu;
  1187. begin
  1188. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1189. ai.SetCondition(flags_to_cond(f));
  1190. ai.is_jmp := true;
  1191. list.concat(ai);
  1192. end;
  1193. procedure tcgx86.g_flags2reg(list: taasmoutput; size: TCgSize; const f: tresflags; reg: TRegister);
  1194. var
  1195. ai : taicpu;
  1196. hreg : tregister;
  1197. begin
  1198. hreg:=makeregsize(list,reg,OS_8);
  1199. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1200. ai.setcondition(flags_to_cond(f));
  1201. list.concat(ai);
  1202. if (reg<>hreg) then
  1203. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1204. end;
  1205. procedure tcgx86.g_flags2ref(list: taasmoutput; size: TCgSize; const f: tresflags; const ref: TReference);
  1206. var
  1207. ai : taicpu;
  1208. tmpref : treference;
  1209. begin
  1210. tmpref:=ref;
  1211. make_simple_ref(list,tmpref);
  1212. if not(size in [OS_8,OS_S8]) then
  1213. a_load_const_ref(list,size,0,tmpref);
  1214. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1215. ai.setcondition(flags_to_cond(f));
  1216. list.concat(ai);
  1217. end;
  1218. { ************* concatcopy ************ }
  1219. procedure Tcgx86.g_concatcopy(list:Taasmoutput;const source,dest:Treference;len:aint);
  1220. const
  1221. {$ifdef cpu64bit}
  1222. REGCX=NR_RCX;
  1223. REGSI=NR_RSI;
  1224. REGDI=NR_RDI;
  1225. {$else cpu64bit}
  1226. REGCX=NR_ECX;
  1227. REGSI=NR_ESI;
  1228. REGDI=NR_EDI;
  1229. {$endif cpu64bit}
  1230. type copymode=(copy_move,copy_mmx,copy_string);
  1231. var srcref,dstref:Treference;
  1232. r,r0,r1,r2,r3:Tregister;
  1233. helpsize:aint;
  1234. copysize:byte;
  1235. cgsize:Tcgsize;
  1236. cm:copymode;
  1237. begin
  1238. cm:=copy_move;
  1239. helpsize:=12;
  1240. if cs_littlesize in aktglobalswitches then
  1241. helpsize:=8;
  1242. if (cs_mmx in aktlocalswitches) and
  1243. not(pi_uses_fpu in current_procinfo.flags) and
  1244. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1245. cm:=copy_mmx;
  1246. if (len>helpsize) then
  1247. cm:=copy_string;
  1248. if (cs_littlesize in aktglobalswitches) and
  1249. not((len<=16) and (cm=copy_mmx)) then
  1250. cm:=copy_string;
  1251. case cm of
  1252. copy_move:
  1253. begin
  1254. dstref:=dest;
  1255. srcref:=source;
  1256. copysize:=sizeof(aint);
  1257. cgsize:=int_cgsize(copysize);
  1258. while len<>0 do
  1259. begin
  1260. if len<2 then
  1261. begin
  1262. copysize:=1;
  1263. cgsize:=OS_8;
  1264. end
  1265. else if len<4 then
  1266. begin
  1267. copysize:=2;
  1268. cgsize:=OS_16;
  1269. end
  1270. else if len<8 then
  1271. begin
  1272. copysize:=4;
  1273. cgsize:=OS_32;
  1274. end;
  1275. dec(len,copysize);
  1276. r:=getintregister(list,cgsize);
  1277. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1278. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1279. inc(srcref.offset,copysize);
  1280. inc(dstref.offset,copysize);
  1281. end;
  1282. end;
  1283. copy_mmx:
  1284. begin
  1285. dstref:=dest;
  1286. srcref:=source;
  1287. r0:=getmmxregister(list);
  1288. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1289. if len>=16 then
  1290. begin
  1291. inc(srcref.offset,8);
  1292. r1:=getmmxregister(list);
  1293. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1294. end;
  1295. if len>=24 then
  1296. begin
  1297. inc(srcref.offset,8);
  1298. r2:=getmmxregister(list);
  1299. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1300. end;
  1301. if len>=32 then
  1302. begin
  1303. inc(srcref.offset,8);
  1304. r3:=getmmxregister(list);
  1305. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1306. end;
  1307. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1308. if len>=16 then
  1309. begin
  1310. inc(dstref.offset,8);
  1311. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1312. end;
  1313. if len>=24 then
  1314. begin
  1315. inc(dstref.offset,8);
  1316. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1317. end;
  1318. if len>=32 then
  1319. begin
  1320. inc(dstref.offset,8);
  1321. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1322. end;
  1323. end
  1324. else {copy_string, should be a good fallback in case of unhandled}
  1325. begin
  1326. getcpuregister(list,REGDI);
  1327. a_loadaddr_ref_reg(list,dest,REGDI);
  1328. getcpuregister(list,REGSI);
  1329. a_loadaddr_ref_reg(list,source,REGSI);
  1330. getcpuregister(list,REGCX);
  1331. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1332. if cs_littlesize in aktglobalswitches then
  1333. begin
  1334. a_load_const_reg(list,OS_INT,len,REGCX);
  1335. list.concat(Taicpu.op_none(A_REP,S_NO));
  1336. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1337. end
  1338. else
  1339. begin
  1340. helpsize:=len div sizeof(aint);
  1341. len:=len mod sizeof(aint);
  1342. if helpsize>1 then
  1343. begin
  1344. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1345. list.concat(Taicpu.op_none(A_REP,S_NO));
  1346. end;
  1347. if helpsize>0 then
  1348. begin
  1349. {$ifdef cpu64bit}
  1350. if sizeof(aint)=8 then
  1351. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1352. else
  1353. {$endif cpu64bit}
  1354. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1355. end;
  1356. if len>=4 then
  1357. begin
  1358. dec(len,4);
  1359. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1360. end;
  1361. if len>=2 then
  1362. begin
  1363. dec(len,2);
  1364. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1365. end;
  1366. if len=1 then
  1367. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1368. end;
  1369. ungetcpuregister(list,REGCX);
  1370. ungetcpuregister(list,REGSI);
  1371. ungetcpuregister(list,REGDI);
  1372. end;
  1373. end;
  1374. end;
  1375. {****************************************************************************
  1376. Entry/Exit Code Helpers
  1377. ****************************************************************************}
  1378. procedure tcgx86.g_releasevaluepara_openarray(list : taasmoutput;const l:tlocation);
  1379. begin
  1380. { Nothing to release }
  1381. end;
  1382. procedure tcgx86.g_profilecode(list : taasmoutput);
  1383. var
  1384. pl : tasmlabel;
  1385. mcountprefix : String[4];
  1386. begin
  1387. case target_info.system of
  1388. {$ifndef NOTARGETWIN32}
  1389. system_i386_win32,
  1390. {$endif}
  1391. system_i386_freebsd,
  1392. system_i386_netbsd,
  1393. // system_i386_openbsd,
  1394. system_i386_wdosx :
  1395. begin
  1396. Case target_info.system Of
  1397. system_i386_freebsd : mcountprefix:='.';
  1398. system_i386_netbsd : mcountprefix:='__';
  1399. // system_i386_openbsd : mcountprefix:='.';
  1400. else
  1401. mcountPrefix:='';
  1402. end;
  1403. objectlibrary.getaddrlabel(pl);
  1404. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(aint));
  1405. list.concat(Tai_label.Create(pl));
  1406. list.concat(Tai_const.Create_32bit(0));
  1407. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1408. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1409. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1410. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount');
  1411. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1412. end;
  1413. system_i386_linux:
  1414. a_call_name(list,target_info.Cprefix+'mcount');
  1415. system_i386_go32v2,system_i386_watcom:
  1416. begin
  1417. a_call_name(list,'MCOUNT');
  1418. end;
  1419. system_x86_64_linux:
  1420. begin
  1421. a_call_name(list,'mcount');
  1422. end;
  1423. end;
  1424. end;
  1425. procedure tcgx86.g_stackpointer_alloc(list : taasmoutput;localsize : longint);
  1426. {$ifdef i386}
  1427. {$ifndef NOTARGETWIN32}
  1428. var
  1429. href : treference;
  1430. i : integer;
  1431. again : tasmlabel;
  1432. {$endif NOTARGETWIN32}
  1433. {$endif i386}
  1434. begin
  1435. if localsize>0 then
  1436. begin
  1437. {$ifdef i386}
  1438. {$ifndef NOTARGETWIN32}
  1439. { windows guards only a few pages for stack growing, }
  1440. { so we have to access every page first }
  1441. if (target_info.system=system_i386_win32) and
  1442. (localsize>=winstackpagesize) then
  1443. begin
  1444. if localsize div winstackpagesize<=5 then
  1445. begin
  1446. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1447. for i:=1 to localsize div winstackpagesize do
  1448. begin
  1449. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1450. list.concat(Taicpu.op_const_ref(A_MOV,S_L,0,href));
  1451. end;
  1452. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1453. end
  1454. else
  1455. begin
  1456. objectlibrary.getlabel(again);
  1457. getcpuregister(list,NR_EDI);
  1458. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1459. a_label(list,again);
  1460. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1461. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1462. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1463. a_jmp_cond(list,OC_NE,again);
  1464. ungetcpuregister(list,NR_EDI);
  1465. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize,NR_ESP));
  1466. end
  1467. end
  1468. else
  1469. {$endif NOTARGETWIN32}
  1470. {$endif i386}
  1471. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1472. end;
  1473. end;
  1474. procedure tcgx86.g_proc_entry(list : taasmoutput;localsize : longint;nostackframe:boolean);
  1475. begin
  1476. {$ifdef i386}
  1477. { interrupt support for i386 }
  1478. if (po_interrupt in current_procinfo.procdef.procoptions) then
  1479. begin
  1480. { .... also the segment registers }
  1481. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1482. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1483. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1484. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1485. { save the registers of an interrupt procedure }
  1486. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1487. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1488. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1489. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1490. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1491. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1492. end;
  1493. {$endif i386}
  1494. { save old framepointer }
  1495. if not nostackframe then
  1496. begin
  1497. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  1498. CGmessage(cg_d_stackframe_omited)
  1499. else
  1500. begin
  1501. list.concat(tai_regalloc.alloc(NR_FRAME_POINTER_REG,nil));
  1502. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1503. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1504. { Return address and FP are both on stack }
  1505. dwarfcfi.cfa_def_cfa_offset(list,2*sizeof(aint));
  1506. dwarfcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(aint)));
  1507. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1508. dwarfcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1509. end;
  1510. { allocate stackframe space }
  1511. if localsize<>0 then
  1512. begin
  1513. cg.g_stackpointer_alloc(list,localsize);
  1514. end;
  1515. end;
  1516. { allocate PIC register }
  1517. if cs_create_pic in aktmoduleswitches then
  1518. begin
  1519. a_call_name(list,'FPC_GETEIPINEBX');
  1520. list.concat(taicpu.op_sym_ofs_reg(A_ADD,tcgsize2opsize[OS_ADDR],objectlibrary.newasmsymbol('_GLOBAL_OFFSET_TABLE_',AB_EXTERNAL,AT_DATA),0,NR_PIC_OFFSET_REG));
  1521. list.concat(tai_regalloc.alloc(NR_PIC_OFFSET_REG,nil));
  1522. end;
  1523. end;
  1524. { produces if necessary overflowcode }
  1525. procedure tcgx86.g_overflowcheck(list: taasmoutput; const l:tlocation;def:tdef);
  1526. var
  1527. hl : tasmlabel;
  1528. ai : taicpu;
  1529. cond : TAsmCond;
  1530. begin
  1531. if not(cs_check_overflow in aktlocalswitches) then
  1532. exit;
  1533. objectlibrary.getlabel(hl);
  1534. if not ((def.deftype=pointerdef) or
  1535. ((def.deftype=orddef) and
  1536. (torddef(def).typ in [u64bit,u16bit,u32bit,u8bit,uchar,
  1537. bool8bit,bool16bit,bool32bit]))) then
  1538. cond:=C_NO
  1539. else
  1540. cond:=C_NB;
  1541. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1542. ai.SetCondition(cond);
  1543. ai.is_jmp:=true;
  1544. list.concat(ai);
  1545. a_call_name(list,'FPC_OVERFLOW');
  1546. a_label(list,hl);
  1547. end;
  1548. end.
  1549. {
  1550. $Log$
  1551. Revision 1.139 2004-11-08 20:23:29 florian
  1552. * fixed open arrays when using register variables
  1553. Revision 1.138 2004/11/02 20:50:54 florian
  1554. + added profiler call for x86_64
  1555. Revision 1.137 2004/11/02 18:23:16 florian
  1556. * fixed -<sse register>
  1557. * information about simple moves for sse is given to the register allocator
  1558. Revision 1.136 2004/11/01 23:30:11 peter
  1559. * support > 32bit accesses for x86_64
  1560. * rewrote array size checking to support 64bit
  1561. Revision 1.135 2004/11/01 15:42:47 florian
  1562. * cvt*2* can't write to memory location, fixed
  1563. Revision 1.134 2004/11/01 10:30:06 peter
  1564. * fixed uninited var in a_load_reg_ref
  1565. Revision 1.133 2004/10/31 21:45:04 peter
  1566. * generic tlocation
  1567. * move tlocation to cgutils
  1568. Revision 1.132 2004/10/25 15:36:47 peter
  1569. * save standard registers moved to tcgobj
  1570. Revision 1.131 2004/10/24 20:10:08 peter
  1571. * -Or fixes
  1572. Revision 1.130 2004/10/24 11:44:28 peter
  1573. * small regvar fixes
  1574. * loadref parameter removed from concatcopy,incrrefcount,etc
  1575. Revision 1.129 2004/10/06 19:27:35 jonas
  1576. * regvar fixes from Peter
  1577. Revision 1.128 2004/10/05 20:41:02 peter
  1578. * more spilling rewrites
  1579. Revision 1.127 2004/10/04 20:46:22 peter
  1580. * spilling code rewritten for x86. It now used the generic
  1581. spilling routines. Special x86 optimization still needs
  1582. to be added.
  1583. * Spilling fixed when both operands needed to be spilled
  1584. * Cleanup of spilling routine, do_spill_readwritten removed
  1585. Revision 1.126 2004/10/03 12:42:22 florian
  1586. * made sqrt, sqr and abs internal for the sparc
  1587. Revision 1.125 2004/09/25 14:23:55 peter
  1588. * ungetregister is now only used for cpuregisters, renamed to
  1589. ungetcpuregister
  1590. * renamed (get|unget)explicitregister(s) to ..cpuregister
  1591. * removed location-release/reference_release
  1592. Revision 1.124 2004/06/20 08:55:32 florian
  1593. * logs truncated
  1594. Revision 1.123 2004/06/16 20:07:11 florian
  1595. * dwarf branch merged
  1596. Revision 1.122 2004/05/22 23:34:28 peter
  1597. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  1598. Revision 1.121 2004/04/28 15:19:03 florian
  1599. + syscall directive support for MorphOS added
  1600. Revision 1.120 2004/04/09 14:36:05 peter
  1601. * A_MOVSL renamed to A_MOVSD
  1602. Revision 1.119.2.22 2004/05/28 20:29:50 florian
  1603. * fixed currency trouble on x86-64
  1604. }