nutils.pas 43 KB

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