ncgcnv.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Generate assembler for nodes that handle type conversions which are
  4. the same for all (most) processors
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncgcnv;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncnv,defutil,defcmp;
  23. type
  24. tcgtypeconvnode = class(ttypeconvnode)
  25. procedure second_int_to_int;override;
  26. procedure second_cstring_to_pchar;override;
  27. procedure second_cstring_to_int;override;
  28. procedure second_string_to_chararray;override;
  29. procedure second_array_to_pointer;override;
  30. procedure second_pointer_to_array;override;
  31. procedure second_char_to_string;override;
  32. procedure second_real_to_real;override;
  33. procedure second_cord_to_pointer;override;
  34. procedure second_proc_to_procvar;override;
  35. procedure second_nil_to_methodprocvar;override;
  36. procedure second_bool_to_int;override;
  37. procedure second_bool_to_bool;override;
  38. procedure second_ansistring_to_pchar;override;
  39. procedure second_class_to_intf;override;
  40. procedure second_char_to_char;override;
  41. procedure second_nothing;override;
  42. procedure pass_generate_code;override;
  43. end;
  44. tcgasnode = class(tasnode)
  45. procedure pass_generate_code;override;
  46. end;
  47. implementation
  48. uses
  49. cutils,verbose,globtype,globals,
  50. aasmbase,aasmtai,aasmdata,aasmcpu,symconst,symdef,paramgr,
  51. ncon,ncal,
  52. cpubase,systems,
  53. procinfo,pass_2,
  54. cgbase,
  55. cgutils,cgobj,
  56. ncgutil,
  57. tgobj
  58. ;
  59. procedure tcgtypeconvnode.second_int_to_int;
  60. var
  61. orgsize,
  62. newsize : tcgsize;
  63. ressize,
  64. leftsize : longint;
  65. begin
  66. newsize:=def_cgsize(resultdef);
  67. { insert range check if not explicit conversion }
  68. if not(nf_explicit in flags) then
  69. cg.g_rangecheck(current_asmdata.CurrAsmList,left.location,left.resultdef,resultdef);
  70. { is the result size smaller? when typecasting from void
  71. we always reuse the current location, because there is
  72. nothing that we can load in a register }
  73. ressize := resultdef.size;
  74. leftsize := left.resultdef.size;
  75. if (ressize<>leftsize) and
  76. not is_void(left.resultdef) then
  77. begin
  78. location_copy(location,left.location);
  79. { reuse a loc_reference when the newsize is smaller than
  80. than the original, else load it to a register }
  81. if (location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  82. (ressize<leftsize) then
  83. begin
  84. location.size:=newsize;
  85. if (target_info.endian = ENDIAN_BIG) then
  86. inc(location.reference.offset,leftsize-ressize);
  87. end
  88. else
  89. location_force_reg(current_asmdata.CurrAsmList,location,newsize,false);
  90. {$ifndef cpu64bit}
  91. // if is_signed(left.resultdef) and
  92. {$endif cpu64bit}
  93. end
  94. else
  95. begin
  96. { no special loading is required, reuse current location }
  97. { that's not true, if you go from signed to unsiged or }
  98. { vice versa, you need sign extension/removal if the }
  99. { value is already in a register (at least for archs }
  100. { which don't have 8bit register components etc) (JM) }
  101. location_copy(location,left.location);
  102. location.size:=newsize;
  103. orgsize := def_cgsize(left.resultdef);
  104. if (ressize < tcgsize2size[OS_INT]) and
  105. (location.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  106. (orgsize <> newsize) then
  107. begin
  108. location.register := cg.getintregister(current_asmdata.CurrAsmList,newsize);
  109. location.loc := LOC_REGISTER;
  110. cg.a_load_reg_reg(current_asmdata.CurrAsmList,orgsize,newsize,left.location.register,location.register);
  111. end;
  112. end;
  113. end;
  114. procedure tcgtypeconvnode.second_cstring_to_pchar;
  115. var
  116. hr : treference;
  117. begin
  118. if left.nodetype<>stringconstn then
  119. internalerror(200601131);
  120. location_reset(location,LOC_REGISTER,OS_ADDR);
  121. case tstringconstnode(left).cst_type of
  122. cst_conststring :
  123. begin
  124. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  125. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  126. end;
  127. cst_shortstring :
  128. begin
  129. inc(left.location.reference.offset);
  130. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  131. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  132. end;
  133. cst_ansistring :
  134. begin
  135. if tstringconstnode(left).len=0 then
  136. begin
  137. reference_reset(hr);
  138. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  139. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  140. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  141. end
  142. else
  143. begin
  144. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  145. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  146. end;
  147. end;
  148. cst_longstring:
  149. begin
  150. {!!!!!!!}
  151. internalerror(8888);
  152. end;
  153. cst_widestring:
  154. begin
  155. if tstringconstnode(left).len=0 then
  156. begin
  157. reference_reset(hr);
  158. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  159. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  160. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  161. end
  162. else
  163. begin
  164. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  165. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_INT,left.location.reference,
  166. location.register);
  167. end;
  168. end;
  169. end;
  170. end;
  171. procedure tcgtypeconvnode.second_cstring_to_int;
  172. begin
  173. { this can't happen because constants are already processed in
  174. pass 1 }
  175. internalerror(200510013);
  176. end;
  177. procedure tcgtypeconvnode.second_string_to_chararray;
  178. begin
  179. if is_chararray(left.resultdef) then
  180. begin
  181. location_copy(location,left.location);
  182. exit;
  183. end;
  184. { should be handled already in resultdef pass (JM) }
  185. internalerror(200108292);
  186. end;
  187. procedure tcgtypeconvnode.second_array_to_pointer;
  188. begin
  189. location_reset(location,LOC_REGISTER,OS_ADDR);
  190. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  191. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  192. end;
  193. procedure tcgtypeconvnode.second_pointer_to_array;
  194. begin
  195. location_reset(location,LOC_REFERENCE,OS_NO);
  196. case left.location.loc of
  197. LOC_CREGISTER,
  198. LOC_REGISTER :
  199. begin
  200. {$ifdef cpu_uses_separate_address_registers}
  201. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  202. begin
  203. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  204. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  205. left.location.register,location.reference.base);
  206. end
  207. else
  208. {$endif}
  209. location.reference.base := left.location.register;
  210. end;
  211. LOC_REFERENCE,
  212. LOC_CREFERENCE :
  213. begin
  214. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  215. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,
  216. location.reference.base);
  217. location_freetemp(current_asmdata.CurrAsmList,left.location);
  218. end;
  219. else
  220. internalerror(2002032216);
  221. end;
  222. end;
  223. procedure tcgtypeconvnode.second_char_to_string;
  224. begin
  225. location_reset(location,LOC_REFERENCE,OS_NO);
  226. case tstringdef(resultdef).stringtype of
  227. st_shortstring :
  228. begin
  229. tg.GetTemp(current_asmdata.CurrAsmList,256,tt_normal,location.reference);
  230. cg.a_load_loc_ref(current_asmdata.CurrAsmList,left.location.size,left.location,
  231. location.reference);
  232. location_freetemp(current_asmdata.CurrAsmList,left.location);
  233. end;
  234. { the rest is removed in the resultdef pass and converted to compilerprocs }
  235. else
  236. internalerror(4179);
  237. end;
  238. end;
  239. procedure tcgtypeconvnode.second_real_to_real;
  240. begin
  241. location_reset(location,expectloc,def_cgsize(resultdef));
  242. {$ifdef x86}
  243. { extended types in memory which should be loaded into the sse unit
  244. must be converted by the fpu first, so force them to be loaded into
  245. the fpu }
  246. if (expectloc=LOC_MMREGISTER) and
  247. (left.location.size in [OS_F80,OS_C64]) then
  248. begin
  249. if (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) then
  250. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  251. { round them down to the proper precision }
  252. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,left.location.register);
  253. left.location.size:=location.size;
  254. end;
  255. {$endif x86}
  256. case left.location.loc of
  257. LOC_FPUREGISTER,
  258. LOC_CFPUREGISTER:
  259. begin
  260. case expectloc of
  261. LOC_FPUREGISTER:
  262. begin
  263. { on sparc a move from double -> single means from two to one register. }
  264. { On all other platforms it also needs rounding to avoid that }
  265. { single(double_regvar) = double_regvar is true in all cases }
  266. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  267. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register);
  268. end;
  269. LOC_MMREGISTER:
  270. begin
  271. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  272. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  273. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register,mms_movescalar);
  274. end
  275. else
  276. internalerror(2003012262);
  277. end;
  278. exit
  279. end;
  280. LOC_CREFERENCE,
  281. LOC_REFERENCE:
  282. begin
  283. if expectloc=LOC_MMREGISTER then
  284. begin
  285. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  286. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar)
  287. end
  288. else
  289. begin
  290. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  291. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  292. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register);
  293. end;
  294. location_freetemp(current_asmdata.CurrAsmList,left.location);
  295. end;
  296. LOC_MMREGISTER,
  297. LOC_CMMREGISTER:
  298. begin
  299. case expectloc of
  300. LOC_FPUREGISTER:
  301. begin
  302. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  303. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  304. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register);
  305. end;
  306. LOC_MMREGISTER:
  307. begin
  308. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  309. cg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,left.location.size,location.size,left.location.register,location.register,mms_movescalar);
  310. end;
  311. else
  312. internalerror(2003012261);
  313. end;
  314. end;
  315. else
  316. internalerror(2002032215);
  317. end;
  318. end;
  319. procedure tcgtypeconvnode.second_cord_to_pointer;
  320. begin
  321. { this can't happen because constants are already processed in
  322. pass 1 }
  323. internalerror(47423985);
  324. end;
  325. procedure tcgtypeconvnode.second_proc_to_procvar;
  326. begin
  327. if tabstractprocdef(resultdef).is_addressonly then
  328. begin
  329. location_reset(location,LOC_REGISTER,OS_ADDR);
  330. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  331. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  332. end
  333. else
  334. location_copy(location,left.location);
  335. end;
  336. procedure Tcgtypeconvnode.second_nil_to_methodprocvar;
  337. var r:Treference;
  338. begin
  339. tg.gettemp(current_asmdata.currasmlist,2*sizeof(aword),tt_normal,r);
  340. location_reset(location,LOC_REFERENCE,OS_NO);
  341. location.reference:=r;
  342. cg.a_load_const_ref(current_asmdata.currasmlist,OS_ADDR,0,r);
  343. inc(r.offset,sizeof(aword));
  344. cg.a_load_const_ref(current_asmdata.currasmlist,OS_ADDR,0,r);
  345. end;
  346. procedure tcgtypeconvnode.second_bool_to_int;
  347. var
  348. oldTrueLabel,oldFalseLabel : tasmlabel;
  349. begin
  350. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  351. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  352. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  353. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  354. secondpass(left);
  355. location_copy(location,left.location);
  356. { byte(boolean) or word(wordbool) or longint(longbool) must }
  357. { be accepted for var parameters }
  358. if not((nf_explicit in flags) and
  359. (left.resultdef.size=resultdef.size) and
  360. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER])) then
  361. location_force_reg(current_asmdata.CurrAsmList,location,def_cgsize(resultdef),false);
  362. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  363. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  364. end;
  365. procedure tcgtypeconvnode.second_bool_to_bool;
  366. begin
  367. { we can reuse the conversion already available
  368. in bool_to_int to resize the value. But when the
  369. size of the new boolean is smaller we need to calculate
  370. the value as is done in int_to_bool. This is needed because
  371. the bits that define the true status can be outside the limits
  372. of the new size and truncating the register can result in a 0
  373. value }
  374. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  375. begin
  376. secondpass(left);
  377. if (left.location.loc <> left.expectloc) then
  378. internalerror(20060409);
  379. location_copy(location,left.location);
  380. end
  381. else
  382. if resultdef.size<left.resultdef.size then
  383. second_int_to_bool
  384. else
  385. second_bool_to_int;
  386. end;
  387. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  388. var
  389. l1 : tasmlabel;
  390. hr : treference;
  391. begin
  392. location_reset(location,LOC_REGISTER,OS_ADDR);
  393. current_asmdata.getjumplabel(l1);
  394. case left.location.loc of
  395. LOC_CREGISTER,LOC_REGISTER:
  396. begin
  397. {$ifdef cpu_uses_separate_address_registers}
  398. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  399. begin
  400. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  401. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  402. left.location.register,location.register);
  403. end
  404. else
  405. {$endif}
  406. location.register := left.location.register;
  407. end;
  408. LOC_CREFERENCE,LOC_REFERENCE:
  409. begin
  410. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  411. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  412. end;
  413. else
  414. internalerror(2002032214);
  415. end;
  416. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_NE,0,location.register,l1);
  417. reference_reset(hr);
  418. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  419. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  420. cg.a_label(current_asmdata.CurrAsmList,l1);
  421. end;
  422. procedure tcgtypeconvnode.second_class_to_intf;
  423. var
  424. l1 : tasmlabel;
  425. hd : tobjectdef;
  426. ImplIntf : TImplementedInterface;
  427. begin
  428. location_reset(location,LOC_REGISTER,OS_ADDR);
  429. case left.location.loc of
  430. LOC_CREFERENCE,
  431. LOC_REFERENCE:
  432. begin
  433. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  434. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  435. location_freetemp(current_asmdata.CurrAsmList,left.location);
  436. end;
  437. LOC_CREGISTER:
  438. begin
  439. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  440. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.register);
  441. end;
  442. LOC_REGISTER:
  443. location.register:=left.location.register;
  444. else
  445. internalerror(121120001);
  446. end;
  447. current_asmdata.getjumplabel(l1);
  448. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,location.register,l1);
  449. hd:=tobjectdef(left.resultdef);
  450. while assigned(hd) do
  451. begin
  452. ImplIntf:=hd.find_implemented_interface(tobjectdef(resultdef));
  453. if assigned(ImplIntf) then
  454. begin
  455. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,ImplIntf.ioffset,location.register);
  456. break;
  457. end;
  458. hd:=hd.childof;
  459. end;
  460. if hd=nil then
  461. internalerror(2002081301);
  462. cg.a_label(current_asmdata.CurrAsmList,l1);
  463. end;
  464. procedure tcgtypeconvnode.second_char_to_char;
  465. begin
  466. internalerror(2007081202);
  467. end;
  468. procedure tcgtypeconvnode.second_nothing;
  469. var
  470. newsize : tcgsize;
  471. begin
  472. { we reuse the old value }
  473. location_copy(location,left.location);
  474. { Floats should never be returned as LOC_CONSTANT, do the
  475. moving to memory before the new size is set.
  476. Also when converting from a float to a non-float
  477. or the other way round, move to memory first to prevent
  478. invalid LOC_FPUREGISTER locations }
  479. if (
  480. (resultdef.typ=floatdef) and
  481. (location.loc=LOC_CONSTANT)
  482. ) or
  483. (
  484. (left.resultdef.typ=floatdef) xor
  485. (resultdef.typ=floatdef)
  486. ) then
  487. location_force_mem(current_asmdata.CurrAsmList,location);
  488. { but use the new size, but we don't know the size of all arrays }
  489. newsize:=def_cgsize(resultdef);
  490. location.size:=newsize;
  491. end;
  492. {$ifdef TESTOBJEXT2}
  493. procedure tcgtypeconvnode.checkobject;
  494. begin
  495. { no checking by default }
  496. end;
  497. {$endif TESTOBJEXT2}
  498. procedure tcgtypeconvnode.pass_generate_code;
  499. begin
  500. { the boolean routines can be called with LOC_JUMP and
  501. call secondpass themselves in the helper }
  502. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  503. begin
  504. secondpass(left);
  505. if codegenerror then
  506. exit;
  507. end;
  508. second_call_helper(convtype);
  509. {$ifdef TESTOBJEXT2}
  510. { Check explicit conversions to objects pointers !! }
  511. if p^.explizit and
  512. (p^.resultdef.typ=pointerdef) and
  513. (tpointerdef(p^.resultdef).definition.typ=objectdef) and not
  514. (tobjectdef(tpointerdef(p^.resultdef).definition).isclass) and
  515. ((tobjectdef(tpointerdef(p^.resultdef).definition).options and oo_hasvmt)<>0) and
  516. (cs_check_range in current_settings.localswitches) then
  517. checkobject;
  518. {$endif TESTOBJEXT2}
  519. end;
  520. procedure tcgasnode.pass_generate_code;
  521. begin
  522. secondpass(call);
  523. location_copy(location,call.location);
  524. end;
  525. begin
  526. ctypeconvnode := tcgtypeconvnode;
  527. casnode := tcgasnode;
  528. end.