nutils.pas 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Type checking and register allocation for inline nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nutils;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,symsym,symbase,symtable,
  23. node;
  24. const
  25. NODE_COMPLEXITY_INF = 255;
  26. type
  27. { resultdef of functions that process on all nodes in a (sub)tree }
  28. foreachnoderesult = (
  29. { false, continue recursion }
  30. fen_false,
  31. { false, stop recursion }
  32. fen_norecurse_false,
  33. { true, continue recursion }
  34. fen_true,
  35. { true, stop recursion }
  36. fen_norecurse_true
  37. );
  38. tforeachprocmethod = (pm_preprocess,pm_postprocess);
  39. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  40. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  41. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  42. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  43. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  44. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  45. { checks if the given node tree contains only nodes of the given type,
  46. if this isn't the case, an ie is thrown
  47. }
  48. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  49. procedure load_procvar_from_calln(var p1:tnode);
  50. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  51. function get_high_value_sym(vs: tparavarsym):tsym; { marking it as inline causes IE 200311075 during loading from ppu file }
  52. function load_high_value_node(vs:tparavarsym):tnode;
  53. function load_self_node:tnode;
  54. function load_result_node:tnode;
  55. function load_self_pointer_node:tnode;
  56. function load_vmt_pointer_node:tnode;
  57. function is_self_node(p:tnode):boolean;
  58. function call_fail_node:tnode;
  59. function initialize_data_node(p:tnode):tnode;
  60. function finalize_data_node(p:tnode):tnode;
  61. function node_complexity(p: tnode): cardinal;
  62. function node_resources_fpu(p: tnode): cardinal;
  63. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  64. { tries to simplify the given node }
  65. procedure dosimplify(var n : tnode);
  66. { returns true if n is only a tree of administrative nodes
  67. containing no code }
  68. function has_no_code(n : tnode) : boolean;
  69. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  70. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  71. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  72. { returns true if n is an array element access of a bitpacked array with
  73. elements of the which the vitsize mod 8 <> 0, or if is a field access
  74. with bitsize mod 8 <> 0 or bitoffset mod 8 <> 0 of an element in a
  75. bitpacked structure }
  76. function is_bitpacked_access(n: tnode): boolean;
  77. { creates a load of field 'fieldname' in the record/class/...
  78. represented by n; assumes the resultdef of n is set }
  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. if procmethod=pm_preprocess then
  220. result:=process_children(result);
  221. case f(n,arg) of
  222. fen_norecurse_false:
  223. exit;
  224. fen_norecurse_true:
  225. begin
  226. result := true;
  227. exit;
  228. end;
  229. fen_true:
  230. result := true;
  231. { result is already false
  232. fen_false:
  233. result := false; }
  234. end;
  235. if procmethod=pm_postprocess then
  236. result:=process_children(result);
  237. end;
  238. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  239. begin
  240. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  241. end;
  242. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  243. begin
  244. if not(n.nodetype in pnodetypeset(arg)^) then
  245. internalerror(200610141);
  246. result:=fen_true;
  247. end;
  248. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  249. begin
  250. foreachnodestatic(n,@do_check,@typeset);
  251. end;
  252. procedure load_procvar_from_calln(var p1:tnode);
  253. var
  254. p2 : tnode;
  255. begin
  256. if p1.nodetype<>calln then
  257. internalerror(200212251);
  258. { was it a procvar, then we simply remove the calln and
  259. reuse the right }
  260. if assigned(tcallnode(p1).right) then
  261. begin
  262. p2:=tcallnode(p1).right;
  263. tcallnode(p1).right:=nil;
  264. end
  265. else
  266. begin
  267. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  268. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  269. { when the methodpointer is typen we've something like:
  270. tobject.create. Then only the address is needed of the
  271. method without a self pointer }
  272. if assigned(tcallnode(p1).methodpointer) and
  273. (tcallnode(p1).methodpointer.nodetype<>typen) then
  274. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  275. end;
  276. typecheckpass(p2);
  277. p1.free;
  278. p1:=p2;
  279. end;
  280. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  281. var
  282. hp : tnode;
  283. begin
  284. result:=false;
  285. if (p1.resultdef.typ<>procvardef) or
  286. (tponly and
  287. not(m_tp_procvar in current_settings.modeswitches)) then
  288. exit;
  289. { ignore vecn,subscriptn }
  290. hp:=p1;
  291. repeat
  292. case hp.nodetype of
  293. vecn,
  294. derefn,
  295. typeconvn,
  296. subscriptn :
  297. hp:=tunarynode(hp).left;
  298. else
  299. break;
  300. end;
  301. until false;
  302. { a tempref is used when it is loaded from a withsymtable }
  303. if (hp.nodetype in [calln,loadn,temprefn]) then
  304. begin
  305. hp:=ccallnode.create_procvar(nil,p1);
  306. typecheckpass(hp);
  307. p1:=hp;
  308. result:=true;
  309. end;
  310. end;
  311. function get_high_value_sym(vs: tparavarsym):tsym;
  312. begin
  313. result := tsym(vs.owner.Find('high'+vs.name));
  314. end;
  315. function get_local_or_para_sym(const aname:string):tsym;
  316. var
  317. pd : tprocdef;
  318. begin
  319. result:=nil;
  320. { is not assigned while parsing a property }
  321. if not assigned(current_procinfo) then
  322. exit;
  323. { we can't use searchsym here, because the
  324. symtablestack is not fully setup when pass1
  325. is run for nested procedures }
  326. pd:=current_procinfo.procdef;
  327. repeat
  328. result := tsym(pd.localst.Find(aname));
  329. if assigned(result) then
  330. break;
  331. result := tsym(pd.parast.Find(aname));
  332. if assigned(result) then
  333. break;
  334. { try the parent of a nested function }
  335. if assigned(pd.owner.defowner) and
  336. (pd.owner.defowner.typ=procdef) then
  337. pd:=tprocdef(pd.owner.defowner)
  338. else
  339. break;
  340. until false;
  341. end;
  342. function load_high_value_node(vs:tparavarsym):tnode;
  343. var
  344. srsym : tsym;
  345. begin
  346. result:=nil;
  347. srsym:=get_high_value_sym(vs);
  348. if assigned(srsym) then
  349. begin
  350. result:=cloadnode.create(srsym,vs.owner);
  351. typecheckpass(result);
  352. end
  353. else
  354. CGMessage(parser_e_illegal_expression);
  355. end;
  356. function load_self_node:tnode;
  357. var
  358. srsym : tsym;
  359. begin
  360. result:=nil;
  361. srsym:=get_local_or_para_sym('self');
  362. if assigned(srsym) then
  363. begin
  364. result:=cloadnode.create(srsym,srsym.owner);
  365. include(result.flags,nf_is_self);
  366. end
  367. else
  368. begin
  369. result:=cerrornode.create;
  370. CGMessage(parser_e_illegal_expression);
  371. end;
  372. typecheckpass(result);
  373. end;
  374. function load_result_node:tnode;
  375. var
  376. srsym : tsym;
  377. begin
  378. result:=nil;
  379. srsym:=get_local_or_para_sym('result');
  380. if assigned(srsym) then
  381. result:=cloadnode.create(srsym,srsym.owner)
  382. else
  383. begin
  384. result:=cerrornode.create;
  385. CGMessage(parser_e_illegal_expression);
  386. end;
  387. typecheckpass(result);
  388. end;
  389. function load_self_pointer_node:tnode;
  390. var
  391. srsym : tsym;
  392. begin
  393. result:=nil;
  394. srsym:=get_local_or_para_sym('self');
  395. if assigned(srsym) then
  396. begin
  397. result:=cloadnode.create(srsym,srsym.owner);
  398. include(result.flags,nf_load_self_pointer);
  399. end
  400. else
  401. begin
  402. result:=cerrornode.create;
  403. CGMessage(parser_e_illegal_expression);
  404. end;
  405. typecheckpass(result);
  406. end;
  407. function load_vmt_pointer_node:tnode;
  408. var
  409. srsym : tsym;
  410. begin
  411. result:=nil;
  412. srsym:=get_local_or_para_sym('vmt');
  413. if assigned(srsym) then
  414. result:=cloadnode.create(srsym,srsym.owner)
  415. else
  416. begin
  417. result:=cerrornode.create;
  418. CGMessage(parser_e_illegal_expression);
  419. end;
  420. typecheckpass(result);
  421. end;
  422. function is_self_node(p:tnode):boolean;
  423. begin
  424. is_self_node:=(p.nodetype=loadn) and
  425. (tloadnode(p).symtableentry.typ=paravarsym) and
  426. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  427. end;
  428. function call_fail_node:tnode;
  429. var
  430. para : tcallparanode;
  431. newstatement : tstatementnode;
  432. srsym : tsym;
  433. begin
  434. result:=internalstatements(newstatement);
  435. { call fail helper and exit normal }
  436. if is_class(current_objectdef) then
  437. begin
  438. srsym:=search_class_member(current_objectdef,'FREEINSTANCE');
  439. if assigned(srsym) and
  440. (srsym.typ=procsym) then
  441. begin
  442. { if self<>0 and vmt<>0 then freeinstance }
  443. addstatement(newstatement,cifnode.create(
  444. caddnode.create(andn,
  445. caddnode.create(unequaln,
  446. load_self_pointer_node,
  447. cnilnode.create),
  448. caddnode.create(unequaln,
  449. load_vmt_pointer_node,
  450. cnilnode.create)),
  451. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node,[]),
  452. nil));
  453. end
  454. else
  455. internalerror(200305108);
  456. end
  457. else
  458. if is_object(current_objectdef) then
  459. begin
  460. { parameter 3 : vmt_offset }
  461. { parameter 2 : pointer to vmt }
  462. { parameter 1 : self pointer }
  463. para:=ccallparanode.create(
  464. cordconstnode.create(current_objectdef.vmt_offset,s32inttype,false),
  465. ccallparanode.create(
  466. ctypeconvnode.create_internal(
  467. load_vmt_pointer_node,
  468. voidpointertype),
  469. ccallparanode.create(
  470. ctypeconvnode.create_internal(
  471. load_self_pointer_node,
  472. voidpointertype),
  473. nil)));
  474. addstatement(newstatement,
  475. ccallnode.createintern('fpc_help_fail',para));
  476. end
  477. else
  478. internalerror(200305132);
  479. { self:=nil }
  480. addstatement(newstatement,cassignmentnode.create(
  481. load_self_pointer_node,
  482. cnilnode.create));
  483. { exit }
  484. addstatement(newstatement,cexitnode.create(nil));
  485. end;
  486. function initialize_data_node(p:tnode):tnode;
  487. begin
  488. if not assigned(p.resultdef) then
  489. typecheckpass(p);
  490. if is_ansistring(p.resultdef) or
  491. is_wide_or_unicode_string(p.resultdef) or
  492. is_interfacecom(p.resultdef) or
  493. is_dynamic_array(p.resultdef) then
  494. begin
  495. result:=cassignmentnode.create(
  496. ctypeconvnode.create_internal(p,voidpointertype),
  497. cnilnode.create
  498. );
  499. end
  500. else
  501. begin
  502. result:=ccallnode.createintern('fpc_initialize',
  503. ccallparanode.create(
  504. caddrnode.create_internal(
  505. crttinode.create(
  506. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  507. ccallparanode.create(
  508. caddrnode.create_internal(p),
  509. nil)));
  510. end;
  511. end;
  512. function finalize_data_node(p:tnode):tnode;
  513. var
  514. newstatement : tstatementnode;
  515. begin
  516. if not assigned(p.resultdef) then
  517. typecheckpass(p);
  518. if is_ansistring(p.resultdef) then
  519. begin
  520. result:=internalstatements(newstatement);
  521. addstatement(newstatement,ccallnode.createintern('fpc_ansistr_decr_ref',
  522. ccallparanode.create(
  523. ctypeconvnode.create_internal(p,voidpointertype),
  524. nil)));
  525. addstatement(newstatement,cassignmentnode.create(
  526. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  527. cnilnode.create
  528. ));
  529. end
  530. else if is_widestring(p.resultdef) then
  531. begin
  532. result:=internalstatements(newstatement);
  533. addstatement(newstatement,ccallnode.createintern('fpc_widestr_decr_ref',
  534. ccallparanode.create(
  535. ctypeconvnode.create_internal(p,voidpointertype),
  536. nil)));
  537. addstatement(newstatement,cassignmentnode.create(
  538. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  539. cnilnode.create
  540. ));
  541. end
  542. else if is_unicodestring(p.resultdef) then
  543. begin
  544. result:=internalstatements(newstatement);
  545. addstatement(newstatement,ccallnode.createintern('fpc_unicodestr_decr_ref',
  546. ccallparanode.create(
  547. ctypeconvnode.create_internal(p,voidpointertype),
  548. nil)));
  549. addstatement(newstatement,cassignmentnode.create(
  550. ctypeconvnode.create_internal(p.getcopy,voidpointertype),
  551. cnilnode.create
  552. ));
  553. end
  554. else if is_interfacecom(p.resultdef) then
  555. begin
  556. result:=internalstatements(newstatement);
  557. addstatement(newstatement,ccallnode.createintern('fpc_intf_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
  567. result:=ccallnode.createintern('fpc_finalize',
  568. ccallparanode.create(
  569. caddrnode.create_internal(
  570. crttinode.create(
  571. tstoreddef(p.resultdef),initrtti,rdt_normal)),
  572. ccallparanode.create(
  573. caddrnode.create_internal(p),
  574. nil)));
  575. end;
  576. { this function must return a very high value ("infinity") for }
  577. { trees containing a call, the rest can be balanced more or less }
  578. { at will, probably best mainly in terms of required memory }
  579. { accesses }
  580. function node_complexity(p: tnode): cardinal;
  581. var
  582. correction: byte;
  583. {$ifdef ARM}
  584. dummy : byte;
  585. {$endif ARM}
  586. begin
  587. result := 0;
  588. while assigned(p) do
  589. begin
  590. case p.nodetype of
  591. { floating point constants usually need loading from memory }
  592. realconstn,
  593. temprefn,
  594. loadvmtaddrn,
  595. { main reason for the next one: we can't take the address of }
  596. { loadparentfpnode, so replacing it by a temp which is the }
  597. { address of this node's location and then dereferencing }
  598. { doesn't work. If changed, check whether webtbs/tw0935 }
  599. { still works with nodeinlining (JM) }
  600. loadparentfpn:
  601. begin
  602. result := 1;
  603. exit;
  604. end;
  605. loadn:
  606. begin
  607. { threadvars need a helper call }
  608. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  609. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  610. inc(result,5)
  611. else
  612. inc(result);
  613. if (result >= NODE_COMPLEXITY_INF) then
  614. result := NODE_COMPLEXITY_INF;
  615. exit;
  616. end;
  617. subscriptn:
  618. begin
  619. if is_class_or_interface_or_dispinterface_or_objc(tunarynode(p).left.resultdef) then
  620. inc(result,2);
  621. if (result = NODE_COMPLEXITY_INF) then
  622. exit;
  623. p := tunarynode(p).left;
  624. end;
  625. blockn,
  626. callparan:
  627. p := tunarynode(p).left;
  628. notn,
  629. derefn :
  630. begin
  631. inc(result);
  632. if (result = NODE_COMPLEXITY_INF) then
  633. exit;
  634. p := tunarynode(p).left;
  635. end;
  636. typeconvn:
  637. begin
  638. { may be more complex in some cases }
  639. 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
  640. inc(result);
  641. if (result = NODE_COMPLEXITY_INF) then
  642. exit;
  643. p := tunarynode(p).left;
  644. end;
  645. vecn,
  646. statementn:
  647. begin
  648. inc(result,node_complexity(tbinarynode(p).left));
  649. if (result >= NODE_COMPLEXITY_INF) then
  650. begin
  651. result := NODE_COMPLEXITY_INF;
  652. exit;
  653. end;
  654. p := tbinarynode(p).right;
  655. end;
  656. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  657. shln,shrn,
  658. equaln,unequaln,gtn,gten,ltn,lten,
  659. assignn:
  660. begin
  661. {$ifdef CPU64BITALU}
  662. correction:=1;
  663. {$else CPU64BITALU}
  664. correction:=2;
  665. {$endif CPU64BITALU}
  666. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  667. if (p.nodetype in [muln,divn,modn]) then
  668. inc(result,5*correction*correction);
  669. if (result >= NODE_COMPLEXITY_INF) then
  670. begin
  671. result := NODE_COMPLEXITY_INF;
  672. exit;
  673. end;
  674. p := tbinarynode(p).right;
  675. end;
  676. ordconstn:
  677. begin
  678. {$ifdef ARM}
  679. if not(is_shifter_const(tordconstnode(p).value.svalue,dummy)) then
  680. result:=2;
  681. {$endif ARM}
  682. exit;
  683. end;
  684. stringconstn,
  685. tempcreaten,
  686. tempdeleten,
  687. pointerconstn,
  688. nothingn,
  689. niln:
  690. exit;
  691. inlinen:
  692. begin
  693. { this code assumes that the inline node has }
  694. { already been firstpassed, and consequently }
  695. { that inline nodes which are transformed into }
  696. { calls already have been transformed }
  697. case tinlinenode(p).inlinenumber of
  698. in_lo_qword,
  699. in_hi_qword,
  700. in_lo_long,
  701. in_hi_long,
  702. in_lo_word,
  703. in_hi_word,
  704. in_length_x,
  705. in_assigned_x,
  706. in_pred_x,
  707. in_succ_x,
  708. in_round_real,
  709. in_trunc_real,
  710. in_int_real,
  711. in_frac_real,
  712. in_cos_real,
  713. in_sin_real,
  714. in_arctan_real,
  715. in_pi_real,
  716. in_abs_real,
  717. in_sqr_real,
  718. in_sqrt_real,
  719. in_ln_real,
  720. in_unaligned_x,
  721. in_prefetch_var:
  722. begin
  723. inc(result);
  724. p:=tunarynode(p).left;
  725. end;
  726. in_abs_long:
  727. begin
  728. inc(result,3);
  729. if (result >= NODE_COMPLEXITY_INF) then
  730. begin
  731. result:=NODE_COMPLEXITY_INF;
  732. exit;
  733. end;
  734. p:=tunarynode(p).left;
  735. end;
  736. in_sizeof_x,
  737. in_typeof_x:
  738. begin
  739. inc(result);
  740. if (tinlinenode(p).left.nodetype<>typen) then
  741. { get instance vmt }
  742. p:=tunarynode(p).left
  743. else
  744. { type vmt = global symbol, result is }
  745. { already increased above }
  746. exit;
  747. end;
  748. {$ifdef SUPPORT_MMX}
  749. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  750. {$endif SUPPORT_MMX}
  751. { load from global symbol }
  752. in_typeinfo_x,
  753. { load frame pointer }
  754. in_get_frame,
  755. in_get_caller_frame,
  756. in_get_caller_addr:
  757. begin
  758. inc(result);
  759. exit;
  760. end;
  761. in_inc_x,
  762. in_dec_x,
  763. in_include_x_y,
  764. in_exclude_x_y,
  765. in_assert_x_y :
  766. begin
  767. { operation (add, sub, or, and }
  768. inc(result);
  769. { left expression }
  770. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  771. if (result >= NODE_COMPLEXITY_INF) then
  772. begin
  773. result := NODE_COMPLEXITY_INF;
  774. exit;
  775. end;
  776. p:=tcallparanode(tunarynode(p).left).right;
  777. if assigned(p) then
  778. p:=tcallparanode(p).left;
  779. end;
  780. else
  781. begin
  782. result := NODE_COMPLEXITY_INF;
  783. exit;
  784. end;
  785. end;
  786. end;
  787. else
  788. begin
  789. result := NODE_COMPLEXITY_INF;
  790. exit;
  791. end;
  792. end;
  793. end;
  794. end;
  795. { this function returns an indication how much fpu registers
  796. will be required.
  797. Note: The algorithms need to be pessimistic to prevent a
  798. fpu stack overflow on i386 }
  799. function node_resources_fpu(p: tnode): cardinal;
  800. var
  801. res1,res2,res3 : cardinal;
  802. begin
  803. result:=0;
  804. res1:=0;
  805. res2:=0;
  806. res3:=0;
  807. if p.inheritsfrom(tunarynode) then
  808. begin
  809. if assigned(tunarynode(p).left) then
  810. res1:=node_resources_fpu(tunarynode(p).left);
  811. if p.inheritsfrom(tbinarynode) then
  812. begin
  813. if assigned(tbinarynode(p).right) then
  814. res2:=node_resources_fpu(tbinarynode(p).right);
  815. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  816. res3:=node_resources_fpu(ttertiarynode(p).third)
  817. end;
  818. end;
  819. result:=max(max(res1,res2),res3);
  820. case p.nodetype of
  821. calln:
  822. { it could be a recursive call, so we never really know the number of used fpu registers }
  823. result:=maxfpuregs;
  824. realconstn,
  825. typeconvn,
  826. loadn :
  827. begin
  828. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  829. result:=max(result,1);
  830. end;
  831. assignn,
  832. addn,subn,muln,slashn,
  833. equaln,unequaln,gtn,gten,ltn,lten :
  834. begin
  835. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  836. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  837. result:=max(result,2);
  838. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  839. inc(result);
  840. end;
  841. end;
  842. end;
  843. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  844. begin
  845. result:=fen_true;
  846. n.fileinfo:=pfileposinfo(arg)^;
  847. end;
  848. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  849. begin
  850. foreachnodestatic(n,@setnodefilepos,@filepos);
  851. end;
  852. {$ifdef FPCMT}
  853. threadvar
  854. {$else FPCMT}
  855. var
  856. {$endif FPCMT}
  857. treechanged : boolean;
  858. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  859. var
  860. hn : tnode;
  861. begin
  862. result:=fen_false;
  863. // do_typecheckpass(n);
  864. hn:=n.simplify;
  865. if assigned(hn) then
  866. begin
  867. treechanged:=true;
  868. n.free;
  869. n:=hn;
  870. typecheckpass(n);
  871. end;
  872. end;
  873. { tries to simplify the given node calling the simplify method recursively }
  874. procedure dosimplify(var n : tnode);
  875. begin
  876. repeat
  877. treechanged:=false;
  878. foreachnodestatic(pm_preprocess,n,@callsimplify,nil);
  879. until not(treechanged);
  880. end;
  881. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  882. var
  883. hpropsym : tpropertysym;
  884. begin
  885. result:=false;
  886. { find property in the overriden list }
  887. hpropsym:=propsym;
  888. repeat
  889. propaccesslist:=hpropsym.propaccesslist[pap];
  890. if not propaccesslist.empty then
  891. begin
  892. result:=true;
  893. exit;
  894. end;
  895. hpropsym:=hpropsym.overridenpropsym;
  896. until not assigned(hpropsym);
  897. end;
  898. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  899. var
  900. plist : ppropaccesslistitem;
  901. begin
  902. plist:=pl.firstsym;
  903. while assigned(plist) do
  904. begin
  905. case plist^.sltype of
  906. sl_load :
  907. begin
  908. addsymref(plist^.sym);
  909. if not assigned(st) then
  910. st:=plist^.sym.owner;
  911. { p1 can already contain the loadnode of
  912. the class variable. When there is no tree yet we
  913. may need to load it for with or objects }
  914. if not assigned(p1) then
  915. begin
  916. case st.symtabletype of
  917. withsymtable :
  918. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  919. ObjectSymtable :
  920. p1:=load_self_node;
  921. end;
  922. end;
  923. if assigned(p1) then
  924. p1:=csubscriptnode.create(plist^.sym,p1)
  925. else
  926. p1:=cloadnode.create(plist^.sym,st);
  927. end;
  928. sl_subscript :
  929. begin
  930. addsymref(plist^.sym);
  931. p1:=csubscriptnode.create(plist^.sym,p1);
  932. end;
  933. sl_typeconv :
  934. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  935. sl_absolutetype :
  936. begin
  937. p1:=ctypeconvnode.create(p1,plist^.def);
  938. include(p1.flags,nf_absolute);
  939. end;
  940. sl_vec :
  941. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  942. else
  943. internalerror(200110205);
  944. end;
  945. plist:=plist^.next;
  946. end;
  947. end;
  948. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  949. var
  950. sl : tpropaccesslist;
  951. procedure addnode(p:tnode);
  952. begin
  953. case p.nodetype of
  954. subscriptn :
  955. begin
  956. addnode(tsubscriptnode(p).left);
  957. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  958. end;
  959. typeconvn :
  960. begin
  961. addnode(ttypeconvnode(p).left);
  962. if nf_absolute in ttypeconvnode(p).flags then
  963. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  964. else
  965. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  966. end;
  967. vecn :
  968. begin
  969. addnode(tvecnode(p).left);
  970. if tvecnode(p).right.nodetype=ordconstn then
  971. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  972. else
  973. begin
  974. Message(parser_e_illegal_expression);
  975. { recovery }
  976. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  977. end;
  978. end;
  979. loadn :
  980. sl.addsym(sl_load,tloadnode(p).symtableentry);
  981. else
  982. internalerror(200310282);
  983. end;
  984. end;
  985. begin
  986. sl:=tpropaccesslist.create;
  987. addnode(p1);
  988. result:=sl;
  989. end;
  990. function is_bitpacked_access(n: tnode): boolean;
  991. begin
  992. case n.nodetype of
  993. vecn:
  994. result:=
  995. is_packed_array(tvecnode(n).left.resultdef) and
  996. (tarraydef(tvecnode(n).left.resultdef).elepackedbitsize mod 8 <> 0);
  997. subscriptn:
  998. result:=
  999. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  1000. ((tsubscriptnode(n).vs.vardef.packedbitsize mod 8 <> 0) or
  1001. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  1002. else
  1003. result:=false;
  1004. end;
  1005. end;
  1006. function genloadfield(n: tnode; const fieldname: string): tnode;
  1007. var
  1008. vs : tsym;
  1009. begin
  1010. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  1011. if not assigned(vs) or
  1012. (vs.typ<>fieldvarsym) then
  1013. internalerror(2010061902);
  1014. result:=csubscriptnode.create(vs,n);
  1015. end;
  1016. function has_no_code(n : tnode) : boolean;
  1017. begin
  1018. if n=nil then
  1019. begin
  1020. result:=true;
  1021. exit;
  1022. end;
  1023. result:=false;
  1024. case n.nodetype of
  1025. nothingn:
  1026. begin
  1027. result:=true;
  1028. exit;
  1029. end;
  1030. blockn:
  1031. begin
  1032. result:=has_no_code(tblocknode(n).left);
  1033. exit;
  1034. end;
  1035. statementn:
  1036. begin
  1037. repeat
  1038. result:=has_no_code(tstatementnode(n).left);
  1039. n:=tstatementnode(n).right;
  1040. until not(result) or not assigned(n);
  1041. exit;
  1042. end;
  1043. end;
  1044. end;
  1045. end.