ncgcnv.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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_REGISTER :
  197. begin
  198. {$ifdef cpu_uses_separate_address_registers}
  199. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  200. begin
  201. location.reference.base:=rg.getaddressregister(current_asmdata.CurrAsmList);
  202. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  203. left.location.register,location.reference.base);
  204. end
  205. else
  206. {$endif}
  207. location.reference.base := left.location.register;
  208. end;
  209. LOC_CREGISTER :
  210. begin
  211. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  212. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,
  213. location.reference.base);
  214. end;
  215. LOC_REFERENCE,
  216. LOC_CREFERENCE :
  217. begin
  218. location.reference.base:=cg.getaddressregister(current_asmdata.CurrAsmList);
  219. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,
  220. location.reference.base);
  221. location_freetemp(current_asmdata.CurrAsmList,left.location);
  222. end;
  223. else
  224. internalerror(2002032216);
  225. end;
  226. end;
  227. procedure tcgtypeconvnode.second_char_to_string;
  228. begin
  229. location_reset(location,LOC_REFERENCE,OS_NO);
  230. case tstringdef(resultdef).string_typ of
  231. st_shortstring :
  232. begin
  233. tg.GetTemp(current_asmdata.CurrAsmList,256,tt_normal,location.reference);
  234. cg.a_load_loc_ref(current_asmdata.CurrAsmList,left.location.size,left.location,
  235. location.reference);
  236. location_freetemp(current_asmdata.CurrAsmList,left.location);
  237. end;
  238. { the rest is removed in the resultdef pass and converted to compilerprocs }
  239. else
  240. internalerror(4179);
  241. end;
  242. end;
  243. procedure tcgtypeconvnode.second_real_to_real;
  244. begin
  245. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  246. {$ifdef x86}
  247. { extended types in memory which should be loaded into the sse unit
  248. must be converted by the fpu first, so force them to be loaded into
  249. the fpu }
  250. if (expectloc=LOC_MMREGISTER) and
  251. (left.location.loc in [LOC_CREFERENCE,LOC_REFERENCE]) and
  252. (left.location.size=OS_F80) then
  253. location_force_fpureg(current_asmdata.CurrAsmList,left.location,false);
  254. {$endif x86}
  255. case left.location.loc of
  256. LOC_FPUREGISTER,
  257. LOC_CFPUREGISTER:
  258. begin
  259. location_copy(location,left.location);
  260. location.size:=def_cgsize(resultdef);
  261. case expectloc of
  262. LOC_FPUREGISTER:
  263. ;
  264. LOC_MMREGISTER:
  265. location_force_mmregscalar(current_asmdata.CurrAsmList,location,false);
  266. else
  267. internalerror(2003012262);
  268. end;
  269. exit
  270. end;
  271. LOC_CREFERENCE,
  272. LOC_REFERENCE:
  273. begin
  274. if expectloc=LOC_MMREGISTER then
  275. begin
  276. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  277. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  278. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar)
  279. end
  280. else
  281. begin
  282. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,left.location.size);
  283. cg.a_loadfpu_loc_reg(current_asmdata.CurrAsmList,left.location,location.register);
  284. end;
  285. location_freetemp(current_asmdata.CurrAsmList,left.location);
  286. end;
  287. LOC_MMREGISTER,
  288. LOC_CMMREGISTER:
  289. begin
  290. location_copy(location,left.location);
  291. case expectloc of
  292. LOC_FPUREGISTER:
  293. begin
  294. location_force_fpureg(current_asmdata.CurrAsmList,location,false);
  295. location.size:=def_cgsize(resultdef);
  296. end;
  297. LOC_MMREGISTER:
  298. ;
  299. else
  300. internalerror(2003012261);
  301. end;
  302. end;
  303. else
  304. internalerror(2002032215);
  305. end;
  306. end;
  307. procedure tcgtypeconvnode.second_cord_to_pointer;
  308. begin
  309. { this can't happen because constants are already processed in
  310. pass 1 }
  311. internalerror(47423985);
  312. end;
  313. procedure tcgtypeconvnode.second_proc_to_procvar;
  314. begin
  315. if tabstractprocdef(resultdef).is_addressonly then
  316. begin
  317. location_reset(location,LOC_REGISTER,OS_ADDR);
  318. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  319. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,location.register);
  320. end
  321. else
  322. location_copy(location,left.location);
  323. end;
  324. procedure tcgtypeconvnode.second_bool_to_int;
  325. var
  326. oldTrueLabel,oldFalseLabel : tasmlabel;
  327. begin
  328. oldTrueLabel:=current_procinfo.CurrTrueLabel;
  329. oldFalseLabel:=current_procinfo.CurrFalseLabel;
  330. current_asmdata.getjumplabel(current_procinfo.CurrTrueLabel);
  331. current_asmdata.getjumplabel(current_procinfo.CurrFalseLabel);
  332. secondpass(left);
  333. location_copy(location,left.location);
  334. { byte(boolean) or word(wordbool) or longint(longbool) must }
  335. { be accepted for var parameters }
  336. if not((nf_explicit in flags) and
  337. (left.resultdef.size=resultdef.size) and
  338. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER])) then
  339. location_force_reg(current_asmdata.CurrAsmList,location,def_cgsize(resultdef),false);
  340. current_procinfo.CurrTrueLabel:=oldTrueLabel;
  341. current_procinfo.CurrFalseLabel:=oldFalseLabel;
  342. end;
  343. procedure tcgtypeconvnode.second_bool_to_bool;
  344. begin
  345. { we can reuse the conversion already available
  346. in bool_to_int to resize the value. But when the
  347. size of the new boolean is smaller we need to calculate
  348. the value as is done in int_to_bool. This is needed because
  349. the bits that define the true status can be outside the limits
  350. of the new size and truncating the register can result in a 0
  351. value }
  352. if (left.expectloc in [LOC_FLAGS,LOC_JUMP]) then
  353. begin
  354. secondpass(left);
  355. if (left.location.loc <> left.expectloc) then
  356. internalerror(20060409);
  357. location_copy(location,left.location);
  358. end
  359. else
  360. if resultdef.size<left.resultdef.size then
  361. second_int_to_bool
  362. else
  363. second_bool_to_int;
  364. end;
  365. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  366. var
  367. l1 : tasmlabel;
  368. hr : treference;
  369. begin
  370. location_reset(location,LOC_REGISTER,OS_ADDR);
  371. current_asmdata.getjumplabel(l1);
  372. case left.location.loc of
  373. LOC_CREGISTER,LOC_REGISTER:
  374. begin
  375. {$ifdef cpu_uses_separate_address_registers}
  376. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  377. begin
  378. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  379. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,
  380. left.location.register,location.register);
  381. end
  382. else
  383. {$endif}
  384. location.register := left.location.register;
  385. end;
  386. LOC_CREFERENCE,LOC_REFERENCE:
  387. begin
  388. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  389. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  390. end;
  391. else
  392. internalerror(2002032214);
  393. end;
  394. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_NE,0,location.register,l1);
  395. reference_reset(hr);
  396. hr.symbol:=current_asmdata.RefAsmSymbol('FPC_EMPTYCHAR');
  397. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,hr,location.register);
  398. cg.a_label(current_asmdata.CurrAsmList,l1);
  399. end;
  400. procedure tcgtypeconvnode.second_class_to_intf;
  401. var
  402. l1 : tasmlabel;
  403. hd : tobjectdef;
  404. begin
  405. location_reset(location,LOC_REGISTER,OS_ADDR);
  406. case left.location.loc of
  407. LOC_CREFERENCE,
  408. 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. location_freetemp(current_asmdata.CurrAsmList,left.location);
  413. end;
  414. LOC_CREGISTER:
  415. begin
  416. location.register:=cg.getaddressregister(current_asmdata.CurrAsmList);
  417. cg.a_load_reg_reg(current_asmdata.CurrAsmList,OS_ADDR,OS_ADDR,left.location.register,location.register);
  418. end;
  419. LOC_REGISTER:
  420. location.register:=left.location.register;
  421. else
  422. internalerror(121120001);
  423. end;
  424. current_asmdata.getjumplabel(l1);
  425. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_ADDR,OC_EQ,0,location.register,l1);
  426. hd:=tobjectdef(left.resultdef);
  427. while assigned(hd) do
  428. begin
  429. if hd.implementedinterfaces.searchintf(resultdef)<>-1 then
  430. begin
  431. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_ADDR,
  432. hd.implementedinterfaces.ioffsets(
  433. hd.implementedinterfaces.searchintf(resultdef)),location.register);
  434. break;
  435. end;
  436. hd:=hd.childof;
  437. end;
  438. if hd=nil then
  439. internalerror(2002081301);
  440. cg.a_label(current_asmdata.CurrAsmList,l1);
  441. end;
  442. procedure tcgtypeconvnode.second_char_to_char;
  443. begin
  444. {$warning todo: add RTL routine for widechar-char conversion }
  445. { Quick hack to at least generate 'working' code (PFV) }
  446. second_int_to_int;
  447. end;
  448. procedure tcgtypeconvnode.second_nothing;
  449. var
  450. newsize : tcgsize;
  451. begin
  452. { we reuse the old value }
  453. location_copy(location,left.location);
  454. { Floats should never be returned as LOC_CONSTANT, do the
  455. moving to memory before the new size is set.
  456. Also when converting from a float to a non-float
  457. or the other way round, move to memory first to prevent
  458. invalid LOC_FPUREGISTER locations }
  459. if (
  460. (resultdef.deftype=floatdef) and
  461. (location.loc=LOC_CONSTANT)
  462. ) or
  463. (
  464. (left.resultdef.deftype=floatdef) xor
  465. (resultdef.deftype=floatdef)
  466. ) then
  467. location_force_mem(current_asmdata.CurrAsmList,location);
  468. { but use the new size, but we don't know the size of all arrays }
  469. newsize:=def_cgsize(resultdef);
  470. location.size:=newsize;
  471. end;
  472. {$ifdef TESTOBJEXT2}
  473. procedure tcgtypeconvnode.checkobject;
  474. begin
  475. { no checking by default }
  476. end;
  477. {$endif TESTOBJEXT2}
  478. procedure tcgtypeconvnode.pass_generate_code;
  479. begin
  480. { the boolean routines can be called with LOC_JUMP and
  481. call secondpass themselves in the helper }
  482. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  483. begin
  484. secondpass(left);
  485. if codegenerror then
  486. exit;
  487. end;
  488. second_call_helper(convtype);
  489. {$ifdef TESTOBJEXT2}
  490. { Check explicit conversions to objects pointers !! }
  491. if p^.explizit and
  492. (p^.resultdef.deftype=pointerdef) and
  493. (tpointerdef(p^.resultdef).definition.deftype=objectdef) and not
  494. (tobjectdef(tpointerdef(p^.resultdef).definition).isclass) and
  495. ((tobjectdef(tpointerdef(p^.resultdef).definition).options and oo_hasvmt)<>0) and
  496. (cs_check_range in current_settings.localswitches) then
  497. checkobject;
  498. {$endif TESTOBJEXT2}
  499. end;
  500. procedure tcgasnode.pass_generate_code;
  501. begin
  502. secondpass(call);
  503. location_copy(location,call.location);
  504. end;
  505. begin
  506. ctypeconvnode := tcgtypeconvnode;
  507. casnode := tcgasnode;
  508. end.