pass_2.pas 8.6 KB

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