Merged in new FastCPU stuff with existing code.
[gem5.git] / arch / isa_parser.py
1 #! /usr/bin/env python
2
3 # $Id$
4
5 # Copyright (c) 2003 The Regents of The University of Michigan
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are
10 # met: redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer;
12 # redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution;
15 # neither the name of the copyright holders nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 import os
32 import sys
33 import re
34 import string
35 import traceback
36 # get type names
37 from types import *
38
39 # Prepend the directory where the PLY lex & yacc modules are found
40 # to the search path. Assumes we're compiling in a subdirectory
41 # of 'build' in the current tree.
42 sys.path[0:0] = [os.environ['M5_EXT'] + '/ply']
43
44 import lex
45 import yacc
46
47 #####################################################################
48 #
49 # Lexer
50 #
51 # The PLY lexer module takes two things as input:
52 # - A list of token names (the string list 'tokens')
53 # - A regular expression describing a match for each token. The
54 # regexp for token FOO can be provided in two ways:
55 # - as a string variable named t_FOO
56 # - as the doc string for a function named t_FOO. In this case,
57 # the function is also executed, allowing an action to be
58 # associated with each token match.
59 #
60 #####################################################################
61
62 # Reserved words. These are listed separately as they are matched
63 # using the same regexp as generic IDs, but distinguished in the
64 # t_ID() function. The PLY documentation suggests this approach.
65 reserved = (
66 'BITFIELD', 'DECODE', 'DECODER', 'DEFAULT', 'DEF', 'EXEC', 'FORMAT',
67 'HEADER', 'LET', 'NAMESPACE', 'OPERAND_TYPES', 'OPERANDS',
68 'OUTPUT', 'SIGNED', 'TEMPLATE'
69 )
70
71 # List of tokens. The lex module requires this.
72 tokens = reserved + (
73 # identifier
74 'ID',
75
76 # integer literal
77 'INTLIT',
78
79 # string literal
80 'STRLIT',
81
82 # code literal
83 'CODELIT',
84
85 # ( ) [ ] { } < > , ; : :: *
86 'LPAREN', 'RPAREN',
87 # not used any more... commented out to suppress PLY warning
88 # 'LBRACKET', 'RBRACKET',
89 'LBRACE', 'RBRACE',
90 'LESS', 'GREATER',
91 'COMMA', 'SEMI', 'COLON', 'DBLCOLON',
92 'ASTERISK',
93
94 # C preprocessor directives
95 'CPPDIRECTIVE'
96 )
97
98 # Regular expressions for token matching
99 t_LPAREN = r'\('
100 t_RPAREN = r'\)'
101 # not used any more... commented out to suppress PLY warning
102 # t_LBRACKET = r'\['
103 # t_RBRACKET = r'\]'
104 t_LBRACE = r'\{'
105 t_RBRACE = r'\}'
106 t_LESS = r'\<'
107 t_GREATER = r'\>'
108 t_COMMA = r','
109 t_SEMI = r';'
110 t_COLON = r':'
111 t_DBLCOLON = r'::'
112 t_ASTERISK = r'\*'
113
114 # Identifiers and reserved words
115 reserved_map = { }
116 for r in reserved:
117 reserved_map[r.lower()] = r
118
119 def t_ID(t):
120 r'[A-Za-z_]\w*'
121 t.type = reserved_map.get(t.value,'ID')
122 return t
123
124 # Integer literal
125 def t_INTLIT(t):
126 r'(0x[\da-fA-F]+)|\d+'
127 try:
128 t.value = int(t.value,0)
129 except ValueError:
130 error(t.lineno, 'Integer value "%s" too large' % t.value)
131 t.value = 0
132 return t
133
134 # String literal. Note that these use only single quotes, and
135 # can span multiple lines.
136 def t_STRLIT(t):
137 r"(?m)'([^'])+'"
138 # strip off quotes
139 t.value = t.value[1:-1]
140 t.lineno += t.value.count('\n')
141 return t
142
143
144 # "Code literal"... like a string literal, but delimiters are
145 # '{{' and '}}' so they get formatted nicely under emacs c-mode
146 def t_CODELIT(t):
147 r"(?m)\{\{([^\}]|}(?!\}))+\}\}"
148 # strip off {{ & }}
149 t.value = t.value[2:-2]
150 t.lineno += t.value.count('\n')
151 return t
152
153 def t_CPPDIRECTIVE(t):
154 r'^\#.*\n'
155 t.lineno += t.value.count('\n')
156 return t
157
158 #
159 # The functions t_NEWLINE, t_ignore, and t_error are
160 # special for the lex module.
161 #
162
163 # Newlines
164 def t_NEWLINE(t):
165 r'\n+'
166 t.lineno += t.value.count('\n')
167
168 # Comments
169 def t_comment(t):
170 r'//.*'
171
172 # Completely ignored characters
173 t_ignore = ' \t\x0c'
174
175 # Error handler
176 def t_error(t):
177 error(t.lineno, "illegal character '%s'" % t.value[0])
178 t.skip(1)
179
180 # Build the lexer
181 lex.lex()
182
183 #####################################################################
184 #
185 # Parser
186 #
187 # Every function whose name starts with 'p_' defines a grammar rule.
188 # The rule is encoded in the function's doc string, while the
189 # function body provides the action taken when the rule is matched.
190 # The argument to each function is a list of the values of the
191 # rule's symbols: t[0] for the LHS, and t[1..n] for the symbols
192 # on the RHS. For tokens, the value is copied from the t.value
193 # attribute provided by the lexer. For non-terminals, the value
194 # is assigned by the producing rule; i.e., the job of the grammar
195 # rule function is to set the value for the non-terminal on the LHS
196 # (by assigning to t[0]).
197 #####################################################################
198
199 # The LHS of the first grammar rule is used as the start symbol
200 # (in this case, 'specification'). Note that this rule enforces
201 # that there will be exactly one namespace declaration, with 0 or more
202 # global defs/decls before and after it. The defs & decls before
203 # the namespace decl will be outside the namespace; those after
204 # will be inside. The decoder function is always inside the namespace.
205 def p_specification(t):
206 'specification : opt_defs_and_outputs name_decl opt_defs_and_outputs decode_block'
207 global_code = t[1]
208 isa_name = t[2]
209 namespace = isa_name + "Inst"
210 # wrap the decode block as a function definition
211 t[4].wrap_decode_block('''
212 StaticInstPtr<%(isa_name)s>
213 %(isa_name)s::decodeInst(%(isa_name)s::MachInst machInst)
214 {
215 using namespace %(namespace)s;
216 ''' % vars(), '}')
217 # both the latter output blocks and the decode block are in the namespace
218 namespace_code = t[3] + t[4]
219 # pass it all back to the caller of yacc.parse()
220 t[0] = (isa_name, namespace, global_code, namespace_code)
221
222 # ISA name declaration looks like "namespace <foo>;"
223 def p_name_decl(t):
224 'name_decl : NAMESPACE ID SEMI'
225 t[0] = t[2]
226
227 # 'opt_defs_and_outputs' is a possibly empty sequence of
228 # def and/or output statements.
229 def p_opt_defs_and_outputs_0(t):
230 'opt_defs_and_outputs : empty'
231 t[0] = GenCode()
232
233 def p_opt_defs_and_outputs_1(t):
234 'opt_defs_and_outputs : defs_and_outputs'
235 t[0] = t[1]
236
237 def p_defs_and_outputs_0(t):
238 'defs_and_outputs : def_or_output'
239 t[0] = t[1]
240
241 def p_defs_and_outputs_1(t):
242 'defs_and_outputs : defs_and_outputs def_or_output'
243 t[0] = t[1] + t[2]
244
245 # The list of possible definition/output statements.
246 def p_def_or_output(t):
247 '''def_or_output : def_format
248 | def_bitfield
249 | def_template
250 | def_operand_types
251 | def_operands
252 | output_header
253 | output_decoder
254 | output_exec
255 | global_let'''
256 t[0] = t[1]
257
258 # Output blocks 'output <foo> {{...}}' (C++ code blocks) are copied
259 # directly to the appropriate output section.
260
261 # Massage output block by substituting in template definitions and bit
262 # operators. We handle '%'s embedded in the string that don't
263 # indicate template substitutions (or CPU-specific symbols, which get
264 # handled in GenCode) by doubling them first so that the format
265 # operation will reduce them back to single '%'s.
266 def process_output(s):
267 # protect any non-substitution '%'s (not followed by '(')
268 s = re.sub(r'%(?!\()', '%%', s)
269 # protects cpu-specific symbols too
270 s = protect_cpu_symbols(s)
271 return substBitOps(s % templateMap)
272
273 def p_output_header(t):
274 'output_header : OUTPUT HEADER CODELIT SEMI'
275 t[0] = GenCode(header_output = process_output(t[3]))
276
277 def p_output_decoder(t):
278 'output_decoder : OUTPUT DECODER CODELIT SEMI'
279 t[0] = GenCode(decoder_output = process_output(t[3]))
280
281 def p_output_exec(t):
282 'output_exec : OUTPUT EXEC CODELIT SEMI'
283 t[0] = GenCode(exec_output = process_output(t[3]))
284
285 # global let blocks 'let {{...}}' (Python code blocks) are executed
286 # directly when seen. Note that these execute in a special variable
287 # context 'exportContext' to prevent the code from polluting this
288 # script's namespace.
289 def p_global_let(t):
290 'global_let : LET CODELIT SEMI'
291 updateExportContext()
292 try:
293 exec fixPythonIndentation(t[2]) in exportContext
294 except Exception, exc:
295 error(t.lineno(1),
296 'error: %s in global let block "%s".' % (exc, t[2]))
297 t[0] = GenCode() # contributes nothing to the output C++ file
298
299 # Define the mapping from operand type extensions to C++ types and bit
300 # widths (stored in operandTypeMap).
301 def p_def_operand_types(t):
302 'def_operand_types : DEF OPERAND_TYPES CODELIT SEMI'
303 s = 'global operandTypeMap; operandTypeMap = {' + t[3] + '}'
304 try:
305 exec s
306 except Exception, exc:
307 error(t.lineno(1),
308 'error: %s in def operand_types block "%s".' % (exc, t[3]))
309 t[0] = GenCode() # contributes nothing to the output C++ file
310
311 # Define the mapping from operand names to operand classes and other
312 # traits. Stored in operandTraitsMap.
313 def p_def_operands(t):
314 'def_operands : DEF OPERANDS CODELIT SEMI'
315 s = 'global operandTraitsMap; operandTraitsMap = {' + t[3] + '}'
316 try:
317 exec s
318 except Exception, exc:
319 error(t.lineno(1),
320 'error: %s in def operands block "%s".' % (exc, t[3]))
321 defineDerivedOperandVars()
322 t[0] = GenCode() # contributes nothing to the output C++ file
323
324 # A bitfield definition looks like:
325 # 'def [signed] bitfield <ID> [<first>:<last>]'
326 # This generates a preprocessor macro in the output file.
327 def p_def_bitfield_0(t):
328 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT COLON INTLIT GREATER SEMI'
329 expr = 'bits(machInst, %2d, %2d)' % (t[6], t[8])
330 if (t[2] == 'signed'):
331 expr = 'sext<%d>(%s)' % (t[6] - t[8] + 1, expr)
332 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
333 t[0] = GenCode(header_output = hash_define)
334
335 # alternate form for single bit: 'def [signed] bitfield <ID> [<bit>]'
336 def p_def_bitfield_1(t):
337 'def_bitfield : DEF opt_signed BITFIELD ID LESS INTLIT GREATER SEMI'
338 expr = 'bits(machInst, %2d, %2d)' % (t[6], t[6])
339 if (t[2] == 'signed'):
340 expr = 'sext<%d>(%s)' % (1, expr)
341 hash_define = '#undef %s\n#define %s\t%s\n' % (t[4], t[4], expr)
342 t[0] = GenCode(header_output = hash_define)
343
344 def p_opt_signed_0(t):
345 'opt_signed : SIGNED'
346 t[0] = t[1]
347
348 def p_opt_signed_1(t):
349 'opt_signed : empty'
350 t[0] = ''
351
352 # Global map variable to hold templates
353 templateMap = {}
354
355 def p_def_template(t):
356 'def_template : DEF TEMPLATE ID CODELIT SEMI'
357 templateMap[t[3]] = Template(t[4])
358 t[0] = GenCode()
359
360 # An instruction format definition looks like
361 # "def format <fmt>(<params>) {{...}};"
362 def p_def_format(t):
363 'def_format : DEF FORMAT ID LPAREN param_list RPAREN CODELIT SEMI'
364 (id, params, code) = (t[3], t[5], t[7])
365 defFormat(id, params, code, t.lineno(1))
366 t[0] = GenCode()
367
368 # The formal parameter list for an instruction format is a possibly
369 # empty list of comma-separated parameters.
370 def p_param_list_0(t):
371 'param_list : empty'
372 t[0] = [ ]
373
374 def p_param_list_1(t):
375 'param_list : param'
376 t[0] = [t[1]]
377
378 def p_param_list_2(t):
379 'param_list : param_list COMMA param'
380 t[0] = t[1]
381 t[0].append(t[3])
382
383 # Each formal parameter is either an identifier or an identifier
384 # preceded by an asterisk. As in Python, the latter (if present) gets
385 # a tuple containing all the excess positional arguments, allowing
386 # varargs functions.
387 def p_param_0(t):
388 'param : ID'
389 t[0] = t[1]
390
391 def p_param_1(t):
392 'param : ASTERISK ID'
393 # just concatenate them: '*ID'
394 t[0] = t[1] + t[2]
395
396 # End of format definition-related rules.
397 ##############
398
399 #
400 # A decode block looks like:
401 # decode <field1> [, <field2>]* [default <inst>] { ... }
402 #
403 def p_decode_block(t):
404 'decode_block : DECODE ID opt_default LBRACE decode_stmt_list RBRACE'
405 default_defaults = defaultStack.pop()
406 codeObj = t[5]
407 # use the "default defaults" only if there was no explicit
408 # default statement in decode_stmt_list
409 if not codeObj.has_decode_default:
410 codeObj += default_defaults
411 codeObj.wrap_decode_block('switch (%s) {\n' % t[2], '}\n')
412 t[0] = codeObj
413
414 # The opt_default statement serves only to push the "default defaults"
415 # onto defaultStack. This value will be used by nested decode blocks,
416 # and used and popped off when the current decode_block is processed
417 # (in p_decode_block() above).
418 def p_opt_default_0(t):
419 'opt_default : empty'
420 # no default specified: reuse the one currently at the top of the stack
421 defaultStack.push(defaultStack.top())
422 # no meaningful value returned
423 t[0] = None
424
425 def p_opt_default_1(t):
426 'opt_default : DEFAULT inst'
427 # push the new default
428 codeObj = t[2]
429 codeObj.wrap_decode_block('\ndefault:\n', 'break;\n')
430 defaultStack.push(codeObj)
431 # no meaningful value returned
432 t[0] = None
433
434 def p_decode_stmt_list_0(t):
435 'decode_stmt_list : decode_stmt'
436 t[0] = t[1]
437
438 def p_decode_stmt_list_1(t):
439 'decode_stmt_list : decode_stmt decode_stmt_list'
440 if (t[1].has_decode_default and t[2].has_decode_default):
441 error(t.lineno(1), 'Two default cases in decode block')
442 t[0] = t[1] + t[2]
443
444 #
445 # Decode statement rules
446 #
447 # There are four types of statements allowed in a decode block:
448 # 1. Format blocks 'format <foo> { ... }'
449 # 2. Nested decode blocks
450 # 3. Instruction definitions.
451 # 4. C preprocessor directives.
452
453
454 # Preprocessor directives found in a decode statement list are passed
455 # through to the output, replicated to all of the output code
456 # streams. This works well for ifdefs, so we can ifdef out both the
457 # declarations and the decode cases generated by an instruction
458 # definition. Handling them as part of the grammar makes it easy to
459 # keep them in the right place with respect to the code generated by
460 # the other statements.
461 def p_decode_stmt_cpp(t):
462 'decode_stmt : CPPDIRECTIVE'
463 t[0] = GenCode(t[1], t[1], t[1], t[1])
464
465 # A format block 'format <foo> { ... }' sets the default instruction
466 # format used to handle instruction definitions inside the block.
467 # This format can be overridden by using an explicit format on the
468 # instruction definition or with a nested format block.
469 def p_decode_stmt_format(t):
470 'decode_stmt : FORMAT push_format_id LBRACE decode_stmt_list RBRACE'
471 # The format will be pushed on the stack when 'push_format_id' is
472 # processed (see below). Once the parser has recognized the full
473 # production (though the right brace), we're done with the format,
474 # so now we can pop it.
475 formatStack.pop()
476 t[0] = t[4]
477
478 # This rule exists so we can set the current format (& push the stack)
479 # when we recognize the format name part of the format block.
480 def p_push_format_id(t):
481 'push_format_id : ID'
482 try:
483 formatStack.push(formatMap[t[1]])
484 t[0] = ('', '// format %s' % t[1])
485 except KeyError:
486 error(t.lineno(1), 'instruction format "%s" not defined.' % t[1])
487
488 # Nested decode block: if the value of the current field matches the
489 # specified constant, do a nested decode on some other field.
490 def p_decode_stmt_decode(t):
491 'decode_stmt : case_label COLON decode_block'
492 label = t[1]
493 codeObj = t[3]
494 # just wrap the decoding code from the block as a case in the
495 # outer switch statement.
496 codeObj.wrap_decode_block('\n%s:\n' % label)
497 codeObj.has_decode_default = (label == 'default')
498 t[0] = codeObj
499
500 # Instruction definition (finally!).
501 def p_decode_stmt_inst(t):
502 'decode_stmt : case_label COLON inst SEMI'
503 label = t[1]
504 codeObj = t[3]
505 codeObj.wrap_decode_block('\n%s:' % label, 'break;\n')
506 codeObj.has_decode_default = (label == 'default')
507 t[0] = codeObj
508
509 # The case label is either a list of one or more constants or 'default'
510 def p_case_label_0(t):
511 'case_label : intlit_list'
512 t[0] = ': '.join(map(lambda a: 'case %#x' % a, t[1]))
513
514 def p_case_label_1(t):
515 'case_label : DEFAULT'
516 t[0] = 'default'
517
518 #
519 # The constant list for a decode case label must be non-empty, but may have
520 # one or more comma-separated integer literals in it.
521 #
522 def p_intlit_list_0(t):
523 'intlit_list : INTLIT'
524 t[0] = [t[1]]
525
526 def p_intlit_list_1(t):
527 'intlit_list : intlit_list COMMA INTLIT'
528 t[0] = t[1]
529 t[0].append(t[3])
530
531 # Define an instruction using the current instruction format (specified
532 # by an enclosing format block).
533 # "<mnemonic>(<args>)"
534 def p_inst_0(t):
535 'inst : ID LPAREN arg_list RPAREN'
536 # Pass the ID and arg list to the current format class to deal with.
537 currentFormat = formatStack.top()
538 codeObj = currentFormat.defineInst(t[1], t[3], t.lineno(1))
539 args = ','.join(map(str, t[3]))
540 args = re.sub('(?m)^', '//', args)
541 args = re.sub('^//', '', args)
542 comment = '\n// %s::%s(%s)\n' % (currentFormat.id, t[1], args)
543 codeObj.prepend_all(comment)
544 t[0] = codeObj
545
546 # Define an instruction using an explicitly specified format:
547 # "<fmt>::<mnemonic>(<args>)"
548 def p_inst_1(t):
549 'inst : ID DBLCOLON ID LPAREN arg_list RPAREN'
550 try:
551 format = formatMap[t[1]]
552 except KeyError:
553 error(t.lineno(1), 'instruction format "%s" not defined.' % t[1])
554 codeObj = format.defineInst(t[3], t[5], t.lineno(1))
555 comment = '\n// %s::%s(%s)\n' % (t[1], t[3], t[5])
556 codeObj.prepend_all(comment)
557 t[0] = codeObj
558
559 def p_arg_list_0(t):
560 'arg_list : empty'
561 t[0] = [ ]
562
563 def p_arg_list_1(t):
564 'arg_list : arg'
565 t[0] = [t[1]]
566
567 def p_arg_list_2(t):
568 'arg_list : arg_list COMMA arg'
569 t[0] = t[1]
570 t[0].append(t[3])
571
572 def p_arg(t):
573 '''arg : ID
574 | INTLIT
575 | STRLIT
576 | CODELIT'''
577 t[0] = t[1]
578
579 #
580 # Empty production... use in other rules for readability.
581 #
582 def p_empty(t):
583 'empty :'
584 pass
585
586 # Parse error handler. Note that the argument here is the offending
587 # *token*, not a grammar symbol (hence the need to use t.value)
588 def p_error(t):
589 if t:
590 error(t.lineno, "syntax error at '%s'" % t.value)
591 else:
592 error_bt(0, "unknown syntax error")
593
594 # END OF GRAMMAR RULES
595 #
596 # Now build the parser.
597 yacc.yacc()
598
599
600 #####################################################################
601 #
602 # Support Classes
603 #
604 #####################################################################
605
606 ################
607 # CpuModel class
608 #
609 # The CpuModel class encapsulates everything we need to know about a
610 # particular CPU model.
611
612 class CpuModel:
613 # List of all CPU models. Accessible as CpuModel.list.
614 list = []
615
616 # Constructor. Automatically adds models to CpuModel.list.
617 def __init__(self, name, filename, includes, strings):
618 self.name = name
619 self.filename = filename # filename for output exec code
620 self.includes = includes # include files needed in exec file
621 # The 'strings' dict holds all the per-CPU symbols we can
622 # substitute into templates etc.
623 self.strings = strings
624 # Add self to list.
625 CpuModel.list.append(self)
626
627 # Define CPU models. The following lines should contain the only
628 # CPU-model-specific information in this file. Note that the ISA
629 # description itself should have *no* CPU-model-specific content.
630 CpuModel('SimpleCPU', 'simple_cpu_exec.cc',
631 '#include "cpu/simple_cpu/simple_cpu.hh"',
632 { 'CPU_exec_context': 'SimpleCPU' })
633 CpuModel('FullCPU', 'full_cpu_exec.cc',
634 '#include "cpu/full_cpu/dyn_inst.hh"',
635 { 'CPU_exec_context': 'DynInst' })
636
637 # Expand template with CPU-specific references into a dictionary with
638 # an entry for each CPU model name. The entry key is the model name
639 # and the corresponding value is the template with the CPU-specific
640 # refs substituted for that model.
641 def expand_cpu_symbols_to_dict(template):
642 # Protect '%'s that don't go with CPU-specific terms
643 t = re.sub(r'%(?!\(CPU_)', '%%', template)
644 result = {}
645 for cpu in CpuModel.list:
646 result[cpu.name] = t % cpu.strings
647 return result
648
649 # *If* the template has CPU-specific references, return a single
650 # string containing a copy of the template for each CPU model with the
651 # corresponding values substituted in. If the template has no
652 # CPU-specific references, it is returned unmodified.
653 def expand_cpu_symbols_to_string(template):
654 if template.find('%(CPU_') != -1:
655 return reduce(lambda x,y: x+y,
656 expand_cpu_symbols_to_dict(template).values())
657 else:
658 return template
659
660 # Protect CPU-specific references by doubling the corresponding '%'s
661 # (in preparation for substituting a different set of references into
662 # the template).
663 def protect_cpu_symbols(template):
664 return re.sub(r'%(?=\(CPU_)', '%%', template)
665
666 ###############
667 # GenCode class
668 #
669 # The GenCode class encapsulates generated code destined for various
670 # output files. The header_output and decoder_output attributes are
671 # strings containing code destined for decoder.hh and decoder.cc
672 # respectively. The decode_block attribute contains code to be
673 # incorporated in the decode function itself (that will also end up in
674 # decoder.cc). The exec_output attribute is a dictionary with a key
675 # for each CPU model name; the value associated with a particular key
676 # is the string of code for that CPU model's exec.cc file. The
677 # has_decode_default attribute is used in the decode block to allow
678 # explicit default clauses to override default default clauses.
679
680 class GenCode:
681 # Constructor. At this point we substitute out all CPU-specific
682 # symbols. For the exec output, these go into the per-model
683 # dictionary. For all other output types they get collapsed into
684 # a single string.
685 def __init__(self,
686 header_output = '', decoder_output = '', exec_output = '',
687 decode_block = '', has_decode_default = False):
688 self.header_output = expand_cpu_symbols_to_string(header_output)
689 self.decoder_output = expand_cpu_symbols_to_string(decoder_output)
690 if isinstance(exec_output, dict):
691 self.exec_output = exec_output
692 elif isinstance(exec_output, str):
693 # If the exec_output arg is a single string, we replicate
694 # it for each of the CPU models, substituting and
695 # %(CPU_foo)s params appropriately.
696 self.exec_output = expand_cpu_symbols_to_dict(exec_output)
697 self.decode_block = expand_cpu_symbols_to_string(decode_block)
698 self.has_decode_default = has_decode_default
699
700 # Override '+' operator: generate a new GenCode object that
701 # concatenates all the individual strings in the operands.
702 def __add__(self, other):
703 exec_output = {}
704 for cpu in CpuModel.list:
705 n = cpu.name
706 exec_output[n] = self.exec_output[n] + other.exec_output[n]
707 return GenCode(self.header_output + other.header_output,
708 self.decoder_output + other.decoder_output,
709 exec_output,
710 self.decode_block + other.decode_block,
711 self.has_decode_default or other.has_decode_default)
712
713 # Prepend a string (typically a comment) to all the strings.
714 def prepend_all(self, pre):
715 self.header_output = pre + self.header_output
716 self.decoder_output = pre + self.decoder_output
717 self.decode_block = pre + self.decode_block
718 for cpu in CpuModel.list:
719 self.exec_output[cpu.name] = pre + self.exec_output[cpu.name]
720
721 # Wrap the decode block in a pair of strings (e.g., 'case foo:'
722 # and 'break;'). Used to build the big nested switch statement.
723 def wrap_decode_block(self, pre, post = ''):
724 self.decode_block = pre + indent(self.decode_block) + post
725
726 ################
727 # Format object.
728 #
729 # A format object encapsulates an instruction format. It must provide
730 # a defineInst() method that generates the code for an instruction
731 # definition.
732
733 class Format:
734 def __init__(self, id, params, code):
735 # constructor: just save away arguments
736 self.id = id
737 self.params = params
738 label = 'def format ' + id
739 self.user_code = compile(fixPythonIndentation(code), label, 'exec')
740 param_list = string.join(params, ", ")
741 f = '''def defInst(_code, _context, %s):
742 my_locals = vars().copy()
743 exec _code in _context, my_locals
744 return my_locals\n''' % param_list
745 c = compile(f, label + ' wrapper', 'exec')
746 exec c
747 self.func = defInst
748
749 def defineInst(self, name, args, lineno):
750 context = {}
751 updateExportContext()
752 context.update(exportContext)
753 context.update({ 'name': name, 'Name': string.capitalize(name) })
754 try:
755 vars = self.func(self.user_code, context, *args)
756 except Exception, exc:
757 error(lineno, 'error defining "%s": %s.' % (name, exc))
758 for k in vars.keys():
759 if k not in ('header_output', 'decoder_output',
760 'exec_output', 'decode_block'):
761 del vars[k]
762 return GenCode(**vars)
763
764 # Special null format to catch an implicit-format instruction
765 # definition outside of any format block.
766 class NoFormat:
767 def __init__(self):
768 self.defaultInst = ''
769
770 def defineInst(self, name, args, lineno):
771 error(lineno,
772 'instruction definition "%s" with no active format!' % name)
773
774 # This dictionary maps format name strings to Format objects.
775 formatMap = {}
776
777 # Define a new format
778 def defFormat(id, params, code, lineno):
779 # make sure we haven't already defined this one
780 if formatMap.get(id, None) != None:
781 error(lineno, 'format %s redefined.' % id)
782 # create new object and store in global map
783 formatMap[id] = Format(id, params, code)
784
785
786 ##############
787 # Stack: a simple stack object. Used for both formats (formatStack)
788 # and default cases (defaultStack).
789
790 class Stack:
791 def __init__(self, initItem):
792 self.stack = [ initItem ]
793
794 def push(self, item):
795 self.stack.append(item);
796
797 def pop(self):
798 return self.stack.pop()
799
800 def top(self):
801 return self.stack[-1]
802
803 # The global format stack.
804 formatStack = Stack(NoFormat())
805
806 # The global default case stack.
807 defaultStack = Stack( None )
808
809 ###################
810 # Utility functions
811
812 #
813 # Indent every line in string 's' by two spaces
814 # (except preprocessor directives).
815 # Used to make nested code blocks look pretty.
816 #
817 def indent(s):
818 return re.sub(r'(?m)^(?!\#)', ' ', s)
819
820 #
821 # Munge a somewhat arbitrarily formatted piece of Python code
822 # (e.g. from a format 'let' block) into something whose indentation
823 # will get by the Python parser.
824 #
825 # The two keys here are that Python will give a syntax error if
826 # there's any whitespace at the beginning of the first line, and that
827 # all lines at the same lexical nesting level must have identical
828 # indentation. Unfortunately the way code literals work, an entire
829 # let block tends to have some initial indentation. Rather than
830 # trying to figure out what that is and strip it off, we prepend 'if
831 # 1:' to make the let code the nested block inside the if (and have
832 # the parser automatically deal with the indentation for us).
833 #
834 # We don't want to do this if (1) the code block is empty or (2) the
835 # first line of the block doesn't have any whitespace at the front.
836
837 def fixPythonIndentation(s):
838 # get rid of blank lines first
839 s = re.sub(r'(?m)^\s*\n', '', s);
840 if (s != '' and re.match(r'[ \t]', s[0])):
841 s = 'if 1:\n' + s
842 return s
843
844 # Error handler. Just call exit. Output formatted to work under
845 # Emacs compile-mode.
846 def error(lineno, string):
847 sys.exit("%s:%d: %s" % (input_filename, lineno, string))
848
849 # Like error(), but include a Python stack backtrace (for processing
850 # Python exceptions).
851 def error_bt(lineno, string):
852 traceback.print_exc()
853 print >> sys.stderr, "%s:%d: %s" % (input_filename, lineno, string)
854 sys.exit(1)
855
856
857 #####################################################################
858 #
859 # Bitfield Operator Support
860 #
861 #####################################################################
862
863 bitOp1ArgRE = re.compile(r'<\s*(\w+)\s*:\s*>')
864
865 bitOpWordRE = re.compile(r'(?<![\w\.])([\w\.]+)<\s*(\w+)\s*:\s*(\w+)\s*>')
866 bitOpExprRE = re.compile(r'\)<\s*(\w+)\s*:\s*(\w+)\s*>')
867
868 def substBitOps(code):
869 # first convert single-bit selectors to two-index form
870 # i.e., <n> --> <n:n>
871 code = bitOp1ArgRE.sub(r'<\1:\1>', code)
872 # simple case: selector applied to ID (name)
873 # i.e., foo<a:b> --> bits(foo, a, b)
874 code = bitOpWordRE.sub(r'bits(\1, \2, \3)', code)
875 # if selector is applied to expression (ending in ')'),
876 # we need to search backward for matching '('
877 match = bitOpExprRE.search(code)
878 while match:
879 exprEnd = match.start()
880 here = exprEnd - 1
881 nestLevel = 1
882 while nestLevel > 0:
883 if code[here] == '(':
884 nestLevel -= 1
885 elif code[here] == ')':
886 nestLevel += 1
887 here -= 1
888 if here < 0:
889 sys.exit("Didn't find '('!")
890 exprStart = here+1
891 newExpr = r'bits(%s, %s, %s)' % (code[exprStart:exprEnd+1],
892 match.group(1), match.group(2))
893 code = code[:exprStart] + newExpr + code[match.end():]
894 match = bitOpExprRE.search(code)
895 return code
896
897
898 ####################
899 # Template objects.
900 #
901 # Template objects are format strings that allow substitution from
902 # the attribute spaces of other objects (e.g. InstObjParams instances).
903
904 class Template:
905 def __init__(self, t):
906 self.template = t
907
908 def subst(self, d):
909 # Start with the template namespace. Make a copy since we're
910 # going to modify it.
911 myDict = templateMap.copy()
912 # if the argument is a dictionary, we just use it.
913 if isinstance(d, dict):
914 myDict.update(d)
915 # if the argument is an object, we use its attribute map.
916 elif hasattr(d, '__dict__'):
917 myDict.update(d.__dict__)
918 else:
919 raise TypeError, "Template.subst() arg must be or have dictionary"
920 # CPU-model-specific substitutions are handled later (in GenCode).
921 return protect_cpu_symbols(self.template) % myDict
922
923 # Convert to string. This handles the case when a template with a
924 # CPU-specific term gets interpolated into another template or into
925 # an output block.
926 def __str__(self):
927 return expand_cpu_symbols_to_string(self.template)
928
929 #####################################################################
930 #
931 # Code Parser
932 #
933 # The remaining code is the support for automatically extracting
934 # instruction characteristics from pseudocode.
935 #
936 #####################################################################
937
938 # Force the argument to be a list
939 def makeList(list_or_item):
940 if not list_or_item:
941 return []
942 elif type(list_or_item) == ListType:
943 return list_or_item
944 else:
945 return [ list_or_item ]
946
947 # generate operandSizeMap based on provided operandTypeMap:
948 # basically generate equiv. C++ type and make is_signed flag
949 def buildOperandSizeMap():
950 global operandSizeMap
951 operandSizeMap = {}
952 for ext in operandTypeMap.keys():
953 (desc, size) = operandTypeMap[ext]
954 if desc == 'signed int':
955 type = 'int%d_t' % size
956 is_signed = 1
957 elif desc == 'unsigned int':
958 type = 'uint%d_t' % size
959 is_signed = 0
960 elif desc == 'float':
961 is_signed = 1 # shouldn't really matter
962 if size == 32:
963 type = 'float'
964 elif size == 64:
965 type = 'double'
966 if type == '':
967 error(0, 'Unrecognized type description "%s" in operandTypeMap')
968 operandSizeMap[ext] = (size, type, is_signed)
969
970 #
971 # Base class for operand traits. An instance of this class (or actually
972 # a class derived from this one) encapsulates the traits of a particular
973 # operand type (e.g., "32-bit integer register").
974 #
975 class OperandTraits:
976 def __init__(self, dflt_ext, reg_spec, flags, sort_pri):
977 # Force construction of operandSizeMap from operandTypeMap
978 # if it hasn't happened yet
979 if not globals().has_key('operandSizeMap'):
980 buildOperandSizeMap()
981 self.dflt_ext = dflt_ext
982 (self.dflt_size, self.dflt_type, self.dflt_is_signed) = \
983 operandSizeMap[dflt_ext]
984 self.reg_spec = reg_spec
985 # Canonical flag structure is a triple of lists, where each list
986 # indicates the set of flags implied by this operand always, when
987 # used as a source, and when used as a dest, respectively.
988 # For simplicity this can be initialized using a variety of fairly
989 # obvious shortcuts; we convert these to canonical form here.
990 if not flags:
991 # no flags specified (e.g., 'None')
992 self.flags = ( [], [], [] )
993 elif type(flags) == StringType:
994 # a single flag: assumed to be unconditional
995 self.flags = ( [ flags ], [], [] )
996 elif type(flags) == ListType:
997 # a list of flags: also assumed to be unconditional
998 self.flags = ( flags, [], [] )
999 elif type(flags) == TupleType:
1000 # it's a tuple: it should be a triple,
1001 # but each item could be a single string or a list
1002 (uncond_flags, src_flags, dest_flags) = flags
1003 self.flags = (makeList(uncond_flags),
1004 makeList(src_flags), makeList(dest_flags))
1005 self.sort_pri = sort_pri
1006
1007 def isMem(self):
1008 return 0
1009
1010 def isReg(self):
1011 return 0
1012
1013 def isFloatReg(self):
1014 return 0
1015
1016 def isIntReg(self):
1017 return 0
1018
1019 def isControlReg(self):
1020 return 0
1021
1022 def getFlags(self, op_desc):
1023 # note the empty slice '[:]' gives us a copy of self.flags[0]
1024 # instead of a reference to it
1025 my_flags = self.flags[0][:]
1026 if op_desc.is_src:
1027 my_flags += self.flags[1]
1028 if op_desc.is_dest:
1029 my_flags += self.flags[2]
1030 return my_flags
1031
1032 def makeDecl(self, op_desc):
1033 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1034 # Note that initializations in the declarations are solely
1035 # to avoid 'uninitialized variable' errors from the compiler.
1036 return type + ' ' + op_desc.munged_name + ' = 0;\n';
1037
1038 class IntRegOperandTraits(OperandTraits):
1039 def isReg(self):
1040 return 1
1041
1042 def isIntReg(self):
1043 return 1
1044
1045 def makeConstructor(self, op_desc):
1046 c = ''
1047 if op_desc.is_src:
1048 c += '\n\t_srcRegIdx[%d] = %s;' % \
1049 (op_desc.src_reg_idx, self.reg_spec)
1050 if op_desc.is_dest:
1051 c += '\n\t_destRegIdx[%d] = %s;' % \
1052 (op_desc.dest_reg_idx, self.reg_spec)
1053 return c
1054
1055 def makeRead(self, op_desc):
1056 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1057 if (type == 'float' or type == 'double'):
1058 error(0, 'Attempt to read integer register as FP')
1059 if (size == self.dflt_size):
1060 return '%s = xc->readIntReg(this, %d);\n' % \
1061 (op_desc.munged_name, op_desc.src_reg_idx)
1062 else:
1063 return '%s = bits(xc->readIntReg(this, %d), %d, 0);\n' % \
1064 (op_desc.munged_name, op_desc.src_reg_idx, size-1)
1065
1066 def makeWrite(self, op_desc):
1067 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1068 if (type == 'float' or type == 'double'):
1069 error(0, 'Attempt to write integer register as FP')
1070 if (size != self.dflt_size and is_signed):
1071 final_val = 'sext<%d>(%s)' % (size, op_desc.munged_name)
1072 else:
1073 final_val = op_desc.munged_name
1074 wb = '''
1075 {
1076 %s final_val = %s;
1077 xc->setIntReg(this, %d, final_val);\n
1078 if (traceData) { traceData->setData(final_val); }
1079 }''' % (self.dflt_type, final_val, op_desc.dest_reg_idx)
1080 return wb
1081
1082 class FloatRegOperandTraits(OperandTraits):
1083 def isReg(self):
1084 return 1
1085
1086 def isFloatReg(self):
1087 return 1
1088
1089 def makeConstructor(self, op_desc):
1090 c = ''
1091 if op_desc.is_src:
1092 c += '\n\t_srcRegIdx[%d] = %s + FP_Base_DepTag;' % \
1093 (op_desc.src_reg_idx, self.reg_spec)
1094 if op_desc.is_dest:
1095 c += '\n\t_destRegIdx[%d] = %s + FP_Base_DepTag;' % \
1096 (op_desc.dest_reg_idx, self.reg_spec)
1097 return c
1098
1099 def makeRead(self, op_desc):
1100 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1101 bit_select = 0
1102 if (type == 'float'):
1103 func = 'readFloatRegSingle'
1104 elif (type == 'double'):
1105 func = 'readFloatRegDouble'
1106 else:
1107 func = 'readFloatRegInt'
1108 if (size != self.dflt_size):
1109 bit_select = 1
1110 base = 'xc->%s(this, %d)' % \
1111 (func, op_desc.src_reg_idx)
1112 if bit_select:
1113 return '%s = bits(%s, %d, 0);\n' % \
1114 (op_desc.munged_name, base, size-1)
1115 else:
1116 return '%s = %s;\n' % (op_desc.munged_name, base)
1117
1118 def makeWrite(self, op_desc):
1119 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1120 final_val = op_desc.munged_name
1121 if (type == 'float'):
1122 func = 'setFloatRegSingle'
1123 elif (type == 'double'):
1124 func = 'setFloatRegDouble'
1125 else:
1126 func = 'setFloatRegInt'
1127 type = 'uint%d_t' % self.dflt_size
1128 if (size != self.dflt_size and is_signed):
1129 final_val = 'sext<%d>(%s)' % (size, op_desc.munged_name)
1130 wb = '''
1131 {
1132 %s final_val = %s;
1133 xc->%s(this, %d, final_val);\n
1134 if (traceData) { traceData->setData(final_val); }
1135 }''' % (type, final_val, func, op_desc.dest_reg_idx)
1136 return wb
1137
1138 class ControlRegOperandTraits(OperandTraits):
1139 def isReg(self):
1140 return 1
1141
1142 def isControlReg(self):
1143 return 1
1144
1145 def makeConstructor(self, op_desc):
1146 c = ''
1147 if op_desc.is_src:
1148 c += '\n\t_srcRegIdx[%d] = %s_DepTag;' % \
1149 (op_desc.src_reg_idx, self.reg_spec)
1150 if op_desc.is_dest:
1151 c += '\n\t_destRegIdx[%d] = %s_DepTag;' % \
1152 (op_desc.dest_reg_idx, self.reg_spec)
1153 return c
1154
1155 def makeRead(self, op_desc):
1156 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1157 bit_select = 0
1158 if (type == 'float' or type == 'double'):
1159 error(0, 'Attempt to read control register as FP')
1160 base = 'xc->read%s()' % self.reg_spec
1161 if size == self.dflt_size:
1162 return '%s = %s;\n' % (op_desc.munged_name, base)
1163 else:
1164 return '%s = bits(%s, %d, 0);\n' % \
1165 (op_desc.munged_name, base, size-1)
1166
1167 def makeWrite(self, op_desc):
1168 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1169 if (type == 'float' or type == 'double'):
1170 error(0, 'Attempt to write control register as FP')
1171 wb = 'xc->set%s(%s);\n' % (self.reg_spec, op_desc.munged_name)
1172 wb += 'if (traceData) { traceData->setData(%s); }' % \
1173 op_desc.munged_name
1174 return wb
1175
1176 class MemOperandTraits(OperandTraits):
1177 def isMem(self):
1178 return 1
1179
1180 def makeConstructor(self, op_desc):
1181 return ''
1182
1183 def makeDecl(self, op_desc):
1184 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1185 # Note that initializations in the declarations are solely
1186 # to avoid 'uninitialized variable' errors from the compiler.
1187 # Declare memory data variable.
1188 c = '%s %s = 0;\n' % (type, op_desc.munged_name)
1189 # Declare var to hold memory access flags.
1190 c += 'unsigned %s_flags = memAccessFlags;\n' % op_desc.base_name
1191 # If this operand is a dest (i.e., it's a store operation),
1192 # then we need to declare a variable for the write result code
1193 # as well.
1194 if op_desc.is_dest:
1195 c += 'uint64_t %s_write_result = 0;\n' % op_desc.base_name
1196 return c
1197
1198 def makeRead(self, op_desc):
1199 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1200 eff_type = 'uint%d_t' % size
1201 return 'fault = xc->read(EA, (%s&)%s, %s_flags);\n' \
1202 % (eff_type, op_desc.munged_name, op_desc.base_name)
1203
1204 def makeWrite(self, op_desc):
1205 (size, type, is_signed) = operandSizeMap[op_desc.eff_ext]
1206 eff_type = 'uint%d_t' % size
1207 return 'fault = xc->write((%s&)%s, EA, %s_flags,' \
1208 ' &%s_write_result);\n' \
1209 % (eff_type, op_desc.munged_name, op_desc.base_name,
1210 op_desc.base_name)
1211
1212 class NPCOperandTraits(OperandTraits):
1213 def makeConstructor(self, op_desc):
1214 return ''
1215
1216 def makeRead(self, op_desc):
1217 return '%s = xc->readPC() + 4;\n' % op_desc.munged_name
1218
1219 def makeWrite(self, op_desc):
1220 return 'xc->setNextPC(%s);\n' % op_desc.munged_name
1221
1222
1223 exportContextSymbols = ('IntRegOperandTraits', 'FloatRegOperandTraits',
1224 'ControlRegOperandTraits', 'MemOperandTraits',
1225 'NPCOperandTraits', 'InstObjParams', 'CodeBlock',
1226 're', 'string')
1227
1228 exportContext = {}
1229
1230 def updateExportContext():
1231 exportContext.update(exportDict(*exportContextSymbols))
1232 exportContext.update(templateMap)
1233
1234
1235 def exportDict(*symNames):
1236 return dict([(s, eval(s)) for s in symNames])
1237
1238
1239 #
1240 # Define operand variables that get derived from the basic declaration
1241 # of ISA-specific operands in operandTraitsMap. This function must be
1242 # called by the ISA description file explicitly after defining
1243 # operandTraitsMap (in a 'let' block).
1244 #
1245 def defineDerivedOperandVars():
1246 global operands
1247 operands = operandTraitsMap.keys()
1248
1249 operandsREString = (r'''
1250 (?<![\w\.]) # neg. lookbehind assertion: prevent partial matches
1251 ((%s)(?:\.(\w+))?) # match: operand with optional '.' then suffix
1252 (?![\w\.]) # neg. lookahead assertion: prevent partial matches
1253 '''
1254 % string.join(operands, '|'))
1255
1256 global operandsRE
1257 operandsRE = re.compile(operandsREString, re.MULTILINE|re.VERBOSE)
1258
1259 # Same as operandsREString, but extension is mandatory, and only two
1260 # groups are returned (base and ext, not full name as above).
1261 # Used for subtituting '_' for '.' to make C++ identifiers.
1262 operandsWithExtREString = (r'(?<![\w\.])(%s)\.(\w+)(?![\w\.])'
1263 % string.join(operands, '|'))
1264
1265 global operandsWithExtRE
1266 operandsWithExtRE = re.compile(operandsWithExtREString, re.MULTILINE)
1267
1268
1269 #
1270 # Operand descriptor class. An instance of this class represents
1271 # a specific operand for a code block.
1272 #
1273 class OperandDescriptor:
1274 def __init__(self, full_name, base_name, ext, is_src, is_dest):
1275 self.full_name = full_name
1276 self.base_name = base_name
1277 self.ext = ext
1278 self.is_src = is_src
1279 self.is_dest = is_dest
1280 self.traits = operandTraitsMap[base_name]
1281 # The 'effective extension' (eff_ext) is either the actual
1282 # extension, if one was explicitly provided, or the default.
1283 # The 'munged name' replaces the '.' between the base and
1284 # extension (if any) with a '_' to make a legal C++ variable name.
1285 if ext:
1286 self.eff_ext = ext
1287 self.munged_name = base_name + '_' + ext
1288 else:
1289 self.eff_ext = self.traits.dflt_ext
1290 self.munged_name = base_name
1291
1292 # Finalize additional fields (primarily code fields). This step
1293 # is done separately since some of these fields may depend on the
1294 # register index enumeration that hasn't been performed yet at the
1295 # time of __init__().
1296 def finalize(self):
1297 self.flags = self.traits.getFlags(self)
1298 self.constructor = self.traits.makeConstructor(self)
1299 self.op_decl = self.traits.makeDecl(self)
1300
1301 if self.is_src:
1302 self.op_rd = self.traits.makeRead(self)
1303 else:
1304 self.op_rd = ''
1305
1306 if self.is_dest:
1307 self.op_wb = self.traits.makeWrite(self)
1308 else:
1309 self.op_wb = ''
1310
1311 class OperandDescriptorList:
1312 def __init__(self):
1313 self.items = []
1314 self.bases = {}
1315
1316 def __len__(self):
1317 return len(self.items)
1318
1319 def __getitem__(self, index):
1320 return self.items[index]
1321
1322 def append(self, op_desc):
1323 self.items.append(op_desc)
1324 self.bases[op_desc.base_name] = op_desc
1325
1326 def find_base(self, base_name):
1327 # like self.bases[base_name], but returns None if not found
1328 # (rather than raising exception)
1329 return self.bases.get(base_name)
1330
1331 # internal helper function for concat[Some]Attr{Strings|Lists}
1332 def __internalConcatAttrs(self, attr_name, filter, result):
1333 for op_desc in self.items:
1334 if filter(op_desc):
1335 result += getattr(op_desc, attr_name)
1336 return result
1337
1338 # return a single string that is the concatenation of the (string)
1339 # values of the specified attribute for all operands
1340 def concatAttrStrings(self, attr_name):
1341 return self.__internalConcatAttrs(attr_name, lambda x: 1, '')
1342
1343 # like concatAttrStrings, but only include the values for the operands
1344 # for which the provided filter function returns true
1345 def concatSomeAttrStrings(self, filter, attr_name):
1346 return self.__internalConcatAttrs(attr_name, filter, '')
1347
1348 # return a single list that is the concatenation of the (list)
1349 # values of the specified attribute for all operands
1350 def concatAttrLists(self, attr_name):
1351 return self.__internalConcatAttrs(attr_name, lambda x: 1, [])
1352
1353 # like concatAttrLists, but only include the values for the operands
1354 # for which the provided filter function returns true
1355 def concatSomeAttrLists(self, filter, attr_name):
1356 return self.__internalConcatAttrs(attr_name, filter, [])
1357
1358 def sort(self):
1359 self.items.sort(lambda a, b: a.traits.sort_pri - b.traits.sort_pri)
1360
1361 # Regular expression object to match C++ comments
1362 # (used in findOperands())
1363 commentRE = re.compile(r'//.*\n')
1364
1365 # Regular expression object to match assignment statements
1366 # (used in findOperands())
1367 assignRE = re.compile(r'\s*=(?!=)', re.MULTILINE)
1368
1369 #
1370 # Find all the operands in the given code block. Returns an operand
1371 # descriptor list (instance of class OperandDescriptorList).
1372 #
1373 def findOperands(code):
1374 operands = OperandDescriptorList()
1375 # delete comments so we don't accidentally match on reg specifiers inside
1376 code = commentRE.sub('', code)
1377 # search for operands
1378 next_pos = 0
1379 while 1:
1380 match = operandsRE.search(code, next_pos)
1381 if not match:
1382 # no more matches: we're done
1383 break
1384 op = match.groups()
1385 # regexp groups are operand full name, base, and extension
1386 (op_full, op_base, op_ext) = op
1387 # if the token following the operand is an assignment, this is
1388 # a destination (LHS), else it's a source (RHS)
1389 is_dest = (assignRE.match(code, match.end()) != None)
1390 is_src = not is_dest
1391 # see if we've already seen this one
1392 op_desc = operands.find_base(op_base)
1393 if op_desc:
1394 if op_desc.ext != op_ext:
1395 error(0, 'Inconsistent extensions for operand %s' % op_base)
1396 op_desc.is_src = op_desc.is_src or is_src
1397 op_desc.is_dest = op_desc.is_dest or is_dest
1398 else:
1399 # new operand: create new descriptor
1400 op_desc = OperandDescriptor(op_full, op_base, op_ext,
1401 is_src, is_dest)
1402 operands.append(op_desc)
1403 # start next search after end of current match
1404 next_pos = match.end()
1405 operands.sort()
1406 # enumerate source & dest register operands... used in building
1407 # constructor later
1408 srcRegs = 0
1409 destRegs = 0
1410 operands.numFPDestRegs = 0
1411 operands.numIntDestRegs = 0
1412 for op_desc in operands:
1413 if op_desc.traits.isReg():
1414 if op_desc.is_src:
1415 op_desc.src_reg_idx = srcRegs
1416 srcRegs += 1
1417 if op_desc.is_dest:
1418 op_desc.dest_reg_idx = destRegs
1419 destRegs += 1
1420 if op_desc.traits.isFloatReg():
1421 operands.numFPDestRegs += 1
1422 elif op_desc.traits.isIntReg():
1423 operands.numIntDestRegs += 1
1424 operands.numSrcRegs = srcRegs
1425 operands.numDestRegs = destRegs
1426 # now make a final pass to finalize op_desc fields that may depend
1427 # on the register enumeration
1428 for op_desc in operands:
1429 op_desc.finalize()
1430 return operands
1431
1432 # Munge operand names in code string to make legal C++ variable names.
1433 # (Will match munged_name attribute of OperandDescriptor object.)
1434 def substMungedOpNames(code):
1435 return operandsWithExtRE.sub(r'\1_\2', code)
1436
1437 def joinLists(t):
1438 return map(string.join, t)
1439
1440 def makeFlagConstructor(flag_list):
1441 if len(flag_list) == 0:
1442 return ''
1443 # filter out repeated flags
1444 flag_list.sort()
1445 i = 1
1446 while i < len(flag_list):
1447 if flag_list[i] == flag_list[i-1]:
1448 del flag_list[i]
1449 else:
1450 i += 1
1451 pre = '\n\tflags['
1452 post = '] = true;'
1453 code = pre + string.join(flag_list, post + pre) + post
1454 return code
1455
1456 class CodeBlock:
1457 def __init__(self, code):
1458 self.orig_code = code
1459 self.operands = findOperands(code)
1460 self.code = substMungedOpNames(substBitOps(code))
1461 self.constructor = self.operands.concatAttrStrings('constructor')
1462 self.constructor += \
1463 '\n\t_numSrcRegs = %d;' % self.operands.numSrcRegs
1464 self.constructor += \
1465 '\n\t_numDestRegs = %d;' % self.operands.numDestRegs
1466 self.constructor += \
1467 '\n\t_numFPDestRegs = %d;' % self.operands.numFPDestRegs
1468 self.constructor += \
1469 '\n\t_numIntDestRegs = %d;' % self.operands.numIntDestRegs
1470
1471 self.op_decl = self.operands.concatAttrStrings('op_decl')
1472
1473 is_mem = lambda op: op.traits.isMem()
1474 not_mem = lambda op: not op.traits.isMem()
1475
1476 self.op_rd = self.operands.concatAttrStrings('op_rd')
1477 self.op_wb = self.operands.concatAttrStrings('op_wb')
1478 self.op_mem_rd = \
1479 self.operands.concatSomeAttrStrings(is_mem, 'op_rd')
1480 self.op_mem_wb = \
1481 self.operands.concatSomeAttrStrings(is_mem, 'op_wb')
1482 self.op_nonmem_rd = \
1483 self.operands.concatSomeAttrStrings(not_mem, 'op_rd')
1484 self.op_nonmem_wb = \
1485 self.operands.concatSomeAttrStrings(not_mem, 'op_wb')
1486
1487 self.flags = self.operands.concatAttrLists('flags')
1488
1489 # Make a basic guess on the operand class (function unit type).
1490 # These are good enough for most cases, and will be overridden
1491 # later otherwise.
1492 if 'IsStore' in self.flags:
1493 self.op_class = 'WrPort'
1494 elif 'IsLoad' in self.flags or 'IsPrefetch' in self.flags:
1495 self.op_class = 'RdPort'
1496 elif 'IsFloating' in self.flags:
1497 self.op_class = 'FloatADD'
1498 else:
1499 self.op_class = 'IntALU'
1500
1501 # Assume all instruction flags are of the form 'IsFoo'
1502 instFlagRE = re.compile(r'Is.*')
1503
1504 # OpClass constants are just a little more complicated
1505 opClassRE = re.compile(r'Int.*|Float.*|.*Port|No_OpClass')
1506
1507 class InstObjParams:
1508 def __init__(self, mnem, class_name, base_class = '',
1509 code_block = None, opt_args = []):
1510 self.mnemonic = mnem
1511 self.class_name = class_name
1512 self.base_class = base_class
1513 if code_block:
1514 for code_attr in code_block.__dict__.keys():
1515 setattr(self, code_attr, getattr(code_block, code_attr))
1516 else:
1517 self.constructor = ''
1518 self.flags = []
1519 # Optional arguments are assumed to be either StaticInst flags
1520 # or an OpClass value. To avoid having to import a complete
1521 # list of these values to match against, we do it ad-hoc
1522 # with regexps.
1523 for oa in opt_args:
1524 if instFlagRE.match(oa):
1525 self.flags.append(oa)
1526 elif opClassRE.match(oa):
1527 self.op_class = oa
1528 else:
1529 error(0, 'InstObjParams: optional arg "%s" not recognized '
1530 'as StaticInst::Flag or OpClass.' % oa)
1531
1532 # add flag initialization to contructor here to include
1533 # any flags added via opt_args
1534 self.constructor += makeFlagConstructor(self.flags)
1535
1536 # if 'IsFloating' is set, add call to the FP enable check
1537 # function (which should be provided by isa_desc via a declare)
1538 if 'IsFloating' in self.flags:
1539 self.fp_enable_check = 'fault = checkFpEnableFault(xc);'
1540 else:
1541 self.fp_enable_check = ''
1542
1543 #######################
1544 #
1545 # Output file template
1546 #
1547
1548 file_template = '''
1549 /*
1550 * Copyright (c) 2003
1551 * The Regents of The University of Michigan
1552 * All Rights Reserved
1553 *
1554 * This code is part of the M5 simulator, developed by Nathan Binkert,
1555 * Erik Hallnor, Steve Raasch, and Steve Reinhardt, with contributions
1556 * from Ron Dreslinski, Dave Greene, and Lisa Hsu.
1557 *
1558 * Permission is granted to use, copy, create derivative works and
1559 * redistribute this software and such derivative works for any
1560 * purpose, so long as the copyright notice above, this grant of
1561 * permission, and the disclaimer below appear in all copies made; and
1562 * so long as the name of The University of Michigan is not used in
1563 * any advertising or publicity pertaining to the use or distribution
1564 * of this software without specific, written prior authorization.
1565 *
1566 * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
1567 * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND
1568 * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER
1569 * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
1570 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1571 * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE
1572 * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT,
1573 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM
1574 * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
1575 * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH
1576 * DAMAGES.
1577 */
1578
1579 /*
1580 * DO NOT EDIT THIS FILE!!!
1581 *
1582 * It was automatically generated from the ISA description in %(filename)s
1583 */
1584
1585 %(includes)s
1586
1587 %(global_output)s
1588
1589 namespace %(namespace)s {
1590
1591 %(namespace_output)s
1592
1593 } // namespace %(namespace)s
1594 '''
1595
1596
1597 # Update the output file only if the new contents are different from
1598 # the current contents. Minimizes the files that need to be rebuilt
1599 # after minor changes.
1600 def update_if_needed(file, contents):
1601 update = False
1602 if os.access(file, os.R_OK):
1603 f = open(file, 'r')
1604 old_contents = f.read()
1605 f.close()
1606 if contents != old_contents:
1607 print 'Updating', file
1608 os.remove(file) # in case it's write-protected
1609 update = True
1610 else:
1611 print 'File', file, 'is unchanged'
1612 else:
1613 print 'Generating', file
1614 update = True
1615 if update:
1616 f = open(file, 'w')
1617 f.write(contents)
1618 f.close()
1619
1620 #
1621 # Read in and parse the ISA description.
1622 #
1623 def parse_isa_desc(isa_desc_file, output_dir, include_path):
1624 # set a global var for the input filename... used in error messages
1625 global input_filename
1626 input_filename = isa_desc_file
1627
1628 # Suck the ISA description file in.
1629 input = open(isa_desc_file)
1630 isa_desc = input.read()
1631 input.close()
1632
1633 # Parse it.
1634 (isa_name, namespace, global_code, namespace_code) = yacc.parse(isa_desc)
1635
1636 # grab the last three path components of isa_desc_file to put in
1637 # the output
1638 filename = '/'.join(isa_desc_file.split('/')[-3:])
1639
1640 # generate decoder.hh
1641 includes = '#include "base/bitfield.hh" // for bitfield support'
1642 global_output = global_code.header_output
1643 namespace_output = namespace_code.header_output
1644 update_if_needed(output_dir + '/decoder.hh', file_template % vars())
1645
1646 # generate decoder.cc
1647 includes = '#include "%s/decoder.hh"' % include_path
1648 global_output = global_code.decoder_output
1649 namespace_output = namespace_code.decoder_output
1650 namespace_output += namespace_code.decode_block
1651 update_if_needed(output_dir + '/decoder.cc', file_template % vars())
1652
1653 # generate per-cpu exec files
1654 for cpu in CpuModel.list:
1655 includes = '#include "%s/decoder.hh"\n' % include_path
1656 includes += cpu.includes
1657 global_output = global_code.exec_output[cpu.name]
1658 namespace_output = namespace_code.exec_output[cpu.name]
1659 update_if_needed(output_dir + '/' + cpu.filename,
1660 file_template % vars())
1661
1662 # Called as script: get args from command line.
1663 if __name__ == '__main__':
1664 parse_isa_desc(sys.argv[1], sys.argv[2], sys.argv[3])