rgcpu.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. globtype, 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. ofs : asizeint;
  103. begin
  104. if (abs(spilltemp.offset)>63) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  105. begin
  106. helplist:=TAsmList.create;
  107. ofs:=spilltemp.offset;
  108. helplist.concat(tai_regalloc.alloc(NR_R26,nil));
  109. helplist.concat(tai_regalloc.alloc(NR_R27,nil));
  110. if (CPUAVR_HAS_ADIW in cpu_capabilities[current_settings.cputype]) and (ofs>0) and (ofs<=126) then
  111. begin
  112. { this might be converted into movw }
  113. helplist.concat(taicpu.op_reg_reg(A_MOV,NR_R26,spilltemp.base));
  114. helplist.concat(taicpu.op_reg_reg(A_MOV,NR_R27,cg.GetNextReg(spilltemp.base)));
  115. while ofs>0 do
  116. begin
  117. helplist.concat(taicpu.op_reg_const(A_ADIW,NR_R26,min(63,ofs)));
  118. dec(ofs,min(63,ofs));
  119. end;
  120. end
  121. else
  122. begin
  123. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R26,lo(word(spilltemp.offset))));
  124. helplist.concat(taicpu.op_reg_const(A_LDI,NR_R27,hi(word(spilltemp.offset))));
  125. helplist.concat(taicpu.op_reg_reg(A_ADD,NR_R26,spilltemp.base));
  126. helplist.concat(taicpu.op_reg_reg(A_ADC,NR_R27,cg.GetNextReg(spilltemp.base)));
  127. end;
  128. reference_reset_base(tmpref,NR_R26,0,spilltemp.temppos,1,[]);
  129. helplist.concat(spilling_create_store(tempreg,tmpref));
  130. helplist.concat(tai_regalloc.dealloc(NR_R26,nil));
  131. helplist.concat(tai_regalloc.dealloc(NR_R27,nil));
  132. list.insertlistafter(pos,helplist);
  133. helplist.free;
  134. end
  135. else
  136. inherited;
  137. end;
  138. procedure trgintcpu.add_cpu_interferences(p : tai);
  139. var
  140. r : tsuperregister;
  141. begin
  142. if p.typ=ait_instruction then
  143. begin
  144. case taicpu(p).opcode of
  145. A_CPI,
  146. A_ANDI,
  147. A_ORI,
  148. A_SUBI,
  149. A_SBCI,
  150. A_LDI:
  151. for r:=RS_R0 to RS_R15 do
  152. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  153. A_STS:
  154. for r:=RS_R0 to RS_R15 do
  155. add_edge(r,GetSupReg(taicpu(p).oper[1]^.reg));
  156. A_ADIW:
  157. for r:=RS_R0 to RS_R31 do
  158. if not (r in [RS_R24,RS_R26,RS_R28,RS_R30]) then
  159. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  160. A_MULS:
  161. begin
  162. for r:=RS_R0 to RS_R15 do
  163. add_edge(r,GetSupReg(taicpu(p).oper[0]^.reg));
  164. for r:=RS_R0 to RS_R15 do
  165. add_edge(r,GetSupReg(taicpu(p).oper[1]^.reg));
  166. end;
  167. A_LDD:
  168. for r:=RS_R0 to RS_R31 do
  169. if not (r in [RS_R28,RS_R30]) then
  170. add_edge(r,GetSupReg(taicpu(p).oper[1]^.ref^.base));
  171. A_STD:
  172. for r:=RS_R0 to RS_R31 do
  173. if not (r in [RS_R28,RS_R30]) then
  174. add_edge(r,GetSupReg(taicpu(p).oper[0]^.ref^.base));
  175. end;
  176. end;
  177. end;
  178. function trgcpu.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  179. begin
  180. result:=false;
  181. if not(spilltemp.offset in [0..63]) or (CPUAVR_16_REGS in cpu_capabilities[current_settings.cputype]) then
  182. exit;
  183. { Replace 'mov dst,orgreg' with 'ldd dst,spilltemp'
  184. and 'mov orgreg,src' with 'std spilltemp,src' }
  185. with instr do
  186. begin
  187. if (opcode=A_MOV) and (ops=2) and (oper[1]^.typ=top_reg) and (oper[0]^.typ=top_reg) then
  188. begin
  189. if (getregtype(oper[0]^.reg)=regtype) and
  190. (get_alias(getsupreg(oper[0]^.reg))=orgreg) and
  191. (get_alias(getsupreg(oper[1]^.reg))<>orgreg) then
  192. begin
  193. instr.loadref(0,spilltemp);
  194. opcode:=A_STD;
  195. result:=true;
  196. end
  197. else if (getregtype(oper[1]^.reg)=regtype) and
  198. (get_alias(getsupreg(oper[1]^.reg))=orgreg) and
  199. (get_alias(getsupreg(oper[0]^.reg))<>orgreg) then
  200. begin
  201. instr.loadref(1,spilltemp);
  202. opcode:=A_LDD;
  203. result:=true;
  204. end;
  205. end;
  206. end;
  207. end;
  208. end.