nutils.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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<>0 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(unequaln,
  420. load_vmt_pointer_node,
  421. cnilnode.create)),
  422. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  423. nil));
  424. end
  425. else
  426. internalerror(200305108);
  427. end
  428. else
  429. if is_object(current_procinfo.procdef._class) then
  430. begin
  431. { parameter 3 : vmt_offset }
  432. { parameter 2 : pointer to vmt }
  433. { parameter 1 : self pointer }
  434. para:=ccallparanode.create(
  435. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  436. ccallparanode.create(
  437. ctypeconvnode.create_internal(
  438. load_vmt_pointer_node,
  439. voidpointertype),
  440. ccallparanode.create(
  441. ctypeconvnode.create_internal(
  442. load_self_pointer_node,
  443. voidpointertype),
  444. nil)));
  445. addstatement(newstatement,
  446. ccallnode.createintern('fpc_help_fail',para));
  447. end
  448. else
  449. internalerror(200305132);
  450. { self:=nil }
  451. addstatement(newstatement,cassignmentnode.create(
  452. load_self_pointer_node,
  453. cnilnode.create));
  454. { exit }
  455. addstatement(newstatement,cexitnode.create(nil));
  456. end;
  457. function initialize_data_node(p:tnode):tnode;
  458. begin
  459. if not assigned(p.resultdef) then
  460. typecheckpass(p);
  461. if is_ansistring(p.resultdef) or
  462. is_widestring(p.resultdef) or
  463. is_interfacecom(p.resultdef) or
  464. is_dynamic_array(p.resultdef) then
  465. begin
  466. result:=cassignmentnode.create(
  467. ctypeconvnode.create_internal(p,voidpointertype),
  468. cnilnode.create
  469. );
  470. end
  471. else
  472. begin
  473. result:=ccallnode.createintern('fpc_initialize',
  474. ccallparanode.create(
  475. caddrnode.create_internal(
  476. crttinode.create(
  477. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  478. ccallparanode.create(
  479. caddrnode.create_internal(p),
  480. nil)));
  481. end;
  482. end;
  483. function finalize_data_node(p:tnode):tnode;
  484. var
  485. newstatement : tstatementnode;
  486. begin
  487. if not assigned(p.resultdef) then
  488. typecheckpass(p);
  489. if is_ansistring(p.resultdef) then
  490. begin
  491. result:=internalstatements(newstatement);
  492. addstatement(newstatement,ccallnode.createintern('fpc_ansistr_decr_ref',
  493. ccallparanode.create(
  494. ctypeconvnode.create_internal(p,voidpointertype),
  495. nil)));
  496. addstatement(newstatement,cassignmentnode.create(
  497. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  498. cnilnode.create
  499. ));
  500. end
  501. else if is_widestring(p.resultdef) then
  502. begin
  503. result:=internalstatements(newstatement);
  504. addstatement(newstatement,ccallnode.createintern('fpc_widestr_decr_ref',
  505. ccallparanode.create(
  506. ctypeconvnode.create_internal(p,voidpointertype),
  507. nil)));
  508. addstatement(newstatement,cassignmentnode.create(
  509. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  510. cnilnode.create
  511. ));
  512. end
  513. else if is_interfacecom(p.resultdef) then
  514. begin
  515. result:=internalstatements(newstatement);
  516. addstatement(newstatement,ccallnode.createintern('fpc_intf_decr_ref',
  517. ccallparanode.create(
  518. ctypeconvnode.create_internal(p,voidpointertype),
  519. nil)));
  520. addstatement(newstatement,cassignmentnode.create(
  521. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  522. cnilnode.create
  523. ));
  524. end
  525. else
  526. result:=ccallnode.createintern('fpc_finalize',
  527. ccallparanode.create(
  528. caddrnode.create_internal(
  529. crttinode.create(
  530. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  531. ccallparanode.create(
  532. caddrnode.create_internal(p),
  533. nil)));
  534. end;
  535. { this function must return a very high value ("infinity") for }
  536. { trees containing a call, the rest can be balanced more or less }
  537. { at will, probably best mainly in terms of required memory }
  538. { accesses }
  539. function node_complexity(p: tnode): cardinal;
  540. begin
  541. result := 0;
  542. while assigned(p) do
  543. begin
  544. case p.nodetype of
  545. temprefn,
  546. loadvmtaddrn,
  547. { main reason for the next one: we can't take the address of }
  548. { loadparentfpnode, so replacing it by a temp which is the }
  549. { address of this node's location and then dereferencing }
  550. { doesn't work. If changed, check whether webtbs/tw0935 }
  551. { still works with nodeinlining (JM) }
  552. loadparentfpn:
  553. begin
  554. result := 1;
  555. exit;
  556. end;
  557. loadn:
  558. begin
  559. { threadvars need a helper call }
  560. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  561. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  562. inc(result,5)
  563. else
  564. inc(result);
  565. if (result >= NODE_COMPLEXITY_INF) then
  566. result := NODE_COMPLEXITY_INF;
  567. exit;
  568. end;
  569. subscriptn:
  570. begin
  571. if is_class_or_interface(tunarynode(p).left.resultdef) then
  572. inc(result);
  573. if (result = NODE_COMPLEXITY_INF) then
  574. exit;
  575. p := tunarynode(p).left;
  576. end;
  577. blockn,
  578. callparan:
  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. { this function returns an indication how much fpu registers
  639. will be required.
  640. Note: The algorithms need to be pessimistic to prevent a
  641. fpu stack overflow on i386 }
  642. function node_resources_fpu(p: tnode): cardinal;
  643. var
  644. res1,res2,res3 : cardinal;
  645. begin
  646. result:=0;
  647. res1:=0;
  648. res2:=0;
  649. res3:=0;
  650. if p.inheritsfrom(tunarynode) then
  651. begin
  652. if assigned(tunarynode(p).left) then
  653. res1:=node_resources_fpu(tunarynode(p).left);
  654. if p.inheritsfrom(tbinarynode) then
  655. begin
  656. if assigned(tbinarynode(p).right) then
  657. res2:=node_resources_fpu(tbinarynode(p).right);
  658. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  659. res3:=node_resources_fpu(ttertiarynode(p).third)
  660. end;
  661. end;
  662. result:=max(max(res1,res2),res3);
  663. case p.nodetype of
  664. calln:
  665. { it could be a recursive call, so we never really know the number of used fpu registers }
  666. result:=maxfpuregs;
  667. realconstn,
  668. typeconvn,
  669. loadn :
  670. begin
  671. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  672. result:=max(result,1);
  673. end;
  674. assignn,
  675. addn,subn,muln,slashn,
  676. equaln,unequaln,gtn,gten,ltn,lten :
  677. begin
  678. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  679. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  680. result:=max(result,2);
  681. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  682. inc(result);
  683. end;
  684. end;
  685. end;
  686. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  687. begin
  688. result:=fen_true;
  689. n.fileinfo:=pfileposinfo(arg)^;
  690. end;
  691. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  692. begin
  693. foreachnodestatic(n,@setnodefilepos,@filepos);
  694. end;
  695. {$ifdef FPCMT}
  696. threadvar
  697. {$else FPCMT}
  698. var
  699. {$endif FPCMT}
  700. treechanged : boolean;
  701. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  702. var
  703. hn : tnode;
  704. begin
  705. result:=fen_false;
  706. // do_typecheckpass(n);
  707. hn:=n.simplify;
  708. if assigned(hn) then
  709. begin
  710. treechanged:=true;
  711. n.free;
  712. n:=hn;
  713. typecheckpass(n);
  714. end;
  715. end;
  716. { tries to simplify the given node calling the simplify method recursively }
  717. procedure dosimplify(var n : tnode);
  718. begin
  719. repeat
  720. treechanged:=false;
  721. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  722. until not(treechanged);
  723. end;
  724. end.