#!/usr/bin/env python

#
# Copyright 2012 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.
#

#
# Emits a C++ file to be compiled and linked into libv8 to support postmortem
# debugging tools.  Most importantly, this tool emits constants describing V8
# internals:
#
#    v8dbg_type_CLASS__TYPE = VALUE             Describes class type values
#    v8dbg_class_CLASS__FIELD__TYPE = OFFSET    Describes class fields
#    v8dbg_parent_CLASS__PARENT                 Describes class hierarchy
#    v8dbg_frametype_NAME = VALUE               Describes stack frame values
#    v8dbg_off_fp_NAME = OFFSET                 Frame pointer offsets
#    v8dbg_prop_NAME = OFFSET                   Object property offsets
#    v8dbg_NAME = VALUE                         Miscellaneous values
#
# These constants are declared as global integers so that they'll be present in
# the generated libv8 binary.
#

import re
import sys

#
# Miscellaneous constants such as tags and masks used for object identification,
# enumeration values used as indexes in internal tables, etc..
#
consts_misc = [
    { 'name': 'FirstNonstringType',     'value': 'FIRST_NONSTRING_TYPE' },
    { 'name': 'APIObjectType',          'value': 'JS_API_OBJECT_TYPE' },
    { 'name': 'SpecialAPIObjectType',   'value': 'JS_SPECIAL_API_OBJECT_TYPE' },

    { 'name': 'IsNotStringMask',        'value': 'kIsNotStringMask' },
    { 'name': 'StringTag',              'value': 'kStringTag' },
    { 'name': 'NotStringTag',           'value': 'kNotStringTag' },

    { 'name': 'StringEncodingMask',     'value': 'kStringEncodingMask' },
    { 'name': 'TwoByteStringTag',       'value': 'kTwoByteStringTag' },
    { 'name': 'OneByteStringTag',       'value': 'kOneByteStringTag' },

    { 'name': 'StringRepresentationMask',
        'value': 'kStringRepresentationMask' },
    { 'name': 'SeqStringTag',           'value': 'kSeqStringTag' },
    { 'name': 'ConsStringTag',          'value': 'kConsStringTag' },
    { 'name': 'ExternalStringTag',      'value': 'kExternalStringTag' },
    { 'name': 'SlicedStringTag',        'value': 'kSlicedStringTag' },

    { 'name': 'HeapObjectTag',          'value': 'kHeapObjectTag' },
    { 'name': 'HeapObjectTagMask',      'value': 'kHeapObjectTagMask' },
    { 'name': 'SmiTag',                 'value': 'kSmiTag' },
    { 'name': 'SmiTagMask',             'value': 'kSmiTagMask' },
    { 'name': 'SmiValueShift',          'value': 'kSmiTagSize' },
    { 'name': 'SmiShiftSize',           'value': 'kSmiShiftSize' },
    { 'name': 'PointerSizeLog2',        'value': 'kPointerSizeLog2' },

    { 'name': 'OddballFalse',           'value': 'Oddball::kFalse' },
    { 'name': 'OddballTrue',            'value': 'Oddball::kTrue' },
    { 'name': 'OddballTheHole',         'value': 'Oddball::kTheHole' },
    { 'name': 'OddballNull',            'value': 'Oddball::kNull' },
    { 'name': 'OddballArgumentsMarker', 'value': 'Oddball::kArgumentsMarker' },
    { 'name': 'OddballUndefined',       'value': 'Oddball::kUndefined' },
    { 'name': 'OddballUninitialized',   'value': 'Oddball::kUninitialized' },
    { 'name': 'OddballOther',           'value': 'Oddball::kOther' },
    { 'name': 'OddballException',       'value': 'Oddball::kException' },

    { 'name': 'prop_idx_first',
        'value': 'DescriptorArray::kFirstIndex' },
    { 'name': 'prop_type_field',
        'value': 'DATA' },
    { 'name': 'prop_type_const_field',
        'value': 'DATA_CONSTANT' },
    { 'name': 'prop_type_mask',
        'value': 'PropertyDetails::TypeField::kMask' },
    { 'name': 'prop_index_mask',
        'value': 'PropertyDetails::FieldIndexField::kMask' },
    { 'name': 'prop_index_shift',
        'value': 'PropertyDetails::FieldIndexField::kShift' },
    { 'name': 'prop_representation_mask',
        'value': 'PropertyDetails::RepresentationField::kMask' },
    { 'name': 'prop_representation_shift',
        'value': 'PropertyDetails::RepresentationField::kShift' },
    { 'name': 'prop_representation_integer8',
        'value': 'Representation::Kind::kInteger8' },
    { 'name': 'prop_representation_uinteger8',
        'value': 'Representation::Kind::kUInteger8' },
    { 'name': 'prop_representation_integer16',
        'value': 'Representation::Kind::kInteger16' },
    { 'name': 'prop_representation_uinteger16',
        'value': 'Representation::Kind::kUInteger16' },
    { 'name': 'prop_representation_smi',
        'value': 'Representation::Kind::kSmi' },
    { 'name': 'prop_representation_integer32',
        'value': 'Representation::Kind::kInteger32' },
    { 'name': 'prop_representation_double',
        'value': 'Representation::Kind::kDouble' },
    { 'name': 'prop_representation_heapobject',
        'value': 'Representation::Kind::kHeapObject' },
    { 'name': 'prop_representation_tagged',
        'value': 'Representation::Kind::kTagged' },
    { 'name': 'prop_representation_external',
        'value': 'Representation::Kind::kExternal' },

    { 'name': 'prop_desc_key',
        'value': 'DescriptorArray::kDescriptorKey' },
    { 'name': 'prop_desc_details',
        'value': 'DescriptorArray::kDescriptorDetails' },
    { 'name': 'prop_desc_value',
        'value': 'DescriptorArray::kDescriptorValue' },
    { 'name': 'prop_desc_size',
        'value': 'DescriptorArray::kDescriptorSize' },

    { 'name': 'elements_fast_holey_elements',
        'value': 'FAST_HOLEY_ELEMENTS' },
    { 'name': 'elements_fast_elements',
        'value': 'FAST_ELEMENTS' },
    { 'name': 'elements_dictionary_elements',
        'value': 'DICTIONARY_ELEMENTS' },

    { 'name': 'bit_field2_elements_kind_mask',
        'value': 'Map::ElementsKindBits::kMask' },
    { 'name': 'bit_field2_elements_kind_shift',
        'value': 'Map::ElementsKindBits::kShift' },
    { 'name': 'bit_field3_dictionary_map_shift',
        'value': 'Map::DictionaryMap::kShift' },
    { 'name': 'bit_field3_number_of_own_descriptors_mask',
        'value': 'Map::NumberOfOwnDescriptorsBits::kMask' },
    { 'name': 'bit_field3_number_of_own_descriptors_shift',
        'value': 'Map::NumberOfOwnDescriptorsBits::kShift' },

    { 'name': 'off_fp_context',
        'value': 'StandardFrameConstants::kContextOffset' },
    { 'name': 'off_fp_constant_pool',
        'value': 'StandardFrameConstants::kConstantPoolOffset' },
    { 'name': 'off_fp_function',
        'value': 'JavaScriptFrameConstants::kFunctionOffset' },
    { 'name': 'off_fp_args',
        'value': 'JavaScriptFrameConstants::kLastParameterOffset' },

    { 'name': 'scopeinfo_idx_nparams',
        'value': 'ScopeInfo::kParameterCount' },
    { 'name': 'scopeinfo_idx_nstacklocals',
        'value': 'ScopeInfo::kStackLocalCount' },
    { 'name': 'scopeinfo_idx_ncontextlocals',
        'value': 'ScopeInfo::kContextLocalCount' },
    { 'name': 'scopeinfo_idx_first_vars',
        'value': 'ScopeInfo::kVariablePartIndex' },

    { 'name': 'sharedfunctioninfo_start_position_mask',
        'value': 'SharedFunctionInfo::kStartPositionMask' },
    { 'name': 'sharedfunctioninfo_start_position_shift',
        'value': 'SharedFunctionInfo::kStartPositionShift' },

    { 'name': 'jsarray_buffer_was_neutered_mask',
        'value': 'JSArrayBuffer::WasNeutered::kMask' },
    { 'name': 'jsarray_buffer_was_neutered_shift',
        'value': 'JSArrayBuffer::WasNeutered::kShift' },

    { 'name': 'context_idx_closure',
        'value': 'Context::CLOSURE_INDEX' },
    { 'name': 'context_idx_native',
        'value': 'Context::NATIVE_CONTEXT_INDEX' },
    { 'name': 'context_idx_prev',
        'value': 'Context::PREVIOUS_INDEX' },
    { 'name': 'context_idx_ext',
        'value': 'Context::EXTENSION_INDEX' },
    { 'name': 'context_min_slots',
        'value': 'Context::MIN_CONTEXT_SLOTS' },

    { 'name': 'namedictionaryshape_prefix_size',
        'value': 'NameDictionaryShape::kPrefixSize' },
    { 'name': 'namedictionaryshape_entry_size',
        'value': 'NameDictionaryShape::kEntrySize' },
    { 'name': 'globaldictionaryshape_entry_size',
        'value': 'GlobalDictionaryShape::kEntrySize' },

    { 'name': 'namedictionary_prefix_start_index',
        'value': 'NameDictionary::kPrefixStartIndex' },

    { 'name': 'seedednumberdictionaryshape_prefix_size',
        'value': 'SeededNumberDictionaryShape::kPrefixSize' },
    { 'name': 'seedednumberdictionaryshape_entry_size',
        'value': 'SeededNumberDictionaryShape::kEntrySize' },

    { 'name': 'unseedednumberdictionaryshape_prefix_size',
        'value': 'UnseededNumberDictionaryShape::kPrefixSize' },
    { 'name': 'unseedednumberdictionaryshape_entry_size',
        'value': 'UnseededNumberDictionaryShape::kEntrySize' }
];

