nutils.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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,node;
  23. const
  24. NODE_COMPLEXITY_INF = 255;
  25. type
  26. { resultdef of functions that process on all nodes in a (sub)tree }
  27. foreachnoderesult = (
  28. { false, continue recursion }
  29. fen_false,
  30. { false, stop recursion }
  31. fen_norecurse_false,
  32. { true, continue recursion }
  33. fen_true,
  34. { true, stop recursion }
  35. fen_norecurse_true
  36. );
  37. tforeachprocmethod = (pm_preprocess,pm_postprocess);
  38. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  39. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  40. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  41. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  42. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  43. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  44. { checks if the given node tree contains only nodes of the given type,
  45. if this isn't the case, an ie is thrown
  46. }
  47. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  48. procedure load_procvar_from_calln(var p1:tnode);
  49. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  50. function get_high_value_sym(vs: tparavarsym):tsym; { marking it as inline causes IE 200311075 during loading from ppu file }
  51. function load_high_value_node(vs:tparavarsym):tnode;
  52. function load_self_node:tnode;
  53. function load_result_node:tnode;
  54. function load_self_pointer_node:tnode;
  55. function load_vmt_pointer_node:tnode;
  56. function is_self_node(p:tnode):boolean;
  57. function call_fail_node:tnode;
  58. function initialize_data_node(p:tnode):tnode;
  59. function finalize_data_node(p:tnode):tnode;
  60. function node_complexity(p: tnode): cardinal;
  61. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  62. { tries to simplify the given node }
  63. procedure dosimplify(var n : tnode);
  64. implementation
  65. uses
  66. verbose,constexp,globals,
  67. symconst,symbase,symdef,symtable,
  68. defutil,defcmp,
  69. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,
  70. cgbase,procinfo,
  71. pass_1;
  72. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  73. function process_children(res : boolean) : boolean;
  74. var
  75. i: longint;
  76. begin
  77. result:=res;
  78. case n.nodetype of
  79. asn:
  80. if assigned(tasnode(n).call) then
  81. begin
  82. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  83. exit
  84. end;
  85. calln:
  86. begin
  87. result := foreachnode(procmethod,tcallnode(n).callinitblock,f,arg) or result;
  88. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  89. result := foreachnode(procmethod,tcallnode(n).callcleanupblock,f,arg) or result;
  90. end;
  91. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  92. begin
  93. { not in one statement, won't work because of b- }
  94. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  95. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  96. end;
  97. raisen:
  98. { frame tree }
  99. result := foreachnode(traisenode(n).third,f,arg) or result;
  100. casen:
  101. begin
  102. for i := 0 to tcasenode(n).blocks.count-1 do
  103. if assigned(tcasenode(n).blocks[i]) then
  104. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  105. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  106. end;
  107. end;
  108. if n.inheritsfrom(tbinarynode) then
  109. begin
  110. { first process the "payload" of statementnodes }
  111. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  112. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  113. end
  114. else if n.inheritsfrom(tunarynode) then
  115. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  116. end;
  117. begin
  118. result := false;
  119. if not assigned(n) then
  120. exit;
  121. if procmethod=pm_preprocess then
  122. result:=process_children(result);
  123. case f(n,arg) of
  124. fen_norecurse_false:
  125. exit;
  126. fen_norecurse_true:
  127. begin
  128. result := true;
  129. exit;
  130. end;
  131. fen_true:
  132. result := true;
  133. { result is already false
  134. fen_false:
  135. result := false; }
  136. end;
  137. if procmethod=pm_postprocess then
  138. result:=process_children(result);
  139. end;
  140. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  141. begin
  142. result:=foreachnode(pm_postprocess,n,f,arg);
  143. end;
  144. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  145. function process_children(res : boolean) : boolean;
  146. var
  147. i: longint;
  148. begin
  149. result:=res;
  150. case n.nodetype of
  151. asn:
  152. if assigned(tasnode(n).call) then
  153. begin
  154. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  155. exit
  156. end;
  157. calln:
  158. begin
  159. result := foreachnodestatic(procmethod,tcallnode(n).callinitblock,f,arg) or result;
  160. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  161. result := foreachnodestatic(procmethod,tcallnode(n).callcleanupblock,f,arg) or result;
  162. end;
  163. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  164. begin
  165. { not in one statement, won't work because of b- }
  166. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  167. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  168. end;
  169. raisen:
  170. { frame tree }
  171. result := foreachnodestatic(traisenode(n).third,f,arg) or result;
  172. casen:
  173. begin
  174. for i := 0 to tcasenode(n).blocks.count-1 do
  175. if assigned(tcasenode(n).blocks[i]) then
  176. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  177. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  178. end;
  179. end;
  180. if n.inheritsfrom(tbinarynode) then
  181. begin
  182. { first process the "payload" of statementnodes }
  183. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  184. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  185. end
  186. else if n.inheritsfrom(tunarynode) then
  187. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  188. end;
  189. begin
  190. result := false;
  191. if not assigned(n) then
  192. exit;
  193. if procmethod=pm_preprocess then
  194. result:=process_children(result);
  195. case f(n,arg) of
  196. fen_norecurse_false:
  197. exit;
  198. fen_norecurse_true:
  199. begin
  200. result := true;
  201. exit;
  202. end;
  203. fen_true:
  204. result := true;
  205. { result is already false
  206. fen_false:
  207. result := false; }
  208. end;
  209. if procmethod=pm_postprocess then
  210. result:=process_children(result);
  211. end;
  212. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  213. begin
  214. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  215. end;
  216. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  217. begin
  218. if not(n.nodetype in pnodetypeset(arg)^) then
  219. internalerror(200610141);
  220. result:=fen_true;
  221. end;
  222. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  223. begin
  224. foreachnodestatic(n,@do_check,@typeset);
  225. end;
  226. procedure load_procvar_from_calln(var p1:tnode);
  227. var
  228. p2 : tnode;
  229. begin
  230. if p1.nodetype<>calln then
  231. internalerror(200212251);
  232. { was it a procvar, then we simply remove the calln and
  233. reuse the right }
  234. if assigned(tcallnode(p1).right) then
  235. begin
  236. p2:=tcallnode(p1).right;
  237. tcallnode(p1).right:=nil;
  238. end
  239. else
  240. begin
  241. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  242. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  243. { when the methodpointer is typen we've something like:
  244. tobject.create. Then only the address is needed of the
  245. method without a self pointer }
  246. if assigned(tcallnode(p1).methodpointer) and
  247. (tcallnode(p1).methodpointer.nodetype<>typen) then
  248. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  249. end;
  250. typecheckpass(p2);
  251. p1.free;
  252. p1:=p2;
  253. end;
  254. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  255. var
  256. hp : tnode;
  257. begin
  258. result:=false;
  259. if (p1.resultdef.typ<>procvardef) or
  260. (tponly and
  261. not(m_tp_procvar in current_settings.modeswitches)) then
  262. exit;
  263. { ignore vecn,subscriptn }
  264. hp:=p1;
  265. repeat
  266. case hp.nodetype of
  267. vecn,
  268. derefn,
  269. typeconvn,
  270. subscriptn :
  271. hp:=tunarynode(hp).left;
  272. else
  273. break;
  274. end;
  275. until false;
  276. { a tempref is used when it is loaded from a withsymtable }
  277. if (hp.nodetype in [calln,loadn,temprefn]) then
  278. begin
  279. hp:=ccallnode.create_procvar(nil,p1);
  280. typecheckpass(hp);
  281. p1:=hp;
  282. result:=true;
  283. end;
  284. end;
  285. function get_high_value_sym(vs: tparavarsym):tsym;
  286. begin
  287. result := tsym(vs.owner.Find('high'+vs.name));
  288. end;
  289. function get_local_or_para_sym(const aname:string):tsym;
  290. var
  291. pd : tprocdef;
  292. begin
  293. { we can't use searchsym here, because the
  294. symtablestack is not fully setup when pass1
  295. is run for nested procedures }
  296. pd:=current_procinfo.procdef;
  297. repeat
  298. result := tsym(pd.localst.Find(aname));
  299. if assigned(result) then
  300. break;
  301. result := tsym(pd.parast.Find(aname));
  302. if assigned(result) then
  303. break;
  304. { try the parent of a nested function }
  305. if assigned(pd.owner.defowner) and
  306. (pd.owner.defowner.typ=procdef) then
  307. pd:=tprocdef(pd.owner.defowner)
  308. else
  309. break;
  310. until false;
  311. end;
  312. function load_high_value_node(vs:tparavarsym):tnode;
  313. var
  314. srsym : tsym;
  315. begin
  316. result:=nil;
  317. srsym:=get_high_value_sym(vs);
  318. if assigned(srsym) then
  319. begin
  320. result:=cloadnode.create(srsym,vs.owner);
  321. typecheckpass(result);
  322. end
  323. else
  324. CGMessage(parser_e_illegal_expression);
  325. end;
  326. function load_self_node:tnode;
  327. var
  328. srsym : tsym;
  329. begin
  330. result:=nil;
  331. srsym:=get_local_or_para_sym('self');
  332. if assigned(srsym) then
  333. begin
  334. result:=cloadnode.create(srsym,srsym.owner);
  335. include(result.flags,nf_is_self);
  336. end
  337. else
  338. begin
  339. result:=cerrornode.create;
  340. CGMessage(parser_e_illegal_expression);
  341. end;
  342. typecheckpass(result);
  343. end;
  344. function load_result_node:tnode;
  345. var
  346. srsym : tsym;
  347. begin
  348. result:=nil;
  349. srsym:=get_local_or_para_sym('result');
  350. if assigned(srsym) then
  351. result:=cloadnode.create(srsym,srsym.owner)
  352. else
  353. begin
  354. result:=cerrornode.create;
  355. CGMessage(parser_e_illegal_expression);
  356. end;
  357. typecheckpass(result);
  358. end;
  359. function load_self_pointer_node:tnode;
  360. var
  361. srsym : tsym;
  362. begin
  363. result:=nil;
  364. srsym:=get_local_or_para_sym('self');
  365. if assigned(srsym) then
  366. begin
  367. result:=cloadnode.create(srsym,srsym.owner);
  368. include(result.flags,nf_load_self_pointer);
  369. end
  370. else
  371. begin
  372. result:=cerrornode.create;
  373. CGMessage(parser_e_illegal_expression);
  374. end;
  375. typecheckpass(result);
  376. end;
  377. function load_vmt_pointer_node:tnode;
  378. var
  379. srsym : tsym;
  380. begin
  381. result:=nil;
  382. srsym:=get_local_or_para_sym('vmt');
  383. if assigned(srsym) then
  384. result:=cloadnode.create(srsym,srsym.owner)
  385. else
  386. begin
  387. result:=cerrornode.create;
  388. CGMessage(parser_e_illegal_expression);
  389. end;
  390. typecheckpass(result);
  391. end;
  392. function is_self_node(p:tnode):boolean;
  393. begin
  394. is_self_node:=(p.nodetype=loadn) and
  395. (tloadnode(p).symtableentry.typ=paravarsym) and
  396. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  397. end;
  398. function call_fail_node:tnode;
  399. var
  400. para : tcallparanode;
  401. newstatement : tstatementnode;
  402. srsym : tsym;
  403. begin
  404. result:=internalstatements(newstatement);
  405. { call fail helper and exit normal }
  406. if is_class(current_procinfo.procdef._class) then
  407. begin
  408. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  409. if assigned(srsym) and
  410. (srsym.typ=procsym) then
  411. begin
  412. { if self<>0 and vmt=1 then freeinstance }
  413. addstatement(newstatement,cifnode.create(
  414. caddnode.create(andn,
  415. caddnode.create(unequaln,
  416. load_self_pointer_node,
  417. cnilnode.create),
  418. caddnode.create(equaln,
  419. ctypeconvnode.create(
  420. load_vmt_pointer_node,
  421. voidpointertype),
  422. cpointerconstnode.create(1,voidpointertype))),
  423. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  424. nil));
  425. end
  426. else
  427. internalerror(200305108);
  428. end
  429. else
  430. if is_object(current_procinfo.procdef._class) then
  431. begin
  432. { parameter 3 : vmt_offset }
  433. { parameter 2 : pointer to vmt }
  434. { parameter 1 : self pointer }
  435. para:=ccallparanode.create(
  436. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  437. ccallparanode.create(
  438. ctypeconvnode.create_internal(
  439. load_vmt_pointer_node,
  440. voidpointertype),
  441. ccallparanode.create(
  442. ctypeconvnode.create_internal(
  443. load_self_pointer_node,
  444. voidpointertype),
  445. nil)));
  446. addstatement(newstatement,
  447. ccallnode.createintern('fpc_help_fail',para));
  448. end
  449. else
  450. internalerror(200305132);
  451. { self:=nil }
  452. addstatement(newstatement,cassignmentnode.create(
  453. load_self_pointer_node,
  454. cnilnode.create));
  455. { exit }
  456. addstatement(newstatement,cexitnode.create(nil));
  457. end;
  458. function initialize_data_node(p:tnode):tnode;
  459. begin
  460. if not assigned(p.resultdef) then
  461. typecheckpass(p);
  462. if is_ansistring(p.resultdef) or
  463. is_widestring(p.resultdef) or
  464. is_interfacecom(p.resultdef) or
  465. is_dynamic_array(p.resultdef) then
  466. begin
  467. result:=cassignmentnode.create(
  468. ctypeconvnode.create_internal(p,voidpointertype),
  469. cnilnode.create
  470. );
  471. end
  472. else
  473. begin
  474. result:=ccallnode.createintern('fpc_initialize',
  475. ccallparanode.create(
  476. caddrnode.create_internal(
  477. crttinode.create(
  478. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  479. ccallparanode.create(
  480. caddrnode.create_internal(p),
  481. nil)));
  482. end;
  483. end;
  484. function finalize_data_node(p:tnode):tnode;
  485. var
  486. newstatement : tstatementnode;
  487. begin
  488. if not assigned(p.resultdef) then
  489. typecheckpass(p);
  490. if is_ansistring(p.resultdef) then
  491. begin
  492. result:=internalstatements(newstatement);
  493. addstatement(newstatement,ccallnode.createintern('fpc_ansistr_decr_ref',
  494. ccallparanode.create(
  495. ctypeconvnode.create_internal(p,voidpointertype),
  496. nil)));
  497. addstatement(newstatement,cassignmentnode.create(
  498. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  499. cnilnode.create
  500. ));
  501. end
  502. else if is_widestring(p.resultdef) then
  503. begin
  504. result:=internalstatements(newstatement);
  505. addstatement(newstatement,ccallnode.createintern('fpc_widestr_decr_ref',
  506. ccallparanode.create(
  507. ctypeconvnode.create_internal(p,voidpointertype),
  508. nil)));
  509. addstatement(newstatement,cassignmentnode.create(
  510. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  511. cnilnode.create
  512. ));
  513. end
  514. else if is_interfacecom(p.resultdef) then
  515. begin
  516. result:=internalstatements(newstatement);
  517. addstatement(newstatement,ccallnode.createintern('fpc_intf_decr_ref',
  518. ccallparanode.create(
  519. ctypeconvnode.create_internal(p,voidpointertype),
  520. nil)));
  521. addstatement(newstatement,cassignmentnode.create(
  522. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  523. cnilnode.create
  524. ));
  525. end
  526. else
  527. result:=ccallnode.createintern('fpc_finalize',
  528. ccallparanode.create(
  529. caddrnode.create_internal(
  530. crttinode.create(
  531. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  532. ccallparanode.create(
  533. caddrnode.create_internal(p),
  534. nil)));
  535. end;
  536. { this function must return a very high value ("infinity") for }
  537. { trees containing a call, the rest can be balanced more or less }
  538. { at will, probably best mainly in terms of required memory }
  539. { accesses }
  540. function node_complexity(p: tnode): cardinal;
  541. begin
  542. result := 0;
  543. while assigned(p) do
  544. begin
  545. case p.nodetype of
  546. temprefn,
  547. loadvmtaddrn,
  548. { main reason for the next one: we can't take the address of }
  549. { loadparentfpnode, so replacing it by a temp which is the }
  550. { address of this node's location and then dereferencing }
  551. { doesn't work. If changed, check whether webtbs/tw0935 }
  552. { still works with nodeinlining (JM) }
  553. loadparentfpn:
  554. begin
  555. result := 1;
  556. exit;
  557. end;
  558. loadn:
  559. begin
  560. { threadvars need a helper call }
  561. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  562. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  563. inc(result,5)
  564. else
  565. inc(result);
  566. if (result >= NODE_COMPLEXITY_INF) then
  567. result := NODE_COMPLEXITY_INF;
  568. exit;
  569. end;
  570. subscriptn:
  571. begin
  572. if is_class_or_interface(tunarynode(p).left.resultdef) then
  573. inc(result);
  574. if (result = NODE_COMPLEXITY_INF) then
  575. exit;
  576. p := tunarynode(p).left;
  577. end;
  578. blockn:
  579. p := tunarynode(p).left;
  580. notn,
  581. derefn :
  582. begin
  583. inc(result);
  584. if (result = NODE_COMPLEXITY_INF) then
  585. exit;
  586. p := tunarynode(p).left;
  587. end;
  588. typeconvn:
  589. begin
  590. { may be more complex in some cases }
  591. 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
  592. inc(result);
  593. if (result = NODE_COMPLEXITY_INF) then
  594. exit;
  595. p := tunarynode(p).left;
  596. end;
  597. vecn,
  598. statementn:
  599. begin
  600. inc(result,node_complexity(tbinarynode(p).left));
  601. if (result >= NODE_COMPLEXITY_INF) then
  602. begin
  603. result := NODE_COMPLEXITY_INF;
  604. exit;
  605. end;
  606. p := tbinarynode(p).right;
  607. end;
  608. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  609. shln,shrn,
  610. equaln,unequaln,gtn,gten,ltn,lten,
  611. assignn:
  612. begin
  613. inc(result,node_complexity(tbinarynode(p).left)+1);
  614. if (p.nodetype in [muln,divn,modn]) then
  615. inc(result,5);
  616. if (result >= NODE_COMPLEXITY_INF) then
  617. begin
  618. result := NODE_COMPLEXITY_INF;
  619. exit;
  620. end;
  621. p := tbinarynode(p).right;
  622. end;
  623. tempcreaten,
  624. tempdeleten,
  625. ordconstn,
  626. pointerconstn,
  627. nothingn,
  628. niln:
  629. exit;
  630. else
  631. begin
  632. result := NODE_COMPLEXITY_INF;
  633. exit;
  634. end;
  635. end;
  636. end;
  637. end;
  638. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  639. begin
  640. result:=fen_true;
  641. n.fileinfo:=pfileposinfo(arg)^;
  642. end;
  643. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  644. begin
  645. foreachnodestatic(n,@setnodefilepos,@filepos);
  646. end;
  647. {$ifdef FPCMT}
  648. threadvar
  649. {$else FPCMT}
  650. var
  651. {$endif FPCMT}
  652. treechanged : boolean;
  653. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  654. var
  655. hn : tnode;
  656. begin
  657. result:=fen_false;
  658. // do_typecheckpass(n);
  659. hn:=n.simplify;
  660. if assigned(hn) then
  661. begin
  662. treechanged:=true;
  663. n.free;
  664. n:=hn;
  665. typecheckpass(n);
  666. end;
  667. end;
  668. { tries to simplify the given node calling the simplify method recursively }
  669. procedure dosimplify(var n : tnode);
  670. begin
  671. repeat
  672. treechanged:=false;
  673. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  674. until not(treechanged);
  675. end;
  676. end.