ncnv.pas 39 KB

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