module.hh revision 13291:ccbae1f89cd3
1/*
2 * Copyright 2018 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __SYSTEMC_CORE_MODULE_HH__
31#define __SYSTEMC_CORE_MODULE_HH__
32
33#include <cassert>
34#include <list>
35#include <map>
36#include <sstream>
37#include <string>
38#include <vector>
39
40#include "systemc/core/object.hh"
41#include "systemc/ext/core/sc_module.hh"
42
43namespace sc_core
44{
45
46class sc_port_base;
47class sc_export_base;
48
49} // namespace sc_core
50
51namespace sc_gem5
52{
53
54class UniqueNameGen
55{
56  private:
57    std::map<std::string, int> counts;
58    std::string buf;
59
60  public:
61    const char *
62    gen(std::string seed)
63    {
64        std::ostringstream os;
65        os << seed << "_" << counts[seed]++;
66        buf = os.str();
67        return buf.c_str();
68    }
69};
70
71class Module
72{
73  private:
74    const char *_name;
75    sc_core::sc_module *_sc_mod;
76    Object *_obj;
77    bool _ended;
78    bool _deprecatedConstructor;
79
80    UniqueNameGen nameGen;
81
82  public:
83    Module(const char *name);
84    ~Module();
85
86    static Module *
87    fromScModule(::sc_core::sc_module *mod)
88    {
89        return mod->_gem5_module;
90    }
91
92    void finish(Object *this_obj);
93
94    const char *name() const { return _name; }
95    void endModule() { _ended = true; }
96    void deprecatedConstructor() { _deprecatedConstructor = true; }
97
98    sc_core::sc_module *
99    sc_mod() const
100    {
101        assert(_sc_mod);
102        return _sc_mod;
103    }
104
105    void
106    sc_mod(sc_core::sc_module *sc_mod)
107    {
108        assert(!_sc_mod);
109        _sc_mod = sc_mod;
110    }
111
112    Object *
113    obj()
114    {
115        assert(_obj);
116        return _obj;
117    }
118
119    void pop();
120
121    const char *uniqueName(const char *seed) { return nameGen.gen(seed); }
122
123    void bindPorts(std::vector<const ::sc_core::sc_bind_proxy *> &proxies);
124
125    std::vector<::sc_core::sc_port_base *> ports;
126    std::vector<::sc_core::sc_export_base *> exports;
127
128    int bindingIndex;
129
130    void beforeEndOfElaboration();
131    void endOfElaboration();
132    void startOfSimulation();
133    void endOfSimulation();
134};
135
136Module *currentModule();
137Module *newModuleChecked();
138Module *newModule();
139
140static inline Module *
141pickParentModule()
142{
143    ::sc_core::sc_object *obj = pickParentObj();
144    auto mod = dynamic_cast<::sc_core::sc_module *>(obj);
145    if (!mod)
146        return nullptr;
147    return Module::fromScModule(mod);
148}
149static inline void
150pushParentModule(Module *m)
151{
152    pushParentObj(m->obj()->sc_obj());
153}
154static inline void
155popParentModule()
156{
157    assert(pickParentModule());
158    popParentObj();
159}
160
161extern std::list<Module *> allModules;
162
163} // namespace sc_gem5
164
165#endif  //__SYSTEMC_CORE_MODULE_HH__
166