nutils.pas 59 KB

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