nx64cnv.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  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. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  112. emit_const_ref(A_BT,S_L,31,href);
  113. dec(href.offset,4);
  114. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_Q,href,location.register));
  115. end;
  116. else
  117. internalerror(200710181);
  118. end;
  119. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2);
  120. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  121. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(sizeof(pint)));
  122. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  123. reference_reset_symbol(href,l1,0,4,[]);
  124. { simplify for PIC }
  125. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  126. { I got these constant from a test program (FK) }
  127. if is_double(resultdef) then
  128. begin
  129. { double (2^64) }
  130. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  131. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($43f00000));
  132. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  133. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSD,S_NO,href,location.register));
  134. end
  135. else if is_single(resultdef) then
  136. begin
  137. { single(2^64) }
  138. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($5f800000));
  139. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSS,S_NO,href,location.register));
  140. end
  141. else
  142. internalerror(200506071);
  143. cg.a_label(current_asmdata.CurrAsmList,l2);
  144. end
  145. else
  146. inherited second_int_to_real;
  147. end;
  148. end
  149. else
  150. inherited second_int_to_real;
  151. end;
  152. begin
  153. ctypeconvnode:=tx8664typeconvnode;
  154. end.