nutils.pas 26 KB

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