nutils.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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,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,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,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,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. { we can't use searchsym here, because the
  303. symtablestack is not fully setup when pass1
  304. is run for nested procedures }
  305. pd:=current_procinfo.procdef;
  306. repeat
  307. result := tsym(pd.localst.Find(aname));
  308. if assigned(result) then
  309. break;
  310. result := tsym(pd.parast.Find(aname));
  311. if assigned(result) then
  312. break;
  313. { try the parent of a nested function }
  314. if assigned(pd.owner.defowner) and
  315. (pd.owner.defowner.typ=procdef) then
  316. pd:=tprocdef(pd.owner.defowner)
  317. else
  318. break;
  319. until false;
  320. end;
  321. function load_high_value_node(vs:tparavarsym):tnode;
  322. var
  323. srsym : tsym;
  324. begin
  325. result:=nil;
  326. srsym:=get_high_value_sym(vs);
  327. if assigned(srsym) then
  328. begin
  329. result:=cloadnode.create(srsym,vs.owner);
  330. typecheckpass(result);
  331. end
  332. else
  333. CGMessage(parser_e_illegal_expression);
  334. end;
  335. function load_self_node:tnode;
  336. var
  337. srsym : tsym;
  338. begin
  339. result:=nil;
  340. srsym:=get_local_or_para_sym('self');
  341. if assigned(srsym) then
  342. begin
  343. result:=cloadnode.create(srsym,srsym.owner);
  344. include(result.flags,nf_is_self);
  345. end
  346. else
  347. begin
  348. result:=cerrornode.create;
  349. CGMessage(parser_e_illegal_expression);
  350. end;
  351. typecheckpass(result);
  352. end;
  353. function load_result_node:tnode;
  354. var
  355. srsym : tsym;
  356. begin
  357. result:=nil;
  358. srsym:=get_local_or_para_sym('result');
  359. if assigned(srsym) then
  360. result:=cloadnode.create(srsym,srsym.owner)
  361. else
  362. begin
  363. result:=cerrornode.create;
  364. CGMessage(parser_e_illegal_expression);
  365. end;
  366. typecheckpass(result);
  367. end;
  368. function load_self_pointer_node:tnode;
  369. var
  370. srsym : tsym;
  371. begin
  372. result:=nil;
  373. srsym:=get_local_or_para_sym('self');
  374. if assigned(srsym) then
  375. begin
  376. result:=cloadnode.create(srsym,srsym.owner);
  377. include(result.flags,nf_load_self_pointer);
  378. end
  379. else
  380. begin
  381. result:=cerrornode.create;
  382. CGMessage(parser_e_illegal_expression);
  383. end;
  384. typecheckpass(result);
  385. end;
  386. function load_vmt_pointer_node:tnode;
  387. var
  388. srsym : tsym;
  389. begin
  390. result:=nil;
  391. srsym:=get_local_or_para_sym('vmt');
  392. if assigned(srsym) then
  393. result:=cloadnode.create(srsym,srsym.owner)
  394. else
  395. begin
  396. result:=cerrornode.create;
  397. CGMessage(parser_e_illegal_expression);
  398. end;
  399. typecheckpass(result);
  400. end;
  401. function is_self_node(p:tnode):boolean;
  402. begin
  403. is_self_node:=(p.nodetype=loadn) and
  404. (tloadnode(p).symtableentry.typ=paravarsym) and
  405. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  406. end;
  407. function call_fail_node:tnode;
  408. var
  409. para : tcallparanode;
  410. newstatement : tstatementnode;
  411. srsym : tsym;
  412. begin
  413. result:=internalstatements(newstatement);
  414. { call fail helper and exit normal }
  415. if is_class(current_procinfo.procdef._class) then
  416. begin
  417. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  418. if assigned(srsym) and
  419. (srsym.typ=procsym) then
  420. begin
  421. { if self<>0 and vmt<>0 then freeinstance }
  422. addstatement(newstatement,cifnode.create(
  423. caddnode.create(andn,
  424. caddnode.create(unequaln,
  425. load_self_pointer_node,
  426. cnilnode.create),
  427. caddnode.create(unequaln,
  428. load_vmt_pointer_node,
  429. cnilnode.create)),
  430. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  431. nil));
  432. end
  433. else
  434. internalerror(200305108);
  435. end
  436. else
  437. if is_object(current_procinfo.procdef._class) then
  438. begin
  439. { parameter 3 : vmt_offset }
  440. { parameter 2 : pointer to vmt }
  441. { parameter 1 : self pointer }
  442. para:=ccallparanode.create(
  443. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  444. ccallparanode.create(
  445. ctypeconvnode.create_internal(
  446. load_vmt_pointer_node,
  447. voidpointertype),
  448. ccallparanode.create(
  449. ctypeconvnode.create_internal(
  450. load_self_pointer_node,
  451. voidpointertype),
  452. nil)));
  453. addstatement(newstatement,
  454. ccallnode.createintern('fpc_help_fail',para));
  455. end
  456. else
  457. internalerror(200305132);
  458. { self:=nil }
  459. addstatement(newstatement,cassignmentnode.create(
  460. load_self_pointer_node,
  461. cnilnode.create));
  462. { exit }
  463. addstatement(newstatement,cexitnode.create(nil));
  464. end;
  465. function initialize_data_node(p:tnode):tnode;
  466. begin
  467. if not assigned(p.resultdef) then
  468. typecheckpass(p);
  469. if is_ansistring(p.resultdef) or
  470. is_widestring(p.resultdef) or
  471. is_interfacecom(p.resultdef) or
  472. is_dynamic_array(p.resultdef) then
  473. begin
  474. result:=cassignmentnode.create(
  475. ctypeconvnode.create_internal(p,voidpointertype),
  476. cnilnode.create
  477. );
  478. end
  479. else
  480. begin
  481. result:=ccallnode.createintern('fpc_initialize',
  482. ccallparanode.create(
  483. caddrnode.create_internal(
  484. crttinode.create(
  485. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  486. ccallparanode.create(
  487. caddrnode.create_internal(p),
  488. nil)));
  489. end;
  490. end;
  491. function finalize_data_node(p:tnode):tnode;
  492. var
  493. newstatement : tstatementnode;
  494. begin
  495. if not assigned(p.resultdef) then
  496. typecheckpass(p);
  497. if is_ansistring(p.resultdef) then
  498. begin
  499. result:=internalstatements(newstatement);
  500. addstatement(newstatement,ccallnode.createintern('fpc_ansistr_decr_ref',
  501. ccallparanode.create(
  502. ctypeconvnode.create_internal(p,voidpointertype),
  503. nil)));
  504. addstatement(newstatement,cassignmentnode.create(
  505. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  506. cnilnode.create
  507. ));
  508. end
  509. else if is_widestring(p.resultdef) then
  510. begin
  511. result:=internalstatements(newstatement);
  512. addstatement(newstatement,ccallnode.createintern('fpc_widestr_decr_ref',
  513. ccallparanode.create(
  514. ctypeconvnode.create_internal(p,voidpointertype),
  515. nil)));
  516. addstatement(newstatement,cassignmentnode.create(
  517. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  518. cnilnode.create
  519. ));
  520. end
  521. else if is_interfacecom(p.resultdef) then
  522. begin
  523. result:=internalstatements(newstatement);
  524. addstatement(newstatement,ccallnode.createintern('fpc_intf_decr_ref',
  525. ccallparanode.create(
  526. ctypeconvnode.create_internal(p,voidpointertype),
  527. nil)));
  528. addstatement(newstatement,cassignmentnode.create(
  529. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  530. cnilnode.create
  531. ));
  532. end
  533. else
  534. result:=ccallnode.createintern('fpc_finalize',
  535. ccallparanode.create(
  536. caddrnode.create_internal(
  537. crttinode.create(
  538. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  539. ccallparanode.create(
  540. caddrnode.create_internal(p),
  541. nil)));
  542. end;
  543. { this function must return a very high value ("infinity") for }
  544. { trees containing a call, the rest can be balanced more or less }
  545. { at will, probably best mainly in terms of required memory }
  546. { accesses }
  547. function node_complexity(p: tnode): cardinal;
  548. begin
  549. result := 0;
  550. while assigned(p) do
  551. begin
  552. case p.nodetype of
  553. temprefn,
  554. loadvmtaddrn,
  555. { main reason for the next one: we can't take the address of }
  556. { loadparentfpnode, so replacing it by a temp which is the }
  557. { address of this node's location and then dereferencing }
  558. { doesn't work. If changed, check whether webtbs/tw0935 }
  559. { still works with nodeinlining (JM) }
  560. loadparentfpn:
  561. begin
  562. result := 1;
  563. exit;
  564. end;
  565. loadn:
  566. begin
  567. { threadvars need a helper call }
  568. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  569. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  570. inc(result,5)
  571. else
  572. inc(result);
  573. if (result >= NODE_COMPLEXITY_INF) then
  574. result := NODE_COMPLEXITY_INF;
  575. exit;
  576. end;
  577. subscriptn:
  578. begin
  579. if is_class_or_interface(tunarynode(p).left.resultdef) then
  580. inc(result);
  581. if (result = NODE_COMPLEXITY_INF) then
  582. exit;
  583. p := tunarynode(p).left;
  584. end;
  585. blockn,
  586. callparan:
  587. p := tunarynode(p).left;
  588. notn,
  589. derefn :
  590. begin
  591. inc(result);
  592. if (result = NODE_COMPLEXITY_INF) then
  593. exit;
  594. p := tunarynode(p).left;
  595. end;
  596. typeconvn:
  597. begin
  598. { may be more complex in some cases }
  599. 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
  600. inc(result);
  601. if (result = NODE_COMPLEXITY_INF) then
  602. exit;
  603. p := tunarynode(p).left;
  604. end;
  605. vecn,
  606. statementn:
  607. begin
  608. inc(result,node_complexity(tbinarynode(p).left));
  609. if (result >= NODE_COMPLEXITY_INF) then
  610. begin
  611. result := NODE_COMPLEXITY_INF;
  612. exit;
  613. end;
  614. p := tbinarynode(p).right;
  615. end;
  616. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  617. shln,shrn,
  618. equaln,unequaln,gtn,gten,ltn,lten,
  619. assignn:
  620. begin
  621. inc(result,node_complexity(tbinarynode(p).left)+1);
  622. if (p.nodetype in [muln,divn,modn]) then
  623. inc(result,5);
  624. if (result >= NODE_COMPLEXITY_INF) then
  625. begin
  626. result := NODE_COMPLEXITY_INF;
  627. exit;
  628. end;
  629. p := tbinarynode(p).right;
  630. end;
  631. tempcreaten,
  632. tempdeleten,
  633. ordconstn,
  634. pointerconstn,
  635. nothingn,
  636. niln:
  637. exit;
  638. inlinen:
  639. begin
  640. { this code assumes that the inline node has }
  641. { already been firstpassed, and consequently }
  642. { that inline nodes which are transformed into }
  643. { calls already have been transformed }
  644. case tinlinenode(p).inlinenumber of
  645. in_lo_qword,
  646. in_hi_qword,
  647. in_lo_long,
  648. in_hi_long,
  649. in_lo_word,
  650. in_hi_word,
  651. in_length_x,
  652. in_assigned_x,
  653. in_pred_x,
  654. in_succ_x,
  655. in_round_real,
  656. in_trunc_real,
  657. in_int_real,
  658. in_frac_real,
  659. in_cos_real,
  660. in_sin_real,
  661. in_arctan_real,
  662. in_pi_real,
  663. in_abs_real,
  664. in_sqr_real,
  665. in_sqrt_real,
  666. in_ln_real,
  667. {$ifdef SUPPORT_UNALIGNED}
  668. in_unaligned_x,
  669. {$endif SUPPORT_UNALIGNED}
  670. in_prefetch_var:
  671. begin
  672. inc(result);
  673. p:=tunarynode(p).left;
  674. end;
  675. in_abs_long:
  676. begin
  677. inc(result,10);
  678. if (result >= NODE_COMPLEXITY_INF) then
  679. begin
  680. result:=NODE_COMPLEXITY_INF;
  681. exit;
  682. end;
  683. p:=tunarynode(p).left;
  684. end;
  685. in_sizeof_x,
  686. in_typeof_x:
  687. begin
  688. inc(result);
  689. if (tinlinenode(p).left.nodetype<>typen) then
  690. { get instance vmt }
  691. p:=tunarynode(p).left
  692. else
  693. { type vmt = global symbol, result is }
  694. { already increased above }
  695. exit;
  696. end;
  697. {$ifdef SUPPORT_MMX}
  698. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  699. {$endif SUPPORT_MMX}
  700. { load from global symbol }
  701. in_typeinfo_x,
  702. { load frame pointer }
  703. in_get_frame,
  704. in_get_caller_frame,
  705. in_get_caller_addr:
  706. begin
  707. inc(result);
  708. exit;
  709. end;
  710. in_inc_x,
  711. in_dec_x,
  712. in_include_x_y,
  713. in_exclude_x_y,
  714. in_assert_x_y :
  715. begin
  716. { operation (add, sub, or, and }
  717. inc(result);
  718. { left expression }
  719. inc(result,node_complexity(tbinarynode(p).left));
  720. if (result >= NODE_COMPLEXITY_INF) then
  721. begin
  722. result := NODE_COMPLEXITY_INF;
  723. exit;
  724. end;
  725. p := tbinarynode(p).right;
  726. end;
  727. else
  728. begin
  729. result := NODE_COMPLEXITY_INF;
  730. exit;
  731. end;
  732. end;
  733. end;
  734. else
  735. begin
  736. result := NODE_COMPLEXITY_INF;
  737. exit;
  738. end;
  739. end;
  740. end;
  741. end;
  742. { this function returns an indication how much fpu registers
  743. will be required.
  744. Note: The algorithms need to be pessimistic to prevent a
  745. fpu stack overflow on i386 }
  746. function node_resources_fpu(p: tnode): cardinal;
  747. var
  748. res1,res2,res3 : cardinal;
  749. begin
  750. result:=0;
  751. res1:=0;
  752. res2:=0;
  753. res3:=0;
  754. if p.inheritsfrom(tunarynode) then
  755. begin
  756. if assigned(tunarynode(p).left) then
  757. res1:=node_resources_fpu(tunarynode(p).left);
  758. if p.inheritsfrom(tbinarynode) then
  759. begin
  760. if assigned(tbinarynode(p).right) then
  761. res2:=node_resources_fpu(tbinarynode(p).right);
  762. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  763. res3:=node_resources_fpu(ttertiarynode(p).third)
  764. end;
  765. end;
  766. result:=max(max(res1,res2),res3);
  767. case p.nodetype of
  768. calln:
  769. { it could be a recursive call, so we never really know the number of used fpu registers }
  770. result:=maxfpuregs;
  771. realconstn,
  772. typeconvn,
  773. loadn :
  774. begin
  775. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  776. result:=max(result,1);
  777. end;
  778. assignn,
  779. addn,subn,muln,slashn,
  780. equaln,unequaln,gtn,gten,ltn,lten :
  781. begin
  782. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  783. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  784. result:=max(result,2);
  785. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  786. inc(result);
  787. end;
  788. end;
  789. end;
  790. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  791. begin
  792. result:=fen_true;
  793. n.fileinfo:=pfileposinfo(arg)^;
  794. end;
  795. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  796. begin
  797. foreachnodestatic(n,@setnodefilepos,@filepos);
  798. end;
  799. {$ifdef FPCMT}
  800. threadvar
  801. {$else FPCMT}
  802. var
  803. {$endif FPCMT}
  804. treechanged : boolean;
  805. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  806. var
  807. hn : tnode;
  808. begin
  809. result:=fen_false;
  810. // do_typecheckpass(n);
  811. hn:=n.simplify;
  812. if assigned(hn) then
  813. begin
  814. treechanged:=true;
  815. n.free;
  816. n:=hn;
  817. typecheckpass(n);
  818. end;
  819. end;
  820. { tries to simplify the given node calling the simplify method recursively }
  821. procedure dosimplify(var n : tnode);
  822. begin
  823. repeat
  824. treechanged:=false;
  825. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  826. until not(treechanged);
  827. end;
  828. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  829. var
  830. plist : ppropaccesslistitem;
  831. begin
  832. plist:=pl.firstsym;
  833. while assigned(plist) do
  834. begin
  835. case plist^.sltype of
  836. sl_load :
  837. begin
  838. addsymref(plist^.sym);
  839. if not assigned(st) then
  840. st:=plist^.sym.owner;
  841. { p1 can already contain the loadnode of
  842. the class variable. When there is no tree yet we
  843. may need to load it for with or objects }
  844. if not assigned(p1) then
  845. begin
  846. case st.symtabletype of
  847. withsymtable :
  848. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  849. ObjectSymtable :
  850. p1:=load_self_node;
  851. end;
  852. end;
  853. if assigned(p1) then
  854. p1:=csubscriptnode.create(plist^.sym,p1)
  855. else
  856. p1:=cloadnode.create(plist^.sym,st);
  857. end;
  858. sl_subscript :
  859. begin
  860. addsymref(plist^.sym);
  861. p1:=csubscriptnode.create(plist^.sym,p1);
  862. end;
  863. sl_typeconv :
  864. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  865. sl_absolutetype :
  866. begin
  867. p1:=ctypeconvnode.create(p1,plist^.def);
  868. include(p1.flags,nf_absolute);
  869. end;
  870. sl_vec :
  871. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  872. else
  873. internalerror(200110205);
  874. end;
  875. plist:=plist^.next;
  876. end;
  877. end;
  878. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  879. var
  880. sl : tpropaccesslist;
  881. procedure addnode(p:tnode);
  882. begin
  883. case p.nodetype of
  884. subscriptn :
  885. begin
  886. addnode(tsubscriptnode(p).left);
  887. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  888. end;
  889. typeconvn :
  890. begin
  891. addnode(ttypeconvnode(p).left);
  892. if nf_absolute in ttypeconvnode(p).flags then
  893. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  894. else
  895. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  896. end;
  897. vecn :
  898. begin
  899. addnode(tvecnode(p).left);
  900. if tvecnode(p).right.nodetype=ordconstn then
  901. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  902. else
  903. begin
  904. Message(parser_e_illegal_expression);
  905. { recovery }
  906. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  907. end;
  908. end;
  909. loadn :
  910. sl.addsym(sl_load,tloadnode(p).symtableentry);
  911. else
  912. internalerror(200310282);
  913. end;
  914. end;
  915. begin
  916. sl:=tpropaccesslist.create;
  917. addnode(p1);
  918. result:=sl;
  919. end;
  920. function has_no_code(n : tnode) : boolean;
  921. begin
  922. if n=nil then
  923. begin
  924. result:=true;
  925. exit;
  926. end;
  927. result:=false;
  928. case n.nodetype of
  929. nothingn:
  930. begin
  931. result:=true;
  932. exit;
  933. end;
  934. blockn:
  935. begin
  936. result:=has_no_code(tblocknode(n).left);
  937. exit;
  938. end;
  939. end;
  940. end;
  941. end.