nutils.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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, rangecheck: 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. if is_object(tabstractvarsym(srsym).vardef) or is_record(tabstractvarsym(srsym).vardef) then
  502. include(tloadnode(result).loadnodeflags,loadnf_load_addr);
  503. end
  504. else
  505. begin
  506. result:=cerrornode.create;
  507. CGMessage(parser_e_illegal_expression);
  508. end;
  509. typecheckpass(result);
  510. end;
  511. function load_vmt_pointer_node:tnode;
  512. var
  513. srsym : tsym;
  514. begin
  515. result:=nil;
  516. srsym:=get_local_or_para_sym('vmt');
  517. if assigned(srsym) then
  518. result:=cloadnode.create(srsym,srsym.owner)
  519. else
  520. begin
  521. result:=cerrornode.create;
  522. CGMessage(parser_e_illegal_expression);
  523. end;
  524. typecheckpass(result);
  525. end;
  526. function is_self_node(p:tnode):boolean;
  527. begin
  528. is_self_node:=(p.nodetype=loadn) and
  529. (tloadnode(p).symtableentry.typ=paravarsym) and
  530. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  531. end;
  532. function load_vmt_for_self_node(self_node: tnode): tnode;
  533. var
  534. self_resultdef: tdef;
  535. obj_def: tobjectdef;
  536. self_temp,
  537. vmt_temp: ttempcreatenode;
  538. check_self,n: tnode;
  539. stat: tstatementnode;
  540. block: tblocknode;
  541. paras: tcallparanode;
  542. docheck,is_typecasted_classref: boolean;
  543. begin
  544. self_resultdef:=self_node.resultdef;
  545. case self_resultdef.typ of
  546. classrefdef:
  547. begin
  548. obj_def:=tobjectdef(tclassrefdef(self_resultdef).pointeddef);
  549. end;
  550. objectdef:
  551. obj_def:=tobjectdef(self_resultdef);
  552. else
  553. internalerror(2015052701);
  554. end;
  555. n:=self_node;
  556. is_typecasted_classref:=false;
  557. if (n.nodetype=typeconvn) then
  558. begin
  559. while assigned(n) and (n.nodetype=typeconvn) and (nf_explicit in ttypeconvnode(n).flags) do
  560. n:=ttypeconvnode(n).left;
  561. if assigned(n) and (n.resultdef.typ=classrefdef) then
  562. is_typecasted_classref:=true;
  563. end;
  564. if is_classhelper(obj_def) then
  565. obj_def:=tobjectdef(tobjectdef(obj_def).extendeddef);
  566. docheck:=
  567. not(is_interface(obj_def)) and
  568. not(is_cppclass(obj_def)) and
  569. not(is_objc_class_or_protocol(obj_def)) and
  570. (([cs_check_object,cs_check_range]*current_settings.localswitches)<>[]);
  571. block:=nil;
  572. stat:=nil;
  573. if docheck then
  574. begin
  575. { check for nil self-pointer }
  576. block:=internalstatements(stat);
  577. self_temp:=ctempcreatenode.create_value(
  578. self_resultdef,self_resultdef.size,tt_persistent,true,
  579. self_node);
  580. addstatement(stat,self_temp);
  581. { in case of an object, self can only be nil if it's a dereferenced
  582. node somehow
  583. }
  584. if not is_object(self_resultdef) or
  585. (actualtargetnode(@self_node)^.nodetype=derefn) then
  586. begin
  587. check_self:=ctemprefnode.create(self_temp);
  588. if is_object(self_resultdef) then
  589. check_self:=caddrnode.create(check_self);
  590. addstatement(stat,cifnode.create(
  591. caddnode.create(equaln,
  592. ctypeconvnode.create_explicit(
  593. check_self,
  594. voidpointertype
  595. ),
  596. cnilnode.create),
  597. ccallnode.createintern('fpc_objecterror',nil),
  598. nil)
  599. );
  600. end;
  601. addstatement(stat,ctempdeletenode.create_normal_temp(self_temp));
  602. self_node:=ctemprefnode.create(self_temp);
  603. end;
  604. { in case of a classref, the "instance" is a pointer
  605. to pointer to a VMT and there is no vmt field }
  606. if is_typecasted_classref or (self_resultdef.typ=classrefdef) then
  607. result:=self_node
  608. { get the VMT field in case of a class/object }
  609. else if (self_resultdef.typ=objectdef) and
  610. assigned(tobjectdef(self_resultdef).vmt_field) then
  611. result:=csubscriptnode.create(tobjectdef(self_resultdef).vmt_field,self_node)
  612. { in case of an interface, the "instance" is a pointer to a pointer
  613. to a VMT -> dereference once already }
  614. else
  615. { in case of an interface/classref, the "instance" is a pointer
  616. to pointer to a VMT and there is no vmt field }
  617. result:=cderefnode.create(
  618. ctypeconvnode.create_explicit(
  619. self_node,
  620. cpointerdef.getreusable(voidpointertype)
  621. )
  622. );
  623. result:=ctypeconvnode.create_explicit(
  624. result,
  625. cpointerdef.getreusable(obj_def.vmt_def));
  626. typecheckpass(result);
  627. if docheck then
  628. begin
  629. { add a vmt validity check }
  630. vmt_temp:=ctempcreatenode.create_value(result.resultdef,result.resultdef.size,tt_persistent,true,result);
  631. addstatement(stat,vmt_temp);
  632. paras:=ccallparanode.create(ctemprefnode.create(vmt_temp),nil);
  633. if cs_check_object in current_settings.localswitches then
  634. begin
  635. paras:=ccallparanode.create(
  636. cloadvmtaddrnode.create(ctypenode.create(obj_def)),
  637. paras
  638. );
  639. addstatement(stat,
  640. ccallnode.createintern(
  641. 'fpc_check_object_ext',paras
  642. )
  643. );
  644. end
  645. else
  646. addstatement(stat,
  647. ccallnode.createintern(
  648. 'fpc_check_object',paras
  649. )
  650. );
  651. addstatement(stat,ctempdeletenode.create_normal_temp(vmt_temp));
  652. addstatement(stat,ctemprefnode.create(vmt_temp));
  653. result:=block;
  654. end
  655. end;
  656. { this function must return a very high value ("infinity") for }
  657. { trees containing a call, the rest can be balanced more or less }
  658. { at will, probably best mainly in terms of required memory }
  659. { accesses }
  660. function node_complexity(p: tnode): cardinal;
  661. var
  662. correction: byte;
  663. {$ifdef ARM}
  664. dummy : byte;
  665. {$endif ARM}
  666. begin
  667. result := 0;
  668. while assigned(p) do
  669. begin
  670. case p.nodetype of
  671. { floating point constants usually need loading from memory }
  672. realconstn:
  673. begin
  674. result:=2;
  675. exit;
  676. end;
  677. rttin,
  678. setconstn,
  679. stringconstn,
  680. temprefn,
  681. loadvmtaddrn,
  682. { main reason for the next one: we can't take the address of }
  683. { loadparentfpnode, so replacing it by a temp which is the }
  684. { address of this node's location and then dereferencing }
  685. { doesn't work. If changed, check whether webtbs/tw0935 }
  686. { still works with nodeinlining (JM) }
  687. loadparentfpn:
  688. begin
  689. result := 1;
  690. exit;
  691. end;
  692. loadn:
  693. begin
  694. if assigned(tloadnode(p).left) then
  695. inc(result,node_complexity(tloadnode(p).left));
  696. { threadvars need a helper call }
  697. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  698. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  699. inc(result,5)
  700. else if not((tloadnode(p).symtableentry.typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  701. (tabstractvarsym(tloadnode(p).symtableentry).varregable in [vr_intreg,vr_mmreg,vr_fpureg])) then
  702. inc(result);
  703. if (tloadnode(p).symtableentry.typ=paravarsym) and
  704. not(tabstractvarsym(tloadnode(p).symtableentry).varregable=vr_addr) and
  705. tloadnode(p).is_addr_param_load then
  706. inc(result);
  707. if (result >= NODE_COMPLEXITY_INF) then
  708. result := NODE_COMPLEXITY_INF;
  709. exit;
  710. end;
  711. subscriptn:
  712. begin
  713. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  714. is_bitpacked_access(p) then
  715. inc(result,2)
  716. { non-packed, int. regable records cause no extra
  717. overhead no overhead if the fields are aligned to register boundaries }
  718. else if tstoreddef(p.resultdef).is_intregable and (tsubscriptnode(p).vs.fieldoffset mod sizeof(aint)<>0) then
  719. inc(result,1);
  720. if (result = NODE_COMPLEXITY_INF) then
  721. exit;
  722. p := tunarynode(p).left;
  723. end;
  724. labeln,
  725. blockn:
  726. p := tunarynode(p).left;
  727. callparan:
  728. begin
  729. { call to decr? }
  730. if is_managed_type(tunarynode(p).left.resultdef) and
  731. assigned(tcallparanode(p).parasym) and (tcallparanode(p).parasym.varspez=vs_out) then
  732. begin
  733. result:=NODE_COMPLEXITY_INF;
  734. exit;
  735. end
  736. else
  737. begin
  738. inc(result);
  739. if (result = NODE_COMPLEXITY_INF) then
  740. exit;
  741. p := tunarynode(p).left;
  742. end;
  743. end;
  744. notn,
  745. derefn :
  746. begin
  747. inc(result);
  748. if (result = NODE_COMPLEXITY_INF) then
  749. exit;
  750. p := tunarynode(p).left;
  751. end;
  752. addrn:
  753. begin
  754. inc(result);
  755. if (result = NODE_COMPLEXITY_INF) then
  756. exit;
  757. p := tunarynode(p).left;
  758. end;
  759. typeconvn:
  760. begin
  761. { may be more complex in some cases }
  762. if not(ttypeconvnode(p).retains_value_location) and
  763. not((ttypeconvnode(p).convtype=tc_pointer_2_array) and (ttypeconvnode(p).left.expectloc in [LOC_CREGISTER,LOC_REGISTER,LOC_CONSTANT])) then
  764. inc(result);
  765. if result = NODE_COMPLEXITY_INF then
  766. exit;
  767. p := tunarynode(p).left;
  768. end;
  769. vecn,
  770. statementn:
  771. begin
  772. inc(result,node_complexity(tbinarynode(p).left));
  773. if (result >= NODE_COMPLEXITY_INF) then
  774. begin
  775. result := NODE_COMPLEXITY_INF;
  776. exit;
  777. end;
  778. p := tbinarynode(p).right;
  779. end;
  780. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  781. shln,shrn,
  782. equaln,unequaln,gtn,gten,ltn,lten,
  783. assignn:
  784. begin
  785. {$ifdef CPU64BITALU}
  786. correction:=1;
  787. {$else CPU64BITALU}
  788. correction:=2;
  789. {$endif CPU64BITALU}
  790. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  791. if (p.nodetype in [muln,divn,modn]) then
  792. inc(result,5*correction*correction);
  793. if (result >= NODE_COMPLEXITY_INF) then
  794. begin
  795. result := NODE_COMPLEXITY_INF;
  796. exit;
  797. end;
  798. p := tbinarynode(p).right;
  799. end;
  800. ordconstn:
  801. begin
  802. {$ifdef ARM}
  803. if not(is_shifter_const(aint(tordconstnode(p).value.svalue),dummy)) then
  804. result:=2;
  805. {$endif ARM}
  806. exit;
  807. end;
  808. exitn:
  809. begin
  810. inc(result,2);
  811. if (result >= NODE_COMPLEXITY_INF) then
  812. begin
  813. result := NODE_COMPLEXITY_INF;
  814. exit;
  815. end;
  816. p:=texitnode(p).left;
  817. end;
  818. tempcreaten,
  819. tempdeleten,
  820. pointerconstn,
  821. nothingn,
  822. niln:
  823. exit;
  824. inlinen:
  825. begin
  826. { this code assumes that the inline node has }
  827. { already been firstpassed, and consequently }
  828. { that inline nodes which are transformed into }
  829. { calls already have been transformed }
  830. case tinlinenode(p).inlinenumber of
  831. in_lo_qword,
  832. in_hi_qword,
  833. in_lo_long,
  834. in_hi_long,
  835. in_lo_word,
  836. in_hi_word,
  837. in_length_x,
  838. in_assigned_x,
  839. in_pred_x,
  840. in_succ_x,
  841. in_round_real,
  842. in_trunc_real,
  843. in_int_real,
  844. in_frac_real,
  845. in_pi_real,
  846. in_abs_real,
  847. in_aligned_x,
  848. in_unaligned_x,
  849. in_volatile_x,
  850. in_prefetch_var:
  851. begin
  852. inc(result);
  853. p:=tunarynode(p).left;
  854. end;
  855. in_cos_real,
  856. in_sin_real,
  857. in_arctan_real,
  858. in_sqr_real,
  859. in_sqrt_real,
  860. in_ln_real:
  861. begin
  862. inc(result,2);
  863. if (result >= NODE_COMPLEXITY_INF) then
  864. begin
  865. result:=NODE_COMPLEXITY_INF;
  866. exit;
  867. end;
  868. p:=tunarynode(p).left;
  869. end;
  870. in_abs_long:
  871. begin
  872. inc(result,3);
  873. if (result >= NODE_COMPLEXITY_INF) then
  874. begin
  875. result:=NODE_COMPLEXITY_INF;
  876. exit;
  877. end;
  878. p:=tunarynode(p).left;
  879. end;
  880. in_sizeof_x,
  881. in_typeof_x:
  882. begin
  883. inc(result);
  884. if (tinlinenode(p).left.nodetype<>typen) then
  885. { get instance vmt }
  886. p:=tunarynode(p).left
  887. else
  888. { type vmt = global symbol, result is }
  889. { already increased above }
  890. exit;
  891. end;
  892. {$ifdef SUPPORT_MMX}
  893. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  894. {$endif SUPPORT_MMX}
  895. { load from global symbol }
  896. in_typeinfo_x,
  897. { load frame pointer }
  898. in_get_frame,
  899. in_get_caller_frame,
  900. in_get_caller_addr:
  901. begin
  902. inc(result);
  903. exit;
  904. end;
  905. in_inc_x,
  906. in_dec_x,
  907. in_include_x_y,
  908. in_exclude_x_y,
  909. in_assert_x_y :
  910. begin
  911. { operation (add, sub, or, and }
  912. inc(result);
  913. { left expression }
  914. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  915. if (result >= NODE_COMPLEXITY_INF) then
  916. begin
  917. result := NODE_COMPLEXITY_INF;
  918. exit;
  919. end;
  920. p:=tcallparanode(tunarynode(p).left).right;
  921. if assigned(p) then
  922. p:=tcallparanode(p).left;
  923. end;
  924. else
  925. begin
  926. result := NODE_COMPLEXITY_INF;
  927. exit;
  928. end;
  929. end;
  930. end;
  931. finalizetempsn:
  932. begin
  933. result:=NODE_COMPLEXITY_INF;
  934. exit;
  935. end;
  936. else
  937. begin
  938. result := NODE_COMPLEXITY_INF;
  939. exit;
  940. end;
  941. end;
  942. end;
  943. end;
  944. { this function returns an indication how much fpu registers
  945. will be required.
  946. Note: The algorithms need to be pessimistic to prevent a
  947. fpu stack overflow on i386 }
  948. function node_resources_fpu(p: tnode): cardinal;
  949. var
  950. res1,res2,res3 : cardinal;
  951. begin
  952. result:=0;
  953. res1:=0;
  954. res2:=0;
  955. res3:=0;
  956. if p.inheritsfrom(tunarynode) then
  957. begin
  958. if assigned(tunarynode(p).left) then
  959. res1:=node_resources_fpu(tunarynode(p).left);
  960. if p.inheritsfrom(tbinarynode) then
  961. begin
  962. if assigned(tbinarynode(p).right) then
  963. res2:=node_resources_fpu(tbinarynode(p).right);
  964. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  965. res3:=node_resources_fpu(ttertiarynode(p).third)
  966. end;
  967. end;
  968. result:=max(max(res1,res2),res3);
  969. case p.nodetype of
  970. calln:
  971. { it could be a recursive call, so we never really know the number of used fpu registers }
  972. result:=maxfpuregs;
  973. realconstn,
  974. typeconvn,
  975. loadn :
  976. begin
  977. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  978. result:=max(result,1);
  979. end;
  980. assignn,
  981. addn,subn,muln,slashn,
  982. equaln,unequaln,gtn,gten,ltn,lten :
  983. begin
  984. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  985. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  986. result:=max(result,2);
  987. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  988. inc(result);
  989. end;
  990. else
  991. ;
  992. end;
  993. end;
  994. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  995. begin
  996. result:=fen_true;
  997. n.fileinfo:=pfileposinfo(arg)^;
  998. end;
  999. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  1000. begin
  1001. foreachnodestatic(n,@setnodefilepos,@filepos);
  1002. end;
  1003. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  1004. var
  1005. hn : tnode;
  1006. treechanged : ^boolean;
  1007. begin
  1008. result:=fen_false;
  1009. if n.inheritsfrom(tloopnode) and
  1010. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  1011. begin
  1012. // Try to simplify condition
  1013. doinlinesimplify(tloopnode(n).left);
  1014. // call directly second part below,
  1015. // which might change the loopnode into
  1016. // something else if the conditino is a constant node
  1017. include(tloopnode(n).loopflags,lnf_simplify_processing);
  1018. callsimplify(n,arg);
  1019. // Be careful, n might have change node type
  1020. if n.inheritsfrom(tloopnode) then
  1021. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  1022. end
  1023. else
  1024. begin
  1025. hn:=n.simplify(true);
  1026. if assigned(hn) then
  1027. begin
  1028. treechanged := arg;
  1029. if assigned(treechanged) then
  1030. treechanged^:=true
  1031. else
  1032. internalerror (201008181);
  1033. n.free;
  1034. n:=hn;
  1035. typecheckpass(n);
  1036. end;
  1037. end;
  1038. end;
  1039. { tries to simplify the given node calling the simplify method recursively }
  1040. procedure doinlinesimplify(var n : tnode);
  1041. var
  1042. treechanged : boolean;
  1043. begin
  1044. // Optimize if code first
  1045. repeat
  1046. treechanged:=false;
  1047. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  1048. until not(treechanged);
  1049. end;
  1050. function create_simplified_ord_const(const value: tconstexprint; def: tdef; forinline, rangecheck: boolean): tnode;
  1051. begin
  1052. if not forinline then
  1053. result:=genintconstnode(value)
  1054. else
  1055. result:=cordconstnode.create(value,def,rangecheck);
  1056. end;
  1057. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  1058. var
  1059. plist : ppropaccesslistitem;
  1060. begin
  1061. plist:=pl.firstsym;
  1062. while assigned(plist) do
  1063. begin
  1064. case plist^.sltype of
  1065. sl_load :
  1066. begin
  1067. addsymref(plist^.sym);
  1068. if not assigned(st) then
  1069. st:=plist^.sym.owner;
  1070. if (plist^.sym.typ<>staticvarsym) then
  1071. begin
  1072. { p1 can already contain the loadnode of
  1073. the class variable. When there is no tree yet we
  1074. may need to load it for with or objects }
  1075. if not assigned(p1) then
  1076. begin
  1077. case st.symtabletype of
  1078. withsymtable :
  1079. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1080. ObjectSymtable :
  1081. p1:=load_self_node;
  1082. else
  1083. ;
  1084. end;
  1085. end
  1086. end
  1087. else
  1088. begin
  1089. p1.free;
  1090. p1:=nil;
  1091. end;
  1092. if assigned(p1) then
  1093. p1:=csubscriptnode.create(plist^.sym,p1)
  1094. else
  1095. p1:=cloadnode.create(plist^.sym,st);
  1096. end;
  1097. sl_subscript :
  1098. begin
  1099. addsymref(plist^.sym);
  1100. p1:=csubscriptnode.create(plist^.sym,p1);
  1101. end;
  1102. sl_typeconv :
  1103. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1104. sl_absolutetype :
  1105. begin
  1106. p1:=ctypeconvnode.create(p1,plist^.def);
  1107. include(p1.flags,nf_absolute);
  1108. end;
  1109. sl_vec :
  1110. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1111. else
  1112. internalerror(200110205);
  1113. end;
  1114. plist:=plist^.next;
  1115. end;
  1116. end;
  1117. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1118. var
  1119. sl : tpropaccesslist;
  1120. procedure addnode(p:tnode);
  1121. begin
  1122. case p.nodetype of
  1123. subscriptn :
  1124. begin
  1125. addnode(tsubscriptnode(p).left);
  1126. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1127. end;
  1128. typeconvn :
  1129. begin
  1130. addnode(ttypeconvnode(p).left);
  1131. if nf_absolute in ttypeconvnode(p).flags then
  1132. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1133. else
  1134. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1135. end;
  1136. vecn :
  1137. begin
  1138. addnode(tvecnode(p).left);
  1139. if tvecnode(p).right.nodetype=ordconstn then
  1140. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1141. else
  1142. begin
  1143. Message(parser_e_illegal_expression);
  1144. { recovery }
  1145. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1146. end;
  1147. end;
  1148. loadn :
  1149. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1150. else
  1151. internalerror(200310282);
  1152. end;
  1153. end;
  1154. begin
  1155. sl:=tpropaccesslist.create;
  1156. addnode(p1);
  1157. result:=sl;
  1158. end;
  1159. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  1160. function handle_generic_staticfield_access:boolean;
  1161. var
  1162. tmp : tstoreddef;
  1163. pd : tprocdef;
  1164. begin
  1165. { in case we have a specialization inside a generic (thus the static var sym does not
  1166. exist) we simply simulate a non static access to avoid unnecessary errors }
  1167. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1168. begin
  1169. tmp:=tstoreddef(sym.owner.defowner);
  1170. while assigned(tmp) do
  1171. begin
  1172. if df_generic in tmp.defoptions then
  1173. begin
  1174. p1.free;
  1175. if assigned(current_procinfo) then
  1176. begin
  1177. pd:=current_procinfo.get_normal_proc.procdef;
  1178. if assigned(pd) and pd.no_self_node then
  1179. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1180. else
  1181. p1:=load_self_node;
  1182. end
  1183. else
  1184. p1:=load_self_node;
  1185. p1:=csubscriptnode.create(sym,p1);
  1186. exit(true);
  1187. end;
  1188. tmp:=tstoreddef(tmp.owner.defowner);
  1189. end;
  1190. end;
  1191. result:=false;
  1192. end;
  1193. var
  1194. static_name: shortstring;
  1195. srsymtable: tsymtable;
  1196. begin
  1197. result:=false;
  1198. { generate access code }
  1199. if (sp_static in sym.symoptions) then
  1200. begin
  1201. result:=true;
  1202. if handle_generic_staticfield_access then
  1203. exit;
  1204. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  1205. if sym.owner.defowner.typ=objectdef then
  1206. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  1207. else
  1208. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  1209. if assigned(sym) then
  1210. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1211. p1.free;
  1212. p1:=nil;
  1213. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1214. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1215. end;
  1216. end;
  1217. function is_bitpacked_access(n: tnode): boolean;
  1218. begin
  1219. case n.nodetype of
  1220. vecn:
  1221. result:=
  1222. is_packed_array(tvecnode(n).left.resultdef) and
  1223. { only orddefs and enumdefs are actually bitpacked. Don't consider
  1224. e.g. an access to a 3-byte record as "bitpacked", since it
  1225. isn't }
  1226. (tvecnode(n).left.resultdef.typ = arraydef) and
  1227. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  1228. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  1229. subscriptn:
  1230. result:=
  1231. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1232. { see above }
  1233. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  1234. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  1235. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1236. else
  1237. result:=false;
  1238. end;
  1239. end;
  1240. function genloadfield(n: tnode; const fieldname: string): tnode;
  1241. var
  1242. vs : tsym;
  1243. begin
  1244. if not assigned(n.resultdef) then
  1245. typecheckpass(n);
  1246. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1247. if not assigned(vs) or
  1248. (vs.typ<>fieldvarsym) then
  1249. internalerror(2010061902);
  1250. result:=csubscriptnode.create(vs,n);
  1251. end;
  1252. function has_no_code(n : tnode) : boolean;
  1253. begin
  1254. if n=nil then
  1255. begin
  1256. result:=true;
  1257. exit;
  1258. end;
  1259. case n.nodetype of
  1260. nothingn:
  1261. begin
  1262. result:=true;
  1263. exit;
  1264. end;
  1265. blockn:
  1266. begin
  1267. result:=has_no_code(tblocknode(n).left);
  1268. exit;
  1269. end;
  1270. statementn:
  1271. begin
  1272. repeat
  1273. result:=has_no_code(tstatementnode(n).left);
  1274. n:=tstatementnode(n).right;
  1275. until not(result) or not assigned(n);
  1276. exit;
  1277. end;
  1278. else
  1279. result:=false;
  1280. end;
  1281. end;
  1282. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1283. begin
  1284. result:=fen_false;
  1285. if (n.nodetype in [assignn,calln,asmn,finalizetempsn]) or
  1286. ((n.nodetype=inlinen) and
  1287. tinlinenode(n).may_have_sideeffect_norecurse
  1288. ) or
  1289. ((mhs_exceptions in pmhs_flags(arg)^) and
  1290. ((n.nodetype in [derefn,vecn,subscriptn]) or
  1291. ((n.nodetype in [addn,subn,muln,divn,slashn,unaryminusn]) and (n.localswitches*[cs_check_overflow,cs_check_range]<>[]))
  1292. )
  1293. ) or
  1294. ((n.nodetype=loadn) and
  1295. (
  1296. ((tloadnode(n).symtableentry.typ=absolutevarsym) and (tabsolutevarsym(tloadnode(n).symtableentry).abstyp=toaddr)) or
  1297. ((tloadnode(n).symtableentry.typ in [paravarsym,localvarsym,staticvarsym]) and
  1298. (vo_volatile in tabstractvarsym(tloadnode(n).symtableentry).varoptions)
  1299. )
  1300. )
  1301. ) then
  1302. result:=fen_norecurse_true;
  1303. end;
  1304. function might_have_sideeffects(n : tnode; const flags : tmhs_flags) : boolean;
  1305. begin
  1306. result:=foreachnodestatic(n,@check_for_sideeffect,@flags);
  1307. end;
  1308. function check_for_conditional_nodes(var n: tnode; arg: pointer): foreachnoderesult;
  1309. begin
  1310. result:=fen_false;
  1311. { this check is not complete yet, but sufficent to cover the current use case: swapping
  1312. of trees in expressions }
  1313. if (n.nodetype in [ifn,whilerepeatn,forn,tryexceptn]) or
  1314. ((n.nodetype in [orn,andn]) and is_boolean(n.resultdef) and doshortbooleval(n)) then
  1315. result:=fen_norecurse_true;
  1316. end;
  1317. function has_conditional_nodes(n : tnode) : boolean;
  1318. begin
  1319. result:=foreachnodestatic(n,@check_for_conditional_nodes,nil);
  1320. end;
  1321. var
  1322. nodecount : dword;
  1323. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1324. begin
  1325. inc(nodecount);
  1326. result:=fen_false;
  1327. end;
  1328. function node_count(node : tnode) : dword;
  1329. begin
  1330. nodecount:=0;
  1331. foreachnodestatic(node,@donodecount,nil);
  1332. result:=nodecount;
  1333. end;
  1334. function donodecount_weighted(var n: tnode; arg: pointer): foreachnoderesult;
  1335. begin
  1336. if not(n.nodetype in [blockn,statementn,callparan,nothingn]) then
  1337. inc(nodecount);
  1338. result:=fen_false;
  1339. end;
  1340. function node_count_weighted(node : tnode) : dword;
  1341. begin
  1342. nodecount:=0;
  1343. foreachnodestatic(node,@donodecount_weighted,nil);
  1344. result:=nodecount;
  1345. end;
  1346. function is_const(node : tnode) : boolean;
  1347. begin
  1348. result:=is_constnode(node) or
  1349. ((node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempflags)) or
  1350. ((node.nodetype=loadn) and (tloadnode(node).symtableentry.typ=paravarsym) and (tparavarsym(tloadnode(node).symtableentry).varspez in [vs_const,vs_constref]));
  1351. end;
  1352. function actualtargetnode(n : pnode) : pnode;
  1353. begin
  1354. result:=n;
  1355. case n^.nodetype of
  1356. typeconvn:
  1357. if ttypeconvnode(n^).retains_value_location then
  1358. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1359. else
  1360. ;
  1361. end;
  1362. end;
  1363. procedure replacenode(var dest,src : tnode);
  1364. var
  1365. t : tnode;
  1366. begin
  1367. t:=src;
  1368. { set src nil before free'ing dest because
  1369. src could be part of dest }
  1370. src:=nil;
  1371. dest.Free;
  1372. dest:=t;
  1373. end;
  1374. function get_open_const_array(p : tnode) : tnode;
  1375. begin
  1376. result:=p;
  1377. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1378. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  1379. end;
  1380. function do_node_reset_flags(var n: tnode; arg: pointer): foreachnoderesult;
  1381. begin
  1382. result:=fen_false;
  1383. n.flags:=n.flags-tnodeflags(arg^);
  1384. end;
  1385. procedure node_reset_flags(p : tnode; nf : tnodeflags);
  1386. begin
  1387. foreachnodestatic(p,@do_node_reset_flags,@nf);
  1388. end;
  1389. type
  1390. tlocalswitchchange = record
  1391. cs : tlocalswitch;
  1392. enable : boolean;
  1393. end;
  1394. plocalswitchchange = ^tlocalswitchchange;
  1395. function do_change_local_settings(var p : tnode;plsc : pointer) : foreachnoderesult;
  1396. begin
  1397. if plocalswitchchange(plsc)^.enable then
  1398. include(p.localswitches, plocalswitchchange(plsc)^.cs)
  1399. else
  1400. exclude(p.localswitches, plocalswitchchange(plsc)^.cs);
  1401. result:=fen_true;
  1402. end;
  1403. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  1404. var
  1405. lsc : tlocalswitchchange;
  1406. begin
  1407. lsc.cs:=cs;
  1408. lsc.enable:=enable;
  1409. foreachnodestatic(p,@do_change_local_settings,@lsc);
  1410. end;
  1411. function doshortbooleval(p : tnode) : Boolean;
  1412. begin
  1413. Result:=(p.nodetype in [orn,andn]) and ((nf_short_bool in taddnode(p).flags) or not(cs_full_boolean_eval in p.localswitches));
  1414. end;
  1415. function is_constintvalue(p: tnode; l: Tconstexprint): Boolean;
  1416. begin
  1417. Result:=is_constintnode(p) and (tordconstnode(p).value=l);
  1418. end;
  1419. function is_inlinefunction(p: tnode; i: tinlinenumber): Boolean;
  1420. begin
  1421. Result:=(p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=i);
  1422. end;
  1423. function MatchAndTransformNodesCommutative(n1,n2 : tnode;matchproc : TMatchProc2;transformproc : TTransformProc2;var res : tnode) : Boolean;
  1424. begin
  1425. res:=nil;
  1426. result:=true;
  1427. if matchproc(n1,n2) then
  1428. res:=transformproc(n1,n2)
  1429. else if matchproc(n2,n1) then
  1430. res:=transformproc(n2,n1)
  1431. else
  1432. result:=false;
  1433. end;
  1434. end.