nutils.pas 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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,constexp,
  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 = ({ children are processed before the parent node }
  39. pm_preprocess,
  40. { children are processed after the parent node }
  41. pm_postprocess,
  42. { children are processed after the parent node and
  43. then the parent node is processed again }
  44. pm_postandagain);
  45. foreachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult of object;
  46. staticforeachnodefunction = function(var n: tnode; arg: pointer): foreachnoderesult;
  47. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  48. function foreachnode(procmethod : tforeachprocmethod; var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  49. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  50. function foreachnodestatic(procmethod : tforeachprocmethod; var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  51. { checks if the given node tree contains only nodes of the given type,
  52. if this isn't the case, an ie is thrown
  53. }
  54. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  55. procedure load_procvar_from_calln(var p1:tnode);
  56. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  57. function load_high_value_node(vs:tparavarsym):tnode;
  58. function load_self_node:tnode;
  59. function load_result_node:tnode;
  60. function load_self_pointer_node:tnode;
  61. function load_vmt_pointer_node:tnode;
  62. function is_self_node(p:tnode):boolean;
  63. function node_complexity(p: tnode): cardinal;
  64. function node_resources_fpu(p: tnode): cardinal;
  65. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  66. { tries to simplify the given node after inlining }
  67. procedure doinlinesimplify(var n : tnode);
  68. { creates an ordinal constant, optionally based on the result from a
  69. simplify operation: normally the type is the smallest integer type
  70. that can hold the value, but when inlining the "def" will be used instead,
  71. which was determined during an earlier typecheck pass (because the value
  72. may e.g. be a parameter to a call, which needs to be of the declared
  73. parameter type) }
  74. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  75. { returns true if n is only a tree of administrative nodes
  76. containing no code }
  77. function has_no_code(n : tnode) : boolean;
  78. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  79. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  80. { checks whether sym is a static field and if so, translates the access
  81. to the appropriate node tree }
  82. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  83. { returns true if n is an array element access of a bitpacked array with
  84. elements of the which the vitsize mod 8 <> 0, or if is a field access
  85. with bitsize mod 8 <> 0 or bitoffset mod 8 <> 0 of an element in a
  86. bitpacked structure }
  87. function is_bitpacked_access(n: tnode): boolean;
  88. { creates a load of field 'fieldname' in the record/class/...
  89. represented by n }
  90. function genloadfield(n: tnode; const fieldname: string): tnode;
  91. { returns true, if the tree given might have side effects }
  92. function might_have_sideeffects(n : tnode) : boolean;
  93. { count the number of nodes in the node tree,
  94. rough estimation how large the tree "node" is }
  95. function node_count(node : tnode) : dword;
  96. { returns true, if the value described by node is constant/immutable, this approximation is safe
  97. if no dirty tricks like buffer overflows or pointer magic are used }
  98. function is_const(node : tnode) : boolean;
  99. { returns a pointer to the real node a node refers to,
  100. skipping (absolute) equal type conversions. Returning
  101. a pointer allows the caller to move/remove/replace this
  102. node
  103. }
  104. function actualtargetnode(n : pnode) : pnode;
  105. { moves src into dest, before doing so, right is set to nil and dest is freed.
  106. Because dest and src are var parameters, this can be done inline in an existing
  107. node tree }
  108. procedure replacenode(var dest,src : tnode);
  109. implementation
  110. uses
  111. cutils,verbose,globals,
  112. symconst,symdef,
  113. defutil,defcmp,
  114. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,ninl,
  115. cpubase,cgbase,procinfo,
  116. pass_1;
  117. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  118. function process_children(res : boolean) : boolean;
  119. var
  120. i: longint;
  121. begin
  122. result:=res;
  123. case n.nodetype of
  124. asn:
  125. if assigned(tasnode(n).call) then
  126. begin
  127. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  128. exit
  129. end;
  130. calln:
  131. begin
  132. result := foreachnode(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  133. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  134. result := foreachnode(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  135. result := foreachnode(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  136. end;
  137. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  138. begin
  139. { not in one statement, won't work because of b- }
  140. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  141. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  142. end;
  143. raisen:
  144. { frame tree }
  145. result := foreachnode(ttertiarynode(n).third,f,arg) or result;
  146. tempcreaten:
  147. { temp. initialization code }
  148. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  149. result := foreachnode(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  150. casen:
  151. begin
  152. for i := 0 to tcasenode(n).blocks.count-1 do
  153. if assigned(tcasenode(n).blocks[i]) then
  154. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  155. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  156. end;
  157. end;
  158. if n.inheritsfrom(tbinarynode) then
  159. begin
  160. { first process the "payload" of statementnodes }
  161. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  162. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  163. end
  164. else if n.inheritsfrom(tunarynode) then
  165. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  166. end;
  167. begin
  168. result := false;
  169. if not assigned(n) then
  170. exit;
  171. if procmethod=pm_preprocess then
  172. result:=process_children(result);
  173. case f(n,arg) of
  174. fen_norecurse_false:
  175. exit;
  176. fen_norecurse_true:
  177. begin
  178. result := true;
  179. exit;
  180. end;
  181. fen_true:
  182. result := true;
  183. { result is already false
  184. fen_false:
  185. result := false; }
  186. end;
  187. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  188. result:=process_children(result);
  189. if procmethod=pm_postandagain then
  190. begin
  191. case f(n,arg) of
  192. fen_norecurse_false:
  193. exit;
  194. fen_norecurse_true:
  195. begin
  196. result := true;
  197. exit;
  198. end;
  199. fen_true:
  200. result := true;
  201. end;
  202. end;
  203. end;
  204. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  205. begin
  206. result:=foreachnode(pm_postprocess,n,f,arg);
  207. end;
  208. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  209. function process_children(res : boolean) : boolean;
  210. var
  211. i: longint;
  212. begin
  213. result:=res;
  214. case n.nodetype of
  215. asn:
  216. if assigned(tasnode(n).call) then
  217. begin
  218. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  219. exit
  220. end;
  221. calln:
  222. begin
  223. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  224. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  225. result := foreachnodestatic(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  226. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  227. end;
  228. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  229. begin
  230. { not in one statement, won't work because of b- }
  231. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  232. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  233. end;
  234. raisen:
  235. { frame tree }
  236. result := foreachnodestatic(ttertiarynode(n).third,f,arg) or result;
  237. tempcreaten:
  238. { temp. initialization code }
  239. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  240. result := foreachnodestatic(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  241. casen:
  242. begin
  243. for i := 0 to tcasenode(n).blocks.count-1 do
  244. if assigned(tcasenode(n).blocks[i]) then
  245. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  246. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  247. end;
  248. end;
  249. if n.inheritsfrom(tbinarynode) then
  250. begin
  251. { first process the "payload" of statementnodes }
  252. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  253. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  254. end
  255. else if n.inheritsfrom(tunarynode) then
  256. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  257. end;
  258. begin
  259. result := false;
  260. if not assigned(n) then
  261. exit;
  262. if procmethod=pm_preprocess then
  263. result:=process_children(result);
  264. case f(n,arg) of
  265. fen_norecurse_false:
  266. exit;
  267. fen_norecurse_true:
  268. begin
  269. result := true;
  270. exit;
  271. end;
  272. fen_true:
  273. result := true;
  274. { result is already false
  275. fen_false:
  276. result := false; }
  277. end;
  278. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  279. result:=process_children(result);
  280. if procmethod=pm_postandagain then
  281. begin
  282. case f(n,arg) of
  283. fen_norecurse_false:
  284. exit;
  285. fen_norecurse_true:
  286. begin
  287. result := true;
  288. exit;
  289. end;
  290. fen_true:
  291. result := true;
  292. end;
  293. end;
  294. end;
  295. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  296. begin
  297. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  298. end;
  299. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  300. begin
  301. if not(n.nodetype in pnodetypeset(arg)^) then
  302. internalerror(200610141);
  303. result:=fen_true;
  304. end;
  305. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  306. begin
  307. foreachnodestatic(n,@do_check,@typeset);
  308. end;
  309. procedure load_procvar_from_calln(var p1:tnode);
  310. var
  311. p2 : tnode;
  312. begin
  313. if p1.nodetype<>calln then
  314. internalerror(200212251);
  315. { was it a procvar, then we simply remove the calln and
  316. reuse the right }
  317. if assigned(tcallnode(p1).right) then
  318. begin
  319. p2:=tcallnode(p1).right;
  320. tcallnode(p1).right:=nil;
  321. end
  322. else
  323. begin
  324. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  325. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  326. { when the methodpointer is typen we've something like:
  327. tobject.create. Then only the address is needed of the
  328. method without a self pointer }
  329. if assigned(tcallnode(p1).methodpointer) and
  330. (tcallnode(p1).methodpointer.nodetype<>typen) then
  331. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  332. end;
  333. typecheckpass(p2);
  334. p1.free;
  335. p1:=p2;
  336. end;
  337. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  338. var
  339. hp : tnode;
  340. begin
  341. result:=false;
  342. if (p1.resultdef.typ<>procvardef) or
  343. (tponly and
  344. not(m_tp_procvar in current_settings.modeswitches)) then
  345. exit;
  346. { ignore vecn,subscriptn }
  347. hp:=p1;
  348. repeat
  349. case hp.nodetype of
  350. vecn,
  351. derefn,
  352. typeconvn,
  353. subscriptn :
  354. hp:=tunarynode(hp).left;
  355. else
  356. break;
  357. end;
  358. until false;
  359. { a tempref is used when it is loaded from a withsymtable }
  360. if (hp.nodetype in [calln,loadn,temprefn]) then
  361. begin
  362. hp:=ccallnode.create_procvar(nil,p1);
  363. typecheckpass(hp);
  364. p1:=hp;
  365. result:=true;
  366. end;
  367. end;
  368. function get_local_or_para_sym(const aname:string):tsym;
  369. var
  370. pd : tprocdef;
  371. begin
  372. result:=nil;
  373. { is not assigned while parsing a property }
  374. if not assigned(current_procinfo) then
  375. exit;
  376. { we can't use searchsym here, because the
  377. symtablestack is not fully setup when pass1
  378. is run for nested procedures }
  379. pd:=current_procinfo.procdef;
  380. repeat
  381. result := tsym(pd.localst.Find(aname));
  382. if assigned(result) then
  383. break;
  384. result := tsym(pd.parast.Find(aname));
  385. if assigned(result) then
  386. break;
  387. { try the parent of a nested function }
  388. if assigned(pd.owner.defowner) and
  389. (pd.owner.defowner.typ=procdef) then
  390. pd:=tprocdef(pd.owner.defowner)
  391. else
  392. break;
  393. until false;
  394. end;
  395. function load_high_value_node(vs:tparavarsym):tnode;
  396. var
  397. srsym : tsym;
  398. begin
  399. result:=nil;
  400. srsym:=get_high_value_sym(vs);
  401. if assigned(srsym) then
  402. begin
  403. result:=cloadnode.create(srsym,vs.owner);
  404. typecheckpass(result);
  405. end
  406. else
  407. CGMessage(parser_e_illegal_expression);
  408. end;
  409. function load_self_node:tnode;
  410. var
  411. srsym : tsym;
  412. begin
  413. result:=nil;
  414. srsym:=get_local_or_para_sym('self');
  415. if assigned(srsym) then
  416. begin
  417. result:=cloadnode.create(srsym,srsym.owner);
  418. include(tloadnode(result).loadnodeflags,loadnf_is_self);
  419. end
  420. else
  421. begin
  422. result:=cerrornode.create;
  423. CGMessage(parser_e_illegal_expression);
  424. end;
  425. typecheckpass(result);
  426. end;
  427. function load_result_node:tnode;
  428. var
  429. srsym : tsym;
  430. begin
  431. result:=nil;
  432. srsym:=get_local_or_para_sym('result');
  433. if assigned(srsym) then
  434. result:=cloadnode.create(srsym,srsym.owner)
  435. else
  436. begin
  437. result:=cerrornode.create;
  438. CGMessage(parser_e_illegal_expression);
  439. end;
  440. typecheckpass(result);
  441. end;
  442. function load_self_pointer_node:tnode;
  443. var
  444. srsym : tsym;
  445. begin
  446. result:=nil;
  447. srsym:=get_local_or_para_sym('self');
  448. if assigned(srsym) then
  449. begin
  450. result:=cloadnode.create(srsym,srsym.owner);
  451. include(tloadnode(result).loadnodeflags,loadnf_load_self_pointer);
  452. end
  453. else
  454. begin
  455. result:=cerrornode.create;
  456. CGMessage(parser_e_illegal_expression);
  457. end;
  458. typecheckpass(result);
  459. end;
  460. function load_vmt_pointer_node:tnode;
  461. var
  462. srsym : tsym;
  463. begin
  464. result:=nil;
  465. srsym:=get_local_or_para_sym('vmt');
  466. if assigned(srsym) then
  467. result:=cloadnode.create(srsym,srsym.owner)
  468. else
  469. begin
  470. result:=cerrornode.create;
  471. CGMessage(parser_e_illegal_expression);
  472. end;
  473. typecheckpass(result);
  474. end;
  475. function is_self_node(p:tnode):boolean;
  476. begin
  477. is_self_node:=(p.nodetype=loadn) and
  478. (tloadnode(p).symtableentry.typ=paravarsym) and
  479. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  480. end;
  481. { this function must return a very high value ("infinity") for }
  482. { trees containing a call, the rest can be balanced more or less }
  483. { at will, probably best mainly in terms of required memory }
  484. { accesses }
  485. function node_complexity(p: tnode): cardinal;
  486. var
  487. correction: byte;
  488. {$ifdef ARM}
  489. dummy : byte;
  490. {$endif ARM}
  491. begin
  492. result := 0;
  493. while assigned(p) do
  494. begin
  495. case p.nodetype of
  496. { floating point constants usually need loading from memory }
  497. realconstn:
  498. begin
  499. result:=2;
  500. exit;
  501. end;
  502. setconstn,
  503. stringconstn,
  504. temprefn,
  505. loadvmtaddrn,
  506. { main reason for the next one: we can't take the address of }
  507. { loadparentfpnode, so replacing it by a temp which is the }
  508. { address of this node's location and then dereferencing }
  509. { doesn't work. If changed, check whether webtbs/tw0935 }
  510. { still works with nodeinlining (JM) }
  511. loadparentfpn:
  512. begin
  513. result := 1;
  514. exit;
  515. end;
  516. loadn:
  517. begin
  518. if assigned(tloadnode(p).left) then
  519. inc(result,node_complexity(tloadnode(p).left));
  520. { threadvars need a helper call }
  521. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  522. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  523. inc(result,5)
  524. else
  525. inc(result);
  526. if (tloadnode(p).symtableentry.typ=paravarsym) and tloadnode(p).is_addr_param_load then
  527. inc(result);
  528. if (result >= NODE_COMPLEXITY_INF) then
  529. result := NODE_COMPLEXITY_INF;
  530. exit;
  531. end;
  532. subscriptn:
  533. begin
  534. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  535. is_bitpacked_access(p) then
  536. inc(result,2)
  537. else if tstoreddef(p.resultdef).is_intregable then
  538. inc(result,1);
  539. if (result = NODE_COMPLEXITY_INF) then
  540. exit;
  541. p := tunarynode(p).left;
  542. end;
  543. labeln,
  544. blockn,
  545. callparan:
  546. p := tunarynode(p).left;
  547. notn,
  548. derefn :
  549. begin
  550. inc(result);
  551. if (result = NODE_COMPLEXITY_INF) then
  552. exit;
  553. p := tunarynode(p).left;
  554. end;
  555. addrn:
  556. begin
  557. inc(result);
  558. if (result = NODE_COMPLEXITY_INF) then
  559. exit;
  560. p := tunarynode(p).left;
  561. end;
  562. typeconvn:
  563. begin
  564. { may be more complex in some cases }
  565. if not(ttypeconvnode(p).retains_value_location) then
  566. inc(result);
  567. if (result = NODE_COMPLEXITY_INF) then
  568. exit;
  569. p := tunarynode(p).left;
  570. end;
  571. vecn,
  572. statementn:
  573. begin
  574. inc(result,node_complexity(tbinarynode(p).left));
  575. if (result >= NODE_COMPLEXITY_INF) then
  576. begin
  577. result := NODE_COMPLEXITY_INF;
  578. exit;
  579. end;
  580. p := tbinarynode(p).right;
  581. end;
  582. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  583. shln,shrn,
  584. equaln,unequaln,gtn,gten,ltn,lten,
  585. assignn:
  586. begin
  587. {$ifdef CPU64BITALU}
  588. correction:=1;
  589. {$else CPU64BITALU}
  590. correction:=2;
  591. {$endif CPU64BITALU}
  592. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  593. if (p.nodetype in [muln,divn,modn]) then
  594. inc(result,5*correction*correction);
  595. if (result >= NODE_COMPLEXITY_INF) then
  596. begin
  597. result := NODE_COMPLEXITY_INF;
  598. exit;
  599. end;
  600. p := tbinarynode(p).right;
  601. end;
  602. ordconstn:
  603. begin
  604. {$ifdef ARM}
  605. if not(is_shifter_const(tordconstnode(p).value.svalue,dummy)) then
  606. result:=2;
  607. {$endif ARM}
  608. exit;
  609. end;
  610. exitn:
  611. begin
  612. inc(result,2);
  613. if (result >= NODE_COMPLEXITY_INF) then
  614. begin
  615. result := NODE_COMPLEXITY_INF;
  616. exit;
  617. end;
  618. p:=texitnode(p).left;
  619. end;
  620. tempcreaten,
  621. tempdeleten,
  622. pointerconstn,
  623. nothingn,
  624. niln:
  625. exit;
  626. inlinen:
  627. begin
  628. { this code assumes that the inline node has }
  629. { already been firstpassed, and consequently }
  630. { that inline nodes which are transformed into }
  631. { calls already have been transformed }
  632. case tinlinenode(p).inlinenumber of
  633. in_lo_qword,
  634. in_hi_qword,
  635. in_lo_long,
  636. in_hi_long,
  637. in_lo_word,
  638. in_hi_word,
  639. in_length_x,
  640. in_assigned_x,
  641. in_pred_x,
  642. in_succ_x,
  643. in_round_real,
  644. in_trunc_real,
  645. in_int_real,
  646. in_frac_real,
  647. in_cos_real,
  648. in_sin_real,
  649. in_arctan_real,
  650. in_pi_real,
  651. in_abs_real,
  652. in_sqr_real,
  653. in_sqrt_real,
  654. in_ln_real,
  655. in_aligned_x,
  656. in_unaligned_x,
  657. in_prefetch_var:
  658. begin
  659. inc(result);
  660. p:=tunarynode(p).left;
  661. end;
  662. in_abs_long:
  663. begin
  664. inc(result,3);
  665. if (result >= NODE_COMPLEXITY_INF) then
  666. begin
  667. result:=NODE_COMPLEXITY_INF;
  668. exit;
  669. end;
  670. p:=tunarynode(p).left;
  671. end;
  672. in_sizeof_x,
  673. in_typeof_x:
  674. begin
  675. inc(result);
  676. if (tinlinenode(p).left.nodetype<>typen) then
  677. { get instance vmt }
  678. p:=tunarynode(p).left
  679. else
  680. { type vmt = global symbol, result is }
  681. { already increased above }
  682. exit;
  683. end;
  684. {$ifdef SUPPORT_MMX}
  685. in_mmx_pcmpeqb..in_mmx_pcmpgtw,
  686. {$endif SUPPORT_MMX}
  687. { load from global symbol }
  688. in_typeinfo_x,
  689. { load frame pointer }
  690. in_get_frame,
  691. in_get_caller_frame,
  692. in_get_caller_addr:
  693. begin
  694. inc(result);
  695. exit;
  696. end;
  697. in_inc_x,
  698. in_dec_x,
  699. in_include_x_y,
  700. in_exclude_x_y,
  701. in_assert_x_y :
  702. begin
  703. { operation (add, sub, or, and }
  704. inc(result);
  705. { left expression }
  706. inc(result,node_complexity(tcallparanode(tunarynode(p).left).left));
  707. if (result >= NODE_COMPLEXITY_INF) then
  708. begin
  709. result := NODE_COMPLEXITY_INF;
  710. exit;
  711. end;
  712. p:=tcallparanode(tunarynode(p).left).right;
  713. if assigned(p) then
  714. p:=tcallparanode(p).left;
  715. end;
  716. else
  717. begin
  718. result := NODE_COMPLEXITY_INF;
  719. exit;
  720. end;
  721. end;
  722. end;
  723. else
  724. begin
  725. result := NODE_COMPLEXITY_INF;
  726. exit;
  727. end;
  728. end;
  729. end;
  730. end;
  731. { this function returns an indication how much fpu registers
  732. will be required.
  733. Note: The algorithms need to be pessimistic to prevent a
  734. fpu stack overflow on i386 }
  735. function node_resources_fpu(p: tnode): cardinal;
  736. var
  737. res1,res2,res3 : cardinal;
  738. begin
  739. result:=0;
  740. res1:=0;
  741. res2:=0;
  742. res3:=0;
  743. if p.inheritsfrom(tunarynode) then
  744. begin
  745. if assigned(tunarynode(p).left) then
  746. res1:=node_resources_fpu(tunarynode(p).left);
  747. if p.inheritsfrom(tbinarynode) then
  748. begin
  749. if assigned(tbinarynode(p).right) then
  750. res2:=node_resources_fpu(tbinarynode(p).right);
  751. if p.inheritsfrom(ttertiarynode) and assigned(ttertiarynode(p).third) then
  752. res3:=node_resources_fpu(ttertiarynode(p).third)
  753. end;
  754. end;
  755. result:=max(max(res1,res2),res3);
  756. case p.nodetype of
  757. calln:
  758. { it could be a recursive call, so we never really know the number of used fpu registers }
  759. result:=maxfpuregs;
  760. realconstn,
  761. typeconvn,
  762. loadn :
  763. begin
  764. if p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  765. result:=max(result,1);
  766. end;
  767. assignn,
  768. addn,subn,muln,slashn,
  769. equaln,unequaln,gtn,gten,ltn,lten :
  770. begin
  771. if (tbinarynode(p).left.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) or
  772. (tbinarynode(p).right.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER])then
  773. result:=max(result,2);
  774. if(p.expectloc in [LOC_CFPUREGISTER,LOC_FPUREGISTER]) then
  775. inc(result);
  776. end;
  777. end;
  778. end;
  779. function setnodefilepos(var n: tnode; arg: pointer): foreachnoderesult;
  780. begin
  781. result:=fen_true;
  782. n.fileinfo:=pfileposinfo(arg)^;
  783. end;
  784. procedure node_tree_set_filepos(var n:tnode;const filepos:tfileposinfo);
  785. begin
  786. foreachnodestatic(n,@setnodefilepos,@filepos);
  787. end;
  788. function callsimplify(var n: tnode; arg: pointer): foreachnoderesult;
  789. var
  790. hn : tnode;
  791. treechanged : ^boolean;
  792. begin
  793. result:=fen_false;
  794. if n.inheritsfrom(tloopnode) and
  795. not (lnf_simplify_processing in tloopnode(n).loopflags) then
  796. begin
  797. // Try to simplify condition
  798. doinlinesimplify(tloopnode(n).left);
  799. // call directly second part below,
  800. // which might change the loopnode into
  801. // something else if the conditino is a constant node
  802. include(tloopnode(n).loopflags,lnf_simplify_processing);
  803. callsimplify(n,arg);
  804. // Be careful, n might have change node type
  805. if n.inheritsfrom(tloopnode) then
  806. exclude(tloopnode(n).loopflags,lnf_simplify_processing);
  807. end
  808. else
  809. begin
  810. hn:=n.simplify(true);
  811. if assigned(hn) then
  812. begin
  813. treechanged := arg;
  814. if assigned(treechanged) then
  815. treechanged^:=true
  816. else
  817. internalerror (201008181);
  818. n.free;
  819. n:=hn;
  820. typecheckpass(n);
  821. end;
  822. end;
  823. end;
  824. { tries to simplify the given node calling the simplify method recursively }
  825. procedure doinlinesimplify(var n : tnode);
  826. var
  827. treechanged : boolean;
  828. begin
  829. // Optimize if code first
  830. repeat
  831. treechanged:=false;
  832. foreachnodestatic(pm_postandagain,n,@callsimplify,@treechanged);
  833. until not(treechanged);
  834. end;
  835. function create_simplified_ord_const(value: tconstexprint; def: tdef; forinline: boolean): tnode;
  836. begin
  837. if not forinline then
  838. result:=genintconstnode(value)
  839. else
  840. result:=cordconstnode.create(value,def,cs_check_range in current_settings.localswitches);
  841. end;
  842. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  843. var
  844. plist : ppropaccesslistitem;
  845. begin
  846. plist:=pl.firstsym;
  847. while assigned(plist) do
  848. begin
  849. case plist^.sltype of
  850. sl_load :
  851. begin
  852. addsymref(plist^.sym);
  853. if not assigned(st) then
  854. st:=plist^.sym.owner;
  855. if (plist^.sym.typ<>staticvarsym) then
  856. begin
  857. { p1 can already contain the loadnode of
  858. the class variable. When there is no tree yet we
  859. may need to load it for with or objects }
  860. if not assigned(p1) then
  861. begin
  862. case st.symtabletype of
  863. withsymtable :
  864. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  865. ObjectSymtable :
  866. p1:=load_self_node;
  867. end;
  868. end
  869. end
  870. else
  871. begin
  872. p1.free;
  873. p1:=nil;
  874. end;
  875. if assigned(p1) then
  876. p1:=csubscriptnode.create(plist^.sym,p1)
  877. else
  878. p1:=cloadnode.create(plist^.sym,st);
  879. end;
  880. sl_subscript :
  881. begin
  882. addsymref(plist^.sym);
  883. p1:=csubscriptnode.create(plist^.sym,p1);
  884. end;
  885. sl_typeconv :
  886. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  887. sl_absolutetype :
  888. begin
  889. p1:=ctypeconvnode.create(p1,plist^.def);
  890. include(p1.flags,nf_absolute);
  891. end;
  892. sl_vec :
  893. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  894. else
  895. internalerror(200110205);
  896. end;
  897. plist:=plist^.next;
  898. end;
  899. end;
  900. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  901. var
  902. sl : tpropaccesslist;
  903. procedure addnode(p:tnode);
  904. begin
  905. case p.nodetype of
  906. subscriptn :
  907. begin
  908. addnode(tsubscriptnode(p).left);
  909. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  910. end;
  911. typeconvn :
  912. begin
  913. addnode(ttypeconvnode(p).left);
  914. if nf_absolute in ttypeconvnode(p).flags then
  915. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  916. else
  917. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  918. end;
  919. vecn :
  920. begin
  921. addnode(tvecnode(p).left);
  922. if tvecnode(p).right.nodetype=ordconstn then
  923. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  924. else
  925. begin
  926. Message(parser_e_illegal_expression);
  927. { recovery }
  928. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  929. end;
  930. end;
  931. loadn :
  932. sl.addsym(sl_load,tloadnode(p).symtableentry);
  933. else
  934. internalerror(200310282);
  935. end;
  936. end;
  937. begin
  938. sl:=tpropaccesslist.create;
  939. addnode(p1);
  940. result:=sl;
  941. end;
  942. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  943. var
  944. static_name: shortstring;
  945. srsymtable: tsymtable;
  946. begin
  947. result:=false;
  948. { generate access code }
  949. if (sp_static in sym.symoptions) then
  950. begin
  951. result:=true;
  952. if not nested then
  953. static_name:=lower(sym.owner.name^)+'_'+sym.name
  954. else
  955. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  956. if sym.owner.defowner.typ=objectdef then
  957. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,[ssf_search_helper])
  958. else
  959. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  960. if assigned(sym) then
  961. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  962. p1.free;
  963. p1:=nil;
  964. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  965. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  966. end;
  967. end;
  968. function is_bitpacked_access(n: tnode): boolean;
  969. begin
  970. case n.nodetype of
  971. vecn:
  972. result:=
  973. is_packed_array(tvecnode(n).left.resultdef) and
  974. { only orddefs and enumdefs are actually bitpacked. Don't consider
  975. e.g. an access to a 3-byte record as "bitpacked", since it
  976. isn't }
  977. (tvecnode(n).left.resultdef.typ = arraydef) and
  978. (tarraydef(tvecnode(n).left.resultdef).elementdef.typ in [orddef,enumdef]) and
  979. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  980. subscriptn:
  981. result:=
  982. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  983. { see above }
  984. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  985. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  986. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  987. else
  988. result:=false;
  989. end;
  990. end;
  991. function genloadfield(n: tnode; const fieldname: string): tnode;
  992. var
  993. vs : tsym;
  994. begin
  995. if not assigned(n.resultdef) then
  996. typecheckpass(n);
  997. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  998. if not assigned(vs) or
  999. (vs.typ<>fieldvarsym) then
  1000. internalerror(2010061902);
  1001. result:=csubscriptnode.create(vs,n);
  1002. end;
  1003. function has_no_code(n : tnode) : boolean;
  1004. begin
  1005. if n=nil then
  1006. begin
  1007. result:=true;
  1008. exit;
  1009. end;
  1010. result:=false;
  1011. case n.nodetype of
  1012. nothingn:
  1013. begin
  1014. result:=true;
  1015. exit;
  1016. end;
  1017. blockn:
  1018. begin
  1019. result:=has_no_code(tblocknode(n).left);
  1020. exit;
  1021. end;
  1022. statementn:
  1023. begin
  1024. repeat
  1025. result:=has_no_code(tstatementnode(n).left);
  1026. n:=tstatementnode(n).right;
  1027. until not(result) or not assigned(n);
  1028. exit;
  1029. end;
  1030. end;
  1031. end;
  1032. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1033. begin
  1034. result:=fen_false;
  1035. if (n.nodetype in [assignn,calln,asmn]) or
  1036. ((n.nodetype=inlinen) and
  1037. (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  1038. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,in_settextbuf_file_x,
  1039. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  1040. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle])
  1041. ) then
  1042. result:=fen_norecurse_true;
  1043. end;
  1044. function might_have_sideeffects(n : tnode) : boolean;
  1045. begin
  1046. result:=foreachnodestatic(n,@check_for_sideeffect,nil);
  1047. end;
  1048. var
  1049. nodecount : dword;
  1050. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1051. begin
  1052. inc(nodecount);
  1053. result:=fen_false;
  1054. end;
  1055. function node_count(node : tnode) : dword;
  1056. begin
  1057. nodecount:=0;
  1058. foreachnodestatic(node,@donodecount,nil);
  1059. result:=nodecount;
  1060. end;
  1061. function is_const(node : tnode) : boolean;
  1062. begin
  1063. result:=(node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempinfo^.flags)
  1064. end;
  1065. function actualtargetnode(n : pnode) : pnode;
  1066. begin
  1067. result:=n;
  1068. case n^.nodetype of
  1069. typeconvn:
  1070. if ttypeconvnode(n^).retains_value_location then
  1071. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1072. end;
  1073. end;
  1074. procedure replacenode(var dest,src : tnode);
  1075. var
  1076. t : tnode;
  1077. begin
  1078. t:=src;
  1079. { set src nil before free'ing dest because
  1080. src could be part of dest }
  1081. src:=nil;
  1082. dest.Free;
  1083. dest:=t;
  1084. end;
  1085. end.