nx86mat.pas 32 KB

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