nx86mat.pas 10 KB

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