ncnv.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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_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,symdef,symsym,symtable,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. @ttypeconvnode.first_nothing,
  638. @ttypeconvnode.first_nothing
  639. );
  640. type
  641. tprocedureofobject = function : tnode of object;
  642. var
  643. r : packed record
  644. proc : pointer;
  645. obj : pointer;
  646. end;
  647. begin
  648. { this is a little bit dirty but it works }
  649. { and should be quite portable too }
  650. r.proc:=firstconvert[c];
  651. r.obj:=self;
  652. first_call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  653. end;
  654. function ttypeconvnode.pass_1 : tnode;
  655. var
  656. hp : tnode;
  657. aprocdef : pprocdef;
  658. begin
  659. pass_1:=nil;
  660. aprocdef:=nil;
  661. { if explicite type cast, then run firstpass }
  662. if (nf_explizit in flags) or not assigned(left.resulttype) then
  663. firstpass(left);
  664. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  665. begin
  666. codegenerror:=true;
  667. Message(parser_e_no_type_not_allowed_here);
  668. end;
  669. if codegenerror then
  670. begin
  671. resulttype:=generrordef;
  672. exit;
  673. end;
  674. if not assigned(left.resulttype) then
  675. begin
  676. codegenerror:=true;
  677. internalerror(52349);
  678. exit;
  679. end;
  680. { load the value_str from the left part }
  681. registers32:=left.registers32;
  682. registersfpu:=left.registersfpu;
  683. {$ifdef SUPPORT_MMX}
  684. registersmmx:=left.registersmmx;
  685. {$endif}
  686. set_location(location,left.location);
  687. { remove obsolete type conversions }
  688. if is_equal(left.resulttype,resulttype) then
  689. begin
  690. { becuase is_equal only checks the basetype for sets we need to
  691. check here if we are loading a smallset into a normalset }
  692. if (resulttype^.deftype=setdef) and
  693. (left.resulttype^.deftype=setdef) and
  694. (psetdef(resulttype)^.settype<>smallset) and
  695. (psetdef(left.resulttype)^.settype=smallset) then
  696. begin
  697. { try to define the set as a normalset if it's a constant set }
  698. if left.nodetype=setconstn then
  699. begin
  700. resulttype:=left.resulttype;
  701. psetdef(resulttype)^.settype:=normset
  702. end
  703. else
  704. convtype:=tc_load_smallset;
  705. exit;
  706. end
  707. else
  708. begin
  709. pass_1:=left;
  710. left.resulttype:=resulttype;
  711. left:=nil;
  712. exit;
  713. end;
  714. end;
  715. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  716. if assigned(aprocdef) then
  717. begin
  718. procinfo^.flags:=procinfo^.flags or pi_do_call;
  719. hp:=gencallnode(overloaded_operators[_assignment],nil);
  720. { tell explicitly which def we must use !! (PM) }
  721. tcallnode(hp).procdefinition:=aprocdef;
  722. tcallnode(hp).left:=gencallparanode(left,nil);
  723. left:=nil;
  724. firstpass(hp);
  725. pass_1:=hp;
  726. exit;
  727. end;
  728. if isconvertable(left.resulttype,resulttype,convtype,left,left.nodetype,nf_explizit in flags)=0 then
  729. begin
  730. {Procedures have a resulttype of voiddef and functions of their
  731. own resulttype. They will therefore always be incompatible with
  732. a procvar. Because isconvertable cannot check for procedures we
  733. use an extra check for them.}
  734. if (m_tp_procvar in aktmodeswitches) then
  735. begin
  736. if (resulttype^.deftype=procvardef) and
  737. (is_procsym_load(left) or is_procsym_call(left)) then
  738. begin
  739. if is_procsym_call(left) then
  740. begin
  741. {if left.right=nil then
  742. begin}
  743. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  744. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  745. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  746. tcallnode(left).symtableproc,
  747. tcallnode(left).methodpointer.getcopy)
  748. else
  749. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  750. tcallnode(left).symtableproc);
  751. firstpass(hp);
  752. left.free;
  753. left:=hp;
  754. aprocdef:=pprocdef(left.resulttype);
  755. (* end
  756. else
  757. begin
  758. left.right.nodetype:=loadn;
  759. left.right.symtableentry:=left.right.symtableentry;
  760. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  761. hp:=left.right;
  762. putnode(left);
  763. left:=hp;
  764. { should we do that ? }
  765. firstpass(left);
  766. if not is_equal(left.resulttype,resulttype) then
  767. begin
  768. CGMessage(type_e_mismatch);
  769. exit;
  770. end
  771. else
  772. begin
  773. hp:=p;
  774. p:=left;
  775. resulttype:=hp.resulttype;
  776. putnode(hp);
  777. exit;
  778. end;
  779. end; *)
  780. end
  781. else
  782. begin
  783. if (left.nodetype<>addrn) then
  784. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  785. end;
  786. convtype:=tc_proc_2_procvar;
  787. { Now check if the procedure we are going to assign to
  788. the procvar, is compatible with the procvar's type }
  789. if assigned(aprocdef) then
  790. begin
  791. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  792. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  793. pass_1:=first_call_helper(convtype);
  794. end
  795. else
  796. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  797. exit;
  798. end;
  799. end;
  800. if nf_explizit in flags then
  801. begin
  802. { check if the result could be in a register }
  803. if not(pstoreddef(resulttype)^.is_intregable) and
  804. not(pstoreddef(resulttype)^.is_fpuregable) then
  805. make_not_regable(left);
  806. { boolean to byte are special because the
  807. location can be different }
  808. if is_integer(resulttype) and
  809. is_boolean(left.resulttype) then
  810. begin
  811. convtype:=tc_bool_2_int;
  812. pass_1:=first_call_helper(convtype);
  813. exit;
  814. end;
  815. { ansistring to pchar }
  816. if is_pchar(resulttype) and
  817. is_ansistring(left.resulttype) then
  818. begin
  819. convtype:=tc_ansistring_2_pchar;
  820. pass_1:=first_call_helper(convtype);
  821. exit;
  822. end;
  823. { do common tc_equal cast }
  824. convtype:=tc_equal;
  825. { enum to ordinal will always be s32bit }
  826. if (left.resulttype^.deftype=enumdef) and
  827. is_ordinal(resulttype) then
  828. begin
  829. if left.nodetype=ordconstn then
  830. begin
  831. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  832. firstpass(hp);
  833. pass_1:=hp;
  834. exit;
  835. end
  836. else
  837. begin
  838. if isconvertable(s32bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  839. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  840. end;
  841. end
  842. { ordinal to enumeration }
  843. else
  844. if (resulttype^.deftype=enumdef) and
  845. is_ordinal(left.resulttype) then
  846. begin
  847. if left.nodetype=ordconstn then
  848. begin
  849. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  850. firstpass(hp);
  851. pass_1:=hp;
  852. exit;
  853. end
  854. else
  855. begin
  856. if IsConvertable(left.resulttype,s32bitdef,convtype,nil,ordconstn,false)=0 then
  857. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  858. end;
  859. end
  860. { nil to ordinal node }
  861. else if is_ordinal(resulttype) and
  862. (left.nodetype=niln) then
  863. begin
  864. hp:=genordinalconstnode(0,resulttype);
  865. firstpass(hp);
  866. pass_1:=hp;
  867. exit;
  868. end
  869. {Are we typecasting an ordconst to a char?}
  870. else
  871. if is_char(resulttype) 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,u8bitdef,convtype,nil,ordconstn,false)=0 then
  884. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  885. end;
  886. end
  887. { Are we char to ordinal }
  888. else
  889. if is_char(left.resulttype) and
  890. is_ordinal(resulttype) then
  891. begin
  892. if left.nodetype=ordconstn then
  893. begin
  894. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  895. firstpass(hp);
  896. pass_1:=hp;
  897. exit;
  898. end
  899. else
  900. begin
  901. if IsConvertable(u8bitdef,resulttype,convtype,nil,ordconstn,false)=0 then
  902. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  903. end;
  904. end
  905. { only if the same size or formal def }
  906. { why do we allow typecasting of voiddef ?? (PM) }
  907. else
  908. begin
  909. if not(
  910. (left.resulttype^.deftype=formaldef) or
  911. (left.resulttype^.size=resulttype^.size) or
  912. (is_equal(left.resulttype,voiddef) and
  913. (left.nodetype=derefn))
  914. ) then
  915. CGMessage(cg_e_illegal_type_conversion);
  916. if ((left.resulttype^.deftype=orddef) and
  917. (resulttype^.deftype=pointerdef)) or
  918. ((resulttype^.deftype=orddef) and
  919. (left.resulttype^.deftype=pointerdef))
  920. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  921. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  922. end;
  923. { the conversion into a strutured type is only }
  924. { possible, if the source is no register }
  925. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  926. ((resulttype^.deftype=objectdef) and not(is_class(resulttype)))
  927. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  928. it also works if the assignment is overloaded
  929. YES but this code is not executed if assignment is overloaded (PM)
  930. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  931. CGMessage(cg_e_illegal_type_conversion);
  932. end
  933. else
  934. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  935. end;
  936. { tp7 procvar support, when right is not a procvardef and we got a
  937. loadn of a procvar then convert to a calln, the check for the
  938. result is already done in is_convertible, also no conflict with
  939. @procvar is here because that has an extra addrn }
  940. if (m_tp_procvar in aktmodeswitches) and
  941. (resulttype^.deftype<>procvardef) and
  942. (left.resulttype^.deftype=procvardef) and
  943. (left.nodetype=loadn) then
  944. begin
  945. hp:=gencallnode(nil,nil);
  946. tcallnode(hp).right:=left;
  947. firstpass(hp);
  948. left:=hp;
  949. end;
  950. { ordinal contants can be directly converted }
  951. { but not int64/qword }
  952. if (left.nodetype=ordconstn) and is_ordinal(resulttype) and
  953. not(is_64bitint(resulttype)) then
  954. begin
  955. { range checking is done in genordinalconstnode (PFV) }
  956. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  957. firstpass(hp);
  958. pass_1:=hp;
  959. exit;
  960. end;
  961. if convtype<>tc_equal then
  962. pass_1:=first_call_helper(convtype);
  963. end;
  964. {*****************************************************************************
  965. TISNODE
  966. *****************************************************************************}
  967. constructor tisnode.create(l,r : tnode);
  968. begin
  969. inherited create(isn,l,r);
  970. end;
  971. function tisnode.pass_1 : tnode;
  972. begin
  973. pass_1:=nil;
  974. firstpass(left);
  975. set_varstate(left,true);
  976. firstpass(right);
  977. set_varstate(right,true);
  978. if codegenerror then
  979. exit;
  980. if (right.resulttype^.deftype<>classrefdef) then
  981. CGMessage(type_e_mismatch);
  982. left_right_max;
  983. { left must be a class }
  984. if (left.resulttype^.deftype<>objectdef) or
  985. not(is_class(left.resulttype)) then
  986. CGMessage(type_e_mismatch);
  987. { the operands must be related }
  988. if (not(pobjectdef(left.resulttype)^.is_related(
  989. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  990. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  991. pobjectdef(left.resulttype)))) then
  992. CGMessage(type_e_mismatch);
  993. location.loc:=LOC_FLAGS;
  994. resulttype:=booldef;
  995. end;
  996. {*****************************************************************************
  997. TASNODE
  998. *****************************************************************************}
  999. constructor tasnode.create(l,r : tnode);
  1000. begin
  1001. inherited create(asn,l,r);
  1002. end;
  1003. function tasnode.pass_1 : tnode;
  1004. begin
  1005. pass_1:=nil;
  1006. firstpass(right);
  1007. set_varstate(right,true);
  1008. firstpass(left);
  1009. set_varstate(left,true);
  1010. if codegenerror then
  1011. exit;
  1012. if (right.resulttype^.deftype<>classrefdef) then
  1013. CGMessage(type_e_mismatch);
  1014. left_right_max;
  1015. { left must be a class }
  1016. if (left.resulttype^.deftype<>objectdef) or
  1017. not(is_class(left.resulttype)) then
  1018. CGMessage(type_e_mismatch);
  1019. { the operands must be related }
  1020. if (not(pobjectdef(left.resulttype)^.is_related(
  1021. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1022. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1023. pobjectdef(left.resulttype)))) then
  1024. CGMessage(type_e_mismatch);
  1025. set_location(location,left.location);
  1026. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1027. end;
  1028. begin
  1029. ctypeconvnode:=ttypeconvnode;
  1030. casnode:=tasnode;
  1031. cisnode:=tisnode;
  1032. end.
  1033. {
  1034. $Log$
  1035. Revision 1.10 2000-11-04 14:25:20 florian
  1036. + merged Attila's changes for interfaces, not tested yet
  1037. Revision 1.9 2000/10/31 22:02:48 peter
  1038. * symtable splitted, no real code changes
  1039. Revision 1.8 2000/10/14 21:52:55 peter
  1040. * fixed memory leaks
  1041. Revision 1.7 2000/10/14 10:14:50 peter
  1042. * moehrendorf oct 2000 rewrite
  1043. Revision 1.6 2000/10/01 19:48:24 peter
  1044. * lot of compile updates for cg11
  1045. Revision 1.5 2000/09/28 19:49:52 florian
  1046. *** empty log message ***
  1047. Revision 1.4 2000/09/27 18:14:31 florian
  1048. * fixed a lot of syntax errors in the n*.pas stuff
  1049. Revision 1.3 2000/09/26 20:06:13 florian
  1050. * hmm, still a lot of work to get things compilable
  1051. Revision 1.2 2000/09/26 14:59:34 florian
  1052. * more conversion work done
  1053. Revision 1.1 2000/09/25 15:37:14 florian
  1054. * more fixes
  1055. }