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 be
5# found in the LICENSE file.
6# Copyright 2012, Google Inc.
7#
8
9"""
10Produces a table from the in-memory representation.  Useful for storing the
11optimized table for later use.
12"""
13
14from __future__ import print_function
15
16import dgen_opt
17
18def dump_tables(tables, out):
19  """Dumps the given tables into a text file.
20
21    Args:
22        tables: list of Table objects to process.
23        out: an output stream.
24    """
25  if len(tables) == 0:
26    raise Exception('No tables provided.')
27
28  _generate_header(out)
29  for t in tables:
30    _generate_table(t, out)
31
32
33def _generate_header(out):
34  # TODO do we need a big ridiculous license banner in generated code?
35  out.write('# DO NOT EDIT: GENERATED CODE\n')
36
37
38def _generate_table(t, out):
39  rows = dgen_opt.optimize_rows(t.rows)
40  print('Table %s: %d rows minimized to %d.' % (t.name, len(t.rows), len(rows)))
41
42  out.write('\n')
43  out.write('-- %s (%s)\n' % (t.name, t.citation))
44  num_cols = len(rows[0].patterns)
45  headers = ['pat%- 31s' % (str(n) + '(31:0)') for n in range(0, num_cols)]
46  out.write(''.join(headers))
47  out.write('\n')
48  for row in rows:
49    out.write(''.join(['%- 34s' % p for p in row.patterns]))
50    out.write(row.action)
51    if row.arch:
52      out.write('(%s)' % row.arch)
53    out.write('\n')
54