parser.py revision 6657:ef5fae93a3b2
1# Copyright (c) 2009 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
28
29import os.path
30import re
31import sys
32
33from m5.util.grammar import Grammar, TokenError, ParseError
34
35import slicc.ast as ast
36import slicc.util as util
37from slicc.symbols import SymbolTable
38
39def read_slicc(sources):
40    if not isinstance(sources, (list,tuple)):
41        sources = [ sources ]
42
43    for source in sources:
44        for sm_file in file(source, "r"):
45            sm_file = sm_file.strip()
46            if not sm_file:
47                continue
48            if sm_file.startswith("#"):
49                continue
50            yield sm_file
51
52class SLICC(Grammar):
53    def __init__(self, **kwargs):
54        super(SLICC, self).__init__(**kwargs)
55        self.decl_list_vec = []
56        self.current_file = None
57        self.symtab = SymbolTable()
58
59    def parse(self, filename):
60        self.current_file = filename
61        f = file(filename, 'r')
62        text = f.read()
63        try:
64            decl_list = super(SLICC, self).parse(text)
65        except (TokenError, ParseError), e:
66            sys.exit("%s: %s:%d" % (e, filename, e.token.lineno))
67        self.decl_list_vec.append(decl_list)
68        self.current_file = None
69
70    def _load(self, *filenames):
71        filenames = list(filenames)
72        while filenames:
73            f = filenames.pop(0)
74            if isinstance(f, (list, tuple)):
75                filenames[0:0] = list(f)
76                continue
77
78            yield f
79            if f.endswith(".slicc"):
80                dirname,basename = os.path.split(f)
81                filenames[0:0] = [ os.path.join(dirname, x) \
82                                   for x in read_slicc(f)]
83            else:
84                assert f.endswith(".sm")
85                self.parse(f)
86
87    def load(self, *filenames, **kwargs):
88        verbose = kwargs.pop("verbose", False)
89        if kwargs:
90            raise TypeError
91
92        gen = self._load(*filenames)
93        if verbose:
94            return gen
95        else:
96            # Run out the generator if we don't want the verbosity
97            for foo in gen:
98                pass
99
100    def findMachines(self):
101        for decl_list in self.decl_list_vec:
102            decl_list.findMachines()
103
104    def generate(self):
105        for decl_list in self.decl_list_vec:
106            decl_list.generate()
107
108    def writeCodeFiles(self, code_path):
109        util.makeDir(code_path)
110        self.symtab.writeCodeFiles(code_path)
111
112    def writeHTMLFiles(self, code_path):
113        util.makeDir(code_path)
114        self.symtab.writeHTMLFiles(code_path)
115
116    def files(self):
117        cc = set([
118            'ControllerFactory.cc',
119            'MachineType.cc'])
120
121        hh = set([
122            'ControllerFactory.hh',
123            'MachineType.hh',
124            'Types.hh' ])
125
126        for decl_list in self.decl_list_vec:
127            decl_list.files(hh, cc)
128
129        return hh, cc
130
131    t_ignore = '\t '
132
133    # C or C++ comment (ignore)
134    def t_c_comment(self, t):
135        r'/\*(.|\n)*?\*/'
136        t.lexer.lineno += t.value.count('\n')
137
138    def t_cpp_comment(self, t):
139        r'//.*'
140
141    # Define a rule so we can track line numbers
142    def t_newline(self, t):
143        r'\n+'
144        t.lexer.lineno += len(t.value)
145
146    reserved = {
147        'global' : 'GLOBAL',
148        'machine' : 'MACHINE',
149        'in_port' : 'IN_PORT',
150        'out_port' : 'OUT_PORT',
151        'action' : 'ACTION',
152        'transition' : 'TRANS',
153        'structure' : 'STRUCT',
154        'external_type' : 'EXTERN_TYPE',
155        'enumeration' : 'ENUM',
156        'peek' : 'PEEK',
157        'enqueue' : 'ENQUEUE',
158        'copy_head' : 'COPY_HEAD',
159        'check_allocate' : 'CHECK_ALLOCATE',
160        'check_stop_slots' : 'CHECK_STOP_SLOTS',
161        'if' : 'IF',
162        'else' : 'ELSE',
163        'return' : 'RETURN',
164        'THIS' : 'THIS',
165        'CHIP' : 'CHIP',
166        'void' : 'VOID',
167        'new' : 'NEW',
168    }
169
170    literals = ':[]{}(),='
171
172    tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
173               'LEFTSHIFT', 'RIGHTSHIFT',
174               'NOT', 'AND', 'OR',
175               'PLUS', 'DASH', 'STAR', 'SLASH',
176               'DOUBLE_COLON', 'SEMI',
177               'ASSIGN', 'DOT',
178               'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
179    tokens += reserved.values()
180
181    t_EQ = r'=='
182    t_NE = r'!='
183    t_LT = r'<'
184    t_GT = r'>'
185    t_LE = r'<='
186    t_GE = r'>='
187    t_LEFTSHIFT = r'<<'
188    t_RIGHTSHIFT = r'>>'
189    t_NOT = r'!'
190    t_AND = r'&&'
191    t_OR = r'\|\|'
192    t_PLUS = r'\+'
193    t_DASH = r'-'
194    t_STAR = r'\*'
195    t_SLASH = r'/'
196    t_DOUBLE_COLON = r'::'
197    t_SEMI = r';'
198    t_ASSIGN = r':='
199    t_DOT = r'\.'
200
201    precedence = (
202        ('left', 'AND', 'OR'),
203        ('left', 'EQ', 'NE'),
204        ('left', 'LT', 'GT', 'LE', 'GE'),
205        ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
206        ('left', 'PLUS', 'DASH'),
207        ('left', 'STAR', 'SLASH'),
208        ('right', 'NOT', 'UMINUS'),
209    )
210
211    def t_IDENT(self, t):
212        r'[a-zA-Z_][a-zA-Z_0-9]*'
213        if t.value == 'true':
214            t.type = 'LIT_BOOL'
215            t.value = True
216            return t
217
218        if t.value == 'false':
219            t.type = 'LIT_BOOL'
220            t.value = False
221            return t
222
223        # Check for reserved words
224        t.type = self.reserved.get(t.value, 'IDENT')
225        return t
226
227    def t_FLOATNUMBER(self, t):
228        '[0-9]+[.][0-9]+'
229        try:
230            t.value = float(t.value)
231        except ValueError:
232            raise TokenError("Illegal float", t)
233        return t
234
235    def t_NUMBER(self, t):
236        r'[0-9]+'
237        try:
238            t.value = int(t.value)
239        except ValueError:
240            raise TokenError("Illegal number", t)
241        return t
242
243    def t_STRING1(self, t):
244        r'\"[^"\n]*\"'
245        t.type = 'STRING'
246        t.value = t.value[1:-1]
247        return t
248
249    def t_STRING2(self, t):
250        r"\'[^'\n]*\'"
251        t.type = 'STRING'
252        t.value = t.value[1:-1]
253        return t
254
255    def p_file(self, p):
256        "file : decls"
257        p[0] = p[1]
258
259    def p_empty(self, p):
260        "empty :"
261
262    def p_decls(self, p):
263        "decls : declsx"
264        p[0] = ast.DeclListAST(self, p[1])
265
266    def p_declsx__list(self, p):
267        "declsx : decl declsx"
268        p[0] = [ p[1] ] + p[2]
269
270    def p_declsx__none(self, p):
271        "declsx : empty"
272        p[0] = []
273
274    def p_decl__machine(self, p):
275        "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
276        p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9])
277
278    def p_decl__action(self, p):
279        "decl : ACTION '(' ident pairs ')' statements"
280        p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6])
281
282    def p_decl__in_port(self, p):
283        "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
284        p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10])
285
286    def p_decl__out_port(self, p):
287        "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
288        p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8])
289
290    def p_decl__trans0(self, p):
291        "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
292        p[0] = ast.TransitionDeclAST(self, p[3], p[5], p[7], p[8], p[10])
293
294    def p_decl__trans1(self, p):
295        "decl : TRANS '(' idents ',' idents           pairs ')' idents"
296        p[0] = ast.TransitionDeclAST(self, p[3], p[5], None, p[6], p[8])
297
298    def p_decl__extern0(self, p):
299        "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
300        p[4]["external"] = "yes"
301        p[0] = ast.TypeDeclAST(self, p[3], p[4], [])
302
303    def p_decl__extern1(self, p):
304        "decl : EXTERN_TYPE '(' type pairs ')' '{' type_methods '}'"
305        p[4]["external"] = "yes"
306        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
307
308    def p_decl__global(self, p):
309        "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
310        p[4]["global"] = "yes"
311        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
312
313    def p_decl__struct(self, p):
314        "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
315        p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
316
317    def p_decl__enum(self, p):
318        "decl : ENUM '(' type pairs ')' '{' type_enums   '}'"
319        p[4]["enumeration"] = "yes"
320        p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7])
321
322    def p_decl__object(self, p):
323        "decl : type ident pairs SEMI"
324        p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
325
326    def p_decl__func_decl(self, p):
327        """decl : void ident '(' params ')' pairs SEMI
328                | type ident '(' params ')' pairs SEMI"""
329        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
330
331    def p_decl__func_def(self, p):
332        """decl : void ident '(' params ')' pairs statements
333                | type ident '(' params ')' pairs statements"""
334        p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
335
336    # Type fields
337    def p_type_members__list(self, p):
338        "type_members : type_member type_members"
339        p[0] = [ p[1] ] + p[2]
340
341    def p_type_members__empty(self, p):
342        "type_members : empty"
343        p[0] = []
344
345    def p_type_member__1(self, p):
346        "type_member : type ident pairs SEMI"
347        p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
348
349    def p_type_member__2(self, p):
350        "type_member : type ident ASSIGN expr SEMI"
351        p[0] = ast.TypeFieldMemberAST(self, p[1], p[2],
352                                      ast.PairListAST(self), p[4])
353
354    # Methods
355    def p_type_methods__list(self, p):
356        "type_methods : type_method type_methods"
357        p[0] = [ p[1] ] + p[2]
358
359    def p_type_methods(self, p):
360        "type_methods : empty"
361        p[0] = []
362
363    def p_type_method(self, p):
364        "type_method : type_or_void ident '(' types ')' pairs SEMI"
365        p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
366
367    # Enum fields
368    def p_type_enums__list(self, p):
369        "type_enums : type_enum type_enums"
370        p[0] = [ p[1] ] + p[2]
371
372    def p_type_enums__empty(self, p):
373        "type_enums : empty"
374        p[0] = []
375
376    def p_type_enum(self, p):
377        "type_enum : ident pairs SEMI"
378        p[0] = ast.TypeFieldEnumAST(self, p[1], p[2])
379
380    # Type
381    def p_types__multiple(self, p):
382        "types : type ',' types"
383        p[0] = [ p[1] ] + p[3]
384
385    def p_types__one(self, p):
386        "types : type"
387        p[0] = [ p[1] ]
388
389    def p_types__empty(self, p):
390        "types : empty"
391        p[0] = []
392
393    def p_type(self, p):
394        "type : ident"
395        p[0] = ast.TypeAST(self, p[1])
396
397    def p_void(self, p):
398        "void : VOID"
399        p[0] = ast.TypeAST(self, p[1])
400
401    def p_type_or_void(self, p):
402        """type_or_void : type
403                        | void"""
404        p[0] = p[1]
405
406    # Formal Param
407    def p_params__many(self, p):
408        "params : param ',' params"
409        p[0] = [ p[1] ] + p[3]
410
411    def p_params__one(self, p):
412        "params : param"
413        p[0] = [ p[1] ]
414
415    def p_params__none(self, p):
416        "params : empty"
417        p[0] = []
418
419    def p_param(self, p):
420        "param : type ident"
421        p[0] = ast.FormalParamAST(self, p[1], p[2])
422
423    # Idents and lists
424    def p_idents__braced(self, p):
425        "idents : '{' identx '}'"
426        p[0] = p[2]
427
428    def p_idents__bare(self, p):
429        "idents : ident"
430        p[0] = [ p[1] ]
431
432    def p_identx__multiple_1(self, p):
433        """identx : ident SEMI identx
434                  | ident ',' identx"""
435        p[0] = [ p[1] ] + p[3]
436
437    def p_identx__multiple_2(self, p):
438        "identx : ident identx"
439        p[0] = [ p[1] ] + p[2]
440
441    def p_identx__single(self, p):
442        "identx : empty"
443        p[0] = [ ]
444
445    def p_ident(self, p):
446        "ident : IDENT"
447        p[0] = p[1]
448
449    # Pair and pair lists
450    def p_pairs__list(self, p):
451        "pairs : ',' pairsx"
452        p[0] = p[2]
453
454    def p_pairs__empty(self, p):
455        "pairs : empty"
456        p[0] = ast.PairListAST(self)
457
458    def p_pairsx__many(self, p):
459        "pairsx : pair ',' pairsx"
460        p[0] = p[3]
461        p[0].addPair(p[1])
462
463    def p_pairsx__one(self, p):
464        "pairsx : pair"
465        p[0] = ast.PairListAST(self)
466        p[0].addPair(p[1])
467
468    def p_pair__assign(self, p):
469        """pair : ident '=' STRING
470                | ident '=' ident"""
471        p[0] = ast.PairAST(self, p[1], p[3])
472
473    def p_pair__literal(self, p):
474        "pair : STRING"
475        p[0] = ast.PairAST(self, "short", p[1])
476
477    # Below are the rules for action descriptions
478    def p_statements__inner(self, p):
479        "statements : '{' statements_inner '}'"
480        p[0] = ast.StatementListAST(self, p[2])
481
482    def p_statements__none(self, p):
483        "statements : '{' '}'"
484        p[0] = ast.StatementListAST(self, [])
485
486    def p_statements_inner__many(self, p):
487        "statements_inner : statement statements_inner"
488        p[0] = [ p[1] ] + p[2]
489
490    def p_statements_inner__one(self, p):
491        "statements_inner : statement"
492        p[0] = [ p[1] ]
493
494    def p_exprs__multiple(self, p):
495        "exprs : expr ',' exprs"
496        p[0] = [ p[1] ] + p[3]
497
498    def p_exprs__one(self, p):
499        "exprs : expr"
500        p[0] = [ p[1] ]
501
502    def p_exprs__empty(self, p):
503        "exprs : empty"""
504        p[0] = []
505
506    def p_statement__expression(self, p):
507        "statement : expr SEMI"
508        p[0] = ast.ExprStatementAST(self, p[1])
509
510    def p_statement__assign(self, p):
511        "statement : expr ASSIGN expr SEMI"
512        p[0] = ast.AssignStatementAST(self, p[1], p[3])
513
514    def p_statement__enqueue(self, p):
515        "statement : ENQUEUE '(' var ',' type pairs ')' statements"
516        p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[6], p[8])
517
518    def p_statement__peek(self, p):
519        "statement : PEEK '(' var ',' type ')' statements"
520        p[0] = ast.PeekStatementAST(self, p[3], p[5], p[7], "peek")
521
522    def p_statement__copy_head(self, p):
523        "statement : COPY_HEAD '(' var ',' var pairs ')' SEMI"
524        p[0] = ast.CopyHeadStatementAST(self, p[3], p[5], p[6])
525
526    def p_statement__check_allocate(self, p):
527        "statement : CHECK_ALLOCATE '(' var ')' SEMI"
528        p[0] = ast.CheckAllocateStatementAST(self, p[3])
529
530    def p_statement__check_stop(self, p):
531        "statement : CHECK_STOP_SLOTS '(' var ',' STRING ',' STRING ')' SEMI"
532        p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
533
534    def p_statement__return(self, p):
535        "statement : RETURN expr SEMI"
536        p[0] = ast.ReturnStatementAST(self, p[2])
537
538    def p_statement__if(self, p):
539        "statement : if_statement"
540        p[0] = p[1]
541
542    def p_if_statement__if(self, p):
543        "if_statement : IF '(' expr ')' statements"
544        p[0] = ast.IfStatementAST(self, p[3], p[5], None)
545
546    def p_if_statement__if_else(self, p):
547        "if_statement : IF '(' expr ')' statements ELSE statements"
548        p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
549
550    def p_statement__if_else_if(self, p):
551        "if_statement : IF '(' expr ')' statements ELSE if_statement"
552        p[0] = ast.IfStatementAST(self, p[3], p[5],
553                                  ast.StatementListAST(self, p[7]))
554
555    def p_expr__var(self, p):
556        "aexpr : var"
557        p[0] = p[1]
558
559    def p_expr__literal(self, p):
560        "aexpr : literal"
561        p[0] = p[1]
562
563    def p_expr__enumeration(self, p):
564        "aexpr : enumeration"
565        p[0] = p[1]
566
567    def p_expr__func_call(self, p):
568        "aexpr : ident '(' exprs ')'"
569        p[0] = ast.FuncCallExprAST(self, p[1], p[3])
570
571    def p_expr__new(self, p):
572        "aexpr : NEW type"
573        p[0] = ast.NewExprAST(self, p[2])
574
575    # globally access a local chip component and call a method
576    def p_expr__local_chip_method(self, p):
577        "aexpr : THIS DOT var '[' expr ']' DOT var DOT ident '(' exprs ')'"
578        p[0] = ast.LocalChipMethodAST(self, p[3], p[5], p[8], p[10], p[12])
579
580    # globally access a local chip component and access a data member
581    def p_expr__local_chip_member(self, p):
582        "aexpr : THIS DOT var '[' expr ']' DOT var DOT field"
583        p[0] = ast.LocalChipMemberAST(self, p[3], p[5], p[8], p[10])
584
585    # globally access a specified chip component and call a method
586    def p_expr__specified_chip_method(self, p):
587        "aexpr : CHIP '[' expr ']' DOT var '[' expr ']' DOT var DOT ident '(' exprs ')'"
588        p[0] = ast.SpecifiedChipMethodAST(self, p[3], p[6], p[8], p[11], p[13],
589                                          p[15])
590
591    # globally access a specified chip component and access a data member
592    def p_expr__specified_chip_member(self, p):
593        "aexpr : CHIP '[' expr ']' DOT var '[' expr ']' DOT var DOT field"
594        p[0] = ast.SpecifiedChipMemberAST(self, p[3], p[6], p[8], p[11], p[13])
595
596    def p_expr__member(self, p):
597        "aexpr : aexpr DOT ident"
598        p[0] = ast.MemberExprAST(self, p[1], p[3])
599
600    def p_expr__member_method_call(self, p):
601        "aexpr : aexpr DOT ident '(' exprs ')'"
602        p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
603
604    def p_expr__member_method_call_lookup(self, p):
605        "aexpr : aexpr '[' exprs ']'"
606        p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup", p[3])
607
608    def p_expr__class_method_call(self, p):
609        "aexpr : type DOUBLE_COLON ident '(' exprs ')'"
610        p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
611
612    def p_expr__aexpr(self, p):
613        "expr : aexpr"
614        p[0] = p[1]
615
616    def p_expr__binary_op(self, p):
617        """expr : expr STAR  expr
618                | expr SLASH expr
619                | expr PLUS  expr
620                | expr DASH  expr
621                | expr LT    expr
622                | expr GT    expr
623                | expr LE    expr
624                | expr GE    expr
625                | expr EQ    expr
626                | expr NE    expr
627                | expr AND   expr
628                | expr OR    expr
629                | expr RIGHTSHIFT expr
630                | expr LEFTSHIFT  expr"""
631        p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
632
633    # FIXME - unary not
634    def p_expr__unary_op(self, p):
635        """expr : NOT expr
636                | DASH expr %prec UMINUS"""
637        p[0] = PrefixOperatorExpr(p[1], p[2])
638
639    def p_expr__parens(self, p):
640        "aexpr : '(' expr ')'"
641        p[0] = p[2]
642
643    def p_literal__string(self, p):
644        "literal : STRING"
645        p[0] = ast.LiteralExprAST(self, p[1], "string")
646
647    def p_literal__number(self, p):
648        "literal : NUMBER"
649        p[0] = ast.LiteralExprAST(self, p[1], "int")
650
651    def p_literal__float(self, p):
652        "literal : FLOATNUMBER"
653        p[0] = ast.LiteralExprAST(self, p[1], "int")
654
655    def p_literal__bool(self, p):
656        "literal : LIT_BOOL"
657        p[0] = ast.LiteralExprAST(self, p[1], "bool")
658
659    def p_enumeration(self, p):
660        "enumeration : ident ':' ident"
661        p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
662
663    def p_var(self, p):
664        "var : ident"
665        p[0] = ast.VarExprAST(self, p[1])
666
667    def p_field(self, p):
668        "field : ident"
669        p[0] = p[1]
670