nutils.pas 35 KB

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