rgcpu.pas 14 KB

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