nutils.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  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. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  79. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  80. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  81. { checks whether sym is a static field and if so, translates the access
  82. to the appropriate node tree }
  83. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  84. { returns true if n is an array element access of a bitpacked array with
  85. elements of the which the vitsize mod 8 <> 0, or if is a field access
  86. with bitsize mod 8 <> 0 or bitoffset mod 8 <> 0 of an element in a
  87. bitpacked structure }
  88. function is_bitpacked_access(n: tnode): boolean;
  89. { creates a load of field 'fieldname' in the record/class/...
  90. represented by n }
  91. function genloadfield(n: tnode; const fieldname: string): tnode;
  92. { returns true, if the tree given might have side effects }
  93. function might_have_sideeffects(n : tnode) : boolean;
  94. { count the number of nodes in the node tree,
  95. rough estimation how large the tree "node" is }
  96. function node_count(node : tnode) : dword;
  97. implementation
  98. uses
  99. cutils,verbose,globals,
  100. symconst,symdef,
  101. defutil,defcmp,
  102. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,ninl,
  103. cpubase,cgbase,procinfo,
  104. pass_1;
  105. function foreachnode(procmethod : tforeachprocmethod;var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  106. function process_children(res : boolean) : boolean;
  107. var
  108. i: longint;
  109. begin
  110. result:=res;
  111. case n.nodetype of
  112. asn:
  113. if assigned(tasnode(n).call) then
  114. begin
  115. result := foreachnode(procmethod,tasnode(n).call,f,arg);
  116. exit
  117. end;
  118. calln:
  119. begin
  120. result := foreachnode(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  121. result := foreachnode(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  122. result := foreachnode(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  123. result := foreachnode(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  124. end;
  125. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  126. begin
  127. { not in one statement, won't work because of b- }
  128. result := foreachnode(procmethod,tloopnode(n).t1,f,arg) or result;
  129. result := foreachnode(procmethod,tloopnode(n).t2,f,arg) or result;
  130. end;
  131. raisen:
  132. { frame tree }
  133. result := foreachnode(ttertiarynode(n).third,f,arg) or result;
  134. tempcreaten:
  135. { temp. initialization code }
  136. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  137. result := foreachnode(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  138. casen:
  139. begin
  140. for i := 0 to tcasenode(n).blocks.count-1 do
  141. if assigned(tcasenode(n).blocks[i]) then
  142. result := foreachnode(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  143. result := foreachnode(procmethod,tcasenode(n).elseblock,f,arg) or result;
  144. end;
  145. end;
  146. if n.inheritsfrom(tbinarynode) then
  147. begin
  148. { first process the "payload" of statementnodes }
  149. result := foreachnode(procmethod,tbinarynode(n).left,f,arg) or result;
  150. result := foreachnode(procmethod,tbinarynode(n).right,f,arg) or result;
  151. end
  152. else if n.inheritsfrom(tunarynode) then
  153. result := foreachnode(procmethod,tunarynode(n).left,f,arg) or result;
  154. end;
  155. begin
  156. result := false;
  157. if not assigned(n) then
  158. exit;
  159. if procmethod=pm_preprocess then
  160. result:=process_children(result);
  161. case f(n,arg) of
  162. fen_norecurse_false:
  163. exit;
  164. fen_norecurse_true:
  165. begin
  166. result := true;
  167. exit;
  168. end;
  169. fen_true:
  170. result := true;
  171. { result is already false
  172. fen_false:
  173. result := false; }
  174. end;
  175. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  176. result:=process_children(result);
  177. if procmethod=pm_postandagain then
  178. begin
  179. case f(n,arg) of
  180. fen_norecurse_false:
  181. exit;
  182. fen_norecurse_true:
  183. begin
  184. result := true;
  185. exit;
  186. end;
  187. fen_true:
  188. result := true;
  189. end;
  190. end;
  191. end;
  192. function foreachnode(var n: tnode; f: foreachnodefunction; arg: pointer): boolean;
  193. begin
  194. result:=foreachnode(pm_postprocess,n,f,arg);
  195. end;
  196. function foreachnodestatic(procmethod : tforeachprocmethod;var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  197. function process_children(res : boolean) : boolean;
  198. var
  199. i: longint;
  200. begin
  201. result:=res;
  202. case n.nodetype of
  203. asn:
  204. if assigned(tasnode(n).call) then
  205. begin
  206. result := foreachnodestatic(procmethod,tasnode(n).call,f,arg);
  207. exit
  208. end;
  209. calln:
  210. begin
  211. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callinitblock),f,arg) or result;
  212. result := foreachnodestatic(procmethod,tcallnode(n).methodpointer,f,arg) or result;
  213. result := foreachnodestatic(procmethod,tcallnode(n).funcretnode,f,arg) or result;
  214. result := foreachnodestatic(procmethod,tnode(tcallnode(n).callcleanupblock),f,arg) or result;
  215. end;
  216. ifn, whilerepeatn, forn, tryexceptn, tryfinallyn:
  217. begin
  218. { not in one statement, won't work because of b- }
  219. result := foreachnodestatic(procmethod,tloopnode(n).t1,f,arg) or result;
  220. result := foreachnodestatic(procmethod,tloopnode(n).t2,f,arg) or result;
  221. end;
  222. raisen:
  223. { frame tree }
  224. result := foreachnodestatic(ttertiarynode(n).third,f,arg) or result;
  225. tempcreaten:
  226. { temp. initialization code }
  227. if assigned(ttempcreatenode(n).tempinfo^.tempinitcode) then
  228. result := foreachnodestatic(ttempcreatenode(n).tempinfo^.tempinitcode,f,arg) or result;
  229. casen:
  230. begin
  231. for i := 0 to tcasenode(n).blocks.count-1 do
  232. if assigned(tcasenode(n).blocks[i]) then
  233. result := foreachnodestatic(procmethod,pcaseblock(tcasenode(n).blocks[i])^.statement,f,arg) or result;
  234. result := foreachnodestatic(procmethod,tcasenode(n).elseblock,f,arg) or result;
  235. end;
  236. end;
  237. if n.inheritsfrom(tbinarynode) then
  238. begin
  239. { first process the "payload" of statementnodes }
  240. result := foreachnodestatic(procmethod,tbinarynode(n).left,f,arg) or result;
  241. result := foreachnodestatic(procmethod,tbinarynode(n).right,f,arg) or result;
  242. end
  243. else if n.inheritsfrom(tunarynode) then
  244. result := foreachnodestatic(procmethod,tunarynode(n).left,f,arg) or result;
  245. end;
  246. begin
  247. result := false;
  248. if not assigned(n) then
  249. exit;
  250. if procmethod=pm_preprocess then
  251. result:=process_children(result);
  252. case f(n,arg) of
  253. fen_norecurse_false:
  254. exit;
  255. fen_norecurse_true:
  256. begin
  257. result := true;
  258. exit;
  259. end;
  260. fen_true:
  261. result := true;
  262. { result is already false
  263. fen_false:
  264. result := false; }
  265. end;
  266. if (procmethod=pm_postprocess) or (procmethod=pm_postandagain) then
  267. result:=process_children(result);
  268. if procmethod=pm_postandagain then
  269. begin
  270. case f(n,arg) of
  271. fen_norecurse_false:
  272. exit;
  273. fen_norecurse_true:
  274. begin
  275. result := true;
  276. exit;
  277. end;
  278. fen_true:
  279. result := true;
  280. end;
  281. end;
  282. end;
  283. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction; arg: pointer): boolean;
  284. begin
  285. result:=foreachnodestatic(pm_postprocess,n,f,arg);
  286. end;
  287. function do_check(var n: tnode; arg: pointer): foreachnoderesult;
  288. begin
  289. if not(n.nodetype in pnodetypeset(arg)^) then
  290. internalerror(200610141);
  291. result:=fen_true;
  292. end;
  293. procedure checktreenodetypes(n : tnode;typeset : tnodetypeset);
  294. begin
  295. foreachnodestatic(n,@do_check,@typeset);
  296. end;
  297. procedure load_procvar_from_calln(var p1:tnode);
  298. var
  299. p2 : tnode;
  300. begin
  301. if p1.nodetype<>calln then
  302. internalerror(200212251);
  303. { was it a procvar, then we simply remove the calln and
  304. reuse the right }
  305. if assigned(tcallnode(p1).right) then
  306. begin
  307. p2:=tcallnode(p1).right;
  308. tcallnode(p1).right:=nil;
  309. end
  310. else
  311. begin
  312. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  313. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  314. { when the methodpointer is typen we've something like:
  315. tobject.create. Then only the address is needed of the
  316. method without a self pointer }
  317. if assigned(tcallnode(p1).methodpointer) and
  318. (tcallnode(p1).methodpointer.nodetype<>typen) then
  319. tloadnode(p2).set_mp(tcallnode(p1).methodpointer.getcopy);
  320. end;
  321. typecheckpass(p2);
  322. p1.free;
  323. p1:=p2;
  324. end;
  325. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  326. var
  327. hp : tnode;
  328. begin
  329. result:=false;
  330. if (p1.resultdef.typ<>procvardef) or
  331. (tponly and
  332. not(m_tp_procvar in current_settings.modeswitches)) then
  333. exit;
  334. { ignore vecn,subscriptn }
  335. hp:=p1;
  336. repeat
  337. case hp.nodetype of
  338. vecn,
  339. derefn,
  340. typeconvn,
  341. subscriptn :
  342. hp:=tunarynode(hp).left;
  343. else
  344. break;
  345. end;
  346. until false;
  347. { a tempref is used when it is loaded from a withsymtable }
  348. if (hp.nodetype in [calln,loadn,temprefn]) then
  349. begin
  350. hp:=ccallnode.create_procvar(nil,p1);
  351. typecheckpass(hp);
  352. p1:=hp;
  353. result:=true;
  354. end;
  355. end;
  356. function get_local_or_para_sym(const aname:string):tsym;
  357. var
  358. pd : tprocdef;
  359. begin
  360. result:=nil;
  361. { is not assigned while parsing a property }
  362. if not assigned(current_procinfo) then
  363. exit;
  364. { we can't use searchsym here, because the
  365. symtablestack is not fully setup when pass1
  366. is run for nested procedures }
  367. pd:=current_procinfo.procdef;
  368. repeat
  369. result := tsym(pd.localst.Find(aname));
  370. if assigned(result) then
  371. break;
  372. result := tsym(pd.parast.Find(aname));
  373. if assigned(result) then
  374. break;
  375. { try the parent of a nested function }
  376. if assigned(pd.owner.defowner) and
  377. (pd.owner.defowner.typ=procdef) then
  378. pd:=tprocdef(pd.owner.defowner)
  379. else
  380. break;
  381. until false;
  382. end;
  383. function load_high_value_node(vs:tparavarsym):tnode;
  384. var
  385. srsym : tsym;
  386. begin
  387. result:=nil;
  388. srsym:=get_high_value_sym(vs);
  389. if assigned(srsym) then
  390. begin
  391. result:=cloadnode.create(srsym,vs.owner);
  392. typecheckpass(result);
  393. end
  394. else
  395. CGMessage(parser_e_illegal_expression);
  396. end;
  397. function load_self_node:tnode;
  398. var
  399. srsym : tsym;
  400. begin
  401. result:=nil;
  402. srsym:=get_local_or_para_sym('self');
  403. if assigned(srsym) then
  404. begin
  405. result:=cloadnode.create(srsym,srsym.owner);
  406. include(tloadnode(result).loadnodeflags,loadnf_is_self);
  407. end
  408. else
  409. begin
  410. result:=cerrornode.create;
  411. CGMessage(parser_e_illegal_expression);
  412. end;
  413. typecheckpass(result);
  414. end;
  415. function load_result_node:tnode;
  416. var
  417. srsym : tsym;
  418. begin
  419. result:=nil;
  420. srsym:=get_local_or_para_sym('result');
  421. if assigned(srsym) then
  422. result:=cloadnode.create(srsym,srsym.owner)
  423. else
  424. begin
  425. result:=cerrornode.create;
  426. CGMessage(parser_e_illegal_expression);
  427. end;
  428. typecheckpass(result);
  429. end;
  430. function load_self_pointer_node:tnode;
  431. var
  432. srsym : tsym;
  433. begin
  434. result:=nil;
  435. srsym:=get_local_or_para_sym('self');
  436. if assigned(srsym) then
  437. begin
  438. result:=cloadnode.create(srsym,srsym.owner);
  439. include(tloadnode(result).loadnodeflags,loadnf_load_self_pointer);
  440. end
  441. else
  442. begin
  443. result:=cerrornode.create;
  444. CGMessage(parser_e_illegal_expression);
  445. end;
  446. typecheckpass(result);
  447. end;
  448. function load_vmt_pointer_node:tnode;
  449. var
  450. srsym : tsym;
  451. begin
  452. result:=nil;
  453. srsym:=get_local_or_para_sym('vmt');
  454. if assigned(srsym) then
  455. result:=cloadnode.create(srsym,srsym.owner)
  456. else
  457. begin
  458. result:=cerrornode.create;
  459. CGMessage(parser_e_illegal_expression);
  460. end;
  461. typecheckpass(result);
  462. end;
  463. function is_self_node(p:tnode):boolean;
  464. begin
  465. is_self_node:=(p.nodetype=loadn) and
  466. (tloadnode(p).symtableentry.typ=paravarsym) and
  467. (vo_is_self in tparavarsym(tloadnode(p).symtableentry).varoptions);
  468. end;
  469. { this function must return a very high value ("infinity") for }
  470. { trees containing a call, the rest can be balanced more or less }
  471. { at will, probably best mainly in terms of required memory }
  472. { accesses }
  473. function node_complexity(p: tnode): cardinal;
  474. var
  475. correction: byte;
  476. {$ifdef ARM}
  477. dummy : byte;
  478. {$endif ARM}
  479. begin
  480. result := 0;
  481. while assigned(p) do
  482. begin
  483. case p.nodetype of
  484. { floating point constants usually need loading from memory }
  485. realconstn:
  486. begin
  487. result:=2;
  488. exit;
  489. end;
  490. setconstn,
  491. stringconstn,
  492. temprefn,
  493. loadvmtaddrn,
  494. { main reason for the next one: we can't take the address of }
  495. { loadparentfpnode, so replacing it by a temp which is the }
  496. { address of this node's location and then dereferencing }
  497. { doesn't work. If changed, check whether webtbs/tw0935 }
  498. { still works with nodeinlining (JM) }
  499. loadparentfpn:
  500. begin
  501. result := 1;
  502. exit;
  503. end;
  504. loadn:
  505. begin
  506. if assigned(tloadnode(p).left) then
  507. inc(result,node_complexity(tloadnode(p).left));
  508. { threadvars need a helper call }
  509. if (tloadnode(p).symtableentry.typ=staticvarsym) and
  510. (vo_is_thread_var in tstaticvarsym(tloadnode(p).symtableentry).varoptions) then
  511. inc(result,5)
  512. else
  513. inc(result);
  514. if (tloadnode(p).symtableentry.typ=paravarsym) and tloadnode(p).is_addr_param_load then
  515. inc(result);
  516. if (result >= NODE_COMPLEXITY_INF) then
  517. result := NODE_COMPLEXITY_INF;
  518. exit;
  519. end;
  520. subscriptn:
  521. begin
  522. if is_implicit_pointer_object_type(tunarynode(p).left.resultdef) or
  523. is_bitpacked_access(p) then
  524. inc(result,2)
  525. else if tstoreddef(p.resultdef).is_intregable then
  526. inc(result,1);
  527. if (result = NODE_COMPLEXITY_INF) then
  528. exit;
  529. p := tunarynode(p).left;
  530. end;
  531. blockn,
  532. callparan:
  533. p := tunarynode(p).left;
  534. notn,
  535. derefn :
  536. begin
  537. inc(result);
  538. if (result = NODE_COMPLEXITY_INF) then
  539. exit;
  540. p := tunarynode(p).left;
  541. end;
  542. typeconvn:
  543. begin
  544. { may be more complex in some cases }
  545. if not(ttypeconvnode(p).retains_value_location) then
  546. inc(result);
  547. if (result = NODE_COMPLEXITY_INF) then
  548. exit;
  549. p := tunarynode(p).left;
  550. end;
  551. vecn,
  552. statementn:
  553. begin
  554. inc(result,node_complexity(tbinarynode(p).left));
  555. if (result >= NODE_COMPLEXITY_INF) then
  556. begin
  557. result := NODE_COMPLEXITY_INF;
  558. exit;
  559. end;
  560. p := tbinarynode(p).right;
  561. end;
  562. addn,subn,orn,andn,xorn,muln,divn,modn,symdifn,
  563. shln,shrn,
  564. equaln,unequaln,gtn,gten,ltn,lten,
  565. assignn:
  566. begin
  567. {$ifdef CPU64BITALU}
  568. correction:=1;
  569. {$else CPU64BITALU}
  570. correction:=2;
  571. {$endif CPU64BITALU}
  572. inc(result,node_complexity(tbinarynode(p).left)+1*correction);
  573. if (p.nodetype in [muln,divn,modn]) then
  574. inc(result,5*correction*correction);
  575. if (result >= NODE_COMPLEXITY_INF) then
  576. begin
  577. result := NODE_COMPLEXITY_INF;
  578. exit;
  579. end;
  580. p := tbinarynode(p).right;
  581. end;
  582. ordconstn:
  583. begin
  584. {$ifdef ARM}
  585. if not(is_shifter_const(tordconstnode(p).value.svalue,dummy)) then
  586. result:=2;
  587. {$endif ARM}
  588. exit;
  589. end;
  590. tempcreaten,
  591. tempdeleten,
  592. pointerconstn,
  593. nothingn,
  594. niln:
  595. exit;
  596. inlinen:
  597. begin
  598. { this code assumes that the inline node has }
  599. { already been firstpassed, and consequently }
  600. { that inline nodes which are transformed into }
  601. { calls already have been transformed }
  602. case tinlinenode(p).inlinenumber of
  603. in_lo_qword,
  604. in_hi_qword,
  605. in_lo_long,
  606. in_hi_long,
  607. in_lo_word,
  608. in_hi_word,
  609. in_length_x,
  610. in_assigned_x,
  611. in_pred_x,
  612. in_succ_x,
  613. in_round_real,
  614. in_trunc_real,
  615. in_int_real,
  616. in_frac_real,
  617. in_cos_real,
  618. in_sin_real,
  619. in_arctan_real,
  620. in_pi_real,
  621. in_abs_real,
  622. in_sqr_real,
  623. in_sqrt_real,
  624. in_ln_real,
  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. function getpropaccesslist(propsym:tpropertysym; pap:tpropaccesslisttypes;out propaccesslist:tpropaccesslist):boolean;
  812. var
  813. hpropsym : tpropertysym;
  814. begin
  815. result:=false;
  816. { find property in the overridden list }
  817. hpropsym:=propsym;
  818. repeat
  819. propaccesslist:=hpropsym.propaccesslist[pap];
  820. if not propaccesslist.empty then
  821. begin
  822. result:=true;
  823. exit;
  824. end;
  825. hpropsym:=hpropsym.overriddenpropsym;
  826. until not assigned(hpropsym);
  827. end;
  828. procedure propaccesslist_to_node(var p1:tnode;st:TSymtable;pl:tpropaccesslist);
  829. var
  830. plist : ppropaccesslistitem;
  831. begin
  832. plist:=pl.firstsym;
  833. while assigned(plist) do
  834. begin
  835. case plist^.sltype of
  836. sl_load :
  837. begin
  838. addsymref(plist^.sym);
  839. if not assigned(st) then
  840. st:=plist^.sym.owner;
  841. if (plist^.sym.typ<>staticvarsym) then
  842. begin
  843. { p1 can already contain the loadnode of
  844. the class variable. When there is no tree yet we
  845. may need to load it for with or objects }
  846. if not assigned(p1) then
  847. begin
  848. case st.symtabletype of
  849. withsymtable :
  850. p1:=tnode(twithsymtable(st).withrefnode).getcopy;
  851. ObjectSymtable :
  852. p1:=load_self_node;
  853. end;
  854. end
  855. end
  856. else
  857. begin
  858. p1.free;
  859. p1:=nil;
  860. end;
  861. if assigned(p1) then
  862. p1:=csubscriptnode.create(plist^.sym,p1)
  863. else
  864. p1:=cloadnode.create(plist^.sym,st);
  865. end;
  866. sl_subscript :
  867. begin
  868. addsymref(plist^.sym);
  869. p1:=csubscriptnode.create(plist^.sym,p1);
  870. end;
  871. sl_typeconv :
  872. p1:=ctypeconvnode.create_explicit(p1,plist^.def);
  873. sl_absolutetype :
  874. begin
  875. p1:=ctypeconvnode.create(p1,plist^.def);
  876. include(p1.flags,nf_absolute);
  877. end;
  878. sl_vec :
  879. p1:=cvecnode.create(p1,cordconstnode.create(plist^.value,plist^.valuedef,true));
  880. else
  881. internalerror(200110205);
  882. end;
  883. plist:=plist^.next;
  884. end;
  885. end;
  886. function node_to_propaccesslist(p1:tnode):tpropaccesslist;
  887. var
  888. sl : tpropaccesslist;
  889. procedure addnode(p:tnode);
  890. begin
  891. case p.nodetype of
  892. subscriptn :
  893. begin
  894. addnode(tsubscriptnode(p).left);
  895. sl.addsym(sl_subscript,tsubscriptnode(p).vs);
  896. end;
  897. typeconvn :
  898. begin
  899. addnode(ttypeconvnode(p).left);
  900. if nf_absolute in ttypeconvnode(p).flags then
  901. sl.addtype(sl_absolutetype,ttypeconvnode(p).totypedef)
  902. else
  903. sl.addtype(sl_typeconv,ttypeconvnode(p).totypedef);
  904. end;
  905. vecn :
  906. begin
  907. addnode(tvecnode(p).left);
  908. if tvecnode(p).right.nodetype=ordconstn then
  909. sl.addconst(sl_vec,tordconstnode(tvecnode(p).right).value,tvecnode(p).right.resultdef)
  910. else
  911. begin
  912. Message(parser_e_illegal_expression);
  913. { recovery }
  914. sl.addconst(sl_vec,0,tvecnode(p).right.resultdef);
  915. end;
  916. end;
  917. loadn :
  918. sl.addsym(sl_load,tloadnode(p).symtableentry);
  919. else
  920. internalerror(200310282);
  921. end;
  922. end;
  923. begin
  924. sl:=tpropaccesslist.create;
  925. addnode(p1);
  926. result:=sl;
  927. end;
  928. function handle_staticfield_access(sym: tsym; nested: boolean; var p1: tnode): boolean;
  929. var
  930. static_name: shortstring;
  931. srsymtable: tsymtable;
  932. begin
  933. result:=false;
  934. { generate access code }
  935. if (sp_static in sym.symoptions) then
  936. begin
  937. result:=true;
  938. if not nested then
  939. static_name:=lower(sym.owner.name^)+'_'+sym.name
  940. else
  941. static_name:=lower(generate_nested_name(sym.owner,'_'))+'_'+sym.name;
  942. if sym.owner.defowner.typ=objectdef then
  943. searchsym_in_class(tobjectdef(sym.owner.defowner),tobjectdef(sym.owner.defowner),static_name,sym,srsymtable,true)
  944. else
  945. searchsym_in_record(trecorddef(sym.owner.defowner),static_name,sym,srsymtable);
  946. if assigned(sym) then
  947. check_hints(sym,sym.symoptions,sym.deprecatedmsg);
  948. p1.free;
  949. p1:=nil;
  950. { static syms are always stored as absolutevarsym to handle scope and storage properly }
  951. propaccesslist_to_node(p1,nil,tabsolutevarsym(sym).ref);
  952. end;
  953. end;
  954. function is_bitpacked_access(n: tnode): boolean;
  955. begin
  956. case n.nodetype of
  957. vecn:
  958. result:=
  959. is_packed_array(tvecnode(n).left.resultdef) and
  960. { only orddefs and enumdefs are actually bitpacked. Don't consider
  961. e.g. an access to a 3-byte record as "bitpacked", since it
  962. isn't }
  963. (tvecnode(n).left.resultdef.typ in [orddef,enumdef]) and
  964. not(tarraydef(tvecnode(n).left.resultdef).elepackedbitsize in [8,16,32,64]);
  965. subscriptn:
  966. result:=
  967. is_packed_record_or_object(tsubscriptnode(n).left.resultdef) and
  968. { see above }
  969. (tsubscriptnode(n).vs.vardef.typ in [orddef,enumdef]) and
  970. (not(tsubscriptnode(n).vs.vardef.packedbitsize in [8,16,32,64]) or
  971. (tsubscriptnode(n).vs.fieldoffset mod 8 <> 0));
  972. else
  973. result:=false;
  974. end;
  975. end;
  976. function genloadfield(n: tnode; const fieldname: string): tnode;
  977. var
  978. vs : tsym;
  979. begin
  980. if not assigned(n.resultdef) then
  981. typecheckpass(n);
  982. vs:=tsym(tabstractrecorddef(n.resultdef).symtable.find(fieldname));
  983. if not assigned(vs) or
  984. (vs.typ<>fieldvarsym) then
  985. internalerror(2010061902);
  986. result:=csubscriptnode.create(vs,n);
  987. end;
  988. function has_no_code(n : tnode) : boolean;
  989. begin
  990. if n=nil then
  991. begin
  992. result:=true;
  993. exit;
  994. end;
  995. result:=false;
  996. case n.nodetype of
  997. nothingn:
  998. begin
  999. result:=true;
  1000. exit;
  1001. end;
  1002. blockn:
  1003. begin
  1004. result:=has_no_code(tblocknode(n).left);
  1005. exit;
  1006. end;
  1007. statementn:
  1008. begin
  1009. repeat
  1010. result:=has_no_code(tstatementnode(n).left);
  1011. n:=tstatementnode(n).right;
  1012. until not(result) or not assigned(n);
  1013. exit;
  1014. end;
  1015. end;
  1016. end;
  1017. function check_for_sideeffect(var n: tnode; arg: pointer): foreachnoderesult;
  1018. begin
  1019. result:=fen_false;
  1020. if (n.nodetype in [assignn,calln,asmn]) or
  1021. ((n.nodetype=inlinen) and
  1022. (tinlinenode(n).inlinenumber in [in_write_x,in_writeln_x,in_read_x,in_readln_x,in_str_x_string,
  1023. in_val_x,in_reset_x,in_rewrite_x,in_reset_typedfile,in_rewrite_typedfile,in_settextbuf_file_x,
  1024. in_inc_x,in_dec_x,in_include_x_y,in_exclude_x_y,in_break,in_continue,in_setlength_x,
  1025. in_finalize_x,in_new_x,in_dispose_x,in_exit,in_copy_x,in_initialize_x,in_leave,in_cycle])
  1026. ) then
  1027. result:=fen_norecurse_true;
  1028. end;
  1029. function might_have_sideeffects(n : tnode) : boolean;
  1030. begin
  1031. result:=foreachnodestatic(n,@check_for_sideeffect,nil);
  1032. end;
  1033. var
  1034. nodecount : dword;
  1035. function donodecount(var n: tnode; arg: pointer): foreachnoderesult;
  1036. begin
  1037. inc(nodecount);
  1038. result:=fen_false;
  1039. end;
  1040. { rough estimation how large the tree "node" is }
  1041. function node_count(node : tnode) : dword;
  1042. begin
  1043. nodecount:=0;
  1044. foreachnodestatic(node,@donodecount,nil);
  1045. result:=nodecount;
  1046. end;
  1047. end.