nutils.pas 59 KB

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