n386mat.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i386 assembler 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 n386mat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat,nx86mat;
  22. type
  23. ti386moddivnode = class(tmoddivnode)
  24. procedure pass_2;override;
  25. end;
  26. ti386shlshrnode = class(tshlshrnode)
  27. procedure pass_2;override;
  28. { everything will be handled in pass_2 }
  29. function first_shlshr64bitint: tnode; override;
  30. end;
  31. ti386unaryminusnode = class(tx86unaryminusnode)
  32. end;
  33. ti386notnode = class(tx86notnode)
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,
  38. cutils,verbose,globals,
  39. symconst,symdef,aasmbase,aasmtai,defutil,
  40. cgbase,pass_2,
  41. ncon,
  42. cpubase,cpuinfo,
  43. cga,ncgutil,cgobj,cgutils;
  44. {*****************************************************************************
  45. TI386MODDIVNODE
  46. *****************************************************************************}
  47. function log2(i : dword) : dword;
  48. begin
  49. result:=0;
  50. i:=i shr 1;
  51. while i<>0 do
  52. begin
  53. i:=i shr 1;
  54. inc(result);
  55. end;
  56. end;
  57. procedure ti386moddivnode.pass_2;
  58. var
  59. hreg1,hreg2:Tregister;
  60. power:longint;
  61. hl:Tasmlabel;
  62. op:Tasmop;
  63. e : longint;
  64. d,l,r,s,m,a,n,t : dword;
  65. m_low,m_high,j,k : qword;
  66. begin
  67. secondpass(left);
  68. if codegenerror then
  69. exit;
  70. secondpass(right);
  71. if codegenerror then
  72. exit;
  73. if is_64bitint(resulttype.def) then
  74. { should be handled in pass_1 (JM) }
  75. internalerror(200109052);
  76. { put numerator in register }
  77. location_reset(location,LOC_REGISTER,OS_INT);
  78. location_force_reg(exprasmlist,left.location,OS_INT,false);
  79. hreg1:=left.location.register;
  80. if (nodetype=divn) and (right.nodetype=ordconstn) then
  81. begin
  82. if ispowerof2(tordconstnode(right).value,power) then
  83. begin
  84. { for signed numbers, the numerator must be adjusted before the
  85. shift instruction, but not wih unsigned numbers! Otherwise,
  86. "Cardinal($ffffffff) div 16" overflows! (JM) }
  87. if is_signed(left.resulttype.def) Then
  88. begin
  89. if (aktOptProcessor <> class386) and
  90. not(cs_littlesize in aktglobalswitches) then
  91. { use a sequence without jumps, saw this in
  92. comp.compilers (JM) }
  93. begin
  94. { no jumps, but more operations }
  95. hreg2:=cg.getintregister(exprasmlist,OS_INT);
  96. emit_reg_reg(A_MOV,S_L,hreg1,hreg2);
  97. {If the left value is signed, hreg2=$ffffffff, otherwise 0.}
  98. emit_const_reg(A_SAR,S_L,31,hreg2);
  99. {If signed, hreg2=right value-1, otherwise 0.}
  100. emit_const_reg(A_AND,S_L,tordconstnode(right).value-1,hreg2);
  101. { add to the left value }
  102. emit_reg_reg(A_ADD,S_L,hreg2,hreg1);
  103. { do the shift }
  104. emit_const_reg(A_SAR,S_L,power,hreg1);
  105. end
  106. else
  107. begin
  108. { a jump, but less operations }
  109. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  110. objectlibrary.getjumplabel(hl);
  111. cg.a_jmp_flags(exprasmlist,F_NS,hl);
  112. if power=1 then
  113. emit_reg(A_INC,S_L,hreg1)
  114. else
  115. emit_const_reg(A_ADD,S_L,tordconstnode(right).value-1,hreg1);
  116. cg.a_label(exprasmlist,hl);
  117. emit_const_reg(A_SAR,S_L,power,hreg1);
  118. end
  119. end
  120. else
  121. emit_const_reg(A_SHR,S_L,power,hreg1);
  122. location.register:=hreg1;
  123. end
  124. else
  125. begin
  126. if is_signed(left.resulttype.def) then
  127. begin
  128. e:=tordconstnode(right).value;
  129. d:=abs(e);
  130. { Determine algorithm (a), multiplier (m), and shift factor (s) for 32-bit
  131. signed integer division. Based on: Granlund, T.; Montgomery, P.L.:
  132. "Division by Invariant Integers using Multiplication". SIGPLAN Notices,
  133. Vol. 29, June 1994, page 61.
  134. }
  135. l:=log2(d);
  136. j:=qword($80000000) mod qword(d);
  137. k:=(qword(1) shl (32+l)) div (qword($80000000-j));
  138. m_low:=((qword(1)) shl (32+l)) div d;
  139. m_high:=(((qword(1)) shl (32+l)) + k) div d;
  140. while ((m_low shr 1) < (m_high shr 1)) and (l > 0) do
  141. begin
  142. m_low:=m_low shr 1;
  143. m_high:=m_high shr 1;
  144. dec(l);
  145. end;
  146. m:=m_high;
  147. s:=l;
  148. if (m_high shr 31)<>0 then
  149. a:=1
  150. else
  151. a:=0;
  152. cg.getcpuregister(exprasmlist,NR_EAX);
  153. emit_const_reg(A_MOV,S_L,aint(m),NR_EAX);
  154. cg.getcpuregister(exprasmlist,NR_EDX);
  155. emit_reg(A_IMUL,S_L,hreg1);
  156. emit_reg_reg(A_MOV,S_L,hreg1,NR_EAX);
  157. if a<>0 then
  158. begin
  159. emit_reg_reg(A_ADD,S_L,NR_EAX,NR_EDX);
  160. {
  161. printf ("; dividend: memory location or register other than EAX or EDX\n");
  162. printf ("\n");
  163. printf ("MOV EAX, 0%08LXh\n", m);
  164. printf ("IMUL dividend\n");
  165. printf ("MOV EAX, dividend\n");
  166. printf ("ADD EDX, EAX\n");
  167. if (s) printf ("SAR EDX, %d\n", s);
  168. printf ("SHR EAX, 31\n");
  169. printf ("ADD EDX, EAX\n");
  170. if (e < 0) printf ("NEG EDX\n");
  171. printf ("\n");
  172. printf ("; quotient now in EDX\n");
  173. }
  174. end;
  175. {
  176. printf ("; dividend: memory location of register other than EAX or EDX\n");
  177. printf ("\n");
  178. printf ("MOV EAX, 0%08LXh\n", m);
  179. printf ("IMUL dividend\n");
  180. printf ("MOV EAX, dividend\n");
  181. if (s) printf ("SAR EDX, %d\n", s);
  182. printf ("SHR EAX, 31\n");
  183. printf ("ADD EDX, EAX\n");
  184. if (e < 0) printf ("NEG EDX\n");
  185. printf ("\n");
  186. printf ("; quotient now in EDX\n");
  187. }
  188. if s<>0 then
  189. emit_const_reg(A_SAR,S_L,s,NR_EDX);
  190. emit_const_reg(A_SHR,S_L,31,NR_EAX);
  191. emit_reg_reg(A_ADD,S_L,NR_EAX,NR_EDX);
  192. if e<0 then
  193. emit_reg(A_NEG,S_L,NR_EDX);
  194. cg.ungetcpuregister(exprasmlist,NR_EDX);
  195. cg.ungetcpuregister(exprasmlist,NR_EAX);
  196. location.register:=cg.getintregister(exprasmlist,OS_INT);
  197. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,NR_EDX,location.register)
  198. end
  199. else
  200. begin
  201. d:=tordconstnode(right).value;
  202. if d>=$80000000 then
  203. begin
  204. emit_const_reg(A_CMP,S_L,d,hreg1);
  205. location.register:=cg.getintregister(exprasmlist,OS_INT);
  206. emit_const_reg(A_MOV,S_L,0,location.register);
  207. emit_const_reg(A_SBB,S_L,-1,location.register);
  208. end
  209. else
  210. begin
  211. { Reduce divisor until it becomes odd }
  212. n:=0;
  213. t:=d;
  214. while (t and 1)=0 do
  215. begin
  216. t:=t shr 1;
  217. inc(n);
  218. end;
  219. { Generate m, s for algorithm 0. Based on: Granlund, T.; Montgomery,
  220. P.L.: "Division by Invariant Integers using Multiplication".
  221. SIGPLAN Notices, Vol. 29, June 1994, page 61.
  222. }
  223. l:=log2(t)+1;
  224. j:=qword($ffffffff) mod qword(t);
  225. k:=(qword(1) shl (32+l)) div (qword($ffffffff-j));
  226. m_low:=((qword(1)) shl (32+l)) div t;
  227. m_high:=(((qword(1)) shl (32+l)) + k) div t;
  228. while ((m_low shr 1) < (m_high shr 1)) and (l>0) do
  229. begin
  230. m_low:=m_low shr 1;
  231. m_high:=m_high shr 1;
  232. l:=l-1;
  233. end;
  234. if (m_high shr 32)=0 then
  235. begin
  236. m:=dword(m_high);
  237. s:=l;
  238. a:=0;
  239. end
  240. { Generate m, s for algorithm 1. Based on: Magenheimer, D.J.; et al:
  241. "Integer Multiplication and Division on the HP Precision Architecture".
  242. IEEE Transactions on Computers, Vol 37, No. 8, August 1988, page 980.
  243. }
  244. else
  245. begin
  246. s:=log2(t);
  247. m_low:=(qword(1) shl (32+s)) div qword(t);
  248. r:=dword(((qword(1)) shl (32+s)) mod qword(t));
  249. if (r < ((t>>1)+1)) then
  250. m:=dword(m_low)
  251. else
  252. m:=dword(m_low)+1;
  253. a:=1;
  254. end;
  255. { Reduce multiplier for either algorithm to smallest possible }
  256. while (m and 1)=0 do
  257. begin
  258. m:=m shr 1;
  259. dec(s);
  260. end;
  261. { Adjust multiplier for reduction of even divisors }
  262. inc(s,n);
  263. cg.getcpuregister(exprasmlist,NR_EAX);
  264. emit_const_reg(A_MOV,S_L,aint(m),NR_EAX);
  265. cg.getcpuregister(exprasmlist,NR_EDX);
  266. emit_reg(A_MUL,S_L,hreg1);
  267. if a<>0 then
  268. begin
  269. {
  270. printf ("; dividend: register other than EAX or memory location\n");
  271. printf ("\n");
  272. printf ("MOV EAX, 0%08lXh\n", m);
  273. printf ("MUL dividend\n");
  274. printf ("ADD EAX, 0%08lXh\n", m);
  275. printf ("ADC EDX, 0\n");
  276. if (s) printf ("SHR EDX, %d\n", s);
  277. printf ("\n");
  278. printf ("; quotient now in EDX\n");
  279. }
  280. emit_const_reg(A_ADD,S_L,aint(m),NR_EAX);
  281. emit_const_reg(A_ADC,S_L,0,NR_EDX);
  282. end;
  283. if s<>0 then
  284. emit_const_reg(A_SHR,S_L,aint(s),NR_EDX);
  285. cg.ungetcpuregister(exprasmlist,NR_EDX);
  286. cg.ungetcpuregister(exprasmlist,NR_EAX);
  287. location.register:=cg.getintregister(exprasmlist,OS_INT);
  288. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,NR_EDX,location.register)
  289. end;
  290. end
  291. end
  292. end
  293. else
  294. begin
  295. cg.getcpuregister(exprasmlist,NR_EAX);
  296. emit_reg_reg(A_MOV,S_L,hreg1,NR_EAX);
  297. cg.getcpuregister(exprasmlist,NR_EDX);
  298. {Sign extension depends on the left type.}
  299. if torddef(left.resulttype.def).typ=u32bit then
  300. emit_reg_reg(A_XOR,S_L,NR_EDX,NR_EDX)
  301. else
  302. emit_none(A_CDQ,S_NO);
  303. {Division depends on the right type.}
  304. if Torddef(right.resulttype.def).typ=u32bit then
  305. op:=A_DIV
  306. else
  307. op:=A_IDIV;
  308. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  309. emit_ref(op,S_L,right.location.reference)
  310. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  311. emit_reg(op,S_L,right.location.register)
  312. else
  313. begin
  314. hreg1:=cg.getintregister(exprasmlist,right.location.size);
  315. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,hreg1);
  316. emit_reg(op,S_L,hreg1);
  317. end;
  318. {Copy the result into a new register. Release EAX & EDX.}
  319. cg.ungetcpuregister(exprasmlist,NR_EDX);
  320. cg.ungetcpuregister(exprasmlist,NR_EAX);
  321. location.register:=cg.getintregister(exprasmlist,OS_INT);
  322. if nodetype=divn then
  323. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,NR_EAX,location.register)
  324. else
  325. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,NR_EDX,location.register);
  326. end;
  327. end;
  328. {*****************************************************************************
  329. TI386SHLRSHRNODE
  330. *****************************************************************************}
  331. function ti386shlshrnode.first_shlshr64bitint: tnode;
  332. begin
  333. result := nil;
  334. end;
  335. procedure ti386shlshrnode.pass_2;
  336. var hreg64hi,hreg64lo:Tregister;
  337. op:Tasmop;
  338. v : TConstExprInt;
  339. l1,l2,l3:Tasmlabel;
  340. begin
  341. secondpass(left);
  342. secondpass(right);
  343. { determine operator }
  344. if nodetype=shln then
  345. op:=A_SHL
  346. else
  347. op:=A_SHR;
  348. if is_64bitint(left.resulttype.def) then
  349. begin
  350. location_reset(location,LOC_REGISTER,OS_64);
  351. { load left operator in a register }
  352. location_force_reg(exprasmlist,left.location,OS_64,false);
  353. hreg64hi:=left.location.register64.reghi;
  354. hreg64lo:=left.location.register64.reglo;
  355. { shifting by a constant directly coded: }
  356. if (right.nodetype=ordconstn) then
  357. begin
  358. v:=Tordconstnode(right).value and 63;
  359. if v>31 then
  360. begin
  361. if nodetype=shln then
  362. begin
  363. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  364. if ((v and 31) <> 0) then
  365. emit_const_reg(A_SHL,S_L,v and 31,hreg64lo);
  366. end
  367. else
  368. begin
  369. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  370. if ((v and 31) <> 0) then
  371. emit_const_reg(A_SHR,S_L,v and 31,hreg64hi);
  372. end;
  373. location.register64.reghi:=hreg64lo;
  374. location.register64.reglo:=hreg64hi;
  375. end
  376. else
  377. begin
  378. if nodetype=shln then
  379. begin
  380. emit_const_reg_reg(A_SHLD,S_L,v and 31,hreg64lo,hreg64hi);
  381. emit_const_reg(A_SHL,S_L,v and 31,hreg64lo);
  382. end
  383. else
  384. begin
  385. emit_const_reg_reg(A_SHRD,S_L,v and 31,hreg64hi,hreg64lo);
  386. emit_const_reg(A_SHR,S_L,v and 31,hreg64hi);
  387. end;
  388. location.register64.reglo:=hreg64lo;
  389. location.register64.reghi:=hreg64hi;
  390. end;
  391. end
  392. else
  393. begin
  394. { load right operators in a register }
  395. cg.getcpuregister(exprasmlist,NR_ECX);
  396. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,NR_ECX);
  397. { left operator is already in a register }
  398. { hence are both in a register }
  399. { is it in the case ECX ? }
  400. { the damned shift instructions work only til a count of 32 }
  401. { so we've to do some tricks here }
  402. objectlibrary.getjumplabel(l1);
  403. objectlibrary.getjumplabel(l2);
  404. objectlibrary.getjumplabel(l3);
  405. emit_const_reg(A_CMP,S_L,64,NR_ECX);
  406. cg.a_jmp_flags(exprasmlist,F_L,l1);
  407. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  408. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  409. cg.a_jmp_always(exprasmlist,l3);
  410. cg.a_label(exprasmlist,l1);
  411. emit_const_reg(A_CMP,S_L,32,NR_ECX);
  412. cg.a_jmp_flags(exprasmlist,F_L,l2);
  413. emit_const_reg(A_SUB,S_L,32,NR_ECX);
  414. if nodetype=shln then
  415. begin
  416. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  417. emit_reg_reg(A_MOV,S_L,hreg64lo,hreg64hi);
  418. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  419. cg.a_jmp_always(exprasmlist,l3);
  420. cg.a_label(exprasmlist,l2);
  421. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hreg64lo,hreg64hi);
  422. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  423. end
  424. else
  425. begin
  426. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  427. emit_reg_reg(A_MOV,S_L,hreg64hi,hreg64lo);
  428. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  429. cg.a_jmp_always(exprasmlist,l3);
  430. cg.a_label(exprasmlist,l2);
  431. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hreg64hi,hreg64lo);
  432. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  433. end;
  434. cg.a_label(exprasmlist,l3);
  435. cg.ungetcpuregister(exprasmlist,NR_ECX);
  436. location.register64.reglo:=hreg64lo;
  437. location.register64.reghi:=hreg64hi;
  438. end;
  439. end
  440. else
  441. begin
  442. { load left operators in a register }
  443. location_copy(location,left.location);
  444. location_force_reg(exprasmlist,location,OS_INT,false);
  445. { shifting by a constant directly coded: }
  446. if (right.nodetype=ordconstn) then
  447. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)}
  448. emit_const_reg(op,S_L,tordconstnode(right).value and 31,location.register)
  449. else
  450. begin
  451. { load right operators in a ECX }
  452. cg.getcpuregister(exprasmlist,NR_ECX);
  453. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,NR_ECX);
  454. { right operand is in ECX }
  455. cg.ungetcpuregister(exprasmlist,NR_ECX);
  456. emit_reg_reg(op,S_L,NR_CL,location.register);
  457. end;
  458. end;
  459. end;
  460. begin
  461. cunaryminusnode:=ti386unaryminusnode;
  462. cmoddivnode:=ti386moddivnode;
  463. cshlshrnode:=ti386shlshrnode;
  464. cnotnode:=ti386notnode;
  465. end.