nx64cnv.pas 6.8 KB

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