nutils.pas 20 KB

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