nutils.pas 41 KB

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