ncnv.pas 40 KB

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