nutils.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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; 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. cpointerdef.getreusable(voidpointertype)
  570. )
  571. );
  572. result:=ctypeconvnode.create_explicit(
  573. result,
  574. cpointerdef.getreusable(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. p := tunarynode(p).left;
  670. callparan:
  671. begin
  672. { call to decr? }
  673. if is_managed_type(tunarynode(p).left.resultdef) and
  674. assigned(tcallparanode(p).parasym) and (tcallparanode(p).parasym.varspez=vs_out) then
  675. begin
  676. result:=NODE_COMPLEXITY_INF;
  677. exit;
  678. end
  679. else
  680. begin
  681. inc(result);
  682. if (result = NODE_COMPLEXITY_INF) then
  683. exit;
  684. p := tunarynode(p).left;
  685. end;
  686. end;
  687. notn,
  688. derefn :
  689. begin
  690. inc(result);
  691. if (result = NODE_COMPLEXITY_INF) then
  692. exit;
  693. p := tunarynode(p).left;
  694. end;
  695. addrn:
  696. begin
  697. inc(result);
  698. if (result = NODE_COMPLEXITY_INF) then
  699. exit;
  700. p := tunarynode(p).left;
  701. end;
  702. typeconvn:
  703. begin
  704. { may be more complex in some cases }
  705. if not(ttypeconvnode(p).retains_value_location) then
  706. inc(result);
  707. if (result = NODE_COMPLEXITY_INF) then
  708. exit;
  709. p := tunarynode(p).left;
  710. end;
  711. vecn,
  712. statementn:
  713. begin
  714. inc(result,node_complexity(tbinarynode(p).left));
  715. if (result >= NODE_COMPLEXITY_INF) then
  716. begin
  717. result := NODE_COMPLEXITY_INF;
  718. exit;
  719. end;
  720. p := tbinarynode(p).right;
  721. end;
  722. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  723. shln,shrn,
  724. equaln,unequaln,gtn,gten,ltn,lten,
  725. assignn:
  726. begin
  727. {$ifdef CPU64BITALU}
  728. correction:=1;
  729. {$else CPU64BITALU}
  730. correction:=2;
  731. {$endif CPU64BITALU}
  732. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  733. if (p.nodetype in [muln,divn,modn]) then
  734. inc(result,5*correction*correction);
  735. if (result >= NODE_COMPLEXITY_INF) then
  736. begin
  737. result := NODE_COMPLEXITY_INF;
  738. exit;
  739. end;
  740. p := tbinarynode(p).right;
  741. end;
  742. ordconstn:
  743. begin
  744. {$ifdef ARM}
  745. if not(is_shifter_const(aint(tordconstnode(p).value.svalue),dummy)) then
  746. result:=2;
  747. {$endif ARM}
  748. exit;
  749. end;
  750. exitn:
  751. begin
  752. inc(result,2);
  753. if (result >= NODE_COMPLEXITY_INF) then
  754. begin
  755. result := NODE_COMPLEXITY_INF;
  756. exit;
  757. end;
  758. p:=texitnode(p).left;
  759. end;
  760. tempcreaten,
  761. tempdeleten,
  762. pointerconstn,
  763. nothingn,
  764. niln:
  765. exit;
  766. inlinen:
  767. begin
  768. { this code assumes that the inline node has }
  769. { already been firstpassed, and consequently }
  770. { that inline nodes which are transformed into }
  771. { calls already have been transformed }
  772. case tinlinenode(p).inlinenumber of
  773. in_lo_qword,
  774. in_hi_qword,
  775. in_lo_long,
  776. in_hi_long,
  777. in_lo_word,
  778. in_hi_word,
  779. in_length_x,
  780. in_assigned_x,
  781. in_pred_x,
  782. in_succ_x,
  783. in_round_real,
  784. in_trunc_real,
  785. in_int_real,
  786. in_frac_real,
  787. in_pi_real,
  788. in_abs_real,
  789. in_aligned_x,
  790. in_unaligned_x,
  791. in_prefetch_var:
  792. begin
  793. inc(result);
  794. p:=tunarynode(p).left;
  795. end;
  796. in_cos_real,
  797. in_sin_real,
  798. in_arctan_real,
  799. in_sqr_real,
  800. in_sqrt_real,
  801. in_ln_real:
  802. begin
  803. inc(result,2);
  804. if (result >= NODE_COMPLEXITY_INF) then
  805. begin
  806. result:=NODE_COMPLEXITY_INF;
  807. exit;
  808. end;
  809. p:=tunarynode(p).left;
  810. end;
  811. in_abs_long:
  812. begin
  813. inc(result,3);
  814. if (result >= NODE_COMPLEXITY_INF) then
  815. begin
  816. result:=NODE_COMPLEXITY_INF;
  817. exit;
  818. end;
  819. p:=tunarynode(p).left;
  820. end;
  821. in_sizeof_x,
  822. in_typeof_x:
  823. begin
  824. inc(result);
  825. if (tinlinenode(p).left.nodetype<>typen) then
  826. { get instance vmt }
  827. p:=tunarynode(p).left
  828. else
  829. { type vmt = global symbol, result is }
  830. { already increased above }
  831. exit;
  832. end;
  833. {$ifdef SUPPORT_MMX}
  834. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  835. {$endif SUPPORT_MMX}
  836. { load from global symbol }
  837. in_typeinfo_x,
  838. { load frame pointer }
  839. in_get_frame,
  840. in_get_caller_frame,
  841. in_get_caller_addr:
  842. begin
  843. inc(result);
  844. exit;
  845. end;
  846. in_inc_x,
  847. in_dec_x,
  848. in_include_x_y,
  849. in_exclude_x_y,
  850. in_assert_x_y :
  851. begin
  852. { operation (add, sub, or, and }
  853. inc(result);
  854. { left expression }
  855. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  856. if (result >= NODE_COMPLEXITY_INF) then
  857. begin
  858. result := NODE_COMPLEXITY_INF;
  859. exit;
  860. end;
  861. p:=tcallparanode(tunarynode(p).left).right;
  862. if assigned(p) then
  863. p:=tcallparanode(p).left;
  864. end;
  865. else
  866. begin
  867. result := NODE_COMPLEXITY_INF;
  868. exit;
  869. end;
  870. end;
  871. end;
  872. else
  873. begin
  874. result := NODE_COMPLEXITY_INF;
  875. exit;
  876. end;
  877. end;
  878. end;
  879. end;
  880. { this function returns an indication how much fpu registers
  881. will be required.
  882. Note: The algorithms need to be pessimistic to prevent a
  883. fpu stack overflow on i386 }
  884. function node_resources_fpu(p: tnode): cardinal;
  885. var
  886. res1,res2,res3 : cardinal;
  887. begin
  888. result:=0;
  889. res1:=0;
  890. res2:=0;
  891. res3:=0;
  892. if p.inheritsfrom(tunarynode) then
  893. begin
  894. if assigned(tunarynode(p).left) then
  895. res1:=node_resources_fpu(tunarynode(p).left);
  896. if p.inheritsfrom(tbinarynode) then
  897. begin
  898. if assigned(tbinarynode(p).right) then
  899. res2:=node_resources_fpu(tbinarynode(p).right);
  900. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  901. res3:=node_resources_fpu(ttertiarynode(p).third)
  902. end;
  903. end;
  904. result:=max(max(res1,res2),res3);
  905. case p.nodetype of
  906. calln:
  907. { it could be a recursive call, so we never really know the number of used fpu registers }
  908. result:=maxfpuregs;
  909. realconstn,
  910. typeconvn,
  911. loadn :
  912. begin
  913. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  914. result:=max(result,1);
  915. end;
  916. assignn,
  917. addn,subn,muln,slashn,
  918. equaln,unequaln,gtn,gten,ltn,lten :
  919. begin
  920. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  921. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  922. result:=max(result,2);
  923. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  924. inc(result);
  925. end;
  926. end;
  927. end;
  928. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  929. begin
  930. result:=fen_true;
  931. n.fileinfo:=pfileposinfo(arg)^;
  932. end;
  933. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  934. begin
  935. foreachnodestatic(n,@setnodefilepos,@filepos);
  936. end;
  937. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  938. var
  939. hn : tnode;
  940. treechanged : ^boolean;
  941. begin
  942. result:=fen_false;
  943. if n.inheritsfrom(tloopnode) and
  944. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  945. begin
  946. // Try to simplify condition
  947. doinlinesimplify(tloopnode(n).left);
  948. // call directly second part below,
  949. // which might change the loopnode into
  950. // something else if the conditino is a constant node
  951. include(tloopnode(n).loopflags,lnf_simplify_processing);
  952. callsimplify(n,arg);
  953. // Be careful, n might have change node type
  954. if n.inheritsfrom(tloopnode) then
  955. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  956. end
  957. else
  958. begin
  959. hn:=n.simplify(true);
  960. if assigned(hn) then
  961. begin
  962. treechanged := arg;
  963. if assigned(treechanged) then
  964. treechanged^:=true
  965. else
  966. internalerror (201008181);
  967. n.free;
  968. n:=hn;
  969. typecheckpass(n);
  970. end;
  971. end;
  972. end;
  973. { tries to simplify the given node calling the simplify method recursively }
  974. procedure doinlinesimplify(var n : tnode);
  975. var
  976. treechanged : boolean;
  977. begin
  978. // Optimize if code first
  979. repeat
  980. treechanged:=false;
  981. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  982. until not(treechanged);
  983. end;
  984. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  985. begin
  986. if not forinline then
  987. result:=genintconstnode(value)
  988. else
  989. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  990. end;
  991. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  992. var
  993. plist : ppropaccesslistitem;
  994. begin
  995. plist:=pl.firstsym;
  996. while assigned(plist) do
  997. begin
  998. case plist^.sltype of
  999. sl_load :
  1000. begin
  1001. addsymref(plist^.sym);
  1002. if not assigned(st) then
  1003. st:=plist^.sym.owner;
  1004. if (plist^.sym.typ<>staticvarsym) then
  1005. begin
  1006. { p1 can already contain the loadnode of
  1007. the class variable. When there is no tree yet we
  1008. may need to load it for with or objects }
  1009. if not assigned(p1) then
  1010. begin
  1011. case st.symtabletype of
  1012. withsymtable :
  1013. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1014. ObjectSymtable :
  1015. p1:=load_self_node;
  1016. end;
  1017. end
  1018. end
  1019. else
  1020. begin
  1021. p1.free;
  1022. p1:=nil;
  1023. end;
  1024. if assigned(p1) then
  1025. p1:=csubscriptnode.create(plist^.sym,p1)
  1026. else
  1027. p1:=cloadnode.create(plist^.sym,st);
  1028. end;
  1029. sl_subscript :
  1030. begin
  1031. addsymref(plist^.sym);
  1032. p1:=csubscriptnode.create(plist^.sym,p1);
  1033. end;
  1034. sl_typeconv :
  1035. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1036. sl_absolutetype :
  1037. begin
  1038. p1:=ctypeconvnode.create(p1,plist^.def);
  1039. include(p1.flags,nf_absolute);
  1040. end;
  1041. sl_vec :
  1042. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1043. else
  1044. internalerror(200110205);
  1045. end;
  1046. plist:=plist^.next;
  1047. end;
  1048. end;
  1049. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1050. var
  1051. sl : tpropaccesslist;
  1052. procedure addnode(p:tnode);
  1053. begin
  1054. case p.nodetype of
  1055. subscriptn :
  1056. begin
  1057. addnode(tsubscriptnode(p).left);
  1058. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1059. end;
  1060. typeconvn :
  1061. begin
  1062. addnode(ttypeconvnode(p).left);
  1063. if nf_absolute in ttypeconvnode(p).flags then
  1064. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1065. else
  1066. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1067. end;
  1068. vecn :
  1069. begin
  1070. addnode(tvecnode(p).left);
  1071. if tvecnode(p).right.nodetype=ordconstn then
  1072. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1073. else
  1074. begin
  1075. Message(parser_e_illegal_expression);
  1076. { recovery }
  1077. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1078. end;
  1079. end;
  1080. loadn :
  1081. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1082. else
  1083. internalerror(200310282);
  1084. end;
  1085. end;
  1086. begin
  1087. sl:=tpropaccesslist.create;
  1088. addnode(p1);
  1089. result:=sl;
  1090. end;
  1091. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  1092. function handle_generic_staticfield_access:boolean;
  1093. var
  1094. tmp : tstoreddef;
  1095. pd : tprocdef;
  1096. begin
  1097. { in case we have a specialization inside a generic (thus the static var sym does not
  1098. exist) we simply simulate a non static access to avoid unnecessary errors }
  1099. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1100. begin
  1101. tmp:=tstoreddef(sym.owner.defowner);
  1102. while assigned(tmp) do
  1103. begin
  1104. if df_generic in tmp.defoptions then
  1105. begin
  1106. p1.free;
  1107. if assigned(current_procinfo) then
  1108. begin
  1109. pd:=current_procinfo.get_normal_proc.procdef;
  1110. if assigned(pd) and pd.no_self_node then
  1111. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1112. else
  1113. p1:=load_self_node;
  1114. end
  1115. else
  1116. p1:=load_self_node;
  1117. p1:=csubscriptnode.create(sym,p1);
  1118. exit(true);
  1119. end;
  1120. tmp:=tstoreddef(tmp.owner.defowner);
  1121. end;
  1122. end;
  1123. result:=false;
  1124. end;
  1125. var
  1126. static_name: shortstring;
  1127. srsymtable: tsymtable;
  1128. begin
  1129. result:=false;
  1130. { generate access code }
  1131. if (sp_static in sym.symoptions) then
  1132. begin
  1133. result:=true;
  1134. if handle_generic_staticfield_access then
  1135. exit;
  1136. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  1137. if sym.owner.defowner.typ=objectdef then
  1138. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  1139. else
  1140. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  1141. if assigned(sym) then
  1142. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1143. p1.free;
  1144. p1:=nil;
  1145. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1146. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1147. end;
  1148. end;
  1149. function is_bitpacked_access(n: tnode): boolean;
  1150. begin
  1151. case n.nodetype of
  1152. vecn:
  1153. result:=
  1154. is_packed_array(tvecnode(n).left.resultdef) and
  1155. { only orddefs and enumdefs are actually bitpacked. Don't consider
  1156. e.g. an access to a 3-byte record as "bitpacked", since it
  1157. isn't }
  1158. (tvecnode(n).left.resultdef.typ = arraydef) and
  1159. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  1160. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  1161. subscriptn:
  1162. result:=
  1163. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1164. { see above }
  1165. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  1166. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  1167. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1168. else
  1169. result:=false;
  1170. end;
  1171. end;
  1172. function genloadfield(n: tnode; const fieldname: string): tnode;
  1173. var
  1174. vs : tsym;
  1175. begin
  1176. if not assigned(n.resultdef) then
  1177. typecheckpass(n);
  1178. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1179. if not assigned(vs) or
  1180. (vs.typ<>fieldvarsym) then
  1181. internalerror(2010061902);
  1182. result:=csubscriptnode.create(vs,n);
  1183. end;
  1184. function has_no_code(n : tnode) : boolean;
  1185. begin
  1186. if n=nil then
  1187. begin
  1188. result:=true;
  1189. exit;
  1190. end;
  1191. result:=false;
  1192. case n.nodetype of
  1193. nothingn:
  1194. begin
  1195. result:=true;
  1196. exit;
  1197. end;
  1198. blockn:
  1199. begin
  1200. result:=has_no_code(tblocknode(n).left);
  1201. exit;
  1202. end;
  1203. statementn:
  1204. begin
  1205. repeat
  1206. result:=has_no_code(tstatementnode(n).left);
  1207. n:=tstatementnode(n).right;
  1208. until not(result) or not assigned(n);
  1209. exit;
  1210. end;
  1211. end;
  1212. end;
  1213. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1214. begin
  1215. result:=fen_false;
  1216. if (n.nodetype in [assignn,calln,asmn]) or
  1217. ((n.nodetype=inlinen) and
  1218. (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  1219. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,in_settextbuf_file_x,
  1220. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  1221. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle])
  1222. ) then
  1223. result:=fen_norecurse_true;
  1224. end;
  1225. function might_have_sideeffects(n : tnode) : boolean;
  1226. begin
  1227. result:=foreachnodestatic(n,@check_for_sideeffect,nil);
  1228. end;
  1229. var
  1230. nodecount : dword;
  1231. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1232. begin
  1233. inc(nodecount);
  1234. result:=fen_false;
  1235. end;
  1236. function node_count(node : tnode) : dword;
  1237. begin
  1238. nodecount:=0;
  1239. foreachnodestatic(node,@donodecount,nil);
  1240. result:=nodecount;
  1241. end;
  1242. function is_const(node : tnode) : boolean;
  1243. begin
  1244. result:=is_constnode(node) or
  1245. ((node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempinfo^.flags)) or
  1246. ((node.nodetype=loadn) and (tloadnode(node).symtableentry.typ=paravarsym) and (tparavarsym(tloadnode(node).symtableentry).varspez in [vs_const,vs_constref]));
  1247. end;
  1248. function actualtargetnode(n : pnode) : pnode;
  1249. begin
  1250. result:=n;
  1251. case n^.nodetype of
  1252. typeconvn:
  1253. if ttypeconvnode(n^).retains_value_location then
  1254. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1255. end;
  1256. end;
  1257. procedure replacenode(var dest,src : tnode);
  1258. var
  1259. t : tnode;
  1260. begin
  1261. t:=src;
  1262. { set src nil before free'ing dest because
  1263. src could be part of dest }
  1264. src:=nil;
  1265. dest.Free;
  1266. dest:=t;
  1267. end;
  1268. function get_open_const_array(p : tnode) : tnode;
  1269. begin
  1270. result:=p;
  1271. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1272. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  1273. end;
  1274. function do_node_reset_flags(var n: tnode; arg: pointer): foreachnoderesult;
  1275. begin
  1276. result:=fen_false;
  1277. n.flags:=n.flags-tnodeflags(arg^);
  1278. end;
  1279. procedure node_reset_flags(p : tnode; nf : tnodeflags);
  1280. begin
  1281. foreachnodestatic(p,@do_node_reset_flags,@nf);
  1282. end;
  1283. end.