nutils.pas 52 KB

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