nutils.pas 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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, tryfinallyn:
  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, tryfinallyn:
  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. else
  888. begin
  889. result := NODE_COMPLEXITY_INF;
  890. exit;
  891. end;
  892. end;
  893. end;
  894. end;
  895. { this function returns an indication how much fpu registers
  896. will be required.
  897. Note: The algorithms need to be pessimistic to prevent a
  898. fpu stack overflow on i386 }
  899. function node_resources_fpu(p: tnode): cardinal;
  900. var
  901. res1,res2,res3 : cardinal;
  902. begin
  903. result:=0;
  904. res1:=0;
  905. res2:=0;
  906. res3:=0;
  907. if p.inheritsfrom(tunarynode) then
  908. begin
  909. if assigned(tunarynode(p).left) then
  910. res1:=node_resources_fpu(tunarynode(p).left);
  911. if p.inheritsfrom(tbinarynode) then
  912. begin
  913. if assigned(tbinarynode(p).right) then
  914. res2:=node_resources_fpu(tbinarynode(p).right);
  915. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  916. res3:=node_resources_fpu(ttertiarynode(p).third)
  917. end;
  918. end;
  919. result:=max(max(res1,res2),res3);
  920. case p.nodetype of
  921. calln:
  922. { it could be a recursive call, so we never really know the number of used fpu registers }
  923. result:=maxfpuregs;
  924. realconstn,
  925. typeconvn,
  926. loadn :
  927. begin
  928. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  929. result:=max(result,1);
  930. end;
  931. assignn,
  932. addn,subn,muln,slashn,
  933. equaln,unequaln,gtn,gten,ltn,lten :
  934. begin
  935. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  936. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  937. result:=max(result,2);
  938. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  939. inc(result);
  940. end;
  941. end;
  942. end;
  943. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  944. begin
  945. result:=fen_true;
  946. n.fileinfo:=pfileposinfo(arg)^;
  947. end;
  948. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  949. begin
  950. foreachnodestatic(n,@setnodefilepos,@filepos);
  951. end;
  952. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  953. var
  954. hn : tnode;
  955. treechanged : ^boolean;
  956. begin
  957. result:=fen_false;
  958. if n.inheritsfrom(tloopnode) and
  959. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  960. begin
  961. // Try to simplify condition
  962. doinlinesimplify(tloopnode(n).left);
  963. // call directly second part below,
  964. // which might change the loopnode into
  965. // something else if the conditino is a constant node
  966. include(tloopnode(n).loopflags,lnf_simplify_processing);
  967. callsimplify(n,arg);
  968. // Be careful, n might have change node type
  969. if n.inheritsfrom(tloopnode) then
  970. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  971. end
  972. else
  973. begin
  974. hn:=n.simplify(true);
  975. if assigned(hn) then
  976. begin
  977. treechanged := arg;
  978. if assigned(treechanged) then
  979. treechanged^:=true
  980. else
  981. internalerror (201008181);
  982. n.free;
  983. n:=hn;
  984. typecheckpass(n);
  985. end;
  986. end;
  987. end;
  988. { tries to simplify the given node calling the simplify method recursively }
  989. procedure doinlinesimplify(var n : tnode);
  990. var
  991. treechanged : boolean;
  992. begin
  993. // Optimize if code first
  994. repeat
  995. treechanged:=false;
  996. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  997. until not(treechanged);
  998. end;
  999. function create_simplified_ord_const(const value: tconstexprint; def: tdef; forinline: boolean): tnode;
  1000. begin
  1001. if not forinline then
  1002. result:=genintconstnode(value)
  1003. else
  1004. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  1005. end;
  1006. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  1007. var
  1008. plist : ppropaccesslistitem;
  1009. begin
  1010. plist:=pl.firstsym;
  1011. while assigned(plist) do
  1012. begin
  1013. case plist^.sltype of
  1014. sl_load :
  1015. begin
  1016. addsymref(plist^.sym);
  1017. if not assigned(st) then
  1018. st:=plist^.sym.owner;
  1019. if (plist^.sym.typ<>staticvarsym) then
  1020. begin
  1021. { p1 can already contain the loadnode of
  1022. the class variable. When there is no tree yet we
  1023. may need to load it for with or objects }
  1024. if not assigned(p1) then
  1025. begin
  1026. case st.symtabletype of
  1027. withsymtable :
  1028. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1029. ObjectSymtable :
  1030. p1:=load_self_node;
  1031. end;
  1032. end
  1033. end
  1034. else
  1035. begin
  1036. p1.free;
  1037. p1:=nil;
  1038. end;
  1039. if assigned(p1) then
  1040. p1:=csubscriptnode.create(plist^.sym,p1)
  1041. else
  1042. p1:=cloadnode.create(plist^.sym,st);
  1043. end;
  1044. sl_subscript :
  1045. begin
  1046. addsymref(plist^.sym);
  1047. p1:=csubscriptnode.create(plist^.sym,p1);
  1048. end;
  1049. sl_typeconv :
  1050. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1051. sl_absolutetype :
  1052. begin
  1053. p1:=ctypeconvnode.create(p1,plist^.def);
  1054. include(p1.flags,nf_absolute);
  1055. end;
  1056. sl_vec :
  1057. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1058. else
  1059. internalerror(200110205);
  1060. end;
  1061. plist:=plist^.next;
  1062. end;
  1063. end;
  1064. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1065. var
  1066. sl : tpropaccesslist;
  1067. procedure addnode(p:tnode);
  1068. begin
  1069. case p.nodetype of
  1070. subscriptn :
  1071. begin
  1072. addnode(tsubscriptnode(p).left);
  1073. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1074. end;
  1075. typeconvn :
  1076. begin
  1077. addnode(ttypeconvnode(p).left);
  1078. if nf_absolute in ttypeconvnode(p).flags then
  1079. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1080. else
  1081. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1082. end;
  1083. vecn :
  1084. begin
  1085. addnode(tvecnode(p).left);
  1086. if tvecnode(p).right.nodetype=ordconstn then
  1087. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1088. else
  1089. begin
  1090. Message(parser_e_illegal_expression);
  1091. { recovery }
  1092. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1093. end;
  1094. end;
  1095. loadn :
  1096. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1097. else
  1098. internalerror(200310282);
  1099. end;
  1100. end;
  1101. begin
  1102. sl:=tpropaccesslist.create;
  1103. addnode(p1);
  1104. result:=sl;
  1105. end;
  1106. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  1107. function handle_generic_staticfield_access:boolean;
  1108. var
  1109. tmp : tstoreddef;
  1110. pd : tprocdef;
  1111. begin
  1112. { in case we have a specialization inside a generic (thus the static var sym does not
  1113. exist) we simply simulate a non static access to avoid unnecessary errors }
  1114. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1115. begin
  1116. tmp:=tstoreddef(sym.owner.defowner);
  1117. while assigned(tmp) do
  1118. begin
  1119. if df_generic in tmp.defoptions then
  1120. begin
  1121. p1.free;
  1122. if assigned(current_procinfo) then
  1123. begin
  1124. pd:=current_procinfo.get_normal_proc.procdef;
  1125. if assigned(pd) and pd.no_self_node then
  1126. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1127. else
  1128. p1:=load_self_node;
  1129. end
  1130. else
  1131. p1:=load_self_node;
  1132. p1:=csubscriptnode.create(sym,p1);
  1133. exit(true);
  1134. end;
  1135. tmp:=tstoreddef(tmp.owner.defowner);
  1136. end;
  1137. end;
  1138. result:=false;
  1139. end;
  1140. var
  1141. static_name: shortstring;
  1142. srsymtable: tsymtable;
  1143. begin
  1144. result:=false;
  1145. { generate access code }
  1146. if (sp_static in sym.symoptions) then
  1147. begin
  1148. result:=true;
  1149. if handle_generic_staticfield_access then
  1150. exit;
  1151. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  1152. if sym.owner.defowner.typ=objectdef then
  1153. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  1154. else
  1155. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  1156. if assigned(sym) then
  1157. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1158. p1.free;
  1159. p1:=nil;
  1160. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1161. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1162. end;
  1163. end;
  1164. function is_bitpacked_access(n: tnode): boolean;
  1165. begin
  1166. case n.nodetype of
  1167. vecn:
  1168. result:=
  1169. is_packed_array(tvecnode(n).left.resultdef) and
  1170. { only orddefs and enumdefs are actually bitpacked. Don't consider
  1171. e.g. an access to a 3-byte record as "bitpacked", since it
  1172. isn't }
  1173. (tvecnode(n).left.resultdef.typ = arraydef) and
  1174. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  1175. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  1176. subscriptn:
  1177. result:=
  1178. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1179. { see above }
  1180. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  1181. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  1182. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1183. else
  1184. result:=false;
  1185. end;
  1186. end;
  1187. function genloadfield(n: tnode; const fieldname: string): tnode;
  1188. var
  1189. vs : tsym;
  1190. begin
  1191. if not assigned(n.resultdef) then
  1192. typecheckpass(n);
  1193. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1194. if not assigned(vs) or
  1195. (vs.typ<>fieldvarsym) then
  1196. internalerror(2010061902);
  1197. result:=csubscriptnode.create(vs,n);
  1198. end;
  1199. function has_no_code(n : tnode) : boolean;
  1200. begin
  1201. if n=nil then
  1202. begin
  1203. result:=true;
  1204. exit;
  1205. end;
  1206. result:=false;
  1207. case n.nodetype of
  1208. nothingn:
  1209. begin
  1210. result:=true;
  1211. exit;
  1212. end;
  1213. blockn:
  1214. begin
  1215. result:=has_no_code(tblocknode(n).left);
  1216. exit;
  1217. end;
  1218. statementn:
  1219. begin
  1220. repeat
  1221. result:=has_no_code(tstatementnode(n).left);
  1222. n:=tstatementnode(n).right;
  1223. until not(result) or not assigned(n);
  1224. exit;
  1225. end;
  1226. end;
  1227. end;
  1228. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1229. begin
  1230. result:=fen_false;
  1231. if (n.nodetype in [assignn,calln,asmn]) or
  1232. ((n.nodetype=inlinen) and
  1233. (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  1234. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,
  1235. in_reset_typedfile_name,in_rewrite_typedfile_name,in_settextbuf_file_x,
  1236. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  1237. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle,
  1238. 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,
  1239. in_shr_assign_x_y,in_rol_assign_x_y,in_ror_assign_x_y,in_neg_assign_x,in_not_assign_x])
  1240. ) or
  1241. ((mhs_exceptions in pmhs_flags(arg)^) and
  1242. ((n.nodetype in [derefn,vecn,subscriptn]) or
  1243. ((n.nodetype in [addn,subn,muln,divn,slashn,unaryminusn]) and (n.localswitches*[cs_check_overflow,cs_check_range]<>[]))
  1244. )
  1245. ) then
  1246. result:=fen_norecurse_true;
  1247. end;
  1248. function might_have_sideeffects(n : tnode; const flags : tmhs_flags) : boolean;
  1249. begin
  1250. result:=foreachnodestatic(n,@check_for_sideeffect,@flags);
  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.