rgcpu.pas 8.0 KB

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