nx64add.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Code generation for add nodes on the x86-64
  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 nx64add;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nadd,cpubase,nx86add;
  22. type
  23. tx8664addnode = class(tx86addnode)
  24. function use_generic_mul64bit: boolean; override;
  25. procedure second_addordinal; override;
  26. procedure second_mul;
  27. end;
  28. implementation
  29. uses
  30. globtype,globals,verbose,
  31. aasmbase,aasmdata,
  32. defutil,
  33. cgbase,cgutils,cga,cgobj,hlcgobj,cgx86,
  34. tgobj;
  35. function tx8664addnode.use_generic_mul64bit: boolean;
  36. begin
  37. result:=false;
  38. end;
  39. {*****************************************************************************
  40. Addordinal
  41. *****************************************************************************}
  42. procedure tx8664addnode.second_addordinal;
  43. begin
  44. { filter unsigned MUL opcode, which requires special handling.
  45. Note that when overflow checking is off, we can use IMUL instead. }
  46. if (nodetype=muln) and
  47. needoverflowcheck and
  48. (not(is_signed(left.resultdef)) or
  49. not(is_signed(right.resultdef))) then
  50. begin
  51. second_mul;
  52. exit;
  53. end;
  54. inherited second_addordinal;
  55. end;
  56. {*****************************************************************************
  57. MUL
  58. *****************************************************************************}
  59. procedure tx8664addnode.second_mul;
  60. var
  61. reg,rega,regd:Tregister;
  62. ref:Treference;
  63. use_ref:boolean;
  64. hl4 : tasmlabel;
  65. cgsize:TCgSize;
  66. opsize:topsize;
  67. begin
  68. reference_reset(ref,0,[]);
  69. reg:=NR_NO;
  70. cgsize:=def_cgsize(resultdef);
  71. opsize:=TCGSize2OpSize[cgsize];
  72. case cgsize of
  73. OS_S64,OS_64:
  74. begin
  75. rega:=NR_RAX;
  76. regd:=NR_RDX;
  77. end;
  78. OS_S32,OS_32:
  79. begin
  80. rega:=NR_EAX;
  81. regd:=NR_EDX;
  82. end;
  83. else
  84. internalerror(2013102703);
  85. end;
  86. pass_left_right;
  87. { The location.register will be filled in later (JM) }
  88. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  89. { Mul supports registers and references, so if not register/reference,
  90. load the location into a register}
  91. use_ref:=false;
  92. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  93. reg:=left.location.register
  94. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  95. begin
  96. ref:=left.location.reference;
  97. use_ref:=true;
  98. end
  99. else
  100. begin
  101. {LOC_CONSTANT for example.}
  102. reg:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  103. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,left.resultdef,resultdef,left.location,reg);
  104. end;
  105. { Allocate RAX. }
  106. cg.getcpuregister(current_asmdata.CurrAsmList,rega);
  107. { Load the right value. }
  108. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,right.resultdef,resultdef,right.location,rega);
  109. { Also allocate RDX, since it is also modified by a mul (JM). }
  110. cg.getcpuregister(current_asmdata.CurrAsmList,regd);
  111. if needoverflowcheck then
  112. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  113. if use_ref then
  114. emit_ref(A_MUL,opsize,ref)
  115. else
  116. emit_reg(A_MUL,opsize,reg);
  117. if needoverflowcheck then
  118. begin
  119. current_asmdata.getjumplabel(hl4);
  120. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  121. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  122. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  123. cg.a_label(current_asmdata.CurrAsmList,hl4);
  124. end;
  125. { Free RDX,RAX }
  126. cg.ungetcpuregister(current_asmdata.CurrAsmList,regd);
  127. cg.ungetcpuregister(current_asmdata.CurrAsmList,rega);
  128. { Allocate a new register and store the result in RAX in it. }
  129. location.register:=cg.getintregister(current_asmdata.CurrAsmList,cgsize);
  130. emit_reg_reg(A_MOV,opsize,rega,location.register);
  131. location_freetemp(current_asmdata.CurrAsmList,left.location);
  132. location_freetemp(current_asmdata.CurrAsmList,right.location);
  133. end;
  134. begin
  135. caddnode:=tx8664addnode;
  136. end.