3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "mem/slicc/symbols/Transition.hh"
36 #include "mem/slicc/symbols/State.hh"
37 #include "mem/slicc/symbols/Event.hh"
38 #include "mem/slicc/symbols/Action.hh"
39 #include "mem/gems_common/util.hh"
40 #include "mem/slicc/symbols/Var.hh"
42 Transition::Transition(string state
, string event
, string nextState
,
43 const Vector
<string
>& actionList
,
44 const Location
& location
,
45 const Map
<string
, string
>& pairMap
)
46 : Symbol(state
+ "|" + event
, location
, pairMap
)
50 m_nextState
= nextState
;
51 m_actionList
= actionList
;
53 // Ptrs are undefined at this point
56 m_nextStatePtr
= NULL
;
57 m_actionPtrsValid
= false;
60 void Transition::checkIdents(const Vector
<State
*>& states
,
61 const Vector
<Event
*>& events
,
62 const Vector
<Action
*>& actions
)
64 m_statePtr
= findIndex(states
, m_state
);
65 m_eventPtr
= findIndex(events
, m_event
);
66 m_nextStatePtr
= findIndex(states
, m_nextState
);
68 for(int i
=0; i
< m_actionList
.size(); i
++) {
69 Action
* action_ptr
= findIndex(actions
, m_actionList
[i
]);
70 int size
= action_ptr
->getResources().keys().size();
71 for (int j
=0; j
< size
; j
++) {
72 Var
* var_ptr
= action_ptr
->getResources().keys()[j
];
73 if (var_ptr
->getType()->cIdent() != "DNUCAStopTable") {
74 int num
= atoi((action_ptr
->getResources().lookup(var_ptr
)).c_str());
75 if (m_resources
.exist(var_ptr
)) {
76 num
+= atoi((m_resources
.lookup(var_ptr
)).c_str());
78 m_resources
.add(var_ptr
, int_to_string(num
));
80 m_resources
.add(var_ptr
, action_ptr
->getResources().lookup(var_ptr
));
83 m_actionPtrs
.insertAtBottom(action_ptr
);
85 m_actionPtrsValid
= true;
88 const string
& Transition::getStateShorthand() const
90 assert(m_statePtr
!= NULL
);
91 return m_statePtr
->getShorthand();
94 const string
& Transition::getEventShorthand() const
96 assert(m_eventPtr
!= NULL
);
97 return m_eventPtr
->getShorthand();
100 const string
& Transition::getNextStateShorthand() const
102 assert(m_nextStatePtr
!= NULL
);
103 return m_nextStatePtr
->getShorthand();
106 string
Transition::getActionShorthands() const
108 assert(m_actionPtrsValid
);
110 int numActions
= m_actionPtrs
.size();
111 for (int i
=0; i
<numActions
; i
++) {
112 str
+= m_actionPtrs
[i
]->getShorthand();
117 void Transition::print(ostream
& out
) const
119 out
<< "[Transition: ";
120 out
<< "(" << m_state
;
121 if (m_statePtr
!= NULL
) {
122 out
<< ":" << *m_statePtr
;
124 out
<< ", " << m_event
;
125 if (m_eventPtr
!= NULL
) {
126 out
<< ":" << *m_eventPtr
;
130 if (m_nextStatePtr
!= NULL
) {
131 out
<< ":" << *m_nextStatePtr
;
138 Event
* Transition::findIndex(const Vector
<Event
*>& vec
, string ident
)
140 int size
= vec
.size();
141 for(int i
=0; i
<size
; i
++) {
142 if (ident
== vec
[i
]->getIdent()) {
146 error("Event not found: " + ident
);
150 State
* Transition::findIndex(const Vector
<State
*>& vec
, string ident
)
152 int size
= vec
.size();
153 for(int i
=0; i
<size
; i
++) {
154 if (ident
== vec
[i
]->getIdent()) {
158 error("State not found: " + ident
);
162 Action
* Transition::findIndex(const Vector
<Action
*>& vec
, string ident
)
164 int size
= vec
.size();
165 for(int i
=0; i
<size
; i
++) {
166 if (ident
== vec
[i
]->getIdent()) {
170 error("Action not found: " + ident
);