nx86add.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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. symtype,
  22. cgbase,
  23. cpubase,
  24. node,nadd,ncgadd;
  25. type
  26. tx86addnode = class(tcgaddnode)
  27. protected
  28. function getresflags(unsigned : boolean) : tresflags;
  29. procedure left_must_be_reg(opdef: tdef; opsize:TCGSize;noswap:boolean);
  30. procedure check_left_and_right_fpureg(force_fpureg: boolean);
  31. procedure emit_op_right_left(op:TAsmOp;opsize:TCgSize);
  32. procedure emit_generic_code(op:TAsmOp;opsize:TCgSize;unsigned,extra_not,mboverflow:boolean);
  33. procedure second_cmpfloatsse;
  34. procedure second_cmpfloatavx;
  35. procedure second_addfloatsse;
  36. procedure second_addfloatavx;
  37. public
  38. procedure second_addfloat;override;
  39. {$ifndef i8086}
  40. procedure second_addsmallset;override;
  41. {$endif not i8086}
  42. procedure second_add64bit;override;
  43. procedure second_cmpfloat;override;
  44. procedure second_cmpsmallset;override;
  45. procedure second_cmp64bit;override;
  46. procedure second_cmpordinal;override;
  47. {$ifdef SUPPORT_MMX}
  48. procedure second_opmmx;override;
  49. {$endif SUPPORT_MMX}
  50. procedure second_opvector;override;
  51. end;
  52. implementation
  53. uses
  54. globtype,globals,
  55. verbose,cutils,
  56. cpuinfo,
  57. aasmbase,aasmtai,aasmdata,aasmcpu,
  58. symconst,symdef,
  59. cgobj,hlcgobj,cgx86,cga,cgutils,
  60. paramgr,tgobj,ncgutil,
  61. ncon,nset,ninl,
  62. defutil;
  63. {*****************************************************************************
  64. Helpers
  65. *****************************************************************************}
  66. procedure tx86addnode.emit_generic_code(op:TAsmOp;opsize:TCGSize;unsigned,extra_not,mboverflow:boolean);
  67. var
  68. power : longint;
  69. hl4 : tasmlabel;
  70. r : Tregister;
  71. begin
  72. { at this point, left.location.loc should be LOC_REGISTER }
  73. if right.location.loc=LOC_REGISTER then
  74. begin
  75. { right.location is a LOC_REGISTER }
  76. { when swapped another result register }
  77. if (nodetype=subn) and (nf_swapped in flags) then
  78. begin
  79. if extra_not then
  80. emit_reg(A_NOT,TCGSize2Opsize[opsize],left.location.register);
  81. emit_reg_reg(op,TCGSize2Opsize[opsize],left.location.register,right.location.register);
  82. { newly swapped also set swapped flag }
  83. location_swap(left.location,right.location);
  84. toggleflag(nf_swapped);
  85. end
  86. else
  87. begin
  88. if extra_not then
  89. emit_reg(A_NOT,TCGSize2Opsize[opsize],right.location.register);
  90. if (op=A_ADD) or (op=A_OR) or (op=A_AND) or (op=A_XOR) or (op=A_IMUL) then
  91. location_swap(left.location,right.location);
  92. emit_reg_reg(op,TCGSize2Opsize[opsize],right.location.register,left.location.register);
  93. end;
  94. end
  95. else
  96. begin
  97. { right.location is not a LOC_REGISTER }
  98. if (nodetype=subn) and (nf_swapped in flags) then
  99. begin
  100. if extra_not then
  101. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NOT,opsize,left.location.register,left.location.register);
  102. r:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  103. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,cgsize_orddef(opsize),right.location,r);
  104. emit_reg_reg(op,TCGSize2Opsize[opsize],left.location.register,r);
  105. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,r,left.location.register);
  106. end
  107. else
  108. begin
  109. { Optimizations when right.location is a constant value }
  110. if (op=A_CMP) and
  111. (nodetype in [equaln,unequaln]) and
  112. (right.location.loc=LOC_CONSTANT) and
  113. (right.location.value=0) then
  114. begin
  115. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  116. end
  117. else
  118. if (op=A_ADD) and
  119. (right.location.loc=LOC_CONSTANT) and
  120. (right.location.value=1) and
  121. not(cs_check_overflow in current_settings.localswitches) then
  122. begin
  123. emit_reg(A_INC,TCGSize2Opsize[opsize],left.location.register);
  124. end
  125. else
  126. if (op=A_SUB) and
  127. (right.location.loc=LOC_CONSTANT) and
  128. (right.location.value=1) and
  129. not(cs_check_overflow in current_settings.localswitches) then
  130. begin
  131. emit_reg(A_DEC,TCGSize2Opsize[opsize],left.location.register);
  132. end
  133. else
  134. if (op=A_IMUL) and
  135. (right.location.loc=LOC_CONSTANT) and
  136. (ispowerof2(int64(right.location.value),power)) and
  137. not(cs_check_overflow in current_settings.localswitches) then
  138. begin
  139. emit_const_reg(A_SHL,TCGSize2Opsize[opsize],power,left.location.register);
  140. end
  141. else
  142. begin
  143. if extra_not then
  144. begin
  145. r:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  146. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,cgsize_orddef(opsize),right.location,r);
  147. emit_reg(A_NOT,TCGSize2Opsize[opsize],r);
  148. emit_reg_reg(A_AND,TCGSize2Opsize[opsize],r,left.location.register);
  149. end
  150. else
  151. begin
  152. emit_op_right_left(op,opsize);
  153. end;
  154. end;
  155. end;
  156. end;
  157. { only in case of overflow operations }
  158. { produce overflow code }
  159. { we must put it here directly, because sign of operation }
  160. { is in unsigned VAR!! }
  161. if mboverflow then
  162. begin
  163. if cs_check_overflow in current_settings.localswitches then
  164. begin
  165. current_asmdata.getjumplabel(hl4);
  166. if unsigned then
  167. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4)
  168. else
  169. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NO,hl4);
  170. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  171. cg.a_label(current_asmdata.CurrAsmList,hl4);
  172. end;
  173. end;
  174. end;
  175. procedure tx86addnode.left_must_be_reg(opdef: tdef; opsize:TCGSize;noswap:boolean);
  176. begin
  177. { left location is not a register? }
  178. if (left.location.loc<>LOC_REGISTER) then
  179. begin
  180. { if right is register then we can swap the locations }
  181. if (not noswap) and
  182. (right.location.loc=LOC_REGISTER) then
  183. begin
  184. location_swap(left.location,right.location);
  185. toggleflag(nf_swapped);
  186. end
  187. else
  188. begin
  189. { maybe we can reuse a constant register when the
  190. operation is a comparison that doesn't change the
  191. value of the register }
  192. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,(nodetype in [ltn,lten,gtn,gten,equaln,unequaln]));
  193. end;
  194. end;
  195. if (right.location.loc<>LOC_CONSTANT) and
  196. (tcgsize2unsigned[right.location.size]<>tcgsize2unsigned[opsize]) then
  197. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  198. if (left.location.loc<>LOC_CONSTANT) and
  199. (tcgsize2unsigned[left.location.size]<>tcgsize2unsigned[opsize]) then
  200. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  201. end;
  202. procedure tx86addnode.check_left_and_right_fpureg(force_fpureg: boolean);
  203. begin
  204. if (right.location.loc<>LOC_FPUREGISTER) then
  205. begin
  206. if (force_fpureg) then
  207. begin
  208. location_force_fpureg(current_asmdata.CurrAsmList,right.location,false);
  209. if (left.location.loc<>LOC_FPUREGISTER) then
  210. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false)
  211. else
  212. { left was on the stack => swap }
  213. toggleflag(nf_swapped);
  214. end
  215. end
  216. { the nominator in st0 }
  217. else if (left.location.loc<>LOC_FPUREGISTER) then
  218. begin
  219. if (force_fpureg) then
  220. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false)
  221. end
  222. else
  223. begin
  224. { fpu operands are always in the wrong order on the stack }
  225. toggleflag(nf_swapped);
  226. end;
  227. end;
  228. procedure tx86addnode.emit_op_right_left(op:TAsmOp;opsize:TCgsize);
  229. {$ifdef x86_64}
  230. var
  231. tmpreg : tregister;
  232. {$endif x86_64}
  233. begin
  234. if (right.location.loc in [LOC_CSUBSETREG,LOC_SUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  235. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  236. { left must be a register }
  237. case right.location.loc of
  238. LOC_REGISTER,
  239. LOC_CREGISTER :
  240. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,TCGSize2Opsize[opsize],right.location.register,left.location.register));
  241. LOC_REFERENCE,
  242. LOC_CREFERENCE :
  243. begin
  244. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  245. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,TCGSize2Opsize[opsize],right.location.reference,left.location.register));
  246. end;
  247. LOC_CONSTANT :
  248. begin
  249. {$ifdef x86_64}
  250. { x86_64 only supports signed 32 bits constants directly }
  251. if (opsize in [OS_S64,OS_64]) and
  252. ((right.location.value<low(longint)) or (right.location.value>high(longint))) then
  253. begin
  254. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  255. cg.a_load_const_reg(current_asmdata.CurrAsmList,opsize,right.location.value,tmpreg);
  256. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,TCGSize2Opsize[opsize],tmpreg,left.location.register));
  257. end
  258. else
  259. {$endif x86_64}
  260. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg(op,TCGSize2Opsize[opsize],right.location.value,left.location.register));
  261. end;
  262. else
  263. internalerror(200203232);
  264. end;
  265. end;
  266. function tx86addnode.getresflags(unsigned : boolean) : tresflags;
  267. begin
  268. case nodetype of
  269. equaln : getresflags:=F_E;
  270. unequaln : getresflags:=F_NE;
  271. else
  272. if not(unsigned) then
  273. begin
  274. if nf_swapped in flags then
  275. case nodetype of
  276. ltn : getresflags:=F_G;
  277. lten : getresflags:=F_GE;
  278. gtn : getresflags:=F_L;
  279. gten : getresflags:=F_LE;
  280. end
  281. else
  282. case nodetype of
  283. ltn : getresflags:=F_L;
  284. lten : getresflags:=F_LE;
  285. gtn : getresflags:=F_G;
  286. gten : getresflags:=F_GE;
  287. end;
  288. end
  289. else
  290. begin
  291. if nf_swapped in flags then
  292. case nodetype of
  293. ltn : getresflags:=F_A;
  294. lten : getresflags:=F_AE;
  295. gtn : getresflags:=F_B;
  296. gten : getresflags:=F_BE;
  297. end
  298. else
  299. case nodetype of
  300. ltn : getresflags:=F_B;
  301. lten : getresflags:=F_BE;
  302. gtn : getresflags:=F_A;
  303. gten : getresflags:=F_AE;
  304. end;
  305. end;
  306. end;
  307. end;
  308. {*****************************************************************************
  309. AddSmallSet
  310. *****************************************************************************}
  311. {$ifndef i8086}
  312. procedure tx86addnode.second_addsmallset;
  313. var
  314. setbase : aint;
  315. opdef : tdef;
  316. opsize : TCGSize;
  317. op : TAsmOp;
  318. extra_not,
  319. noswap : boolean;
  320. all_member_optimization:boolean;
  321. begin
  322. pass_left_right;
  323. noswap:=false;
  324. extra_not:=false;
  325. all_member_optimization:=false;
  326. opdef:=resultdef;
  327. opsize:=int_cgsize(opdef.size);
  328. if (left.resultdef.typ=setdef) then
  329. setbase:=tsetdef(left.resultdef).setbase
  330. else
  331. setbase:=tsetdef(right.resultdef).setbase;
  332. case nodetype of
  333. addn :
  334. begin
  335. { adding elements is not commutative }
  336. if (nf_swapped in flags) and (left.nodetype=setelementn) then
  337. swapleftright;
  338. { are we adding set elements ? }
  339. if right.nodetype=setelementn then
  340. begin
  341. { no range support for smallsets! }
  342. if assigned(tsetelementnode(right).right) then
  343. internalerror(43244);
  344. { btsb isn't supported }
  345. if opsize=OS_8 then
  346. begin
  347. opsize:=OS_32;
  348. opdef:=u32inttype;
  349. end;
  350. { bts requires both elements to be registers }
  351. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  352. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,opdef,true);
  353. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,right.location,setbase);
  354. op:=A_BTS;
  355. noswap:=true;
  356. end
  357. else
  358. op:=A_OR;
  359. end;
  360. symdifn :
  361. op:=A_XOR;
  362. muln :
  363. op:=A_AND;
  364. subn :
  365. begin
  366. op:=A_AND;
  367. if (not(nf_swapped in flags) and (left.location.loc=LOC_CONSTANT) and (left.location.value=-1)) or
  368. ((nf_swapped in flags) and (right.location.loc=LOC_CONSTANT) and (right.location.value=-1)) then
  369. all_member_optimization:=true;
  370. if (not(nf_swapped in flags)) and
  371. (right.location.loc=LOC_CONSTANT) then
  372. right.location.value := not(right.location.value)
  373. else if (nf_swapped in flags) and
  374. (left.location.loc=LOC_CONSTANT) then
  375. left.location.value := not(left.location.value)
  376. else
  377. extra_not:=true;
  378. end;
  379. xorn :
  380. op:=A_XOR;
  381. orn :
  382. op:=A_OR;
  383. andn :
  384. op:=A_AND;
  385. else
  386. internalerror(2003042215);
  387. end;
  388. if all_member_optimization then
  389. begin
  390. {A set expression [0..31]-x can be implemented with a simple NOT.}
  391. if nf_swapped in flags then
  392. begin
  393. { newly swapped also set swapped flag }
  394. location_swap(left.location,right.location);
  395. toggleflag(nf_swapped);
  396. end;
  397. hlcg.location_force_reg(current_asmdata.currAsmList,right.location,right.resultdef,opdef,false);
  398. emit_reg(A_NOT,TCGSize2Opsize[opsize],right.location.register);
  399. location:=right.location;
  400. end
  401. else
  402. begin
  403. { left must be a register }
  404. left_must_be_reg(opdef,opsize,noswap);
  405. emit_generic_code(op,opsize,true,extra_not,false);
  406. location_freetemp(current_asmdata.CurrAsmList,right.location);
  407. { left is always a register and contains the result }
  408. location:=left.location;
  409. end;
  410. { fix the changed opsize we did above because of the missing btsb }
  411. if opsize<>int_cgsize(resultdef.size) then
  412. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,opdef,cgsize_orddef(int_cgsize(resultdef.size)),false);
  413. end;
  414. {$endif not i8086}
  415. procedure tx86addnode.second_cmpsmallset;
  416. var
  417. opdef : tdef;
  418. opsize : TCGSize;
  419. op : TAsmOp;
  420. begin
  421. pass_left_right;
  422. opdef:=left.resultdef;
  423. opsize:=int_cgsize(opdef.size);
  424. case nodetype of
  425. equaln,
  426. unequaln :
  427. op:=A_CMP;
  428. lten,gten:
  429. begin
  430. if (not(nf_swapped in flags) and (nodetype = lten)) or
  431. ((nf_swapped in flags) and (nodetype = gten)) then
  432. swapleftright;
  433. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,opdef,false);
  434. emit_op_right_left(A_AND,opsize);
  435. op:=A_CMP;
  436. { warning: ugly hack, we need a JE so change the node to equaln }
  437. nodetype:=equaln;
  438. end;
  439. else
  440. internalerror(2003042215);
  441. end;
  442. { left must be a register }
  443. left_must_be_reg(opdef,opsize,false);
  444. emit_generic_code(op,opsize,true,false,false);
  445. location_freetemp(current_asmdata.CurrAsmList,right.location);
  446. location_freetemp(current_asmdata.CurrAsmList,left.location);
  447. location_reset(location,LOC_FLAGS,OS_NO);
  448. location.resflags:=getresflags(true);
  449. end;
  450. {*****************************************************************************
  451. AddMMX
  452. *****************************************************************************}
  453. {$ifdef SUPPORT_MMX}
  454. procedure tx86addnode.second_opmmx;
  455. var
  456. op : TAsmOp;
  457. cmpop : boolean;
  458. mmxbase : tmmxtype;
  459. hreg,
  460. hregister : tregister;
  461. begin
  462. pass_left_right;
  463. cmpop:=false;
  464. mmxbase:=mmx_type(left.resultdef);
  465. location_reset(location,LOC_MMXREGISTER,def_cgsize(resultdef));
  466. case nodetype of
  467. addn :
  468. begin
  469. if (cs_mmx_saturation in current_settings.localswitches) then
  470. begin
  471. case mmxbase of
  472. mmxs8bit:
  473. op:=A_PADDSB;
  474. mmxu8bit:
  475. op:=A_PADDUSB;
  476. mmxs16bit,mmxfixed16:
  477. op:=A_PADDSW;
  478. mmxu16bit:
  479. op:=A_PADDUSW;
  480. end;
  481. end
  482. else
  483. begin
  484. case mmxbase of
  485. mmxs8bit,mmxu8bit:
  486. op:=A_PADDB;
  487. mmxs16bit,mmxu16bit,mmxfixed16:
  488. op:=A_PADDW;
  489. mmxs32bit,mmxu32bit:
  490. op:=A_PADDD;
  491. end;
  492. end;
  493. end;
  494. muln :
  495. begin
  496. case mmxbase of
  497. mmxs16bit,mmxu16bit:
  498. op:=A_PMULLW;
  499. mmxfixed16:
  500. op:=A_PMULHW;
  501. end;
  502. end;
  503. subn :
  504. begin
  505. if (cs_mmx_saturation in current_settings.localswitches) then
  506. begin
  507. case mmxbase of
  508. mmxs8bit:
  509. op:=A_PSUBSB;
  510. mmxu8bit:
  511. op:=A_PSUBUSB;
  512. mmxs16bit,mmxfixed16:
  513. op:=A_PSUBSB;
  514. mmxu16bit:
  515. op:=A_PSUBUSW;
  516. end;
  517. end
  518. else
  519. begin
  520. case mmxbase of
  521. mmxs8bit,mmxu8bit:
  522. op:=A_PSUBB;
  523. mmxs16bit,mmxu16bit,mmxfixed16:
  524. op:=A_PSUBW;
  525. mmxs32bit,mmxu32bit:
  526. op:=A_PSUBD;
  527. end;
  528. end;
  529. end;
  530. xorn:
  531. op:=A_PXOR;
  532. orn:
  533. op:=A_POR;
  534. andn:
  535. op:=A_PAND;
  536. else
  537. internalerror(2003042214);
  538. end;
  539. { left and right no register? }
  540. { then one must be demanded }
  541. if (left.location.loc<>LOC_MMXREGISTER) then
  542. begin
  543. if (right.location.loc=LOC_MMXREGISTER) then
  544. begin
  545. location_swap(left.location,right.location);
  546. toggleflag(nf_swapped);
  547. end
  548. else
  549. begin
  550. { register variable ? }
  551. if (left.location.loc=LOC_CMMXREGISTER) then
  552. begin
  553. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  554. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  555. end
  556. else
  557. begin
  558. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  559. internalerror(200203245);
  560. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  561. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  562. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  563. end;
  564. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  565. left.location.register:=hregister;
  566. end;
  567. end;
  568. { at this point, left.location.loc should be LOC_MMXREGISTER }
  569. if right.location.loc<>LOC_MMXREGISTER then
  570. begin
  571. if (nodetype=subn) and (nf_swapped in flags) then
  572. begin
  573. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  574. if right.location.loc=LOC_CMMXREGISTER then
  575. begin
  576. emit_reg_reg(A_MOVQ,S_NO,right.location.register,hreg);
  577. emit_reg_reg(op,S_NO,left.location.register,hreg);
  578. end
  579. else
  580. begin
  581. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  582. internalerror(200203247);
  583. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  584. emit_ref_reg(A_MOVQ,S_NO,right.location.reference,hreg);
  585. emit_reg_reg(op,S_NO,left.location.register,hreg);
  586. end;
  587. location.register:=hreg;
  588. end
  589. else
  590. begin
  591. if (right.location.loc=LOC_CMMXREGISTER) then
  592. emit_reg_reg(op,S_NO,right.location.register,left.location.register)
  593. else
  594. begin
  595. if not(right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  596. internalerror(200203246);
  597. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  598. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  599. end;
  600. location.register:=left.location.register;
  601. end;
  602. end
  603. else
  604. begin
  605. { right.location=LOC_MMXREGISTER }
  606. if (nodetype=subn) and (nf_swapped in flags) then
  607. begin
  608. emit_reg_reg(op,S_NO,left.location.register,right.location.register);
  609. location_swap(left.location,right.location);
  610. toggleflag(nf_swapped);
  611. end
  612. else
  613. begin
  614. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  615. end;
  616. location.register:=left.location.register;
  617. end;
  618. location_freetemp(current_asmdata.CurrAsmList,right.location);
  619. if cmpop then
  620. location_freetemp(current_asmdata.CurrAsmList,left.location);
  621. end;
  622. {$endif SUPPORT_MMX}
  623. {*****************************************************************************
  624. AddFloat
  625. *****************************************************************************}
  626. procedure tx86addnode.second_addfloatsse;
  627. var
  628. op : topcg;
  629. sqr_sum : boolean;
  630. tmp : tnode;
  631. begin
  632. sqr_sum:=false;
  633. if (current_settings.fputype>=fpu_sse3) and
  634. use_vectorfpu(resultdef) and
  635. (nodetype in [addn,subn]) and
  636. (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) and
  637. (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  638. begin
  639. sqr_sum:=true;
  640. tmp:=tinlinenode(left).left;
  641. tinlinenode(left).left:=nil;
  642. left.free;
  643. left:=tmp;
  644. tmp:=tinlinenode(right).left;
  645. tinlinenode(right).left:=nil;
  646. right.free;
  647. right:=tmp;
  648. end;
  649. pass_left_right;
  650. check_left_and_right_fpureg(false);
  651. if (nf_swapped in flags) then
  652. { can't use swapleftright if both are on the fpu stack, since then }
  653. { both are "R_ST" -> nothing would change -> manually switch }
  654. if (left.location.loc = LOC_FPUREGISTER) and
  655. (right.location.loc = LOC_FPUREGISTER) then
  656. emit_none(A_FXCH,S_NO)
  657. else
  658. swapleftright;
  659. case nodetype of
  660. addn :
  661. op:=OP_ADD;
  662. muln :
  663. op:=OP_MUL;
  664. subn :
  665. op:=OP_SUB;
  666. slashn :
  667. op:=OP_DIV;
  668. else
  669. internalerror(200312231);
  670. end;
  671. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  672. if sqr_sum then
  673. begin
  674. if nf_swapped in flags then
  675. swapleftright;
  676. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  677. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  678. location:=left.location;
  679. if is_double(resultdef) then
  680. begin
  681. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,right.location.register,location.register));
  682. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPD,S_NO,location.register,location.register));
  683. case nodetype of
  684. addn:
  685. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPD,S_NO,location.register,location.register));
  686. subn:
  687. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPD,S_NO,location.register,location.register));
  688. else
  689. internalerror(201108162);
  690. end;
  691. end
  692. else
  693. begin
  694. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_UNPCKLPS,S_NO,right.location.register,location.register));
  695. { ensure that bits 64..127 contain valid values }
  696. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,location.register,location.register));
  697. { the data is now in bits 0..32 and 64..95 }
  698. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPS,S_NO,location.register,location.register));
  699. case nodetype of
  700. addn:
  701. begin
  702. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPS,S_NO,location.register,location.register));
  703. end;
  704. subn:
  705. begin
  706. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPS,S_NO,location.register,location.register));
  707. end;
  708. else
  709. internalerror(201108163);
  710. end;
  711. end
  712. end
  713. { we can use only right as left operand if the operation is commutative }
  714. else if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  715. begin
  716. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  717. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,right.location.size,location.size,right.location.register,location.register,mms_movescalar);
  718. { force floating point reg. location to be written to memory,
  719. we don't force it to mm register because writing to memory
  720. allows probably shorter code because there is no direct fpu->mm register
  721. copy instruction
  722. }
  723. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  724. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  725. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,location.size,left.location,location.register,mms_movescalar);
  726. end
  727. else
  728. begin
  729. if nf_swapped in flags then
  730. swapleftright;
  731. { force floating point reg. location to be written to memory,
  732. we don't force it to mm register because writing to memory
  733. allows probably shorter code because there is no direct fpu->mm register
  734. copy instruction
  735. }
  736. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  737. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  738. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  739. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  740. { force floating point reg. location to be written to memory,
  741. we don't force it to mm register because writing to memory
  742. allows probably shorter code because there is no direct fpu->mm register
  743. copy instruction
  744. }
  745. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  746. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  747. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,location.size,right.location,location.register,mms_movescalar);
  748. end;
  749. end;
  750. procedure tx86addnode.second_addfloatavx;
  751. var
  752. op : topcg;
  753. sqr_sum : boolean;
  754. tmp : tnode;
  755. begin
  756. sqr_sum:=false;
  757. {$ifdef dummy}
  758. if (current_settings.fputype>=fpu_sse3) and
  759. use_vectorfpu(resultdef) and
  760. (nodetype in [addn,subn]) and
  761. (left.nodetype=inlinen) and (tinlinenode(left).inlinenumber=in_sqr_real) and
  762. (right.nodetype=inlinen) and (tinlinenode(right).inlinenumber=in_sqr_real) then
  763. begin
  764. sqr_sum:=true;
  765. tmp:=tinlinenode(left).left;
  766. tinlinenode(left).left:=nil;
  767. left.free;
  768. left:=tmp;
  769. tmp:=tinlinenode(right).left;
  770. tinlinenode(right).left:=nil;
  771. right.free;
  772. right:=tmp;
  773. end;
  774. {$endif dummy}
  775. pass_left_right;
  776. check_left_and_right_fpureg(false);
  777. if (nf_swapped in flags) then
  778. { can't use swapleftright if both are on the fpu stack, since then }
  779. { both are "R_ST" -> nothing would change -> manually switch }
  780. if (left.location.loc = LOC_FPUREGISTER) and
  781. (right.location.loc = LOC_FPUREGISTER) then
  782. emit_none(A_FXCH,S_NO)
  783. else
  784. swapleftright;
  785. case nodetype of
  786. addn :
  787. op:=OP_ADD;
  788. muln :
  789. op:=OP_MUL;
  790. subn :
  791. op:=OP_SUB;
  792. slashn :
  793. op:=OP_DIV;
  794. else
  795. internalerror(200312231);
  796. end;
  797. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  798. if sqr_sum then
  799. begin
  800. if nf_swapped in flags then
  801. swapleftright;
  802. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  803. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  804. location:=left.location;
  805. if is_double(resultdef) then
  806. begin
  807. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,right.location.register,location.register));
  808. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPD,S_NO,location.register,location.register));
  809. case nodetype of
  810. addn:
  811. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPD,S_NO,location.register,location.register));
  812. subn:
  813. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPD,S_NO,location.register,location.register));
  814. else
  815. internalerror(201108162);
  816. end;
  817. end
  818. else
  819. begin
  820. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_UNPCKLPS,S_NO,right.location.register,location.register));
  821. { ensure that bits 64..127 contain valid values }
  822. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_SHUFPD,S_NO,%00,location.register,location.register));
  823. { the data is now in bits 0..32 and 64..95 }
  824. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_MULPS,S_NO,location.register,location.register));
  825. case nodetype of
  826. addn:
  827. begin
  828. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HADDPS,S_NO,location.register,location.register));
  829. end;
  830. subn:
  831. begin
  832. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_HSUBPS,S_NO,location.register,location.register));
  833. end;
  834. else
  835. internalerror(201108163);
  836. end;
  837. end
  838. end
  839. { we can use only right as left operand if the operation is commutative }
  840. else if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  841. begin
  842. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  843. { force floating point reg. location to be written to memory,
  844. we don't force it to mm register because writing to memory
  845. allows probably shorter code because there is no direct fpu->mm register
  846. copy instruction
  847. }
  848. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  849. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  850. cg.a_opmm_loc_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  851. left.location,
  852. right.location.register,
  853. location.register,
  854. mms_movescalar);
  855. end
  856. else
  857. begin
  858. if (nf_swapped in flags) then
  859. swapleftright;
  860. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  861. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  862. { force floating point reg. location to be written to memory,
  863. we don't force it to mm register because writing to memory
  864. allows probably shorter code because there is no direct fpu->mm register
  865. copy instruction
  866. }
  867. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  868. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  869. cg.a_opmm_loc_reg_reg(current_asmdata.CurrAsmList,op,location.size,
  870. right.location,
  871. left.location.register,
  872. location.register,
  873. mms_movescalar);
  874. end;
  875. end;
  876. procedure tx86addnode.second_cmpfloatsse;
  877. var
  878. op : tasmop;
  879. begin
  880. if is_single(left.resultdef) then
  881. op:=A_COMISS
  882. else if is_double(left.resultdef) then
  883. op:=A_COMISD
  884. else
  885. internalerror(200402222);
  886. pass_left_right;
  887. location_reset(location,LOC_FLAGS,def_cgsize(resultdef));
  888. { we can use only right as left operand if the operation is commutative }
  889. if (right.location.loc=LOC_MMREGISTER) then
  890. begin
  891. { force floating point reg. location to be written to memory,
  892. we don't force it to mm register because writing to memory
  893. allows probably shorter code because there is no direct fpu->mm register
  894. copy instruction
  895. }
  896. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  897. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  898. case left.location.loc of
  899. LOC_REFERENCE,LOC_CREFERENCE:
  900. begin
  901. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  902. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,left.location.reference,right.location.register));
  903. end;
  904. LOC_MMREGISTER,LOC_CMMREGISTER:
  905. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,left.location.register,right.location.register));
  906. else
  907. internalerror(200402221);
  908. end;
  909. if nf_swapped in flags then
  910. exclude(flags,nf_swapped)
  911. else
  912. include(flags,nf_swapped)
  913. end
  914. else
  915. begin
  916. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,false);
  917. { force floating point reg. location to be written to memory,
  918. we don't force it to mm register because writing to memory
  919. allows probably shorter code because there is no direct fpu->mm register
  920. copy instruction
  921. }
  922. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  923. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  924. case right.location.loc of
  925. LOC_REFERENCE,LOC_CREFERENCE:
  926. begin
  927. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  928. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,right.location.reference,left.location.register));
  929. end;
  930. LOC_MMREGISTER,LOC_CMMREGISTER:
  931. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,right.location.register,left.location.register));
  932. else
  933. internalerror(200402223);
  934. end;
  935. end;
  936. location.resflags:=getresflags(true);
  937. end;
  938. procedure tx86addnode.second_cmpfloatavx;
  939. var
  940. op : tasmop;
  941. begin
  942. if is_single(left.resultdef) then
  943. op:=A_VCOMISS
  944. else if is_double(left.resultdef) then
  945. op:=A_VCOMISD
  946. else
  947. internalerror(200402222);
  948. pass_left_right;
  949. location_reset(location,LOC_FLAGS,def_cgsize(resultdef));
  950. { we can use only right as left operand if the operation is commutative }
  951. if (right.location.loc=LOC_MMREGISTER) then
  952. begin
  953. { force floating point reg. location to be written to memory,
  954. we don't force it to mm register because writing to memory
  955. allows probably shorter code because there is no direct fpu->mm register
  956. copy instruction
  957. }
  958. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  959. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  960. case left.location.loc of
  961. LOC_REFERENCE,LOC_CREFERENCE:
  962. begin
  963. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  964. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,left.location.reference,right.location.register));
  965. end;
  966. LOC_MMREGISTER,LOC_CMMREGISTER:
  967. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,left.location.register,right.location.register));
  968. else
  969. internalerror(200402221);
  970. end;
  971. if nf_swapped in flags then
  972. exclude(flags,nf_swapped)
  973. else
  974. include(flags,nf_swapped)
  975. end
  976. else
  977. begin
  978. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  979. { force floating point reg. location to be written to memory,
  980. we don't force it to mm register because writing to memory
  981. allows probably shorter code because there is no direct fpu->mm register
  982. copy instruction
  983. }
  984. if right.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  985. hlcg.location_force_mem(current_asmdata.CurrAsmList,right.location,right.resultdef);
  986. case right.location.loc of
  987. LOC_REFERENCE,LOC_CREFERENCE:
  988. begin
  989. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  990. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_NO,right.location.reference,left.location.register));
  991. end;
  992. LOC_MMREGISTER,LOC_CMMREGISTER:
  993. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_NO,right.location.register,left.location.register));
  994. else
  995. internalerror(200402223);
  996. end;
  997. end;
  998. location.resflags:=getresflags(true);
  999. end;
  1000. procedure tx86addnode.second_opvector;
  1001. var
  1002. op : topcg;
  1003. begin
  1004. pass_left_right;
  1005. if (nf_swapped in flags) then
  1006. swapleftright;
  1007. case nodetype of
  1008. addn :
  1009. op:=OP_ADD;
  1010. muln :
  1011. op:=OP_MUL;
  1012. subn :
  1013. op:=OP_SUB;
  1014. slashn :
  1015. op:=OP_DIV;
  1016. else
  1017. internalerror(200610071);
  1018. end;
  1019. if fits_in_mm_register(left.resultdef) then
  1020. begin
  1021. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  1022. { we can use only right as left operand if the operation is commutative }
  1023. if (right.location.loc=LOC_MMREGISTER) and (op in [OP_ADD,OP_MUL]) then
  1024. begin
  1025. location.register:=right.location.register;
  1026. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,tfloat2tcgsize[tfloatdef(left.resultdef).floattype],left.location,location.register,nil);
  1027. end
  1028. else
  1029. begin
  1030. location_force_mmreg(current_asmdata.CurrAsmList,left.location,false);
  1031. location.register:=left.location.register;
  1032. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,op,
  1033. tfloat2tcgsize[tfloatdef(tarraydef(left.resultdef).elementdef).floattype],right.location,location.register,nil);
  1034. end;
  1035. end
  1036. else
  1037. begin
  1038. { not yet supported }
  1039. internalerror(200610072);
  1040. end
  1041. end;
  1042. procedure tx86addnode.second_addfloat;
  1043. var
  1044. op : TAsmOp;
  1045. begin
  1046. if use_vectorfpu(resultdef) then
  1047. begin
  1048. if UseAVX then
  1049. second_addfloatavx
  1050. else
  1051. second_addfloatsse;
  1052. exit;
  1053. end;
  1054. pass_left_right;
  1055. case nodetype of
  1056. addn :
  1057. op:=A_FADDP;
  1058. muln :
  1059. op:=A_FMULP;
  1060. subn :
  1061. op:=A_FSUBP;
  1062. slashn :
  1063. op:=A_FDIVP;
  1064. else
  1065. internalerror(2003042214);
  1066. end;
  1067. check_left_and_right_fpureg(true);
  1068. { if we swaped the tree nodes, then use the reverse operator }
  1069. if nf_swapped in flags then
  1070. begin
  1071. if (nodetype=slashn) then
  1072. op:=A_FDIVRP
  1073. else if (nodetype=subn) then
  1074. op:=A_FSUBRP;
  1075. end;
  1076. emit_reg_reg(op,S_NO,NR_ST,NR_ST1);
  1077. tcgx86(cg).dec_fpu_stack;
  1078. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  1079. location.register:=NR_ST;
  1080. end;
  1081. procedure tx86addnode.second_cmpfloat;
  1082. var
  1083. resflags : tresflags;
  1084. {$ifdef i8086}
  1085. tmpref: treference;
  1086. {$endif i8086}
  1087. begin
  1088. if use_vectorfpu(left.resultdef) or use_vectorfpu(right.resultdef) then
  1089. begin
  1090. if UseAVX then
  1091. second_cmpfloatavx
  1092. else
  1093. second_cmpfloatsse;
  1094. exit;
  1095. end;
  1096. pass_left_right;
  1097. check_left_and_right_fpureg(true);
  1098. {$ifndef x86_64}
  1099. if current_settings.cputype<cpu_Pentium2 then
  1100. begin
  1101. emit_none(A_FCOMPP,S_NO);
  1102. tcgx86(cg).dec_fpu_stack;
  1103. tcgx86(cg).dec_fpu_stack;
  1104. { load fpu flags }
  1105. {$ifdef i8086}
  1106. if current_settings.cputype < cpu_286 then
  1107. begin
  1108. tg.gettemp(current_asmdata.CurrAsmList,2,2,tt_normal,tmpref);
  1109. emit_ref(A_FNSTSW,S_NO,tmpref);
  1110. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1111. emit_ref_reg(A_MOV,S_W,tmpref,NR_AX);
  1112. emit_none(A_SAHF,S_NO);
  1113. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1114. tg.ungettemp(current_asmdata.CurrAsmList,tmpref);
  1115. end
  1116. else
  1117. {$endif i8086}
  1118. begin
  1119. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1120. emit_reg(A_FNSTSW,S_NO,NR_AX);
  1121. emit_none(A_SAHF,S_NO);
  1122. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  1123. end;
  1124. if nf_swapped in flags then
  1125. begin
  1126. case nodetype of
  1127. equaln : resflags:=F_E;
  1128. unequaln : resflags:=F_NE;
  1129. ltn : resflags:=F_A;
  1130. lten : resflags:=F_AE;
  1131. gtn : resflags:=F_B;
  1132. gten : resflags:=F_BE;
  1133. end;
  1134. end
  1135. else
  1136. begin
  1137. case nodetype of
  1138. equaln : resflags:=F_E;
  1139. unequaln : resflags:=F_NE;
  1140. ltn : resflags:=F_B;
  1141. lten : resflags:=F_BE;
  1142. gtn : resflags:=F_A;
  1143. gten : resflags:=F_AE;
  1144. end;
  1145. end;
  1146. end
  1147. else
  1148. {$endif x86_64}
  1149. begin
  1150. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCOMIP,S_NO,NR_ST1,NR_ST0));
  1151. { fcomip pops only one fpu register }
  1152. current_asmdata.CurrAsmList.concat(taicpu.op_reg(A_FSTP,S_NO,NR_ST0));
  1153. tcgx86(cg).dec_fpu_stack;
  1154. tcgx86(cg).dec_fpu_stack;
  1155. { load fpu flags }
  1156. if nf_swapped in flags then
  1157. begin
  1158. case nodetype of
  1159. equaln : resflags:=F_E;
  1160. unequaln : resflags:=F_NE;
  1161. ltn : resflags:=F_A;
  1162. lten : resflags:=F_AE;
  1163. gtn : resflags:=F_B;
  1164. gten : resflags:=F_BE;
  1165. end;
  1166. end
  1167. else
  1168. begin
  1169. case nodetype of
  1170. equaln : resflags:=F_E;
  1171. unequaln : resflags:=F_NE;
  1172. ltn : resflags:=F_B;
  1173. lten : resflags:=F_BE;
  1174. gtn : resflags:=F_A;
  1175. gten : resflags:=F_AE;
  1176. end;
  1177. end;
  1178. end;
  1179. location_reset(location,LOC_FLAGS,OS_NO);
  1180. location.resflags:=resflags;
  1181. end;
  1182. {*****************************************************************************
  1183. Add64bit
  1184. *****************************************************************************}
  1185. procedure tx86addnode.second_add64bit;
  1186. begin
  1187. {$ifdef cpu64bitalu}
  1188. second_addordinal;
  1189. {$else cpu64bitalu}
  1190. { must be implemented separate }
  1191. internalerror(200402042);
  1192. {$endif cpu64bitalu}
  1193. end;
  1194. procedure tx86addnode.second_cmp64bit;
  1195. begin
  1196. {$ifdef cpu64bitalu}
  1197. second_cmpordinal;
  1198. {$else cpu64bitalu}
  1199. { must be implemented separate }
  1200. internalerror(200402043);
  1201. {$endif cpu64bitalu}
  1202. end;
  1203. {*****************************************************************************
  1204. AddOrdinal
  1205. *****************************************************************************}
  1206. procedure tx86addnode.second_cmpordinal;
  1207. var
  1208. opdef : tdef;
  1209. opsize : tcgsize;
  1210. unsigned : boolean;
  1211. begin
  1212. unsigned:=not(is_signed(left.resultdef)) or
  1213. not(is_signed(right.resultdef));
  1214. opdef:=left.resultdef;
  1215. opsize:=def_cgsize(opdef);
  1216. pass_left_right;
  1217. left_must_be_reg(opdef,opsize,false);
  1218. emit_generic_code(A_CMP,opsize,unsigned,false,false);
  1219. location_freetemp(current_asmdata.CurrAsmList,right.location);
  1220. location_freetemp(current_asmdata.CurrAsmList,left.location);
  1221. location_reset(location,LOC_FLAGS,OS_NO);
  1222. location.resflags:=getresflags(unsigned);
  1223. end;
  1224. begin
  1225. caddnode:=tx86addnode;
  1226. end.