pass_2.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit handles the codegeneration pass
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pass_2;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node;
  22. type
  23. tenumflowcontrol = (fc_exit,fc_break,fc_continue,fc_inflowcontrol,fc_gotolabel);
  24. tflowcontrol = set of tenumflowcontrol;
  25. var
  26. flowcontrol : tflowcontrol;
  27. { produces the actual code }
  28. function do_secondpass(var p : tnode) : boolean;
  29. procedure secondpass(var p : tnode);
  30. implementation
  31. uses
  32. {$ifdef EXTDEBUG}
  33. cutils,
  34. {$endif}
  35. globtype,systems,verbose,
  36. globals,
  37. paramgr,
  38. aasmtai,aasmdata,
  39. cgbase,
  40. nflw,cgobj;
  41. {*****************************************************************************
  42. SecondPass
  43. *****************************************************************************}
  44. {$ifdef EXTDEBUG}
  45. var
  46. secondprefix : string;
  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. '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. 'dataconstn'
  124. );
  125. var
  126. p: pchar;
  127. begin
  128. if entry then
  129. begin
  130. secondprefix:=secondprefix+' ';
  131. p := strpnew(secondprefix+'second '+secondnames[ht]+' (entry)')
  132. end
  133. else
  134. begin
  135. p := strpnew(secondprefix+'second '+secondnames[ht]+' (exit)');
  136. delete(secondprefix,length(secondprefix),1);
  137. end;
  138. current_asmdata.CurrAsmList.concat(tai_comment.create(p));
  139. end;
  140. {$endif EXTDEBUG}
  141. procedure secondpass(var p : tnode);
  142. var
  143. oldcodegenerror : boolean;
  144. oldlocalswitches : tlocalswitches;
  145. oldpos : tfileposinfo;
  146. begin
  147. if not assigned(p) then
  148. internalerror(200208221);
  149. if not(nf_error in p.flags) then
  150. begin
  151. oldcodegenerror:=codegenerror;
  152. oldlocalswitches:=current_settings.localswitches;
  153. oldpos:=current_filepos;
  154. current_filepos:=p.fileinfo;
  155. current_settings.localswitches:=p.localswitches;
  156. codegenerror:=false;
  157. {$ifdef EXTDEBUG}
  158. if (p.expectloc=LOC_INVALID) then
  159. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  160. if (p.location.loc<>LOC_INVALID) then
  161. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  162. if (cs_asm_nodes in current_settings.globalswitches) then
  163. logsecond(p.nodetype,true);
  164. {$endif EXTDEBUG}
  165. p.pass_generate_code;
  166. {$ifdef EXTDEBUG}
  167. if (cs_asm_nodes in current_settings.globalswitches) then
  168. logsecond(p.nodetype,false);
  169. if (not codegenerror) then
  170. begin
  171. if (p.location.loc=LOC_INVALID) then
  172. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype]);
  173. end;
  174. {$endif EXTDEBUG}
  175. if codegenerror then
  176. include(p.flags,nf_error);
  177. codegenerror:=codegenerror or oldcodegenerror;
  178. current_settings.localswitches:=oldlocalswitches;
  179. current_filepos:=oldpos;
  180. end
  181. else
  182. codegenerror:=true;
  183. end;
  184. function do_secondpass(var p : tnode) : boolean;
  185. begin
  186. { current_asmdata.CurrAsmList must be empty }
  187. if not current_asmdata.CurrAsmList.empty then
  188. internalerror(200405201);
  189. { clear errors before starting }
  190. codegenerror:=false;
  191. if not(nf_error in p.flags) then
  192. secondpass(p);
  193. do_secondpass:=codegenerror;
  194. end;
  195. end.