nx64add.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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,
  30. aasmbase,aasmtai,aasmdata,
  31. defutil,
  32. cgbase,cgutils,cga,cgobj,
  33. tgobj;
  34. {*****************************************************************************
  35. Addordinal
  36. *****************************************************************************}
  37. procedure tx8664addnode.second_addordinal;
  38. begin
  39. { filter unsigned MUL opcode, which requires special handling }
  40. if (nodetype=muln) and
  41. (not(is_signed(left.resultdef)) or
  42. not(is_signed(right.resultdef))) then
  43. begin
  44. second_mul;
  45. exit;
  46. end;
  47. inherited second_addordinal;
  48. end;
  49. {*****************************************************************************
  50. MUL
  51. *****************************************************************************}
  52. procedure tx8664addnode.second_mul;
  53. var reg:Tregister;
  54. ref:Treference;
  55. use_ref:boolean;
  56. hl4 : tasmlabel;
  57. begin
  58. pass_left_right;
  59. { The location.register will be filled in later (JM) }
  60. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  61. { Mul supports registers and references, so if not register/reference,
  62. load the location into a register}
  63. use_ref:=false;
  64. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  65. reg:=left.location.register
  66. else if left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  67. begin
  68. ref:=left.location.reference;
  69. use_ref:=true;
  70. end
  71. else
  72. begin
  73. {LOC_CONSTANT for example.}
  74. reg:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  75. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,left.location,reg);
  76. end;
  77. { Allocate RAX. }
  78. cg.getcpuregister(current_asmdata.CurrAsmList,NR_RAX);
  79. { Load the right value. }
  80. cg.a_load_loc_reg(current_asmdata.CurrAsmList,OS_INT,right.location,NR_RAX);
  81. { Also allocate RDX, since it is also modified by a mul (JM). }
  82. cg.getcpuregister(current_asmdata.CurrAsmList,NR_RDX);
  83. if use_ref then
  84. emit_ref(A_MUL,S_Q,ref)
  85. else
  86. emit_reg(A_MUL,S_Q,reg);
  87. if cs_check_overflow in current_settings.localswitches then
  88. begin
  89. current_asmdata.getjumplabel(hl4);
  90. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_AE,hl4);
  91. cg.a_call_name(current_asmdata.CurrAsmList,'FPC_OVERFLOW',false);
  92. cg.a_label(current_asmdata.CurrAsmList,hl4);
  93. end;
  94. { Free RDX,RAX }
  95. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RDX);
  96. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_RAX);
  97. { Allocate a new register and store the result in RAX in it. }
  98. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  99. emit_reg_reg(A_MOV,S_Q,NR_RAX,location.register);
  100. location_freetemp(current_asmdata.CurrAsmList,left.location);
  101. location_freetemp(current_asmdata.CurrAsmList,right.location);
  102. end;
  103. begin
  104. caddnode:=tx8664addnode;
  105. end.