nutils.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Type checking and register allocation for inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nutils;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globals,
  23. symsym,node;
  24. const
  25. NODE_COMPLEXITY_INF = 255;
  26. type
  27. { resulttype of functions that process on all nodes in a (sub)tree }
  28. foreachnoderesult = (
  29. { false, continue recursion }
  30. fen_false,
  31. { false, stop recursion }
  32. fen_norecurse_false,
  33. { true, continue recursion }
  34. fen_true,
  35. { true, stop recursion }
  36. fen_norecurse_true
  37. );
  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. procedure load_procvar_from_calln(var p1:tnode);
  43. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  44. function load_high_value_node(vs:tparavarsym):tnode;
  45. function load_self_node:tnode;
  46. function load_result_node:tnode;
  47. function load_self_pointer_node:tnode;
  48. function load_vmt_pointer_node:tnode;
  49. function is_self_node(p:tnode):boolean;
  50. function call_fail_node:tnode;
  51. function initialize_data_node(p:tnode):tnode;
  52. function finalize_data_node(p:tnode):tnode;
  53. function node_complexity(p: tnode): cardinal;
  54. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  55. implementation
  56. uses
  57. globtype,verbose,
  58. symconst,symbase,symtype,symdef,symtable,
  59. defutil,defcmp,
  60. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,
  61. cgbase,procinfo,
  62. pass_1;
  63. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  64. var
  65. i: longint;
  66. begin
  67. result := false;
  68. if not assigned(n) then
  69. exit;
  70. case f(n,arg) of
  71. fen_norecurse_false:
  72. exit;
  73. fen_norecurse_true:
  74. begin
  75. result := true;
  76. exit;
  77. end;
  78. fen_true:
  79. result := true;
  80. { result is already false
  81. fen_false:
  82. result := false; }
  83. end;
  84. case n.nodetype of
  85. calln:
  86. begin
  87. { not in one statement, won't work because of b- }
  88. result := foreachnode(tcallnode(n).methodpointer,f,arg) or result;
  89. {$ifdef PASS2INLINE}
  90. result := foreachnode(tcallnode(n).inlinecode,f,arg) or result;
  91. {$endif PASS2INLINE}
  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(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  118. var
  119. i: longint;
  120. begin
  121. result := false;
  122. if not assigned(n) then
  123. exit;
  124. case f(n,arg) of
  125. fen_norecurse_false:
  126. exit;
  127. fen_norecurse_true:
  128. begin
  129. result := true;
  130. exit;
  131. end;
  132. fen_true:
  133. result := true;
  134. { result is already false
  135. fen_false:
  136. result := false; }
  137. end;
  138. case n.nodetype of
  139. calln:
  140. begin
  141. result := foreachnodestatic(tcallnode(n).methodpointer,f,arg) or result;
  142. {$ifdef PASS2INLINE}
  143. result := foreachnodestatic(tcallnode(n).inlinecode,f,arg) or result;
  144. {$endif PASS2INLINE}
  145. end;
  146. ifn, whilerepeatn, forn:
  147. begin
  148. { not in one statement, won't work because of b- }
  149. result := foreachnodestatic(tloopnode(n).t1,f,arg) or result;
  150. result := foreachnodestatic(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(pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  159. result := foreachnodestatic(tcasenode(n).elseblock,f,arg) or result;
  160. end;
  161. end;
  162. if n.inheritsfrom(tbinarynode) then
  163. begin
  164. result := foreachnodestatic(tbinarynode(n).right,f,arg) or result;
  165. result := foreachnodestatic(tbinarynode(n).left,f,arg) or result;
  166. end
  167. else if n.inheritsfrom(tunarynode) then
  168. result := foreachnodestatic(tunarynode(n).left,f,arg) or result;
  169. end;
  170. procedure load_procvar_from_calln(var p1:tnode);
  171. var
  172. p2 : tnode;
  173. begin
  174. if p1.nodetype<>calln then
  175. internalerror(200212251);
  176. { was it a procvar, then we simply remove the calln and
  177. reuse the right }
  178. if assigned(tcallnode(p1).right) then
  179. begin
  180. p2:=tcallnode(p1).right;
  181. tcallnode(p1).right:=nil;
  182. end
  183. else
  184. begin
  185. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  186. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  187. { when the methodpointer is typen we've something like:
  188. tobject.create. Then only the address is needed of the
  189. method without a self pointer }
  190. if assigned(tcallnode(p1).methodpointer) and
  191. (tcallnode(p1).methodpointer.nodetype<>typen) then
  192. tloadnode(p2).set_mp(tcallnode(p1).get_load_methodpointer);
  193. end;
  194. resulttypepass(p2);
  195. p1.free;
  196. p1:=p2;
  197. end;
  198. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  199. var
  200. hp : tnode;
  201. begin
  202. result:=false;
  203. if (p1.resulttype.def.deftype<>procvardef) or
  204. (tponly and
  205. not(m_tp_procvar in aktmodeswitches)) then
  206. exit;
  207. { ignore vecn,subscriptn }
  208. hp:=p1;
  209. repeat
  210. case hp.nodetype of
  211. vecn,
  212. derefn,
  213. typeconvn,
  214. subscriptn :
  215. hp:=tunarynode(hp).left;
  216. else
  217. break;
  218. end;
  219. until false;
  220. { a tempref is used when it is loaded from a withsymtable }
  221. if (hp.nodetype in [loadn,temprefn]) then
  222. begin
  223. hp:=ccallnode.create_procvar(nil,p1);
  224. resulttypepass(hp);
  225. p1:=hp;
  226. result:=true;
  227. end;
  228. end;
  229. function load_high_value_node(vs:tparavarsym):tnode;
  230. var
  231. srsym : tsym;
  232. srsymtable : tsymtable;
  233. begin
  234. result:=nil;
  235. srsymtable:=vs.owner;
  236. srsym:=searchsymonlyin(srsymtable,'high'+vs.name);
  237. if assigned(srsym) then
  238. begin
  239. result:=cloadnode.create(srsym,srsymtable);
  240. resulttypepass(result);
  241. end
  242. else
  243. CGMessage(parser_e_illegal_expression);
  244. end;
  245. function load_self_node:tnode;
  246. var
  247. srsym : tsym;
  248. srsymtable : tsymtable;
  249. begin
  250. result:=nil;
  251. searchsym('self',srsym,srsymtable);
  252. if assigned(srsym) then
  253. begin
  254. result:=cloadnode.create(srsym,srsymtable);
  255. include(result.flags,nf_is_self);
  256. resulttypepass(result);
  257. end
  258. else
  259. CGMessage(parser_e_illegal_expression);
  260. end;
  261. function load_result_node:tnode;
  262. var
  263. srsym : tsym;
  264. srsymtable : tsymtable;
  265. begin
  266. result:=nil;
  267. searchsym('result',srsym,srsymtable);
  268. if assigned(srsym) then
  269. begin
  270. result:=cloadnode.create(srsym,srsymtable);
  271. resulttypepass(result);
  272. end
  273. else
  274. CGMessage(parser_e_illegal_expression);
  275. end;
  276. function load_self_pointer_node:tnode;
  277. var
  278. srsym : tsym;
  279. srsymtable : tsymtable;
  280. begin
  281. result:=nil;
  282. searchsym('self',srsym,srsymtable);
  283. if assigned(srsym) then
  284. begin
  285. result:=cloadnode.create(srsym,srsymtable);
  286. include(result.flags,nf_load_self_pointer);
  287. resulttypepass(result);
  288. end
  289. else
  290. CGMessage(parser_e_illegal_expression);
  291. end;
  292. function load_vmt_pointer_node:tnode;
  293. var
  294. srsym : tsym;
  295. srsymtable : tsymtable;
  296. begin
  297. result:=nil;
  298. searchsym('vmt',srsym,srsymtable);
  299. if assigned(srsym) then
  300. begin
  301. result:=cloadnode.create(srsym,srsymtable);
  302. resulttypepass(result);
  303. end
  304. else
  305. CGMessage(parser_e_illegal_expression);
  306. end;
  307. function is_self_node(p:tnode):boolean;
  308. begin
  309. is_self_node:=(p.nodetype=loadn) and
  310. (tloadnode(p).symtableentry.typ=paravarsym) and
  311. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  312. end;
  313. function call_fail_node:tnode;
  314. var
  315. para : tcallparanode;
  316. newstatement : tstatementnode;
  317. srsym : tsym;
  318. begin
  319. result:=internalstatements(newstatement);
  320. { call fail helper and exit normal }
  321. if is_class(current_procinfo.procdef._class) then
  322. begin
  323. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  324. if assigned(srsym) and
  325. (srsym.typ=procsym) then
  326. begin
  327. { if self<>0 and vmt=1 then freeinstance }
  328. addstatement(newstatement,cifnode.create(
  329. caddnode.create(andn,
  330. caddnode.create(unequaln,
  331. load_self_pointer_node,
  332. cnilnode.create),
  333. caddnode.create(equaln,
  334. ctypeconvnode.create(
  335. load_vmt_pointer_node,
  336. voidpointertype),
  337. cpointerconstnode.create(1,voidpointertype))),
  338. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  339. nil));
  340. end
  341. else
  342. internalerror(200305108);
  343. end
  344. else
  345. if is_object(current_procinfo.procdef._class) then
  346. begin
  347. { parameter 3 : vmt_offset }
  348. { parameter 2 : pointer to vmt }
  349. { parameter 1 : self pointer }
  350. para:=ccallparanode.create(
  351. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  352. ccallparanode.create(
  353. ctypeconvnode.create_internal(
  354. load_vmt_pointer_node,
  355. voidpointertype),
  356. ccallparanode.create(
  357. ctypeconvnode.create_internal(
  358. load_self_pointer_node,
  359. voidpointertype),
  360. nil)));
  361. addstatement(newstatement,
  362. ccallnode.createintern('fpc_help_fail',para));
  363. end
  364. else
  365. internalerror(200305132);
  366. { self:=nil }
  367. addstatement(newstatement,cassignmentnode.create(
  368. load_self_pointer_node,
  369. cnilnode.create));
  370. { exit }
  371. addstatement(newstatement,cexitnode.create(nil));
  372. end;
  373. function initialize_data_node(p:tnode):tnode;
  374. begin
  375. if not assigned(p.resulttype.def) then
  376. resulttypepass(p);
  377. if is_ansistring(p.resulttype.def) or
  378. is_widestring(p.resulttype.def) or
  379. is_interfacecom(p.resulttype.def) or
  380. is_dynamic_array(p.resulttype.def) then
  381. begin
  382. result:=cassignmentnode.create(
  383. ctypeconvnode.create_internal(p,voidpointertype),
  384. cnilnode.create
  385. );
  386. end
  387. else
  388. begin
  389. result:=ccallnode.createintern('fpc_initialize',
  390. ccallparanode.create(
  391. caddrnode.create_internal(
  392. crttinode.create(
  393. tstoreddef(p.resulttype.def),initrtti)),
  394. ccallparanode.create(
  395. caddrnode.create_internal(p),
  396. nil)));
  397. end;
  398. end;
  399. function finalize_data_node(p:tnode):tnode;
  400. begin
  401. if not assigned(p.resulttype.def) then
  402. resulttypepass(p);
  403. result:=ccallnode.createintern('fpc_finalize',
  404. ccallparanode.create(
  405. caddrnode.create_internal(
  406. crttinode.create(
  407. tstoreddef(p.resulttype.def),initrtti)),
  408. ccallparanode.create(
  409. caddrnode.create_internal(p),
  410. nil)));
  411. end;
  412. { this function must return a very high value ("infinity") for }
  413. { trees containing a call, the rest can be balanced more or less }
  414. { at will, probably best mainly in terms of required memory }
  415. { accesses }
  416. function node_complexity(p: tnode): cardinal;
  417. begin
  418. result := 0;
  419. while true do
  420. begin
  421. case p.nodetype of
  422. temprefn,
  423. loadvmtaddrn,
  424. { main reason for the next one: we can't take the address of }
  425. { loadparentfpnode, so replacing it by a temp which is the }
  426. { address of this node's location and then dereferencing }
  427. { doesn't work. If changed, check whether webtbs/tw0935 }
  428. { still works with nodeinlining (JM) }
  429. loadparentfpn:
  430. begin
  431. result := 1;
  432. exit;
  433. end;
  434. loadn:
  435. begin
  436. { threadvars need a helper call }
  437. if (tloadnode(p).symtableentry.typ=globalvarsym) and
  438. (vo_is_thread_var in tglobalvarsym(tloadnode(p).symtableentry).varoptions) then
  439. inc(result,5)
  440. else
  441. inc(result);
  442. if (result >= NODE_COMPLEXITY_INF) then
  443. result := NODE_COMPLEXITY_INF;
  444. exit;
  445. end;
  446. subscriptn,
  447. blockn:
  448. p := tunarynode(p).left;
  449. derefn :
  450. begin
  451. inc(result);
  452. if (result = NODE_COMPLEXITY_INF) then
  453. exit;
  454. p := tunarynode(p).left;
  455. end;
  456. typeconvn:
  457. begin
  458. { may be more complex in some cases }
  459. 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
  460. inc(result);
  461. if (result = NODE_COMPLEXITY_INF) then
  462. exit;
  463. p := tunarynode(p).left;
  464. end;
  465. vecn,
  466. statementn:
  467. begin
  468. inc(result,node_complexity(tbinarynode(p).left));
  469. if (result >= NODE_COMPLEXITY_INF) then
  470. begin
  471. result := NODE_COMPLEXITY_INF;
  472. exit;
  473. end;
  474. p := tbinarynode(p).right;
  475. end;
  476. { better: make muln/divn/modn more expensive }
  477. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  478. assignn:
  479. begin
  480. inc(result,node_complexity(tbinarynode(p).left)+1);
  481. if (result >= NODE_COMPLEXITY_INF) then
  482. begin
  483. result := NODE_COMPLEXITY_INF;
  484. exit;
  485. end;
  486. p := tbinarynode(p).right;
  487. end;
  488. tempcreaten,
  489. tempdeleten,
  490. ordconstn,
  491. pointerconstn:
  492. exit;
  493. else
  494. begin
  495. result := NODE_COMPLEXITY_INF;
  496. exit;
  497. end;
  498. end;
  499. end;
  500. end;
  501. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  502. begin
  503. result:=fen_true;
  504. n.fileinfo:=pfileposinfo(arg)^;
  505. end;
  506. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  507. begin
  508. foreachnodestatic(n,@setnodefilepos,@filepos);
  509. end;
  510. end.
  511. {
  512. $Log$
  513. Revision 1.31 2005-04-06 11:49:37 michael
  514. * Fix methodpointer copy from callnode to loadnode
  515. Revision 1.30 2005/02/14 17:13:06 peter
  516. * truncate log
  517. Revision 1.29 2005/01/04 16:39:46 peter
  518. * set nf_is_self node flag when self is loaded
  519. }