rgcpu.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the register allocator for m68k
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit rgcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. aasmbase,aasmtai,aasmdata,aasmcpu,
  22. cgbase,cgutils,cpubase,
  23. rgobj;
  24. type
  25. trgcpu = class(trgobj)
  26. function do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;override;
  27. end;
  28. implementation
  29. { returns True if source operand of MOVE can be replaced with spilltemp when its destination is ref^. }
  30. function isvalidmovedest(ref: preference): boolean; inline;
  31. begin
  32. { The following is for Coldfire, for other CPUs it maybe can be relaxed. }
  33. result:=(ref^.symbol=nil) and (ref^.scalefactor<=1) and
  34. (ref^.index=NR_NO) and (ref^.base<>NR_NO) and (ref^.offset>=low(smallint)) and
  35. (ref^.offset<=high(smallint));
  36. end;
  37. procedure trgcpu.do_spill_read(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);
  38. var
  39. helpins : tai;
  40. tmpref : treference;
  41. helplist : tasmlist;
  42. hreg : tregister;
  43. begin
  44. if (abs(spilltemp.offset)>32767) and (current_settings.cputype in (cpu_coldfire + [cpu_mc68000])) then
  45. begin
  46. helplist:=tasmlist.create;
  47. if getregtype(tempreg)=R_INTREGISTER then
  48. hreg:=tempreg
  49. else
  50. hreg:=cg.getintregister(helplist,OS_ADDR);
  51. {$ifdef DEBUG_SPILLING}
  52. helplist.concat(tai_comment.Create(strpnew('Spilling: Read, large offset')));
  53. {$endif}
  54. helplist.concat(taicpu.op_const_reg(A_MOVE,S_L,spilltemp.offset,hreg));
  55. reference_reset_base(tmpref,spilltemp.base,0,sizeof(aint));
  56. tmpref.index:=hreg;
  57. helpins:=spilling_create_load(tmpref,tempreg);
  58. helplist.concat(helpins);
  59. list.insertlistafter(pos,helplist);
  60. helplist.free;
  61. end
  62. else
  63. inherited do_spill_read(list,pos,spilltemp,tempreg);
  64. end;
  65. procedure trgcpu.do_spill_written(list:tasmlist;pos:tai;const spilltemp:treference;tempreg:tregister);
  66. var
  67. tmpref : treference;
  68. helplist : tasmlist;
  69. hreg : tregister;
  70. begin
  71. if (abs(spilltemp.offset)>32767) and (current_settings.cputype in (cpu_coldfire + [cpu_mc68000])) then
  72. begin
  73. helplist:=tasmlist.create;
  74. if getregtype(tempreg)=R_INTREGISTER then
  75. hreg:=getregisterinline(helplist,[R_SUBWHOLE])
  76. else
  77. hreg:=cg.getintregister(helplist,OS_ADDR);
  78. {$ifdef DEBUG_SPILLING}
  79. helplist.concat(tai_comment.Create(strpnew('Spilling: Write, large offset')));
  80. {$endif}
  81. helplist.concat(taicpu.op_const_reg(A_MOVE,S_L,spilltemp.offset,hreg));
  82. reference_reset_base(tmpref,spilltemp.base,0,sizeof(aint));
  83. tmpref.index:=hreg;
  84. helplist.concat(spilling_create_store(tempreg,tmpref));
  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_written(list,pos,spilltemp,tempreg);
  92. end;
  93. function trgcpu.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  94. var
  95. opidx: longint;
  96. begin
  97. result:=false;
  98. opidx:=-1;
  99. { TODO: support more instructions (on m68k almost all are eligible) }
  100. if (not (regtype in [R_INTREGISTER,R_ADDRESSREGISTER])) or (instr.opcode<>A_MOVE) then
  101. exit;
  102. if (instr.ops<>2) then
  103. exit;
  104. if (instr.oper[0]^.typ=top_reg) and (get_alias(getsupreg(instr.oper[0]^.reg))=orgreg) then
  105. begin
  106. { source can be replaced if dest is register or a 'simple' reference }
  107. if (instr.oper[1]^.typ=top_reg) or
  108. ((instr.oper[1]^.typ=top_ref) and isvalidmovedest(instr.oper[1]^.ref)) then
  109. opidx:=0;
  110. end
  111. else if (instr.oper[1]^.typ=top_reg) and (get_alias(getsupreg(instr.oper[1]^.reg))=orgreg) and
  112. (instr.oper[0]^.typ=top_reg) then
  113. opidx:=1;
  114. if (opidx<0) then
  115. exit;
  116. instr.oper[opidx]^.typ:=top_ref;
  117. new(instr.oper[opidx]^.ref);
  118. instr.oper[opidx]^.ref^:=spilltemp;
  119. case instr.opsize of
  120. S_B: inc(instr.oper[opidx]^.ref^.offset,3);
  121. S_W: inc(instr.oper[opidx]^.ref^.offset,2);
  122. end;
  123. result:=true;
  124. end;
  125. end.