ncnv.pas 44 KB

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