ncgcnv.pas 20 KB

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