nutils.pas 38 KB

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