slicc: have a central mechanism for creating a code_formatter.

This makes it easier to add global variables like protocol
This commit is contained in:
Nathan Binkert 2010-03-10 16:22:26 -08:00
parent 1068ca85d0
commit cf86532857
20 changed files with 59 additions and 73 deletions

View file

@ -63,8 +63,9 @@ def slicc_scanner(node, env, path):
env.Append(SCANNERS=Scanner(function=slicc_scanner,skeys=['.slicc']))
def slicc_emitter(target, source, env):
protocol = source[0].get_contents()
files = [s.srcnode().abspath for s in source[1:]]
slicc = SLICC(debug=True)
slicc = SLICC(protocol, debug=True)
print "SLICC parsing..."
for name in slicc.load(files, verbose=True):
print " %s" % name
@ -104,7 +105,7 @@ def slicc_action(target, source, env):
if not isdir(hdir):
os.mkdir(hdir)
slicc = SLICC(debug=True)
slicc = SLICC(protocol, debug=True)
files = [str(s) for s in source[1:]]
slicc.load(files, verbose=False)

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.util import PairContainer, Location
class AST(PairContainer):
@ -54,7 +52,7 @@ class AST(PairContainer):
def embedError(self, message, *args):
if args:
message = message % args
code = code_formatter()
code = self.slicc.codeFormatter()
code('''
cerr << "Runtime Error at ${{self.location}}, Ruby Time: " << g_eventQueue_ptr->getTime() << ": "<< $message << ", PID: " << getpid() << endl;
char c; cerr << "press return to continue." << endl; cin.get(c); abort();

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.DeclAST import DeclAST
from slicc.symbols import Action, Type, Var
@ -55,7 +53,7 @@ class ActionDeclAST(DeclAST):
self.symtab.newSymbol(var)
# Do not allows returns in actions
code = code_formatter()
code = self.slicc.codeFormatter()
self.statement_list.generate(code, None)
self.pairs["c_code"] = str(code)

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.StatementAST import StatementAST
class AssignStatementAST(StatementAST):
@ -39,8 +37,8 @@ class AssignStatementAST(StatementAST):
return "[AssignStatementAST: %r := %r]" % (self.lvalue, self.rvalue)
def generate(self, code, return_type):
lcode = code_formatter()
rcode = code_formatter()
lcode = self.slicc.codeFormatter()
rcode = self.slicc.codeFormatter()
ltype = self.lvalue.generate(lcode)
rtype = self.rvalue.generate(rcode)

View file

@ -45,7 +45,7 @@ class CheckStopSlotsStatementAST(StatementAST):
var = self.variable.var
assert var not in self.resources
check_code = code_formatter()
check_code = self.slicc.codeFormatter()
if self.condStr == "((*in_msg_ptr)).m_isOnChipSearch":
check_code('''
const Response9Msg* in_msg_ptr =

View file

@ -24,8 +24,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.AST import AST
class ExprAST(AST):
@ -37,7 +35,7 @@ class ExprAST(AST):
pass
def inline(self, get_type=False):
code = code_formatter(fix_newlines=False)
code = self.slicc.codeFormatter(fix_newlines=False)
return_type = self.generate(code)
if get_type:
return return_type, code

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.StatementAST import StatementAST
from slicc.symbols import Type

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util.code_formatter import code_formatter
from slicc.ast.DeclAST import DeclAST
from slicc.symbols import Func, Type
@ -70,7 +68,7 @@ class FuncDeclAST(DeclAST):
types.append(type)
params.append(ident)
body = code_formatter()
body = self.slicc.codeFormatter()
if self.statements is None:
self["external"] = "yes"
else:

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.StatementAST import StatementAST
from slicc.symbols import Type
@ -45,7 +43,7 @@ class IfStatementAST(StatementAST):
return "[IfStatement: %r%r%r]" % (self.cond, self.then, self.else_)
def generate(self, code, return_type):
cond_code = code_formatter()
cond_code = self.slicc.codeFormatter()
cond_type = self.cond.generate(cond_code)
if cond_type != self.symtab.find("bool", Type):

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.DeclAST import DeclAST
from slicc.ast.TypeAST import TypeAST
from slicc.symbols import Func, Type, Var
@ -48,7 +46,7 @@ class InPortDeclAST(DeclAST):
symtab = self.symtab
void_type = symtab.find("void", Type)
code = code_formatter()
code = self.slicc.codeFormatter()
queue_type = self.var_expr.generate(code)
if not queue_type.isInPort:
self.error("The inport queue's type must have the 'inport' " + \
@ -115,7 +113,7 @@ class InPortDeclAST(DeclAST):
symtab.newSymbol(func)
if self.statements is not None:
rcode = code_formatter()
rcode = self.slicc.codeFormatter()
rcode.indent()
rcode.indent()
self.statements.generate(rcode, None)

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.ExprAST import ExprAST
from slicc.symbols import Type
@ -42,8 +40,8 @@ class InfixOperatorExprAST(ExprAST):
return "[InfixExpr: %r %s %r]" % (self.left, self.op, self.right)
def generate(self, code):
lcode = code_formatter()
rcode = code_formatter()
lcode = self.slicc.codeFormatter()
rcode = self.slicc.codeFormatter()
ltype = self.left.generate(lcode)
rtype = self.right.generate(rcode)

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.ExprAST import ExprAST
class MemberExprAST(ExprAST):

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.ExprAST import ExprAST
class MethodCallExprAST(ExprAST):
@ -36,7 +34,7 @@ class MethodCallExprAST(ExprAST):
self.expr_ast_vec = expr_ast_vec
def generate(self, code):
tmp = code_formatter()
tmp = self.slicc.codeFormatter()
paramTypes = []
for expr_ast in self.expr_ast_vec:
return_type = expr_ast.generate(tmp)
@ -91,7 +89,7 @@ class MemberMethodCallExprAST(MethodCallExprAST):
self.obj_expr_ast,
self.expr_ast_vec)
def generate_prefix(self, paramTypes):
code = code_formatter()
code = self.slicc.codeFormatter()
# member method call
obj_type = self.obj_expr_ast.generate(code)

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.ast.DeclAST import DeclAST
from slicc.ast.TypeAST import TypeAST
from slicc.symbols import Var
@ -45,7 +43,7 @@ class OutPortDeclAST(DeclAST):
return "[OutPortDecl: %r]" % self.ident
def generate(self):
code = code_formatter(newlines=False)
code = self.slicc.codeFormatter(newlines=False)
queue_type = self.var_expr.generate(code)
if not queue_type.isOutPort:

View file

@ -30,6 +30,7 @@ import os.path
import re
import sys
from m5.util import code_formatter
from m5.util.grammar import Grammar, TokenError, ParseError
import slicc.ast as ast
@ -50,11 +51,17 @@ def read_slicc(sources):
yield sm_file
class SLICC(Grammar):
def __init__(self, **kwargs):
def __init__(self, protocol, **kwargs):
super(SLICC, self).__init__(**kwargs)
self.decl_list_vec = []
self.current_file = None
self.symtab = SymbolTable()
self.protocol = protocol
self.symtab = SymbolTable(self)
def codeFormatter(self, *args, **kwargs):
code = code_formatter(*args, **kwargs)
code['protocol'] = self.protocol
return code
def parse(self, filename):
self.current_file = filename

View file

@ -25,8 +25,6 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter
from slicc.symbols.Symbol import Symbol
from slicc.symbols.Type import Type
@ -71,7 +69,7 @@ class Func(Symbol):
if "external" in self:
return
code = code_formatter()
code = self.symtab.codeFormatter()
# Header
code('''

View file

@ -25,7 +25,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter, orderdict
from m5.util import orderdict
from slicc.symbols.Symbol import Symbol
from slicc.symbols.Var import Var
@ -150,7 +150,7 @@ class StateMachine(Symbol):
func.writeCodeFiles(path)
def printControllerPython(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
py_ident = "%s_Controller" % ident
c_ident = "%s_Controller" % self.ident
@ -180,7 +180,7 @@ class $py_ident(RubyController):
def printControllerHH(self, path):
'''Output the method declarations for the class declaration'''
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
c_ident = "%s_Controller" % self.ident
@ -301,7 +301,7 @@ static int m_num_controllers;
def printControllerCC(self, path):
'''Output the actions for performing the actions'''
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
c_ident = "%s_Controller" % self.ident
@ -641,7 +641,7 @@ void $c_ident::${{action.ident}}(const Address& addr)
def printCWakeup(self, path):
'''Output the wakeup loop for the events'''
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
code('''
@ -696,7 +696,7 @@ void ${ident}_Controller::wakeup()
def printCSwitch(self, path):
'''Output switch statement for transition table'''
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
code('''
@ -778,7 +778,7 @@ TransitionResult ${ident}_Controller::doTransitionWorker(${ident}_Event event, $
case_string = "%s_State_%s, %s_Event_%s" % \
(self.ident, trans.state.ident, self.ident, trans.event.ident)
case = code_formatter()
case = self.symtab.codeFormatter()
# Only set next_state if it changes
if trans.state != trans.nextState:
ns_ident = trans.nextState.ident
@ -853,7 +853,7 @@ if (!%s.areNSlotsAvailable(%s)) {
code.write(path, "%s_Transitions.cc" % self.ident)
def printProfilerHH(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
code('''
@ -888,7 +888,7 @@ class ${ident}_Profiler {
code.write(path, "%s_Profiler.hh" % self.ident)
def printProfilerCC(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
ident = self.ident
code('''
@ -967,7 +967,7 @@ void ${ident}_Profiler::dumpStats(ostream& out) const
# **************************
def frameRef(self, click_href, click_target, over_href, over_target_num,
text):
code = code_formatter(fix_newlines=False)
code = self.symtab.codeFormatter(fix_newlines=False)
code("""<A href=\"$click_href\" target=\"$click_target\" onMouseOver=\"if (parent.frames[$over_target_num].location != parent.location + '$over_href') { parent.frames[$over_target_num].location='$over_href' }\" >${{html.formatShorthand(text)}}</A>""")
return str(code)
@ -998,7 +998,7 @@ void ${ident}_Profiler::dumpStats(ostream& out) const
code.write(path, name)
def printHTMLTransitions(self, path, active_state):
code = code_formatter()
code = self.symtab.codeFormatter()
code('''
<HTML><BODY link="blue" vlink="blue">

View file

@ -33,7 +33,9 @@ from slicc.symbols.Type import Type
from slicc.util import Location
class SymbolTable(object):
def __init__(self):
def __init__(self, slicc):
self.slicc = slicc
self.sym_vec = []
self.sym_map_vec = [ {} ]
self.machine_components = {}
@ -52,6 +54,9 @@ class SymbolTable(object):
def __repr__(self):
return "[SymbolTable]" # FIXME
def codeFormatter(self, *args, **kwargs):
return self.slicc.codeFormatter(*args, **kwargs)
def newSymbol(self, sym):
self.registerSym(str(sym), sym)
self.sym_vec.append(sym)
@ -118,7 +123,7 @@ class SymbolTable(object):
yield symbol
def writeCodeFiles(self, path):
code = code_formatter()
code = self.codeFormatter()
code('''
/** Auto generated C++ code started by $__file__:$__line__ */
@ -140,7 +145,7 @@ class SymbolTable(object):
else:
name = "empty.html"
code = code_formatter()
code = self.codeFormatter()
code('''
<html>
<head>
@ -154,7 +159,7 @@ class SymbolTable(object):
''')
code.write(path, "index.html")
code = code_formatter()
code = self.codeFormatter()
code("<HTML></HTML>")
code.write(path, "empty.html")

View file

@ -25,7 +25,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from m5.util import code_formatter, orderdict
from m5.util import orderdict
from slicc.util import PairContainer
from slicc.symbols.Symbol import Symbol
@ -191,7 +191,7 @@ class Type(Symbol):
self.printTypeCC(path)
def printTypeHH(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
code('''
/** \\file ${{self.c_ident}}.hh
*
@ -375,7 +375,7 @@ ostream& operator<<(ostream& out, const ${{self.c_ident}}& obj)
code.write(path, "%s.hh" % self.c_ident)
def printTypeCC(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
code('''
/** \\file ${{self.c_ident}}.cc
@ -412,7 +412,7 @@ void ${{self.c_ident}}::print(ostream& out) const
code.write(path, "%s.cc" % self.c_ident)
def printEnumHH(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
code('''
/** \\file ${{self.c_ident}}.hh
*
@ -470,7 +470,7 @@ ostream& operator<<(ostream& out, const ${{self.c_ident}}& obj);
code.write(path, "%s.hh" % self.c_ident)
def printEnumCC(self, path):
code = code_formatter()
code = self.symtab.codeFormatter()
code('''
/** \\file ${{self.c_ident}}.hh
*

View file

@ -55,15 +55,14 @@ class lookup(object):
if item == '__line__':
return self.frame.f_lineno
if self.formatter.locals and item in self.frame.f_locals:
return self.frame.f_locals[item]
if item in self.dict:
return self.dict[item]
if self.formatter.locals or self.formatter.globals:
if self.formatter.locals and item in self.frame.f_locals:
return self.frame.f_locals[item]
if self.formatter.globals and item in self.frame.f_globals:
return self.frame.f_globals[item]
if self.formatter.globals and item in self.frame.f_globals:
return self.frame.f_globals[item]
if item in __builtin__.__dict__:
return __builtin__.__dict__[item]
@ -124,7 +123,7 @@ class code_formatter(object):
self._dict = {}
self._indent_level = 0
self._indent_spaces = 4
self.globals = kwargs.pop('globals',type(self).globals)
self.globals = kwargs.pop('globals', type(self).globals)
self.locals = kwargs.pop('locals', type(self).locals)
self._fix_newlines = \
kwargs.pop('fix_newlines', type(self).fix_newlines)