rgcpu.pas 9.6 KB

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