nx86add.pas 38 KB

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