2015-07-09 15:35:29 +03:00
|
|
|
# vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
# 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/.
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
# ----------------------------------------------------------------------------
|
2015-07-09 15:35:29 +03:00
|
|
|
# This script checks that SpiderMonkey MacroAssembler methods are properly
|
|
|
|
# annotated.
|
|
|
|
#
|
|
|
|
# The MacroAssembler has one interface for all platforms, but it might have one
|
|
|
|
# definition per platform. The code of the MacroAssembler use a macro to
|
|
|
|
# annotate the method declarations, in order to delete the function if it is not
|
|
|
|
# present on the current platform, and also to locate the files in which the
|
|
|
|
# methods are defined.
|
|
|
|
#
|
|
|
|
# This script scans the MacroAssembler.h header, for method declarations.
|
|
|
|
# It also scans MacroAssembler-/arch/.cpp, MacroAssembler-/arch/-inl.h, and
|
|
|
|
# MacroAssembler-inl.h for method definitions. The result of both scans are
|
|
|
|
# uniformized, and compared, to determine if the MacroAssembler.h header as
|
|
|
|
# proper methods annotations.
|
2017-09-15 19:14:41 +03:00
|
|
|
# ----------------------------------------------------------------------------
|
2015-07-09 15:35:29 +03:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import difflib
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2017-05-19 02:06:49 +03:00
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
architecture_independent = set(['generic'])
|
|
|
|
all_unsupported_architectures_names = set(['mips32', 'mips64', 'mips_shared'])
|
|
|
|
all_architecture_names = set(['x86', 'x64', 'arm', 'arm64'])
|
|
|
|
all_shared_architecture_names = set(['x86_shared', 'arm', 'arm64'])
|
2015-07-09 15:35:29 +03:00
|
|
|
|
2015-08-17 12:32:14 +03:00
|
|
|
reBeforeArg = "(?<=[(,\s])"
|
|
|
|
reArgType = "(?P<type>[\w\s:*&]+)"
|
|
|
|
reArgName = "(?P<name>\s\w+)"
|
|
|
|
reArgDefault = "(?P<default>(?:\s=[^,)]+)?)"
|
|
|
|
reAfterArg = "(?=[,)])"
|
2018-05-22 01:01:01 +03:00
|
|
|
reMatchArg = re.compile(reBeforeArg + reArgType +
|
|
|
|
reArgName + reArgDefault + reAfterArg)
|
2015-08-17 12:32:14 +03:00
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
|
|
|
def get_normalized_signatures(signature, fileAnnot=None):
|
2016-02-10 18:22:36 +03:00
|
|
|
# Remove static
|
|
|
|
signature = signature.replace('static', '')
|
2015-07-09 15:35:29 +03:00
|
|
|
# Remove semicolon.
|
|
|
|
signature = signature.replace(';', ' ')
|
|
|
|
# Normalize spaces.
|
|
|
|
signature = re.sub(r'\s+', ' ', signature).strip()
|
2015-08-17 12:32:14 +03:00
|
|
|
# Match arguments, and keep only the type.
|
|
|
|
signature = reMatchArg.sub('\g<type>', signature)
|
2015-07-09 15:35:29 +03:00
|
|
|
# Remove class name
|
|
|
|
signature = signature.replace('MacroAssembler::', '')
|
|
|
|
|
|
|
|
# Extract list of architectures
|
|
|
|
archs = ['generic']
|
|
|
|
if fileAnnot:
|
|
|
|
archs = [fileAnnot['arch']]
|
|
|
|
|
|
|
|
if 'DEFINED_ON(' in signature:
|
2018-05-22 01:01:01 +03:00
|
|
|
archs = re.sub(
|
|
|
|
r'.*DEFINED_ON\((?P<archs>[^()]*)\).*', '\g<archs>', signature).split(',')
|
2015-07-09 15:35:29 +03:00
|
|
|
archs = [a.strip() for a in archs]
|
|
|
|
signature = re.sub(r'\s+DEFINED_ON\([^()]*\)', '', signature)
|
|
|
|
|
|
|
|
elif 'PER_ARCH' in signature:
|
|
|
|
archs = all_architecture_names
|
|
|
|
signature = re.sub(r'\s+PER_ARCH', '', signature)
|
|
|
|
|
|
|
|
elif 'PER_SHARED_ARCH' in signature:
|
|
|
|
archs = all_shared_architecture_names
|
|
|
|
signature = re.sub(r'\s+PER_SHARED_ARCH', '', signature)
|
|
|
|
|
2018-01-22 23:51:58 +03:00
|
|
|
elif 'OOL_IN_HEADER' in signature:
|
|
|
|
assert archs == ['generic']
|
|
|
|
signature = re.sub(r'\s+OOL_IN_HEADER', '', signature)
|
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
else:
|
|
|
|
# No signature annotation, the list of architectures remains unchanged.
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Extract inline annotation
|
|
|
|
inline = False
|
|
|
|
if fileAnnot:
|
|
|
|
inline = fileAnnot['inline']
|
|
|
|
|
|
|
|
if 'inline ' in signature:
|
|
|
|
signature = re.sub(r'inline\s+', '', signature)
|
|
|
|
inline = True
|
|
|
|
|
2015-08-17 12:32:15 +03:00
|
|
|
inlinePrefx = ''
|
|
|
|
if inline:
|
|
|
|
inlinePrefx = 'inline '
|
2017-09-15 19:14:41 +03:00
|
|
|
signatures = [
|
|
|
|
{'arch': a, 'sig': inlinePrefx + signature}
|
2015-07-09 15:35:29 +03:00
|
|
|
for a in archs
|
|
|
|
]
|
|
|
|
|
2015-08-17 12:32:15 +03:00
|
|
|
return signatures
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
file_suffixes = set([
|
|
|
|
a.replace('_', '-') for a in
|
|
|
|
all_architecture_names.union(all_shared_architecture_names)
|
2017-06-29 21:49:10 +03:00
|
|
|
.union(all_unsupported_architectures_names)
|
2015-07-09 15:35:29 +03:00
|
|
|
])
|
2017-09-15 19:14:41 +03:00
|
|
|
|
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
def get_file_annotation(filename):
|
|
|
|
origFilename = filename
|
|
|
|
filename = filename.split('/')[-1]
|
|
|
|
|
|
|
|
inline = False
|
|
|
|
if filename.endswith('.cpp'):
|
|
|
|
filename = filename[:-len('.cpp')]
|
|
|
|
elif filename.endswith('-inl.h'):
|
|
|
|
inline = True
|
|
|
|
filename = filename[:-len('-inl.h')]
|
2018-01-22 23:51:58 +03:00
|
|
|
elif filename.endswith('.h'):
|
|
|
|
# This allows the definitions block in MacroAssembler.h to be
|
|
|
|
# style-checked.
|
|
|
|
inline = True
|
|
|
|
filename = filename[:-len('.h')]
|
2015-07-09 15:35:29 +03:00
|
|
|
else:
|
|
|
|
raise Exception('unknown file name', origFilename)
|
|
|
|
|
|
|
|
arch = 'generic'
|
|
|
|
for suffix in file_suffixes:
|
|
|
|
if filename == 'MacroAssembler-' + suffix:
|
|
|
|
arch = suffix
|
|
|
|
break
|
|
|
|
|
|
|
|
return {
|
|
|
|
'inline': inline,
|
|
|
|
'arch': arch.replace('-', '_')
|
|
|
|
}
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
def get_macroassembler_definitions(filename):
|
|
|
|
try:
|
|
|
|
fileAnnot = get_file_annotation(filename)
|
2018-01-31 22:32:08 +03:00
|
|
|
except Exception:
|
2015-07-09 15:35:29 +03:00
|
|
|
return []
|
|
|
|
|
|
|
|
style_section = False
|
|
|
|
code_section = False
|
|
|
|
lines = ''
|
|
|
|
signatures = []
|
2017-05-19 02:06:49 +03:00
|
|
|
with open(filename) as f:
|
2015-07-09 15:35:29 +03:00
|
|
|
for line in f:
|
|
|
|
if '//{{{ check_macroassembler_style' in line:
|
|
|
|
style_section = True
|
|
|
|
elif '//}}} check_macroassembler_style' in line:
|
|
|
|
style_section = False
|
|
|
|
if not style_section:
|
|
|
|
continue
|
|
|
|
|
|
|
|
line = re.sub(r'//.*', '', line)
|
2017-09-15 18:20:18 +03:00
|
|
|
if line.startswith('{') or line.strip() == "{}":
|
2015-07-09 15:35:29 +03:00
|
|
|
if 'MacroAssembler::' in lines:
|
2018-05-22 01:01:01 +03:00
|
|
|
signatures.extend(
|
|
|
|
get_normalized_signatures(lines, fileAnnot))
|
2017-09-15 19:14:41 +03:00
|
|
|
if line.strip() != "{}": # Empty declaration, no need to declare
|
2017-09-15 18:20:18 +03:00
|
|
|
# a new code section
|
|
|
|
code_section = True
|
2015-07-09 15:35:29 +03:00
|
|
|
continue
|
|
|
|
if line.startswith('}'):
|
|
|
|
code_section = False
|
|
|
|
lines = ''
|
|
|
|
continue
|
|
|
|
if code_section:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if len(line.strip()) == 0:
|
|
|
|
lines = ''
|
|
|
|
continue
|
|
|
|
lines = lines + line
|
|
|
|
# Continue until we have a complete declaration
|
|
|
|
if '{' not in lines:
|
|
|
|
continue
|
|
|
|
# Skip variable declarations
|
|
|
|
if ')' not in lines:
|
|
|
|
lines = ''
|
|
|
|
continue
|
|
|
|
|
|
|
|
return signatures
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
def get_macroassembler_declaration(filename):
|
|
|
|
style_section = False
|
|
|
|
lines = ''
|
|
|
|
signatures = []
|
2017-05-19 02:06:49 +03:00
|
|
|
with open(filename) as f:
|
2015-07-09 15:35:29 +03:00
|
|
|
for line in f:
|
2018-01-22 23:51:58 +03:00
|
|
|
if '//{{{ check_macroassembler_decl_style' in line:
|
2015-07-09 15:35:29 +03:00
|
|
|
style_section = True
|
2018-01-22 23:51:58 +03:00
|
|
|
elif '//}}} check_macroassembler_decl_style' in line:
|
2015-07-09 15:35:29 +03:00
|
|
|
style_section = False
|
|
|
|
if not style_section:
|
|
|
|
continue
|
|
|
|
|
|
|
|
line = re.sub(r'//.*', '', line)
|
|
|
|
if len(line.strip()) == 0:
|
|
|
|
lines = ''
|
|
|
|
continue
|
|
|
|
lines = lines + line
|
|
|
|
# Continue until we have a complete declaration
|
|
|
|
if ';' not in lines:
|
|
|
|
continue
|
|
|
|
# Skip variable declarations
|
|
|
|
if ')' not in lines:
|
|
|
|
lines = ''
|
|
|
|
continue
|
|
|
|
|
|
|
|
signatures.extend(get_normalized_signatures(lines))
|
|
|
|
lines = ''
|
|
|
|
|
|
|
|
return signatures
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
def append_signatures(d, sigs):
|
|
|
|
for s in sigs:
|
|
|
|
if s['sig'] not in d:
|
|
|
|
d[s['sig']] = []
|
2017-09-15 19:14:41 +03:00
|
|
|
d[s['sig']].append(s['arch'])
|
2015-07-09 15:35:29 +03:00
|
|
|
return d
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
def generate_file_content(signatures):
|
|
|
|
output = []
|
|
|
|
for s in sorted(signatures.keys()):
|
2015-07-14 02:57:00 +03:00
|
|
|
archs = set(sorted(signatures[s]))
|
2017-06-29 21:49:10 +03:00
|
|
|
archs -= all_unsupported_architectures_names
|
2015-07-09 15:35:29 +03:00
|
|
|
if len(archs.symmetric_difference(architecture_independent)) == 0:
|
|
|
|
output.append(s + ';\n')
|
|
|
|
if s.startswith('inline'):
|
2018-01-22 23:51:58 +03:00
|
|
|
# TODO, bug 1432600: This is mistaken for OOL_IN_HEADER
|
|
|
|
# functions. (Such annotation is already removed by the time
|
|
|
|
# this function sees the signature here.)
|
2015-07-09 15:35:29 +03:00
|
|
|
output.append(' is defined in MacroAssembler-inl.h\n')
|
|
|
|
else:
|
|
|
|
output.append(' is defined in MacroAssembler.cpp\n')
|
|
|
|
else:
|
|
|
|
if len(archs.symmetric_difference(all_architecture_names)) == 0:
|
|
|
|
output.append(s + ' PER_ARCH;\n')
|
|
|
|
elif len(archs.symmetric_difference(all_shared_architecture_names)) == 0:
|
|
|
|
output.append(s + ' PER_SHARED_ARCH;\n')
|
|
|
|
else:
|
2018-05-22 01:01:01 +03:00
|
|
|
output.append(
|
|
|
|
s + ' DEFINED_ON(' + ', '.join(sorted(archs)) + ');\n')
|
2017-06-29 21:49:10 +03:00
|
|
|
for a in sorted(archs):
|
2015-07-09 15:35:29 +03:00
|
|
|
a = a.replace('_', '-')
|
|
|
|
masm = '%s/MacroAssembler-%s' % (a, a)
|
|
|
|
if s.startswith('inline'):
|
|
|
|
output.append(' is defined in %s-inl.h\n' % masm)
|
|
|
|
else:
|
|
|
|
output.append(' is defined in %s.cpp\n' % masm)
|
|
|
|
return output
|
|
|
|
|
2017-09-15 19:14:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
def check_style():
|
|
|
|
# We read from the header file the signature of each function.
|
|
|
|
decls = dict() # type: dict(signature => ['x86', 'x64'])
|
|
|
|
|
|
|
|
# We infer from each file the signature of each MacroAssembler function.
|
|
|
|
defs = dict() # type: dict(signature => ['x86', 'x64'])
|
|
|
|
|
2018-04-19 14:02:00 +03:00
|
|
|
root_dir = os.path.join('js', 'src', 'jit')
|
|
|
|
for dirpath, dirnames, filenames in os.walk(root_dir):
|
|
|
|
for filename in filenames:
|
2017-08-24 01:21:16 +03:00
|
|
|
if 'MacroAssembler' not in filename:
|
|
|
|
continue
|
2015-07-09 15:35:29 +03:00
|
|
|
|
2018-04-19 14:02:00 +03:00
|
|
|
filepath = os.path.join(dirpath, filename).replace('\\', '/')
|
|
|
|
|
|
|
|
if filepath.endswith('MacroAssembler.h'):
|
2018-05-22 01:01:01 +03:00
|
|
|
decls = append_signatures(
|
|
|
|
decls, get_macroassembler_declaration(filepath))
|
|
|
|
defs = append_signatures(
|
|
|
|
defs, get_macroassembler_definitions(filepath))
|
2017-05-19 02:06:49 +03:00
|
|
|
|
2018-04-19 14:02:00 +03:00
|
|
|
if not decls or not defs:
|
|
|
|
raise Exception("Did not find any definitions or declarations")
|
2015-07-09 15:35:29 +03:00
|
|
|
|
|
|
|
# Compare declarations and definitions output.
|
|
|
|
difflines = difflib.unified_diff(generate_file_content(decls),
|
|
|
|
generate_file_content(defs),
|
|
|
|
fromfile='check_macroassembler_style.py declared syntax',
|
|
|
|
tofile='check_macroassembler_style.py found definitions')
|
|
|
|
ok = True
|
|
|
|
for diffline in difflines:
|
|
|
|
ok = False
|
|
|
|
print(diffline, end='')
|
|
|
|
|
|
|
|
return ok
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
ok = check_style()
|
|
|
|
|
|
|
|
if ok:
|
|
|
|
print('TEST-PASS | check_macroassembler_style.py | ok')
|
|
|
|
else:
|
2017-09-15 19:14:41 +03:00
|
|
|
print('TEST-UNEXPECTED-FAIL | check_macroassembler_style.py | actual output does not match expected output; diff is above') # noqa: E501
|
2015-07-09 15:35:29 +03:00
|
|
|
|
|
|
|
sys.exit(0 if ok else 1)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|