nx64add.pas 4.9 KB

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