nutils.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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(traisenode(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(traisenode(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. setconstn,
  482. stringconstn,
  483. temprefn,
  484. loadvmtaddrn,
  485. { main reason for the next one: we can't take the address of }
  486. { loadparentfpnode, so replacing it by a temp which is the }
  487. { address of this node's location and then dereferencing }
  488. { doesn't work. If changed, check whether webtbs/tw0935 }
  489. { still works with nodeinlining (JM) }
  490. loadparentfpn:
  491. begin
  492. result := 1;
  493. exit;
  494. end;
  495. loadn:
  496. begin
  497. if assigned(tloadnode(p).left) then
  498. inc(result,node_complexity(tloadnode(p).left));
  499. { threadvars need a helper call }
  500. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  501. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  502. inc(result,5)
  503. else
  504. inc(result);
  505. if (result >= NODE_COMPLEXITY_INF) then
  506. result := NODE_COMPLEXITY_INF;
  507. exit;
  508. end;
  509. subscriptn:
  510. begin
  511. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  512. is_bitpacked_access(p) then
  513. inc(result,2)
  514. else if tstoreddef(p.resultdef).is_intregable then
  515. inc(result,1);
  516. if (result = NODE_COMPLEXITY_INF) then
  517. exit;
  518. p := tunarynode(p).left;
  519. end;
  520. blockn,
  521. callparan:
  522. p := tunarynode(p).left;
  523. notn,
  524. derefn :
  525. begin
  526. inc(result);
  527. if (result = NODE_COMPLEXITY_INF) then
  528. exit;
  529. p := tunarynode(p).left;
  530. end;
  531. typeconvn:
  532. begin
  533. { may be more complex in some cases }
  534. if not(ttypeconvnode(p).convtype in [tc_equal,tc_int_2_int,tc_bool_2_bool,tc_real_2_real,tc_cord_2_pointer]) then
  535. inc(result);
  536. if (result = NODE_COMPLEXITY_INF) then
  537. exit;
  538. p := tunarynode(p).left;
  539. end;
  540. vecn,
  541. statementn:
  542. begin
  543. inc(result,node_complexity(tbinarynode(p).left));
  544. if (result >= NODE_COMPLEXITY_INF) then
  545. begin
  546. result := NODE_COMPLEXITY_INF;
  547. exit;
  548. end;
  549. p := tbinarynode(p).right;
  550. end;
  551. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  552. shln,shrn,
  553. equaln,unequaln,gtn,gten,ltn,lten,
  554. assignn:
  555. begin
  556. {$ifdef CPU64BITALU}
  557. correction:=1;
  558. {$else CPU64BITALU}
  559. correction:=2;
  560. {$endif CPU64BITALU}
  561. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  562. if (p.nodetype in [muln,divn,modn]) then
  563. inc(result,5*correction*correction);
  564. if (result >= NODE_COMPLEXITY_INF) then
  565. begin
  566. result := NODE_COMPLEXITY_INF;
  567. exit;
  568. end;
  569. p := tbinarynode(p).right;
  570. end;
  571. ordconstn:
  572. begin
  573. {$ifdef ARM}
  574. if not(is_shifter_const(tordconstnode(p).value.svalue,dummy)) then
  575. result:=2;
  576. {$endif ARM}
  577. exit;
  578. end;
  579. tempcreaten,
  580. tempdeleten,
  581. pointerconstn,
  582. nothingn,
  583. niln:
  584. exit;
  585. inlinen:
  586. begin
  587. { this code assumes that the inline node has }
  588. { already been firstpassed, and consequently }
  589. { that inline nodes which are transformed into }
  590. { calls already have been transformed }
  591. case tinlinenode(p).inlinenumber of
  592. in_lo_qword,
  593. in_hi_qword,
  594. in_lo_long,
  595. in_hi_long,
  596. in_lo_word,
  597. in_hi_word,
  598. in_length_x,
  599. in_assigned_x,
  600. in_pred_x,
  601. in_succ_x,
  602. in_round_real,
  603. in_trunc_real,
  604. in_int_real,
  605. in_frac_real,
  606. in_cos_real,
  607. in_sin_real,
  608. in_arctan_real,
  609. in_pi_real,
  610. in_abs_real,
  611. in_sqr_real,
  612. in_sqrt_real,
  613. in_ln_real,
  614. in_unaligned_x,
  615. in_prefetch_var:
  616. begin
  617. inc(result);
  618. p:=tunarynode(p).left;
  619. end;
  620. in_abs_long:
  621. begin
  622. inc(result,3);
  623. if (result >= NODE_COMPLEXITY_INF) then
  624. begin
  625. result:=NODE_COMPLEXITY_INF;
  626. exit;
  627. end;
  628. p:=tunarynode(p).left;
  629. end;
  630. in_sizeof_x,
  631. in_typeof_x:
  632. begin
  633. inc(result);
  634. if (tinlinenode(p).left.nodetype<>typen) then
  635. { get instance vmt }
  636. p:=tunarynode(p).left
  637. else
  638. { type vmt = global symbol, result is }
  639. { already increased above }
  640. exit;
  641. end;
  642. {$ifdef SUPPORT_MMX}
  643. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  644. {$endif SUPPORT_MMX}
  645. { load from global symbol }
  646. in_typeinfo_x,
  647. { load frame pointer }
  648. in_get_frame,
  649. in_get_caller_frame,
  650. in_get_caller_addr:
  651. begin
  652. inc(result);
  653. exit;
  654. end;
  655. in_inc_x,
  656. in_dec_x,
  657. in_include_x_y,
  658. in_exclude_x_y,
  659. in_assert_x_y :
  660. begin
  661. { operation (add, sub, or, and }
  662. inc(result);
  663. { left expression }
  664. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  665. if (result >= NODE_COMPLEXITY_INF) then
  666. begin
  667. result := NODE_COMPLEXITY_INF;
  668. exit;
  669. end;
  670. p:=tcallparanode(tunarynode(p).left).right;
  671. if assigned(p) then
  672. p:=tcallparanode(p).left;
  673. end;
  674. else
  675. begin
  676. result := NODE_COMPLEXITY_INF;
  677. exit;
  678. end;
  679. end;
  680. end;
  681. else
  682. begin
  683. result := NODE_COMPLEXITY_INF;
  684. exit;
  685. end;
  686. end;
  687. end;
  688. end;
  689. { this function returns an indication how much fpu registers
  690. will be required.
  691. Note: The algorithms need to be pessimistic to prevent a
  692. fpu stack overflow on i386 }
  693. function node_resources_fpu(p: tnode): cardinal;
  694. var
  695. res1,res2,res3 : cardinal;
  696. begin
  697. result:=0;
  698. res1:=0;
  699. res2:=0;
  700. res3:=0;
  701. if p.inheritsfrom(tunarynode) then
  702. begin
  703. if assigned(tunarynode(p).left) then
  704. res1:=node_resources_fpu(tunarynode(p).left);
  705. if p.inheritsfrom(tbinarynode) then
  706. begin
  707. if assigned(tbinarynode(p).right) then
  708. res2:=node_resources_fpu(tbinarynode(p).right);
  709. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  710. res3:=node_resources_fpu(ttertiarynode(p).third)
  711. end;
  712. end;
  713. result:=max(max(res1,res2),res3);
  714. case p.nodetype of
  715. calln:
  716. { it could be a recursive call, so we never really know the number of used fpu registers }
  717. result:=maxfpuregs;
  718. realconstn,
  719. typeconvn,
  720. loadn :
  721. begin
  722. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  723. result:=max(result,1);
  724. end;
  725. assignn,
  726. addn,subn,muln,slashn,
  727. equaln,unequaln,gtn,gten,ltn,lten :
  728. begin
  729. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  730. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  731. result:=max(result,2);
  732. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  733. inc(result);
  734. end;
  735. end;
  736. end;
  737. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  738. begin
  739. result:=fen_true;
  740. n.fileinfo:=pfileposinfo(arg)^;
  741. end;
  742. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  743. begin
  744. foreachnodestatic(n,@setnodefilepos,@filepos);
  745. end;
  746. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  747. var
  748. hn : tnode;
  749. treechanged : ^boolean;
  750. begin
  751. result:=fen_false;
  752. if n.inheritsfrom(tloopnode) and
  753. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  754. begin
  755. // Try to simplify condition
  756. doinlinesimplify(tloopnode(n).left);
  757. // call directly second part below,
  758. // which might change the loopnode into
  759. // something else if the conditino is a constant node
  760. include(tloopnode(n).loopflags,lnf_simplify_processing);
  761. callsimplify(n,arg);
  762. // Be careful, n might have change node type
  763. if n.inheritsfrom(tloopnode) then
  764. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  765. end
  766. else
  767. begin
  768. hn:=n.simplify(true);
  769. if assigned(hn) then
  770. begin
  771. treechanged := arg;
  772. if assigned(treechanged) then
  773. treechanged^:=true
  774. else
  775. internalerror (201008181);
  776. n.free;
  777. n:=hn;
  778. typecheckpass(n);
  779. end;
  780. end;
  781. end;
  782. { tries to simplify the given node calling the simplify method recursively }
  783. procedure doinlinesimplify(var n : tnode);
  784. var
  785. treechanged : boolean;
  786. begin
  787. // Optimize if code first
  788. repeat
  789. treechanged:=false;
  790. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  791. until not(treechanged);
  792. end;
  793. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  794. begin
  795. if not forinline then
  796. result:=genintconstnode(value)
  797. else
  798. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  799. end;
  800. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  801. var
  802. hpropsym : tpropertysym;
  803. begin
  804. result:=false;
  805. { find property in the overridden list }
  806. hpropsym:=propsym;
  807. repeat
  808. propaccesslist:=hpropsym.propaccesslist[pap];
  809. if not propaccesslist.empty then
  810. begin
  811. result:=true;
  812. exit;
  813. end;
  814. hpropsym:=hpropsym.overriddenpropsym;
  815. until not assigned(hpropsym);
  816. end;
  817. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  818. var
  819. plist : ppropaccesslistitem;
  820. begin
  821. plist:=pl.firstsym;
  822. while assigned(plist) do
  823. begin
  824. case plist^.sltype of
  825. sl_load :
  826. begin
  827. addsymref(plist^.sym);
  828. if not assigned(st) then
  829. st:=plist^.sym.owner;
  830. if (plist^.sym.typ<>staticvarsym) then
  831. begin
  832. { p1 can already contain the loadnode of
  833. the class variable. When there is no tree yet we
  834. may need to load it for with or objects }
  835. if not assigned(p1) then
  836. begin
  837. case st.symtabletype of
  838. withsymtable :
  839. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  840. ObjectSymtable :
  841. p1:=load_self_node;
  842. end;
  843. end
  844. end
  845. else
  846. begin
  847. p1.free;
  848. p1:=nil;
  849. end;
  850. if assigned(p1) then
  851. p1:=csubscriptnode.create(plist^.sym,p1)
  852. else
  853. p1:=cloadnode.create(plist^.sym,st);
  854. end;
  855. sl_subscript :
  856. begin
  857. addsymref(plist^.sym);
  858. p1:=csubscriptnode.create(plist^.sym,p1);
  859. end;
  860. sl_typeconv :
  861. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  862. sl_absolutetype :
  863. begin
  864. p1:=ctypeconvnode.create(p1,plist^.def);
  865. include(p1.flags,nf_absolute);
  866. end;
  867. sl_vec :
  868. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  869. else
  870. internalerror(200110205);
  871. end;
  872. plist:=plist^.next;
  873. end;
  874. end;
  875. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  876. var
  877. sl : tpropaccesslist;
  878. procedure addnode(p:tnode);
  879. begin
  880. case p.nodetype of
  881. subscriptn :
  882. begin
  883. addnode(tsubscriptnode(p).left);
  884. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  885. end;
  886. typeconvn :
  887. begin
  888. addnode(ttypeconvnode(p).left);
  889. if nf_absolute in ttypeconvnode(p).flags then
  890. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  891. else
  892. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  893. end;
  894. vecn :
  895. begin
  896. addnode(tvecnode(p).left);
  897. if tvecnode(p).right.nodetype=ordconstn then
  898. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  899. else
  900. begin
  901. Message(parser_e_illegal_expression);
  902. { recovery }
  903. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  904. end;
  905. end;
  906. loadn :
  907. sl.addsym(sl_load,tloadnode(p).symtableentry);
  908. else
  909. internalerror(200310282);
  910. end;
  911. end;
  912. begin
  913. sl:=tpropaccesslist.create;
  914. addnode(p1);
  915. result:=sl;
  916. end;
  917. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  918. var
  919. static_name: shortstring;
  920. srsymtable: tsymtable;
  921. begin
  922. result:=false;
  923. { generate access code }
  924. if (sp_static in sym.symoptions) then
  925. begin
  926. result:=true;
  927. if not nested then
  928. static_name:=lower(sym.owner.name^)+'_'+sym.name
  929. else
  930. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  931. if sym.owner.defowner.typ=objectdef then
  932. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,true)
  933. else
  934. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  935. if assigned(sym) then
  936. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  937. p1.free;
  938. p1:=nil;
  939. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  940. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  941. end;
  942. end;
  943. function is_bitpacked_access(n: tnode): boolean;
  944. begin
  945. case n.nodetype of
  946. vecn:
  947. result:=
  948. is_packed_array(tvecnode(n).left.resultdef) and
  949. { only orddefs and enumdefs are actually bitpacked. Don't consider
  950. e.g. an access to a 3-byte record as "bitpacked", since it
  951. isn't }
  952. (tvecnode(n).left.resultdef.typ in [orddef,enumdef]) and
  953. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  954. subscriptn:
  955. result:=
  956. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  957. { see above }
  958. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  959. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  960. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  961. else
  962. result:=false;
  963. end;
  964. end;
  965. function genloadfield(n: tnode; const fieldname: string): tnode;
  966. var
  967. vs : tsym;
  968. begin
  969. if not assigned(n.resultdef) then
  970. typecheckpass(n);
  971. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  972. if not assigned(vs) or
  973. (vs.typ<>fieldvarsym) then
  974. internalerror(2010061902);
  975. result:=csubscriptnode.create(vs,n);
  976. end;
  977. function has_no_code(n : tnode) : boolean;
  978. begin
  979. if n=nil then
  980. begin
  981. result:=true;
  982. exit;
  983. end;
  984. result:=false;
  985. case n.nodetype of
  986. nothingn:
  987. begin
  988. result:=true;
  989. exit;
  990. end;
  991. blockn:
  992. begin
  993. result:=has_no_code(tblocknode(n).left);
  994. exit;
  995. end;
  996. statementn:
  997. begin
  998. repeat
  999. result:=has_no_code(tstatementnode(n).left);
  1000. n:=tstatementnode(n).right;
  1001. until not(result) or not assigned(n);
  1002. exit;
  1003. end;
  1004. end;
  1005. end;
  1006. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1007. begin
  1008. result:=fen_false;
  1009. if (n.nodetype in [assignn,calln,asmn]) or
  1010. ((n.nodetype=inlinen) and
  1011. (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  1012. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,in_settextbuf_file_x,
  1013. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  1014. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle])
  1015. ) then
  1016. result:=fen_norecurse_true;
  1017. end;
  1018. function might_have_sideeffects(n : tnode) : boolean;
  1019. begin
  1020. result:=foreachnodestatic(n,@check_for_sideeffect,nil);
  1021. end;
  1022. var
  1023. nodecount : dword;
  1024. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1025. begin
  1026. inc(nodecount);
  1027. result:=fen_false;
  1028. end;
  1029. { rough estimation how large the tree "node" is }
  1030. function node_count(node : tnode) : dword;
  1031. begin
  1032. nodecount:=0;
  1033. foreachnodestatic(node,@donodecount,nil);
  1034. result:=nodecount;
  1035. end;
  1036. end.