2
0

rgcpu.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. procedure add_constraints(reg:tregister);override;
  31. function get_spill_subreg(r:tregister) : tsubregister;override;
  32. end;
  33. trgcputhumb2 = class(trgobj)
  34. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  35. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);override;
  36. end;
  37. trgintcputhumb2 = class(trgcputhumb2)
  38. procedure add_cpu_interferences(p : tai);override;
  39. end;
  40. trgintcpu = class(trgcpu)
  41. procedure add_cpu_interferences(p : tai);override;
  42. end;
  43. implementation
  44. uses
  45. verbose, cutils,globtype,
  46. cgobj,
  47. procinfo;
  48. procedure trgintcputhumb2.add_cpu_interferences(p: tai);
  49. begin
  50. if p.typ=ait_instruction then
  51. begin
  52. case taicpu(p).opcode of
  53. A_ADD:
  54. begin
  55. if taicpu(p).ops = 3 then
  56. begin
  57. if (taicpu(p).oper[0]^.typ = top_reg) and
  58. (taicpu(p).oper[1]^.typ = top_reg) and
  59. (taicpu(p).oper[2]^.typ in [top_reg, top_shifterop]) then
  60. begin
  61. { if d == 13 || (d == 15 && S == ‘0’) || n == 15 || m IN [13,15] then UNPREDICTABLE; }
  62. add_edge(getsupreg(taicpu(p).oper[0]^.reg), RS_R13);
  63. if taicpu(p).oppostfix <> PF_S then
  64. add_edge(getsupreg(taicpu(p).oper[0]^.reg), RS_R15);
  65. add_edge(getsupreg(taicpu(p).oper[1]^.reg), RS_R15);
  66. if (taicpu(p).oper[2]^.typ = top_shifterop) and
  67. (taicpu(p).oper[2]^.shifterop^.rs <> NR_NO) then
  68. begin
  69. add_edge(getsupreg(taicpu(p).oper[2]^.shifterop^.rs), RS_R13);
  70. add_edge(getsupreg(taicpu(p).oper[2]^.shifterop^.rs), RS_R15);
  71. end
  72. else if (taicpu(p).oper[2]^.typ = top_reg) then
  73. begin
  74. add_edge(getsupreg(taicpu(p).oper[2]^.reg), RS_R13);
  75. add_edge(getsupreg(taicpu(p).oper[2]^.reg), RS_R15);
  76. end;
  77. end;
  78. end;
  79. end;
  80. end;
  81. end;
  82. end;
  83. procedure trgcpu.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  84. var
  85. tmpref : treference;
  86. helplist : TAsmList;
  87. l : tasmlabel;
  88. hreg : tregister;
  89. begin
  90. { don't load spilled register between
  91. mov lr,pc
  92. mov pc,r4
  93. but befure the mov lr,pc
  94. }
  95. if assigned(pos.previous) and
  96. (pos.typ=ait_instruction) and
  97. (taicpu(pos).opcode=A_MOV) and
  98. (taicpu(pos).oper[0]^.typ=top_reg) and
  99. (taicpu(pos).oper[0]^.reg=NR_R14) and
  100. (taicpu(pos).oper[1]^.typ=top_reg) and
  101. (taicpu(pos).oper[1]^.reg=NR_PC) then
  102. pos:=tai(pos.previous);
  103. if abs(spilltemp.offset)>4095 then
  104. begin
  105. helplist:=TAsmList.create;
  106. reference_reset(tmpref,sizeof(aint));
  107. { create consts entry }
  108. current_asmdata.getjumplabel(l);
  109. cg.a_label(current_procinfo.aktlocaldata,l);
  110. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  111. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  112. { load consts entry }
  113. if getregtype(tempreg)=R_INTREGISTER then
  114. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  115. else
  116. hreg:=cg.getintregister(helplist,OS_ADDR);
  117. tmpref.symbol:=l;
  118. tmpref.base:=NR_R15;
  119. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  120. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  121. tmpref.index:=hreg;
  122. if spilltemp.index<>NR_NO then
  123. internalerror(200401263);
  124. helplist.concat(spilling_create_load(tmpref,tempreg));
  125. if getregtype(tempreg)=R_INTREGISTER then
  126. ungetregisterinline(helplist,hreg);
  127. list.insertlistafter(pos,helplist);
  128. helplist.free;
  129. end
  130. else
  131. inherited do_spill_read(list,pos,spilltemp,tempreg);
  132. end;
  133. procedure trgcpu.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  134. var
  135. tmpref : treference;
  136. helplist : TAsmList;
  137. l : tasmlabel;
  138. hreg : tregister;
  139. begin
  140. if abs(spilltemp.offset)>4095 then
  141. begin
  142. helplist:=TAsmList.create;
  143. reference_reset(tmpref,sizeof(aint));
  144. { create consts entry }
  145. current_asmdata.getjumplabel(l);
  146. cg.a_label(current_procinfo.aktlocaldata,l);
  147. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  148. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  149. { load consts entry }
  150. if getregtype(tempreg)=R_INTREGISTER then
  151. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  152. else
  153. hreg:=cg.getintregister(helplist,OS_ADDR);
  154. tmpref.symbol:=l;
  155. tmpref.base:=NR_R15;
  156. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  157. if spilltemp.index<>NR_NO then
  158. internalerror(200401263);
  159. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(pint));
  160. tmpref.index:=hreg;
  161. helplist.concat(spilling_create_store(tempreg,tmpref));
  162. if getregtype(tempreg)=R_INTREGISTER then
  163. ungetregisterinline(helplist,hreg);
  164. list.insertlistafter(pos,helplist);
  165. helplist.free;
  166. end
  167. else
  168. inherited do_spill_written(list,pos,spilltemp,tempreg);
  169. end;
  170. procedure trgcpu.add_constraints(reg:tregister);
  171. var
  172. supreg,i : Tsuperregister;
  173. begin
  174. case getsubreg(reg) of
  175. { Let 32bit floats conflict with all double precision regs > 15
  176. (since these don't have 32 bit equivalents) }
  177. R_SUBFS:
  178. begin
  179. supreg:=getsupreg(reg);
  180. for i:=RS_D16 to RS_D31 do
  181. add_edge(supreg,i);
  182. end;
  183. end;
  184. end;
  185. function trgcpu.get_spill_subreg(r:tregister) : tsubregister;
  186. begin
  187. if (getregtype(r)<>R_MMREGISTER) then
  188. result:=defaultsub
  189. else
  190. result:=getsubreg(r);
  191. end;
  192. procedure trgcputhumb2.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  193. var
  194. tmpref : treference;
  195. helplist : TAsmList;
  196. l : tasmlabel;
  197. hreg : tregister;
  198. begin
  199. { don't load spilled register between
  200. mov lr,pc
  201. mov pc,r4
  202. but befure the mov lr,pc
  203. }
  204. if assigned(pos.previous) and
  205. (pos.typ=ait_instruction) and
  206. (taicpu(pos).opcode=A_MOV) and
  207. (taicpu(pos).oper[0]^.typ=top_reg) and
  208. (taicpu(pos).oper[0]^.reg=NR_R14) and
  209. (taicpu(pos).oper[1]^.typ=top_reg) and
  210. (taicpu(pos).oper[1]^.reg=NR_PC) then
  211. pos:=tai(pos.previous);
  212. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  213. begin
  214. helplist:=TAsmList.create;
  215. reference_reset(tmpref,sizeof(aint));
  216. { create consts entry }
  217. current_asmdata.getjumplabel(l);
  218. cg.a_label(current_procinfo.aktlocaldata,l);
  219. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  220. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  221. { load consts entry }
  222. if getregtype(tempreg)=R_INTREGISTER then
  223. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  224. else
  225. hreg:=cg.getintregister(helplist,OS_ADDR);
  226. tmpref.symbol:=l;
  227. tmpref.base:=NR_R15;
  228. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  229. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(aint));
  230. tmpref.index:=hreg;
  231. if spilltemp.index<>NR_NO then
  232. internalerror(200401263);
  233. helplist.concat(spilling_create_load(tmpref,tempreg));
  234. if getregtype(tempreg)=R_INTREGISTER then
  235. ungetregisterinline(helplist,hreg);
  236. list.insertlistafter(pos,helplist);
  237. helplist.free;
  238. end
  239. else
  240. inherited do_spill_read(list,pos,spilltemp,tempreg);
  241. end;
  242. procedure trgcputhumb2.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  243. var
  244. tmpref : treference;
  245. helplist : TAsmList;
  246. l : tasmlabel;
  247. hreg : tregister;
  248. begin
  249. if (spilltemp.offset>4095) or (spilltemp.offset<-255) then
  250. begin
  251. helplist:=TAsmList.create;
  252. reference_reset(tmpref,sizeof(aint));
  253. { create consts entry }
  254. current_asmdata.getjumplabel(l);
  255. cg.a_label(current_procinfo.aktlocaldata,l);
  256. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  257. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(spilltemp.offset));
  258. { load consts entry }
  259. if getregtype(tempreg)=R_INTREGISTER then
  260. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  261. else
  262. hreg:=cg.getintregister(helplist,OS_ADDR);
  263. tmpref.symbol:=l;
  264. tmpref.base:=NR_R15;
  265. helplist.concat(taicpu.op_reg_ref(A_LDR,hreg,tmpref));
  266. if spilltemp.index<>NR_NO then
  267. internalerror(200401263);
  268. reference_reset_base(tmpref,current_procinfo.framepointer,0,sizeof(pint));
  269. tmpref.index:=hreg;
  270. helplist.concat(spilling_create_store(tempreg,tmpref));
  271. if getregtype(tempreg)=R_INTREGISTER then
  272. ungetregisterinline(helplist,hreg);
  273. list.insertlistafter(pos,helplist);
  274. helplist.free;
  275. end
  276. else
  277. inherited do_spill_written(list,pos,spilltemp,tempreg);
  278. end;
  279. procedure trgintcpu.add_cpu_interferences(p : tai);
  280. var
  281. r : tregister;
  282. begin
  283. if p.typ=ait_instruction then
  284. begin
  285. case taicpu(p).opcode of
  286. A_MUL:
  287. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  288. A_UMULL,
  289. A_UMLAL,
  290. A_SMULL,
  291. A_SMLAL:
  292. begin
  293. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[1]^.reg));
  294. add_edge(getsupreg(taicpu(p).oper[1]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  295. add_edge(getsupreg(taicpu(p).oper[0]^.reg),getsupreg(taicpu(p).oper[2]^.reg));
  296. end;
  297. A_LDRB,
  298. A_STRB,
  299. A_STR,
  300. A_LDR,
  301. A_LDRH,
  302. A_STRH:
  303. { don't mix up the framepointer and stackpointer with pre/post indexed operations }
  304. if (taicpu(p).oper[1]^.typ=top_ref) and
  305. (taicpu(p).oper[1]^.ref^.addressmode in [AM_PREINDEXED,AM_POSTINDEXED]) then
  306. begin
  307. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(current_procinfo.framepointer));
  308. { FIXME: temp variable r is needed here to avoid Internal error 20060521 }
  309. { while compiling the compiler. }
  310. r:=NR_STACK_POINTER_REG;
  311. if current_procinfo.framepointer<>r then
  312. add_edge(getsupreg(taicpu(p).oper[1]^.ref^.base),getsupreg(r));
  313. end;
  314. end;
  315. end;
  316. end;
  317. end.