nutils.pas 54 KB

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