2
0

n386mat.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 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. l2,l3:Tasmlabel;
  98. begin
  99. { shifting by a constant directly coded: }
  100. if (right.nodetype=ordconstn) then
  101. begin
  102. v:=Tordconstnode(right).value and 63;
  103. { load left operator in a register }
  104. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) or
  105. ((left.location.loc=LOC_CREGISTER) and ((v.svalue and 31)<>0)) then
  106. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  107. hreg64hi:=left.location.register64.reghi;
  108. hreg64lo:=left.location.register64.reglo;
  109. location_reset(location,left.location.loc,def_cgsize(resultdef));
  110. if v>31 then
  111. begin
  112. if nodetype=shln then
  113. begin
  114. hreg64hi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  115. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  116. if ((v and 31) <> 0) then
  117. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  118. end
  119. else
  120. begin
  121. hreg64lo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  122. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  123. if ((v and 31) <> 0) then
  124. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  125. end;
  126. location.register64.reghi:=hreg64lo;
  127. location.register64.reglo:=hreg64hi;
  128. end
  129. else
  130. begin
  131. if (v.svalue and 31)<>0 then
  132. begin
  133. if nodetype=shln then
  134. begin
  135. emit_const_reg_reg(A_SHLD,S_L,v.svalue and 31,hreg64lo,hreg64hi);
  136. emit_const_reg(A_SHL,S_L,v.svalue and 31,hreg64lo);
  137. end
  138. else
  139. begin
  140. emit_const_reg_reg(A_SHRD,S_L,v.svalue and 31,hreg64hi,hreg64lo);
  141. emit_const_reg(A_SHR,S_L,v.svalue and 31,hreg64hi);
  142. end;
  143. end;
  144. location.register64.reglo:=hreg64lo;
  145. location.register64.reghi:=hreg64hi;
  146. end;
  147. end
  148. else
  149. begin
  150. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  151. { load left operator in a register }
  152. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef,false);
  153. hreg64hi:=left.location.register64.reghi;
  154. hreg64lo:=left.location.register64.reglo;
  155. { load right operators in a register }
  156. cg.getcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  157. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,u32inttype,right.location,NR_ECX);
  158. { left operator is already in a register }
  159. { hence are both in a register }
  160. { is it in the case ECX ? }
  161. { the damned shift instructions work only til a count of 32 }
  162. { so we've to do some tricks here }
  163. current_asmdata.getjumplabel(l2);
  164. current_asmdata.getjumplabel(l3);
  165. emit_const_reg(A_TEST,S_B,32,NR_CL);
  166. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l2);
  167. if nodetype=shln then
  168. begin
  169. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  170. emit_reg_reg(A_MOV,S_L,hreg64lo,hreg64hi);
  171. emit_reg_reg(A_XOR,S_L,hreg64lo,hreg64lo);
  172. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  173. cg.a_label(current_asmdata.CurrAsmList,l2);
  174. emit_reg_reg_reg(A_SHLD,S_L,NR_CL,hreg64lo,hreg64hi);
  175. emit_reg_reg(A_SHL,S_L,NR_CL,hreg64lo);
  176. end
  177. else
  178. begin
  179. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  180. emit_reg_reg(A_MOV,S_L,hreg64hi,hreg64lo);
  181. emit_reg_reg(A_XOR,S_L,hreg64hi,hreg64hi);
  182. cg.a_jmp_always(current_asmdata.CurrAsmList,l3);
  183. cg.a_label(current_asmdata.CurrAsmList,l2);
  184. emit_reg_reg_reg(A_SHRD,S_L,NR_CL,hreg64hi,hreg64lo);
  185. emit_reg_reg(A_SHR,S_L,NR_CL,hreg64hi);
  186. end;
  187. cg.a_label(current_asmdata.CurrAsmList,l3);
  188. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_ECX);
  189. location.register64.reglo:=hreg64lo;
  190. location.register64.reghi:=hreg64hi;
  191. end;
  192. end;
  193. begin
  194. cunaryminusnode:=ti386unaryminusnode;
  195. cmoddivnode:=ti386moddivnode;
  196. cshlshrnode:=ti386shlshrnode;
  197. cnotnode:=ti386notnode;
  198. end.