nutils.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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).methodpointer,f,arg) or result;
  92. {$ifdef PASS2INLINE}
  93. result := foreachnode(tcallnode(n).inlinecode,f,arg) or result;
  94. {$endif PASS2INLINE}
  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. result := foreachnode(tbinarynode(n).right,f,arg) or result;
  115. result := foreachnode(tbinarynode(n).left,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).methodpointer,f,arg) or result;
  130. {$ifdef PASS2INLINE}
  131. result := foreachnodestatic(procmethod,tcallnode(n).inlinecode,f,arg) or result;
  132. {$endif PASS2INLINE}
  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. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  153. result := foreachnodestatic(procmethod,tbinarynode(n).left,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. 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 [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. srsymtable : tsymtable;
  248. begin
  249. result:=nil;
  250. srsymtable:=vs.owner;
  251. srsym:=searchsymonlyin(srsymtable,'high'+vs.name);
  252. if assigned(srsym) then
  253. begin
  254. result:=cloadnode.create(srsym,srsymtable);
  255. resulttypepass(result);
  256. end
  257. else
  258. CGMessage(parser_e_illegal_expression);
  259. end;
  260. function load_self_node:tnode;
  261. var
  262. srsym : tsym;
  263. srsymtable : tsymtable;
  264. begin
  265. result:=nil;
  266. searchsym('self',srsym,srsymtable);
  267. if assigned(srsym) then
  268. begin
  269. result:=cloadnode.create(srsym,srsymtable);
  270. include(result.flags,nf_is_self);
  271. end
  272. else
  273. begin
  274. result:=cerrornode.create;
  275. CGMessage(parser_e_illegal_expression);
  276. end;
  277. resulttypepass(result);
  278. end;
  279. function load_result_node:tnode;
  280. var
  281. srsym : tsym;
  282. srsymtable : tsymtable;
  283. begin
  284. result:=nil;
  285. searchsym('result',srsym,srsymtable);
  286. if assigned(srsym) then
  287. result:=cloadnode.create(srsym,srsymtable)
  288. else
  289. begin
  290. result:=cerrornode.create;
  291. CGMessage(parser_e_illegal_expression);
  292. end;
  293. resulttypepass(result);
  294. end;
  295. function load_self_pointer_node:tnode;
  296. var
  297. srsym : tsym;
  298. srsymtable : tsymtable;
  299. begin
  300. result:=nil;
  301. searchsym('self',srsym,srsymtable);
  302. if assigned(srsym) then
  303. begin
  304. result:=cloadnode.create(srsym,srsymtable);
  305. include(result.flags,nf_load_self_pointer);
  306. end
  307. else
  308. begin
  309. result:=cerrornode.create;
  310. CGMessage(parser_e_illegal_expression);
  311. end;
  312. resulttypepass(result);
  313. end;
  314. function load_vmt_pointer_node:tnode;
  315. var
  316. srsym : tsym;
  317. srsymtable : tsymtable;
  318. begin
  319. result:=nil;
  320. searchsym('vmt',srsym,srsymtable);
  321. if assigned(srsym) then
  322. result:=cloadnode.create(srsym,srsymtable)
  323. else
  324. begin
  325. result:=cerrornode.create;
  326. CGMessage(parser_e_illegal_expression);
  327. end;
  328. resulttypepass(result);
  329. end;
  330. function is_self_node(p:tnode):boolean;
  331. begin
  332. is_self_node:=(p.nodetype=loadn) and
  333. (tloadnode(p).symtableentry.typ=paravarsym) and
  334. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  335. end;
  336. function call_fail_node:tnode;
  337. var
  338. para : tcallparanode;
  339. newstatement : tstatementnode;
  340. srsym : tsym;
  341. begin
  342. result:=internalstatements(newstatement);
  343. { call fail helper and exit normal }
  344. if is_class(current_procinfo.procdef._class) then
  345. begin
  346. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  347. if assigned(srsym) and
  348. (srsym.typ=procsym) then
  349. begin
  350. { if self<>0 and vmt=1 then freeinstance }
  351. addstatement(newstatement,cifnode.create(
  352. caddnode.create(andn,
  353. caddnode.create(unequaln,
  354. load_self_pointer_node,
  355. cnilnode.create),
  356. caddnode.create(equaln,
  357. ctypeconvnode.create(
  358. load_vmt_pointer_node,
  359. voidpointertype),
  360. cpointerconstnode.create(1,voidpointertype))),
  361. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  362. nil));
  363. end
  364. else
  365. internalerror(200305108);
  366. end
  367. else
  368. if is_object(current_procinfo.procdef._class) then
  369. begin
  370. { parameter 3 : vmt_offset }
  371. { parameter 2 : pointer to vmt }
  372. { parameter 1 : self pointer }
  373. para:=ccallparanode.create(
  374. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  375. ccallparanode.create(
  376. ctypeconvnode.create_internal(
  377. load_vmt_pointer_node,
  378. voidpointertype),
  379. ccallparanode.create(
  380. ctypeconvnode.create_internal(
  381. load_self_pointer_node,
  382. voidpointertype),
  383. nil)));
  384. addstatement(newstatement,
  385. ccallnode.createintern('fpc_help_fail',para));
  386. end
  387. else
  388. internalerror(200305132);
  389. { self:=nil }
  390. addstatement(newstatement,cassignmentnode.create(
  391. load_self_pointer_node,
  392. cnilnode.create));
  393. { exit }
  394. addstatement(newstatement,cexitnode.create(nil));
  395. end;
  396. function initialize_data_node(p:tnode):tnode;
  397. begin
  398. if not assigned(p.resulttype.def) then
  399. resulttypepass(p);
  400. if is_ansistring(p.resulttype.def) or
  401. is_widestring(p.resulttype.def) or
  402. is_interfacecom(p.resulttype.def) or
  403. is_dynamic_array(p.resulttype.def) then
  404. begin
  405. result:=cassignmentnode.create(
  406. ctypeconvnode.create_internal(p,voidpointertype),
  407. cnilnode.create
  408. );
  409. end
  410. else
  411. begin
  412. result:=ccallnode.createintern('fpc_initialize',
  413. ccallparanode.create(
  414. caddrnode.create_internal(
  415. crttinode.create(
  416. tstoreddef(p.resulttype.def),initrtti)),
  417. ccallparanode.create(
  418. caddrnode.create_internal(p),
  419. nil)));
  420. end;
  421. end;
  422. function finalize_data_node(p:tnode):tnode;
  423. begin
  424. if not assigned(p.resulttype.def) then
  425. resulttypepass(p);
  426. result:=ccallnode.createintern('fpc_finalize',
  427. ccallparanode.create(
  428. caddrnode.create_internal(
  429. crttinode.create(
  430. tstoreddef(p.resulttype.def),initrtti)),
  431. ccallparanode.create(
  432. caddrnode.create_internal(p),
  433. nil)));
  434. end;
  435. { this function must return a very high value ("infinity") for }
  436. { trees containing a call, the rest can be balanced more or less }
  437. { at will, probably best mainly in terms of required memory }
  438. { accesses }
  439. function node_complexity(p: tnode): cardinal;
  440. begin
  441. result := 0;
  442. while true do
  443. begin
  444. case p.nodetype of
  445. temprefn,
  446. loadvmtaddrn,
  447. { main reason for the next one: we can't take the address of }
  448. { loadparentfpnode, so replacing it by a temp which is the }
  449. { address of this node's location and then dereferencing }
  450. { doesn't work. If changed, check whether webtbs/tw0935 }
  451. { still works with nodeinlining (JM) }
  452. loadparentfpn:
  453. begin
  454. result := 1;
  455. exit;
  456. end;
  457. loadn:
  458. begin
  459. { threadvars need a helper call }
  460. if (tloadnode(p).symtableentry.typ=globalvarsym) and
  461. (vo_is_thread_var in tglobalvarsym(tloadnode(p).symtableentry).varoptions) then
  462. inc(result,5)
  463. else
  464. inc(result);
  465. if (result >= NODE_COMPLEXITY_INF) then
  466. result := NODE_COMPLEXITY_INF;
  467. exit;
  468. end;
  469. subscriptn,
  470. blockn:
  471. p := tunarynode(p).left;
  472. derefn :
  473. begin
  474. inc(result);
  475. if (result = NODE_COMPLEXITY_INF) then
  476. exit;
  477. p := tunarynode(p).left;
  478. end;
  479. typeconvn:
  480. begin
  481. { may be more complex in some cases }
  482. 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
  483. inc(result);
  484. if (result = NODE_COMPLEXITY_INF) then
  485. exit;
  486. p := tunarynode(p).left;
  487. end;
  488. vecn,
  489. statementn:
  490. begin
  491. inc(result,node_complexity(tbinarynode(p).left));
  492. if (result >= NODE_COMPLEXITY_INF) then
  493. begin
  494. result := NODE_COMPLEXITY_INF;
  495. exit;
  496. end;
  497. p := tbinarynode(p).right;
  498. end;
  499. { better: make muln/divn/modn more expensive }
  500. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  501. assignn:
  502. begin
  503. inc(result,node_complexity(tbinarynode(p).left)+1);
  504. if (result >= NODE_COMPLEXITY_INF) then
  505. begin
  506. result := NODE_COMPLEXITY_INF;
  507. exit;
  508. end;
  509. p := tbinarynode(p).right;
  510. end;
  511. tempcreaten,
  512. tempdeleten,
  513. ordconstn,
  514. pointerconstn:
  515. exit;
  516. else
  517. begin
  518. result := NODE_COMPLEXITY_INF;
  519. exit;
  520. end;
  521. end;
  522. end;
  523. end;
  524. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  525. begin
  526. result:=fen_true;
  527. n.fileinfo:=pfileposinfo(arg)^;
  528. end;
  529. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  530. begin
  531. foreachnodestatic(n,@setnodefilepos,@filepos);
  532. end;
  533. {$ifdef FPCMT}
  534. threadvar
  535. {$else FPCMT}
  536. var
  537. {$endif FPCMT}
  538. treechanged : boolean;
  539. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  540. var
  541. hn : tnode;
  542. begin
  543. result:=fen_false;
  544. do_resulttypepass(n);
  545. hn:=n.simplify;
  546. if assigned(hn) then
  547. begin
  548. treechanged:=true;
  549. n:=hn;
  550. end;
  551. end;
  552. { tries to simplify the given node calling the simplify method recursively }
  553. procedure dosimplify(var n : tnode);
  554. begin
  555. repeat
  556. treechanged:=false;
  557. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  558. until not(treechanged);
  559. end;
  560. end.