vmtype.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,918 行 · 第 1/4 页

JAVA
1,918
字号
                    state |= VmTypeState.ST_INVALID;
                    state &= ~VmTypeState.ST_COMPILING;
                    errorMsg = ex.toString();
                    throw new LinkageError("Failed to compile " + name, ex);
                }
                final int declared = getNoDeclaredMethods();
                if (count != declared) {
                    errorMsg = "Compiled skipped some methods ("
                            + (declared - count);
                    throw new LinkageError(errorMsg);
                }

                state &= ~VmTypeState.ST_COMPILING;

                notifyAll();
            }
        }
    }

    /**
     * Verify this type.
     */
    private synchronized void doVerify() {
        if (isVerified()) { return; }
        if (isVerifying()) { throw new Error("Recursive verify in " + getName()); }

        state |= VmTypeState.ST_VERIFYING;

        // Verify the superclass (if any)
        if (superClass != null) {
            superClass.verify();
        }

        // TODO implement verification

        state |= VmTypeState.ST_VERIFIED;
        state &= ~VmTypeState.ST_VERIFYING;

        notifyAll();
    }

    /**
     * Do the prepare action required to instantiate this object
     */
    protected abstract void prepareForInstantiation();

    /**
     * Prepare the virtual method table
     * 
     * @param allInterfaces
     * @return The tib
     */
    protected abstract Object[] prepareTIB(HashSet allInterfaces);

    /**
     * Prepare the interface method table
     * 
     * @param allInterfaces
     * @return The imt builder
     */
    protected abstract IMTBuilder prepareIMT(HashSet allInterfaces);

    /**
     * Create the super classes array for this type.
     * 
     * @param allInterfaces
     *            All interfaces directly or indirectly implemented by this
     *            class
     * @return The super classes array
     */
    protected VmType[] createSuperClassesArray(HashSet allInterfaces) {

        final int length = superClassDepth + 1 + allInterfaces.size();
        final VmType[] array = new VmType[ length];
        array[ 0] = this;
        VmType superPtr = superClass;
        for (int i = 0; i < superClassDepth; i++) {
            array[ superClassDepth - i] = superPtr;
            superPtr = superPtr.getSuperClass();
        }

        int index = superClassDepth + 1;
        for (Iterator i = allInterfaces.iterator(); i.hasNext();) {
            final VmInterfaceClass intfClass = (VmInterfaceClass) i.next();
            array[ index++] = intfClass;
        }

        return array;
    }

    /**
     * Fill the given hashset with all interface implemented by the given type C.
     * 
     * @param all
     *            A HashSet of VmInterfaceClass instances.
     * @param C
     */
    private void getAllInterfaces(HashSet all, VmType C) {
        while (C != null) {
            final VmImplementedInterface[] it = C.interfaceTable;
            if (it != null) {
                int count = it.length;
                for (int i = 0; i < count; i++) {
                    final VmInterfaceClass ic = it[ i].getResolvedVmClass();
                    if (!all.contains(ic)) {
                        all.add(ic);
                        getAllInterfaces(all, ic);
                    }
                }
            }
            C = C.getSuperClass();
        }
    }

    /**
     * Resolve all constant references in the constants pool
     * 
     * @param clc
     */
    public final void resolveCpRefs(VmClassLoader clc) {
        if (!resolvedCpRefs) {
            prepare();
            if (superClass != null) {
                superClass.resolveCpRefs(clc);
            }

            /**
             * Step 2b: Load the classes of my fields
             */
            final int fcnt = getNoDeclaredFields();
            for (int i = 0; i < fcnt; i++) {
                final VmField fs = fieldTable[ i];
                fs.resolve(clc);
            }

            /**
             * Step 2c: Load the classes of my methods
             */
            final int mcnt = getNoDeclaredMethods();
            for (int i = 0; i < mcnt; i++) {
                final VmMethod mts = methodTable[ i];
                mts.resolve(clc);
            }

            VmCP cp = this.cp;
            if (cp != null) {
                for (int i = 0; i < cp.getLength(); i++) {
                    Object obj = cp.getAny(i);
                    if (obj instanceof VmConstObject) {
                        ((VmConstObject) obj).resolve(clc);
                    }
                }
            }
            resolvedCpRefs = true;
        }
    }

    public boolean isCpRefsResolved() {
        return resolvedCpRefs;
    }

    /**
     * Compile all the methods in this class during bootstrapping.
     * 
     * @param compiler
     * @param os
     * @param optLevel
     *            The optimization level
     * @return The number of compiled methods
     */
    public final synchronized int compileBootstrap(NativeCodeCompiler compiler,
            NativeStream os, int optLevel) {
        if (!isPrepared()) { throw new IllegalStateException(
                "VmType must have been prepared"); }
        int rc = 0;
        if (!isCompiled()) {
            final VmMethod[] mt = methodTable;
            if (mt != null) {
                final int count = mt.length;
                for (int i = 0; i < count; i++) {
                    final VmMethod method = mt[ i];
                    //if (optLevel > method.getNativeCodeOptLevel()) {
                    compiler.compileBootstrap(method, os, optLevel);
                    rc++;
                    //method.setModifier(true, Modifier.ACC_COMPILED);
                    //}
                }
            }
            state |= VmTypeState.ST_COMPILED;
        }
        if (arrayClass != null) {
            arrayClass.link();
            rc += arrayClass.compileBootstrap(compiler, os, optLevel);
        }
        return rc;
    }

    /**
     * Compile all the methods in this class during runtime.
     * 
     * @param optLevel
     *            The optimization level
     * @return The number of compiled methods
     */
    public final synchronized int compileRuntime(int optLevel) {
        if (!isPrepared()) { throw new IllegalStateException(
                "VmType must have been prepared"); }
        return doCompileRuntime(optLevel);
    }

    /**
     * Compile all the methods in this class during runtime.
     * 
     * @param optLevel
     *            The optimization level
     * @return The number of compiled methods
     */
    private final int doCompileRuntime(int optLevel) {
        final VmMethod[] mt = this.methodTable;
        int compileCount = 0;
        if (mt != null) {
            final int count = mt.length;
            for (int i = 0; i < count; i++) {
                final VmMethod method = mt[ i];
                if (optLevel > method.getNativeCodeOptLevel()) {
                    loader.compileRuntime(method, optLevel);
                    //method.setModifier(true, Modifier.ACC_COMPILED);
                    compileCount++;
                }
            }
        }
        this.state |= VmTypeState.ST_COMPILED;
        return compileCount;
    }

    /**
     * Sets the superClass.
     * 
     * @param superClass
     *            The superClass to set
     */
    protected void setSuperClass(VmNormalClass superClass) {
        if (superClass == null) { throw new IllegalArgumentException(
                "superClass cannot be null"); }
        if (this.superClass == null) {
            this.superClass = superClass;
        } else {
            throw new IllegalArgumentException("Cannot overwrite superClass");
        }
    }

    /**
     * Sets the mTable.
     * 
     * @param methodTable
     *            The method table to set
     */
    protected final void setMethodTable(VmMethod[] methodTable) {
        if (this.methodTable == null) {
            this.methodTable = methodTable;
            Arrays.sort(methodTable, new MethodComparator());
        } else {
            throw new IllegalArgumentException("Cannot overwrite method table");
        }
    }

    /**
     * Sets the fieldTable.
     * 
     * @param fieldTable
     *            The fieldTable to set
     */
    protected void setFieldTable(VmField[] fieldTable) {
        if (this.fieldTable == null) {
            this.fieldTable = fieldTable;
        } else {
            throw new IllegalArgumentException("Cannot overwrite field table");
        }
    }

    /**
     * Sets the interfaceTable.
     * 
     * @param interfaceTable
     *            The interfaceTable to set
     */
    protected void setInterfaceTable(VmImplementedInterface[] interfaceTable) {
        if (this.interfaceTable == null) {
            this.interfaceTable = interfaceTable;
        } else {
            throw new IllegalArgumentException(
                    "Cannot overwrite interface table");
        }
    }

    /**
     * Sets the cp.
     * 
     * @param cp
     *            The cp to set
     */
    protected void setCp(VmCP cp) {
        if (this.cp == null) {
            this.cp = cp;
        } else {
            throw new IllegalArgumentException("Cannot overwrite constant pool");
        }
    }

    /**
     * Is this class a primitive type?
     * 
     * @return boolean
     */
    public boolean isPrimitive() {
        return false;
    }

    /**
     * Is this class a reference type. A reference type is an array of a
     * non-primitive class.
     * 
     * @return boolean
     */
    public final boolean isReferenceType() {
        return (!isPrimitive() || isArray());
    }

    /**
     * Verify this object before it is written into the bootimage by the
     * bootimage builder.
     * 
     * @see org.jnode.vm.VmSystemObject#verifyBeforeEmit()
     */
    public void verifyBeforeEmit() {
        super.verifyBeforeEmit();
        VmMethod clinit = getInitializerMethod();
        if (clinit == null) {
            state |= VmTypeState.ST_INITIALIZED;
        }
    }

    /**
     * Invoke the static initializer of this class. This method is not
     * synchronized since it is called frequently. A simple test is done to see
     * if the class has already been initialized, if not a synchronized helper
     * method is called to do the actual initialization.
     */
    public final void initialize() {
        link();
        if ((superClass != null) && !isArray()) {
            /* The direct super-class must be initialized first $2.17.4 */
            superClass.initialize();
        }
        if (!isInitialized()) {
            doInitialize();
        }
    }

    /**
     * Invoke the static initializer of this class.
     */
    private synchronized void doInitialize() {
        if (!isInitialized()) {
            if (!isInitializing()) {
                state |= VmTypeState.ST_INITIALIZING;
                /*
                 * Screen.debug("initialize("); Screen.debug(name);
                 */
                final VmMethod initMethod = getInitializerMethod();
                if (initMethod != null) {
                    try {
                        VmReflection.invokeStatic(initMethod);
                    } catch (InvocationTargetException ex) {
                        final Throwable targetEx = ex.getTargetException();
                        if (targetEx != null) {
                            ex.getTargetException().printStackTrace();
                            //Unsafe.die("VmType.doInitialize");
                            throw new ExceptionInInitializerError(ex
                                    .getTargetException());
                        } else {
                            throw new ExceptionInInitializerError(
                                    "targetEx == null");
                        }
                    }
                }
                state |= VmTypeState.ST_INITIALIZED;
            }
        }
    }

    /**
     * Gets the <clinit>method or null if no <clinit>method was found in this
     * class.
     * 
     * @return VmMethod
     */
    private VmMethod getInitializerMethod() {
        final VmMethod[] mt = this.methodTable;
        if (mt != null) {
            int count = mt.length;
            for (int i = 0; i < count; i++) {
                VmMethod m = mt[ i];
                if (m.isInitializer()) { return m; }
            }
        }
        return null;
    }

    /**
     * @see org.jnode.vm.VmSystemObject#getExtraInfo()
     * @return String
     */
    public String getExtraInfo() {
        return "Modifiers: " + Modifier.toString(modifiers);
    }

    public static class MethodComparator implements Comparator {

        /**
         * @param o1
         * @param o2
         * @see java.util.Comparator#compare(java.lang.Object,
         *      java.lang.Object)
         * @return int
         */
        public int compare(Object o1, Object o2) {
            int m1 = ((VmMember) o1).getMemberHashCode();
            int m2 = ((VmMember) o2).getMemberHashCode();

            if (m1 < m2) {
                return -1;
            } else if (m1 > m2) {
                return 1;
            } else {
                return 0;
            }
        }

        /**
         * @param obj
         * @see java.lang.Object#equals(java.lang.Object)
         * @return boolean
         */
        public boolean equals(Object obj) {
            return (obj instanceof MethodComparator);
        }

    }

    /**
     * Gets the number of super classes until (and including) Object. E.g. this
     * is 0 for Object and 1 for an interface.
     * 
     * @return int
     */
    public final int getSuperClassDepth() {
        return superClassDepth;
    }

    /**
     * Gets the super classes array of this type
     * 
     * @return The super classes array
     */
    protected final VmType[] getSuperClassesArray() {
        return superClassesArray;
    }
    
    /**
     * Gets the index of this type in the statics table.
     * @return Returns the staticsIndex.
     */
    public final int getStaticsIndex() {
        return this.staticsIndex;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?