nx86mat.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate x86 code for math nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nx86mat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat,ncgmat;
  23. type
  24. tx86unaryminusnode = class(tcgunaryminusnode)
  25. {$ifdef SUPPORT_MMX}
  26. procedure second_mmx;override;
  27. {$endif SUPPORT_MMX}
  28. procedure second_float;override;
  29. function pass_1:tnode;override;
  30. end;
  31. tx86notnode = class(tcgnotnode)
  32. procedure second_boolean;override;
  33. {$ifdef SUPPORT_MMX}
  34. procedure second_mmx;override;
  35. {$endif SUPPORT_MMX}
  36. end;
  37. implementation
  38. uses
  39. globtype,systems,
  40. cutils,verbose,globals,
  41. symconst,symdef,aasmbase,aasmtai,defutil,
  42. cgbase,pass_1,pass_2,
  43. ncon,
  44. cpubase,cpuinfo,
  45. cga,ncgutil,cgobj,cgx86;
  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:=cg.getmmxregister(exprasmlist,OS_M64);
  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:=cg.getmmxregister(exprasmlist,OS_M64);
  104. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  105. end;
  106. LOC_REFERENCE,
  107. LOC_CREFERENCE:
  108. begin
  109. reference_release(exprasmlist,left.location.reference);
  110. location.register:=cg.getmmxregister(exprasmlist,OS_M64);
  111. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  112. end;
  113. else
  114. internalerror(200203225);
  115. end;
  116. if cs_mmx_saturation in aktlocalswitches then
  117. case mmx_type(resulttype.def) of
  118. mmxs8bit:
  119. op:=A_PSUBSB;
  120. mmxu8bit:
  121. op:=A_PSUBUSB;
  122. mmxs16bit,mmxfixed16:
  123. op:=A_PSUBSW;
  124. mmxu16bit:
  125. op:=A_PSUBUSW;
  126. end
  127. else
  128. case mmx_type(resulttype.def) of
  129. mmxs8bit,mmxu8bit:
  130. op:=A_PSUBB;
  131. mmxs16bit,mmxu16bit,mmxfixed16:
  132. op:=A_PSUBW;
  133. mmxs32bit,mmxu32bit:
  134. op:=A_PSUBD;
  135. end;
  136. emit_reg_reg(op,S_NO,location.register,hreg);
  137. cg.ungetregister(exprasmlist,hreg);
  138. emit_reg_reg(A_MOVQ,S_NO,hreg,location.register);
  139. end;
  140. {$endif SUPPORT_MMX}
  141. procedure tx86unaryminusnode.second_float;
  142. var
  143. reg : tregister;
  144. begin
  145. secondpass(left);
  146. if expectloc=LOC_MMREGISTER then
  147. begin
  148. reg:=cg.getmmregister(exprasmlist,OS_M128);
  149. { zero out the register
  150. op size doesn't matter }
  151. cg.a_opmm_reg_reg(exprasmlist,OP_XOR,OS_F32,reg,reg,nil);
  152. { move to a mm compatible location }
  153. if left.location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER] then
  154. location_force_mem(exprasmlist,left.location);
  155. cg.a_opmm_loc_reg(exprasmlist,OP_SUB,left.location.size,left.location,reg,mms_movescalar);
  156. location_reset(location,LOC_MMREGISTER,def_cgsize(resulttype.def));
  157. location.register:=reg;
  158. end
  159. else
  160. begin
  161. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  162. case left.location.loc of
  163. LOC_REFERENCE,
  164. LOC_CREFERENCE:
  165. begin
  166. location.register:=NR_ST;
  167. cg.a_loadfpu_ref_reg(exprasmlist,
  168. def_cgsize(left.resulttype.def),
  169. left.location.reference,location.register);
  170. emit_none(A_FCHS,S_NO);
  171. end;
  172. LOC_FPUREGISTER,
  173. LOC_CFPUREGISTER:
  174. begin
  175. { "load st,st" is ignored by the code generator }
  176. cg.a_loadfpu_reg_reg(exprasmlist,left.location.size,left.location.register,NR_ST);
  177. location.register:=NR_ST;
  178. emit_none(A_FCHS,S_NO);
  179. end;
  180. else
  181. internalerror(200312241);
  182. end;
  183. end;
  184. end;
  185. {*****************************************************************************
  186. TX86NOTNODE
  187. *****************************************************************************}
  188. procedure tx86notnode.second_boolean;
  189. var
  190. hl : tasmlabel;
  191. opsize : tcgsize;
  192. begin
  193. opsize:=def_cgsize(resulttype.def);
  194. if left.expectloc=LOC_JUMP then
  195. begin
  196. location_reset(location,LOC_JUMP,OS_NO);
  197. hl:=truelabel;
  198. truelabel:=falselabel;
  199. falselabel:=hl;
  200. secondpass(left);
  201. maketojumpbool(exprasmlist,left,lr_load_regvars);
  202. hl:=truelabel;
  203. truelabel:=falselabel;
  204. falselabel:=hl;
  205. end
  206. else
  207. begin
  208. { the second pass could change the location of left }
  209. { if it is a register variable, so we've to do }
  210. { this before the case statement }
  211. secondpass(left);
  212. case left.expectloc of
  213. LOC_FLAGS :
  214. begin
  215. location_reset(location,LOC_FLAGS,OS_NO);
  216. location.resflags:=left.location.resflags;
  217. inverse_flags(location.resflags);
  218. end;
  219. LOC_CONSTANT,
  220. LOC_REGISTER,
  221. LOC_CREGISTER,
  222. LOC_REFERENCE,
  223. LOC_CREFERENCE :
  224. begin
  225. location_force_reg(exprasmlist,left.location,opsize,true);
  226. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  227. location_reset(location,LOC_FLAGS,OS_NO);
  228. location.resflags:=F_E;
  229. end;
  230. else
  231. internalerror(200203224);
  232. end;
  233. end;
  234. end;
  235. {$ifdef SUPPORT_MMX}
  236. procedure tx86notnode.second_mmx;
  237. var hreg,r:Tregister;
  238. begin
  239. secondpass(left);
  240. location_reset(location,LOC_MMXREGISTER,OS_NO);
  241. r:=cg.getintregister(exprasmlist,OS_INT);
  242. emit_const_reg(A_MOV,S_L,longint($ffffffff),r);
  243. { load operand }
  244. case left.location.loc of
  245. LOC_MMXREGISTER:
  246. location_copy(location,left.location);
  247. LOC_CMMXREGISTER:
  248. begin
  249. location.register:=cg.getmmxregister(exprasmlist,OS_M64);
  250. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  251. end;
  252. LOC_REFERENCE,
  253. LOC_CREFERENCE:
  254. begin
  255. location_release(exprasmlist,left.location);
  256. location.register:=cg.getmmxregister(exprasmlist,OS_M64);
  257. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  258. end;
  259. end;
  260. { load mask }
  261. hreg:=cg.getmmxregister(exprasmlist,OS_M64);
  262. emit_reg_reg(A_MOVD,S_NO,r,hreg);
  263. cg.ungetregister(exprasmlist,r);
  264. { lower 32 bit }
  265. emit_reg_reg(A_PXOR,S_D,hreg,location.register);
  266. { shift mask }
  267. emit_const_reg(A_PSLLQ,S_NO,32,hreg);
  268. { higher 32 bit }
  269. cg.ungetregister(exprasmlist,hreg);
  270. emit_reg_reg(A_PXOR,S_D,hreg,location.register);
  271. end;
  272. {$endif SUPPORT_MMX}
  273. end.
  274. {
  275. $Log$
  276. Revision 1.6 2004-09-25 14:23:55 peter
  277. * ungetregister is now only used for cpuregisters, renamed to
  278. ungetcpuregister
  279. * renamed (get|unget)explicitregister(s) to ..cpuregister
  280. * removed location-release/reference_release
  281. Revision 1.5 2004/06/20 08:55:32 florian
  282. * logs truncated
  283. Revision 1.4 2004/06/16 20:07:11 florian
  284. * dwarf branch merged
  285. Revision 1.3.2.1 2004/04/27 18:18:26 peter
  286. * aword -> aint
  287. Revision 1.3 2004/02/03 22:32:54 peter
  288. * renamed xNNbittype to xNNinttype
  289. * renamed registers32 to registersint
  290. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  291. Revision 1.2 2004/01/20 12:59:37 florian
  292. * common addnode code for x86-64 and i386
  293. }