nutils.pas 37 KB

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