nutils.pas 60 KB

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