nx86cnv.pas 19 KB

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