nutils.pas 21 KB

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