From 0bbf63f17a636ffbfcfae4ea20fc306c145bc3a3 Mon Sep 17 00:00:00 2001 From: Nathan Binkert Date: Fri, 12 Mar 2010 18:42:56 -0800 Subject: slicc: Change the code generation so that the generated code is easier to read --- src/mem/slicc/symbols/Func.py | 3 +- src/mem/slicc/symbols/StateMachine.py | 220 +++++++++++++++++++++++----------- src/mem/slicc/symbols/Type.py | 132 +++++++++++++------- 3 files changed, 242 insertions(+), 113 deletions(-) (limited to 'src/mem/slicc/symbols') diff --git a/src/mem/slicc/symbols/Func.py b/src/mem/slicc/symbols/Func.py index a05f0a03b..459cca52a 100644 --- a/src/mem/slicc/symbols/Func.py +++ b/src/mem/slicc/symbols/Func.py @@ -95,7 +95,8 @@ class Func(Symbol): params = ', '.join(self.param_strings) code(''' -$return_type ${klass}::${{self.c_ident}}($params) +$return_type +${klass}::${{self.c_ident}}($params) { ${{self.body}} } diff --git a/src/mem/slicc/symbols/StateMachine.py b/src/mem/slicc/symbols/StateMachine.py index e86b4245a..6614c7bd0 100644 --- a/src/mem/slicc/symbols/StateMachine.py +++ b/src/mem/slicc/symbols/StateMachine.py @@ -187,14 +187,14 @@ class $py_ident(RubyController): self.message_buffer_names = [] code(''' -/** \\file $ident.hh +/** \\file $c_ident.hh * * Auto generated C++ code started by $__file__:$__line__ * Created by slicc definition of Module "${{self.short}}" */ -#ifndef ${ident}_CONTROLLER_H -#define ${ident}_CONTROLLER_H +#ifndef __${ident}_CONTROLLER_HH__ +#define __${ident}_CONTROLLER_HH__ #include #include @@ -220,9 +220,11 @@ class $py_ident(RubyController): code(''' extern std::stringstream ${ident}_transitionComment; -class $c_ident : public AbstractController { -#ifdef CHECK_COHERENCE -#endif /* CHECK_COHERENCE */ +class $c_ident : public AbstractController +{ +// the coherence checker needs to call isBlockExclusive() and isBlockShared() +// making the Chip a friend class is an easy way to do this for now + public: typedef ${c_ident}Params Params; $c_ident(const Params *p); @@ -241,6 +243,7 @@ public: void clearStats(); void blockOnQueue(Address addr, MessageBuffer* port); void unblock(Address addr); + private: ''') @@ -255,8 +258,15 @@ private: code(''' int m_number_of_TBEs; -TransitionResult doTransition(${ident}_Event event, ${ident}_State state, const Address& addr); // in ${ident}_Transitions.cc -TransitionResult doTransitionWorker(${ident}_Event event, ${ident}_State state, ${ident}_State& next_state, const Address& addr); // in ${ident}_Transitions.cc +TransitionResult doTransition(${ident}_Event event, + ${ident}_State state, + const Address& addr); + +TransitionResult doTransitionWorker(${ident}_Event event, + ${ident}_State state, + ${ident}_State& next_state, + const Address& addr); + std::string m_name; int m_transitions_per_cycle; int m_buffer_size; @@ -269,6 +279,7 @@ bool m_is_blocking; map< Address, MessageBuffer* > m_block_map; ${ident}_Profiler s_profiler; static int m_num_controllers; + // Internal functions ''') @@ -288,7 +299,7 @@ static int m_num_controllers; # the controller internal variables code(''' -// Object +// Objects ''') for var in self.objects: th = var.get("template_hack", "") @@ -299,7 +310,7 @@ static int m_num_controllers; code.dedent() code('};') - code('#endif // ${ident}_CONTROLLER_H') + code('#endif // __${ident}_CONTROLLER_H__') code.write(path, '%s.hh' % c_ident) def printControllerCC(self, path): @@ -310,7 +321,7 @@ static int m_num_controllers; c_ident = "%s_Controller" % self.ident code(''' -/** \\file $ident.cc +/** \\file $c_ident.cc * * Auto generated C++ code started by $__file__:$__line__ * Created by slicc definition of Module "${{self.short}}" @@ -344,11 +355,12 @@ ${c_ident}Params::create() return new $c_ident(this); } - int $c_ident::m_num_controllers = 0; +// for adding information to the protocol debug trace stringstream ${ident}_transitionComment; #define APPEND_TRANSITION_COMMENT(str) (${ident}_transitionComment << str) + /** \\brief constructor */ $c_ident::$c_ident(const Params *p) : AbstractController(p) @@ -411,13 +423,18 @@ m_dma_sequencer_ptr->setController(this); code(''' } -void $c_ident::init() +void +$c_ident::init() { + MachineType machine_type; + int base; + m_machineID.type = MachineType_${ident}; m_machineID.num = m_version; - // Objects + // initialize objects s_profiler.setVersion(m_version); + ''') code.indent() @@ -445,18 +462,16 @@ void $c_ident::init() args = "m_number_of_TBEs" else: args = var.get("constructor_hack", "") - args = "(%s)" % args - code('$expr$args;') - else: - code(';') + code('$expr($args);') code('assert($vid != NULL);') if "default" in var: - code('(*$vid) = ${{var["default"]}}; // Object default') + code('*$vid = ${{var["default"]}}; // Object default') elif "default" in vtype: - code('(*$vid) = ${{vtype["default"]}}; // Type ${{vtype.ident}} default') + comment = "Type %s default" % vtype.ident + code('*$vid = ${{vtype["default"]}}; // $comment') # Set ordering if "ordered" in var and "trigger_queue" not in var: @@ -480,7 +495,9 @@ void $c_ident::init() assert var.machine is not None code(''' -$vid = m_net_ptr->get${network}NetQueue(m_version+MachineType_base_number(string_to_MachineType("${{var.machine.ident}}")), $ordered, $vnet); +machine_type = string_to_MachineType("${{var.machine.ident}}"); +base = MachineType_base_number(machine_type); +$vid = m_net_ptr->get${network}NetQueue(m_version + base, $ordered, $vnet); ''') code('assert($vid != NULL);') @@ -508,7 +525,10 @@ if (m_buffer_size > 0) { ''') # set description (may be overriden later by port def) - code('$vid->setDescription("[Version " + int_to_string(m_version) + ", ${ident}, name=${{var.c_ident}}]");') + code(''' +$vid->setDescription("[Version " + int_to_string(m_version) + ", ${ident}, name=${{var.c_ident}}]"); + +''') # Set the queue consumers code.insert_newline() @@ -553,51 +573,77 @@ if (m_buffer_size > 0) { mq_ident = "NULL" code(''' -int $c_ident::getNumControllers() { +int +$c_ident::getNumControllers() +{ return m_num_controllers; } -MessageBuffer* $c_ident::getMandatoryQueue() const { +MessageBuffer* +$c_ident::getMandatoryQueue() const +{ return $mq_ident; } -const int & $c_ident::getVersion() const{ +const int & +$c_ident::getVersion() const +{ return m_version; } -const string $c_ident::toString() const{ +const string +$c_ident::toString() const +{ return "$c_ident"; } -const string $c_ident::getName() const{ +const string +$c_ident::getName() const +{ return m_name; } -const MachineType $c_ident::getMachineType() const{ + +const MachineType +$c_ident::getMachineType() const +{ return MachineType_${ident}; } -void $c_ident::blockOnQueue(Address addr, MessageBuffer* port) { +void +$c_ident::blockOnQueue(Address addr, MessageBuffer* port) +{ m_is_blocking = true; m_block_map[addr] = port; } -void $c_ident::unblock(Address addr) { + +void +$c_ident::unblock(Address addr) +{ m_block_map.erase(addr); if (m_block_map.size() == 0) { m_is_blocking = false; } } -void $c_ident::print(ostream& out) const { out << "[$c_ident " << m_version << "]"; } +void +$c_ident::print(ostream& out) const +{ + out << "[$c_ident " << m_version << "]"; +} -void $c_ident::printConfig(ostream& out) const { +void +$c_ident::printConfig(ostream& out) const +{ out << "$c_ident config: " << m_name << endl; out << " version: " << m_version << endl; - for (map::const_iterator it = m_cfg.begin(); it != m_cfg.end(); it++) { - out << " " << (*it).first << ": " << (*it).second << endl; - } + map::const_iterator it; + for (it = m_cfg.begin(); it != m_cfg.end(); it++) + out << " " << it->first << ": " << it->second << endl; } -void $c_ident::printStats(ostream& out) const { +void +$c_ident::printStats(ostream& out) const +{ ''') # # Cache and Memory Controllers have specific profilers associated with @@ -638,7 +684,8 @@ void $c_ident::clearStats() { code(''' /** \\brief ${{action.desc}} */ -void $c_ident::${{action.ident}}(const Address& addr) +void +$c_ident::${{action.ident}}(const Address& addr) { DEBUG_MSG(GENERATED_COMP, HighPrio, "executing"); ${{action["c_code"]}} @@ -657,6 +704,7 @@ void $c_ident::${{action.ident}}(const Address& addr) // Auto generated C++ code started by $__file__:$__line__ // ${ident}: ${{self.short}} +#include "base/misc.hh" #include "mem/ruby/common/Global.hh" #include "mem/ruby/slicc_interface/RubySlicc_includes.hh" #include "mem/protocol/${ident}_Controller.hh" @@ -665,16 +713,22 @@ void $c_ident::${{action.ident}}(const Address& addr) #include "mem/protocol/Types.hh" #include "mem/ruby/system/System.hh" -void ${ident}_Controller::wakeup() +void +${ident}_Controller::wakeup() { + // DEBUG_EXPR(GENERATED_COMP, MedPrio, *this); + // DEBUG_EXPR(GENERATED_COMP, MedPrio, g_eventQueue_ptr->getTime()); int counter = 0; while (true) { // Some cases will put us into an infinite loop without this limit assert(counter <= m_transitions_per_cycle); if (counter == m_transitions_per_cycle) { - g_system_ptr->getProfiler()->controllerBusy(m_machineID); // Count how often we\'re fully utilized - g_eventQueue_ptr->scheduleEvent(this, 1); // Wakeup in another cycle and try again + // Count how often we are fully utilized + g_system_ptr->getProfiler()->controllerBusy(m_machineID); + + // Wakeup in another cycle and try again + g_eventQueue_ptr->scheduleEvent(this, 1); break; } ''') @@ -697,6 +751,8 @@ void ${ident}_Controller::wakeup() code(''' break; // If we got this far, we have nothing left todo } + // g_eventQueue_ptr->scheduleEvent(this, 1); + // DEBUG_NEWLINE(GENERATED_COMP, MedPrio); } ''') @@ -724,8 +780,10 @@ void ${ident}_Controller::wakeup() #define GET_TRANSITION_COMMENT() (${ident}_transitionComment.str()) #define CLEAR_TRANSITION_COMMENT() (${ident}_transitionComment.str("")) -TransitionResult ${ident}_Controller::doTransition(${ident}_Event event, ${ident}_State state, const Address& addr -) +TransitionResult +${ident}_Controller::doTransition(${ident}_Event event, + ${ident}_State state, + const Address &addr) { ${ident}_State next_state = state; @@ -736,24 +794,28 @@ TransitionResult ${ident}_Controller::doTransition(${ident}_Event event, ${ident DEBUG_EXPR(GENERATED_COMP, MedPrio,event); DEBUG_EXPR(GENERATED_COMP, MedPrio,addr); - TransitionResult result = doTransitionWorker(event, state, next_state, addr); + TransitionResult result = + doTransitionWorker(event, state, next_state, addr); if (result == TransitionResult_Valid) { DEBUG_EXPR(GENERATED_COMP, MedPrio, next_state); DEBUG_NEWLINE(GENERATED_COMP, MedPrio); s_profiler.countTransition(state, event); if (Debug::getProtocolTrace()) { - g_system_ptr->getProfiler()->profileTransition("${ident}", m_version, addr, + g_system_ptr->getProfiler()->profileTransition("${ident}", + m_version, addr, ${ident}_State_to_string(state), ${ident}_Event_to_string(event), - ${ident}_State_to_string(next_state), GET_TRANSITION_COMMENT()); + ${ident}_State_to_string(next_state), + GET_TRANSITION_COMMENT()); } CLEAR_TRANSITION_COMMENT(); ${ident}_setState(addr, next_state); } else if (result == TransitionResult_ResourceStall) { if (Debug::getProtocolTrace()) { - g_system_ptr->getProfiler()->profileTransition("${ident}", m_version, addr, + g_system_ptr->getProfiler()->profileTransition("${ident}", + m_version, addr, ${ident}_State_to_string(state), ${ident}_Event_to_string(event), ${ident}_State_to_string(next_state), @@ -763,7 +825,8 @@ TransitionResult ${ident}_Controller::doTransition(${ident}_Event event, ${ident DEBUG_MSG(GENERATED_COMP, HighPrio, "stalling"); DEBUG_NEWLINE(GENERATED_COMP, MedPrio); if (Debug::getProtocolTrace()) { - g_system_ptr->getProfiler()->profileTransition("${ident}", m_version, addr, + g_system_ptr->getProfiler()->profileTransition("${ident}", + m_version, addr, ${ident}_State_to_string(state), ${ident}_Event_to_string(event), ${ident}_State_to_string(next_state), @@ -774,8 +837,11 @@ TransitionResult ${ident}_Controller::doTransition(${ident}_Event event, ${ident return result; } -TransitionResult ${ident}_Controller::doTransitionWorker(${ident}_Event event, ${ident}_State state, ${ident}_State& next_state, const Address& addr -) +TransitionResult +${ident}_Controller::doTransitionWorker(${ident}_Event event, + ${ident}_State state, + ${ident}_State& next_state, + const Address& addr) { switch(HASH_FUN(state, event)) { ''') @@ -801,9 +867,8 @@ TransitionResult ${ident}_Controller::doTransitionWorker(${ident}_Event event, $ for key,val in res.iteritems(): if key.type.ident != "DNUCAStopTable": val = ''' -if (!%s.areNSlotsAvailable(%s)) { +if (!%s.areNSlotsAvailable(%s)) return TransitionResult_ResourceStall; -} ''' % (key.code, val) case_sorter.append(val) @@ -843,9 +908,7 @@ if (!%s.areNSlotsAvailable(%s)) { # the same code for trans in transitions: code(' case HASH_FUN($trans):') - code(' {') code(' $case') - code(' }') code(''' default: @@ -869,8 +932,8 @@ if (!%s.areNSlotsAvailable(%s)) { // Auto generated C++ code started by $__file__:$__line__ // ${ident}: ${{self.short}} -#ifndef ${ident}_PROFILER_H -#define ${ident}_PROFILER_H +#ifndef __${ident}_PROFILER_HH_ +#define __${ident}_PROFILER_HH_ #include @@ -878,7 +941,8 @@ if (!%s.areNSlotsAvailable(%s)) { #include "mem/protocol/${ident}_State.hh" #include "mem/protocol/${ident}_Event.hh" -class ${ident}_Profiler { +class ${ident}_Profiler +{ public: ${ident}_Profiler(); void setVersion(int version); @@ -894,7 +958,7 @@ class ${ident}_Profiler { int m_version; }; -#endif // ${ident}_PROFILER_H +#endif // __${ident}_PROFILER_HH__ ''') code.write(path, "%s_Profiler.hh" % self.ident) @@ -920,11 +984,15 @@ ${ident}_Profiler::${ident}_Profiler() m_event_counters[event] = 0; } } -void ${ident}_Profiler::setVersion(int version) + +void +${ident}_Profiler::setVersion(int version) { m_version = version; } -void ${ident}_Profiler::clearStats() + +void +${ident}_Profiler::clearStats() { for (int state = 0; state < ${ident}_State_NUM; state++) { for (int event = 0; event < ${ident}_Event_NUM; event++) { @@ -936,17 +1004,22 @@ void ${ident}_Profiler::clearStats() m_event_counters[event] = 0; } } -void ${ident}_Profiler::countTransition(${ident}_State state, ${ident}_Event event) +void +${ident}_Profiler::countTransition(${ident}_State state, ${ident}_Event event) { assert(m_possible[state][event]); m_counters[state][event]++; m_event_counters[event]++; } -void ${ident}_Profiler::possibleTransition(${ident}_State state, ${ident}_Event event) +void +${ident}_Profiler::possibleTransition(${ident}_State state, + ${ident}_Event event) { m_possible[state][event] = true; } -void ${ident}_Profiler::dumpStats(std::ostream& out) const + +void +${ident}_Profiler::dumpStats(std::ostream& out) const { using namespace std; @@ -962,7 +1035,8 @@ void ${ident}_Profiler::dumpStats(std::ostream& out) const for (int event = 0; event < ${ident}_Event_NUM; event++) { if (m_possible[state][event]) { int count = m_counters[state][event]; - out << (${ident}_State) state << " " << (${ident}_Event) event << " " << count; + out << (${ident}_State) state << " " + << (${ident}_Event) event << " " << count; if (count == 0) { out << " <-- "; } @@ -978,10 +1052,14 @@ void ${ident}_Profiler::dumpStats(std::ostream& out) const # ************************** # ******* HTML Files ******* # ************************** - def frameRef(self, click_href, click_target, over_href, over_target_num, - text): + def frameRef(self, click_href, click_target, over_href, over_num, text): code = self.symtab.codeFormatter(fix_newlines=False) - code("""${{html.formatShorthand(text)}}""") + code(""" + ${{html.formatShorthand(text)}} + """) return str(code) def writeHTMLFiles(self, path): @@ -1014,7 +1092,8 @@ void ${ident}_Profiler::dumpStats(std::ostream& out) const code = self.symtab.codeFormatter() code(''' - + +

${{html.formatShorthand(self.short)}}: ''') @@ -1098,13 +1177,12 @@ void ${ident}_Profiler::dumpStats(std::ostream& out) const else: color = "white" - fix = code.nofix() code('') for action in trans.actions: href = "%s_action_%s.html" % (self.ident, action.ident) ref = self.frameRef(href, "Status", href, "1", action.short) - code(' $ref\n') + code(' $ref') if next != state: if trans.actions: code('/') @@ -1112,8 +1190,7 @@ void ${ident}_Profiler::dumpStats(std::ostream& out) const over = "%s_State_%s.html" % (self.ident, next.ident) ref = self.frameRef(click, "Table", over, "1", next.short) code("$ref") - code("\n") - code.fix(fix) + code("") # -- Each row if state == active_state: @@ -1129,6 +1206,7 @@ void ${ident}_Profiler::dumpStats(std::ostream& out) const ''') code(''' + ''') diff --git a/src/mem/slicc/symbols/Type.py b/src/mem/slicc/symbols/Type.py index a13cc2385..645291ac2 100644 --- a/src/mem/slicc/symbols/Type.py +++ b/src/mem/slicc/symbols/Type.py @@ -199,8 +199,8 @@ class Type(Symbol): * Auto generated C++ code started by $__file__:$__line__ */ -#ifndef ${{self.c_ident}}_H -#define ${{self.c_ident}}_H +#ifndef __${{self.c_ident}}_HH__ +#define __${{self.c_ident}}_HH__ #include @@ -218,17 +218,14 @@ class Type(Symbol): parent = " : public %s" % self["interface"] code(''' -$klass ${{self.c_ident}}$parent { +$klass ${{self.c_ident}}$parent +{ public: ${{self.c_ident}}() + { ''', klass="class") - # Call superclass constructor - if "interface" in self: - code(' : ${{self["interface"]}}()') - code.indent() - code("{") if not self.isGlobal: code.indent() for dm in self.data_members.values(): @@ -245,9 +242,6 @@ $klass ${{self.c_ident}}$parent { code.dedent() code('}') - # ******** Default destructor ******** - code('~${{self.c_ident}}() { };') - # ******** Full init constructor ******** if not self.isGlobal: params = [ 'const %s& local_%s' % (dm.type.c_ident, dm.ident) \ @@ -279,7 +273,9 @@ $klass ${{self.c_ident}}$parent { # create a static factory method if "interface" in self: code(''' -static ${{self["interface"]}}* create() { +static ${{self["interface"]}}* +create() +{ return new ${{self.c_ident}}(); } ''') @@ -290,10 +286,29 @@ static ${{self["interface"]}}* create() { if self.isMessage: code(''' -Message* clone() const { checkAllocator(); return s_allocator_ptr->allocate(*this); } -void destroy() { checkAllocator(); s_allocator_ptr->deallocate(this); } +Message * +clone() const +{ + checkAllocator(); + return s_allocator_ptr->allocate(*this); +} + +void +destroy() +{ + checkAllocator(); + s_allocator_ptr->deallocate(this); +} + static Allocator<${{self.c_ident}}>* s_allocator_ptr; -static void checkAllocator() { if (s_allocator_ptr == NULL) { s_allocator_ptr = new Allocator<${{self.c_ident}}>; }} + +static void +checkAllocator() +{ + if (s_allocator_ptr == NULL) { + s_allocator_ptr = new Allocator<${{self.c_ident}}>; + } +} ''') if not self.isGlobal: @@ -304,7 +319,11 @@ static void checkAllocator() { if (s_allocator_ptr == NULL) { s_allocator_ptr = /** \\brief Const accessor method for ${{dm.ident}} field. * \\return ${{dm.ident}} field */ -const ${{dm.type.c_ident}}& get${{dm.ident}}() const { return m_${{dm.ident}}; } +const ${{dm.type.c_ident}}& +get${{dm.ident}}() const +{ + return m_${{dm.ident}}; +} ''') # Non-const Get methods for each field @@ -314,7 +333,11 @@ const ${{dm.type.c_ident}}& get${{dm.ident}}() const { return m_${{dm.ident}}; } /** \\brief Non-const accessor method for ${{dm.ident}} field. * \\return ${{dm.ident}} field */ -${{dm.type.c_ident}}& get${{dm.ident}}() { return m_${{dm.ident}}; } +${{dm.type.c_ident}}& +get${{dm.ident}}() +{ + return m_${{dm.ident}}; +} ''') #Set methods for each field @@ -322,7 +345,11 @@ ${{dm.type.c_ident}}& get${{dm.ident}}() { return m_${{dm.ident}}; } for dm in self.data_members.values(): code(''' /** \\brief Mutator method for ${{dm.ident}} field */ -void set${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}}) { m_${{dm.ident}} = local_${{dm.ident}}; } +void +set${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}}) +{ + m_${{dm.ident}} = local_${{dm.ident}}; +} ''') code('void print(std::ostream& out) const;') @@ -346,11 +373,10 @@ void set${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}}) { m_${{dm assert self.isGlobal init = " = %s" % (dm.init_code) - desc = "" if "desc" in dm: - desc = '/**< %s */' % dm["desc"] + code('/** ${{dm["desc"]}} */') - code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init; $desc') + code('$const${{dm.type.c_ident}} m_${{dm.ident}}$init;') if self.isMessage: code('unsigned proc_id;') @@ -360,18 +386,19 @@ void set${{dm.ident}}(const ${{dm.type.c_ident}}& local_${{dm.ident}}) { m_${{dm code(''' // Output operator declaration -std::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj); +std::ostream& +operator<<(std::ostream& out, const ${{self.c_ident}}& obj); // Output operator definition -extern inline -std::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj) +extern inline std::ostream& +operator<<(std::ostream& out, const ${{self.c_ident}}& obj) { obj.print(out); out << std::flush; return out; } -#endif // ${{self.c_ident}}_H +#endif // __${{self.c_ident}}_HH__ ''') code.write(path, "%s.hh" % self.c_ident) @@ -396,7 +423,8 @@ using namespace std; code('Allocator<${{self.c_ident}}>* ${{self.c_ident}}::s_allocator_ptr = NULL;') code(''' /** \\brief Print the state of this object */ -void ${{self.c_ident}}::print(ostream& out) const +void +${{self.c_ident}}::print(ostream& out) const { out << "[${{self.c_ident}}: "; ''') @@ -424,14 +452,16 @@ void ${{self.c_ident}}::print(ostream& out) const * * Auto generated C++ code started by $__file__:$__line__ */ -#ifndef ${{self.c_ident}}_H -#define ${{self.c_ident}}_H + +#ifndef __${{self.c_ident}}_HH__ +#define __${{self.c_ident}}_HH__ #include #include #include "mem/ruby/common/Global.hh" +// Class definition /** \\enum ${{self.c_ident}} * \\brief ${{self.desc}} */ @@ -452,8 +482,14 @@ enum ${{self.c_ident}} { code(''' ${{self.c_ident}}_NUM }; + +// Code to convert from a string to the enumeration ${{self.c_ident}} string_to_${{self.c_ident}}(const std::string& str); + +// Code to convert state to a string std::string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj); + +// Code to increment an enumeration type ${{self.c_ident}} &operator++(${{self.c_ident}} &e); ''') @@ -473,7 +509,7 @@ int ${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj); code(''' std::ostream& operator<<(std::ostream& out, const ${{self.c_ident}}& obj); -#endif // ${{self.c_ident}}_H +#endif // __${{self.c_ident}}_HH__ ''') code.write(path, "%s.hh" % self.c_ident) @@ -500,14 +536,18 @@ using namespace std; code('#include "mem/protocol/${{enum.ident}}_Controller.hh"') code(''' -ostream& operator<<(ostream& out, const ${{self.c_ident}}& obj) +// Code for output operator +ostream& +operator<<(ostream& out, const ${{self.c_ident}}& obj) { out << ${{self.c_ident}}_to_string(obj); out << flush; return out; } -string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj) +// Code to convert state to a string +string +${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj) { switch(obj) { ''') @@ -527,17 +567,19 @@ string ${{self.c_ident}}_to_string(const ${{self.c_ident}}& obj) } } -${{self.c_ident}} string_to_${{self.c_ident}}(const string& str) +// Code to convert from a string to the enumeration +${{self.c_ident}} +string_to_${{self.c_ident}}(const string& str) { ''') # For each field + start = "" code.indent() - code("if (false) {") - start = "} else " for enum in self.enums.itervalues(): code('${start}if (str == "${{enum.ident}}") {') code(' return ${{self.c_ident}}_${{enum.ident}};') + start = "} else " code.dedent() code(''' @@ -547,7 +589,10 @@ ${{self.c_ident}} string_to_${{self.c_ident}}(const string& str) } } -${{self.c_ident}}& operator++(${{self.c_ident}}& e) { +// Code to increment an enumeration type +${{self.c_ident}}& +operator++(${{self.c_ident}}& e) +{ assert(e < ${{self.c_ident}}_NUM); return e = ${{self.c_ident}}(e+1); } @@ -557,12 +602,14 @@ ${{self.c_ident}}& operator++(${{self.c_ident}}& e) { # components for each Machine if self.isMachineType: code(''' -/** \\brief returns the base vector index for each machine type to be used by NetDest +/** \\brief returns the base vector index for each machine type to be + * used by NetDest * * \\return the base vector index for each machine type to be used by NetDest * \\see NetDest.hh */ -int ${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj) +int +${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj) { switch(obj) { ''') @@ -587,9 +634,10 @@ int ${{self.c_ident}}_base_level(const ${{self.c_ident}}& obj) /** \\brief returns the machine type for each base vector index used by NetDest * - * \\return the MachineTYpe + * \\return the MachineType */ -MachineType ${{self.c_ident}}_from_base_level(int type) +MachineType +${{self.c_ident}}_from_base_level(int type) { switch(type) { ''') @@ -614,7 +662,8 @@ MachineType ${{self.c_ident}}_from_base_level(int type) * * \\return the base number of components for each machine */ -int ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj) +int +${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj) { int base = 0; switch(obj) { @@ -641,7 +690,8 @@ int ${{self.c_ident}}_base_number(const ${{self.c_ident}}& obj) /** \\brief returns the total number of components for each machine * \\return the total number of components for each machine */ -int ${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj) +int +${{self.c_ident}}_base_count(const ${{self.c_ident}}& obj) { switch(obj) { ''') -- cgit v1.2.3