ncnv.pas 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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:=csetconstnode.create(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. { set the new tree }
  303. p:=tarrayconstructornode(buildp);
  304. end;
  305. {*****************************************************************************
  306. TTYPECONVNODE
  307. *****************************************************************************}
  308. constructor ttypeconvnode.create(node : tnode;t : pdef);
  309. begin
  310. inherited create(typeconvn,node);
  311. convtype:=tc_not_possible;
  312. resulttype:=t;
  313. set_file_line(node);
  314. end;
  315. function ttypeconvnode.getcopy : tnode;
  316. var
  317. n : ttypeconvnode;
  318. begin
  319. n:=ttypeconvnode(inherited getcopy);
  320. n.convtype:=convtype;
  321. getcopy:=n;
  322. end;
  323. function ttypeconvnode.first_int_to_int : tnode;
  324. begin
  325. first_int_to_int:=nil;
  326. if (left.location.loc<>LOC_REGISTER) and
  327. (resulttype^.size>left.resulttype^.size) then
  328. location.loc:=LOC_REGISTER;
  329. if is_64bitint(resulttype) then
  330. registers32:=max(registers32,2)
  331. else
  332. registers32:=max(registers32,1);
  333. end;
  334. function ttypeconvnode.first_cstring_to_pchar : tnode;
  335. begin
  336. first_cstring_to_pchar:=nil;
  337. registers32:=1;
  338. location.loc:=LOC_REGISTER;
  339. end;
  340. function ttypeconvnode.first_string_to_chararray : tnode;
  341. begin
  342. first_string_to_chararray:=nil;
  343. registers32:=1;
  344. location.loc:=LOC_REGISTER;
  345. end;
  346. function ttypeconvnode.first_string_to_string : tnode;
  347. begin
  348. first_string_to_string:=nil;
  349. if pstringdef(resulttype)^.string_typ<>
  350. pstringdef(left.resulttype)^.string_typ then
  351. begin
  352. if left.nodetype=stringconstn then
  353. begin
  354. tstringconstnode(left).stringtype:=pstringdef(resulttype)^.string_typ;
  355. tstringconstnode(left).resulttype:=resulttype;
  356. { remove typeconv node }
  357. first_string_to_string:=left;
  358. left:=nil;
  359. exit;
  360. end
  361. else
  362. procinfo^.flags:=procinfo^.flags or pi_do_call;
  363. end;
  364. { for simplicity lets first keep all ansistrings
  365. as LOC_MEM, could also become LOC_REGISTER }
  366. if pstringdef(resulttype)^.string_typ in [st_ansistring,st_widestring] then
  367. { we may use ansistrings so no fast exit here }
  368. procinfo^.no_fast_exit:=true;
  369. location.loc:=LOC_MEM;
  370. end;
  371. function ttypeconvnode.first_char_to_string : tnode;
  372. var
  373. hp : tstringconstnode;
  374. begin
  375. first_char_to_string:=nil;
  376. if left.nodetype=ordconstn then
  377. begin
  378. hp:=genstringconstnode(chr(tordconstnode(left).value),st_default);
  379. hp.stringtype:=pstringdef(resulttype)^.string_typ;
  380. firstpass(hp);
  381. first_char_to_string:=hp;
  382. end
  383. else
  384. location.loc:=LOC_MEM;
  385. end;
  386. function ttypeconvnode.first_nothing : tnode;
  387. begin
  388. first_nothing:=nil;
  389. location.loc:=LOC_MEM;
  390. end;
  391. function ttypeconvnode.first_array_to_pointer : tnode;
  392. begin
  393. first_array_to_pointer:=nil;
  394. if registers32<1 then
  395. registers32:=1;
  396. location.loc:=LOC_REGISTER;
  397. end;
  398. function ttypeconvnode.first_int_to_real : tnode;
  399. var
  400. t : trealconstnode;
  401. begin
  402. first_int_to_real:=nil;
  403. if left.nodetype=ordconstn then
  404. begin
  405. t:=genrealconstnode(tordconstnode(left).value,pfloatdef(resulttype));
  406. firstpass(t);
  407. first_int_to_real:=t;
  408. exit;
  409. end;
  410. if registersfpu<1 then
  411. registersfpu:=1;
  412. location.loc:=LOC_FPU;
  413. end;
  414. function ttypeconvnode.first_int_to_fix : tnode;
  415. var
  416. t : tnode;
  417. begin
  418. first_int_to_fix:=nil;
  419. if left.nodetype=ordconstn then
  420. begin
  421. t:=genfixconstnode(tordconstnode(left).value shl 16,resulttype);
  422. firstpass(t);
  423. first_int_to_fix:=t;
  424. exit;
  425. end;
  426. if registers32<1 then
  427. registers32:=1;
  428. location.loc:=LOC_REGISTER;
  429. end;
  430. function ttypeconvnode.first_real_to_fix : tnode;
  431. var
  432. t : tnode;
  433. begin
  434. first_real_to_fix:=nil;
  435. if left.nodetype=realconstn then
  436. begin
  437. t:=genfixconstnode(round(trealconstnode(left).value_real*65536),resulttype);
  438. firstpass(t);
  439. first_real_to_fix:=t;
  440. exit;
  441. end;
  442. { at least one fpu and int register needed }
  443. if registers32<1 then
  444. registers32:=1;
  445. if registersfpu<1 then
  446. registersfpu:=1;
  447. location.loc:=LOC_REGISTER;
  448. end;
  449. function ttypeconvnode.first_fix_to_real : tnode;
  450. var
  451. t : tnode;
  452. begin
  453. first_fix_to_real:=nil;
  454. if left.nodetype=fixconstn then
  455. begin
  456. t:=genrealconstnode(round(tfixconstnode(left).value_fix/65536.0),resulttype);
  457. firstpass(t);
  458. first_fix_to_real:=t;
  459. exit;
  460. end;
  461. if registersfpu<1 then
  462. registersfpu:=1;
  463. location.loc:=LOC_FPU;
  464. end;
  465. function ttypeconvnode.first_real_to_real : tnode;
  466. var
  467. t : tnode;
  468. begin
  469. first_real_to_real:=nil;
  470. if left.nodetype=realconstn then
  471. begin
  472. t:=genrealconstnode(trealconstnode(left).value_real,resulttype);
  473. firstpass(t);
  474. first_real_to_real:=t;
  475. exit;
  476. end;
  477. { comp isn't a floating type }
  478. {$ifdef i386}
  479. if (pfloatdef(resulttype)^.typ=s64comp) and
  480. (pfloatdef(left.resulttype)^.typ<>s64comp) and
  481. not (nf_explizit in flags) then
  482. CGMessage(type_w_convert_real_2_comp);
  483. {$endif}
  484. if registersfpu<1 then
  485. registersfpu:=1;
  486. location.loc:=LOC_FPU;
  487. end;
  488. function ttypeconvnode.first_pointer_to_array : tnode;
  489. begin
  490. first_pointer_to_array:=nil;
  491. if registers32<1 then
  492. registers32:=1;
  493. location.loc:=LOC_REFERENCE;
  494. end;
  495. function ttypeconvnode.first_chararray_to_string : tnode;
  496. begin
  497. first_chararray_to_string:=nil;
  498. { the only important information is the location of the }
  499. { result }
  500. { other stuff is done by firsttypeconv }
  501. location.loc:=LOC_MEM;
  502. end;
  503. function ttypeconvnode.first_cchar_to_pchar : tnode;
  504. begin
  505. first_cchar_to_pchar:=nil;
  506. left:=gentypeconvnode(left,cshortstringdef);
  507. { convert constant char to constant string }
  508. firstpass(left);
  509. { evalute tree }
  510. first_cchar_to_pchar:=pass_1;
  511. end;
  512. function ttypeconvnode.first_bool_to_int : tnode;
  513. begin
  514. first_bool_to_int:=nil;
  515. { byte(boolean) or word(wordbool) or longint(longbool) must
  516. be accepted for var parameters }
  517. if (nf_explizit in flags) and
  518. (left.resulttype^.size=resulttype^.size) and
  519. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  520. exit;
  521. location.loc:=LOC_REGISTER;
  522. if registers32<1 then
  523. registers32:=1;
  524. end;
  525. function ttypeconvnode.first_int_to_bool : tnode;
  526. begin
  527. first_int_to_bool:=nil;
  528. { byte(boolean) or word(wordbool) or longint(longbool) must
  529. be accepted for var parameters }
  530. if (nf_explizit in flags) and
  531. (left.resulttype^.size=resulttype^.size) and
  532. (left.location.loc in [LOC_REFERENCE,LOC_MEM,LOC_CREGISTER]) then
  533. exit;
  534. location.loc:=LOC_REGISTER;
  535. { need if bool to bool !!
  536. not very nice !!
  537. left:=gentypeconvnode(left,s32bitdef);
  538. left.explizit:=true;
  539. firstpass(left); }
  540. if registers32<1 then
  541. registers32:=1;
  542. end;
  543. function ttypeconvnode.first_bool_to_bool : tnode;
  544. begin
  545. first_bool_to_bool:=nil;
  546. location.loc:=LOC_REGISTER;
  547. if registers32<1 then
  548. registers32:=1;
  549. end;
  550. function ttypeconvnode.first_proc_to_procvar : tnode;
  551. begin
  552. first_proc_to_procvar:=nil;
  553. { hmmm, I'am not sure if that is necessary (FK) }
  554. firstpass(left);
  555. if codegenerror then
  556. exit;
  557. if (left.location.loc<>LOC_REFERENCE) then
  558. CGMessage(cg_e_illegal_expression);
  559. registers32:=left.registers32;
  560. if registers32<1 then
  561. registers32:=1;
  562. location.loc:=LOC_REGISTER;
  563. end;
  564. function ttypeconvnode.first_load_smallset : tnode;
  565. begin
  566. first_load_smallset:=nil;
  567. end;
  568. function ttypeconvnode.first_cord_to_pointer : tnode;
  569. var
  570. t : tnode;
  571. begin
  572. first_cord_to_pointer:=nil;
  573. if left.nodetype=ordconstn then
  574. begin
  575. { check if we have a valid pointer constant (JM) }
  576. if (sizeof(tordconstnode) > sizeof(tpointerord)) then
  577. if (sizeof(tpointerord) = 4) then
  578. begin
  579. if (tordconstnode(left).value < low(longint)) or
  580. (tordconstnode(left).value > high(cardinal)) then
  581. CGMessage(parser_e_range_check_error);
  582. end
  583. else if (sizeof(tpointerord) = 8) then
  584. begin
  585. if (tordconstnode(left).value < low(int64)) or
  586. (tordconstnode(left).value > high(qword)) then
  587. CGMessage(parser_e_range_check_error);
  588. end
  589. else
  590. internalerror(2001020801);
  591. t:=genpointerconstnode(tpointerord(tordconstnode(left).value),resulttype);
  592. firstpass(t);
  593. first_cord_to_pointer:=t;
  594. exit;
  595. end
  596. else
  597. internalerror(432472389);
  598. end;
  599. function ttypeconvnode.first_pchar_to_string : tnode;
  600. begin
  601. first_pchar_to_string:=nil;
  602. location.loc:=LOC_REFERENCE;
  603. end;
  604. function ttypeconvnode.first_ansistring_to_pchar : tnode;
  605. begin
  606. first_ansistring_to_pchar:=nil;
  607. location.loc:=LOC_REGISTER;
  608. if registers32<1 then
  609. registers32:=1;
  610. end;
  611. function ttypeconvnode.first_arrayconstructor_to_set : tnode;
  612. var
  613. hp : tnode;
  614. begin
  615. first_arrayconstructor_to_set:=nil;
  616. if left.nodetype<>arrayconstructorn then
  617. internalerror(5546);
  618. { remove typeconv node }
  619. hp:=left;
  620. left:=nil;
  621. { create a set constructor tree }
  622. arrayconstructor_to_set(tarrayconstructornode(hp));
  623. { now firstpass the set }
  624. firstpass(hp);
  625. first_arrayconstructor_to_set:=hp;
  626. end;
  627. function ttypeconvnode.first_class_to_intf : tnode;
  628. begin
  629. first_class_to_intf:=nil;
  630. location.loc:=LOC_REFERENCE;
  631. if registers32<1 then
  632. registers32:=1;
  633. end;
  634. function ttypeconvnode.first_call_helper(c : tconverttype) : tnode;
  635. const
  636. firstconvert : array[tconverttype] of pointer = (
  637. @ttypeconvnode.first_nothing, {equal}
  638. @ttypeconvnode.first_nothing, {not_possible}
  639. @ttypeconvnode.first_string_to_string,
  640. @ttypeconvnode.first_char_to_string,
  641. @ttypeconvnode.first_pchar_to_string,
  642. @ttypeconvnode.first_cchar_to_pchar,
  643. @ttypeconvnode.first_cstring_to_pchar,
  644. @ttypeconvnode.first_ansistring_to_pchar,
  645. @ttypeconvnode.first_string_to_chararray,
  646. @ttypeconvnode.first_chararray_to_string,
  647. @ttypeconvnode.first_array_to_pointer,
  648. @ttypeconvnode.first_pointer_to_array,
  649. @ttypeconvnode.first_int_to_int,
  650. @ttypeconvnode.first_int_to_bool,
  651. @ttypeconvnode.first_bool_to_bool,
  652. @ttypeconvnode.first_bool_to_int,
  653. @ttypeconvnode.first_real_to_real,
  654. @ttypeconvnode.first_int_to_real,
  655. @ttypeconvnode.first_int_to_fix,
  656. @ttypeconvnode.first_real_to_fix,
  657. @ttypeconvnode.first_fix_to_real,
  658. @ttypeconvnode.first_proc_to_procvar,
  659. @ttypeconvnode.first_arrayconstructor_to_set,
  660. @ttypeconvnode.first_load_smallset,
  661. @ttypeconvnode.first_cord_to_pointer,
  662. @ttypeconvnode.first_nothing,
  663. @ttypeconvnode.first_nothing,
  664. @ttypeconvnode.first_class_to_intf
  665. );
  666. type
  667. tprocedureofobject = function : tnode of object;
  668. var
  669. r : packed record
  670. proc : pointer;
  671. obj : pointer;
  672. end;
  673. begin
  674. { this is a little bit dirty but it works }
  675. { and should be quite portable too }
  676. r.proc:=firstconvert[c];
  677. r.obj:=self;
  678. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  679. end;
  680. function ttypeconvnode.pass_1 : tnode;
  681. var
  682. hp : tnode;
  683. aprocdef : pprocdef;
  684. enable_range_check: boolean;
  685. begin
  686. pass_1:=nil;
  687. aprocdef:=nil;
  688. { if explicite type cast, then run firstpass }
  689. if (nf_explizit in flags) or not assigned(left.resulttype) then
  690. firstpass(left);
  691. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  692. begin
  693. codegenerror:=true;
  694. Message(parser_e_no_type_not_allowed_here);
  695. end;
  696. if codegenerror then
  697. begin
  698. resulttype:=generrordef;
  699. exit;
  700. end;
  701. if not assigned(left.resulttype) then
  702. begin
  703. codegenerror:=true;
  704. internalerror(52349);
  705. exit;
  706. end;
  707. { load the value_str from the left part }
  708. registers32:=left.registers32;
  709. registersfpu:=left.registersfpu;
  710. {$ifdef SUPPORT_MMX}
  711. registersmmx:=left.registersmmx;
  712. {$endif}
  713. set_location(location,left.location);
  714. { remove obsolete type conversions }
  715. if is_equal(left.resulttype,resulttype) then
  716. begin
  717. { becuase is_equal only checks the basetype for sets we need to
  718. check here if we are loading a smallset into a normalset }
  719. if (resulttype^.deftype=setdef) and
  720. (left.resulttype^.deftype=setdef) and
  721. (psetdef(resulttype)^.settype<>smallset) and
  722. (psetdef(left.resulttype)^.settype=smallset) then
  723. begin
  724. { try to define the set as a normalset if it's a constant set }
  725. if left.nodetype=setconstn then
  726. begin
  727. resulttype:=left.resulttype;
  728. psetdef(resulttype)^.settype:=normset
  729. end
  730. else
  731. convtype:=tc_load_smallset;
  732. exit;
  733. end
  734. else
  735. begin
  736. pass_1:=left;
  737. left.resulttype:=resulttype;
  738. left:=nil;
  739. exit;
  740. end;
  741. end;
  742. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  743. if assigned(aprocdef) then
  744. begin
  745. procinfo^.flags:=procinfo^.flags or pi_do_call;
  746. hp:=gencallnode(overloaded_operators[_assignment],nil);
  747. { tell explicitly which def we must use !! (PM) }
  748. tcallnode(hp).procdefinition:=aprocdef;
  749. tcallnode(hp).left:=gencallparanode(left,nil);
  750. left:=nil;
  751. firstpass(hp);
  752. pass_1:=hp;
  753. exit;
  754. end;
  755. if isconvertable(left.resulttype,resulttype,convtype,left,left.nodetype,nf_explizit in flags)=0 then
  756. begin
  757. {Procedures have a resulttype of voiddef and functions of their
  758. own resulttype. They will therefore always be incompatible with
  759. a procvar. Because isconvertable cannot check for procedures we
  760. use an extra check for them.}
  761. if (m_tp_procvar in aktmodeswitches) then
  762. begin
  763. if (resulttype^.deftype=procvardef) and
  764. (is_procsym_load(left) or is_procsym_call(left)) then
  765. begin
  766. if is_procsym_call(left) then
  767. begin
  768. {if left.right=nil then
  769. begin}
  770. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  771. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  772. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  773. tcallnode(left).symtableproc,
  774. tcallnode(left).methodpointer.getcopy)
  775. else
  776. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  777. tcallnode(left).symtableproc);
  778. firstpass(hp);
  779. left.free;
  780. left:=hp;
  781. aprocdef:=pprocdef(left.resulttype);
  782. (* end
  783. else
  784. begin
  785. left.right.nodetype:=loadn;
  786. left.right.symtableentry:=left.right.symtableentry;
  787. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  788. hp:=left.right;
  789. putnode(left);
  790. left:=hp;
  791. { should we do that ? }
  792. firstpass(left);
  793. if not is_equal(left.resulttype,resulttype) then
  794. begin
  795. CGMessage(type_e_mismatch);
  796. exit;
  797. end
  798. else
  799. begin
  800. hp:=p;
  801. p:=left;
  802. resulttype:=hp.resulttype;
  803. putnode(hp);
  804. exit;
  805. end;
  806. end; *)
  807. end
  808. else
  809. begin
  810. if (left.nodetype<>addrn) then
  811. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  812. end;
  813. convtype:=tc_proc_2_procvar;
  814. { Now check if the procedure we are going to assign to
  815. the procvar, is compatible with the procvar's type }
  816. if assigned(aprocdef) then
  817. begin
  818. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  819. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  820. pass_1:=first_call_helper(convtype);
  821. end
  822. else
  823. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  824. exit;
  825. end;
  826. end;
  827. if nf_explizit in flags then
  828. begin
  829. { check if the result could be in a register }
  830. if not(pstoreddef(resulttype)^.is_intregable) and
  831. not(pstoreddef(resulttype)^.is_fpuregable) then
  832. make_not_regable(left);
  833. { boolean to byte are special because the
  834. location can be different }
  835. if is_integer(resulttype) and
  836. is_boolean(left.resulttype) then
  837. begin
  838. convtype:=tc_bool_2_int;
  839. pass_1:=first_call_helper(convtype);
  840. exit;
  841. end;
  842. { ansistring to pchar }
  843. if is_pchar(resulttype) and
  844. is_ansistring(left.resulttype) then
  845. begin
  846. convtype:=tc_ansistring_2_pchar;
  847. pass_1:=first_call_helper(convtype);
  848. exit;
  849. end;
  850. { do common tc_equal cast }
  851. convtype:=tc_equal;
  852. { enum to ordinal will always be s32bit }
  853. if (left.resulttype^.deftype=enumdef) and
  854. is_ordinal(resulttype) then
  855. begin
  856. if left.nodetype=ordconstn then
  857. begin
  858. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  859. firstpass(hp);
  860. pass_1:=hp;
  861. exit;
  862. end
  863. else
  864. begin
  865. if isconvertable(s32bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  866. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  867. end;
  868. end
  869. { ordinal to enumeration }
  870. else
  871. if (resulttype^.deftype=enumdef) and
  872. is_ordinal(left.resulttype) then
  873. begin
  874. if left.nodetype=ordconstn then
  875. begin
  876. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  877. firstpass(hp);
  878. pass_1:=hp;
  879. exit;
  880. end
  881. else
  882. begin
  883. if IsConvertable(left.resulttype,s32bitdef,convtype,nil,ordconstn,false)=0 then
  884. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  885. end;
  886. end
  887. { nil to ordinal node }
  888. else if is_ordinal(resulttype) and
  889. (left.nodetype=niln) then
  890. begin
  891. hp:=genordinalconstnode(0,resulttype);
  892. firstpass(hp);
  893. pass_1:=hp;
  894. exit;
  895. end
  896. {Are we typecasting an ordconst to a char?}
  897. else
  898. if is_char(resulttype) and
  899. is_ordinal(left.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(left.resulttype,u8bitdef,convtype,nil,ordconstn,false)=0 then
  911. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  912. end;
  913. end
  914. { Are we char to ordinal }
  915. else
  916. if is_char(left.resulttype) and
  917. is_ordinal(resulttype) then
  918. begin
  919. if left.nodetype=ordconstn then
  920. begin
  921. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  922. firstpass(hp);
  923. pass_1:=hp;
  924. exit;
  925. end
  926. else
  927. begin
  928. if IsConvertable(u8bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  929. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  930. end;
  931. end
  932. { only if the same size or formal def }
  933. { why do we allow typecasting of voiddef ?? (PM) }
  934. else
  935. begin
  936. if not(
  937. (left.resulttype^.deftype=formaldef) or
  938. (left.resulttype^.size=resulttype^.size) or
  939. (is_equal(left.resulttype,voiddef) and
  940. (left.nodetype=derefn))
  941. ) then
  942. CGMessage(cg_e_illegal_type_conversion);
  943. if ((left.resulttype^.deftype=orddef) and
  944. (resulttype^.deftype=pointerdef)) or
  945. ((resulttype^.deftype=orddef) and
  946. (left.resulttype^.deftype=pointerdef))
  947. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  948. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  949. end;
  950. { the conversion into a strutured type is only }
  951. { possible, if the source is no register }
  952. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  953. ((resulttype^.deftype=objectdef) and not(is_class(resulttype)))
  954. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  955. it also works if the assignment is overloaded
  956. YES but this code is not executed if assignment is overloaded (PM)
  957. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  958. CGMessage(cg_e_illegal_type_conversion);
  959. end
  960. else
  961. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  962. end;
  963. { tp7 procvar support, when right is not a procvardef and we got a
  964. loadn of a procvar then convert to a calln, the check for the
  965. result is already done in is_convertible, also no conflict with
  966. @procvar is here because that has an extra addrn }
  967. if (m_tp_procvar in aktmodeswitches) and
  968. (resulttype^.deftype<>procvardef) and
  969. (left.resulttype^.deftype=procvardef) and
  970. (left.nodetype=loadn) then
  971. begin
  972. hp:=gencallnode(nil,nil);
  973. tcallnode(hp).right:=left;
  974. firstpass(hp);
  975. left:=hp;
  976. end;
  977. { ordinal contants can be directly converted }
  978. if (left.nodetype=ordconstn) and is_ordinal(resulttype) then
  979. begin
  980. { range checking is done in genordinalconstnode (PFV) }
  981. { disable for explicit type casts (JM) }
  982. if (nf_explizit in flags) and
  983. (cs_check_range in aktlocalswitches) then
  984. begin
  985. exclude(aktlocalswitches,cs_check_range);
  986. enable_range_check := true;
  987. end
  988. else
  989. enable_range_check := false;
  990. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  991. { do sign extension if necessary (JM) }
  992. if not (cs_check_range in aktlocalswitches) and
  993. is_signed(resulttype) then
  994. with tordconstnode(hp) do
  995. case resulttype^.size of
  996. 1: value := shortint(value);
  997. 2: value := smallint(value);
  998. 4: value := longint(value);
  999. end;
  1000. if enable_range_check then
  1001. include(aktlocalswitches,cs_check_range);
  1002. firstpass(hp);
  1003. pass_1:=hp;
  1004. exit;
  1005. end;
  1006. if convtype<>tc_equal then
  1007. pass_1:=first_call_helper(convtype);
  1008. end;
  1009. {*****************************************************************************
  1010. TISNODE
  1011. *****************************************************************************}
  1012. constructor tisnode.create(l,r : tnode);
  1013. begin
  1014. inherited create(isn,l,r);
  1015. end;
  1016. function tisnode.pass_1 : tnode;
  1017. begin
  1018. pass_1:=nil;
  1019. firstpass(left);
  1020. set_varstate(left,true);
  1021. firstpass(right);
  1022. set_varstate(right,true);
  1023. if codegenerror then
  1024. exit;
  1025. if (right.resulttype^.deftype<>classrefdef) then
  1026. CGMessage(type_e_mismatch);
  1027. left_right_max;
  1028. { left must be a class }
  1029. if (left.resulttype^.deftype<>objectdef) or
  1030. not(is_class(left.resulttype)) then
  1031. CGMessage(type_e_mismatch);
  1032. { the operands must be related }
  1033. if (not(pobjectdef(left.resulttype)^.is_related(
  1034. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1035. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1036. pobjectdef(left.resulttype)))) then
  1037. CGMessage(type_e_mismatch);
  1038. location.loc:=LOC_FLAGS;
  1039. resulttype:=booldef;
  1040. end;
  1041. {*****************************************************************************
  1042. TASNODE
  1043. *****************************************************************************}
  1044. constructor tasnode.create(l,r : tnode);
  1045. begin
  1046. inherited create(asn,l,r);
  1047. end;
  1048. function tasnode.pass_1 : tnode;
  1049. begin
  1050. pass_1:=nil;
  1051. firstpass(right);
  1052. set_varstate(right,true);
  1053. firstpass(left);
  1054. set_varstate(left,true);
  1055. if codegenerror then
  1056. exit;
  1057. if (right.resulttype^.deftype<>classrefdef) then
  1058. CGMessage(type_e_mismatch);
  1059. left_right_max;
  1060. { left must be a class }
  1061. if (left.resulttype^.deftype<>objectdef) or
  1062. not(is_class(left.resulttype)) then
  1063. CGMessage(type_e_mismatch);
  1064. { the operands must be related }
  1065. if (not(pobjectdef(left.resulttype)^.is_related(
  1066. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1067. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1068. pobjectdef(left.resulttype)))) then
  1069. CGMessage(type_e_mismatch);
  1070. set_location(location,left.location);
  1071. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1072. end;
  1073. function ttypeconvnode.docompare(p: tnode) : boolean;
  1074. begin
  1075. docompare :=
  1076. inherited docompare(p) and
  1077. (convtype = ttypeconvnode(p).convtype);
  1078. end;
  1079. begin
  1080. ctypeconvnode:=ttypeconvnode;
  1081. casnode:=tasnode;
  1082. cisnode:=tisnode;
  1083. end.
  1084. {
  1085. $Log$
  1086. Revision 1.17 2001-02-08 13:09:03 jonas
  1087. * fixed web bug 1396: tpointerord is now a cardinal instead of a longint,
  1088. but added a hack in ncnv so that pointer(-1) still works
  1089. Revision 1.16 2000/12/31 11:14:10 jonas
  1090. + implemented/fixed docompare() mathods for all nodes (not tested)
  1091. + nopt.pas, nadd.pas, i386/n386opt.pas: optimized nodes for adding strings
  1092. and constant strings/chars together
  1093. * n386add.pas: don't copy temp strings (of size 256) to another temp string
  1094. when adding
  1095. Revision 1.15 2000/12/08 12:41:01 jonas
  1096. * fixed bug in sign extension patch
  1097. Revision 1.14 2000/12/07 17:19:42 jonas
  1098. * new constant handling: from now on, hex constants >$7fffffff are
  1099. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1100. and became $ffffffff80000000), all constants in the longint range
  1101. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1102. are cardinals and the rest are int64's.
  1103. * added lots of longint typecast to prevent range check errors in the
  1104. compiler and rtl
  1105. * type casts of symbolic ordinal constants are now preserved
  1106. * fixed bug where the original resulttype wasn't restored correctly
  1107. after doing a 64bit rangecheck
  1108. Revision 1.13 2000/11/29 00:30:32 florian
  1109. * unused units removed from uses clause
  1110. * some changes for widestrings
  1111. Revision 1.12 2000/11/20 16:06:04 jonas
  1112. + allow evaluation of 64bit constant expressions at compile time
  1113. * disable range checking for explicit typecasts of constant expressions
  1114. Revision 1.11 2000/11/12 23:24:11 florian
  1115. * interfaces are basically running
  1116. Revision 1.10 2000/11/04 14:25:20 florian
  1117. + merged Attila's changes for interfaces, not tested yet
  1118. Revision 1.9 2000/10/31 22:02:48 peter
  1119. * symtable splitted, no real code changes
  1120. Revision 1.8 2000/10/14 21:52:55 peter
  1121. * fixed memory leaks
  1122. Revision 1.7 2000/10/14 10:14:50 peter
  1123. * moehrendorf oct 2000 rewrite
  1124. Revision 1.6 2000/10/01 19:48:24 peter
  1125. * lot of compile updates for cg11
  1126. Revision 1.5 2000/09/28 19:49:52 florian
  1127. *** empty log message ***
  1128. Revision 1.4 2000/09/27 18:14:31 florian
  1129. * fixed a lot of syntax errors in the n*.pas stuff
  1130. Revision 1.3 2000/09/26 20:06:13 florian
  1131. * hmm, still a lot of work to get things compilable
  1132. Revision 1.2 2000/09/26 14:59:34 florian
  1133. * more conversion work done
  1134. Revision 1.1 2000/09/25 15:37:14 florian
  1135. * more fixes
  1136. }