nutils.pas 35 KB

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