nutils.pas 21 KB

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