ncnv.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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,nld;
  23. type
  24. ttypeconvnode = class(tunarynode)
  25. convtype : 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) : ttypeconvnode;
  68. procedure arrayconstructor_to_set(var p : tarrayconstructnode);
  69. implementation
  70. uses
  71. globtype,systems,tokens,
  72. cutils,cobjects,verbose,globals,
  73. symconst,aasm,types,ncon,ncal,
  74. nset,nadd,
  75. {$ifdef newcg}
  76. cgbase,
  77. {$else newcg}
  78. hcodegen,
  79. {$endif newcg}
  80. htypechk,pass_1,cpubase;
  81. function gentypeconvnode(node : tnode;t : pdef) : ttypeconvnode;
  82. begin
  83. gentypeconvnode:=ctypeconvnode.create(node,t);
  84. end;
  85. {*****************************************************************************
  86. Array constructor to Set Conversion
  87. *****************************************************************************}
  88. procedure arrayconstructor_to_set(var p : tarrayconstructnode);
  89. var
  90. constp : tsetconstnode;
  91. buildp,
  92. p2,p3,p4 : tnode;
  93. pd : pdef;
  94. constset : pconstset;
  95. constsetlo,
  96. constsethi : longint;
  97. procedure update_constsethi(p:pdef);
  98. begin
  99. if ((p^.deftype=orddef) and
  100. (porddef(p)^.high>=constsethi)) then
  101. begin
  102. constsethi:=porddef(p)^.high;
  103. if pd=nil then
  104. begin
  105. if (constsethi>255) or
  106. (porddef(p)^.low<0) then
  107. pd:=u8bitdef
  108. else
  109. pd:=p;
  110. end;
  111. if constsethi>255 then
  112. constsethi:=255;
  113. end
  114. else if ((p^.deftype=enumdef) and
  115. (penumdef(p)^.max>=constsethi)) then
  116. begin
  117. if pd=nil then
  118. pd:=p;
  119. constsethi:=penumdef(p)^.max;
  120. end;
  121. end;
  122. procedure do_set(pos : longint);
  123. var
  124. mask,l : longint;
  125. begin
  126. if (pos>255) or (pos<0) then
  127. Message(parser_e_illegal_set_expr);
  128. if pos>constsethi then
  129. constsethi:=pos;
  130. if pos<constsetlo then
  131. constsetlo:=pos;
  132. l:=pos shr 3;
  133. mask:=1 shl (pos mod 8);
  134. { do we allow the same twice }
  135. if (constset^[l] and mask)<>0 then
  136. Message(parser_e_illegal_set_expr);
  137. constset^[l]:=constset^[l] or mask;
  138. end;
  139. var
  140. l : longint;
  141. lr,hr : longint;
  142. begin
  143. new(constset);
  144. FillChar(constset^,sizeof(constset^),0);
  145. pd:=nil;
  146. constsetlo:=0;
  147. constsethi:=0;
  148. constp:=csetconstnode.create(nil,nil);
  149. constp.value_set:=constset;
  150. if assigned(p.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 p.left.nodetype=arrayconstructrangen then
  157. begin
  158. p2:=tarrayconstructorrangenode(p.left).left;
  159. p3:=tarrayconstructorrangenode(p.left).right;
  160. tarrayconstructorrangenode(p.left).left:=nil;
  161. tarrayconstructorrangenode(p.left).right:=nil;
  162. { node is not used anymore }
  163. p.left.free;
  164. end
  165. else
  166. begin
  167. p2:=p.left;
  168. p3:=nil;
  169. end;
  170. firstpass(p2);
  171. if assigned(p3) then
  172. firstpass(p3);
  173. if codegenerror then
  174. break;
  175. case p2.resulttype^.deftype of
  176. enumdef,
  177. orddef:
  178. begin
  179. getrange(p2.resulttype,lr,hr);
  180. if assigned(p3) then
  181. begin
  182. { this isn't good, you'll get problems with
  183. type t010 = 0..10;
  184. ts = set of t010;
  185. var s : ts;b : t010
  186. begin s:=[1,2,b]; end.
  187. if is_integer(p3^.resulttype) then
  188. begin
  189. p3:=gentypeconvnode(p3,u8bitdef);
  190. firstpass(p3);
  191. end;
  192. }
  193. if assigned(pd) and not(is_equal(pd,p3.resulttype)) then
  194. begin
  195. aktfilepos:=p3.fileinfo;
  196. CGMessage(type_e_typeconflict_in_set);
  197. end
  198. else
  199. begin
  200. if (p2.nodetype=ordconstn) and (p3.nodetype=ordconstn) then
  201. begin
  202. if not(is_integer(p3.resulttype)) then
  203. pd:=p3.resulttype
  204. else
  205. begin
  206. p3:=gentypeconvnode(p3,u8bitdef);
  207. p2:=gentypeconvnode(p2,u8bitdef);
  208. firstpass(p2);
  209. firstpass(p3);
  210. end;
  211. for l:=tordconstnode(p2).value to tordconstnode(p3).value do
  212. do_set(l);
  213. p2.free;
  214. p3.free;
  215. end
  216. else
  217. begin
  218. update_constsethi(p2.resulttype);
  219. p2:=gentypeconvnode(p2,pd);
  220. firstpass(p2);
  221. update_constsethi(p3.resulttype);
  222. p3:=gentypeconvnode(p3,pd);
  223. firstpass(p3);
  224. if assigned(pd) then
  225. p3:=gentypeconvnode(p3,pd)
  226. else
  227. p3:=gentypeconvnode(p3,u8bitdef);
  228. firstpass(p3);
  229. p4:=csetelementnode.create(p2,p3);
  230. end;
  231. end;
  232. end
  233. else
  234. begin
  235. { Single value }
  236. if p2.nodetype=ordconstn then
  237. begin
  238. if not(is_integer(p2.resulttype)) then
  239. update_constsethi(p2.resulttype)
  240. else
  241. begin
  242. p2:=gentypeconvnode(p2,u8bitdef);
  243. firstpass(p2);
  244. end;
  245. do_set(tordconstnode(p2).value);
  246. p2.free;
  247. end
  248. else
  249. begin
  250. update_constsethi(p2.resulttype);
  251. if assigned(pd) then
  252. p2:=gentypeconvnode(p2,pd)
  253. else
  254. p2:=gentypeconvnode(p2,u8bitdef);
  255. firstpass(p2);
  256. p4:=csetelementnode.create(p2,nil);
  257. end;
  258. end;
  259. end;
  260. stringdef : begin
  261. { if we've already set elements which are constants }
  262. { throw an error }
  263. if ((pd=nil) and assigned(buildp)) or
  264. not(is_equal(pd,cchardef)) then
  265. CGMessage(type_e_typeconflict_in_set)
  266. else
  267. for l:=1 to length(pstring(tstringconstnode(p2).value_str)^) do
  268. do_set(ord(pstring(tstringconstnode(p2).value_str)^[l]));
  269. if pd=nil then
  270. pd:=cchardef;
  271. p2.free;
  272. end;
  273. else
  274. CGMessage(type_e_ordinal_expr_expected);
  275. end;
  276. { insert the set creation tree }
  277. if assigned(p4) then
  278. buildp:=caddnode.create(addn,buildp,p4);
  279. { load next and dispose current node }
  280. p2:=p;
  281. p:=tarrayconstructnode(p.right);
  282. tarrayconstructnode(p2).right:=nil;
  283. p2.free;
  284. end;
  285. if (pd=nil) then
  286. begin
  287. pd:=u8bitdef;
  288. constsethi:=255;
  289. end;
  290. end
  291. else
  292. begin
  293. { empty set [], only remove node }
  294. p.free;
  295. end;
  296. { set the initial set type }
  297. constp.resulttype:=new(psetdef,init(pd,constsethi));
  298. { set the new tree }
  299. p:=tarrayconstructnode(buildp);
  300. end;
  301. {*****************************************************************************
  302. TTYPECONVNODE
  303. *****************************************************************************}
  304. constructor ttypeconvnode.create(node : tnode;t : pdef);
  305. begin
  306. inherited create(typeconvn,node);
  307. convtype:=tc_not_possible;
  308. resulttype:=t;
  309. set_file_line(node);
  310. end;
  311. function ttypeconvnode.getcopy : tnode;
  312. var
  313. n : ttypeconvnode;
  314. begin
  315. n:=ttypeconvnode(inherited getcopy);
  316. n.convtype:=convtype;
  317. getcopy:=n;
  318. end;
  319. function ttypeconvnode.first_int_to_int : tnode;
  320. begin
  321. first_int_to_int:=nil;
  322. if (left.location.loc<>LOC_REGISTER) and
  323. (resulttype^.size>left.resulttype^.size) then
  324. location.loc:=LOC_REGISTER;
  325. if is_64bitint(resulttype) then
  326. registers32:=max(registers32,2)
  327. else
  328. registers32:=max(registers32,1);
  329. end;
  330. function ttypeconvnode.first_cstring_to_pchar : tnode;
  331. begin
  332. first_cstring_to_pchar:=nil;
  333. registers32:=1;
  334. location.loc:=LOC_REGISTER;
  335. end;
  336. function ttypeconvnode.first_string_to_chararray : tnode;
  337. begin
  338. first_string_to_chararray:=nil;
  339. registers32:=1;
  340. location.loc:=LOC_REGISTER;
  341. end;
  342. function ttypeconvnode.first_string_to_string : tnode;
  343. var
  344. t : 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<>arrayconstructn then
  599. internalerror(5546);
  600. { remove typeconv node }
  601. hp:=left;
  602. left:=nil;
  603. { create a set constructor tree }
  604. // !!!!!!!arrayconstructor_to_set(hp);
  605. internalerror(2609001);
  606. {$warning FIX ME !!!!!!!!}
  607. { now firstpass the set }
  608. firstpass(hp);
  609. first_arrayconstructor_to_set:=hp;
  610. end;
  611. function ttypeconvnode.call_helper(c : tconverttype) : tnode;
  612. {$warning FIX ME !!!!!!!!!}
  613. {
  614. const
  615. firstconvert : array[tconverttype] of pointer = (
  616. @ttypeconvnode.first_nothing), {equal}
  617. @ttypeconvnode.first_nothing, {not_possible}
  618. @ttypeconvnode.first_string_to_string,
  619. @ttypeconvnode.first_char_to_string,
  620. @ttypeconvnode.first_pchar_to_string,
  621. @ttypeconvnode.first_cchar_to_pchar,
  622. @ttypeconvnode.first_cstring_to_pchar,
  623. @ttypeconvnode.first_ansistring_to_pchar,
  624. @ttypeconvnode.first_string_to_chararray,
  625. @ttypeconvnode.first_chararray_to_string,
  626. @ttypeconvnode.first_array_to_pointer,
  627. @ttypeconvnode.first_pointer_to_array,
  628. @ttypeconvnode.first_int_to_int,
  629. @ttypeconvnode.first_int_to_bool,
  630. @ttypeconvnode.first_bool_to_bool,
  631. @ttypeconvnode.first_bool_to_int,
  632. @ttypeconvnode.first_real_to_real,
  633. @ttypeconvnode.first_int_to_real,
  634. @ttypeconvnode.first_int_to_fix,
  635. @ttypeconvnode.first_real_to_fix,
  636. @ttypeconvnode.first_fix_to_real,
  637. @ttypeconvnode.first_proc_to_procvar,
  638. @ttypeconvnode.first_arrayconstructor_to_set,
  639. @ttypeconvnode.first_load_smallset,
  640. @ttypeconvnode.first_cord_to_pointer
  641. );
  642. }
  643. type
  644. tprocedureofobject = function : tnode of object;
  645. var
  646. r : packed record
  647. proc : pointer;
  648. obj : pointer;
  649. end;
  650. begin
  651. { this is a little bit dirty but it works }
  652. { and should be quite portable too }
  653. // !!!! r.proc:=firstconvert[c];
  654. {$warning FIX ME !!!!!}
  655. internalerror(2609002);
  656. r.obj:=self;
  657. call_helper:=tprocedureofobject(r){$ifdef FPC}();{$endif FPC}
  658. end;
  659. function ttypeconvnode.pass_1 : tnode;
  660. var
  661. hp : tnode;
  662. aprocdef : pprocdef;
  663. begin
  664. pass_1:=nil;
  665. aprocdef:=nil;
  666. { if explicite type cast, then run firstpass }
  667. if (nf_explizit in flags) or not assigned(left.resulttype) then
  668. firstpass(left);
  669. if (left.nodetype=typen) and (left.resulttype=generrordef) then
  670. begin
  671. codegenerror:=true;
  672. Message(parser_e_no_type_not_allowed_here);
  673. end;
  674. if codegenerror then
  675. begin
  676. resulttype:=generrordef;
  677. exit;
  678. end;
  679. if not assigned(left.resulttype) then
  680. begin
  681. codegenerror:=true;
  682. internalerror(52349);
  683. exit;
  684. end;
  685. { load the value_str from the left part }
  686. registers32:=left.registers32;
  687. registersfpu:=left.registersfpu;
  688. {$ifdef SUPPORT_MMX}
  689. registersmmx:=left.registersmmx;
  690. {$endif}
  691. set_location(location,left.location);
  692. { remove obsolete type conversions }
  693. if is_equal(left.resulttype,resulttype) then
  694. begin
  695. { becuase is_equal only checks the basetype for sets we need to
  696. check here if we are loading a smallset into a normalset }
  697. if (resulttype^.deftype=setdef) and
  698. (left.resulttype^.deftype=setdef) and
  699. (psetdef(resulttype)^.settype<>smallset) and
  700. (psetdef(left.resulttype)^.settype=smallset) then
  701. begin
  702. { try to define the set as a normalset if it's a constant set }
  703. if left.nodetype=setconstn then
  704. begin
  705. resulttype:=left.resulttype;
  706. psetdef(resulttype)^.settype:=normset
  707. end
  708. else
  709. convtype:=tc_load_smallset;
  710. exit;
  711. end
  712. else
  713. begin
  714. pass_1:=left;
  715. left.resulttype:=resulttype;
  716. left:=nil;
  717. exit;
  718. end;
  719. end;
  720. aprocdef:=assignment_overloaded(left.resulttype,resulttype);
  721. if assigned(aprocdef) then
  722. begin
  723. procinfo^.flags:=procinfo^.flags or pi_do_call;
  724. hp:=gencallnode(overloaded_operators[_assignment],nil);
  725. { tell explicitly which def we must use !! (PM) }
  726. tcallnode(hp).procdefinition:=aprocdef;
  727. tcallnode(hp).left:=gencallparanode(left,nil);
  728. left:=nil;
  729. firstpass(hp);
  730. pass_1:=hp;
  731. exit;
  732. end;
  733. if isconvertable(left.resulttype,resulttype,convtype,left.nodetype,nf_explizit in flags)=0 then
  734. begin
  735. {Procedures have a resulttype of voiddef and functions of their
  736. own resulttype. They will therefore always be incompatible with
  737. a procvar. Because isconvertable cannot check for procedures we
  738. use an extra check for them.}
  739. if (m_tp_procvar in aktmodeswitches) then
  740. begin
  741. if (resulttype^.deftype=procvardef) and
  742. (is_procsym_load(left) or is_procsym_call(left)) then
  743. begin
  744. if is_procsym_call(left) then
  745. begin
  746. {if left.right=nil then
  747. begin}
  748. if (tcallnode(left).symtableprocentry^.owner^.symtabletype=objectsymtable){ and
  749. (pobjectdef(left.symtableprocentry^.owner^.defowner)^.is_class) }then
  750. hp:=genloadmethodcallnode(pprocsym(tcallnode(left).symtableprocentry),
  751. tcallnode(left).symtableproc,
  752. tcallnode(left).methodpointer.getcopy)
  753. else
  754. hp:=genloadcallnode(pprocsym(tcallnode(left).symtableprocentry),
  755. tcallnode(left).symtableproc);
  756. left.free;
  757. firstpass(hp);
  758. left:=hp;
  759. aprocdef:=pprocdef(left.resulttype);
  760. (* end
  761. else
  762. begin
  763. left.right.nodetype:=loadn;
  764. left.right.symtableentry:=left.right.symtableentry;
  765. left.right.resulttype:=pvarsym(left.symtableentry)^.definition;
  766. hp:=left.right;
  767. putnode(left);
  768. left:=hp;
  769. { should we do that ? }
  770. firstpass(left);
  771. if not is_equal(left.resulttype,resulttype) then
  772. begin
  773. CGMessage(type_e_mismatch);
  774. exit;
  775. end
  776. else
  777. begin
  778. hp:=p;
  779. p:=left;
  780. resulttype:=hp.resulttype;
  781. putnode(hp);
  782. exit;
  783. end;
  784. end; *)
  785. end
  786. else
  787. begin
  788. if (left.nodetype<>addrn) then
  789. aprocdef:=pprocsym(tloadnode(left).symtableentry)^.definition;
  790. end;
  791. convtype:=tc_proc_2_procvar;
  792. { Now check if the procedure we are going to assign to
  793. the procvar, is compatible with the procvar's type }
  794. if assigned(aprocdef) then
  795. begin
  796. if not proc_to_procvar_equal(aprocdef,pprocvardef(resulttype)) then
  797. CGMessage2(type_e_incompatible_types,aprocdef^.typename,resulttype^.typename);
  798. pass_1:=call_helper(convtype);
  799. end
  800. else
  801. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  802. exit;
  803. end;
  804. end;
  805. if nf_explizit in flags then
  806. begin
  807. { check if the result could be in a register }
  808. if not(resulttype^.is_intregable) and
  809. not(resulttype^.is_fpuregable) then
  810. make_not_regable(left);
  811. { boolean to byte are special because the
  812. location can be different }
  813. if is_integer(resulttype) and
  814. is_boolean(left.resulttype) then
  815. begin
  816. convtype:=tc_bool_2_int;
  817. pass_1:=call_helper(convtype);
  818. exit;
  819. end;
  820. { ansistring to pchar }
  821. if is_pchar(resulttype) and
  822. is_ansistring(left.resulttype) then
  823. begin
  824. convtype:=tc_ansistring_2_pchar;
  825. pass_1:=call_helper(convtype);
  826. exit;
  827. end;
  828. { do common tc_equal cast }
  829. convtype:=tc_equal;
  830. { enum to ordinal will always be s32bit }
  831. if (left.resulttype^.deftype=enumdef) and
  832. is_ordinal(resulttype) then
  833. begin
  834. if left.nodetype=ordconstn then
  835. begin
  836. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  837. firstpass(hp);
  838. pass_1:=hp;
  839. exit;
  840. end
  841. else
  842. begin
  843. if isconvertable(s32bitdef,resulttype,convtype,ordconstn,false)=0 then
  844. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  845. end;
  846. end
  847. { ordinal to enumeration }
  848. else
  849. if (resulttype^.deftype=enumdef) and
  850. is_ordinal(left.resulttype) then
  851. begin
  852. if left.nodetype=ordconstn then
  853. begin
  854. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  855. firstpass(hp);
  856. pass_1:=hp;
  857. exit;
  858. end
  859. else
  860. begin
  861. if IsConvertable(left.resulttype,s32bitdef,convtype,ordconstn,false)=0 then
  862. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  863. end;
  864. end
  865. { nil to ordinal node }
  866. else if is_ordinal(resulttype) and
  867. (left.nodetype=niln) then
  868. begin
  869. hp:=genordinalconstnode(0,resulttype);
  870. firstpass(hp);
  871. pass_1:=hp;
  872. exit;
  873. end
  874. {Are we typecasting an ordconst to a char?}
  875. else
  876. if is_char(resulttype) and
  877. is_ordinal(left.resulttype) then
  878. begin
  879. if left.nodetype=ordconstn then
  880. begin
  881. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  882. firstpass(hp);
  883. pass_1:=hp;
  884. exit;
  885. end
  886. else
  887. begin
  888. if IsConvertable(left.resulttype,u8bitdef,convtype,ordconstn,false)=0 then
  889. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  890. end;
  891. end
  892. { Are we char to ordinal }
  893. else
  894. if is_char(left.resulttype) and
  895. is_ordinal(resulttype) then
  896. begin
  897. if left.nodetype=ordconstn then
  898. begin
  899. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  900. firstpass(hp);
  901. pass_1:=hp;
  902. exit;
  903. end
  904. else
  905. begin
  906. if IsConvertable(u8bitdef,resulttype,convtype,ordconstn,false)=0 then
  907. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  908. end;
  909. end
  910. { only if the same size or formal def }
  911. { why do we allow typecasting of voiddef ?? (PM) }
  912. else
  913. begin
  914. if not(
  915. (left.resulttype^.deftype=formaldef) or
  916. (left.resulttype^.size=resulttype^.size) or
  917. (is_equal(left.resulttype,voiddef) and
  918. (left.nodetype=derefn))
  919. ) then
  920. CGMessage(cg_e_illegal_type_conversion);
  921. if ((left.resulttype^.deftype=orddef) and
  922. (resulttype^.deftype=pointerdef)) or
  923. ((resulttype^.deftype=orddef) and
  924. (left.resulttype^.deftype=pointerdef))
  925. {$ifdef extdebug}and (firstpasscount=0){$endif} then
  926. CGMessage(cg_d_pointer_to_longint_conv_not_portable);
  927. end;
  928. { the conversion into a strutured type is only }
  929. { possible, if the source is no register }
  930. if ((resulttype^.deftype in [recorddef,stringdef,arraydef]) or
  931. ((resulttype^.deftype=objectdef) and not(pobjectdef(resulttype)^.is_class))
  932. ) and (left.location.loc in [LOC_REGISTER,LOC_CREGISTER]) { and
  933. it also works if the assignment is overloaded
  934. YES but this code is not executed if assignment is overloaded (PM)
  935. not assigned(assignment_overloaded(left.resulttype,resulttype))} then
  936. CGMessage(cg_e_illegal_type_conversion);
  937. end
  938. else
  939. CGMessage2(type_e_incompatible_types,left.resulttype^.typename,resulttype^.typename);
  940. end;
  941. { tp7 procvar support, when right is not a procvardef and we got a
  942. loadn of a procvar then convert to a calln, the check for the
  943. result is already done in is_convertible, also no conflict with
  944. @procvar is here because that has an extra addrn }
  945. if (m_tp_procvar in aktmodeswitches) and
  946. (resulttype^.deftype<>procvardef) and
  947. (left.resulttype^.deftype=procvardef) and
  948. (left.nodetype=loadn) then
  949. begin
  950. hp:=gencallnode(nil,nil);
  951. tcallnode(hp).right:=left;
  952. firstpass(hp);
  953. left:=hp;
  954. end;
  955. { ordinal contants can be directly converted }
  956. { but not int64/qword }
  957. if (left.nodetype=ordconstn) and is_ordinal(resulttype) and
  958. not(is_64bitint(resulttype)) then
  959. begin
  960. { range checking is done in genordinalconstnode (PFV) }
  961. hp:=genordinalconstnode(tordconstnode(left).value,resulttype);
  962. firstpass(hp);
  963. pass_1:=hp;
  964. exit;
  965. end;
  966. if convtype<>tc_equal then
  967. pass_1:=call_helper(convtype);
  968. end;
  969. {*****************************************************************************
  970. TISNODE
  971. *****************************************************************************}
  972. constructor tisnode.create(l,r : tnode);
  973. begin
  974. inherited create(isn,l,r);
  975. end;
  976. function tisnode.pass_1 : tnode;
  977. begin
  978. pass_1:=nil;
  979. firstpass(left);
  980. left.set_varstate(true);
  981. firstpass(right);
  982. right.set_varstate(true);
  983. if codegenerror then
  984. exit;
  985. if (right.resulttype^.deftype<>classrefdef) then
  986. CGMessage(type_e_mismatch);
  987. left_right_max;
  988. { left must be a class }
  989. if (left.resulttype^.deftype<>objectdef) or
  990. not(pobjectdef(left.resulttype)^.is_class) then
  991. CGMessage(type_e_mismatch);
  992. { the operands must be related }
  993. if (not(pobjectdef(left.resulttype)^.is_related(
  994. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  995. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  996. pobjectdef(left.resulttype)))) then
  997. CGMessage(type_e_mismatch);
  998. location.loc:=LOC_FLAGS;
  999. resulttype:=booldef;
  1000. end;
  1001. {*****************************************************************************
  1002. TASNODE
  1003. *****************************************************************************}
  1004. constructor tasnode.create(l,r : tnode);
  1005. begin
  1006. inherited create(asn,l,r);
  1007. end;
  1008. function tasnode.pass_1 : tnode;
  1009. begin
  1010. pass_1:=nil;
  1011. firstpass(right);
  1012. right.set_varstate(true);
  1013. firstpass(left);
  1014. left.set_varstate(true);
  1015. if codegenerror then
  1016. exit;
  1017. if (right.resulttype^.deftype<>classrefdef) then
  1018. CGMessage(type_e_mismatch);
  1019. left_right_max;
  1020. { left must be a class }
  1021. if (left.resulttype^.deftype<>objectdef) or
  1022. not(pobjectdef(left.resulttype)^.is_class) then
  1023. CGMessage(type_e_mismatch);
  1024. { the operands must be related }
  1025. if (not(pobjectdef(left.resulttype)^.is_related(
  1026. pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)))) and
  1027. (not(pobjectdef(pclassrefdef(right.resulttype)^.pointertype.def)^.is_related(
  1028. pobjectdef(left.resulttype)))) then
  1029. CGMessage(type_e_mismatch);
  1030. set_location(location,left.location);
  1031. resulttype:=pclassrefdef(right.resulttype)^.pointertype.def;
  1032. end;
  1033. begin
  1034. ctypeconvnode:=ttypeconvnode;
  1035. casnode:=tasnode;
  1036. cisnode:=tisnode;
  1037. end.
  1038. {
  1039. $Log$
  1040. Revision 1.5 2000-09-28 19:49:52 florian
  1041. *** empty log message ***
  1042. Revision 1.4 2000/09/27 18:14:31 florian
  1043. * fixed a lot of syntax errors in the n*.pas stuff
  1044. Revision 1.3 2000/09/26 20:06:13 florian
  1045. * hmm, still a lot of work to get things compilable
  1046. Revision 1.2 2000/09/26 14:59:34 florian
  1047. * more conversion work done
  1048. Revision 1.1 2000/09/25 15:37:14 florian
  1049. * more fixes
  1050. }