n386mat.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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_2;override;
  25. end;
  26. ti386shlshrnode = class(tshlshrnode)
  27. procedure pass_2;override;
  28. { everything will be handled in pass_2 }
  29. function first_shlshr64bitint: tnode; override;
  30. end;
  31. ti386unaryminusnode = class(tx86unaryminusnode)
  32. end;
  33. ti386notnode = class(tx86notnode)
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,
  38. cutils,verbose,globals,
  39. symconst,symdef,aasmbase,aasmtai,defutil,
  40. cgbase,pass_2,
  41. ncon,
  42. cpubase,cpuinfo,
  43. cga,ncgutil,cgobj,cgutils;
  44. {*****************************************************************************
  45. TI386MODDIVNODE
  46. *****************************************************************************}
  47. procedure ti386moddivnode.pass_2;
  48. var hreg1,hreg2:Tregister;
  49. power:longint;
  50. hl:Tasmlabel;
  51. op:Tasmop;
  52. begin
  53. secondpass(left);
  54. if codegenerror then
  55. exit;
  56. secondpass(right);
  57. if codegenerror then
  58. exit;
  59. if is_64bitint(resulttype.def) then
  60. { should be handled in pass_1 (JM) }
  61. internalerror(200109052);
  62. { put numerator in register }
  63. location_reset(location,LOC_REGISTER,OS_INT);
  64. location_force_reg(exprasmlist,left.location,OS_INT,false);
  65. hreg1:=left.location.register;
  66. if (nodetype=divn) and (right.nodetype=ordconstn) and
  67. ispowerof2(tordconstnode(right).value,power) then
  68. begin
  69. { for signed numbers, the numerator must be adjusted before the
  70. shift instruction, but not wih unsigned numbers! Otherwise,
  71. "Cardinal($ffffffff) div 16" overflows! (JM) }
  72. if is_signed(left.resulttype.def) Then
  73. begin
  74. if (aktOptProcessor <> class386) and
  75. not(cs_littlesize in aktglobalswitches) then
  76. { use a sequence without jumps, saw this in
  77. comp.compilers (JM) }
  78. begin
  79. { no jumps, but more operations }
  80. hreg2:=cg.getintregister(exprasmlist,OS_INT);
  81. emit_reg_reg(A_MOV,S_L,hreg1,hreg2);
  82. {If the left value is signed, hreg2=$ffffffff, otherwise 0.}
  83. emit_const_reg(A_SAR,S_L,31,hreg2);
  84. {If signed, hreg2=right value-1, otherwise 0.}
  85. emit_const_reg(A_AND,S_L,tordconstnode(right).value-1,hreg2);
  86. { add to the left value }
  87. emit_reg_reg(A_ADD,S_L,hreg2,hreg1);
  88. { do the shift }
  89. emit_const_reg(A_SAR,S_L,power,hreg1);
  90. end
  91. else
  92. begin
  93. { a jump, but less operations }
  94. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  95. objectlibrary.getlabel(hl);
  96. cg.a_jmp_flags(exprasmlist,F_NS,hl);
  97. if power=1 then
  98. emit_reg(A_INC,S_L,hreg1)
  99. else
  100. emit_const_reg(A_ADD,S_L,tordconstnode(right).value-1,hreg1);
  101. cg.a_label(exprasmlist,hl);
  102. emit_const_reg(A_SAR,S_L,power,hreg1);
  103. end
  104. end
  105. else
  106. emit_const_reg(A_SHR,S_L,power,hreg1);
  107. location.register:=hreg1;
  108. end
  109. else
  110. begin
  111. cg.getcpuregister(exprasmlist,NR_EAX);
  112. emit_reg_reg(A_MOV,S_L,hreg1,NR_EAX);
  113. cg.getcpuregister(exprasmlist,NR_EDX);
  114. {Sign extension depends on the left type.}
  115. if torddef(left.resulttype.def).typ=u32bit then
  116. emit_reg_reg(A_XOR,S_L,NR_EDX,NR_EDX)
  117. else
  118. emit_none(A_CDQ,S_NO);
  119. {Division depends on the right type.}
  120. if Torddef(right.resulttype.def).typ=u32bit then
  121. op:=A_DIV
  122. else
  123. op:=A_IDIV;
  124. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  125. emit_ref(op,S_L,right.location.reference)
  126. else if right.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  127. emit_reg(op,S_L,right.location.register)
  128. else
  129. begin
  130. hreg1:=cg.getintregister(exprasmlist,right.location.size);
  131. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,hreg1);
  132. emit_reg(op,S_L,hreg1);
  133. end;
  134. {Copy the result into a new register. Release EAX & EDX.}
  135. cg.ungetcpuregister(exprasmlist,NR_EDX);
  136. cg.ungetcpuregister(exprasmlist,NR_EAX);
  137. location.register:=cg.getintregister(exprasmlist,OS_INT);
  138. if nodetype=divn then
  139. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,NR_EAX,location.register)
  140. else
  141. cg.a_load_reg_reg(exprasmlist,OS_INT,OS_INT,NR_EDX,location.register);
  142. end;
  143. end;
  144. {*****************************************************************************
  145. TI386SHLRSHRNODE
  146. *****************************************************************************}
  147. function ti386shlshrnode.first_shlshr64bitint: tnode;
  148. begin
  149. result := nil;
  150. end;
  151. procedure ti386shlshrnode.pass_2;
  152. var hreg64hi,hreg64lo:Tregister;
  153. op:Tasmop;
  154. v : TConstExprInt;
  155. l1,l2,l3:Tasmlabel;
  156. begin
  157. secondpass(left);
  158. secondpass(right);
  159. { determine operator }
  160. if nodetype=shln then
  161. op:=A_SHL
  162. else
  163. op:=A_SHR;
  164. if is_64bitint(left.resulttype.def) then
  165. begin
  166. location_reset(location,LOC_REGISTER,OS_64);
  167. { load left operator in a register }
  168. location_force_reg(exprasmlist,left.location,OS_64,false);
  169. hreg64hi:=left.location.register64.reghi;
  170. hreg64lo:=left.location.register64.reglo;
  171. { shifting by a constant directly coded: }
  172. if (right.nodetype=ordconstn) then
  173. begin
  174. v:=Tordconstnode(right).value and 63;
  175. if v>31 then
  176. begin
  177. if nodetype=shln then
  178. begin
  179. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  180. if ((v and 31) <> 0) then
  181. emit_const_reg(A_SHL,S_L,v and 31,hreg64lo);
  182. end
  183. else
  184. begin
  185. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  186. if ((v and 31) <> 0) then
  187. emit_const_reg(A_SHR,S_L,v and 31,hreg64hi);
  188. end;
  189. location.register64.reghi:=hreg64lo;
  190. location.register64.reglo:=hreg64hi;
  191. end
  192. else
  193. begin
  194. if nodetype=shln then
  195. begin
  196. emit_const_reg_reg(A_SHLD,S_L,v and 31,hreg64lo,hreg64hi);
  197. emit_const_reg(A_SHL,S_L,v and 31,hreg64lo);
  198. end
  199. else
  200. begin
  201. emit_const_reg_reg(A_SHRD,S_L,v and 31,hreg64hi,hreg64lo);
  202. emit_const_reg(A_SHR,S_L,v and 31,hreg64hi);
  203. end;
  204. location.register64.reglo:=hreg64lo;
  205. location.register64.reghi:=hreg64hi;
  206. end;
  207. end
  208. else
  209. begin
  210. { load right operators in a register }
  211. cg.getcpuregister(exprasmlist,NR_ECX);
  212. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,NR_ECX);
  213. { left operator is already in a register }
  214. { hence are both in a register }
  215. { is it in the case ECX ? }
  216. { the damned shift instructions work only til a count of 32 }
  217. { so we've to do some tricks here }
  218. objectlibrary.getlabel(l1);
  219. objectlibrary.getlabel(l2);
  220. objectlibrary.getlabel(l3);
  221. emit_const_reg(A_CMP,S_L,64,NR_ECX);
  222. cg.a_jmp_flags(exprasmlist,F_L,l1);
  223. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  224. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  225. cg.a_jmp_always(exprasmlist,l3);
  226. cg.a_label(exprasmlist,l1);
  227. emit_const_reg(A_CMP,S_L,32,NR_ECX);
  228. cg.a_jmp_flags(exprasmlist,F_L,l2);
  229. emit_const_reg(A_SUB,S_L,32,NR_ECX);
  230. if nodetype=shln then
  231. begin
  232. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  233. emit_reg_reg(A_MOV,S_L,hreg64lo,hreg64hi);
  234. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  235. cg.a_jmp_always(exprasmlist,l3);
  236. cg.a_label(exprasmlist,l2);
  237. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hreg64lo,hreg64hi);
  238. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  239. end
  240. else
  241. begin
  242. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  243. emit_reg_reg(A_MOV,S_L,hreg64hi,hreg64lo);
  244. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  245. cg.a_jmp_always(exprasmlist,l3);
  246. cg.a_label(exprasmlist,l2);
  247. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hreg64hi,hreg64lo);
  248. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  249. end;
  250. cg.a_label(exprasmlist,l3);
  251. cg.ungetcpuregister(exprasmlist,NR_ECX);
  252. location.register64.reglo:=hreg64lo;
  253. location.register64.reghi:=hreg64hi;
  254. end;
  255. end
  256. else
  257. begin
  258. { load left operators in a register }
  259. location_copy(location,left.location);
  260. location_force_reg(exprasmlist,location,OS_INT,false);
  261. { shifting by a constant directly coded: }
  262. if (right.nodetype=ordconstn) then
  263. { l shl 32 should 0 imho, but neither TP nor Delphi do it in this way (FK)}
  264. emit_const_reg(op,S_L,tordconstnode(right).value and 31,location.register)
  265. else
  266. begin
  267. { load right operators in a ECX }
  268. cg.getcpuregister(exprasmlist,NR_ECX);
  269. cg.a_load_loc_reg(exprasmlist,OS_32,right.location,NR_ECX);
  270. { right operand is in ECX }
  271. cg.ungetcpuregister(exprasmlist,NR_ECX);
  272. emit_reg_reg(op,S_L,NR_CL,location.register);
  273. end;
  274. end;
  275. end;
  276. begin
  277. cunaryminusnode:=ti386unaryminusnode;
  278. cmoddivnode:=ti386moddivnode;
  279. cshlshrnode:=ti386shlshrnode;
  280. cnotnode:=ti386notnode;
  281. end.