cgx86.pas 62 KB

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