nutils.pas 38 KB

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