nutils.pas 60 KB

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