n386mat.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. hlcgobj;
  44. {*****************************************************************************
  45. TI386MODDIVNODE
  46. *****************************************************************************}
  47. procedure ti386moddivnode.pass_generate_code;
  48. var
  49. hreg1,hreg2:Tregister;
  50. power:longint;
  51. hl:Tasmlabel;
  52. op:Tasmop;
  53. e : longint;
  54. d,m: dword;
  55. s: byte;
  56. sm: aint;
  57. m_add: boolean;
  58. begin
  59. secondpass(left);
  60. if codegenerror then
  61. exit;
  62. secondpass(right);
  63. if codegenerror then
  64. exit;
  65. if is_64bitint(resultdef) then
  66. { should be handled in pass_1 (JM) }
  67. internalerror(200109052);
  68. { put numerator in register }
  69. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  70. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  71. hreg1:=left.location.register;
  72. if (nodetype=divn) and (right.nodetype=ordconstn) then
  73. begin
  74. if ispowerof2(tordconstnode(right).value.svalue,power) then
  75. begin
  76. { for signed numbers, the numerator must be adjusted before the
  77. shift instruction, but not wih unsigned numbers! Otherwise,
  78. "Cardinal($ffffffff) div 16" overflows! (JM) }
  79. if is_signed(left.resultdef) Then
  80. begin
  81. if (current_settings.optimizecputype <> cpu_386) and
  82. not(cs_opt_size in current_settings.optimizerswitches) then
  83. { use a sequence without jumps, saw this in
  84. comp.compilers (JM) }
  85. begin
  86. { no jumps, but more operations }
  87. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  88. emit_reg_reg(A_MOV,S_L,hreg1,hreg2);
  89. {If the left value is signed, hreg2=$ffffffff, otherwise 0.}
  90. emit_const_reg(A_SAR,S_L,31,hreg2);
  91. {If signed, hreg2=right value-1, otherwise 0.}
  92. emit_const_reg(A_AND,S_L,tordconstnode(right).value.svalue-1,hreg2);
  93. { add to the left value }
  94. emit_reg_reg(A_ADD,S_L,hreg2,hreg1);
  95. { do the shift }
  96. emit_const_reg(A_SAR,S_L,power,hreg1);
  97. end
  98. else
  99. begin
  100. { a jump, but less operations }
  101. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  102. current_asmdata.getjumplabel(hl);
  103. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NS,hl);
  104. if power=1 then
  105. emit_reg(A_INC,S_L,hreg1)
  106. else
  107. emit_const_reg(A_ADD,S_L,tordconstnode(right).value.svalue-1,hreg1);
  108. cg.a_label(current_asmdata.CurrAsmList,hl);
  109. emit_const_reg(A_SAR,S_L,power,hreg1);
  110. end
  111. end
  112. else
  113. emit_const_reg(A_SHR,S_L,power,hreg1);
  114. location.register:=hreg1;
  115. end
  116. else
  117. begin
  118. if is_signed(left.resultdef) then
  119. begin
  120. e:=tordconstnode(right).value.svalue;
  121. calc_divconst_magic_signed(32,e,sm,s);
  122. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  123. emit_const_reg(A_MOV,S_L,sm,NR_EAX);
  124. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  125. emit_reg(A_IMUL,S_L,hreg1);
  126. { only the high half of result is used }
  127. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  128. { add or subtract dividend }
  129. if (e>0) and (sm<0) then
  130. emit_reg_reg(A_ADD,S_L,hreg1,NR_EDX)
  131. else if (e<0) and (sm>0) then
  132. emit_reg_reg(A_SUB,S_L,hreg1,NR_EDX);
  133. { shift if necessary }
  134. if (s<>0) then
  135. emit_const_reg(A_SAR,S_L,s,NR_EDX);
  136. { extract and add the sign bit }
  137. if (e<0) then
  138. emit_reg_reg(A_MOV,S_L,NR_EDX,hreg1);
  139. { if e>=0, hreg1 still contains dividend }
  140. emit_const_reg(A_SHR,S_L,31,hreg1);
  141. emit_reg_reg(A_ADD,S_L,hreg1,NR_EDX);
  142. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  143. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  144. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EDX,location.register)
  145. end
  146. else
  147. begin
  148. d:=tordconstnode(right).value.svalue;
  149. if d>=$80000000 then
  150. begin
  151. emit_const_reg(A_CMP,S_L,aint(d),hreg1);
  152. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  153. emit_const_reg(A_MOV,S_L,0,location.register);
  154. emit_const_reg(A_SBB,S_L,-1,location.register);
  155. end
  156. else
  157. begin
  158. calc_divconst_magic_unsigned(32,d,m,m_add,s);
  159. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  160. emit_const_reg(A_MOV,S_L,aint(m),NR_EAX);
  161. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  162. emit_reg(A_MUL,S_L,hreg1);
  163. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  164. if m_add then
  165. begin
  166. emit_reg_reg(A_SUB,S_L,NR_EDX,hreg1);
  167. emit_const_reg(A_SHR,S_L,1,hreg1);
  168. emit_reg_reg(A_ADD,S_L,hreg1,NR_EDX);
  169. dec(s);
  170. end;
  171. if s<>0 then
  172. emit_const_reg(A_SHR,S_L,aint(s),NR_EDX);
  173. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  174. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  175. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EDX,location.register)
  176. end;
  177. end
  178. end
  179. end
  180. else
  181. begin
  182. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  183. emit_reg_reg(A_MOV,S_L,hreg1,NR_EAX);
  184. cg.getcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  185. {Sign extension depends on the left type.}
  186. if torddef(left.resultdef).ordtype=u32bit then
  187. emit_reg_reg(A_XOR,S_L,NR_EDX,NR_EDX)
  188. else
  189. emit_none(A_CDQ,S_NO);
  190. {Division depends on the right type.}
  191. if Torddef(right.resultdef).ordtype=u32bit then
  192. op:=A_DIV
  193. else
  194. op:=A_IDIV;
  195. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  196. emit_ref(op,S_L,right.location.reference)
  197. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  198. emit_reg(op,S_L,right.location.register)
  199. else
  200. begin
  201. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,right.location.size);
  202. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u32inttype,right.location,hreg1);
  203. emit_reg(op,S_L,hreg1);
  204. end;
  205. {Copy the result into a new register. Release EAX & EDX.}
  206. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EDX);
  207. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_EAX);
  208. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  209. if nodetype=divn then
  210. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EAX,location.register)
  211. else
  212. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,NR_EDX,location.register);
  213. end;
  214. end;
  215. {*****************************************************************************
  216. TI386SHLRSHRNODE
  217. *****************************************************************************}
  218. function ti386shlshrnode.first_shlshr64bitint: tnode;
  219. begin
  220. result := nil;
  221. end;
  222. procedure ti386shlshrnode.second_64bit;
  223. var
  224. hreg64hi,hreg64lo:Tregister;
  225. v : TConstExprInt;
  226. l1,l2,l3:Tasmlabel;
  227. begin
  228. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  229. { load left operator in a register }
  230. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  231. hreg64hi:=left.location.register64.reghi;
  232. hreg64lo:=left.location.register64.reglo;
  233. { shifting by a constant directly coded: }
  234. if (right.nodetype=ordconstn) then
  235. begin
  236. v:=Tordconstnode(right).value and 63;
  237. if v>31 then
  238. begin
  239. if nodetype=shln then
  240. begin
  241. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  242. if ((v and 31) <> 0) then
  243. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  244. end
  245. else
  246. begin
  247. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  248. if ((v and 31) <> 0) then
  249. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  250. end;
  251. location.register64.reghi:=hreg64lo;
  252. location.register64.reglo:=hreg64hi;
  253. end
  254. else
  255. begin
  256. if nodetype=shln then
  257. begin
  258. emit_const_reg_reg(A_SHLD,S_L,v.svalue and 31,hreg64lo,hreg64hi);
  259. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  260. end
  261. else
  262. begin
  263. emit_const_reg_reg(A_SHRD,S_L,v.svalue and 31,hreg64hi,hreg64lo);
  264. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  265. end;
  266. location.register64.reglo:=hreg64lo;
  267. location.register64.reghi:=hreg64hi;
  268. end;
  269. end
  270. else
  271. begin
  272. { load right operators in a register }
  273. cg.getcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  274. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u32inttype,right.location,NR_ECX);
  275. { left operator is already in a register }
  276. { hence are both in a register }
  277. { is it in the case ECX ? }
  278. { the damned shift instructions work only til a count of 32 }
  279. { so we've to do some tricks here }
  280. current_asmdata.getjumplabel(l1);
  281. current_asmdata.getjumplabel(l2);
  282. current_asmdata.getjumplabel(l3);
  283. emit_const_reg(A_CMP,S_L,64,NR_ECX);
  284. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_L,l1);
  285. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  286. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  287. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  288. cg.a_label(current_asmdata.CurrAsmList,l1);
  289. emit_const_reg(A_CMP,S_L,32,NR_ECX);
  290. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_L,l2);
  291. emit_const_reg(A_SUB,S_L,32,NR_ECX);
  292. if nodetype=shln then
  293. begin
  294. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  295. emit_reg_reg(A_MOV,S_L,hreg64lo,hreg64hi);
  296. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  297. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  298. cg.a_label(current_asmdata.CurrAsmList,l2);
  299. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hreg64lo,hreg64hi);
  300. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  301. end
  302. else
  303. begin
  304. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  305. emit_reg_reg(A_MOV,S_L,hreg64hi,hreg64lo);
  306. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  307. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  308. cg.a_label(current_asmdata.CurrAsmList,l2);
  309. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hreg64hi,hreg64lo);
  310. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  311. end;
  312. cg.a_label(current_asmdata.CurrAsmList,l3);
  313. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  314. location.register64.reglo:=hreg64lo;
  315. location.register64.reghi:=hreg64hi;
  316. end;
  317. end;
  318. begin
  319. cunaryminusnode:=ti386unaryminusnode;
  320. cmoddivnode:=ti386moddivnode;
  321. cshlshrnode:=ti386shlshrnode;
  322. cnotnode:=ti386notnode;
  323. end.