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 f = set([
118 'ControllerFactory.cc',
119 'ControllerFactory.hh',
118 'MachineType.cc',
119 'MachineType.hh',
120 'Types.hh' ])
121
122 for decl_list in self.decl_list_vec:
123 f |= decl_list.files()
124
125 return f
126
127 t_ignore = '\t '
128
129 # C or C++ comment (ignore)
130 def t_c_comment(self, t):
131 r'/\*(.|\n)*?\*/'
132 t.lexer.lineno += t.value.count('\n')
133
134 def t_cpp_comment(self, t):
135 r'//.*'
136
137 # Define a rule so we can track line numbers
138 def t_newline(self, t):
139 r'\n+'
140 t.lexer.lineno += len(t.value)
141
142 reserved = {
143 'global' : 'GLOBAL',
144 'machine' : 'MACHINE',
145 'in_port' : 'IN_PORT',
146 'out_port' : 'OUT_PORT',
147 'action' : 'ACTION',
148 'transition' : 'TRANS',
149 'structure' : 'STRUCT',
150 'external_type' : 'EXTERN_TYPE',
151 'enumeration' : 'ENUM',
152 'peek' : 'PEEK',
153 'enqueue' : 'ENQUEUE',
154 'copy_head' : 'COPY_HEAD',
155 'check_allocate' : 'CHECK_ALLOCATE',
156 'check_stop_slots' : 'CHECK_STOP_SLOTS',
157 'if' : 'IF',
158 'else' : 'ELSE',
159 'return' : 'RETURN',
160 'THIS' : 'THIS',
161 'CHIP' : 'CHIP',
162 'void' : 'VOID',
163 'new' : 'NEW',
164 }
165
166 literals = ':[]{}(),='
167
168 tokens = [ 'EQ', 'NE', 'LT', 'GT', 'LE', 'GE',
169 'LEFTSHIFT', 'RIGHTSHIFT',
170 'NOT', 'AND', 'OR',
171 'PLUS', 'DASH', 'STAR', 'SLASH',
172 'DOUBLE_COLON', 'SEMI',
173 'ASSIGN', 'DOT',
174 'IDENT', 'LIT_BOOL', 'FLOATNUMBER', 'NUMBER', 'STRING' ]
175 tokens += reserved.values()
176
177 t_EQ = r'=='
178 t_NE = r'!='
179 t_LT = r'<'
180 t_GT = r'>'
181 t_LE = r'<='
182 t_GE = r'>='
183 t_LEFTSHIFT = r'<<'
184 t_RIGHTSHIFT = r'>>'
185 t_NOT = r'!'
186 t_AND = r'&&'
187 t_OR = r'\|\|'
188 t_PLUS = r'\+'
189 t_DASH = r'-'
190 t_STAR = r'\*'
191 t_SLASH = r'/'
192 t_DOUBLE_COLON = r'::'
193 t_SEMI = r';'
194 t_ASSIGN = r':='
195 t_DOT = r'\.'
196
197 precedence = (
198 ('left', 'AND', 'OR'),
199 ('left', 'EQ', 'NE'),
200 ('left', 'LT', 'GT', 'LE', 'GE'),
201 ('left', 'RIGHTSHIFT', 'LEFTSHIFT'),
202 ('left', 'PLUS', 'DASH'),
203 ('left', 'STAR', 'SLASH'),
204 ('right', 'NOT', 'UMINUS'),
205 )
206
207 def t_IDENT(self, t):
208 r'[a-zA-Z_][a-zA-Z_0-9]*'
209 if t.value == 'true':
210 t.type = 'LIT_BOOL'
211 t.value = True
212 return t
213
214 if t.value == 'false':
215 t.type = 'LIT_BOOL'
216 t.value = False
217 return t
218
219 # Check for reserved words
220 t.type = self.reserved.get(t.value, 'IDENT')
221 return t
222
223 def t_FLOATNUMBER(self, t):
224 '[0-9]+[.][0-9]+'
225 try:
226 t.value = float(t.value)
227 except ValueError:
228 raise TokenError("Illegal float", t)
229 return t
230
231 def t_NUMBER(self, t):
232 r'[0-9]+'
233 try:
234 t.value = int(t.value)
235 except ValueError:
236 raise TokenError("Illegal number", t)
237 return t
238
239 def t_STRING1(self, t):
240 r'\"[^"\n]*\"'
241 t.type = 'STRING'
242 t.value = t.value[1:-1]
243 return t
244
245 def t_STRING2(self, t):
246 r"\'[^'\n]*\'"
247 t.type = 'STRING'
248 t.value = t.value[1:-1]
249 return t
250
251 def p_file(self, p):
252 "file : decls"
253 p[0] = p[1]
254
255 def p_empty(self, p):
256 "empty :"
257
258 def p_decls(self, p):
259 "decls : declsx"
260 p[0] = ast.DeclListAST(self, p[1])
261
262 def p_declsx__list(self, p):
263 "declsx : decl declsx"
264 p[0] = [ p[1] ] + p[2]
265
266 def p_declsx__none(self, p):
267 "declsx : empty"
268 p[0] = []
269
270 def p_decl__machine(self, p):
271 "decl : MACHINE '(' ident pairs ')' ':' params '{' decls '}'"
272 p[0] = ast.MachineAST(self, p[3], p[4], p[7], p[9])
273
274 def p_decl__action(self, p):
275 "decl : ACTION '(' ident pairs ')' statements"
276 p[0] = ast.ActionDeclAST(self, p[3], p[4], p[6])
277
278 def p_decl__in_port(self, p):
279 "decl : IN_PORT '(' ident ',' type ',' var pairs ')' statements"
280 p[0] = ast.InPortDeclAST(self, p[3], p[5], p[7], p[8], p[10])
281
282 def p_decl__out_port(self, p):
283 "decl : OUT_PORT '(' ident ',' type ',' var pairs ')' SEMI"
284 p[0] = ast.OutPortDeclAST(self, p[3], p[5], p[7], p[8])
285
286 def p_decl__trans0(self, p):
287 "decl : TRANS '(' idents ',' idents ',' ident pairs ')' idents"
288 p[0] = ast.TransitionDeclAST(self, p[3], p[5], p[7], p[8], p[10])
289
290 def p_decl__trans1(self, p):
291 "decl : TRANS '(' idents ',' idents pairs ')' idents"
292 p[0] = ast.TransitionDeclAST(self, p[3], p[5], None, p[6], p[8])
293
294 def p_decl__extern0(self, p):
295 "decl : EXTERN_TYPE '(' type pairs ')' SEMI"
296 p[4]["external"] = "yes"
297 p[0] = ast.TypeDeclAST(self, p[3], p[4], [])
298
299 def p_decl__extern1(self, p):
300 "decl : EXTERN_TYPE '(' type pairs ')' '{' type_methods '}'"
301 p[4]["external"] = "yes"
302 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
303
304 def p_decl__global(self, p):
305 "decl : GLOBAL '(' type pairs ')' '{' type_members '}'"
306 p[4]["global"] = "yes"
307 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
308
309 def p_decl__struct(self, p):
310 "decl : STRUCT '(' type pairs ')' '{' type_members '}'"
311 p[0] = ast.TypeDeclAST(self, p[3], p[4], p[7])
312
313 def p_decl__enum(self, p):
314 "decl : ENUM '(' type pairs ')' '{' type_enums '}'"
315 p[4]["enumeration"] = "yes"
316 p[0] = ast.EnumDeclAST(self, p[3], p[4], p[7])
317
318 def p_decl__object(self, p):
319 "decl : type ident pairs SEMI"
320 p[0] = ast.ObjDeclAST(self, p[1], p[2], p[3])
321
322 def p_decl__func_decl(self, p):
323 """decl : void ident '(' params ')' pairs SEMI
324 | type ident '(' params ')' pairs SEMI"""
325 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], None)
326
327 def p_decl__func_def(self, p):
328 """decl : void ident '(' params ')' pairs statements
329 | type ident '(' params ')' pairs statements"""
330 p[0] = ast.FuncDeclAST(self, p[1], p[2], p[4], p[6], p[7])
331
332 # Type fields
333 def p_type_members__list(self, p):
334 "type_members : type_member type_members"
335 p[0] = [ p[1] ] + p[2]
336
337 def p_type_members__empty(self, p):
338 "type_members : empty"
339 p[0] = []
340
341 def p_type_member__1(self, p):
342 "type_member : type ident pairs SEMI"
343 p[0] = ast.TypeFieldMemberAST(self, p[1], p[2], p[3], None)
344
345 def p_type_member__2(self, p):
346 "type_member : type ident ASSIGN expr SEMI"
347 p[0] = ast.TypeFieldMemberAST(self, p[1], p[2],
348 ast.PairListAST(self), p[4])
349
350 # Methods
351 def p_type_methods__list(self, p):
352 "type_methods : type_method type_methods"
353 p[0] = [ p[1] ] + p[2]
354
355 def p_type_methods(self, p):
356 "type_methods : empty"
357 p[0] = []
358
359 def p_type_method(self, p):
360 "type_method : type_or_void ident '(' types ')' pairs SEMI"
361 p[0] = ast.TypeFieldMethodAST(self, p[1], p[2], p[4], p[6])
362
363 # Enum fields
364 def p_type_enums__list(self, p):
365 "type_enums : type_enum type_enums"
366 p[0] = [ p[1] ] + p[2]
367
368 def p_type_enums__empty(self, p):
369 "type_enums : empty"
370 p[0] = []
371
372 def p_type_enum(self, p):
373 "type_enum : ident pairs SEMI"
374 p[0] = ast.TypeFieldEnumAST(self, p[1], p[2])
375
376 # Type
377 def p_types__multiple(self, p):
378 "types : type ',' types"
379 p[0] = [ p[1] ] + p[3]
380
381 def p_types__one(self, p):
382 "types : type"
383 p[0] = [ p[1] ]
384
385 def p_types__empty(self, p):
386 "types : empty"
387 p[0] = []
388
389 def p_type(self, p):
390 "type : ident"
391 p[0] = ast.TypeAST(self, p[1])
392
393 def p_void(self, p):
394 "void : VOID"
395 p[0] = ast.TypeAST(self, p[1])
396
397 def p_type_or_void(self, p):
398 """type_or_void : type
399 | void"""
400 p[0] = p[1]
401
402 # Formal Param
403 def p_params__many(self, p):
404 "params : param ',' params"
405 p[0] = [ p[1] ] + p[3]
406
407 def p_params__one(self, p):
408 "params : param"
409 p[0] = [ p[1] ]
410
411 def p_params__none(self, p):
412 "params : empty"
413 p[0] = []
414
415 def p_param(self, p):
416 "param : type ident"
417 p[0] = ast.FormalParamAST(self, p[1], p[2])
418
419 def p_param__default(self, p):
420 "param : type ident '=' NUMBER"
421 p[0] = ast.FormalParamAST(self, p[1], p[2], p[4])
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 pairs ')' statements"
520 p[0] = ast.PeekStatementAST(self, p[3], p[5], p[6], p[8], "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]