n8086mat.pas 20 KB

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