rgcpu.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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,aasmsym,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; orgsupreg: tsuperregister); override;
  29. procedure do_spill_written(list: TAsmList; pos: tai; const spilltemp: treference; tempreg: tregister; orgsupreg: tsuperregister); override;
  30. function do_spill_replace(list: TAsmList; instr: tai_cpu_abstract_sym; 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; orgsupreg: tsuperregister);
  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;
  72. end;
  73. procedure trgcpu.do_spill_written(list: TAsmList; pos: tai; const spilltemp: treference; tempreg: tregister; orgsupreg: tsuperregister);
  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;
  98. end;
  99. function trgcpu.do_spill_replace(list: TAsmList; instr: tai_cpu_abstract_sym; 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. { TODO: A_MOV_S and A_MOV_D for float registers are also replaceable }
  105. if (instr.opcode<>A_MOVE) or (abs(spilltemp.offset)>32767) then
  106. exit;
  107. if (instr.ops<>2) or
  108. (instr.oper[0]^.typ<>top_reg) or
  109. (instr.oper[1]^.typ<>top_reg) or
  110. (getregtype(instr.oper[0]^.reg)<>regtype) or
  111. (getregtype(instr.oper[1]^.reg)<>regtype) then
  112. InternalError(2013061001);
  113. if get_alias(getsupreg(instr.oper[1]^.reg))=orgreg then
  114. begin
  115. instr.opcode:=A_LW;
  116. end
  117. else if get_alias(getsupreg(instr.oper[0]^.reg))=orgreg then
  118. begin
  119. instr.opcode:=A_SW;
  120. instr.oper[0]^:=instr.oper[1]^;
  121. end
  122. else
  123. InternalError(2013061002);
  124. instr.oper[1]^.typ:=top_ref;
  125. new(instr.oper[1]^.ref);
  126. instr.oper[1]^.ref^:=spilltemp;
  127. result:=true;
  128. end;
  129. procedure trgintcpu.add_cpu_interferences(p: tai);
  130. var
  131. supreg: tsuperregister;
  132. begin
  133. if p.typ<>ait_instruction then
  134. exit;
  135. if (taicpu(p).ops>=1) and (taicpu(p).oper[0]^.typ=top_reg) and
  136. (getregtype(taicpu(p).oper[0]^.reg)=regtype) and
  137. (taicpu(p).spilling_get_operation_type(0) in [operand_write,operand_readwrite]) then
  138. begin
  139. { prevent merging registers with frame/stack pointer, $zero and $at
  140. if an instruction writes to the register }
  141. supreg:=getsupreg(taicpu(p).oper[0]^.reg);
  142. add_edge(supreg,RS_STACK_POINTER_REG);
  143. add_edge(supreg,RS_FRAME_POINTER_REG);
  144. add_edge(supreg,RS_R0);
  145. add_edge(supreg,RS_R1);
  146. end;
  147. end;
  148. end.