nx86add.pas 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Common code generation for add nodes on the i386 and x86
  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. unit nx86add;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cgbase,
  22. cpubase,
  23. node,nadd,ncgadd;
  24. type
  25. tx86addnode = class(tcgaddnode)
  26. protected
  27. function getresflags(unsigned : boolean) : tresflags;
  28. procedure left_must_be_reg(opsize:TCGSize;noswap:boolean);
  29. procedure check_left_and_right_fpureg(force_fpureg: boolean);
  30. procedure emit_op_right_left(op:TAsmOp;opsize:TCgSize);
  31. procedure emit_generic_code(op:TAsmOp;opsize:TCgSize;unsigned,extra_not,mboverflow:boolean);
  32. procedure second_cmpfloatsse;
  33. procedure second_addfloatsse;
  34. procedure second_mul;virtual;abstract;
  35. public
  36. procedure second_addfloat;override;
  37. procedure second_addsmallset;override;
  38. procedure second_add64bit;override;
  39. procedure second_addordinal;override;
  40. procedure second_cmpfloat;override;
  41. procedure second_cmpsmallset;override;
  42. procedure second_cmp64bit;override;
  43. procedure second_cmpordinal;override;
  44. {$ifdef SUPPORT_MMX}
  45. procedure second_opmmxset;override;
  46. procedure second_opmmx;override;
  47. {$endif SUPPORT_MMX}
  48. procedure second_opvector;override;
  49. end;
  50. implementation
  51. uses
  52. globtype,globals,
  53. verbose,cutils,
  54. cpuinfo,
  55. aasmbase,aasmtai,aasmdata,aasmcpu,
  56. symconst,symdef,
  57. cgobj,cgx86,cga,cgutils,
  58. paramgr,tgobj,ncgutil,
  59. ncon,nset,
  60. defutil;
  61. {*****************************************************************************
  62. Helpers
  63. *****************************************************************************}
  64. procedure tx86addnode.emit_generic_code(op:TAsmOp;opsize:TCGSize;unsigned,extra_not,mboverflow:boolean);
  65. var
  66. power : longint;
  67. hl4 : tasmlabel;
  68. r : Tregister;
  69. begin
  70. { at this point, left.location.loc should be LOC_REGISTER }
  71. if right.location.loc=LOC_REGISTER then
  72. begin
  73. { right.location is a LOC_REGISTER }
  74. { when swapped another result register }
  75. if (nodetype=subn) and (nf_swapped in flags) then
  76. begin
  77. if extra_not then
  78. emit_reg(A_NOT,TCGSize2Opsize[opsize],left.location.register);
  79. emit_reg_reg(op,TCGSize2Opsize[opsize],left.location.register,right.location.register);
  80. { newly swapped also set swapped flag }
  81. location_swap(left.location,right.location);
  82. toggleflag(nf_swapped);
  83. end
  84. else
  85. begin
  86. if extra_not then
  87. emit_reg(A_NOT,TCGSize2Opsize[opsize],right.location.register);
  88. if (op=A_ADD) or (op=A_OR) or (op=A_AND) or (op=A_XOR) or (op=A_IMUL) then
  89. location_swap(left.location,right.location);
  90. emit_reg_reg(op,TCGSize2Opsize[opsize],right.location.register,left.location.register);
  91. end;
  92. end
  93. else
  94. begin
  95. { right.location is not a LOC_REGISTER }
  96. if (nodetype=subn) and (nf_swapped in flags) then
  97. begin
  98. if extra_not then
  99. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,opsize,left.location.register,left.location.register);
  100. r:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  101. cg.a_load_loc_reg(current_asmdata.CurrAsmList,opsize,right.location,r);
  102. emit_reg_reg(op,TCGSize2Opsize[opsize],left.location.register,r);
  103. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,r,left.location.register);
  104. end
  105. else
  106. begin
  107. { Optimizations when right.location is a constant value }
  108. if (op=A_CMP) and
  109. (nodetype in [equaln,unequaln]) and
  110. (right.location.loc=LOC_CONSTANT) and
  111. (right.location.value=0) then
  112. begin
  113. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  114. end
  115. else
  116. if (op=A_ADD) and
  117. (right.location.loc=LOC_CONSTANT) and
  118. (right.location.value=1) and
  119. not(cs_check_overflow in current_settings.localswitches) then
  120. begin
  121. emit_reg(A_INC,TCGSize2Opsize[opsize],left.location.register);
  122. end
  123. else
  124. if (op=A_SUB) and
  125. (right.location.loc=LOC_CONSTANT) and
  126. (right.location.value=1) and
  127. not(cs_check_overflow in current_settings.localswitches) then
  128. begin
  129. emit_reg(A_DEC,TCGSize2Opsize[opsize],left.location.register);
  130. end
  131. else
  132. if (op=A_IMUL) and
  133. (right.location.loc=LOC_CONSTANT) and
  134. (ispowerof2(int64(right.location.value),power)) and
  135. not(cs_check_overflow in current_settings.localswitches) then
  136. begin
  137. emit_const_reg(A_SHL,TCGSize2Opsize[opsize],power,left.location.register);
  138. end
  139. else
  140. begin
  141. if extra_not then
  142. begin
  143. r:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  144. cg.a_load_loc_reg(current_asmdata.CurrAsmList,opsize,right.location,r);
  145. emit_reg(A_NOT,TCGSize2Opsize[opsize],r);
  146. emit_reg_reg(A_AND,TCGSize2Opsize[opsize],r,left.location.register);
  147. end
  148. else
  149. begin
  150. emit_op_right_left(op,opsize);
  151. end;
  152. end;
  153. end;
  154. end;
  155. { only in case of overflow operations }
  156. { produce overflow code }
  157. { we must put it here directly, because sign of operation }
  158. { is in unsigned VAR!! }
  159. if mboverflow then
  160. begin
  161. if cs_check_overflow in current_settings.localswitches then
  162. begin
  163. current_asmdata.getjumplabel(hl4);
  164. if unsigned then
  165. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  166. else
  167. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  168. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW');
  169. cg.a_label(current_asmdata.CurrAsmList,hl4);
  170. end;
  171. end;
  172. end;
  173. procedure tx86addnode.left_must_be_reg(opsize:TCGSize;noswap:boolean);
  174. begin
  175. { left location is not a register? }
  176. if (left.location.loc<>LOC_REGISTER) then
  177. begin
  178. { if right is register then we can swap the locations }
  179. if (not noswap) and
  180. (right.location.loc=LOC_REGISTER) then
  181. begin
  182. location_swap(left.location,right.location);
  183. toggleflag(nf_swapped);
  184. end
  185. else
  186. begin
  187. { maybe we can reuse a constant register when the
  188. operation is a comparison that doesn't change the
  189. value of the register }
  190. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  191. end;
  192. end;
  193. end;
  194. procedure tx86addnode.check_left_and_right_fpureg(force_fpureg: boolean);
  195. begin
  196. if (right.location.loc<>LOC_FPUREGISTER) then
  197. begin
  198. if (force_fpureg) then
  199. begin
  200. location_force_fpureg(current_asmdata.CurrAsmList,right.location,false);
  201. if (left.location.loc<>LOC_FPUREGISTER) then
  202. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false)
  203. else
  204. { left was on the stack => swap }
  205. toggleflag(nf_swapped);
  206. end
  207. end
  208. { the nominator in st0 }
  209. else if (left.location.loc<>LOC_FPUREGISTER) then
  210. begin
  211. if (force_fpureg) then
  212. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false)
  213. end
  214. else
  215. begin
  216. { fpu operands are always in the wrong order on the stack }
  217. toggleflag(nf_swapped);
  218. end;
  219. end;
  220. procedure tx86addnode.emit_op_right_left(op:TAsmOp;opsize:TCgsize);
  221. {$ifdef x86_64}
  222. var
  223. tmpreg : tregister;
  224. {$endif x86_64}
  225. begin
  226. if (right.location.loc in [LOC_CSUBSETREG,LOC_SUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  227. location_force_reg(current_asmdata.CurrAsmList,right.location,def_cgsize(right.resultdef),true);
  228. { left must be a register }
  229. case right.location.loc of
  230. LOC_REGISTER,
  231. LOC_CREGISTER :
  232. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,TCGSize2Opsize[opsize],right.location.register,left.location.register));
  233. LOC_REFERENCE,
  234. LOC_CREFERENCE :
  235. begin
  236. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  237. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,TCGSize2Opsize[opsize],right.location.reference,left.location.register));
  238. end;
  239. LOC_CONSTANT :
  240. begin
  241. {$ifdef x86_64}
  242. { x86_64 only supports signed 32 bits constants directly }
  243. if (opsize in [OS_S64,OS_64]) and
  244. ((right.location.value<low(longint)) or (right.location.value>high(longint))) then
  245. begin
  246. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  247. cg.a_load_const_reg(current_asmdata.CurrAsmList,opsize,right.location.value,tmpreg);
  248. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,TCGSize2Opsize[opsize],tmpreg,left.location.register));
  249. end
  250. else
  251. {$endif x86_64}
  252. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(op,TCGSize2Opsize[opsize],right.location.value,left.location.register));
  253. end;
  254. else
  255. internalerror(200203232);
  256. end;
  257. end;
  258. function tx86addnode.getresflags(unsigned : boolean) : tresflags;
  259. begin
  260. case nodetype of
  261. equaln : getresflags:=F_E;
  262. unequaln : getresflags:=F_NE;
  263. else
  264. if not(unsigned) then
  265. begin
  266. if nf_swapped in flags then
  267. case nodetype of
  268. ltn : getresflags:=F_G;
  269. lten : getresflags:=F_GE;
  270. gtn : getresflags:=F_L;
  271. gten : getresflags:=F_LE;
  272. end
  273. else
  274. case nodetype of
  275. ltn : getresflags:=F_L;
  276. lten : getresflags:=F_LE;
  277. gtn : getresflags:=F_G;
  278. gten : getresflags:=F_GE;
  279. end;
  280. end
  281. else
  282. begin
  283. if nf_swapped in flags then
  284. case nodetype of
  285. ltn : getresflags:=F_A;
  286. lten : getresflags:=F_AE;
  287. gtn : getresflags:=F_B;
  288. gten : getresflags:=F_BE;
  289. end
  290. else
  291. case nodetype of
  292. ltn : getresflags:=F_B;
  293. lten : getresflags:=F_BE;
  294. gtn : getresflags:=F_A;
  295. gten : getresflags:=F_AE;
  296. end;
  297. end;
  298. end;
  299. end;
  300. {*****************************************************************************
  301. AddSmallSet
  302. *****************************************************************************}
  303. procedure tx86addnode.second_addsmallset;
  304. var
  305. opsize : TCGSize;
  306. op : TAsmOp;
  307. extra_not,
  308. noswap : boolean;
  309. begin
  310. pass_left_right;
  311. noswap:=false;
  312. extra_not:=false;
  313. opsize:=int_cgsize(resultdef.size);
  314. case nodetype of
  315. addn :
  316. begin
  317. { adding elements is not commutative }
  318. if (nf_swapped in flags) and (left.nodetype=setelementn) then
  319. swapleftright;
  320. { are we adding set elements ? }
  321. if right.nodetype=setelementn then
  322. begin
  323. { no range support for smallsets! }
  324. if assigned(tsetelementnode(right).right) then
  325. internalerror(43244);
  326. { btsb isn't supported }
  327. if opsize=OS_8 then
  328. opsize:=OS_32;
  329. { bts requires both elements to be registers }
  330. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  331. location_force_reg(current_asmdata.CurrAsmList,right.location,opsize,true);
  332. op:=A_BTS;
  333. noswap:=true;
  334. end
  335. else
  336. op:=A_OR;
  337. end;
  338. symdifn :
  339. op:=A_XOR;
  340. muln :
  341. op:=A_AND;
  342. subn :
  343. begin
  344. op:=A_AND;
  345. if (not(nf_swapped in flags)) and
  346. (right.location.loc=LOC_CONSTANT) then
  347. right.location.value := not(right.location.value)
  348. else if (nf_swapped in flags) and
  349. (left.location.loc=LOC_CONSTANT) then
  350. left.location.value := not(left.location.value)
  351. else
  352. extra_not:=true;
  353. end;
  354. xorn :
  355. op:=A_XOR;
  356. orn :
  357. op:=A_OR;
  358. andn :
  359. op:=A_AND;
  360. else
  361. internalerror(2003042215);
  362. end;
  363. { left must be a register }
  364. left_must_be_reg(opsize,noswap);
  365. emit_generic_code(op,opsize,true,extra_not,false);
  366. location_freetemp(current_asmdata.CurrAsmList,right.location);
  367. { left is always a register and contains the result }
  368. location:=left.location;
  369. { fix the changed opsize we did above because of the missing btsb }
  370. if opsize<>int_cgsize(resultdef.size) then
  371. location_force_reg(current_asmdata.CurrAsmList,location,int_cgsize(resultdef.size),false);
  372. end;
  373. procedure tx86addnode.second_cmpsmallset;
  374. var
  375. opsize : TCGSize;
  376. op : TAsmOp;
  377. begin
  378. pass_left_right;
  379. opsize:=int_cgsize(left.resultdef.size);
  380. case nodetype of
  381. equaln,
  382. unequaln :
  383. op:=A_CMP;
  384. lten,gten:
  385. begin
  386. if (not(nf_swapped in flags) and (nodetype = lten)) or
  387. ((nf_swapped in flags) and (nodetype = gten)) then
  388. swapleftright;
  389. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  390. emit_op_right_left(A_AND,opsize);
  391. op:=A_CMP;
  392. { warning: ugly hack, we need a JE so change the node to equaln }
  393. nodetype:=equaln;
  394. end;
  395. else
  396. internalerror(2003042215);
  397. end;
  398. { left must be a register }
  399. left_must_be_reg(opsize,false);
  400. emit_generic_code(op,opsize,true,false,false);
  401. location_freetemp(current_asmdata.CurrAsmList,right.location);
  402. location_freetemp(current_asmdata.CurrAsmList,left.location);
  403. location_reset(location,LOC_FLAGS,OS_NO);
  404. location.resflags:=getresflags(true);
  405. end;
  406. {*****************************************************************************
  407. AddMMX
  408. *****************************************************************************}
  409. {$ifdef SUPPORT_MMX}
  410. procedure tx86addnode.second_opmmx;
  411. var
  412. op : TAsmOp;
  413. cmpop : boolean;
  414. mmxbase : tmmxtype;
  415. hreg,
  416. hregister : tregister;
  417. begin
  418. pass_left_right;
  419. cmpop:=false;
  420. mmxbase:=mmx_type(left.resultdef);
  421. location_reset(location,LOC_MMXREGISTER,def_cgsize(resultdef));
  422. case nodetype of
  423. addn :
  424. begin
  425. if (cs_mmx_saturation in current_settings.localswitches) then
  426. begin
  427. case mmxbase of
  428. mmxs8bit:
  429. op:=A_PADDSB;
  430. mmxu8bit:
  431. op:=A_PADDUSB;
  432. mmxs16bit,mmxfixed16:
  433. op:=A_PADDSW;
  434. mmxu16bit:
  435. op:=A_PADDUSW;
  436. end;
  437. end
  438. else
  439. begin
  440. case mmxbase of
  441. mmxs8bit,mmxu8bit:
  442. op:=A_PADDB;
  443. mmxs16bit,mmxu16bit,mmxfixed16:
  444. op:=A_PADDW;
  445. mmxs32bit,mmxu32bit:
  446. op:=A_PADDD;
  447. end;
  448. end;
  449. end;
  450. muln :
  451. begin
  452. case mmxbase of
  453. mmxs16bit,mmxu16bit:
  454. op:=A_PMULLW;
  455. mmxfixed16:
  456. op:=A_PMULHW;
  457. end;
  458. end;
  459. subn :
  460. begin
  461. if (cs_mmx_saturation in current_settings.localswitches) then
  462. begin
  463. case mmxbase of
  464. mmxs8bit:
  465. op:=A_PSUBSB;
  466. mmxu8bit:
  467. op:=A_PSUBUSB;
  468. mmxs16bit,mmxfixed16:
  469. op:=A_PSUBSB;
  470. mmxu16bit:
  471. op:=A_PSUBUSW;
  472. end;
  473. end
  474. else
  475. begin
  476. case mmxbase of
  477. mmxs8bit,mmxu8bit:
  478. op:=A_PSUBB;
  479. mmxs16bit,mmxu16bit,mmxfixed16:
  480. op:=A_PSUBW;
  481. mmxs32bit,mmxu32bit:
  482. op:=A_PSUBD;
  483. end;
  484. end;
  485. end;
  486. xorn:
  487. op:=A_PXOR;
  488. orn:
  489. op:=A_POR;
  490. andn:
  491. op:=A_PAND;
  492. else
  493. internalerror(2003042214);
  494. end;
  495. { left and right no register? }
  496. { then one must be demanded }
  497. if (left.location.loc<>LOC_MMXREGISTER) then
  498. begin
  499. if (right.location.loc=LOC_MMXREGISTER) then
  500. begin
  501. location_swap(left.location,right.location);
  502. toggleflag(nf_swapped);
  503. end
  504. else
  505. begin
  506. { register variable ? }
  507. if (left.location.loc=LOC_CMMXREGISTER) then
  508. begin
  509. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  510. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  511. end
  512. else
  513. begin
  514. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  515. internalerror(200203245);
  516. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  517. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  518. end;
  519. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  520. left.location.register:=hregister;
  521. end;
  522. end;
  523. { at this point, left.location.loc should be LOC_MMXREGISTER }
  524. if right.location.loc<>LOC_MMXREGISTER then
  525. begin
  526. if (nodetype=subn) and (nf_swapped in flags) then
  527. begin
  528. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  529. if right.location.loc=LOC_CMMXREGISTER then
  530. begin
  531. emit_reg_reg(A_MOVQ,S_NO,right.location.register,hreg);
  532. emit_reg_reg(op,S_NO,left.location.register,hreg);
  533. end
  534. else
  535. begin
  536. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  537. internalerror(200203247);
  538. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,hreg);
  539. emit_reg_reg(op,S_NO,left.location.register,hreg);
  540. end;
  541. location.register:=hreg;
  542. end
  543. else
  544. begin
  545. if (right.location.loc=LOC_CMMXREGISTER) then
  546. emit_reg_reg(op,S_NO,right.location.register,left.location.register)
  547. else
  548. begin
  549. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  550. internalerror(200203246);
  551. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  552. end;
  553. location.register:=left.location.register;
  554. end;
  555. end
  556. else
  557. begin
  558. { right.location=LOC_MMXREGISTER }
  559. if (nodetype=subn) and (nf_swapped in flags) then
  560. begin
  561. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  562. location_swap(left.location,right.location);
  563. toggleflag(nf_swapped);
  564. end
  565. else
  566. begin
  567. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  568. end;
  569. location.register:=left.location.register;
  570. end;
  571. location_freetemp(current_asmdata.CurrAsmList,right.location);
  572. if cmpop then
  573. location_freetemp(current_asmdata.CurrAsmList,left.location);
  574. end;
  575. {$endif SUPPORT_MMX}
  576. {*****************************************************************************
  577. addmmxset
  578. *****************************************************************************}
  579. {$ifdef SUPPORT_MMX}
  580. procedure tx86addnode.second_opmmxset;
  581. var opsize : TCGSize;
  582. op : TAsmOp;
  583. cmpop,
  584. noswap : boolean;
  585. begin
  586. pass_left_right;
  587. cmpop:=false;
  588. noswap:=false;
  589. opsize:=OS_32;
  590. case nodetype of
  591. addn:
  592. begin
  593. { are we adding set elements ? }
  594. if right.nodetype=setelementn then
  595. begin
  596. { adding elements is not commutative }
  597. { if nf_swapped in flags then
  598. swapleftright;}
  599. { bts requires both elements to be registers }
  600. { location_force_reg(current_asmdata.CurrAsmList,left.location,opsize_2_cgsize[opsize],false);
  601. location_force_reg(current_asmdata.CurrAsmList,right.location,opsize_2_cgsize[opsize],true);
  602. op:=A_BTS;
  603. noswap:=true;}
  604. end
  605. else
  606. op:=A_POR;
  607. end;
  608. symdifn :
  609. op:=A_PXOR;
  610. muln:
  611. op:=A_PAND;
  612. subn:
  613. op:=A_PANDN;
  614. equaln,
  615. unequaln :
  616. begin
  617. op:=A_PCMPEQD;
  618. cmpop:=true;
  619. end;
  620. lten,gten:
  621. begin
  622. if (not(nf_swapped in flags) and (nodetype = lten)) or
  623. ((nf_swapped in flags) and (nodetype = gten)) then
  624. swapleftright;
  625. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  626. emit_op_right_left(A_AND,opsize);
  627. op:=A_PCMPEQD;
  628. cmpop:=true;
  629. { warning: ugly hack, we need a JE so change the node to equaln }
  630. nodetype:=equaln;
  631. end;
  632. xorn :
  633. op:=A_PXOR;
  634. orn :
  635. op:=A_POR;
  636. andn :
  637. op:=A_PAND;
  638. else
  639. internalerror(2003042215);
  640. end;
  641. { left must be a register }
  642. left_must_be_reg(opsize,noswap);
  643. { emit_generic_code(op,opsize,true,extra_not,false);}
  644. location_freetemp(current_asmdata.CurrAsmList,right.location);
  645. if cmpop then
  646. location_freetemp(current_asmdata.CurrAsmList,left.location);
  647. end;
  648. {$endif SUPPORT_MMX}
  649. {*****************************************************************************
  650. AddFloat
  651. *****************************************************************************}
  652. procedure tx86addnode.second_addfloatsse;
  653. var
  654. op : topcg;
  655. begin
  656. pass_left_right;
  657. check_left_and_right_fpureg(false);
  658. if (nf_swapped in flags) then
  659. { can't use swapleftright if both are on the fpu stack, since then }
  660. { both are "R_ST" -> nothing would change -> manually switch }
  661. if (left.location.loc = LOC_FPUREGISTER) and
  662. (right.location.loc = LOC_FPUREGISTER) then
  663. emit_none(A_FXCH,S_NO)
  664. else
  665. swapleftright;
  666. case nodetype of
  667. addn :
  668. op:=OP_ADD;
  669. muln :
  670. op:=OP_MUL;
  671. subn :
  672. op:=OP_SUB;
  673. slashn :
  674. op:=OP_DIV;
  675. else
  676. internalerror(200312231);
  677. end;
  678. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  679. { we can use only right as left operand if the operation is commutative }
  680. if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  681. begin
  682. location.register:=right.location.register;
  683. { force floating point reg. location to be written to memory,
  684. we don't force it to mm register because writing to memory
  685. allows probably shorter code because there is no direct fpu->mm register
  686. copy instruction
  687. }
  688. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  689. location_force_mem(current_asmdata.CurrAsmList,left.location);
  690. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,location.size,left.location,location.register,mms_movescalar);
  691. end
  692. else
  693. begin
  694. if (nf_swapped in flags) then
  695. swapleftright;
  696. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  697. location.register:=left.location.register;
  698. { force floating point reg. location to be written to memory,
  699. we don't force it to mm register because writing to memory
  700. allows probably shorter code because there is no direct fpu->mm register
  701. copy instruction
  702. }
  703. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  704. location_force_mem(current_asmdata.CurrAsmList,right.location);
  705. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,location.register,mms_movescalar);
  706. end;
  707. end;
  708. procedure tx86addnode.second_cmpfloatsse;
  709. var
  710. op : tasmop;
  711. begin
  712. if is_single(left.resultdef) then
  713. op:=A_COMISS
  714. else if is_double(left.resultdef) then
  715. op:=A_COMISD
  716. else
  717. internalerror(200402222);
  718. pass_left_right;
  719. location_reset(location,LOC_FLAGS,def_cgsize(resultdef));
  720. { we can use only right as left operand if the operation is commutative }
  721. if (right.location.loc=LOC_MMREGISTER) then
  722. begin
  723. { force floating point reg. location to be written to memory,
  724. we don't force it to mm register because writing to memory
  725. allows probably shorter code because there is no direct fpu->mm register
  726. copy instruction
  727. }
  728. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  729. location_force_mem(current_asmdata.CurrAsmList,left.location);
  730. case left.location.loc of
  731. LOC_REFERENCE,LOC_CREFERENCE:
  732. begin
  733. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  734. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,left.location.reference,right.location.register));
  735. end;
  736. LOC_MMREGISTER,LOC_CMMREGISTER:
  737. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,left.location.register,right.location.register));
  738. else
  739. internalerror(200402221);
  740. end;
  741. if nf_swapped in flags then
  742. exclude(flags,nf_swapped)
  743. else
  744. include(flags,nf_swapped)
  745. end
  746. else
  747. begin
  748. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  749. { force floating point reg. location to be written to memory,
  750. we don't force it to mm register because writing to memory
  751. allows probably shorter code because there is no direct fpu->mm register
  752. copy instruction
  753. }
  754. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  755. location_force_mem(current_asmdata.CurrAsmList,right.location);
  756. case right.location.loc of
  757. LOC_REFERENCE,LOC_CREFERENCE:
  758. begin
  759. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  760. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,right.location.reference,left.location.register));
  761. end;
  762. LOC_MMREGISTER,LOC_CMMREGISTER:
  763. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,right.location.register,left.location.register));
  764. else
  765. internalerror(200402223);
  766. end;
  767. end;
  768. location.resflags:=getresflags(true);
  769. end;
  770. procedure tx86addnode.second_opvector;
  771. var
  772. op : topcg;
  773. begin
  774. pass_left_right;
  775. if (nf_swapped in flags) then
  776. swapleftright;
  777. case nodetype of
  778. addn :
  779. op:=OP_ADD;
  780. muln :
  781. op:=OP_MUL;
  782. subn :
  783. op:=OP_SUB;
  784. slashn :
  785. op:=OP_DIV;
  786. else
  787. internalerror(200610071);
  788. end;
  789. if fits_in_mm_register(left.resultdef) then
  790. begin
  791. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  792. { we can use only right as left operand if the operation is commutative }
  793. if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  794. begin
  795. location.register:=right.location.register;
  796. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,tfloat2tcgsize[tfloatdef(left.resultdef).floattype],left.location,location.register,nil);
  797. end
  798. else
  799. begin
  800. location_force_mmreg(current_asmdata.CurrAsmList,left.location,false);
  801. location.register:=left.location.register;
  802. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,
  803. tfloat2tcgsize[tfloatdef(tarraydef(left.resultdef).elementdef).floattype],right.location,location.register,nil);
  804. end;
  805. end
  806. else
  807. begin
  808. { not yet supported }
  809. internalerror(200610072);
  810. end
  811. end;
  812. procedure tx86addnode.second_addfloat;
  813. var
  814. op : TAsmOp;
  815. begin
  816. if use_sse(resultdef) then
  817. begin
  818. second_addfloatsse;
  819. exit;
  820. end;
  821. pass_left_right;
  822. case nodetype of
  823. addn :
  824. op:=A_FADDP;
  825. muln :
  826. op:=A_FMULP;
  827. subn :
  828. op:=A_FSUBP;
  829. slashn :
  830. op:=A_FDIVP;
  831. else
  832. internalerror(2003042214);
  833. end;
  834. check_left_and_right_fpureg(true);
  835. { if we swaped the tree nodes, then use the reverse operator }
  836. if nf_swapped in flags then
  837. begin
  838. if (nodetype=slashn) then
  839. op:=A_FDIVRP
  840. else if (nodetype=subn) then
  841. op:=A_FSUBRP;
  842. end;
  843. emit_reg_reg(op,S_NO,NR_ST,NR_ST1);
  844. tcgx86(cg).dec_fpu_stack;
  845. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  846. location.register:=NR_ST;
  847. end;
  848. procedure tx86addnode.second_cmpfloat;
  849. var
  850. resflags : tresflags;
  851. begin
  852. if use_sse(left.resultdef) or use_sse(right.resultdef) then
  853. begin
  854. second_cmpfloatsse;
  855. exit;
  856. end;
  857. pass_left_right;
  858. check_left_and_right_fpureg(true);
  859. {$ifndef x86_64}
  860. if current_settings.cputype<cpu_Pentium2 then
  861. begin
  862. emit_none(A_FCOMPP,S_NO);
  863. tcgx86(cg).dec_fpu_stack;
  864. tcgx86(cg).dec_fpu_stack;
  865. { load fpu flags }
  866. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  867. emit_reg(A_FNSTSW,S_NO,NR_AX);
  868. emit_none(A_SAHF,S_NO);
  869. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  870. if nf_swapped in flags then
  871. begin
  872. case nodetype of
  873. equaln : resflags:=F_E;
  874. unequaln : resflags:=F_NE;
  875. ltn : resflags:=F_A;
  876. lten : resflags:=F_AE;
  877. gtn : resflags:=F_B;
  878. gten : resflags:=F_BE;
  879. end;
  880. end
  881. else
  882. begin
  883. case nodetype of
  884. equaln : resflags:=F_E;
  885. unequaln : resflags:=F_NE;
  886. ltn : resflags:=F_B;
  887. lten : resflags:=F_BE;
  888. gtn : resflags:=F_A;
  889. gten : resflags:=F_AE;
  890. end;
  891. end;
  892. end
  893. else
  894. {$endif x86_64}
  895. begin
  896. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCOMIP,S_NO,NR_ST1,NR_ST0));
  897. { fcomip pops only one fpu register }
  898. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_FSTP,S_NO,NR_ST0));
  899. tcgx86(cg).dec_fpu_stack;
  900. tcgx86(cg).dec_fpu_stack;
  901. { load fpu flags }
  902. if nf_swapped in flags then
  903. begin
  904. case nodetype of
  905. equaln : resflags:=F_E;
  906. unequaln : resflags:=F_NE;
  907. ltn : resflags:=F_A;
  908. lten : resflags:=F_AE;
  909. gtn : resflags:=F_B;
  910. gten : resflags:=F_BE;
  911. end;
  912. end
  913. else
  914. begin
  915. case nodetype of
  916. equaln : resflags:=F_E;
  917. unequaln : resflags:=F_NE;
  918. ltn : resflags:=F_B;
  919. lten : resflags:=F_BE;
  920. gtn : resflags:=F_A;
  921. gten : resflags:=F_AE;
  922. end;
  923. end;
  924. end;
  925. location_reset(location,LOC_FLAGS,OS_NO);
  926. location.resflags:=resflags;
  927. end;
  928. {*****************************************************************************
  929. Add64bit
  930. *****************************************************************************}
  931. procedure tx86addnode.second_add64bit;
  932. begin
  933. {$ifdef cpu64bit}
  934. second_addordinal;
  935. {$else cpu64bit}
  936. { must be implemented separate }
  937. internalerror(200402042);
  938. {$endif cpu64bit}
  939. end;
  940. procedure tx86addnode.second_cmp64bit;
  941. begin
  942. {$ifdef cpu64bit}
  943. second_cmpordinal;
  944. {$else cpu64bit}
  945. { must be implemented separate }
  946. internalerror(200402043);
  947. {$endif cpu64bit}
  948. end;
  949. {*****************************************************************************
  950. AddOrdinal
  951. *****************************************************************************}
  952. procedure tx86addnode.second_addordinal;
  953. begin
  954. { filter unsigned MUL opcode, which requires special handling }
  955. if (nodetype=muln) and
  956. (not(is_signed(left.resultdef)) or
  957. not(is_signed(right.resultdef))) then
  958. begin
  959. second_mul;
  960. exit;
  961. end;
  962. inherited second_addordinal;
  963. end;
  964. procedure tx86addnode.second_cmpordinal;
  965. var
  966. opsize : tcgsize;
  967. unsigned : boolean;
  968. begin
  969. unsigned:=not(is_signed(left.resultdef)) or
  970. not(is_signed(right.resultdef));
  971. opsize:=def_cgsize(left.resultdef);
  972. pass_left_right;
  973. left_must_be_reg(opsize,false);
  974. emit_generic_code(A_CMP,opsize,unsigned,false,false);
  975. location_freetemp(current_asmdata.CurrAsmList,right.location);
  976. location_freetemp(current_asmdata.CurrAsmList,left.location);
  977. location_reset(location,LOC_FLAGS,OS_NO);
  978. location.resflags:=getresflags(unsigned);
  979. end;
  980. begin
  981. caddnode:=tx86addnode;
  982. end.