nutils.pas 51 KB

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