nutils.pas 58 KB

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