nx64cnv.pas 7.9 KB

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