pass_2.pas 7.0 KB

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