home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffeh / readClassConfig.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-17  |  5.1 KB  |  193 lines

  1. /*
  2.  * readClassConfig.h
  3.  * Various bits of information in a Java class file.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #ifndef __readclassconfig_h
  15. #define __readclassconfig_h
  16.  
  17. #include <stdio.h>
  18.  
  19. typedef FILE classFile;
  20. typedef void classes;
  21. typedef void methods;
  22.  
  23. extern FILE* include;
  24. extern FILE* stub;
  25. extern char className[];
  26.  
  27. extern char* translateSig(char*, FILE*, int*);
  28. extern char* translateSigType(char*, char*);
  29.  
  30. /*
  31.  * Add a class.
  32.  */
  33. #define    ADDCLASS(this, super, access, constants)            \
  34.     if (super!= 0) {                        \
  35.         printf("WARNING: Superclasses not currently supported.\n");\
  36.     }
  37.  
  38. /*
  39.  * Read and process a field.
  40.  */
  41. #define    READFIELD_START()                        \
  42.     fprintf(include, "/* DO NOT EDIT THIS FILE - it is machine generated */\n");\
  43.     fprintf(include, "#include <native.h>\n");            \
  44.     fprintf(include, "/* Header for class %s */\n", className);    \
  45.     fprintf(include, "\n");                        \
  46.     fprintf(include, "#ifndef _Included_%s\n", className);        \
  47.     fprintf(include, "#define _Included_%s\n", className);        \
  48.     fprintf(include, "\n");                        \
  49.     fprintf(include, "typedef struct Class%s {\n", className)
  50.  
  51. #define    READFIELD_END()                            \
  52.     fprintf(include, "} Class%s;\n", className);            \
  53.     fprintf(include, "HandleTo(%s);\n\n", className)
  54.  
  55. #define    READFIELD(fp, this)                        \
  56.     do {                                \
  57.         field_info f;                        \
  58.                                     \
  59.         readu2(&f.access_flags, fp);                \
  60.         readu2(&f.name_index, fp);                \
  61.         readu2(&f.signature_index, fp);                \
  62.                                     \
  63.         /* Ignore statics */                    \
  64.         if (f.access_flags & ACC_STATIC) {            \
  65.             continue;                    \
  66.         }                            \
  67.         fprintf(include, "  ");                    \
  68.         translateSig((char*)constant_pool->data[f.signature_index], include, 0);\
  69.         fprintf(include, " %s;\n", (char*)constant_pool->data[f.name_index]);\
  70.     } while(0)
  71.  
  72. /*
  73.  * Read and process a method.
  74.  */
  75. #define    READMETHOD(fp, this)                        \
  76.     do {                                \
  77.         method_info m;                        \
  78.         char* name;                        \
  79.         char* sig;                        \
  80.         char* str;                        \
  81.         char* ret;                        \
  82.         char type;                        \
  83.         int j;                            \
  84.         int isVoid;                        \
  85.         int args;                        \
  86.         int isStatic;                        \
  87.                                     \
  88.         readu2(&m.access_flags, fp);                \
  89.         readu2(&m.name_index, fp);                \
  90.         readu2(&m.signature_index, fp);                \
  91.                                     \
  92.         /* Only generate stubs for native methods */        \
  93.         if (!(m.access_flags & ACC_NATIVE)) {            \
  94.             continue;                    \
  95.         }                            \
  96.         args = 0;                        \
  97.         isStatic = 1;                        \
  98.         if (!(m.access_flags & ACC_STATIC)) {            \
  99.             isStatic = 0;                    \
  100.         }                            \
  101.         /* Generate method prototype */                \
  102.         name = (char*)constant_pool->data[m.name_index];    \
  103.         sig = (char*)constant_pool->data[m.signature_index];    \
  104.         ret = strchr(sig,')');                    \
  105.         ret++;                            \
  106.         fprintf(include, "extern ");                \
  107.         translateSig(ret, include, 0);                \
  108.         fprintf(include, " %s_%s(", className, name);        \
  109.         str = sig + 1;                        \
  110.         if (!isStatic) {                    \
  111.             args++;                        \
  112.             fprintf(include, "struct H%s*", className);    \
  113.             if (str[0] != ')') {                \
  114.                 fprintf(include, ", ");            \
  115.             }                        \
  116.         }                            \
  117.         while (str[0] != ')') {                    \
  118.             str = translateSig(str, include, &args);    \
  119.             if (str[0] != ')') {                \
  120.                 fprintf(include, ", ");            \
  121.             }                        \
  122.         }                            \
  123.         fprintf(include, ");\n");                \
  124.         /* Generate method stub */                \
  125.         fprintf(stub, "\n/* SYMBOL: %s %s%s */\n", className, name, sig);\
  126.         translateSigType(ret, &type);                \
  127.         if (type == 'v') {                    \
  128.             isVoid  = 1;                    \
  129.         }                            \
  130.         else {                            \
  131.             isVoid  = 0;                    \
  132.         }                            \
  133.                                     \
  134.         switch (type) {                        \
  135.         case 'p':                        \
  136.             fprintf(stub, "void*\n");            \
  137.             break;                        \
  138.         case 'i':                        \
  139.             fprintf(stub, "long\n");            \
  140.             break;                        \
  141.         case 'd':                        \
  142.             fprintf(stub, "double\n");            \
  143.             break;                        \
  144.         case 'f':                        \
  145.             fprintf(stub, "float\n");            \
  146.             break;                        \
  147.         case 'l':                        \
  148.             fprintf(stub, "long long\n");            \
  149.             break;                        \
  150.         case 'v':                        \
  151.             fprintf(stub, "void\n");            \
  152.             break;                        \
  153.         default:                        \
  154.             abort();                    \
  155.         }                            \
  156.         fprintf(stub, "Kaffe_%s_%s_stub(int _S_)\n", className, name);\
  157.         fprintf(stub, "{\n");                    \
  158.         fprintf(stub, "\textern ");                \
  159.         translateSig(ret, stub, 0);                \
  160.         fprintf(stub, " %s_%s();\n", className, name);        \
  161.         fprintf(stub, "\tstack_item* _P_ = (stack_item*)&_S_;\n");\
  162.         fprintf(stub, "\t");                    \
  163.         if (!isVoid) {                        \
  164.             fprintf(stub, "return ");            \
  165.         }                            \
  166.         fprintf(stub, "%s_%s(", className, name);        \
  167.         str = sig + 1;                        \
  168.         if (!isStatic) {                    \
  169.             fprintf(stub, "_P_[%d].p", args-1);        \
  170.             args--;                        \
  171.             if (str[0] != ')') {                \
  172.                 fprintf(stub, ", ");            \
  173.             }                        \
  174.         }                            \
  175.         for (j = args-1; str[0] != ')'; j--) {            \
  176.             str = translateSigType(str, &type);        \
  177.             if (type == 'l' || type == 'd') {        \
  178.                 fprintf(stub, "_P_[%d].i, _P_[%d].i", j-1, j);\
  179.                 j--;                    \
  180.             }                        \
  181.             else {                        \
  182.                 fprintf(stub, "_P_[%d].%c", j, type);    \
  183.             }                        \
  184.             if (str[0] != ')') {                \
  185.                 fprintf(stub, ", ");            \
  186.             }                        \
  187.         }                            \
  188.         fprintf(stub, ");\n");                    \
  189.         fprintf(stub, "}\n");                    \
  190.     } while(0)
  191.  
  192. #endif
  193.