rgcpu.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. This unit implements the avr 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. }
  18. unit rgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. aasmbase,aasmtai,aasmdata,aasmcpu,aasmsym,
  23. cgbase,cgutils,
  24. cpubase,
  25. rgobj;
  26. type
  27. trgcpu = class(trgobj)
  28. procedure add_constraints(reg:tregister);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. trgintcpu = class(trgcpu)
  34. procedure add_cpu_interferences(p : tai);override;
  35. end;
  36. implementation
  37. uses
  38. verbose, cutils,
  39. globals,
  40. cgobj,
  41. procinfo,
  42. cpuinfo;
  43. procedure trgcpu.add_constraints(reg:tregister);
  44. {var
  45. supreg,i : Tsuperregister;}
  46. begin
  47. case getsubreg(reg) of
  48. { Let 64bit floats conflict with all odd float regs }
  49. R_SUBFD:
  50. begin
  51. {
  52. supreg:=getsupreg(reg);
  53. i:=RS_F1;
  54. while (i<=RS_F31) do
  55. begin
  56. add_edge(supreg,i);
  57. inc(i,2);
  58. end;
  59. }
  60. end;
  61. { Let 64bit ints conflict with all odd int regs }
  62. R_SUBQ:
  63. begin
  64. {
  65. supreg:=getsupreg(reg);
  66. i:=RS_G1;
  67. while (i<=RS_I7) do
  68. begin
  69. add_edge(supreg,i);
  70. inc(i,2);
  71. end;
  72. }
  73. end;
  74. end;
  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. begin
  82. if (abs(spilltemp.offset)>63) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  83. begin
  84. helplist:=TAsmList.create;
  85. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R26,lo(word(spilltemp.offset))));
  86. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R27,hi(word(spilltemp.offset))));
  87. helplist.concat(taicpu.op_reg_reg(A_ADD,NR_R26,spilltemp.base));
  88. helplist.concat(taicpu.op_reg_reg(A_ADC,NR_R27,cg.GetNextReg(spilltemp.base)));
  89. reference_reset_base(tmpref,NR_R26,0,spilltemp.temppos,1,[]);
  90. helpins:=spilling_create_load(tmpref,tempreg);
  91. helplist.concat(helpins);
  92. list.insertlistafter(pos,helplist);
  93. helplist.free;
  94. end
  95. else
  96. inherited;
  97. end;
  98. procedure trgcpu.do_spill_written(list: TAsmList; pos: tai; const spilltemp: treference; tempreg: tregister; orgsupreg: tsuperregister);
  99. var
  100. tmpref : treference;
  101. helplist : TAsmList;
  102. begin
  103. if (abs(spilltemp.offset)>63) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  104. begin
  105. helplist:=TAsmList.create;
  106. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R26,lo(word(spilltemp.offset))));
  107. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R27,hi(word(spilltemp.offset))));
  108. helplist.concat(taicpu.op_reg_reg(A_ADD,NR_R26,spilltemp.base));
  109. helplist.concat(taicpu.op_reg_reg(A_ADC,NR_R27,cg.GetNextReg(spilltemp.base)));
  110. reference_reset_base(tmpref,NR_R26,0,spilltemp.temppos,1,[]);
  111. helplist.concat(spilling_create_store(tempreg,tmpref));
  112. list.insertlistafter(pos,helplist);
  113. helplist.free;
  114. end
  115. else
  116. inherited;
  117. end;
  118. procedure trgintcpu.add_cpu_interferences(p : tai);
  119. var
  120. r : tsuperregister;
  121. begin
  122. if p.typ=ait_instruction then
  123. begin
  124. case taicpu(p).opcode of
  125. A_CPI,
  126. A_ANDI,
  127. A_ORI,
  128. A_SUBI,
  129. A_SBCI,
  130. A_LDI:
  131. for r:=RS_R0 to RS_R15 do
  132. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  133. A_STS:
  134. for r:=RS_R0 to RS_R15 do
  135. add_edge(r,GetSupReg(taicpu(p).oper[1]^.reg));
  136. A_ADIW:
  137. for r:=RS_R0 to RS_R31 do
  138. if not (r in [RS_R24,RS_R26,RS_R28,RS_R30]) then
  139. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  140. A_MULS:
  141. begin
  142. for r:=RS_R0 to RS_R15 do
  143. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  144. for r:=RS_R0 to RS_R15 do
  145. add_edge(r,GetSupReg(taicpu(p).oper[1]^.reg));
  146. end;
  147. A_LDD:
  148. for r:=RS_R0 to RS_R31 do
  149. if not (r in [RS_R28,RS_R30]) then
  150. add_edge(r,GetSupReg(taicpu(p).oper[1]^.ref^.base));
  151. A_STD:
  152. for r:=RS_R0 to RS_R31 do
  153. if not (r in [RS_R28,RS_R30]) then
  154. add_edge(r,GetSupReg(taicpu(p).oper[0]^.ref^.base));
  155. end;
  156. end;
  157. end;
  158. function trgcpu.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  159. begin
  160. result:=false;
  161. if not(spilltemp.offset in [0..63]) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  162. exit;
  163. { Replace 'mov dst,orgreg' with 'ldd dst,spilltemp'
  164. and 'mov orgreg,src' with 'std spilltemp,src' }
  165. with instr do
  166. begin
  167. if (opcode=A_MOV) and (ops=2) and (oper[1]^.typ=top_reg) and (oper[0]^.typ=top_reg) then
  168. begin
  169. if (getregtype(oper[0]^.reg)=regtype) and
  170. (get_alias(getsupreg(oper[0]^.reg))=orgreg) and
  171. (get_alias(getsupreg(oper[1]^.reg))<>orgreg) then
  172. begin
  173. instr.loadref(0,spilltemp);
  174. opcode:=A_STD;
  175. result:=true;
  176. end
  177. else if (getregtype(oper[1]^.reg)=regtype) and
  178. (get_alias(getsupreg(oper[1]^.reg))=orgreg) and
  179. (get_alias(getsupreg(oper[0]^.reg))<>orgreg) then
  180. begin
  181. instr.loadref(1,spilltemp);
  182. opcode:=A_LDD;
  183. result:=true;
  184. end;
  185. end;
  186. end;
  187. end;
  188. end.