nutils.pas 30 KB

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