nutils.pas 58 KB

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