nx86cnv.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate for x86-64 and i386 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 nx86cnv;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgcnv,defutil,defcmp;
  22. type
  23. tx86typeconvnode = class(tcgtypeconvnode)
  24. protected
  25. function first_real_to_real : tnode;override;
  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_set_to_set;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. function tx86typeconvnode.first_real_to_real : tnode;
  58. begin
  59. first_real_to_real:=nil;
  60. { comp isn't a floating type }
  61. if (tfloatdef(resultdef).floattype=s64comp) and
  62. (tfloatdef(left.resultdef).floattype<>s64comp) and
  63. not (nf_explicit in flags) then
  64. CGMessage(type_w_convert_real_2_comp);
  65. if use_sse(resultdef) then
  66. expectloc:=LOC_MMREGISTER
  67. else
  68. expectloc:=LOC_FPUREGISTER;
  69. end;
  70. procedure tx86typeconvnode.second_int_to_bool;
  71. var
  72. hregister : tregister;
  73. {$ifndef cpu64bit}
  74. href : treference;
  75. {$endif cpu64bit}
  76. resflags : tresflags;
  77. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  78. begin
  79. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  80. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  81. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  82. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  83. secondpass(left);
  84. if codegenerror then
  85. exit;
  86. { byte(boolean) or word(wordbool) or longint(longbool) must }
  87. { be accepted for var parameters }
  88. if (nf_explicit in flags) and
  89. (left.resultdef.size=resultdef.size) and
  90. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  91. begin
  92. location_copy(location,left.location);
  93. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  94. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  95. exit;
  96. end;
  97. { Load left node into flag F_NE/F_E }
  98. resflags:=F_NE;
  99. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  100. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  101. case left.location.loc of
  102. LOC_CREFERENCE,
  103. LOC_REFERENCE :
  104. begin
  105. {$ifndef cpu64bit}
  106. if left.location.size in [OS_64,OS_S64] then
  107. begin
  108. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  109. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  110. href:=left.location.reference;
  111. inc(href.offset,4);
  112. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  113. end
  114. else
  115. {$endif cpu64bit}
  116. begin
  117. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  118. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  119. end;
  120. end;
  121. LOC_FLAGS :
  122. begin
  123. resflags:=left.location.resflags;
  124. end;
  125. LOC_REGISTER,LOC_CREGISTER :
  126. begin
  127. {$ifndef cpu64bit}
  128. if left.location.size in [OS_64,OS_S64] then
  129. begin
  130. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  131. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  132. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  133. end
  134. else
  135. {$endif cpu64bit}
  136. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  137. end;
  138. LOC_JUMP :
  139. begin
  140. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  141. current_asmdata.getjumplabel(hlabel);
  142. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  143. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  144. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  145. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  146. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  147. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  148. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  149. end;
  150. else
  151. internalerror(10062);
  152. end;
  153. { load flags to register }
  154. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  155. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  156. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  157. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  158. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  159. end;
  160. function tx86typeconvnode.first_int_to_real : tnode;
  161. begin
  162. first_int_to_real:=nil;
  163. expectloc:=LOC_FPUREGISTER;
  164. end;
  165. procedure tx86typeconvnode.second_int_to_real;
  166. var
  167. href : treference;
  168. l1,l2 : tasmlabel;
  169. signtested : boolean;
  170. begin
  171. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  172. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  173. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  174. if (left.location.loc=LOC_REGISTER) and (torddef(left.resultdef).ordtype=u64bit) then
  175. begin
  176. {$ifdef cpu64bit}
  177. emit_const_reg(A_BT,S_Q,63,left.location.register);
  178. {$else cpu64bit}
  179. emit_const_reg(A_BT,S_L,31,left.location.register64.reghi);
  180. {$endif cpu64bit}
  181. signtested:=true;
  182. end
  183. else
  184. signtested:=false;
  185. { We need to load from a reference }
  186. location_force_mem(current_asmdata.CurrAsmList,left.location);
  187. { For u32bit we need to load it as comp and need to
  188. make it 64bits }
  189. if (torddef(left.resultdef).ordtype=u32bit) then
  190. begin
  191. tg.GetTemp(current_asmdata.CurrAsmList,8,tt_normal,href);
  192. location_freetemp(current_asmdata.CurrAsmList,left.location);
  193. cg.a_load_ref_ref(current_asmdata.CurrAsmList,left.location.size,OS_32,left.location.reference,href);
  194. inc(href.offset,4);
  195. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_32,0,href);
  196. dec(href.offset,4);
  197. left.location.reference:=href;
  198. end;
  199. { Load from reference to fpu reg }
  200. case torddef(left.resultdef).ordtype of
  201. u32bit,
  202. scurrency,
  203. s64bit:
  204. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,left.location.reference));
  205. u64bit:
  206. begin
  207. { unsigned 64 bit ints are harder to handle:
  208. we load bits 0..62 and then check bit 63:
  209. if it is 1 then we add $80000000 000000000
  210. as double }
  211. current_asmdata.getdatalabel(l1);
  212. current_asmdata.getjumplabel(l2);
  213. if not(signtested) then
  214. begin
  215. inc(left.location.reference.offset,4);
  216. emit_const_ref(A_BT,S_L,31,left.location.reference);
  217. dec(left.location.reference.offset,4);
  218. end;
  219. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,left.location.reference));
  220. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2);
  221. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  222. { I got this constant from a test program (FK) }
  223. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  224. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(longint ($80000000)));
  225. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($0000403f));
  226. reference_reset_symbol(href,l1,0);
  227. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  228. current_asmdata.CurrAsmList.concat(Taicpu.Op_ref(A_FLD,S_FX,href));
  229. current_asmdata.CurrAsmList.concat(Taicpu.Op_reg_reg(A_FADDP,S_NO,NR_ST,NR_ST1));
  230. cg.a_label(current_asmdata.CurrAsmList,l2);
  231. end
  232. else
  233. begin
  234. if left.resultdef.size<4 then
  235. begin
  236. tg.GetTemp(current_asmdata.CurrAsmList,4,tt_normal,href);
  237. location_freetemp(current_asmdata.CurrAsmList,left.location);
  238. cg.a_load_ref_ref(current_asmdata.CurrAsmList,left.location.size,OS_32,left.location.reference,href);
  239. left.location.reference:=href;
  240. end;
  241. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IL,left.location.reference));
  242. end;
  243. end;
  244. location_freetemp(current_asmdata.CurrAsmList,left.location);
  245. tcgx86(cg).inc_fpu_stack;
  246. location.register:=NR_ST;
  247. end;
  248. begin
  249. ctypeconvnode:=tx86typeconvnode
  250. end.