1 # Copyright (c) 2009 The Hewlett-Packard Development Company
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.
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.
27 # Authors: Nathan Binkert
33 from m5
.util
import code_formatter
34 from m5
.util
.grammar
import Grammar
, TokenError
, ParseError
36 import slicc
.ast
as ast
37 import slicc
.util
as util
38 from slicc
.symbols
import SymbolTable
40 def read_slicc(sources
):
41 if not isinstance(sources
, (list,tuple)):
44 for source
in sources
:
45 for sm_file
in file(source
, "r"):
46 sm_file
= sm_file
.strip()
49 if sm_file
.startswith("#"):
54 def __init__(self
, protocol
, **kwargs
):
55 super(SLICC
, self
).__init
__(**kwargs
)
56 self
.decl_list_vec
= []
57 self
.current_file
= None
58 self
.protocol
= protocol
59 self
.symtab
= SymbolTable(self
)
61 def codeFormatter(self
, *args
, **kwargs
):
62 code
= code_formatter(*args
, **kwargs
)
63 code
['protocol'] = self
.protocol
66 def parse(self
, filename
):
67 self
.current_file
= filename
68 f
= file(filename
, 'r')
71 decl_list
= super(SLICC
, self
).parse(text
)
72 except (TokenError
, ParseError
), e
:
73 sys
.exit("%s: %s:%d" % (e
, filename
, e
.token
.lineno
))
74 self
.decl_list_vec
.append(decl_list
)
75 self
.current_file
= None
77 def _load(self
, *filenames
):
78 filenames
= list(filenames
)
81 if isinstance(f
, (list, tuple)):
82 filenames
[0:0] = list(f
)
86 if f
.endswith(".slicc"):
87 dirname
,basename
= os
.path
.split(f
)
88 filenames
[0:0] = [ os
.path
.join(dirname
, x
) \
89 for x
in read_slicc(f
)]
91 assert f
.endswith(".sm")
94 def load(self
, *filenames
, **kwargs
):
95 verbose
= kwargs
.pop("verbose", False)
99 gen
= self
._load
(*filenames
)
103 # Run out the generator if we don't want the verbosity
107 def findMachines(self
):
108 for decl_list
in self
.decl_list_vec
:
109 decl_list
.findMachines()
112 for decl_list
in self
.decl_list_vec
:
115 def writeCodeFiles(self
, code_path
):
116 util
.makeDir(code_path
)
117 self
.symtab
.writeCodeFiles(code_path
)
119 def writeHTMLFiles(self
, code_path
):
120 util
.makeDir(code_path
)
121 self
.symtab
.writeHTMLFiles(code_path
)
129 for decl_list
in self
.decl_list_vec
:
130 f |
= decl_list
.files()
136 # C or C++ comment (ignore)
137 def t_c_comment(self
, t
):
139 t
.lexer
.lineno
+= t
.value
.count('\n')
141 def t_cpp_comment(self
, t
):
144 # Define a rule so we can track line numbers
145 def t_newline(self
, t
):
147 t
.lexer
.lineno
+= len(t
.value
)
151 'machine' : 'MACHINE',
152 'in_port' : 'IN_PORT',
153 'out_port' : 'OUT_PORT',
155 'transition' : 'TRANS',
156 'structure' : 'STRUCT',
157 'external_type' : 'EXTERN_TYPE',
158 'enumeration' : 'ENUM',
159 'state_declaration' : 'STATE_DECL',
161 'stall_and_wait' : 'STALL_AND_WAIT',
162 'wake_up_dependents' : 'WAKE_UP_DEPENDENTS',
163 'enqueue' : 'ENQUEUE',
164 'copy_head' : 'COPY_HEAD',
165 'check_allocate' : 'CHECK_ALLOCATE',
166 'check_stop_slots' : 'CHECK_STOP_SLOTS',
167 'static_cast' : 'STATIC_CAST',
169 'is_valid' : 'IS_VALID',
170 'is_invalid' : 'IS_INVALID',
180 literals
= ':[]{}(),='
182 tokens
= [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
183 'LEFTSHIFT', 'RIGHTSHIFT',
185 'PLUS', 'DASH', 'STAR', 'SLASH',
186 'DOUBLE_COLON', 'SEMI',
188 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
189 tokens
+= reserved
.values()
206 t_DOUBLE_COLON
= r
'::'
212 ('left', 'AND', 'OR'),
213 ('left', 'EQ', 'NE'),
214 ('left', 'LT', 'GT', 'LE', 'GE'),
215 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
216 ('left', 'PLUS', 'DASH'),
217 ('left', 'STAR', 'SLASH'),
218 ('right', 'NOT', 'UMINUS'),
221 def t_IDENT(self
, t
):
222 r
'[a-zA-Z_][a-zA-Z_0-9]*'
223 if t
.value
== 'true':
228 if t
.value
== 'false':
233 # Check for reserved words
234 t
.type = self
.reserved
.get(t
.value
, 'IDENT')
237 def t_FLOATNUMBER(self
, t
):
240 t
.value
= float(t
.value
)
242 raise TokenError("Illegal float", t
)
245 def t_NUMBER(self
, t
):
248 t
.value
= int(t
.value
)
250 raise TokenError("Illegal number", t
)
253 def t_STRING1(self
, t
):
256 t
.value
= t
.value
[1:-1]
259 def t_STRING2(self
, t
):
262 t
.value
= t
.value
[1:-1]
269 def p_empty(self
, p
):
272 def p_decls(self
, p
):
274 p
[0] = ast
.DeclListAST(self
, p
[1])
276 def p_declsx__list(self
, p
):
277 "declsx : decl declsx"
278 p
[0] = [ p
[1] ] + p
[2]
280 def p_declsx__none(self
, p
):
284 def p_decl__machine(self
, p
):
285 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
286 p
[0] = ast
.MachineAST(self
, p
[3], p
[4], p
[7], p
[9])
288 def p_decl__action(self
, p
):
289 "decl : ACTION '(' ident pairs ')' statements"
290 p
[0] = ast
.ActionDeclAST(self
, p
[3], p
[4], p
[6])
292 def p_decl__in_port(self
, p
):
293 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
294 p
[0] = ast
.InPortDeclAST(self
, p
[3], p
[5], p
[7], p
[8], p
[10])
296 def p_decl__out_port(self
, p
):
297 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
298 p
[0] = ast
.OutPortDeclAST(self
, p
[3], p
[5], p
[7], p
[8])
300 def p_decl__trans0(self
, p
):
301 "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
302 p
[0] = ast
.TransitionDeclAST(self
, p
[3], p
[5], p
[7], p
[8], p
[10])
304 def p_decl__trans1(self
, p
):
305 "decl : TRANS '(' idents ',' idents pairs ')' idents"
306 p
[0] = ast
.TransitionDeclAST(self
, p
[3], p
[5], None, p
[6], p
[8])
308 def p_decl__extern0(self
, p
):
309 "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
310 p
[4]["external"] = "yes"
311 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], [])
313 def p_decl__extern1(self
, p
):
314 "decl : EXTERN_TYPE '(' type pairs ')' '{' type_methods '}'"
315 p
[4]["external"] = "yes"
316 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
318 def p_decl__global(self
, p
):
319 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
320 p
[4]["global"] = "yes"
321 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
323 def p_decl__struct(self
, p
):
324 "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
325 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
327 def p_decl__enum(self
, p
):
328 "decl : ENUM '(' type pairs ')' '{' type_enums '}'"
329 p
[4]["enumeration"] = "yes"
330 p
[0] = ast
.EnumDeclAST(self
, p
[3], p
[4], p
[7])
332 def p_decl__state_decl(self
, p
):
333 "decl : STATE_DECL '(' type pairs ')' '{' type_states '}'"
334 p
[4]["enumeration"] = "yes"
335 p
[4]["state_decl"] = "yes"
336 p
[0] = ast
.StateDeclAST(self
, p
[3], p
[4], p
[7])
338 def p_decl__object(self
, p
):
339 "decl : type ident pairs SEMI"
340 p
[0] = ast
.ObjDeclAST(self
, p
[1], p
[2], p
[3])
342 def p_decl__func_decl(self
, p
):
343 """decl : void ident '(' params ')' pairs SEMI
344 | type ident '(' params ')' pairs SEMI"""
345 p
[0] = ast
.FuncDeclAST(self
, p
[1], p
[2], p
[4], p
[6], None)
347 def p_decl__func_def(self
, p
):
348 """decl : void ident '(' params ')' pairs statements
349 | type ident '(' params ')' pairs statements"""
350 p
[0] = ast
.FuncDeclAST(self
, p
[1], p
[2], p
[4], p
[6], p
[7])
353 def p_type_members__list(self
, p
):
354 "type_members : type_member type_members"
355 p
[0] = [ p
[1] ] + p
[2]
357 def p_type_members__empty(self
, p
):
358 "type_members : empty"
361 def p_type_member__1(self
, p
):
362 "type_member : type ident pairs SEMI"
363 p
[0] = ast
.TypeFieldMemberAST(self
, p
[1], p
[2], p
[3], None)
365 def p_type_member__2(self
, p
):
366 "type_member : type ident ASSIGN expr SEMI"
367 p
[0] = ast
.TypeFieldMemberAST(self
, p
[1], p
[2],
368 ast
.PairListAST(self
), p
[4])
371 def p_type_methods__list(self
, p
):
372 "type_methods : type_method type_methods"
373 p
[0] = [ p
[1] ] + p
[2]
375 def p_type_methods(self
, p
):
376 "type_methods : empty"
379 def p_type_method(self
, p
):
380 "type_method : type_or_void ident '(' types ')' pairs SEMI"
381 p
[0] = ast
.TypeFieldMethodAST(self
, p
[1], p
[2], p
[4], p
[6])
384 def p_type_enums__list(self
, p
):
385 "type_enums : type_enum type_enums"
386 p
[0] = [ p
[1] ] + p
[2]
388 def p_type_enums__empty(self
, p
):
392 def p_type_enum(self
, p
):
393 "type_enum : ident pairs SEMI"
394 p
[0] = ast
.TypeFieldEnumAST(self
, p
[1], p
[2])
397 def p_type_states__list(self
, p
):
398 "type_states : type_state type_states"
399 p
[0] = [ p
[1] ] + p
[2]
401 def p_type_states__empty(self
, p
):
402 "type_states : empty"
405 def p_type_state(self
, p
):
406 "type_state : ident ',' enumeration pairs SEMI"
407 p
[0] = ast
.TypeFieldStateAST(self
, p
[1], p
[3], p
[4])
410 def p_types__multiple(self
, p
):
411 "types : type ',' types"
412 p
[0] = [ p
[1] ] + p
[3]
414 def p_types__one(self
, p
):
418 def p_types__empty(self
, p
):
422 def p_typestr__multi(self
, p
):
423 "typestr : typestr DOUBLE_COLON ident"
424 p
[0] = '%s::%s' % (p
[1], p
[3])
426 def p_typestr__single(self
, p
):
430 def p_type__one(self
, p
):
432 p
[0] = ast
.TypeAST(self
, p
[1])
436 p
[0] = ast
.TypeAST(self
, p
[1])
438 def p_type_or_void(self
, p
):
439 """type_or_void : type
444 def p_params__many(self
, p
):
445 "params : param ',' params"
446 p
[0] = [ p
[1] ] + p
[3]
448 def p_params__one(self
, p
):
452 def p_params__none(self
, p
):
456 def p_param(self
, p
):
458 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2])
460 def p_param__pointer(self
, p
):
461 "param : type STAR ident"
462 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[3], None, True)
464 def p_param__pointer_default(self
, p
):
465 "param : type STAR ident '=' STRING"
466 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[3], p
[5], True)
468 def p_param__default_number(self
, p
):
469 "param : type ident '=' NUMBER"
470 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
472 def p_param__default_bool(self
, p
):
473 "param : type ident '=' LIT_BOOL"
474 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
476 def p_param__default_string(self
, p
):
477 "param : type ident '=' STRING"
478 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
481 def p_idents__braced(self
, p
):
482 "idents : '{' identx '}'"
485 def p_idents__bare(self
, p
):
489 def p_identx__multiple_1(self
, p
):
490 """identx : ident SEMI identx
491 | ident ',' identx"""
492 p
[0] = [ p
[1] ] + p
[3]
494 def p_identx__multiple_2(self
, p
):
495 "identx : ident identx"
496 p
[0] = [ p
[1] ] + p
[2]
498 def p_identx__single(self
, p
):
502 def p_ident(self
, p
):
506 # Pair and pair lists
507 def p_pairs__list(self
, p
):
511 def p_pairs__empty(self
, p
):
513 p
[0] = ast
.PairListAST(self
)
515 def p_pairsx__many(self
, p
):
516 "pairsx : pair ',' pairsx"
520 def p_pairsx__one(self
, p
):
522 p
[0] = ast
.PairListAST(self
)
525 def p_pair__assign(self
, p
):
526 """pair : ident '=' STRING
528 | ident '=' NUMBER"""
529 p
[0] = ast
.PairAST(self
, p
[1], p
[3])
531 def p_pair__literal(self
, p
):
533 p
[0] = ast
.PairAST(self
, "short", p
[1])
535 # Below are the rules for action descriptions
536 def p_statements__inner(self
, p
):
537 "statements : '{' statements_inner '}'"
538 p
[0] = ast
.StatementListAST(self
, p
[2])
540 def p_statements__none(self
, p
):
541 "statements : '{' '}'"
542 p
[0] = ast
.StatementListAST(self
, [])
544 def p_statements_inner__many(self
, p
):
545 "statements_inner : statement statements_inner"
546 p
[0] = [ p
[1] ] + p
[2]
548 def p_statements_inner__one(self
, p
):
549 "statements_inner : statement"
552 def p_exprs__multiple(self
, p
):
553 "exprs : expr ',' exprs"
554 p
[0] = [ p
[1] ] + p
[3]
556 def p_exprs__one(self
, p
):
560 def p_exprs__empty(self
, p
):
564 def p_statement__expression(self, p):
565 "statement
: expr SEMI
"
566 p[0] = ast.ExprStatementAST(self, p[1])
568 def p_statement__assign(self, p):
569 "statement
: expr ASSIGN expr SEMI
"
570 p[0] = ast.AssignStatementAST(self, p[1], p[3])
572 def p_statement__enqueue(self, p):
573 "statement
: ENQUEUE
'(' var
',' type pairs
')' statements
"
574 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[6], p[8])
576 def p_statement__stall_and_wait(self, p):
577 "statement
: STALL_AND_WAIT
'(' var
',' var
')' SEMI
"
578 p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5])
580 def p_statement__wake_up_dependents(self, p):
581 "statement
: WAKE_UP_DEPENDENTS
'(' var
')' SEMI
"
582 p[0] = ast.WakeUpDependentsStatementAST(self, p[3])
584 def p_statement__peek(self, p):
585 "statement
: PEEK
'(' var
',' type pairs
')' statements
"
586 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek
")
588 def p_statement__copy_head(self, p):
589 "statement
: COPY_HEAD
'(' var
',' var pairs
')' SEMI
"
590 p[0] = ast.CopyHeadStatementAST(self, p[3], p[5], p[6])
592 def p_statement__check_allocate(self, p):
593 "statement
: CHECK_ALLOCATE
'(' var
')' SEMI
"
594 p[0] = ast.CheckAllocateStatementAST(self, p[3])
596 def p_statement__check_stop(self, p):
597 "statement
: CHECK_STOP_SLOTS
'(' var
',' STRING
',' STRING
')' SEMI
"
598 p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
600 def p_statement__static_cast(self, p):
601 "aexpr
: STATIC_CAST
'(' type ',' expr
')'"
602 p[0] = ast.StaticCastAST(self, p[3], "ref
", p[5])
604 def p_statement__static_cast_ptr(self, p):
605 "aexpr
: STATIC_CAST
'(' type ',' STRING
',' expr
')'"
606 p[0] = ast.StaticCastAST(self, p[3], p[5], p[7])
608 def p_statement__return(self, p):
609 "statement
: RETURN expr SEMI
"
610 p[0] = ast.ReturnStatementAST(self, p[2])
612 def p_statement__if(self, p):
613 "statement
: if_statement
"
616 def p_if_statement__if(self, p):
617 "if_statement
: IF
'(' expr
')' statements
"
618 p[0] = ast.IfStatementAST(self, p[3], p[5], None)
620 def p_if_statement__if_else(self, p):
621 "if_statement
: IF
'(' expr
')' statements ELSE statements
"
622 p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
624 def p_statement__if_else_if(self, p):
625 "if_statement
: IF
'(' expr
')' statements ELSE if_statement
"
626 p[0] = ast.IfStatementAST(self, p[3], p[5],
627 ast.StatementListAST(self, p[7]))
629 def p_expr__var(self, p):
633 def p_expr__localvar(self, p):
635 p[0] = ast.LocalVariableAST(self, p[1], p[2])
637 def p_expr__literal(self, p):
641 def p_expr__enumeration(self, p):
642 "aexpr
: enumeration
"
645 def p_expr__func_call(self, p):
646 "aexpr
: ident
'(' exprs
')'"
647 p[0] = ast.FuncCallExprAST(self, p[1], p[3])
649 def p_expr__new(self, p):
651 p[0] = ast.NewExprAST(self, p[2])
653 def p_expr__null(self, p):
655 p[0] = ast.OodAST(self)
657 # globally access a local chip component and call a method
658 def p_expr__local_chip_method(self, p):
659 "aexpr
: THIS DOT var
'[' expr
']' DOT var DOT ident
'(' exprs
')'"
660 p[0] = ast.LocalChipMethodAST(self, p[3], p[5], p[8], p[10], p[12])
662 # globally access a local chip component and access a data member
663 def p_expr__local_chip_member(self, p):
664 "aexpr
: THIS DOT var
'[' expr
']' DOT var DOT field
"
665 p[0] = ast.LocalChipMemberAST(self, p[3], p[5], p[8], p[10])
667 # globally access a specified chip component and call a method
668 def p_expr__specified_chip_method(self, p):
669 "aexpr
: CHIP
'[' expr
']' DOT var
'[' expr
']' DOT var DOT ident
'(' exprs
')'"
670 p[0] = ast.SpecifiedChipMethodAST(self, p[3], p[6], p[8], p[11], p[13],
673 # globally access a specified chip component and access a data member
674 def p_expr__specified_chip_member(self, p):
675 "aexpr
: CHIP
'[' expr
']' DOT var
'[' expr
']' DOT var DOT field
"
676 p[0] = ast.SpecifiedChipMemberAST(self, p[3], p[6], p[8], p[11], p[13])
678 def p_expr__member(self, p):
679 "aexpr
: aexpr DOT ident
"
680 p[0] = ast.MemberExprAST(self, p[1], p[3])
682 def p_expr__member_method_call(self, p):
683 "aexpr
: aexpr DOT ident
'(' exprs
')'"
684 p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
686 def p_expr__member_method_call_lookup(self, p):
687 "aexpr
: aexpr
'[' exprs
']'"
688 p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup
", p[3])
690 def p_expr__class_method_call(self, p):
691 "aexpr
: type DOUBLE_COLON ident
'(' exprs
')'"
692 p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
694 def p_expr__aexpr(self, p):
698 def p_expr__binary_op(self, p):
699 """expr : expr STAR expr
711 | expr RIGHTSHIFT expr
712 | expr LEFTSHIFT expr"""
713 p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
716 def p_expr__unary_op(self, p):
718 | DASH expr %prec UMINUS"""
719 p[0] = PrefixOperatorExpr(p[1], p[2])
721 def p_expr__parens(self, p):
722 "aexpr
: '(' expr
')'"
725 def p_expr__is_valid_ptr(self, p):
726 "aexpr
: IS_VALID
'(' var
')'"
727 p[0] = ast.IsValidPtrExprAST(self, p[3], True)
729 def p_expr__is_invalid_ptr(self, p):
730 "aexpr
: IS_INVALID
'(' var
')'"
731 p[0] = ast.IsValidPtrExprAST(self, p[3], False)
733 def p_literal__string(self, p):
735 p[0] = ast.LiteralExprAST(self, p[1], "std
::string
")
737 def p_literal__number(self, p):
739 p[0] = ast.LiteralExprAST(self, p[1], "int")
741 def p_literal__float(self, p):
742 "literal
: FLOATNUMBER
"
743 p[0] = ast.LiteralExprAST(self, p[1], "int")
745 def p_literal__bool(self, p):
747 p[0] = ast.LiteralExprAST(self, p[1], "bool")
749 def p_enumeration(self, p):
750 "enumeration
: ident
':' ident
"
751 p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
755 p[0] = ast.VarExprAST(self, p[1])
757 def p_field(self, p):