nutils.pas 61 KB

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