n386mat.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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(tx86shlshrnode)
  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. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  72. emit_reg_reg(A_TEST,S_L,hreg1,hreg1);
  73. current_asmdata.getjumplabel(hl);
  74. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NS,hl);
  75. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  76. if power=1 then
  77. emit_reg(A_INC,S_L,hreg1)
  78. else
  79. emit_const_reg(A_ADD,S_L,tordconstnode(right).value.svalue-1,hreg1);
  80. cg.a_label(current_asmdata.CurrAsmList,hl);
  81. emit_const_reg(A_SAR,S_L,power,hreg1);
  82. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  83. location.register:=hreg1;
  84. end
  85. else
  86. inherited pass_generate_code;
  87. end;
  88. {*****************************************************************************
  89. TI386SHLRSHRNODE
  90. *****************************************************************************}
  91. function ti386shlshrnode.first_shlshr64bitint: tnode;
  92. begin
  93. result := nil;
  94. end;
  95. procedure ti386shlshrnode.second_64bit;
  96. var
  97. hreg64hi,hreg64lo:Tregister;
  98. v : TConstExprInt;
  99. l2,l3:Tasmlabel;
  100. begin
  101. { shifting by a constant directly coded: }
  102. if (right.nodetype=ordconstn) then
  103. begin
  104. v:=Tordconstnode(right).value and 63;
  105. { load left operator in a register }
  106. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  107. ((left.location.loc=LOC_CREGISTER) and ((v.svalue and 31)<>0)) then
  108. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  109. hreg64hi:=left.location.register64.reghi;
  110. hreg64lo:=left.location.register64.reglo;
  111. location_reset(location,left.location.loc,def_cgsize(resultdef));
  112. if v>31 then
  113. begin
  114. if nodetype=shln then
  115. begin
  116. hreg64hi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  117. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  118. if ((v and 31) <> 0) then
  119. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  120. end
  121. else
  122. begin
  123. hreg64lo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  124. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  125. if ((v and 31) <> 0) then
  126. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  127. end;
  128. location.register64.reghi:=hreg64lo;
  129. location.register64.reglo:=hreg64hi;
  130. end
  131. else
  132. begin
  133. if (v.svalue and 31)<>0 then
  134. begin
  135. if nodetype=shln then
  136. begin
  137. emit_const_reg_reg(A_SHLD,S_L,v.svalue and 31,hreg64lo,hreg64hi);
  138. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  139. end
  140. else
  141. begin
  142. emit_const_reg_reg(A_SHRD,S_L,v.svalue and 31,hreg64hi,hreg64lo);
  143. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  144. end;
  145. end;
  146. location.register64.reglo:=hreg64lo;
  147. location.register64.reghi:=hreg64hi;
  148. end;
  149. end
  150. else
  151. begin
  152. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  153. { load left operator in a register }
  154. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  155. hreg64hi:=left.location.register64.reghi;
  156. hreg64lo:=left.location.register64.reglo;
  157. { load right operators in a register }
  158. cg.getcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  159. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u32inttype,right.location,NR_ECX);
  160. { left operator is already in a register }
  161. { hence are both in a register }
  162. { is it in the case ECX ? }
  163. { the damned shift instructions work only til a count of 32 }
  164. { so we've to do some tricks here }
  165. current_asmdata.getjumplabel(l2);
  166. current_asmdata.getjumplabel(l3);
  167. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  168. emit_const_reg(A_TEST,S_B,32,NR_CL);
  169. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l2);
  170. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  171. if nodetype=shln then
  172. begin
  173. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  174. emit_reg_reg(A_MOV,S_L,hreg64lo,hreg64hi);
  175. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  176. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  177. cg.a_label(current_asmdata.CurrAsmList,l2);
  178. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hreg64lo,hreg64hi);
  179. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  180. end
  181. else
  182. begin
  183. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  184. emit_reg_reg(A_MOV,S_L,hreg64hi,hreg64lo);
  185. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  186. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  187. cg.a_label(current_asmdata.CurrAsmList,l2);
  188. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hreg64hi,hreg64lo);
  189. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  190. end;
  191. cg.a_label(current_asmdata.CurrAsmList,l3);
  192. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  193. location.register64.reglo:=hreg64lo;
  194. location.register64.reghi:=hreg64hi;
  195. end;
  196. end;
  197. begin
  198. cunaryminusnode:=ti386unaryminusnode;
  199. cmoddivnode:=ti386moddivnode;
  200. cshlshrnode:=ti386shlshrnode;
  201. cnotnode:=ti386notnode;
  202. end.