nutils.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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
  699. inc(result);
  700. if (tloadnode(p).symtableentry.typ=paravarsym) and tloadnode(p).is_addr_param_load then
  701. inc(result);
  702. if (result >= NODE_COMPLEXITY_INF) then
  703. result := NODE_COMPLEXITY_INF;
  704. exit;
  705. end;
  706. subscriptn:
  707. begin
  708. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  709. is_bitpacked_access(p) then
  710. inc(result,2)
  711. else if tstoreddef(p.resultdef).is_intregable then
  712. inc(result,1);
  713. if (result = NODE_COMPLEXITY_INF) then
  714. exit;
  715. p := tunarynode(p).left;
  716. end;
  717. labeln,
  718. blockn:
  719. p := tunarynode(p).left;
  720. callparan:
  721. begin
  722. { call to decr? }
  723. if is_managed_type(tunarynode(p).left.resultdef) and
  724. assigned(tcallparanode(p).parasym) and (tcallparanode(p).parasym.varspez=vs_out) then
  725. begin
  726. result:=NODE_COMPLEXITY_INF;
  727. exit;
  728. end
  729. else
  730. begin
  731. inc(result);
  732. if (result = NODE_COMPLEXITY_INF) then
  733. exit;
  734. p := tunarynode(p).left;
  735. end;
  736. end;
  737. notn,
  738. derefn :
  739. begin
  740. inc(result);
  741. if (result = NODE_COMPLEXITY_INF) then
  742. exit;
  743. p := tunarynode(p).left;
  744. end;
  745. addrn:
  746. begin
  747. inc(result);
  748. if (result = NODE_COMPLEXITY_INF) then
  749. exit;
  750. p := tunarynode(p).left;
  751. end;
  752. typeconvn:
  753. begin
  754. { may be more complex in some cases }
  755. if not(ttypeconvnode(p).retains_value_location) and
  756. not((ttypeconvnode(p).convtype=tc_pointer_2_array) and (ttypeconvnode(p).left.expectloc in [LOC_CREGISTER,LOC_REGISTER,LOC_CONSTANT])) then
  757. inc(result);
  758. if result = NODE_COMPLEXITY_INF then
  759. exit;
  760. p := tunarynode(p).left;
  761. end;
  762. vecn,
  763. statementn:
  764. begin
  765. inc(result,node_complexity(tbinarynode(p).left));
  766. if (result >= NODE_COMPLEXITY_INF) then
  767. begin
  768. result := NODE_COMPLEXITY_INF;
  769. exit;
  770. end;
  771. p := tbinarynode(p).right;
  772. end;
  773. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  774. shln,shrn,
  775. equaln,unequaln,gtn,gten,ltn,lten,
  776. assignn:
  777. begin
  778. {$ifdef CPU64BITALU}
  779. correction:=1;
  780. {$else CPU64BITALU}
  781. correction:=2;
  782. {$endif CPU64BITALU}
  783. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  784. if (p.nodetype in [muln,divn,modn]) then
  785. inc(result,5*correction*correction);
  786. if (result >= NODE_COMPLEXITY_INF) then
  787. begin
  788. result := NODE_COMPLEXITY_INF;
  789. exit;
  790. end;
  791. p := tbinarynode(p).right;
  792. end;
  793. ordconstn:
  794. begin
  795. {$ifdef ARM}
  796. if not(is_shifter_const(aint(tordconstnode(p).value.svalue),dummy)) then
  797. result:=2;
  798. {$endif ARM}
  799. exit;
  800. end;
  801. exitn:
  802. begin
  803. inc(result,2);
  804. if (result >= NODE_COMPLEXITY_INF) then
  805. begin
  806. result := NODE_COMPLEXITY_INF;
  807. exit;
  808. end;
  809. p:=texitnode(p).left;
  810. end;
  811. tempcreaten,
  812. tempdeleten,
  813. pointerconstn,
  814. nothingn,
  815. niln:
  816. exit;
  817. inlinen:
  818. begin
  819. { this code assumes that the inline node has }
  820. { already been firstpassed, and consequently }
  821. { that inline nodes which are transformed into }
  822. { calls already have been transformed }
  823. case tinlinenode(p).inlinenumber of
  824. in_lo_qword,
  825. in_hi_qword,
  826. in_lo_long,
  827. in_hi_long,
  828. in_lo_word,
  829. in_hi_word,
  830. in_length_x,
  831. in_assigned_x,
  832. in_pred_x,
  833. in_succ_x,
  834. in_round_real,
  835. in_trunc_real,
  836. in_int_real,
  837. in_frac_real,
  838. in_pi_real,
  839. in_abs_real,
  840. in_aligned_x,
  841. in_unaligned_x,
  842. in_volatile_x,
  843. in_prefetch_var:
  844. begin
  845. inc(result);
  846. p:=tunarynode(p).left;
  847. end;
  848. in_cos_real,
  849. in_sin_real,
  850. in_arctan_real,
  851. in_sqr_real,
  852. in_sqrt_real,
  853. in_ln_real:
  854. begin
  855. inc(result,2);
  856. if (result >= NODE_COMPLEXITY_INF) then
  857. begin
  858. result:=NODE_COMPLEXITY_INF;
  859. exit;
  860. end;
  861. p:=tunarynode(p).left;
  862. end;
  863. in_abs_long:
  864. begin
  865. inc(result,3);
  866. if (result >= NODE_COMPLEXITY_INF) then
  867. begin
  868. result:=NODE_COMPLEXITY_INF;
  869. exit;
  870. end;
  871. p:=tunarynode(p).left;
  872. end;
  873. in_sizeof_x,
  874. in_typeof_x:
  875. begin
  876. inc(result);
  877. if (tinlinenode(p).left.nodetype<>typen) then
  878. { get instance vmt }
  879. p:=tunarynode(p).left
  880. else
  881. { type vmt = global symbol, result is }
  882. { already increased above }
  883. exit;
  884. end;
  885. {$ifdef SUPPORT_MMX}
  886. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  887. {$endif SUPPORT_MMX}
  888. { load from global symbol }
  889. in_typeinfo_x,
  890. { load frame pointer }
  891. in_get_frame,
  892. in_get_caller_frame,
  893. in_get_caller_addr:
  894. begin
  895. inc(result);
  896. exit;
  897. end;
  898. in_inc_x,
  899. in_dec_x,
  900. in_include_x_y,
  901. in_exclude_x_y,
  902. in_assert_x_y :
  903. begin
  904. { operation (add, sub, or, and }
  905. inc(result);
  906. { left expression }
  907. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  908. if (result >= NODE_COMPLEXITY_INF) then
  909. begin
  910. result := NODE_COMPLEXITY_INF;
  911. exit;
  912. end;
  913. p:=tcallparanode(tunarynode(p).left).right;
  914. if assigned(p) then
  915. p:=tcallparanode(p).left;
  916. end;
  917. else
  918. begin
  919. result := NODE_COMPLEXITY_INF;
  920. exit;
  921. end;
  922. end;
  923. end;
  924. finalizetempsn:
  925. begin
  926. result:=NODE_COMPLEXITY_INF;
  927. exit;
  928. end;
  929. else
  930. begin
  931. result := NODE_COMPLEXITY_INF;
  932. exit;
  933. end;
  934. end;
  935. end;
  936. end;
  937. { this function returns an indication how much fpu registers
  938. will be required.
  939. Note: The algorithms need to be pessimistic to prevent a
  940. fpu stack overflow on i386 }
  941. function node_resources_fpu(p: tnode): cardinal;
  942. var
  943. res1,res2,res3 : cardinal;
  944. begin
  945. result:=0;
  946. res1:=0;
  947. res2:=0;
  948. res3:=0;
  949. if p.inheritsfrom(tunarynode) then
  950. begin
  951. if assigned(tunarynode(p).left) then
  952. res1:=node_resources_fpu(tunarynode(p).left);
  953. if p.inheritsfrom(tbinarynode) then
  954. begin
  955. if assigned(tbinarynode(p).right) then
  956. res2:=node_resources_fpu(tbinarynode(p).right);
  957. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  958. res3:=node_resources_fpu(ttertiarynode(p).third)
  959. end;
  960. end;
  961. result:=max(max(res1,res2),res3);
  962. case p.nodetype of
  963. calln:
  964. { it could be a recursive call, so we never really know the number of used fpu registers }
  965. result:=maxfpuregs;
  966. realconstn,
  967. typeconvn,
  968. loadn :
  969. begin
  970. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  971. result:=max(result,1);
  972. end;
  973. assignn,
  974. addn,subn,muln,slashn,
  975. equaln,unequaln,gtn,gten,ltn,lten :
  976. begin
  977. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  978. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  979. result:=max(result,2);
  980. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  981. inc(result);
  982. end;
  983. else
  984. ;
  985. end;
  986. end;
  987. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  988. begin
  989. result:=fen_true;
  990. n.fileinfo:=pfileposinfo(arg)^;
  991. end;
  992. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  993. begin
  994. foreachnodestatic(n,@setnodefilepos,@filepos);
  995. end;
  996. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  997. var
  998. hn : tnode;
  999. treechanged : ^boolean;
  1000. begin
  1001. result:=fen_false;
  1002. if n.inheritsfrom(tloopnode) and
  1003. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  1004. begin
  1005. // Try to simplify condition
  1006. doinlinesimplify(tloopnode(n).left);
  1007. // call directly second part below,
  1008. // which might change the loopnode into
  1009. // something else if the conditino is a constant node
  1010. include(tloopnode(n).loopflags,lnf_simplify_processing);
  1011. callsimplify(n,arg);
  1012. // Be careful, n might have change node type
  1013. if n.inheritsfrom(tloopnode) then
  1014. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  1015. end
  1016. else
  1017. begin
  1018. hn:=n.simplify(true);
  1019. if assigned(hn) then
  1020. begin
  1021. treechanged := arg;
  1022. if assigned(treechanged) then
  1023. treechanged^:=true
  1024. else
  1025. internalerror (201008181);
  1026. n.free;
  1027. n:=hn;
  1028. typecheckpass(n);
  1029. end;
  1030. end;
  1031. end;
  1032. { tries to simplify the given node calling the simplify method recursively }
  1033. procedure doinlinesimplify(var n : tnode);
  1034. var
  1035. treechanged : boolean;
  1036. begin
  1037. // Optimize if code first
  1038. repeat
  1039. treechanged:=false;
  1040. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  1041. until not(treechanged);
  1042. end;
  1043. function create_simplified_ord_const(const value: tconstexprint; def: tdef; forinline: boolean): tnode;
  1044. begin
  1045. if not forinline then
  1046. result:=genintconstnode(value)
  1047. else
  1048. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  1049. end;
  1050. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  1051. var
  1052. plist : ppropaccesslistitem;
  1053. begin
  1054. plist:=pl.firstsym;
  1055. while assigned(plist) do
  1056. begin
  1057. case plist^.sltype of
  1058. sl_load :
  1059. begin
  1060. addsymref(plist^.sym);
  1061. if not assigned(st) then
  1062. st:=plist^.sym.owner;
  1063. if (plist^.sym.typ<>staticvarsym) then
  1064. begin
  1065. { p1 can already contain the loadnode of
  1066. the class variable. When there is no tree yet we
  1067. may need to load it for with or objects }
  1068. if not assigned(p1) then
  1069. begin
  1070. case st.symtabletype of
  1071. withsymtable :
  1072. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1073. ObjectSymtable :
  1074. p1:=load_self_node;
  1075. else
  1076. ;
  1077. end;
  1078. end
  1079. end
  1080. else
  1081. begin
  1082. p1.free;
  1083. p1:=nil;
  1084. end;
  1085. if assigned(p1) then
  1086. p1:=csubscriptnode.create(plist^.sym,p1)
  1087. else
  1088. p1:=cloadnode.create(plist^.sym,st);
  1089. end;
  1090. sl_subscript :
  1091. begin
  1092. addsymref(plist^.sym);
  1093. p1:=csubscriptnode.create(plist^.sym,p1);
  1094. end;
  1095. sl_typeconv :
  1096. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1097. sl_absolutetype :
  1098. begin
  1099. p1:=ctypeconvnode.create(p1,plist^.def);
  1100. include(p1.flags,nf_absolute);
  1101. end;
  1102. sl_vec :
  1103. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1104. else
  1105. internalerror(200110205);
  1106. end;
  1107. plist:=plist^.next;
  1108. end;
  1109. end;
  1110. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1111. var
  1112. sl : tpropaccesslist;
  1113. procedure addnode(p:tnode);
  1114. begin
  1115. case p.nodetype of
  1116. subscriptn :
  1117. begin
  1118. addnode(tsubscriptnode(p).left);
  1119. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1120. end;
  1121. typeconvn :
  1122. begin
  1123. addnode(ttypeconvnode(p).left);
  1124. if nf_absolute in ttypeconvnode(p).flags then
  1125. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1126. else
  1127. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1128. end;
  1129. vecn :
  1130. begin
  1131. addnode(tvecnode(p).left);
  1132. if tvecnode(p).right.nodetype=ordconstn then
  1133. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1134. else
  1135. begin
  1136. Message(parser_e_illegal_expression);
  1137. { recovery }
  1138. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1139. end;
  1140. end;
  1141. loadn :
  1142. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1143. else
  1144. internalerror(200310282);
  1145. end;
  1146. end;
  1147. begin
  1148. sl:=tpropaccesslist.create;
  1149. addnode(p1);
  1150. result:=sl;
  1151. end;
  1152. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  1153. function handle_generic_staticfield_access:boolean;
  1154. var
  1155. tmp : tstoreddef;
  1156. pd : tprocdef;
  1157. begin
  1158. { in case we have a specialization inside a generic (thus the static var sym does not
  1159. exist) we simply simulate a non static access to avoid unnecessary errors }
  1160. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1161. begin
  1162. tmp:=tstoreddef(sym.owner.defowner);
  1163. while assigned(tmp) do
  1164. begin
  1165. if df_generic in tmp.defoptions then
  1166. begin
  1167. p1.free;
  1168. if assigned(current_procinfo) then
  1169. begin
  1170. pd:=current_procinfo.get_normal_proc.procdef;
  1171. if assigned(pd) and pd.no_self_node then
  1172. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1173. else
  1174. p1:=load_self_node;
  1175. end
  1176. else
  1177. p1:=load_self_node;
  1178. p1:=csubscriptnode.create(sym,p1);
  1179. exit(true);
  1180. end;
  1181. tmp:=tstoreddef(tmp.owner.defowner);
  1182. end;
  1183. end;
  1184. result:=false;
  1185. end;
  1186. var
  1187. static_name: shortstring;
  1188. srsymtable: tsymtable;
  1189. begin
  1190. result:=false;
  1191. { generate access code }
  1192. if (sp_static in sym.symoptions) then
  1193. begin
  1194. result:=true;
  1195. if handle_generic_staticfield_access then
  1196. exit;
  1197. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  1198. if sym.owner.defowner.typ=objectdef then
  1199. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  1200. else
  1201. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  1202. if assigned(sym) then
  1203. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1204. p1.free;
  1205. p1:=nil;
  1206. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1207. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1208. end;
  1209. end;
  1210. function is_bitpacked_access(n: tnode): boolean;
  1211. begin
  1212. case n.nodetype of
  1213. vecn:
  1214. result:=
  1215. is_packed_array(tvecnode(n).left.resultdef) and
  1216. { only orddefs and enumdefs are actually bitpacked. Don't consider
  1217. e.g. an access to a 3-byte record as "bitpacked", since it
  1218. isn't }
  1219. (tvecnode(n).left.resultdef.typ = arraydef) and
  1220. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  1221. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  1222. subscriptn:
  1223. result:=
  1224. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1225. { see above }
  1226. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  1227. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  1228. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1229. else
  1230. result:=false;
  1231. end;
  1232. end;
  1233. function genloadfield(n: tnode; const fieldname: string): tnode;
  1234. var
  1235. vs : tsym;
  1236. begin
  1237. if not assigned(n.resultdef) then
  1238. typecheckpass(n);
  1239. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1240. if not assigned(vs) or
  1241. (vs.typ<>fieldvarsym) then
  1242. internalerror(2010061902);
  1243. result:=csubscriptnode.create(vs,n);
  1244. end;
  1245. function has_no_code(n : tnode) : boolean;
  1246. begin
  1247. if n=nil then
  1248. begin
  1249. result:=true;
  1250. exit;
  1251. end;
  1252. case n.nodetype of
  1253. nothingn:
  1254. begin
  1255. result:=true;
  1256. exit;
  1257. end;
  1258. blockn:
  1259. begin
  1260. result:=has_no_code(tblocknode(n).left);
  1261. exit;
  1262. end;
  1263. statementn:
  1264. begin
  1265. repeat
  1266. result:=has_no_code(tstatementnode(n).left);
  1267. n:=tstatementnode(n).right;
  1268. until not(result) or not assigned(n);
  1269. exit;
  1270. end;
  1271. else
  1272. result:=false;
  1273. end;
  1274. end;
  1275. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1276. begin
  1277. result:=fen_false;
  1278. if (n.nodetype in [assignn,calln,asmn,finalizetempsn]) or
  1279. ((n.nodetype=inlinen) and
  1280. tinlinenode(n).may_have_sideeffect_norecurse
  1281. ) or
  1282. ((mhs_exceptions in pmhs_flags(arg)^) and
  1283. ((n.nodetype in [derefn,vecn,subscriptn]) or
  1284. ((n.nodetype in [addn,subn,muln,divn,slashn,unaryminusn]) and (n.localswitches*[cs_check_overflow,cs_check_range]<>[]))
  1285. )
  1286. ) then
  1287. result:=fen_norecurse_true;
  1288. end;
  1289. function might_have_sideeffects(n : tnode; const flags : tmhs_flags) : boolean;
  1290. begin
  1291. result:=foreachnodestatic(n,@check_for_sideeffect,@flags);
  1292. end;
  1293. function check_for_conditional_nodes(var n: tnode; arg: pointer): foreachnoderesult;
  1294. begin
  1295. result:=fen_false;
  1296. { this check is not complete yet, but sufficent to cover the current use case: swapping
  1297. of trees in expressions }
  1298. if (n.nodetype in [ifn,whilerepeatn,forn,tryexceptn]) or
  1299. ((n.nodetype in [orn,andn]) and is_boolean(n.resultdef) and doshortbooleval(n)) then
  1300. result:=fen_norecurse_true;
  1301. end;
  1302. function has_conditional_nodes(n : tnode) : boolean;
  1303. begin
  1304. result:=foreachnodestatic(n,@check_for_conditional_nodes,nil);
  1305. end;
  1306. var
  1307. nodecount : dword;
  1308. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1309. begin
  1310. inc(nodecount);
  1311. result:=fen_false;
  1312. end;
  1313. function node_count(node : tnode) : dword;
  1314. begin
  1315. nodecount:=0;
  1316. foreachnodestatic(node,@donodecount,nil);
  1317. result:=nodecount;
  1318. end;
  1319. function donodecount_weighted(var n: tnode; arg: pointer): foreachnoderesult;
  1320. begin
  1321. if not(n.nodetype in [blockn,statementn,callparan,nothingn]) then
  1322. inc(nodecount);
  1323. result:=fen_false;
  1324. end;
  1325. function node_count_weighted(node : tnode) : dword;
  1326. begin
  1327. nodecount:=0;
  1328. foreachnodestatic(node,@donodecount_weighted,nil);
  1329. result:=nodecount;
  1330. end;
  1331. function is_const(node : tnode) : boolean;
  1332. begin
  1333. result:=is_constnode(node) or
  1334. ((node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempflags)) or
  1335. ((node.nodetype=loadn) and (tloadnode(node).symtableentry.typ=paravarsym) and (tparavarsym(tloadnode(node).symtableentry).varspez in [vs_const,vs_constref]));
  1336. end;
  1337. function actualtargetnode(n : pnode) : pnode;
  1338. begin
  1339. result:=n;
  1340. case n^.nodetype of
  1341. typeconvn:
  1342. if ttypeconvnode(n^).retains_value_location then
  1343. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1344. else
  1345. ;
  1346. end;
  1347. end;
  1348. procedure replacenode(var dest,src : tnode);
  1349. var
  1350. t : tnode;
  1351. begin
  1352. t:=src;
  1353. { set src nil before free'ing dest because
  1354. src could be part of dest }
  1355. src:=nil;
  1356. dest.Free;
  1357. dest:=t;
  1358. end;
  1359. function get_open_const_array(p : tnode) : tnode;
  1360. begin
  1361. result:=p;
  1362. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1363. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  1364. end;
  1365. function do_node_reset_flags(var n: tnode; arg: pointer): foreachnoderesult;
  1366. begin
  1367. result:=fen_false;
  1368. n.flags:=n.flags-tnodeflags(arg^);
  1369. end;
  1370. procedure node_reset_flags(p : tnode; nf : tnodeflags);
  1371. begin
  1372. foreachnodestatic(p,@do_node_reset_flags,@nf);
  1373. end;
  1374. type
  1375. tlocalswitchchange = record
  1376. cs : tlocalswitch;
  1377. enable : boolean;
  1378. end;
  1379. plocalswitchchange = ^tlocalswitchchange;
  1380. function do_change_local_settings(var p : tnode;plsc : pointer) : foreachnoderesult;
  1381. begin
  1382. if plocalswitchchange(plsc)^.enable then
  1383. include(p.localswitches, plocalswitchchange(plsc)^.cs)
  1384. else
  1385. exclude(p.localswitches, plocalswitchchange(plsc)^.cs);
  1386. result:=fen_true;
  1387. end;
  1388. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  1389. var
  1390. lsc : tlocalswitchchange;
  1391. begin
  1392. lsc.cs:=cs;
  1393. lsc.enable:=enable;
  1394. foreachnodestatic(p,@do_change_local_settings,@lsc);
  1395. end;
  1396. function doshortbooleval(p : tnode) : Boolean;
  1397. begin
  1398. Result:=(p.nodetype in [orn,andn]) and ((nf_short_bool in taddnode(p).flags) or not(cs_full_boolean_eval in p.localswitches));
  1399. end;
  1400. function is_constintvalue(p: tnode; l: Tconstexprint): Boolean;
  1401. begin
  1402. Result:=is_constintnode(p) and (tordconstnode(p).value=l);
  1403. end;
  1404. function is_inlinefunction(p: tnode; i: tinlinenumber): Boolean;
  1405. begin
  1406. Result:=(p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=i);
  1407. end;
  1408. function MatchAndTransformNodesCommutative(n1,n2 : tnode;matchproc : TMatchProc2;transformproc : TTransformProc2;var res : tnode) : Boolean;
  1409. begin
  1410. res:=nil;
  1411. result:=true;
  1412. if matchproc(n1,n2) then
  1413. res:=transformproc(n1,n2)
  1414. else if matchproc(n2,n1) then
  1415. res:=transformproc(n2,n1)
  1416. else
  1417. result:=false;
  1418. end;
  1419. end.