pass_1.pas 8.5 KB

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