nx64cnv.pas 6.8 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,defutil,pass_1,
  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. function first_int_to_real : tnode;override;
  36. procedure second_int_to_real;override;
  37. { procedure second_real_to_real;override; }
  38. { procedure second_cord_to_pointer;override; }
  39. { procedure second_proc_to_procvar;override; }
  40. { procedure second_bool_to_int;override; }
  41. { procedure second_int_to_bool;override; }
  42. { procedure second_load_smallset;override; }
  43. { procedure second_ansistring_to_pchar;override; }
  44. { procedure second_pchar_to_string;override; }
  45. { procedure second_class_to_intf;override; }
  46. { procedure second_char_to_char;override; }
  47. end;
  48. implementation
  49. uses
  50. verbose,globals,globtype,
  51. aasmbase,aasmtai,aasmdata,aasmcpu,
  52. symconst,symdef,
  53. cgbase,cga,
  54. ncnv,
  55. cpubase,
  56. cgutils,cgobj,hlcgobj,cgx86;
  57. function tx8664typeconvnode.first_int_to_real : tnode;
  58. begin
  59. result:=nil;
  60. if use_vectorfpu(resultdef) and
  61. (torddef(left.resultdef).ordtype=u32bit) then
  62. begin
  63. inserttypeconv(left,s64inttype);
  64. firstpass(left);
  65. end
  66. else
  67. result:=inherited first_int_to_real;
  68. if use_vectorfpu(resultdef) then
  69. expectloc:=LOC_MMREGISTER;
  70. end;
  71. procedure tx8664typeconvnode.second_int_to_real;
  72. var
  73. href : treference;
  74. l1,l2 : tasmlabel;
  75. op : tasmop;
  76. begin
  77. if use_vectorfpu(resultdef) then
  78. begin
  79. if is_double(resultdef) then
  80. op:=A_CVTSI2SD
  81. else if is_single(resultdef) then
  82. op:=A_CVTSI2SS
  83. else
  84. internalerror(200506061);
  85. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  86. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  87. case torddef(left.resultdef).ordtype of
  88. u64bit:
  89. begin
  90. { unsigned 64 bit ints are harder to handle:
  91. we load bits 0..62 and then check bit 63:
  92. if it is 1 then we add $80000000 000000000
  93. as double }
  94. current_asmdata.getglobaldatalabel(l1);
  95. current_asmdata.getjumplabel(l2);
  96. { Get sign bit }
  97. if not(left.location.loc in [LOC_REGISTER,LOC_REFERENCE]) then
  98. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  99. case left.location.loc of
  100. LOC_REGISTER :
  101. begin
  102. emit_const_reg(A_BT,S_Q,63,left.location.register);
  103. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_Q,left.location.register,location.register));
  104. end;
  105. LOC_REFERENCE :
  106. begin
  107. href:=left.location.reference;
  108. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  109. inc(href.offset,4);
  110. emit_const_ref(A_BT,S_L,31,href);
  111. dec(href.offset,4);
  112. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_Q,href,location.register));
  113. end;
  114. else
  115. internalerror(200710181);
  116. end;
  117. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2);
  118. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(sizeof(pint)));
  119. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  120. reference_reset_symbol(href,l1,0,4,[]);
  121. { simplify for PIC }
  122. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  123. { I got these constant from a test program (FK) }
  124. if is_double(resultdef) then
  125. begin
  126. { double (2^64) }
  127. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  128. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($43f00000));
  129. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  130. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSD,S_NO,href,location.register));
  131. end
  132. else if is_single(resultdef) then
  133. begin
  134. { single(2^64) }
  135. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($5f800000));
  136. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSS,S_NO,href,location.register));
  137. end
  138. else
  139. internalerror(200506071);
  140. cg.a_label(current_asmdata.CurrAsmList,l2);
  141. end
  142. else
  143. inherited second_int_to_real;
  144. end;
  145. end
  146. else
  147. inherited second_int_to_real;
  148. end;
  149. begin
  150. ctypeconvnode:=tx8664typeconvnode;
  151. end.