nutils.pas 62 KB

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