ncnv.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Florian Klaempfl
  4. Type checking and register allocation for type converting nodes
  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 ncnv;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node,
  23. symtype,types,
  24. nld;
  25. type
  26. ttypeconvnode = class(tunarynode)
  27. convtype : tconverttype;
  28. constructor create(node : tnode;t : pdef);virtual;
  29. function getcopy : tnode;override;
  30. function pass_1 : tnode;override;
  31. function first_int_to_int : tnode;virtual;
  32. function first_cstring_to_pchar : tnode;virtual;
  33. function first_string_to_chararray : tnode;virtual;
  34. function first_string_to_string : tnode;virtual;
  35. function first_char_to_string : tnode;virtual;
  36. function first_nothing : tnode;virtual;
  37. function first_array_to_pointer : tnode;virtual;
  38. function first_int_to_real : tnode;virtual;
  39. function first_int_to_fix : tnode;virtual;
  40. function first_real_to_fix : tnode;virtual;
  41. function first_fix_to_real : tnode;virtual;
  42. function first_real_to_real : tnode;virtual;
  43. function first_pointer_to_array : tnode;virtual;
  44. function first_chararray_to_string : tnode;virtual;
  45. function first_cchar_to_pchar : tnode;virtual;
  46. function first_bool_to_int : tnode;virtual;
  47. function first_int_to_bool : tnode;virtual;
  48. function first_bool_to_bool : tnode;virtual;
  49. function first_proc_to_procvar : tnode;virtual;
  50. function first_load_smallset : tnode;virtual;
  51. function first_cord_to_pointer : tnode;virtual;
  52. function first_pchar_to_string : tnode;virtual;
  53. function first_ansistring_to_pchar : tnode;virtual;
  54. function first_arrayconstructor_to_set : tnode;virtual;
  55. function first_class_to_intf : tnode;virtual;
  56. function first_call_helper(c : tconverttype) : tnode;
  57. function docompare(p: tnode) : boolean; override;
  58. end;
  59. tasnode = class(tbinarynode)
  60. constructor create(l,r : tnode);virtual;
  61. function pass_1 : tnode;override;
  62. end;
  63. tisnode = class(tbinarynode)
  64. constructor create(l,r : tnode);virtual;
  65. function pass_1 : tnode;override;
  66. end;
  67. var
  68. ctypeconvnode : class of ttypeconvnode;
  69. casnode : class of tasnode;
  70. cisnode : class of tisnode;
  71. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  72. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  73. implementation
  74. uses
  75. globtype,systems,tokens,
  76. cutils,verbose,globals,
  77. symconst,symdef,symsym,symtable,
  78. ncon,ncal,nset,nadd,
  79. {$ifdef newcg}
  80. cgbase,
  81. {$else newcg}
  82. hcodegen,
  83. {$endif newcg}
  84. htypechk,pass_1,cpubase,cpuinfo;
  85. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  86. begin
  87. gentypeconvnode:=ctypeconvnode.create(node,t);
  88. end;
  89. {*****************************************************************************
  90. Array constructor to Set Conversion
  91. *****************************************************************************}
  92. procedure arrayconstructor_to_set(var p : tarrayconstructornode);
  93. var
  94. constp : tsetconstnode;
  95. buildp,
  96. p2,p3,p4 : tnode;
  97. pd : pdef;
  98. constset : pconstset;
  99. constsetlo,
  100. constsethi : longint;
  101. procedure update_constsethi(p:pdef);
  102. begin
  103. if ((p^.deftype=orddef) and
  104. (porddef(p)^.high>=constsethi)) then
  105. begin
  106. constsethi:=porddef(p)^.high;
  107. if pd=nil then
  108. begin
  109. if (constsethi>255) or
  110. (porddef(p)^.low<0) then
  111. pd:=u8bitdef
  112. else
  113. pd:=p;
  114. end;
  115. if constsethi>255 then
  116. constsethi:=255;
  117. end
  118. else if ((p^.deftype=enumdef) and
  119. (penumdef(p)^.max>=constsethi)) then
  120. begin
  121. if pd=nil then
  122. pd:=p;
  123. constsethi:=penumdef(p)^.max;
  124. end;
  125. end;
  126. procedure do_set(pos : longint);
  127. var
  128. mask,l : longint;
  129. begin
  130. if (pos>255) or (pos<0) then
  131. Message(parser_e_illegal_set_expr);
  132. if pos>constsethi then
  133. constsethi:=pos;
  134. if pos<constsetlo then
  135. constsetlo:=pos;
  136. l:=pos shr 3;
  137. mask:=1 shl (pos mod 8);
  138. { do we allow the same twice }
  139. if (constset^[l] and mask)<>0 then
  140. Message(parser_e_illegal_set_expr);
  141. constset^[l]:=constset^[l] or mask;
  142. end;
  143. var
  144. l : longint;
  145. lr,hr : longint;
  146. begin
  147. new(constset);
  148. FillChar(constset^,sizeof(constset^),0);
  149. pd:=nil;
  150. constsetlo:=0;
  151. constsethi:=0;
  152. constp:=gensetconstnode(nil,nil);
  153. constp.value_set:=constset;
  154. buildp:=constp;
  155. if assigned(p.left) then
  156. begin
  157. while assigned(p) do
  158. begin
  159. p4:=nil; { will contain the tree to create the set }
  160. {split a range into p2 and p3 }
  161. if p.left.nodetype=arrayconstructorrangen then
  162. begin
  163. p2:=tarrayconstructorrangenode(p.left).left;
  164. p3:=tarrayconstructorrangenode(p.left).right;
  165. tarrayconstructorrangenode(p.left).left:=nil;
  166. tarrayconstructorrangenode(p.left).right:=nil;
  167. end
  168. else
  169. begin
  170. p2:=p.left;
  171. p.left:=nil;
  172. p3:=nil;
  173. end;
  174. firstpass(p2);
  175. if assigned(p3) then
  176. firstpass(p3);
  177. if codegenerror then
  178. break;
  179. case p2.resulttype^.deftype of
  180. enumdef,
  181. orddef:
  182. begin
  183. getrange(p2.resulttype,lr,hr);
  184. if assigned(p3) then
  185. begin
  186. { this isn't good, you'll get problems with
  187. type t010 = 0..10;
  188. ts = set of t010;
  189. var s : ts;b : t010
  190. begin s:=[1,2,b]; end.
  191. if is_integer(p3^.resulttype) then
  192. begin
  193. p3:=gentypeconvnode(p3,u8bitdef);
  194. firstpass(p3);
  195. end;
  196. }
  197. if assigned(pd) and not(is_equal(pd,p3.resulttype)) then
  198. begin
  199. aktfilepos:=p3.fileinfo;
  200. CGMessage(type_e_typeconflict_in_set);
  201. end
  202. else
  203. begin
  204. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  205. begin
  206. if not(is_integer(p3.resulttype)) then
  207. pd:=p3.resulttype
  208. else
  209. begin
  210. p3:=gentypeconvnode(p3,u8bitdef);
  211. p2:=gentypeconvnode(p2,u8bitdef);
  212. firstpass(p2);
  213. firstpass(p3);
  214. end;
  215. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  216. do_set(l);
  217. p2.free;
  218. p3.free;
  219. end
  220. else
  221. begin
  222. update_constsethi(p2.resulttype);
  223. p2:=gentypeconvnode(p2,pd);
  224. firstpass(p2);
  225. update_constsethi(p3.resulttype);
  226. p3:=gentypeconvnode(p3,pd);
  227. firstpass(p3);
  228. if assigned(pd) then
  229. p3:=gentypeconvnode(p3,pd)
  230. else
  231. p3:=gentypeconvnode(p3,u8bitdef);
  232. firstpass(p3);
  233. p4:=csetelementnode.create(p2,p3);
  234. end;
  235. end;
  236. end
  237. else
  238. begin
  239. { Single value }
  240. if p2.nodetype=ordconstn then
  241. begin
  242. if not(is_integer(p2.resulttype)) then
  243. update_constsethi(p2.resulttype)
  244. else
  245. begin
  246. p2:=gentypeconvnode(p2,u8bitdef);
  247. firstpass(p2);
  248. end;
  249. do_set(tordconstnode(p2).value);
  250. p2.free;
  251. end
  252. else
  253. begin
  254. update_constsethi(p2.resulttype);
  255. if assigned(pd) then
  256. p2:=gentypeconvnode(p2,pd)
  257. else
  258. p2:=gentypeconvnode(p2,u8bitdef);
  259. firstpass(p2);
  260. p4:=csetelementnode.create(p2,nil);
  261. end;
  262. end;
  263. end;
  264. stringdef : begin
  265. { if we've already set elements which are constants }
  266. { throw an error }
  267. if ((pd=nil) and assigned(buildp)) or
  268. not(is_equal(pd,cchardef)) then
  269. CGMessage(type_e_typeconflict_in_set)
  270. else
  271. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  272. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  273. if pd=nil then
  274. pd:=cchardef;
  275. p2.free;
  276. end;
  277. else
  278. CGMessage(type_e_ordinal_expr_expected);
  279. end;
  280. { insert the set creation tree }
  281. if assigned(p4) then
  282. buildp:=caddnode.create(addn,buildp,p4);
  283. { load next and dispose current node }
  284. p2:=p;
  285. p:=tarrayconstructornode(tarrayconstructornode(p2).right);
  286. tarrayconstructornode(p2).right:=nil;
  287. p2.free;
  288. end;
  289. if (pd=nil) then
  290. begin
  291. pd:=u8bitdef;
  292. constsethi:=255;
  293. end;
  294. end
  295. else
  296. begin
  297. { empty set [], only remove node }
  298. p.free;
  299. end;
  300. { set the initial set type }
  301. constp.resulttype:=new(psetdef,init(pd,constsethi));
  302. firstpass(buildp);
  303. { set the new tree }
  304. p:=tarrayconstructornode(buildp);
  305. end;
  306. {*****************************************************************************
  307. TTYPECONVNODE
  308. *****************************************************************************}
  309. constructor ttypeconvnode.create(node : tnode;t : pdef);
  310. begin
  311. inherited create(typeconvn,node);
  312. convtype:=tc_not_possible;
  313. resulttype:=t;
  314. set_file_line(node);
  315. end;
  316. function ttypeconvnode.getcopy : tnode;
  317. var
  318. n : ttypeconvnode;
  319. begin
  320. n:=ttypeconvnode(inherited getcopy);
  321. n.convtype:=convtype;
  322. getcopy:=n;
  323. end;
  324. function ttypeconvnode.first_int_to_int : tnode;
  325. begin
  326. first_int_to_int:=nil;
  327. if (left.location.loc<>LOC_REGISTER) and
  328. (resulttype^.size>left.resulttype^.size) then
  329. location.loc:=LOC_REGISTER;
  330. if is_64bitint(resulttype) then
  331. registers32:=max(registers32,2)
  332. else
  333. registers32:=max(registers32,1);
  334. end;
  335. function ttypeconvnode.first_cstring_to_pchar : tnode;
  336. begin
  337. first_cstring_to_pchar:=nil;
  338. registers32:=1;
  339. location.loc:=LOC_REGISTER;
  340. end;
  341. function ttypeconvnode.first_string_to_chararray : tnode;
  342. begin
  343. first_string_to_chararray:=nil;
  344. registers32:=1;
  345. location.loc:=LOC_REGISTER;
  346. end;
  347. function ttypeconvnode.first_string_to_string : tnode;
  348. begin
  349. first_string_to_string:=nil;
  350. if pstringdef(resulttype)^.string_typ<>
  351. pstringdef(left.resulttype)^.string_typ then
  352. begin
  353. if left.nodetype=stringconstn then
  354. begin
  355. tstringconstnode(left).stringtype:=pstringdef(resulttype)^.string_typ;
  356. tstringconstnode(left).resulttype:=resulttype;
  357. { remove typeconv node }
  358. first_string_to_string:=left;
  359. left:=nil;
  360. exit;
  361. end
  362. else
  363. procinfo^.flags:=procinfo^.flags or pi_do_call;
  364. end;
  365. { for simplicity lets first keep all ansistrings
  366. as LOC_MEM, could also become LOC_REGISTER }
  367. if pstringdef(resulttype)^.string_typ in [st_ansistring,st_widestring] then
  368. { we may use ansistrings so no fast exit here }
  369. procinfo^.no_fast_exit:=true;
  370. location.loc:=LOC_MEM;
  371. end;
  372. function ttypeconvnode.first_char_to_string : tnode;
  373. var
  374. hp : tstringconstnode;
  375. begin
  376. first_char_to_string:=nil;
  377. if left.nodetype=ordconstn then
  378. begin
  379. hp:=genstringconstnode(chr(tordconstnode(left).value),st_default);
  380. hp.stringtype:=pstringdef(resulttype)^.string_typ;
  381. firstpass(hp);
  382. first_char_to_string:=hp;
  383. end
  384. else
  385. location.loc:=LOC_MEM;
  386. end;
  387. function ttypeconvnode.first_nothing : tnode;
  388. begin
  389. first_nothing:=nil;
  390. location.loc:=LOC_MEM;
  391. end;
  392. function ttypeconvnode.first_array_to_pointer : tnode;
  393. begin
  394. first_array_to_pointer:=nil;
  395. if registers32<1 then
  396. registers32:=1;
  397. location.loc:=LOC_REGISTER;
  398. end;
  399. function ttypeconvnode.first_int_to_real : tnode;
  400. var
  401. t : trealconstnode;
  402. begin
  403. first_int_to_real:=nil;
  404. if left.nodetype=ordconstn then
  405. begin
  406. t:=genrealconstnode(tordconstnode(left).value,pfloatdef(resulttype));
  407. firstpass(t);
  408. first_int_to_real:=t;
  409. exit;
  410. end;
  411. if registersfpu<1 then
  412. registersfpu:=1;
  413. location.loc:=LOC_FPU;
  414. end;
  415. function ttypeconvnode.first_int_to_fix : tnode;
  416. var
  417. t : tnode;
  418. begin
  419. first_int_to_fix:=nil;
  420. if left.nodetype=ordconstn then
  421. begin
  422. t:=genfixconstnode(tordconstnode(left).value shl 16,resulttype);
  423. firstpass(t);
  424. first_int_to_fix:=t;
  425. exit;
  426. end;
  427. if registers32<1 then
  428. registers32:=1;
  429. location.loc:=LOC_REGISTER;
  430. end;
  431. function ttypeconvnode.first_real_to_fix : tnode;
  432. var
  433. t : tnode;
  434. begin
  435. first_real_to_fix:=nil;
  436. if left.nodetype=realconstn then
  437. begin
  438. t:=genfixconstnode(round(trealconstnode(left).value_real*65536),resulttype);
  439. firstpass(t);
  440. first_real_to_fix:=t;
  441. exit;
  442. end;
  443. { at least one fpu and int register needed }
  444. if registers32<1 then
  445. registers32:=1;
  446. if registersfpu<1 then
  447. registersfpu:=1;
  448. location.loc:=LOC_REGISTER;
  449. end;
  450. function ttypeconvnode.first_fix_to_real : tnode;
  451. var
  452. t : tnode;
  453. begin
  454. first_fix_to_real:=nil;
  455. if left.nodetype=fixconstn then
  456. begin
  457. t:=genrealconstnode(round(tfixconstnode(left).value_fix/65536.0),resulttype);
  458. firstpass(t);
  459. first_fix_to_real:=t;
  460. exit;
  461. end;
  462. if registersfpu<1 then
  463. registersfpu:=1;
  464. location.loc:=LOC_FPU;
  465. end;
  466. function ttypeconvnode.first_real_to_real : tnode;
  467. var
  468. t : tnode;
  469. begin
  470. first_real_to_real:=nil;
  471. if left.nodetype=realconstn then
  472. begin
  473. t:=genrealconstnode(trealconstnode(left).value_real,resulttype);
  474. firstpass(t);
  475. first_real_to_real:=t;
  476. exit;
  477. end;
  478. { comp isn't a floating type }
  479. {$ifdef i386}
  480. if (pfloatdef(resulttype)^.typ=s64comp) and
  481. (pfloatdef(left.resulttype)^.typ<>s64comp) and
  482. not (nf_explizit in flags) then
  483. CGMessage(type_w_convert_real_2_comp);
  484. {$endif}
  485. if registersfpu<1 then
  486. registersfpu:=1;
  487. location.loc:=LOC_FPU;
  488. end;
  489. function ttypeconvnode.first_pointer_to_array : tnode;
  490. begin
  491. first_pointer_to_array:=nil;
  492. if registers32<1 then
  493. registers32:=1;
  494. location.loc:=LOC_REFERENCE;
  495. end;
  496. function ttypeconvnode.first_chararray_to_string : tnode;
  497. begin
  498. first_chararray_to_string:=nil;
  499. { the only important information is the location of the }
  500. { result }
  501. { other stuff is done by firsttypeconv }
  502. location.loc:=LOC_MEM;
  503. end;
  504. function ttypeconvnode.first_cchar_to_pchar : tnode;
  505. begin
  506. first_cchar_to_pchar:=nil;
  507. left:=gentypeconvnode(left,cshortstringdef);
  508. { convert constant char to constant string }
  509. firstpass(left);
  510. { evalute tree }
  511. first_cchar_to_pchar:=pass_1;
  512. end;
  513. function ttypeconvnode.first_bool_to_int : tnode;
  514. begin
  515. first_bool_to_int:=nil;
  516. { byte(boolean) or word(wordbool) or longint(longbool) must
  517. be accepted for var parameters }
  518. if (nf_explizit in flags) and
  519. (left.resulttype^.size=resulttype^.size) and
  520. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  521. exit;
  522. location.loc:=LOC_REGISTER;
  523. if registers32<1 then
  524. registers32:=1;
  525. end;
  526. function ttypeconvnode.first_int_to_bool : tnode;
  527. begin
  528. first_int_to_bool:=nil;
  529. { byte(boolean) or word(wordbool) or longint(longbool) must
  530. be accepted for var parameters }
  531. if (nf_explizit in flags) and
  532. (left.resulttype^.size=resulttype^.size) and
  533. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  534. exit;
  535. location.loc:=LOC_REGISTER;
  536. { need if bool to bool !!
  537. not very nice !!
  538. left:=gentypeconvnode(left,s32bitdef);
  539. left.explizit:=true;
  540. firstpass(left); }
  541. if registers32<1 then
  542. registers32:=1;
  543. end;
  544. function ttypeconvnode.first_bool_to_bool : tnode;
  545. begin
  546. first_bool_to_bool:=nil;
  547. location.loc:=LOC_REGISTER;
  548. if registers32<1 then
  549. registers32:=1;
  550. end;
  551. function ttypeconvnode.first_proc_to_procvar : tnode;
  552. begin
  553. first_proc_to_procvar:=nil;
  554. { hmmm, I'am not sure if that is necessary (FK) }
  555. firstpass(left);
  556. if codegenerror then
  557. exit;
  558. if (left.location.loc<>LOC_REFERENCE) then
  559. CGMessage(cg_e_illegal_expression);
  560. registers32:=left.registers32;
  561. if registers32<1 then
  562. registers32:=1;
  563. location.loc:=LOC_REGISTER;
  564. end;
  565. function ttypeconvnode.first_load_smallset : tnode;
  566. begin
  567. first_load_smallset:=nil;
  568. end;
  569. function ttypeconvnode.first_cord_to_pointer : tnode;
  570. var
  571. t : tnode;
  572. begin
  573. first_cord_to_pointer:=nil;
  574. if left.nodetype=ordconstn then
  575. begin
  576. { check if we have a valid pointer constant (JM) }
  577. if (sizeof(tordconstnode) > sizeof(tpointerord)) then
  578. if (sizeof(tpointerord) = 4) then
  579. begin
  580. if (tordconstnode(left).value < low(longint)) or
  581. (tordconstnode(left).value > high(cardinal)) then
  582. CGMessage(parser_e_range_check_error);
  583. end
  584. else if (sizeof(tpointerord) = 8) then
  585. begin
  586. if (tordconstnode(left).value < low(int64)) or
  587. (tordconstnode(left).value > high(qword)) then
  588. CGMessage(parser_e_range_check_error);
  589. end
  590. else
  591. internalerror(2001020801);
  592. t:=genpointerconstnode(tpointerord(tordconstnode(left).value),resulttype);
  593. firstpass(t);
  594. first_cord_to_pointer:=t;
  595. exit;
  596. end
  597. else
  598. internalerror(432472389);
  599. end;
  600. function ttypeconvnode.first_pchar_to_string : tnode;
  601. begin
  602. first_pchar_to_string:=nil;
  603. location.loc:=LOC_REFERENCE;
  604. end;
  605. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  606. begin
  607. first_ansistring_to_pchar:=nil;
  608. location.loc:=LOC_REGISTER;
  609. if registers32<1 then
  610. registers32:=1;
  611. end;
  612. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  613. var
  614. hp : tnode;
  615. begin
  616. first_arrayconstructor_to_set:=nil;
  617. if left.nodetype<>arrayconstructorn then
  618. internalerror(5546);
  619. { remove typeconv node }
  620. hp:=left;
  621. left:=nil;
  622. { create a set constructor tree }
  623. arrayconstructor_to_set(tarrayconstructornode(hp));
  624. { now firstpass the set }
  625. firstpass(hp);
  626. first_arrayconstructor_to_set:=hp;
  627. end;
  628. function ttypeconvnode.first_class_to_intf : tnode;
  629. begin
  630. first_class_to_intf:=nil;
  631. location.loc:=LOC_REFERENCE;
  632. if registers32<1 then
  633. registers32:=1;
  634. end;
  635. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  636. const
  637. firstconvert : array[tconverttype] of pointer = (
  638. @ttypeconvnode.first_nothing, {equal}
  639. @ttypeconvnode.first_nothing, {not_possible}
  640. @ttypeconvnode.first_string_to_string,
  641. @ttypeconvnode.first_char_to_string,
  642. @ttypeconvnode.first_pchar_to_string,
  643. @ttypeconvnode.first_cchar_to_pchar,
  644. @ttypeconvnode.first_cstring_to_pchar,
  645. @ttypeconvnode.first_ansistring_to_pchar,
  646. @ttypeconvnode.first_string_to_chararray,
  647. @ttypeconvnode.first_chararray_to_string,
  648. @ttypeconvnode.first_array_to_pointer,
  649. @ttypeconvnode.first_pointer_to_array,
  650. @ttypeconvnode.first_int_to_int,
  651. @ttypeconvnode.first_int_to_bool,
  652. @ttypeconvnode.first_bool_to_bool,
  653. @ttypeconvnode.first_bool_to_int,
  654. @ttypeconvnode.first_real_to_real,
  655. @ttypeconvnode.first_int_to_real,
  656. @ttypeconvnode.first_int_to_fix,
  657. @ttypeconvnode.first_real_to_fix,
  658. @ttypeconvnode.first_fix_to_real,
  659. @ttypeconvnode.first_proc_to_procvar,
  660. @ttypeconvnode.first_arrayconstructor_to_set,
  661. @ttypeconvnode.first_load_smallset,
  662. @ttypeconvnode.first_cord_to_pointer,
  663. @ttypeconvnode.first_nothing,
  664. @ttypeconvnode.first_nothing,
  665. @ttypeconvnode.first_class_to_intf
  666. );
  667. type
  668. tprocedureofobject = function : tnode of object;
  669. var
  670. r : packed record
  671. proc : pointer;
  672. obj : pointer;
  673. end;
  674. begin
  675. { this is a little bit dirty but it works }
  676. { and should be quite portable too }
  677. r.proc:=firstconvert[c];
  678. r.obj:=self;
  679. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  680. end;
  681. function ttypeconvnode.pass_1 : tnode;
  682. var
  683. hp : tnode;
  684. aprocdef : pprocdef;
  685. enable_range_check: boolean;
  686. begin
  687. pass_1:=nil;
  688. aprocdef:=nil;
  689. { if explicite type cast, then run firstpass }
  690. if (nf_explizit in flags) or not assigned(left.resulttype) then
  691. firstpass(left);
  692. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  693. begin
  694. codegenerror:=true;
  695. Message(parser_e_no_type_not_allowed_here);
  696. end;
  697. if codegenerror then
  698. begin
  699. resulttype:=generrordef;
  700. exit;
  701. end;
  702. if not assigned(left.resulttype) then
  703. begin
  704. codegenerror:=true;
  705. internalerror(52349);
  706. exit;
  707. end;
  708. { load the value_str from the left part }
  709. registers32:=left.registers32;
  710. registersfpu:=left.registersfpu;
  711. {$ifdef SUPPORT_MMX}
  712. registersmmx:=left.registersmmx;
  713. {$endif}
  714. set_location(location,left.location);
  715. { remove obsolete type conversions }
  716. if is_equal(left.resulttype,resulttype) then
  717. begin
  718. { becuase is_equal only checks the basetype for sets we need to
  719. check here if we are loading a smallset into a normalset }
  720. if (resulttype^.deftype=setdef) and
  721. (left.resulttype^.deftype=setdef) and
  722. (psetdef(resulttype)^.settype<>smallset) and
  723. (psetdef(left.resulttype)^.settype=smallset) then
  724. begin
  725. { try to define the set as a normalset if it's a constant set }
  726. if left.nodetype=setconstn then
  727. begin
  728. resulttype:=left.resulttype;
  729. psetdef(resulttype)^.settype:=normset
  730. end
  731. else
  732. convtype:=tc_load_smallset;
  733. exit;
  734. end
  735. else
  736. begin
  737. pass_1:=left;
  738. left.resulttype:=resulttype;
  739. left:=nil;
  740. exit;
  741. end;
  742. end;
  743. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  744. if assigned(aprocdef) then
  745. begin
  746. procinfo^.flags:=procinfo^.flags or pi_do_call;
  747. hp:=gencallnode(overloaded_operators[_assignment],nil);
  748. { tell explicitly which def we must use !! (PM) }
  749. tcallnode(hp).procdefinition:=aprocdef;
  750. tcallnode(hp).left:=gencallparanode(left,nil);
  751. left:=nil;
  752. firstpass(hp);
  753. pass_1:=hp;
  754. exit;
  755. end;
  756. if isconvertable(left.resulttype,resulttype,convtype,left,left.nodetype,nf_explizit in flags)=0 then
  757. begin
  758. {Procedures have a resulttype of voiddef and functions of their
  759. own resulttype. They will therefore always be incompatible with
  760. a procvar. Because isconvertable cannot check for procedures we
  761. use an extra check for them.}
  762. if (m_tp_procvar in aktmodeswitches) then
  763. begin
  764. if (resulttype^.deftype=procvardef) and
  765. (is_procsym_load(left) or is_procsym_call(left)) then
  766. begin
  767. if is_procsym_call(left) then
  768. begin
  769. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable) and
  770. assigned(tcallnode(left).methodpointer) then
  771. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  772. tcallnode(left).symtableproc,tcallnode(left).methodpointer.getcopy)
  773. else
  774. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  775. tcallnode(left).symtableproc);
  776. firstpass(hp);
  777. left.free;
  778. left:=hp;
  779. aprocdef:=pprocdef(left.resulttype);
  780. end
  781. else
  782. begin
  783. if (left.nodetype<>addrn) then
  784. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  785. end;
  786. convtype:=tc_proc_2_procvar;
  787. { Now check if the procedure we are going to assign to
  788. the procvar, is compatible with the procvar's type }
  789. if assigned(aprocdef) then
  790. begin
  791. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  792. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  793. pass_1:=first_call_helper(convtype);
  794. end
  795. else
  796. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  797. exit;
  798. end;
  799. end;
  800. if nf_explizit in flags then
  801. begin
  802. { check if the result could be in a register }
  803. if not(pstoreddef(resulttype)^.is_intregable) and
  804. not(pstoreddef(resulttype)^.is_fpuregable) then
  805. make_not_regable(left);
  806. { boolean to byte are special because the
  807. location can be different }
  808. if is_integer(resulttype) and
  809. is_boolean(left.resulttype) then
  810. begin
  811. convtype:=tc_bool_2_int;
  812. pass_1:=first_call_helper(convtype);
  813. exit;
  814. end;
  815. { ansistring to pchar }
  816. if is_pchar(resulttype) and
  817. is_ansistring(left.resulttype) then
  818. begin
  819. convtype:=tc_ansistring_2_pchar;
  820. pass_1:=first_call_helper(convtype);
  821. exit;
  822. end;
  823. { do common tc_equal cast }
  824. convtype:=tc_equal;
  825. { enum to ordinal will always be s32bit }
  826. if (left.resulttype^.deftype=enumdef) and
  827. is_ordinal(resulttype) then
  828. begin
  829. if left.nodetype=ordconstn then
  830. begin
  831. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  832. firstpass(hp);
  833. pass_1:=hp;
  834. exit;
  835. end
  836. else
  837. begin
  838. if isconvertable(s32bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  839. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  840. end;
  841. end
  842. { ordinal to enumeration }
  843. else
  844. if (resulttype^.deftype=enumdef) and
  845. is_ordinal(left.resulttype) then
  846. begin
  847. if left.nodetype=ordconstn then
  848. begin
  849. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  850. firstpass(hp);
  851. pass_1:=hp;
  852. exit;
  853. end
  854. else
  855. begin
  856. if IsConvertable(left.resulttype,s32bitdef,convtype,nil,ordconstn,false)=0 then
  857. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  858. end;
  859. end
  860. { nil to ordinal node }
  861. else if is_ordinal(resulttype) and
  862. (left.nodetype=niln) then
  863. begin
  864. hp:=genordinalconstnode(0,resulttype);
  865. firstpass(hp);
  866. pass_1:=hp;
  867. exit;
  868. end
  869. { constant pointer to ordinal }
  870. else if is_ordinal(resulttype) and
  871. (left.nodetype=pointerconstn) then
  872. begin
  873. hp:=genordinalconstnode(tpointerconstnode(left).value,resulttype);
  874. firstpass(hp);
  875. pass_1:=hp;
  876. exit;
  877. end
  878. {Are we typecasting an ordconst to a char?}
  879. else
  880. if is_char(resulttype) and
  881. is_ordinal(left.resulttype) then
  882. begin
  883. if left.nodetype=ordconstn then
  884. begin
  885. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  886. firstpass(hp);
  887. pass_1:=hp;
  888. exit;
  889. end
  890. else
  891. begin
  892. if IsConvertable(left.resulttype,u8bitdef,convtype,nil,ordconstn,false)=0 then
  893. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  894. end;
  895. end
  896. { Are we char to ordinal }
  897. else
  898. if is_char(left.resulttype) and
  899. is_ordinal(resulttype) then
  900. begin
  901. if left.nodetype=ordconstn then
  902. begin
  903. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  904. firstpass(hp);
  905. pass_1:=hp;
  906. exit;
  907. end
  908. else
  909. begin
  910. if IsConvertable(u8bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  911. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  912. end;
  913. end
  914. { only if the same size or formal def }
  915. { why do we allow typecasting of voiddef ?? (PM) }
  916. else
  917. begin
  918. if not(
  919. (left.resulttype^.deftype=formaldef) or
  920. (left.resulttype^.size=resulttype^.size) or
  921. (is_equal(left.resulttype,voiddef) and
  922. (left.nodetype=derefn))
  923. ) then
  924. CGMessage(cg_e_illegal_type_conversion);
  925. if ((left.resulttype^.deftype=orddef) and
  926. (resulttype^.deftype=pointerdef)) or
  927. ((resulttype^.deftype=orddef) and
  928. (left.resulttype^.deftype=pointerdef))
  929. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  930. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  931. end;
  932. { the conversion into a strutured type is only }
  933. { possible, if the source is no register }
  934. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  935. ((resulttype^.deftype=objectdef) and not(is_class(resulttype)))
  936. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  937. it also works if the assignment is overloaded
  938. YES but this code is not executed if assignment is overloaded (PM)
  939. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  940. CGMessage(cg_e_illegal_type_conversion);
  941. end
  942. else
  943. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  944. end;
  945. { tp7 procvar support, when right is not a procvardef and we got a
  946. loadn of a procvar then convert to a calln, the check for the
  947. result is already done in is_convertible, also no conflict with
  948. @procvar is here because that has an extra addrn }
  949. if (m_tp_procvar in aktmodeswitches) and
  950. (resulttype^.deftype<>procvardef) and
  951. (left.resulttype^.deftype=procvardef) and
  952. (left.nodetype=loadn) then
  953. begin
  954. hp:=gencallnode(nil,nil);
  955. tcallnode(hp).right:=left;
  956. firstpass(hp);
  957. left:=hp;
  958. end;
  959. { ordinal contants can be directly converted }
  960. if (left.nodetype=ordconstn) and is_ordinal(resulttype) then
  961. begin
  962. { range checking is done in genordinalconstnode (PFV) }
  963. { disable for explicit type casts (JM) }
  964. if (nf_explizit in flags) and
  965. (cs_check_range in aktlocalswitches) then
  966. begin
  967. exclude(aktlocalswitches,cs_check_range);
  968. enable_range_check := true;
  969. end
  970. else
  971. enable_range_check := false;
  972. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  973. { do sign extension if necessary (JM) }
  974. if not (cs_check_range in aktlocalswitches) and
  975. is_signed(resulttype) then
  976. with tordconstnode(hp) do
  977. case resulttype^.size of
  978. 1: value := shortint(value);
  979. 2: value := smallint(value);
  980. 4: value := longint(value);
  981. end;
  982. if enable_range_check then
  983. include(aktlocalswitches,cs_check_range);
  984. firstpass(hp);
  985. pass_1:=hp;
  986. exit;
  987. end;
  988. if convtype<>tc_equal then
  989. pass_1:=first_call_helper(convtype);
  990. end;
  991. {*****************************************************************************
  992. TISNODE
  993. *****************************************************************************}
  994. constructor tisnode.create(l,r : tnode);
  995. begin
  996. inherited create(isn,l,r);
  997. end;
  998. function tisnode.pass_1 : tnode;
  999. begin
  1000. pass_1:=nil;
  1001. firstpass(left);
  1002. set_varstate(left,true);
  1003. firstpass(right);
  1004. set_varstate(right,true);
  1005. if codegenerror then
  1006. exit;
  1007. if (right.resulttype^.deftype<>classrefdef) then
  1008. CGMessage(type_e_mismatch);
  1009. left_right_max;
  1010. { left must be a class }
  1011. if (left.resulttype^.deftype<>objectdef) or
  1012. not(is_class(left.resulttype)) then
  1013. CGMessage(type_e_mismatch);
  1014. { the operands must be related }
  1015. if (not(pobjectdef(left.resulttype)^.is_related(
  1016. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1017. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1018. pobjectdef(left.resulttype)))) then
  1019. CGMessage(type_e_mismatch);
  1020. location.loc:=LOC_FLAGS;
  1021. resulttype:=booldef;
  1022. end;
  1023. {*****************************************************************************
  1024. TASNODE
  1025. *****************************************************************************}
  1026. constructor tasnode.create(l,r : tnode);
  1027. begin
  1028. inherited create(asn,l,r);
  1029. end;
  1030. function tasnode.pass_1 : tnode;
  1031. begin
  1032. pass_1:=nil;
  1033. firstpass(right);
  1034. set_varstate(right,true);
  1035. firstpass(left);
  1036. set_varstate(left,true);
  1037. if codegenerror then
  1038. exit;
  1039. if (right.resulttype^.deftype<>classrefdef) then
  1040. CGMessage(type_e_mismatch);
  1041. left_right_max;
  1042. { left must be a class }
  1043. if (left.resulttype^.deftype<>objectdef) or
  1044. not(is_class(left.resulttype)) then
  1045. CGMessage(type_e_mismatch);
  1046. { the operands must be related }
  1047. if (not(pobjectdef(left.resulttype)^.is_related(
  1048. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1049. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1050. pobjectdef(left.resulttype)))) then
  1051. CGMessage(type_e_mismatch);
  1052. set_location(location,left.location);
  1053. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1054. end;
  1055. function ttypeconvnode.docompare(p: tnode) : boolean;
  1056. begin
  1057. docompare :=
  1058. inherited docompare(p) and
  1059. (convtype = ttypeconvnode(p).convtype);
  1060. end;
  1061. begin
  1062. ctypeconvnode:=ttypeconvnode;
  1063. casnode:=tasnode;
  1064. cisnode:=tisnode;
  1065. end.
  1066. {
  1067. $Log$
  1068. Revision 1.21 2001-03-08 17:44:47 jonas
  1069. * fixed web bug 1430
  1070. Revision 1.20 2001/02/21 11:49:50 jonas
  1071. * evaluate typecasts of const pointers to ordinals inline ('merged')
  1072. Revision 1.19 2001/02/20 18:37:10 peter
  1073. * removed unused code
  1074. Revision 1.18 2001/02/20 13:14:18 marco
  1075. * Fix from Peter for passing a procedure of method to a other method in a method
  1076. Revision 1.17 2001/02/08 13:09:03 jonas
  1077. * fixed web bug 1396: tpointerord is now a cardinal instead of a longint,
  1078. but added a hack in ncnv so that pointer(-1) still works
  1079. Revision 1.16 2000/12/31 11:14:10 jonas
  1080. + implemented/fixed docompare() mathods for all nodes (not tested)
  1081. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1082. and constant strings/chars together
  1083. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1084. when adding
  1085. Revision 1.15 2000/12/08 12:41:01 jonas
  1086. * fixed bug in sign extension patch
  1087. Revision 1.14 2000/12/07 17:19:42 jonas
  1088. * new constant handling: from now on, hex constants >$7fffffff are
  1089. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1090. and became $ffffffff80000000), all constants in the longint range
  1091. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1092. are cardinals and the rest are int64's.
  1093. * added lots of longint typecast to prevent range check errors in the
  1094. compiler and rtl
  1095. * type casts of symbolic ordinal constants are now preserved
  1096. * fixed bug where the original resulttype wasn't restored correctly
  1097. after doing a 64bit rangecheck
  1098. Revision 1.13 2000/11/29 00:30:32 florian
  1099. * unused units removed from uses clause
  1100. * some changes for widestrings
  1101. Revision 1.12 2000/11/20 16:06:04 jonas
  1102. + allow evaluation of 64bit constant expressions at compile time
  1103. * disable range checking for explicit typecasts of constant expressions
  1104. Revision 1.11 2000/11/12 23:24:11 florian
  1105. * interfaces are basically running
  1106. Revision 1.10 2000/11/04 14:25:20 florian
  1107. + merged Attila's changes for interfaces, not tested yet
  1108. Revision 1.9 2000/10/31 22:02:48 peter
  1109. * symtable splitted, no real code changes
  1110. Revision 1.8 2000/10/14 21:52:55 peter
  1111. * fixed memory leaks
  1112. Revision 1.7 2000/10/14 10:14:50 peter
  1113. * moehrendorf oct 2000 rewrite
  1114. Revision 1.6 2000/10/01 19:48:24 peter
  1115. * lot of compile updates for cg11
  1116. Revision 1.5 2000/09/28 19:49:52 florian
  1117. *** empty log message ***
  1118. Revision 1.4 2000/09/27 18:14:31 florian
  1119. * fixed a lot of syntax errors in the n*.pas stuff
  1120. Revision 1.3 2000/09/26 20:06:13 florian
  1121. * hmm, still a lot of work to get things compilable
  1122. Revision 1.2 2000/09/26 14:59:34 florian
  1123. * more conversion work done
  1124. Revision 1.1 2000/09/25 15:37:14 florian
  1125. * more fixes
  1126. }