nutils.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. 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. function call_fail_node:tnode;
  40. function initialize_data_node(p:tnode):tnode;
  41. function finalize_data_node(p:tnode):tnode;
  42. implementation
  43. uses
  44. verbose,
  45. symconst,symsym,symtype,symdef,symtable,
  46. nbas,ncon,ncnv,nld,nflw,nset,ncal,nadd,nmem,
  47. pass_1;
  48. function foreachnode(var n: tnode; f: foreachnodefunction): boolean;
  49. begin
  50. result := false;
  51. if not assigned(n) then
  52. exit;
  53. case f(n) of
  54. fen_norecurse_false:
  55. exit;
  56. fen_norecurse_true:
  57. begin
  58. result := true;
  59. exit;
  60. end;
  61. fen_true:
  62. result := true;
  63. { result is already false
  64. fen_false:
  65. result := false; }
  66. end;
  67. case n.nodetype of
  68. calln:
  69. begin
  70. { not in one statement, won't work because of b- }
  71. result := foreachnode(tcallnode(n).methodpointer,f) or result;
  72. result := foreachnode(tcallnode(n).inlinecode,f) or result;
  73. end;
  74. ifn, whilerepeatn, forn:
  75. begin
  76. { not in one statement, won't work because of b- }
  77. result := foreachnode(tloopnode(n).t1,f) or result;
  78. result := foreachnode(tloopnode(n).t2,f) or result;
  79. end;
  80. raisen:
  81. result := foreachnode(traisenode(n).frametree,f) or result;
  82. casen:
  83. result := foreachnode(tcasenode(n). elseblock,f) or result;
  84. end;
  85. if n.inheritsfrom(tbinarynode) then
  86. begin
  87. result := foreachnode(tbinarynode(n).right,f) or result;
  88. result := foreachnode(tbinarynode(n).left,f) or result;
  89. end
  90. else if n.inheritsfrom(tunarynode) then
  91. result := foreachnode(tunarynode(n).left,f) or result;
  92. end;
  93. function foreachnodestatic(var n: tnode; f: staticforeachnodefunction): boolean;
  94. begin
  95. result := false;
  96. if not assigned(n) then
  97. exit;
  98. case f(n) of
  99. fen_norecurse_false:
  100. exit;
  101. fen_norecurse_true:
  102. begin
  103. result := true;
  104. exit;
  105. end;
  106. fen_true:
  107. result := true;
  108. { result is already false
  109. fen_false:
  110. result := false; }
  111. end;
  112. case n.nodetype of
  113. calln:
  114. begin
  115. result := foreachnodestatic(tcallnode(n).methodpointer,f) or result;
  116. result := foreachnodestatic(tcallnode(n).inlinecode,f) or result;
  117. end;
  118. ifn, whilerepeatn, forn:
  119. begin
  120. { not in one statement, won't work because of b- }
  121. result := foreachnodestatic(tloopnode(n).t1,f) or result;
  122. result := foreachnodestatic(tloopnode(n).t2,f) or result;
  123. end;
  124. raisen:
  125. result := foreachnodestatic(traisenode(n).frametree,f) or result;
  126. casen:
  127. result := foreachnodestatic(tcasenode(n). elseblock,f) or result;
  128. end;
  129. if n.inheritsfrom(tbinarynode) then
  130. begin
  131. result := foreachnodestatic(tbinarynode(n).right,f) or result;
  132. result := foreachnodestatic(tbinarynode(n).left,f) or result;
  133. end
  134. else if n.inheritsfrom(tunarynode) then
  135. result := foreachnodestatic(tunarynode(n).left,f) or result;
  136. end;
  137. function call_fail_node:tnode;
  138. var
  139. para : tcallparanode;
  140. newstatement : tstatementnode;
  141. srsym : tsym;
  142. begin
  143. result:=internalstatements(newstatement,true);
  144. { call fail helper and exit normal }
  145. if is_class(current_procdef._class) then
  146. begin
  147. srsym:=search_class_member(current_procdef._class,'FREEINSTANCE');
  148. if assigned(srsym) and
  149. (srsym.typ=procsym) then
  150. begin
  151. { if self<>0 and vmt=1 then freeinstance }
  152. addstatement(newstatement,cifnode.create(
  153. caddnode.create(andn,
  154. caddnode.create(unequaln,
  155. load_self_pointer_node,
  156. cnilnode.create),
  157. caddnode.create(equaln,
  158. ctypeconvnode.create(
  159. load_vmt_pointer_node,
  160. voidpointertype),
  161. cpointerconstnode.create(1,voidpointertype))),
  162. ccallnode.create(nil,tprocsym(srsym),srsym.owner,load_self_node),
  163. nil));
  164. end
  165. else
  166. internalerror(200305108);
  167. end
  168. else
  169. if is_object(current_procdef._class) then
  170. begin
  171. { parameter 3 : vmt_offset }
  172. { parameter 2 : pointer to vmt }
  173. { parameter 1 : self pointer }
  174. para:=ccallparanode.create(
  175. cordconstnode.create(current_procdef._class.vmt_offset,s32bittype,false),
  176. ccallparanode.create(
  177. ctypeconvnode.create_explicit(
  178. load_vmt_pointer_node,
  179. voidpointertype),
  180. ccallparanode.create(
  181. ctypeconvnode.create_explicit(
  182. load_self_pointer_node,
  183. voidpointertype),
  184. nil)));
  185. addstatement(newstatement,
  186. ccallnode.createintern('fpc_help_fail',para));
  187. end
  188. else
  189. internalerror(200305132);
  190. { self:=nil }
  191. addstatement(newstatement,cassignmentnode.create(
  192. load_self_pointer_node,
  193. cnilnode.create));
  194. { exit }
  195. addstatement(newstatement,cexitnode.create(nil));
  196. end;
  197. function initialize_data_node(p:tnode):tnode;
  198. begin
  199. if not assigned(p.resulttype.def) then
  200. resulttypepass(p);
  201. result:=ccallnode.createintern('fpc_initialize',
  202. ccallparanode.create(
  203. caddrnode.create(
  204. crttinode.create(
  205. tstoreddef(p.resulttype.def),initrtti)),
  206. ccallparanode.create(
  207. caddrnode.create(p),
  208. nil)));
  209. end;
  210. function finalize_data_node(p:tnode):tnode;
  211. begin
  212. if not assigned(p.resulttype.def) then
  213. resulttypepass(p);
  214. result:=ccallnode.createintern('fpc_finalize',
  215. ccallparanode.create(
  216. caddrnode.create(
  217. crttinode.create(
  218. tstoreddef(p.resulttype.def),initrtti)),
  219. ccallparanode.create(
  220. caddrnode.create(p),
  221. nil)));
  222. end;
  223. end.
  224. {
  225. $Log$
  226. Revision 1.5 2003-05-26 21:17:17 peter
  227. * procinlinenode removed
  228. * aktexit2label removed, fast exit removed
  229. + tcallnode.inlined_pass_2 added
  230. Revision 1.4 2003/05/16 14:33:31 peter
  231. * regvar fixes
  232. Revision 1.3 2003/05/13 20:54:06 peter
  233. * fail checks vmt value before calling dispose
  234. Revision 1.2 2003/05/13 19:14:41 peter
  235. * failn removed
  236. * inherited result code check moven to pexpr
  237. Revision 1.1 2003/04/23 12:35:34 florian
  238. * fixed several issues with powerpc
  239. + applied a patch from Jonas for nested function calls (PowerPC only)
  240. * ...
  241. }