n8086mat.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i8086 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 n8086mat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat,nx86mat;
  22. type
  23. ti8086moddivnode = class(tmoddivnode)
  24. procedure pass_generate_code;override;
  25. end;
  26. ti8086shlshrnode = class(tcgshlshrnode)
  27. procedure second_64bit;override;
  28. function first_shlshr64bitint: tnode; override;
  29. end;
  30. ti8086unaryminusnode = class(tx86unaryminusnode)
  31. end;
  32. ti8086notnode = class(tx86notnode)
  33. end;
  34. implementation
  35. uses
  36. globtype,systems,constexp,
  37. cutils,verbose,globals,
  38. symconst,symdef,aasmbase,aasmtai,aasmdata,aasmcpu,defutil,
  39. cgbase,pass_2,
  40. ncon,
  41. cpubase,cpuinfo,
  42. cga,ncgutil,cgobj,cgutils,
  43. hlcgobj;
  44. {*****************************************************************************
  45. ti8086moddivnode
  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 ti8086moddivnode.pass_generate_code;
  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(resultdef) then
  74. { should be handled in pass_1 (JM) }
  75. internalerror(200109052);
  76. { put numerator in register }
  77. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  78. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  79. hreg1:=left.location.register;
  80. if (nodetype=divn) and (right.nodetype=ordconstn) then
  81. begin
  82. if ispowerof2(tordconstnode(right).value.svalue,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.resultdef) Then
  88. begin
  89. if (current_settings.optimizecputype <> cpu_386) and
  90. not(cs_opt_size in current_settings.optimizerswitches) 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(current_asmdata.CurrAsmList,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.svalue-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. current_asmdata.getjumplabel(hl);
  111. cg.a_jmp_flags(current_asmdata.CurrAsmList,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.svalue-1,hreg1);
  116. cg.a_label(current_asmdata.CurrAsmList,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.resultdef) then
  127. begin
  128. e:=tordconstnode(right).value.svalue;
  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:=dword(m_high);
  147. s:=l;
  148. if (m_high shr 31)<>0 then
  149. a:=1
  150. else
  151. a:=0;
  152. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  153. emit_const_reg(A_MOV,S_L,aint(m),NR_EAX);
  154. cg.getcpuregister(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList,NR_EDX);
  195. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  196. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  197. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EDX,location.register)
  198. end
  199. else
  200. begin
  201. d:=tordconstnode(right).value.svalue;
  202. if d>=$80000000 then
  203. begin
  204. emit_const_reg(A_CMP,S_L,aint(d),hreg1);
  205. location.register:=cg.getintregister(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList,NR_EAX);
  264. emit_const_reg(A_MOV,S_L,aint(m),NR_EAX);
  265. cg.getcpuregister(current_asmdata.CurrAsmList,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(current_asmdata.CurrAsmList,NR_EDX);
  286. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  287. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  288. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EDX,location.register)
  289. end;
  290. end
  291. end
  292. end
  293. else
  294. begin
  295. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  296. emit_reg_reg(A_MOV,S_L,hreg1,NR_EAX);
  297. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  298. {Sign extension depends on the left type.}
  299. if torddef(left.resultdef).ordtype=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.resultdef).ordtype=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(current_asmdata.CurrAsmList,right.location.size);
  315. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u32inttype,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(current_asmdata.CurrAsmList,NR_EDX);
  320. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  321. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  322. if nodetype=divn then
  323. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EAX,location.register)
  324. else
  325. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EDX,location.register);
  326. end;
  327. end;
  328. {*****************************************************************************
  329. TI8086SHLRSHRNODE
  330. *****************************************************************************}
  331. function ti8086shlshrnode.first_shlshr64bitint: tnode;
  332. begin
  333. result := nil;
  334. end;
  335. procedure ti8086shlshrnode.second_64bit;
  336. var
  337. hreg64hi,hreg64lo:Tregister;
  338. v : TConstExprInt;
  339. l1,l2,l3:Tasmlabel;
  340. ai: taicpu;
  341. begin
  342. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  343. { load left operator in a register }
  344. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  345. hreg64hi:=left.location.register64.reghi;
  346. hreg64lo:=left.location.register64.reglo;
  347. if right.nodetype=ordconstn then
  348. v:=Tordconstnode(right).value and 63;
  349. { shifting by 0 directly coded: }
  350. if (right.nodetype=ordconstn) and (v=0) then
  351. begin
  352. { ultra hyper fast shift by 0 }
  353. end
  354. { shifting by 1 directly coded: }
  355. else if (right.nodetype=ordconstn) and (v=1) then
  356. begin
  357. if nodetype=shln then
  358. begin
  359. emit_const_reg(A_SHL,S_W,1,hreg64lo);
  360. emit_const_reg(A_RCL,S_W,1,GetNextReg(hreg64lo));
  361. emit_const_reg(A_RCL,S_W,1,hreg64hi);
  362. emit_const_reg(A_RCL,S_W,1,GetNextReg(hreg64hi));
  363. end
  364. else
  365. begin
  366. emit_const_reg(A_SHR,S_W,1,GetNextReg(hreg64hi));
  367. emit_const_reg(A_RCR,S_W,1,hreg64hi);
  368. emit_const_reg(A_RCR,S_W,1,GetNextReg(hreg64lo));
  369. emit_const_reg(A_RCR,S_W,1,hreg64lo);
  370. end;
  371. end
  372. else
  373. begin
  374. { load right operators in a register }
  375. cg.getcpuregister(current_asmdata.CurrAsmList,NR_CX);
  376. { shifting by a constant? }
  377. if right.nodetype=ordconstn then
  378. begin
  379. v:=Tordconstnode(right).value and 63;
  380. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,u16inttype,v,NR_CX);
  381. end
  382. else
  383. begin
  384. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u16inttype,right.location,NR_CX);
  385. { left operator is already in a register }
  386. { hence are both in a register }
  387. { is it in the case CX ? }
  388. end;
  389. current_asmdata.getjumplabel(l1);
  390. current_asmdata.getjumplabel(l2);
  391. current_asmdata.getjumplabel(l3);
  392. { for consts, we don't need the extra checks for 0 or >= 64, since
  393. we've already handled them earlier as a special case }
  394. if right.nodetype<>ordconstn then
  395. begin
  396. emit_const_reg(A_CMP,S_L,64,NR_CX);
  397. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_L,l1);
  398. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,hreg64lo,hreg64lo);
  399. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_XOR,OS_32,hreg64hi,hreg64hi);
  400. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  401. cg.a_label(current_asmdata.CurrAsmList,l1);
  402. emit_reg_reg(A_TEST,S_W,NR_CX,NR_CX);
  403. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l3);
  404. end;
  405. cg.a_label(current_asmdata.CurrAsmList,l2);
  406. if nodetype=shln then
  407. begin
  408. emit_const_reg(A_SHL,S_W,1,hreg64lo);
  409. emit_const_reg(A_RCL,S_W,1,GetNextReg(hreg64lo));
  410. emit_const_reg(A_RCL,S_W,1,hreg64hi);
  411. emit_const_reg(A_RCL,S_W,1,GetNextReg(hreg64hi));
  412. end
  413. else
  414. begin
  415. emit_const_reg(A_SHR,S_W,1,GetNextReg(hreg64hi));
  416. emit_const_reg(A_RCR,S_W,1,hreg64hi);
  417. emit_const_reg(A_RCR,S_W,1,GetNextReg(hreg64lo));
  418. emit_const_reg(A_RCR,S_W,1,hreg64lo);
  419. end;
  420. ai:=Taicpu.Op_Sym(A_LOOP,S_W,l2);
  421. ai.is_jmp := True;
  422. current_asmdata.CurrAsmList.Concat(ai);
  423. cg.a_label(current_asmdata.CurrAsmList,l3);
  424. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_CX);
  425. end;
  426. location.register64.reglo:=hreg64lo;
  427. location.register64.reghi:=hreg64hi;
  428. end;
  429. begin
  430. cunaryminusnode:=ti8086unaryminusnode;
  431. cmoddivnode:=ti8086moddivnode;
  432. cshlshrnode:=ti8086shlshrnode;
  433. cnotnode:=ti8086notnode;
  434. end.