nutils.pas 54 KB

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