pass_2.pas 7.5 KB

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