nutils.pas 43 KB

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