pass_2.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit handles the codegeneration 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_2;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. tenumflowcontrol = (fc_exit,fc_break,fc_continue);
  25. tflowcontrol = set of tenumflowcontrol;
  26. var
  27. allow_multi_pass2 : boolean;
  28. flowcontrol : tflowcontrol;
  29. { produces the actual code }
  30. function do_secondpass(var p : tnode) : boolean;
  31. procedure secondpass(var p : tnode);
  32. implementation
  33. uses
  34. {$ifdef EXTDEBUG}
  35. cutils,
  36. {$endif}
  37. globtype,systems,verbose,
  38. globals,
  39. paramgr,
  40. aasmtai,
  41. cgbase,
  42. nflw,cgobj;
  43. {*****************************************************************************
  44. SecondPass
  45. *****************************************************************************}
  46. {$ifdef EXTDEBUG}
  47. procedure logsecond(ht:tnodetype; entry: boolean);
  48. const
  49. secondnames: array[tnodetype] of string[13] =
  50. ('<emptynode>',
  51. 'add-addn', {addn}
  52. 'add-muln', {muln}
  53. 'add-subn', {subn}
  54. 'moddiv-divn', {divn}
  55. 'add-symdifn', {symdifn}
  56. 'moddiv-modn', {modn}
  57. 'assignment', {assignn}
  58. 'load', {loadn}
  59. 'nothing-range', {range}
  60. 'add-ltn', {ltn}
  61. 'add-lten', {lten}
  62. 'add-gtn', {gtn}
  63. 'add-gten', {gten}
  64. 'add-equaln', {equaln}
  65. 'add-unequaln', {unequaln}
  66. 'in', {inn}
  67. 'add-orn', {orn}
  68. 'add-xorn', {xorn}
  69. 'shlshr-shrn', {shrn}
  70. 'shlshr-shln', {shln}
  71. 'add-slashn', {slashn}
  72. 'add-andn', {andn}
  73. 'subscriptn', {subscriptn}
  74. 'deref', {derefn}
  75. 'addr', {addrn}
  76. 'ordconst', {ordconstn}
  77. 'typeconv', {typeconvn}
  78. 'calln', {calln}
  79. 'noth-callpar',{callparan}
  80. 'realconst', {realconstn}
  81. 'unaryminus', {unaryminusn}
  82. 'asm', {asmn}
  83. 'vecn', {vecn}
  84. 'pointerconst',{pointerconstn}
  85. 'stringconst', {stringconstn}
  86. 'not', {notn}
  87. 'inline', {inlinen}
  88. 'niln', {niln}
  89. 'error', {errorn}
  90. 'nothing-typen', {typen}
  91. 'setelement', {setelementn}
  92. 'setconst', {setconstn}
  93. 'blockn', {blockn}
  94. 'statement', {statementn}
  95. 'ifn', {ifn}
  96. 'breakn', {breakn}
  97. 'continuen', {continuen}
  98. 'while_repeat', {whilerepeatn}
  99. 'for', {forn}
  100. 'exitn', {exitn}
  101. 'with', {withn}
  102. 'case', {casen}
  103. 'label', {labeln}
  104. 'goto', {goton}
  105. 'tryexcept', {tryexceptn}
  106. 'raise', {raisen}
  107. 'tryfinally', {tryfinallyn}
  108. 'on', {onn}
  109. 'is', {isn}
  110. 'as', {asn}
  111. 'error-caret', {caretn}
  112. 'add-starstar', {starstarn}
  113. 'arrayconstruc', {arrayconstructn}
  114. 'noth-arrcnstr', {arrayconstructrangen}
  115. 'tempcreaten',
  116. 'temprefn',
  117. 'tempdeleten',
  118. 'addoptn',
  119. 'nothing-nothg', {nothingn}
  120. 'loadvmt', {loadvmtn}
  121. 'guidconstn',
  122. 'rttin',
  123. 'loadparentfpn'
  124. );
  125. var
  126. p: pchar;
  127. begin
  128. if entry then
  129. p := strpnew('second '+secondnames[ht]+' (entry)')
  130. else
  131. p := strpnew('second '+secondnames[ht]+' (exit)');
  132. exprasmlist.concat(tai_comment.create(p));
  133. end;
  134. {$endif EXTDEBUG}
  135. procedure secondpass(var p : tnode);
  136. var
  137. oldcodegenerror : boolean;
  138. oldlocalswitches : tlocalswitches;
  139. oldpos : tfileposinfo;
  140. begin
  141. if not assigned(p) then
  142. internalerror(200208221);
  143. if not(nf_error in p.flags) then
  144. begin
  145. oldcodegenerror:=codegenerror;
  146. oldlocalswitches:=aktlocalswitches;
  147. oldpos:=aktfilepos;
  148. if not inlining_procedure then
  149. aktfilepos:=p.fileinfo;
  150. aktlocalswitches:=p.localswitches;
  151. codegenerror:=false;
  152. {$ifdef EXTDEBUG}
  153. if (p.expectloc=LOC_INVALID) then
  154. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  155. if (not allow_multi_pass2) and
  156. (p.location.loc<>LOC_INVALID) then
  157. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  158. if (cs_asm_nodes in aktglobalswitches) then
  159. logsecond(p.nodetype,true);
  160. {$endif EXTDEBUG}
  161. p.pass_2;
  162. {$ifdef EXTDEBUG}
  163. if (cs_asm_nodes in aktglobalswitches) then
  164. logsecond(p.nodetype,false);
  165. if (not codegenerror) then
  166. begin
  167. if (p.location.loc=LOC_INVALID) then
  168. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype])
  169. else if (p.location.loc<>p.expectloc) then
  170. Comment(V_Warning,'Location is different in secondpass: '+nodetype2str[p.nodetype]);
  171. end;
  172. {$endif EXTDEBUG}
  173. if codegenerror then
  174. include(p.flags,nf_error);
  175. codegenerror:=codegenerror or oldcodegenerror;
  176. aktlocalswitches:=oldlocalswitches;
  177. aktfilepos:=oldpos;
  178. end
  179. else
  180. codegenerror:=true;
  181. end;
  182. function do_secondpass(var p : tnode) : boolean;
  183. begin
  184. { exprasmlist must be empty }
  185. if not exprasmlist.empty then
  186. internalerror(200405201);
  187. { clear errors before starting }
  188. codegenerror:=false;
  189. if not(nf_error in p.flags) then
  190. secondpass(p);
  191. do_secondpass:=codegenerror;
  192. end;
  193. end.
  194. {
  195. $Log$
  196. Revision 1.76 2004-06-20 08:55:30 florian
  197. * logs truncated
  198. Revision 1.75 2004/05/23 15:06:21 peter
  199. * implicit_finally flag must be set in pass1
  200. * add check whether the implicit frame is generated when expected
  201. }