DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
#!/usr/bin/python -B
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import print_function
import re
from xml.sax.saxutils import escape

quoted_pat = re.compile(r"([^A-Za-z0-9]|^)'([^']+)'")
js_pat = re.compile(r"([^A-Za-z0-9]|^)(JS[A-Z0-9_\*]+)")


def codify(text):
    text = re.sub(quoted_pat, '\\1<code>\\2</code>', text)
    text = re.sub(js_pat, '\\1<code>\\2</code>', text)

    return text


space_star_space_pat = re.compile('^\s*\* ?', re.M)


def get_comment_body(comment):
    return re.sub(space_star_space_pat, '', comment).split('\n')


quote_pat = re.compile('"([^"]+)"')
str_pat = re.compile('js_([^_]+)_str')


def parse_name(s):
    m = quote_pat.search(s)
    if m:
        return m.group(1)
    m = str_pat.search(s)
    if m:
        return m.group(1)
    return s


csv_pat = re.compile(', *')


def parse_csv(s):
    a = csv_pat.split(s)
    if len(a) == 1 and a[0] == '':
        return []
    return a


def get_stack_count(stack):
    if stack == '':
        return 0
    if '...' in stack:
        return -1
    return len(stack.split(','))


def parse_index(comment):
    index = []
    current_types = None
    category_name = ''
    category_pat = re.compile('\[([^\]]+)\]')
    for line in get_comment_body(comment):
        m = category_pat.search(line)
        if m:
            category_name = m.group(1)
            if category_name == 'Index':
                continue
            current_types = []
            index.append((category_name, current_types))
        else:
            type_name = line.strip()
            if type_name and current_types is not None:
                current_types.append((type_name, []))

    return index

# Holds the information stored in the comment with the following format:
#   /*
#    * {desc}
#    *   Category: {category_name}
#    *   Type: {type_name}
#    *   Operands: {operands}
#    *   Stack: {stack_uses} => {stack_defs}
#    *   length: {length_override}
#    *   nuses: {nuses_override}
#    *   ndefs: {ndefs_override}
#    */


class CommentInfo:
    def __init__(self):
        self.desc = ''
        self.category_name = ''
        self.type_name = ''
        self.operands = ''
        self.stack_uses = ''
        self.stack_defs = ''
        self.length_override = ''
        self.nuses_override = ''
        self.ndefs_override = ''

# Holds the information stored in the macro with the following format:
#   MACRO({name}, {value}, {display_name}, {image}, {length}, {nuses}, {ndefs},
#         {flags})
# and the information from CommentInfo.


class OpcodeInfo:
    def __init__(self, comment_info):
        self.name = ''
        self.value = ''
        self.display_name = ''
        self.image = ''
        self.length = ''
        self.nuses = ''
        self.ndefs = ''
        self.flags = ''

        self.operands_array = []
        self.stack_uses_array = []
        self.stack_defs_array = []

        self.desc = comment_info.desc
        self.category_name = comment_info.category_name
        self.type_name = comment_info.type_name
        self.operands = comment_info.operands
        self.operands_array = comment_info.operands_array
        self.stack_uses = comment_info.stack_uses
        self.stack_uses_array = comment_info.stack_uses_array
        self.stack_defs = comment_info.stack_defs
        self.stack_defs_array = comment_info.stack_defs_array
        self.length_override = comment_info.length_override
        self.nuses_override = comment_info.nuses_override
        self.ndefs_override = comment_info.ndefs_override

        # List of OpcodeInfo that corresponds to macros after this.
        #   /*
        #    * comment
        #    */
        #   MACRO(JSOP_SUB, ...)
        #   MACRO(JSOP_MUL, ...)
        #   MACRO(JSOP_DIV, ...)
        self.group = []

        self.sort_key = ''


def find_by_name(list, name):
    for (n, body) in list:
        if n == name:
            return body

    return None


def add_to_index(index, opcode):
    types = find_by_name(index, opcode.category_name)
    if types is None:
        raise Exception('Category is not listed in index: '
                        '{name}'.format(name=opcode.category_name))
    opcodes = find_by_name(types, opcode.type_name)
    if opcodes is None:
        if opcode.type_name:
            raise Exception('Type is not listed in {category}: '
                            '{name}'.format(category=opcode.category_name,
                                            name=opcode.type_name))
        types.append((opcode.type_name, [opcode]))
        return

    opcodes.append(opcode)


def format_desc(descs):
    current_type = ''
    desc = ''
    for (type, line) in descs:
        if type != current_type:
            if current_type:
                desc += '</{name}>\n'.format(name=current_type)
            current_type = type
            if type:
                desc += '<{name}>'.format(name=current_type)
        if current_type:
            desc += line + '\n'
    if current_type:
        desc += '</{name}>'.format(name=current_type)

    return desc


tag_pat = re.compile('^\s*[A-Za-z]+:\s*|\s*$')


def get_tag_value(line):
    return re.sub(tag_pat, '', line)


