nutils.pas 23 KB

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