nutils.pas 51 KB

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