nutils.pas 49 KB

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