rgcpu.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. function get_spill_subreg(r : tregister) : tsubregister;override;
  28. procedure do_spill_read(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  29. procedure do_spill_written(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  30. function do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;override;
  31. end;
  32. trgintcpu=class(trgcpu)
  33. procedure add_cpu_interferences(p:tai);override;
  34. end;
  35. implementation
  36. uses
  37. globtype,
  38. verbose,cutils,
  39. cgobj;
  40. function trgcpu.get_spill_subreg(r : tregister) : tsubregister;
  41. begin
  42. if getregtype(r)=R_FPUREGISTER then
  43. result:=getsubreg(r)
  44. else
  45. result:=defaultsub;
  46. end;
  47. procedure trgcpu.do_spill_read(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);
  48. var
  49. helpins : tai;
  50. tmpref : treference;
  51. helplist : tasmlist;
  52. hreg : tregister;
  53. begin
  54. if abs(spilltemp.offset)>32767 then
  55. begin
  56. helplist:=tasmlist.create;
  57. if getregtype(tempreg)=R_INTREGISTER then
  58. hreg:=tempreg
  59. else
  60. hreg:=cg.getintregister(helplist,OS_ADDR);
  61. helplist.concat(taicpu.op_reg_const(A_LUI,hreg,spilltemp.offset shr 16));
  62. helplist.concat(taicpu.op_reg_reg_const(A_ORI,hreg,hreg,spilltemp.offset and $FFFF));
  63. helplist.concat(taicpu.op_reg_reg_reg(A_ADDU,hreg,hreg,spilltemp.base));
  64. reference_reset_base(tmpref,hreg,0,sizeof(aint));
  65. helpins:=spilling_create_load(tmpref,tempreg);
  66. helplist.concat(helpins);
  67. list.insertlistafter(pos,helplist);
  68. helplist.free;
  69. end
  70. else
  71. inherited do_spill_read(list,pos,spilltemp,tempreg);
  72. end;
  73. procedure trgcpu.do_spill_written(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);
  74. var
  75. tmpref : treference;
  76. helplist : tasmlist;
  77. hreg : tregister;
  78. begin
  79. if abs(spilltemp.offset)>32767 then
  80. begin
  81. helplist:=tasmlist.create;
  82. if getregtype(tempreg)=R_INTREGISTER then
  83. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  84. else
  85. hreg:=cg.getintregister(helplist,OS_ADDR);
  86. helplist.concat(taicpu.op_reg_const(A_LUI,hreg,spilltemp.offset shr 16));
  87. helplist.concat(taicpu.op_reg_reg_const(A_ORI,hreg,hreg,spilltemp.offset and $FFFF));
  88. helplist.concat(taicpu.op_reg_reg_reg(A_ADDU,hreg,hreg,spilltemp.base));
  89. reference_reset_base(tmpref,hreg,0,sizeof(aint));
  90. helplist.concat(spilling_create_store(tempreg,tmpref));
  91. if getregtype(tempreg)=R_INTREGISTER then
  92. ungetregisterinline(helplist,hreg);
  93. list.insertlistafter(pos,helplist);
  94. helplist.free;
  95. end
  96. else
  97. inherited do_spill_written(list,pos,spilltemp,tempreg);
  98. end;
  99. function trgcpu.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  100. begin
  101. result:=false;
  102. { Replace 'move orgreg,src' with 'sw src,spilltemp'
  103. and 'move dst,orgreg' with 'lw dst,spilltemp' }
  104. if (not (instr.opcode in [A_MOVE,A_MOV_S,A_MOV_D,A_MTC1])) or (abs(spilltemp.offset)>32767) then
  105. exit;
  106. if (instr.ops<>2) or
  107. (instr.oper[0]^.typ<>top_reg) or
  108. (instr.oper[1]^.typ<>top_reg) then
  109. InternalError(2013061001);
  110. if (getregtype(instr.oper[0]^.reg)<>regtype) or
  111. (getregtype(instr.oper[1]^.reg)<>regtype) then
  112. begin
  113. if (instr.opcode=A_MTC1) then
  114. begin
  115. { TODO: MTC1 src,orgreg ==> SW src,0/4(spilltemp) (endian-dependent!!) }
  116. if (regtype=R_FPUREGISTER) then
  117. exit;
  118. end
  119. else
  120. InternalError(2013061003);
  121. end;
  122. if get_alias(getsupreg(instr.oper[1]^.reg))=orgreg then
  123. begin
  124. case instr.opcode of
  125. A_MOVE: instr.opcode:=A_LW;
  126. A_MOV_S: instr.opcode:=A_LWC1;
  127. A_MOV_D: instr.opcode:=A_LDC1;
  128. else
  129. InternalError(2013061004);
  130. end;
  131. end
  132. else if get_alias(getsupreg(instr.oper[0]^.reg))=orgreg then
  133. begin
  134. case instr.opcode of
  135. A_MOVE: instr.opcode:=A_SW;
  136. A_MOV_S: instr.opcode:=A_SWC1;
  137. A_MOV_D: instr.opcode:=A_SDC1;
  138. A_MTC1:
  139. begin
  140. if (getregtype(instr.oper[0]^.reg)<>R_INTREGISTER) then
  141. InternalError(2013061006);
  142. instr.opcode:=A_LWC1;
  143. end
  144. else
  145. InternalError(2013061005);
  146. end;
  147. instr.oper[0]^:=instr.oper[1]^;
  148. end
  149. else
  150. InternalError(2013061002);
  151. instr.oper[1]^.typ:=top_ref;
  152. new(instr.oper[1]^.ref);
  153. instr.oper[1]^.ref^:=spilltemp;
  154. result:=true;
  155. end;
  156. procedure trgintcpu.add_cpu_interferences(p: tai);
  157. var
  158. supreg: tsuperregister;
  159. begin
  160. if p.typ<>ait_instruction then
  161. exit;
  162. if (taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and
  163. (getregtype(taicpu(p).oper[0]^.reg)=regtype) and
  164. (taicpu(p).spilling_get_operation_type(0) in [operand_write,operand_readwrite]) then
  165. begin
  166. { prevent merging registers with frame/stack pointer, $zero and $at
  167. if an instruction writes to the register }
  168. supreg:=getsupreg(taicpu(p).oper[0]^.reg);
  169. add_edge(supreg,RS_STACK_POINTER_REG);
  170. add_edge(supreg,RS_FRAME_POINTER_REG);
  171. add_edge(supreg,RS_R0);
  172. add_edge(supreg,RS_R1);
  173. end;
  174. end;
  175. end.