nutils.pas 38 KB

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