nutils.pas 54 KB

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