n8086mat.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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(tx86shlshrnode)
  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. invertsign: Boolean;
  83. begin
  84. secondpass(left);
  85. if codegenerror then
  86. exit;
  87. secondpass(right);
  88. if codegenerror then
  89. exit;
  90. if is_64bitint(resultdef) or is_32bitint(resultdef) then
  91. { should be handled in pass_1 (JM) }
  92. internalerror(200109052);
  93. { put numerator in register }
  94. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  95. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  96. hreg1:=left.location.register;
  97. if (nodetype=divn) and (right.nodetype=ordconstn) then
  98. begin
  99. if isabspowerof2(tordconstnode(right).value,power) then
  100. begin
  101. { for signed numbers, the numerator must be adjusted before the
  102. shift instruction, but not wih unsigned numbers! Otherwise,
  103. "Cardinal($ffffffff) div 16" overflows! (JM) }
  104. if is_signed(left.resultdef) Then
  105. begin
  106. invertsign:=tordconstnode(right).value<0;
  107. if (current_settings.optimizecputype > cpu_386) and
  108. not(cs_opt_size in current_settings.optimizerswitches) then
  109. { use a sequence without jumps, saw this in
  110. comp.compilers (JM) }
  111. begin
  112. { no jumps, but more operations }
  113. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  114. emit_reg_reg(A_MOV,S_W,hreg1,hreg2);
  115. if power=1 then
  116. begin
  117. {If the left value is negative, hreg2=(1 shl power)-1=1, otherwise 0.}
  118. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,OS_16,15,hreg2);
  119. end
  120. else
  121. begin
  122. {If the left value is negative, hreg2=$ffff, otherwise 0.}
  123. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,15,hreg2);
  124. {If negative, hreg2=(1 shl power)-1, otherwise 0.}
  125. emit_const_reg(A_AND,S_W,(aint(1) shl power)-1,hreg2);
  126. end;
  127. { add to the left value }
  128. emit_reg_reg(A_ADD,S_W,hreg2,hreg1);
  129. { do the shift }
  130. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,power,hreg1);
  131. end
  132. else
  133. begin
  134. { a jump, but less operations }
  135. emit_reg_reg(A_TEST,S_W,hreg1,hreg1);
  136. current_asmdata.getjumplabel(hl);
  137. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NS,hl);
  138. if power=1 then
  139. emit_reg(A_INC,S_W,hreg1)
  140. else
  141. emit_const_reg(A_ADD,S_W,(aint(1) shl power)-1,hreg1);
  142. cg.a_label(current_asmdata.CurrAsmList,hl);
  143. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,power,hreg1);
  144. end;
  145. if invertsign then
  146. emit_reg(A_NEG,S_W,hreg1);
  147. end
  148. else
  149. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,OS_16,power,hreg1);
  150. location.register:=hreg1;
  151. end
  152. else
  153. begin
  154. if is_signed(left.resultdef) then
  155. begin
  156. e:=tordconstnode(right).value.svalue;
  157. d:=abs(e);
  158. { Determine algorithm (a), multiplier (m), and shift factor (s) for 16-bit
  159. signed integer division. Based on: Granlund, T.; Montgomery, P.L.:
  160. "Division by Invariant Integers using Multiplication". SIGPLAN Notices,
  161. Vol. 29, June 1994, page 61.
  162. }
  163. l:=log2(d);
  164. j:=dword($8000) mod dword(d);
  165. k:=(dword(1) shl (16+l)) div (dword($8000-j));
  166. m_low:=((dword(1)) shl (16+l)) div d;
  167. m_high:=(((dword(1)) shl (16+l)) + k) div d;
  168. while ((m_low shr 1) < (m_high shr 1)) and (l > 0) do
  169. begin
  170. m_low:=m_low shr 1;
  171. m_high:=m_high shr 1;
  172. dec(l);
  173. end;
  174. m:=word(m_high);
  175. s:=l;
  176. if (m_high shr 15)<>0 then
  177. a:=1
  178. else
  179. a:=0;
  180. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  181. emit_const_reg(A_MOV,S_W,aint(m),NR_AX);
  182. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  183. emit_reg(A_IMUL,S_W,hreg1);
  184. emit_reg_reg(A_MOV,S_W,hreg1,NR_AX);
  185. if a<>0 then
  186. begin
  187. emit_reg_reg(A_ADD,S_W,NR_AX,NR_DX);
  188. {
  189. printf ("; dividend: memory location or register other than AX or DX\n");
  190. printf ("\n");
  191. printf ("MOV AX, 0%08LXh\n", m);
  192. printf ("IMUL dividend\n");
  193. printf ("MOV AX, dividend\n");
  194. printf ("ADD DX, AX\n");
  195. if (s) printf ("SAR DX, %d\n", s);
  196. printf ("SHR AX, 15\n");
  197. printf ("ADD DX, AX\n");
  198. if (e < 0) printf ("NEG DX\n");
  199. printf ("\n");
  200. printf ("; quotient now in DX\n");
  201. }
  202. end;
  203. {
  204. printf ("; dividend: memory location of register other than AX or DX\n");
  205. printf ("\n");
  206. printf ("MOV AX, 0%08LXh\n", m);
  207. printf ("IMUL dividend\n");
  208. printf ("MOV AX, dividend\n");
  209. if (s) printf ("SAR DX, %d\n", s);
  210. printf ("SHR AX, 15\n");
  211. printf ("ADD DX, AX\n");
  212. if (e < 0) printf ("NEG DX\n");
  213. printf ("\n");
  214. printf ("; quotient now in DX\n");
  215. }
  216. if s<>0 then
  217. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SAR,OS_16,s,NR_DX);
  218. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,OS_16,15,NR_AX);
  219. emit_reg_reg(A_ADD,S_W,NR_AX,NR_DX);
  220. if e<0 then
  221. emit_reg(A_NEG,S_W,NR_DX);
  222. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  223. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  224. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  225. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,location.register)
  226. end
  227. else
  228. begin
  229. d:=tordconstnode(right).value.svalue;
  230. if d>=$8000 then
  231. begin
  232. emit_const_reg(A_CMP,S_W,aint(d),hreg1);
  233. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  234. emit_const_reg(A_MOV,S_W,0,location.register);
  235. emit_const_reg(A_SBB,S_W,-1,location.register);
  236. end
  237. else
  238. begin
  239. { Reduce divisor until it becomes odd }
  240. n:=0;
  241. t:=d;
  242. while (t and 1)=0 do
  243. begin
  244. t:=t shr 1;
  245. inc(n);
  246. end;
  247. { Generate m, s for algorithm 0. Based on: Granlund, T.; Montgomery,
  248. P.L.: "Division by Invariant Integers using Multiplication".
  249. SIGPLAN Notices, Vol. 29, June 1994, page 61.
  250. }
  251. l:=log2(t)+1;
  252. j:=dword($ffff) mod dword(t);
  253. k:=(dword(1) shl (16+l)) div (dword($ffff-j));
  254. m_low:=((dword(1)) shl (16+l)) div t;
  255. m_high:=(((dword(1)) shl (16+l)) + k) div t;
  256. while ((m_low shr 1) < (m_high shr 1)) and (l>0) do
  257. begin
  258. m_low:=m_low shr 1;
  259. m_high:=m_high shr 1;
  260. l:=l-1;
  261. end;
  262. if (m_high shr 16)=0 then
  263. begin
  264. m:=word(m_high);
  265. s:=l;
  266. a:=0;
  267. end
  268. { Generate m, s for algorithm 1. Based on: Magenheimer, D.J.; et al:
  269. "Integer Multiplication and Division on the HP Precision Architecture".
  270. IEEE Transactions on Computers, Vol 37, No. 8, August 1988, page 980.
  271. }
  272. else
  273. begin
  274. s:=log2(t);
  275. m_low:=(dword(1) shl (16+s)) div dword(t);
  276. r:=word(((dword(1)) shl (16+s)) mod dword(t));
  277. if (r < ((t>>1)+1)) then
  278. m:=word(m_low)
  279. else
  280. m:=word(m_low)+1;
  281. a:=1;
  282. end;
  283. { Reduce multiplier for either algorithm to smallest possible }
  284. while (m and 1)=0 do
  285. begin
  286. m:=m shr 1;
  287. dec(s);
  288. end;
  289. { Adjust multiplier for reduction of even divisors }
  290. inc(s,n);
  291. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  292. emit_const_reg(A_MOV,S_W,aint(m),NR_AX);
  293. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  294. emit_reg(A_MUL,S_W,hreg1);
  295. if a<>0 then
  296. begin
  297. {
  298. printf ("; dividend: register other than AX or memory location\n");
  299. printf ("\n");
  300. printf ("MOV AX, 0%08lXh\n", m);
  301. printf ("MUL dividend\n");
  302. printf ("ADD AX, 0%08lXh\n", m);
  303. printf ("ADC DX, 0\n");
  304. if (s) printf ("SHR DX, %d\n", s);
  305. printf ("\n");
  306. printf ("; quotient now in DX\n");
  307. }
  308. emit_const_reg(A_ADD,S_W,aint(m),NR_AX);
  309. emit_const_reg(A_ADC,S_W,0,NR_DX);
  310. end;
  311. if s<>0 then
  312. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHR,OS_16,aint(s),NR_DX);
  313. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  314. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  315. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  316. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,location.register)
  317. end;
  318. end
  319. end
  320. end
  321. else
  322. begin
  323. cg.getcpuregister(current_asmdata.CurrAsmList,NR_AX);
  324. emit_reg_reg(A_MOV,S_W,hreg1,NR_AX);
  325. cg.getcpuregister(current_asmdata.CurrAsmList,NR_DX);
  326. {Sign extension depends on the left type.}
  327. if torddef(left.resultdef).ordtype=u16bit then
  328. emit_reg_reg(A_XOR,S_W,NR_DX,NR_DX)
  329. else
  330. emit_none(A_CWD,S_NO);
  331. {Division depends on the right type.}
  332. if Torddef(right.resultdef).ordtype=u16bit then
  333. op:=A_DIV
  334. else
  335. op:=A_IDIV;
  336. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  337. emit_ref(op,S_W,right.location.reference)
  338. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  339. emit_reg(op,S_W,right.location.register)
  340. else
  341. begin
  342. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,right.location.size);
  343. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u16inttype,right.location,hreg1);
  344. emit_reg(op,S_W,hreg1);
  345. end;
  346. {Copy the result into a new register. Release AX & DX.}
  347. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_DX);
  348. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_AX);
  349. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  350. if nodetype=divn then
  351. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_AX,location.register)
  352. else
  353. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_DX,location.register);
  354. end;
  355. end;
  356. {*****************************************************************************
  357. TI8086SHLRSHRNODE
  358. *****************************************************************************}
  359. function ti8086shlshrnode.first_shlshr64bitint: tnode;
  360. begin
  361. result := nil;
  362. end;
  363. procedure ti8086shlshrnode.second_64bit;
  364. var
  365. hreg64hi,hreg64lo:Tregister;
  366. v : TConstExprInt;
  367. tmpreg64: tregister64;
  368. begin
  369. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  370. { load left operator in a register }
  371. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  372. hreg64hi:=left.location.register64.reghi;
  373. hreg64lo:=left.location.register64.reglo;
  374. location.register64.reglo:=hreg64lo;
  375. location.register64.reghi:=hreg64hi;
  376. if right.nodetype=ordconstn then
  377. begin
  378. v:=Tordconstnode(right).value and 63;
  379. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  380. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  381. if nodetype=shln then
  382. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_64,v,left.location.register64,location.register64)
  383. else
  384. cg64.a_op64_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_64,v,left.location.register64,location.register64);
  385. end
  386. else
  387. begin
  388. { load right operators in a register }
  389. tmpreg64.reghi:=NR_NO;
  390. tmpreg64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  391. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u16inttype,right.location,tmpreg64.reglo);
  392. if nodetype=shln then
  393. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_64,tmpreg64,location.register64)
  394. else
  395. cg64.a_op64_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_64,tmpreg64,location.register64);
  396. end;
  397. end;
  398. begin
  399. cunaryminusnode:=ti8086unaryminusnode;
  400. cmoddivnode:=ti8086moddivnode;
  401. cshlshrnode:=ti8086shlshrnode;
  402. cnotnode:=ti8086notnode;
  403. end.