n386mat.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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(tx86moddivnode)
  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:Tregister;
  50. power:longint;
  51. hl:Tasmlabel;
  52. begin
  53. if is_64bitint(resultdef) then
  54. { should be handled in pass_1 (JM) }
  55. internalerror(200109052);
  56. if (nodetype=divn) and (right.nodetype=ordconstn) and
  57. is_signed(left.resultdef) and
  58. ispowerof2(tordconstnode(right).value.svalue,power) and
  59. ((current_settings.optimizecputype = cpu_386) or
  60. (cs_opt_size in current_settings.optimizerswitches)) then
  61. begin
  62. { signed divide-by-power-of-two optimized for size }
  63. secondpass(left);
  64. if codegenerror then
  65. exit;
  66. secondpass(right);
  67. if codegenerror then
  68. exit;
  69. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  70. hreg1:=left.location.register;
  71. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  72. current_asmdata.getjumplabel(hl);
  73. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NS,hl);
  74. if power=1 then
  75. emit_reg(A_INC,S_L,hreg1)
  76. else
  77. emit_const_reg(A_ADD,S_L,tordconstnode(right).value.svalue-1,hreg1);
  78. cg.a_label(current_asmdata.CurrAsmList,hl);
  79. emit_const_reg(A_SAR,S_L,power,hreg1);
  80. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  81. location.register:=hreg1;
  82. end
  83. else
  84. inherited pass_generate_code;
  85. end;
  86. {*****************************************************************************
  87. TI386SHLRSHRNODE
  88. *****************************************************************************}
  89. function ti386shlshrnode.first_shlshr64bitint: tnode;
  90. begin
  91. result := nil;
  92. end;
  93. procedure ti386shlshrnode.second_64bit;
  94. var
  95. hreg64hi,hreg64lo:Tregister;
  96. v : TConstExprInt;
  97. l1,l2,l3:Tasmlabel;
  98. begin
  99. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  100. { load left operator in a register }
  101. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  102. hreg64hi:=left.location.register64.reghi;
  103. hreg64lo:=left.location.register64.reglo;
  104. { shifting by a constant directly coded: }
  105. if (right.nodetype=ordconstn) then
  106. begin
  107. v:=Tordconstnode(right).value and 63;
  108. if v>31 then
  109. begin
  110. if nodetype=shln then
  111. begin
  112. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  113. if ((v and 31) <> 0) then
  114. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  115. end
  116. else
  117. begin
  118. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  119. if ((v and 31) <> 0) then
  120. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  121. end;
  122. location.register64.reghi:=hreg64lo;
  123. location.register64.reglo:=hreg64hi;
  124. end
  125. else
  126. begin
  127. if nodetype=shln then
  128. begin
  129. emit_const_reg_reg(A_SHLD,S_L,v.svalue and 31,hreg64lo,hreg64hi);
  130. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  131. end
  132. else
  133. begin
  134. emit_const_reg_reg(A_SHRD,S_L,v.svalue and 31,hreg64hi,hreg64lo);
  135. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  136. end;
  137. location.register64.reglo:=hreg64lo;
  138. location.register64.reghi:=hreg64hi;
  139. end;
  140. end
  141. else
  142. begin
  143. { load right operators in a register }
  144. cg.getcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  145. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u32inttype,right.location,NR_ECX);
  146. { left operator is already in a register }
  147. { hence are both in a register }
  148. { is it in the case ECX ? }
  149. { the damned shift instructions work only til a count of 32 }
  150. { so we've to do some tricks here }
  151. current_asmdata.getjumplabel(l1);
  152. current_asmdata.getjumplabel(l2);
  153. current_asmdata.getjumplabel(l3);
  154. emit_const_reg(A_CMP,S_L,64,NR_ECX);
  155. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_L,l1);
  156. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  157. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  158. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  159. cg.a_label(current_asmdata.CurrAsmList,l1);
  160. emit_const_reg(A_CMP,S_L,32,NR_ECX);
  161. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_L,l2);
  162. emit_const_reg(A_SUB,S_L,32,NR_ECX);
  163. if nodetype=shln then
  164. begin
  165. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  166. emit_reg_reg(A_MOV,S_L,hreg64lo,hreg64hi);
  167. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  168. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  169. cg.a_label(current_asmdata.CurrAsmList,l2);
  170. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hreg64lo,hreg64hi);
  171. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  172. end
  173. else
  174. begin
  175. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  176. emit_reg_reg(A_MOV,S_L,hreg64hi,hreg64lo);
  177. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  178. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  179. cg.a_label(current_asmdata.CurrAsmList,l2);
  180. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hreg64hi,hreg64lo);
  181. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  182. end;
  183. cg.a_label(current_asmdata.CurrAsmList,l3);
  184. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  185. location.register64.reglo:=hreg64lo;
  186. location.register64.reghi:=hreg64hi;
  187. end;
  188. end;
  189. begin
  190. cunaryminusnode:=ti386unaryminusnode;
  191. cmoddivnode:=ti386moddivnode;
  192. cshlshrnode:=ti386shlshrnode;
  193. cnotnode:=ti386notnode;
  194. end.