nutils.pas 50 KB

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