nutils.pas 20 KB

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