pass_2.pas 7.8 KB

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