ncnv.pas 43 KB

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