pass_1.pas 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit handles the typecheck and node conversion pass
  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 pass_1;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. var
  24. resulttypepasscnt,
  25. multiresulttypepasscnt : longint;
  26. procedure resulttypepass(var p : tnode);
  27. function do_resulttypepass(var p : tnode) : boolean;
  28. procedure firstpass(var p : tnode);
  29. function do_firstpass(var p : tnode) : boolean;
  30. implementation
  31. uses
  32. globtype,systems,
  33. cutils,globals,
  34. cgbase,symdef,
  35. {$ifdef extdebug}
  36. htypechk,
  37. {$endif extdebug}
  38. tgobj
  39. ;
  40. {*****************************************************************************
  41. Global procedures
  42. *****************************************************************************}
  43. procedure resulttypepass(var p : tnode);
  44. var
  45. oldcodegenerror : boolean;
  46. oldlocalswitches : tlocalswitches;
  47. oldpos : tfileposinfo;
  48. hp : tnode;
  49. begin
  50. inc(resulttypepasscnt);
  51. if (p.resulttype.def=nil) then
  52. begin
  53. oldcodegenerror:=codegenerror;
  54. oldpos:=aktfilepos;
  55. oldlocalswitches:=aktlocalswitches;
  56. codegenerror:=false;
  57. aktfilepos:=p.fileinfo;
  58. aktlocalswitches:=p.localswitches;
  59. hp:=p.det_resulttype;
  60. { should the node be replaced? }
  61. if assigned(hp) then
  62. begin
  63. p.free;
  64. { run resulttypepass }
  65. resulttypepass(hp);
  66. { switch to new node }
  67. p:=hp;
  68. end;
  69. aktlocalswitches:=oldlocalswitches;
  70. aktfilepos:=oldpos;
  71. if codegenerror then
  72. begin
  73. include(p.flags,nf_error);
  74. { default to errortype if no type is set yet }
  75. if p.resulttype.def=nil then
  76. p.resulttype:=generrortype;
  77. end;
  78. codegenerror:=codegenerror or oldcodegenerror;
  79. end
  80. else
  81. begin
  82. { update the codegenerror boolean with the previous result of this node }
  83. if (nf_error in p.flags) then
  84. codegenerror:=true;
  85. inc(multiresulttypepasscnt);
  86. end;
  87. end;
  88. function do_resulttypepass(var p : tnode) : boolean;
  89. begin
  90. codegenerror:=false;
  91. resulttypepass(p);
  92. do_resulttypepass:=codegenerror;
  93. end;
  94. procedure firstpass(var p : tnode);
  95. var
  96. oldcodegenerror : boolean;
  97. oldlocalswitches : tlocalswitches;
  98. oldpos : tfileposinfo;
  99. hp : tnode;
  100. begin
  101. {$ifdef extdebug}
  102. inc(total_of_firstpass);
  103. {$endif extdebug}
  104. oldcodegenerror:=codegenerror;
  105. oldpos:=aktfilepos;
  106. oldlocalswitches:=aktlocalswitches;
  107. {$ifdef extdebug}
  108. if p.firstpasscount>0 then
  109. inc(firstpass_several);
  110. {$endif extdebug}
  111. if not(nf_error in p.flags) then
  112. begin
  113. codegenerror:=false;
  114. aktfilepos:=p.fileinfo;
  115. aktlocalswitches:=p.localswitches;
  116. { determine the resulttype if not done }
  117. if (p.resulttype.def=nil) then
  118. begin
  119. aktfilepos:=p.fileinfo;
  120. aktlocalswitches:=p.localswitches;
  121. hp:=p.det_resulttype;
  122. { should the node be replaced? }
  123. if assigned(hp) then
  124. begin
  125. p.free;
  126. { run resulttypepass }
  127. resulttypepass(hp);
  128. { switch to new node }
  129. p:=hp;
  130. end;
  131. if codegenerror then
  132. begin
  133. include(p.flags,nf_error);
  134. { default to errortype if no type is set yet }
  135. if p.resulttype.def=nil then
  136. p.resulttype:=generrortype;
  137. end;
  138. aktlocalswitches:=oldlocalswitches;
  139. aktfilepos:=oldpos;
  140. codegenerror:=codegenerror or oldcodegenerror;
  141. end;
  142. { first pass }
  143. if not(nf_error in p.flags) then
  144. begin
  145. aktfilepos:=p.fileinfo;
  146. aktlocalswitches:=p.localswitches;
  147. hp:=p.pass_1;
  148. { should the node be replaced? }
  149. if assigned(hp) then
  150. begin
  151. p.free;
  152. p:=hp;
  153. end;
  154. if codegenerror then
  155. include(p.flags,nf_error);
  156. end;
  157. codegenerror:=codegenerror or oldcodegenerror;
  158. aktlocalswitches:=oldlocalswitches;
  159. aktfilepos:=oldpos;
  160. end
  161. else
  162. codegenerror:=true;
  163. {$ifdef extdebug}
  164. if count_ref then
  165. inc(p.firstpasscount);
  166. {$endif extdebug}
  167. end;
  168. function do_firstpass(var p : tnode) : boolean;
  169. begin
  170. codegenerror:=false;
  171. firstpass(p);
  172. do_firstpass:=codegenerror;
  173. end;
  174. end.
  175. {
  176. $Log$
  177. Revision 1.22 2002-05-16 19:46:42 carl
  178. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  179. + try to fix temp allocation (still in ifdef)
  180. + generic constructor calls
  181. + start of tassembler / tmodulebase class cleanup
  182. Revision 1.20 2002/04/04 19:06:00 peter
  183. * removed unused units
  184. * use tlocation.size in cg.a_*loc*() routines
  185. Revision 1.19 2002/03/31 20:26:35 jonas
  186. + a_loadfpu_* and a_loadmm_* methods in tcg
  187. * register allocation is now handled by a class and is mostly processor
  188. independent (+rgobj.pas and i386/rgcpu.pas)
  189. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  190. * some small improvements and fixes to the optimizer
  191. * some register allocation fixes
  192. * some fpuvaroffset fixes in the unary minus node
  193. * push/popusedregisters is now called rg.save/restoreusedregisters and
  194. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  195. also better optimizable)
  196. * fixed and optimized register saving/restoring for new/dispose nodes
  197. * LOC_FPU locations now also require their "register" field to be set to
  198. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  199. - list field removed of the tnode class because it's not used currently
  200. and can cause hard-to-find bugs
  201. Revision 1.18 2001/10/20 17:23:43 peter
  202. * fixed firstpass when det_resulttype returns an error
  203. Revision 1.17 2001/09/02 21:18:28 peter
  204. * split constsym.value in valueord,valueordptr,valueptr. The valueordptr
  205. is used for holding target platform pointer values. As those can be
  206. bigger than the source platform.
  207. Revision 1.16 2001/08/26 13:36:44 florian
  208. * some cg reorganisation
  209. * some PPC updates
  210. Revision 1.15 2001/07/06 15:29:39 peter
  211. * fixed EXTDEBUG
  212. Revision 1.14 2001/04/15 09:48:30 peter
  213. * fixed crash in labelnode
  214. * easier detection of goto and label in try blocks
  215. Revision 1.13 2001/04/13 01:22:10 peter
  216. * symtable change to classes
  217. * range check generation and errors fixed, make cycle DEBUG=1 works
  218. * memory leaks fixed
  219. Revision 1.12 2001/04/02 21:20:31 peter
  220. * resulttype rewrite
  221. Revision 1.11 2000/12/18 21:56:52 peter
  222. * extdebug fixes
  223. Revision 1.10 2000/11/29 00:30:35 florian
  224. * unused units removed from uses clause
  225. * some changes for widestrings
  226. Revision 1.9 2000/10/14 10:14:51 peter
  227. * moehrendorf oct 2000 rewrite
  228. Revision 1.8 2000/10/01 19:48:25 peter
  229. * lot of compile updates for cg11
  230. Revision 1.7 2000/09/30 16:08:45 peter
  231. * more cg11 updates
  232. Revision 1.6 2000/09/28 19:49:52 florian
  233. *** empty log message ***
  234. Revision 1.5 2000/09/24 21:15:34 florian
  235. * some errors fix to get more stuff compilable
  236. Revision 1.4 2000/09/24 15:06:21 peter
  237. * use defines.inc
  238. Revision 1.3 2000/09/19 23:09:07 pierre
  239. * problems wih extdebug cond. solved
  240. Revision 1.2 2000/07/13 11:32:44 michael
  241. + removed logs
  242. }