rgcpu.pas 9.7 KB

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