nutils.pas 35 KB

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