nutils.pas 61 KB

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