nutils.pas 60 KB

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