nutils.pas 37 KB

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