diff --git a/doc/jit-plan.txt b/doc/jit-plan.txt index 5118e09a48..9d71cf576a 100644 --- a/doc/jit-plan.txt +++ b/doc/jit-plan.txt @@ -1,32 +1,32 @@ From Cam's email on Mar 2025 -[ ] Mixin vs Annotation — also: Exception +[X] Mixin vs Annotation — also: Exception [ ] Service API review, also const [ ] make/immutable review -[ ] Gold/types -[ ] mixin design! -[ ] AST vs byte code design +[-] Gold/types +[X] mixin design! +[X] AST vs byte code design [ ] GC & RAM usage design [ ] Fiber <-> virtual thread design [ ] Future design -[ ] String design & review — graphemes, normalization +[X] String design & review — graphemes, normalization [ ] Struct vs Object, also Public/RO references and +/- types/masking [ ] Unions -[ ] Immutable Objects -[ ] Properties -[ ] Calling convention!!! -[ ] Property/Field layout +[X] Immutable Objects +[X] Properties +[X] Calling convention!!! +[X] Property/Field layout [ ] Container design & lifecycle/masking [ ] Bin code caching/storage/JIT/optJIT [ ] Safepoints — how to include & what do they do? [ ] CPU measure -[ ] Intrinsics — how to design "the divide" +[X] Intrinsics — how to design "the divide" [ ] Service boundary design & automation [ ] Class, Type, Generics [ ] Specialization -[ ] default args +[X] default args [x] Construction — 7 steps (BaseTemplate.Construct proceed) -[ ] Enum | Enumeration type design +[X] Enum | Enumeration type design Review eg. Prev & Next [ ] obj persistence (native optimization) [ ] Container persistence (shelf) diff --git a/javatools/src/main/java/org/xvm/asm/MethodStructure.java b/javatools/src/main/java/org/xvm/asm/MethodStructure.java index 41019bf95a..bbcfc9b2bd 100644 --- a/javatools/src/main/java/org/xvm/asm/MethodStructure.java +++ b/javatools/src/main/java/org/xvm/asm/MethodStructure.java @@ -1271,6 +1271,53 @@ public void setConstructFinally(MethodStructure structFinally) { m_idFinally = structFinally.getIdentityConstant(); } + /** + * @return true iff this constructor or a constructor it invokes has a finalizer + */ + public boolean hasConstructorFinalizers() { + assert isConstructor(); + return hasConstructorFinalizers(new HashSet<>()); + } + + /** + * @return true iff this constructor or any constructor it invokes has a finalizer (recursively) + */ + private boolean hasConstructorFinalizers(Set setVisited) { + // constructor cycles are not expected; this guard is simply defensive + if (!setVisited.add(getIdentityConstant())) { + return false; + } + + if (getConstructFinally() != null) { + return true; + } + + if (hasCode()) { + Constant[] aconst = getLocalConstants(); + for (Op op : getOps()) { + op = op.ensureOp(); + + switch (op.getOpCode()) { + case Op.OP_CONSTR_0, Op.OP_CONSTR_1, Op.OP_CONSTR_N, Op.OP_CONSTR_T: + int nCtorId = ((OpCallable) op).getFunctionId(); + assert nCtorId <= Op.CONSTANT_OFFSET; + + MethodConstant idCtor = (MethodConstant) aconst[Op.CONSTANT_OFFSET - nCtorId]; + MethodStructure ctor = (MethodStructure) idCtor.getComponent(); + + if (ctor.hasConstructorFinalizers(setVisited)) { + return true; + } + break; + + default: + break; + } + } + } + return false; + } + /** * @return true iff this method has code */ diff --git a/javatools/src/main/java/org/xvm/asm/OpCallable.java b/javatools/src/main/java/org/xvm/asm/OpCallable.java index db7b012db3..2aaa706f89 100644 --- a/javatools/src/main/java/org/xvm/asm/OpCallable.java +++ b/javatools/src/main/java/org/xvm/asm/OpCallable.java @@ -152,12 +152,15 @@ public void registerConstants(ConstantRegistry registry) { public String toString() { return super.toString() + ' ' + getFunctionString() + '(' + getParamsString() + ") -> " + getReturnsString(); } + protected String getFunctionString() { return Argument.toIdString(m_argFunction, m_nFunctionId); } + protected String getParamsString() { return ""; } + protected static String getParamsString(int[] anArgValue, Argument[] aArgValue) { StringBuilder sb = new StringBuilder(); int cArgNums = anArgValue == null ? 0 : anArgValue.length; @@ -171,6 +174,7 @@ protected static String getParamsString(int[] anArgValue, Argument[] aArgValue) } return sb.toString(); } + protected String getReturnsString() { if (m_anRetValue != null || m_aArgReturn != null) { // multi-return @@ -194,6 +198,13 @@ protected String getReturnsString() { // ----- helper methods ------------------------------------------------------------------------ + /** + * @return the encoded function argument id + */ + public int getFunctionId() { + return m_nFunctionId; + } + /** * This Op holds a constant for the constructor of a child of the compile-time parent. * The run-time type of the parent could extend the compile-time type and that parent @@ -824,7 +835,7 @@ protected int buildCall(BuildContext bctx, CodeBuilder code, int[] anArgValue) { boolean fTuple = isTupleReturn(); if (fTuple) { - jmdCall = jmdCall.standardOnly(); + jmdCall = jmdCall.asStandardOnly(); } int[] anRet = isMultiReturn() @@ -888,7 +899,7 @@ protected int buildCall(BuildContext bctx, CodeBuilder code, int[] anArgValue) { } if (isTupleReturn()) { - jmdCall = jmdCall.standardOnly(); + jmdCall = jmdCall.asStandardOnly(); } MethodTypeDesc mdCall; diff --git a/javatools/src/main/java/org/xvm/asm/OpInvocable.java b/javatools/src/main/java/org/xvm/asm/OpInvocable.java index 01f13f0ae6..ad873a270b 100644 --- a/javatools/src/main/java/org/xvm/asm/OpInvocable.java +++ b/javatools/src/main/java/org/xvm/asm/OpInvocable.java @@ -492,15 +492,9 @@ private void buildSingleInvoke(BuildContext bctx, CodeBuilder code, ClassDesc cd call = bctx.builder.resolvePrivateDetachedCall(infoMethod, typeInvoke); } - JitMethodDesc jmd; - if (call == null) { - jmd = infoMethod.getJitDesc(bctx.builder, typeInvoke); - } else { - jmd = call.jmd(); - code.checkcast(call.receiverCD() == null ? call.owner() : call.receiverCD()); - } - - String methodName = infoMethod.ensureJitMethodName(bctx.typeSystem); + JitMethodDesc jmd = call == null + ? infoMethod.getJitDesc(bctx.builder, typeInvoke) + : call.jmd(); if (isTupleReturn()) { if (jmd.isPrimitivized() && fUnboxed) { @@ -508,7 +502,7 @@ private void buildSingleInvoke(BuildContext bctx, CodeBuilder code, ClassDesc cd cdInvoke = bctx.builder.getCCDesc(typeInvoke); fUnboxed = false; } - jmd = jmd.standardOnly(); + jmd = jmd.asStandardOnly(); } boolean fOptimized = jmd.isOptimized; @@ -521,11 +515,17 @@ private void buildSingleInvoke(BuildContext bctx, CodeBuilder code, ClassDesc cd Builder.unbox(code, typeInvoke); } - if (!isPropCall && (bodyHead.getIdentity().getNestedDepth() > 2 || - infoMethod.getTypeInfo() == null)) { - // nested and private mixin methods absent from TypeInfo need to be built here; - // the second check is based on the synthetic info produced by computeMethodInfo() - bctx.buildMethod(methodName, bodyHead); + String methodName = infoMethod.ensureJitMethodName(bctx.typeSystem); + TypeInfo infoOwner = infoMethod.getTypeInfo(); + if (!isPropCall) { + IdentityConstant idNamespace = bodyHead.getIdentity().getNamespace(); + if (infoOwner == null || + idNamespace.isNested() && + !idNamespace.equals(infoOwner.getType().getDefiningConstant())) { + // direct members are assembled by their type's method loop; nested methods + // and methods absent from TypeInfo are assembled on demand + bctx.buildMethod(methodName, bodyHead); + } } MethodTypeDesc md; diff --git a/javatools/src/main/java/org/xvm/asm/constants/AccessTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/AccessTypeConstant.java index 898009de9a..d738c3a40d 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/AccessTypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/AccessTypeConstant.java @@ -210,6 +210,11 @@ public String getJitICName(TypeSystem ts) { return m_constType.getJitICName(ts); } + @Override + public String getJitImplName(TypeSystem ts) { + return m_constType.getJitImplName(ts); + } + // ----- Constant methods ---------------------------------------------------------------------- @Override diff --git a/javatools/src/main/java/org/xvm/asm/constants/ClassConstant.java b/javatools/src/main/java/org/xvm/asm/constants/ClassConstant.java index e39f6a8a56..3451b5cb37 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/ClassConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/ClassConstant.java @@ -300,13 +300,15 @@ public boolean isClass() { @Override public TypeConstant getType() { - ClassStructure clz = (ClassStructure) getComponent(); + ConstantPool pool = getConstantPool(); + ClassStructure clz = (ClassStructure) getComponent(); - return clz.isVirtualChild() ? getConstantPool().ensureVirtualChildTypeConstant( + // an inner class's outer is the containing class, not an intervening method or property + return clz.isVirtualChild() ? pool.ensureVirtualChildTypeConstant( getParentConstant().getType(), getName()) - : clz.isInnerChild() ? getConstantPool().ensureInnerChildTypeConstant( - getParentConstant().getType(), this) - : clz.isAnonInnerClass() ? getConstantPool().ensureAnonymousClassTypeConstant( + : clz.isInnerChild() ? pool.ensureInnerChildTypeConstant( + getParentConstant().getClassIdentity().getFormalType(), this) + : clz.isAnonInnerClass() ? pool.ensureAnonymousClassTypeConstant( getParentConstant().getType(), this) : super.getType(); } diff --git a/javatools/src/main/java/org/xvm/asm/constants/PropertyClassTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/PropertyClassTypeConstant.java index 26b90f1411..e54e3eeaa9 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/PropertyClassTypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/PropertyClassTypeConstant.java @@ -71,6 +71,14 @@ public class PropertyClassTypeConstant * @param idProp the property id */ public PropertyClassTypeConstant(ConstantPool pool, TypeConstant typeParent, PropertyConstant idProp) { + this(pool, typeParent, idProp, false); + } + + /** + * Internal constructor. + */ + private PropertyClassTypeConstant(ConstantPool pool, TypeConstant typeParent, + PropertyConstant idProp, boolean fNativeRebase) { super(pool, typeParent); // unlike VirtualChildConstant, it's never unresolved @@ -81,7 +89,8 @@ public PropertyClassTypeConstant(ConstantPool pool, TypeConstant typeParent, Pro throw new IllegalArgumentException("property is required"); } - m_idProp = idProp; + m_idProp = idProp; + f_fNativeRebase = fNativeRebase; } /** @@ -97,7 +106,8 @@ public PropertyClassTypeConstant(ConstantPool pool, Format format, DataInput in) throws IOException { super(pool, format, in); - m_iProp = readIndex(in); + m_iProp = readIndex(in); + f_fNativeRebase = false; } @Override @@ -134,7 +144,33 @@ public PropertyInfo getPropertyInfo() { * @return the property ref type */ public TypeConstant getRefType() { - return getPropertyInfo().getBaseRefType(); + PropertyInfo info = getPropertyInfo(); + TypeConstant type = info.getBaseRefType(); + + return f_fNativeRebase + ? info.getType().asNativeRef(getConstantPool(), info.isVar()) + .adoptAnnotations(getConstantPool(), type) + : type; + } + + /** + * @return the runtime-only property type based on the native Ref/Var implementation + */ + public PropertyClassTypeConstant asNativeRebase() { + return f_fNativeRebase + ? this + : getConstantPool().register(new PropertyClassTypeConstant( + getConstantPool(), m_typeParent, m_idProp, true)); + } + + /** + * @return the property type based on the ordinary Ref/Var interface + */ + public PropertyClassTypeConstant asStandardRef() { + return f_fNativeRebase + ? getConstantPool().register(new PropertyClassTypeConstant( + getConstantPool(), m_typeParent, m_idProp, false)) + : this; } // ----- TypeConstant methods ------------------------------------------------------------------ @@ -156,7 +192,9 @@ public Constant getDefiningConstant() { @Override protected TypeConstant cloneSingle(ConstantPool pool, TypeConstant type) { - return pool.ensurePropertyClassTypeConstant(type, m_idProp); + PropertyClassTypeConstant typeClone = (PropertyClassTypeConstant) + pool.ensurePropertyClassTypeConstant(type, m_idProp); + return f_fNativeRebase ? typeClone.asNativeRebase() : typeClone; } @Override @@ -267,8 +305,10 @@ protected Usage checkProduction(String sTypeName, Access access, List listExplode = new ArrayList<>(); PropertyStructure structProp = infoProp.getHead().getStructure(); - collectChildInfo(idBase, false, structProp, mapTypeParams, mapContribProps, - mapContribMethods, mapContribChildren, listExplode, mapVirtProps, - 0, 0, errs); + + // collect the property's own members as ordinary Ref/Var declarations + IdentityConstant idRef = idBase instanceof NativeRebaseConstant idNative + ? idNative.getClassConstant() + : idBase; + collectChildInfo(idRef, false, structProp, mapTypeParams, mapContribProps, + mapContribMethods, mapContribChildren, listExplode, mapVirtProps, 0, 0, errs); for (PropertyConstant idProp : listExplode) { PropertyInfo propExplode = mapContribProps.remove(idProp); @@ -343,7 +388,7 @@ protected TypeInfo buildTypeInfo(ErrorListener errs) { if (mapContribProps.isEmpty() && mapContribMethods.isEmpty() && mapContribChildren.isEmpty()) { // nothing has been added - return infoBase; + continue; } if (!mapContribProps.isEmpty()) { @@ -401,10 +446,10 @@ protected TypeInfo buildTypeInfo(ErrorListener errs) { } } - return new TypeInfoReal(this, cInvals, infoBase.getClassStructure(), + return new TypeInfoReal(ensureAccess(Access.PRIVATE), cInvals, infoBase.getClassStructure(), idBase.getNestedDepth() + 1, false, mapTypeParams, Annotation.NO_ANNOTATIONS, infoBase.getMixinAnnotations(), typeBase, null, null, - Collections.emptyList(), ListMap.empty(), ListMap.empty(), + infoBase.getContributionList(), infoBase.getClassChain(), infoBase.getDefaultChain(), mapProps, mapMethods, mapVirtProps, mapVirtMethods, mapChildren, null, TypeInfo.Progress.Complete); } @@ -413,7 +458,7 @@ protected TypeInfo buildTypeInfo(ErrorListener errs) { @Override public TypeConstant getJitCCType() { - return getRefType().getJitCCType(); + return this; } @Override @@ -421,10 +466,25 @@ public TypeConstant getJitICType() { return this; } + @Override + public String getJitCCName(TypeSystem ts) { + return f_fNativeRebase ? getJitICName(ts) : getRefType().getJitCCName(ts); + } + + @Override + public boolean isJitL2Specialized() { + return getRefType().isJitL2Specialized(); + } + + @Override + public boolean isJitAnnotatedRef() { + return getRefType().isJitAnnotatedRef(); + } + @Override public String getJitICName(TypeSystem ts) { - ModuleLoader loader = ts.findOwnerLoader(getParentType()); - TypeConstant type = loader.typeSystem.pool().register(this); + ModuleLoader loader = ts.findOwnerLoader(getParentType()); + TypeConstant type = loader.typeSystem.pool().register(asStandardRef()); StringBuilder sbName = new StringBuilder(getProperty().getClassJitName(ts)); TypeSystem.appendJitSuffix(sbName, type.getPosition()); @@ -477,13 +537,16 @@ protected int compareDetails(Constant obj) { } n = this.m_idProp.compareTo(that.m_idProp); + if (n == 0) { + n = Boolean.compare(this.f_fNativeRebase, that.f_fNativeRebase); + } } return n; } @Override public String getValueString() { - return m_typeParent.getValueString() + '.' + m_idProp.getName(); + return getRefType().getValueString(); } // ----- XvmStructure methods ------------------------------------------------------------------ @@ -498,6 +561,9 @@ protected void registerConstants(ConstantPool pool) { @Override protected void assemble(DataOutput out) throws IOException { + if (f_fNativeRebase) { + throw new IllegalStateException("Native property class types cannot be serialized"); + } super.assemble(out); writeMagnitude(out, m_idProp.getPosition()); @@ -508,7 +574,8 @@ protected void assemble(DataOutput out) @Override protected int computeHashCode() { return Hash.of(m_typeParent, - Hash.of(m_idProp)); + Hash.of(m_idProp, + Hash.of(f_fNativeRebase))); } // ----- data fields --------------------------------------------------------------------------- @@ -527,4 +594,9 @@ protected int computeHashCode() { * Cached property info. */ private transient PropertyInfo m_info; + + /** + * Indicates that the property type uses the native Ref/Var implementation at runtime. + */ + private final transient boolean f_fNativeRebase; } diff --git a/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java b/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java index 4c0f38fc19..b9f63b41cb 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java +++ b/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java @@ -30,6 +30,8 @@ import org.xvm.javajit.JitMethodDesc; import org.xvm.javajit.TypeSystem; +import org.xvm.javajit.builders.AnnotationHelper; + import org.xvm.util.Handy; import org.xvm.util.Severity; @@ -1385,11 +1387,20 @@ private ConstantPool pool() { // ----- JIT support --------------------------------------------------------------------------- /** - * @return true iff the JIT requires a field that stores a Ref object for this property + * @return true iff the JIT requires Ref storage for this property, on the containing object + * or in the container for a static property */ public boolean isJitInflated() { // TODO: handle Future in a later phase; keep its stored-Ref implementation for now - return containsRefAnnotation(pool().clzFuture()); + return isConstant() && isJitRefAnnotated() || + containsRefAnnotation(pool().clzFuture()); + } + + /** + * @return true iff the property contains any runtime reference annotations + */ + public boolean isJitRefAnnotated() { + return isRefAnnotated() && AnnotationHelper.hasRuntimeAnnotations(getRefAnnotations()); } /** diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java index fa2a018b02..d7d0900458 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java @@ -2267,7 +2267,7 @@ private TypeInfo buildTypeInfoImpl(ErrorListener errs) { } if (typeUnderlying instanceof PropertyClassTypeConstant typePropClass) { - return typePropClass.buildTypeInfo(errs); + return typePropClass.buildPrivateInfo(errs); } // this implementation only deals with modifying (not including immutable) and terminal type @@ -3621,7 +3621,7 @@ private boolean computeIncomplete(Composition composition, TypeConstant typeCont // if we're building Object or some interface that Object depends on, then we ignore // chicken-and-egg problems because the Object interface should still be able to be // successfully and correctly built; specifically, Object depends on Comparable, - // but in reality, Comparable "is a" Object, so we need to solve the chicken-and-egg + // but in reality, Comparable "isA" Object, so we need to solve the chicken-and-egg // problem by allowing the creation of Object to proceed return false; } @@ -7546,7 +7546,8 @@ public boolean isJitAssignableTo(TypeConstant that) { // likewise, for an annotation A into C: // Ecstasy: A --into--> C, so A.isA(C), but // Java: JCC(A) is not a subtype of JCC(C); A -> C requires a checkcast - boolean fIntoCast = Builder.isEmbedded(typeThisJit.getExplicitClassFormat()) && + boolean fIntoCast = typeThisJit.isExplicitClassIdentity(true) && + Builder.isEmbedded(typeThisJit.getExplicitClassFormat()) && typeThisJit.getExplicitClassInto(true).isA(typeThatJit); return typeThisJit.equals(typeThatJit) || !fIntoCast && @@ -7561,7 +7562,8 @@ public boolean isJitAssignableTo(TypeConstant that) { * @return true iff this type's JCC is a Java interface */ public boolean isJitCCInterface() { - return isJitImplInterface() || isDerivedVirtualChildClass(); + TypeConstant typeJit = getJitCCType(); + return typeJit.isJitImplInterface() || typeJit.isDerivedVirtualChildClass(); } /** @@ -7573,7 +7575,8 @@ public boolean isJitImplInterface() { TypeConstant typeJit = getJitCCType(); return (typeJit.isInterfaceType() || Builder.isEmbedded(typeJit.getExplicitClassFormat())) && !typeJit.isTuple() - && !typeJit.isTypeOfType(); + && !typeJit.isTypeOfType() + && !typeJit.isNativeRef(); } /** @@ -7616,8 +7619,9 @@ public JitTypeDesc getJitDesc(Builder builder) { * the corresponding type. It's the minimal (the widest) type that produces the same "JIT Call * Class Name' CC(T). * - *

The canonical type C(T) is always a {@link #isSingleUnderlyingClass single underlying - * class} that could parameterized by non-parameterized callable JIT types as parameters. + *

For class types, CC(T) is a {@link #isSingleUnderlyingClass single underlying class} that + * could be parameterized by non-parameterized callable JIT types as parameters. Property class + * types retain their property identity. * *

The following should hold : *