rgcpu.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. {
  2. Copyright (c) 1998-2009 by Florian Klaempfl and David Zhang
  3. This unit implements the register allocator for MIPS(EL)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  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. trgintcpu=class(trgcpu)
  34. procedure add_cpu_interferences(p:tai);override;
  35. end;
  36. implementation
  37. uses
  38. globtype,
  39. verbose,cutils,
  40. cgobj;
  41. procedure trgcpu.add_constraints(reg:tregister);
  42. var
  43. supreg,i : Tsuperregister;
  44. begin
  45. case getsubreg(reg) of
  46. { Let 64bit floats conflict with all odd float regs }
  47. R_SUBFD:
  48. begin
  49. supreg:=getsupreg(reg);
  50. i:=RS_F1;
  51. while (i<=RS_F31) do
  52. begin
  53. add_edge(supreg,i);
  54. inc(i,2);
  55. end;
  56. end;
  57. { Let 64bit ints conflict with all odd int regs }
  58. R_SUBQ:
  59. begin
  60. supreg:=getsupreg(reg);
  61. i:=RS_R1;
  62. while (i<=RS_R31) do
  63. begin
  64. add_edge(supreg,i);
  65. inc(i,2);
  66. end;
  67. end;
  68. end;
  69. end;
  70. function trgcpu.get_spill_subreg(r : tregister) : tsubregister;
  71. begin
  72. if getregtype(r)=R_FPUREGISTER then
  73. result:=getsubreg(r)
  74. else
  75. result:=defaultsub;
  76. end;
  77. procedure trgcpu.do_spill_read(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);
  78. var
  79. helpins : tai;
  80. tmpref : treference;
  81. helplist : tasmlist;
  82. hreg : tregister;
  83. begin
  84. if abs(spilltemp.offset)>32767 then
  85. begin
  86. helplist:=tasmlist.create;
  87. if getregtype(tempreg)=R_INTREGISTER then
  88. hreg:=tempreg
  89. else
  90. hreg:=cg.getintregister(helplist,OS_ADDR);
  91. helplist.concat(taicpu.op_reg_const(A_LUI,hreg,spilltemp.offset shr 16));
  92. helplist.concat(taicpu.op_reg_reg_const(A_ORI,hreg,hreg,spilltemp.offset and $FFFF));
  93. helplist.concat(taicpu.op_reg_reg_reg(A_ADDU,hreg,hreg,spilltemp.base));
  94. reference_reset_base(tmpref,hreg,0,sizeof(aint));
  95. helpins:=spilling_create_load(tmpref,tempreg);
  96. helplist.concat(helpins);
  97. list.insertlistafter(pos,helplist);
  98. helplist.free;
  99. end
  100. else
  101. inherited do_spill_read(list,pos,spilltemp,tempreg);
  102. end;
  103. procedure trgcpu.do_spill_written(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);
  104. var
  105. tmpref : treference;
  106. helplist : tasmlist;
  107. hreg : tregister;
  108. begin
  109. if abs(spilltemp.offset)>32767 then
  110. begin
  111. helplist:=tasmlist.create;
  112. if getregtype(tempreg)=R_INTREGISTER then
  113. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  114. else
  115. hreg:=cg.getintregister(helplist,OS_ADDR);
  116. helplist.concat(taicpu.op_reg_const(A_LUI,hreg,spilltemp.offset shr 16));
  117. helplist.concat(taicpu.op_reg_reg_const(A_ORI,hreg,hreg,spilltemp.offset and $FFFF));
  118. helplist.concat(taicpu.op_reg_reg_reg(A_ADDU,hreg,hreg,spilltemp.base));
  119. reference_reset_base(tmpref,hreg,0,sizeof(aint));
  120. helplist.concat(spilling_create_store(tempreg,tmpref));
  121. if getregtype(tempreg)=R_INTREGISTER then
  122. ungetregisterinline(helplist,hreg);
  123. list.insertlistafter(pos,helplist);
  124. helplist.free;
  125. end
  126. else
  127. inherited do_spill_written(list,pos,spilltemp,tempreg);
  128. end;
  129. function trgcpu.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  130. begin
  131. result:=false;
  132. { Replace 'move orgreg,src' with 'sw src,spilltemp'
  133. and 'move dst,orgreg' with 'lw dst,spilltemp' }
  134. { TODO: A_MOV_S and A_MOV_D for float registers are also replaceable }
  135. if (instr.opcode<>A_MOVE) or (abs(spilltemp.offset)>32767) then
  136. exit;
  137. if (instr.ops<>2) or
  138. (instr.oper[0]^.typ<>top_reg) or
  139. (instr.oper[1]^.typ<>top_reg) or
  140. (getregtype(instr.oper[0]^.reg)<>regtype) or
  141. (getregtype(instr.oper[1]^.reg)<>regtype) then
  142. InternalError(2013061001);
  143. if get_alias(getsupreg(instr.oper[1]^.reg))=orgreg then
  144. begin
  145. instr.opcode:=A_LW;
  146. end
  147. else if get_alias(getsupreg(instr.oper[0]^.reg))=orgreg then
  148. begin
  149. instr.opcode:=A_SW;
  150. instr.oper[0]^:=instr.oper[1]^;
  151. end
  152. else
  153. InternalError(2013061002);
  154. instr.oper[1]^.typ:=top_ref;
  155. new(instr.oper[1]^.ref);
  156. instr.oper[1]^.ref^:=spilltemp;
  157. result:=true;
  158. end;
  159. procedure trgintcpu.add_cpu_interferences(p: tai);
  160. var
  161. supreg: tsuperregister;
  162. begin
  163. if p.typ<>ait_instruction then
  164. exit;
  165. if (taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and
  166. (getregtype(taicpu(p).oper[0]^.reg)=regtype) and
  167. (taicpu(p).spilling_get_operation_type(0) in [operand_write,operand_readwrite]) then
  168. begin
  169. { prevent merging registers with frame/stack pointer, $zero and $at
  170. if an instruction writes to the register }
  171. supreg:=getsupreg(taicpu(p).oper[0]^.reg);
  172. add_edge(supreg,RS_STACK_POINTER_REG);
  173. add_edge(supreg,RS_FRAME_POINTER_REG);
  174. add_edge(supreg,RS_R0);
  175. add_edge(supreg,RS_R1);
  176. end;
  177. end;
  178. end.