nutils.pas 37 KB

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