cgx86.pas 68 KB

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