nutils.pas 53 KB

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