nutils.pas 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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. If more than max nodes, returns max, so node_count(n, 10 + 1) <= 10 answers whether the tree has ≤10 nodes but avoids traversing the remaining 990. }
  111. function node_count(node : tnode; max : dword = High(dword)) : dword;
  112. function node_count_weighted(node : tnode; max : dword = High(dword)) : dword;
  113. { returns true, if the value described by node is constant/immutable, this approximation is safe
  114. if no dirty tricks like buffer overflows or pointer magic are used }
  115. function is_const(node : tnode) : boolean;
  116. { returns a pointer to the real node a node refers to,
  117. skipping (absolute) equal type conversions. Returning
  118. a pointer allows the caller to move/remove/replace this
  119. node
  120. }
  121. function actualtargetnode(n : pnode) : pnode;
  122. { moves src into dest, before doing so, right is set to nil and dest is freed.
  123. Because dest and src are var parameters, this can be done inline in an existing
  124. node tree }
  125. procedure replacenode(var dest,src : tnode);
  126. { strip off deref/addr combinations when looking for a the load node of an open array/array of const
  127. since there is no possiblity to defined a pointer to an open array/array of const, we have not to
  128. take care of type casts, further, it means also that deref/addr nodes must always appear coupled
  129. }
  130. function get_open_const_array(p : tnode) : tnode;
  131. { excludes the flags passed in nf from the node tree passed }
  132. procedure node_reset_flags(p : tnode;nf : TNodeFlags; tnf : TTransientNodeFlags);
  133. { include or exclude cs from p.localswitches }
  134. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  135. { returns true, if p is a node which shall be short boolean evaluated,
  136. if it is not an orn/andn with boolean operans, the result is undefined }
  137. function doshortbooleval(p : tnode) : Boolean;
  138. { returns true if the node has the int value l }
  139. function is_constintvalue(p : tnode;l : Tconstexprint) : Boolean;
  140. { if the node is a constant node which can be an int value, this value is returned }
  141. function get_int_value(p : tnode): Tconstexprint;
  142. { returns true if the node is an inline node of type i }
  143. function is_inlinefunction(p : tnode;i : tinlinenumber) : Boolean;
  144. { checks if p is a series of length(a) statments, if yes, they are returned
  145. in a and the function returns true }
  146. function GetStatements(p : tnode;var a : array of tstatementnode) : Boolean;
  147. { checks if p is a single statement, if yes, it is returned in s }
  148. function IsSingleStatement(p : tnode;var s : tnode) : Boolean;
  149. type
  150. TMatchProc2 = function(n1,n2 : tnode) : Boolean is nested;
  151. TTransformProc2 = function(n1,n2 : tnode) : tnode is nested;
  152. TMatchProc4 = function(n1,n2,n3,n4 : tnode) : Boolean is nested;
  153. TTransformProc4 = function(n1,n2,n3,n4 : tnode) : tnode is nested;
  154. { calls matchproc with n1 and n2 as parameters, if it returns true, transformproc is called, does the same with the nodes swapped,
  155. the result of transformproc is assigned to res }
  156. function MatchAndTransformNodesCommutative(n1,n2 : tnode;matchproc : TMatchProc2;transformproc : TTransformProc2;var res : tnode) : Boolean;
  157. { calls matchproc with n1, n2, n3 and n4 as parameters being considered as the leafs of commutative nodes so all 8 possible
  158. combinations are tested, if it returns true, transformproc is called,
  159. the result of transformproc is assigned to res
  160. this allows to find pattern like (3*a)+(3*b) and transfrom them into 3*(a+b)
  161. }
  162. function MatchAndTransformNodesCommutative(n1,n2,n3,n4 : tnode;matchproc : TMatchProc4;transformproc : TTransformProc4;var res : tnode) : Boolean;
  163. {
  164. resets all flags so that nf_write/nf_modify information is regenerated
  165. }
  166. procedure node_reset_pass1_write(n: tnode);
  167. { Returns True if n one of its children has a type that appears in TypeList }
  168. function has_node_of_type(n: TNode; TypeList: TNodeTypeSet): Boolean; {$IFDEF USEINLINE}inline;{$ENDIF USEINLINE}
  169. implementation
  170. uses
  171. cutils,verbose,globals,
  172. symconst,symdef,
  173. defcmp,defutil,
  174. ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,ninl,
  175. cpubase,cgbase,procinfo,
  176. pass_1;
  177. type
  178. ForEachNodeContext = object
  179. procmethod: tforeachprocmethod;
  180. f: staticforeachnodefunction;
  181. arg: pointer;
  182. res: boolean;
  183. procedure perform(var n: tnode);
  184. procedure process_children(n: tnode);
  185. procedure process_casenode(n: tcasenode);
  186. end;
  187. procedure ForEachNodeContext.perform(var n: tnode);
  188. var
  189. fr: foreachnoderesult;
  190. begin
  191. if not assigned(n) then
  192. exit;
  193. if procmethod=pm_preprocess then
  194. process_children(n);
  195. fr:=f(n,arg);
  196. res:=(fr in [fen_true, fen_norecurse_true]) or res;
  197. if fr in [fen_norecurse_false, fen_norecurse_true] then
  198. exit;
  199. if procmethod in [pm_postprocess,pm_postandagain] then
  200. begin
  201. process_children(n);
  202. if procmethod=pm_postandagain then
  203. begin
  204. fr:=f(n,arg);
  205. res:=(fr in [fen_true, fen_norecurse_true]) or res;
  206. end;
  207. end;
  208. end;
  209. procedure ForEachNodeContext.process_children(n: tnode);
  210. begin
  211. case n.nodetype of
  212. asn:
  213. if assigned(tasnode(n).call) then
  214. begin
  215. perform(tasnode(n).call);
  216. exit
  217. end;
  218. calln:
  219. begin
  220. perform(tnode(tcallnode(n).callinitblock));
  221. perform(tcallnode(n).methodpointer);
  222. perform(tcallnode(n).funcretnode);
  223. perform(tnode(tcallnode(n).vmt_entry));
  224. perform(tnode(tcallnode(n).callcleanupblock));
  225. end;
  226. callparan:
  227. begin
  228. perform(tnode(tcallparanode(n).fparainit));
  229. perform(tcallparanode(n).fparacopyback);
  230. end;
  231. ifn, whilerepeatn, forn, tryexceptn:
  232. begin
  233. perform(tloopnode(n).t1);
  234. perform(tloopnode(n).t2);
  235. end;
  236. raisen, tryfinallyn:
  237. { frame tree/copy of finally code }
  238. perform(ttertiarynode(n).third);
  239. tempcreaten:
  240. { temp. initialization code }
  241. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  242. perform(ttempcreatenode(n).tempinfo^.tempinitcode);
  243. casen:
  244. process_casenode(tcasenode(n));
  245. else
  246. ;
  247. end;
  248. if n.inheritsfrom(tbinarynode) then
  249. begin
  250. { first process the "payload" of statementnodes }
  251. perform(tbinarynode(n).left);
  252. perform(tbinarynode(n).right);
  253. end
  254. else if n.inheritsfrom(tunarynode) then
  255. perform(tunarynode(n).left);
  256. end;
  257. procedure ForEachNodeContext.process_casenode(n: tcasenode);
  258. var
  259. i: SizeInt;
  260. block: pointer;
  261. begin
  262. for i := 0 to n.blocks.count-1 do
  263. begin
  264. block := n.blocks[i];
  265. if assigned(block) then
  266. perform(pcaseblock(block)^.statement);
  267. end;
  268. perform(n.elseblock);
  269. end;
  270. { Adapts foreachnodefunction to staticforeachnodefunction. }
  271. type
  272. BoundToStaticForEachNodeContext = record
  273. f: foreachnodefunction;
  274. arg: pointer;
  275. end;
  276. function BoundToStaticForEachNodeAdapter(var n: tnode; arg: pointer): foreachnoderesult;
  277. var
  278. adaptCtx: ^BoundToStaticForEachNodeContext absolute arg;
  279. begin
  280. result := adaptCtx^.f(n, adaptCtx^.arg);
  281. end;
  282. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  283. var
  284. adaptCtx: BoundToStaticForEachNodeContext;
  285. begin
  286. adaptCtx.f := f;
  287. adaptCtx.arg := arg;
  288. result:=foreachnodestatic(procmethod,n,@BoundToStaticForEachNodeAdapter,@adaptCtx);
  289. end;
  290. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  291. begin
  292. result:=foreachnode(pm_postprocess,n,f,arg);
  293. end;
  294. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  295. var
  296. fen: ForEachNodeContext;
  297. begin
  298. fen.procmethod := procmethod;
  299. fen.f := f;
  300. fen.arg := arg;
  301. fen.res := false;
  302. fen.perform(n);
  303. result := fen.res;
  304. end;
  305. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  306. begin
  307. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  308. end;
  309. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  310. begin
  311. if not(n.nodetype in pnodetypeset(arg)^) then
  312. internalerror(200610141);
  313. result:=fen_true;
  314. end;
  315. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  316. begin
  317. foreachnodestatic(n,@do_check,@typeset);
  318. end;
  319. procedure load_procvar_from_calln(var p1:tnode);
  320. var
  321. p2 : tnode;
  322. begin
  323. if p1.nodetype<>calln then
  324. internalerror(200212251);
  325. { was it a procvar, then we simply remove the calln and
  326. reuse the right }
  327. if assigned(tcallnode(p1).right) then
  328. begin
  329. p2:=tcallnode(p1).right;
  330. tcallnode(p1).right:=nil;
  331. end
  332. else
  333. begin
  334. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  335. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  336. { when the methodpointer is typen we've something like:
  337. tobject.create. Then only the address is needed of the
  338. method without a self pointer }
  339. if assigned(tcallnode(p1).methodpointer) and
  340. (tcallnode(p1).methodpointer.nodetype<>typen) then
  341. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  342. end;
  343. typecheckpass(p2);
  344. p1.free;
  345. p1:=p2;
  346. end;
  347. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  348. var
  349. hp : tnode;
  350. begin
  351. result:=false;
  352. if not (p1.resultdef.typ in [procvardef,objectdef]) or
  353. (
  354. (p1.resultdef.typ=objectdef) and
  355. (
  356. not is_invokable(p1.resultdef) or
  357. (nf_load_procvar in p1.flags) or
  358. not (
  359. is_funcref(p1.resultdef) or
  360. invokable_has_argless_invoke(tobjectdef(p1.resultdef))
  361. )
  362. )
  363. ) or
  364. (tponly and
  365. not(m_tp_procvar in current_settings.modeswitches)) then
  366. exit;
  367. { ignore vecn,subscriptn }
  368. hp:=p1;
  369. repeat
  370. case hp.nodetype of
  371. vecn,
  372. derefn,
  373. typeconvn,
  374. subscriptn :
  375. hp:=tunarynode(hp).left;
  376. blockn:
  377. hp:=laststatement(tblocknode(hp)).left
  378. else
  379. break;
  380. end;
  381. until false;
  382. { a tempref is used when it is loaded from a withsymtable }
  383. if (hp.nodetype in [calln,loadn,temprefn]) then
  384. begin
  385. hp:=ccallnode.create_procvar(nil,p1);
  386. typecheckpass(hp);
  387. p1:=hp;
  388. result:=true;
  389. end;
  390. end;
  391. function get_local_or_para_sym(const aname: string): tabstractvarsym;
  392. var
  393. pd: tprocdef;
  394. ressym: tsym;
  395. begin
  396. ressym:=nil;
  397. result:=nil;
  398. { is not assigned while parsing a property }
  399. if not assigned(current_procinfo) then
  400. exit;
  401. { we can't use searchsym here, because the
  402. symtablestack is not fully setup when pass1
  403. is run for nested procedures }
  404. pd:=current_procinfo.procdef;
  405. repeat
  406. ressym:=tsym(pd.localst.Find(aname));
  407. if assigned(ressym) then
  408. break;
  409. ressym:=tsym(pd.parast.Find(aname));
  410. if assigned(ressym) then
  411. break;
  412. { try the parent of a nested function }
  413. if assigned(pd.owner.defowner) and
  414. (pd.owner.defowner.typ=procdef) then
  415. pd:=tprocdef(pd.owner.defowner)
  416. else
  417. break;
  418. until false;
  419. if assigned(ressym) and
  420. not(ressym.typ in [localvarsym,paravarsym]) then
  421. internalerror(2020122604);
  422. result:=tabstractvarsym(ressym);
  423. end;
  424. function load_high_value_node(vs:tparavarsym):tnode;
  425. begin
  426. result:=gen_load_var(get_high_value_sym(vs));
  427. typecheckpass(result);
  428. end;
  429. function load_self_node:tnode;
  430. begin
  431. result:=gen_load_var(get_local_or_para_sym('self'));
  432. if result.nodetype=loadn then
  433. include(tloadnode(result).loadnodeflags,loadnf_is_self)
  434. else if result.nodetype<>errorn then
  435. internalerror(2020122603);
  436. typecheckpass(result);
  437. end;
  438. function load_result_node:tnode;
  439. begin
  440. result:=gen_load_var(get_local_or_para_sym('result'));
  441. typecheckpass(result);
  442. end;
  443. function load_safecallresult_node: tnode;
  444. begin
  445. result:=gen_load_var(get_local_or_para_sym('safecallresult'));
  446. typecheckpass(result);
  447. end;
  448. function load_self_pointer_node:tnode;
  449. var
  450. srsym : tabstractvarsym;
  451. begin
  452. srsym:=get_local_or_para_sym('self');
  453. result:=gen_load_var(srsym);
  454. if assigned(srsym) and
  455. (is_object(tabstractvarsym(srsym).vardef) or is_record(tabstractvarsym(srsym).vardef)) then
  456. begin
  457. if result.nodetype=loadn then
  458. include(tloadnode(result).loadnodeflags,loadnf_load_addr)
  459. else if result.nodetype<>errorn then
  460. internalerror(2020122602);
  461. end;
  462. typecheckpass(result);
  463. end;
  464. function load_vmt_pointer_node:tnode;
  465. begin
  466. result:=gen_load_var(get_local_or_para_sym('vmt'));
  467. typecheckpass(result);
  468. end;
  469. function is_self_node(p:tnode):boolean;
  470. begin
  471. is_self_node:=(p.nodetype=loadn) and
  472. (tloadnode(p).symtableentry.typ=paravarsym) and
  473. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  474. end;
  475. function load_vmt_for_self_node(self_node: tnode): tnode;
  476. var
  477. self_resultdef: tdef;
  478. obj_def: tobjectdef;
  479. self_temp,
  480. vmt_temp: ttempcreatenode;
  481. check_self,n: tnode;
  482. stat: tstatementnode;
  483. block: tblocknode;
  484. paras: tcallparanode;
  485. docheck,is_typecasted_classref: boolean;
  486. begin
  487. self_resultdef:=self_node.resultdef;
  488. case self_resultdef.typ of
  489. classrefdef:
  490. begin
  491. obj_def:=tobjectdef(tclassrefdef(self_resultdef).pointeddef);
  492. end;
  493. objectdef:
  494. obj_def:=tobjectdef(self_resultdef);
  495. else
  496. internalerror(2015052701);
  497. end;
  498. n:=self_node;
  499. is_typecasted_classref:=false;
  500. if (n.nodetype=typeconvn) then
  501. begin
  502. while assigned(n) and (n.nodetype=typeconvn) and (nf_explicit in ttypeconvnode(n).flags) do
  503. n:=ttypeconvnode(n).left;
  504. if assigned(n) and (n.resultdef.typ=classrefdef) then
  505. is_typecasted_classref:=true;
  506. end;
  507. if is_classhelper(obj_def) then
  508. obj_def:=tobjectdef(tobjectdef(obj_def).extendeddef);
  509. docheck:=
  510. not(is_interface(obj_def)) and
  511. not(is_cppclass(obj_def)) and
  512. not(is_objc_class_or_protocol(obj_def)) and
  513. (([cs_check_object,cs_check_range]*current_settings.localswitches)<>[]);
  514. block:=nil;
  515. stat:=nil;
  516. self_temp:=nil;
  517. if docheck then
  518. begin
  519. { check for nil self-pointer }
  520. block:=internalstatements(stat);
  521. if is_object(self_resultdef) then
  522. begin
  523. self_temp:=ctempcreatenode.create_value(
  524. cpointerdef.getreusable(self_resultdef),cpointerdef.getreusable(self_resultdef).size,tt_persistent,true,
  525. caddrnode.create(self_node));
  526. end
  527. else
  528. self_temp:=ctempcreatenode.create_value(
  529. self_resultdef,self_resultdef.size,tt_persistent,true,
  530. self_node);
  531. addstatement(stat,self_temp);
  532. { in case of an object, self can only be nil if it's a dereferenced
  533. node somehow
  534. }
  535. if not is_object(self_resultdef) or
  536. (actualtargetnode(@self_node)^.nodetype=derefn) then
  537. begin
  538. check_self:=ctemprefnode.create(self_temp);
  539. addstatement(stat,cifnode.create(
  540. caddnode.create(equaln,
  541. ctypeconvnode.create_explicit(
  542. check_self,
  543. voidpointertype
  544. ),
  545. cnilnode.create),
  546. ccallnode.createintern('fpc_objecterror',nil),
  547. nil)
  548. );
  549. end;
  550. if is_object(self_resultdef) then
  551. self_node:=cderefnode.create(ctemprefnode.create(self_temp))
  552. else
  553. self_node:=ctemprefnode.create(self_temp)
  554. end;
  555. { in case of a classref, the "instance" is a pointer
  556. to pointer to a VMT and there is no vmt field }
  557. if is_typecasted_classref or (self_resultdef.typ=classrefdef) then
  558. result:=self_node
  559. { get the VMT field in case of a class/object }
  560. else if (self_resultdef.typ=objectdef) and
  561. assigned(tobjectdef(self_resultdef).vmt_field) then
  562. result:=csubscriptnode.create(tobjectdef(self_resultdef).vmt_field,self_node)
  563. { in case of an interface, the "instance" is a pointer to a pointer
  564. to a VMT -> dereference once already }
  565. else
  566. { in case of an interface/classref, the "instance" is a pointer
  567. to pointer to a VMT and there is no vmt field }
  568. result:=cderefnode.create(
  569. ctypeconvnode.create_explicit(
  570. self_node,
  571. cpointerdef.getreusable(voidpointertype)
  572. )
  573. );
  574. result:=ctypeconvnode.create_explicit(
  575. result,
  576. cpointerdef.getreusable(obj_def.vmt_def));
  577. typecheckpass(result);
  578. if docheck then
  579. begin
  580. { add a vmt validity check }
  581. vmt_temp:=ctempcreatenode.create_value(result.resultdef,result.resultdef.size,tt_persistent,true,result);
  582. addstatement(stat,vmt_temp);
  583. paras:=ccallparanode.create(ctemprefnode.create(vmt_temp),nil);
  584. if cs_check_object in current_settings.localswitches then
  585. begin
  586. paras:=ccallparanode.create(
  587. cloadvmtaddrnode.create(ctypenode.create(obj_def)),
  588. paras
  589. );
  590. addstatement(stat,
  591. ccallnode.createintern(
  592. 'fpc_check_object_ext',paras
  593. )
  594. );
  595. end
  596. else
  597. addstatement(stat,
  598. ccallnode.createintern(
  599. 'fpc_check_object',paras
  600. )
  601. );
  602. addstatement(stat,ctempdeletenode.create_normal_temp(vmt_temp));
  603. addstatement(stat,ctempdeletenode.create(self_temp));
  604. addstatement(stat,ctemprefnode.create(vmt_temp));
  605. result:=block;
  606. end
  607. end;
  608. { this function must return a very high value ("infinity") for }
  609. { trees containing a call, the rest can be balanced more or less }
  610. { at will, probably best mainly in terms of required memory }
  611. { accesses }
  612. function node_complexity(p: tnode): cardinal;
  613. var
  614. correction: byte;
  615. {$ifdef ARM}
  616. dummy : byte;
  617. {$endif ARM}
  618. begin
  619. result := 0;
  620. while assigned(p) do
  621. begin
  622. case p.nodetype of
  623. { floating point constants usually need loading from memory }
  624. realconstn:
  625. begin
  626. result:=2;
  627. exit;
  628. end;
  629. rttin,
  630. setconstn,
  631. stringconstn,
  632. temprefn,
  633. loadvmtaddrn,
  634. { main reason for the next one: we can't take the address of }
  635. { loadparentfpnode, so replacing it by a temp which is the }
  636. { address of this node's location and then dereferencing }
  637. { doesn't work. If changed, check whether webtbs/tw0935 }
  638. { still works with nodeinlining (JM) }
  639. loadparentfpn:
  640. begin
  641. result := 1;
  642. exit;
  643. end;
  644. loadn:
  645. begin
  646. if assigned(tloadnode(p).left) then
  647. inc(result,node_complexity(tloadnode(p).left));
  648. { threadvars need a helper call }
  649. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  650. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  651. inc(result,5)
  652. else if not((tloadnode(p).symtableentry.typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  653. (tabstractvarsym(tloadnode(p).symtableentry).varregable in [vr_intreg,vr_mmreg,vr_fpureg])) then
  654. inc(result);
  655. if (tloadnode(p).symtableentry.typ=paravarsym) and
  656. not(tabstractvarsym(tloadnode(p).symtableentry).varregable=vr_addr) and
  657. tloadnode(p).is_addr_param_load then
  658. inc(result);
  659. if (result >= NODE_COMPLEXITY_INF) then
  660. result := NODE_COMPLEXITY_INF;
  661. exit;
  662. end;
  663. subscriptn:
  664. begin
  665. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  666. is_bitpacked_access(p) then
  667. inc(result,2)
  668. { non-packed, int. regable records cause no extra
  669. overhead no overhead if the fields are aligned to register boundaries }
  670. else if tstoreddef(p.resultdef).is_intregable and (tsubscriptnode(p).vs.fieldoffset mod sizeof(aint)<>0) then
  671. inc(result,1);
  672. if (result = NODE_COMPLEXITY_INF) then
  673. exit;
  674. p := tunarynode(p).left;
  675. end;
  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. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1302. begin
  1303. if PDWord(arg)^>0 then
  1304. begin
  1305. dec(PDWord(arg)^);
  1306. result:=fen_false;
  1307. end
  1308. else
  1309. result:=fen_norecurse_false;
  1310. end;
  1311. function node_count(node : tnode; max : dword = High(dword)) : dword;
  1312. var
  1313. left : dword;
  1314. begin
  1315. left:=max;
  1316. foreachnodestatic(node,@donodecount,@left);
  1317. result:=max-left;
  1318. end;
  1319. function donodecount_weighted(var n: tnode; arg: pointer): foreachnoderesult;
  1320. begin
  1321. if PDWord(arg)^>0 then
  1322. begin
  1323. if not(n.nodetype in [blockn,statementn,callparan,nothingn]) then
  1324. dec(PDWord(arg)^);
  1325. result:=fen_false;
  1326. end
  1327. else
  1328. result:=fen_norecurse_false;
  1329. end;
  1330. function node_count_weighted(node : tnode; max : dword = High(dword)) : dword;
  1331. var
  1332. left : dword;
  1333. begin
  1334. left:=max;
  1335. foreachnodestatic(node,@donodecount_weighted,@left);
  1336. result:=max-left;
  1337. end;
  1338. function is_const(node : tnode) : boolean;
  1339. begin
  1340. result:=is_constnode(node) or
  1341. ((node.nodetype=temprefn) and (ti_const in ttemprefnode(node).tempflags)) or
  1342. ((node.nodetype=loadn) and (tloadnode(node).symtableentry.typ=paravarsym) and (tparavarsym(tloadnode(node).symtableentry).varspez in [vs_const,vs_constref]));
  1343. end;
  1344. function actualtargetnode(n : pnode) : pnode;
  1345. begin
  1346. result:=n;
  1347. case n^.nodetype of
  1348. typeconvn:
  1349. if ttypeconvnode(n^).retains_value_location then
  1350. result:=actualtargetnode(@ttypeconvnode(n^).left);
  1351. else
  1352. ;
  1353. end;
  1354. end;
  1355. procedure replacenode(var dest,src : tnode);
  1356. var
  1357. t : tnode;
  1358. begin
  1359. t:=src;
  1360. { set src nil before free'ing dest because
  1361. src could be part of dest }
  1362. src:=nil;
  1363. dest.Free;
  1364. dest:=t;
  1365. end;
  1366. function get_open_const_array(p : tnode) : tnode;
  1367. begin
  1368. result:=p;
  1369. if (p.nodetype=derefn) and (tderefnode(p).left.nodetype=addrn) then
  1370. result:=get_open_const_array(taddrnode(tderefnode(result).left).left);
  1371. end;
  1372. type
  1373. TFlagSet = record
  1374. nf : TNodeFlags;
  1375. tnf : TTransientNodeFlags;
  1376. end;
  1377. function do_node_reset_flags(var n: tnode; arg: pointer): foreachnoderesult;
  1378. begin
  1379. result:=fen_false;
  1380. n.flags:=n.flags-TFlagSet(arg^).nf;
  1381. n.transientflags:=n.transientflags-TFlagSet(arg^).tnf;
  1382. end;
  1383. procedure node_reset_flags(p : tnode; nf : TNodeFlags; tnf : TTransientNodeFlags);
  1384. var
  1385. FlagSet: TFlagSet;
  1386. begin
  1387. FlagSet.nf:=nf;
  1388. FlagSet.tnf:=tnf;
  1389. foreachnodestatic(p,@do_node_reset_flags,@FlagSet);
  1390. end;
  1391. type
  1392. tlocalswitchchange = record
  1393. cs : tlocalswitch;
  1394. enable : boolean;
  1395. end;
  1396. plocalswitchchange = ^tlocalswitchchange;
  1397. function do_change_local_settings(var p : tnode;plsc : pointer) : foreachnoderesult;
  1398. begin
  1399. if plocalswitchchange(plsc)^.enable then
  1400. include(p.localswitches, plocalswitchchange(plsc)^.cs)
  1401. else
  1402. exclude(p.localswitches, plocalswitchchange(plsc)^.cs);
  1403. result:=fen_true;
  1404. end;
  1405. procedure node_change_local_switch(p : tnode;cs : tlocalswitch;enable : boolean);
  1406. var
  1407. lsc : tlocalswitchchange;
  1408. begin
  1409. lsc.cs:=cs;
  1410. lsc.enable:=enable;
  1411. foreachnodestatic(p,@do_change_local_settings,@lsc);
  1412. end;
  1413. function doshortbooleval(p : tnode) : Boolean;
  1414. begin
  1415. Result:=(p.nodetype in [orn,andn]) and ((anf_short_bool in taddnode(p).addnodeflags) or not(cs_full_boolean_eval in p.localswitches));
  1416. end;
  1417. function is_constintvalue(p: tnode; l: Tconstexprint): Boolean;
  1418. begin
  1419. Result:=is_constintnode(p) and (tordconstnode(p).value=l);
  1420. end;
  1421. function get_int_value(p: tnode): Tconstexprint;
  1422. begin
  1423. case p.nodetype of
  1424. ordconstn:
  1425. result:=tordconstnode(p).value;
  1426. pointerconstn:
  1427. result:=tpointerconstnode(p).value;
  1428. niln:
  1429. result:=0;
  1430. else
  1431. internalerror(2002080202);
  1432. end;
  1433. end;
  1434. function is_inlinefunction(p: tnode; i: tinlinenumber): Boolean;
  1435. begin
  1436. Result:=(p.nodetype=inlinen) and (tinlinenode(p).inlinenumber=i);
  1437. end;
  1438. { checks if p is a series of length(a) statments, if yes, they are returned
  1439. in a and the function returns true }
  1440. function GetStatements(p : tnode;var a : array of tstatementnode) : Boolean;
  1441. var
  1442. i: Integer;
  1443. begin
  1444. Result:=false;
  1445. for i:=0 to high(a) do
  1446. begin
  1447. if not(assigned(p)) or not(p.nodetype=statementn) then
  1448. exit;
  1449. a[i]:=tstatementnode(p);
  1450. p:=tstatementnode(p).right;
  1451. end;
  1452. Result:=true;
  1453. end;
  1454. function IsSingleStatement(p: tnode; var s: tnode): Boolean;
  1455. begin
  1456. Result:=false;
  1457. if assigned(p) then
  1458. case p.nodetype of
  1459. blockn:
  1460. Result:=IsSingleStatement(tblocknode(p).statements,s);
  1461. statementn:
  1462. if not(assigned(tstatementnode(p).next)) then
  1463. begin
  1464. Result:=true;
  1465. s:=tstatementnode(p).statement;
  1466. end;
  1467. inlinen,
  1468. assignn,
  1469. calln:
  1470. begin
  1471. s:=p;
  1472. Result:=true;
  1473. end
  1474. else
  1475. ;
  1476. end;
  1477. end;
  1478. function MatchAndTransformNodesCommutative(n1,n2 : tnode;matchproc : TMatchProc2;transformproc : TTransformProc2;var res : tnode) : Boolean;
  1479. begin
  1480. res:=nil;
  1481. result:=true;
  1482. if matchproc(n1,n2) then
  1483. res:=transformproc(n1,n2)
  1484. else if matchproc(n2,n1) then
  1485. res:=transformproc(n2,n1)
  1486. else
  1487. result:=false;
  1488. end;
  1489. function MatchAndTransformNodesCommutative(n1,n2,n3,n4 : tnode;matchproc : TMatchProc4;transformproc : TTransformProc4;var res : tnode) : Boolean;
  1490. begin
  1491. res:=nil;
  1492. result:=true;
  1493. if matchproc(n1,n2,n3,n4) then
  1494. res:=transformproc(n1,n2,n3,n4)
  1495. else if matchproc(n1,n2,n4,n3) then
  1496. res:=transformproc(n1,n2,n4,n3)
  1497. else if matchproc(n2,n1,n3,n4) then
  1498. res:=transformproc(n2,n1,n3,n4)
  1499. else if matchproc(n2,n1,n4,n3) then
  1500. res:=transformproc(n2,n1,n4,n3)
  1501. else if matchproc(n3,n4,n1,n2) then
  1502. res:=transformproc(n3,n4,n1,n2)
  1503. else if matchproc(n4,n3,n1,n2) then
  1504. res:=transformproc(n4,n3,n1,n2)
  1505. else if matchproc(n3,n4,n2,n1) then
  1506. res:=transformproc(n3,n4,n2,n1)
  1507. else if matchproc(n4,n3,n2,n1) then
  1508. res:=transformproc(n4,n3,n2,n1)
  1509. else
  1510. result:=false;
  1511. end;
  1512. function _node_reset_pass1_write(var n: tnode; arg: pointer): foreachnoderesult;
  1513. begin
  1514. Result := fen_false;
  1515. n.flags := n.flags - [nf_write,nf_modify];
  1516. n.transientflags := n.transientflags - [tnf_pass1_done];
  1517. if n.nodetype = assignn then
  1518. begin
  1519. { Force re-evaluation of assignments so nf_modify and nf_write
  1520. flags are correctly set. }
  1521. n.resultdef := nil;
  1522. Result := fen_true;
  1523. end;
  1524. end;
  1525. procedure node_reset_pass1_write(n: tnode);
  1526. begin
  1527. foreachnodestatic(n,@_node_reset_pass1_write,nil);
  1528. end;
  1529. function node_in_list(var n: tnode; arg: pointer): foreachnoderesult;
  1530. begin
  1531. if (n.nodetype in PNodeTypeSet(arg)^) then
  1532. result := fen_norecurse_true
  1533. else
  1534. result := fen_false;
  1535. end;
  1536. function has_node_of_type(n: TNode; TypeList: TNodeTypeSet): Boolean;
  1537. begin
  1538. Result := foreachnodestatic(n, @node_in_list, @TypeList);
  1539. end;
  1540. end.