nutils.pas 39 KB

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