pass_2.pas 7.4 KB

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