nutils.pas 38 KB

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