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
, 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 self
.decl_list_vec
= []
56 self
.protocol
= protocol
57 self
.symtab
= SymbolTable(self
)
59 def codeFormatter(self
, *args
, **kwargs
):
60 code
= code_formatter(*args
, **kwargs
)
61 code
['protocol'] = self
.protocol
64 def parse(self
, filename
):
66 decl_list
= self
.parse_file(filename
)
69 self
.decl_list_vec
.append(decl_list
)
71 def _load(self
, *filenames
):
72 filenames
= list(filenames
)
75 if isinstance(f
, (list, tuple)):
76 filenames
[0:0] = list(f
)
80 if f
.endswith(".slicc"):
81 dirname
,basename
= os
.path
.split(f
)
82 filenames
[0:0] = [ os
.path
.join(dirname
, x
) \
83 for x
in read_slicc(f
)]
85 assert f
.endswith(".sm")
88 def load(self
, *filenames
, **kwargs
):
89 verbose
= kwargs
.pop("verbose", False)
93 gen
= self
._load
(*filenames
)
97 # Run out the generator if we don't want the verbosity
101 def findMachines(self
):
102 for decl_list
in self
.decl_list_vec
:
103 decl_list
.findMachines()
106 for decl_list
in self
.decl_list_vec
:
109 def writeCodeFiles(self
, code_path
):
110 util
.makeDir(code_path
)
111 self
.symtab
.writeCodeFiles(code_path
)
113 def writeHTMLFiles(self
, code_path
):
114 util
.makeDir(code_path
)
115 self
.symtab
.writeHTMLFiles(code_path
)
123 for decl_list
in self
.decl_list_vec
:
124 f |
= decl_list
.files()
130 # C or C++ comment (ignore)
131 def t_c_comment(self
, t
):
133 t
.lexer
.lineno
+= t
.value
.count('\n')
135 def t_cpp_comment(self
, t
):
138 # Define a rule so we can track line numbers
139 def t_newline(self
, t
):
141 t
.lexer
.lineno
+= len(t
.value
)
145 'machine' : 'MACHINE',
146 'in_port' : 'IN_PORT',
147 'out_port' : 'OUT_PORT',
149 'transition' : 'TRANS',
150 'structure' : 'STRUCT',
151 'external_type' : 'EXTERN_TYPE',
152 'enumeration' : 'ENUM',
153 'state_declaration' : 'STATE_DECL',
155 'stall_and_wait' : 'STALL_AND_WAIT',
156 'enqueue' : 'ENQUEUE',
157 'copy_head' : 'COPY_HEAD',
158 'check_allocate' : 'CHECK_ALLOCATE',
159 'check_stop_slots' : 'CHECK_STOP_SLOTS',
160 'static_cast' : 'STATIC_CAST',
162 'is_valid' : 'IS_VALID',
163 'is_invalid' : 'IS_INVALID',
173 literals
= ':[]{}(),='
175 tokens
= [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
176 'LEFTSHIFT', 'RIGHTSHIFT',
178 'PLUS', 'DASH', 'STAR', 'SLASH',
179 'DOUBLE_COLON', 'SEMI',
181 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
182 tokens
+= reserved
.values()
199 t_DOUBLE_COLON
= r
'::'
205 ('left', 'AND', 'OR'),
206 ('left', 'EQ', 'NE'),
207 ('left', 'LT', 'GT', 'LE', 'GE'),
208 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
209 ('left', 'PLUS', 'DASH'),
210 ('left', 'STAR', 'SLASH'),
211 ('right', 'NOT', 'UMINUS'),
214 def t_IDENT(self
, t
):
215 r
'[a-zA-Z_][a-zA-Z_0-9]*'
216 if t
.value
== 'true':
221 if t
.value
== 'false':
226 # Check for reserved words
227 t
.type = self
.reserved
.get(t
.value
, 'IDENT')
230 def t_FLOATNUMBER(self
, t
):
233 t
.value
= float(t
.value
)
235 raise ParseError("Illegal float", t
)
238 def t_NUMBER(self
, t
):
241 t
.value
= int(t
.value
)
243 raise ParseError("Illegal number", t
)
246 def t_STRING1(self
, t
):
249 t
.value
= t
.value
[1:-1]
252 def t_STRING2(self
, t
):
255 t
.value
= t
.value
[1:-1]
262 def p_empty(self
, p
):
265 def p_decls(self
, p
):
267 p
[0] = ast
.DeclListAST(self
, p
[1])
269 def p_declsx__list(self
, p
):
270 "declsx : decl declsx"
271 p
[0] = [ p
[1] ] + p
[2]
273 def p_declsx__none(self
, p
):
277 def p_decl__machine(self
, p
):
278 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
279 p
[0] = ast
.MachineAST(self
, p
[3], p
[4], p
[7], p
[9])
281 def p_decl__action(self
, p
):
282 "decl : ACTION '(' ident pairs ')' statements"
283 p
[0] = ast
.ActionDeclAST(self
, p
[3], p
[4], p
[6])
285 def p_decl__in_port(self
, p
):
286 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
287 p
[0] = ast
.InPortDeclAST(self
, p
[3], p
[5], p
[7], p
[8], p
[10])
289 def p_decl__out_port(self
, p
):
290 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
291 p
[0] = ast
.OutPortDeclAST(self
, p
[3], p
[5], p
[7], p
[8])
293 def p_decl__trans0(self
, p
):
294 "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
295 p
[0] = ast
.TransitionDeclAST(self
, p
[3], p
[5], p
[7], p
[8], p
[10])
297 def p_decl__trans1(self
, p
):
298 "decl : TRANS '(' idents ',' idents pairs ')' idents"
299 p
[0] = ast
.TransitionDeclAST(self
, p
[3], p
[5], None, p
[6], p
[8])
301 def p_decl__extern0(self
, p
):
302 "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
303 p
[4]["external"] = "yes"
304 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], [])
306 def p_decl__global(self
, p
):
307 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
308 p
[4]["global"] = "yes"
309 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
311 def p_decl__struct(self
, p
):
312 "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
313 p
[0] = ast
.TypeDeclAST(self
, p
[3], p
[4], p
[7])
315 def p_decl__enum(self
, p
):
316 "decl : ENUM '(' type pairs ')' '{' type_enums '}'"
317 p
[4]["enumeration"] = "yes"
318 p
[0] = ast
.EnumDeclAST(self
, p
[3], p
[4], p
[7])
320 def p_decl__state_decl(self
, p
):
321 "decl : STATE_DECL '(' type pairs ')' '{' type_states '}'"
322 p
[4]["enumeration"] = "yes"
323 p
[4]["state_decl"] = "yes"
324 p
[0] = ast
.StateDeclAST(self
, p
[3], p
[4], p
[7])
326 def p_decl__object(self
, p
):
327 "decl : type ident pairs SEMI"
328 p
[0] = ast
.ObjDeclAST(self
, p
[1], p
[2], p
[3])
330 def p_decl__func_decl(self
, p
):
331 """decl : void ident '(' params ')' pairs SEMI
332 | type ident '(' params ')' pairs SEMI"""
333 p
[0] = ast
.FuncDeclAST(self
, p
[1], p
[2], p
[4], p
[6], None)
335 def p_decl__func_def(self
, p
):
336 """decl : void ident '(' params ')' pairs statements
337 | type ident '(' params ')' pairs statements"""
338 p
[0] = ast
.FuncDeclAST(self
, p
[1], p
[2], p
[4], p
[6], p
[7])
341 def p_type_members__list(self
, p
):
342 "type_members : type_member type_members"
343 p
[0] = [ p
[1] ] + p
[2]
345 def p_type_members__empty(self
, p
):
346 "type_members : empty"
349 def p_type_method__0(self
, p
):
350 "type_member : type_or_void ident '(' types ')' pairs SEMI"
351 p
[0] = ast
.TypeFieldMethodAST(self
, p
[1], p
[2], p
[4], p
[6])
353 def p_type_member__1(self
, p
):
354 "type_member : type_or_void ident pairs SEMI"
355 p
[0] = ast
.TypeFieldMemberAST(self
, p
[1], p
[2], p
[3], None)
357 def p_type_member__2(self
, p
):
358 "type_member : type_or_void ident ASSIGN expr SEMI"
359 p
[0] = ast
.TypeFieldMemberAST(self
, p
[1], p
[2],
360 ast
.PairListAST(self
), p
[4])
363 def p_type_enums__list(self
, p
):
364 "type_enums : type_enum type_enums"
365 p
[0] = [ p
[1] ] + p
[2]
367 def p_type_enums__empty(self
, p
):
371 def p_type_enum(self
, p
):
372 "type_enum : ident pairs SEMI"
373 p
[0] = ast
.TypeFieldEnumAST(self
, p
[1], p
[2])
376 def p_type_states__list(self
, p
):
377 "type_states : type_state type_states"
378 p
[0] = [ p
[1] ] + p
[2]
380 def p_type_states__empty(self
, p
):
381 "type_states : empty"
384 def p_type_state(self
, p
):
385 "type_state : ident ',' enumeration pairs SEMI"
386 p
[0] = ast
.TypeFieldStateAST(self
, p
[1], p
[3], p
[4])
389 def p_types__multiple(self
, p
):
390 "types : type ',' types"
391 p
[0] = [ p
[1] ] + p
[3]
393 def p_types__one(self
, p
):
397 def p_types__empty(self
, p
):
401 def p_typestr__multi(self
, p
):
402 "typestr : typestr DOUBLE_COLON ident"
403 p
[0] = '%s::%s' % (p
[1], p
[3])
405 def p_typestr__single(self
, p
):
409 def p_type__one(self
, p
):
411 p
[0] = ast
.TypeAST(self
, p
[1])
415 p
[0] = ast
.TypeAST(self
, p
[1])
417 def p_type_or_void(self
, p
):
418 """type_or_void : type
423 def p_params__many(self
, p
):
424 "params : param ',' params"
425 p
[0] = [ p
[1] ] + p
[3]
427 def p_params__one(self
, p
):
431 def p_params__none(self
, p
):
435 def p_param(self
, p
):
437 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2])
439 def p_param__pointer(self
, p
):
440 "param : type STAR ident"
441 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[3], None, True)
443 def p_param__pointer_default(self
, p
):
444 "param : type STAR ident '=' STRING"
445 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[3], p
[5], True)
447 def p_param__default_number(self
, p
):
448 "param : type ident '=' NUMBER"
449 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
451 def p_param__default_bool(self
, p
):
452 "param : type ident '=' LIT_BOOL"
453 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
455 def p_param__default_string(self
, p
):
456 "param : type ident '=' STRING"
457 p
[0] = ast
.FormalParamAST(self
, p
[1], p
[2], p
[4])
460 def p_idents__braced(self
, p
):
461 "idents : '{' identx '}'"
464 def p_idents__bare(self
, p
):
468 def p_identx__multiple_1(self
, p
):
469 """identx : ident SEMI identx
470 | ident ',' identx"""
471 p
[0] = [ p
[1] ] + p
[3]
473 def p_identx__multiple_2(self
, p
):
474 "identx : ident identx"
475 p
[0] = [ p
[1] ] + p
[2]
477 def p_identx__single(self
, p
):
481 def p_ident(self
, p
):
485 # Pair and pair lists
486 def p_pairs__list(self
, p
):
490 def p_pairs__empty(self
, p
):
492 p
[0] = ast
.PairListAST(self
)
494 def p_pairsx__many(self
, p
):
495 "pairsx : pair ',' pairsx"
499 def p_pairsx__one(self
, p
):
501 p
[0] = ast
.PairListAST(self
)
504 def p_pair__assign(self
, p
):
505 """pair : ident '=' STRING
507 | ident '=' NUMBER"""
508 p
[0] = ast
.PairAST(self
, p
[1], p
[3])
510 def p_pair__literal(self
, p
):
512 p
[0] = ast
.PairAST(self
, "short", p
[1])
514 # Below are the rules for action descriptions
515 def p_statements__inner(self
, p
):
516 "statements : '{' statements_inner '}'"
517 p
[0] = ast
.StatementListAST(self
, p
[2])
519 def p_statements__none(self
, p
):
520 "statements : '{' '}'"
521 p
[0] = ast
.StatementListAST(self
, [])
523 def p_statements_inner__many(self
, p
):
524 "statements_inner : statement statements_inner"
525 p
[0] = [ p
[1] ] + p
[2]
527 def p_statements_inner__one(self
, p
):
528 "statements_inner : statement"
531 def p_exprs__multiple(self
, p
):
532 "exprs : expr ',' exprs"
533 p
[0] = [ p
[1] ] + p
[3]
535 def p_exprs__one(self
, p
):
539 def p_exprs__empty(self
, p
):
543 def p_statement__expression(self, p):
544 "statement
: expr SEMI
"
545 p[0] = ast.ExprStatementAST(self, p[1])
547 def p_statement__assign(self, p):
548 "statement
: expr ASSIGN expr SEMI
"
549 p[0] = ast.AssignStatementAST(self, p[1], p[3])
551 def p_statement__enqueue(self, p):
552 "statement
: ENQUEUE
'(' var
',' type pairs
')' statements
"
553 p[0] = ast.EnqueueStatementAST(self, p[3], p[5], p[6], p[8])
555 def p_statement__stall_and_wait(self, p):
556 "statement
: STALL_AND_WAIT
'(' var
',' var
')' SEMI
"
557 p[0] = ast.StallAndWaitStatementAST(self, p[3], p[5])
559 def p_statement__peek(self, p):
560 "statement
: PEEK
'(' var
',' type pairs
')' statements
"
561 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "peek
")
563 def p_statement__copy_head(self, p):
564 "statement
: COPY_HEAD
'(' var
',' var pairs
')' SEMI
"
565 p[0] = ast.CopyHeadStatementAST(self, p[3], p[5], p[6])
567 def p_statement__check_allocate(self, p):
568 "statement
: CHECK_ALLOCATE
'(' var
')' SEMI
"
569 p[0] = ast.CheckAllocateStatementAST(self, p[3])
571 def p_statement__check_stop(self, p):
572 "statement
: CHECK_STOP_SLOTS
'(' var
',' STRING
',' STRING
')' SEMI
"
573 p[0] = ast.CheckStopStatementAST(self, p[3], p[5], p[7])
575 def p_statement__static_cast(self, p):
576 "aexpr
: STATIC_CAST
'(' type ',' expr
')'"
577 p[0] = ast.StaticCastAST(self, p[3], "ref
", p[5])
579 def p_statement__static_cast_ptr(self, p):
580 "aexpr
: STATIC_CAST
'(' type ',' STRING
',' expr
')'"
581 p[0] = ast.StaticCastAST(self, p[3], p[5], p[7])
583 def p_statement__return(self, p):
584 "statement
: RETURN expr SEMI
"
585 p[0] = ast.ReturnStatementAST(self, p[2])
587 def p_statement__if(self, p):
588 "statement
: if_statement
"
591 def p_if_statement__if(self, p):
592 "if_statement
: IF
'(' expr
')' statements
"
593 p[0] = ast.IfStatementAST(self, p[3], p[5], None)
595 def p_if_statement__if_else(self, p):
596 "if_statement
: IF
'(' expr
')' statements ELSE statements
"
597 p[0] = ast.IfStatementAST(self, p[3], p[5], p[7])
599 def p_statement__if_else_if(self, p):
600 "if_statement
: IF
'(' expr
')' statements ELSE if_statement
"
601 p[0] = ast.IfStatementAST(self, p[3], p[5],
602 ast.StatementListAST(self, p[7]))
604 def p_expr__var(self, p):
608 def p_expr__localvar(self, p):
610 p[0] = ast.LocalVariableAST(self, p[1], p[2])
612 def p_expr__literal(self, p):
616 def p_expr__enumeration(self, p):
617 "aexpr
: enumeration
"
620 def p_expr__func_call(self, p):
621 "aexpr
: ident
'(' exprs
')'"
622 p[0] = ast.FuncCallExprAST(self, p[1], p[3])
624 def p_expr__new(self, p):
626 p[0] = ast.NewExprAST(self, p[2])
628 def p_expr__null(self, p):
630 p[0] = ast.OodAST(self)
632 # globally access a local chip component and call a method
633 def p_expr__local_chip_method(self, p):
634 "aexpr
: THIS DOT var
'[' expr
']' DOT var DOT ident
'(' exprs
')'"
635 p[0] = ast.LocalChipMethodAST(self, p[3], p[5], p[8], p[10], p[12])
637 # globally access a local chip component and access a data member
638 def p_expr__local_chip_member(self, p):
639 "aexpr
: THIS DOT var
'[' expr
']' DOT var DOT field
"
640 p[0] = ast.LocalChipMemberAST(self, p[3], p[5], p[8], p[10])
642 # globally access a specified chip component and call a method
643 def p_expr__specified_chip_method(self, p):
644 "aexpr
: CHIP
'[' expr
']' DOT var
'[' expr
']' DOT var DOT ident
'(' exprs
')'"
645 p[0] = ast.SpecifiedChipMethodAST(self, p[3], p[6], p[8], p[11], p[13],
648 # globally access a specified chip component and access a data member
649 def p_expr__specified_chip_member(self, p):
650 "aexpr
: CHIP
'[' expr
']' DOT var
'[' expr
']' DOT var DOT field
"
651 p[0] = ast.SpecifiedChipMemberAST(self, p[3], p[6], p[8], p[11], p[13])
653 def p_expr__member(self, p):
654 "aexpr
: aexpr DOT ident
"
655 p[0] = ast.MemberExprAST(self, p[1], p[3])
657 def p_expr__member_method_call(self, p):
658 "aexpr
: aexpr DOT ident
'(' exprs
')'"
659 p[0] = ast.MemberMethodCallExprAST(self, p[1], p[3], p[5])
661 def p_expr__member_method_call_lookup(self, p):
662 "aexpr
: aexpr
'[' exprs
']'"
663 p[0] = ast.MemberMethodCallExprAST(self, p[1], "lookup
", p[3])
665 def p_expr__class_method_call(self, p):
666 "aexpr
: type DOUBLE_COLON ident
'(' exprs
')'"
667 p[0] = ast.ClassMethodCallExprAST(self, p[1], p[3], p[5])
669 def p_expr__aexpr(self, p):
673 def p_expr__binary_op(self, p):
674 """expr : expr STAR expr
686 | expr RIGHTSHIFT expr
687 | expr LEFTSHIFT expr"""
688 p[0] = ast.InfixOperatorExprAST(self, p[1], p[2], p[3])
691 def p_expr__unary_op(self, p):
693 | DASH expr %prec UMINUS"""
694 p[0] = PrefixOperatorExpr(p[1], p[2])
696 def p_expr__parens(self, p):
697 "aexpr
: '(' expr
')'"
700 def p_expr__is_valid_ptr(self, p):
701 "aexpr
: IS_VALID
'(' var
')'"
702 p[0] = ast.IsValidPtrExprAST(self, p[3], True)
704 def p_expr__is_invalid_ptr(self, p):
705 "aexpr
: IS_INVALID
'(' var
')'"
706 p[0] = ast.IsValidPtrExprAST(self, p[3], False)
708 def p_literal__string(self, p):
710 p[0] = ast.LiteralExprAST(self, p[1], "std
::string
")
712 def p_literal__number(self, p):
714 p[0] = ast.LiteralExprAST(self, p[1], "int")
716 def p_literal__float(self, p):
717 "literal
: FLOATNUMBER
"
718 p[0] = ast.LiteralExprAST(self, p[1], "int")
720 def p_literal__bool(self, p):
722 p[0] = ast.LiteralExprAST(self, p[1], "bool")
724 def p_enumeration(self, p):
725 "enumeration
: ident
':' ident
"
726 p[0] = ast.EnumExprAST(self, ast.TypeAST(self, p[1]), p[3])
730 p[0] = ast.VarExprAST(self, p[1])
732 def p_field(self, p):