nutils.pas 52 KB

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