nutils.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nutils;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,symsym,symbase,symtable,
  23. node;
  24. const
  25. NODE_COMPLEXITY_INF = 255;
  26. type
  27. { resultdef of functions that process on all nodes in a (sub)tree }
  28. foreachnoderesult = (
  29. { false, continue recursion }
  30. fen_false,
  31. { false, stop recursion }
  32. fen_norecurse_false,
  33. { true, continue recursion }
  34. fen_true,
  35. { true, stop recursion }
  36. fen_norecurse_true
  37. );
  38. tforeachprocmethod = (pm_preprocess,pm_postprocess);
  39. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  40. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  41. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  42. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  43. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  44. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  45. { checks if the given node tree contains only nodes of the given type,
  46. if this isn't the case, an ie is thrown
  47. }
  48. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  49. procedure load_procvar_from_calln(var p1:tnode);
  50. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  51. function get_high_value_sym(vs: tparavarsym):tsym; { marking it as inline causes IE 200311075 during loading from ppu file }
  52. function load_high_value_node(vs:tparavarsym):tnode;
  53. function load_self_node:tnode;
  54. function load_result_node:tnode;
  55. function load_self_pointer_node:tnode;
  56. function load_vmt_pointer_node:tnode;
  57. function is_self_node(p:tnode):boolean;
  58. function call_fail_node:tnode;
  59. function initialize_data_node(p:tnode):tnode;
  60. function finalize_data_node(p:tnode):tnode;
  61. function node_complexity(p: tnode): cardinal;
  62. function node_resources_fpu(p: tnode): cardinal;
  63. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  64. { tries to simplify the given node }
  65. procedure dosimplify(var n : tnode);
  66. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  67. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  68. implementation
  69. uses
  70. cutils,verbose,constexp,globals,
  71. symconst,symdef,
  72. defutil,defcmp,
  73. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,
  74. cpubase,cgbase,procinfo,
  75. pass_1;
  76. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  77. function process_children(res : boolean) : boolean;
  78. var
  79. i: longint;
  80. begin
  81. result:=res;
  82. case n.nodetype of
  83. asn:
  84. if assigned(tasnode(n).call) then
  85. begin
  86. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  87. exit
  88. end;
  89. calln:
  90. begin
  91. result := foreachnode(procmethod,tcallnode(n).callinitblock,f,arg) or result;
  92. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  93. result := foreachnode(procmethod,tcallnode(n).callcleanupblock,f,arg) or result;
  94. end;
  95. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  96. begin
  97. { not in one statement, won't work because of b- }
  98. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  99. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  100. end;
  101. raisen:
  102. { frame tree }
  103. result := foreachnode(traisenode(n).third,f,arg) or result;
  104. casen:
  105. begin
  106. for i := 0 to tcasenode(n).blocks.count-1 do
  107. if assigned(tcasenode(n).blocks[i]) then
  108. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  109. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  110. end;
  111. end;
  112. if n.inheritsfrom(tbinarynode) then
  113. begin
  114. { first process the "payload" of statementnodes }
  115. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  116. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  117. end
  118. else if n.inheritsfrom(tunarynode) then
  119. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  120. end;
  121. begin
  122. result := false;
  123. if not assigned(n) then
  124. exit;
  125. if procmethod=pm_preprocess then
  126. result:=process_children(result);
  127. case f(n,arg) of
  128. fen_norecurse_false:
  129. exit;
  130. fen_norecurse_true:
  131. begin
  132. result := true;
  133. exit;
  134. end;
  135. fen_true:
  136. result := true;
  137. { result is already false
  138. fen_false:
  139. result := false; }
  140. end;
  141. if procmethod=pm_postprocess then
  142. result:=process_children(result);
  143. end;
  144. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  145. begin
  146. result:=foreachnode(pm_postprocess,n,f,arg);
  147. end;
  148. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  149. function process_children(res : boolean) : boolean;
  150. var
  151. i: longint;
  152. begin
  153. result:=res;
  154. case n.nodetype of
  155. asn:
  156. if assigned(tasnode(n).call) then
  157. begin
  158. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  159. exit
  160. end;
  161. calln:
  162. begin
  163. result := foreachnodestatic(procmethod,tcallnode(n).callinitblock,f,arg) or result;
  164. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  165. result := foreachnodestatic(procmethod,tcallnode(n).callcleanupblock,f,arg) or result;
  166. end;
  167. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  168. begin
  169. { not in one statement, won't work because of b- }
  170. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  171. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  172. end;
  173. raisen:
  174. { frame tree }
  175. result := foreachnodestatic(traisenode(n).third,f,arg) or result;
  176. casen:
  177. begin
  178. for i := 0 to tcasenode(n).blocks.count-1 do
  179. if assigned(tcasenode(n).blocks[i]) then
  180. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  181. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  182. end;
  183. end;
  184. if n.inheritsfrom(tbinarynode) then
  185. begin
  186. { first process the "payload" of statementnodes }
  187. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  188. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  189. end
  190. else if n.inheritsfrom(tunarynode) then
  191. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  192. end;
  193. begin
  194. result := false;
  195. if not assigned(n) then
  196. exit;
  197. if procmethod=pm_preprocess then
  198. result:=process_children(result);
  199. case f(n,arg) of
  200. fen_norecurse_false:
  201. exit;
  202. fen_norecurse_true:
  203. begin
  204. result := true;
  205. exit;
  206. end;
  207. fen_true:
  208. result := true;
  209. { result is already false
  210. fen_false:
  211. result := false; }
  212. end;
  213. if procmethod=pm_postprocess then
  214. result:=process_children(result);
  215. end;
  216. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  217. begin
  218. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  219. end;
  220. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  221. begin
  222. if not(n.nodetype in pnodetypeset(arg)^) then
  223. internalerror(200610141);
  224. result:=fen_true;
  225. end;
  226. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  227. begin
  228. foreachnodestatic(n,@do_check,@typeset);
  229. end;
  230. procedure load_procvar_from_calln(var p1:tnode);
  231. var
  232. p2 : tnode;
  233. begin
  234. if p1.nodetype<>calln then
  235. internalerror(200212251);
  236. { was it a procvar, then we simply remove the calln and
  237. reuse the right }
  238. if assigned(tcallnode(p1).right) then
  239. begin
  240. p2:=tcallnode(p1).right;
  241. tcallnode(p1).right:=nil;
  242. end
  243. else
  244. begin
  245. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  246. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  247. { when the methodpointer is typen we've something like:
  248. tobject.create. Then only the address is needed of the
  249. method without a self pointer }
  250. if assigned(tcallnode(p1).methodpointer) and
  251. (tcallnode(p1).methodpointer.nodetype<>typen) then
  252. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  253. end;
  254. typecheckpass(p2);
  255. p1.free;
  256. p1:=p2;
  257. end;
  258. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  259. var
  260. hp : tnode;
  261. begin
  262. result:=false;
  263. if (p1.resultdef.typ<>procvardef) or
  264. (tponly and
  265. not(m_tp_procvar in current_settings.modeswitches)) then
  266. exit;
  267. { ignore vecn,subscriptn }
  268. hp:=p1;
  269. repeat
  270. case hp.nodetype of
  271. vecn,
  272. derefn,
  273. typeconvn,
  274. subscriptn :
  275. hp:=tunarynode(hp).left;
  276. else
  277. break;
  278. end;
  279. until false;
  280. { a tempref is used when it is loaded from a withsymtable }
  281. if (hp.nodetype in [calln,loadn,temprefn]) then
  282. begin
  283. hp:=ccallnode.create_procvar(nil,p1);
  284. typecheckpass(hp);
  285. p1:=hp;
  286. result:=true;
  287. end;
  288. end;
  289. function get_high_value_sym(vs: tparavarsym):tsym;
  290. begin
  291. result := tsym(vs.owner.Find('high'+vs.name));
  292. end;
  293. function get_local_or_para_sym(const aname:string):tsym;
  294. var
  295. pd : tprocdef;
  296. begin
  297. { we can't use searchsym here, because the
  298. symtablestack is not fully setup when pass1
  299. is run for nested procedures }
  300. pd:=current_procinfo.procdef;
  301. repeat
  302. result := tsym(pd.localst.Find(aname));
  303. if assigned(result) then
  304. break;
  305. result := tsym(pd.parast.Find(aname));
  306. if assigned(result) then
  307. break;
  308. { try the parent of a nested function }
  309. if assigned(pd.owner.defowner) and
  310. (pd.owner.defowner.typ=procdef) then
  311. pd:=tprocdef(pd.owner.defowner)
  312. else
  313. break;
  314. until false;
  315. end;
  316. function load_high_value_node(vs:tparavarsym):tnode;
  317. var
  318. srsym : tsym;
  319. begin
  320. result:=nil;
  321. srsym:=get_high_value_sym(vs);
  322. if assigned(srsym) then
  323. begin
  324. result:=cloadnode.create(srsym,vs.owner);
  325. typecheckpass(result);
  326. end
  327. else
  328. CGMessage(parser_e_illegal_expression);
  329. end;
  330. function load_self_node:tnode;
  331. var
  332. srsym : tsym;
  333. begin
  334. result:=nil;
  335. srsym:=get_local_or_para_sym('self');
  336. if assigned(srsym) then
  337. begin
  338. result:=cloadnode.create(srsym,srsym.owner);
  339. include(result.flags,nf_is_self);
  340. end
  341. else
  342. begin
  343. result:=cerrornode.create;
  344. CGMessage(parser_e_illegal_expression);
  345. end;
  346. typecheckpass(result);
  347. end;
  348. function load_result_node:tnode;
  349. var
  350. srsym : tsym;
  351. begin
  352. result:=nil;
  353. srsym:=get_local_or_para_sym('result');
  354. if assigned(srsym) then
  355. result:=cloadnode.create(srsym,srsym.owner)
  356. else
  357. begin
  358. result:=cerrornode.create;
  359. CGMessage(parser_e_illegal_expression);
  360. end;
  361. typecheckpass(result);
  362. end;
  363. function load_self_pointer_node:tnode;
  364. var
  365. srsym : tsym;
  366. begin
  367. result:=nil;
  368. srsym:=get_local_or_para_sym('self');
  369. if assigned(srsym) then
  370. begin
  371. result:=cloadnode.create(srsym,srsym.owner);
  372. include(result.flags,nf_load_self_pointer);
  373. end
  374. else
  375. begin
  376. result:=cerrornode.create;
  377. CGMessage(parser_e_illegal_expression);
  378. end;
  379. typecheckpass(result);
  380. end;
  381. function load_vmt_pointer_node:tnode;
  382. var
  383. srsym : tsym;
  384. begin
  385. result:=nil;
  386. srsym:=get_local_or_para_sym('vmt');
  387. if assigned(srsym) then
  388. result:=cloadnode.create(srsym,srsym.owner)
  389. else
  390. begin
  391. result:=cerrornode.create;
  392. CGMessage(parser_e_illegal_expression);
  393. end;
  394. typecheckpass(result);
  395. end;
  396. function is_self_node(p:tnode):boolean;
  397. begin
  398. is_self_node:=(p.nodetype=loadn) and
  399. (tloadnode(p).symtableentry.typ=paravarsym) and
  400. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  401. end;
  402. function call_fail_node:tnode;
  403. var
  404. para : tcallparanode;
  405. newstatement : tstatementnode;
  406. srsym : tsym;
  407. begin
  408. result:=internalstatements(newstatement);
  409. { call fail helper and exit normal }
  410. if is_class(current_procinfo.procdef._class) then
  411. begin
  412. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  413. if assigned(srsym) and
  414. (srsym.typ=procsym) then
  415. begin
  416. { if self<>0 and vmt<>0 then freeinstance }
  417. addstatement(newstatement,cifnode.create(
  418. caddnode.create(andn,
  419. caddnode.create(unequaln,
  420. load_self_pointer_node,
  421. cnilnode.create),
  422. caddnode.create(unequaln,
  423. load_vmt_pointer_node,
  424. cnilnode.create)),
  425. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  426. nil));
  427. end
  428. else
  429. internalerror(200305108);
  430. end
  431. else
  432. if is_object(current_procinfo.procdef._class) then
  433. begin
  434. { parameter 3 : vmt_offset }
  435. { parameter 2 : pointer to vmt }
  436. { parameter 1 : self pointer }
  437. para:=ccallparanode.create(
  438. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  439. ccallparanode.create(
  440. ctypeconvnode.create_internal(
  441. load_vmt_pointer_node,
  442. voidpointertype),
  443. ccallparanode.create(
  444. ctypeconvnode.create_internal(
  445. load_self_pointer_node,
  446. voidpointertype),
  447. nil)));
  448. addstatement(newstatement,
  449. ccallnode.createintern('fpc_help_fail',para));
  450. end
  451. else
  452. internalerror(200305132);
  453. { self:=nil }
  454. addstatement(newstatement,cassignmentnode.create(
  455. load_self_pointer_node,
  456. cnilnode.create));
  457. { exit }
  458. addstatement(newstatement,cexitnode.create(nil));
  459. end;
  460. function initialize_data_node(p:tnode):tnode;
  461. begin
  462. if not assigned(p.resultdef) then
  463. typecheckpass(p);
  464. if is_ansistring(p.resultdef) or
  465. is_widestring(p.resultdef) or
  466. is_interfacecom(p.resultdef) or
  467. is_dynamic_array(p.resultdef) then
  468. begin
  469. result:=cassignmentnode.create(
  470. ctypeconvnode.create_internal(p,voidpointertype),
  471. cnilnode.create
  472. );
  473. end
  474. else
  475. begin
  476. result:=ccallnode.createintern('fpc_initialize',
  477. ccallparanode.create(
  478. caddrnode.create_internal(
  479. crttinode.create(
  480. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  481. ccallparanode.create(
  482. caddrnode.create_internal(p),
  483. nil)));
  484. end;
  485. end;
  486. function finalize_data_node(p:tnode):tnode;
  487. var
  488. newstatement : tstatementnode;
  489. begin
  490. if not assigned(p.resultdef) then
  491. typecheckpass(p);
  492. if is_ansistring(p.resultdef) then
  493. begin
  494. result:=internalstatements(newstatement);
  495. addstatement(newstatement,ccallnode.createintern('fpc_ansistr_decr_ref',
  496. ccallparanode.create(
  497. ctypeconvnode.create_internal(p,voidpointertype),
  498. nil)));
  499. addstatement(newstatement,cassignmentnode.create(
  500. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  501. cnilnode.create
  502. ));
  503. end
  504. else if is_widestring(p.resultdef) then
  505. begin
  506. result:=internalstatements(newstatement);
  507. addstatement(newstatement,ccallnode.createintern('fpc_widestr_decr_ref',
  508. ccallparanode.create(
  509. ctypeconvnode.create_internal(p,voidpointertype),
  510. nil)));
  511. addstatement(newstatement,cassignmentnode.create(
  512. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  513. cnilnode.create
  514. ));
  515. end
  516. else if is_interfacecom(p.resultdef) then
  517. begin
  518. result:=internalstatements(newstatement);
  519. addstatement(newstatement,ccallnode.createintern('fpc_intf_decr_ref',
  520. ccallparanode.create(
  521. ctypeconvnode.create_internal(p,voidpointertype),
  522. nil)));
  523. addstatement(newstatement,cassignmentnode.create(
  524. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  525. cnilnode.create
  526. ));
  527. end
  528. else
  529. result:=ccallnode.createintern('fpc_finalize',
  530. ccallparanode.create(
  531. caddrnode.create_internal(
  532. crttinode.create(
  533. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  534. ccallparanode.create(
  535. caddrnode.create_internal(p),
  536. nil)));
  537. end;
  538. { this function must return a very high value ("infinity") for }
  539. { trees containing a call, the rest can be balanced more or less }
  540. { at will, probably best mainly in terms of required memory }
  541. { accesses }
  542. function node_complexity(p: tnode): cardinal;
  543. begin
  544. result := 0;
  545. while assigned(p) do
  546. begin
  547. case p.nodetype of
  548. temprefn,
  549. loadvmtaddrn,
  550. { main reason for the next one: we can't take the address of }
  551. { loadparentfpnode, so replacing it by a temp which is the }
  552. { address of this node's location and then dereferencing }
  553. { doesn't work. If changed, check whether webtbs/tw0935 }
  554. { still works with nodeinlining (JM) }
  555. loadparentfpn:
  556. begin
  557. result := 1;
  558. exit;
  559. end;
  560. loadn:
  561. begin
  562. { threadvars need a helper call }
  563. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  564. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  565. inc(result,5)
  566. else
  567. inc(result);
  568. if (result >= NODE_COMPLEXITY_INF) then
  569. result := NODE_COMPLEXITY_INF;
  570. exit;
  571. end;
  572. subscriptn:
  573. begin
  574. if is_class_or_interface(tunarynode(p).left.resultdef) then
  575. inc(result);
  576. if (result = NODE_COMPLEXITY_INF) then
  577. exit;
  578. p := tunarynode(p).left;
  579. end;
  580. blockn,
  581. callparan:
  582. p := tunarynode(p).left;
  583. notn,
  584. derefn :
  585. begin
  586. inc(result);
  587. if (result = NODE_COMPLEXITY_INF) then
  588. exit;
  589. p := tunarynode(p).left;
  590. end;
  591. typeconvn:
  592. begin
  593. { may be more complex in some cases }
  594. if not(ttypeconvnode(p).convtype in [tc_equal,tc_int_2_int,tc_bool_2_bool,tc_real_2_real,tc_cord_2_pointer]) then
  595. inc(result);
  596. if (result = NODE_COMPLEXITY_INF) then
  597. exit;
  598. p := tunarynode(p).left;
  599. end;
  600. vecn,
  601. statementn:
  602. begin
  603. inc(result,node_complexity(tbinarynode(p).left));
  604. if (result >= NODE_COMPLEXITY_INF) then
  605. begin
  606. result := NODE_COMPLEXITY_INF;
  607. exit;
  608. end;
  609. p := tbinarynode(p).right;
  610. end;
  611. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  612. shln,shrn,
  613. equaln,unequaln,gtn,gten,ltn,lten,
  614. assignn:
  615. begin
  616. inc(result,node_complexity(tbinarynode(p).left)+1);
  617. if (p.nodetype in [muln,divn,modn]) then
  618. inc(result,5);
  619. if (result >= NODE_COMPLEXITY_INF) then
  620. begin
  621. result := NODE_COMPLEXITY_INF;
  622. exit;
  623. end;
  624. p := tbinarynode(p).right;
  625. end;
  626. tempcreaten,
  627. tempdeleten,
  628. ordconstn,
  629. pointerconstn,
  630. nothingn,
  631. niln:
  632. exit;
  633. else
  634. begin
  635. result := NODE_COMPLEXITY_INF;
  636. exit;
  637. end;
  638. end;
  639. end;
  640. end;
  641. { this function returns an indication how much fpu registers
  642. will be required.
  643. Note: The algorithms need to be pessimistic to prevent a
  644. fpu stack overflow on i386 }
  645. function node_resources_fpu(p: tnode): cardinal;
  646. var
  647. res1,res2,res3 : cardinal;
  648. begin
  649. result:=0;
  650. res1:=0;
  651. res2:=0;
  652. res3:=0;
  653. if p.inheritsfrom(tunarynode) then
  654. begin
  655. if assigned(tunarynode(p).left) then
  656. res1:=node_resources_fpu(tunarynode(p).left);
  657. if p.inheritsfrom(tbinarynode) then
  658. begin
  659. if assigned(tbinarynode(p).right) then
  660. res2:=node_resources_fpu(tbinarynode(p).right);
  661. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  662. res3:=node_resources_fpu(ttertiarynode(p).third)
  663. end;
  664. end;
  665. result:=max(max(res1,res2),res3);
  666. case p.nodetype of
  667. calln:
  668. { it could be a recursive call, so we never really know the number of used fpu registers }
  669. result:=maxfpuregs;
  670. realconstn,
  671. typeconvn,
  672. loadn :
  673. begin
  674. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  675. result:=max(result,1);
  676. end;
  677. assignn,
  678. addn,subn,muln,slashn,
  679. equaln,unequaln,gtn,gten,ltn,lten :
  680. begin
  681. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  682. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  683. result:=max(result,2);
  684. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  685. inc(result);
  686. end;
  687. end;
  688. end;
  689. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  690. begin
  691. result:=fen_true;
  692. n.fileinfo:=pfileposinfo(arg)^;
  693. end;
  694. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  695. begin
  696. foreachnodestatic(n,@setnodefilepos,@filepos);
  697. end;
  698. {$ifdef FPCMT}
  699. threadvar
  700. {$else FPCMT}
  701. var
  702. {$endif FPCMT}
  703. treechanged : boolean;
  704. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  705. var
  706. hn : tnode;
  707. begin
  708. result:=fen_false;
  709. // do_typecheckpass(n);
  710. hn:=n.simplify;
  711. if assigned(hn) then
  712. begin
  713. treechanged:=true;
  714. n.free;
  715. n:=hn;
  716. typecheckpass(n);
  717. end;
  718. end;
  719. { tries to simplify the given node calling the simplify method recursively }
  720. procedure dosimplify(var n : tnode);
  721. begin
  722. repeat
  723. treechanged:=false;
  724. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  725. until not(treechanged);
  726. end;
  727. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  728. var
  729. plist : ppropaccesslistitem;
  730. begin
  731. plist:=pl.firstsym;
  732. while assigned(plist) do
  733. begin
  734. case plist^.sltype of
  735. sl_load :
  736. begin
  737. addsymref(plist^.sym);
  738. if not assigned(st) then
  739. st:=plist^.sym.owner;
  740. { p1 can already contain the loadnode of
  741. the class variable. When there is no tree yet we
  742. may need to load it for with or objects }
  743. if not assigned(p1) then
  744. begin
  745. case st.symtabletype of
  746. withsymtable :
  747. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  748. ObjectSymtable :
  749. p1:=load_self_node;
  750. end;
  751. end;
  752. if assigned(p1) then
  753. p1:=csubscriptnode.create(plist^.sym,p1)
  754. else
  755. p1:=cloadnode.create(plist^.sym,st);
  756. end;
  757. sl_subscript :
  758. begin
  759. addsymref(plist^.sym);
  760. p1:=csubscriptnode.create(plist^.sym,p1);
  761. end;
  762. sl_typeconv :
  763. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  764. sl_absolutetype :
  765. begin
  766. p1:=ctypeconvnode.create(p1,plist^.def);
  767. include(p1.flags,nf_absolute);
  768. end;
  769. sl_vec :
  770. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  771. else
  772. internalerror(200110205);
  773. end;
  774. plist:=plist^.next;
  775. end;
  776. end;
  777. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  778. var
  779. sl : tpropaccesslist;
  780. procedure addnode(p:tnode);
  781. begin
  782. case p.nodetype of
  783. subscriptn :
  784. begin
  785. addnode(tsubscriptnode(p).left);
  786. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  787. end;
  788. typeconvn :
  789. begin
  790. addnode(ttypeconvnode(p).left);
  791. if nf_absolute in ttypeconvnode(p).flags then
  792. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  793. else
  794. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  795. end;
  796. vecn :
  797. begin
  798. addnode(tvecnode(p).left);
  799. if tvecnode(p).right.nodetype=ordconstn then
  800. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  801. else
  802. begin
  803. Message(parser_e_illegal_expression);
  804. { recovery }
  805. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  806. end;
  807. end;
  808. loadn :
  809. sl.addsym(sl_load,tloadnode(p).symtableentry);
  810. else
  811. internalerror(200310282);
  812. end;
  813. end;
  814. begin
  815. sl:=tpropaccesslist.create;
  816. addnode(p1);
  817. result:=sl;
  818. end;
  819. end.