/compiler/x86/rgx86.pas

https://github.com/slibre/freepascal · Pascal · 415 lines · 220 code · 47 blank · 148 comment · 16 complexity · 7af374c71919652e977fbf354255fbac MD5 · raw file

  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. fpuvaroffset : byte;
  54. constructor create;
  55. function getregisterfpu(list: TAsmList) : tregister;
  56. procedure ungetregisterfpu(list: TAsmList; r : tregister);
  57. { pushes and restores registers }
  58. procedure saveusedfpuregisters(list:TAsmList;
  59. var saved:Tpushedsavedfpu;
  60. const s:Tcpuregisterset);
  61. procedure restoreusedfpuregisters(list:TAsmList;
  62. const saved:Tpushedsavedfpu);
  63. { corrects the fpu stack register by ofs }
  64. function correct_fpuregister(r : tregister;ofs : byte) : tregister;
  65. end;
  66. implementation
  67. uses
  68. systems,
  69. verbose;
  70. const
  71. { This value is used in tsaved. If the array value is equal
  72. to this, then this means that this register is not used.}
  73. reg_not_saved = $7fffffff;
  74. {******************************************************************************
  75. Trgcpu
  76. ******************************************************************************}
  77. function trgx86.get_spill_subreg(r : tregister) : tsubregister;
  78. begin
  79. result:=getsubreg(r);
  80. end;
  81. function trgx86.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  82. {Decide wether a "replace" spill is possible, i.e. wether we can replace a register
  83. in an instruction by a memory reference. For example, in "mov ireg26d,0", the imaginary
  84. register ireg26d can be replaced by a memory reference.}
  85. var
  86. n,replaceoper : longint;
  87. begin
  88. result:=false;
  89. with instr do
  90. begin
  91. replaceoper:=-1;
  92. case ops of
  93. 1 :
  94. begin
  95. if (oper[0]^.typ=top_reg) and
  96. (getregtype(oper[0]^.reg)=regtype) then
  97. begin
  98. if get_alias(getsupreg(oper[0]^.reg))<>orgreg then
  99. internalerror(200410101);
  100. replaceoper:=0;
  101. end;
  102. end;
  103. 2,3 :
  104. begin
  105. { We can handle opcodes with 2 and 3 operands the same way. The opcodes
  106. with 3 registers are shrd/shld, where the 3rd operand is const or CL,
  107. that doesn't need spilling.
  108. However, due to AT&T order inside the compiler, the 3rd operand is
  109. numbered 0, so look at operand no. 1 and 2 if we have 3 operands by
  110. adding a "n". }
  111. n:=0;
  112. if ops=3 then
  113. n:=1;
  114. if (oper[n+0]^.typ=top_reg) and
  115. (oper[n+1]^.typ=top_reg) and
  116. ((getregtype(oper[n+0]^.reg)<>regtype) or
  117. (getregtype(oper[n+1]^.reg)<>regtype) or
  118. (get_alias(getsupreg(oper[n+0]^.reg))<>get_alias(getsupreg(oper[n+1]^.reg)))) then
  119. begin
  120. if (getregtype(oper[n+0]^.reg)=regtype) and
  121. (get_alias(getsupreg(oper[n+0]^.reg))=orgreg) then
  122. replaceoper:=0+n
  123. else if (getregtype(oper[n+1]^.reg)=regtype) and
  124. (get_alias(getsupreg(oper[n+1]^.reg))=orgreg) then
  125. replaceoper:=1+n;
  126. end
  127. else if (oper[n+0]^.typ=top_reg) and
  128. (oper[n+1]^.typ=top_const) then
  129. begin
  130. if (getregtype(oper[0+n]^.reg)=regtype) and
  131. (get_alias(getsupreg(oper[0+n]^.reg))=orgreg) then
  132. replaceoper:=0+n
  133. else
  134. internalerror(200704282);
  135. end
  136. else if (oper[n+0]^.typ=top_const) and
  137. (oper[n+1]^.typ=top_reg) then
  138. begin
  139. if (getregtype(oper[1+n]^.reg)=regtype) and
  140. (get_alias(getsupreg(oper[1+n]^.reg))=orgreg) then
  141. replaceoper:=1+n
  142. else
  143. internalerror(200704283);
  144. end;
  145. case replaceoper of
  146. 0 :
  147. begin
  148. { Some instructions don't allow memory references
  149. for source }
  150. case instr.opcode of
  151. A_BT,
  152. A_BTS,
  153. A_BTC,
  154. A_BTR,
  155. { shufp* would require 16 byte alignment for memory locations so we force the source
  156. operand into a register }
  157. A_SHUFPD,
  158. A_SHUFPS :
  159. replaceoper:=-1;
  160. end;
  161. end;
  162. 1 :
  163. begin
  164. { Some instructions don't allow memory references
  165. for destination }
  166. case instr.opcode of
  167. A_CMOVcc,
  168. A_MOVZX,
  169. A_MOVSX,
  170. A_MOVSXD,
  171. A_MULSS,
  172. A_MULSD,
  173. A_SUBSS,
  174. A_SUBSD,
  175. A_ADDSD,
  176. A_ADDSS,
  177. A_DIVSD,
  178. A_DIVSS,
  179. A_SHLD,
  180. A_SHRD,
  181. A_COMISD,
  182. A_COMISS,
  183. A_CVTDQ2PD,
  184. A_CVTDQ2PS,
  185. A_CVTPD2DQ,
  186. A_CVTPD2PI,
  187. A_CVTPD2PS,
  188. A_CVTPI2PD,
  189. A_CVTPS2DQ,
  190. A_CVTPS2PD,
  191. A_CVTSD2SI,
  192. A_CVTSD2SS,
  193. A_CVTSI2SD,
  194. A_CVTSS2SD,
  195. A_CVTTPD2PI,
  196. A_CVTTPD2DQ,
  197. A_CVTTPS2DQ,
  198. A_CVTTSD2SI,
  199. A_CVTPI2PS,
  200. A_CVTPS2PI,
  201. A_CVTSI2SS,
  202. A_CVTSS2SI,
  203. A_CVTTPS2PI,
  204. A_CVTTSS2SI,
  205. A_IMUL,
  206. A_XORPD,
  207. A_XORPS,
  208. A_ORPD,
  209. A_ORPS,
  210. A_ANDPD,
  211. A_ANDPS,
  212. A_UNPCKLPS,
  213. A_UNPCKHPS,
  214. A_SHUFPD,
  215. A_SHUFPS:
  216. replaceoper:=-1;
  217. {$ifdef x86_64}
  218. A_MOV:
  219. { 64 bit constants can only be moved into registers }
  220. if (oper[0]^.typ=top_const) and
  221. (oper[1]^.typ=top_reg) and
  222. ((oper[0]^.val<low(longint)) or
  223. (oper[0]^.val>high(longint))) then
  224. replaceoper:=-1;
  225. {$endif x86_64}
  226. end;
  227. end;
  228. end;
  229. end;
  230. end;
  231. {$ifdef x86_64}
  232. { 32 bit operations on 32 bit registers on x86_64 can result in
  233. zeroing the upper 32 bits of the register. This does not happen
  234. with memory operations, so we have to perform these calculations
  235. in registers. }
  236. if (instr.opsize=S_L) then
  237. replaceoper:=-1;
  238. {$endif x86_64}
  239. { Replace register with spill reference }
  240. if replaceoper<>-1 then
  241. begin
  242. oper[replaceoper]^.typ:=top_ref;
  243. new(oper[replaceoper]^.ref);
  244. oper[replaceoper]^.ref^:=spilltemp;
  245. { memory locations aren't guaranteed to be aligned }
  246. case opcode of
  247. A_MOVAPS:
  248. opcode:=A_MOVSS;
  249. A_MOVAPD:
  250. opcode:=A_MOVSD;
  251. end;
  252. result:=true;
  253. end;
  254. end;
  255. end;
  256. {******************************************************************************
  257. Trgx86fpu
  258. ******************************************************************************}
  259. constructor Trgx86fpu.create;
  260. begin
  261. used_in_proc:=[];
  262. unusedregsfpu:=usableregsfpu;
  263. end;
  264. function trgx86fpu.getregisterfpu(list: TAsmList) : tregister;
  265. begin
  266. { note: don't return R_ST0, see comments above implementation of }
  267. { a_loadfpu_* methods in cgcpu (JM) }
  268. result:=NR_ST;
  269. end;
  270. procedure trgx86fpu.ungetregisterfpu(list : TAsmList; r : tregister);
  271. begin
  272. { nothing to do, fpu stack management is handled by the load/ }
  273. { store operations in cgcpu (JM) }
  274. end;
  275. function trgx86fpu.correct_fpuregister(r : tregister;ofs : byte) : tregister;
  276. begin
  277. correct_fpuregister:=r;
  278. setsupreg(correct_fpuregister,ofs);
  279. end;
  280. procedure trgx86fpu.saveusedfpuregisters(list: TAsmList;
  281. var saved : tpushedsavedfpu;
  282. const s: tcpuregisterset);
  283. { var
  284. r : tregister;
  285. hr : treference; }
  286. begin
  287. used_in_proc:=used_in_proc+s;
  288. { TODO: firstsavefpureg}
  289. (*
  290. { don't try to save the fpu registers if not desired (e.g. for }
  291. { the 80x86) }
  292. if firstsavefpureg <> R_NO then
  293. for r.enum:=firstsavefpureg to lastsavefpureg do
  294. begin
  295. saved[r.enum].ofs:=reg_not_saved;
  296. { if the register is used by the calling subroutine and if }
  297. { it's not a regvar (those are handled separately) }
  298. if not is_reg_var_other[r.enum] and
  299. (r.enum in s) and
  300. { and is present in use }
  301. not(r.enum in unusedregsfpu) then
  302. begin
  303. { then save it }
  304. tg.GetTemp(list,extended_size,tt_persistent,hr);
  305. saved[r.enum].ofs:=hr.offset;
  306. cg.a_loadfpu_reg_ref(list,OS_FLOAT,OS_FLOAT,r,hr);
  307. cg.a_reg_dealloc(list,r);
  308. include(unusedregsfpu,r.enum);
  309. inc(countunusedregsfpu);
  310. end;
  311. end;
  312. *)
  313. end;
  314. procedure trgx86fpu.restoreusedfpuregisters(list : TAsmList;
  315. const saved : tpushedsavedfpu);
  316. {
  317. var
  318. r,r2 : tregister;
  319. hr : treference;
  320. }
  321. begin
  322. { TODO: firstsavefpureg}
  323. (*
  324. if firstsavefpureg <> R_NO then
  325. for r.enum:=lastsavefpureg downto firstsavefpureg do
  326. begin
  327. if saved[r.enum].ofs <> reg_not_saved then
  328. begin
  329. r2.enum:=R_INTREGISTER;
  330. r2.number:=NR_FRAME_POINTER_REG;
  331. reference_reset_base(hr,r2,saved[r.enum].ofs);
  332. cg.a_reg_alloc(list,r);
  333. cg.a_loadfpu_ref_reg(list,OS_FLOAT,OS_FLOAT,hr,r);
  334. if not (r.enum in unusedregsfpu) then
  335. { internalerror(10)
  336. in n386cal we always save/restore the reg *state*
  337. using save/restoreunusedstate -> the current state
  338. may not be real (JM) }
  339. else
  340. begin
  341. dec(countunusedregsfpu);
  342. exclude(unusedregsfpu,r.enum);
  343. end;
  344. tg.UnGetTemp(list,hr);
  345. end;
  346. end;
  347. *)
  348. end;
  349. (*
  350. procedure Trgx86fpu.saveotherregvars(list: TAsmList; const s: totherregisterset);
  351. var
  352. r: Tregister;
  353. begin
  354. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  355. exit;
  356. if firstsavefpureg <> NR_NO then
  357. for r.enum := firstsavefpureg to lastsavefpureg do
  358. if is_reg_var_other[r.enum] and
  359. (r.enum in s) then
  360. store_regvar(list,r);
  361. end;
  362. *)
  363. end.