rgcpu.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. {
  2. Copyright (c) 1998-2003 by Florian Klaempfl
  3. This unit implements the arm 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,
  23. cgbase,cgutils,
  24. cpubase,
  25. rgobj;
  26. type
  27. trgcpu = class(trgobj)
  28. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  29. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  30. end;
  31. trgcputhumb2 = class(trgobj)
  32. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  33. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  34. end;
  35. trgintcpu = class(trgcpu)
  36. procedure add_cpu_interferences(p : tai);override;
  37. end;
  38. implementation
  39. uses
  40. verbose, cutils,globtype,
  41. cgobj,
  42. procinfo;
  43. procedure trgcpu.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  44. var
  45. tmpref : treference;
  46. helplist : TAsmList;
  47. l : tasmlabel;
  48. hreg : tregister;
  49. begin
  50. { don't load spilled register between
  51. mov lr,pc
  52. mov pc,r4
  53. but befure the mov lr,pc
  54. }
  55. if assigned(pos.previous) and
  56. (pos.typ=ait_instruction) and
  57. (taicpu(pos).opcode=A_MOV) and
  58. (taicpu(pos).oper[0]^.typ=top_reg) and
  59. (taicpu(pos).oper[0]^.reg=NR_R14) and
  60. (taicpu(pos).oper[1]^.typ=top_reg) and
  61. (taicpu(pos).oper[1]^.reg=NR_PC) then
  62. pos:=tai(pos.previous);
  63. if abs(spilltemp.offset)>4095 then
  64. begin
  65. helplist:=TAsmList.create;
  66. reference_reset(tmpref,sizeof(aint));
  67. { create consts entry }
  68. current_asmdata.getjumplabel(l);
  69. cg.a_label(current_procinfo.aktlocaldata,l);
  70. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  71. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  72. { load consts entry }
  73. if getregtype(tempreg)=R_INTREGISTER then
  74. hreg:=getregisterinline(helplist,R_SUBWHOLE)
  75. else
  76. hreg:=cg.getintregister(helplist,OS_ADDR);
  77. tmpref.symbol:=l;
  78. tmpref.base:=NR_R15;
  79. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  80. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  81. tmpref.index:=hreg;
  82. if spilltemp.index<>NR_NO then
  83. internalerror(200401263);
  84. helplist.concat(spilling_create_load(tmpref,tempreg));
  85. if getregtype(tempreg)=R_INTREGISTER then
  86. ungetregisterinline(helplist,hreg);
  87. list.insertlistafter(pos,helplist);
  88. helplist.free;
  89. end
  90. else
  91. inherited do_spill_read(list,pos,spilltemp,tempreg);
  92. end;
  93. procedure trgcpu.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  94. var
  95. tmpref : treference;
  96. helplist : TAsmList;
  97. l : tasmlabel;
  98. hreg : tregister;
  99. begin
  100. if abs(spilltemp.offset)>4095 then
  101. begin
  102. helplist:=TAsmList.create;
  103. reference_reset(tmpref,sizeof(aint));
  104. { create consts entry }
  105. current_asmdata.getjumplabel(l);
  106. cg.a_label(current_procinfo.aktlocaldata,l);
  107. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  108. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  109. { load consts entry }
  110. if getregtype(tempreg)=R_INTREGISTER then
  111. hreg:=getregisterinline(helplist,R_SUBWHOLE)
  112. else
  113. hreg:=cg.getintregister(helplist,OS_ADDR);
  114. tmpref.symbol:=l;
  115. tmpref.base:=NR_R15;
  116. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  117. if spilltemp.index<>NR_NO then
  118. internalerror(200401263);
  119. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(pint));
  120. tmpref.index:=hreg;
  121. helplist.concat(spilling_create_store(tempreg,tmpref));
  122. if getregtype(tempreg)=R_INTREGISTER then
  123. ungetregisterinline(helplist,hreg);
  124. list.insertlistafter(pos,helplist);
  125. helplist.free;
  126. end
  127. else
  128. inherited do_spill_written(list,pos,spilltemp,tempreg);
  129. end;
  130. procedure trgcputhumb2.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  131. var
  132. tmpref : treference;
  133. helplist : TAsmList;
  134. l : tasmlabel;
  135. hreg : tregister;
  136. begin
  137. { don't load spilled register between
  138. mov lr,pc
  139. mov pc,r4
  140. but befure the mov lr,pc
  141. }
  142. if assigned(pos.previous) and
  143. (pos.typ=ait_instruction) and
  144. (taicpu(pos).opcode=A_MOV) and
  145. (taicpu(pos).oper[0]^.typ=top_reg) and
  146. (taicpu(pos).oper[0]^.reg=NR_R14) and
  147. (taicpu(pos).oper[1]^.typ=top_reg) and
  148. (taicpu(pos).oper[1]^.reg=NR_PC) then
  149. pos:=tai(pos.previous);
  150. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  151. begin
  152. helplist:=TAsmList.create;
  153. reference_reset(tmpref,sizeof(aint));
  154. { create consts entry }
  155. current_asmdata.getjumplabel(l);
  156. cg.a_label(current_procinfo.aktlocaldata,l);
  157. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  158. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  159. { load consts entry }
  160. if getregtype(tempreg)=R_INTREGISTER then
  161. hreg:=getregisterinline(helplist,R_SUBWHOLE)
  162. else
  163. hreg:=cg.getintregister(helplist,OS_ADDR);
  164. tmpref.symbol:=l;
  165. tmpref.base:=NR_R15;
  166. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  167. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  168. tmpref.index:=hreg;
  169. if spilltemp.index<>NR_NO then
  170. internalerror(200401263);
  171. helplist.concat(spilling_create_load(tmpref,tempreg));
  172. if getregtype(tempreg)=R_INTREGISTER then
  173. ungetregisterinline(helplist,hreg);
  174. list.insertlistafter(pos,helplist);
  175. helplist.free;
  176. end
  177. else
  178. inherited do_spill_read(list,pos,spilltemp,tempreg);
  179. end;
  180. procedure trgcputhumb2.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  181. var
  182. tmpref : treference;
  183. helplist : TAsmList;
  184. l : tasmlabel;
  185. hreg : tregister;
  186. begin
  187. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  188. begin
  189. helplist:=TAsmList.create;
  190. reference_reset(tmpref,sizeof(aint));
  191. { create consts entry }
  192. current_asmdata.getjumplabel(l);
  193. cg.a_label(current_procinfo.aktlocaldata,l);
  194. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  195. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  196. { load consts entry }
  197. if getregtype(tempreg)=R_INTREGISTER then
  198. hreg:=getregisterinline(helplist,R_SUBWHOLE)
  199. else
  200. hreg:=cg.getintregister(helplist,OS_ADDR);
  201. tmpref.symbol:=l;
  202. tmpref.base:=NR_R15;
  203. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  204. if spilltemp.index<>NR_NO then
  205. internalerror(200401263);
  206. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(pint));
  207. tmpref.index:=hreg;
  208. helplist.concat(spilling_create_store(tempreg,tmpref));
  209. if getregtype(tempreg)=R_INTREGISTER then
  210. ungetregisterinline(helplist,hreg);
  211. list.insertlistafter(pos,helplist);
  212. helplist.free;
  213. end
  214. else
  215. inherited do_spill_written(list,pos,spilltemp,tempreg);
  216. end;
  217. procedure trgintcpu.add_cpu_interferences(p : tai);
  218. var
  219. r : tregister;
  220. begin
  221. if p.typ=ait_instruction then
  222. begin
  223. case taicpu(p).opcode of
  224. A_MUL:
  225. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  226. A_UMULL,
  227. A_UMLAL,
  228. A_SMULL,
  229. A_SMLAL:
  230. begin
  231. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  232. add_edge(getsupreg(taicpu(p).oper[1]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  233. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  234. end;
  235. A_LDRB,
  236. A_STRB,
  237. A_STR,
  238. A_LDR,
  239. A_LDRH,
  240. A_STRH:
  241. { don't mix up the framepointer and stackpointer with pre/post indexed operations }
  242. if (taicpu(p).oper[1]^.typ=top_ref) and
  243. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  244. begin
  245. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(current_procinfo.framepointer));
  246. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  247. { while compiling the compiler. }
  248. r:=NR_STACK_POINTER_REG;
  249. if current_procinfo.framepointer<>r then
  250. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(r));
  251. end;
  252. end;
  253. end;
  254. end;
  255. end.