nx86add.pas 39 KB

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