nutils.pas 21 KB

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