nx86mat.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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,symdef,
  42. aasmbase,aasmtai,aasmdata,defutil,
  43. cgbase,pass_1,pass_2,
  44. ncon,
  45. cpubase,procinfo,
  46. cga,ncgutil,cgobj,cgx86,cgutils;
  47. {*****************************************************************************
  48. TI386UNARYMINUSNODE
  49. *****************************************************************************}
  50. function tx86unaryminusnode.pass_1 : tnode;
  51. begin
  52. result:=nil;
  53. firstpass(left);
  54. if codegenerror then
  55. exit;
  56. if (left.resultdef.typ=floatdef) then
  57. begin
  58. if use_sse(left.resultdef) then
  59. begin
  60. if (registersmm < 1) then
  61. registersmm := 1;
  62. expectloc:=LOC_MMREGISTER;
  63. end
  64. else
  65. begin
  66. if (registersfpu < 1) then
  67. registersfpu := 1;
  68. expectloc:=LOC_FPUREGISTER;
  69. end;
  70. end
  71. {$ifdef SUPPORT_MMX}
  72. else
  73. if (cs_mmx in current_settings.localswitches) and
  74. is_mmx_able_array(left.resultdef) then
  75. begin
  76. registersint:=left.registersint;
  77. registersfpu:=left.registersfpu;
  78. registersmmx:=left.registersmmx;
  79. if (left.location.loc<>LOC_MMXREGISTER) and
  80. (registersmmx<1) then
  81. registersmmx:=1;
  82. end
  83. {$endif SUPPORT_MMX}
  84. else
  85. inherited pass_1;
  86. end;
  87. {$ifdef SUPPORT_MMX}
  88. procedure tx86unaryminusnode.second_mmx;
  89. var
  90. op : tasmop;
  91. hreg : tregister;
  92. begin
  93. secondpass(left);
  94. location_reset(location,LOC_MMXREGISTER,OS_NO);
  95. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  96. emit_reg_reg(A_PXOR,S_NO,hreg,hreg);
  97. case left.location.loc of
  98. LOC_MMXREGISTER:
  99. begin
  100. location.register:=left.location.register;
  101. end;
  102. LOC_CMMXREGISTER:
  103. begin
  104. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  105. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  106. end;
  107. LOC_REFERENCE,
  108. LOC_CREFERENCE:
  109. begin
  110. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  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 current_settings.localswitches then
  117. case mmx_type(resultdef) 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(resultdef) 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. 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(current_asmdata.CurrAsmList,left.location,false);
  150. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  151. { make life of register allocator easier }
  152. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  153. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),def_cgsize(resultdef),left.location.register,location.register,mms_movescalar);
  154. reg:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  155. current_asmdata.getdatalabel(l1);
  156. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  157. case def_cgsize(resultdef) of
  158. OS_F32:
  159. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(longint(1 shl 31)));
  160. OS_F64:
  161. begin
  162. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(0));
  163. current_asmdata.asmlists[al_typedconsts].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(current_asmdata.CurrAsmList,def_cgsize(resultdef),def_cgsize(resultdef),href,reg,mms_movescalar);
  170. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.location.size,reg,location.register,nil);
  171. end
  172. else
  173. begin
  174. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  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(current_asmdata.CurrAsmList,
  181. left.location.size,location.size,
  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(current_asmdata.CurrAsmList,left.location.size,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(resultdef);
  207. if left.expectloc=LOC_JUMP then
  208. begin
  209. location_reset(location,LOC_JUMP,OS_NO);
  210. hl:=current_procinfo.CurrTrueLabel;
  211. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  212. current_procinfo.CurrFalseLabel:=hl;
  213. secondpass(left);
  214. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  215. hl:=current_procinfo.CurrTrueLabel;
  216. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  217. current_procinfo.CurrFalseLabel:=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. LOC_SUBSETREG,
  238. LOC_CSUBSETREG,
  239. LOC_SUBSETREF,
  240. LOC_CSUBSETREF :
  241. begin
  242. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  243. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  244. location_reset(location,LOC_FLAGS,OS_NO);
  245. location.resflags:=F_E;
  246. end;
  247. else
  248. internalerror(200203224);
  249. end;
  250. end;
  251. end;
  252. {$ifdef SUPPORT_MMX}
  253. procedure tx86notnode.second_mmx;
  254. var hreg,r:Tregister;
  255. begin
  256. secondpass(left);
  257. location_reset(location,LOC_MMXREGISTER,OS_NO);
  258. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  259. emit_const_reg(A_MOV,S_L,longint($ffffffff),r);
  260. { load operand }
  261. case left.location.loc of
  262. LOC_MMXREGISTER:
  263. location_copy(location,left.location);
  264. LOC_CMMXREGISTER:
  265. begin
  266. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  267. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  268. end;
  269. LOC_REFERENCE,
  270. LOC_CREFERENCE:
  271. begin
  272. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  273. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  274. end;
  275. end;
  276. { load mask }
  277. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  278. emit_reg_reg(A_MOVD,S_NO,r,hreg);
  279. { lower 32 bit }
  280. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  281. { shift mask }
  282. emit_const_reg(A_PSLLQ,S_B,32,hreg);
  283. { higher 32 bit }
  284. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  285. end;
  286. {$endif SUPPORT_MMX}
  287. end.