nutils.pas 62 KB

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