pass_2.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 which contains the exit statement }
  39. fc_block_with_exit);
  40. tflowcontrol = set of tenumflowcontrol;
  41. var
  42. flowcontrol : tflowcontrol;
  43. { produces the actual code }
  44. function do_secondpass(var p : tnode) : boolean;
  45. procedure secondpass(p : tnode);
  46. implementation
  47. uses
  48. cutils,
  49. globtype,verbose,
  50. globals,
  51. aasmdata,
  52. cgobj
  53. {$ifdef EXTDEBUG}
  54. ,cgbase
  55. ,aasmtai
  56. {$endif}
  57. ;
  58. {*****************************************************************************
  59. SecondPass
  60. *****************************************************************************}
  61. {$ifdef EXTDEBUG}
  62. var
  63. secondprefix : string;
  64. procedure logsecond(ht:tnodetype; entry: boolean);
  65. const
  66. secondnames: array[tnodetype] of string[13] =
  67. ('<emptynode>',
  68. 'add-addn', {addn}
  69. 'add-muln', {muln}
  70. 'add-subn', {subn}
  71. 'moddiv-divn', {divn}
  72. 'add-symdifn', {symdifn}
  73. 'moddiv-modn', {modn}
  74. 'assignment', {assignn}
  75. 'load', {loadn}
  76. 'nothing-range', {range}
  77. 'add-ltn', {ltn}
  78. 'add-lten', {lten}
  79. 'add-gtn', {gtn}
  80. 'add-gten', {gten}
  81. 'add-equaln', {equaln}
  82. 'add-unequaln', {unequaln}
  83. 'in', {inn}
  84. 'add-orn', {orn}
  85. 'add-xorn', {xorn}
  86. 'shlshr-shrn', {shrn}
  87. 'shlshr-shln', {shln}
  88. 'add-slashn', {slashn}
  89. 'add-andn', {andn}
  90. 'subscriptn', {subscriptn}
  91. 'deref', {derefn}
  92. 'addr', {addrn}
  93. 'ordconst', {ordconstn}
  94. 'typeconv', {typeconvn}
  95. 'calln', {calln}
  96. 'noth-callpar',{callparan}
  97. 'realconst', {realconstn}
  98. 'unaryminus', {unaryminusn}
  99. 'unaryplus', {unaryplusn}
  100. 'asm', {asmn}
  101. 'vecn', {vecn}
  102. 'pointerconst',{pointerconstn}
  103. 'stringconst', {stringconstn}
  104. 'not', {notn}
  105. 'inline', {inlinen}
  106. 'niln', {niln}
  107. 'error', {errorn}
  108. 'nothing-typen', {typen}
  109. 'setelement', {setelementn}
  110. 'setconst', {setconstn}
  111. 'blockn', {blockn}
  112. 'statement', {statementn}
  113. 'ifn', {ifn}
  114. 'breakn', {breakn}
  115. 'continuen', {continuen}
  116. 'while_repeat', {whilerepeatn}
  117. 'for', {forn}
  118. 'exitn', {exitn}
  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. 'finalizetemps'
  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. oldexecutionweight : longint;
  167. begin
  168. if not assigned(p) then
  169. internalerror(200208221);
  170. if not(nf_error in p.flags) then
  171. begin
  172. oldcodegenerror:=codegenerror;
  173. oldlocalswitches:=current_settings.localswitches;
  174. oldpos:=current_filepos;
  175. current_filepos:=p.fileinfo;
  176. current_settings.localswitches:=p.localswitches;
  177. codegenerror:=false;
  178. oldexecutionweight:=cg.executionweight;
  179. if assigned(p.optinfo) then
  180. cg.executionweight:=min(p.optinfo^.executionweight,high(cg.executionweight))
  181. else
  182. cg.executionweight:=100;
  183. {$ifdef EXTDEBUG}
  184. if (p.expectloc=LOC_INVALID) then
  185. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  186. if (p.location.loc<>LOC_INVALID) then
  187. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  188. if (cs_asm_nodes in current_settings.globalswitches) then
  189. logsecond(p.nodetype,true);
  190. {$endif EXTDEBUG}
  191. p.pass_generate_code;
  192. {$ifdef EXTDEBUG}
  193. if (cs_asm_nodes in current_settings.globalswitches) then
  194. logsecond(p.nodetype,false);
  195. if (not codegenerror) then
  196. begin
  197. if (p.location.loc<>p.expectloc) then
  198. begin
  199. if ((p.location.loc=loc_register) and (p.expectloc=loc_cregister))
  200. or ((p.location.loc=loc_fpuregister) and (p.expectloc=loc_cfpuregister))
  201. or ((p.location.loc=loc_reference) and (p.expectloc=loc_creference)) then
  202. Comment(V_Note,'Location ('+tcgloc2str[p.location.loc]+') not equal to expectloc ('+tcgloc2str[p.expectloc]+'): '+nodetype2str[p.nodetype])
  203. else
  204. Comment(V_Warning,'Location ('+tcgloc2str[p.location.loc]+') not equal to expectloc ('+tcgloc2str[p.expectloc]+'): '+nodetype2str[p.nodetype]);
  205. end;
  206. if (p.location.loc=LOC_INVALID) then
  207. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype]);
  208. end;
  209. {$endif EXTDEBUG}
  210. if codegenerror then
  211. include(p.flags,nf_error);
  212. codegenerror:=codegenerror or oldcodegenerror;
  213. current_settings.localswitches:=oldlocalswitches;
  214. current_filepos:=oldpos;
  215. cg.executionweight:=oldexecutionweight;
  216. end
  217. else
  218. codegenerror:=true;
  219. end;
  220. function do_secondpass(var p : tnode) : boolean;
  221. begin
  222. { current_asmdata.CurrAsmList must be empty }
  223. if not current_asmdata.CurrAsmList.empty then
  224. internalerror(200405201);
  225. { clear errors before starting }
  226. codegenerror:=false;
  227. if not(nf_error in p.flags) then
  228. secondpass(p);
  229. do_secondpass:=codegenerror;
  230. end;
  231. end.