nutils.pas 37 KB

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