summaryrefslogtreecommitdiff
path: root/tools/gen_class.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/gen_class.py')
-rwxr-xr-xtools/gen_class.py123
1 files changed, 80 insertions, 43 deletions
diff --git a/tools/gen_class.py b/tools/gen_class.py
index 866576c..b52871e 100755
--- a/tools/gen_class.py
+++ b/tools/gen_class.py
@@ -16,6 +16,12 @@ custom implementations, passed the full argument list and the receiver —
the stub instance the method was called on, null for statics) before falling
back to a real no-arg instance of the declared return type.
+With --no-log the stubs skip KotlinProxyLog entirely: the loaded()/call()
+logging is dropped, and reference-return methods ask
+kotlin.proxy.KotlinProxyNoLog.returnValue (same signature, no logging)
+instead of KotlinProxyLog.returnValue. The emitted classes then reference
+kotlin.proxy.KotlinProxyNoLog only.
+
The optional Signature attribute is OMITTED entirely, so there are no
generics in the emitted class files and therefore no generics clashes.
The JVM links by name + erased descriptor, both of which are copied
@@ -26,7 +32,7 @@ needed.
Usage:
python3 gen_class.py --scan-jars <list> --def-jars <list> --out <dir>
- [--fallback-jars <list>] [--report <file>]
+ [--fallback-jars <list>] [--report <file>] [--no-log]
"""
import argparse
@@ -38,6 +44,7 @@ import zipfile
MAGIC = 0xCAFEBABE
MAJOR, MINOR = 52, 0 # Java 8
KPL = "kotlin/proxy/KotlinProxyLog"
+KNPL = "kotlin/proxy/KotlinProxyNoLog"
OBJ = "java/lang/Object"
CALL_DESC = "(Ljava/lang/String;Ljava/lang/String;)V"
CALL_ARG_DESC = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Object;)V"
@@ -435,30 +442,35 @@ def new_instance_target(desc):
return internal
-def log_body(cp, member, dotname, desc, is_static, call_ref):
- """Straight-line: log the call (with the first reference param, when
- there is one), produce the return value, return.
+def log_body(cp, member, dotname, desc, is_static, call_ref, no_log):
+ """Straight-line: (unless no_log) log the call (with the first reference
+ param, when there is one), produce the return value, return.
- Reference-class returns ask KotlinProxyLog.returnValue, which dispatches
- to KotlinProxyOverrides.invoke (per-method custom implementations, passed
- the full argument list and the receiver — aload_0, or null for statics)
- and falls back to materializing a real instance of the declared return
- type — so the daemon can proceed past e.g. createArguments() into
- doExecute() with a populated args object."""
- slot, has_arg = first_param_ref_slot(desc, is_static)
- if has_arg:
- call_arg_ref = cp.methodref(KPL, "call", CALL_ARG_DESC)
- b = ldc(cp.string(member)) + ldc(cp.string(dotname)) + aload(slot) \
- + bytes([0xb8]) + struct.pack(">H", call_arg_ref)
- max_stack = 3
- else:
- b = ldc(cp.string(member)) + ldc(cp.string(dotname)) \
- + bytes([0xb8]) + struct.pack(">H", call_ref)
- max_stack = 2
+ Reference-class returns ask KotlinProxyLog.returnValue — or
+ KotlinProxyNoLog.returnValue in --no-log mode, which skips the logging —
+ which dispatches to KotlinProxyOverrides.invoke (per-method custom
+ implementations, passed the full argument list and the receiver —
+ aload_0, or null for statics) and falls back to materializing a real
+ instance of the declared return type — so the daemon can proceed past
+ e.g. createArguments() into doExecute() with a populated args object."""
+ b = b""
+ max_stack = 0
+ if not no_log:
+ slot, has_arg = first_param_ref_slot(desc, is_static)
+ if has_arg:
+ call_arg_ref = cp.methodref(KPL, "call", CALL_ARG_DESC)
+ b = ldc(cp.string(member)) + ldc(cp.string(dotname)) + aload(slot) \
+ + bytes([0xb8]) + struct.pack(">H", call_arg_ref)
+ max_stack = 3
+ else:
+ b = ldc(cp.string(member)) + ldc(cp.string(dotname)) \
+ + bytes([0xb8]) + struct.pack(">H", call_ref)
+ max_stack = 2
ti = new_instance_target(desc)
if ti is not None:
# [argsArray, member, owner, returnType, instance] -> returnValue(...)
- rv = cp.methodref(KPL, "returnValue", RETURN_VALUE_DESC)
+ rv = cp.methodref(KNPL if no_log else KPL, "returnValue",
+ RETURN_VALUE_DESC)
b += args_array_body(cp, desc, is_static)
b += ldc(cp.string(member)) + ldc(cp.string(dotname)) \
+ ldc(cp.string(ti.replace("/", ".")))
@@ -468,17 +480,22 @@ def log_body(cp, member, dotname, desc, is_static, call_ref):
# the verifier is happy (null passes checkcast, so un-instantiable
# types still just return null).
b += bytes([0xc0]) + struct.pack(">H", cp.cls(ti))
- max_stack = 5
+ max_stack = max(max_stack, 5)
else:
push, _ = ret_bytes(desc)
b += push
+ r = desc[desc.rindex(")") + 1:]
+ # With no_log there is no call() on the stack, so the default push
+ # itself sets the peak: 2 for J/D, 1 for other non-void, 0 for void.
+ max_stack = max(max_stack, 2 if r in "JD" else (1 if r != "V" else 0))
_, ret = ret_bytes(desc)
b += ret
max_locals = count_locals(desc, is_static)
return code_attr(cp, max_stack, max_locals, b)
-def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum):
+def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum,
+ no_log):
# Enums: java.lang.Enum has no no-arg ctor, so the super call must be
# Enum.<init>(String name, int ordinal). The enum ctor's own name/ordinal
# params (last two) are ignored — we pass constants; unused params are
@@ -491,18 +508,27 @@ def ctor_body(cp, super_name, member, dotname, desc, call_ref, is_enum):
max_stack = 3
else:
sup = cp.methodref(super_name, "<init>", "()V")
- max_stack = 2
+ # with no_log there is no trailing call(), so only aload_0 is live
+ max_stack = 2 if not no_log else 1
b += bytes([0xb7]) + struct.pack(">H", sup) # invokespecial
- b += ldc(cp.string(member)) + ldc(cp.string(dotname))
- b += bytes([0xb8]) + struct.pack(">H", call_ref)
+ if not no_log:
+ b += ldc(cp.string(member)) + ldc(cp.string(dotname))
+ b += bytes([0xb8]) + struct.pack(">H", call_ref)
b += b"\xb1" # return
return code_attr(cp, max_stack, count_locals(desc, False), b)
-def clinit_body(cp, dotname, loaded_ref, internal, companion=None):
- b = ldc(cp.string(dotname))
- b += bytes([0xb8]) + struct.pack(">H", loaded_ref)
- max_stack = 2
+def clinit_body(cp, dotname, loaded_ref, internal, companion=None,
+ no_log=False):
+ b = b""
+ if no_log:
+ # no KotlinProxyLog.loaded() — the body is the companion init (or just
+ # return) only.
+ max_stack = 0
+ else:
+ b = ldc(cp.string(dotname))
+ b += bytes([0xb8]) + struct.pack(">H", loaded_ref)
+ max_stack = 2
if companion:
# new; dup; aconst_null -> 3 on the stack before invokespecial pops
max_stack = 3
@@ -583,7 +609,7 @@ def field_bytes(cp, name, desc, access):
# ----------------------------------------------------------------------
# class emission
# ----------------------------------------------------------------------
-def emit_class(cp, name, info, is_iface, dotname, companion=None):
+def emit_class(cp, name, info, is_iface, dotname, companion=None, no_log=False):
this_idx = cp.cls(name)
super_name = (info.get("super") or OBJ) if not is_iface else OBJ
super_idx = cp.cls(super_name)
@@ -596,8 +622,11 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
if name == OBJ:
return None
- call_ref = cp.methodref(KPL, "call", CALL_DESC)
- loaded_ref = cp.methodref(KPL, "loaded", LOADED_DESC)
+ # With no_log the stubs never touch KotlinProxyLog, so keep those methodrefs
+ # out of the constant pool entirely (only KotlinProxyNoLog is referenced,
+ # lazily, from the reference-return bodies).
+ call_ref = None if no_log else cp.methodref(KPL, "call", CALL_DESC)
+ loaded_ref = None if no_log else cp.methodref(KPL, "loaded", LOADED_DESC)
fields = []
for f in info.get("fields", []):
@@ -627,7 +656,7 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
if md == "()V":
has_noarg = True
body = ctor_body(cp, super_name, mn, dotname, md, call_ref,
- is_enum)
+ is_enum, no_log)
methods.append(method_bytes(cp, mn, md, ma, body))
elif (ma & A_BRIDGE) and not md[1:md.rindex(")")] \
and mn in concrete_noparam:
@@ -640,21 +669,22 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
methods.append(method_bytes(cp, mn, md, ma, None))
else:
body = log_body(cp, mn, dotname, md,
- bool(ma & A_STATIC), call_ref)
+ bool(ma & A_STATIC), call_ref, no_log)
methods.append(method_bytes(cp, mn, md, ma, body))
if not is_iface:
# guarantee a no-arg ctor so child stubs can invokespecial super()
if not has_noarg:
body = ctor_body(cp, super_name, "<init>", dotname, "()V",
- call_ref, is_enum)
+ call_ref, is_enum, no_log)
methods.append(method_bytes(cp, "<init>", "()V",
A_PUBLIC, body))
- # synthesized static initializer -> KotlinProxyLog.loaded(name),
- # plus the companion-object init when the class has one
+ # synthesized static initializer -> KotlinProxyLog.loaded(name)
+ # (skipped with --no-log), plus the companion-object init when the
+ # class has one
methods.append(method_bytes(cp, "<clinit>", "()V",
A_STATIC | A_PRIVATE,
clinit_body(cp, dotname, loaded_ref,
- name, companion)))
+ name, companion, no_log)))
out = bytearray()
out += struct.pack(">IHH", MAGIC, MINOR, MAJOR)
@@ -675,14 +705,15 @@ def emit_class(cp, name, info, is_iface, dotname, companion=None):
return bytes(out)
-def emit_minimal(cp, name, is_iface):
+def emit_minimal(cp, name, is_iface, no_log=False):
"""A class/interface with no definition found: bare structure +
(for classes) a no-arg ctor and a loaded() clinit."""
info = {"access": A_PUBLIC, "super": OBJ, "interfaces": [],
"fields": [], "methods": []}
if is_iface:
info["access"] |= A_INTERFACE | A_ABSTRACT
- return emit_class(cp, name, info, is_iface, name.replace("/", "."))
+ return emit_class(cp, name, info, is_iface, name.replace("/", "."),
+ no_log=no_log)
# ----------------------------------------------------------------------
@@ -712,6 +743,11 @@ def main():
"the seed set only. Use with --seed to stub just the "
"compiler entry point; its super/interfaces are then "
"resolved from the real jars at load time")
+ ap.add_argument("--no-log", action="store_true",
+ help="do not call KotlinProxyLog: drop the loaded()/call() "
+ "logging and route reference returns through "
+ "KotlinProxyNoLog.returnValue instead (the stubs then "
+ "reference kotlin.proxy.KotlinProxyNoLog only)")
args = ap.parse_args()
exclude = [p for p in (args.exclude.split(",") if args.exclude else [])
@@ -826,14 +862,15 @@ def main():
cp = CP()
if e is None:
is_iface = kind_hint.get(name) == "interface"
- data = emit_minimal(cp, name, is_iface)
+ data = emit_minimal(cp, name, is_iface, args.no_log)
minimal.append(name)
n_minimal += 1
else:
_, info = e
is_iface = bool(info["access"] & A_INTERFACE)
companion = None if is_iface else companion_init(name, info, defs)
- data = emit_class(cp, name, info, is_iface, dot, companion)
+ data = emit_class(cp, name, info, is_iface, dot, companion,
+ args.no_log)
n_full += 1
if is_iface:
n_iface += 1