nutils.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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. if is_object(tabstractvarsym(srsym).vardef) or is_record(tabstractvarsym(srsym).vardef) then
  502. include(tloadnode(result).loadnodeflags,loadnf_load_addr);
  503. end
  504. else
  505. begin
  506. result:=cerrornode.create;
  507. CGMessage(parser_e_illegal_expression);
  508. end;
  509. typecheckpass(result);
  510. end;
  511. function load_vmt_pointer_node:tnode;
  512. var
  513. srsym : tsym;
  514. begin
  515. result:=nil;
  516. srsym:=get_local_or_para_sym('vmt');
  517. if assigned(srsym) then
  518. result:=cloadnode.create(srsym,srsym.owner)
  519. else
  520. begin
  521. result:=cerrornode.create;
  522. CGMessage(parser_e_illegal_expression);
  523. end;
  524. typecheckpass(result);
  525. end;
  526. function is_self_node(p:tnode):boolean;
  527. begin
  528. is_self_node:=(p.nodetype=loadn) and
  529. (tloadnode(p).symtableentry.typ=paravarsym) and
  530. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  531. end;
  532. function load_vmt_for_self_node(self_node: tnode): tnode;
  533. var
  534. self_resultdef: tdef;
  535. obj_def: tobjectdef;
  536. self_temp,
  537. vmt_temp: ttempcreatenode;
  538. check_self,n: tnode;
  539. stat: tstatementnode;
  540. block: tblocknode;
  541. paras: tcallparanode;
  542. docheck,is_typecasted_classref: boolean;
  543. begin
  544. self_resultdef:=self_node.resultdef;
  545. case self_resultdef.typ of
  546. classrefdef:
  547. begin
  548. obj_def:=tobjectdef(tclassrefdef(self_resultdef).pointeddef);
  549. end;
  550. objectdef:
  551. obj_def:=tobjectdef(self_resultdef);
  552. else
  553. internalerror(2015052701);
  554. end;
  555. n:=self_node;
  556. is_typecasted_classref:=false;
  557. if (n.nodetype=typeconvn) then
  558. begin
  559. while assigned(n) and (n.nodetype=typeconvn) and (nf_explicit in ttypeconvnode(n).flags) do
  560. n:=ttypeconvnode(n).left;
  561. if assigned(n) and (n.resultdef.typ=classrefdef) then
  562. is_typecasted_classref:=true;
  563. end;
  564. if is_classhelper(obj_def) then
  565. obj_def:=tobjectdef(tobjectdef(obj_def).extendeddef);
  566. docheck:=
  567. not(is_interface(obj_def)) and
  568. not(is_cppclass(obj_def)) and
  569. not(is_objc_class_or_protocol(obj_def)) and
  570. (([cs_check_object,cs_check_range]*current_settings.localswitches)<>[]);
  571. block:=nil;
  572. stat:=nil;
  573. if docheck then
  574. begin
  575. { check for nil self-pointer }
  576. block:=internalstatements(stat);
  577. self_temp:=ctempcreatenode.create_value(
  578. self_resultdef,self_resultdef.size,tt_persistent,true,
  579. self_node);
  580. addstatement(stat,self_temp);
  581. { in case of an object, self can only be nil if it's a dereferenced
  582. node somehow
  583. }
  584. if not is_object(self_resultdef) or
  585. (actualtargetnode(@self_node)^.nodetype=derefn) then
  586. begin
  587. check_self:=ctemprefnode.create(self_temp);
  588. if is_object(self_resultdef) then
  589. check_self:=caddrnode.create(check_self);
  590. addstatement(stat,cifnode.create(
  591. caddnode.create(equaln,
  592. ctypeconvnode.create_explicit(
  593. check_self,
  594. voidpointertype
  595. ),
  596. cnilnode.create),
  597. ccallnode.createintern('fpc_objecterror',nil),
  598. nil)
  599. );
  600. end;
  601. addstatement(stat,ctempdeletenode.create_normal_temp(self_temp));
  602. self_node:=ctemprefnode.create(self_temp);
  603. end;
  604. { in case of a classref, the "instance" is a pointer
  605. to pointer to a VMT and there is no vmt field }
  606. if is_typecasted_classref or (self_resultdef.typ=classrefdef) then
  607. result:=self_node
  608. { get the VMT field in case of a class/object }
  609. else if (self_resultdef.typ=objectdef) and
  610. assigned(tobjectdef(self_resultdef).vmt_field) then
  611. result:=csubscriptnode.create(tobjectdef(self_resultdef).vmt_field,self_node)
  612. { in case of an interface, the "instance" is a pointer to a pointer
  613. to a VMT -> dereference once already }
  614. else
  615. { in case of an interface/classref, the "instance" is a pointer
  616. to pointer to a VMT and there is no vmt field }
  617. result:=cderefnode.create(
  618. ctypeconvnode.create_explicit(
  619. self_node,
  620. cpointerdef.getreusable(voidpointertype)
  621. )
  622. );
  623. result:=ctypeconvnode.create_explicit(
  624. result,
  625. cpointerdef.getreusable(obj_def.vmt_def));
  626. typecheckpass(result);
  627. if docheck then
  628. begin
  629. { add a vmt validity check }
  630. vmt_temp:=ctempcreatenode.create_value(result.resultdef,result.resultdef.size,tt_persistent,true,result);
  631. addstatement(stat,vmt_temp);
  632. paras:=ccallparanode.create(ctemprefnode.create(vmt_temp),nil);
  633. if cs_check_object in current_settings.localswitches then
  634. begin
  635. paras:=ccallparanode.create(
  636. cloadvmtaddrnode.create(ctypenode.create(obj_def)),
  637. paras
  638. );
  639. addstatement(stat,
  640. ccallnode.createintern(
  641. 'fpc_check_object_ext',paras
  642. )
  643. );
  644. end
  645. else
  646. addstatement(stat,
  647. ccallnode.createintern(
  648. 'fpc_check_object',paras
  649. )
  650. );
  651. addstatement(stat,ctempdeletenode.create_normal_temp(vmt_temp));
  652. addstatement(stat,ctemprefnode.create(vmt_temp));
  653. result:=block;
  654. end
  655. end;
  656. { this function must return a very high value ("infinity") for }
  657. { trees containing a call, the rest can be balanced more or less }
  658. { at will, probably best mainly in terms of required memory }
  659. { accesses }
  660. function node_complexity(p: tnode): cardinal;
  661. var
  662. correction: byte;
  663. {$ifdef ARM}
  664. dummy : byte;
  665. {$endif ARM}
  666. begin
  667. result := 0;
  668. while assigned(p) do
  669. begin
  670. case p.nodetype of
  671. { floating point constants usually need loading from memory }
  672. realconstn:
  673. begin
  674. result:=2;
  675. exit;
  676. end;
  677. setconstn,
  678. stringconstn,
  679. temprefn,
  680. loadvmtaddrn,
  681. { main reason for the next one: we can't take the address of }
  682. { loadparentfpnode, so replacing it by a temp which is the }
  683. { address of this node's location and then dereferencing }
  684. { doesn't work. If changed, check whether webtbs/tw0935 }
  685. { still works with nodeinlining (JM) }
  686. loadparentfpn:
  687. begin
  688. result := 1;
  689. exit;
  690. end;
  691. loadn:
  692. begin
  693. if assigned(tloadnode(p).left) then
  694. inc(result,node_complexity(tloadnode(p).left));
  695. { threadvars need a helper call }
  696. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  697. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  698. inc(result,5)
  699. else if not((tloadnode(p).symtableentry.typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  700. (tabstractvarsym(tloadnode(p).symtableentry).varregable in [vr_intreg,vr_mmreg,vr_fpureg])) then
  701. inc(result);
  702. if (tloadnode(p).symtableentry.typ=paravarsym) and
  703. not(tabstractvarsym(tloadnode(p).symtableentry).varregable=vr_addr) and
  704. tloadnode(p).is_addr_param_load then
  705. inc(result);
  706. if (result >= NODE_COMPLEXITY_INF) then
  707. result := NODE_COMPLEXITY_INF;
  708. exit;
  709. end;
  710. subscriptn:
  711. begin
  712. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  713. is_bitpacked_access(p) then
  714. inc(result,2)
  715. { non-packed, int. regable records cause no extra
  716. overhead no overhead if the fields are aligned to register boundaries }
  717. else if tstoreddef(p.resultdef).is_intregable and (tsubscriptnode(p).vs.fieldoffset mod sizeof(aint)<>0) then
  718. inc(result,1);
  719. if (result = NODE_COMPLEXITY_INF) then
  720. exit;
  721. p := tunarynode(p).left;
  722. end;
  723. labeln,
  724. blockn:
  725. p := tunarynode(p).left;
  726. callparan:
  727. begin
  728. { call to decr? }
  729. if is_managed_type(tunarynode(p).left.resultdef) and
  730. assigned(tcallparanode(p).parasym) and (tcallparanode(p).parasym.varspez=vs_out) then
  731. begin
  732. result:=NODE_COMPLEXITY_INF;
  733. exit;
  734. end
  735. else
  736. begin
  737. inc(result);
  738. if (result = NODE_COMPLEXITY_INF) then
  739. exit;
  740. p := tunarynode(p).left;
  741. end;
  742. end;
  743. notn,
  744. derefn :
  745. begin
  746. inc(result);
  747. if (result = NODE_COMPLEXITY_INF) then
  748. exit;
  749. p := tunarynode(p).left;
  750. end;
  751. addrn:
  752. begin
  753. inc(result);
  754. if (result = NODE_COMPLEXITY_INF) then
  755. exit;
  756. p := tunarynode(p).left;
  757. end;
  758. typeconvn:
  759. begin
  760. { may be more complex in some cases }
  761. if not(ttypeconvnode(p).retains_value_location) and
  762. not((ttypeconvnode(p).convtype=tc_pointer_2_array) and (ttypeconvnode(p).left.expectloc in [LOC_CREGISTER,LOC_REGISTER,LOC_CONSTANT])) then
  763. inc(result);
  764. if result = NODE_COMPLEXITY_INF then
  765. exit;
  766. p := tunarynode(p).left;
  767. end;
  768. vecn,
  769. statementn:
  770. begin
  771. inc(result,node_complexity(tbinarynode(p).left));
  772. if (result >= NODE_COMPLEXITY_INF) then
  773. begin
  774. result := NODE_COMPLEXITY_INF;
  775. exit;
  776. end;
  777. p := tbinarynode(p).right;
  778. end;
  779. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  780. shln,shrn,
  781. equaln,unequaln,gtn,gten,ltn,lten,
  782. assignn:
  783. begin
  784. {$ifdef CPU64BITALU}
  785. correction:=1;
  786. {$else CPU64BITALU}
  787. correction:=2;
  788. {$endif CPU64BITALU}
  789. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  790. if (p.nodetype in [muln,divn,modn]) then
  791. inc(result,5*correction*correction);
  792. if (result >= NODE_COMPLEXITY_INF) then
  793. begin
  794. result := NODE_COMPLEXITY_INF;
  795. exit;
  796. end;
  797. p := tbinarynode(p).right;
  798. end;
  799. ordconstn:
  800. begin
  801. {$ifdef ARM}
  802. if not(is_shifter_const(aint(tordconstnode(p).value.svalue),dummy)) then
  803. result:=2;
  804. {$endif ARM}
  805. exit;
  806. end;
  807. exitn:
  808. begin
  809. inc(result,2);
  810. if (result >= NODE_COMPLEXITY_INF) then
  811. begin
  812. result := NODE_COMPLEXITY_INF;
  813. exit;
  814. end;
  815. p:=texitnode(p).left;
  816. end;
  817. tempcreaten,
  818. tempdeleten,
  819. pointerconstn,
  820. nothingn,
  821. niln:
  822. exit;
  823. inlinen:
  824. begin
  825. { this code assumes that the inline node has }
  826. { already been firstpassed, and consequently }
  827. { that inline nodes which are transformed into }
  828. { calls already have been transformed }
  829. case tinlinenode(p).inlinenumber of
  830. in_lo_qword,
  831. in_hi_qword,
  832. in_lo_long,
  833. in_hi_long,
  834. in_lo_word,
  835. in_hi_word,
  836. in_length_x,
  837. in_assigned_x,
  838. in_pred_x,
  839. in_succ_x,
  840. in_round_real,
  841. in_trunc_real,
  842. in_int_real,
  843. in_frac_real,
  844. in_pi_real,
  845. in_abs_real,
  846. in_aligned_x,
  847. in_unaligned_x,
  848. in_volatile_x,
  849. in_prefetch_var:
  850. begin
  851. inc(result);
  852. p:=tunarynode(p).left;
  853. end;
  854. in_cos_real,
  855. in_sin_real,
  856. in_arctan_real,
  857. in_sqr_real,
  858. in_sqrt_real,
  859. in_ln_real:
  860. begin
  861. inc(result,2);
  862. if (result >= NODE_COMPLEXITY_INF) then
  863. begin
  864. result:=NODE_COMPLEXITY_INF;
  865. exit;
  866. end;
  867. p:=tunarynode(p).left;
  868. end;
  869. in_abs_long:
  870. begin
  871. inc(result,3);
  872. if (result >= NODE_COMPLEXITY_INF) then
  873. begin
  874. result:=NODE_COMPLEXITY_INF;
  875. exit;
  876. end;
  877. p:=tunarynode(p).left;
  878. end;
  879. in_sizeof_x,
  880. in_typeof_x:
  881. begin
  882. inc(result);
  883. if (tinlinenode(p).left.nodetype<>typen) then
  884. { get instance vmt }
  885. p:=tunarynode(p).left
  886. else
  887. { type vmt = global symbol, result is }
  888. { already increased above }
  889. exit;
  890. end;
  891. {$ifdef SUPPORT_MMX}
  892. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  893. {$endif SUPPORT_MMX}
  894. { load from global symbol }
  895. in_typeinfo_x,
  896. { load frame pointer }
  897. in_get_frame,
  898. in_get_caller_frame,
  899. in_get_caller_addr:
  900. begin
  901. inc(result);
  902. exit;
  903. end;
  904. in_inc_x,
  905. in_dec_x,
  906. in_include_x_y,
  907. in_exclude_x_y,
  908. in_assert_x_y :
  909. begin
  910. { operation (add, sub, or, and }
  911. inc(result);
  912. { left expression }
  913. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  914. if (result >= NODE_COMPLEXITY_INF) then
  915. begin
  916. result := NODE_COMPLEXITY_INF;
  917. exit;
  918. end;
  919. p:=tcallparanode(tunarynode(p).left).right;
  920. if assigned(p) then
  921. p:=tcallparanode(p).left;
  922. end;
  923. else
  924. begin
  925. result := NODE_COMPLEXITY_INF;
  926. exit;
  927. end;
  928. end;
  929. end;
  930. finalizetempsn:
  931. begin
  932. result:=NODE_COMPLEXITY_INF;
  933. exit;
  934. end;
  935. else
  936. begin
  937. result := NODE_COMPLEXITY_INF;
  938. exit;
  939. end;
  940. end;
  941. end;
  942. end;
  943. { this function returns an indication how much fpu registers
  944. will be required.
  945. Note: The algorithms need to be pessimistic to prevent a
  946. fpu stack overflow on i386 }
  947. function node_resources_fpu(p: tnode): cardinal;
  948. var
  949. res1,res2,res3 : cardinal;
  950. begin
  951. result:=0;
  952. res1:=0;
  953. res2:=0;
  954. res3:=0;
  955. if p.inheritsfrom(tunarynode) then
  956. begin
  957. if assigned(tunarynode(p).left) then
  958. res1:=node_resources_fpu(tunarynode(p).left);
  959. if p.inheritsfrom(tbinarynode) then
  960. begin
  961. if assigned(tbinarynode(p).right) then
  962. res2:=node_resources_fpu(tbinarynode(p).right);
  963. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  964. res3:=node_resources_fpu(ttertiarynode(p).third)
  965. end;
  966. end;
  967. result:=max(max(res1,res2),res3);
  968. case p.nodetype of
  969. calln:
  970. { it could be a recursive call, so we never really know the number of used fpu registers }
  971. result:=maxfpuregs;
  972. realconstn,
  973. typeconvn,
  974. loadn :
  975. begin
  976. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  977. result:=max(result,1);
  978. end;
  979. assignn,
  980. addn,subn,muln,slashn,
  981. equaln,unequaln,gtn,gten,ltn,lten :
  982. begin
  983. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  984. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  985. result:=max(result,2);
  986. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  987. inc(result);
  988. end;
  989. else
  990. ;
  991. end;
  992. end;
  993. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  994. begin
  995. result:=fen_true;
  996. n.fileinfo:=pfileposinfo(arg)^;
  997. end;
  998. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  999. begin
  1000. foreachnodestatic(n,@setnodefilepos,@filepos);
  1001. end;
  1002. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  1003. var
  1004. hn : tnode;
  1005. treechanged : ^boolean;
  1006. begin
  1007. result:=fen_false;
  1008. if n.inheritsfrom(tloopnode) and
  1009. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  1010. begin
  1011. // Try to simplify condition
  1012. doinlinesimplify(tloopnode(n).left);
  1013. // call directly second part below,
  1014. // which might change the loopnode into
  1015. // something else if the conditino is a constant node
  1016. include(tloopnode(n).loopflags,lnf_simplify_processing);
  1017. callsimplify(n,arg);
  1018. // Be careful, n might have change node type
  1019. if n.inheritsfrom(tloopnode) then
  1020. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  1021. end
  1022. else
  1023. begin
  1024. hn:=n.simplify(true);
  1025. if assigned(hn) then
  1026. begin
  1027. treechanged := arg;
  1028. if assigned(treechanged) then
  1029. treechanged^:=true
  1030. else
  1031. internalerror (201008181);
  1032. n.free;
  1033. n:=hn;
  1034. typecheckpass(n);
  1035. end;
  1036. end;
  1037. end;
  1038. { tries to simplify the given node calling the simplify method recursively }
  1039. procedure doinlinesimplify(var n : tnode);
  1040. var
  1041. treechanged : boolean;
  1042. begin
  1043. // Optimize if code first
  1044. repeat
  1045. treechanged:=false;
  1046. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  1047. until not(treechanged);
  1048. end;
  1049. function create_simplified_ord_const(const value: tconstexprint; def: tdef; forinline, rangecheck: boolean): tnode;
  1050. begin
  1051. if not forinline then
  1052. result:=genintconstnode(value)
  1053. else
  1054. result:=cordconstnode.create(value,def,rangecheck);
  1055. end;
  1056. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  1057. var
  1058. plist : ppropaccesslistitem;
  1059. begin
  1060. plist:=pl.firstsym;
  1061. while assigned(plist) do
  1062. begin
  1063. case plist^.sltype of
  1064. sl_load :
  1065. begin
  1066. addsymref(plist^.sym);
  1067. if not assigned(st) then
  1068. st:=plist^.sym.owner;
  1069. if (plist^.sym.typ<>staticvarsym) then
  1070. begin
  1071. { p1 can already contain the loadnode of
  1072. the class variable. When there is no tree yet we
  1073. may need to load it for with or objects }
  1074. if not assigned(p1) then
  1075. begin
  1076. case st.symtabletype of
  1077. withsymtable :
  1078. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  1079. ObjectSymtable :
  1080. p1:=load_self_node;
  1081. else
  1082. ;
  1083. end;
  1084. end
  1085. end
  1086. else
  1087. begin
  1088. p1.free;
  1089. p1:=nil;
  1090. end;
  1091. if assigned(p1) then
  1092. p1:=csubscriptnode.create(plist^.sym,p1)
  1093. else
  1094. p1:=cloadnode.create(plist^.sym,st);
  1095. end;
  1096. sl_subscript :
  1097. begin
  1098. addsymref(plist^.sym);
  1099. p1:=csubscriptnode.create(plist^.sym,p1);
  1100. end;
  1101. sl_typeconv :
  1102. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  1103. sl_absolutetype :
  1104. begin
  1105. p1:=ctypeconvnode.create(p1,plist^.def);
  1106. include(p1.flags,nf_absolute);
  1107. end;
  1108. sl_vec :
  1109. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  1110. else
  1111. internalerror(200110205);
  1112. end;
  1113. plist:=plist^.next;
  1114. end;
  1115. end;
  1116. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  1117. var
  1118. sl : tpropaccesslist;
  1119. procedure addnode(p:tnode);
  1120. begin
  1121. case p.nodetype of
  1122. subscriptn :
  1123. begin
  1124. addnode(tsubscriptnode(p).left);
  1125. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1126. end;
  1127. typeconvn :
  1128. begin
  1129. addnode(ttypeconvnode(p).left);
  1130. if nf_absolute in ttypeconvnode(p).flags then
  1131. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1132. else
  1133. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1134. end;
  1135. vecn :
  1136. begin
  1137. addnode(tvecnode(p).left);
  1138. if tvecnode(p).right.nodetype=ordconstn then
  1139. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1140. else
  1141. begin
  1142. Message(parser_e_illegal_expression);
  1143. { recovery }
  1144. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1145. end;
  1146. end;
  1147. loadn :
  1148. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1149. else
  1150. internalerror(200310282);
  1151. end;
  1152. end;
  1153. begin
  1154. sl:=tpropaccesslist.create;
  1155. addnode(p1);
  1156. result:=sl;
  1157. end;
  1158. function handle_staticfield_access(sym: tsym; var p1: tnode): boolean;
  1159. function handle_generic_staticfield_access:boolean;
  1160. var
  1161. tmp : tstoreddef;
  1162. pd : tprocdef;
  1163. begin
  1164. { in case we have a specialization inside a generic (thus the static var sym does not
  1165. exist) we simply simulate a non static access to avoid unnecessary errors }
  1166. if assigned(sym.owner.defowner) and (df_specialization in tstoreddef(sym.owner.defowner).defoptions) then
  1167. begin
  1168. tmp:=tstoreddef(sym.owner.defowner);
  1169. while assigned(tmp) do
  1170. begin
  1171. if df_generic in tmp.defoptions then
  1172. begin
  1173. p1.free;
  1174. if assigned(current_procinfo) then
  1175. begin
  1176. pd:=current_procinfo.get_normal_proc.procdef;
  1177. if assigned(pd) and pd.no_self_node then
  1178. p1:=cloadvmtaddrnode.create(ctypenode.create(pd.struct))
  1179. else
  1180. p1:=load_self_node;
  1181. end
  1182. else
  1183. p1:=load_self_node;
  1184. p1:=csubscriptnode.create(sym,p1);
  1185. exit(true);
  1186. end;
  1187. tmp:=tstoreddef(tmp.owner.defowner);
  1188. end;
  1189. end;
  1190. result:=false;
  1191. end;
  1192. var
  1193. static_name: shortstring;
  1194. srsymtable: tsymtable;
  1195. begin
  1196. result:=false;
  1197. { generate access code }
  1198. if (sp_static in sym.symoptions) then
  1199. begin
  1200. result:=true;
  1201. if handle_generic_staticfield_access then
  1202. exit;
  1203. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  1204. if sym.owner.defowner.typ=objectdef then
  1205. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  1206. else
  1207. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  1208. if assigned(sym) then
  1209. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  1210. p1.free;
  1211. p1:=nil;
  1212. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  1213. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  1214. end;
  1215. end;
  1216. function is_bitpacked_access(n: tnode): boolean;
  1217. begin
  1218. case n.nodetype of
  1219. vecn:
  1220. result:=
  1221. is_packed_array(tvecnode(n).left.resultdef) and
  1222. { only orddefs and enumdefs are actually bitpacked. Don't consider
  1223. e.g. an access to a 3-byte record as "bitpacked", since it
  1224. isn't }
  1225. (tvecnode(n).left.resultdef.typ = arraydef) and
  1226. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  1227. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  1228. subscriptn:
  1229. result:=
  1230. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1231. { see above }
  1232. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  1233. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  1234. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1235. else
  1236. result:=false;
  1237. end;
  1238. end;
  1239. function genloadfield(n: tnode; const fieldname: string): tnode;
  1240. var
  1241. vs : tsym;
  1242. begin
  1243. if not assigned(n.resultdef) then
  1244. typecheckpass(n);
  1245. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1246. if not assigned(vs) or
  1247. (vs.typ<>fieldvarsym) then
  1248. internalerror(2010061902);
  1249. result:=csubscriptnode.create(vs,n);
  1250. end;
  1251. function has_no_code(n : tnode) : boolean;
  1252. begin
  1253. if n=nil then
  1254. begin
  1255. result:=true;
  1256. exit;
  1257. end;
  1258. case n.nodetype of
  1259. nothingn:
  1260. begin
  1261. result:=true;
  1262. exit;
  1263. end;
  1264. blockn:
  1265. begin
  1266. result:=has_no_code(tblocknode(n).left);
  1267. exit;
  1268. end;
  1269. statementn:
  1270. begin
  1271. repeat
  1272. result:=has_no_code(tstatementnode(n).left);
  1273. n:=tstatementnode(n).right;
  1274. until not(result) or not assigned(n);
  1275. exit;
  1276. end;
  1277. else
  1278. result:=false;
  1279. end;
  1280. end;
  1281. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1282. begin
  1283. result:=fen_false;
  1284. if (n.nodetype in [assignn,calln,asmn,finalizetempsn]) or
  1285. ((n.nodetype=inlinen) and
  1286. tinlinenode(n).may_have_sideeffect_norecurse
  1287. ) or
  1288. ((mhs_exceptions in pmhs_flags(arg)^) and
  1289. ((n.nodetype in [derefn,vecn,subscriptn]) or
  1290. ((n.nodetype in [addn,subn,muln,divn,slashn,unaryminusn]) and (n.localswitches*[cs_check_overflow,cs_check_range]<>[]))
  1291. )
  1292. ) or
  1293. ((n.nodetype=loadn) and
  1294. (
  1295. ((tloadnode(n).symtableentry.typ=absolutevarsym) and (tabsolutevarsym(tloadnode(n).symtableentry).abstyp=toaddr)) or
  1296. ((tloadnode(n).symtableentry.typ in [paravarsym,localvarsym,staticvarsym]) and
  1297. (vo_volatile in tabstractvarsym(tloadnode(n).symtableentry).varoptions)
  1298. )
  1299. )
  1300. ) then
  1301. result:=fen_norecurse_true;
  1302. end;
  1303. function might_have_sideeffects(n : tnode; const flags : tmhs_flags) : boolean;
  1304. begin
  1305. result:=foreachnodestatic(n,@check_for_sideeffect,@flags);
  1306. end;
  1307. function check_for_conditional_nodes(var n: tnode; arg: pointer): foreachnoderesult;
  1308. begin
  1309. result:=fen_false;
  1310. { this check is not complete yet, but sufficent to cover the current use case: swapping
  1311. of trees in expressions }
  1312. if (n.nodetype in [ifn,whilerepeatn,forn,tryexceptn]) or
  1313. ((n.nodetype in [orn,andn]) and is_boolean(n.resultdef) and doshortbooleval(n)) then
  1314. result:=fen_norecurse_true;
  1315. end;
  1316. function has_conditional_nodes(n : tnode) : boolean;
  1317. begin
  1318. result:=foreachnodestatic(n,@check_for_conditional_nodes,nil);
  1319. end;
  1320. var
  1321. nodecount : dword;
  1322. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1323. begin
  1324. inc(nodecount);
  1325. result:=fen_false;
  1326. end;
  1327. function node_count(node : tnode) : dword;
  1328. begin
  1329. nodecount:=0;
  1330. foreachnodestatic(node,@donodecount,nil);
  1331. result:=nodecount;
  1332. end;
  1333. function donodecount_weighted(var n: tnode; arg: pointer): foreachnoderesult;
  1334. begin
  1335. if not(n.nodetype in [blockn,statementn,callparan,nothingn]) then
  1336. inc(nodecount);
  1337. result:=fen_false;
  1338. end;
  1339. function node_count_weighted(node : tnode) : dword;
  1340. begin
  1341. nodecount:=0;
  1342. foreachnodestatic(node,@donodecount_weighted,nil);
  1343. result:=nodecount;
  1344. end;
  1345. function is_const(node : tnode) : boolean;
  1346. begin
  1347. result:=is_constnode(node) or
  1348. ((node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempflags)) or
  1349. ((node.nodetype=loadn) and (tloadnode(node).symtableentry.typ=paravarsym) and (tparavarsym(tloadnode(node).symtableentry).varspez in [vs_const,vs_constref]));
  1350. end;
  1351. function actualtargetnode(n : pnode) : pnode;
  1352. begin
  1353. result:=n;
  1354. case n^.nodetype of
  1355. typeconvn:
  1356. if ttypeconvnode(n^).retains_value_location then
  1357. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1358. else
  1359. ;
  1360. end;
  1361. end;
  1362. procedure replacenode(var dest,src : tnode);
  1363. var
  1364. t : tnode;
  1365. begin
  1366. t:=src;
  1367. { set src nil before free'ing dest because
  1368. src could be part of dest }
  1369. src:=nil;
  1370. dest.Free;
  1371. dest:=t;
  1372. end;
  1373. function get_open_const_array(p : tnode) : tnode;
  1374. begin
  1375. result:=p;
  1376. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1377. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  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-tnodeflags(arg^);
  1383. end;
  1384. procedure node_reset_flags(p : tnode; nf : tnodeflags);
  1385. begin
  1386. foreachnodestatic(p,@do_node_reset_flags,@nf);
  1387. end;
  1388. type
  1389. tlocalswitchchange = record
  1390. cs : tlocalswitch;
  1391. enable : boolean;
  1392. end;
  1393. plocalswitchchange = ^tlocalswitchchange;
  1394. function do_change_local_settings(var p : tnode;plsc : pointer) : foreachnoderesult;
  1395. begin
  1396. if plocalswitchchange(plsc)^.enable then
  1397. include(p.localswitches, plocalswitchchange(plsc)^.cs)
  1398. else
  1399. exclude(p.localswitches, plocalswitchchange(plsc)^.cs);
  1400. result:=fen_true;
  1401. end;
  1402. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  1403. var
  1404. lsc : tlocalswitchchange;
  1405. begin
  1406. lsc.cs:=cs;
  1407. lsc.enable:=enable;
  1408. foreachnodestatic(p,@do_change_local_settings,@lsc);
  1409. end;
  1410. function doshortbooleval(p : tnode) : Boolean;
  1411. begin
  1412. Result:=(p.nodetype in [orn,andn]) and ((nf_short_bool in taddnode(p).flags) or not(cs_full_boolean_eval in p.localswitches));
  1413. end;
  1414. function is_constintvalue(p: tnode; l: Tconstexprint): Boolean;
  1415. begin
  1416. Result:=is_constintnode(p) and (tordconstnode(p).value=l);
  1417. end;
  1418. function is_inlinefunction(p: tnode; i: tinlinenumber): Boolean;
  1419. begin
  1420. Result:=(p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=i);
  1421. end;
  1422. function MatchAndTransformNodesCommutative(n1,n2 : tnode;matchproc : TMatchProc2;transformproc : TTransformProc2;var res : tnode) : Boolean;
  1423. begin
  1424. res:=nil;
  1425. result:=true;
  1426. if matchproc(n1,n2) then
  1427. res:=transformproc(n1,n2)
  1428. else if matchproc(n2,n1) then
  1429. res:=transformproc(n2,n1)
  1430. else
  1431. result:=false;
  1432. end;
  1433. end.