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; 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. blockn:
  367. hp:=laststatement(tblocknode(hp)).left
  368. else
  369. break;
  370. end;
  371. until false;
  372. { a tempref is used when it is loaded from a withsymtable }
  373. if (hp.nodetype in [calln,loadn,temprefn]) then
  374. begin
  375. hp:=ccallnode.create_procvar(nil,p1);
  376. typecheckpass(hp);
  377. p1:=hp;
  378. result:=true;
  379. end;
  380. end;
  381. function get_local_or_para_sym(const aname: string): tsym;
  382. var
  383. pd : tprocdef;
  384. begin
  385. result:=nil;
  386. { is not assigned while parsing a property }
  387. if not assigned(current_procinfo) then
  388. exit;
  389. { we can't use searchsym here, because the
  390. symtablestack is not fully setup when pass1
  391. is run for nested procedures }
  392. pd:=current_procinfo.procdef;
  393. repeat
  394. result := tsym(pd.localst.Find(aname));
  395. if assigned(result) then
  396. break;
  397. result := tsym(pd.parast.Find(aname));
  398. if assigned(result) then
  399. break;
  400. { try the parent of a nested function }
  401. if assigned(pd.owner.defowner) and
  402. (pd.owner.defowner.typ=procdef) then
  403. pd:=tprocdef(pd.owner.defowner)
  404. else
  405. break;
  406. until false;
  407. end;
  408. function load_high_value_node(vs:tparavarsym):tnode;
  409. var
  410. srsym : tsym;
  411. begin
  412. result:=nil;
  413. srsym:=get_high_value_sym(vs);
  414. if assigned(srsym) then
  415. begin
  416. result:=cloadnode.create(srsym,vs.owner);
  417. typecheckpass(result);
  418. end
  419. else
  420. CGMessage(parser_e_illegal_expression);
  421. end;
  422. function load_self_node:tnode;
  423. var
  424. srsym : tsym;
  425. begin
  426. result:=nil;
  427. srsym:=get_local_or_para_sym('self');
  428. if assigned(srsym) then
  429. begin
  430. result:=cloadnode.create(srsym,srsym.owner);
  431. include(tloadnode(result).loadnodeflags,loadnf_is_self);
  432. end
  433. else
  434. begin
  435. result:=cerrornode.create;
  436. CGMessage(parser_e_illegal_expression);
  437. end;
  438. typecheckpass(result);
  439. end;
  440. function load_result_node:tnode;
  441. var
  442. srsym : tsym;
  443. begin
  444. result:=nil;
  445. srsym:=get_local_or_para_sym('result');
  446. if assigned(srsym) then
  447. result:=cloadnode.create(srsym,srsym.owner)
  448. else
  449. begin
  450. result:=cerrornode.create;
  451. CGMessage(parser_e_illegal_expression);
  452. end;
  453. typecheckpass(result);
  454. end;
  455. function load_self_pointer_node:tnode;
  456. var
  457. srsym : tsym;
  458. begin
  459. result:=nil;
  460. srsym:=get_local_or_para_sym('self');
  461. if assigned(srsym) then
  462. begin
  463. result:=cloadnode.create(srsym,srsym.owner);
  464. include(tloadnode(result).loadnodeflags,loadnf_load_self_pointer);
  465. end
  466. else
  467. begin
  468. result:=cerrornode.create;
  469. CGMessage(parser_e_illegal_expression);
  470. end;
  471. typecheckpass(result);
  472. end;
  473. function load_vmt_pointer_node:tnode;
  474. var
  475. srsym : tsym;
  476. begin
  477. result:=nil;
  478. srsym:=get_local_or_para_sym('vmt');
  479. if assigned(srsym) then
  480. result:=cloadnode.create(srsym,srsym.owner)
  481. else
  482. begin
  483. result:=cerrornode.create;
  484. CGMessage(parser_e_illegal_expression);
  485. end;
  486. typecheckpass(result);
  487. end;
  488. function is_self_node(p:tnode):boolean;
  489. begin
  490. is_self_node:=(p.nodetype=loadn) and
  491. (tloadnode(p).symtableentry.typ=paravarsym) and
  492. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  493. end;
  494. function load_vmt_for_self_node(self_node: tnode): tnode;
  495. var
  496. self_resultdef: tdef;
  497. obj_def: tobjectdef;
  498. self_temp,
  499. vmt_temp: ttempcreatenode;
  500. check_self: tnode;
  501. stat: tstatementnode;
  502. block: tblocknode;
  503. paras: tcallparanode;
  504. docheck: boolean;
  505. begin
  506. self_resultdef:=self_node.resultdef;
  507. case self_resultdef.typ of
  508. classrefdef:
  509. obj_def:=tobjectdef(tclassrefdef(self_resultdef).pointeddef);
  510. objectdef:
  511. obj_def:=tobjectdef(self_resultdef);
  512. else
  513. internalerror(2015052701);
  514. end;
  515. if is_classhelper(obj_def) then
  516. obj_def:=tobjectdef(tobjectdef(obj_def).extendeddef);
  517. docheck:=
  518. not(is_interface(obj_def)) and
  519. not(is_cppclass(obj_def)) and
  520. not(is_objc_class_or_protocol(obj_def)) and
  521. (([cs_check_object,cs_check_range]*current_settings.localswitches)<>[]);
  522. block:=nil;
  523. stat:=nil;
  524. if docheck then
  525. begin
  526. { check for nil self-pointer }
  527. block:=internalstatements(stat);
  528. self_temp:=ctempcreatenode.create_value(
  529. self_resultdef,self_resultdef.size,tt_persistent,true,
  530. self_node);
  531. addstatement(stat,self_temp);
  532. { in case of an object, self can only be nil if it's a dereferenced
  533. node somehow
  534. }
  535. if not is_object(self_resultdef) or
  536. (actualtargetnode(@self_node)^.nodetype=derefn) then
  537. begin
  538. check_self:=ctemprefnode.create(self_temp);
  539. if is_object(self_resultdef) then
  540. check_self:=caddrnode.create(check_self);
  541. addstatement(stat,cifnode.create(
  542. caddnode.create(equaln,
  543. ctypeconvnode.create_explicit(
  544. check_self,
  545. voidpointertype
  546. ),
  547. cnilnode.create),
  548. ccallnode.createintern('fpc_objecterror',nil),
  549. nil)
  550. );
  551. end;
  552. addstatement(stat,ctempdeletenode.create_normal_temp(self_temp));
  553. self_node:=ctemprefnode.create(self_temp);
  554. end;
  555. { get the VMT field in case of a class/object }
  556. if (self_resultdef.typ=objectdef) and
  557. assigned(tobjectdef(self_resultdef).vmt_field) then
  558. result:=csubscriptnode.create(tobjectdef(self_resultdef).vmt_field,self_node)
  559. { in case of a classref, the "instance" is a pointer
  560. to pointer to a VMT and there is no vmt field }
  561. else if self_resultdef.typ=classrefdef then
  562. result:=self_node
  563. { in case of an interface, the "instance" is a pointer to a pointer
  564. to a VMT -> dereference once already }
  565. else
  566. { in case of an interface/classref, the "instance" is a pointer
  567. to pointer to a VMT and there is no vmt field }
  568. result:=cderefnode.create(
  569. ctypeconvnode.create_explicit(
  570. self_node,
  571. cpointerdef.getreusable(voidpointertype)
  572. )
  573. );
  574. result:=ctypeconvnode.create_explicit(
  575. result,
  576. cpointerdef.getreusable(obj_def.vmt_def));
  577. typecheckpass(result);
  578. if docheck then
  579. begin
  580. { add a vmt validity check }
  581. vmt_temp:=ctempcreatenode.create_value(result.resultdef,result.resultdef.size,tt_persistent,true,result);
  582. addstatement(stat,vmt_temp);
  583. paras:=ccallparanode.create(ctemprefnode.create(vmt_temp),nil);
  584. if cs_check_object in current_settings.localswitches then
  585. begin
  586. paras:=ccallparanode.create(
  587. cloadvmtaddrnode.create(ctypenode.create(obj_def)),
  588. paras
  589. );
  590. addstatement(stat,
  591. ccallnode.createintern(
  592. 'fpc_check_object_ext',paras
  593. )
  594. );
  595. end
  596. else
  597. addstatement(stat,
  598. ccallnode.createintern(
  599. 'fpc_check_object',paras
  600. )
  601. );
  602. addstatement(stat,ctempdeletenode.create_normal_temp(vmt_temp));
  603. addstatement(stat,ctemprefnode.create(vmt_temp));
  604. result:=block;
  605. end
  606. end;
  607. { this function must return a very high value ("infinity") for }
  608. { trees containing a call, the rest can be balanced more or less }
  609. { at will, probably best mainly in terms of required memory }
  610. { accesses }
  611. function node_complexity(p: tnode): cardinal;
  612. var
  613. correction: byte;
  614. {$ifdef ARM}
  615. dummy : byte;
  616. {$endif ARM}
  617. begin
  618. result := 0;
  619. while assigned(p) do
  620. begin
  621. case p.nodetype of
  622. { floating point constants usually need loading from memory }
  623. realconstn:
  624. begin
  625. result:=2;
  626. exit;
  627. end;
  628. setconstn,
  629. stringconstn,
  630. temprefn,
  631. loadvmtaddrn,
  632. { main reason for the next one: we can't take the address of }
  633. { loadparentfpnode, so replacing it by a temp which is the }
  634. { address of this node's location and then dereferencing }
  635. { doesn't work. If changed, check whether webtbs/tw0935 }
  636. { still works with nodeinlining (JM) }
  637. loadparentfpn:
  638. begin
  639. result := 1;
  640. exit;
  641. end;
  642. loadn:
  643. begin
  644. if assigned(tloadnode(p).left) then
  645. inc(result,node_complexity(tloadnode(p).left));
  646. { threadvars need a helper call }
  647. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  648. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  649. inc(result,5)
  650. else
  651. inc(result);
  652. if (tloadnode(p).symtableentry.typ=paravarsym) and tloadnode(p).is_addr_param_load then
  653. inc(result);
  654. if (result >= NODE_COMPLEXITY_INF) then
  655. result := NODE_COMPLEXITY_INF;
  656. exit;
  657. end;
  658. subscriptn:
  659. begin
  660. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  661. is_bitpacked_access(p) then
  662. inc(result,2)
  663. else if tstoreddef(p.resultdef).is_intregable then
  664. inc(result,1);
  665. if (result = NODE_COMPLEXITY_INF) then
  666. exit;
  667. p := tunarynode(p).left;
  668. end;
  669. labeln,
  670. blockn:
  671. p := tunarynode(p).left;
  672. callparan:
  673. begin
  674. { call to decr? }
  675. if is_managed_type(tunarynode(p).left.resultdef) and
  676. assigned(tcallparanode(p).parasym) and (tcallparanode(p).parasym.varspez=vs_out) then
  677. begin
  678. result:=NODE_COMPLEXITY_INF;
  679. exit;
  680. end
  681. else
  682. begin
  683. inc(result);
  684. if (result = NODE_COMPLEXITY_INF) then
  685. exit;
  686. p := tunarynode(p).left;
  687. end;
  688. end;
  689. notn,
  690. derefn :
  691. begin
  692. inc(result);
  693. if (result = NODE_COMPLEXITY_INF) then
  694. exit;
  695. p := tunarynode(p).left;
  696. end;
  697. addrn:
  698. begin
  699. inc(result);
  700. if (result = NODE_COMPLEXITY_INF) then
  701. exit;
  702. p := tunarynode(p).left;
  703. end;
  704. typeconvn:
  705. begin
  706. { may be more complex in some cases }
  707. if not(ttypeconvnode(p).retains_value_location) then
  708. inc(result);
  709. if (result = NODE_COMPLEXITY_INF) then
  710. exit;
  711. p := tunarynode(p).left;
  712. end;
  713. vecn,
  714. statementn:
  715. begin
  716. inc(result,node_complexity(tbinarynode(p).left));
  717. if (result >= NODE_COMPLEXITY_INF) then
  718. begin
  719. result := NODE_COMPLEXITY_INF;
  720. exit;
  721. end;
  722. p := tbinarynode(p).right;
  723. end;
  724. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  725. shln,shrn,
  726. equaln,unequaln,gtn,gten,ltn,lten,
  727. assignn:
  728. begin
  729. {$ifdef CPU64BITALU}
  730. correction:=1;
  731. {$else CPU64BITALU}
  732. correction:=2;
  733. {$endif CPU64BITALU}
  734. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  735. if (p.nodetype in [muln,divn,modn]) then
  736. inc(result,5*correction*correction);
  737. if (result >= NODE_COMPLEXITY_INF) then
  738. begin
  739. result := NODE_COMPLEXITY_INF;
  740. exit;
  741. end;
  742. p := tbinarynode(p).right;
  743. end;
  744. ordconstn:
  745. begin
  746. {$ifdef ARM}
  747. if not(is_shifter_const(aint(tordconstnode(p).value.svalue),dummy)) then
  748. result:=2;
  749. {$endif ARM}
  750. exit;
  751. end;
  752. exitn:
  753. begin
  754. inc(result,2);
  755. if (result >= NODE_COMPLEXITY_INF) then
  756. begin
  757. result := NODE_COMPLEXITY_INF;
  758. exit;
  759. end;
  760. p:=texitnode(p).left;
  761. end;
  762. tempcreaten,
  763. tempdeleten,
  764. pointerconstn,
  765. nothingn,
  766. niln:
  767. exit;
  768. inlinen:
  769. begin
  770. { this code assumes that the inline node has }
  771. { already been firstpassed, and consequently }
  772. { that inline nodes which are transformed into }
  773. { calls already have been transformed }
  774. case tinlinenode(p).inlinenumber of
  775. in_lo_qword,
  776. in_hi_qword,
  777. in_lo_long,
  778. in_hi_long,
  779. in_lo_word,
  780. in_hi_word,
  781. in_length_x,
  782. in_assigned_x,
  783. in_pred_x,
  784. in_succ_x,
  785. in_round_real,
  786. in_trunc_real,
  787. in_int_real,
  788. in_frac_real,
  789. in_pi_real,
  790. in_abs_real,
  791. in_aligned_x,
  792. in_unaligned_x,
  793. in_prefetch_var:
  794. begin
  795. inc(result);
  796. p:=tunarynode(p).left;
  797. end;
  798. in_cos_real,
  799. in_sin_real,
  800. in_arctan_real,
  801. in_sqr_real,
  802. in_sqrt_real,
  803. in_ln_real:
  804. begin
  805. inc(result,2);
  806. if (result >= NODE_COMPLEXITY_INF) then
  807. begin
  808. result:=NODE_COMPLEXITY_INF;
  809. exit;
  810. end;
  811. p:=tunarynode(p).left;
  812. end;
  813. in_abs_long:
  814. begin
  815. inc(result,3);
  816. if (result >= NODE_COMPLEXITY_INF) then
  817. begin
  818. result:=NODE_COMPLEXITY_INF;
  819. exit;
  820. end;
  821. p:=tunarynode(p).left;
  822. end;
  823. in_sizeof_x,
  824. in_typeof_x:
  825. begin
  826. inc(result);
  827. if (tinlinenode(p).left.nodetype<>typen) then
  828. { get instance vmt }
  829. p:=tunarynode(p).left
  830. else
  831. { type vmt = global symbol, result is }
  832. { already increased above }
  833. exit;
  834. end;
  835. {$ifdef SUPPORT_MMX}
  836. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  837. {$endif SUPPORT_MMX}
  838. { load from global symbol }
  839. in_typeinfo_x,
  840. { load frame pointer }
  841. in_get_frame,
  842. in_get_caller_frame,
  843. in_get_caller_addr:
  844. begin
  845. inc(result);
  846. exit;
  847. end;
  848. in_inc_x,
  849. in_dec_x,
  850. in_include_x_y,
  851. in_exclude_x_y,
  852. in_assert_x_y :
  853. begin
  854. { operation (add, sub, or, and }
  855. inc(result);
  856. { left expression }
  857. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  858. if (result >= NODE_COMPLEXITY_INF) then
  859. begin
  860. result := NODE_COMPLEXITY_INF;
  861. exit;
  862. end;
  863. p:=tcallparanode(tunarynode(p).left).right;
  864. if assigned(p) then
  865. p:=tcallparanode(p).left;
  866. end;
  867. else
  868. begin
  869. result := NODE_COMPLEXITY_INF;
  870. exit;
  871. end;
  872. end;
  873. end;
  874. else
  875. begin
  876. result := NODE_COMPLEXITY_INF;
  877. exit;
  878. end;
  879. end;
  880. end;
  881. end;
  882. { this function returns an indication how much fpu registers
  883. will be required.
  884. Note: The algorithms need to be pessimistic to prevent a
  885. fpu stack overflow on i386 }
  886. function node_resources_fpu(p: tnode): cardinal;
  887. var
  888. res1,res2,res3 : cardinal;
  889. begin
  890. result:=0;
  891. res1:=0;
  892. res2:=0;
  893. res3:=0;
  894. if p.inheritsfrom(tunarynode) then
  895. begin
  896. if assigned(tunarynode(p).left) then
  897. res1:=node_resources_fpu(tunarynode(p).left);
  898. if p.inheritsfrom(tbinarynode) then
  899. begin
  900. if assigned(tbinarynode(p).right) then
  901. res2:=node_resources_fpu(tbinarynode(p).right);
  902. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  903. res3:=node_resources_fpu(ttertiarynode(p).third)
  904. end;
  905. end;
  906. result:=max(max(res1,res2),res3);
  907. case p.nodetype of
  908. calln:
  909. { it could be a recursive call, so we never really know the number of used fpu registers }
  910. result:=maxfpuregs;
  911. realconstn,
  912. typeconvn,
  913. loadn :
  914. begin
  915. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  916. result:=max(result,1);
  917. end;
  918. assignn,
  919. addn,subn,muln,slashn,
  920. equaln,unequaln,gtn,gten,ltn,lten :
  921. begin
  922. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  923. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  924. result:=max(result,2);
  925. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  926. inc(result);
  927. end;
  928. end;
  929. end;
  930. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  931. begin
  932. result:=fen_true;
  933. n.fileinfo:=pfileposinfo(arg)^;
  934. end;
  935. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  936. begin
  937. foreachnodestatic(n,@setnodefilepos,@filepos);
  938. end;
  939. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  940. var
  941. hn : tnode;
  942. treechanged : ^boolean;
  943. begin
  944. result:=fen_false;
  945. if n.inheritsfrom(tloopnode) and
  946. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  947. begin
  948. // Try to simplify condition
  949. doinlinesimplify(tloopnode(n).left);
  950. // call directly second part below,
  951. // which might change the loopnode into
  952. // something else if the conditino is a constant node
  953. include(tloopnode(n).loopflags,lnf_simplify_processing);
  954. callsimplify(n,arg);
  955. // Be careful, n might have change node type
  956. if n.inheritsfrom(tloopnode) then
  957. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  958. end
  959. else
  960. begin
  961. hn:=n.simplify(true);
  962. if assigned(hn) then
  963. begin
  964. treechanged := arg;
  965. if assigned(treechanged) then
  966. treechanged^:=true
  967. else
  968. internalerror (201008181);
  969. n.free;
  970. n:=hn;
  971. typecheckpass(n);
  972. end;
  973. end;
  974. end;
  975. { tries to simplify the given node calling the simplify method recursively }
  976. procedure doinlinesimplify(var n : tnode);
  977. var
  978. treechanged : boolean;
  979. begin
  980. // Optimize if code first
  981. repeat
  982. treechanged:=false;
  983. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  984. until not(treechanged);
  985. end;
  986. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  987. begin
  988. if not forinline then
  989. result:=genintconstnode(value)
  990. else
  991. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  992. end;
  993. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  994. var
  995. plist : ppropaccesslistitem;
  996. begin
  997. plist:=pl.firstsym;
  998. while assigned(plist) do
  999. begin
  1000. case plist^.sltype of
  1001. sl_load :
  1002. begin
  1003. addsymref(plist^.sym);
  1004. if not assigned(st) then
  1005. st:=plist^.sym.owner;
  1006. if (plist^.sym.typ<>staticvarsym) then
  1007. begin
  1008. { p1 can already contain the loadnode of
  1009. the class variable. When there is no tree yet we
  1010. may need to load it for with or objects }
  1011. if not assigned(p1) then
  1012. begin
  1013. case st.symtabletype of
  1014. withsymtable :
  1015. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1016. ObjectSymtable :
  1017. p1:=load_self_node;
  1018. end;
  1019. end
  1020. end
  1021. else
  1022. begin
  1023. p1.free;
  1024. p1:=nil;
  1025. end;
  1026. if assigned(p1) then
  1027. p1:=csubscriptnode.create(plist^.sym,p1)
  1028. else
  1029. p1:=cloadnode.create(plist^.sym,st);
  1030. end;
  1031. sl_subscript :
  1032. begin
  1033. addsymref(plist^.sym);
  1034. p1:=csubscriptnode.create(plist^.sym,p1);
  1035. end;
  1036. sl_typeconv :
  1037. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1038. sl_absolutetype :
  1039. begin
  1040. p1:=ctypeconvnode.create(p1,plist^.def);
  1041. include(p1.flags,nf_absolute);
  1042. end;
  1043. sl_vec :
  1044. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1045. else
  1046. internalerror(200110205);
  1047. end;
  1048. plist:=plist^.next;
  1049. end;
  1050. end;
  1051. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1052. var
  1053. sl : tpropaccesslist;
  1054. procedure addnode(p:tnode);
  1055. begin
  1056. case p.nodetype of
  1057. subscriptn :
  1058. begin
  1059. addnode(tsubscriptnode(p).left);
  1060. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1061. end;
  1062. typeconvn :
  1063. begin
  1064. addnode(ttypeconvnode(p).left);
  1065. if nf_absolute in ttypeconvnode(p).flags then
  1066. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1067. else
  1068. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1069. end;
  1070. vecn :
  1071. begin
  1072. addnode(tvecnode(p).left);
  1073. if tvecnode(p).right.nodetype=ordconstn then
  1074. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1075. else
  1076. begin
  1077. Message(parser_e_illegal_expression);
  1078. { recovery }
  1079. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1080. end;
  1081. end;
  1082. loadn :
  1083. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1084. else
  1085. internalerror(200310282);
  1086. end;
  1087. end;
  1088. begin
  1089. sl:=tpropaccesslist.create;
  1090. addnode(p1);
  1091. result:=sl;
  1092. end;
  1093. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  1094. function handle_generic_staticfield_access:boolean;
  1095. var
  1096. tmp : tstoreddef;
  1097. pd : tprocdef;
  1098. begin
  1099. { in case we have a specialization inside a generic (thus the static var sym does not
  1100. exist) we simply simulate a non static access to avoid unnecessary errors }
  1101. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1102. begin
  1103. tmp:=tstoreddef(sym.owner.defowner);
  1104. while assigned(tmp) do
  1105. begin
  1106. if df_generic in tmp.defoptions then
  1107. begin
  1108. p1.free;
  1109. if assigned(current_procinfo) then
  1110. begin
  1111. pd:=current_procinfo.get_normal_proc.procdef;
  1112. if assigned(pd) and pd.no_self_node then
  1113. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1114. else
  1115. p1:=load_self_node;
  1116. end
  1117. else
  1118. p1:=load_self_node;
  1119. p1:=csubscriptnode.create(sym,p1);
  1120. exit(true);
  1121. end;
  1122. tmp:=tstoreddef(tmp.owner.defowner);
  1123. end;
  1124. end;
  1125. result:=false;
  1126. end;
  1127. var
  1128. static_name: shortstring;
  1129. srsymtable: tsymtable;
  1130. begin
  1131. result:=false;
  1132. { generate access code }
  1133. if (sp_static in sym.symoptions) then
  1134. begin
  1135. result:=true;
  1136. if handle_generic_staticfield_access then
  1137. exit;
  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).tempflags)) 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.