nx86cnv.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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;
  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,globals,globtype,
  50. aasmbase,aasmtai,aasmdata,aasmcpu,
  51. symconst,symdef,
  52. cgbase,cga,pass_1,pass_2,
  53. cpuinfo,
  54. ncnv,
  55. cpubase,
  56. cgutils,cgobj,hlcgobj,cgx86,
  57. tgobj;
  58. function tx86typeconvnode.first_real_to_real : tnode;
  59. begin
  60. first_real_to_real:=nil;
  61. { comp isn't a floating type }
  62. if (tfloatdef(resultdef).floattype=s64comp) and
  63. (tfloatdef(left.resultdef).floattype<>s64comp) and
  64. not (nf_explicit in flags) then
  65. CGMessage(type_w_convert_real_2_comp);
  66. if use_vectorfpu(resultdef) then
  67. expectloc:=LOC_MMREGISTER
  68. else
  69. expectloc:=LOC_FPUREGISTER;
  70. end;
  71. procedure tx86typeconvnode.second_int_to_bool;
  72. var
  73. {$ifndef cpu64bitalu}
  74. hreg2,
  75. hregister : tregister;
  76. href : treference;
  77. i : integer;
  78. {$endif not cpu64bitalu}
  79. resflags : tresflags;
  80. hlabel : tasmlabel;
  81. newsize : tcgsize;
  82. begin
  83. secondpass(left);
  84. if codegenerror then
  85. exit;
  86. { Explicit typecasts from any ordinal type to a boolean type }
  87. { must not change the ordinal value }
  88. if (nf_explicit in flags) and
  89. not(left.location.loc in [LOC_FLAGS,LOC_JUMP]) then
  90. begin
  91. location_copy(location,left.location);
  92. newsize:=def_cgsize(resultdef);
  93. { change of size? change sign only if location is LOC_(C)REGISTER? Then we have to sign/zero-extend }
  94. if (tcgsize2size[newsize]<>tcgsize2size[left.location.size]) or
  95. ((newsize<>left.location.size) and (location.loc in [LOC_REGISTER,LOC_CREGISTER])) then
  96. hlcg.location_force_reg(current_asmdata.CurrAsmList,location,left.resultdef,resultdef,true)
  97. else
  98. location.size:=newsize;
  99. exit;
  100. end;
  101. { Load left node into flag F_NE/F_E }
  102. resflags:=F_NE;
  103. if (left.location.loc in [LOC_SUBSETREG,LOC_CSUBSETREG,LOC_SUBSETREF,LOC_CSUBSETREF]) then
  104. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  105. case left.location.loc of
  106. LOC_CREFERENCE,
  107. LOC_REFERENCE :
  108. begin
  109. {$ifndef cpu64bitalu}
  110. if left.location.size in [OS_64,OS_S64{$ifdef cpu16bitalu},OS_32,OS_S32{$endif}] then
  111. begin
  112. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  113. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_INT,OS_INT,left.location.reference,hregister);
  114. href:=left.location.reference;
  115. for i:=2 to tcgsize2size[left.location.size] div tcgsize2size[OS_INT] do
  116. begin
  117. inc(href.offset,tcgsize2size[OS_INT]);
  118. cg.a_op_ref_reg(current_asmdata.CurrAsmList,OP_OR,OS_INT,href,hregister);
  119. end;
  120. end
  121. else
  122. {$endif not cpu64bitalu}
  123. begin
  124. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  125. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  126. end;
  127. end;
  128. LOC_FLAGS :
  129. begin
  130. resflags:=left.location.resflags;
  131. end;
  132. LOC_REGISTER,LOC_CREGISTER :
  133. begin
  134. {$if defined(cpu32bitalu)}
  135. if left.location.size in [OS_64,OS_S64] then
  136. begin
  137. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  138. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,left.location.register64.reglo,hregister);
  139. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_32,left.location.register64.reghi,hregister);
  140. end
  141. else
  142. {$elseif defined(cpu16bitalu)}
  143. if left.location.size in [OS_64,OS_S64] then
  144. begin
  145. hregister:=cg.getintregister(current_asmdata.CurrAsmList,OS_16);
  146. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_16,OS_16,left.location.register64.reglo,hregister);
  147. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,cg.GetNextReg(left.location.register64.reglo),hregister);
  148. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.register64.reghi,hregister);
  149. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,cg.GetNextReg(left.location.register64.reghi),hregister);
  150. end
  151. else
  152. if left.location.size in [OS_32,OS_S32] then
  153. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,OS_16,left.location.register,cg.GetNextReg(left.location.register))
  154. else
  155. {$endif}
  156. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_OR,left.location.size,left.location.register,left.location.register);
  157. end;
  158. LOC_JUMP :
  159. begin
  160. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  161. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  162. current_asmdata.getjumplabel(hlabel);
  163. cg.a_label(current_asmdata.CurrAsmList,left.location.truelabel);
  164. if not(is_cbool(resultdef)) then
  165. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,1,location.register)
  166. else
  167. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,-1,location.register);
  168. cg.a_jmp_always(current_asmdata.CurrAsmList,hlabel);
  169. cg.a_label(current_asmdata.CurrAsmList,left.location.falselabel);
  170. cg.a_load_const_reg(current_asmdata.CurrAsmList,location.size,0,location.register);
  171. cg.a_label(current_asmdata.CurrAsmList,hlabel);
  172. end;
  173. else
  174. internalerror(10062);
  175. end;
  176. if (left.location.loc<>LOC_JUMP) then
  177. begin
  178. { load flags to register }
  179. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  180. {$ifndef cpu64bitalu}
  181. if (location.size in [OS_64,OS_S64]) then
  182. begin
  183. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  184. cg.g_flags2reg(current_asmdata.CurrAsmList,OS_32,resflags,hreg2);
  185. if (is_cbool(resultdef)) then
  186. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,OS_32,hreg2,hreg2);
  187. location.register64.reglo:=hreg2;
  188. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  189. if (is_cbool(resultdef)) then
  190. { reglo is either 0 or -1 -> reghi has to become the same }
  191. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_32,OS_32,location.register64.reglo,location.register64.reghi)
  192. else
  193. { unsigned }
  194. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_32,0,location.register64.reghi);
  195. end
  196. else
  197. {$endif not cpu64bitalu}
  198. begin
  199. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  200. cg.g_flags2reg(current_asmdata.CurrAsmList,location.size,resflags,location.register);
  201. if (is_cbool(resultdef)) then
  202. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_NEG,location.size,location.register,location.register);
  203. end
  204. end;
  205. end;
  206. function tx86typeconvnode.first_int_to_real : tnode;
  207. begin
  208. first_int_to_real:=nil;
  209. if (left.resultdef.size<4) then
  210. begin
  211. inserttypeconv(left,s32inttype);
  212. firstpass(left)
  213. end;
  214. if use_vectorfpu(resultdef) and
  215. (torddef(left.resultdef).ordtype = s32bit) then
  216. expectloc:=LOC_MMREGISTER
  217. else
  218. expectloc:=LOC_FPUREGISTER;
  219. end;
  220. procedure tx86typeconvnode.second_int_to_real;
  221. var
  222. leftref,
  223. href : treference;
  224. l1,l2 : tasmlabel;
  225. op: tasmop;
  226. opsize: topsize;
  227. signtested : boolean;
  228. use_bt: boolean; { true = use BT (386+), false = use TEST (286-) }
  229. begin
  230. {$ifdef i8086}
  231. use_bt:=current_settings.cputype>=cpu_386;
  232. {$else i8086}
  233. use_bt:=true;
  234. {$endif i8086}
  235. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) then
  236. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  237. if use_vectorfpu(resultdef) and
  238. {$ifdef cpu64bitalu}
  239. (torddef(left.resultdef).ordtype in [s32bit,s64bit]) then
  240. {$else cpu64bitalu}
  241. (torddef(left.resultdef).ordtype=s32bit) then
  242. {$endif cpu64bitalu}
  243. begin
  244. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  245. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  246. if UseAVX then
  247. case location.size of
  248. OS_F32:
  249. op:=A_VCVTSI2SS;
  250. OS_F64:
  251. op:=A_VCVTSI2SD;
  252. else
  253. internalerror(2007120902);
  254. end
  255. else
  256. case location.size of
  257. OS_F32:
  258. op:=A_CVTSI2SS;
  259. OS_F64:
  260. op:=A_CVTSI2SD;
  261. else
  262. internalerror(2007120902);
  263. end;
  264. { don't use left.location.size, because that one may be OS_32/OS_64
  265. if the lower bound of the orddef >= 0
  266. }
  267. case torddef(left.resultdef).ordtype of
  268. s32bit:
  269. opsize:=S_L;
  270. s64bit:
  271. opsize:=S_Q;
  272. else
  273. internalerror(2007120903);
  274. end;
  275. case left.location.loc of
  276. LOC_REFERENCE,
  277. LOC_CREFERENCE:
  278. begin
  279. href:=left.location.reference;
  280. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  281. if UseAVX then
  282. { VCVTSI2.. requires a second source operand to copy bits 64..127 }
  283. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_reg(op,opsize,href,location.register,location.register))
  284. else
  285. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(op,opsize,href,location.register));
  286. end;
  287. LOC_REGISTER,
  288. LOC_CREGISTER:
  289. if UseAVX then
  290. { VCVTSI2.. requires a second source operand to copy bits 64..127 }
  291. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,opsize,left.location.register,location.register,location.register))
  292. else
  293. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,opsize,left.location.register,location.register));
  294. end;
  295. end
  296. else
  297. begin
  298. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  299. if (left.location.loc=LOC_REGISTER) and (torddef(left.resultdef).ordtype=u64bit) then
  300. begin
  301. if use_bt then
  302. begin
  303. {$if defined(cpu64bitalu)}
  304. emit_const_reg(A_BT,S_Q,63,left.location.register);
  305. {$elseif defined(cpu32bitalu)}
  306. emit_const_reg(A_BT,S_L,31,left.location.register64.reghi);
  307. {$elseif defined(cpu16bitalu)}
  308. emit_const_reg(A_BT,S_W,15,cg.GetNextReg(left.location.register64.reghi));
  309. {$endif}
  310. end
  311. else
  312. begin
  313. {$ifdef i8086}
  314. emit_const_reg(A_TEST,S_W,aint($8000),cg.GetNextReg(left.location.register64.reghi));
  315. {$else i8086}
  316. internalerror(2013052510);
  317. {$endif i8086}
  318. end;
  319. signtested:=true;
  320. end
  321. else
  322. signtested:=false;
  323. { We need to load from a reference }
  324. hlcg.location_force_mem(current_asmdata.CurrAsmList,left.location,left.resultdef);
  325. { don't change left.location.reference, because if it's a temp we
  326. need the original location at the end so we can free it }
  327. leftref:=left.location.reference;
  328. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,leftref);
  329. { For u32bit we need to load it as comp and need to
  330. make it 64bits }
  331. if (torddef(left.resultdef).ordtype=u32bit) then
  332. begin
  333. tg.GetTemp(current_asmdata.CurrAsmList,8,8,tt_normal,href);
  334. location_freetemp(current_asmdata.CurrAsmList,left.location);
  335. cg.a_load_ref_ref(current_asmdata.CurrAsmList,left.location.size,OS_32,leftref,href);
  336. inc(href.offset,4);
  337. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_32,0,href);
  338. dec(href.offset,4);
  339. { could be a temp with an offset > 32 bit on x86_64 }
  340. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  341. leftref:=href;
  342. end;
  343. { Load from reference to fpu reg }
  344. case torddef(left.resultdef).ordtype of
  345. u32bit,
  346. scurrency,
  347. s64bit:
  348. begin
  349. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,leftref));
  350. end;
  351. u64bit:
  352. begin
  353. { unsigned 64 bit ints are harder to handle:
  354. we load bits 0..62 and then check bit 63:
  355. if it is 1 then we add 2**64 as float.
  356. Since 2**64 can be represented exactly, use a single-precision
  357. constant to save space. }
  358. current_asmdata.getglobaldatalabel(l1);
  359. current_asmdata.getjumplabel(l2);
  360. if not(signtested) then
  361. begin
  362. if use_bt then
  363. begin
  364. {$if defined(cpu64bitalu) or defined(cpu32bitalu)}
  365. inc(leftref.offset,4);
  366. emit_const_ref(A_BT,S_L,31,leftref);
  367. dec(leftref.offset,4);
  368. {$elseif defined(cpu16bitalu)}
  369. inc(leftref.offset,6);
  370. emit_const_ref(A_BT,S_W,15,leftref);
  371. dec(leftref.offset,6);
  372. {$endif}
  373. end
  374. else
  375. begin
  376. {$ifdef i8086}
  377. { reading a byte, instead of word is faster on a true }
  378. { 8088, because of the 8-bit data bus }
  379. inc(leftref.offset,7);
  380. emit_const_ref(A_TEST,S_B,aint($80),leftref);
  381. dec(leftref.offset,7);
  382. {$else i8086}
  383. internalerror(2013052511);
  384. {$endif i8086}
  385. end;
  386. end;
  387. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IQ,leftref));
  388. if use_bt then
  389. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NC,l2)
  390. else
  391. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_E,l2);
  392. new_section(current_asmdata.asmlists[al_typedconsts],sec_rodata_norel,l1.name,const_align(sizeof(pint)));
  393. current_asmdata.asmlists[al_typedconsts].concat(Tai_label.Create(l1));
  394. { I got this constant from a test program (FK) }
  395. current_asmdata.asmlists[al_typedconsts].concat(Tai_const.Create_32bit($5f800000));
  396. reference_reset_symbol(href,l1,0,4,[]);
  397. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList,href);
  398. current_asmdata.CurrAsmList.concat(Taicpu.Op_ref(A_FADD,S_FS,href));
  399. cg.a_label(current_asmdata.CurrAsmList,l2);
  400. end
  401. else
  402. begin
  403. if left.resultdef.size<4 then
  404. internalerror(2007120901);
  405. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_FILD,S_IL,leftref));
  406. end;
  407. end;
  408. tcgx86(cg).inc_fpu_stack;
  409. location.register:=NR_ST;
  410. end;
  411. location_freetemp(current_asmdata.CurrAsmList,left.location);
  412. end;
  413. begin
  414. ctypeconvnode:=tx86typeconvnode
  415. end.