def get_opcodes(dir):
    iter_pat = re.compile(r"/\*(.*?)\*/"  # either a documentation comment...
                          r"|"
                          r"MACRO\("      # or a MACRO(...) call
                          r"(?P<name>[^,]+),\s*"
                          r"(?P<value>[0-9]+),\s*"
                          r"(?P<display_name>[^,]+,)\s*"
                          r"(?P<image>[^,]+),\s*"
                          r"(?P<length>[0-9\-]+),\s*"
                          r"(?P<nuses>[0-9\-]+),\s*"
                          r"(?P<ndefs>[0-9\-]+),\s*"
                          r"(?P<flags>[^\)]+)"
                          r"\)", re.S)
    stack_pat = re.compile(r"^(?P<uses>.*?)"
                           r"\s*=>\s*"
                           r"(?P<defs>.*?)$")

    opcodes = dict()
    index = []

    with open('{dir}/js/src/vm/Opcodes.h'.format(dir=dir), 'r') as f:
        data = f.read()

    comment_info = None
    opcode = None

    # The first opcode after the comment.
    group_head = None

    for m in re.finditer(iter_pat, data):
        comment = m.group(1)
        name = m.group('name')

        if comment:
            if '[Index]' in comment:
                index = parse_index(comment)
                continue

            if 'Operands:' not in comment:
                continue

            group_head = None

            comment_info = CommentInfo()

            state = 'desc'
            stack = ''
            descs = []

            for line in get_comment_body(comment):
                if line.startswith('  Category:'):
                    state = 'category'
                    comment_info.category_name = get_tag_value(line)
                elif line.startswith('  Type:'):
                    state = 'type'
                    comment_info.type_name = get_tag_value(line)
                elif line.startswith('  Operands:'):
                    state = 'operands'
                    comment_info.operands = get_tag_value(line)
                elif line.startswith('  Stack:'):
                    state = 'stack'
                    stack = get_tag_value(line)
                elif line.startswith('  len:'):
                    state = 'len'
                    comment_info.length_override = get_tag_value(line)
                elif line.startswith('  nuses:'):
                    state = 'nuses'
                    comment_info.nuses_override = get_tag_value(line)
                elif line.startswith('  ndefs:'):
                    state = 'ndefs'
                    comment_info.ndefs_override = get_tag_value(line)
                elif state == 'desc':
                    if line.startswith(' '):
                        descs.append(('pre', escape(line[1:])))
                    else:
                        line = line.strip()
                        if line == '':
                            descs.append(('', line))
                        else:
                            descs.append(('p', codify(escape(line))))
                elif line.startswith('  '):
                    if state == 'operands':
                        comment_info.operands += line.strip()
                    elif state == 'stack':
                        stack += line.strip()
                    elif state == 'len':
                        comment_info.length_override += line.strip()
                    elif state == 'nuses':
                        comment_info.nuses_override += line.strip()
                    elif state == 'ndefs':
                        comment_info.ndefs_override += line.strip()

            comment_info.desc = format_desc(descs)

            comment_info.operands_array = parse_csv(comment_info.operands)
            comment_info.stack_uses_array = parse_csv(comment_info.stack_uses)
            comment_info.stack_defs_array = parse_csv(comment_info.stack_defs)

            m2 = stack_pat.search(stack)
            if m2:
                comment_info.stack_uses = m2.group('uses')
                comment_info.stack_defs = m2.group('defs')
        elif name and not name.startswith('JSOP_UNUSED'):
            opcode = OpcodeInfo(comment_info)

            opcode.name = name
            opcode.value = int(m.group('value'))
            opcode.display_name = parse_name(m.group('display_name'))
            opcode.image = parse_name(m.group('image'))
            opcode.length = m.group('length')
            opcode.nuses = m.group('nuses')
            opcode.ndefs = m.group('ndefs')
            opcode.flags = m.group('flags').split('|')

            if not group_head:
                group_head = opcode

                opcode.sort_key = opcode.name
                if opcode.category_name == '':
                    raise Exception('Category is not specified for '
                                    '{name}'.format(name=opcode.name))
                add_to_index(index, opcode)
            else:
                if group_head.length != opcode.length:
                    raise Exception('length should be same for opcodes of the'
                                    ' same group: '
                                    '{value1}({name1}) != '
                                    '{value2}({name2})'.format(
                                        name1=group_head.name,
                                        value1=group_head.length,
                                        name2=opcode.name,
                                        value2=opcode.length))
                if group_head.nuses != opcode.nuses:
                    raise Exception('nuses should be same for opcodes of the'
                                    ' same group: '
                                    '{value1}({name1}) != '
                                    '{value2}({name2})'.format(
                                        name1=group_head.name,
                                        value1=group_head.nuses,
                                        name2=opcode.name,
                                        value2=opcode.nuses))
                if group_head.ndefs != opcode.ndefs:
                    raise Exception('ndefs should be same for opcodes of the'
                                    ' same group: '
                                    '{value1}({name1}) != '
                                    '{value2}({name2})'.format(
                                        name1=group_head.name,
                                        value1=group_head.ndefs,
                                        name2=opcode.name,
                                        value2=opcode.ndefs))

                group_head.group.append(opcode)

                if opcode.name < group_head.name:
                    group_head.sort_key = opcode.name

            opcodes[name] = opcode

            # Verify stack notation.
            nuses = int(opcode.nuses)
            ndefs = int(opcode.ndefs)

            stack_nuses = get_stack_count(opcode.stack_uses)
            stack_ndefs = get_stack_count(opcode.stack_defs)

            if nuses != -1 and stack_nuses != -1 and nuses != stack_nuses:
                raise Exception('nuses should match stack notation: {name}: '
                                '{nuses} != {stack_nuses} '
                                '(stack_nuses)'.format(
                                    name=name,
                                    nuses=nuses,
                                    stack_nuses=stack_nuses,
                                    stack_uses=opcode.stack_uses))
            if ndefs != -1 and stack_ndefs != -1 and ndefs != stack_ndefs:
                raise Exception('ndefs should match stack notation: {name}: '
                                '{ndefs} != {stack_ndefs} '
                                '(stack_ndefs)'.format(
                                    name=name,
                                    ndefs=ndefs,
                                    stack_ndefs=stack_ndefs,
                                    stack_defs=opcode.stack_defs))

    return index, opcodes