gen-postmortem-metadata.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2012 the V8 project authors. All rights reserved.
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following
  12. # disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived
  16. # from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #
  30. #
  31. # Emits a C++ file to be compiled and linked into libv8 to support postmortem
  32. # debugging tools. Most importantly, this tool emits constants describing V8
  33. # internals:
  34. #
  35. # v8dbg_type_CLASS__TYPE = VALUE Describes class type values
  36. # v8dbg_class_CLASS__FIELD__TYPE = OFFSET Describes class fields
  37. # v8dbg_parent_CLASS__PARENT Describes class hierarchy
  38. # v8dbg_frametype_NAME = VALUE Describes stack frame values
  39. # v8dbg_off_fp_NAME = OFFSET Frame pointer offsets
  40. # v8dbg_prop_NAME = OFFSET Object property offsets
  41. # v8dbg_NAME = VALUE Miscellaneous values
  42. #
  43. # These constants are declared as global integers so that they'll be present in
  44. # the generated libv8 binary.
  45. #
  46. # for py2/py3 compatibility
  47. from __future__ import print_function
  48. import io
  49. import re
  50. import sys
  51. #
  52. # Miscellaneous constants such as tags and masks used for object identification,
  53. # enumeration values used as indexes in internal tables, etc..
  54. #
  55. consts_misc = [
  56. { 'name': 'FirstNonstringType', 'value': 'FIRST_NONSTRING_TYPE' },
  57. { 'name': 'APIObjectType', 'value': 'JS_API_OBJECT_TYPE' },
  58. { 'name': 'SpecialAPIObjectType', 'value': 'JS_SPECIAL_API_OBJECT_TYPE' },
  59. { 'name': 'FirstContextType', 'value': 'FIRST_CONTEXT_TYPE' },
  60. { 'name': 'LastContextType', 'value': 'LAST_CONTEXT_TYPE' },
  61. { 'name': 'IsNotStringMask', 'value': 'kIsNotStringMask' },
  62. { 'name': 'StringTag', 'value': 'kStringTag' },
  63. { 'name': 'StringEncodingMask', 'value': 'kStringEncodingMask' },
  64. { 'name': 'TwoByteStringTag', 'value': 'kTwoByteStringTag' },
  65. { 'name': 'OneByteStringTag', 'value': 'kOneByteStringTag' },
  66. { 'name': 'StringRepresentationMask',
  67. 'value': 'kStringRepresentationMask' },
  68. { 'name': 'SeqStringTag', 'value': 'kSeqStringTag' },
  69. { 'name': 'ConsStringTag', 'value': 'kConsStringTag' },
  70. { 'name': 'ExternalStringTag', 'value': 'kExternalStringTag' },
  71. { 'name': 'SlicedStringTag', 'value': 'kSlicedStringTag' },
  72. { 'name': 'ThinStringTag', 'value': 'kThinStringTag' },
  73. { 'name': 'HeapObjectTag', 'value': 'kHeapObjectTag' },
  74. { 'name': 'HeapObjectTagMask', 'value': 'kHeapObjectTagMask' },
  75. { 'name': 'SmiTag', 'value': 'kSmiTag' },
  76. { 'name': 'SmiTagMask', 'value': 'kSmiTagMask' },
  77. { 'name': 'SmiValueShift', 'value': 'kSmiTagSize' },
  78. { 'name': 'SmiShiftSize', 'value': 'kSmiShiftSize' },
  79. { 'name': 'SystemPointerSize', 'value': 'kSystemPointerSize' },
  80. { 'name': 'SystemPointerSizeLog2', 'value': 'kSystemPointerSizeLog2' },
  81. { 'name': 'TaggedSize', 'value': 'kTaggedSize' },
  82. { 'name': 'TaggedSizeLog2', 'value': 'kTaggedSizeLog2' },
  83. { 'name': 'CodeKindFieldMask', 'value': 'Code::KindField::kMask' },
  84. { 'name': 'CodeKindFieldShift', 'value': 'Code::KindField::kShift' },
  85. { 'name': 'CodeKindBytecodeHandler',
  86. 'value': 'static_cast<int>(CodeKind::BYTECODE_HANDLER)' },
  87. { 'name': 'CodeKindInterpretedFunction',
  88. 'value': 'static_cast<int>(CodeKind::INTERPRETED_FUNCTION)' },
  89. { 'name': 'CodeKindBaseline',
  90. 'value': 'static_cast<int>(CodeKind::BASELINE)' },
  91. { 'name': 'OddballFalse', 'value': 'Oddball::kFalse' },
  92. { 'name': 'OddballTrue', 'value': 'Oddball::kTrue' },
  93. { 'name': 'OddballTheHole', 'value': 'Oddball::kTheHole' },
  94. { 'name': 'OddballNull', 'value': 'Oddball::kNull' },
  95. { 'name': 'OddballArgumentsMarker', 'value': 'Oddball::kArgumentsMarker' },
  96. { 'name': 'OddballUndefined', 'value': 'Oddball::kUndefined' },
  97. { 'name': 'OddballUninitialized', 'value': 'Oddball::kUninitialized' },
  98. { 'name': 'OddballOther', 'value': 'Oddball::kOther' },
  99. { 'name': 'OddballException', 'value': 'Oddball::kException' },
  100. { 'name': 'ContextRegister', 'value': 'kContextRegister.code()' },
  101. { 'name': 'ReturnRegister0', 'value': 'kReturnRegister0.code()' },
  102. { 'name': 'JSFunctionRegister', 'value': 'kJSFunctionRegister.code()' },
  103. { 'name': 'InterpreterBytecodeOffsetRegister',
  104. 'value': 'kInterpreterBytecodeOffsetRegister.code()' },
  105. { 'name': 'InterpreterBytecodeArrayRegister',
  106. 'value': 'kInterpreterBytecodeArrayRegister.code()' },
  107. { 'name': 'RuntimeCallFunctionRegister',
  108. 'value': 'kRuntimeCallFunctionRegister.code()' },
  109. { 'name': 'prop_kind_Data',
  110. 'value': 'static_cast<int>(PropertyKind::kData)' },
  111. { 'name': 'prop_kind_Accessor',
  112. 'value': 'static_cast<int>(PropertyKind::kAccessor)' },
  113. { 'name': 'prop_kind_mask',
  114. 'value': 'PropertyDetails::KindField::kMask' },
  115. { 'name': 'prop_location_Descriptor',
  116. 'value': 'static_cast<int>(PropertyLocation::kDescriptor)' },
  117. { 'name': 'prop_location_Field',
  118. 'value': 'static_cast<int>(PropertyLocation::kField)' },
  119. { 'name': 'prop_location_mask',
  120. 'value': 'PropertyDetails::LocationField::kMask' },
  121. { 'name': 'prop_location_shift',
  122. 'value': 'PropertyDetails::LocationField::kShift' },
  123. { 'name': 'prop_attributes_NONE', 'value': 'NONE' },
  124. { 'name': 'prop_attributes_READ_ONLY', 'value': 'READ_ONLY' },
  125. { 'name': 'prop_attributes_DONT_ENUM', 'value': 'DONT_ENUM' },
  126. { 'name': 'prop_attributes_DONT_DELETE', 'value': 'DONT_DELETE' },
  127. { 'name': 'prop_attributes_mask',
  128. 'value': 'PropertyDetails::AttributesField::kMask' },
  129. { 'name': 'prop_attributes_shift',
  130. 'value': 'PropertyDetails::AttributesField::kShift' },
  131. { 'name': 'prop_index_mask',
  132. 'value': 'PropertyDetails::FieldIndexField::kMask' },
  133. { 'name': 'prop_index_shift',
  134. 'value': 'PropertyDetails::FieldIndexField::kShift' },
  135. { 'name': 'prop_representation_mask',
  136. 'value': 'PropertyDetails::RepresentationField::kMask' },
  137. { 'name': 'prop_representation_shift',
  138. 'value': 'PropertyDetails::RepresentationField::kShift' },
  139. { 'name': 'prop_representation_smi',
  140. 'value': 'Representation::Kind::kSmi' },
  141. { 'name': 'prop_representation_double',
  142. 'value': 'Representation::Kind::kDouble' },
  143. { 'name': 'prop_representation_heapobject',
  144. 'value': 'Representation::Kind::kHeapObject' },
  145. { 'name': 'prop_representation_tagged',
  146. 'value': 'Representation::Kind::kTagged' },
  147. { 'name': 'prop_desc_key',
  148. 'value': 'DescriptorArray::kEntryKeyIndex' },
  149. { 'name': 'prop_desc_details',
  150. 'value': 'DescriptorArray::kEntryDetailsIndex' },
  151. { 'name': 'prop_desc_value',
  152. 'value': 'DescriptorArray::kEntryValueIndex' },
  153. { 'name': 'prop_desc_size',
  154. 'value': 'DescriptorArray::kEntrySize' },
  155. { 'name': 'elements_fast_holey_elements',
  156. 'value': 'HOLEY_ELEMENTS' },
  157. { 'name': 'elements_fast_elements',
  158. 'value': 'PACKED_ELEMENTS' },
  159. { 'name': 'elements_dictionary_elements',
  160. 'value': 'DICTIONARY_ELEMENTS' },
  161. { 'name': 'bit_field2_elements_kind_mask',
  162. 'value': 'Map::Bits2::ElementsKindBits::kMask' },
  163. { 'name': 'bit_field2_elements_kind_shift',
  164. 'value': 'Map::Bits2::ElementsKindBits::kShift' },
  165. { 'name': 'bit_field3_is_dictionary_map_shift',
  166. 'value': 'Map::Bits3::IsDictionaryMapBit::kShift' },
  167. { 'name': 'bit_field3_number_of_own_descriptors_mask',
  168. 'value': 'Map::Bits3::NumberOfOwnDescriptorsBits::kMask' },
  169. { 'name': 'bit_field3_number_of_own_descriptors_shift',
  170. 'value': 'Map::Bits3::NumberOfOwnDescriptorsBits::kShift' },
  171. { 'name': 'class_Map__instance_descriptors_offset',
  172. 'value': 'Map::kInstanceDescriptorsOffset' },
  173. { 'name': 'off_fp_context_or_frame_type',
  174. 'value': 'CommonFrameConstants::kContextOrFrameTypeOffset'},
  175. { 'name': 'off_fp_context',
  176. 'value': 'StandardFrameConstants::kContextOffset' },
  177. { 'name': 'off_fp_constant_pool',
  178. 'value': 'StandardFrameConstants::kConstantPoolOffset' },
  179. { 'name': 'off_fp_function',
  180. 'value': 'StandardFrameConstants::kFunctionOffset' },
  181. { 'name': 'off_fp_args',
  182. 'value': 'StandardFrameConstants::kFixedFrameSizeAboveFp' },
  183. { 'name': 'off_fp_bytecode_array',
  184. 'value': 'UnoptimizedFrameConstants::kBytecodeArrayFromFp' },
  185. { 'name': 'off_fp_bytecode_offset',
  186. 'value': 'UnoptimizedFrameConstants::kBytecodeOffsetOrFeedbackVectorFromFp' },
  187. { 'name': 'scopeinfo_idx_nparams',
  188. 'value': 'ScopeInfo::kParameterCount' },
  189. { 'name': 'scopeinfo_idx_ncontextlocals',
  190. 'value': 'ScopeInfo::kContextLocalCount' },
  191. { 'name': 'scopeinfo_idx_first_vars',
  192. 'value': 'ScopeInfo::kVariablePartIndex' },
  193. { 'name': 'jsarray_buffer_was_detached_mask',
  194. 'value': 'JSArrayBuffer::WasDetachedBit::kMask' },
  195. { 'name': 'jsarray_buffer_was_detached_shift',
  196. 'value': 'JSArrayBuffer::WasDetachedBit::kShift' },
  197. { 'name': 'context_idx_scope_info',
  198. 'value': 'Context::SCOPE_INFO_INDEX' },
  199. { 'name': 'context_idx_prev',
  200. 'value': 'Context::PREVIOUS_INDEX' },
  201. { 'name': 'context_min_slots',
  202. 'value': 'Context::MIN_CONTEXT_SLOTS' },
  203. { 'name': 'native_context_embedder_data_offset',
  204. 'value': 'Internals::kNativeContextEmbedderDataOffset' },
  205. { 'name': 'namedictionaryshape_prefix_size',
  206. 'value': 'NameDictionaryShape::kPrefixSize' },
  207. { 'name': 'namedictionaryshape_entry_size',
  208. 'value': 'NameDictionaryShape::kEntrySize' },
  209. { 'name': 'globaldictionaryshape_entry_size',
  210. 'value': 'GlobalDictionaryShape::kEntrySize' },
  211. { 'name': 'namedictionary_prefix_start_index',
  212. 'value': 'NameDictionary::kPrefixStartIndex' },
  213. { 'name': 'numberdictionaryshape_prefix_size',
  214. 'value': 'NumberDictionaryShape::kPrefixSize' },
  215. { 'name': 'numberdictionaryshape_entry_size',
  216. 'value': 'NumberDictionaryShape::kEntrySize' },
  217. { 'name': 'simplenumberdictionaryshape_prefix_size',
  218. 'value': 'SimpleNumberDictionaryShape::kPrefixSize' },
  219. { 'name': 'simplenumberdictionaryshape_entry_size',
  220. 'value': 'SimpleNumberDictionaryShape::kEntrySize' },
  221. { 'name': 'type_JSError__JS_ERROR_TYPE', 'value': 'JS_ERROR_TYPE' },
  222. ];
  223. #
  224. # The following useful fields are missing accessors, so we define fake ones.
  225. # Please note that extra accessors should _only_ be added to expose offsets that
  226. # can be used to access actual V8 objects' properties. They should not be added
  227. # for exposing other values. For instance, enumeration values or class'
  228. # constants should be exposed by adding an entry in the "consts_misc" table, not
  229. # in this "extras_accessors" table.
  230. #
  231. extras_accessors = [
  232. 'JSFunction, context, Context, kContextOffset',
  233. 'JSFunction, shared, SharedFunctionInfo, kSharedFunctionInfoOffset',
  234. 'HeapObject, map, Map, kMapOffset',
  235. 'JSObject, elements, Object, kElementsOffset',
  236. 'JSObject, internal_fields, uintptr_t, kHeaderSize',
  237. 'FixedArray, data, uintptr_t, kHeaderSize',
  238. 'BytecodeArray, data, uintptr_t, kHeaderSize',
  239. 'JSArrayBuffer, backing_store, uintptr_t, kBackingStoreOffset',
  240. 'JSArrayBuffer, byte_length, size_t, kByteLengthOffset',
  241. 'JSArrayBufferView, byte_length, size_t, kByteLengthOffset',
  242. 'JSArrayBufferView, byte_offset, size_t, kByteOffsetOffset',
  243. 'JSDate, value, Object, kValueOffset',
  244. 'JSRegExp, source, Object, kSourceOffset',
  245. 'JSTypedArray, external_pointer, uintptr_t, kExternalPointerOffset',
  246. 'JSTypedArray, length, Object, kLengthOffset',
  247. 'Map, instance_size_in_words, char, kInstanceSizeInWordsOffset',
  248. 'Map, inobject_properties_start_or_constructor_function_index, char, kInobjectPropertiesStartOrConstructorFunctionIndexOffset',
  249. 'Map, instance_type, uint16_t, kInstanceTypeOffset',
  250. 'Map, bit_field, char, kBitFieldOffset',
  251. 'Map, bit_field2, char, kBitField2Offset',
  252. 'Map, bit_field3, int, kBitField3Offset',
  253. 'Map, prototype, Object, kPrototypeOffset',
  254. 'Oddball, kind_offset, int, kKindOffset',
  255. 'HeapNumber, value, double, kValueOffset',
  256. 'ExternalString, resource, Object, kResourceOffset',
  257. 'SeqOneByteString, chars, char, kHeaderSize',
  258. 'SeqTwoByteString, chars, char, kHeaderSize',
  259. 'UncompiledData, inferred_name, String, kInferredNameOffset',
  260. 'UncompiledData, start_position, int32_t, kStartPositionOffset',
  261. 'UncompiledData, end_position, int32_t, kEndPositionOffset',
  262. 'Script, source, Object, kSourceOffset',
  263. 'Script, name, Object, kNameOffset',
  264. 'Script, line_ends, Object, kLineEndsOffset',
  265. 'SharedFunctionInfo, raw_function_token_offset, int16_t, kFunctionTokenOffsetOffset',
  266. 'SharedFunctionInfo, internal_formal_parameter_count, uint16_t, kFormalParameterCountOffset',
  267. 'SharedFunctionInfo, flags, int, kFlagsOffset',
  268. 'SharedFunctionInfo, length, uint16_t, kLengthOffset',
  269. 'SlicedString, parent, String, kParentOffset',
  270. 'Code, flags, uint32_t, kFlagsOffset',
  271. 'Code, instruction_start, uintptr_t, kHeaderSize',
  272. 'Code, instruction_size, int, kInstructionSizeOffset',
  273. 'String, length, int32_t, kLengthOffset',
  274. 'DescriptorArray, header_size, uintptr_t, kHeaderSize',
  275. 'ConsString, first, String, kFirstOffset',
  276. 'ConsString, second, String, kSecondOffset',
  277. 'SlicedString, offset, SMI, kOffsetOffset',
  278. 'ThinString, actual, String, kActualOffset',
  279. 'Symbol, name, Object, kDescriptionOffset',
  280. 'FixedArrayBase, length, SMI, kLengthOffset',
  281. ];
  282. #
  283. # The following is a whitelist of classes we expect to find when scanning the
  284. # source code. This list is not exhaustive, but it's still useful to identify
  285. # when this script gets out of sync with the source. See load_objects().
  286. #
  287. expected_classes = [
  288. 'ConsString', 'FixedArray', 'HeapNumber', 'JSArray', 'JSFunction',
  289. 'JSObject', 'JSRegExp', 'JSPrimitiveWrapper', 'Map', 'Oddball', 'Script',
  290. 'SeqOneByteString', 'SharedFunctionInfo', 'ScopeInfo', 'JSPromise',
  291. 'DescriptorArray'
  292. ];
  293. #
  294. # The following structures store high-level representations of the structures
  295. # for which we're going to emit descriptive constants.
  296. #
  297. types = {}; # set of all type names
  298. typeclasses = {}; # maps type names to corresponding class names
  299. klasses = {}; # known classes, including parents
  300. fields = []; # field declarations
  301. header = '''
  302. /*
  303. * This file is generated by %s. Do not edit directly.
  304. */
  305. #include "src/init/v8.h"
  306. #include "src/codegen/register.h"
  307. #include "src/execution/frames.h"
  308. #include "src/execution/frames-inl.h" /* for architecture-specific frame constants */
  309. #include "src/objects/contexts.h"
  310. #include "src/objects/objects.h"
  311. #include "src/objects/data-handler.h"
  312. #include "src/objects/js-promise.h"
  313. #include "src/objects/js-regexp-string-iterator.h"
  314. namespace v8 {
  315. namespace internal {
  316. extern "C" {
  317. /* stack frame constants */
  318. #define FRAME_CONST(value, klass) \
  319. V8_EXPORT int v8dbg_frametype_##klass = StackFrame::value;
  320. STACK_FRAME_TYPE_LIST(FRAME_CONST)
  321. #undef FRAME_CONST
  322. ''' % sys.argv[0]
  323. footer = '''
  324. }
  325. }
  326. }
  327. '''
  328. #
  329. # Get the base class
  330. #
  331. def get_base_class(klass):
  332. if (klass == 'Object'):
  333. return klass;
  334. if (not (klass in klasses)):
  335. return None;
  336. k = klasses[klass];
  337. return get_base_class(k['parent']);
  338. #
  339. # Loads class hierarchy and type information from "objects.h" etc.
  340. #
  341. def load_objects():
  342. #
  343. # Construct a dictionary for the classes we're sure should be present.
  344. #
  345. checktypes = {};
  346. for klass in expected_classes:
  347. checktypes[klass] = True;
  348. for filename in sys.argv[2:]:
  349. if not filename.endswith("-inl.h"):
  350. load_objects_from_file(filename, checktypes)
  351. if (len(checktypes) > 0):
  352. for klass in checktypes:
  353. print('error: expected class \"%s\" not found' % klass);
  354. sys.exit(1);
  355. def load_objects_from_file(objfilename, checktypes):
  356. objfile = io.open(objfilename, 'r', encoding='utf-8');
  357. in_insttype = False;
  358. in_torque_insttype = False
  359. in_torque_fulldef = False
  360. typestr = '';
  361. torque_typestr = ''
  362. torque_fulldefstr = ''
  363. uncommented_file = ''
  364. #
  365. # Iterate the header file line-by-line to collect type and class
  366. # information. For types, we accumulate a string representing the entire
  367. # InstanceType enum definition and parse it later because it's easier to
  368. # do so without the embedded newlines.
  369. #
  370. for line in objfile:
  371. if (line.startswith('enum InstanceType : uint16_t {')):
  372. in_insttype = True;
  373. continue;
  374. if (line.startswith('#define TORQUE_ASSIGNED_INSTANCE_TYPE_LIST')):
  375. in_torque_insttype = True
  376. continue
  377. if (line.startswith('#define TORQUE_INSTANCE_CHECKERS_SINGLE_FULLY_DEFINED')):
  378. in_torque_fulldef = True
  379. continue
  380. if (in_insttype and line.startswith('};')):
  381. in_insttype = False;
  382. continue;
  383. if (in_torque_insttype and (not line or line.isspace())):
  384. in_torque_insttype = False
  385. continue
  386. if (in_torque_fulldef and (not line or line.isspace())):
  387. in_torque_fulldef = False
  388. continue
  389. pre = line.strip()
  390. line = re.sub('// .*', '', line.strip());
  391. if (in_insttype):
  392. typestr += line;
  393. continue;
  394. if (in_torque_insttype):
  395. torque_typestr += line
  396. continue
  397. if (in_torque_fulldef):
  398. torque_fulldefstr += line
  399. continue
  400. uncommented_file += '\n' + line
  401. for match in re.finditer(r'\nclass(?:\s+V8_EXPORT(?:_PRIVATE)?)?'
  402. r'\s+(\w[^:;]*)'
  403. r'(?:: public (\w[^{]*))?\s*{\s*',
  404. uncommented_file):
  405. klass = match.group(1).strip();
  406. pklass = match.group(2);
  407. if (pklass):
  408. # Check for generated Torque class.
  409. gen_match = re.match(
  410. r'TorqueGenerated\w+\s*<\s*\w+,\s*(\w+)\s*>',
  411. pklass)
  412. if (gen_match):
  413. pklass = gen_match.group(1)
  414. # Strip potential template arguments from parent
  415. # class.
  416. match = re.match(r'(\w+)(<.*>)?', pklass.strip());
  417. pklass = match.group(1).strip();
  418. klasses[klass] = { 'parent': pklass };
  419. #
  420. # Process the instance type declaration.
  421. #
  422. entries = typestr.split(',');
  423. for entry in entries:
  424. types[re.sub('\s*=.*', '', entry).lstrip()] = True;
  425. entries = torque_typestr.split('\\')
  426. for entry in entries:
  427. name = re.sub(r' *V\(|\).*', '', entry)
  428. types[name] = True
  429. entries = torque_fulldefstr.split('\\')
  430. for entry in entries:
  431. entry = entry.strip()
  432. if not entry:
  433. continue
  434. start = entry.find('(');
  435. end = entry.find(')', start);
  436. rest = entry[start + 1: end];
  437. args = re.split('\s*,\s*', rest);
  438. typename = args[0]
  439. typeconst = args[1]
  440. types[typeconst] = True
  441. typeclasses[typeconst] = typename
  442. #
  443. # Infer class names for each type based on a systematic transformation.
  444. # For example, "JS_FUNCTION_TYPE" becomes "JSFunction". We find the
  445. # class for each type rather than the other way around because there are
  446. # fewer cases where one type maps to more than one class than the other
  447. # way around.
  448. #
  449. for type in types:
  450. usetype = type
  451. #
  452. # Remove the "_TYPE" suffix and then convert to camel case,
  453. # except that a "JS" prefix remains uppercase (as in
  454. # "JS_FUNCTION_TYPE" => "JSFunction").
  455. #
  456. if (not usetype.endswith('_TYPE')):
  457. continue;
  458. usetype = usetype[0:len(usetype) - len('_TYPE')];
  459. parts = usetype.split('_');
  460. cctype = '';
  461. if (parts[0] == 'JS'):
  462. cctype = 'JS';
  463. start = 1;
  464. else:
  465. cctype = '';
  466. start = 0;
  467. for ii in range(start, len(parts)):
  468. part = parts[ii];
  469. cctype += part[0].upper() + part[1:].lower();
  470. #
  471. # Mapping string types is more complicated. Both types and
  472. # class names for Strings specify a representation (e.g., Seq,
  473. # Cons, External, or Sliced) and an encoding (TwoByte/OneByte),
  474. # In the simplest case, both of these are explicit in both
  475. # names, as in:
  476. #
  477. # EXTERNAL_ONE_BYTE_STRING_TYPE => ExternalOneByteString
  478. #
  479. # However, either the representation or encoding can be omitted
  480. # from the type name, in which case "Seq" and "TwoByte" are
  481. # assumed, as in:
  482. #
  483. # STRING_TYPE => SeqTwoByteString
  484. #
  485. # Additionally, sometimes the type name has more information
  486. # than the class, as in:
  487. #
  488. # CONS_ONE_BYTE_STRING_TYPE => ConsString
  489. #
  490. # To figure this out dynamically, we first check for a
  491. # representation and encoding and add them if they're not
  492. # present. If that doesn't yield a valid class name, then we
  493. # strip out the representation.
  494. #
  495. if (cctype.endswith('String')):
  496. if (cctype.find('Cons') == -1 and
  497. cctype.find('External') == -1 and
  498. cctype.find('Sliced') == -1):
  499. if (cctype.find('OneByte') != -1):
  500. cctype = re.sub('OneByteString$',
  501. 'SeqOneByteString', cctype);
  502. else:
  503. cctype = re.sub('String$',
  504. 'SeqString', cctype);
  505. if (cctype.find('OneByte') == -1):
  506. cctype = re.sub('String$', 'TwoByteString',
  507. cctype);
  508. if (not (cctype in klasses)):
  509. cctype = re.sub('OneByte', '', cctype);
  510. cctype = re.sub('TwoByte', '', cctype);
  511. #
  512. # Despite all that, some types have no corresponding class.
  513. #
  514. if (cctype in klasses):
  515. typeclasses[type] = cctype;
  516. if (cctype in checktypes):
  517. del checktypes[cctype];
  518. #
  519. # For a given macro call, pick apart the arguments and return an object
  520. # describing the corresponding output constant. See load_fields().
  521. #
  522. def parse_field(call):
  523. # Replace newlines with spaces.
  524. for ii in range(0, len(call)):
  525. if (call[ii] == '\n'):
  526. call[ii] == ' ';
  527. idx = call.find('(');
  528. kind = call[0:idx];
  529. rest = call[idx + 1: len(call) - 1];
  530. args = re.split('\s*,\s*', rest);
  531. consts = [];
  532. klass = args[0];
  533. field = args[1];
  534. dtype = None
  535. offset = None
  536. if kind.startswith('WEAK_ACCESSORS'):
  537. dtype = 'weak'
  538. offset = args[2];
  539. elif not (kind.startswith('SMI_ACCESSORS') or kind.startswith('ACCESSORS_TO_SMI')):
  540. dtype = args[2].replace('<', '_').replace('>', '_')
  541. offset = args[3];
  542. else:
  543. offset = args[2];
  544. dtype = 'SMI'
  545. assert(offset is not None and dtype is not None);
  546. return ({
  547. 'name': 'class_%s__%s__%s' % (klass, field, dtype),
  548. 'value': '%s::%s' % (klass, offset)
  549. });
  550. #
  551. # Load field offset information from objects-inl.h etc.
  552. #
  553. def load_fields():
  554. for filename in sys.argv[2:]:
  555. if filename.endswith("-inl.h"):
  556. load_fields_from_file(filename)
  557. for body in extras_accessors:
  558. fields.append(parse_field('ACCESSORS(%s)' % body));
  559. def load_fields_from_file(filename):
  560. inlfile = io.open(filename, 'r', encoding='utf-8');
  561. #
  562. # Each class's fields and the corresponding offsets are described in the
  563. # source by calls to macros like "ACCESSORS" (and friends). All we do
  564. # here is extract these macro invocations, taking into account that they
  565. # may span multiple lines and may contain nested parentheses. We also
  566. # call parse_field() to pick apart the invocation.
  567. #
  568. prefixes = [ 'ACCESSORS', 'ACCESSORS2', 'ACCESSORS_GCSAFE',
  569. 'SMI_ACCESSORS', 'ACCESSORS_TO_SMI',
  570. 'RELEASE_ACQUIRE_ACCESSORS', 'WEAK_ACCESSORS' ];
  571. prefixes += ([ prefix + "_CHECKED" for prefix in prefixes ] +
  572. [ prefix + "_CHECKED2" for prefix in prefixes ])
  573. current = '';
  574. opens = 0;
  575. for line in inlfile:
  576. if (opens > 0):
  577. # Continuation line
  578. for ii in range(0, len(line)):
  579. if (line[ii] == '('):
  580. opens += 1;
  581. elif (line[ii] == ')'):
  582. opens -= 1;
  583. if (opens == 0):
  584. break;
  585. current += line[0:ii + 1];
  586. continue;
  587. for prefix in prefixes:
  588. if (not line.startswith(prefix + '(')):
  589. continue;
  590. if (len(current) > 0):
  591. fields.append(parse_field(current));
  592. current = '';
  593. for ii in range(len(prefix), len(line)):
  594. if (line[ii] == '('):
  595. opens += 1;
  596. elif (line[ii] == ')'):
  597. opens -= 1;
  598. if (opens == 0):
  599. break;
  600. current += line[0:ii + 1];
  601. if (len(current) > 0):
  602. fields.append(parse_field(current));
  603. current = '';
  604. #
  605. # Emit a block of constants.
  606. #
  607. def emit_set(out, consts):
  608. lines = set() # To remove duplicates.
  609. # Fix up overzealous parses. This could be done inside the
  610. # parsers but as there are several, it's easiest to do it here.
  611. ws = re.compile('\s+')
  612. for const in consts:
  613. name = ws.sub('', const['name'])
  614. value = ws.sub('', str(const['value'])) # Can be a number.
  615. lines.add('V8_EXPORT int v8dbg_%s = %s;\n' % (name, value))
  616. for line in lines:
  617. out.write(line);
  618. out.write('\n');
  619. #
  620. # Emit the whole output file.
  621. #
  622. def emit_config():
  623. out = open(sys.argv[1], 'w');
  624. out.write(header);
  625. out.write('/* miscellaneous constants */\n');
  626. emit_set(out, consts_misc);
  627. out.write('/* class type information */\n');
  628. consts = [];
  629. for typename in sorted(typeclasses):
  630. klass = typeclasses[typename];
  631. consts.append({
  632. 'name': 'type_%s__%s' % (klass, typename),
  633. 'value': typename
  634. });
  635. emit_set(out, consts);
  636. out.write('/* class hierarchy information */\n');
  637. consts = [];
  638. for klassname in sorted(klasses):
  639. pklass = klasses[klassname]['parent'];
  640. bklass = get_base_class(klassname);
  641. if (bklass != 'Object'):
  642. continue;
  643. if (pklass == None):
  644. continue;
  645. consts.append({
  646. 'name': 'parent_%s__%s' % (klassname, pklass),
  647. 'value': 0
  648. });
  649. emit_set(out, consts);
  650. out.write('/* field information */\n');
  651. emit_set(out, fields);
  652. out.write(footer);
  653. if (len(sys.argv) < 4):
  654. print('usage: %s output.cc objects.h objects-inl.h' % sys.argv[0]);
  655. sys.exit(2);
  656. load_objects();
  657. load_fields();
  658. emit_config();