nx86cnv.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. begin
  67. if registersmm<1 then
  68. registersmm:=1;
  69. expectloc:=LOC_MMREGISTER;
  70. end
  71. else
  72. begin
  73. if registersfpu<1 then
  74. registersfpu:=1;
  75. expectloc:=LOC_FPUREGISTER;
  76. end;
  77. end;
  78. procedure tx86typeconvnode.second_int_to_bool;
  79. var
  80. hregister : tregister;
  81. {$ifndef cpu64bit}
  82. href : treference;
  83. {$endif cpu64bit}
  84. resflags : tresflags;
  85. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  86. begin
  87. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  88. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  89. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  90. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  91. secondpass(left);
  92. if codegenerror then
  93. exit;
  94. { byte(boolean) or word(wordbool) or longint(longbool) must }
  95. { be accepted for var parameters }
  96. if (nf_explicit in flags) and
  97. (left.resultdef.size=resultdef.size) and
  98. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER]) then
  99. begin
  100. location_copy(location,left.location);
  101. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  102. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  103. exit;
  104. end;
  105. { Load left node into flag F_NE/F_E }
  106. resflags:=F_NE;
  107. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  108. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  109. case left.location.loc of
  110. LOC_CREFERENCE,
  111. LOC_REFERENCE :
  112. begin
  113. {$ifndef cpu64bit}
  114. if left.location.size in [OS_64,OS_S64] then
  115. begin
  116. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  117. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  118. href:=left.location.reference;
  119. inc(href.offset,4);
  120. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  121. end
  122. else
  123. {$endif cpu64bit}
  124. begin
  125. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  126. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  127. end;
  128. end;
  129. LOC_FLAGS :
  130. begin
  131. resflags:=left.location.resflags;
  132. end;
  133. LOC_REGISTER,LOC_CREGISTER :
  134. begin
  135. {$ifndef cpu64bit}
  136. if left.location.size in [OS_64,OS_S64] then
  137. begin
  138. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  139. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  140. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  141. end
  142. else
  143. {$endif cpu64bit}
  144. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  145. end;
  146. LOC_JUMP :
  147. begin
  148. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  149. current_asmdata.getjumplabel(hlabel);
  150. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  151. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,1,hregister);
  152. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  153. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  154. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,0,hregister);
  155. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  156. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,hregister,hregister);
  157. end;
  158. else
  159. internalerror(10062);
  160. end;
  161. { load flags to register }
  162. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  163. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  164. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  165. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  166. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  167. end;
  168. function tx86typeconvnode.first_int_to_real : tnode;
  169. begin
  170. first_int_to_real:=nil;
  171. if registersfpu<1 then
  172. registersfpu:=1;
  173. expectloc:=LOC_FPUREGISTER;
  174. end;
  175. procedure tx86typeconvnode.second_int_to_real;
  176. var
  177. href : treference;
  178. hregister : tregister;
  179. l1,l2 : tasmlabel;
  180. signtested : boolean;
  181. hreg : tregister;
  182. op : tasmop;
  183. begin
  184. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  185. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  186. {$ifdef x86_64}
  187. if use_sse(resultdef) then
  188. begin
  189. { We can only directly convert s32bit and s64bit,u64bit values, for other
  190. values convert first to s64bit }
  191. if not(torddef(left.resultdef).ordtype in [s32bit,s64bit,u64bit]) then
  192. begin
  193. hreg:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  194. location_force_reg(current_asmdata.CurrAsmList,left.location,OS_S64,false);
  195. end;
  196. if is_double(resultdef) then
  197. op:=A_CVTSI2SD
  198. else if is_single(resultdef) then
  199. op:=A_CVTSI2SS
  200. else
  201. internalerror(200506061);
  202. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  203. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  204. case torddef(left.resultdef).ordtype of
  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. { Get sign bit }
  214. if (left.location.loc=LOC_REGISTER) then
  215. begin
  216. emit_const_reg(A_BT,S_Q,63,left.location.register);
  217. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,S_Q,left.location.register,location.register));
  218. end
  219. else
  220. begin
  221. inc(left.location.reference.offset,4);
  222. emit_const_ref(A_BT,S_L,31,left.location.reference);
  223. dec(left.location.reference.offset,4);
  224. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,S_Q,left.location.reference,location.register));
  225. end;
  226. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2);
  227. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  228. reference_reset_symbol(href,l1,0);
  229. { I got these constant from a test program (FK) }
  230. if is_double(resultdef) then
  231. begin
  232. { double (2^64) }
  233. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  234. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($43f00000));
  235. { simplify for PIC }
  236. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  237. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSD,S_NO,href,location.register));
  238. end
  239. else if is_single(resultdef) then
  240. begin
  241. { single(2^64) }
  242. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($5f800000));
  243. { simplify for PIC }
  244. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  245. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ADDSS,S_NO,href,location.register));
  246. end
  247. else
  248. internalerror(200506071);
  249. cg.a_label(current_asmdata.CurrAsmList,l2);
  250. end
  251. else
  252. begin
  253. case left.location.loc of
  254. LOC_CREFERENCE,
  255. LOC_REFERENCE :
  256. current_asmdata.CurrAsmList.concat(Taicpu.op_ref_reg(op,tcgsize2opsize[left.location.size],left.location.reference,location.register));
  257. LOC_CREGISTER,
  258. LOC_REGISTER :
  259. current_asmdata.CurrAsmList.concat(Taicpu.op_reg_reg(op,tcgsize2opsize[left.location.size],left.location.register,location.register));
  260. else
  261. internalerror(200506072);
  262. end;
  263. end;
  264. end;
  265. end
  266. else
  267. {$endif x86_64}
  268. begin
  269. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  270. if (left.location.loc=LOC_REGISTER) and (torddef(left.resultdef).ordtype=u64bit) then
  271. begin
  272. {$ifdef cpu64bit}
  273. emit_const_reg(A_BT,S_Q,63,left.location.register);
  274. {$else cpu64bit}
  275. emit_const_reg(A_BT,S_L,31,left.location.register64.reghi);
  276. {$endif cpu64bit}
  277. signtested:=true;
  278. end
  279. else
  280. signtested:=false;
  281. { We need to load from a reference }
  282. location_force_mem(current_asmdata.CurrAsmList,left.location);
  283. { For u32bit we need to load it as comp and need to
  284. make it 64bits }
  285. if (torddef(left.resultdef).ordtype=u32bit) then
  286. begin
  287. tg.GetTemp(current_asmdata.CurrAsmList,8,tt_normal,href);
  288. location_freetemp(current_asmdata.CurrAsmList,left.location);
  289. cg.a_load_ref_ref(current_asmdata.CurrAsmList,left.location.size,OS_32,left.location.reference,href);
  290. inc(href.offset,4);
  291. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_32,0,href);
  292. dec(href.offset,4);
  293. left.location.reference:=href;
  294. end;
  295. { Load from reference to fpu reg }
  296. case torddef(left.resultdef).ordtype of
  297. u32bit,
  298. scurrency,
  299. s64bit:
  300. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,left.location.reference));
  301. u64bit:
  302. begin
  303. { unsigned 64 bit ints are harder to handle:
  304. we load bits 0..62 and then check bit 63:
  305. if it is 1 then we add $80000000 000000000
  306. as double }
  307. current_asmdata.getdatalabel(l1);
  308. current_asmdata.getjumplabel(l2);
  309. if not(signtested) then
  310. begin
  311. inc(left.location.reference.offset,4);
  312. emit_const_ref(A_BT,S_L,31,left.location.reference);
  313. dec(left.location.reference.offset,4);
  314. end;
  315. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,left.location.reference));
  316. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2);
  317. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  318. { I got this constant from a test program (FK) }
  319. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(0));
  320. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit(longint ($80000000)));
  321. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($0000403f));
  322. reference_reset_symbol(href,l1,0);
  323. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  324. current_asmdata.CurrAsmList.concat(Taicpu.Op_ref(A_FLD,S_FX,href));
  325. current_asmdata.CurrAsmList.concat(Taicpu.Op_reg_reg(A_FADDP,S_NO,NR_ST,NR_ST1));
  326. cg.a_label(current_asmdata.CurrAsmList,l2);
  327. end
  328. else
  329. begin
  330. if left.resultdef.size<4 then
  331. begin
  332. tg.GetTemp(current_asmdata.CurrAsmList,4,tt_normal,href);
  333. location_freetemp(current_asmdata.CurrAsmList,left.location);
  334. cg.a_load_ref_ref(current_asmdata.CurrAsmList,left.location.size,OS_32,left.location.reference,href);
  335. left.location.reference:=href;
  336. end;
  337. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IL,left.location.reference));
  338. end;
  339. end;
  340. location_freetemp(current_asmdata.CurrAsmList,left.location);
  341. tcgx86(cg).inc_fpu_stack;
  342. location.register:=NR_ST;
  343. end;
  344. end;
  345. begin
  346. ctypeconvnode:=tx86typeconvnode
  347. end.