nutils.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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. globtype,
  22. symtype,symsym,symbase,symtable,
  23. node;
  24. const
  25. NODE_COMPLEXITY_INF = 255;
  26. type
  27. { resultdef 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. tforeachprocmethod = (pm_preprocess,pm_postprocess);
  39. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  40. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  41. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  42. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  43. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  44. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  45. { checks if the given node tree contains only nodes of the given type,
  46. if this isn't the case, an ie is thrown
  47. }
  48. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  49. procedure load_procvar_from_calln(var p1:tnode);
  50. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  51. function get_high_value_sym(vs: tparavarsym):tsym; { marking it as inline causes IE 200311075 during loading from ppu file }
  52. function load_high_value_node(vs:tparavarsym):tnode;
  53. function load_self_node:tnode;
  54. function load_result_node:tnode;
  55. function load_self_pointer_node:tnode;
  56. function load_vmt_pointer_node:tnode;
  57. function is_self_node(p:tnode):boolean;
  58. function call_fail_node:tnode;
  59. function initialize_data_node(p:tnode):tnode;
  60. function finalize_data_node(p:tnode):tnode;
  61. function node_complexity(p: tnode): cardinal;
  62. function node_resources_fpu(p: tnode): cardinal;
  63. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  64. { tries to simplify the given node }
  65. procedure dosimplify(var n : tnode);
  66. { returns true if n is only a tree of administrative nodes
  67. containing no code }
  68. function has_no_code(n : tnode) : boolean;
  69. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  70. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  71. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  72. implementation
  73. uses
  74. cutils,verbose,constexp,globals,
  75. symconst,symdef,
  76. defutil,defcmp,
  77. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,ninl,
  78. cpubase,cgbase,procinfo,
  79. pass_1;
  80. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  81. function process_children(res : boolean) : boolean;
  82. var
  83. i: longint;
  84. begin
  85. result:=res;
  86. case n.nodetype of
  87. asn:
  88. if assigned(tasnode(n).call) then
  89. begin
  90. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  91. exit
  92. end;
  93. calln:
  94. begin
  95. result := foreachnode(procmethod,tcallnode(n).callinitblock,f,arg) or result;
  96. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  97. result := foreachnode(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  98. result := foreachnode(procmethod,tcallnode(n).callcleanupblock,f,arg) or result;
  99. end;
  100. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  101. begin
  102. { not in one statement, won't work because of b- }
  103. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  104. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  105. end;
  106. raisen:
  107. { frame tree }
  108. result := foreachnode(traisenode(n).third,f,arg) or result;
  109. casen:
  110. begin
  111. for i := 0 to tcasenode(n).blocks.count-1 do
  112. if assigned(tcasenode(n).blocks[i]) then
  113. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  114. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  115. end;
  116. end;
  117. if n.inheritsfrom(tbinarynode) then
  118. begin
  119. { first process the "payload" of statementnodes }
  120. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  121. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  122. end
  123. else if n.inheritsfrom(tunarynode) then
  124. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  125. end;
  126. begin
  127. result := false;
  128. if not assigned(n) then
  129. exit;
  130. if procmethod=pm_preprocess then
  131. result:=process_children(result);
  132. case f(n,arg) of
  133. fen_norecurse_false:
  134. exit;
  135. fen_norecurse_true:
  136. begin
  137. result := true;
  138. exit;
  139. end;
  140. fen_true:
  141. result := true;
  142. { result is already false
  143. fen_false:
  144. result := false; }
  145. end;
  146. if procmethod=pm_postprocess then
  147. result:=process_children(result);
  148. end;
  149. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  150. begin
  151. result:=foreachnode(pm_postprocess,n,f,arg);
  152. end;
  153. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  154. function process_children(res : boolean) : boolean;
  155. var
  156. i: longint;
  157. begin
  158. result:=res;
  159. case n.nodetype of
  160. asn:
  161. if assigned(tasnode(n).call) then
  162. begin
  163. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  164. exit
  165. end;
  166. calln:
  167. begin
  168. result := foreachnodestatic(procmethod,tcallnode(n).callinitblock,f,arg) or result;
  169. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  170. result := foreachnodestatic(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  171. result := foreachnodestatic(procmethod,tcallnode(n).callcleanupblock,f,arg) or result;
  172. end;
  173. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  174. begin
  175. { not in one statement, won't work because of b- }
  176. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  177. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  178. end;
  179. raisen:
  180. { frame tree }
  181. result := foreachnodestatic(traisenode(n).third,f,arg) or result;
  182. casen:
  183. begin
  184. for i := 0 to tcasenode(n).blocks.count-1 do
  185. if assigned(tcasenode(n).blocks[i]) then
  186. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  187. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  188. end;
  189. end;
  190. if n.inheritsfrom(tbinarynode) then
  191. begin
  192. { first process the "payload" of statementnodes }
  193. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  194. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  195. end
  196. else if n.inheritsfrom(tunarynode) then
  197. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  198. end;
  199. begin
  200. result := false;
  201. if not assigned(n) then
  202. exit;
  203. if procmethod=pm_preprocess then
  204. result:=process_children(result);
  205. case f(n,arg) of
  206. fen_norecurse_false:
  207. exit;
  208. fen_norecurse_true:
  209. begin
  210. result := true;
  211. exit;
  212. end;
  213. fen_true:
  214. result := true;
  215. { result is already false
  216. fen_false:
  217. result := false; }
  218. end;
  219. if procmethod=pm_postprocess then
  220. result:=process_children(result);
  221. end;
  222. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  223. begin
  224. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  225. end;
  226. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  227. begin
  228. if not(n.nodetype in pnodetypeset(arg)^) then
  229. internalerror(200610141);
  230. result:=fen_true;
  231. end;
  232. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  233. begin
  234. foreachnodestatic(n,@do_check,@typeset);
  235. end;
  236. procedure load_procvar_from_calln(var p1:tnode);
  237. var
  238. p2 : tnode;
  239. begin
  240. if p1.nodetype<>calln then
  241. internalerror(200212251);
  242. { was it a procvar, then we simply remove the calln and
  243. reuse the right }
  244. if assigned(tcallnode(p1).right) then
  245. begin
  246. p2:=tcallnode(p1).right;
  247. tcallnode(p1).right:=nil;
  248. end
  249. else
  250. begin
  251. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  252. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  253. { when the methodpointer is typen we've something like:
  254. tobject.create. Then only the address is needed of the
  255. method without a self pointer }
  256. if assigned(tcallnode(p1).methodpointer) and
  257. (tcallnode(p1).methodpointer.nodetype<>typen) then
  258. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  259. end;
  260. typecheckpass(p2);
  261. p1.free;
  262. p1:=p2;
  263. end;
  264. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  265. var
  266. hp : tnode;
  267. begin
  268. result:=false;
  269. if (p1.resultdef.typ<>procvardef) or
  270. (tponly and
  271. not(m_tp_procvar in current_settings.modeswitches)) then
  272. exit;
  273. { ignore vecn,subscriptn }
  274. hp:=p1;
  275. repeat
  276. case hp.nodetype of
  277. vecn,
  278. derefn,
  279. typeconvn,
  280. subscriptn :
  281. hp:=tunarynode(hp).left;
  282. else
  283. break;
  284. end;
  285. until false;
  286. { a tempref is used when it is loaded from a withsymtable }
  287. if (hp.nodetype in [calln,loadn,temprefn]) then
  288. begin
  289. hp:=ccallnode.create_procvar(nil,p1);
  290. typecheckpass(hp);
  291. p1:=hp;
  292. result:=true;
  293. end;
  294. end;
  295. function get_high_value_sym(vs: tparavarsym):tsym;
  296. begin
  297. result := tsym(vs.owner.Find('high'+vs.name));
  298. end;
  299. function get_local_or_para_sym(const aname:string):tsym;
  300. var
  301. pd : tprocdef;
  302. begin
  303. { we can't use searchsym here, because the
  304. symtablestack is not fully setup when pass1
  305. is run for nested procedures }
  306. pd:=current_procinfo.procdef;
  307. repeat
  308. result := tsym(pd.localst.Find(aname));
  309. if assigned(result) then
  310. break;
  311. result := tsym(pd.parast.Find(aname));
  312. if assigned(result) then
  313. break;
  314. { try the parent of a nested function }
  315. if assigned(pd.owner.defowner) and
  316. (pd.owner.defowner.typ=procdef) then
  317. pd:=tprocdef(pd.owner.defowner)
  318. else
  319. break;
  320. until false;
  321. end;
  322. function load_high_value_node(vs:tparavarsym):tnode;
  323. var
  324. srsym : tsym;
  325. begin
  326. result:=nil;
  327. srsym:=get_high_value_sym(vs);
  328. if assigned(srsym) then
  329. begin
  330. result:=cloadnode.create(srsym,vs.owner);
  331. typecheckpass(result);
  332. end
  333. else
  334. CGMessage(parser_e_illegal_expression);
  335. end;
  336. function load_self_node:tnode;
  337. var
  338. srsym : tsym;
  339. begin
  340. result:=nil;
  341. srsym:=get_local_or_para_sym('self');
  342. if assigned(srsym) then
  343. begin
  344. result:=cloadnode.create(srsym,srsym.owner);
  345. include(result.flags,nf_is_self);
  346. end
  347. else
  348. begin
  349. result:=cerrornode.create;
  350. CGMessage(parser_e_illegal_expression);
  351. end;
  352. typecheckpass(result);
  353. end;
  354. function load_result_node:tnode;
  355. var
  356. srsym : tsym;
  357. begin
  358. result:=nil;
  359. srsym:=get_local_or_para_sym('result');
  360. if assigned(srsym) then
  361. result:=cloadnode.create(srsym,srsym.owner)
  362. else
  363. begin
  364. result:=cerrornode.create;
  365. CGMessage(parser_e_illegal_expression);
  366. end;
  367. typecheckpass(result);
  368. end;
  369. function load_self_pointer_node:tnode;
  370. var
  371. srsym : tsym;
  372. begin
  373. result:=nil;
  374. srsym:=get_local_or_para_sym('self');
  375. if assigned(srsym) then
  376. begin
  377. result:=cloadnode.create(srsym,srsym.owner);
  378. include(result.flags,nf_load_self_pointer);
  379. end
  380. else
  381. begin
  382. result:=cerrornode.create;
  383. CGMessage(parser_e_illegal_expression);
  384. end;
  385. typecheckpass(result);
  386. end;
  387. function load_vmt_pointer_node:tnode;
  388. var
  389. srsym : tsym;
  390. begin
  391. result:=nil;
  392. srsym:=get_local_or_para_sym('vmt');
  393. if assigned(srsym) then
  394. result:=cloadnode.create(srsym,srsym.owner)
  395. else
  396. begin
  397. result:=cerrornode.create;
  398. CGMessage(parser_e_illegal_expression);
  399. end;
  400. typecheckpass(result);
  401. end;
  402. function is_self_node(p:tnode):boolean;
  403. begin
  404. is_self_node:=(p.nodetype=loadn) and
  405. (tloadnode(p).symtableentry.typ=paravarsym) and
  406. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  407. end;
  408. function call_fail_node:tnode;
  409. var
  410. para : tcallparanode;
  411. newstatement : tstatementnode;
  412. srsym : tsym;
  413. begin
  414. result:=internalstatements(newstatement);
  415. { call fail helper and exit normal }
  416. if is_class(current_objectdef) then
  417. begin
  418. srsym:=search_class_member(current_objectdef,'FREEINSTANCE');
  419. if assigned(srsym) and
  420. (srsym.typ=procsym) then
  421. begin
  422. { if self<>0 and vmt<>0 then freeinstance }
  423. addstatement(newstatement,cifnode.create(
  424. caddnode.create(andn,
  425. caddnode.create(unequaln,
  426. load_self_pointer_node,
  427. cnilnode.create),
  428. caddnode.create(unequaln,
  429. load_vmt_pointer_node,
  430. cnilnode.create)),
  431. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  432. nil));
  433. end
  434. else
  435. internalerror(200305108);
  436. end
  437. else
  438. if is_object(current_objectdef) then
  439. begin
  440. { parameter 3 : vmt_offset }
  441. { parameter 2 : pointer to vmt }
  442. { parameter 1 : self pointer }
  443. para:=ccallparanode.create(
  444. cordconstnode.create(current_objectdef.vmt_offset,s32inttype,false),
  445. ccallparanode.create(
  446. ctypeconvnode.create_internal(
  447. load_vmt_pointer_node,
  448. voidpointertype),
  449. ccallparanode.create(
  450. ctypeconvnode.create_internal(
  451. load_self_pointer_node,
  452. voidpointertype),
  453. nil)));
  454. addstatement(newstatement,
  455. ccallnode.createintern('fpc_help_fail',para));
  456. end
  457. else
  458. internalerror(200305132);
  459. { self:=nil }
  460. addstatement(newstatement,cassignmentnode.create(
  461. load_self_pointer_node,
  462. cnilnode.create));
  463. { exit }
  464. addstatement(newstatement,cexitnode.create(nil));
  465. end;
  466. function initialize_data_node(p:tnode):tnode;
  467. begin
  468. if not assigned(p.resultdef) then
  469. typecheckpass(p);
  470. if is_ansistring(p.resultdef) or
  471. is_wide_or_unicode_string(p.resultdef) or
  472. is_interfacecom(p.resultdef) or
  473. is_dynamic_array(p.resultdef) then
  474. begin
  475. result:=cassignmentnode.create(
  476. ctypeconvnode.create_internal(p,voidpointertype),
  477. cnilnode.create
  478. );
  479. end
  480. else
  481. begin
  482. result:=ccallnode.createintern('fpc_initialize',
  483. ccallparanode.create(
  484. caddrnode.create_internal(
  485. crttinode.create(
  486. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  487. ccallparanode.create(
  488. caddrnode.create_internal(p),
  489. nil)));
  490. end;
  491. end;
  492. function finalize_data_node(p:tnode):tnode;
  493. var
  494. newstatement : tstatementnode;
  495. begin
  496. if not assigned(p.resultdef) then
  497. typecheckpass(p);
  498. if is_ansistring(p.resultdef) then
  499. begin
  500. result:=internalstatements(newstatement);
  501. addstatement(newstatement,ccallnode.createintern('fpc_ansistr_decr_ref',
  502. ccallparanode.create(
  503. ctypeconvnode.create_internal(p,voidpointertype),
  504. nil)));
  505. addstatement(newstatement,cassignmentnode.create(
  506. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  507. cnilnode.create
  508. ));
  509. end
  510. else if is_widestring(p.resultdef) then
  511. begin
  512. result:=internalstatements(newstatement);
  513. addstatement(newstatement,ccallnode.createintern('fpc_widestr_decr_ref',
  514. ccallparanode.create(
  515. ctypeconvnode.create_internal(p,voidpointertype),
  516. nil)));
  517. addstatement(newstatement,cassignmentnode.create(
  518. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  519. cnilnode.create
  520. ));
  521. end
  522. else if is_unicodestring(p.resultdef) then
  523. begin
  524. result:=internalstatements(newstatement);
  525. addstatement(newstatement,ccallnode.createintern('fpc_unicodestr_decr_ref',
  526. ccallparanode.create(
  527. ctypeconvnode.create_internal(p,voidpointertype),
  528. nil)));
  529. addstatement(newstatement,cassignmentnode.create(
  530. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  531. cnilnode.create
  532. ));
  533. end
  534. else if is_interfacecom(p.resultdef) then
  535. begin
  536. result:=internalstatements(newstatement);
  537. addstatement(newstatement,ccallnode.createintern('fpc_intf_decr_ref',
  538. ccallparanode.create(
  539. ctypeconvnode.create_internal(p,voidpointertype),
  540. nil)));
  541. addstatement(newstatement,cassignmentnode.create(
  542. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  543. cnilnode.create
  544. ));
  545. end
  546. else
  547. result:=ccallnode.createintern('fpc_finalize',
  548. ccallparanode.create(
  549. caddrnode.create_internal(
  550. crttinode.create(
  551. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  552. ccallparanode.create(
  553. caddrnode.create_internal(p),
  554. nil)));
  555. end;
  556. { this function must return a very high value ("infinity") for }
  557. { trees containing a call, the rest can be balanced more or less }
  558. { at will, probably best mainly in terms of required memory }
  559. { accesses }
  560. function node_complexity(p: tnode): cardinal;
  561. begin
  562. result := 0;
  563. while assigned(p) do
  564. begin
  565. case p.nodetype of
  566. { floating point constants usually need loading from memory }
  567. realconstn,
  568. temprefn,
  569. loadvmtaddrn,
  570. { main reason for the next one: we can't take the address of }
  571. { loadparentfpnode, so replacing it by a temp which is the }
  572. { address of this node's location and then dereferencing }
  573. { doesn't work. If changed, check whether webtbs/tw0935 }
  574. { still works with nodeinlining (JM) }
  575. loadparentfpn:
  576. begin
  577. result := 1;
  578. exit;
  579. end;
  580. loadn:
  581. begin
  582. { threadvars need a helper call }
  583. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  584. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  585. inc(result,5)
  586. else
  587. inc(result);
  588. if (result >= NODE_COMPLEXITY_INF) then
  589. result := NODE_COMPLEXITY_INF;
  590. exit;
  591. end;
  592. subscriptn:
  593. begin
  594. if is_class_or_interface(tunarynode(p).left.resultdef) then
  595. inc(result);
  596. if (result = NODE_COMPLEXITY_INF) then
  597. exit;
  598. p := tunarynode(p).left;
  599. end;
  600. blockn,
  601. callparan:
  602. p := tunarynode(p).left;
  603. notn,
  604. derefn :
  605. begin
  606. inc(result);
  607. if (result = NODE_COMPLEXITY_INF) then
  608. exit;
  609. p := tunarynode(p).left;
  610. end;
  611. typeconvn:
  612. begin
  613. { may be more complex in some cases }
  614. 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
  615. inc(result);
  616. if (result = NODE_COMPLEXITY_INF) then
  617. exit;
  618. p := tunarynode(p).left;
  619. end;
  620. vecn,
  621. statementn:
  622. begin
  623. inc(result,node_complexity(tbinarynode(p).left));
  624. if (result >= NODE_COMPLEXITY_INF) then
  625. begin
  626. result := NODE_COMPLEXITY_INF;
  627. exit;
  628. end;
  629. p := tbinarynode(p).right;
  630. end;
  631. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  632. shln,shrn,
  633. equaln,unequaln,gtn,gten,ltn,lten,
  634. assignn:
  635. begin
  636. inc(result,node_complexity(tbinarynode(p).left)+1);
  637. if (p.nodetype in [muln,divn,modn]) then
  638. inc(result,5);
  639. if (result >= NODE_COMPLEXITY_INF) then
  640. begin
  641. result := NODE_COMPLEXITY_INF;
  642. exit;
  643. end;
  644. p := tbinarynode(p).right;
  645. end;
  646. stringconstn,
  647. tempcreaten,
  648. tempdeleten,
  649. ordconstn,
  650. pointerconstn,
  651. nothingn,
  652. niln:
  653. exit;
  654. inlinen:
  655. begin
  656. { this code assumes that the inline node has }
  657. { already been firstpassed, and consequently }
  658. { that inline nodes which are transformed into }
  659. { calls already have been transformed }
  660. case tinlinenode(p).inlinenumber of
  661. in_lo_qword,
  662. in_hi_qword,
  663. in_lo_long,
  664. in_hi_long,
  665. in_lo_word,
  666. in_hi_word,
  667. in_length_x,
  668. in_assigned_x,
  669. in_pred_x,
  670. in_succ_x,
  671. in_round_real,
  672. in_trunc_real,
  673. in_int_real,
  674. in_frac_real,
  675. in_cos_real,
  676. in_sin_real,
  677. in_arctan_real,
  678. in_pi_real,
  679. in_abs_real,
  680. in_sqr_real,
  681. in_sqrt_real,
  682. in_ln_real,
  683. {$ifdef SUPPORT_UNALIGNED}
  684. in_unaligned_x,
  685. {$endif SUPPORT_UNALIGNED}
  686. in_prefetch_var:
  687. begin
  688. inc(result);
  689. p:=tunarynode(p).left;
  690. end;
  691. in_abs_long:
  692. begin
  693. inc(result,3);
  694. if (result >= NODE_COMPLEXITY_INF) then
  695. begin
  696. result:=NODE_COMPLEXITY_INF;
  697. exit;
  698. end;
  699. p:=tunarynode(p).left;
  700. end;
  701. in_sizeof_x,
  702. in_typeof_x:
  703. begin
  704. inc(result);
  705. if (tinlinenode(p).left.nodetype<>typen) then
  706. { get instance vmt }
  707. p:=tunarynode(p).left
  708. else
  709. { type vmt = global symbol, result is }
  710. { already increased above }
  711. exit;
  712. end;
  713. {$ifdef SUPPORT_MMX}
  714. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  715. {$endif SUPPORT_MMX}
  716. { load from global symbol }
  717. in_typeinfo_x,
  718. { load frame pointer }
  719. in_get_frame,
  720. in_get_caller_frame,
  721. in_get_caller_addr:
  722. begin
  723. inc(result);
  724. exit;
  725. end;
  726. in_inc_x,
  727. in_dec_x,
  728. in_include_x_y,
  729. in_exclude_x_y,
  730. in_assert_x_y :
  731. begin
  732. { operation (add, sub, or, and }
  733. inc(result);
  734. { left expression }
  735. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  736. if (result >= NODE_COMPLEXITY_INF) then
  737. begin
  738. result := NODE_COMPLEXITY_INF;
  739. exit;
  740. end;
  741. p:=tcallparanode(tunarynode(p).left).right;
  742. if assigned(p) then
  743. p:=tcallparanode(p).left;
  744. end;
  745. else
  746. begin
  747. result := NODE_COMPLEXITY_INF;
  748. exit;
  749. end;
  750. end;
  751. end;
  752. else
  753. begin
  754. result := NODE_COMPLEXITY_INF;
  755. exit;
  756. end;
  757. end;
  758. end;
  759. end;
  760. { this function returns an indication how much fpu registers
  761. will be required.
  762. Note: The algorithms need to be pessimistic to prevent a
  763. fpu stack overflow on i386 }
  764. function node_resources_fpu(p: tnode): cardinal;
  765. var
  766. res1,res2,res3 : cardinal;
  767. begin
  768. result:=0;
  769. res1:=0;
  770. res2:=0;
  771. res3:=0;
  772. if p.inheritsfrom(tunarynode) then
  773. begin
  774. if assigned(tunarynode(p).left) then
  775. res1:=node_resources_fpu(tunarynode(p).left);
  776. if p.inheritsfrom(tbinarynode) then
  777. begin
  778. if assigned(tbinarynode(p).right) then
  779. res2:=node_resources_fpu(tbinarynode(p).right);
  780. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  781. res3:=node_resources_fpu(ttertiarynode(p).third)
  782. end;
  783. end;
  784. result:=max(max(res1,res2),res3);
  785. case p.nodetype of
  786. calln:
  787. { it could be a recursive call, so we never really know the number of used fpu registers }
  788. result:=maxfpuregs;
  789. realconstn,
  790. typeconvn,
  791. loadn :
  792. begin
  793. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  794. result:=max(result,1);
  795. end;
  796. assignn,
  797. addn,subn,muln,slashn,
  798. equaln,unequaln,gtn,gten,ltn,lten :
  799. begin
  800. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  801. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  802. result:=max(result,2);
  803. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  804. inc(result);
  805. end;
  806. end;
  807. end;
  808. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  809. begin
  810. result:=fen_true;
  811. n.fileinfo:=pfileposinfo(arg)^;
  812. end;
  813. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  814. begin
  815. foreachnodestatic(n,@setnodefilepos,@filepos);
  816. end;
  817. {$ifdef FPCMT}
  818. threadvar
  819. {$else FPCMT}
  820. var
  821. {$endif FPCMT}
  822. treechanged : boolean;
  823. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  824. var
  825. hn : tnode;
  826. begin
  827. result:=fen_false;
  828. // do_typecheckpass(n);
  829. hn:=n.simplify;
  830. if assigned(hn) then
  831. begin
  832. treechanged:=true;
  833. n.free;
  834. n:=hn;
  835. typecheckpass(n);
  836. end;
  837. end;
  838. { tries to simplify the given node calling the simplify method recursively }
  839. procedure dosimplify(var n : tnode);
  840. begin
  841. repeat
  842. treechanged:=false;
  843. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  844. until not(treechanged);
  845. end;
  846. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  847. var
  848. hpropsym : tpropertysym;
  849. begin
  850. result:=false;
  851. { find property in the overriden list }
  852. hpropsym:=propsym;
  853. repeat
  854. propaccesslist:=hpropsym.propaccesslist[pap];
  855. if not propaccesslist.empty then
  856. begin
  857. result:=true;
  858. exit;
  859. end;
  860. hpropsym:=hpropsym.overridenpropsym;
  861. until not assigned(hpropsym);
  862. end;
  863. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  864. var
  865. plist : ppropaccesslistitem;
  866. begin
  867. plist:=pl.firstsym;
  868. while assigned(plist) do
  869. begin
  870. case plist^.sltype of
  871. sl_load :
  872. begin
  873. addsymref(plist^.sym);
  874. if not assigned(st) then
  875. st:=plist^.sym.owner;
  876. { p1 can already contain the loadnode of
  877. the class variable. When there is no tree yet we
  878. may need to load it for with or objects }
  879. if not assigned(p1) then
  880. begin
  881. case st.symtabletype of
  882. withsymtable :
  883. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  884. ObjectSymtable :
  885. p1:=load_self_node;
  886. end;
  887. end;
  888. if assigned(p1) then
  889. p1:=csubscriptnode.create(plist^.sym,p1)
  890. else
  891. p1:=cloadnode.create(plist^.sym,st);
  892. end;
  893. sl_subscript :
  894. begin
  895. addsymref(plist^.sym);
  896. p1:=csubscriptnode.create(plist^.sym,p1);
  897. end;
  898. sl_typeconv :
  899. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  900. sl_absolutetype :
  901. begin
  902. p1:=ctypeconvnode.create(p1,plist^.def);
  903. include(p1.flags,nf_absolute);
  904. end;
  905. sl_vec :
  906. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  907. else
  908. internalerror(200110205);
  909. end;
  910. plist:=plist^.next;
  911. end;
  912. end;
  913. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  914. var
  915. sl : tpropaccesslist;
  916. procedure addnode(p:tnode);
  917. begin
  918. case p.nodetype of
  919. subscriptn :
  920. begin
  921. addnode(tsubscriptnode(p).left);
  922. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  923. end;
  924. typeconvn :
  925. begin
  926. addnode(ttypeconvnode(p).left);
  927. if nf_absolute in ttypeconvnode(p).flags then
  928. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  929. else
  930. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  931. end;
  932. vecn :
  933. begin
  934. addnode(tvecnode(p).left);
  935. if tvecnode(p).right.nodetype=ordconstn then
  936. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  937. else
  938. begin
  939. Message(parser_e_illegal_expression);
  940. { recovery }
  941. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  942. end;
  943. end;
  944. loadn :
  945. sl.addsym(sl_load,tloadnode(p).symtableentry);
  946. else
  947. internalerror(200310282);
  948. end;
  949. end;
  950. begin
  951. sl:=tpropaccesslist.create;
  952. addnode(p1);
  953. result:=sl;
  954. end;
  955. function has_no_code(n : tnode) : boolean;
  956. begin
  957. if n=nil then
  958. begin
  959. result:=true;
  960. exit;
  961. end;
  962. result:=false;
  963. case n.nodetype of
  964. nothingn:
  965. begin
  966. result:=true;
  967. exit;
  968. end;
  969. blockn:
  970. begin
  971. result:=has_no_code(tblocknode(n).left);
  972. exit;
  973. end;
  974. end;
  975. end;
  976. end.