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
|
|
|
|
2019-07-08 20:30:34 +03:00
|
|
|
from __future__ import absolute_import
|
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()
|
2018-11-28 17:27:35 +03:00
|
|
|
# Remove new-line induced spaces after opening braces.
|
|
|
|
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} for a in archs]
|
2015-07-09 15:35:29 +03:00
|
|
|
|
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).union(
|
2017-06-29 21:49:10 +03:00
|
|
|
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
|
|
|
|
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:
|
2018-11-28 17:27:35 +03:00
|
|
|
if style_section:
|
|
|
|
raise "check_macroassembler_style section already opened."
|
2015-07-09 15:35:29 +03:00
|
|
|
style_section = True
|
2018-11-28 17:27:35 +03:00
|
|
|
braces_depth = 0
|
2015-07-09 15:35:29 +03:00
|
|
|
elif "//}}} check_macroassembler_style" in line:
|
|
|
|
style_section = False
|
|
|
|
if not style_section:
|
|
|
|
continue
|
|
|
|
|
2019-01-28 03:21:41 +03:00
|
|
|
# Ignore preprocessor directives.
|
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
|
2018-11-28 17:27:35 +03:00
|
|
|
# Remove comments from the processed line.
|
2015-07-09 15:35:29 +03:00
|
|
|
line = re.sub(r"//.*", "", line)
|
2018-11-28 17:27:35 +03:00
|
|
|
|
|
|
|
# Locate and count curly braces.
|
|
|
|
open_curly_brace = line.find("{")
|
|
|
|
was_braces_depth = braces_depth
|
|
|
|
braces_depth = braces_depth + line.count("{") - line.count("}")
|
|
|
|
|
|
|
|
# Raise an error if the check_macroassembler_style macro is used
|
|
|
|
# across namespaces / classes scopes.
|
|
|
|
if braces_depth < 0:
|
|
|
|
raise "check_macroassembler_style annotations are not well scoped."
|
|
|
|
|
|
|
|
# If the current line contains an opening curly brace, check if
|
|
|
|
# this line combines with the previous one can be identified as a
|
|
|
|
# MacroAssembler function signature.
|
|
|
|
if open_curly_brace != -1 and was_braces_depth == 0:
|
|
|
|
lines = lines + line[:open_curly_brace]
|
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))
|
2015-07-09 15:35:29 +03:00
|
|
|
lines = ""
|
|
|
|
continue
|
2018-11-28 17:27:35 +03:00
|
|
|
|
2018-11-28 17:27:35 +03:00
|
|
|
# We do not aggregate any lines if we are scanning lines which are
|
|
|
|
# in-between a set of curly braces.
|
|
|
|
if braces_depth > 0:
|
2018-11-29 17:03:08 +03:00
|
|
|
continue
|
2018-11-28 17:27:35 +03:00
|
|
|
if was_braces_depth != 0:
|
|
|
|
line = line[line.rfind("}") + 1 :]
|
|
|
|
|
|
|
|
# This logic is used to remove template instantiation, static
|
|
|
|
# variable definitions and function declaration from the next
|
|
|
|
# function definition.
|
|
|
|
last_semi_colon = line.rfind(";")
|
|
|
|
if last_semi_colon != -1:
|
2018-11-29 17:03:08 +03:00
|
|
|
lines = ""
|
2018-11-28 17:27:35 +03:00
|
|
|
line = line[last_semi_colon + 1 :]
|
|
|
|
|
|
|
|
# Aggregate lines of non-braced text, which corresponds to the space
|
|
|
|
# where we are expecting to find function definitions.
|
|
|
|
lines = lines + line
|
2015-07-09 15:35:29 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-01-28 03:21:41 +03:00
|
|
|
# Ignore preprocessor directives.
|
|
|
|
if line.startswith("#"):
|
|
|
|
continue
|
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
line = re.sub(r"//.*", "", line)
|
2018-11-28 17:27:35 +03:00
|
|
|
if len(line.strip()) == 0 or "public:" in line or "private:" in line:
|
2015-07-09 15:35:29 +03:00
|
|
|
lines = ""
|
|
|
|
continue
|
2019-01-28 03:21:41 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
lines = lines + line
|
2018-11-28 17:27:35 +03:00
|
|
|
|
2015-07-09 15:35:29 +03:00
|
|
|
# Continue until we have a complete declaration
|
|
|
|
if ";" not in lines:
|
|
|
|
continue
|
2018-11-28 17:27:35 +03:00
|
|
|
|
|
|
|
# Skip member declarations: which are lines ending with a
|
|
|
|
# semi-colon without any list of arguments.
|
2015-07-09 15:35:29 +03:00
|
|
|
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)
|
2020-10-26 21:21:44 +03:00
|
|
|
)
|
2018-05-22 01:01:01 +03:00
|
|
|
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()
|