nutils.pas 59 KB

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