nutils.pas 60 KB

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