n386mat.pas 20 KB

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