nutils.pas 52 KB

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