nutils.pas 51 KB

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