nutils.pas 38 KB

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