nutils.pas 40 KB

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