⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfile.java

📁 java 到c的转换程序的原代码.对喜欢C程序而不懂JAVA程序的人很有帮助
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    // Static method table    d.println();    d.println("static struct mt_generic sm_table[] = {");    for (int i = 0; i < c.smtable.length; i++) {	Field f = c.smtable[i];	int localp = f.isInArray (c.methods) ? 1 : 0;	d.println("    {TMIT_native_code, (Void(*)())" + f.cname +		  ",\n\t(const Char *)&nmsm_" + i + "," + f.name.length () +                  ",(const Char *)&sgsm_" + i + "," + f.signature.length () +                  ",\n\t" + localp + ",0x" + Integer.toHexString (f.access) +                  "," + f.classfilePos + 		  "," + (localp == 1 ? ("xt_" + f.cname) : "0") + "},");    }    if (c.smtable.length == 0)	d.println("    {TMIT_undefined}");    d.println("};");}/** Search the class fields, and if any have initializers, create a function  * that will assign them before we start real execution; this will be  * needed for reflection. */static private booleanemitStaticFinalInit (PrintWriter d, // Where to write data                     ClassData c) // Information on class{    boolean haveSFI;            // Have we started an SFI function?    haveSFI = false;    for (int i = 0; i < c.cvtable.length; i++) {        Field f = c.cvtable [i];        byte adata [];        int cind;        // See if the field has a constant value (initializer)        adata = Attribute.find (f.attributes, "ConstantValue");        if (null == adata) {            continue;        }        /* OK, it does.  What's the value of the initializer?  If the         * exception is thrown below, we probably had an invalid class         * file. */        cind = -1;        try {            cind = (new DataInputStream (new ByteArrayInputStream (adata))).readShort ();        } catch (IOException e) {        }        if (0 > cind) {            continue;        }        /* OK, we're going to need an SFI.  If we haven't started one, do         * so now. */        if (! haveSFI) {            haveSFI = true;            d.println ();            d.println ("static void\ninitStaticFields (void) {");            d.println ("    extern struct in_java_lang_String *intern_string(struct in_java_lang_String *str);");        }        /* Name of the field we're going to assign. */        String fname = "cl_" + c.cname + ".V." + f.cname;        /* Do the initialization within a separate block so we can define         * local variables to assign in the init. */        d.println ("   {");        Constant ival = c.constants [cind];        switch (ival.tag) {            case Constant.LONG: // Just assign value	        String s;	        long v = ((Long)ival.value).longValue();	        if (v > (long)Integer.MIN_VALUE && v < (long)Integer.MAX_VALUE) 		{		    s = ival.value.toString();	        } else {		    // now we must use "ANSI C" to construct a possibly 		    // "long long" val		    int lh = (int)(v >>> 32);		    int rh = (int)v;		    s = "((((Long)0x" + Integer.toHexString(lh) +		        "u)	 << 32) | ((Long)0x" + Integer.toHexString(rh) +			"u))";	        }	        d.println ("   " + fname + " = " + s + ";");	        break;            case Constant.FLOAT: { // Union integral representation with float                float fv;                                fv = ((Float)ival.value).floatValue ();                d.println ("   union fconst _fv = {0x" +                           Integer.toHexString (Float.floatToIntBits (fv)) +                           "};");                d.println ("   " + fname + " = _fv.v;");                break;            }            case Constant.DOUBLE: { // Union integral repr with doublen                double dv;                dv = ((Double)ival.value).doubleValue ();                d.println ("   union dconst _dv = {0x" +                           Long.toHexString (Double.doubleToLongBits (dv)) +                           "L};");                d.println ("   " + fname + " = _dv.v;");                break;            }            case Constant.INTEGER: // Just the value                d.println ("   " + fname + " = " +                           ((Integer)ival.value).toString() +                           ";");                break;            case Constant.STRING: {                /* Create a character array containing the data, then set                 * up a java.lang.String object pointing to it.  Remember to                 * intern the string. */                String sv = (String) ival.value;                d.println ("    static const CARRAY(" + sv.length () +                           ") _svchars = {&acl_char, 0, " + sv.length () + ", 0,");                Repr.emitCharData (d, sv);                d.println ("    };");                d.println ("    static struct in_java_lang_String _svjls =");                d.println ("    { &cl_java_lang_String, 0, (Object) &_svchars, 0, " + sv.length () + "};");                d.println ("   " + fname + " = intern_string (&_svjls);");                }                break;            default:                throw new InternalError ("Invalid constant type for static final initializer");        }        d.println ("   }");    }    if (haveSFI) {        /* Close off the function if we have one. */        d.println ("}");    }    return haveSFI;}//  clstruct(d, c) -- write class struct.////  Must be kept in sync with class struct definition in toba.h./* The class structure format number; keep this in sync with toba.h */static private final int TOBA_CLASSSTRUCT_VERSION = 43;static private void clstruct(PrintWriter d, ClassData c){    /* Emit the initializer function for static finals (if there is one);     * remember whether it's there so we can save its address for future     * invocation. */    boolean haveSFI = emitStaticFinalInit (d, c);    // First, generate a static canonical class reference for the class.    // We don't have a real toba.classfile.ClassRef structure, so do something    // that looks much the same. */    d.println();    d.println("#ifndef h_toba_classfile_ClassRef");    d.println("extern struct cl_generic cl_toba_classfile_ClassRef;");    d.println("#endif /* h_toba_classfile_ClassRef */");    d.println("static struct { /* pseudo in_toba_classfile_ClassRef */");    d.println("   struct cl_generic *class;");    d.println("   struct monitor *monitor;");    d.println("   Object name;");    d.println("   Object refClass;");    d.println("   Object loadedRefdClasses;");    d.println("} inr_" + c.cname + " = {");    d.println("  (struct cl_generic *)&cl_toba_classfile_ClassRef.C, 0, &classname, &cl_" +              c.cname + ".C.classclass, 0};");    d.println();    d.println("struct cl_" + c.cname + " cl_" + c.cname + " = { {");    if ((c.access & ClassData.ACC_INTERFACE) != 0)	d.println("    1, 1,"); // needinit, flags (IS_RESOLVED == 1)    else	d.println("    1, 0,");    d.println("    &classname,");		// class name String    d.println("    &cl_java_lang_Class.C, 0,");	// class Class instance    d.println("    sizeof(struct in_" + c.cname + "),");    d.println("    " + c.imtable.length + ",");    d.println("    " + c.smtable.length + ",");    d.println("    " + c.ivtable.length + ",");    d.println("    " + c.cvtable.length + ",");    d.println("    " + nsupers + ", supers,");    d.println("    " + ninters + ", " + c.interfaces.length +    			", inters, HASHMASK, htable,");    d.println("    " + nothers + ", others,");    d.println("    0, 0,");			// arrayclass, elemclass    d.println("    ch_" + c.cname + ",");	// string pool    d.println("    st_" + c.cname + ",");	// string list    methodref(d, c, "<clinit>", false);		// class initializer    constructor(d, c);				// constructor    methodref(d, c, "finalize", true);		// finalizer    d.println("    " + (haveSFI ? "initStaticFields" : "0") + ",");    d.println("    0,");                        // classloader - 0 since                                                // system class loader    d.println("    " + CFile.TOBA_CLASSSTRUCT_VERSION + ","); // class structure version    d.println("    0x" + Integer.toHexString (c.access) + ","); // class access flags    d.println("    0,");                        // class data (none to start)    d.println("    (struct in_toba_classfile_ClassRef *)&inr_" + c.cname + ",");     // canonical class reference structure    d.println("    iv_table, cv_table,");	// variable tables    d.println("    sm_table},");		// static method table        methodsigs(d, c);				// instance methods and sigs    d.println("};");}//  methodref(d, c, name, climb) -- find "()V" method and write its namestatic private void methodref    (PrintWriter d, ClassData c, String name, boolean climb){    Field m = c.getmethod(name, climb);    if (m != null)	d.println("    " + m.cname + ",");    else	d.println("    0,");			// not found}//  constructor(c, d) -- generate constructor or exception tosser referencestatic private void constructor(PrintWriter d, ClassData c){    if ((c.access & (ClassData.ACC_ABSTRACT | ClassData.ACC_INTERFACE)) != 0) {	d.println("    throwInstantiationException,");	return;    }    Field m = c.getmethod("<init>", false);    if (m == null)	d.println("    throwNoSuchMethodError,");    else	d.println("    " + m.cname + ",");}//  methodsigs(d, c) -- generate signatures and references for method table.static private void methodsigs(PrintWriter d, ClassData c){    d.println ("    { /* methodsigs */");    for (int i = 0; i < c.imtable.length; i++) {	Field m = c.imtable[i];            	if ((m.access & ClassData.ACC_ABSTRACT) != 0)	    d.print("\t{TMIT_abstract, 0");	else {	    String s = String.valueOf(m.hashcode);	    d.print("\t{TMIT_native_code, " + m.cname);	}	int localp = m.isInArray (c.methods) ? 1 : 0;        d.println(",(const Char *)&nmim_" + i + "," + m.name.length () +                  ",\n\t(const Char *)&sgim_" + i + "," + m.signature.length() +		  "," + localp + ",0x" + Integer.toHexString(m.access) +                   "," + m.classfilePos + "," +		  (localp == 1 ? ("xt_" + m.cname) : "0") + "},");    };    d.println("    } /* end of methodsigs */");}//  gfloats(d, table) -- generated needed floating constants for class.static void gfloats(PrintWriter d, Constant table[]){    d.println();    for (int i = 1; i < table.length; i++) {	Constant c = table[i];	if (c == null || (c.tag != Constant.FLOAT && c.tag != Constant.DOUBLE))	    continue;	double v = ((Number)c.value).doubleValue();	if (Repr.simplefloat(v))	    continue;			// will generate in-line value	if (c.tag == Constant.FLOAT)	    d.println("static union fconst fc" + i + " = { 0x" +		Integer.toHexString(Float.floatToIntBits((float)v)) + " };");	else {	    long b = Double.doubleToLongBits(v);	    String lh = Integer.toHexString((int)(b >> 32));	    String rh = Integer.toHexString((int)(b));	    d.println("static union dconst dc" + i + " = { (ULong)0x" + lh +		"<<32 | (ULong)(UInt)0x" + rh + " };");	}    }}//  strref(k) -- generate reference index for constant k, which is a string.static int strref(Constant k){    int n;    n = strnums[k.index];		// get index into string pool    if (n != 0)				// if already allocated    	return n;			// return index        // append to string pool and create a new slot    n = strcount++;			// count entry    strpool.append((String)k.value);	// append characters to pool    strlens[n] = ((String)k.value).length();	// remember length    strnums[k.index] = n;		// remember index    return n;				//   and return it}//  strdump(d, c) -- dump string pool on stream d for class c.static void strdump(PrintWriter d, ClassData c){    String chname = "ch_" + c.cname;    // generate string pool (a C array of Chars)    d.println();    d.println();    d.println();    d.print("const Char " + chname + "[] = {  /* string pool */");    Repr.emitCharData (d, strpool.toString ());    d.println("};");    // generate array of pointers to ends of strings    int base = 0;		/* starting point of current string */    d.println();    d.println("const void *st_" + c.cname + "[] = {");    for (int i = 0; i < strcount; i++) {	/* generate pointer */        d.print("    " + chname + "+" + (base + strlens[i]) +	    ",\t/* " + i + ". ");	/* in comment, print first 40 chars of each constant */	for (int j = 0; j < Repr.GCHARS_PER_CMMT && j < strlens[i]; j++) {	    char ch = strpool.charAt(base + j);	    if (ch >= ' ' && ch <= '~' && ch != '/' && ch != '?')	    	d.print(ch);	    else	    	d.print("%");	/* use % as stand-in for troublesome chars */	}	d.println(" */");	base += strlens[i];	/* incr base for next string */    }    d.println("    0};");	/* terminate table with sentinel */}} // class CFile

⌨️ 快捷键说明

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