nx86mat.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  153. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  154. { make life of register allocator easier }
  155. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  156. current_asmdata.getglobaldatalabel(l1);
  157. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(sizeof(pint)));
  158. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  159. case def_cgsize(resultdef) of
  160. OS_F32:
  161. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(longint(1 shl 31)));
  162. OS_F64:
  163. begin
  164. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(0));
  165. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(-(1 shl 31)));
  166. end
  167. else
  168. internalerror(2004110215);
  169. end;
  170. reference_reset_symbol(href,l1,0,resultdef.alignment,[]);
  171. if UseAVX then
  172. cg.a_opmm_ref_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.location.size,href,left.location.register,location.register,nil)
  173. else
  174. begin
  175. reg:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  176. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),def_cgsize(resultdef),href,reg,mms_movescalar);
  177. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),def_cgsize(resultdef),left.location.register,location.register,mms_movescalar);
  178. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.location.size,reg,location.register,nil);
  179. end;
  180. end
  181. else
  182. begin
  183. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  184. case left.location.loc of
  185. LOC_REFERENCE,
  186. LOC_CREFERENCE:
  187. begin
  188. location.register:=NR_ST;
  189. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  190. left.location.size,location.size,
  191. left.location.reference,location.register);
  192. emit_none(A_FCHS,S_NO);
  193. end;
  194. LOC_FPUREGISTER,
  195. LOC_CFPUREGISTER:
  196. begin
  197. { "load st,st" is ignored by the code generator }
  198. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,NR_ST);
  199. location.register:=NR_ST;
  200. emit_none(A_FCHS,S_NO);
  201. end;
  202. else
  203. internalerror(200312241);
  204. end;
  205. end;
  206. end;
  207. {*****************************************************************************
  208. TX86NOTNODE
  209. *****************************************************************************}
  210. procedure tx86notnode.second_boolean;
  211. var
  212. opsize : tcgsize;
  213. {$if defined(cpu32bitalu) or defined(cpu16bitalu)}
  214. hreg: tregister;
  215. {$endif}
  216. begin
  217. opsize:=def_cgsize(resultdef);
  218. if not handle_locjump then
  219. begin
  220. { the second pass could change the location of left }
  221. { if it is a register variable, so we've to do }
  222. { this before the case statement }
  223. secondpass(left);
  224. case left.location.loc of
  225. LOC_FLAGS :
  226. begin
  227. location_reset(location,LOC_FLAGS,OS_NO);
  228. location.resflags:=left.location.resflags;
  229. inverse_flags(location.resflags);
  230. end;
  231. LOC_CREFERENCE,
  232. LOC_REFERENCE:
  233. begin
  234. {$if defined(cpu32bitalu)}
  235. if is_64bit(resultdef) then
  236. begin
  237. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_32);
  238. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  239. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hreg);
  240. inc(left.location.reference.offset,4);
  241. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.reference,hreg);
  242. end
  243. else
  244. {$elseif defined(cpu16bitalu)}
  245. if is_64bit(resultdef) then
  246. begin
  247. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_16);
  248. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  249. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,hreg);
  250. inc(left.location.reference.offset,2);
  251. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  252. inc(left.location.reference.offset,2);
  253. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  254. inc(left.location.reference.offset,2);
  255. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  256. end
  257. else if is_32bit(resultdef) then
  258. begin
  259. hreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_16);
  260. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  261. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.reference,hreg);
  262. inc(left.location.reference.offset,2);
  263. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.reference,hreg);
  264. end
  265. else
  266. {$endif}
  267. emit_const_ref(A_CMP, TCGSize2Opsize[opsize], 0, left.location.reference);
  268. location_reset(location,LOC_FLAGS,OS_NO);
  269. location.resflags:=F_E;
  270. end;
  271. LOC_CONSTANT,
  272. LOC_REGISTER,
  273. LOC_CREGISTER,
  274. LOC_SUBSETREG,
  275. LOC_CSUBSETREG,
  276. LOC_SUBSETREF,
  277. LOC_CSUBSETREF :
  278. begin
  279. {$if defined(cpu32bitalu)}
  280. if is_64bit(resultdef) then
  281. begin
  282. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  283. emit_reg_reg(A_OR,S_L,left.location.register64.reghi,left.location.register64.reglo);
  284. end
  285. else
  286. {$elseif defined(cpu16bitalu)}
  287. if is_64bit(resultdef) then
  288. begin
  289. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  290. emit_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register64.reghi),left.location.register64.reghi);
  291. emit_reg_reg(A_OR,S_W,cg.GetNextReg(left.location.register64.reglo),left.location.register64.reglo);
  292. emit_reg_reg(A_OR,S_W,left.location.register64.reghi,left.location.register64.reglo);
  293. end
  294. else if is_32bit(resultdef) then
  295. begin
  296. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  297. emit_reg_reg(A_OR,S_L,cg.GetNextReg(left.location.register),left.location.register);
  298. end
  299. else
  300. {$endif}
  301. begin
  302. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,true);
  303. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  304. end;
  305. location_reset(location,LOC_FLAGS,OS_NO);
  306. location.resflags:=F_E;
  307. end;
  308. else
  309. internalerror(200203224);
  310. end;
  311. end;
  312. end;
  313. {$ifdef SUPPORT_MMX}
  314. procedure tx86notnode.second_mmx;
  315. var hreg,r:Tregister;
  316. begin
  317. secondpass(left);
  318. location_reset(location,LOC_MMXREGISTER,OS_NO);
  319. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  320. emit_const_reg(A_MOV,S_L,longint($ffffffff),r);
  321. { load operand }
  322. case left.location.loc of
  323. LOC_MMXREGISTER:
  324. location_copy(location,left.location);
  325. LOC_CMMXREGISTER:
  326. begin
  327. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  328. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  329. end;
  330. LOC_REFERENCE,
  331. LOC_CREFERENCE:
  332. begin
  333. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  334. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  335. end;
  336. else
  337. internalerror(2019050906);
  338. end;
  339. { load mask }
  340. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  341. emit_reg_reg(A_MOVD,S_NO,r,hreg);
  342. { lower 32 bit }
  343. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  344. { shift mask }
  345. emit_const_reg(A_PSLLQ,S_B,32,hreg);
  346. { higher 32 bit }
  347. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  348. end;
  349. {$endif SUPPORT_MMX}
  350. {*****************************************************************************
  351. TX86MODDIVNODE
  352. *****************************************************************************}
  353. procedure tx86moddivnode.pass_generate_code;
  354. var
  355. hreg1,hreg2,hreg3,rega,regd,tempreg:Tregister;
  356. power:longint;
  357. instr:TAiCpu;
  358. op:Tasmop;
  359. cgsize:TCgSize;
  360. opsize:topsize;
  361. e, sm: aint;
  362. d,m: aword;
  363. m_add, invertsign: boolean;
  364. s: byte;
  365. label
  366. DefaultDiv;
  367. begin
  368. secondpass(left);
  369. if codegenerror then
  370. exit;
  371. secondpass(right);
  372. if codegenerror then
  373. exit;
  374. { put numerator in register }
  375. cgsize:=def_cgsize(resultdef);
  376. opsize:=TCGSize2OpSize[cgsize];
  377. if not (cgsize in [OS_32,OS_S32,OS_64,OS_S64]) then
  378. InternalError(2013102702);
  379. rega:=newreg(R_INTREGISTER,RS_EAX,cgsize2subreg(R_INTREGISTER,cgsize));
  380. regd:=newreg(R_INTREGISTER,RS_EDX,cgsize2subreg(R_INTREGISTER,cgsize));
  381. location_reset(location,LOC_REGISTER,cgsize);
  382. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  383. hreg1:=left.location.register;
  384. if (nodetype=divn) and (right.nodetype=ordconstn) then
  385. begin
  386. if isabspowerof2(tordconstnode(right).value,power) then
  387. begin
  388. { for signed numbers, the numerator must be adjusted before the
  389. shift instruction, but not with unsigned numbers! Otherwise,
  390. "Cardinal($ffffffff) div 16" overflows! (JM) }
  391. if is_signed(left.resultdef) Then
  392. begin
  393. invertsign:=tordconstnode(right).value<0;
  394. { use a sequence without jumps, saw this in
  395. comp.compilers (JM) }
  396. { no jumps, but more operations }
  397. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  398. emit_reg_reg(A_MOV,opsize,hreg1,hreg2);
  399. if power=1 then
  400. begin
  401. {If the left value is negative, hreg2=(1 shl power)-1=1, otherwise 0.}
  402. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-1,hreg2);
  403. end
  404. else
  405. begin
  406. {If the left value is negative, hreg2=$ffffffff, otherwise 0.}
  407. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,resultdef.size*8-1,hreg2);
  408. {If negative, hreg2=(1 shl power)-1, otherwise 0.}
  409. { (don't use emit_const_reg, because if value>high(longint)
  410. then it must first be loaded into a register) }
  411. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,cgsize,(aint(1) shl power)-1,hreg2);
  412. end;
  413. { add to the left value }
  414. emit_reg_reg(A_ADD,opsize,hreg2,hreg1);
  415. { do the shift }
  416. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,power,hreg1);
  417. if invertsign then
  418. emit_reg(A_NEG,opsize,hreg1);
  419. end
  420. else
  421. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,power,hreg1);
  422. location.register:=hreg1;
  423. end
  424. else
  425. begin
  426. if is_signed(left.resultdef) then
  427. begin
  428. e:=tordconstnode(right).value.svalue;
  429. calc_divconst_magic_signed(resultdef.size*8,e,sm,s);
  430. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  431. emit_const_reg(A_MOV,opsize,sm,rega);
  432. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  433. emit_reg(A_IMUL,opsize,hreg1);
  434. { only the high half of result is used }
  435. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  436. { add or subtract dividend }
  437. if (e>0) and (sm<0) then
  438. emit_reg_reg(A_ADD,opsize,hreg1,regd)
  439. else if (e<0) and (sm>0) then
  440. emit_reg_reg(A_SUB,opsize,hreg1,regd);
  441. { shift if necessary }
  442. if (s<>0) then
  443. emit_const_reg(A_SAR,opsize,s,regd);
  444. { extract and add the sign bit }
  445. if (e<0) then
  446. emit_reg_reg(A_MOV,opsize,regd,hreg1);
  447. { if e>=0, hreg1 still contains dividend }
  448. emit_const_reg(A_SHR,opsize,left.resultdef.size*8-1,hreg1);
  449. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  450. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  451. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  452. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register)
  453. end
  454. else
  455. begin
  456. d:=tordconstnode(right).value.svalue;
  457. if d>=aword(1) shl (left.resultdef.size*8-1) then
  458. begin
  459. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  460. { Ensure that the whole register is 0, since SETcc only sets the lowest byte }
  461. { If the operands are 64 bits, this XOR routine will be shrunk by the
  462. peephole optimizer. [Kit] }
  463. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  464. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  465. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in CMP }
  466. begin
  467. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  468. emit_const_reg(A_MOV,opsize,aint(d),hreg2);
  469. emit_reg_reg(A_CMP,opsize,hreg2,hreg1);
  470. end
  471. else
  472. emit_const_reg(A_CMP,opsize,aint(d),hreg1);
  473. { NOTE: SBB and SETAE are both 3 bytes long without the REX prefix,
  474. both use an ALU for their execution and take a single cycle to
  475. run. The only difference is that SETAE does not modify the flags,
  476. allowing for some possible reuse. [Kit] }
  477. { Emit a SETcc instruction that depends on the carry bit being zero,
  478. that is, the numerator is greater than or equal to the denominator. }
  479. tempreg:=cg.makeregsize(current_asmdata.CurrAsmList,location.register,OS_8);
  480. instr:=TAiCpu.op_reg(A_SETcc,S_B,tempreg);
  481. instr.condition:=C_AE;
  482. current_asmdata.CurrAsmList.concat(instr);
  483. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  484. end
  485. else
  486. begin
  487. calc_divconst_magic_unsigned(resultdef.size*8,d,m,m_add,s);
  488. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  489. emit_const_reg(A_MOV,opsize,aint(m),rega);
  490. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  491. emit_reg(A_MUL,opsize,hreg1);
  492. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  493. if m_add then
  494. begin
  495. { addition can overflow, shift first bit considering carry,
  496. then shift remaining bits in regular way. }
  497. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  498. emit_const_reg(A_RCR,opsize,1,regd);
  499. dec(s);
  500. end;
  501. if s<>0 then
  502. emit_const_reg(A_SHR,opsize,aint(s),regd);
  503. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  504. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  505. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register)
  506. end;
  507. end;
  508. end;
  509. end
  510. else if (nodetype=modn) and (right.nodetype=ordconstn) and not(is_signed(left.resultdef)) then
  511. begin
  512. { unsigned modulus by a (+/-)power-of-2 constant? }
  513. if isabspowerof2(tordconstnode(right).value,power) then
  514. begin
  515. emit_const_reg(A_AND,opsize,(aint(1) shl power)-1,hreg1);
  516. location.register:=hreg1;
  517. end
  518. else
  519. begin
  520. d:=tordconstnode(right).value.svalue;
  521. if d>=aword(1) shl (left.resultdef.size*8-1) then
  522. begin
  523. if not (CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) then
  524. goto DefaultDiv;
  525. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  526. hreg3:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  527. m := aword(-aint(d)); { Two's complement of d }
  528. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in CMP }
  529. begin
  530. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  531. emit_const_reg(A_MOV,opsize,aint(d),hreg2);
  532. emit_const_reg(A_MOV,opsize,aint(m),hreg3);
  533. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  534. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  535. emit_reg_reg(A_CMP,opsize,hreg2,hreg1);
  536. end
  537. else
  538. begin
  539. emit_const_reg(A_MOV,opsize,aint(m),hreg3);
  540. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  541. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  542. emit_const_reg(A_CMP,opsize,aint(d),hreg1);
  543. end;
  544. { Emit conditional move that depends on the carry flag being zero,
  545. that is, the comparison result is above or equal }
  546. instr:=TAiCpu.op_reg_reg(A_CMOVcc,opsize,hreg3,location.register);
  547. instr.condition := C_AE;
  548. current_asmdata.CurrAsmList.concat(instr);
  549. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  550. emit_reg_reg(A_ADD,opsize,hreg1,location.register);
  551. end
  552. else
  553. begin
  554. { Convert the division to a multiplication }
  555. calc_divconst_magic_unsigned(resultdef.size*8,d,m,m_add,s);
  556. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  557. emit_const_reg(A_MOV,opsize,aint(m),rega);
  558. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  559. emit_reg(A_MUL,opsize,hreg1);
  560. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  561. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  562. emit_reg_reg(A_MOV,opsize,hreg1,hreg2);
  563. if m_add then
  564. begin
  565. { addition can overflow, shift first bit considering carry,
  566. then shift remaining bits in regular way. }
  567. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  568. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  569. emit_const_reg(A_RCR,opsize,1,regd);
  570. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  571. dec(s);
  572. end;
  573. if s<>0 then
  574. emit_const_reg(A_SHR,opsize,aint(s),regd); { R/EDX now contains the quotient }
  575. { Now multiply the quotient by the original denominator and
  576. subtract the product from the original numerator to get
  577. the remainder. }
  578. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in IMUL }
  579. begin
  580. hreg3:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  581. emit_const_reg(A_MOV,opsize,aint(d),hreg3);
  582. emit_reg_reg(A_IMUL,opsize,hreg3,regd);
  583. end
  584. else
  585. emit_const_reg(A_IMUL,opsize,aint(d),regd);
  586. emit_reg_reg(A_SUB,opsize,regd,hreg2);
  587. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  588. location.register:=hreg2;
  589. end;
  590. end;
  591. end
  592. else
  593. begin
  594. DefaultDiv:
  595. {Bring denominator to a register.}
  596. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  597. emit_reg_reg(A_MOV,opsize,hreg1,rega);
  598. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  599. {Sign extension depends on the left type.}
  600. if is_signed(left.resultdef) then
  601. case left.resultdef.size of
  602. {$ifdef x86_64}
  603. 8:
  604. emit_none(A_CQO,S_NO);
  605. {$endif x86_64}
  606. 4:
  607. emit_none(A_CDQ,S_NO);
  608. else
  609. internalerror(2013102701);
  610. end
  611. else
  612. emit_reg_reg(A_XOR,opsize,regd,regd);
  613. { Division depends on the result type }
  614. if is_signed(resultdef) then
  615. op:=A_IDIV
  616. else
  617. op:=A_DIV;
  618. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  619. emit_ref(op,opsize,right.location.reference)
  620. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  621. emit_reg(op,opsize,right.location.register)
  622. else
  623. begin
  624. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,right.location.size);
  625. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,right.resultdef,right.location,hreg1);
  626. emit_reg(op,opsize,hreg1);
  627. end;
  628. { Copy the result into a new register. Release R/EAX & R/EDX.}
  629. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  630. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  631. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  632. if nodetype=divn then
  633. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,rega,location.register)
  634. else
  635. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register);
  636. end;
  637. end;
  638. {$ifdef SUPPORT_MMX}
  639. procedure tx86shlshrnode.second_mmx;
  640. var
  641. op : TAsmOp;
  642. mmxbase : tmmxtype;
  643. hregister : tregister;
  644. begin
  645. secondpass(left);
  646. if codegenerror then
  647. exit;
  648. secondpass(right);
  649. if codegenerror then
  650. exit;
  651. op:=A_NOP;
  652. mmxbase:=mmx_type(left.resultdef);
  653. location_reset(location,LOC_MMXREGISTER,def_cgsize(resultdef));
  654. case nodetype of
  655. shrn :
  656. case mmxbase of
  657. mmxs16bit,mmxu16bit,mmxfixed16:
  658. op:=A_PSRLW;
  659. mmxs32bit,mmxu32bit:
  660. op:=A_PSRLD;
  661. mmxs64bit,mmxu64bit:
  662. op:=A_PSRLQ;
  663. else
  664. Internalerror(2018022504);
  665. end;
  666. shln :
  667. case mmxbase of
  668. mmxs16bit,mmxu16bit,mmxfixed16:
  669. op:=A_PSLLW;
  670. mmxs32bit,mmxu32bit:
  671. op:=A_PSLLD;
  672. mmxs64bit,mmxu64bit:
  673. op:=A_PSLLD;
  674. else
  675. Internalerror(2018022503);
  676. end;
  677. else
  678. internalerror(2018022502);
  679. end;
  680. { left and right no register? }
  681. { then one must be demanded }
  682. if (left.location.loc<>LOC_MMXREGISTER) then
  683. begin
  684. { register variable ? }
  685. if (left.location.loc=LOC_CMMXREGISTER) then
  686. begin
  687. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  688. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  689. end
  690. else
  691. begin
  692. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  693. internalerror(2018022505);
  694. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  695. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  696. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  697. end;
  698. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  699. left.location.register:=hregister;
  700. end;
  701. { at this point, left.location.loc should be LOC_MMXREGISTER }
  702. case right.location.loc of
  703. LOC_MMXREGISTER,LOC_CMMXREGISTER:
  704. begin
  705. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  706. location.register:=left.location.register;
  707. end;
  708. LOC_CONSTANT:
  709. emit_const_reg(op,S_NO,right.location.value,left.location.register);
  710. LOC_REFERENCE,LOC_CREFERENCE:
  711. begin
  712. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  713. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  714. end;
  715. else
  716. internalerror(2018022506);
  717. end;
  718. location.register:=left.location.register;
  719. location_freetemp(current_asmdata.CurrAsmList,right.location);
  720. end;
  721. {$endif SUPPORT_MMX}
  722. end.