nutils.pas 53 KB

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