nutils.pas 21 KB

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