nutils.pas 38 KB

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