nx86mat.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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_vectorfpu(left.resultdef) then
  59. expectloc:=LOC_MMREGISTER
  60. else
  61. expectloc:=LOC_FPUREGISTER;
  62. end
  63. {$ifdef SUPPORT_MMX}
  64. else
  65. if (cs_mmx in current_settings.localswitches) and
  66. is_mmx_able_array(left.resultdef) then
  67. begin
  68. expectloc:=LOC_MMXREGISTER;
  69. end
  70. {$endif SUPPORT_MMX}
  71. else
  72. inherited pass_1;
  73. end;
  74. {$ifdef SUPPORT_MMX}
  75. procedure tx86unaryminusnode.second_mmx;
  76. var
  77. op : tasmop;
  78. hreg : tregister;
  79. begin
  80. secondpass(left);
  81. location_reset(location,LOC_MMXREGISTER,OS_NO);
  82. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  83. emit_reg_reg(A_PXOR,S_NO,hreg,hreg);
  84. case left.location.loc of
  85. LOC_MMXREGISTER:
  86. begin
  87. location.register:=left.location.register;
  88. end;
  89. LOC_CMMXREGISTER:
  90. begin
  91. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  92. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  93. end;
  94. LOC_REFERENCE,
  95. LOC_CREFERENCE:
  96. begin
  97. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  98. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  99. end;
  100. else
  101. internalerror(200203225);
  102. end;
  103. if cs_mmx_saturation in current_settings.localswitches then
  104. case mmx_type(resultdef) of
  105. mmxs8bit:
  106. op:=A_PSUBSB;
  107. mmxu8bit:
  108. op:=A_PSUBUSB;
  109. mmxs16bit,mmxfixed16:
  110. op:=A_PSUBSW;
  111. mmxu16bit:
  112. op:=A_PSUBUSW;
  113. end
  114. else
  115. case mmx_type(resultdef) of
  116. mmxs8bit,mmxu8bit:
  117. op:=A_PSUBB;
  118. mmxs16bit,mmxu16bit,mmxfixed16:
  119. op:=A_PSUBW;
  120. mmxs32bit,mmxu32bit:
  121. op:=A_PSUBD;
  122. end;
  123. emit_reg_reg(op,S_NO,location.register,hreg);
  124. emit_reg_reg(A_MOVQ,S_NO,hreg,location.register);
  125. end;
  126. {$endif SUPPORT_MMX}
  127. procedure tx86unaryminusnode.second_float;
  128. var
  129. reg : tregister;
  130. href : treference;
  131. l1 : tasmlabel;
  132. begin
  133. secondpass(left);
  134. if expectloc=LOC_MMREGISTER then
  135. begin
  136. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  137. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  138. { make life of register allocator easier }
  139. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  140. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),def_cgsize(resultdef),left.location.register,location.register,mms_movescalar);
  141. reg:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  142. current_asmdata.getdatalabel(l1);
  143. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(sizeof(pint)));
  144. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  145. case def_cgsize(resultdef) of
  146. OS_F32:
  147. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(longint(1 shl 31)));
  148. OS_F64:
  149. begin
  150. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(0));
  151. current_asmdata.asmlists[al_typedconsts].concat(tai_const.create_32bit(-(1 shl 31)));
  152. end
  153. else
  154. internalerror(2004110215);
  155. end;
  156. reference_reset_symbol(href,l1,0,resultdef.alignment);
  157. cg.a_loadmm_ref_reg(current_asmdata.CurrAsmList,def_cgsize(resultdef),def_cgsize(resultdef),href,reg,mms_movescalar);
  158. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_XOR,left.location.size,reg,location.register,nil);
  159. end
  160. else
  161. begin
  162. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  163. case left.location.loc of
  164. LOC_REFERENCE,
  165. LOC_CREFERENCE:
  166. begin
  167. location.register:=NR_ST;
  168. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  169. left.location.size,location.size,
  170. left.location.reference,location.register);
  171. emit_none(A_FCHS,S_NO);
  172. end;
  173. LOC_FPUREGISTER,
  174. LOC_CFPUREGISTER:
  175. begin
  176. { "load st,st" is ignored by the code generator }
  177. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,NR_ST);
  178. location.register:=NR_ST;
  179. emit_none(A_FCHS,S_NO);
  180. end;
  181. else
  182. internalerror(200312241);
  183. end;
  184. end;
  185. end;
  186. {*****************************************************************************
  187. TX86NOTNODE
  188. *****************************************************************************}
  189. procedure tx86notnode.second_boolean;
  190. var
  191. hl : tasmlabel;
  192. opsize : tcgsize;
  193. begin
  194. opsize:=def_cgsize(resultdef);
  195. if left.expectloc=LOC_JUMP then
  196. begin
  197. location_reset(location,LOC_JUMP,OS_NO);
  198. hl:=current_procinfo.CurrTrueLabel;
  199. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  200. current_procinfo.CurrFalseLabel:=hl;
  201. secondpass(left);
  202. maketojumpbool(current_asmdata.CurrAsmList,left,lr_load_regvars);
  203. hl:=current_procinfo.CurrTrueLabel;
  204. current_procinfo.CurrTrueLabel:=current_procinfo.CurrFalseLabel;
  205. current_procinfo.CurrFalseLabel:=hl;
  206. end
  207. else
  208. begin
  209. { the second pass could change the location of left }
  210. { if it is a register variable, so we've to do }
  211. { this before the case statement }
  212. secondpass(left);
  213. case left.expectloc of
  214. LOC_FLAGS :
  215. begin
  216. location_reset(location,LOC_FLAGS,OS_NO);
  217. location.resflags:=left.location.resflags;
  218. inverse_flags(location.resflags);
  219. end;
  220. LOC_CONSTANT,
  221. LOC_REGISTER,
  222. LOC_CREGISTER,
  223. LOC_REFERENCE,
  224. LOC_CREFERENCE,
  225. LOC_SUBSETREG,
  226. LOC_CSUBSETREG,
  227. LOC_SUBSETREF,
  228. LOC_CSUBSETREF :
  229. begin
  230. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  231. emit_reg_reg(A_TEST,TCGSize2Opsize[opsize],left.location.register,left.location.register);
  232. location_reset(location,LOC_FLAGS,OS_NO);
  233. location.resflags:=F_E;
  234. end;
  235. else
  236. internalerror(200203224);
  237. end;
  238. end;
  239. end;
  240. {$ifdef SUPPORT_MMX}
  241. procedure tx86notnode.second_mmx;
  242. var hreg,r:Tregister;
  243. begin
  244. secondpass(left);
  245. location_reset(location,LOC_MMXREGISTER,OS_NO);
  246. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  247. emit_const_reg(A_MOV,S_L,longint($ffffffff),r);
  248. { load operand }
  249. case left.location.loc of
  250. LOC_MMXREGISTER:
  251. location_copy(location,left.location);
  252. LOC_CMMXREGISTER:
  253. begin
  254. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  255. emit_reg_reg(A_MOVQ,S_NO,left.location.register,location.register);
  256. end;
  257. LOC_REFERENCE,
  258. LOC_CREFERENCE:
  259. begin
  260. location.register:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  261. emit_ref_reg(A_MOVQ,S_NO,left.location.reference,location.register);
  262. end;
  263. end;
  264. { load mask }
  265. hreg:=tcgx86(cg).getmmxregister(current_asmdata.CurrAsmList);
  266. emit_reg_reg(A_MOVD,S_NO,r,hreg);
  267. { lower 32 bit }
  268. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  269. { shift mask }
  270. emit_const_reg(A_PSLLQ,S_B,32,hreg);
  271. { higher 32 bit }
  272. emit_reg_reg(A_PXOR,S_NO,hreg,location.register);
  273. end;
  274. {$endif SUPPORT_MMX}
  275. end.