n8086mat.pas 19 KB

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