nutils.pas 62 KB

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