nutils.pas 48 KB

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