nx86mat.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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. {$ifdef x86_64}
  505. if (cgsize in [OS_64,OS_S64]) and (d > $7FFFFFFF) then { Cannot use 64-bit constants in IMUL }
  506. begin
  507. hreg3:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  508. emit_const_reg(A_MOV,opsize,aint(d),hreg3);
  509. emit_reg_reg(A_IMUL,opsize,hreg3,regd);
  510. end
  511. else
  512. {$endif x86_64}
  513. {$endif x86_64}
  514. emit_const_reg(A_IMUL,opsize,aint(d),regd);
  515. emit_reg_reg(A_SUB,opsize,regd,hreg2);
  516. end;
  517. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  518. if not DoMod then
  519. begin
  520. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  521. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,hreg2);
  522. end;
  523. location.register:=hreg2;
  524. end;
  525. begin
  526. secondpass(left);
  527. if codegenerror then
  528. exit;
  529. secondpass(right);
  530. if codegenerror then
  531. exit;
  532. { put numerator in register }
  533. cgsize:=def_cgsize(resultdef);
  534. opsize:=TCGSize2OpSize[cgsize];
  535. rega:=newreg(R_INTREGISTER,RS_EAX,cgsize2subreg(R_INTREGISTER,cgsize));
  536. if cgsize in [OS_8,OS_S8] then
  537. regd:=NR_AH
  538. else
  539. regd:=newreg(R_INTREGISTER,RS_EDX,cgsize2subreg(R_INTREGISTER,cgsize));
  540. location_reset(location,LOC_REGISTER,cgsize);
  541. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  542. hreg1:=left.location.register;
  543. if (nodetype=divn) and (right.nodetype=ordconstn) then
  544. begin
  545. if isabspowerof2(tordconstnode(right).value,power) then
  546. begin
  547. { for signed numbers, the numerator must be adjusted before the
  548. shift instruction, but not with unsigned numbers! Otherwise,
  549. "Cardinal($ffffffff) div 16" overflows! (JM) }
  550. if is_signed(left.resultdef) Then
  551. begin
  552. invertsign:=tordconstnode(right).value<0;
  553. { use a sequence without jumps, saw this in
  554. comp.compilers (JM) }
  555. { no jumps, but more operations }
  556. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  557. emit_reg_reg(A_MOV,opsize,hreg1,hreg2);
  558. if power=1 then
  559. begin
  560. {If the left value is negative, hreg2=(1 shl power)-1=1, otherwise 0.}
  561. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-1,hreg2);
  562. end
  563. else
  564. begin
  565. {If the left value is negative, hreg2=$ffffffff, otherwise 0.}
  566. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,resultdef.size*8-1,hreg2);
  567. {If negative, hreg2=(1 shl power)-1, otherwise 0.}
  568. { (don't use emit_const_reg, because if value>high(longint)
  569. then it must first be loaded into a register) }
  570. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,cgsize,(aint(1) shl power)-1,hreg2);
  571. end;
  572. { add to the left value }
  573. emit_reg_reg(A_ADD,opsize,hreg2,hreg1);
  574. { do the shift }
  575. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,power,hreg1);
  576. if invertsign then
  577. emit_reg(A_NEG,opsize,hreg1);
  578. end
  579. else
  580. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,power,hreg1);
  581. location.register:=hreg1;
  582. end
  583. else
  584. begin
  585. if is_signed(left.resultdef) then
  586. begin
  587. e:=tordconstnode(right).value.svalue;
  588. calc_divconst_magic_signed(resultdef.size*8,e,sm,s);
  589. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  590. emit_const_reg(A_MOV,opsize,sm,rega);
  591. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  592. emit_reg(A_IMUL,opsize,hreg1);
  593. { only the high half of result is used }
  594. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  595. { add or subtract dividend }
  596. if (e>0) and (sm<0) then
  597. emit_reg_reg(A_ADD,opsize,hreg1,regd)
  598. else if (e<0) and (sm>0) then
  599. emit_reg_reg(A_SUB,opsize,hreg1,regd);
  600. { shift if necessary }
  601. if (s<>0) then
  602. emit_const_reg(A_SAR,opsize,s,regd);
  603. { extract and add the sign bit }
  604. if (e<0) then
  605. emit_reg_reg(A_MOV,opsize,regd,hreg1);
  606. { if e>=0, hreg1 still contains dividend }
  607. emit_const_reg(A_SHR,opsize,left.resultdef.size*8-1,hreg1);
  608. emit_reg_reg(A_ADD,opsize,hreg1,regd);
  609. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  610. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  611. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register)
  612. end
  613. else
  614. begin
  615. d:=tordconstnode(right).value.uvalue;
  616. if d>=aword(1) shl (left.resultdef.size*8-1) then
  617. begin
  618. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  619. { Ensure that the whole register is 0, since SETcc only sets the lowest byte }
  620. { If the operands are 64 bits, this XOR routine will be shrunk by the
  621. peephole optimizer. [Kit] }
  622. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  623. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in CMP }
  624. begin
  625. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  626. emit_const_reg(A_MOV,opsize,aint(d),hreg2);
  627. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  628. emit_reg_reg(A_CMP,opsize,hreg2,hreg1);
  629. end
  630. else
  631. begin
  632. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  633. emit_const_reg(A_CMP,opsize,aint(d),hreg1);
  634. end;
  635. { NOTE: SBB and SETAE are both 3 bytes long without the REX prefix,
  636. both use an ALU for their execution and take a single cycle to
  637. run. The only difference is that SETAE does not modify the flags,
  638. allowing for some possible reuse. [Kit] }
  639. { Emit a SETcc instruction that depends on the carry bit being zero,
  640. that is, the numerator is greater than or equal to the denominator. }
  641. tempreg:=cg.makeregsize(current_asmdata.CurrAsmList,location.register,OS_8);
  642. instr:=TAiCpu.op_reg(A_SETcc,S_B,tempreg);
  643. instr.condition:=C_AE;
  644. current_asmdata.CurrAsmList.concat(instr);
  645. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  646. end
  647. else
  648. DoUnsignedReciprocalDivision;
  649. end;
  650. end;
  651. end
  652. else if (nodetype=modn) and (right.nodetype=ordconstn) and not(is_signed(left.resultdef)) then
  653. begin
  654. { unsigned modulus by a (+/-)power-of-2 constant? }
  655. if isabspowerof2(tordconstnode(right).value,power) then
  656. begin
  657. emit_const_reg(A_AND,opsize,(aint(1) shl power)-1,hreg1);
  658. location.register:=hreg1;
  659. end
  660. else
  661. begin
  662. d:=tordconstnode(right).value.uvalue;
  663. if d>=aword(1) shl (left.resultdef.size*8-1) then
  664. begin
  665. if not (CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) then
  666. goto DefaultDiv;
  667. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  668. hreg3:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  669. m := aword(-aint(d)); { Two's complement of d }
  670. if (cgsize in [OS_64,OS_S64]) then { Cannot use 64-bit constants in CMP }
  671. begin
  672. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  673. emit_const_reg(A_MOV,opsize,aint(d),hreg2);
  674. emit_const_reg(A_MOV,opsize,aint(m),hreg3);
  675. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  676. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  677. emit_reg_reg(A_CMP,opsize,hreg2,hreg1);
  678. end
  679. else
  680. begin
  681. emit_const_reg(A_MOV,opsize,aint(m),hreg3);
  682. emit_reg_reg(A_XOR,opsize,location.register,location.register);
  683. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  684. emit_const_reg(A_CMP,opsize,aint(d),hreg1);
  685. end;
  686. { Emit conditional move that depends on the carry flag being zero,
  687. that is, the comparison result is above or equal }
  688. instr:=TAiCpu.op_reg_reg(A_CMOVcc,opsize,hreg3,location.register);
  689. instr.condition := C_AE;
  690. current_asmdata.CurrAsmList.concat(instr);
  691. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  692. emit_reg_reg(A_ADD,opsize,hreg1,location.register);
  693. end
  694. else
  695. { Convert the division to a multiplication }
  696. DoUnsignedReciprocalDivision;
  697. end;
  698. end
  699. else if (nodetype=modn) and (right.nodetype=ordconstn) and (is_signed(left.resultdef)) and isabspowerof2(tordconstnode(right).value,power) then
  700. begin
  701. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  702. if power=1 then
  703. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-power,hreg1,hreg2)
  704. else
  705. begin
  706. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SAR,cgsize,resultdef.size*8-1,hreg1,hreg2);
  707. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,cgsize,resultdef.size*8-power,hreg2,hreg2);
  708. end;
  709. emit_reg_reg(A_ADD,opsize,hreg1,hreg2);
  710. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,cgsize,not((aint(1) shl power)-1),hreg2);
  711. emit_reg_reg(A_SUB,opsize,hreg2,hreg1);
  712. location.register:=hreg1;
  713. end
  714. else
  715. begin
  716. DefaultDiv:
  717. {Bring denominator to a register.}
  718. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  719. emit_reg_reg(A_MOV,opsize,hreg1,rega);
  720. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  721. {Sign extension depends on the left type.}
  722. if is_signed(left.resultdef) then
  723. case left.resultdef.size of
  724. {$ifdef x86_64}
  725. 8:
  726. emit_none(A_CQO,S_NO);
  727. {$endif x86_64}
  728. 4:
  729. emit_none(A_CDQ,S_NO);
  730. else
  731. internalerror(2013102704);
  732. end
  733. else
  734. emit_reg_reg(A_XOR,opsize,regd,regd);
  735. { Division depends on the result type }
  736. if is_signed(resultdef) then
  737. op:=A_IDIV
  738. else
  739. op:=A_DIV;
  740. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  741. emit_ref(op,opsize,right.location.reference)
  742. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  743. emit_reg(op,opsize,right.location.register)
  744. else
  745. begin
  746. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,right.location.size);
  747. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,right.resultdef,right.location,hreg1);
  748. emit_reg(op,opsize,hreg1);
  749. end;
  750. { Copy the result into a new register. Release R/EAX & R/EDX.}
  751. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  752. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  753. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  754. if nodetype=divn then
  755. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,rega,location.register)
  756. else
  757. cg.a_load_reg_reg(current_asmdata.CurrAsmList,cgsize,cgsize,regd,location.register);
  758. end;
  759. end;
  760. {$ifdef SUPPORT_MMX}
  761. procedure tx86shlshrnode.second_mmx;
  762. var
  763. op : TAsmOp;
  764. mmxbase : tmmxtype;
  765. hregister : tregister;
  766. begin
  767. secondpass(left);
  768. if codegenerror then
  769. exit;
  770. secondpass(right);
  771. if codegenerror then
  772. exit;
  773. op:=A_NOP;
  774. mmxbase:=mmx_type(left.resultdef);
  775. location_reset(location,LOC_MMXREGISTER,def_cgsize(resultdef));
  776. case nodetype of
  777. shrn :
  778. case mmxbase of
  779. mmxs16bit,mmxu16bit,mmxfixed16:
  780. op:=A_PSRLW;
  781. mmxs32bit,mmxu32bit:
  782. op:=A_PSRLD;
  783. mmxs64bit,mmxu64bit:
  784. op:=A_PSRLQ;
  785. else
  786. Internalerror(2018022504);
  787. end;
  788. shln :
  789. case mmxbase of
  790. mmxs16bit,mmxu16bit,mmxfixed16:
  791. op:=A_PSLLW;
  792. mmxs32bit,mmxu32bit:
  793. op:=A_PSLLD;
  794. mmxs64bit,mmxu64bit:
  795. op:=A_PSLLD;
  796. else
  797. Internalerror(2018022503);
  798. end;
  799. else
  800. internalerror(2018022502);
  801. end;
  802. { left and right no register? }
  803. { then one must be demanded }
  804. if (left.location.loc<>LOC_MMXREGISTER) then
  805. begin
  806. { register variable ? }
  807. if (left.location.loc=LOC_CMMXREGISTER) then
  808. begin
  809. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  810. emit_reg_reg(A_MOVQ,S_NO,left.location.register,hregister);
  811. end
  812. else
  813. begin
  814. if not(left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  815. internalerror(2018022505);
  816. hregister:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  817. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,left.location.reference);
  818. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,hregister);
  819. end;
  820. location_reset(left.location,LOC_MMXREGISTER,OS_NO);
  821. left.location.register:=hregister;
  822. end;
  823. { at this point, left.location.loc should be LOC_MMXREGISTER }
  824. case right.location.loc of
  825. LOC_MMXREGISTER,LOC_CMMXREGISTER:
  826. begin
  827. emit_reg_reg(op,S_NO,right.location.register,left.location.register);
  828. location.register:=left.location.register;
  829. end;
  830. LOC_CONSTANT:
  831. emit_const_reg(op,S_NO,right.location.value,left.location.register);
  832. LOC_REFERENCE,LOC_CREFERENCE:
  833. begin
  834. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,right.location.reference);
  835. emit_ref_reg(op,S_NO,right.location.reference,left.location.register);
  836. end;
  837. else
  838. internalerror(2018022506);
  839. end;
  840. location.register:=left.location.register;
  841. location_freetemp(current_asmdata.CurrAsmList,right.location);
  842. end;
  843. {$endif SUPPORT_MMX}
  844. end.