nx86mem.pas 5.7 KB

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