nutils.pas 40 KB

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