ObjDeclAST.py revision 6657
113207Sgabeblack@google.com# Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
213207Sgabeblack@google.com# Copyright (c) 2009 The Hewlett-Packard Development Company
313207Sgabeblack@google.com# All rights reserved.
413207Sgabeblack@google.com#
513207Sgabeblack@google.com# Redistribution and use in source and binary forms, with or without
613207Sgabeblack@google.com# modification, are permitted provided that the following conditions are
713207Sgabeblack@google.com# met: redistributions of source code must retain the above copyright
813207Sgabeblack@google.com# notice, this list of conditions and the following disclaimer;
913207Sgabeblack@google.com# redistributions in binary form must reproduce the above copyright
1013207Sgabeblack@google.com# notice, this list of conditions and the following disclaimer in the
1113207Sgabeblack@google.com# documentation and/or other materials provided with the distribution;
1213207Sgabeblack@google.com# neither the name of the copyright holders nor the names of its
1313207Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
1413207Sgabeblack@google.com# this software without specific prior written permission.
1513207Sgabeblack@google.com#
1613207Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713207Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813207Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913207Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013207Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113207Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213207Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313207Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413207Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513207Sgabeblack@google.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613207Sgabeblack@google.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713207Sgabeblack@google.com
2813207Sgabeblack@google.comfrom slicc.ast.DeclAST import DeclAST
2913207Sgabeblack@google.comfrom slicc.symbols import Var
3013207Sgabeblack@google.com
3113207Sgabeblack@google.comclass ObjDeclAST(DeclAST):
3213207Sgabeblack@google.com    def __init__(self, slicc, type_ast, ident, pairs):
3313207Sgabeblack@google.com        super(ObjDeclAST, self).__init__(slicc, pairs)
3413273Sgabeblack@google.com
3513207Sgabeblack@google.com        self.type_ast = type_ast
3613207Sgabeblack@google.com        self.ident = ident
3713239Sgabeblack@google.com
3813207Sgabeblack@google.com    def __repr__(self):
3913207Sgabeblack@google.com        return "[ObjDecl: %r]" % self.ident
4013207Sgabeblack@google.com
4113207Sgabeblack@google.com    def generate(self):
4213207Sgabeblack@google.com        machineComponentSym = False
4313207Sgabeblack@google.com
4413207Sgabeblack@google.com        self["chip_object"] = "yes"
4513207Sgabeblack@google.com
4613288Sgabeblack@google.com        if "hack" in self:
4713207Sgabeblack@google.com            warning("'hack=' is now deprecated")
4813207Sgabeblack@google.com
4913207Sgabeblack@google.com        if "network" in self and "virtual_network" not in self:
5013207Sgabeblack@google.com            self.error("Network queues require a 'virtual_network' attribute")
5113207Sgabeblack@google.com
5213207Sgabeblack@google.com        type = self.type_ast.type
5313207Sgabeblack@google.com        if type.isBuffer and "ordered" not in self:
5413207Sgabeblack@google.com            self.error("Buffer object decls require an 'ordered' attribute")
5513207Sgabeblack@google.com
5613207Sgabeblack@google.com        if "ordered" in self:
5713207Sgabeblack@google.com            value = self["ordered"]
5813207Sgabeblack@google.com
5913207Sgabeblack@google.com            if value not in ("true", "false"):
6013207Sgabeblack@google.com                self.error("The 'ordered' attribute is '%s' " + \
6113273Sgabeblack@google.com                           "must be 'true' or 'false'.", value)
6213273Sgabeblack@google.com
6313207Sgabeblack@google.com        if "random" in self:
6413207Sgabeblack@google.com            value = self["random"]
6513288Sgabeblack@google.com            if value not in ("true", "false"):
6613207Sgabeblack@google.com                self.error("The 'random' attribute is '%s' " + \
6713207Sgabeblack@google.com                           "must be 'true' or 'false'.", value)
6813239Sgabeblack@google.com
6913207Sgabeblack@google.com        machine = self.symtab.state_machine
7013239Sgabeblack@google.com
7113239Sgabeblack@google.com        # FIXME : should all use accessors here to avoid public member
7213239Sgabeblack@google.com        # variables
7313239Sgabeblack@google.com        if self.ident == "id":
7413239Sgabeblack@google.com            c_code = "m_chip_ptr.getID()"
7513239Sgabeblack@google.com        elif self.ident == "version":
7613239Sgabeblack@google.com            c_code = "m_version"
7713239Sgabeblack@google.com        elif self.ident == "machineID":
7813239Sgabeblack@google.com            c_code = "m_machineID"
7913207Sgabeblack@google.com        elif machine:
8013239Sgabeblack@google.com            c_code = "(*m_%s_%s_ptr)" % (machine.ident, self.ident)
8113207Sgabeblack@google.com        else:
8213207Sgabeblack@google.com            c_code = "(*m_%s_ptr)" % (self.ident)
8313207Sgabeblack@google.com
8413207Sgabeblack@google.com        v = Var(self.symtab, self.ident, self.location, type, c_code,
8513207Sgabeblack@google.com                self.pairs, machine)
8613273Sgabeblack@google.com
8713273Sgabeblack@google.com        if machine:
8813207Sgabeblack@google.com            machine.addObject(v)
8913207Sgabeblack@google.com
9013207Sgabeblack@google.com        self.symtab.newSymbol(v)
9113207Sgabeblack@google.com
9213207Sgabeblack@google.com        # used to cheat-- that is, access components in other machines
9313207Sgabeblack@google.com        if machineComponentSym:
9413207Sgabeblack@google.com            self.symtab.newMachComponentSym(v)
9513207Sgabeblack@google.com