ncgcnv.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Generate assembler for nodes that handle type conversions which are
  5. the same for all (most) processors
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit ncgcnv;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. node,ncnv,defutil,defcmp;
  24. type
  25. tcgtypeconvnode = class(ttypeconvnode)
  26. procedure second_int_to_int;override;
  27. procedure second_cstring_to_pchar;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_2;override;
  42. end;
  43. tcgasnode = class(tasnode)
  44. procedure pass_2;override;
  45. end;
  46. implementation
  47. uses
  48. cutils,verbose,globtype,globals,
  49. aasmbase,aasmtai,aasmcpu,symconst,symdef,paramgr,
  50. ncon,ncal,
  51. cpubase,cpuinfo,systems,
  52. pass_2,
  53. procinfo,cgbase,
  54. cgutils,cgobj,
  55. ncgutil,
  56. tgobj
  57. ;
  58. procedure tcgtypeconvnode.second_int_to_int;
  59. var
  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. end
  89. else
  90. begin
  91. { no special loading is required, reuse current location }
  92. location_copy(location,left.location);
  93. location.size:=newsize;
  94. end;
  95. end;
  96. procedure tcgtypeconvnode.second_cstring_to_pchar;
  97. var
  98. hr : treference;
  99. begin
  100. location_release(exprasmlist,left.location);
  101. location_reset(location,LOC_REGISTER,OS_ADDR);
  102. case tstringdef(left.resulttype.def).string_typ of
  103. st_shortstring :
  104. begin
  105. inc(left.location.reference.offset);
  106. location.register:=cg.getaddressregister(exprasmlist);
  107. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  108. end;
  109. {$ifdef ansistring_bits}
  110. st_ansistring16,st_ansistring32,st_ansistring64 :
  111. {$else}
  112. st_ansistring :
  113. {$endif}
  114. begin
  115. if (left.nodetype=stringconstn) and
  116. (str_length(left)=0) then
  117. begin
  118. reference_reset(hr);
  119. hr.symbol:=objectlibrary.newasmsymbol('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  120. location.register:=cg.getaddressregister(exprasmlist);
  121. cg.a_loadaddr_ref_reg(exprasmlist,hr,location.register);
  122. end
  123. else
  124. begin
  125. location.register:=cg.getaddressregister(exprasmlist);
  126. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  127. end;
  128. end;
  129. st_longstring:
  130. begin
  131. {!!!!!!!}
  132. internalerror(8888);
  133. end;
  134. st_widestring:
  135. begin
  136. if (left.nodetype=stringconstn) and
  137. (str_length(left)=0) then
  138. begin
  139. reference_reset(hr);
  140. hr.symbol:=objectlibrary.newasmsymbol('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  141. location.register:=cg.getaddressregister(exprasmlist);
  142. cg.a_loadaddr_ref_reg(exprasmlist,hr,location.register);
  143. end
  144. else
  145. begin
  146. location.register:=cg.getintregister(exprasmlist,OS_INT);
  147. {$ifdef fpc}
  148. {$warning Todo: convert widestrings to ascii when typecasting them to pchars}
  149. {$endif}
  150. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_INT,left.location.reference,
  151. location.register);
  152. end;
  153. end;
  154. end;
  155. end;
  156. procedure tcgtypeconvnode.second_string_to_chararray;
  157. var
  158. arrsize: longint;
  159. begin
  160. with tarraydef(resulttype.def) do
  161. arrsize := highrange-lowrange+1;
  162. if (left.nodetype = stringconstn) and
  163. { left.length+1 since there's always a terminating #0 character (JM) }
  164. (tstringconstnode(left).len+1 >= arrsize) and
  165. (tstringdef(left.resulttype.def).string_typ=st_shortstring) then
  166. begin
  167. location_copy(location,left.location);
  168. inc(location.reference.offset);
  169. exit;
  170. end
  171. else
  172. { should be handled already in resulttype pass (JM) }
  173. internalerror(200108292);
  174. end;
  175. procedure tcgtypeconvnode.second_array_to_pointer;
  176. begin
  177. location_release(exprasmlist,left.location);
  178. location_reset(location,LOC_REGISTER,OS_ADDR);
  179. location.register:=cg.getaddressregister(exprasmlist);
  180. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  181. end;
  182. procedure tcgtypeconvnode.second_pointer_to_array;
  183. begin
  184. location_reset(location,LOC_REFERENCE,OS_NO);
  185. case left.location.loc of
  186. LOC_REGISTER :
  187. begin
  188. {$ifdef cpu_uses_separate_address_registers}
  189. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  190. begin
  191. location_release(exprasmlist,left.location);
  192. location.reference.base:=rg.getaddressregister(exprasmlist);
  193. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  194. left.location.register,location.reference.base);
  195. end
  196. else
  197. {$endif}
  198. location.reference.base := left.location.register;
  199. end;
  200. LOC_CREGISTER :
  201. begin
  202. location.reference.base:=cg.getaddressregister(exprasmlist);
  203. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,
  204. location.reference.base);
  205. end;
  206. LOC_REFERENCE,
  207. LOC_CREFERENCE :
  208. begin
  209. location_release(exprasmlist,left.location);
  210. location.reference.base:=cg.getaddressregister(exprasmlist);
  211. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,
  212. location.reference.base);
  213. location_freetemp(exprasmlist,left.location);
  214. end;
  215. else
  216. internalerror(2002032216);
  217. end;
  218. end;
  219. procedure tcgtypeconvnode.second_char_to_string;
  220. begin
  221. location_reset(location,LOC_REFERENCE,OS_NO);
  222. case tstringdef(resulttype.def).string_typ of
  223. st_shortstring :
  224. begin
  225. location_release(exprasmlist,left.location);
  226. tg.GetTemp(exprasmlist,256,tt_normal,location.reference);
  227. cg.a_load_loc_ref(exprasmlist,left.location.size,left.location,
  228. location.reference);
  229. location_freetemp(exprasmlist,left.location);
  230. end;
  231. { the rest is removed in the resulttype pass and converted to compilerprocs }
  232. else
  233. internalerror(4179);
  234. end;
  235. end;
  236. procedure tcgtypeconvnode.second_real_to_real;
  237. begin
  238. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  239. case left.location.loc of
  240. LOC_FPUREGISTER,
  241. LOC_CFPUREGISTER:
  242. begin
  243. location_copy(location,left.location);
  244. location.size:=def_cgsize(resulttype.def);
  245. case expectloc of
  246. LOC_FPUREGISTER:
  247. ;
  248. LOC_MMREGISTER:
  249. location_force_mmregscalar(exprasmlist,location,false);
  250. else
  251. internalerror(2003012262);
  252. end;
  253. exit
  254. end;
  255. LOC_CREFERENCE,
  256. LOC_REFERENCE:
  257. begin
  258. location_release(exprasmlist,left.location);
  259. location.register:=cg.getfpuregister(exprasmlist,left.location.size);
  260. cg.a_loadfpu_loc_reg(exprasmlist,left.location,location.register);
  261. location_freetemp(exprasmlist,left.location);
  262. end;
  263. LOC_MMREGISTER,
  264. LOC_CMMREGISTER:
  265. begin
  266. location_copy(location,left.location);
  267. case expectloc of
  268. LOC_FPUREGISTER:
  269. begin
  270. location_force_fpureg(exprasmlist,location,false);
  271. location.size:=def_cgsize(resulttype.def);
  272. end;
  273. LOC_MMREGISTER:
  274. ;
  275. else
  276. internalerror(2003012261);
  277. end;
  278. end;
  279. else
  280. internalerror(2002032215);
  281. end;
  282. end;
  283. procedure tcgtypeconvnode.second_cord_to_pointer;
  284. begin
  285. { this can't happen because constants are already processed in
  286. pass 1 }
  287. internalerror(47423985);
  288. end;
  289. procedure tcgtypeconvnode.second_proc_to_procvar;
  290. begin
  291. { method pointer ? }
  292. if tabstractprocdef(left.resulttype.def).is_methodpointer and
  293. not(tabstractprocdef(left.resulttype.def).is_addressonly) then
  294. begin
  295. location_copy(location,left.location);
  296. end
  297. else
  298. begin
  299. location_release(exprasmlist,left.location);
  300. location_reset(location,LOC_REGISTER,OS_ADDR);
  301. location.register:=cg.getaddressregister(exprasmlist);
  302. cg.a_loadaddr_ref_reg(exprasmlist,left.location.reference,location.register);
  303. end;
  304. end;
  305. procedure tcgtypeconvnode.second_bool_to_int;
  306. var
  307. oldtruelabel,oldfalselabel : tasmlabel;
  308. begin
  309. oldtruelabel:=truelabel;
  310. oldfalselabel:=falselabel;
  311. objectlibrary.getlabel(truelabel);
  312. objectlibrary.getlabel(falselabel);
  313. secondpass(left);
  314. location_copy(location,left.location);
  315. { byte(boolean) or word(wordbool) or longint(longbool) must }
  316. { be accepted for var parameters }
  317. if not((nf_explicit in flags) and
  318. (left.resulttype.def.size=resulttype.def.size) and
  319. (left.location.loc in [LOC_REFERENCE,LOC_CREFERENCE,LOC_CREGISTER])) then
  320. location_force_reg(exprasmlist,location,def_cgsize(resulttype.def),false);
  321. truelabel:=oldtruelabel;
  322. falselabel:=oldfalselabel;
  323. end;
  324. procedure tcgtypeconvnode.second_bool_to_bool;
  325. begin
  326. { we can reuse the conversion already available
  327. in bool_to_int to resize the value. But when the
  328. size of the new boolean is smaller we need to calculate
  329. the value as is done in int_to_bool. This is needed because
  330. the bits that define the true status can be outside the limits
  331. of the new size and truncating the register can result in a 0
  332. value }
  333. if resulttype.def.size<left.resulttype.def.size then
  334. second_int_to_bool
  335. else
  336. second_bool_to_int;
  337. end;
  338. procedure tcgtypeconvnode.second_ansistring_to_pchar;
  339. var
  340. l1 : tasmlabel;
  341. hr : treference;
  342. begin
  343. location_reset(location,LOC_REGISTER,OS_ADDR);
  344. objectlibrary.getlabel(l1);
  345. case left.location.loc of
  346. LOC_CREGISTER,LOC_REGISTER:
  347. begin
  348. {$ifdef cpu_uses_separate_address_registers}
  349. if getregtype(left.location.register)<>R_ADDRESSREGISTER then
  350. begin
  351. location_release(exprasmlist,left.location);
  352. location.register:=cg.getaddressregister(exprasmlist);
  353. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,
  354. left.location.register,location.register);
  355. end
  356. else
  357. {$endif}
  358. location.register := left.location.register;
  359. end;
  360. LOC_CREFERENCE,LOC_REFERENCE:
  361. begin
  362. location_release(exprasmlist,left.location);
  363. location.register:=cg.getaddressregister(exprasmlist);
  364. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  365. location_freetemp(exprasmlist,left.location);
  366. end;
  367. else
  368. internalerror(2002032214);
  369. end;
  370. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_NE,0,location.register,l1);
  371. reference_reset(hr);
  372. hr.symbol:=objectlibrary.newasmsymbol('FPC_EMPTYCHAR',AB_EXTERNAL,AT_DATA);
  373. cg.a_loadaddr_ref_reg(exprasmlist,hr,location.register);
  374. cg.a_label(exprasmlist,l1);
  375. end;
  376. procedure tcgtypeconvnode.second_class_to_intf;
  377. var
  378. l1 : tasmlabel;
  379. hd : tobjectdef;
  380. begin
  381. location_reset(location,LOC_REGISTER,OS_ADDR);
  382. case left.location.loc of
  383. LOC_CREFERENCE,
  384. LOC_REFERENCE:
  385. begin
  386. location_release(exprasmlist,left.location);
  387. location.register:=cg.getaddressregister(exprasmlist);
  388. cg.a_load_ref_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.reference,location.register);
  389. location_freetemp(exprasmlist,left.location);
  390. end;
  391. LOC_CREGISTER:
  392. begin
  393. location.register:=cg.getaddressregister(exprasmlist);
  394. cg.a_load_reg_reg(exprasmlist,OS_ADDR,OS_ADDR,left.location.register,location.register);
  395. end;
  396. LOC_REGISTER:
  397. location.register:=left.location.register;
  398. else
  399. internalerror(121120001);
  400. end;
  401. objectlibrary.getlabel(l1);
  402. cg.a_cmp_const_reg_label(exprasmlist,OS_ADDR,OC_EQ,0,location.register,l1);
  403. hd:=tobjectdef(left.resulttype.def);
  404. while assigned(hd) do
  405. begin
  406. if hd.implementedinterfaces.searchintf(resulttype.def)<>-1 then
  407. begin
  408. cg.a_op_const_reg(exprasmlist,OP_ADD,OS_ADDR,
  409. hd.implementedinterfaces.ioffsets(
  410. hd.implementedinterfaces.searchintf(resulttype.def))^,location.register);
  411. break;
  412. end;
  413. hd:=hd.childof;
  414. end;
  415. if hd=nil then
  416. internalerror(2002081301);
  417. cg.a_label(exprasmlist,l1);
  418. end;
  419. procedure tcgtypeconvnode.second_char_to_char;
  420. begin
  421. {$ifdef fpc}
  422. {$warning todo: add RTL routine for widechar-char conversion }
  423. {$endif}
  424. { Quick hack to at least generate 'working' code (PFV) }
  425. second_int_to_int;
  426. end;
  427. procedure tcgtypeconvnode.second_nothing;
  428. begin
  429. { we reuse the old value }
  430. location_copy(location,left.location);
  431. { Floats should never be returned as LOC_CONSTANT, do the
  432. moving to memory before the new size is set }
  433. if (resulttype.def.deftype=floatdef) and
  434. (location.loc=LOC_CONSTANT) then
  435. location_force_mem(exprasmlist,location);
  436. { but use the new size, but we don't know the size of all arrays }
  437. location.size:=def_cgsize(resulttype.def);
  438. end;
  439. {$ifdef TESTOBJEXT2}
  440. procedure tcgtypeconvnode.checkobject;
  441. begin
  442. { no checking by default }
  443. end;
  444. {$endif TESTOBJEXT2}
  445. procedure tcgtypeconvnode.pass_2;
  446. begin
  447. { the boolean routines can be called with LOC_JUMP and
  448. call secondpass themselves in the helper }
  449. if not(convtype in [tc_bool_2_int,tc_bool_2_bool,tc_int_2_bool]) then
  450. begin
  451. secondpass(left);
  452. if codegenerror then
  453. exit;
  454. end;
  455. second_call_helper(convtype);
  456. {$ifdef TESTOBJEXT2}
  457. { Check explicit conversions to objects pointers !! }
  458. if p^.explizit and
  459. (p^.resulttype.def.deftype=pointerdef) and
  460. (tpointerdef(p^.resulttype.def).definition.deftype=objectdef) and not
  461. (tobjectdef(tpointerdef(p^.resulttype.def).definition).isclass) and
  462. ((tobjectdef(tpointerdef(p^.resulttype.def).definition).options and oo_hasvmt)<>0) and
  463. (cs_check_range in aktlocalswitches) then
  464. checkobject;
  465. {$endif TESTOBJEXT2}
  466. end;
  467. procedure tcgasnode.pass_2;
  468. begin
  469. secondpass(call);
  470. location_copy(location,call.location);
  471. end;
  472. begin
  473. ctypeconvnode := tcgtypeconvnode;
  474. casnode := tcgasnode;
  475. end.
  476. {
  477. $Log$
  478. Revision 1.59 2004-06-20 08:55:29 florian
  479. * logs truncated
  480. Revision 1.58 2004/06/16 20:07:08 florian
  481. * dwarf branch merged
  482. Revision 1.57 2004/04/29 19:56:37 daniel
  483. * Prepare compiler infrastructure for multiple ansistring types
  484. Revision 1.56.2.1 2004/04/27 18:18:25 peter
  485. * aword -> aint
  486. Revision 1.56 2004/03/02 00:36:33 olle
  487. * big transformation of Tai_[const_]Symbol.Create[data]name*
  488. Revision 1.55 2004/02/27 10:21:05 florian
  489. * top_symbol killed
  490. + refaddr to treference added
  491. + refsymbol to treference added
  492. * top_local stuff moved to an extra record to save memory
  493. + aint introduced
  494. * tppufile.get/putint64/aint implemented
  495. }