cgx86.pas 68 KB

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