nutils.pas 53 KB

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