nutils.pas 43 KB

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