nx64cnv.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86-64 assembler for type converting 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 nx64cnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgcnv,defutil,defcmp,
  22. nx86cnv;
  23. type
  24. tx8664typeconvnode = class(tx86typeconvnode)
  25. protected
  26. { procedure second_int_to_int;override; }
  27. { procedure second_string_to_string;override; }
  28. { procedure second_cstring_to_pchar;override; }
  29. { procedure second_string_to_chararray;override; }
  30. { procedure second_array_to_pointer;override; }
  31. { procedure second_pointer_to_array;override; }
  32. { procedure second_chararray_to_string;override; }
  33. { procedure second_char_to_string;override; }
  34. { function first_int_to_real: tnode; override; }
  35. procedure second_int_to_real;override;
  36. { procedure second_real_to_real;override; }
  37. { procedure second_cord_to_pointer;override; }
  38. { procedure second_proc_to_procvar;override; }
  39. { procedure second_bool_to_int;override; }
  40. { procedure second_int_to_bool;override; }
  41. { procedure second_load_smallset;override; }
  42. { procedure second_ansistring_to_pchar;override; }
  43. { procedure second_pchar_to_string;override; }
  44. { procedure second_class_to_intf;override; }
  45. { procedure second_char_to_char;override; }
  46. end;
  47. implementation
  48. uses
  49. verbose,systems,globals,globtype,
  50. aasmbase,aasmtai,aasmdata,aasmcpu,
  51. symconst,symdef,
  52. cgbase,cga,procinfo,pass_2,
  53. ncon,ncal,ncnv,
  54. cpubase,
  55. cgutils,cgobj,cgx86,ncgutil,
  56. tgobj;
  57. procedure tx8664typeconvnode.second_int_to_real;
  58. var
  59. href : treference;
  60. l1,l2 : tasmlabel;
  61. op : tasmop;
  62. begin
  63. if use_sse(resultdef) then
  64. begin
  65. { We can only directly convert s32bit and s64bit,u64bit values, for other
  66. values convert first to s64bit }
  67. if not(torddef(left.resultdef).ordtype in [s32bit,s64bit,u64bit]) then
  68. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_S64,false);
  69. if is_double(resultdef) then
  70. op:=A_CVTSI2SD
  71. else if is_single(resultdef) then
  72. op:=A_CVTSI2SS
  73. else
  74. internalerror(200506061);
  75. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  76. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  77. case torddef(left.resultdef).ordtype of
  78. u64bit:
  79. begin
  80. { unsigned 64 bit ints are harder to handle:
  81. we load bits 0..62 and then check bit 63:
  82. if it is 1 then we add $80000000 000000000
  83. as double }
  84. current_asmdata.getdatalabel(l1);
  85. current_asmdata.getjumplabel(l2);
  86. { Get sign bit }
  87. if not(left.location.loc in [LOC_REGISTER,LOC_REFERENCE]) then
  88. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,false);
  89. case left.location.loc of
  90. LOC_REGISTER :
  91. begin
  92. emit_const_reg(A_BT,S_Q,63,left.location.register);
  93. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_Q,left.location.register,location.register));
  94. end;
  95. LOC_REFERENCE :
  96. begin
  97. inc(left.location.reference.offset,4);
  98. emit_const_ref(A_BT,S_L,31,left.location.reference);
  99. dec(left.location.reference.offset,4);
  100. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_Q,left.location.reference,location.register));
  101. end;
  102. else
  103. internalerror(200710181);
  104. end;
  105. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2);
  106. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  107. reference_reset_symbol(href,l1,0);
  108. { I got these constant from a test program (FK) }
  109. if is_double(resultdef) then
  110. begin
  111. { double (2^64) }
  112. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  113. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($43f00000));
  114. { simplify for PIC }
  115. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  116. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSD,S_NO,href,location.register));
  117. end
  118. else if is_single(resultdef) then
  119. begin
  120. { single(2^64) }
  121. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($5f800000));
  122. { simplify for PIC }
  123. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  124. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSS,S_NO,href,location.register));
  125. end
  126. else
  127. internalerror(200506071);
  128. cg.a_label(current_asmdata.CurrAsmList,l2);
  129. end
  130. else
  131. begin
  132. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  133. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,false);
  134. case left.location.loc of
  135. LOC_CREFERENCE,
  136. LOC_REFERENCE :
  137. current_asmdata.CurrAsmList.concat(Taicpu.op_ref_reg(op,tcgsize2opsize[left.location.size],left.location.reference,location.register));
  138. LOC_CREGISTER,
  139. LOC_REGISTER :
  140. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg(op,tcgsize2opsize[left.location.size],left.location.register,location.register));
  141. else
  142. internalerror(200506072);
  143. end;
  144. end;
  145. end;
  146. end
  147. else
  148. inherited second_int_to_real;
  149. end;
  150. begin
  151. ctypeconvnode:=tx8664typeconvnode;
  152. end.