StateDeclAST.py revision 11049
16145Snate@binkert.org# Copyright (c) 2011 Advanced Micro Devices, Inc.
212065Snikos.nikoleris@arm.com# All rights reserved.
312065Snikos.nikoleris@arm.com#
412065Snikos.nikoleris@arm.com# Redistribution and use in source and binary forms, with or without
512065Snikos.nikoleris@arm.com# modification, are permitted provided that the following conditions are
612065Snikos.nikoleris@arm.com# met: redistributions of source code must retain the above copyright
712065Snikos.nikoleris@arm.com# notice, this list of conditions and the following disclaimer;
812065Snikos.nikoleris@arm.com# redistributions in binary form must reproduce the above copyright
912065Snikos.nikoleris@arm.com# notice, this list of conditions and the following disclaimer in the
1012065Snikos.nikoleris@arm.com# documentation and/or other materials provided with the distribution;
1112065Snikos.nikoleris@arm.com# neither the name of the copyright holders nor the names of its
1212065Snikos.nikoleris@arm.com# contributors may be used to endorse or promote products derived from
1312065Snikos.nikoleris@arm.com# this software without specific prior written permission.
146145Snate@binkert.org#
156145Snate@binkert.org# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
166145Snate@binkert.org# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
176145Snate@binkert.org# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
186145Snate@binkert.org# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
196145Snate@binkert.org# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
206145Snate@binkert.org# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
216145Snate@binkert.org# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226145Snate@binkert.org# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236145Snate@binkert.org# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246145Snate@binkert.org# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
256145Snate@binkert.org# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266145Snate@binkert.org
276145Snate@binkert.orgfrom slicc.ast.DeclAST import DeclAST
286145Snate@binkert.orgfrom slicc.symbols import Func, Type
296145Snate@binkert.org
306145Snate@binkert.orgclass StateDeclAST(DeclAST):
316145Snate@binkert.org    def __init__(self, slicc, type_ast, pairs, states):
326145Snate@binkert.org        super(StateDeclAST, self).__init__(slicc, pairs)
336145Snate@binkert.org
346145Snate@binkert.org        self.type_ast = type_ast
356145Snate@binkert.org        self.states = states
366145Snate@binkert.org
376145Snate@binkert.org    def __repr__(self):
386145Snate@binkert.org        return "[StateDecl: %s]" % (self.type_ast)
396145Snate@binkert.org
406145Snate@binkert.org    def files(self, parent=None):
416145Snate@binkert.org        if "external" in self:
427054Snate@binkert.org            return set()
437054Snate@binkert.org
447054Snate@binkert.org        if parent:
457054Snate@binkert.org            ident = "%s_%s" % (parent, self.type_ast.ident)
467054Snate@binkert.org        else:
477054Snate@binkert.org            ident = self.type_ast.ident
487054Snate@binkert.org        s = set(("%s.hh" % ident, "%s.cc" % ident))
497054Snate@binkert.org        return s
507054Snate@binkert.org
516145Snate@binkert.org    def generate(self):
527054Snate@binkert.org        ident = str(self.type_ast)
537054Snate@binkert.org
546145Snate@binkert.org        # Make the new type
557055Snate@binkert.org        t = Type(self.symtab, ident, self.location, self.pairs,
567055Snate@binkert.org                 self.state_machine)
5712065Snikos.nikoleris@arm.com        self.symtab.newSymbol(t)
587454Snate@binkert.org
597055Snate@binkert.org        # Add all of the states of the type to it
6012065Snikos.nikoleris@arm.com        for state in self.states:
6112065Snikos.nikoleris@arm.com            state.generate(t)
6212065Snikos.nikoleris@arm.com
6313784Sgabeblack@google.com        # Add the implicit State_to_string method - FIXME, this is a bit dirty
648257SBrad.Beckmann@amd.com        func_id = "%s_to_string" % t.c_ident
657054Snate@binkert.org
6612065Snikos.nikoleris@arm.com        pairs = { "external" : "yes" }
678645Snilay@cs.wisc.edu        func = Func(self.symtab, func_id + "_" +
689594Snilay@cs.wisc.edu                    t.ident, func_id, self.location,
6913784Sgabeblack@google.com                    self.symtab.find("std::string", Type), [ t ], [], "",
707054Snate@binkert.org                    pairs)
719465Snilay@cs.wisc.edu        self.symtab.newSymbol(func)
726145Snate@binkert.org
736145Snate@binkert.org        # Add the State_to_permission method
746145Snate@binkert.org        func_id = "%s_to_permission" % t.c_ident
756145Snate@binkert.org
769465Snilay@cs.wisc.edu        pairs = { "external" : "yes" }
777054Snate@binkert.org        func = Func(self.symtab, func_id + "_" +
787054Snate@binkert.org                    t.ident, func_id, self.location,
796876Ssteve.reinhardt@amd.com                    self.symtab.find("AccessPermission", Type), [ t ], [], "",
806876Ssteve.reinhardt@amd.com                    pairs)
819594Snilay@cs.wisc.edu        self.symtab.newSymbol(func)
8210917Sbrandon.potter@amd.com