pass_2.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. procedure logsecond(ht:tnodetype; entry: boolean);
  46. const
  47. secondnames: array[tnodetype] of string[13] =
  48. ('<emptynode>',
  49. 'add-addn', {addn}
  50. 'add-muln', {muln}
  51. 'add-subn', {subn}
  52. 'moddiv-divn', {divn}
  53. 'add-symdifn', {symdifn}
  54. 'moddiv-modn', {modn}
  55. 'assignment', {assignn}
  56. 'load', {loadn}
  57. 'nothing-range', {range}
  58. 'add-ltn', {ltn}
  59. 'add-lten', {lten}
  60. 'add-gtn', {gtn}
  61. 'add-gten', {gten}
  62. 'add-equaln', {equaln}
  63. 'add-unequaln', {unequaln}
  64. 'in', {inn}
  65. 'add-orn', {orn}
  66. 'add-xorn', {xorn}
  67. 'shlshr-shrn', {shrn}
  68. 'shlshr-shln', {shln}
  69. 'add-slashn', {slashn}
  70. 'add-andn', {andn}
  71. 'subscriptn', {subscriptn}
  72. 'deref', {derefn}
  73. 'addr', {addrn}
  74. 'ordconst', {ordconstn}
  75. 'typeconv', {typeconvn}
  76. 'calln', {calln}
  77. 'noth-callpar',{callparan}
  78. 'realconst', {realconstn}
  79. 'unaryminus', {unaryminusn}
  80. 'asm', {asmn}
  81. 'vecn', {vecn}
  82. 'pointerconst',{pointerconstn}
  83. 'stringconst', {stringconstn}
  84. 'not', {notn}
  85. 'inline', {inlinen}
  86. 'niln', {niln}
  87. 'error', {errorn}
  88. 'nothing-typen', {typen}
  89. 'setelement', {setelementn}
  90. 'setconst', {setconstn}
  91. 'blockn', {blockn}
  92. 'statement', {statementn}
  93. 'ifn', {ifn}
  94. 'breakn', {breakn}
  95. 'continuen', {continuen}
  96. 'while_repeat', {whilerepeatn}
  97. 'for', {forn}
  98. 'exitn', {exitn}
  99. 'with', {withn}
  100. 'case', {casen}
  101. 'label', {labeln}
  102. 'goto', {goton}
  103. 'tryexcept', {tryexceptn}
  104. 'raise', {raisen}
  105. 'tryfinally', {tryfinallyn}
  106. 'on', {onn}
  107. 'is', {isn}
  108. 'as', {asn}
  109. 'add-starstar', {starstarn}
  110. 'arrayconstruc', {arrayconstructn}
  111. 'noth-arrcnstr', {arrayconstructrangen}
  112. 'tempcreaten',
  113. 'temprefn',
  114. 'tempdeleten',
  115. 'addoptn',
  116. 'nothing-nothg', {nothingn}
  117. 'loadvmt', {loadvmtn}
  118. 'guidconstn',
  119. 'rttin',
  120. 'loadparentfpn',
  121. 'dataconstn'
  122. );
  123. var
  124. p: pchar;
  125. begin
  126. if entry then
  127. p := strpnew('second '+secondnames[ht]+' (entry)')
  128. else
  129. p := strpnew('second '+secondnames[ht]+' (exit)');
  130. current_asmdata.CurrAsmList.concat(tai_comment.create(p));
  131. end;
  132. {$endif EXTDEBUG}
  133. procedure secondpass(var p : tnode);
  134. var
  135. oldcodegenerror : boolean;
  136. oldlocalswitches : tlocalswitches;
  137. oldpos : tfileposinfo;
  138. begin
  139. if not assigned(p) then
  140. internalerror(200208221);
  141. if not(nf_error in p.flags) then
  142. begin
  143. oldcodegenerror:=codegenerror;
  144. oldlocalswitches:=current_settings.localswitches;
  145. oldpos:=current_filepos;
  146. current_filepos:=p.fileinfo;
  147. current_settings.localswitches:=p.localswitches;
  148. codegenerror:=false;
  149. {$ifdef EXTDEBUG}
  150. if (p.expectloc=LOC_INVALID) then
  151. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  152. if (p.location.loc<>LOC_INVALID) then
  153. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  154. if (cs_asm_nodes in current_settings.globalswitches) then
  155. logsecond(p.nodetype,true);
  156. {$endif EXTDEBUG}
  157. p.pass_generate_code;
  158. {$ifdef EXTDEBUG}
  159. if (cs_asm_nodes in current_settings.globalswitches) then
  160. logsecond(p.nodetype,false);
  161. if (not codegenerror) then
  162. begin
  163. if (p.location.loc=LOC_INVALID) then
  164. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype]);
  165. end;
  166. {$endif EXTDEBUG}
  167. if codegenerror then
  168. include(p.flags,nf_error);
  169. codegenerror:=codegenerror or oldcodegenerror;
  170. current_settings.localswitches:=oldlocalswitches;
  171. current_filepos:=oldpos;
  172. end
  173. else
  174. codegenerror:=true;
  175. end;
  176. function do_secondpass(var p : tnode) : boolean;
  177. begin
  178. { current_asmdata.CurrAsmList must be empty }
  179. if not current_asmdata.CurrAsmList.empty then
  180. internalerror(200405201);
  181. { clear errors before starting }
  182. codegenerror:=false;
  183. if not(nf_error in p.flags) then
  184. secondpass(p);
  185. do_secondpass:=codegenerror;
  186. end;
  187. end.