nx86add.pas 38 KB

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