#
# The following useful fields are missing accessors, so we define fake ones.
# Please note that extra accessors should _only_ be added to expose offsets that
# can be used to access actual V8 objects' properties. They should not be added
# for exposing other values. For instance, enumeration values or class'
# constants should be exposed by adding an entry in the "consts_misc" table, not
# in this "extras_accessors" table.
#
extras_accessors = [
    'JSFunction, context, Context, kContextOffset',
    'HeapObject, map, Map, kMapOffset',
    'JSObject, elements, Object, kElementsOffset',
    'JSObject, internal_fields, uintptr_t, kHeaderSize',
    'FixedArray, data, uintptr_t, kHeaderSize',
    'JSArrayBuffer, backing_store, Object, kBackingStoreOffset',
    'JSArrayBufferView, byte_offset, Object, kByteOffsetOffset',
    'JSTypedArray, length, Object, kLengthOffset',
    'Map, instance_attributes, int, kInstanceAttributesOffset',
    'Map, inobject_properties_or_constructor_function_index, int, kInObjectPropertiesOrConstructorFunctionIndexOffset',
    'Map, instance_size, int, kInstanceSizeOffset',
    'Map, bit_field, char, kBitFieldOffset',
    'Map, bit_field2, char, kBitField2Offset',
    'Map, bit_field3, int, kBitField3Offset',
    'Map, prototype, Object, kPrototypeOffset',
    'Oddball, kind_offset, int, kKindOffset',
    'HeapNumber, value, double, kValueOffset',
    'ConsString, first, String, kFirstOffset',
    'ConsString, second, String, kSecondOffset',