nutils.pas 51 KB

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