rgcpu.pas 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. supreg:=getsupreg(reg);
  65. {
  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. hreg : tregister;
  82. begin
  83. if (abs(spilltemp.offset)>63) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  84. begin
  85. helplist:=TAsmList.create;
  86. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R26,lo(word(spilltemp.offset))));
  87. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R27,hi(word(spilltemp.offset))));
  88. helplist.concat(taicpu.op_reg_reg(A_ADD,NR_R26,spilltemp.base));
  89. helplist.concat(taicpu.op_reg_reg(A_ADC,NR_R27,cg.GetNextReg(spilltemp.base)));
  90. reference_reset_base(tmpref,NR_R26,0,spilltemp.temppos,1,[]);
  91. helpins:=spilling_create_load(tmpref,tempreg);
  92. helplist.concat(helpins);
  93. list.insertlistafter(pos,helplist);
  94. helplist.free;
  95. end
  96. else
  97. inherited;
  98. end;
  99. procedure trgcpu.do_spill_written(list: TAsmList; pos: tai; const spilltemp: treference; tempreg: tregister; orgsupreg: tsuperregister);
  100. var
  101. tmpref : treference;
  102. helplist : TAsmList;
  103. hreg : tregister;
  104. begin
  105. if (abs(spilltemp.offset)>63) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  106. begin
  107. helplist:=TAsmList.create;
  108. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R26,lo(word(spilltemp.offset))));
  109. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R27,hi(word(spilltemp.offset))));
  110. helplist.concat(taicpu.op_reg_reg(A_ADD,NR_R26,spilltemp.base));
  111. helplist.concat(taicpu.op_reg_reg(A_ADC,NR_R27,cg.GetNextReg(spilltemp.base)));
  112. reference_reset_base(tmpref,NR_R26,0,spilltemp.temppos,1,[]);
  113. helplist.concat(spilling_create_store(tempreg,tmpref));
  114. list.insertlistafter(pos,helplist);
  115. helplist.free;
  116. end
  117. else
  118. inherited;
  119. end;
  120. procedure trgintcpu.add_cpu_interferences(p : tai);
  121. var
  122. r : tsuperregister;
  123. begin
  124. if p.typ=ait_instruction then
  125. begin
  126. case taicpu(p).opcode of
  127. A_CPI,
  128. A_ANDI,
  129. A_ORI,
  130. A_SUBI,
  131. A_SBCI,
  132. A_LDI:
  133. for r:=RS_R0 to RS_R15 do
  134. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  135. A_STS:
  136. for r:=RS_R0 to RS_R15 do
  137. add_edge(r,GetSupReg(taicpu(p).oper[1]^.reg));
  138. A_ADIW:
  139. for r:=RS_R0 to RS_R31 do
  140. if not (r in [RS_R24,RS_R26,RS_R28,RS_R30]) then
  141. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  142. A_MULS:
  143. begin
  144. for r:=RS_R0 to RS_R15 do
  145. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  146. for r:=RS_R0 to RS_R15 do
  147. add_edge(r,GetSupReg(taicpu(p).oper[1]^.reg));
  148. end;
  149. A_LDD:
  150. for r:=RS_R0 to RS_R31 do
  151. if not (r in [RS_R28,RS_R30]) then
  152. add_edge(r,GetSupReg(taicpu(p).oper[1]^.ref^.base));
  153. A_STD:
  154. for r:=RS_R0 to RS_R31 do
  155. if not (r in [RS_R28,RS_R30]) then
  156. add_edge(r,GetSupReg(taicpu(p).oper[0]^.ref^.base));
  157. end;
  158. end;
  159. end;
  160. function trgcpu.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  161. var
  162. b : byte;
  163. begin
  164. result:=false;
  165. if not(spilltemp.offset in [0..63]) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  166. exit;
  167. { Replace 'mov dst,orgreg' with 'ldd dst,spilltemp'
  168. and 'mov orgreg,src' with 'std spilltemp,src' }
  169. with instr do
  170. begin
  171. if (opcode=A_MOV) and (ops=2) and (oper[1]^.typ=top_reg) and (oper[0]^.typ=top_reg) then
  172. begin
  173. if (getregtype(oper[0]^.reg)=regtype) and
  174. (get_alias(getsupreg(oper[0]^.reg))=orgreg) and
  175. (get_alias(getsupreg(oper[1]^.reg))<>orgreg) then
  176. begin
  177. instr.loadref(0,spilltemp);
  178. opcode:=A_STD;
  179. result:=true;
  180. end
  181. else if (getregtype(oper[1]^.reg)=regtype) and
  182. (get_alias(getsupreg(oper[1]^.reg))=orgreg) and
  183. (get_alias(getsupreg(oper[0]^.reg))<>orgreg) then
  184. begin
  185. instr.loadref(1,spilltemp);
  186. opcode:=A_LDD;
  187. result:=true;
  188. end;
  189. end;
  190. end;
  191. end;
  192. end.