rgx86.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the x86 specific class for the register
  4. allocator
  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 rgx86;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globtype,
  23. cpubase,cpuinfo,cgbase,cgutils,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. rgobj;
  26. type
  27. trgx86 = class(trgobj)
  28. function get_spill_subreg(r : tregister) : tsubregister;override;
  29. function do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;override;
  30. end;
  31. tpushedsavedloc = record
  32. case byte of
  33. 0: (pushed: boolean);
  34. 1: (ofs: longint);
  35. end;
  36. tpushedsavedfpu = array[tsuperregister] of tpushedsavedloc;
  37. trgx86fpu = class
  38. { The "usableregsxxx" contain all registers of type "xxx" that }
  39. { aren't currently allocated to a regvar. The "unusedregsxxx" }
  40. { contain all registers of type "xxx" that aren't currently }
  41. { allocated }
  42. unusedregsfpu,usableregsfpu : Tsuperregisterset;
  43. { these counters contain the number of elements in the }
  44. { unusedregsxxx/usableregsxxx sets }
  45. countunusedregsfpu : byte;
  46. { Contains the registers which are really used by the proc itself.
  47. It doesn't take care of registers used by called procedures
  48. }
  49. used_in_proc : tcpuregisterset;
  50. {reg_pushes_other : regvarother_longintarray;
  51. is_reg_var_other : regvarother_booleanarray;
  52. regvar_loaded_other : regvarother_booleanarray;}
  53. { tries to hold the amount of times which the current tree is processed }
  54. t_times: longint;
  55. fpuvaroffset : byte;
  56. constructor create;
  57. function getregisterfpu(list: TAsmList) : tregister;
  58. procedure ungetregisterfpu(list: TAsmList; r : tregister);
  59. { pushes and restores registers }
  60. procedure saveusedfpuregisters(list:TAsmList;
  61. var saved:Tpushedsavedfpu;
  62. const s:Tcpuregisterset);
  63. procedure restoreusedfpuregisters(list:TAsmList;
  64. const saved:Tpushedsavedfpu);
  65. { corrects the fpu stack register by ofs }
  66. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  67. end;
  68. implementation
  69. uses
  70. systems,
  71. verbose;
  72. const
  73. { This value is used in tsaved. If the array value is equal
  74. to this, then this means that this register is not used.}
  75. reg_not_saved = $7fffffff;
  76. {******************************************************************************
  77. Trgcpu
  78. ******************************************************************************}
  79. function trgx86.get_spill_subreg(r : tregister) : tsubregister;
  80. begin
  81. result:=getsubreg(r);
  82. end;
  83. function trgx86.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  84. var
  85. replaceoper : longint;
  86. begin
  87. result:=false;
  88. with instr do
  89. begin
  90. replaceoper:=-1;
  91. case ops of
  92. 1 :
  93. begin
  94. if (oper[0]^.typ=top_reg) then
  95. begin
  96. if get_alias(getsupreg(oper[0]^.reg))<>orgreg then
  97. internalerror(200410101);
  98. replaceoper:=0;
  99. end;
  100. end;
  101. 2,3 :
  102. begin
  103. { We can handle opcodes with 2 and 3 operands the same way. The opcodes
  104. with 3 registers are shrd/shld, where the 3rd operand is const or CL,
  105. that doesn't need spilling }
  106. if (oper[0]^.typ=top_reg) and
  107. (oper[1]^.typ=top_reg) and
  108. (get_alias(getsupreg(oper[0]^.reg))<>get_alias(getsupreg(oper[1]^.reg))) then
  109. begin
  110. { One of the arguments shall be able to be replaced }
  111. if (getregtype(oper[0]^.reg)=regtype) and
  112. (get_alias(getsupreg(oper[0]^.reg))=orgreg) then
  113. replaceoper:=0
  114. else
  115. if (getregtype(oper[1]^.reg)=regtype) and
  116. (get_alias(getsupreg(oper[1]^.reg))=orgreg) then
  117. replaceoper:=1
  118. else
  119. internalerror(200410106);
  120. case replaceoper of
  121. 0 :
  122. begin
  123. { Some instructions don't allow memory references
  124. for source }
  125. case instr.opcode of
  126. A_BT,
  127. A_BTS,
  128. A_BTC,
  129. A_BTR :
  130. replaceoper:=-1;
  131. end;
  132. end;
  133. 1 :
  134. begin
  135. { Some instructions don't allow memory references
  136. for destination }
  137. case instr.opcode of
  138. A_MOVZX,
  139. A_MOVSX,
  140. A_MULSS,
  141. A_MULSD,
  142. A_SUBSS,
  143. A_SUBSD,
  144. A_ADDSD,
  145. A_ADDSS,
  146. A_DIVSD,
  147. A_DIVSS,
  148. A_SHLD,
  149. A_SHRD,
  150. A_CVTDQ2PD,
  151. A_CVTDQ2PS,
  152. A_CVTPD2DQ,
  153. A_CVTPD2PI,
  154. A_CVTPD2PS,
  155. A_CVTPI2PD,
  156. A_CVTPS2DQ,
  157. A_CVTPS2PD,
  158. A_CVTSD2SI,
  159. A_CVTSD2SS,
  160. A_CVTSI2SD,
  161. A_CVTSS2SD,
  162. A_CVTTPD2PI,
  163. A_CVTTPD2DQ,
  164. A_CVTTPS2DQ,
  165. A_CVTTSD2SI,
  166. A_CVTPI2PS,
  167. A_CVTPS2PI,
  168. A_CVTSI2SS,
  169. A_CVTSS2SI,
  170. A_CVTTPS2PI,
  171. A_CVTTSS2SI,
  172. A_IMUL,
  173. A_XORPD,
  174. A_XORPS,
  175. A_ORPD,
  176. A_ORPS,
  177. A_ANDPD,
  178. A_ANDPS:
  179. replaceoper:=-1;
  180. end;
  181. end;
  182. end;
  183. end;
  184. end;
  185. end;
  186. { Replace register with spill reference }
  187. if replaceoper<>-1 then
  188. begin
  189. oper[replaceoper]^.typ:=top_ref;
  190. new(oper[replaceoper]^.ref);
  191. oper[replaceoper]^.ref^:=spilltemp;
  192. { memory locations aren't guaranteed to be aligned }
  193. case opcode of
  194. A_MOVAPS:
  195. opcode:=A_MOVSS;
  196. A_MOVAPD:
  197. opcode:=A_MOVSD;
  198. end;
  199. result:=true;
  200. end;
  201. end;
  202. end;
  203. {******************************************************************************
  204. Trgx86fpu
  205. ******************************************************************************}
  206. constructor Trgx86fpu.create;
  207. begin
  208. used_in_proc:=[];
  209. t_times := 0;
  210. unusedregsfpu:=usableregsfpu;
  211. end;
  212. function trgx86fpu.getregisterfpu(list: TAsmList) : tregister;
  213. begin
  214. { note: don't return R_ST0, see comments above implementation of }
  215. { a_loadfpu_* methods in cgcpu (JM) }
  216. result:=NR_ST;
  217. end;
  218. procedure trgx86fpu.ungetregisterfpu(list : TAsmList; r : tregister);
  219. begin
  220. { nothing to do, fpu stack management is handled by the load/ }
  221. { store operations in cgcpu (JM) }
  222. end;
  223. function trgx86fpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  224. begin
  225. correct_fpuregister:=r;
  226. setsupreg(correct_fpuregister,ofs);
  227. end;
  228. procedure trgx86fpu.saveusedfpuregisters(list: TAsmList;
  229. var saved : tpushedsavedfpu;
  230. const s: tcpuregisterset);
  231. var
  232. r : tregister;
  233. hr : treference;
  234. begin
  235. used_in_proc:=used_in_proc+s;
  236. {$warning TODO firstsavefpureg}
  237. (*
  238. { don't try to save the fpu registers if not desired (e.g. for }
  239. { the 80x86) }
  240. if firstsavefpureg <> R_NO then
  241. for r.enum:=firstsavefpureg to lastsavefpureg do
  242. begin
  243. saved[r.enum].ofs:=reg_not_saved;
  244. { if the register is used by the calling subroutine and if }
  245. { it's not a regvar (those are handled separately) }
  246. if not is_reg_var_other[r.enum] and
  247. (r.enum in s) and
  248. { and is present in use }
  249. not(r.enum in unusedregsfpu) then
  250. begin
  251. { then save it }
  252. tg.GetTemp(list,extended_size,tt_persistent,hr);
  253. saved[r.enum].ofs:=hr.offset;
  254. cg.a_loadfpu_reg_ref(list,OS_FLOAT,OS_FLOAT,r,hr);
  255. cg.a_reg_dealloc(list,r);
  256. include(unusedregsfpu,r.enum);
  257. inc(countunusedregsfpu);
  258. end;
  259. end;
  260. *)
  261. end;
  262. procedure trgx86fpu.restoreusedfpuregisters(list : TAsmList;
  263. const saved : tpushedsavedfpu);
  264. var
  265. r,r2 : tregister;
  266. hr : treference;
  267. begin
  268. {$warning TODO firstsavefpureg}
  269. (*
  270. if firstsavefpureg <> R_NO then
  271. for r.enum:=lastsavefpureg downto firstsavefpureg do
  272. begin
  273. if saved[r.enum].ofs <> reg_not_saved then
  274. begin
  275. r2.enum:=R_INTREGISTER;
  276. r2.number:=NR_FRAME_POINTER_REG;
  277. reference_reset_base(hr,r2,saved[r.enum].ofs);
  278. cg.a_reg_alloc(list,r);
  279. cg.a_loadfpu_ref_reg(list,OS_FLOAT,OS_FLOAT,hr,r);
  280. if not (r.enum in unusedregsfpu) then
  281. { internalerror(10)
  282. in n386cal we always save/restore the reg *state*
  283. using save/restoreunusedstate -> the current state
  284. may not be real (JM) }
  285. else
  286. begin
  287. dec(countunusedregsfpu);
  288. exclude(unusedregsfpu,r.enum);
  289. end;
  290. tg.UnGetTemp(list,hr);
  291. end;
  292. end;
  293. *)
  294. end;
  295. (*
  296. procedure Trgx86fpu.saveotherregvars(list: TAsmList; const s: totherregisterset);
  297. var
  298. r: Tregister;
  299. begin
  300. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  301. exit;
  302. if firstsavefpureg <> NR_NO then
  303. for r.enum := firstsavefpureg to lastsavefpureg do
  304. if is_reg_var_other[r.enum] and
  305. (r.enum in s) then
  306. store_regvar(list,r);
  307. end;
  308. *)
  309. end.