nutils.pas 38 KB

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