x86compilercontext.java

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

JAVA
612
字号
/*
 * $Id: X86CompilerContext.java,v 1.13 2004/02/26 10:33:39 epr Exp $
 */
package org.jnode.vm.x86.compiler;

import org.jnode.vm.VmSystemObject;
import org.jnode.vm.classmgr.VmClassLoader;
import org.jnode.vm.classmgr.VmField;
import org.jnode.vm.classmgr.VmInstanceField;
import org.jnode.vm.classmgr.VmMethod;
import org.jnode.vm.classmgr.VmType;
import org.jnode.vm.memmgr.VmHeapManager;
import org.jnode.vm.memmgr.VmWriteBarrier;

/**
 * @author epr
 */
public class X86CompilerContext extends VmSystemObject {

    private final VmType vmMethodCodeClass;

    private final VmType vmInstanceMethodClass;

    private final VmType vmInstanceFieldClass;

    private final VmType vmStaticFieldClass;

    private final VmInstanceField vmMemberDeclaringClassField;

    private final VmInstanceField vmFieldOffsetField;

    private final VmInstanceField vmFieldStaticsIndexField;

    private final VmInstanceField vmMethodTibOffsetField;

    private final VmInstanceField vmMethodSelectorField;

    private final VmInstanceField vmMethodInvocationCountField;

    private final VmInstanceField vmMethodNativeCodeField;

    private final VmInstanceField vmConstIMethodRefSelectorField;

    private final int vmThreadSwitchIndicatorOffset;

    private final VmType vmSoftByteCodesClass;

    private final VmType vmMonitorManagerClass;

    private final VmMethod anewarrayMethod;

    private final VmMethod allocArrayMethod;

    private final VmMethod allocPrimitiveArrayMethod;

    private final VmMethod resolveFieldMethod;

    private final VmMethod resolveMethodMethod;

    private final VmMethod resolveClassMethod;

    private final VmMethod allocObjectMethod;

    private final VmMethod monitorEnterMethod;

    private final VmMethod monitorExitMethod;

    private final VmMethod ldivMethod;

    private final VmMethod lremMethod;

    private final VmMethod systemExceptionMethod;

    private final VmMethod vmTypeInitialize;

    private final VmInstanceField vmTypeModifiers;

    private final VmInstanceField vmTypeState;

    private final VmInstanceField vmTypeCp;

    private final VmInstanceField vmCPCp;

    private final VmInstanceField vmProcessorStackEnd;

    private final VmInstanceField vmProcessorStaticsTable;

    private final VmInstanceField vmConstClassResolvedClass;

    private final VmInstanceField vmConstFieldResolvedField;

    private final VmInstanceField vmConstMethodResolvedMethod;

    private final VmMethod arrayStoreWriteBarrier;

    private final VmMethod putfieldWriteBarrier;

    private final VmMethod putstaticWriteBarrier;

    private final VmWriteBarrier writeBarrier;
    
    private final int magic;

