nx86mat.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 code 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 nx86mat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat;
  22. type
  23. tx86unaryminusnode = class(tcgunaryminusnode)
  24. {$ifdef SUPPORT_MMX}
  25. procedure second_mmx;override;
  26. {$endif SUPPORT_MMX}
  27. procedure second_float;override;
  28. function pass_1:tnode;override;
  29. end;
  30. tx86notnode = class(tcgnotnode)
  31. procedure second_boolean;override;
  32. {$ifdef SUPPORT_MMX}
  33. procedure second_mmx;override;
  34. {$endif SUPPORT_MMX}
  35. end;
  36. implementation
  37. uses
  38. globtype,
  39. systems,
  40. cutils,verbose,globals,
  41. symconst,aasmbase,aasmtai,defutil,
  42. cgbase,pass_1,pass_2,
  43. ncon,
  44. cpubase,
  45. cga,ncgutil,cgobj,cgx86,cgutils;
  46. {*****************************************************************************
  47. TI386UNARYMINUSNODE
  48. *****************************************************************************}
  49. function tx86unaryminusnode.pass_1 : tnode;
  50. begin
  51. result:=nil;
  52. firstpass(left);
  53. if codegenerror then
  54. exit;
  55. if (left.resulttype.def.deftype=floatdef) then
  56. begin
  57. if use_sse(left.resulttype.def) then
  58. begin
  59. if (registersmm < 1) then
  60. registersmm := 1;
  61. expectloc:=LOC_MMREGISTER;
  62. end
  63. else
  64. begin
  65. if (registersfpu < 1) then
  66. registersfpu := 1;
  67. expectloc:=LOC_FPUREGISTER;
  68. end;
  69. end
  70. {$ifdef SUPPORT_MMX}
  71. else
  72. if (cs_mmx in aktlocalswitches) and
  73. is_mmx_able_array(left.resulttype.def) then
  74. begin
  75. registersint:=left.registersint;
  76. registersfpu:=left.registersfpu;
  77. registersmmx:=left.registersmmx;
  78. if (left.location.loc<>LOC_MMXREGISTER) and
  79. (registersmmx<1) then
  80. registersmmx:=1;
  81. end
  82. {$endif SUPPORT_MMX}
  83. else
  84. inherited pass_1;
  85. end;
  86. {$ifdef SUPPORT_MMX}
  87. procedure tx86unaryminusnode.second_mmx;
  88. var
  89. op : tasmop;
  90. hreg : tregister;
  91. begin
  92. secondpass(left);
  93. location_reset(location,LOC_MMXREGISTER,OS_NO);
  94. hreg:=tcgx86(cg).getmmxregister(exprasmlist);
  95. emit_reg_reg(A_PXOR,S_NO,hreg,hreg);
  96. case left.location.loc of
  97. LOC_MMXREGISTER:
  98. begin
  99. location.register:=left.location.register;
  100. end;
  101. LOC_CMMXREGISTER:
  102. begin
  103. location.register:=tcgx86(cg).getmmxregister(exprasmlist);
  104. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  105. end;
  106. LOC_REFERENCE,
  107. LOC_CREFERENCE:
  108. begin
  109. location.register:=tcgx86(cg).getmmxregister(exprasmlist);
  110. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  111. end;
  112. else
  113. internalerror(200203225);
  114. end;
  115. if cs_mmx_saturation in aktlocalswitches then
  116. case mmx_type(resulttype.def) of
  117. mmxs8bit:
  118. op:=A_PSUBSB;
  119. mmxu8bit:
  120. op:=A_PSUBUSB;
  121. mmxs16bit,mmxfixed16:
  122. op:=A_PSUBSW;
  123. mmxu16bit:
  124. op:=A_PSUBUSW;
  125. end
  126. else
  127. case mmx_type(resulttype.def) of
  128. mmxs8bit,mmxu8bit:
  129. op:=A_PSUBB;
  130. mmxs16bit,mmxu16bit,mmxfixed16:
  131. op:=A_PSUBW;
  132. mmxs32bit,mmxu32bit:
  133. op:=A_PSUBD;
  134. end;
  135. emit_reg_reg(op,S_NO,location.register,hreg);
  136. emit_reg_reg(A_MOVQ,S_NO,hreg,location.register);
  137. end;
  138. {$endif SUPPORT_MMX}
  139. procedure tx86unaryminusnode.second_float;
  140. var
  141. reg : tregister;
  142. href : treference;
  143. l1 : tasmlabel;
  144. begin
  145. secondpass(left);
  146. if expectloc=LOC_MMREGISTER then
  147. begin
  148. location_force_mmregscalar(exprasmlist,left.location,false);
  149. location_reset(location,LOC_MMREGISTER,def_cgsize(resulttype.def));
  150. { make life of register allocator easier }
  151. location.register:=cg.getmmregister(exprasmlist,OS_M128);
  152. cg.a_loadmm_reg_reg(exprasmlist,def_cgsize(resulttype.def),def_cgsize(resulttype.def),left.location.register,location.register,mms_movescalar);
  153. reg:=cg.getmmregister(exprasmlist,OS_M128);
  154. objectlibrary.getdatalabel(l1);
  155. consts.concat(Tai_label.Create(l1));
  156. case def_cgsize(resulttype.def) of
  157. OS_F32:
  158. consts.concat(tai_const.create_32bit(longint(1 shl 31)));
  159. OS_F64:
  160. begin
  161. consts.concat(tai_const.create_32bit(0));
  162. consts.concat(tai_const.create_32bit(-(1 shl 31)));
  163. end
  164. else
  165. internalerror(2004110215);
  166. end;
  167. reference_reset_symbol(href,l1,0);
  168. cg.a_loadmm_ref_reg(exprasmlist,def_cgsize(resulttype.def),def_cgsize(resulttype.def),href,reg,mms_movescalar);
  169. cg.a_opmm_reg_reg(exprasmlist,OP_XOR,left.location.size,reg,location.register,nil);
  170. end
  171. else
  172. begin
  173. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  174. case left.location.loc of
  175. LOC_REFERENCE,
  176. LOC_CREFERENCE:
  177. begin
  178. location.register:=NR_ST;
  179. cg.a_loadfpu_ref_reg(exprasmlist,
  180. def_cgsize(left.resulttype.def),
  181. left.location.reference,location.register);
  182. emit_none(A_FCHS,S_NO);
  183. end;
  184. LOC_FPUREGISTER,
  185. LOC_CFPUREGISTER:
  186. begin
  187. { "load st,st" is ignored by the code generator }
  188. cg.a_loadfpu_reg_reg(exprasmlist,left.location.size,left.location.register,NR_ST);
  189. location.register:=NR_ST;
  190. emit_none(A_FCHS,S_NO);
  191. end;
  192. else
  193. internalerror(200312241);
  194. end;
  195. end;
  196. end;
  197. {*****************************************************************************
  198. TX86NOTNODE
  199. *****************************************************************************}
  200. procedure tx86notnode.second_boolean;
  201. var
  202. hl : tasmlabel;
  203. opsize : tcgsize;
  204. begin
  205. opsize:=def_cgsize(resulttype.def);
  206. if left.expectloc=LOC_JUMP then
  207. begin
  208. location_reset(location,LOC_JUMP,OS_NO);
  209. hl:=truelabel;
  210. truelabel:=falselabel;
  211. falselabel:=hl;
  212. secondpass(left);
  213. maketojumpbool(exprasmlist,left,lr_load_regvars);
  214. hl:=truelabel;
  215. truelabel:=falselabel;
  216. falselabel:=hl;
  217. end
  218. else
  219. begin
  220. { the second pass could change the location of left }
  221. { if it is a register variable, so we've to do }
  222. { this before the case statement }
  223. secondpass(left);
  224. case left.expectloc of
  225. LOC_FLAGS :
  226. begin
  227. location_reset(location,LOC_FLAGS,OS_NO);
  228. location.resflags:=left.location.resflags;
  229. inverse_flags(location.resflags);
  230. end;
  231. LOC_CONSTANT,
  232. LOC_REGISTER,
  233. LOC_CREGISTER,
  234. LOC_REFERENCE,
  235. LOC_CREFERENCE :
  236. begin
  237. location_force_reg(exprasmlist,left.location,opsize,true);
  238. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  239. location_reset(location,LOC_FLAGS,OS_NO);
  240. location.resflags:=F_E;
  241. end;
  242. else
  243. internalerror(200203224);
  244. end;
  245. end;
  246. end;
  247. {$ifdef SUPPORT_MMX}
  248. procedure tx86notnode.second_mmx;
  249. var hreg,r:Tregister;
  250. begin
  251. secondpass(left);
  252. location_reset(location,LOC_MMXREGISTER,OS_NO);
  253. r:=cg.getintregister(exprasmlist,OS_INT);
  254. emit_const_reg(A_MOV,S_L,longint($ffffffff),r);
  255. { load operand }
  256. case left.location.loc of
  257. LOC_MMXREGISTER:
  258. location_copy(location,left.location);
  259. LOC_CMMXREGISTER:
  260. begin
  261. location.register:=tcgx86(cg).getmmxregister(exprasmlist);
  262. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  263. end;
  264. LOC_REFERENCE,
  265. LOC_CREFERENCE:
  266. begin
  267. location.register:=tcgx86(cg).getmmxregister(exprasmlist);
  268. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  269. end;
  270. end;
  271. { load mask }
  272. hreg:=tcgx86(cg).getmmxregister(exprasmlist);
  273. emit_reg_reg(A_MOVD,S_NO,r,hreg);
  274. { lower 32 bit }
  275. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  276. { shift mask }
  277. emit_const_reg(A_PSLLQ,S_B,32,hreg);
  278. { higher 32 bit }
  279. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  280. end;
  281. {$endif SUPPORT_MMX}
  282. end.