nx86mat.pas 38 KB

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