nutils.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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,constexp,
  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 = ({ children are processed before the parent node }
  39. pm_preprocess,
  40. { children are processed after the parent node }
  41. pm_postprocess,
  42. { children are processed after the parent node and
  43. then the parent node is processed again }
  44. pm_postandagain);
  45. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  46. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  47. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  48. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  49. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  50. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  51. { checks if the given node tree contains only nodes of the given type,
  52. if this isn't the case, an ie is thrown
  53. }
  54. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  55. procedure load_procvar_from_calln(var p1:tnode);
  56. function get_local_or_para_sym(const aname: string): tsym;
  57. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  58. function load_high_value_node(vs:tparavarsym):tnode;
  59. function load_self_node:tnode;
  60. function load_result_node:tnode;
  61. function load_self_pointer_node:tnode;
  62. function load_vmt_pointer_node:tnode;
  63. function is_self_node(p:tnode):boolean;
  64. { create a tree that loads the VMT based on a self-node of an object/class/
  65. interface }
  66. function load_vmt_for_self_node(self_node: tnode): tnode;
  67. function node_complexity(p: tnode): cardinal;
  68. function node_resources_fpu(p: tnode): cardinal;
  69. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  70. { tries to simplify the given node after inlining }
  71. procedure doinlinesimplify(var n : tnode);
  72. { creates an ordinal constant, optionally based on the result from a
  73. simplify operation: normally the type is the smallest integer type
  74. that can hold the value, but when inlining the "def" will be used instead,
  75. which was determined during an earlier typecheck pass (because the value
  76. may e.g. be a parameter to a call, which needs to be of the declared
  77. parameter type) }
  78. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  79. { returns true if n is only a tree of administrative nodes
  80. containing no code }
  81. function has_no_code(n : tnode) : boolean;
  82. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  83. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  84. { checks whether sym is a static field and if so, translates the access
  85. to the appropriate node tree }
  86. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  87. { returns true if n is an array element access of a bitpacked array with
  88. elements of the which the vitsize mod 8 <> 0, or if is a field access
  89. with bitsize mod 8 <> 0 or bitoffset mod 8 <> 0 of an element in a
  90. bitpacked structure }
  91. function is_bitpacked_access(n: tnode): boolean;
  92. { creates a load of field 'fieldname' in the record/class/...
  93. represented by n }
  94. function genloadfield(n: tnode; const fieldname: string): tnode;
  95. { returns true, if the tree given might have side effects }
  96. function might_have_sideeffects(n : tnode) : boolean;
  97. { count the number of nodes in the node tree,
  98. rough estimation how large the tree "node" is }
  99. function node_count(node : tnode) : dword;
  100. { returns true, if the value described by node is constant/immutable, this approximation is safe
  101. if no dirty tricks like buffer overflows or pointer magic are used }
  102. function is_const(node : tnode) : boolean;
  103. { returns a pointer to the real node a node refers to,
  104. skipping (absolute) equal type conversions. Returning
  105. a pointer allows the caller to move/remove/replace this
  106. node
  107. }
  108. function actualtargetnode(n : pnode) : pnode;
  109. { moves src into dest, before doing so, right is set to nil and dest is freed.
  110. Because dest and src are var parameters, this can be done inline in an existing
  111. node tree }
  112. procedure replacenode(var dest,src : tnode);
  113. { strip off deref/addr combinations when looking for a the load node of an open array/array of const
  114. since there is no possiblity to defined a pointer to an open array/array of const, we have not to
  115. take care of type casts, further, it means also that deref/addr nodes must always appear coupled
  116. }
  117. function get_open_const_array(p : tnode) : tnode;
  118. { excludes the flags passed in nf from the node tree passed }
  119. procedure node_reset_flags(p : tnode;nf : tnodeflags);
  120. implementation
  121. uses
  122. cutils,verbose,globals,
  123. symconst,symdef,
  124. defutil,defcmp,htypechk,
  125. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,ninl,
  126. cpubase,cgbase,procinfo,
  127. pass_1;
  128. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  129. function process_children(res : boolean) : boolean;
  130. var
  131. i: longint;
  132. begin
  133. result:=res;
  134. case n.nodetype of
  135. asn:
  136. if assigned(tasnode(n).call) then
  137. begin
  138. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  139. exit
  140. end;
  141. calln:
  142. begin
  143. result := foreachnode(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  144. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  145. result := foreachnode(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  146. result := foreachnode(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  147. end;
  148. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  149. begin
  150. { not in one statement, won't work because of b- }
  151. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  152. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  153. end;
  154. raisen:
  155. { frame tree }
  156. result := foreachnode(ttertiarynode(n).third,f,arg) or result;
  157. tempcreaten:
  158. { temp. initialization code }
  159. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  160. result := foreachnode(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  161. casen:
  162. begin
  163. for i := 0 to tcasenode(n).blocks.count-1 do
  164. if assigned(tcasenode(n).blocks[i]) then
  165. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  166. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  167. end;
  168. end;
  169. if n.inheritsfrom(tbinarynode) then
  170. begin
  171. { first process the "payload" of statementnodes }
  172. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  173. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  174. end
  175. else if n.inheritsfrom(tunarynode) then
  176. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  177. end;
  178. begin
  179. result := false;
  180. if not assigned(n) then
  181. exit;
  182. if procmethod=pm_preprocess then
  183. result:=process_children(result);
  184. case f(n,arg) of
  185. fen_norecurse_false:
  186. exit;
  187. fen_norecurse_true:
  188. begin
  189. result := true;
  190. exit;
  191. end;
  192. fen_true:
  193. result := true;
  194. { result is already false
  195. fen_false:
  196. result := false; }
  197. end;
  198. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  199. result:=process_children(result);
  200. if procmethod=pm_postandagain then
  201. begin
  202. case f(n,arg) of
  203. fen_norecurse_false:
  204. exit;
  205. fen_norecurse_true:
  206. begin
  207. result := true;
  208. exit;
  209. end;
  210. fen_true:
  211. result := true;
  212. end;
  213. end;
  214. end;
  215. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  216. begin
  217. result:=foreachnode(pm_postprocess,n,f,arg);
  218. end;
  219. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  220. function process_children(res : boolean) : boolean;
  221. var
  222. i: longint;
  223. begin
  224. result:=res;
  225. case n.nodetype of
  226. asn:
  227. if assigned(tasnode(n).call) then
  228. begin
  229. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  230. exit
  231. end;
  232. calln:
  233. begin
  234. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  235. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  236. result := foreachnodestatic(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  237. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  238. end;
  239. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  240. begin
  241. { not in one statement, won't work because of b- }
  242. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  243. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  244. end;
  245. raisen:
  246. { frame tree }
  247. result := foreachnodestatic(ttertiarynode(n).third,f,arg) or result;
  248. tempcreaten:
  249. { temp. initialization code }
  250. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  251. result := foreachnodestatic(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  252. casen:
  253. begin
  254. for i := 0 to tcasenode(n).blocks.count-1 do
  255. if assigned(tcasenode(n).blocks[i]) then
  256. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  257. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  258. end;
  259. end;
  260. if n.inheritsfrom(tbinarynode) then
  261. begin
  262. { first process the "payload" of statementnodes }
  263. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  264. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  265. end
  266. else if n.inheritsfrom(tunarynode) then
  267. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  268. end;
  269. begin
  270. result := false;
  271. if not assigned(n) then
  272. exit;
  273. if procmethod=pm_preprocess then
  274. result:=process_children(result);
  275. case f(n,arg) of
  276. fen_norecurse_false:
  277. exit;
  278. fen_norecurse_true:
  279. begin
  280. result := true;
  281. exit;
  282. end;
  283. fen_true:
  284. result := true;
  285. { result is already false
  286. fen_false:
  287. result := false; }
  288. end;
  289. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  290. result:=process_children(result);
  291. if procmethod=pm_postandagain then
  292. begin
  293. case f(n,arg) of
  294. fen_norecurse_false:
  295. exit;
  296. fen_norecurse_true:
  297. begin
  298. result := true;
  299. exit;
  300. end;
  301. fen_true:
  302. result := true;
  303. end;
  304. end;
  305. end;
  306. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  307. begin
  308. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  309. end;
  310. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  311. begin
  312. if not(n.nodetype in pnodetypeset(arg)^) then
  313. internalerror(200610141);
  314. result:=fen_true;
  315. end;
  316. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  317. begin
  318. foreachnodestatic(n,@do_check,@typeset);
  319. end;
  320. procedure load_procvar_from_calln(var p1:tnode);
  321. var
  322. p2 : tnode;
  323. begin
  324. if p1.nodetype<>calln then
  325. internalerror(200212251);
  326. { was it a procvar, then we simply remove the calln and
  327. reuse the right }
  328. if assigned(tcallnode(p1).right) then
  329. begin
  330. p2:=tcallnode(p1).right;
  331. tcallnode(p1).right:=nil;
  332. end
  333. else
  334. begin
  335. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  336. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  337. { when the methodpointer is typen we've something like:
  338. tobject.create. Then only the address is needed of the
  339. method without a self pointer }
  340. if assigned(tcallnode(p1).methodpointer) and
  341. (tcallnode(p1).methodpointer.nodetype<>typen) then
  342. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  343. end;
  344. typecheckpass(p2);
  345. p1.free;
  346. p1:=p2;
  347. end;
  348. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  349. var
  350. hp : tnode;
  351. begin
  352. result:=false;
  353. if (p1.resultdef.typ<>procvardef) or
  354. (tponly and
  355. not(m_tp_procvar in current_settings.modeswitches)) then
  356. exit;
  357. { ignore vecn,subscriptn }
  358. hp:=p1;
  359. repeat
  360. case hp.nodetype of
  361. vecn,
  362. derefn,
  363. typeconvn,
  364. subscriptn :
  365. hp:=tunarynode(hp).left;
  366. else
  367. break;
  368. end;
  369. until false;
  370. { a tempref is used when it is loaded from a withsymtable }
  371. if (hp.nodetype in [calln,loadn,temprefn]) then
  372. begin
  373. hp:=ccallnode.create_procvar(nil,p1);
  374. typecheckpass(hp);
  375. p1:=hp;
  376. result:=true;
  377. end;
  378. end;
  379. function get_local_or_para_sym(const aname: string): tsym;
  380. var
  381. pd : tprocdef;
  382. begin
  383. result:=nil;
  384. { is not assigned while parsing a property }
  385. if not assigned(current_procinfo) then
  386. exit;
  387. { we can't use searchsym here, because the
  388. symtablestack is not fully setup when pass1
  389. is run for nested procedures }
  390. pd:=current_procinfo.procdef;
  391. repeat
  392. result := tsym(pd.localst.Find(aname));
  393. if assigned(result) then
  394. break;
  395. result := tsym(pd.parast.Find(aname));
  396. if assigned(result) then
  397. break;
  398. { try the parent of a nested function }
  399. if assigned(pd.owner.defowner) and
  400. (pd.owner.defowner.typ=procdef) then
  401. pd:=tprocdef(pd.owner.defowner)
  402. else
  403. break;
  404. until false;
  405. end;
  406. function load_high_value_node(vs:tparavarsym):tnode;
  407. var
  408. srsym : tsym;
  409. begin
  410. result:=nil;
  411. srsym:=get_high_value_sym(vs);
  412. if assigned(srsym) then
  413. begin
  414. result:=cloadnode.create(srsym,vs.owner);
  415. typecheckpass(result);
  416. end
  417. else
  418. CGMessage(parser_e_illegal_expression);
  419. end;
  420. function load_self_node:tnode;
  421. var
  422. srsym : tsym;
  423. begin
  424. result:=nil;
  425. srsym:=get_local_or_para_sym('self');
  426. if assigned(srsym) then
  427. begin
  428. result:=cloadnode.create(srsym,srsym.owner);
  429. include(tloadnode(result).loadnodeflags,loadnf_is_self);
  430. end
  431. else
  432. begin
  433. result:=cerrornode.create;
  434. CGMessage(parser_e_illegal_expression);
  435. end;
  436. typecheckpass(result);
  437. end;
  438. function load_result_node:tnode;
  439. var
  440. srsym : tsym;
  441. begin
  442. result:=nil;
  443. srsym:=get_local_or_para_sym('result');
  444. if assigned(srsym) then
  445. result:=cloadnode.create(srsym,srsym.owner)
  446. else
  447. begin
  448. result:=cerrornode.create;
  449. CGMessage(parser_e_illegal_expression);
  450. end;
  451. typecheckpass(result);
  452. end;
  453. function load_self_pointer_node:tnode;
  454. var
  455. srsym : tsym;
  456. begin
  457. result:=nil;
  458. srsym:=get_local_or_para_sym('self');
  459. if assigned(srsym) then
  460. begin
  461. result:=cloadnode.create(srsym,srsym.owner);
  462. include(tloadnode(result).loadnodeflags,loadnf_load_self_pointer);
  463. end
  464. else
  465. begin
  466. result:=cerrornode.create;
  467. CGMessage(parser_e_illegal_expression);
  468. end;
  469. typecheckpass(result);
  470. end;
  471. function load_vmt_pointer_node:tnode;
  472. var
  473. srsym : tsym;
  474. begin
  475. result:=nil;
  476. srsym:=get_local_or_para_sym('vmt');
  477. if assigned(srsym) then
  478. result:=cloadnode.create(srsym,srsym.owner)
  479. else
  480. begin
  481. result:=cerrornode.create;
  482. CGMessage(parser_e_illegal_expression);
  483. end;
  484. typecheckpass(result);
  485. end;
  486. function is_self_node(p:tnode):boolean;
  487. begin
  488. is_self_node:=(p.nodetype=loadn) and
  489. (tloadnode(p).symtableentry.typ=paravarsym) and
  490. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  491. end;
  492. function load_vmt_for_self_node(self_node: tnode): tnode;
  493. var
  494. self_resultdef: tdef;
  495. obj_def: tobjectdef;
  496. self_temp,
  497. vmt_temp: ttempcreatenode;
  498. check_self: tnode;
  499. stat: tstatementnode;
  500. block: tblocknode;
  501. paras: tcallparanode;
  502. docheck: boolean;
  503. begin
  504. self_resultdef:=self_node.resultdef;
  505. case self_resultdef.typ of
  506. classrefdef:
  507. obj_def:=tobjectdef(tclassrefdef(self_resultdef).pointeddef);
  508. objectdef:
  509. obj_def:=tobjectdef(self_resultdef);
  510. else
  511. internalerror(2015052701);
  512. end;
  513. if is_classhelper(obj_def) then
  514. obj_def:=tobjectdef(tobjectdef(obj_def).extendeddef);
  515. docheck:=
  516. not(is_interface(obj_def)) and
  517. not(is_cppclass(obj_def)) and
  518. not(is_objc_class_or_protocol(obj_def)) and
  519. (([cs_check_object,cs_check_range]*current_settings.localswitches)<>[]);
  520. block:=nil;
  521. stat:=nil;
  522. if docheck then
  523. begin
  524. { check for nil self-pointer }
  525. block:=internalstatements(stat);
  526. self_temp:=ctempcreatenode.create_value(
  527. self_resultdef,self_resultdef.size,tt_persistent,true,
  528. self_node);
  529. addstatement(stat,self_temp);
  530. { in case of an object, self can only be nil if it's a dereferenced
  531. node somehow
  532. }
  533. if not is_object(self_resultdef) or
  534. (actualtargetnode(@self_node)^.nodetype=derefn) then
  535. begin
  536. check_self:=ctemprefnode.create(self_temp);
  537. if is_object(self_resultdef) then
  538. check_self:=caddrnode.create(check_self);
  539. addstatement(stat,cifnode.create(
  540. caddnode.create(equaln,
  541. ctypeconvnode.create_explicit(
  542. check_self,
  543. voidpointertype
  544. ),
  545. cnilnode.create),
  546. ccallnode.createintern('fpc_objecterror',nil),
  547. nil)
  548. );
  549. end;
  550. addstatement(stat,ctempdeletenode.create_normal_temp(self_temp));
  551. self_node:=ctemprefnode.create(self_temp);
  552. end;
  553. { get the VMT field in case of a class/object }
  554. if (self_resultdef.typ=objectdef) and
  555. assigned(tobjectdef(self_resultdef).vmt_field) then
  556. result:=csubscriptnode.create(tobjectdef(self_resultdef).vmt_field,self_node)
  557. { in case of a classref, the "instance" is a pointer
  558. to pointer to a VMT and there is no vmt field }
  559. else if self_resultdef.typ=classrefdef then
  560. result:=self_node
  561. { in case of an interface, the "instance" is a pointer to a pointer
  562. to a VMT -> dereference once already }
  563. else
  564. { in case of an interface/classref, the "instance" is a pointer
  565. to pointer to a VMT and there is no vmt field }
  566. result:=cderefnode.create(
  567. ctypeconvnode.create_explicit(
  568. self_node,
  569. getpointerdef(voidpointertype)
  570. )
  571. );
  572. result:=ctypeconvnode.create_explicit(
  573. result,
  574. getpointerdef(obj_def.vmt_def));
  575. typecheckpass(result);
  576. if docheck then
  577. begin
  578. { add a vmt validity check }
  579. vmt_temp:=ctempcreatenode.create_value(result.resultdef,result.resultdef.size,tt_persistent,true,result);
  580. addstatement(stat,vmt_temp);
  581. paras:=ccallparanode.create(ctemprefnode.create(vmt_temp),nil);
  582. if cs_check_object in current_settings.localswitches then
  583. begin
  584. paras:=ccallparanode.create(
  585. cloadvmtaddrnode.create(ctypenode.create(obj_def)),
  586. paras
  587. );
  588. addstatement(stat,
  589. ccallnode.createintern(
  590. 'fpc_check_object_ext',paras
  591. )
  592. );
  593. end
  594. else
  595. addstatement(stat,
  596. ccallnode.createintern(
  597. 'fpc_check_object',paras
  598. )
  599. );
  600. addstatement(stat,ctempdeletenode.create_normal_temp(vmt_temp));
  601. addstatement(stat,ctemprefnode.create(vmt_temp));
  602. result:=block;
  603. end
  604. end;
  605. { this function must return a very high value ("infinity") for }
  606. { trees containing a call, the rest can be balanced more or less }
  607. { at will, probably best mainly in terms of required memory }
  608. { accesses }
  609. function node_complexity(p: tnode): cardinal;
  610. var
  611. correction: byte;
  612. {$ifdef ARM}
  613. dummy : byte;
  614. {$endif ARM}
  615. begin
  616. result := 0;
  617. while assigned(p) do
  618. begin
  619. case p.nodetype of
  620. { floating point constants usually need loading from memory }
  621. realconstn:
  622. begin
  623. result:=2;
  624. exit;
  625. end;
  626. setconstn,
  627. stringconstn,
  628. temprefn,
  629. loadvmtaddrn,
  630. { main reason for the next one: we can't take the address of }
  631. { loadparentfpnode, so replacing it by a temp which is the }
  632. { address of this node's location and then dereferencing }
  633. { doesn't work. If changed, check whether webtbs/tw0935 }
  634. { still works with nodeinlining (JM) }
  635. loadparentfpn:
  636. begin
  637. result := 1;
  638. exit;
  639. end;
  640. loadn:
  641. begin
  642. if assigned(tloadnode(p).left) then
  643. inc(result,node_complexity(tloadnode(p).left));
  644. { threadvars need a helper call }
  645. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  646. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  647. inc(result,5)
  648. else
  649. inc(result);
  650. if (tloadnode(p).symtableentry.typ=paravarsym) and tloadnode(p).is_addr_param_load then
  651. inc(result);
  652. if (result >= NODE_COMPLEXITY_INF) then
  653. result := NODE_COMPLEXITY_INF;
  654. exit;
  655. end;
  656. subscriptn:
  657. begin
  658. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  659. is_bitpacked_access(p) then
  660. inc(result,2)
  661. else if tstoreddef(p.resultdef).is_intregable then
  662. inc(result,1);
  663. if (result = NODE_COMPLEXITY_INF) then
  664. exit;
  665. p := tunarynode(p).left;
  666. end;
  667. labeln,
  668. blockn,
  669. callparan:
  670. p := tunarynode(p).left;
  671. notn,
  672. derefn :
  673. begin
  674. inc(result);
  675. if (result = NODE_COMPLEXITY_INF) then
  676. exit;
  677. p := tunarynode(p).left;
  678. end;
  679. addrn:
  680. begin
  681. inc(result);
  682. if (result = NODE_COMPLEXITY_INF) then
  683. exit;
  684. p := tunarynode(p).left;
  685. end;
  686. typeconvn:
  687. begin
  688. { may be more complex in some cases }
  689. if not(ttypeconvnode(p).retains_value_location) then
  690. inc(result);
  691. if (result = NODE_COMPLEXITY_INF) then
  692. exit;
  693. p := tunarynode(p).left;
  694. end;
  695. vecn,
  696. statementn:
  697. begin
  698. inc(result,node_complexity(tbinarynode(p).left));
  699. if (result >= NODE_COMPLEXITY_INF) then
  700. begin
  701. result := NODE_COMPLEXITY_INF;
  702. exit;
  703. end;
  704. p := tbinarynode(p).right;
  705. end;
  706. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  707. shln,shrn,
  708. equaln,unequaln,gtn,gten,ltn,lten,
  709. assignn:
  710. begin
  711. {$ifdef CPU64BITALU}
  712. correction:=1;
  713. {$else CPU64BITALU}
  714. correction:=2;
  715. {$endif CPU64BITALU}
  716. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  717. if (p.nodetype in [muln,divn,modn]) then
  718. inc(result,5*correction*correction);
  719. if (result >= NODE_COMPLEXITY_INF) then
  720. begin
  721. result := NODE_COMPLEXITY_INF;
  722. exit;
  723. end;
  724. p := tbinarynode(p).right;
  725. end;
  726. ordconstn:
  727. begin
  728. {$ifdef ARM}
  729. if not(is_shifter_const(tordconstnode(p).value.svalue,dummy)) then
  730. result:=2;
  731. {$endif ARM}
  732. exit;
  733. end;
  734. exitn:
  735. begin
  736. inc(result,2);
  737. if (result >= NODE_COMPLEXITY_INF) then
  738. begin
  739. result := NODE_COMPLEXITY_INF;
  740. exit;
  741. end;
  742. p:=texitnode(p).left;
  743. end;
  744. tempcreaten,
  745. tempdeleten,
  746. pointerconstn,
  747. nothingn,
  748. niln:
  749. exit;
  750. inlinen:
  751. begin
  752. { this code assumes that the inline node has }
  753. { already been firstpassed, and consequently }
  754. { that inline nodes which are transformed into }
  755. { calls already have been transformed }
  756. case tinlinenode(p).inlinenumber of
  757. in_lo_qword,
  758. in_hi_qword,
  759. in_lo_long,
  760. in_hi_long,
  761. in_lo_word,
  762. in_hi_word,
  763. in_length_x,
  764. in_assigned_x,
  765. in_pred_x,
  766. in_succ_x,
  767. in_round_real,
  768. in_trunc_real,
  769. in_int_real,
  770. in_frac_real,
  771. in_pi_real,
  772. in_abs_real,
  773. in_aligned_x,
  774. in_unaligned_x,
  775. in_prefetch_var:
  776. begin
  777. inc(result);
  778. p:=tunarynode(p).left;
  779. end;
  780. in_cos_real,
  781. in_sin_real,
  782. in_arctan_real,
  783. in_sqr_real,
  784. in_sqrt_real,
  785. in_ln_real:
  786. begin
  787. inc(result,2);
  788. if (result >= NODE_COMPLEXITY_INF) then
  789. begin
  790. result:=NODE_COMPLEXITY_INF;
  791. exit;
  792. end;
  793. p:=tunarynode(p).left;
  794. end;
  795. in_abs_long:
  796. begin
  797. inc(result,3);
  798. if (result >= NODE_COMPLEXITY_INF) then
  799. begin
  800. result:=NODE_COMPLEXITY_INF;
  801. exit;
  802. end;
  803. p:=tunarynode(p).left;
  804. end;
  805. in_sizeof_x,
  806. in_typeof_x:
  807. begin
  808. inc(result);
  809. if (tinlinenode(p).left.nodetype<>typen) then
  810. { get instance vmt }
  811. p:=tunarynode(p).left
  812. else
  813. { type vmt = global symbol, result is }
  814. { already increased above }
  815. exit;
  816. end;
  817. {$ifdef SUPPORT_MMX}
  818. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  819. {$endif SUPPORT_MMX}
  820. { load from global symbol }
  821. in_typeinfo_x,
  822. { load frame pointer }
  823. in_get_frame,
  824. in_get_caller_frame,
  825. in_get_caller_addr:
  826. begin
  827. inc(result);
  828. exit;
  829. end;
  830. in_inc_x,
  831. in_dec_x,
  832. in_include_x_y,
  833. in_exclude_x_y,
  834. in_assert_x_y :
  835. begin
  836. { operation (add, sub, or, and }
  837. inc(result);
  838. { left expression }
  839. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  840. if (result >= NODE_COMPLEXITY_INF) then
  841. begin
  842. result := NODE_COMPLEXITY_INF;
  843. exit;
  844. end;
  845. p:=tcallparanode(tunarynode(p).left).right;
  846. if assigned(p) then
  847. p:=tcallparanode(p).left;
  848. end;
  849. else
  850. begin
  851. result := NODE_COMPLEXITY_INF;
  852. exit;
  853. end;
  854. end;
  855. end;
  856. else
  857. begin
  858. result := NODE_COMPLEXITY_INF;
  859. exit;
  860. end;
  861. end;
  862. end;
  863. end;
  864. { this function returns an indication how much fpu registers
  865. will be required.
  866. Note: The algorithms need to be pessimistic to prevent a
  867. fpu stack overflow on i386 }
  868. function node_resources_fpu(p: tnode): cardinal;
  869. var
  870. res1,res2,res3 : cardinal;
  871. begin
  872. result:=0;
  873. res1:=0;
  874. res2:=0;
  875. res3:=0;
  876. if p.inheritsfrom(tunarynode) then
  877. begin
  878. if assigned(tunarynode(p).left) then
  879. res1:=node_resources_fpu(tunarynode(p).left);
  880. if p.inheritsfrom(tbinarynode) then
  881. begin
  882. if assigned(tbinarynode(p).right) then
  883. res2:=node_resources_fpu(tbinarynode(p).right);
  884. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  885. res3:=node_resources_fpu(ttertiarynode(p).third)
  886. end;
  887. end;
  888. result:=max(max(res1,res2),res3);
  889. case p.nodetype of
  890. calln:
  891. { it could be a recursive call, so we never really know the number of used fpu registers }
  892. result:=maxfpuregs;
  893. realconstn,
  894. typeconvn,
  895. loadn :
  896. begin
  897. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  898. result:=max(result,1);
  899. end;
  900. assignn,
  901. addn,subn,muln,slashn,
  902. equaln,unequaln,gtn,gten,ltn,lten :
  903. begin
  904. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  905. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  906. result:=max(result,2);
  907. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  908. inc(result);
  909. end;
  910. end;
  911. end;
  912. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  913. begin
  914. result:=fen_true;
  915. n.fileinfo:=pfileposinfo(arg)^;
  916. end;
  917. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  918. begin
  919. foreachnodestatic(n,@setnodefilepos,@filepos);
  920. end;
  921. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  922. var
  923. hn : tnode;
  924. treechanged : ^boolean;
  925. begin
  926. result:=fen_false;
  927. if n.inheritsfrom(tloopnode) and
  928. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  929. begin
  930. // Try to simplify condition
  931. doinlinesimplify(tloopnode(n).left);
  932. // call directly second part below,
  933. // which might change the loopnode into
  934. // something else if the conditino is a constant node
  935. include(tloopnode(n).loopflags,lnf_simplify_processing);
  936. callsimplify(n,arg);
  937. // Be careful, n might have change node type
  938. if n.inheritsfrom(tloopnode) then
  939. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  940. end
  941. else
  942. begin
  943. hn:=n.simplify(true);
  944. if assigned(hn) then
  945. begin
  946. treechanged := arg;
  947. if assigned(treechanged) then
  948. treechanged^:=true
  949. else
  950. internalerror (201008181);
  951. n.free;
  952. n:=hn;
  953. typecheckpass(n);
  954. end;
  955. end;
  956. end;
  957. { tries to simplify the given node calling the simplify method recursively }
  958. procedure doinlinesimplify(var n : tnode);
  959. var
  960. treechanged : boolean;
  961. begin
  962. // Optimize if code first
  963. repeat
  964. treechanged:=false;
  965. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  966. until not(treechanged);
  967. end;
  968. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  969. begin
  970. if not forinline then
  971. result:=genintconstnode(value)
  972. else
  973. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  974. end;
  975. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  976. var
  977. plist : ppropaccesslistitem;
  978. begin
  979. plist:=pl.firstsym;
  980. while assigned(plist) do
  981. begin
  982. case plist^.sltype of
  983. sl_load :
  984. begin
  985. addsymref(plist^.sym);
  986. if not assigned(st) then
  987. st:=plist^.sym.owner;
  988. if (plist^.sym.typ<>staticvarsym) then
  989. begin
  990. { p1 can already contain the loadnode of
  991. the class variable. When there is no tree yet we
  992. may need to load it for with or objects }
  993. if not assigned(p1) then
  994. begin
  995. case st.symtabletype of
  996. withsymtable :
  997. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  998. ObjectSymtable :
  999. p1:=load_self_node;
  1000. end;
  1001. end
  1002. end
  1003. else
  1004. begin
  1005. p1.free;
  1006. p1:=nil;
  1007. end;
  1008. if assigned(p1) then
  1009. p1:=csubscriptnode.create(plist^.sym,p1)
  1010. else
  1011. p1:=cloadnode.create(plist^.sym,st);
  1012. end;
  1013. sl_subscript :
  1014. begin
  1015. addsymref(plist^.sym);
  1016. p1:=csubscriptnode.create(plist^.sym,p1);
  1017. end;
  1018. sl_typeconv :
  1019. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1020. sl_absolutetype :
  1021. begin
  1022. p1:=ctypeconvnode.create(p1,plist^.def);
  1023. include(p1.flags,nf_absolute);
  1024. end;
  1025. sl_vec :
  1026. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1027. else
  1028. internalerror(200110205);
  1029. end;
  1030. plist:=plist^.next;
  1031. end;
  1032. end;
  1033. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1034. var
  1035. sl : tpropaccesslist;
  1036. procedure addnode(p:tnode);
  1037. begin
  1038. case p.nodetype of
  1039. subscriptn :
  1040. begin
  1041. addnode(tsubscriptnode(p).left);
  1042. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1043. end;
  1044. typeconvn :
  1045. begin
  1046. addnode(ttypeconvnode(p).left);
  1047. if nf_absolute in ttypeconvnode(p).flags then
  1048. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1049. else
  1050. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1051. end;
  1052. vecn :
  1053. begin
  1054. addnode(tvecnode(p).left);
  1055. if tvecnode(p).right.nodetype=ordconstn then
  1056. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1057. else
  1058. begin
  1059. Message(parser_e_illegal_expression);
  1060. { recovery }
  1061. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1062. end;
  1063. end;
  1064. loadn :
  1065. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1066. else
  1067. internalerror(200310282);
  1068. end;
  1069. end;
  1070. begin
  1071. sl:=tpropaccesslist.create;
  1072. addnode(p1);
  1073. result:=sl;
  1074. end;
  1075. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  1076. function handle_generic_staticfield_access:boolean;
  1077. var
  1078. tmp : tstoreddef;
  1079. pd : tprocdef;
  1080. begin
  1081. { in case we have a specialization inside a generic (thus the static var sym does not
  1082. exist) we simply simulate a non static access to avoid unnecessary errors }
  1083. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1084. begin
  1085. tmp:=tstoreddef(sym.owner.defowner);
  1086. while assigned(tmp) do
  1087. begin
  1088. if df_generic in tmp.defoptions then
  1089. begin
  1090. p1.free;
  1091. if assigned(current_procinfo) then
  1092. begin
  1093. pd:=current_procinfo.get_normal_proc.procdef;
  1094. if assigned(pd) and pd.no_self_node then
  1095. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1096. else
  1097. p1:=load_self_node;
  1098. end
  1099. else
  1100. p1:=load_self_node;
  1101. p1:=csubscriptnode.create(sym,p1);
  1102. exit(true);
  1103. end;
  1104. tmp:=tstoreddef(tmp.owner.defowner);
  1105. end;
  1106. end;
  1107. result:=false;
  1108. end;
  1109. var
  1110. static_name: shortstring;
  1111. srsymtable: tsymtable;
  1112. begin
  1113. result:=false;
  1114. { generate access code }
  1115. if (sp_static in sym.symoptions) then
  1116. begin
  1117. result:=true;
  1118. if handle_generic_staticfield_access then
  1119. exit;
  1120. if not nested then
  1121. static_name:=lower(sym.owner.name^)+'_'+sym.name
  1122. else
  1123. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  1124. if sym.owner.defowner.typ=objectdef then
  1125. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  1126. else
  1127. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  1128. if assigned(sym) then
  1129. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1130. p1.free;
  1131. p1:=nil;
  1132. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1133. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1134. end;
  1135. end;
  1136. function is_bitpacked_access(n: tnode): boolean;
  1137. begin
  1138. case n.nodetype of
  1139. vecn:
  1140. result:=
  1141. is_packed_array(tvecnode(n).left.resultdef) and
  1142. { only orddefs and enumdefs are actually bitpacked. Don't consider
  1143. e.g. an access to a 3-byte record as "bitpacked", since it
  1144. isn't }
  1145. (tvecnode(n).left.resultdef.typ = arraydef) and
  1146. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  1147. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  1148. subscriptn:
  1149. result:=
  1150. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1151. { see above }
  1152. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  1153. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  1154. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1155. else
  1156. result:=false;
  1157. end;
  1158. end;
  1159. function genloadfield(n: tnode; const fieldname: string): tnode;
  1160. var
  1161. vs : tsym;
  1162. begin
  1163. if not assigned(n.resultdef) then
  1164. typecheckpass(n);
  1165. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1166. if not assigned(vs) or
  1167. (vs.typ<>fieldvarsym) then
  1168. internalerror(2010061902);
  1169. result:=csubscriptnode.create(vs,n);
  1170. end;
  1171. function has_no_code(n : tnode) : boolean;
  1172. begin
  1173. if n=nil then
  1174. begin
  1175. result:=true;
  1176. exit;
  1177. end;
  1178. result:=false;
  1179. case n.nodetype of
  1180. nothingn:
  1181. begin
  1182. result:=true;
  1183. exit;
  1184. end;
  1185. blockn:
  1186. begin
  1187. result:=has_no_code(tblocknode(n).left);
  1188. exit;
  1189. end;
  1190. statementn:
  1191. begin
  1192. repeat
  1193. result:=has_no_code(tstatementnode(n).left);
  1194. n:=tstatementnode(n).right;
  1195. until not(result) or not assigned(n);
  1196. exit;
  1197. end;
  1198. end;
  1199. end;
  1200. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1201. begin
  1202. result:=fen_false;
  1203. if (n.nodetype in [assignn,calln,asmn]) or
  1204. ((n.nodetype=inlinen) and
  1205. (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  1206. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,in_settextbuf_file_x,
  1207. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  1208. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle])
  1209. ) then
  1210. result:=fen_norecurse_true;
  1211. end;
  1212. function might_have_sideeffects(n : tnode) : boolean;
  1213. begin
  1214. result:=foreachnodestatic(n,@check_for_sideeffect,nil);
  1215. end;
  1216. var
  1217. nodecount : dword;
  1218. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1219. begin
  1220. inc(nodecount);
  1221. result:=fen_false;
  1222. end;
  1223. function node_count(node : tnode) : dword;
  1224. begin
  1225. nodecount:=0;
  1226. foreachnodestatic(node,@donodecount,nil);
  1227. result:=nodecount;
  1228. end;
  1229. function is_const(node : tnode) : boolean;
  1230. begin
  1231. result:=(node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempinfo^.flags)
  1232. end;
  1233. function actualtargetnode(n : pnode) : pnode;
  1234. begin
  1235. result:=n;
  1236. case n^.nodetype of
  1237. typeconvn:
  1238. if ttypeconvnode(n^).retains_value_location then
  1239. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1240. end;
  1241. end;
  1242. procedure replacenode(var dest,src : tnode);
  1243. var
  1244. t : tnode;
  1245. begin
  1246. t:=src;
  1247. { set src nil before free'ing dest because
  1248. src could be part of dest }
  1249. src:=nil;
  1250. dest.Free;
  1251. dest:=t;
  1252. end;
  1253. function get_open_const_array(p : tnode) : tnode;
  1254. begin
  1255. result:=p;
  1256. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1257. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  1258. end;
  1259. function do_node_reset_flags(var n: tnode; arg: pointer): foreachnoderesult;
  1260. begin
  1261. result:=fen_false;
  1262. n.flags:=n.flags-tnodeflags(arg^);
  1263. end;
  1264. procedure node_reset_flags(p : tnode; nf : tnodeflags);
  1265. begin
  1266. foreachnodestatic(p,@do_node_reset_flags,@nf);
  1267. end;
  1268. end.