OSDN Git Service

Update lejos_osek to nxtOSEK_v205b0.zip
[nxt-jsp/etrobo-atk.git] / nxtOSEK / lejos_nxj / src / nxtvm / javavm / classes.h
1 /**
2  * classes.h
3  * Contains conterparts of special classes as C structs.
4  */
5  
6 #ifndef _CLASSES_H
7 #define _CLASSES_H
8
9 #include "types.h"
10
11 #define CLASS_MASK      0x00FF
12 #define CLASS_SHIFT     0
13
14 #define GC_MASK         0x2000
15 #define GC_SHIFT        13
16
17 #define IS_ARRAY_MASK   0x4000
18 #define IS_ARRAY_SHIFT  14
19
20 #define IS_ALLOCATED_MASK  0x8000
21 #define IS_ALLOCATED_SHIFT 15
22
23 #define ARRAY_LENGTH_MASK  0x01FF
24 #define ARRAY_LENGTH_SHIFT 0
25
26 #define ELEM_TYPE_MASK  0x1E00
27 #define ELEM_TYPE_SHIFT 9
28
29 #define FREE_BLOCK_SIZE_MASK 0x7FFF
30 #define FREE_BLOCK_SIZE_SHIFT 0
31
32 #define is_array(OBJ_)           ((OBJ_)->flags.objects.isArray)
33 #define is_allocated(OBJ_)       ((OBJ_)->flags.freeBlock.isAllocated)
34 #define get_monitor_count(OBJ_)  ((OBJ_)->monitorCount)
35 #define is_gc(OBJ_)              ((OBJ_)->flags.objects.mark)
36
37 // Double-check these data structures with the 
38 // Java declaration of each corresponding class.
39
40 /**
41  * Object class native structure
42  */
43 typedef struct S_Object
44 {
45   /**
46    * Object/block flags.
47    * Free block:
48    *  -- bits 0-14: Size of free block in words.
49    *  -- bit 15   : Zero (not allocated).
50    * Objects:
51    *  -- bits 0-7 : Class index.
52    *  -- bits 8-12: Unused.
53    *  -- bit 13   : Garbage collection mark.
54    *  -- bit 14   : Zero (not an array).
55    *  -- bit 15   : One (allocated).
56    * Arrays:
57    *  -- bits 0-8 : Array length (0-511).
58    *  -- bits 9-12: Element type.
59    *  -- bit 13   : Garbage collection mark.
60    *  -- bit 14   : One (is an array).
61    *  -- bit 15   : One (allocated).
62    */
63    union _flags
64    {
65      TWOBYTES all;
66      struct _freeBlock
67      {
68        TWOBYTES size:15;
69        TWOBYTES isAllocated:1;
70      }  __attribute__((packed)) freeBlock;
71      struct _objects
72      {
73        byte     _class;
74        byte     padding:5;
75        byte     mark:1;
76        byte     isArray:1;
77        byte     isAllocated:1;
78      }  __attribute__((packed)) objects;
79      struct _arrays
80      {
81        TWOBYTES length:9;
82        TWOBYTES type:4;
83        TWOBYTES mark:1;
84        TWOBYTES isArray:1;
85        TWOBYTES isAllocated:1;
86      } __attribute__((packed)) arrays;
87    } __attribute__((packed)) flags;
88
89   /**
90    * Synchronization state.
91    */
92   byte monitorCount;
93   byte threadId;
94
95 } __attribute__((packed)) Object;
96
97 /**
98  * Thread class native structure
99  */
100 typedef struct S_Thread
101 {
102   Object _super;             // Superclass object storage
103
104   REFERENCE nextThread;      // Intrinsic circular list of threads
105   JINT waitingOn;            // Object who's monitor we want
106   JINT sleepUntil;           // Time to wake up
107   JINT stackFrameArray;      // Array of stack frames
108   JINT stackArray;           // The stack itself
109   JBYTE stackFrameArraySize; // Number of stack frames in use.
110   JBYTE monitorCount;        // Saved monitor depth for context switches
111   JBYTE threadId;            // Unique thread ID
112   JBYTE state;               // RUNNING, DEAD, etc.
113   JBYTE priority;            // The priority
114   JBYTE interruptState;      // INTERRUPT_CLEARED, INTERRUPT_REQUESTED, ...
115   JBYTE daemon;              // true == daemon thread
116 } Thread;
117
118 /**
119  * Runtime class native structure. Doesn't actually contain
120  * any instance data. Maybe it ought to? Like ALL of the leJOS
121  * specific runtime instance data?
122  */
123 typedef struct S_Runtime
124 {
125   Object _super;
126 } Runtime;
127
128 /**
129  * String class native structure
130  */
131 typedef struct S_String
132 {
133   Object _super;
134
135   REFERENCE characters;
136 } String;
137
138
139 #ifdef WIMPY_MATH
140
141 static inline TWOBYTES get_array_length (Object *obj)
142 {
143    TWOBYTES aux;
144    aux = obj->flags.all & ARRAY_LENGTH_MASK;
145    #if (ARRAY_LENGTH_SHIFT == 0)
146    return aux;
147    #else
148    return (aux >> ARRAY_LENGTH_SHIFT);
149    #endif
150 }
151
152 static inline TWOBYTES get_element_type (Object *obj)
153 {
154    TWOBYTES aux;
155    aux = obj->flags.all & ELEM_TYPE_MASK;
156    #if (ELEM_TYPE_SHIFT == 0)
157    return aux;
158    #else
159    return (aux >> ELEM_TYPE_SHIFT);
160    #endif
161 }
162
163 static inline TWOBYTES get_na_class_index (Object *obj)
164 {
165    TWOBYTES aux;
166    aux = obj->flags.all & CLASS_MASK;
167    #if (CLASS_SHIFT == 0)
168    return aux;
169    #else
170    return (aux >> CLASS_SHIFT);
171    #endif
172 }
173
174 #else
175
176 #ifndef notdef
177 #define get_array_length(ARR_)   (((ARR_)->flags.all & ARRAY_LENGTH_MASK) >> ARRAY_LENGTH_SHIFT)
178 #define get_element_type(ARR_)   ((ARR_)->flags.all & ELEM_TYPE_MASK) >> ELEM_TYPE_SHIFT
179 #define get_na_class_index(OBJ_) (((OBJ_)->flags.all & CLASS_MASK) >> CLASS_SHIFT)
180 #define get_free_length(OBJ_)    (((OBJ_)->flags.all & FREE_BLOCK_SIZE_MASK) >> FREE_BLOCK_SIZE_SHIFT)
181 #else
182 #define get_array_length(ARR_)   ((ARR_)->flags.arrays.length)
183 #define get_element_type(ARR_)   ((ARR_)->flags.arrays.type)
184 #define get_na_class_index(OBJ_) ((OBJ_)->flags.objects.class)
185 #define get_free_length(OBJ_)    ((OBJ_)->flags.freeBlock.size)
186 #endif
187
188 #endif // WIMPY_MATH
189
190 #endif // _CLASSES_H
191
192
193
194
195
196
197
198
199