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 'enqueue' : 'ENQUEUE',
163 'copy_head' : 'COPY_HEAD',
164 'check_allocate' : 'CHECK_ALLOCATE',
165 'check_stop_slots' : 'CHECK_STOP_SLOTS',
166 'static_cast' : 'STATIC_CAST',
168 'is_valid' : 'IS_VALID',
169 'is_invalid' : 'IS_INVALID',
179 literals
= ':[]{}(),='
181 tokens
= [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
182 'LEFTSHIFT', 'RIGHTSHIFT',
184 'PLUS', 'DASH', 'STAR', 'SLASH',
185 'DOUBLE_COLON', 'SEMI',
187 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
188 tokens
+= reserved
.values()
205 t_DOUBLE_COLON
= r
'::'
211 ('left', 'AND', 'OR'),
212 ('left', 'EQ', 'NE'),
213 ('left', 'LT', 'GT', 'LE', 'GE'),
214 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
215 ('left', 'PLUS', 'DASH'),
216 ('left', 'STAR', 'SLASH'),
217 ('right', 'NOT', 'UMINUS'),
220 def t_IDENT(self
, t
):
221 r
'[a-zA-Z_][a-zA-Z_0-9]*'
222 if t
.value
== 'true':
227 if t
.value
== 'false':
232 # Check for reserved words
233 t
.type = self
.reserved
.get(t
.value
, 'IDENT')
236 def t_FLOATNUMBER(self
, t
):
239 t
.value
= float(t
.value
)
241 raise TokenError("Illegal float", t
)
244 def t_NUMBER(self
, t
):
247 t
.value
= int(t
.value
)
249 raise TokenError("Illegal number", t
)
252 def t_STRING1(self
, t
):
255 t
.value
= t
.value
[1:-1]
258 def t_STRING2(self
, t
):
261 t
.value
= t
.value
[1:-1]
268 def p_empty(self
, p
):
271 def p_decls(self
, p
):
273 p
[0] = ast
.DeclListAST(self
, p
[1])
275 def p_declsx__list(self
, p
):
276 "declsx : decl declsx"
277 p
[0] = [ p
[1] ] + p
[2]
279 def p_declsx__none(self
, p
):
283 def p_decl__machine(self
, p
):
284 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
285 p
[0] = ast
.MachineAST(self
, p
[3], p
[4], p
[7], p
[9])
287 def p_decl__action(self
, p
):
288 "decl : ACTION '(' ident pairs ')' statements"
289 p
[0] = ast
.ActionDeclAST(self
, p
[3], p
[4], p
[6])
291 def p_decl__in_port(self
, p
):
292 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
293 p
[0] = ast
.InPortDeclAST(self
, p
[3], p
[5], p
[7], p
[8], p
[10])
295 def p_decl__out_port(self
, p
):
296 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
297 p
[0] = ast
.OutPortDeclAST(self
, p
[3], p
[5], p
[7], p
[8])
299 def p_decl__trans0(self
, p
):
300 "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
301 p
[0] = ast
.TransitionDeclAST(self
, p
[3], p
[5], p
[7], p
[8], p
[10])
303 def p_decl__trans1(self
, p
):
304 "decl : TRANS '(' idents ',' idents pairs ')' idents"
305 p
[0] = ast
.TransitionDeclAST(self
, p
[3], p
[5], None, p
[6], p
[8])
307 def p_decl__extern0(self
, p
):
308 "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
309 p
[4]["external"] = "yes"
310 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], [])
312 def p_decl__extern1(self
, p
):
313 "decl : EXTERN_TYPE '(' type pairs ')' '{' type_methods '}'"
314 p
[4]["external"] = "yes"
315 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
317 def p_decl__global(self
, p
):
318 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
319 p
[4]["global"] = "yes"
320 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
322 def p_decl__struct(self
, p
):
323 "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
324 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
326 def p_decl__enum(self
, p
):
327 "decl : ENUM '(' type pairs ')' '{' type_enums '}'"
328 p
[4]["enumeration"] = "yes"
329 p
[0] = ast
.EnumDeclAST(self
, p
[3], p
[4], p
[7])
331 def p_decl__state_decl(self
, p
):
332 "decl : STATE_DECL '(' type pairs ')' '{' type_states '}'"
333 p
[4]["enumeration"] = "yes"
334 p
[4]["state_decl"] = "yes"
335 p
[0] = ast
.StateDeclAST(self
, p
[3], p
[4], p
[7])
337 def p_decl__object(self
, p
):
338 "decl : type ident pairs SEMI"
339 p
[0] = ast
.ObjDeclAST(self
, p
[1], p
[2], p
[3])
341 def p_decl__func_decl(self
, p
):
342 """decl : void ident '(' params ')' pairs SEMI
343 | type ident '(' params ')' pairs SEMI"""
344 p
[0] = ast
.FuncDeclAST(self
, p
[1], p
[2], p
[4], p
[6], None)
346 def p_decl__func_def(self
, p
):
347 """decl : void ident '(' params ')' pairs statements
348 | type ident '(' params ')' pairs statements"""
349 p
[0] = ast
.FuncDeclAST(self
, p
[1], p
[2], p
[4], p
[6], p
[7])
352 def p_type_members__list(self
, p
):
353 "type_members : type_member type_members"
354 p
[0] = [ p
[1] ] + p
[2]
356 def p_type_members__empty(self
, p
):
357 "type_members : empty"
360 def p_type_member__1(self
, p
):
361 "type_member : type ident pairs SEMI"
362 p
[0] = ast
.TypeFieldMemberAST(self
, p
[1], p
[2], p
[3], None)
364 def p_type_member__2(self
, p
):
365 "type_member : type ident ASSIGN expr SEMI"
366 p
[0] = ast
.TypeFieldMemberAST(self
, p
[1], p
[2],
367 ast
.PairListAST(self
), p
[4])
370 def p_type_methods__list(self
, p
):
371 "type_methods : type_method type_methods"
372 p
[0] = [ p
[1] ] + p
[2]
374 def p_type_methods(self
, p
):
375 "type_methods : empty"
378 def p_type_method(self
, p
):
379 "type_method : type_or_void ident '(' types ')' pairs SEMI"
380 p
[0] = ast
.TypeFieldMethodAST(self
, p
[1], p
[2], p
[4], p
[6])
383 def p_type_enums__list(self
, p
):
384 "type_enums : type_enum type_enums"
385 p
[0] = [ p
[1] ] + p
[2]
387 def p_type_enums__empty(self
, p
):
391 def p_type_enum(self
, p
):
392 "type_enum : ident pairs SEMI"
393 p
[0] = ast
.TypeFieldEnumAST(self
, p
[1], p
[2])
396 def p_type_states__list(self
, p
):
397 "type_states : type_state type_states"
398 p
[0] = [ p
[1] ] + p
[2]
400 def p_type_states__empty(self
, p
):
401 "type_states : empty"
404 def p_type_state(self
, p
):
405 "type_state : ident ',' enumeration pairs SEMI"
406 p
[0] = ast
.TypeFieldStateAST(self
, p
[1], p
[3], p
[4])
409 def p_types__multiple(self
, p
):
410 "types : type ',' types"
411 p
[0] = [ p
[1] ] + p
[3]
413 def p_types__one(self
, p
):
417 def p_types__empty(self
, p
):
421 def p_typestr__multi(self
, p
):
422 "typestr : typestr DOUBLE_COLON ident"
423 p
[0] = '%s::%s' % (p
[1], p
[3])
425 def p_typestr__single(self
, p
):
429 def p_type__one(self
, p
):
431 p
[0] = ast
.TypeAST(self
, p
[1])
435 p
[0] = ast
.TypeAST(self
, p
[1])
437 def p_type_or_void(self
, p
):
438 """type_or_void : type
443 def p_params__many(self
, p
):
444 "params : param ',' params"
445 p
[0] = [ p
[1] ] + p
[3]
447 def p_params__one(self
, p
):
451 def p_params__none(self
, p
):
455 def p_param(self
, p
):
457 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2])
459 def p_param__pointer(self
, p
):
460 "param : type STAR ident"
461 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[3], None, True)
463 def p_param__pointer_default(self
, p
):
464 "param : type STAR ident '=' STRING"
465 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[3], p
[5], True)
467 def p_param__default_number(self
, p
):
468 "param : type ident '=' NUMBER"
469 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
471 def p_param__default_bool(self
, p
):
472 "param : type ident '=' LIT_BOOL"
473 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
475 def p_param__default_string(self
, p
):
476 "param : type ident '=' STRING"
477 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
480 def p_idents__braced(self
, p
):
481 "idents : '{' identx '}'"
484 def p_idents__bare(self
, p
):
488 def p_identx__multiple_1(self
, p
):
489 """identx : ident SEMI identx
490 | ident ',' identx"""
491 p
[0] = [ p
[1] ] + p
[3]
493 def p_identx__multiple_2(self
, p
):
494 "identx : ident identx"
495 p
[0] = [ p
[1] ] + p
[2]
497 def p_identx__single(self
, p
):
501 def p_ident(self
, p
):
505 # Pair and pair lists
506 def p_pairs__list(self
, p
):
510 def p_pairs__empty(self
, p
):
512 p
[0] = ast
.PairListAST(self
)
514 def p_pairsx__many(self
, p
):
515 "pairsx : pair ',' pairsx"
519 def p_pairsx__one(self
, p
):
521 p
[0] = ast
.PairListAST(self
)
524 def p_pair__assign(self
, p
):
525 """pair : ident '=' STRING
527 | ident '=' NUMBER"""
528 p
[0] = ast
.PairAST(self
, p
[1], p
[3])
530 def p_pair__literal(self
, p
):
532 p
[0] = ast
.PairAST(self
, "short", p
[1])
534 # Below are the rules for action descriptions
535 def p_statements__inner(self
, p
):
536 "statements : '{' statements_inner '}'"
537 p
[0] = ast
.StatementListAST(self
, p
[2])
539 def p_statements__none(self
, p
):
540 "statements : '{' '}'"
541 p
[0] = ast
.StatementListAST(self
, [])
543 def p_statements_inner__many(self
, p
):
544 "statements_inner : statement statements_inner"
545 p
[0] = [ p
[1] ] + p
[2]
547 def p_statements_inner__one(self
, p
):
548 "statements_inner : statement"
551 def p_exprs__multiple(self
, p
):
552 "exprs : expr ',' exprs"
553 p
[0] = [ p
[1] ] + p
[3]
555 def p_exprs__one(self
, p
):
559 def p_exprs__empty(self
, p
):
563 def p_statement__expression(self, p):
564 "statement
: expr SEMI
"
565 p[0] = ast.ExprStatementAST(self, p[1])
567 def p_statement__assign(self, p):
568 "statement
: expr ASSIGN expr SEMI
"
569 p[0] = ast.AssignStatementAST(self, p[1], p[3])
571 def p_statement__enqueue(self, p):
572 "statement
: ENQUEUE
'(' var
',' type pairs
')' statements
"
573 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[6], p[8])
575 def p_statement__stall_and_wait(self, p):
576 "statement
: STALL_AND_WAIT
'(' var
',' var
')' SEMI
"
577 p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5])
579 def p_statement__peek(self, p):
580 "statement
: PEEK
'(' var
',' type pairs
')' statements
"
581 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek
")
583 def p_statement__copy_head(self, p):
584 "statement
: COPY_HEAD
'(' var
',' var pairs
')' SEMI
"
585 p[0] = ast.CopyHeadStatementAST(self, p[3], p[5], p[6])
587 def p_statement__check_allocate(self, p):
588 "statement
: CHECK_ALLOCATE
'(' var
')' SEMI
"
589 p[0] = ast.CheckAllocateStatementAST(self, p[3])
591 def p_statement__check_stop(self, p):
592 "statement
: CHECK_STOP_SLOTS
'(' var
',' STRING
',' STRING
')' SEMI
"
593 p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
595 def p_statement__static_cast(self, p):
596 "aexpr
: STATIC_CAST
'(' type ',' expr
')'"
597 p[0] = ast.StaticCastAST(self, p[3], "ref
", p[5])
599 def p_statement__static_cast_ptr(self, p):
600 "aexpr
: STATIC_CAST
'(' type ',' STRING
',' expr
')'"
601 p[0] = ast.StaticCastAST(self, p[3], p[5], p[7])
603 def p_statement__return(self, p):
604 "statement
: RETURN expr SEMI
"
605 p[0] = ast.ReturnStatementAST(self, p[2])
607 def p_statement__if(self, p):
608 "statement
: if_statement
"
611 def p_if_statement__if(self, p):
612 "if_statement
: IF
'(' expr
')' statements
"
613 p[0] = ast.IfStatementAST(self, p[3], p[5], None)
615 def p_if_statement__if_else(self, p):
616 "if_statement
: IF
'(' expr
')' statements ELSE statements
"
617 p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
619 def p_statement__if_else_if(self, p):
620 "if_statement
: IF
'(' expr
')' statements ELSE if_statement
"
621 p[0] = ast.IfStatementAST(self, p[3], p[5],
622 ast.StatementListAST(self, p[7]))
624 def p_expr__var(self, p):
628 def p_expr__localvar(self, p):
630 p[0] = ast.LocalVariableAST(self, p[1], p[2])
632 def p_expr__literal(self, p):
636 def p_expr__enumeration(self, p):
637 "aexpr
: enumeration
"
640 def p_expr__func_call(self, p):
641 "aexpr
: ident
'(' exprs
')'"
642 p[0] = ast.FuncCallExprAST(self, p[1], p[3])
644 def p_expr__new(self, p):
646 p[0] = ast.NewExprAST(self, p[2])
648 def p_expr__null(self, p):
650 p[0] = ast.OodAST(self)
652 # globally access a local chip component and call a method
653 def p_expr__local_chip_method(self, p):
654 "aexpr
: THIS DOT var
'[' expr
']' DOT var DOT ident
'(' exprs
')'"
655 p[0] = ast.LocalChipMethodAST(self, p[3], p[5], p[8], p[10], p[12])
657 # globally access a local chip component and access a data member
658 def p_expr__local_chip_member(self, p):
659 "aexpr
: THIS DOT var
'[' expr
']' DOT var DOT field
"
660 p[0] = ast.LocalChipMemberAST(self, p[3], p[5], p[8], p[10])
662 # globally access a specified chip component and call a method
663 def p_expr__specified_chip_method(self, p):
664 "aexpr
: CHIP
'[' expr
']' DOT var
'[' expr
']' DOT var DOT ident
'(' exprs
')'"
665 p[0] = ast.SpecifiedChipMethodAST(self, p[3], p[6], p[8], p[11], p[13],
668 # globally access a specified chip component and access a data member
669 def p_expr__specified_chip_member(self, p):
670 "aexpr
: CHIP
'[' expr
']' DOT var
'[' expr
']' DOT var DOT field
"
671 p[0] = ast.SpecifiedChipMemberAST(self, p[3], p[6], p[8], p[11], p[13])
673 def p_expr__member(self, p):
674 "aexpr
: aexpr DOT ident
"
675 p[0] = ast.MemberExprAST(self, p[1], p[3])
677 def p_expr__member_method_call(self, p):
678 "aexpr
: aexpr DOT ident
'(' exprs
')'"
679 p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
681 def p_expr__member_method_call_lookup(self, p):
682 "aexpr
: aexpr
'[' exprs
']'"
683 p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup
", p[3])
685 def p_expr__class_method_call(self, p):
686 "aexpr
: type DOUBLE_COLON ident
'(' exprs
')'"
687 p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
689 def p_expr__aexpr(self, p):
693 def p_expr__binary_op(self, p):
694 """expr : expr STAR expr
706 | expr RIGHTSHIFT expr
707 | expr LEFTSHIFT expr"""
708 p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
711 def p_expr__unary_op(self, p):
713 | DASH expr %prec UMINUS"""
714 p[0] = PrefixOperatorExpr(p[1], p[2])
716 def p_expr__parens(self, p):
717 "aexpr
: '(' expr
')'"
720 def p_expr__is_valid_ptr(self, p):
721 "aexpr
: IS_VALID
'(' var
')'"
722 p[0] = ast.IsValidPtrExprAST(self, p[3], True)
724 def p_expr__is_invalid_ptr(self, p):
725 "aexpr
: IS_INVALID
'(' var
')'"
726 p[0] = ast.IsValidPtrExprAST(self, p[3], False)
728 def p_literal__string(self, p):
730 p[0] = ast.LiteralExprAST(self, p[1], "std
::string
")
732 def p_literal__number(self, p):
734 p[0] = ast.LiteralExprAST(self, p[1], "int")
736 def p_literal__float(self, p):
737 "literal
: FLOATNUMBER
"
738 p[0] = ast.LiteralExprAST(self, p[1], "int")
740 def p_literal__bool(self, p):
742 p[0] = ast.LiteralExprAST(self, p[1], "bool")
744 def p_enumeration(self, p):
745 "enumeration
: ident
':' ident
"
746 p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
750 p[0] = ast.VarExprAST(self, p[1])
752 def p_field(self, p):