nutils.pas 50 KB

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