sc_except.cpp revision 12027
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  sc_except.cpp -- kill/reset exception handling
23
24  Original Author: Philipp A. Hartmann, OFFIS
25
26 CHANGE LOG APPEARS AT THE END OF THE FILE
27 *****************************************************************************/
28
29#include "sysc/kernel/sc_cmnhdr.h"
30//
31#include "sysc/kernel/sc_except.h"
32#include "sysc/kernel/sc_process.h"
33//
34#include "sysc/utils/sc_report.h"
35
36namespace sc_core {
37
38sc_unwind_exception::sc_unwind_exception( sc_process_b* proc_p, bool is_reset )
39  : m_proc_p(proc_p), m_is_reset( is_reset )
40{
41  sc_assert( m_proc_p );
42  m_proc_p->start_unwinding();
43}
44
45bool
46sc_unwind_exception::active() const
47{
48  return m_proc_p && m_proc_p->is_unwinding();
49}
50
51void
52sc_unwind_exception::clear() const
53{
54  sc_assert( m_proc_p );
55  m_proc_p->clear_unwinding();
56}
57
58const char*
59sc_unwind_exception::what() const throw()
60{
61  return ( m_is_reset ) ? "RESET" : "KILL";
62}
63
64sc_unwind_exception::~sc_unwind_exception() throw()
65{
66  if( active() ) {
67      // can't throw an exception, since we're already throwing
68      // -> abort instead
69      SC_REPORT_FATAL( SC_ID_RETHROW_UNWINDING_, m_proc_p->name() );
70  }
71}
72
73// handle and translate uncaught exceptions here
74//
75// These exceptions can either escape from sc_main() directly,
76// indirectly from an SC_METHOD(), or are thrown from within
77// an SC_(C)THREAD()
78//
79// returns a pointer to a dynamically allocated sc_report object,
80// containing the caught message
81
82sc_report*
83sc_handle_exception()
84{
85    try {
86
87        // re-throw exception here
88        try { throw; }
89
90        catch( sc_report & ) // to be on the safe side
91        {
92            throw; // continue
93        }
94        catch( sc_unwind_exception const & )
95        {
96            sc_assert( false && "Unhandled kill/reset, should never happen" );
97        }
98        catch( std::exception const & x )
99        {
100            SC_REPORT_ERROR( SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_, x.what() );
101        }
102        catch( char const * x )
103        {
104            SC_REPORT_ERROR( SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_, x );
105        }
106        catch( ... )
107        {
108            SC_REPORT_ERROR( SC_ID_SIMULATION_UNCAUGHT_EXCEPTION_,
109                             "UNKNOWN EXCEPTION" );
110        }
111    }
112    // everything is an sc_report now
113    catch( sc_report & rpt )
114    {
115        sc_report* rpt_p = new sc_report;
116        rpt_p->swap( rpt );
117        return rpt_p;
118    }
119    return 0;
120}
121
122} // namespace sc_core
123
124// $Log: sc_except.cpp,v $
125// Revision 1.4  2011/08/26 20:46:09  acg
126//  Andy Goodrich: moved the modification log to the end of the file to
127//  eliminate source line number skew when check-ins are done.
128//
129// Revision 1.3  2011/05/09 04:07:48  acg
130//  Philipp A. Hartmann:
131//    (1) Restore hierarchy in all phase callbacks.
132//    (2) Ensure calls to before_end_of_elaboration.
133//
134// Revision 1.2  2011/02/13 21:47:37  acg
135//  Andy Goodrich: update copyright notice.
136//
137// Revision 1.1  2011/02/10 22:47:38  acg
138//  Andy Goodrich: first check in of Philipp A. Hartmann's new exception
139//  processing code.
140//
141
142// Taf!
143