nx86mem.pas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. tx86derefnode = class(tcgderefnode)
  26. procedure pass_generate_code;override;
  27. end;
  28. tx86vecnode = class(tcgvecnode)
  29. {$ifndef i8086}
  30. procedure update_reference_reg_mul(maybe_const_reg:tregister;l:aint);override;
  31. {$endif not i8086}
  32. end;
  33. implementation
  34. uses
  35. cutils,verbose,
  36. aasmtai,aasmdata,
  37. cgutils,cgobj,
  38. symconst,symdef;
  39. {*****************************************************************************
  40. TX86DEREFNODE
  41. *****************************************************************************}
  42. procedure tx86derefnode.pass_generate_code;
  43. begin
  44. inherited pass_generate_code;
  45. case tpointerdef(left.resultdef).x86pointertyp of
  46. x86pt_near: ;
  47. x86pt_near_cs: location.reference.segment:=NR_CS;
  48. x86pt_near_ds: location.reference.segment:=NR_DS;
  49. x86pt_near_ss: location.reference.segment:=NR_SS;
  50. x86pt_near_es: location.reference.segment:=NR_ES;
  51. x86pt_near_fs: location.reference.segment:=NR_FS;
  52. x86pt_near_gs: location.reference.segment:=NR_GS;
  53. x86pt_far: internalerror(2013050401);
  54. x86pt_huge: internalerror(2013050402);
  55. else
  56. internalerror(2013050403);
  57. end;
  58. end;
  59. {*****************************************************************************
  60. TX86VECNODE
  61. *****************************************************************************}
  62. {$ifndef i8086}
  63. { this routine must, like any other routine, not change the contents }
  64. { of base/index registers of references, as these may be regvars. }
  65. { The register allocator can coalesce one LOC_REGISTER being moved }
  66. { into another (as their live ranges won't overlap), but not a }
  67. { LOC_CREGISTER moved into a LOC_(C)REGISTER most of the time (as }
  68. { the live range of the LOC_CREGISTER will most likely overlap the }
  69. { the live range of the target LOC_(C)REGISTER) }
  70. { The passed register may be a LOC_CREGISTER as well. }
  71. procedure tx86vecnode.update_reference_reg_mul(maybe_const_reg:tregister;l:aint);
  72. var
  73. l2 : integer;
  74. hreg : tregister;
  75. begin
  76. { Optimized for x86 to use the index register and scalefactor }
  77. if location.reference.index=NR_NO then
  78. begin
  79. { no preparations needed }
  80. end
  81. else if location.reference.base=NR_NO then
  82. begin
  83. if (location.reference.scalefactor > 1) then
  84. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  85. case location.reference.scalefactor of
  86. 0,1 : hreg:=location.reference.index;
  87. 2 : cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,1,location.reference.index,hreg);
  88. 4 : cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,location.reference.index,hreg);
  89. 8 : cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,3,location.reference.index,hreg);
  90. else
  91. internalerror(2008091401);
  92. end;
  93. location.reference.base:=hreg;
  94. end
  95. else
  96. begin
  97. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  98. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  99. reference_reset_base(location.reference,hreg,0,location.reference.alignment);
  100. end;
  101. { insert the new index register and scalefactor or
  102. do the multiplication manual }
  103. case l of
  104. 1,2,4,8 :
  105. begin
  106. location.reference.scalefactor:=l;
  107. hreg:=maybe_const_reg;
  108. end;
  109. else
  110. begin
  111. hreg:=cg.getaddressregister(current_asmdata.CurrAsmList);
  112. if ispowerof2(l,l2) then
  113. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,l2,maybe_const_reg,hreg)
  114. else
  115. cg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,maybe_const_reg,hreg);
  116. end;
  117. end;
  118. location.reference.index:=hreg;
  119. end;
  120. {$endif not i8086}
  121. begin
  122. cderefnode:=tx86derefnode;
  123. cvecnode:=tx86vecnode;
  124. end.