nutils.pas 61 KB

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