2
0

n386mem.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate i386 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 n386mem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cpuinfo,cpubase,
  23. node,nmem,ncgmem;
  24. type
  25. ti386addrnode = class(tcgaddrnode)
  26. procedure pass_generate_code;override;
  27. end;
  28. ti386derefnode = class(tcgderefnode)
  29. procedure pass_generate_code;override;
  30. end;
  31. ti386vecnode = class(tcgvecnode)
  32. procedure update_reference_reg_mul(reg:tregister;l:aint);override;
  33. procedure pass_generate_code;override;
  34. end;
  35. implementation
  36. uses
  37. systems,
  38. cutils,verbose,
  39. symdef,paramgr,
  40. aasmtai,aasmdata,
  41. nld,ncon,nadd,
  42. cgutils,cgobj;
  43. {*****************************************************************************
  44. TI386ADDRNODE
  45. *****************************************************************************}
  46. procedure ti386addrnode.pass_generate_code;
  47. begin
  48. inherited pass_generate_code;
  49. { for use of other segments, not used }
  50. {if left.location.reference.segment<>NR_NO then
  51. location.segment:=left.location.reference.segment;}
  52. end;
  53. {*****************************************************************************
  54. TI386DEREFNODE
  55. *****************************************************************************}
  56. procedure ti386derefnode.pass_generate_code;
  57. begin
  58. inherited pass_generate_code;
  59. if tpointerdef(left.resultdef).is_far then
  60. location.reference.segment:=NR_FS;
  61. end;
  62. {*****************************************************************************
  63. TI386VECNODE
  64. *****************************************************************************}
  65. procedure ti386vecnode.update_reference_reg_mul(reg:tregister;l:aint);
  66. var
  67. l2 : integer;
  68. hreg : tregister;
  69. begin
  70. { Optimized for x86 to use the index register and scalefactor }
  71. if location.reference.index=NR_NO then
  72. begin
  73. { no preparations needed }
  74. end
  75. else if location.reference.base=NR_NO then
  76. begin
  77. case location.reference.scalefactor of
  78. 2 : cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,1,location.reference.index);
  79. 4 : cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,2,location.reference.index);
  80. 8 : cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,3,location.reference.index);
  81. end;
  82. location.reference.base:=location.reference.index;
  83. end
  84. else
  85. begin
  86. hreg := cg.getaddressregister(current_asmdata.CurrAsmList);
  87. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,location.reference,hreg);
  88. reference_reset_base(location.reference,hreg,0);
  89. end;
  90. { insert the new index register and scalefactor or
  91. do the multiplication manual }
  92. case l of
  93. 1,2,4,8 : location.reference.scalefactor:=l;
  94. else
  95. begin
  96. if ispowerof2(l,l2) then
  97. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_SHL,OS_ADDR,l2,reg)
  98. else
  99. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_IMUL,OS_ADDR,l,reg);
  100. end;
  101. end;
  102. location.reference.index:=reg;
  103. end;
  104. procedure ti386vecnode.pass_generate_code;
  105. begin
  106. inherited pass_generate_code;
  107. if nf_memseg in flags then
  108. location.reference.segment:=NR_FS;
  109. end;
  110. begin
  111. caddrnode:=ti386addrnode;
  112. cderefnode:=ti386derefnode;
  113. cvecnode:=ti386vecnode;
  114. end.