nutils.pas 57 KB

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