nutils.pas 52 KB

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