nutils.pas 60 KB

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