| Index: tools/objects-generator.py
|
| diff --git a/tools/objects-generator.py b/tools/objects-generator.py
|
| new file mode 100755
|
| index 0000000000000000000000000000000000000000..07dfca33ed527f20a9b1420050ebd601eb007b21
|
| --- /dev/null
|
| +++ b/tools/objects-generator.py
|
| @@ -0,0 +1,137 @@
|
| +#!/usr/bin/env python
|
| +# Copyright 2013 the V8 project authors. All rights reserved.
|
| +# Redistribution and use in source and binary forms, with or without
|
| +# modification, are permitted provided that the following conditions are
|
| +# met:
|
| +#
|
| +# * Redistributions of source code must retain the above copyright
|
| +# notice, this list of conditions and the following disclaimer.
|
| +# * Redistributions in binary form must reproduce the above
|
| +# copyright notice, this list of conditions and the following
|
| +# disclaimer in the documentation and/or other materials provided
|
| +# with the distribution.
|
| +# * Neither the name of Google Inc. nor the names of its
|
| +# contributors may be used to endorse or promote products derived
|
| +# from this software without specific prior written permission.
|
| +#
|
| +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| +
|
| +import codecs
|
| +import optparse
|
| +import pystache
|
| +import re
|
| +import sys
|
| +
|
| +# List of all available keywords.
|
| +BASE = "base"
|
| +BITFIELD = "bitfield"
|
| +COMMENT = "comment"
|
| +FIELDS = "fields"
|
| +NAME = "name"
|
| +TYPE = "type"
|
| +TYPES = "types"
|
| +KEYWORDS = {}
|
| +for key in [BASE, BITFIELD, COMMENT, FIELDS, NAME, TYPE, TYPES]:
|
| + KEYWORDS[key] = key
|
| +
|
| +# List of all available primitive types.
|
| +T_BOOL = "tBool"
|
| +T_SMI = "tSmi"
|
| +for key in [T_BOOL, T_SMI]:
|
| + KEYWORDS[key] = key
|
| +
|
| +# Converts "camel_case" to "CamelCase".
|
| +def ConvertLowerToPascal(name):
|
| + return ''.join(x.capitalize() or '_' for x in name.split('_'))
|
| +
|
| +# Converts "CamelCase" to "CAMEL_CASE".
|
| +def ConvertPascalToUpper(name):
|
| + return '_'.join(x.upper() for x in re.findall('[A-Z][a-z]*', name))
|
| +
|
| +def ParseLayout(layout):
|
| + dictionary = eval(layout, KEYWORDS)
|
| + for t in dictionary[TYPES]:
|
| + t[COMMENT] = t.get(COMMENT, '%s is a %s.' % (t[NAME], t[BASE]))
|
| + t['type_name_upper'] = ConvertPascalToUpper(t[NAME])
|
| + for f in t[FIELDS]:
|
| + f[TYPE] = f.get(TYPE, 'Object')
|
| + f['field_name_lower'] = f[NAME]
|
| + f['field_name_pascal'] = ConvertLowerToPascal(f[NAME])
|
| + f['field_comment'] = f.get(COMMENT)
|
| +
|
| + # Pre-render multi-line comments.
|
| + comment = t[COMMENT]
|
| + if type(comment) == list:
|
| + comment = '\n'.join(map(lambda l : '// ' + l, comment))
|
| + else:
|
| + comment = '// %s' % comment
|
| + t[COMMENT] = comment
|
| +
|
| + # Determine primitive fileds.
|
| + for f in t[FIELDS]:
|
| + if f[TYPE] == T_SMI:
|
| + f['is_primitive'] = True
|
| + f['is_smi'] = True
|
| +
|
| + # Determine bitfields.
|
| + bitfields = []
|
| + for f in t[FIELDS]:
|
| + if not f.has_key(BITFIELD): continue
|
| + bitfield_index = 0
|
| + for bf in f[BITFIELD]:
|
| + bf['bitfield_name_pascal'] = ConvertLowerToPascal(bf[NAME])
|
| + bf['bitfield_start'] = bitfield_index
|
| + bitfield_index += 1
|
| + bitfields.append(f)
|
| + if bitfields: t['bitfields'] = bitfields
|
| +
|
| + # Compute field offsets.
|
| + prev_f = None
|
| + for f in t[FIELDS]:
|
| + if prev_f == None:
|
| + field_offset = '%s::kHeaderSize' % t[BASE]
|
| + else:
|
| + field_offset = 'k%sOffset + %s' % (prev_f['field_name_pascal'], prev_f['field_size'])
|
| + f['field_size'] = 'kPointerSize'
|
| + f['field_offset'] = field_offset
|
| + prev_f = f;
|
| +
|
| + # Compute type sizes.
|
| + t['type_size'] = 'k%sOffset + %s' % (prev_f['field_name_pascal'], prev_f['field_size'])
|
| +
|
| + return dictionary
|
| +
|
| +def RenderOutput(template, dictionary):
|
| + renderer = pystache.Renderer()
|
| + parsed = pystache.parse(template)
|
| + output = renderer.render(parsed, dictionary)
|
| + return output
|
| +
|
| +def Main():
|
| + usage = 'Usage: %prog [options] template layout-file'
|
| + parser = optparse.OptionParser(usage)
|
| + (options, args) = parser.parse_args()
|
| + if len(args) != 2:
|
| + parser.print_help()
|
| + return 1
|
| +
|
| + with codecs.open(args[0], encoding='utf-8') as f:
|
| + template = f.read()
|
| + with codecs.open(args[1], encoding='utf-8') as f:
|
| + layout = f.read()
|
| + dictionary = ParseLayout(layout)
|
| + output = RenderOutput(template, dictionary)
|
| + sys.stdout.write(output)
|
| +
|
| +if __name__ == "__main__":
|
| + sys.exit(Main())
|
|
|