nutils.pas 22 KB

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