ncgcnv.pas 22 KB

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