nutils.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. not(m_tp_procvar in 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. if assigned(tloadnode(p).left) then
  637. inc(result,node_complexity(tloadnode(p).left));
  638. { threadvars need a helper call }
  639. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  640. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  641. inc(result,5)
  642. else
  643. inc(result);
  644. if (result >= NODE_COMPLEXITY_INF) then
  645. result := NODE_COMPLEXITY_INF;
  646. exit;
  647. end;
  648. subscriptn:
  649. begin
  650. if is_class_or_interface_or_dispinterface_or_objc(tunarynode(p).left.resultdef) then
  651. inc(result,2);
  652. if (result = NODE_COMPLEXITY_INF) then
  653. exit;
  654. p := tunarynode(p).left;
  655. end;
  656. blockn,
  657. callparan:
  658. p := tunarynode(p).left;
  659. notn,
  660. derefn :
  661. begin
  662. inc(result);
  663. if (result = NODE_COMPLEXITY_INF) then
  664. exit;
  665. p := tunarynode(p).left;
  666. end;
  667. typeconvn:
  668. begin
  669. { may be more complex in some cases }
  670. 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
  671. inc(result);
  672. if (result = NODE_COMPLEXITY_INF) then
  673. exit;
  674. p := tunarynode(p).left;
  675. end;
  676. vecn,
  677. statementn:
  678. begin
  679. inc(result,node_complexity(tbinarynode(p).left));
  680. if (result >= NODE_COMPLEXITY_INF) then
  681. begin
  682. result := NODE_COMPLEXITY_INF;
  683. exit;
  684. end;
  685. p := tbinarynode(p).right;
  686. end;
  687. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  688. shln,shrn,
  689. equaln,unequaln,gtn,gten,ltn,lten,
  690. assignn:
  691. begin
  692. {$ifdef CPU64BITALU}
  693. correction:=1;
  694. {$else CPU64BITALU}
  695. correction:=2;
  696. {$endif CPU64BITALU}
  697. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  698. if (p.nodetype in [muln,divn,modn]) then
  699. inc(result,5*correction*correction);
  700. if (result >= NODE_COMPLEXITY_INF) then
  701. begin
  702. result := NODE_COMPLEXITY_INF;
  703. exit;
  704. end;
  705. p := tbinarynode(p).right;
  706. end;
  707. ordconstn:
  708. begin
  709. {$ifdef ARM}
  710. if not(is_shifter_const(tordconstnode(p).value.svalue,dummy)) then
  711. result:=2;
  712. {$endif ARM}
  713. exit;
  714. end;
  715. stringconstn,
  716. tempcreaten,
  717. tempdeleten,
  718. pointerconstn,
  719. nothingn,
  720. niln:
  721. exit;
  722. inlinen:
  723. begin
  724. { this code assumes that the inline node has }
  725. { already been firstpassed, and consequently }
  726. { that inline nodes which are transformed into }
  727. { calls already have been transformed }
  728. case tinlinenode(p).inlinenumber of
  729. in_lo_qword,
  730. in_hi_qword,
  731. in_lo_long,
  732. in_hi_long,
  733. in_lo_word,
  734. in_hi_word,
  735. in_length_x,
  736. in_assigned_x,
  737. in_pred_x,
  738. in_succ_x,
  739. in_round_real,
  740. in_trunc_real,
  741. in_int_real,
  742. in_frac_real,
  743. in_cos_real,
  744. in_sin_real,
  745. in_arctan_real,
  746. in_pi_real,
  747. in_abs_real,
  748. in_sqr_real,
  749. in_sqrt_real,
  750. in_ln_real,
  751. in_unaligned_x,
  752. in_prefetch_var:
  753. begin
  754. inc(result);
  755. p:=tunarynode(p).left;
  756. end;
  757. in_abs_long:
  758. begin
  759. inc(result,3);
  760. if (result >= NODE_COMPLEXITY_INF) then
  761. begin
  762. result:=NODE_COMPLEXITY_INF;
  763. exit;
  764. end;
  765. p:=tunarynode(p).left;
  766. end;
  767. in_sizeof_x,
  768. in_typeof_x:
  769. begin
  770. inc(result);
  771. if (tinlinenode(p).left.nodetype<>typen) then
  772. { get instance vmt }
  773. p:=tunarynode(p).left
  774. else
  775. { type vmt = global symbol, result is }
  776. { already increased above }
  777. exit;
  778. end;
  779. {$ifdef SUPPORT_MMX}
  780. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  781. {$endif SUPPORT_MMX}
  782. { load from global symbol }
  783. in_typeinfo_x,
  784. { load frame pointer }
  785. in_get_frame,
  786. in_get_caller_frame,
  787. in_get_caller_addr:
  788. begin
  789. inc(result);
  790. exit;
  791. end;
  792. in_inc_x,
  793. in_dec_x,
  794. in_include_x_y,
  795. in_exclude_x_y,
  796. in_assert_x_y :
  797. begin
  798. { operation (add, sub, or, and }
  799. inc(result);
  800. { left expression }
  801. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  802. if (result >= NODE_COMPLEXITY_INF) then
  803. begin
  804. result := NODE_COMPLEXITY_INF;
  805. exit;
  806. end;
  807. p:=tcallparanode(tunarynode(p).left).right;
  808. if assigned(p) then
  809. p:=tcallparanode(p).left;
  810. end;
  811. else
  812. begin
  813. result := NODE_COMPLEXITY_INF;
  814. exit;
  815. end;
  816. end;
  817. end;
  818. else
  819. begin
  820. result := NODE_COMPLEXITY_INF;
  821. exit;
  822. end;
  823. end;
  824. end;
  825. end;
  826. { this function returns an indication how much fpu registers
  827. will be required.
  828. Note: The algorithms need to be pessimistic to prevent a
  829. fpu stack overflow on i386 }
  830. function node_resources_fpu(p: tnode): cardinal;
  831. var
  832. res1,res2,res3 : cardinal;
  833. begin
  834. result:=0;
  835. res1:=0;
  836. res2:=0;
  837. res3:=0;
  838. if p.inheritsfrom(tunarynode) then
  839. begin
  840. if assigned(tunarynode(p).left) then
  841. res1:=node_resources_fpu(tunarynode(p).left);
  842. if p.inheritsfrom(tbinarynode) then
  843. begin
  844. if assigned(tbinarynode(p).right) then
  845. res2:=node_resources_fpu(tbinarynode(p).right);
  846. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  847. res3:=node_resources_fpu(ttertiarynode(p).third)
  848. end;
  849. end;
  850. result:=max(max(res1,res2),res3);
  851. case p.nodetype of
  852. calln:
  853. { it could be a recursive call, so we never really know the number of used fpu registers }
  854. result:=maxfpuregs;
  855. realconstn,
  856. typeconvn,
  857. loadn :
  858. begin
  859. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  860. result:=max(result,1);
  861. end;
  862. assignn,
  863. addn,subn,muln,slashn,
  864. equaln,unequaln,gtn,gten,ltn,lten :
  865. begin
  866. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  867. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  868. result:=max(result,2);
  869. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  870. inc(result);
  871. end;
  872. end;
  873. end;
  874. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  875. begin
  876. result:=fen_true;
  877. n.fileinfo:=pfileposinfo(arg)^;
  878. end;
  879. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  880. begin
  881. foreachnodestatic(n,@setnodefilepos,@filepos);
  882. end;
  883. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  884. var
  885. hn : tnode;
  886. treechanged : ^boolean;
  887. begin
  888. result:=fen_false;
  889. if n.inheritsfrom(tloopnode) and
  890. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  891. begin
  892. // Try to simplify condition
  893. dosimplify(tloopnode(n).left);
  894. // call directly second part below,
  895. // which might change the loopnode into
  896. // something else if the conditino is a constant node
  897. include(tloopnode(n).loopflags,lnf_simplify_processing);
  898. callsimplify(n,arg);
  899. // Be careful, n might have change node type
  900. if n.inheritsfrom(tloopnode) then
  901. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  902. end
  903. else
  904. begin
  905. hn:=n.simplify;
  906. if assigned(hn) then
  907. begin
  908. treechanged := arg;
  909. if assigned(treechanged) then
  910. treechanged^:=true
  911. else
  912. internalerror (201008181);
  913. n.free;
  914. n:=hn;
  915. typecheckpass(n);
  916. end;
  917. end;
  918. end;
  919. { tries to simplify the given node calling the simplify method recursively }
  920. procedure dosimplify(var n : tnode);
  921. var
  922. treechanged : boolean;
  923. begin
  924. // Optimize if code first
  925. repeat
  926. treechanged:=false;
  927. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  928. until not(treechanged);
  929. end;
  930. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  931. var
  932. hpropsym : tpropertysym;
  933. begin
  934. result:=false;
  935. { find property in the overridden list }
  936. hpropsym:=propsym;
  937. repeat
  938. propaccesslist:=hpropsym.propaccesslist[pap];
  939. if not propaccesslist.empty then
  940. begin
  941. result:=true;
  942. exit;
  943. end;
  944. hpropsym:=hpropsym.overriddenpropsym;
  945. until not assigned(hpropsym);
  946. end;
  947. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  948. var
  949. plist : ppropaccesslistitem;
  950. begin
  951. plist:=pl.firstsym;
  952. while assigned(plist) do
  953. begin
  954. case plist^.sltype of
  955. sl_load :
  956. begin
  957. addsymref(plist^.sym);
  958. if not assigned(st) then
  959. st:=plist^.sym.owner;
  960. { p1 can already contain the loadnode of
  961. the class variable. When there is no tree yet we
  962. may need to load it for with or objects }
  963. if not assigned(p1) then
  964. begin
  965. case st.symtabletype of
  966. withsymtable :
  967. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  968. ObjectSymtable :
  969. p1:=load_self_node;
  970. end;
  971. end;
  972. if assigned(p1) then
  973. p1:=csubscriptnode.create(plist^.sym,p1)
  974. else
  975. p1:=cloadnode.create(plist^.sym,st);
  976. end;
  977. sl_subscript :
  978. begin
  979. addsymref(plist^.sym);
  980. p1:=csubscriptnode.create(plist^.sym,p1);
  981. end;
  982. sl_typeconv :
  983. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  984. sl_absolutetype :
  985. begin
  986. p1:=ctypeconvnode.create(p1,plist^.def);
  987. include(p1.flags,nf_absolute);
  988. end;
  989. sl_vec :
  990. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  991. else
  992. internalerror(200110205);
  993. end;
  994. plist:=plist^.next;
  995. end;
  996. end;
  997. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  998. var
  999. sl : tpropaccesslist;
  1000. procedure addnode(p:tnode);
  1001. begin
  1002. case p.nodetype of
  1003. subscriptn :
  1004. begin
  1005. addnode(tsubscriptnode(p).left);
  1006. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  1007. end;
  1008. typeconvn :
  1009. begin
  1010. addnode(ttypeconvnode(p).left);
  1011. if nf_absolute in ttypeconvnode(p).flags then
  1012. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  1013. else
  1014. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  1015. end;
  1016. vecn :
  1017. begin
  1018. addnode(tvecnode(p).left);
  1019. if tvecnode(p).right.nodetype=ordconstn then
  1020. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  1021. else
  1022. begin
  1023. Message(parser_e_illegal_expression);
  1024. { recovery }
  1025. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  1026. end;
  1027. end;
  1028. loadn :
  1029. sl.addsym(sl_load,tloadnode(p).symtableentry);
  1030. else
  1031. internalerror(200310282);
  1032. end;
  1033. end;
  1034. begin
  1035. sl:=tpropaccesslist.create;
  1036. addnode(p1);
  1037. result:=sl;
  1038. end;
  1039. function is_bitpacked_access(n: tnode): boolean;
  1040. begin
  1041. case n.nodetype of
  1042. vecn:
  1043. result:=
  1044. is_packed_array(tvecnode(n).left.resultdef) and
  1045. (tarraydef(tvecnode(n).left.resultdef).elepackedbitsize mod 8 <> 0);
  1046. subscriptn:
  1047. result:=
  1048. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1049. ((tsubscriptnode(n).vs.vardef.packedbitsize mod 8 <> 0) or
  1050. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1051. else
  1052. result:=false;
  1053. end;
  1054. end;
  1055. function genloadfield(n: tnode; const fieldname: string): tnode;
  1056. var
  1057. vs : tsym;
  1058. begin
  1059. if not assigned(n.resultdef) then
  1060. typecheckpass(n);
  1061. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1062. if not assigned(vs) or
  1063. (vs.typ<>fieldvarsym) then
  1064. internalerror(2010061902);
  1065. result:=csubscriptnode.create(vs,n);
  1066. end;
  1067. function has_no_code(n : tnode) : boolean;
  1068. begin
  1069. if n=nil then
  1070. begin
  1071. result:=true;
  1072. exit;
  1073. end;
  1074. result:=false;
  1075. case n.nodetype of
  1076. nothingn:
  1077. begin
  1078. result:=true;
  1079. exit;
  1080. end;
  1081. blockn:
  1082. begin
  1083. result:=has_no_code(tblocknode(n).left);
  1084. exit;
  1085. end;
  1086. statementn:
  1087. begin
  1088. repeat
  1089. result:=has_no_code(tstatementnode(n).left);
  1090. n:=tstatementnode(n).right;
  1091. until not(result) or not assigned(n);
  1092. exit;
  1093. end;
  1094. end;
  1095. end;
  1096. end.