nutils.pas 18 KB

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