home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / oobj.h < prev    next >
C/C++ Source or Header  |  1996-05-03  |  12KB  |  378 lines

  1. /*
  2.  * @(#)oobj.h    1.56 96/03/28  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. /*
  20.  * Java object header format
  21.  */
  22.  
  23. #ifndef _OOBJ_H_
  24. #define _OOBJ_H_
  25.  
  26. #ifndef JAVA_CLASSFILE_MAGIC
  27.  
  28. #include <stddef.h>
  29.  
  30. #include "typedefs.h"
  31. #include "debug.h"
  32. #include "bool.h"
  33. #include "oobj_md.h"
  34. #include "signature.h"
  35.  
  36. #define JAVA_CLASSFILE_MAGIC              0xCafeBabe
  37.  
  38. #define JAVASRCEXT "java"
  39. #define JAVASRCEXTLEN 4
  40. #define JAVAOBJEXT "class"
  41. #define JAVAOBJEXTLEN 5
  42.  
  43. #define JAVA_VERSION     45
  44. #define JAVA_MINOR_VERSION 3
  45. #define ARRAYHEADER     long alloclen
  46.  
  47. #define HandleTo(T) typedef struct H##T { Class##T *obj; struct methodtable *methods;} H##T
  48.  
  49.  
  50. typedef long OBJECT;
  51. typedef OBJECT Classjava_lang_Object;
  52. typedef OBJECT ClassObject;
  53. HandleTo(java_lang_Object);
  54. typedef Hjava_lang_Object JHandle;
  55. typedef Hjava_lang_Object HObject;
  56.  
  57. typedef unsigned short unicode;
  58.  
  59. extern unicode    *str2unicode(char *, unicode *, long);
  60. extern char    *int642CString(int64_t number, char *buf, int buflen);
  61.  
  62. #define ALIGN(n) (((n)+3)&~3)
  63. #define UCALIGN(n) ((unsigned char *)ALIGN((int)(n)))
  64.  
  65. struct fieldblock {
  66.     struct ClassClass *clazz;
  67.     char *signature;
  68.     char *name;
  69.     unsigned long ID;
  70.     unsigned short access;
  71.     union {
  72.     unsigned long offset;    /* info of data */    
  73.     OBJECT static_value;
  74.     void *static_address;
  75.     } u;
  76. };
  77.  
  78. #define fieldname(fb)    ((fb)->name)
  79. #define fieldsig(fb)     ((fb)->signature)
  80. #define fieldIsArray(fb) (fieldsig(fb)[0] == SIGNATURE_ARRAY)
  81. #define fieldIsClass(fb) (fieldsig(fb)[0] == SIGNATURE_CLASS)
  82. #define    fieldclass(fb)   ((fb)->clazz)
  83.  
  84. struct execenv;
  85.  
  86. struct methodblock {
  87.     struct fieldblock fb;
  88.     unsigned char       *code;    /* the code */
  89.     struct CatchFrame   *exception_table;
  90.     struct lineno       *line_number_table;
  91.     struct localvar     *localvar_table;
  92.  
  93.     unsigned long        code_length;
  94.     unsigned long        exception_table_length;
  95.     unsigned long        line_number_table_length;
  96.     unsigned long        localvar_table_length;
  97.  
  98.     bool_t  (*invoker)
  99.       (JHandle *o, struct methodblock *mb, int args_size, struct execenv *ee);
  100.     unsigned short args_size;    /* total size of all arguments */
  101.     unsigned short maxstack;    /* maximum stack usage */
  102.     unsigned short nlocals;    /* maximum number of locals */
  103.     void *CompiledCode;        /* it's type is machine dependent */
  104.     void *CompiledCodeInfo;     /* it's type is machine dependent */
  105.     long CompiledCodeFlags;    /* machine dependent bits */
  106. };
  107.  
  108. struct ClassClass;
  109. struct HIOstream;
  110.  
  111. struct methodtable {
  112.     struct ClassClass *classdescriptor;
  113.     struct methodblock *methods[1];
  114. };
  115.  
  116. typedef struct {
  117.     char body[1];
  118. } ArrayOfByte;
  119. typedef ArrayOfByte ClassArrayOfByte;
  120. HandleTo(ArrayOfByte);
  121.  
  122. typedef struct {
  123.     unicode body[1];
  124. } ArrayOfChar;
  125. typedef ArrayOfChar ClassArrayOfChar;
  126. HandleTo(ArrayOfChar);
  127.  
  128. typedef struct {
  129.     signed short body[1];
  130. } ArrayOfShort;
  131. typedef ArrayOfShort ClassArrayOfShort;
  132. HandleTo(ArrayOfShort);
  133.  
  134. typedef struct {
  135.     long        body[1];
  136. } ArrayOfInt;
  137. typedef ArrayOfInt ClassArrayOfInt;
  138. HandleTo(ArrayOfInt);
  139.  
  140. typedef struct {
  141.     int64_t        body[1];
  142. } ArrayOfLong;
  143. typedef ArrayOfLong ClassArrayOfLong;
  144. HandleTo(ArrayOfLong);
  145.  
  146. typedef struct {
  147.     float       body[1];
  148. } ArrayOfFloat;
  149. typedef ArrayOfFloat ClassArrayOfFloat;
  150. HandleTo(ArrayOfFloat);
  151.  
  152. typedef struct {
  153.     double       body[1];
  154. } ArrayOfDouble;
  155. typedef ArrayOfDouble ClassArrayOfDouble;
  156. HandleTo(ArrayOfDouble);
  157.  
  158. typedef struct {
  159.     JHandle *(body[1]);
  160. } ArrayOfArray;
  161. typedef ArrayOfArray ClassArrayOfArray;
  162. HandleTo(ArrayOfArray);
  163.  
  164. typedef struct {
  165.     HObject *(body[1]);
  166. } ArrayOfObject;
  167. typedef ArrayOfObject ClassArrayOfObject;
  168. HandleTo(ArrayOfObject);
  169.  
  170. typedef struct Hjava_lang_String HString;
  171.  
  172. typedef struct {
  173.     HString  *(body[1]);
  174. } ArrayOfString;
  175. typedef ArrayOfString ClassArrayOfString;
  176. HandleTo(ArrayOfString);
  177.  
  178.  
  179. /* Note: any handles in this structure must also have explicit
  180.    code in the ScanClasses() routine of the garbage collector
  181.    to mark the handle. */
  182. typedef struct ClassClass {
  183.     /* Things following here are saved in the .class file */
  184.     unsigned short         major_version;
  185.     unsigned short         minor_version;
  186.     char                    *name;
  187.     char                    *super_name;
  188.     char                    *source_name;
  189.     struct Hjava_lang_Class  *superclass;
  190.     struct Hjava_lang_Class  *HandleToSelf;
  191.     HObject            *loader;
  192.     struct methodblock        *finalizer;
  193.  
  194.     union cp_item_type      *constantpool;
  195.     struct methodblock      *methods;
  196.     struct fieldblock       *fields;
  197.     short                   *implements;
  198.  
  199.     struct methodtable      *methodtable;
  200.     struct fieldblock      **slottable;
  201.  
  202.     HArrayOfChar        *classname_array;
  203.  
  204.     unsigned long            thishash;
  205.     unsigned long            totalhash;
  206.  
  207.     unsigned short           constantpool_count;  /* number of items in pool */
  208.     unsigned short           methods_count;       /* number of methods */
  209.     unsigned short           fields_count;        /* number of fields */
  210.     unsigned short           implements_count;    /* number of protocols */
  211.  
  212.     unsigned short           methodtable_size;    /* the size of method table */
  213.     unsigned short           slottbl_size;        /* size of slottable */
  214.     unsigned short           instance_size;       /* (bytes) of an instance */
  215.  
  216.     unsigned short access;           /* how this class can be accesses */
  217.     unsigned short flags;         /* see the CCF_* macros */
  218. } ClassClass;
  219.  
  220. typedef ClassClass Classjava_lang_Class;
  221. HandleTo(java_lang_Class);
  222. typedef Hjava_lang_Class HClass;
  223.  
  224. extern bool_t    createInternalClass(unsigned char *bytes, unsigned char *limit,
  225.                     ClassClass *);
  226. extern void    FreeClass(ClassClass *cb);
  227.  
  228. #define classname(cb)   ((cb)->name)
  229. #define classsupername(cb) ((cb)->super_name)
  230. #define classsrcname(cb) ((cb)->source_name)
  231. #define cbSuperclass(cb)   ((cb)->superclass)
  232. #define cbHandle(cb)       ((cb)->HandleToSelf)
  233. #define    cbLoader(cb)    ((cb)->loader)
  234.  
  235. #define cbConstantPool(cb) ((cb)->constantpool)
  236. #define    cbMethods(cb)      ((cb)->methods)
  237. #define    cbFields(cb)       ((cb)->fields)
  238. #define cbImplements(cb)   ((cb)->implements)
  239. #define cbMethodTable(cb)  ((cb)->methodtable)
  240. #define cbSlotTable(cb)    ((cb)->slottable)
  241.  
  242. #define cbThisHash(cb)     ((cb)->thishash)
  243. #define cbTotalHash(cb)    ((cb)->totalhash)
  244. #define cbSlotTableSize(cb) ((cb)->slottbl_size)
  245. #define cbInstanceSize(cb) ((cb)->instance_size)
  246. #define cbMethodTableSize(cb) ((cb)->methodtable_size)
  247. #define cbClassnameArray(cb)  ((cb)->classname_array)
  248. #define cbAccess(cb)       ((cb)->access)
  249. #define cbIsInterface(cb)  ((cbAccess(cb) & ACC_INTERFACE) != 0)
  250.  
  251. extern char *classname2string(char *str, char *dst, int size);
  252.  
  253. #define twoword_static_address(fb) ((fb)->u.static_address)
  254. #define normal_static_address(fb)  (&(fb)->u.static_value)
  255.  
  256. /* ClassClass flags */
  257. #define CCF_IsResolved      0x02    /* been resolved yet? */
  258. #define CCF_IsError      0x04    /* Resolution caused an error */
  259. #define CCF_IsSoftRef      0x08    /* whether this is class Ref or subclass */
  260. #define CCF_IsInitialized 0x10    /* whether this is class has been inited */
  261. #define CCF_IsLoaded      0x20    /* Is this really the class object or a stub.*/
  262. #define CCF_IsVerified    0x40    /* Is this really the class object or a stub.*/
  263.  
  264. #define CCIs(cb,flag) (((cb)->flags & CCF_Is##flag) != 0)
  265. #define CCSet(cb,flag) ((cb)->flags |= CCF_Is##flag)
  266. #define CCClear(cb,flag) ((cb)->flags &= ~CCF_Is##flag)
  267.  
  268. /* map from pc to line number */
  269. struct lineno {
  270.     unsigned long pc, 
  271.     line_number;
  272. };
  273.  
  274. extern struct lineno *lntbl;
  275. extern long lntsize, lntused;
  276.  
  277. /* Symbol table entry for local variables and parameters.
  278.    pc0/length defines the range that the variable is valid, slot
  279.    is the position in the local variable array in ExecEnv.
  280.    nameoff and sigoff are offsets into the string table for the
  281.    variable name and type signature.  A variable is defined with
  282.    DefineVariable, and at that time, the node for that name is
  283.    stored in the localvar entry.  When code generate is completed
  284.    for a particular scope, a second pass it made to replace the
  285.    src node entry with the correct length. */
  286.  
  287. struct localvar {
  288.     long pc0;            /* starting pc for this variable */
  289.     long length;        /* -1 initially, end pc - pc when we're done */
  290.     short nameoff;        /* offset into string table */
  291.     short sigoff;        /* offset into string table */
  292.     long slot;            /* local variable slot */
  293. };
  294.  
  295. /* Try/catch is implemented as follows.  On a per class basis,
  296.    there is a catch frame handler (below) for each catch frame
  297.    that appears in the source.  It contains the pc range of the
  298.    corresponding try body, a pc to jump to in the event that that
  299.    handler is chosen, and a catchType which must match the object
  300.    being thrown if that catch handler is to be taken.
  301.  
  302.    The list of catch frames are sorted by pc.  If one range is
  303.    inside another, then outer most range (the one that encompasses
  304.    the other) appears last in the list.  Therefore, it is possible
  305.    to search forward, and the first one that matches is the
  306.    innermost one.
  307.  
  308.    Methods with catch handlers will layout the code without the
  309.    catch frames.  After all the code is generated, the catch
  310.    clauses are generated and table entries are created.
  311.  
  312.    When the class is complete, the table entries are dumped along
  313.    with the rest of the class. */
  314.  
  315. struct CatchFrame {
  316.     long    start_pc, end_pc;    /* pc range of corresponding try block */
  317.     long    handler_pc;            /* pc of catch handler */
  318.     void*   compiled_CatchFrame; /* space to be used by machine code */
  319.     short   catchType;            /* type of catch parameter */
  320. };
  321.  
  322. #define MC_SUPER        (1<<5)
  323. #define MC_NARGSMASK    (MC_SUPER-1)
  324. #define MC_INT          (0<<6)
  325. #define MC_FLOAT        (1<<6)
  326. #define MC_VOID         (2<<6)
  327. #define MC_OTHER        (3<<6)
  328. #define MC_TYPEMASK     (3<<6)
  329.  
  330. enum {
  331.     CONSTANT_Utf8 = 1,
  332.     CONSTANT_Unicode,        /* unused */
  333.     CONSTANT_Integer,
  334.     CONSTANT_Float,
  335.     CONSTANT_Long,      
  336.     CONSTANT_Double,
  337.     CONSTANT_Class,
  338.     CONSTANT_String,
  339.     CONSTANT_Fieldref,
  340.     CONSTANT_Methodref,
  341.     CONSTANT_InterfaceMethodref,
  342.     CONSTANT_NameAndType
  343. };
  344.  
  345. union cp_item_type {
  346.     int i;
  347.     void *p;
  348.     float f;
  349.     char *cp;
  350. };
  351. typedef union cp_item_type cp_item_type;
  352.  
  353. #define CONSTANT_POOL_ENTRY_RESOLVED 0x80
  354. #define CONSTANT_POOL_ENTRY_TYPEMASK 0x7F
  355. #define CONSTANT_POOL_TYPE_TABLE_GET(cp,i) (((unsigned char *)(cp))[i])
  356. #define CONSTANT_POOL_TYPE_TABLE_PUT(cp,i,v) (CONSTANT_POOL_TYPE_TABLE_GET(cp,i) = (v))
  357. #define CONSTANT_POOL_TYPE_TABLE_SET_RESOLVED(cp,i) \
  358.     (CONSTANT_POOL_TYPE_TABLE_GET(cp,i) |= CONSTANT_POOL_ENTRY_RESOLVED)
  359. #define CONSTANT_POOL_TYPE_TABLE_IS_RESOLVED(cp,i) \
  360.     ((CONSTANT_POOL_TYPE_TABLE_GET(cp,i) & CONSTANT_POOL_ENTRY_RESOLVED) != 0)
  361. #define CONSTANT_POOL_TYPE_TABLE_GET_TYPE(cp,i) \
  362.         CONSTANT_POOL_TYPE_TABLE_GET(cp,i) & CONSTANT_POOL_ENTRY_TYPEMASK
  363.  
  364. #define CONSTANT_POOL_TYPE_TABLE_INDEX 0
  365. #define CONSTANT_POOL_UNUSED_INDEX 1
  366.  
  367. /* The following are used by the constant pool of "array" classes. */
  368.  
  369. #define CONSTANT_POOL_ARRAY_DEPTH_INDEX 1
  370. #define CONSTANT_POOL_ARRAY_TYPE_INDEX 2
  371. #define CONSTANT_POOL_ARRAY_CLASS_INDEX 3
  372. #define CONSTANT_POOL_ARRAY_CLASSNAME_INDEX 4
  373. #define CONSTANT_POOL_ARRAY_LENGTH 5
  374.  
  375. #endif
  376.  
  377. #endif /* !_OOBJ_H_ */
  378.