n68kmem.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. {
  2. Copyright (c) 2014 by the Free Pascal development team
  3. Generate m68k assembler for in memory related nodes
  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 n68kmem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,
  23. cgbase,cpuinfo,cpubase,
  24. node,nmem,ncgmem;
  25. type
  26. t68kvecnode = class(tcgvecnode)
  27. procedure update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint); override;
  28. procedure update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l:aint); override;
  29. function valid_index_size(size: tcgsize): boolean; override;
  30. //procedure pass_generate_code;override;
  31. end;
  32. implementation
  33. uses
  34. systems,globals,
  35. cutils,verbose,
  36. symdef,paramgr,
  37. aasmtai,aasmdata,
  38. nld,ncon,nadd,
  39. cgutils,cgobj,
  40. defutil;
  41. {*****************************************************************************
  42. T68KVECNODE
  43. *****************************************************************************}
  44. function t68kvecnode.valid_index_size(size: tcgsize): boolean;
  45. begin
  46. if (CPUM68K_HAS_INDEXWORD in cpu_capabilities[current_settings.cputype]) then
  47. result:=tcgsize2signed[size] in [OS_S16,OS_S32]
  48. else
  49. result:=inherited;
  50. end;
  51. { this routine must, like any other routine, not change the contents }
  52. { of base/index registers of references, as these may be regvars. }
  53. { The register allocator can coalesce one LOC_REGISTER being moved }
  54. { into another (as their live ranges won't overlap), but not a }
  55. { LOC_CREGISTER moved into a LOC_(C)REGISTER most of the time (as }
  56. { the live range of the LOC_CREGISTER will most likely overlap the }
  57. { the live range of the target LOC_(C)REGISTER) }
  58. { The passed register may be a LOC_CREGISTER as well. }
  59. procedure t68kvecnode.update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);
  60. var
  61. hreg: tregister;
  62. scaled: boolean;
  63. regcgsize: tcgsize;
  64. begin
  65. scaled:=false;
  66. regcgsize:=def_cgsize(regsize);
  67. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: called')));
  68. if l<>1 then
  69. begin
  70. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: l <> 1')));
  71. { if we have a possibility, setup a scalefactor instead of the MUL }
  72. if not (((CPUM68K_HAS_INDEXSCALE in cpu_capabilities[current_settings.cputype]) and (l in [2,4])) or
  73. ((CPUM68K_HAS_INDEXSCALE8 in cpu_capabilities[current_settings.cputype]) and (l in [2,4,8]))) then
  74. begin
  75. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: mul')));
  76. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  77. cg.a_load_reg_reg(current_asmdata.CurrAsmList,regcgsize,OS_ADDR,maybe_const_reg,hreg);
  78. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,hreg);
  79. regcgsize:=OS_ADDR;
  80. maybe_const_reg:=hreg;
  81. end
  82. else
  83. begin
  84. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: scale')));
  85. scaled:=true;
  86. end;
  87. end;
  88. if (location.reference.base=NR_NO) and not (scaled) and not assigned(location.reference.symbol) then
  89. begin
  90. { prefer an address reg, if we will be a base, for indexes any register works }
  91. if isintregister(maybe_const_reg) then
  92. begin
  93. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: copytoa')));
  94. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  95. cg.a_load_reg_reg(current_asmdata.CurrAsmList,regcgsize,OS_ADDR,maybe_const_reg,hreg);
  96. maybe_const_reg:=hreg;
  97. end;
  98. location.reference.base:=maybe_const_reg;
  99. end
  100. else
  101. begin
  102. if location.reference.index<>NR_NO then
  103. begin
  104. { if we already have an index register, dereference the ref to a new base, to be able to insert an index }
  105. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  106. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  107. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  108. end;
  109. if regcgsize in [OS_8,OS_16] then
  110. begin
  111. { index registers are always sign extended on m68k, so we have to zero extend by hand,
  112. if the index variable is unsigned, and its width is less than the whole register }
  113. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: index zero extend')));
  114. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  115. cg.a_load_reg_reg(current_asmdata.CurrAsmList,regcgsize,OS_ADDR,maybe_const_reg,hreg);
  116. maybe_const_reg:=hreg;
  117. end;
  118. { insert new index register }
  119. location.reference.index:=maybe_const_reg;
  120. if (scaled) then
  121. location.reference.scalefactor:=l;
  122. end;
  123. { update alignment }
  124. if (location.reference.alignment=0) then
  125. internalerror(2009020704);
  126. location.reference.alignment:=newalignment(location.reference.alignment,l);
  127. end;
  128. { see remarks for tcgvecnode.update_reference_reg_mul above }
  129. procedure t68kvecnode.update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l:aint);
  130. var
  131. sref: tsubsetreference;
  132. offsetreg, hreg: tregister;
  133. alignpower: aint;
  134. temp : longint;
  135. begin
  136. { only orddefs are bitpacked. Even then we only need special code in }
  137. { case the bitpacked *byte size* is not a power of two, otherwise }
  138. { everything can be handled using the the regular array code. }
  139. if ((l mod 8) = 0) and
  140. (ispowerof2(l div 8,temp) or
  141. not is_ordinal(resultdef)
  142. {$ifndef cpu64bitalu}
  143. or is_64bitint(resultdef)
  144. {$endif not cpu64bitalu}
  145. ) then
  146. begin
  147. update_reference_reg_mul(maybe_const_reg,regsize,l div 8);
  148. exit;
  149. end;
  150. if (l > 8*sizeof(aint)) then
  151. internalerror(2006080503);
  152. sref.ref := location.reference;
  153. hreg := cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  154. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resultdef).lowrange,maybe_const_reg,hreg);
  155. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,hreg);
  156. { keep alignment for index }
  157. sref.ref.alignment := left.resultdef.alignment;
  158. if not ispowerof2(packedbitsloadsize(l),temp) then
  159. internalerror(2006081201);
  160. alignpower:=temp;
  161. offsetreg := cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  162. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,hreg,offsetreg);
  163. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  164. if (sref.ref.base = NR_NO) then
  165. sref.ref.base := offsetreg
  166. else if (sref.ref.index = NR_NO) then
  167. sref.ref.index := offsetreg
  168. else
  169. begin
  170. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  171. sref.ref.base := offsetreg;
  172. end;
  173. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,hreg);
  174. sref.bitindexreg := hreg;
  175. sref.startbit := 0;
  176. sref.bitlen := resultdef.packedbitsize;
  177. if (left.location.loc = LOC_REFERENCE) then
  178. location.loc := LOC_SUBSETREF
  179. else
  180. location.loc := LOC_CSUBSETREF;
  181. location.sref := sref;
  182. end;
  183. {procedure t68kvecnode.pass_generate_code;
  184. begin
  185. inherited pass_generate_code;
  186. end;}
  187. begin
  188. cvecnode:=t68kvecnode;
  189. end.