nx86cnv.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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_1,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. {$ifndef cpu64bitalu}
  73. hreg2,
  74. {$endif not cpu64bitalu}
  75. hregister : tregister;
  76. {$ifndef cpu64bitalu}
  77. href : treference;
  78. {$endif not cpu64bitalu}
  79. resflags : tresflags;
  80. hlabel,oldTrueLabel,oldFalseLabel : tasmlabel;
  81. newsize : tcgsize;
  82. begin
  83. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  84. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  85. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  86. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  87. secondpass(left);
  88. if codegenerror then
  89. exit;
  90. { Explicit typecasts from any ordinal type to a boolean type }
  91. { must not change the ordinal value }
  92. if (nf_explicit in flags) and
  93. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  94. begin
  95. location_copy(location,left.location);
  96. newsize:=def_cgsize(resultdef);
  97. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  98. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  99. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  100. location_force_reg(current_asmdata.CurrAsmList,location,newsize,true)
  101. else
  102. location.size:=newsize;
  103. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  104. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  105. exit;
  106. end;
  107. { Load left node into flag F_NE/F_E }
  108. resflags:=F_NE;
  109. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  110. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  111. case left.location.loc of
  112. LOC_CREFERENCE,
  113. LOC_REFERENCE :
  114. begin
  115. {$ifndef cpu64bitalu}
  116. if left.location.size in [OS_64,OS_S64] then
  117. begin
  118. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  119. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.reference,hregister);
  120. href:=left.location.reference;
  121. inc(href.offset,4);
  122. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,href,hregister);
  123. end
  124. else
  125. {$endif not cpu64bitalu}
  126. begin
  127. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,true);
  128. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  129. end;
  130. end;
  131. LOC_FLAGS :
  132. begin
  133. resflags:=left.location.resflags;
  134. end;
  135. LOC_REGISTER,LOC_CREGISTER :
  136. begin
  137. {$ifndef cpu64bitalu}
  138. if left.location.size in [OS_64,OS_S64] then
  139. begin
  140. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  141. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  142. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  143. end
  144. else
  145. {$endif not cpu64bitalu}
  146. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  147. end;
  148. LOC_JUMP :
  149. begin
  150. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  151. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  152. current_asmdata.getjumplabel(hlabel);
  153. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  154. if not(is_cbool(resultdef)) then
  155. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  156. else
  157. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  158. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  159. cg.a_label(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  160. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  161. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  162. end;
  163. else
  164. internalerror(10062);
  165. end;
  166. if (left.location.loc<>LOC_JUMP) then
  167. begin
  168. { load flags to register }
  169. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  170. {$ifndef cpu64bitalu}
  171. if (location.size in [OS_64,OS_S64]) then
  172. begin
  173. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  174. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  175. if (is_cbool(resultdef)) then
  176. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  177. location.register64.reglo:=hreg2;
  178. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  179. if (is_cbool(resultdef)) then
  180. { reglo is either 0 or -1 -> reghi has to become the same }
  181. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  182. else
  183. { unsigned }
  184. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  185. end
  186. else
  187. {$endif not cpu64bitalu}
  188. begin
  189. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  190. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  191. if (is_cbool(resultdef)) then
  192. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,location.register,location.register);
  193. end
  194. end;
  195. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  196. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  197. end;
  198. function tx86typeconvnode.first_int_to_real : tnode;
  199. begin
  200. first_int_to_real:=nil;
  201. if (left.resultdef.size<4) then
  202. begin
  203. inserttypeconv(left,s32inttype);
  204. firstpass(left)
  205. end;
  206. if use_sse(resultdef) and
  207. (torddef(left.resultdef).ordtype = s32bit) then
  208. expectloc:=LOC_MMREGISTER
  209. else
  210. expectloc:=LOC_FPUREGISTER;
  211. end;
  212. procedure tx86typeconvnode.second_int_to_real;
  213. var
  214. href : treference;
  215. l1,l2 : tasmlabel;
  216. op: tasmop;
  217. opsize: topsize;
  218. signtested : boolean;
  219. begin
  220. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  221. location_force_reg(current_asmdata.CurrAsmList,left.location,left.location.size,false);
  222. if use_sse(resultdef) and
  223. {$ifdef cpu64bitalu}
  224. (torddef(left.resultdef).ordtype in [s32bit,s64bit]) then
  225. {$else cpu64bitalu}
  226. (torddef(left.resultdef).ordtype=s32bit) then
  227. {$endif cpu64bitalu}
  228. begin
  229. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  230. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  231. case location.size of
  232. OS_F32:
  233. op:=A_CVTSI2SS;
  234. OS_F64:
  235. op:=A_CVTSI2SD;
  236. else
  237. internalerror(2007120902);
  238. end;
  239. { don't use left.location.size, because that one may be OS_32/OS_64
  240. if the lower bound of the orddef >= 0
  241. }
  242. case torddef(left.resultdef).ordtype of
  243. s32bit:
  244. opsize:=S_L;
  245. s64bit:
  246. opsize:=S_Q;
  247. else
  248. internalerror(2007120903);
  249. end;
  250. case left.location.loc of
  251. LOC_REFERENCE,
  252. LOC_CREFERENCE:
  253. begin
  254. href:=left.location.reference;
  255. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  256. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,opsize,href,location.register));
  257. end;
  258. LOC_REGISTER,
  259. LOC_CREGISTER:
  260. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,opsize,left.location.register,location.register));
  261. end;
  262. end
  263. else
  264. begin
  265. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  266. if (left.location.loc=LOC_REGISTER) and (torddef(left.resultdef).ordtype=u64bit) then
  267. begin
  268. {$ifdef cpu64bitalu}
  269. emit_const_reg(A_BT,S_Q,63,left.location.register);
  270. {$else cpu64bitalu}
  271. emit_const_reg(A_BT,S_L,31,left.location.register64.reghi);
  272. {$endif cpu64bitalu}
  273. signtested:=true;
  274. end
  275. else
  276. signtested:=false;
  277. { We need to load from a reference }
  278. location_force_mem(current_asmdata.CurrAsmList,left.location);
  279. { For u32bit we need to load it as comp and need to
  280. make it 64bits }
  281. if (torddef(left.resultdef).ordtype=u32bit) then
  282. begin
  283. tg.GetTemp(current_asmdata.CurrAsmList,8,8,tt_normal,href);
  284. location_freetemp(current_asmdata.CurrAsmList,left.location);
  285. cg.a_load_ref_ref(current_asmdata.CurrAsmList,left.location.size,OS_32,left.location.reference,href);
  286. inc(href.offset,4);
  287. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_32,0,href);
  288. dec(href.offset,4);
  289. left.location.reference:=href;
  290. end;
  291. { Load from reference to fpu reg }
  292. case torddef(left.resultdef).ordtype of
  293. u32bit,
  294. scurrency,
  295. s64bit:
  296. begin
  297. href:=left.location.reference;
  298. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  299. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,href));
  300. end;
  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,4);
  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. internalerror(2007120901);
  332. href:=left.location.reference;
  333. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  334. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IL,href));
  335. end;
  336. end;
  337. tcgx86(cg).inc_fpu_stack;
  338. location.register:=NR_ST;
  339. end;
  340. location_freetemp(current_asmdata.CurrAsmList,left.location);
  341. end;
  342. begin
  343. ctypeconvnode:=tx86typeconvnode
  344. end.