nx86mem.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 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 nx86mem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cpuinfo,cpubase,
  23. node,nmem,ncgmem;
  24. type
  25. tx86vecnode = class(tcgvecnode)
  26. procedure update_reference_reg_mul(maybe_const_reg:tregister;l:aint);override;
  27. end;
  28. implementation
  29. uses
  30. cutils,verbose,
  31. aasmtai,aasmdata,
  32. cgutils,cgobj;
  33. {*****************************************************************************
  34. TX86VECNODE
  35. *****************************************************************************}
  36. { this routine must, like any other routine, not change the contents }
  37. { of base/index registers of references, as these may be regvars. }
  38. { The register allocator can coalesce one LOC_REGISTER being moved }
  39. { into another (as their live ranges won't overlap), but not a }
  40. { LOC_CREGISTER moved into a LOC_(C)REGISTER most of the time (as }
  41. { the live range of the LOC_CREGISTER will most likely overlap the }
  42. { the live range of the target LOC_(C)REGISTER) }
  43. { The passed register may be a LOC_CREGISTER as well. }
  44. procedure tx86vecnode.update_reference_reg_mul(maybe_const_reg:tregister;l:aint);
  45. var
  46. l2 : integer;
  47. hreg : tregister;
  48. begin
  49. { Optimized for x86 to use the index register and scalefactor }
  50. if location.reference.index=NR_NO then
  51. begin
  52. { no preparations needed }
  53. end
  54. else if location.reference.base=NR_NO then
  55. begin
  56. if (location.reference.scalefactor > 1) then
  57. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  58. case location.reference.scalefactor of
  59. 0,1 : hreg:=location.reference.index;
  60. 2 : cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,1,location.reference.index,hreg);
  61. 4 : cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,location.reference.index,hreg);
  62. 8 : cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,3,location.reference.index,hreg);
  63. else
  64. internalerror(2008091401);
  65. end;
  66. location.reference.base:=hreg;
  67. end
  68. else
  69. begin
  70. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  71. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  72. reference_reset_base(location.reference,hreg,0,location.reference.alignment);
  73. end;
  74. { insert the new index register and scalefactor or
  75. do the multiplication manual }
  76. case l of
  77. 1,2,4,8 :
  78. begin
  79. location.reference.scalefactor:=l;
  80. hreg:=maybe_const_reg;
  81. end;
  82. else
  83. begin
  84. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  85. if ispowerof2(l,l2) then
  86. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,l2,maybe_const_reg,hreg)
  87. else
  88. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,maybe_const_reg,hreg);
  89. end;
  90. end;
  91. location.reference.index:=hreg;
  92. end;
  93. begin
  94. cvecnode:=tx86vecnode;
  95. end.