    /**
     * Create a new instance
     * 
     * @param loader
     */
    public X86CompilerContext(VmClassLoader loader, VmHeapManager heapManager, int magic) {
        try {
            this.magic = magic;
            // VmMember class
            final VmType vmMemberType = loader.loadClass(
                    "org.jnode.vm.classmgr.VmMember", true);
            this.vmMemberDeclaringClassField = (VmInstanceField) testField(vmMemberType
                    .getField("declaringClass"));

            // SoftByteCode
            this.vmSoftByteCodesClass = loader.loadClass(
                    "org.jnode.vm.SoftByteCodes", true);
            anewarrayMethod = testMethod(vmSoftByteCodesClass
                    .getMethod(
                            "anewarray",
                            "(Lorg/jnode/vm/classmgr/VmMethod;Lorg/jnode/vm/classmgr/VmType;I)Ljava/lang/Object;"));
            allocArrayMethod = testMethod(vmSoftByteCodesClass.getMethod(
                    "allocArray",
                    "(Lorg/jnode/vm/classmgr/VmType;I)Ljava/lang/Object;"));
            allocPrimitiveArrayMethod = testMethod(vmSoftByteCodesClass
                    .getMethod("allocPrimitiveArray", "(II)Ljava/lang/Object;"));
            resolveFieldMethod = testMethod(vmSoftByteCodesClass
                    .getMethod(
                            "resolveField",
                            "(Lorg/jnode/vm/classmgr/VmMethod;Lorg/jnode/vm/classmgr/VmConstFieldRef;Z)Lorg/jnode/vm/classmgr/VmField;"));
            resolveMethodMethod = testMethod(vmSoftByteCodesClass
                    .getMethod(
                            "resolveMethod",
                            "(Lorg/jnode/vm/classmgr/VmMethod;Lorg/jnode/vm/classmgr/VmConstMethodRef;)Lorg/jnode/vm/classmgr/VmMethod;"));
            resolveClassMethod = testMethod(vmSoftByteCodesClass
                    .getMethod("resolveClass",
                            "(Lorg/jnode/vm/classmgr/VmConstClass;)Lorg/jnode/vm/classmgr/VmType;"));
            allocObjectMethod = testMethod(vmSoftByteCodesClass.getMethod(
                    "allocObject",
                    "(Lorg/jnode/vm/classmgr/VmType;I)Ljava/lang/Object;"));
            systemExceptionMethod = testMethod(vmSoftByteCodesClass.getMethod(
                    "systemException", "(II)Ljava/lang/Throwable;"));

            // Write barrier
            writeBarrier = (heapManager != null) ? heapManager.getWriteBarrier() : null;
            if (writeBarrier != null) {
                final VmType wbClass = loader.loadClass(writeBarrier.getClass()
                        .getName(), true);
                arrayStoreWriteBarrier = testMethod(wbClass.getMethod(
                        "arrayStoreWriteBarrier",
                        "(Ljava/lang/Object;ILjava/lang/Object;)V"));
                putfieldWriteBarrier = testMethod(wbClass.getMethod(
                        "putfieldWriteBarrier",
                        "(Ljava/lang/Object;ILjava/lang/Object;)V"));
                putstaticWriteBarrier = testMethod(wbClass.getMethod(
                        "putstaticWriteBarrier", "(ILjava/lang/Object;)V"));
            } else {
                arrayStoreWriteBarrier = null;
                putfieldWriteBarrier = null;
                putstaticWriteBarrier = null;
            }

            // MonitorManager
            this.vmMonitorManagerClass = loader.loadClass(
                    "org.jnode.vm.MonitorManager", true);
            monitorEnterMethod = testMethod(vmMonitorManagerClass.getMethod(
                    "monitorEnter", "(Ljava/lang/Object;)V"));
            monitorExitMethod = testMethod(vmMonitorManagerClass.getMethod(
                    "monitorExit", "(Ljava/lang/Object;)V"));

            // MathSupport
            final VmType vmClass = loader.loadClass("org.jnode.vm.MathSupport",
                    true);
            ldivMethod = testMethod(vmClass.getMethod("ldiv", "(JJ)J"));
            lremMethod = testMethod(vmClass.getMethod("lrem", "(JJ)J"));

            // VmInstanceField
            this.vmInstanceFieldClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmInstanceField", true);
            vmFieldOffsetField = (VmInstanceField) testField(vmInstanceFieldClass
                    .getField("offset"));

            // VmStaticField
            this.vmStaticFieldClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmStaticField", true);
            vmFieldStaticsIndexField = (VmInstanceField) testField(vmStaticFieldClass
                    .getField("staticsIndex"));

            // VmInstanceMethod
            this.vmInstanceMethodClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmInstanceMethod", true);
            vmMethodTibOffsetField = (VmInstanceField) testField(vmInstanceMethodClass
                    .getField("tibOffset"));

            // VmMethodCode
            this.vmMethodCodeClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmMethodCode", true);
            vmMethodSelectorField = (VmInstanceField) testField(vmInstanceMethodClass
                    .getField("selector"));
            vmMethodInvocationCountField = (VmInstanceField) testField(vmInstanceMethodClass
                    .getField("invocationCount"));
            vmMethodNativeCodeField = (VmInstanceField) testField(vmInstanceMethodClass
                    .getField("nativeCode"));

            // VmConstIMethodRef
            final VmType cimrClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmConstIMethodRef", true);
            this.vmConstIMethodRefSelectorField = (VmInstanceField) testField(cimrClass
                    .getField("selector"));

            // VmProcessor.threadSwitchIndicator
            final VmType procClass = loader.loadClass(
                    "org.jnode.vm.VmProcessor", true);
            vmThreadSwitchIndicatorOffset = ((VmInstanceField) testField(procClass
                    .getField("threadSwitchIndicator"))).getOffset();

            // VmType
            final VmType typeClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmType", true);
            vmTypeInitialize = testMethod(typeClass.getMethod("initialize",
                    "()V"));
            vmTypeModifiers = (VmInstanceField) testField(typeClass
                    .getField("modifiers"));
            vmTypeState = (VmInstanceField) testField(typeClass
                    .getField("state"));
            vmTypeCp = (VmInstanceField) testField(typeClass.getField("cp"));

            // VmCP
            final VmType cpClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmCP", true);
            vmCPCp = (VmInstanceField) testField(cpClass.getField("cp"));

            // VmProcessor
            final VmType processorClass = loader.loadClass(
                    "org.jnode.vm.VmProcessor", true);
            vmProcessorStackEnd = (VmInstanceField) testField(processorClass
                    .getField("stackEnd"));
            vmProcessorStaticsTable = (VmInstanceField) testField(processorClass
                    .getField("staticsTable"));

            // VmConstClass
            final VmType constClassClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmConstClass", true);
            vmConstClassResolvedClass = (VmInstanceField) testField(constClassClass
                    .getField("vmClass"));

            // VmConstField
            final VmType constFieldClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmConstFieldRef", true);
            vmConstFieldResolvedField = (VmInstanceField) testField(constFieldClass
                    .getField("vmResolvedField"));

            // VmConstMethod
            final VmType constMethodClass = loader.loadClass(
                    "org.jnode.vm.classmgr.VmConstMethodRef", true);
            vmConstMethodResolvedMethod = (VmInstanceField) testField(constMethodClass
                    .getField("vmMethod"));

        } catch (ClassNotFoundException ex) {
            throw new NoClassDefFoundError(ex.getMessage());
        }
    }

    private final VmMethod testMethod(VmMethod method) {
        if (method == null) { throw new RuntimeException("Cannot find a method"); }
        return method;
    }

    private final VmField testField(VmField field) {
        if (field == null) { throw new RuntimeException("Cannot find a field"); }
        return field;
    }

    /**
     * Gets the allocArray method
     * 
     * @return method
     */
    public VmMethod getAllocArrayMethod() {
        return allocArrayMethod;
    }

    /**
     * Gets the allocObject method
     * 
     * @return method
     */
    public VmMethod getAllocObjectMethod() {
        return allocObjectMethod;
    }

    /**
     * Gets the anewArray
     * 
     * @return method
     */
    public VmMethod getAnewarrayMethod() {
        return anewarrayMethod;
    }

    /**
     * Gets the ldiv method
     * 

⌨️ 快捷键说明

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