nutils.pas 41 KB

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