2
0

n68kmem.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. begin
  64. scaled:=false;
  65. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: called')));
  66. if l<>1 then
  67. begin
  68. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: l <> 1')));
  69. { if we have a possibility, setup a scalefactor instead of the MUL }
  70. if not (((CPUM68K_HAS_INDEXSCALE in cpu_capabilities[current_settings.cputype]) and (l in [2,4])) or
  71. ((CPUM68K_HAS_INDEXSCALE8 in cpu_capabilities[current_settings.cputype]) and (l in [2,4,8]))) then
  72. begin
  73. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: mul')));
  74. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_S32);
  75. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_IMUL,def_cgsize(regsize),l,maybe_const_reg,hreg);
  76. maybe_const_reg:=hreg;
  77. end
  78. else
  79. begin
  80. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: scale')));
  81. scaled:=true;
  82. end;
  83. end;
  84. if (location.reference.base=NR_NO) and not (scaled) and not assigned(location.reference.symbol) then
  85. begin
  86. { prefer an address reg, if we will be a base, for indexes any register works }
  87. if isintregister(maybe_const_reg) then
  88. begin
  89. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: copytoa')));
  90. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  91. cg.a_load_reg_reg(current_asmdata.CurrAsmList,def_cgsize(regsize),OS_ADDR,maybe_const_reg,hreg);
  92. maybe_const_reg:=hreg;
  93. end;
  94. location.reference.base:=maybe_const_reg;
  95. end
  96. else
  97. begin
  98. if location.reference.index<>NR_NO then
  99. begin
  100. { if we already have an index register, dereference the ref to a new base, to be able to insert an index }
  101. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  102. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  103. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  104. end;
  105. if def_cgsize(regsize) in [OS_8,OS_16] then
  106. begin
  107. { index registers are always sign extended on m68k, so we have to zero extend by hand,
  108. if the index variable is unsigned, and its width is less than the whole register }
  109. //current_asmdata.CurrAsmList.concat(tai_comment.create(strpnew('updref: index zero extend')));
  110. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  111. cg.a_load_reg_reg(current_asmdata.CurrAsmList,def_cgsize(regsize),OS_ADDR,maybe_const_reg,hreg);
  112. maybe_const_reg:=hreg;
  113. end;
  114. { insert new index register }
  115. location.reference.index:=maybe_const_reg;
  116. if (scaled) then
  117. location.reference.scalefactor:=l;
  118. end;
  119. { update alignment }
  120. if (location.reference.alignment=0) then
  121. internalerror(2009020704);
  122. location.reference.alignment:=newalignment(location.reference.alignment,l);
  123. end;
  124. { see remarks for tcgvecnode.update_reference_reg_mul above }
  125. procedure t68kvecnode.update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l:aint);
  126. var
  127. sref: tsubsetreference;
  128. offsetreg, hreg: tregister;
  129. alignpower: aint;
  130. temp : longint;
  131. begin
  132. { only orddefs are bitpacked. Even then we only need special code in }
  133. { case the bitpacked *byte size* is not a power of two, otherwise }
  134. { everything can be handled using the the regular array code. }
  135. if ((l mod 8) = 0) and
  136. (ispowerof2(l div 8,temp) or
  137. not is_ordinal(resultdef)
  138. {$ifndef cpu64bitalu}
  139. or is_64bitint(resultdef)
  140. {$endif not cpu64bitalu}
  141. ) then
  142. begin
  143. update_reference_reg_mul(maybe_const_reg,regsize,l div 8);
  144. exit;
  145. end;
  146. if (l > 8*sizeof(aint)) then
  147. internalerror(2006080503);
  148. sref.ref := location.reference;
  149. hreg := cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  150. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,OS_INT,tarraydef(left.resultdef).lowrange,maybe_const_reg,hreg);
  151. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_INT,l,hreg);
  152. { keep alignment for index }
  153. sref.ref.alignment := left.resultdef.alignment;
  154. if not ispowerof2(packedbitsloadsize(l),temp) then
  155. internalerror(2006081201);
  156. alignpower:=temp;
  157. offsetreg := cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  158. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,OS_ADDR,3+alignpower,hreg,offsetreg);
  159. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,alignpower,offsetreg);
  160. if (sref.ref.base = NR_NO) then
  161. sref.ref.base := offsetreg
  162. else if (sref.ref.index = NR_NO) then
  163. sref.ref.index := offsetreg
  164. else
  165. begin
  166. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,sref.ref.base,offsetreg);
  167. sref.ref.base := offsetreg;
  168. end;
  169. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_AND,OS_INT,(1 shl (3+alignpower))-1,hreg);
  170. sref.bitindexreg := hreg;
  171. sref.startbit := 0;
  172. sref.bitlen := resultdef.packedbitsize;
  173. if (left.location.loc = LOC_REFERENCE) then
  174. location.loc := LOC_SUBSETREF
  175. else
  176. location.loc := LOC_CSUBSETREF;
  177. location.sref := sref;
  178. end;
  179. {procedure t68kvecnode.pass_generate_code;
  180. begin
  181. inherited pass_generate_code;
  182. end;}
  183. begin
  184. cvecnode:=t68kvecnode;
  185. end.