nutils.pas 36 KB

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