rgcpu.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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,aasmsym,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; orgsupreg: tsuperregister); override;
  30. procedure do_spill_written(list: TAsmList; pos: tai; const spilltemp: treference; tempreg: tregister; orgsupreg: tsuperregister); override;
  31. function do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;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; orgsupreg: tsuperregister);
  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. {$ifdef SPARC}
  89. reference_reset(tmpref,sizeof(pint),[]);
  90. tmpref.offset:=spilltemp.offset;
  91. tmpref.refaddr:=addr_high;
  92. helplist.concat(taicpu.op_ref_reg(A_SETHI,tmpref,hreg));
  93. tmpref.refaddr:=addr_low;
  94. helplist.concat(taicpu.op_reg_ref_reg(A_OR,hreg,tmpref,hreg));
  95. {$else SPARC}
  96. if (spilltemp.offset>=-4294967296) and (spilltemp.offset<=-1) then
  97. begin
  98. helplist.concat(taicpu.op_const_reg(A_SETHI,(not(aint(spilltemp.offset)) shr 10) and $3fffff,hreg));
  99. if (aint(spilltemp.offset) and aint($3ff)) or aint($1c00)<>0 then
  100. helplist.concat(taicpu.op_reg_const_reg(A_XOR,hreg,(aint(spilltemp.offset) and aint($3ff)) or aint($1c00),hreg));
  101. end
  102. else
  103. Internalerror(2017090901);
  104. {$endif SPARC}
  105. reference_reset_base(tmpref,hreg,0,spilltemp.temppos,sizeof(aint),[]);
  106. tmpref.index:=spilltemp.base;
  107. helpins:=spilling_create_load(tmpref,tempreg);
  108. helplist.concat(helpins);
  109. list.insertlistafter(pos,helplist);
  110. helplist.free;
  111. end
  112. else
  113. inherited;
  114. end;
  115. procedure trgcpu.do_spill_written(list: TAsmList; pos: tai; const spilltemp: treference; tempreg: tregister; orgsupreg: tsuperregister);
  116. var
  117. tmpref : treference;
  118. helplist : TAsmList;
  119. hreg : tregister;
  120. begin
  121. if abs(spilltemp.offset)>4095 then
  122. begin
  123. helplist:=TAsmList.create;
  124. if getregtype(tempreg)=R_INTREGISTER then
  125. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  126. else
  127. hreg:=cg.getintregister(helplist,OS_ADDR);
  128. {$ifdef SPARC}
  129. reference_reset(tmpref,sizeof(aint),[]);
  130. tmpref.offset:=spilltemp.offset;
  131. tmpref.refaddr:=addr_high;
  132. helplist.concat(taicpu.op_ref_reg(A_SETHI,tmpref,hreg));
  133. tmpref.refaddr:=addr_low;
  134. helplist.concat(taicpu.op_reg_ref_reg(A_OR,hreg,tmpref,hreg));
  135. {$else SPARC}
  136. if (spilltemp.offset>=-4294967296) and (spilltemp.offset<=-1) then
  137. begin
  138. helplist.concat(taicpu.op_const_reg(A_SETHI,(not(aint(spilltemp.offset)) shr 10) and $3fffff,hreg));
  139. if (aint(spilltemp.offset) and aint($3ff)) or aint($1c00)<>0 then
  140. helplist.concat(taicpu.op_reg_const_reg(A_XOR,hreg,(aint(spilltemp.offset) and aint($3ff)) or aint($1c00),hreg));
  141. end
  142. else
  143. Internalerror(2017090901);
  144. {$endif SPARC}
  145. reference_reset_base(tmpref,hreg,0,spilltemp.temppos,sizeof(aint),[]);
  146. tmpref.index:=spilltemp.base;
  147. helplist.concat(spilling_create_store(tempreg,tmpref));
  148. if getregtype(tempreg)=R_INTREGISTER then
  149. ungetregisterinline(helplist,hreg);
  150. list.insertlistafter(pos,helplist);
  151. helplist.free;
  152. end
  153. else
  154. inherited;
  155. end;
  156. function trgcpu.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  157. var
  158. opidx: longint;
  159. begin
  160. result:=false;
  161. { Replace 'mov src,orgreg' with 'st src,spilltemp'
  162. and 'mov orgreg,dst' with 'ld spilltemp,dst' }
  163. if (abs(spilltemp.offset)>4095) then
  164. exit;
  165. if ((regtype=R_INTREGISTER) and (instr.opcode<>A_MOV)) or
  166. ((regtype=R_FPUREGISTER) and (instr.opcode<>A_FMOVs) and (instr.opcode<>A_FMOVd)) then
  167. exit;
  168. { Ignore mis-encoded stuff like 'mov %something,%y' }
  169. if (instr.ops<>2) or
  170. (instr.oper[0]^.typ<>top_reg) or
  171. (instr.oper[1]^.typ<>top_reg) or
  172. (getregtype(instr.oper[0]^.reg)<>regtype) or
  173. (getregtype(instr.oper[1]^.reg)<>regtype) then
  174. exit;
  175. opidx:=-1;
  176. if (getregtype(instr.oper[0]^.reg)=regtype) and (get_alias(getsupreg(instr.oper[0]^.reg))=orgreg) then
  177. begin
  178. if (regtype=R_INTREGISTER) then
  179. instr.opcode:=A_LD_R
  180. else if (getsubreg(instr.oper[0]^.reg)=R_SUBFS) then
  181. instr.opcode:=A_LDF
  182. else
  183. instr.opcode:=A_LDDF;
  184. opidx:=0;
  185. end
  186. else if (getregtype(instr.oper[1]^.reg)=regtype) and (get_alias(getsupreg(instr.oper[1]^.reg))=orgreg) then
  187. begin
  188. if (regtype=R_INTREGISTER) then
  189. instr.opcode:=A_ST_R
  190. else if (getsubreg(instr.oper[1]^.reg)=R_SUBFS) then
  191. instr.opcode:=A_STF
  192. else
  193. instr.opcode:=A_STDF;
  194. opidx:=1;
  195. end
  196. else
  197. InternalError(2013061002);
  198. instr.oper[opidx]^.typ:=top_ref;
  199. new(instr.oper[opidx]^.ref);
  200. instr.oper[opidx]^.ref^:=spilltemp;
  201. result:=true;
  202. end;
  203. end.