rgcpu.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the SPARC 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. unit rgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmbase,aasmcpu,aasmtai,aasmdata,
  22. cgbase,cgutils,
  23. cpubase,
  24. rgobj;
  25. type
  26. trgcpu=class(trgobj)
  27. procedure add_constraints(reg:tregister);override;
  28. function get_spill_subreg(r : tregister) : tsubregister;override;
  29. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  30. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  31. function do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;override;
  32. end;
  33. implementation
  34. uses
  35. verbose,cutils,
  36. globtype,
  37. cgobj;
  38. procedure trgcpu.add_constraints(reg:tregister);
  39. var
  40. supreg,i : Tsuperregister;
  41. begin
  42. case getsubreg(reg) of
  43. { Let 64bit floats conflict with all odd float regs }
  44. R_SUBFD:
  45. begin
  46. supreg:=getsupreg(reg);
  47. i:=RS_F1;
  48. while (i<=RS_F31) do
  49. begin
  50. add_edge(supreg,i);
  51. inc(i,2);
  52. end;
  53. end;
  54. { Let 64bit ints conflict with all odd int regs }
  55. R_SUBQ:
  56. begin
  57. supreg:=getsupreg(reg);
  58. i:=RS_G1;
  59. while (i<=RS_I7) do
  60. begin
  61. add_edge(supreg,i);
  62. inc(i,2);
  63. end;
  64. end;
  65. end;
  66. end;
  67. function trgcpu.get_spill_subreg(r : tregister) : tsubregister;
  68. begin
  69. if getregtype(r)=R_FPUREGISTER then
  70. result:=getsubreg(r)
  71. else
  72. result:=defaultsub;
  73. end;
  74. procedure trgcpu.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  75. var
  76. helpins : tai;
  77. tmpref : treference;
  78. helplist : TAsmList;
  79. hreg : tregister;
  80. begin
  81. if abs(spilltemp.offset)>4095 then
  82. begin
  83. helplist:=TAsmList.create;
  84. if getregtype(tempreg)=R_INTREGISTER then
  85. hreg:=tempreg
  86. else
  87. hreg:=cg.getintregister(helplist,OS_ADDR);
  88. reference_reset(tmpref,sizeof(pint));
  89. tmpref.offset:=spilltemp.offset;
  90. tmpref.refaddr:=addr_high;
  91. helplist.concat(taicpu.op_ref_reg(A_SETHI,tmpref,hreg));
  92. tmpref.refaddr:=addr_low;
  93. helplist.concat(taicpu.op_reg_ref_reg(A_OR,hreg,tmpref,hreg));
  94. reference_reset_base(tmpref,hreg,0,sizeof(aint));
  95. tmpref.index:=spilltemp.base;
  96. helpins:=spilling_create_load(tmpref,tempreg);
  97. helplist.concat(helpins);
  98. list.insertlistafter(pos,helplist);
  99. helplist.free;
  100. end
  101. else
  102. inherited do_spill_read(list,pos,spilltemp,tempreg);
  103. end;
  104. procedure trgcpu.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  105. var
  106. tmpref : treference;
  107. helplist : TAsmList;
  108. hreg : tregister;
  109. begin
  110. if abs(spilltemp.offset)>4095 then
  111. begin
  112. helplist:=TAsmList.create;
  113. if getregtype(tempreg)=R_INTREGISTER then
  114. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  115. else
  116. hreg:=cg.getintregister(helplist,OS_ADDR);
  117. reference_reset(tmpref,sizeof(aint));
  118. tmpref.offset:=spilltemp.offset;
  119. tmpref.refaddr:=addr_high;
  120. helplist.concat(taicpu.op_ref_reg(A_SETHI,tmpref,hreg));
  121. tmpref.refaddr:=addr_low;
  122. helplist.concat(taicpu.op_reg_ref_reg(A_OR,hreg,tmpref,hreg));
  123. reference_reset_base(tmpref,hreg,0,sizeof(aint));
  124. tmpref.index:=spilltemp.base;
  125. helplist.concat(spilling_create_store(tempreg,tmpref));
  126. if getregtype(tempreg)=R_INTREGISTER then
  127. ungetregisterinline(helplist,hreg);
  128. list.insertlistafter(pos,helplist);
  129. helplist.free;
  130. end
  131. else
  132. inherited do_spill_written(list,pos,spilltemp,tempreg);
  133. end;
  134. function trgcpu.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  135. var
  136. opidx: longint;
  137. begin
  138. result:=false;
  139. { Replace 'mov src,orgreg' with 'st src,spilltemp'
  140. and 'mov orgreg,dst' with 'ld spilltemp,dst' }
  141. if (abs(spilltemp.offset)>4095) then
  142. exit;
  143. if ((regtype=R_INTREGISTER) and (instr.opcode<>A_MOV)) or
  144. ((regtype=R_FPUREGISTER) and (instr.opcode<>A_FMOVs) and (instr.opcode<>A_FMOVd)) then
  145. exit;
  146. { Ignore mis-encoded stuff like 'mov %something,%y' }
  147. if (instr.ops<>2) or
  148. (instr.oper[0]^.typ<>top_reg) or
  149. (instr.oper[1]^.typ<>top_reg) or
  150. (getregtype(instr.oper[0]^.reg)<>regtype) or
  151. (getregtype(instr.oper[1]^.reg)<>regtype) then
  152. exit;
  153. opidx:=-1;
  154. if get_alias(getsupreg(instr.oper[0]^.reg))=orgreg then
  155. begin
  156. if (regtype=R_INTREGISTER) then
  157. instr.opcode:=A_LD
  158. else if (getsubreg(instr.oper[0]^.reg)=R_SUBFS) then
  159. instr.opcode:=A_LDF
  160. else
  161. instr.opcode:=A_LDDF;
  162. opidx:=0;
  163. end
  164. else if get_alias(getsupreg(instr.oper[1]^.reg))=orgreg then
  165. begin
  166. if (regtype=R_INTREGISTER) then
  167. instr.opcode:=A_ST
  168. else if (getsubreg(instr.oper[1]^.reg)=R_SUBFS) then
  169. instr.opcode:=A_STF
  170. else
  171. instr.opcode:=A_STDF;
  172. opidx:=1;
  173. end
  174. else
  175. InternalError(2013061002);
  176. instr.oper[opidx]^.typ:=top_ref;
  177. new(instr.oper[opidx]^.ref);
  178. instr.oper[opidx]^.ref^:=spilltemp;
  179. result:=true;
  180. end;
  181. end.