ncgcnv.pas 20 KB

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