n386mat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate i386 assembler for math nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit n386mat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat,ncgmat,nx86mat;
  23. type
  24. ti386moddivnode = class(tmoddivnode)
  25. procedure pass_2;override;
  26. end;
  27. ti386shlshrnode = class(tshlshrnode)
  28. procedure pass_2;override;
  29. { everything will be handled in pass_2 }
  30. function first_shlshr64bitint: tnode; override;
  31. end;
  32. ti386unaryminusnode = class(tx86unaryminusnode)
  33. end;
  34. ti386notnode = class(tx86notnode)
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,verbose,globals,
  40. symconst,symdef,aasmbase,aasmtai,defutil,
  41. cgbase,pass_1,pass_2,
  42. ncon,
  43. cpubase,cpuinfo,
  44. cga,ncgutil,cgobj;
  45. {*****************************************************************************
  46. TI386MODDIVNODE
  47. *****************************************************************************}
  48. procedure ti386moddivnode.pass_2;
  49. var hreg1,hreg2:Tregister;
  50. power:longint;
  51. hl:Tasmlabel;
  52. op:Tasmop;
  53. begin
  54. secondpass(left);
  55. if codegenerror then
  56. exit;
  57. secondpass(right);
  58. if codegenerror then
  59. exit;
  60. if is_64bitint(resulttype.def) then
  61. { should be handled in pass_1 (JM) }
  62. internalerror(200109052);
  63. { put numerator in register }
  64. location_reset(location,LOC_REGISTER,OS_INT);
  65. location_force_reg(exprasmlist,left.location,OS_INT,false);
  66. hreg1:=left.location.register;
  67. if (nodetype=divn) and (right.nodetype=ordconstn) and
  68. ispowerof2(tordconstnode(right).value,power) then
  69. begin
  70. { for signed numbers, the numerator must be adjusted before the
  71. shift instruction, but not wih unsigned numbers! Otherwise,
  72. "Cardinal($ffffffff) div 16" overflows! (JM) }
  73. if is_signed(left.resulttype.def) Then
  74. begin
  75. if (aktOptProcessor <> class386) and
  76. not(cs_littlesize in aktglobalswitches) then
  77. { use a sequence without jumps, saw this in
  78. comp.compilers (JM) }
  79. begin
  80. { no jumps, but more operations }
  81. hreg2:=cg.getintregister(exprasmlist,OS_INT);
  82. emit_reg_reg(A_MOV,S_L,hreg1,hreg2);
  83. {If the left value is signed, hreg2=$ffffffff, otherwise 0.}
  84. emit_const_reg(A_SAR,S_L,31,hreg2);
  85. {If signed, hreg2=right value-1, otherwise 0.}
  86. emit_const_reg(A_AND,S_L,tordconstnode(right).value-1,hreg2);
  87. { add to the left value }
  88. emit_reg_reg(A_ADD,S_L,hreg2,hreg1);
  89. { release EDX if we used it }
  90. cg.ungetregister(exprasmlist,hreg2);
  91. { do the shift }
  92. emit_const_reg(A_SAR,S_L,power,hreg1);
  93. end
  94. else
  95. begin
  96. { a jump, but less operations }
  97. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  98. objectlibrary.getlabel(hl);
  99. cg.a_jmp_flags(exprasmlist,F_NS,hl);
  100. if power=1 then
  101. emit_reg(A_INC,S_L,hreg1)
  102. else
  103. emit_const_reg(A_ADD,S_L,tordconstnode(right).value-1,hreg1);
  104. cg.a_label(exprasmlist,hl);
  105. emit_const_reg(A_SAR,S_L,power,hreg1);
  106. end
  107. end
  108. else
  109. emit_const_reg(A_SHR,S_L,power,hreg1);
  110. location.register:=hreg1;
  111. end
  112. else
  113. begin
  114. {Bring denominator to a register.}
  115. cg.ungetregister(exprasmlist,hreg1);
  116. cg.getexplicitregister(exprasmlist,NR_EAX);
  117. emit_reg_reg(A_MOV,S_L,hreg1,NR_EAX);
  118. cg.getexplicitregister(exprasmlist,NR_EDX);
  119. {Sign extension depends on the left type.}
  120. if torddef(left.resulttype.def).typ=u32bit then
  121. emit_reg_reg(A_XOR,S_L,NR_EDX,NR_EDX)
  122. else
  123. emit_none(A_CDQ,S_NO);
  124. {Division depends on the right type.}
  125. if Torddef(right.resulttype.def).typ=u32bit then
  126. op:=A_DIV
  127. else
  128. op:=A_IDIV;
  129. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  130. emit_ref(op,S_L,right.location.reference)
  131. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  132. emit_reg(op,S_L,right.location.register)
  133. else
  134. begin
  135. hreg1:=cg.getintregister(exprasmlist,right.location.size);
  136. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,hreg1);
  137. cg.ungetregister(exprasmlist,hreg1);
  138. emit_reg(op,S_L,hreg1);
  139. end;
  140. location_release(exprasmlist,right.location);
  141. {Copy the result into a new register. Release EAX & EDX.}
  142. if nodetype=divn then
  143. begin
  144. cg.ungetregister(exprasmlist,NR_EDX);
  145. cg.ungetregister(exprasmlist,NR_EAX);
  146. location.register:=cg.getintregister(exprasmlist,OS_INT);
  147. emit_reg_reg(A_MOV,S_L,NR_EAX,location.register);
  148. end
  149. else
  150. begin
  151. cg.ungetregister(exprasmlist,NR_EAX);
  152. cg.ungetregister(exprasmlist,NR_EDX);
  153. location.register:=cg.getintregister(exprasmlist,OS_INT);
  154. emit_reg_reg(A_MOV,S_L,NR_EDX,location.register);
  155. end;
  156. end;
  157. end;
  158. {*****************************************************************************
  159. TI386SHLRSHRNODE
  160. *****************************************************************************}
  161. function ti386shlshrnode.first_shlshr64bitint: tnode;
  162. begin
  163. result := nil;
  164. end;
  165. procedure ti386shlshrnode.pass_2;
  166. var hregisterhigh,hregisterlow:Tregister;
  167. op:Tasmop;
  168. v : TConstExprInt;
  169. l1,l2,l3:Tasmlabel;
  170. begin
  171. secondpass(left);
  172. secondpass(right);
  173. { determine operator }
  174. if nodetype=shln then
  175. op:=A_SHL
  176. else
  177. op:=A_SHR;
  178. if is_64bitint(left.resulttype.def) then
  179. begin
  180. location_reset(location,LOC_REGISTER,OS_64);
  181. { load left operator in a register }
  182. location_force_reg(exprasmlist,left.location,OS_64,false);
  183. hregisterhigh:=left.location.registerhigh;
  184. hregisterlow:=left.location.registerlow;
  185. { shifting by a constant directly coded: }
  186. if (right.nodetype=ordconstn) then
  187. begin
  188. v:=Tordconstnode(right).value and 63;
  189. if v>31 then
  190. begin
  191. if nodetype=shln then
  192. begin
  193. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  194. if ((v and 31) <> 0) then
  195. emit_const_reg(A_SHL,S_L,v and 31,hregisterlow);
  196. end
  197. else
  198. begin
  199. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  200. if ((v and 31) <> 0) then
  201. emit_const_reg(A_SHR,S_L,v and 31,hregisterhigh);
  202. end;
  203. location.registerhigh:=hregisterlow;
  204. location.registerlow:=hregisterhigh;
  205. end
  206. else
  207. begin
  208. if nodetype=shln then
  209. begin
  210. emit_const_reg_reg(A_SHLD,S_L,v and 31,hregisterlow,hregisterhigh);
  211. emit_const_reg(A_SHL,S_L,v and 31,hregisterlow);
  212. end
  213. else
  214. begin
  215. emit_const_reg_reg(A_SHRD,S_L,v and 31,hregisterhigh,hregisterlow);
  216. emit_const_reg(A_SHR,S_L,v and 31,hregisterhigh);
  217. end;
  218. location.registerlow:=hregisterlow;
  219. location.registerhigh:=hregisterhigh;
  220. end;
  221. end
  222. else
  223. begin
  224. { load right operators in a register }
  225. cg.getexplicitregister(exprasmlist,NR_ECX);
  226. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,NR_ECX);
  227. if right.location.loc<>LOC_CREGISTER then
  228. location_release(exprasmlist,right.location);
  229. { left operator is already in a register }
  230. { hence are both in a register }
  231. { is it in the case ECX ? }
  232. { the damned shift instructions work only til a count of 32 }
  233. { so we've to do some tricks here }
  234. objectlibrary.getlabel(l1);
  235. objectlibrary.getlabel(l2);
  236. objectlibrary.getlabel(l3);
  237. emit_const_reg(A_CMP,S_L,64,NR_ECX);
  238. cg.a_jmp_flags(exprasmlist,F_L,l1);
  239. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  240. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  241. cg.a_jmp_always(exprasmlist,l3);
  242. cg.a_label(exprasmlist,l1);
  243. emit_const_reg(A_CMP,S_L,32,NR_ECX);
  244. cg.a_jmp_flags(exprasmlist,F_L,l2);
  245. emit_const_reg(A_SUB,S_L,32,NR_ECX);
  246. if nodetype=shln then
  247. begin
  248. emit_reg_reg(A_SHL,S_L,NR_CL,hregisterlow);
  249. emit_reg_reg(A_MOV,S_L,hregisterlow,hregisterhigh);
  250. emit_reg_reg(A_XOR,S_L,hregisterlow,hregisterlow);
  251. cg.a_jmp_always(exprasmlist,l3);
  252. cg.a_label(exprasmlist,l2);
  253. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hregisterlow,hregisterhigh);
  254. emit_reg_reg(A_SHL,S_L,NR_CL,hregisterlow);
  255. end
  256. else
  257. begin
  258. emit_reg_reg(A_SHR,S_L,NR_CL,hregisterhigh);
  259. emit_reg_reg(A_MOV,S_L,hregisterhigh,hregisterlow);
  260. emit_reg_reg(A_XOR,S_L,hregisterhigh,hregisterhigh);
  261. cg.a_jmp_always(exprasmlist,l3);
  262. cg.a_label(exprasmlist,l2);
  263. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hregisterhigh,hregisterlow);
  264. emit_reg_reg(A_SHR,S_L,NR_CL,hregisterhigh);
  265. end;
  266. cg.a_label(exprasmlist,l3);
  267. cg.ungetregister(exprasmlist,NR_ECX);
  268. location.registerlow:=hregisterlow;
  269. location.registerhigh:=hregisterhigh;
  270. end;
  271. end
  272. else
  273. begin
  274. { load left operators in a register }
  275. location_copy(location,left.location);
  276. location_force_reg(exprasmlist,location,OS_INT,false);
  277. { shifting by a constant directly coded: }
  278. if (right.nodetype=ordconstn) then
  279. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)}
  280. emit_const_reg(op,S_L,tordconstnode(right).value and 31,location.register)
  281. else
  282. begin
  283. { load right operators in a ECX }
  284. if right.location.loc<>LOC_CREGISTER then
  285. location_release(exprasmlist,right.location);
  286. cg.getexplicitregister(exprasmlist,NR_ECX);
  287. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,NR_ECX);
  288. { right operand is in ECX }
  289. cg.ungetregister(exprasmlist,NR_ECX);
  290. emit_reg_reg(op,S_L,NR_CL,location.register);
  291. end;
  292. end;
  293. end;
  294. begin
  295. cunaryminusnode:=ti386unaryminusnode;
  296. cmoddivnode:=ti386moddivnode;
  297. cshlshrnode:=ti386shlshrnode;
  298. cnotnode:=ti386notnode;
  299. end.
  300. {
  301. $Log$
  302. Revision 1.71 2004-06-20 08:55:31 florian
  303. * logs truncated
  304. Revision 1.70 2004/05/23 14:10:17 peter
  305. * fix shl/shr with value > 63
  306. Revision 1.69 2004/01/20 12:59:37 florian
  307. * common addnode code for x86-64 and i386
  308. }