nutils.pas 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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. tmhs_flag = (
  46. { exceptions (overflow, sigfault etc.) are considered as side effect }
  47. mhs_exceptions
  48. );
  49. tmhs_flags = set of tmhs_flag;
  50. pmhs_flags = ^tmhs_flags;
  51. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  52. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  53. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  54. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  55. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  56. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  57. { checks if the given node tree contains only nodes of the given type,
  58. if this isn't the case, an ie is thrown
  59. }
  60. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  61. procedure load_procvar_from_calln(var p1:tnode);
  62. function get_local_or_para_sym(const aname: string): tsym;
  63. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  64. function load_high_value_node(vs:tparavarsym):tnode;
  65. function load_self_node:tnode;
  66. function load_result_node:tnode;
  67. function load_self_pointer_node:tnode;
  68. function load_vmt_pointer_node:tnode;
  69. function is_self_node(p:tnode):boolean;
  70. { create a tree that loads the VMT based on a self-node of an object/class/
  71. interface }
  72. function load_vmt_for_self_node(self_node: tnode): tnode;
  73. function node_complexity(p: tnode): cardinal;
  74. function node_resources_fpu(p: tnode): cardinal;
  75. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  76. { tries to simplify the given node after inlining }
  77. procedure doinlinesimplify(var n : tnode);
  78. { creates an ordinal constant, optionally based on the result from a
  79. simplify operation: normally the type is the smallest integer type
  80. that can hold the value, but when inlining the "def" will be used instead,
  81. which was determined during an earlier typecheck pass (because the value
  82. may e.g. be a parameter to a call, which needs to be of the declared
  83. parameter type) }
  84. function create_simplified_ord_const(const value: tconstexprint; def: tdef; forinline: boolean): tnode;
  85. { returns true if n is only a tree of administrative nodes
  86. containing no code }
  87. function has_no_code(n : tnode) : boolean;
  88. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  89. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  90. { checks whether sym is a static field and if so, translates the access
  91. to the appropriate node tree }
  92. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  93. { returns true if n is an array element access of a bitpacked array with
  94. elements of the which the vitsize mod 8 <> 0, or if is a field access
  95. with bitsize mod 8 <> 0 or bitoffset mod 8 <> 0 of an element in a
  96. bitpacked structure }
  97. function is_bitpacked_access(n: tnode): boolean;
  98. { creates a load of field 'fieldname' in the record/class/...
  99. represented by n }
  100. function genloadfield(n: tnode; const fieldname: string): tnode;
  101. { returns true, if the tree given might have side effects }
  102. function might_have_sideeffects(n : tnode;const flags : tmhs_flags = []) : boolean;
  103. { count the number of nodes in the node tree,
  104. rough estimation how large the tree "node" is }
  105. function node_count(node : tnode) : dword;
  106. function node_count_weighted(node : tnode) : dword;
  107. { returns true, if the value described by node is constant/immutable, this approximation is safe
  108. if no dirty tricks like buffer overflows or pointer magic are used }
  109. function is_const(node : tnode) : boolean;
  110. { returns a pointer to the real node a node refers to,
  111. skipping (absolute) equal type conversions. Returning
  112. a pointer allows the caller to move/remove/replace this
  113. node
  114. }
  115. function actualtargetnode(n : pnode) : pnode;
  116. { moves src into dest, before doing so, right is set to nil and dest is freed.
  117. Because dest and src are var parameters, this can be done inline in an existing
  118. node tree }
  119. procedure replacenode(var dest,src : tnode);
  120. { strip off deref/addr combinations when looking for a the load node of an open array/array of const
  121. since there is no possiblity to defined a pointer to an open array/array of const, we have not to
  122. take care of type casts, further, it means also that deref/addr nodes must always appear coupled
  123. }
  124. function get_open_const_array(p : tnode) : tnode;
  125. { excludes the flags passed in nf from the node tree passed }
  126. procedure node_reset_flags(p : tnode;nf : tnodeflags);
  127. { include or exclude cs from p.localswitches }
  128. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  129. { returns true, if p is a node which shall be short boolean evaluated,
  130. if it is not an orn/andn with boolean operans, the result is undefined }
  131. function doshortbooleval(p : tnode) : Boolean;
  132. implementation
  133. uses
  134. cutils,verbose,globals,compinnr,
  135. symconst,symdef,
  136. defcmp,defutil,
  137. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,ninl,
  138. cpubase,cgbase,procinfo,
  139. pass_1;
  140. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  141. function process_children(res : boolean) : boolean;
  142. var
  143. i: longint;
  144. begin
  145. result:=res;
  146. case n.nodetype of
  147. asn:
  148. if assigned(tasnode(n).call) then
  149. begin
  150. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  151. exit
  152. end;
  153. calln:
  154. begin
  155. result := foreachnode(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  156. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  157. result := foreachnode(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  158. result := foreachnode(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  159. end;
  160. ifn, whilerepeatn, forn, tryexceptn:
  161. begin
  162. { not in one statement, won't work because of b- }
  163. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  164. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  165. end;
  166. raisen:
  167. { frame tree }
  168. result := foreachnode(ttertiarynode(n).third,f,arg) or result;
  169. tempcreaten:
  170. { temp. initialization code }
  171. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  172. result := foreachnode(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  173. casen:
  174. begin
  175. for i := 0 to tcasenode(n).blocks.count-1 do
  176. if assigned(tcasenode(n).blocks[i]) then
  177. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  178. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  179. end;
  180. end;
  181. if n.inheritsfrom(tbinarynode) then
  182. begin
  183. { first process the "payload" of statementnodes }
  184. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  185. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  186. end
  187. else if n.inheritsfrom(tunarynode) then
  188. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  189. end;
  190. begin
  191. result := false;
  192. if not assigned(n) then
  193. exit;
  194. if procmethod=pm_preprocess then
  195. result:=process_children(result);
  196. case f(n,arg) of
  197. fen_norecurse_false:
  198. exit;
  199. fen_norecurse_true:
  200. begin
  201. result := true;
  202. exit;
  203. end;
  204. fen_true:
  205. result := true;
  206. { result is already false
  207. fen_false:
  208. result := false; }
  209. end;
  210. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  211. result:=process_children(result);
  212. if procmethod=pm_postandagain then
  213. begin
  214. case f(n,arg) of
  215. fen_norecurse_false:
  216. exit;
  217. fen_norecurse_true:
  218. begin
  219. result := true;
  220. exit;
  221. end;
  222. fen_true:
  223. result := true;
  224. end;
  225. end;
  226. end;
  227. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  228. begin
  229. result:=foreachnode(pm_postprocess,n,f,arg);
  230. end;
  231. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  232. function process_children(res : boolean) : boolean;
  233. var
  234. i: longint;
  235. begin
  236. result:=res;
  237. case n.nodetype of
  238. asn:
  239. if assigned(tasnode(n).call) then
  240. begin
  241. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  242. exit
  243. end;
  244. calln:
  245. begin
  246. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  247. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  248. result := foreachnodestatic(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  249. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  250. end;
  251. ifn, whilerepeatn, forn, tryexceptn:
  252. begin
  253. { not in one statement, won't work because of b- }
  254. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  255. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  256. end;
  257. raisen:
  258. { frame tree }
  259. result := foreachnodestatic(ttertiarynode(n).third,f,arg) or result;
  260. tempcreaten:
  261. { temp. initialization code }
  262. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  263. result := foreachnodestatic(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  264. casen:
  265. begin
  266. for i := 0 to tcasenode(n).blocks.count-1 do
  267. if assigned(tcasenode(n).blocks[i]) then
  268. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  269. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  270. end;
  271. end;
  272. if n.inheritsfrom(tbinarynode) then
  273. begin
  274. { first process the "payload" of statementnodes }
  275. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  276. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  277. end
  278. else if n.inheritsfrom(tunarynode) then
  279. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  280. end;
  281. begin
  282. result := false;
  283. if not assigned(n) then
  284. exit;
  285. if procmethod=pm_preprocess then
  286. result:=process_children(result);
  287. case f(n,arg) of
  288. fen_norecurse_false:
  289. exit;
  290. fen_norecurse_true:
  291. begin
  292. result := true;
  293. exit;
  294. end;
  295. fen_true:
  296. result := true;
  297. { result is already false
  298. fen_false:
  299. result := false; }
  300. end;
  301. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  302. result:=process_children(result);
  303. if procmethod=pm_postandagain then
  304. begin
  305. case f(n,arg) of
  306. fen_norecurse_false:
  307. exit;
  308. fen_norecurse_true:
  309. begin
  310. result := true;
  311. exit;
  312. end;
  313. fen_true:
  314. result := true;
  315. end;
  316. end;
  317. end;
  318. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  319. begin
  320. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  321. end;
  322. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  323. begin
  324. if not(n.nodetype in pnodetypeset(arg)^) then
  325. internalerror(200610141);
  326. result:=fen_true;
  327. end;
  328. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  329. begin
  330. foreachnodestatic(n,@do_check,@typeset);
  331. end;
  332. procedure load_procvar_from_calln(var p1:tnode);
  333. var
  334. p2 : tnode;
  335. begin
  336. if p1.nodetype<>calln then
  337. internalerror(200212251);
  338. { was it a procvar, then we simply remove the calln and
  339. reuse the right }
  340. if assigned(tcallnode(p1).right) then
  341. begin
  342. p2:=tcallnode(p1).right;
  343. tcallnode(p1).right:=nil;
  344. end
  345. else
  346. begin
  347. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  348. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  349. { when the methodpointer is typen we've something like:
  350. tobject.create. Then only the address is needed of the
  351. method without a self pointer }
  352. if assigned(tcallnode(p1).methodpointer) and
  353. (tcallnode(p1).methodpointer.nodetype<>typen) then
  354. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  355. end;
  356. typecheckpass(p2);
  357. p1.free;
  358. p1:=p2;
  359. end;
  360. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  361. var
  362. hp : tnode;
  363. begin
  364. result:=false;
  365. if (p1.resultdef.typ<>procvardef) or
  366. (tponly and
  367. not(m_tp_procvar in current_settings.modeswitches)) then
  368. exit;
  369. { ignore vecn,subscriptn }
  370. hp:=p1;
  371. repeat
  372. case hp.nodetype of
  373. vecn,
  374. derefn,
  375. typeconvn,
  376. subscriptn :
  377. hp:=tunarynode(hp).left;
  378. blockn:
  379. hp:=laststatement(tblocknode(hp)).left
  380. else
  381. break;
  382. end;
  383. until false;
  384. { a tempref is used when it is loaded from a withsymtable }
  385. if (hp.nodetype in [calln,loadn,temprefn]) then
  386. begin
  387. hp:=ccallnode.create_procvar(nil,p1);
  388. typecheckpass(hp);
  389. p1:=hp;
  390. result:=true;
  391. end;
  392. end;
  393. function get_local_or_para_sym(const aname: string): tsym;
  394. var
  395. pd : tprocdef;
  396. begin
  397. result:=nil;
  398. { is not assigned while parsing a property }
  399. if not assigned(current_procinfo) then
  400. exit;
  401. { we can't use searchsym here, because the
  402. symtablestack is not fully setup when pass1
  403. is run for nested procedures }
  404. pd:=current_procinfo.procdef;
  405. repeat
  406. result := tsym(pd.localst.Find(aname));
  407. if assigned(result) then
  408. break;
  409. result := tsym(pd.parast.Find(aname));
  410. if assigned(result) then
  411. break;
  412. { try the parent of a nested function }
  413. if assigned(pd.owner.defowner) and
  414. (pd.owner.defowner.typ=procdef) then
  415. pd:=tprocdef(pd.owner.defowner)
  416. else
  417. break;
  418. until false;
  419. end;
  420. function load_high_value_node(vs:tparavarsym):tnode;
  421. var
  422. srsym : tsym;
  423. begin
  424. result:=nil;
  425. srsym:=get_high_value_sym(vs);
  426. if assigned(srsym) then
  427. begin
  428. result:=cloadnode.create(srsym,vs.owner);
  429. typecheckpass(result);
  430. end
  431. else
  432. CGMessage(parser_e_illegal_expression);
  433. end;
  434. function load_self_node:tnode;
  435. var
  436. srsym : tsym;
  437. begin
  438. result:=nil;
  439. srsym:=get_local_or_para_sym('self');
  440. if assigned(srsym) then
  441. begin
  442. result:=cloadnode.create(srsym,srsym.owner);
  443. include(tloadnode(result).loadnodeflags,loadnf_is_self);
  444. end
  445. else
  446. begin
  447. result:=cerrornode.create;
  448. CGMessage(parser_e_illegal_expression);
  449. end;
  450. typecheckpass(result);
  451. end;
  452. function load_result_node:tnode;
  453. var
  454. srsym : tsym;
  455. begin
  456. result:=nil;
  457. srsym:=get_local_or_para_sym('result');
  458. if assigned(srsym) then
  459. result:=cloadnode.create(srsym,srsym.owner)
  460. else
  461. begin
  462. result:=cerrornode.create;
  463. CGMessage(parser_e_illegal_expression);
  464. end;
  465. typecheckpass(result);
  466. end;
  467. function load_self_pointer_node:tnode;
  468. var
  469. srsym : tsym;
  470. begin
  471. result:=nil;
  472. srsym:=get_local_or_para_sym('self');
  473. if assigned(srsym) then
  474. begin
  475. result:=cloadnode.create(srsym,srsym.owner);
  476. include(tloadnode(result).loadnodeflags,loadnf_load_self_pointer);
  477. end
  478. else
  479. begin
  480. result:=cerrornode.create;
  481. CGMessage(parser_e_illegal_expression);
  482. end;
  483. typecheckpass(result);
  484. end;
  485. function load_vmt_pointer_node:tnode;
  486. var
  487. srsym : tsym;
  488. begin
  489. result:=nil;
  490. srsym:=get_local_or_para_sym('vmt');
  491. if assigned(srsym) then
  492. result:=cloadnode.create(srsym,srsym.owner)
  493. else
  494. begin
  495. result:=cerrornode.create;
  496. CGMessage(parser_e_illegal_expression);
  497. end;
  498. typecheckpass(result);
  499. end;
  500. function is_self_node(p:tnode):boolean;
  501. begin
  502. is_self_node:=(p.nodetype=loadn) and
  503. (tloadnode(p).symtableentry.typ=paravarsym) and
  504. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  505. end;
  506. function load_vmt_for_self_node(self_node: tnode): tnode;
  507. var
  508. self_resultdef: tdef;
  509. obj_def: tobjectdef;
  510. self_temp,
  511. vmt_temp: ttempcreatenode;
  512. check_self: tnode;
  513. stat: tstatementnode;
  514. block: tblocknode;
  515. paras: tcallparanode;
  516. docheck: boolean;
  517. begin
  518. self_resultdef:=self_node.resultdef;
  519. case self_resultdef.typ of
  520. classrefdef:
  521. obj_def:=tobjectdef(tclassrefdef(self_resultdef).pointeddef);
  522. objectdef:
  523. obj_def:=tobjectdef(self_resultdef);
  524. else
  525. internalerror(2015052701);
  526. end;
  527. if is_classhelper(obj_def) then
  528. obj_def:=tobjectdef(tobjectdef(obj_def).extendeddef);
  529. docheck:=
  530. not(is_interface(obj_def)) and
  531. not(is_cppclass(obj_def)) and
  532. not(is_objc_class_or_protocol(obj_def)) and
  533. (([cs_check_object,cs_check_range]*current_settings.localswitches)<>[]);
  534. block:=nil;
  535. stat:=nil;
  536. if docheck then
  537. begin
  538. { check for nil self-pointer }
  539. block:=internalstatements(stat);
  540. self_temp:=ctempcreatenode.create_value(
  541. self_resultdef,self_resultdef.size,tt_persistent,true,
  542. self_node);
  543. addstatement(stat,self_temp);
  544. { in case of an object, self can only be nil if it's a dereferenced
  545. node somehow
  546. }
  547. if not is_object(self_resultdef) or
  548. (actualtargetnode(@self_node)^.nodetype=derefn) then
  549. begin
  550. check_self:=ctemprefnode.create(self_temp);
  551. if is_object(self_resultdef) then
  552. check_self:=caddrnode.create(check_self);
  553. addstatement(stat,cifnode.create(
  554. caddnode.create(equaln,
  555. ctypeconvnode.create_explicit(
  556. check_self,
  557. voidpointertype
  558. ),
  559. cnilnode.create),
  560. ccallnode.createintern('fpc_objecterror',nil),
  561. nil)
  562. );
  563. end;
  564. addstatement(stat,ctempdeletenode.create_normal_temp(self_temp));
  565. self_node:=ctemprefnode.create(self_temp);
  566. end;
  567. { get the VMT field in case of a class/object }
  568. if (self_resultdef.typ=objectdef) and
  569. assigned(tobjectdef(self_resultdef).vmt_field) then
  570. result:=csubscriptnode.create(tobjectdef(self_resultdef).vmt_field,self_node)
  571. { in case of a classref, the "instance" is a pointer
  572. to pointer to a VMT and there is no vmt field }
  573. else if self_resultdef.typ=classrefdef then
  574. result:=self_node
  575. { in case of an interface, the "instance" is a pointer to a pointer
  576. to a VMT -> dereference once already }
  577. else
  578. { in case of an interface/classref, the "instance" is a pointer
  579. to pointer to a VMT and there is no vmt field }
  580. result:=cderefnode.create(
  581. ctypeconvnode.create_explicit(
  582. self_node,
  583. cpointerdef.getreusable(voidpointertype)
  584. )
  585. );
  586. result:=ctypeconvnode.create_explicit(
  587. result,
  588. cpointerdef.getreusable(obj_def.vmt_def));
  589. typecheckpass(result);
  590. if docheck then
  591. begin
  592. { add a vmt validity check }
  593. vmt_temp:=ctempcreatenode.create_value(result.resultdef,result.resultdef.size,tt_persistent,true,result);
  594. addstatement(stat,vmt_temp);
  595. paras:=ccallparanode.create(ctemprefnode.create(vmt_temp),nil);
  596. if cs_check_object in current_settings.localswitches then
  597. begin
  598. paras:=ccallparanode.create(
  599. cloadvmtaddrnode.create(ctypenode.create(obj_def)),
  600. paras
  601. );
  602. addstatement(stat,
  603. ccallnode.createintern(
  604. 'fpc_check_object_ext',paras
  605. )
  606. );
  607. end
  608. else
  609. addstatement(stat,
  610. ccallnode.createintern(
  611. 'fpc_check_object',paras
  612. )
  613. );
  614. addstatement(stat,ctempdeletenode.create_normal_temp(vmt_temp));
  615. addstatement(stat,ctemprefnode.create(vmt_temp));
  616. result:=block;
  617. end
  618. end;
  619. { this function must return a very high value ("infinity") for }
  620. { trees containing a call, the rest can be balanced more or less }
  621. { at will, probably best mainly in terms of required memory }
  622. { accesses }
  623. function node_complexity(p: tnode): cardinal;
  624. var
  625. correction: byte;
  626. {$ifdef ARM}
  627. dummy : byte;
  628. {$endif ARM}
  629. begin
  630. result := 0;
  631. while assigned(p) do
  632. begin
  633. case p.nodetype of
  634. { floating point constants usually need loading from memory }
  635. realconstn:
  636. begin
  637. result:=2;
  638. exit;
  639. end;
  640. setconstn,
  641. stringconstn,
  642. temprefn,
  643. loadvmtaddrn,
  644. { main reason for the next one: we can't take the address of }
  645. { loadparentfpnode, so replacing it by a temp which is the }
  646. { address of this node's location and then dereferencing }
  647. { doesn't work. If changed, check whether webtbs/tw0935 }
  648. { still works with nodeinlining (JM) }
  649. loadparentfpn:
  650. begin
  651. result := 1;
  652. exit;
  653. end;
  654. loadn:
  655. begin
  656. if assigned(tloadnode(p).left) then
  657. inc(result,node_complexity(tloadnode(p).left));
  658. { threadvars need a helper call }
  659. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  660. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  661. inc(result,5)
  662. else
  663. inc(result);
  664. if (tloadnode(p).symtableentry.typ=paravarsym) and tloadnode(p).is_addr_param_load then
  665. inc(result);
  666. if (result >= NODE_COMPLEXITY_INF) then
  667. result := NODE_COMPLEXITY_INF;
  668. exit;
  669. end;
  670. subscriptn:
  671. begin
  672. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  673. is_bitpacked_access(p) then
  674. inc(result,2)
  675. else if tstoreddef(p.resultdef).is_intregable then
  676. inc(result,1);
  677. if (result = NODE_COMPLEXITY_INF) then
  678. exit;
  679. p := tunarynode(p).left;
  680. end;
  681. labeln,
  682. blockn:
  683. p := tunarynode(p).left;
  684. callparan:
  685. begin
  686. { call to decr? }
  687. if is_managed_type(tunarynode(p).left.resultdef) and
  688. assigned(tcallparanode(p).parasym) and (tcallparanode(p).parasym.varspez=vs_out) then
  689. begin
  690. result:=NODE_COMPLEXITY_INF;
  691. exit;
  692. end
  693. else
  694. begin
  695. inc(result);
  696. if (result = NODE_COMPLEXITY_INF) then
  697. exit;
  698. p := tunarynode(p).left;
  699. end;
  700. end;
  701. notn,
  702. derefn :
  703. begin
  704. inc(result);
  705. if (result = NODE_COMPLEXITY_INF) then
  706. exit;
  707. p := tunarynode(p).left;
  708. end;
  709. addrn:
  710. begin
  711. inc(result);
  712. if (result = NODE_COMPLEXITY_INF) then
  713. exit;
  714. p := tunarynode(p).left;
  715. end;
  716. typeconvn:
  717. begin
  718. { may be more complex in some cases }
  719. if not(ttypeconvnode(p).retains_value_location) and
  720. not((ttypeconvnode(p).convtype=tc_pointer_2_array) and (ttypeconvnode(p).left.expectloc in [LOC_CREGISTER,LOC_REGISTER,LOC_CONSTANT])) then
  721. inc(result);
  722. if result = NODE_COMPLEXITY_INF then
  723. exit;
  724. p := tunarynode(p).left;
  725. end;
  726. vecn,
  727. statementn:
  728. begin
  729. inc(result,node_complexity(tbinarynode(p).left));
  730. if (result >= NODE_COMPLEXITY_INF) then
  731. begin
  732. result := NODE_COMPLEXITY_INF;
  733. exit;
  734. end;
  735. p := tbinarynode(p).right;
  736. end;
  737. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  738. shln,shrn,
  739. equaln,unequaln,gtn,gten,ltn,lten,
  740. assignn:
  741. begin
  742. {$ifdef CPU64BITALU}
  743. correction:=1;
  744. {$else CPU64BITALU}
  745. correction:=2;
  746. {$endif CPU64BITALU}
  747. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  748. if (p.nodetype in [muln,divn,modn]) then
  749. inc(result,5*correction*correction);
  750. if (result >= NODE_COMPLEXITY_INF) then
  751. begin
  752. result := NODE_COMPLEXITY_INF;
  753. exit;
  754. end;
  755. p := tbinarynode(p).right;
  756. end;
  757. ordconstn:
  758. begin
  759. {$ifdef ARM}
  760. if not(is_shifter_const(aint(tordconstnode(p).value.svalue),dummy)) then
  761. result:=2;
  762. {$endif ARM}
  763. exit;
  764. end;
  765. exitn:
  766. begin
  767. inc(result,2);
  768. if (result >= NODE_COMPLEXITY_INF) then
  769. begin
  770. result := NODE_COMPLEXITY_INF;
  771. exit;
  772. end;
  773. p:=texitnode(p).left;
  774. end;
  775. tempcreaten,
  776. tempdeleten,
  777. pointerconstn,
  778. nothingn,
  779. niln:
  780. exit;
  781. inlinen:
  782. begin
  783. { this code assumes that the inline node has }
  784. { already been firstpassed, and consequently }
  785. { that inline nodes which are transformed into }
  786. { calls already have been transformed }
  787. case tinlinenode(p).inlinenumber of
  788. in_lo_qword,
  789. in_hi_qword,
  790. in_lo_long,
  791. in_hi_long,
  792. in_lo_word,
  793. in_hi_word,
  794. in_length_x,
  795. in_assigned_x,
  796. in_pred_x,
  797. in_succ_x,
  798. in_round_real,
  799. in_trunc_real,
  800. in_int_real,
  801. in_frac_real,
  802. in_pi_real,
  803. in_abs_real,
  804. in_aligned_x,
  805. in_unaligned_x,
  806. in_prefetch_var:
  807. begin
  808. inc(result);
  809. p:=tunarynode(p).left;
  810. end;
  811. in_cos_real,
  812. in_sin_real,
  813. in_arctan_real,
  814. in_sqr_real,
  815. in_sqrt_real,
  816. in_ln_real:
  817. begin
  818. inc(result,2);
  819. if (result >= NODE_COMPLEXITY_INF) then
  820. begin
  821. result:=NODE_COMPLEXITY_INF;
  822. exit;
  823. end;
  824. p:=tunarynode(p).left;
  825. end;
  826. in_abs_long:
  827. begin
  828. inc(result,3);
  829. if (result >= NODE_COMPLEXITY_INF) then
  830. begin
  831. result:=NODE_COMPLEXITY_INF;
  832. exit;
  833. end;
  834. p:=tunarynode(p).left;
  835. end;
  836. in_sizeof_x,
  837. in_typeof_x:
  838. begin
  839. inc(result);
  840. if (tinlinenode(p).left.nodetype<>typen) then
  841. { get instance vmt }
  842. p:=tunarynode(p).left
  843. else
  844. { type vmt = global symbol, result is }
  845. { already increased above }
  846. exit;
  847. end;
  848. {$ifdef SUPPORT_MMX}
  849. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  850. {$endif SUPPORT_MMX}
  851. { load from global symbol }
  852. in_typeinfo_x,
  853. { load frame pointer }
  854. in_get_frame,
  855. in_get_caller_frame,
  856. in_get_caller_addr:
  857. begin
  858. inc(result);
  859. exit;
  860. end;
  861. in_inc_x,
  862. in_dec_x,
  863. in_include_x_y,
  864. in_exclude_x_y,
  865. in_assert_x_y :
  866. begin
  867. { operation (add, sub, or, and }
  868. inc(result);
  869. { left expression }
  870. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  871. if (result >= NODE_COMPLEXITY_INF) then
  872. begin
  873. result := NODE_COMPLEXITY_INF;
  874. exit;
  875. end;
  876. p:=tcallparanode(tunarynode(p).left).right;
  877. if assigned(p) then
  878. p:=tcallparanode(p).left;
  879. end;
  880. else
  881. begin
  882. result := NODE_COMPLEXITY_INF;
  883. exit;
  884. end;
  885. end;
  886. end;
  887. finalizetempsn:
  888. begin
  889. result:=NODE_COMPLEXITY_INF;
  890. exit;
  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: boolean): tnode;
  1005. begin
  1006. if not forinline then
  1007. result:=genintconstnode(value)
  1008. else
  1009. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  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. ) or
  1246. ((mhs_exceptions in pmhs_flags(arg)^) and
  1247. ((n.nodetype in [derefn,vecn,subscriptn]) or
  1248. ((n.nodetype in [addn,subn,muln,divn,slashn,unaryminusn]) and (n.localswitches*[cs_check_overflow,cs_check_range]<>[]))
  1249. )
  1250. ) then
  1251. result:=fen_norecurse_true;
  1252. end;
  1253. function might_have_sideeffects(n : tnode; const flags : tmhs_flags) : boolean;
  1254. begin
  1255. result:=foreachnodestatic(n,@check_for_sideeffect,@flags);
  1256. end;
  1257. var
  1258. nodecount : dword;
  1259. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1260. begin
  1261. inc(nodecount);
  1262. result:=fen_false;
  1263. end;
  1264. function node_count(node : tnode) : dword;
  1265. begin
  1266. nodecount:=0;
  1267. foreachnodestatic(node,@donodecount,nil);
  1268. result:=nodecount;
  1269. end;
  1270. function donodecount_weighted(var n: tnode; arg: pointer): foreachnoderesult;
  1271. begin
  1272. if not(n.nodetype in [blockn,statementn,callparan,nothingn]) then
  1273. inc(nodecount);
  1274. result:=fen_false;
  1275. end;
  1276. function node_count_weighted(node : tnode) : dword;
  1277. begin
  1278. nodecount:=0;
  1279. foreachnodestatic(node,@donodecount_weighted,nil);
  1280. result:=nodecount;
  1281. end;
  1282. function is_const(node : tnode) : boolean;
  1283. begin
  1284. result:=is_constnode(node) or
  1285. ((node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempflags)) or
  1286. ((node.nodetype=loadn) and (tloadnode(node).symtableentry.typ=paravarsym) and (tparavarsym(tloadnode(node).symtableentry).varspez in [vs_const,vs_constref]));
  1287. end;
  1288. function actualtargetnode(n : pnode) : pnode;
  1289. begin
  1290. result:=n;
  1291. case n^.nodetype of
  1292. typeconvn:
  1293. if ttypeconvnode(n^).retains_value_location then
  1294. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1295. end;
  1296. end;
  1297. procedure replacenode(var dest,src : tnode);
  1298. var
  1299. t : tnode;
  1300. begin
  1301. t:=src;
  1302. { set src nil before free'ing dest because
  1303. src could be part of dest }
  1304. src:=nil;
  1305. dest.Free;
  1306. dest:=t;
  1307. end;
  1308. function get_open_const_array(p : tnode) : tnode;
  1309. begin
  1310. result:=p;
  1311. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1312. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  1313. end;
  1314. function do_node_reset_flags(var n: tnode; arg: pointer): foreachnoderesult;
  1315. begin
  1316. result:=fen_false;
  1317. n.flags:=n.flags-tnodeflags(arg^);
  1318. end;
  1319. procedure node_reset_flags(p : tnode; nf : tnodeflags);
  1320. begin
  1321. foreachnodestatic(p,@do_node_reset_flags,@nf);
  1322. end;
  1323. type
  1324. tlocalswitchchange = record
  1325. cs : tlocalswitch;
  1326. enable : boolean;
  1327. end;
  1328. plocalswitchchange = ^tlocalswitchchange;
  1329. function do_change_local_settings(var p : tnode;plsc : pointer) : foreachnoderesult;
  1330. begin
  1331. if plocalswitchchange(plsc)^.enable then
  1332. include(p.localswitches, plocalswitchchange(plsc)^.cs)
  1333. else
  1334. exclude(p.localswitches, plocalswitchchange(plsc)^.cs);
  1335. result:=fen_true;
  1336. end;
  1337. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  1338. var
  1339. lsc : tlocalswitchchange;
  1340. begin
  1341. lsc.cs:=cs;
  1342. lsc.enable:=enable;
  1343. foreachnodestatic(p,@do_change_local_settings,@lsc);
  1344. end;
  1345. function doshortbooleval(p : tnode) : Boolean;
  1346. begin
  1347. Result:=(p.nodetype in [orn,andn]) and ((nf_short_bool in taddnode(p).flags) or not(cs_full_boolean_eval in p.localswitches));
  1348. end;
  1349. end.