Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: src/trusted/validator_mips/dgen/dgen_output.py

Issue 9979025: [MIPS] Adding validator for MIPS architecture. (Closed) Base URL: http://src.chromium.org/native_client/trunk/src/native_client/
Patch Set: Rebased patch, conflict resolved. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/python
2 #
3 # Copyright 2012 The Native Client Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can
5 # be found in the LICENSE file.
6 # Copyright 2012, Google Inc.
7 #
8
9 """
10 Responsible for generating the decoder based on parsed table representations.
11 """
12
13 import dgen_opt
14
15 def generate_decoder(tables, out):
16 """Entry point to the decoder.
17
18 Args:
19 tables: list of Table objects to process.
20 out: a COutput object to write to.
21 """
22 if len(tables) == 0: raise Exception('No tables provided.')
23
24 _generate_header(out)
25 out.line()
26 out.line('namespace nacl_mips_dec {')
27 out.line()
28 _generate_decoder_state_type(tables, out)
29 out.line()
30 _generate_prototypes(tables, out)
31 out.line()
32 _generate_implementations(tables, out)
33 out.line()
34 _generate_init_function(out)
35 out.line()
36 _generate_entry_point(tables[0].name, out)
37 out.line()
38 out.line('} // namespace')
39
40 def _generate_header(out):
41 # TODO do we need a big ridiculous license banner in generated code?
42 out.block_comment('DO NOT EDIT: GENERATED CODE')
43 out.line('#include <stdio.h>')
44 out.line('#include "native_client/src/trusted/validator_mips/decode.h"')
45
46
47 def _generate_decoder_state_type(tables, out):
48 out.block_comment(
49 'This beast holds a bunch of pre-created ClassDecoder instances, which',
50 'we create in init_decode(). Because ClassDecoders are stateless, we',
51 'can freely reuse them -- even across threads -- and avoid allocating',
52 'in the inner decoder loop.'
53 )
54 terminals = set()
55 for t in tables:
56 for r in t.rows:
57 if r.action.startswith('='):
58 terminals.add(r.action[1:])
59
60 out.enter_block('struct DecoderState')
61
62 for t in terminals:
63 out.line('const %s _%s_instance;' % (t, t))
64
65 out.line('DecoderState() :')
66 first = True
67 for t in terminals:
68 if first:
69 out.line('_%s_instance()' % t)
70 else:
71 out.line(',_%s_instance()' % t)
72 first = False
73 out.line('{}')
74
75 out.exit_block(';')
76
77
78 def _generate_prototypes(tables, out):
79 out.block_comment('Prototypes for static table-matching functions.')
80 for t in tables:
81 out.line('static inline const ClassDecoder &decode_%s('
82 'const Instruction insn, const DecoderState *state);' % t.name)
83
84 def _generate_implementations(tables, out):
85 out.block_comment('Table-matching function implementations.')
86 for t in tables:
87 out.line()
88 _generate_table(t, out)
89
90
91 def _generate_init_function(out):
92 out.enter_block('const DecoderState *init_decode()')
93 out.line('return new DecoderState;')
94 out.exit_block()
95
96 out.enter_block('void delete_state(const DecoderState *state)')
97 out.line('delete (DecoderState *)state;')
98 out.exit_block()
99
100 def _generate_entry_point(initial_table_name, out):
101 out.enter_block('const ClassDecoder &decode(const Instruction insn, '
102 'const DecoderState *state)')
103 out.line('return decode_%s(insn, (DecoderState *)state);'
104 % initial_table_name)
105 out.exit_block()
106
107
108 def _generate_table(table, out):
109 """Generates the implementation of a single table."""
110 out.block_comment(
111 'Implementation of table %s.' % table.name,
112 'Specified by: %s.' % table.citation
113 )
114 out.enter_block('static inline const ClassDecoder &decode_%s('
115 'const Instruction insn, const DecoderState *state)' % table.name)
116
117 optimized = dgen_opt.optimize_rows(table.rows)
118 print ("Table %s: %d rows minimized to %d"
119 % (table.name, len(table.rows), len(optimized)))
120 for row in sorted(optimized):
121 exprs = ["(%s)" % p.to_c_expr('insn') for p in row.patterns]
122 out.enter_block('if (%s)' % ' && '.join(exprs))
123
124 if row.action.startswith('='):
125 _generate_terminal(row.action[1:], out)
126 elif row.action.startswith('->'):
127 _generate_table_change(row.action[2:], out)
128 else:
129 raise Exception('Bad table action: %s' % row.action)
130
131 out.exit_block()
132 out.line()
133
134 _generate_safety_net(table, out)
135 out.exit_block()
136
137
138 def _generate_terminal(name, out):
139 out.line('return state->_%s_instance;' % name)
140
141
142 def _generate_table_change(name, out):
143 out.line('return decode_%s(insn, state);' % name)
144
145
146 def _generate_safety_net(table, out):
147 out.line('// Catch any attempt to fall through...')
148 out.line('fprintf(stderr, "TABLE IS INCOMPLETE: %s could not parse %%08X",'
149 'insn.Bits(31,0));' % table.name)
150 _generate_terminal('Forbidden', out)
151
152
153 class COutput(object):
154 """Provides nicely-formatted C++ output."""
155
156 def __init__(self, out):
157 self._out = out
158 self._indent = 0
159
160 def line(self, str = ''):
161 self._out.write(self._tabs())
162 self._out.write(str + '\n')
163
164 def enter_block(self, headline):
165 self.line(headline + ' {')
166 self._indent += 1
167
168 def exit_block(self, footer = ''):
169 self._indent -= 1
170 self.line('}' + footer)
171
172 def block_comment(self, *lines):
173 self.line('/*')
174 for s in lines:
175 self.line(' * ' + s)
176 self.line(' */')
177
178 def _tabs(self):
179 return ' ' * self._indent
180
181
182 def each_index_pair(sequence):
183 """Utility method: Generates each unique index pair in sequence."""
184 for i in range(0, len(sequence)):
185 for j in range(i + 1, len(sequence)):
186 yield (i, j)
187
188
OLDNEW
« no previous file with comments | « src/trusted/validator_mips/dgen/dgen_opt.py ('k') | src/trusted/validator_mips/dgen/generate_decoder.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698