nutils.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Type checking and register allocation for inline nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit nutils;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. symsym,node;
  23. type
  24. { resulttype of functions that process on all nodes in a (sub)tree }
  25. foreachnoderesult = (
  26. { false, continue recursion }
  27. fen_false,
  28. { false, stop recursion }
  29. fen_norecurse_false,
  30. { true, continue recursion }
  31. fen_true,
  32. { true, stop recursion }
  33. fen_norecurse_true
  34. );
  35. foreachnodefunction = function(var n: tnode): foreachnoderesult of object;
  36. staticforeachnodefunction = function(var n: tnode): foreachnoderesult;
  37. function foreachnode(var n: tnode; f: foreachnodefunction): boolean;
  38. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction): boolean;
  39. procedure load_procvar_from_calln(var p1:tnode);
  40. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  41. function load_high_value_node(vs:tvarsym):tnode;
  42. function load_self_node:tnode;
  43. function load_result_node:tnode;
  44. function load_self_pointer_node:tnode;
  45. function load_vmt_pointer_node:tnode;
  46. function is_self_node(p:tnode):boolean;
  47. function call_fail_node:tnode;
  48. function initialize_data_node(p:tnode):tnode;
  49. function finalize_data_node(p:tnode):tnode;
  50. implementation
  51. uses
  52. globtype,globals,verbose,
  53. symconst,symbase,symtype,symdef,symtable,
  54. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,
  55. cgbase,procinfo,
  56. pass_1;
  57. function foreachnode(var n: tnode; f: foreachnodefunction): boolean;
  58. begin
  59. result := false;
  60. if not assigned(n) then
  61. exit;
  62. case f(n) of
  63. fen_norecurse_false:
  64. exit;
  65. fen_norecurse_true:
  66. begin
  67. result := true;
  68. exit;
  69. end;
  70. fen_true:
  71. result := true;
  72. { result is already false
  73. fen_false:
  74. result := false; }
  75. end;
  76. case n.nodetype of
  77. calln:
  78. begin
  79. { not in one statement, won't work because of b- }
  80. result := foreachnode(tcallnode(n).methodpointer,f) or result;
  81. result := foreachnode(tcallnode(n).inlinecode,f) or result;
  82. end;
  83. ifn, whilerepeatn, forn:
  84. begin
  85. { not in one statement, won't work because of b- }
  86. result := foreachnode(tloopnode(n).t1,f) or result;
  87. result := foreachnode(tloopnode(n).t2,f) or result;
  88. end;
  89. raisen:
  90. result := foreachnode(traisenode(n).frametree,f) or result;
  91. casen:
  92. result := foreachnode(tcasenode(n). elseblock,f) or result;
  93. end;
  94. if n.inheritsfrom(tbinarynode) then
  95. begin
  96. result := foreachnode(tbinarynode(n).right,f) or result;
  97. result := foreachnode(tbinarynode(n).left,f) or result;
  98. end
  99. else if n.inheritsfrom(tunarynode) then
  100. result := foreachnode(tunarynode(n).left,f) or result;
  101. end;
  102. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction): boolean;
  103. begin
  104. result := false;
  105. if not assigned(n) then
  106. exit;
  107. case f(n) of
  108. fen_norecurse_false:
  109. exit;
  110. fen_norecurse_true:
  111. begin
  112. result := true;
  113. exit;
  114. end;
  115. fen_true:
  116. result := true;
  117. { result is already false
  118. fen_false:
  119. result := false; }
  120. end;
  121. case n.nodetype of
  122. calln:
  123. begin
  124. result := foreachnodestatic(tcallnode(n).methodpointer,f) or result;
  125. result := foreachnodestatic(tcallnode(n).inlinecode,f) or result;
  126. end;
  127. ifn, whilerepeatn, forn:
  128. begin
  129. { not in one statement, won't work because of b- }
  130. result := foreachnodestatic(tloopnode(n).t1,f) or result;
  131. result := foreachnodestatic(tloopnode(n).t2,f) or result;
  132. end;
  133. raisen:
  134. result := foreachnodestatic(traisenode(n).frametree,f) or result;
  135. casen:
  136. result := foreachnodestatic(tcasenode(n). elseblock,f) or result;
  137. end;
  138. if n.inheritsfrom(tbinarynode) then
  139. begin
  140. result := foreachnodestatic(tbinarynode(n).right,f) or result;
  141. result := foreachnodestatic(tbinarynode(n).left,f) or result;
  142. end
  143. else if n.inheritsfrom(tunarynode) then
  144. result := foreachnodestatic(tunarynode(n).left,f) or result;
  145. end;
  146. procedure load_procvar_from_calln(var p1:tnode);
  147. var
  148. p2 : tnode;
  149. begin
  150. if p1.nodetype<>calln then
  151. internalerror(200212251);
  152. { was it a procvar, then we simply remove the calln and
  153. reuse the right }
  154. if assigned(tcallnode(p1).right) then
  155. begin
  156. p2:=tcallnode(p1).right;
  157. tcallnode(p1).right:=nil;
  158. end
  159. else
  160. begin
  161. p2:=cloadnode.create_procvar(tcallnode(p1).symtableprocentry,
  162. tprocdef(tcallnode(p1).procdefinition),tcallnode(p1).symtableproc);
  163. { when the methodpointer is typen we've something like:
  164. tobject.create. Then only the address is needed of the
  165. method without a self pointer }
  166. if assigned(tcallnode(p1).methodpointer) and
  167. (tcallnode(p1).methodpointer.nodetype<>typen) then
  168. begin
  169. tloadnode(p2).set_mp(tcallnode(p1).methodpointer);
  170. tcallnode(p1).methodpointer:=nil;
  171. end;
  172. end;
  173. resulttypepass(p2);
  174. p1.free;
  175. p1:=p2;
  176. end;
  177. function maybe_call_procvar(var p1:tnode;tponly:boolean):boolean;
  178. var
  179. hp : tnode;
  180. begin
  181. result:=false;
  182. if (p1.resulttype.def.deftype<>procvardef) or
  183. (tponly and
  184. not(m_tp_procvar in aktmodeswitches)) then
  185. exit;
  186. { ignore vecn,subscriptn }
  187. hp:=p1;
  188. repeat
  189. case hp.nodetype of
  190. vecn,
  191. derefn,
  192. typeconvn,
  193. subscriptn :
  194. hp:=tunarynode(hp).left;
  195. else
  196. break;
  197. end;
  198. until false;
  199. if (hp.nodetype=loadn) then
  200. begin
  201. hp:=ccallnode.create_procvar(nil,p1);
  202. resulttypepass(hp);
  203. p1:=hp;
  204. result:=true;
  205. end;
  206. end;
  207. function load_high_value_node(vs:tvarsym):tnode;
  208. var
  209. srsym : tsym;
  210. srsymtable : tsymtable;
  211. begin
  212. result:=nil;
  213. srsymtable:=vs.owner;
  214. srsym:=searchsymonlyin(srsymtable,'high'+vs.name);
  215. if assigned(srsym) then
  216. begin
  217. result:=cloadnode.create(srsym,srsymtable);
  218. resulttypepass(result);
  219. end
  220. else
  221. CGMessage(cg_e_illegal_expression);
  222. end;
  223. function load_self_node:tnode;
  224. var
  225. srsym : tsym;
  226. srsymtable : tsymtable;
  227. begin
  228. result:=nil;
  229. searchsym('self',srsym,srsymtable);
  230. if assigned(srsym) then
  231. begin
  232. result:=cloadnode.create(srsym,srsymtable);
  233. resulttypepass(result);
  234. end
  235. else
  236. CGMessage(cg_e_illegal_expression);
  237. end;
  238. function load_result_node:tnode;
  239. var
  240. srsym : tsym;
  241. srsymtable : tsymtable;
  242. begin
  243. result:=nil;
  244. searchsym('result',srsym,srsymtable);
  245. if assigned(srsym) then
  246. begin
  247. result:=cloadnode.create(srsym,srsymtable);
  248. resulttypepass(result);
  249. end
  250. else
  251. CGMessage(cg_e_illegal_expression);
  252. end;
  253. function load_self_pointer_node:tnode;
  254. var
  255. srsym : tsym;
  256. srsymtable : tsymtable;
  257. begin
  258. result:=nil;
  259. searchsym('self',srsym,srsymtable);
  260. if assigned(srsym) then
  261. begin
  262. result:=cloadnode.create(srsym,srsymtable);
  263. include(result.flags,nf_load_self_pointer);
  264. resulttypepass(result);
  265. end
  266. else
  267. CGMessage(cg_e_illegal_expression);
  268. end;
  269. function load_vmt_pointer_node:tnode;
  270. var
  271. srsym : tsym;
  272. srsymtable : tsymtable;
  273. begin
  274. result:=nil;
  275. searchsym('vmt',srsym,srsymtable);
  276. if assigned(srsym) then
  277. begin
  278. result:=cloadnode.create(srsym,srsymtable);
  279. resulttypepass(result);
  280. end
  281. else
  282. CGMessage(cg_e_illegal_expression);
  283. end;
  284. function is_self_node(p:tnode):boolean;
  285. begin
  286. is_self_node:=(p.nodetype=loadn) and
  287. (tloadnode(p).symtableentry.typ=varsym) and
  288. (vo_is_self in tvarsym(tloadnode(p).symtableentry).varoptions);
  289. end;
  290. function call_fail_node:tnode;
  291. var
  292. para : tcallparanode;
  293. newstatement : tstatementnode;
  294. srsym : tsym;
  295. begin
  296. result:=internalstatements(newstatement);
  297. { call fail helper and exit normal }
  298. if is_class(current_procinfo.procdef._class) then
  299. begin
  300. srsym:=search_class_member(current_procinfo.procdef._class,'FREEINSTANCE');
  301. if assigned(srsym) and
  302. (srsym.typ=procsym) then
  303. begin
  304. { if self<>0 and vmt=1 then freeinstance }
  305. addstatement(newstatement,cifnode.create(
  306. caddnode.create(andn,
  307. caddnode.create(unequaln,
  308. load_self_pointer_node,
  309. cnilnode.create),
  310. caddnode.create(equaln,
  311. ctypeconvnode.create(
  312. load_vmt_pointer_node,
  313. voidpointertype),
  314. cpointerconstnode.create(1,voidpointertype))),
  315. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  316. nil));
  317. end
  318. else
  319. internalerror(200305108);
  320. end
  321. else
  322. if is_object(current_procinfo.procdef._class) then
  323. begin
  324. { parameter 3 : vmt_offset }
  325. { parameter 2 : pointer to vmt }
  326. { parameter 1 : self pointer }
  327. para:=ccallparanode.create(
  328. cordconstnode.create(current_procinfo.procdef._class.vmt_offset,s32inttype,false),
  329. ccallparanode.create(
  330. ctypeconvnode.create_explicit(
  331. load_vmt_pointer_node,
  332. voidpointertype),
  333. ccallparanode.create(
  334. ctypeconvnode.create_explicit(
  335. load_self_pointer_node,
  336. voidpointertype),
  337. nil)));
  338. addstatement(newstatement,
  339. ccallnode.createintern('fpc_help_fail',para));
  340. end
  341. else
  342. internalerror(200305132);
  343. { self:=nil }
  344. addstatement(newstatement,cassignmentnode.create(
  345. load_self_pointer_node,
  346. cnilnode.create));
  347. { exit }
  348. addstatement(newstatement,cexitnode.create(nil));
  349. end;
  350. function initialize_data_node(p:tnode):tnode;
  351. begin
  352. if not assigned(p.resulttype.def) then
  353. resulttypepass(p);
  354. result:=ccallnode.createintern('fpc_initialize',
  355. ccallparanode.create(
  356. caddrnode.create(
  357. crttinode.create(
  358. tstoreddef(p.resulttype.def),initrtti)),
  359. ccallparanode.create(
  360. caddrnode.create(p),
  361. nil)));
  362. end;
  363. function finalize_data_node(p:tnode):tnode;
  364. begin
  365. if not assigned(p.resulttype.def) then
  366. resulttypepass(p);
  367. result:=ccallnode.createintern('fpc_finalize',
  368. ccallparanode.create(
  369. caddrnode.create(
  370. crttinode.create(
  371. tstoreddef(p.resulttype.def),initrtti)),
  372. ccallparanode.create(
  373. caddrnode.create(p),
  374. nil)));
  375. end;
  376. end.
  377. {
  378. $Log$
  379. Revision 1.10 2004-02-20 21:55:59 peter
  380. * procvar cleanup
  381. Revision 1.9 2004/02/03 22:32:54 peter
  382. * renamed xNNbittype to xNNinttype
  383. * renamed registers32 to registersint
  384. * replace some s32bit,u32bit with torddef([su]inttype).def.typ
  385. Revision 1.8 2003/11/10 22:02:52 peter
  386. * cross unit inlining fixed
  387. Revision 1.7 2003/10/01 20:34:49 peter
  388. * procinfo unit contains tprocinfo
  389. * cginfo renamed to cgbase
  390. * moved cgmessage to verbose
  391. * fixed ppc and sparc compiles
  392. Revision 1.6 2003/06/13 21:19:30 peter
  393. * current_procdef removed, use current_procinfo.procdef instead
  394. Revision 1.5 2003/05/26 21:17:17 peter
  395. * procinlinenode removed
  396. * aktexit2label removed, fast exit removed
  397. + tcallnode.inlined_pass_2 added
  398. Revision 1.4 2003/05/16 14:33:31 peter
  399. * regvar fixes
  400. Revision 1.3 2003/05/13 20:54:06 peter
  401. * fail checks vmt value before calling dispose
  402. Revision 1.2 2003/05/13 19:14:41 peter
  403. * failn removed
  404. * inherited result code check moven to pexpr
  405. Revision 1.1 2003/04/23 12:35:34 florian
  406. * fixed several issues with powerpc
  407. + applied a patch from Jonas for nested function calls (PowerPC only)
  408. * ...
  409. }