nx86mat.pas 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 code for math nodes
  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 nx86mat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgmat;
  22. type
  23. tx86unaryminusnode = class(tcgunaryminusnode)
  24. {$ifdef SUPPORT_MMX}
  25. procedure second_mmx;override;
  26. {$endif SUPPORT_MMX}
  27. procedure second_float;override;
  28. function pass_1:tnode;override;
  29. end;
  30. tx86notnode = class(tcgnotnode)
  31. procedure second_boolean;override;
  32. {$ifdef SUPPORT_MMX}
  33. procedure second_mmx;override;
  34. {$endif SUPPORT_MMX}
  35. end;
  36. tx86moddivnode = class(tcgmoddivnode)
  37. procedure pass_generate_code;override;
  38. end;
  39. tx86shlshrnode = class(tcgshlshrnode)
  40. {$ifdef SUPPORT_MMX}
  41. procedure second_mmx;override;
  42. {$endif SUPPORT_MMX}
  43. end;
  44. implementation
  45. uses
  46. globtype,
  47. constexp,
  48. cutils,verbose,globals,
  49. symconst,symdef,
  50. aasmbase,aasmtai,aasmcpu,aasmdata,defutil,
  51. cgbase,pass_1,pass_2,
  52. ncon,
  53. cpubase,cpuinfo,
  54. cga,cgobj,hlcgobj,cgx86,cgutils,
  55. tgobj;
  56. {*****************************************************************************
  57. TI386UNARYMINUSNODE
  58. *****************************************************************************}
  59. function tx86unaryminusnode.pass_1 : tnode;
  60. begin
  61. result:=nil;
  62. firstpass(left);
  63. if codegenerror then
  64. exit;
  65. if (left.resultdef.typ=floatdef) then
  66. begin
  67. if use_vectorfpu(left.resultdef) then
  68. expectloc:=LOC_MMREGISTER
  69. else
  70. expectloc:=LOC_FPUREGISTER;
  71. end
  72. {$ifdef SUPPORT_MMX}
  73. else
  74. if (cs_mmx in current_settings.localswitches) and
  75. is_mmx_able_array(left.resultdef) then
  76. begin
  77. expectloc:=LOC_MMXREGISTER;
  78. end
  79. {$endif SUPPORT_MMX}
  80. else
  81. inherited pass_1;
  82. end;
  83. {$ifdef SUPPORT_MMX}
  84. procedure tx86unaryminusnode.second_mmx;
  85. var
  86. op : tasmop;
  87. hreg : tregister;
  88. begin
  89. op:=A_NONE;
  90. secondpass(left);
  91. location_reset(location,LOC_MMXREGISTER,OS_NO);
  92. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  93. emit_reg_reg(A_PXOR,S_NO,hreg,hreg);
  94. case left.location.loc of
  95. LOC_MMXREGISTER:
  96. begin
  97. location.register:=left.location.register;
  98. end;
  99. LOC_CMMXREGISTER:
  100. begin
  101. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  102. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  103. end;
  104. LOC_REFERENCE,
  105. LOC_CREFERENCE:
  106. begin
  107. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  108. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  109. end;
  110. else
  111. internalerror(200203225);
  112. end;
  113. if cs_mmx_saturation in current_settings.localswitches then
  114. case mmx_type(resultdef) of
  115. mmxs8bit:
  116. op:=A_PSUBSB;
  117. mmxu8bit:
  118. op:=A_PSUBUSB;
  119. mmxs16bit,mmxfixed16:
  120. op:=A_PSUBSW;
  121. mmxu16bit:
  122. op:=A_PSUBUSW;
  123. else
  124. ;
  125. end
  126. else
  127. case mmx_type(resultdef) of
  128. mmxs8bit,mmxu8bit:
  129. op:=A_PSUBB;
  130. mmxs16bit,mmxu16bit,mmxfixed16:
  131. op:=A_PSUBW;
  132. mmxs32bit,mmxu32bit:
  133. op:=A_PSUBD;
  134. else
  135. ;
  136. end;
  137. if op = A_NONE then
  138. internalerror(201408202);
  139. emit_reg_reg(op,S_NO,location.register,hreg);
  140. emit_reg_reg(A_MOVQ,S_NO,hreg,location.register);
  141. end;
  142. {$endif SUPPORT_MMX}
  143. procedure tx86unaryminusnode.second_float;
  144. var
  145. reg : tregister;
  146. href : treference;
  147. l1 : tasmlabel;
  148. begin
  149. secondpass(left);
  150. if expectloc=LOC_MMREGISTER then
  151. begin
  152. if not(left.location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER,LOC_CREFERENCE,LOC_REFERENCE]) then
  153. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  154. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  155. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  156. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_XOR,location.size,location.register,location.register,nil);
  157. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,OP_SUB,location.size,left.location,location.register,mms_movescalar);
  158. end
  159. else
  160. begin
  161. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  162. case left.location.loc of
  163. LOC_REFERENCE,
  164. LOC_CREFERENCE:
  165. begin
  166. location.register:=NR_ST;
  167. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  168. left.location.size,location.size,
  169. left.location.reference,location.register);
  170. emit_none(A_FCHS,S_NO);
  171. end;
  172. LOC_FPUREGISTER,
  173. LOC_CFPUREGISTER:
  174. begin
  175. { "load st,st" is ignored by the code generator }
  176. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,NR_ST);
  177. location.register:=NR_ST;
  178. emit_none(A_FCHS,S_NO);
  179. end;
  180. else
  181. internalerror(200312241);
  182. end;
  183. end;
  184. end;
  185. {*****************************************************************************
  186. TX86NOTNODE
  187. *****************************************************************************}
  188. procedure tx86notnode.second_boolean;
  189. var
  190. opsize : tcgsize;
  191. {$if defined(cpu32bitalu) or defined(cpu16bitalu)}
  192. hreg: tregister;
  193. {$endif}
  194. begin
  195. opsize:=def_cgsize(resultdef);
  196. secondpass(left);
  197. if not handle_locjump then
  198. begin
  199. case left.location.loc of
  200. LOC_FLAGS :
  201. begin
  202. location_reset(location,LOC_FLAGS,OS_NO);
  203. location.resflags:=left.location.resflags;
  204. inverse_flags(location.resflags);
  205. end;
  206. LOC_CREFERENCE,
  207. LOC_REFERENCE:
  208. begin
  209. {$if defined(cpu32bitalu)}
  210. if is_64bit(resultdef) then
  211. begin
  212. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  213. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  214. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  215. inc(left.location.reference.offset,4);
  216. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.reference,hreg);
  217. end
  218. else
  219. {$elseif defined(cpu16bitalu)}
  220. if is_64bit(resultdef) then
  221. begin
  222. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_16);
  223. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  224. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,hreg);
  225. inc(left.location.reference.offset,2);
  226. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  227. inc(left.location.reference.offset,2);
  228. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  229. inc(left.location.reference.offset,2);
  230. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  231. end
  232. else if is_32bit(resultdef) then
  233. begin
  234. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_16);
  235. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  236. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,hreg);
  237. inc(left.location.reference.offset,2);
  238. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  239. end
  240. else
  241. {$endif}
  242. emit_const_ref(A_CMP, TCGSize2Opsize[opsize], 0, left.location.reference);
  243. location_reset(location,LOC_FLAGS,OS_NO);
  244. location.resflags:=F_E;
  245. end;
  246. LOC_CONSTANT,
  247. LOC_REGISTER,
  248. LOC_CREGISTER,
  249. LOC_SUBSETREG,
  250. LOC_CSUBSETREG,
  251. LOC_SUBSETREF,
  252. LOC_CSUBSETREF :
  253. begin
  254. {$if defined(cpu32bitalu)}
  255. if is_64bit(resultdef) then
  256. begin
  257. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  258. emit_reg_reg(A_OR,S_L,left.location.register64.reghi,left.location.register64.reglo);
  259. end
  260. else
  261. {$elseif defined(cpu16bitalu)}
  262. if is_64bit(resultdef) then
  263. begin
  264. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  265. emit_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register64.reghi),left.location.register64.reghi);
  266. emit_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register64.reglo),left.location.register64.reglo);
  267. emit_reg_reg(A_OR,S_W,left.location.register64.reghi,left.location.register64.reglo);
  268. end
  269. else if is_32bit(resultdef) then
  270. begin
  271. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  272. emit_reg_reg(A_OR,S_L,cg.GetNextReg(left.location.register),left.location.register);
  273. end
  274. else
  275. {$endif}
  276. begin
  277. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  278. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  279. end;
  280. location_reset(location,LOC_FLAGS,OS_NO);
  281. location.resflags:=F_E;
  282. end;
  283. else
  284. internalerror(200203224);
  285. end;
  286. end;
  287. end;
  288. {$ifdef SUPPORT_MMX}
  289. procedure tx86notnode.second_mmx;
  290. var hreg,r:Tregister;
  291. begin
  292. secondpass(left);
  293. location_reset(location,LOC_MMXREGISTER,OS_NO);
  294. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  295. emit_const_reg(A_MOV,S_L,longint($ffffffff),r);
  296. { load operand }
  297. case left.location.loc of
  298. LOC_MMXREGISTER:
  299. location_copy(location,left.location);
  300. LOC_CMMXREGISTER:
  301. begin
  302. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  303. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  304. end;
  305. LOC_REFERENCE,
  306. LOC_CREFERENCE:
  307. begin
  308. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  309. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  310. end;
  311. else
  312. internalerror(2019050906);
  313. end;
  314. { load mask }
  315. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  316. emit_reg_reg(A_MOVD,S_NO,r,hreg);
  317. { lower 32 bit }
  318. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  319. { shift mask }
  320. emit_const_reg(A_PSLLQ,S_B,32,hreg);
  321. { higher 32 bit }
  322. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  323. end;
  324. {$endif SUPPORT_MMX}
  325. {*****************************************************************************
  326. TX86MODDIVNODE
  327. *****************************************************************************}
  328. procedure tx86moddivnode.pass_generate_code;
  329. var
  330. hreg1,hreg2,hreg3,rega,regd,tempreg:Tregister;
  331. power:longint;
  332. instr:TAiCpu;
  333. op:Tasmop;
  334. cgsize:TCgSize;
  335. opsize:topsize;
  336. e, sm: aint;
  337. d,m: aword;
  338. m_add, invertsign: boolean;
  339. s: byte;
  340. label
  341. DefaultDiv;
  342. begin
  343. secondpass(left);
  344. if codegenerror then
  345. exit;
  346. secondpass(right);
  347. if codegenerror then
  348. exit;
  349. { put numerator in register }
  350. cgsize:=def_cgsize(resultdef);
  351. opsize:=TCGSize2OpSize[cgsize];
  352. if not (cgsize in [OS_32,OS_S32,OS_64,OS_S64]) then
  353. InternalError(2013102702);
  354. rega:=newreg(R_INTREGISTER,RS_EAX,cgsize2subreg(R_INTREGISTER,cgsize));
  355. regd:=newreg(R_INTREGISTER,RS_EDX,cgsize2subreg(R_INTREGISTER,cgsize));
  356. location_reset(location,LOC_REGISTER,cgsize);
  357. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  358. hreg1:=left.location.register;
  359. if (nodetype=divn) and (right.nodetype=ordconstn) then
  360. begin
  361. if isabspowerof2(tordconstnode(right).value,power) then
  362. begin
  363. { for signed numbers, the numerator must be adjusted before the
  364. shift instruction, but not with unsigned numbers! Otherwise,
  365. "Cardinal($ffffffff) div 16" overflows! (JM) }
  366. if is_signed(left.resultdef) Then
  367. begin
  368. invertsign:=tordconstnode(right).value<0;
  369. { use a sequence without jumps, saw this in
  370. comp.compilers (JM) }
  371. { no jumps, but more operations }
  372. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  373. emit_reg_reg(A_MOV,opsize,hreg1,hreg2);
  374. if power=1 then
  375. begin
  376. {If the left value is negative, hreg2=(1 shl power)-1=1, otherwise 0.}
  377. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-1,hreg2);
  378. end
  379. else
  380. begin
  381. {If the left value is negative, hreg2=$ffffffff, otherwise 0.}
  382. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,resultdef.size*8-1,hreg2);
  383. {If negative, hreg2=(1 shl power)-1, otherwise 0.}
  384. { (don't use emit_const_reg, because if value>high(longint)
  385. then it must first be loaded into a register) }
  386. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,cgsize,(aint(1) shl power)-1,hreg2);
  387. end;
  388. { add to the left value }
  389. emit_reg_reg(A_ADD,opsize,hreg2,hreg1);
  390. { do the shift }
  391. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,power,hreg1);
  392. if invertsign then
  393. emit_reg(A_NEG,opsize,hreg1);
  394. end
  395. else
  396. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,power,hreg1);
  397. location.register:=hreg1;
  398. end
  399. else
  400. begin
  401. if is_signed(left.resultdef) then
  402. begin
  403. e:=tordconstnode(right).value.svalue;
  404. calc_divconst_magic_signed(resultdef.size*8,e,sm,s);
  405. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  406. emit_const_reg(A_MOV,opsize,sm,rega);
  407. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  408. emit_reg(A_IMUL,opsize,hreg1);
  409. { only the high half of result is used }
  410. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  411. { add or subtract dividend }
  412. if (e>0) and (sm<0) then
  413. emit_reg_reg(A_ADD,opsize,hreg1,regd)
  414. else if (e<0) and (sm>0) then
  415. emit_reg_reg(A_SUB,opsize,hreg1,regd);
  416. { shift if necessary }
  417. if (s<>0) then
  418. emit_const_reg(A_SAR,opsize,s,regd);
  419. { extract and add the sign bit }
  420. if (e<0) then
  421. emit_reg_reg(A_MOV,opsize,regd,hreg1);
  422. { if e>=0, hreg1 still contains dividend }
  423. emit_const_reg(A_SHR,opsize,left.resultdef.size*8-1,hreg1);
  424. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  425. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  426. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  427. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register)
  428. end
  429. else
  430. begin
  431. d:=tordconstnode(right).value.svalue;
  432. if d>=aword(1) shl (left.resultdef.size*8-1) then
  433. begin
  434. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  435. { Ensure that the whole register is 0, since SETcc only sets the lowest byte }
  436. { If the operands are 64 bits, this XOR routine will be shrunk by the
  437. peephole optimizer. [Kit] }
  438. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  439. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  440. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in CMP }
  441. begin
  442. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  443. emit_const_reg(A_MOV,opsize,aint(d),hreg2);
  444. emit_reg_reg(A_CMP,opsize,hreg2,hreg1);
  445. end
  446. else
  447. emit_const_reg(A_CMP,opsize,aint(d),hreg1);
  448. { NOTE: SBB and SETAE are both 3 bytes long without the REX prefix,
  449. both use an ALU for their execution and take a single cycle to
  450. run. The only difference is that SETAE does not modify the flags,
  451. allowing for some possible reuse. [Kit] }
  452. { Emit a SETcc instruction that depends on the carry bit being zero,
  453. that is, the numerator is greater than or equal to the denominator. }
  454. tempreg:=cg.makeregsize(current_asmdata.CurrAsmList,location.register,OS_8);
  455. instr:=TAiCpu.op_reg(A_SETcc,S_B,tempreg);
  456. instr.condition:=C_AE;
  457. current_asmdata.CurrAsmList.concat(instr);
  458. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  459. end
  460. else
  461. begin
  462. calc_divconst_magic_unsigned(resultdef.size*8,d,m,m_add,s);
  463. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  464. emit_const_reg(A_MOV,opsize,aint(m),rega);
  465. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  466. emit_reg(A_MUL,opsize,hreg1);
  467. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  468. if m_add then
  469. begin
  470. { addition can overflow, shift first bit considering carry,
  471. then shift remaining bits in regular way. }
  472. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  473. emit_const_reg(A_RCR,opsize,1,regd);
  474. dec(s);
  475. end;
  476. if s<>0 then
  477. emit_const_reg(A_SHR,opsize,aint(s),regd);
  478. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  479. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  480. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register)
  481. end;
  482. end;
  483. end;
  484. end
  485. else if (nodetype=modn) and (right.nodetype=ordconstn) and not(is_signed(left.resultdef)) then
  486. begin
  487. { unsigned modulus by a (+/-)power-of-2 constant? }
  488. if isabspowerof2(tordconstnode(right).value,power) then
  489. begin
  490. emit_const_reg(A_AND,opsize,(aint(1) shl power)-1,hreg1);
  491. location.register:=hreg1;
  492. end
  493. else
  494. begin
  495. d:=tordconstnode(right).value.svalue;
  496. if d>=aword(1) shl (left.resultdef.size*8-1) then
  497. begin
  498. if not (CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) then
  499. goto DefaultDiv;
  500. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  501. hreg3:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  502. m := aword(-aint(d)); { Two's complement of d }
  503. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in CMP }
  504. begin
  505. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  506. emit_const_reg(A_MOV,opsize,aint(d),hreg2);
  507. emit_const_reg(A_MOV,opsize,aint(m),hreg3);
  508. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  509. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  510. emit_reg_reg(A_CMP,opsize,hreg2,hreg1);
  511. end
  512. else
  513. begin
  514. emit_const_reg(A_MOV,opsize,aint(m),hreg3);
  515. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  516. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  517. emit_const_reg(A_CMP,opsize,aint(d),hreg1);
  518. end;
  519. { Emit conditional move that depends on the carry flag being zero,
  520. that is, the comparison result is above or equal }
  521. instr:=TAiCpu.op_reg_reg(A_CMOVcc,opsize,hreg3,location.register);
  522. instr.condition := C_AE;
  523. current_asmdata.CurrAsmList.concat(instr);
  524. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  525. emit_reg_reg(A_ADD,opsize,hreg1,location.register);
  526. end
  527. else
  528. begin
  529. { Convert the division to a multiplication }
  530. calc_divconst_magic_unsigned(resultdef.size*8,d,m,m_add,s);
  531. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  532. emit_const_reg(A_MOV,opsize,aint(m),rega);
  533. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  534. emit_reg(A_MUL,opsize,hreg1);
  535. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  536. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  537. emit_reg_reg(A_MOV,opsize,hreg1,hreg2);
  538. if m_add then
  539. begin
  540. { addition can overflow, shift first bit considering carry,
  541. then shift remaining bits in regular way. }
  542. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  543. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  544. emit_const_reg(A_RCR,opsize,1,regd);
  545. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  546. dec(s);
  547. end;
  548. if s<>0 then
  549. emit_const_reg(A_SHR,opsize,aint(s),regd); { R/EDX now contains the quotient }
  550. { Now multiply the quotient by the original denominator and
  551. subtract the product from the original numerator to get
  552. the remainder. }
  553. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in IMUL }
  554. begin
  555. hreg3:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  556. emit_const_reg(A_MOV,opsize,aint(d),hreg3);
  557. emit_reg_reg(A_IMUL,opsize,hreg3,regd);
  558. end
  559. else
  560. emit_const_reg(A_IMUL,opsize,aint(d),regd);
  561. emit_reg_reg(A_SUB,opsize,regd,hreg2);
  562. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  563. location.register:=hreg2;
  564. end;
  565. end;
  566. end
  567. else if (nodetype=modn) and (right.nodetype=ordconstn) and (is_signed(left.resultdef)) and isabspowerof2(tordconstnode(right).value,power) then
  568. begin
  569. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  570. if power=1 then
  571. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-power,hreg1,hreg2)
  572. else
  573. begin
  574. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,resultdef.size*8-1,hreg1,hreg2);
  575. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-power,hreg2,hreg2);
  576. end;
  577. emit_reg_reg(A_ADD,opsize,hreg1,hreg2);
  578. emit_const_reg(A_AND,opsize,not((aint(1) shl power)-1),hreg2);
  579. emit_reg_reg(A_SUB,opsize,hreg2,hreg1);
  580. location.register:=hreg1;
  581. end
  582. else
  583. begin
  584. DefaultDiv:
  585. {Bring denominator to a register.}
  586. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  587. emit_reg_reg(A_MOV,opsize,hreg1,rega);
  588. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  589. {Sign extension depends on the left type.}
  590. if is_signed(left.resultdef) then
  591. case left.resultdef.size of
  592. {$ifdef x86_64}
  593. 8:
  594. emit_none(A_CQO,S_NO);
  595. {$endif x86_64}
  596. 4:
  597. emit_none(A_CDQ,S_NO);
  598. else
  599. internalerror(2013102704);
  600. end
  601. else
  602. emit_reg_reg(A_XOR,opsize,regd,regd);
  603. { Division depends on the result type }
  604. if is_signed(resultdef) then
  605. op:=A_IDIV
  606. else
  607. op:=A_DIV;
  608. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  609. emit_ref(op,opsize,right.location.reference)
  610. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  611. emit_reg(op,opsize,right.location.register)
  612. else
  613. begin
  614. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,right.location.size);
  615. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,right.resultdef,right.location,hreg1);
  616. emit_reg(op,opsize,hreg1);
  617. end;
  618. { Copy the result into a new register. Release R/EAX & R/EDX.}
  619. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  620. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  621. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  622. if nodetype=divn then
  623. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,rega,location.register)
  624. else
  625. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register);
  626. end;
  627. end;
  628. {$ifdef SUPPORT_MMX}
  629. procedure tx86shlshrnode.second_mmx;
  630. var
  631. op : TAsmOp;
  632. mmxbase : tmmxtype;
  633. hregister : tregister;
  634. begin
  635. secondpass(left);
  636. if codegenerror then
  637. exit;
  638. secondpass(right);
  639. if codegenerror then
  640. exit;
  641. op:=A_NOP;
  642. mmxbase:=mmx_type(left.resultdef);
  643. location_reset(location,LOC_MMXREGISTER,def_cgsize(resultdef));
  644. case nodetype of
  645. shrn :
  646. case mmxbase of
  647. mmxs16bit,mmxu16bit,mmxfixed16:
  648. op:=A_PSRLW;
  649. mmxs32bit,mmxu32bit:
  650. op:=A_PSRLD;
  651. mmxs64bit,mmxu64bit:
  652. op:=A_PSRLQ;
  653. else
  654. Internalerror(2018022504);
  655. end;
  656. shln :
  657. case mmxbase of
  658. mmxs16bit,mmxu16bit,mmxfixed16:
  659. op:=A_PSLLW;
  660. mmxs32bit,mmxu32bit:
  661. op:=A_PSLLD;
  662. mmxs64bit,mmxu64bit:
  663. op:=A_PSLLD;
  664. else
  665. Internalerror(2018022503);
  666. end;
  667. else
  668. internalerror(2018022502);
  669. end;
  670. { left and right no register? }
  671. { then one must be demanded }
  672. if (left.location.loc<>LOC_MMXREGISTER) then
  673. begin
  674. { register variable ? }
  675. if (left.location.loc=LOC_CMMXREGISTER) then
  676. begin
  677. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  678. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  679. end
  680. else
  681. begin
  682. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  683. internalerror(2018022505);
  684. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  685. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  686. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  687. end;
  688. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  689. left.location.register:=hregister;
  690. end;
  691. { at this point, left.location.loc should be LOC_MMXREGISTER }
  692. case right.location.loc of
  693. LOC_MMXREGISTER,LOC_CMMXREGISTER:
  694. begin
  695. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  696. location.register:=left.location.register;
  697. end;
  698. LOC_CONSTANT:
  699. emit_const_reg(op,S_NO,right.location.value,left.location.register);
  700. LOC_REFERENCE,LOC_CREFERENCE:
  701. begin
  702. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  703. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  704. end;
  705. else
  706. internalerror(2018022506);
  707. end;
  708. location.register:=left.location.register;
  709. location_freetemp(current_asmdata.CurrAsmList,right.location);
  710. end;
  711. {$endif SUPPORT_MMX}
  712. end.