1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
11 * SquirrelJME NanoCoat Virtual Machine Header Definitions.
16 #ifndef SQUIRRELJME_NVM_H
17 #define SQUIRRELJME_NVM_H
23 #include "sjme/config.h"
27 #ifndef SJME_CXX_IS_EXTERNED
28 #define SJME_CXX_IS_EXTERNED
29 #define SJME_CXX_SQUIRRELJME_NVM_H
31 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
32 #endif /* #ifdef __cplusplus */
34 /*--------------------------------------------------------------------------*/
37 * Pastes two tokens together.
39 * @param a The first token.
40 * @param b The second token.
43 #define SJME_TOKEN_PASTE(a, b) a##b
46 * Pasting two tokens but with preprocessing.
48 * @param a The first token.
49 * @param b The second token.
52 #define SJME_TOKEN_PASTE_PP(a, b) SJME_TOKEN_PASTE(a, b)
55 * Pastes three tokens together.
57 * @param a The first token.
58 * @param b The second token.
59 * @param c The third token.
62 #define SJME_TOKEN_PASTE3(a, b, c) a##b##c
65 * Pasting three tokens but with preprocessing.
67 * @param a The first token.
68 * @param b The second token.
69 * @param c The third token.
72 #define SJME_TOKEN_PASTE3_PP(a, b, c) SJME_TOKEN_PASTE3(a, b, c)
75 * Pastes four tokens together.
77 * @param a The first token.
78 * @param b The second token.
79 * @param c The third token.
80 * @param d The fourth token.
83 #define SJME_TOKEN_PASTE4(a, b, c, d) a##b##c##d
86 * Pasting four tokens but with preprocessing.
88 * @param a The first token.
89 * @param b The second token.
90 * @param c The third token.
91 * @param d The fourth token.
94 #define SJME_TOKEN_PASTE4_PP(a, b, c, d) SJME_TOKEN_PASTE4(a, b, c, d)
97 * Pastes five tokens together.
99 * @param a The first token.
100 * @param b The second token.
101 * @param c The third token.
102 * @param d The fourth token.
103 * @param e The fifth token.
106 #define SJME_TOKEN_PASTE5(a, b, c, d, e) a##b##c##d##e
109 * Pasting five tokens but with preprocessing.
111 * @param a The first token.
112 * @param b The second token.
113 * @param c The third token.
114 * @param d The fourth token.
115 * @param e The fifth token.
118 #define SJME_TOKEN_PASTE5_PP(a, b, c, d, e) SJME_TOKEN_PASTE5(a, b, c, d, e)
121 * Stringifies the given token.
123 * @param s The token to stringify.
126 #define SJME_TOKEN_STRING(s) #s
129 * Stringifies the given token.
131 * @param s The token to stringify.
134 #define SJME_TOKEN_STRING_PP(s) SJME_TOKEN_STRING(s)
137 * Represents a single token.
139 * @param t The token to resolve.
142 #define SJME_TOKEN_SINGLE(t) t
144 #define SJME_TOKEN_STARS_0
145 #define SJME_TOKEN_STARS_1 *
146 #define SJME_TOKEN_STARS_2 **
147 #define SJME_TOKEN_STARS_3 ***
148 #define SJME_TOKEN_STARS_4 ****
149 #define SJME_TOKEN_STARS_5 *****
150 #define SJME_TOKEN_STARS_6 ******
151 #define SJME_TOKEN_STARS_7 *******
152 #define SJME_TOKEN_STARS_8 ********
153 #define SJME_TOKEN_STARS_9 *********
155 #define SJME_TOKEN_STARS_C0
156 #define SJME_TOKEN_STARS_C1 P
157 #define SJME_TOKEN_STARS_C2 PP
158 #define SJME_TOKEN_STARS_C3 PPP
159 #define SJME_TOKEN_STARS_C4 PPPP
160 #define SJME_TOKEN_STARS_C5 PPPPP
161 #define SJME_TOKEN_STARS_C6 PPPPPP
162 #define SJME_TOKEN_STARS_C7 PPPPPPP
163 #define SJME_TOKEN_STARS_C8 PPPPPPPP
164 #define SJME_TOKEN_STARS_C9 PPPPPPPPP
166 #define SJME_TOKEN_STARS_H0 0
167 #define SJME_TOKEN_STARS_H1 1
168 #define SJME_TOKEN_STARS_H2 1
169 #define SJME_TOKEN_STARS_H3 1
170 #define SJME_TOKEN_STARS_H4 1
171 #define SJME_TOKEN_STARS_H5 1
172 #define SJME_TOKEN_STARS_H6 1
173 #define SJME_TOKEN_STARS_H7 1
174 #define SJME_TOKEN_STARS_H8 1
175 #define SJME_TOKEN_STARS_H9 1
178 * Generic pointer star types.
180 * @param type The type used.
181 * @param numPointerStars The number of pointer stars.
184 #define SJME_TOKEN_TYPE(type, numPointerStars) \
185 type SJME_TOKEN_SINGLE(SJME_TOKEN_STARS_##numPointerStars)
192 #define SJME_TOKEN_SEMI ;
195 * Does this have pointer stars?
197 * @param numPointerStars The number of pointer stars.
200 #define SJME_TOKEN_HAS_STARS(numPointerStars) \
201 SJME_TOKEN_SINGLE(SJME_TOKEN_STARS_H##numPointerStars)
203 /** SquirrelJME version string. */
204 #define SQUIRRELJME_VERSION SJME_TOKEN_STRING_PP(SQUIRRELJME_VERSION_TRIM)
207 * Calculates the size of a struct member.
209 * @param type The type of the struct.
210 * @param member The member to check.
211 * @return The size of the given member.
214 #define SJME_SIZEOF_STRUCT_MEMBER(type, member) \
215 (sizeof((*((type*)0)).member))
218 * Uncommon structure size.
220 * @param structType The structure type.
221 * @param uncommonMember The uncommon member.
222 * @param uncommonSize The uncommon size to use.
225 #define SJME_SIZEOF_UNCOMMON_N(structType, uncommonMember, uncommonSize) \
226 (sizeof(structType) + (offsetof(structType, \
227 uncommonMember[0]) - offsetof(structType, uncommonMember)) + \
231 * Returns the uncommon member.
233 * @param structType The structure type.
234 * @param uncommonMember The uncommon member.
235 * @param uncommonType The uncommon type.
236 * @param base The base structure pointer to access.
239 #define SJME_UNCOMMON_MEMBER(structType, uncommonMember, uncommonType, base) \
240 ((uncommonType*)(void*)(&((structType*)((base)))->uncommonMember))
243 * Basic data type identifier.
247 typedef enum sjme_basicTypeId
250 SJME_BASIC_TYPE_ID_INTEGER
= 0,
253 SJME_JAVA_TYPE_ID_INTEGER
= SJME_BASIC_TYPE_ID_INTEGER
,
256 SJME_BASIC_TYPE_ID_LONG
= 1,
259 SJME_JAVA_TYPE_ID_LONG
= SJME_BASIC_TYPE_ID_LONG
,
262 SJME_BASIC_TYPE_ID_FLOAT
= 2,
265 SJME_JAVA_TYPE_ID_FLOAT
= SJME_BASIC_TYPE_ID_FLOAT
,
268 SJME_BASIC_TYPE_ID_DOUBLE
= 3,
271 SJME_JAVA_TYPE_ID_DOUBLE
= SJME_BASIC_TYPE_ID_DOUBLE
,
274 SJME_BASIC_TYPE_ID_OBJECT
= 4,
277 SJME_JAVA_TYPE_ID_OBJECT
= SJME_BASIC_TYPE_ID_OBJECT
,
279 /** The number of Java type IDs. */
280 SJME_NUM_JAVA_TYPE_IDS
= 5,
282 /** Boolean or byte. */
283 SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
= SJME_NUM_JAVA_TYPE_IDS
,
285 /** Character or short type. */
286 SJME_JAVA_TYPE_ID_SHORT_OR_CHAR
= 6,
289 SJME_BASIC_TYPE_ID_VOID
= 7,
292 SJME_JAVA_TYPE_ID_VOID
= SJME_BASIC_TYPE_ID_VOID
,
294 /** End of extended Java types. */
295 SJME_NUM_EXTENDED_JAVA_TYPE_IDS
= 8,
298 SJME_BASIC_TYPE_ID_SHORT
= SJME_NUM_EXTENDED_JAVA_TYPE_IDS
,
301 SJME_BASIC_TYPE_ID_CHARACTER
= 9,
303 /** Specifically boolean. */
304 SJME_BASIC_TYPE_ID_BOOLEAN
= 10,
306 /** Specifically jbyte. */
307 SJME_BASIC_TYPE_ID_BYTE
= 11,
309 /** Number of basic type IDs. */
310 SJME_NUM_BASIC_TYPE_IDS
= 11
314 * Returns the basic type ID of the given type.
316 * @param type The type to get the basic type of.
319 #define SJME_TYPEOF_BASIC(type) SJME_TOKEN_PASTE_PP(SJME_TYPEOF_BASIC_, type)
322 * Returns the java type ID of the given type.
324 * @param type The type to get the basic type of.
327 #define SJME_TYPEOF_JAVA(type) SJME_TOKEN_PASTE_PP(SJME_TYPEOF_JAVA_, type)
329 #define SJME_TYPEOF_IS_POINTER_X00 0
330 #define SJME_TYPEOF_IS_POINTER_X10 1
331 #define SJME_TYPEOF_IS_POINTER_X01 1
332 #define SJME_TYPEOF_IS_POINTER_X11 1
335 * Is the given type a pointer?
337 * @param type The type used.
338 * @param numPointerStars The number of pointer stars.
339 * @return If this is a pointer or not.
342 #define SJME_TYPEOF_IS_POINTER(type, numPointerStars) \
343 SJME_TOKEN_SINGLE(SJME_TOKEN_PASTE3_PP(SJME_TYPEOF_IS_POINTER_X, \
344 SJME_TOKEN_PASTE_PP(SJME_TYPEOF_IS_POINTER_, type), \
345 SJME_TOKEN_PASTE_PP(SJME_TOKEN_STARS_H, numPointerStars)))
347 #define SJME_TYPEOF_IS_NOT_POINTER_X0 1
348 #define SJME_TYPEOF_IS_NOT_POINTER_X1 0
351 * Is the given type not a pointer?
353 * @param type The type used.
354 * @param numPointerStars The number of pointer stars.
355 * @return If this is not a pointer or it is one.
358 #define SJME_TYPEOF_IS_NOT_POINTER(type, numPointerStars) \
359 SJME_TOKEN_SINGLE(SJME_TOKEN_PASTE_PP(SJME_TYPEOF_IS_NOT_POINTER_X, \
360 SJME_TYPEOF_IS_POINTER(type, numPointerStars)))
362 #define SJME_TYPEOF_IF_POINTER_X0(snippet)
363 #define SJME_TYPEOF_IF_POINTER_X1(snippet) snippet
366 * If the type is a pointer, place the given snippet.
368 * @param type The type used.
369 * @param numPointerStars The number of pointer stars
370 * @param snippet The snippet to place.
371 * @return Either @c snippet or nothing.
374 #define SJME_TYPEOF_IF_POINTER(type, numPointerStars, snippet) \
375 SJME_TOKEN_PASTE_PP(SJME_TYPEOF_IF_POINTER_X, \
376 SJME_TYPEOF_IS_POINTER(type, numPointerStars))(snippet)
379 * If the type is not a pointer, place the given snippet.
381 * @param type The type used.
382 * @param numPointerStars The number of pointer stars
383 * @param snippet The snippet to place.
384 * @return Either @c snippet or nothing.
387 #define SJME_TYPEOF_IF_NOT_POINTER(type, numPointerStars, snippet) \
388 SJME_TOKEN_PASTE_PP(SJME_TYPEOF_IF_POINTER_X, \
389 SJME_TYPEOF_IS_NOT_POINTER(type, numPointerStars))(snippet)
391 #define SJME_TYPEOF_IF_POINTER_ORX0(snippet, orSnippet) orSnippet
392 #define SJME_TYPEOF_IF_POINTER_ORX1(snippet, orSnippet) snippet
395 * If the type is a pointer, place the given snippet.
397 * @param type The type used.
398 * @param numPointerStars The number of pointer stars
399 * @param snippet The snippet to place.
400 * @param orSnippet The snippet if it is a pointer.
401 * @return Either @c snippet or nothing.
404 #define SJME_TYPEOF_IF_POINTER_OR(type, numPointerStars, snippet, orSnippet) \
405 SJME_TOKEN_PASTE_PP(SJME_TYPEOF_IF_POINTER_ORX, \
406 SJME_TYPEOF_IS_POINTER(type, numPointerStars))(snippet, orSnippet)
409 * If the type is not a pointer, place the given snippet.
411 * @param type The type used.
412 * @param numPointerStars The number of pointer stars
413 * @param snippet The snippet to place.
414 * @param orSnippet The snippet if it is a pointer.
415 * @return Either @c snippet or nothing.
418 #define SJME_TYPEOF_IF_NOT_POINTER_OR(type, numPointerStars, snippet, \
420 SJME_TOKEN_PASTE_PP(SJME_TYPEOF_IF_POINTER_ORX, \
421 SJME_TYPEOF_IS_NOT_POINTER(type, numPointerStars))(snippet, orSnippet)
428 typedef enum sjme_jboolean
435 /** Basic @c sjme_jboolean type identifier. */
436 #define SJME_TYPEOF_BASIC_sjme_jboolean SJME_BASIC_TYPE_ID_BOOLEAN
438 /** Java @c sjme_jboolean type identifier. */
439 #define SJME_TYPEOF_JAVA_sjme_jboolean SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
441 /** Is a pointer for @c sjme_jboolean ? */
442 #define SJME_TYPEOF_IS_POINTER_sjme_jboolean 0
449 typedef int8_t sjme_jbyte
;
451 /** Basic @c sjme_jbyte type identifier. */
452 #define SJME_TYPEOF_BASIC_sjme_jbyte SJME_BASIC_TYPE_ID_BYTE
454 /** Java @c sjme_jbyte type identifier. */
455 #define SJME_TYPEOF_JAVA_sjme_jbyte SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
457 /** Is a pointer for @c sjme_jbyte ? */
458 #define SJME_TYPEOF_IS_POINTER_sjme_jbyte 0
461 * Unsigned byte type.
465 typedef uint8_t sjme_jubyte
;
467 /** Basic @c sjme_jubyte type identifier. */
468 #define SJME_TYPEOF_BASIC_sjme_jubyte SJME_BASIC_TYPE_ID_BYTE
470 /** Java @c sjme_jubyte type identifier. */
471 #define SJME_TYPEOF_JAVA_sjme_jubyte SJME_JAVA_TYPE_ID_BOOLEAN_OR_BYTE
473 /** Is a pointer for @c sjme_jubyte ? */
474 #define SJME_TYPEOF_IS_POINTER_sjme_jubyte 0
481 typedef int16_t sjme_jshort
;
483 /** Basic @c sjme_jshort type identifier. */
484 #define SJME_TYPEOF_BASIC_sjme_jshort SJME_BASIC_TYPE_ID_SHORT
486 /** Java @c sjme_jshort type identifier. */
487 #define SJME_TYPEOF_JAVA_sjme_jshort SJME_JAVA_TYPE_ID_INTEGER
489 /** Is a pointer for @c sjme_jshort ? */
490 #define SJME_TYPEOF_IS_POINTER_sjme_jshort 0
497 typedef uint16_t sjme_jchar
;
499 /** Basic @c sjme_jchar type identifier. */
500 #define SJME_TYPEOF_BASIC_sjme_jchar SJME_BASIC_TYPE_ID_CHARACTER
502 /** Java @c sjme_jchar type identifier. */
503 #define SJME_TYPEOF_JAVA_sjme_jchar SJME_JAVA_TYPE_ID_INTEGER
505 /** Is a pointer for @c sjme_jchar ? */
506 #define SJME_TYPEOF_IS_POINTER_sjme_jchar 0
513 typedef int32_t sjme_jint
;
515 /** Basic @c sjme_jint type identifier. */
516 #define SJME_TYPEOF_BASIC_sjme_jint SJME_BASIC_TYPE_ID_INTEGER
518 /** Java @c sjme_jint type identifier. */
519 #define SJME_TYPEOF_JAVA_sjme_jint SJME_JAVA_TYPE_ID_INTEGER
521 /** Is a pointer for @c sjme_jint ? */
522 #define SJME_TYPEOF_IS_POINTER_sjme_jint 0
525 * Unsigned integer type.
529 typedef uint32_t sjme_juint
;
531 /** Basic @c sjme_juint type identifier. */
532 #define SJME_TYPEOF_BASIC_sjme_juint SJME_BASIC_TYPE_ID_INTEGER
534 /** Java @c sjme_juint type identifier. */
535 #define SJME_TYPEOF_JAVA_sjme_juint SJME_JAVA_TYPE_ID_INTEGER
537 /** Is a pointer for @c sjme_juint ? */
538 #define SJME_TYPEOF_IS_POINTER_sjme_juint 0
545 typedef char sjme_cchar
;
547 #if defined(CHAR_BIT) && (CHAR_BIT == 64)
548 /** Basic @c sjme_cchar type identifier. */
549 #define SJME_TYPEOF_BASIC_sjme_cchar SJME_BASIC_TYPE_ID_LONG
550 #elif defined(CHAR_BIT) && (CHAR_BIT == 32)
551 /** Basic @c sjme_cchar type identifier. */
552 #define SJME_TYPEOF_BASIC_sjme_cchar SJME_BASIC_TYPE_ID_INTEGER
553 #elif defined(CHAR_BIT) && (CHAR_BIT == 16)
554 /** Basic @c sjme_cchar type identifier. */
555 #define SJME_TYPEOF_BASIC_sjme_cchar SJME_BASIC_TYPE_ID_SHORT
557 /** Basic @c sjme_cchar type identifier. */
558 #define SJME_TYPEOF_BASIC_sjme_cchar SJME_BASIC_TYPE_ID_BYTE
561 /** Is a pointer for @c sjme_cchar ? */
562 #define SJME_TYPEOF_IS_POINTER_sjme_cchar 0
565 * Pointer to C string.
569 typedef sjme_cchar
* sjme_lpstr
;
571 /** Basic @c sjme_lpstr type identifier. */
572 #define SJME_TYPEOF_BASIC_sjme_lpstr SJME_BASIC_TYPE_ID_OBJECT
574 /** Is a pointer for @c sjme_lpstr ? */
575 #define SJME_TYPEOF_IS_POINTER_sjme_lpstr 1
578 * Pointer to constant C string.
582 typedef const sjme_cchar
* sjme_lpcstr
;
584 /** Basic @c sjme_lpcstr type identifier. */
585 #define SJME_TYPEOF_BASIC_sjme_lpcstr SJME_BASIC_TYPE_ID_OBJECT
587 /** Is a pointer for @c sjme_lpcstr ? */
588 #define SJME_TYPEOF_IS_POINTER_sjme_lpcstr 1
595 typedef void* sjme_pointer
;
597 /** Basic @c sjme_pointer type identifier. */
598 #define SJME_TYPEOF_BASIC_sjme_pointer SJME_BASIC_TYPE_ID_OBJECT
600 /** Is a pointer for @c sjme_pointer ? */
601 #define SJME_TYPEOF_IS_POINTER_sjme_pointer 1
604 * Integer based pointer.
608 typedef intptr_t sjme_intPointer
;
615 typedef struct sjme_jlong
617 #if defined(SJME_CONFIG_HAS_LITTLE_ENDIAN)
632 /** Basic @c sjme_jlong type identifier. */
633 #define SJME_TYPEOF_BASIC_sjme_jlong SJME_BASIC_TYPE_ID_LONG
635 /** Java @c sjme_jlong type identifier. */
636 #define SJME_TYPEOF_JAVA_sjme_jlong SJME_JAVA_TYPE_ID_LONG
638 /** Is a pointer for @c sjme_jlong ? */
639 #define SJME_TYPEOF_IS_POINTER_sjme_jlong 0
646 typedef struct sjme_jfloat
651 /** Basic @c sjme_jfloat type identifier. */
652 #define SJME_TYPEOF_BASIC_sjme_jfloat SJME_BASIC_TYPE_ID_FLOAT
654 /** Java @c sjme_jfloat type identifier. */
655 #define SJME_TYPEOF_JAVA_sjme_jfloat SJME_JAVA_TYPE_ID_FLOAT
657 /** Is a pointer for @c sjme_jfloat ? */
658 #define SJME_TYPEOF_IS_POINTER_sjme_jfloat 0
665 typedef struct sjme_jdouble
667 #if defined(SJME_CONFIG_HAS_LITTLE_ENDIAN)
682 /** Basic @c sjme_jdouble type identifier. */
683 #define SJME_TYPEOF_BASIC_sjme_jdouble SJME_BASIC_TYPE_ID_DOUBLE
685 /** Java @c sjme_jdouble type identifier. */
686 #define SJME_TYPEOF_JAVA_sjme_jdouble SJME_JAVA_TYPE_ID_DOUBLE
688 /** Is a pointer for @c sjme_jdouble ? */
689 #define SJME_TYPEOF_IS_POINTER_sjme_jdouble 0
696 typedef sjme_jint sjme_tempIndex
;
703 typedef struct sjme_range
705 /** Start of the range. */
708 /** End of the range. */
713 * Represents a pointer and a length.
717 typedef struct sjme_pointerLen
719 /** The pointer to the data. */
720 sjme_pointer pointer
;
722 /** The length of the pointer. */
727 * A wrapper used by front ends, which is reserved for use, which stores a
728 * natively bound object accordingly as needed.
732 typedef void* sjme_frontEndWrapper
;
735 * Any data that is needed by the front end, which is reserved for use.
739 typedef void* sjme_frontEndData
;
742 * This structure stores any front end data as needed.
746 typedef struct sjme_frontEnd
748 /** Any wrapper as needed. */
749 sjme_frontEndWrapper wrapper
;
751 /** Any data as needed. */
752 sjme_frontEndData data
;
756 * Wraps the given front end pointer.
758 * @param p The pointer to wrap.
761 #define SJME_FRONT_END_WRAP(p) ((sjme_frontEndWrapper)(p))
763 /** The Java type ID. */
764 typedef sjme_basicTypeId sjme_javaTypeId
;
767 * Represents multiple type IDs.
771 typedef struct sjme_basicTypeIds
773 /** The number of IDs. */
777 const sjme_javaTypeId ids
[sjme_flexibleArrayCount
];
781 * Program counter address.
785 typedef sjme_jint sjme_pcAddr
;
788 * Static linkage type.
792 typedef sjme_jint sjme_staticLinkageType
;
795 * Base object information.
799 typedef struct sjme_jobjectBase
801 /** The reference count of this object, zero it becomes GCed. */
810 typedef sjme_jobjectBase
* sjme_jobject
;
813 * Generic value union.
817 typedef union sjme_jvalue
843 /** Object/Reference. */
852 typedef sjme_jobject sjme_jclass
;
854 /** Basic @c sjme_jobject type identifier. */
855 #define SJME_TYPEOF_BASIC_sjme_jobject SJME_BASIC_TYPE_ID_OBJECT
857 /** Java @c sjme_jobject type identifier. */
858 #define SJME_TYPEOF_JAVA_sjme_jobject SJME_BASIC_TYPE_ID_OBJECT
860 /** Is a pointer for @c sjme_jobject ? */
861 #define SJME_TYPEOF_IS_POINTER_sjme_jobject 1
868 typedef sjme_jobject sjme_jthrowable
;
870 typedef union sjme_anyData
876 sjme_jobject jobject
;
878 /** Temporary index. */
879 sjme_tempIndex tempIndex
;
882 typedef struct sjme_any
884 /** Data type used. */
885 sjme_basicTypeId type
;
887 /** Data stored within. */
892 * Represents the virtual machine state.
896 typedef struct sjme_nvm_state sjme_nvm_state
;
899 * Frame of execution within a thread.
903 typedef struct sjme_nvm_frame sjme_nvm_frame
;
906 * Exception stack trace mechanism storage.
910 typedef volatile struct sjme_exceptTrace sjme_exceptTrace
;
912 typedef struct sjme_nvm_thread
914 /** The VM state this thread is in. */
915 sjme_nvm_state
* inState
;
917 /** The wrapper in the front end. */
918 sjme_frontEnd frontEnd
;
920 /** The thread ID. */
923 /** The top of the stack. */
926 /** The number of frames. */
929 /** Current exception handler go back. */
930 sjme_exceptTrace
* except
;
933 typedef struct sjme_static_constValue
935 /** Integer value. */
945 sjme_jdouble jdouble
;
952 } sjme_static_constValue
;
955 * Represents a field type.
959 typedef struct sjme_static_fieldType
961 /** The hash code for the field type. */
964 /** The field descriptor. */
965 sjme_lpcstr descriptor
;
967 /** The basic type. */
968 sjme_basicTypeId basicType
;
969 } sjme_static_fieldType
;
971 typedef struct sjme_static_classField
976 /** The field type. */
977 const sjme_static_fieldType
* type
;
982 /** The constant value type. */
983 sjme_basicTypeId valueType
;
986 sjme_static_constValue value
;
987 } sjme_static_classField
;
989 typedef struct sjme_static_classFields
991 /** The number of fields. */
995 sjme_static_classField fields
[sjme_flexibleArrayCount
];
996 } sjme_static_classFields
;
999 * Type used for method code functions.
1001 * @param currentState The current virtual machine state.
1002 * @param currentThread The current virtual machine thread.
1003 * @return Will return @c SJME_JNI_TRUE if execution completed without throwing
1004 * a @c Throwable object.
1007 typedef sjme_jboolean (*sjme_methodCodeFunction
)(
1008 struct sjme_nvm_state
* currentState
,
1009 struct sjme_nvm_thread
* currentThread
);
1012 * The variable mapping and setup for any given method.
1016 typedef struct sjme_static_classCodeLimits
1018 /** The maximum number of @c sjme_basicTypeId local/stack variables. */
1019 const sjme_jubyte maxVariables
[SJME_NUM_JAVA_TYPE_IDS
];
1020 } sjme_static_classCodeLimits
;
1023 * Contains information about method code and how variables should be placed
1024 * on execution and stack handling.
1028 typedef struct sjme_static_classCode
1030 /** The variable count and thrown index count used. */
1031 const sjme_static_classCodeLimits
* limits
;
1033 /** The index where thrown objects are placed. */
1034 sjme_jshort thrownVarIndex
;
1036 /** The method code. */
1037 sjme_methodCodeFunction code
;
1038 } sjme_static_classCode
;
1041 * Represents a standard Java method type, using field descriptors.
1045 typedef struct sjme_static_methodType
1047 /** The hash code for the method type. */
1050 /** The descriptor for the method type. */
1051 sjme_lpcstr descriptor
;
1053 /** The return type. */
1054 const sjme_static_fieldType
* returnType
;
1056 /** The number of arguments. */
1059 /** The arguments to the method. */
1060 const sjme_static_fieldType
* argTypes
[0];
1061 } sjme_static_methodType
;
1063 typedef struct sjme_static_classMethod
1072 const sjme_static_methodType
* type
;
1074 /** Method code and any pertaining information. */
1075 const sjme_static_classCode
* code
;
1076 } sjme_static_classMethod
;
1078 typedef struct sjme_static_classMethods
1080 /** The number of methods. */
1084 sjme_static_classMethod methods
[sjme_flexibleArrayCount
];
1085 } sjme_static_classMethods
;
1087 typedef struct sjme_static_classInterface
1089 sjme_lpcstr interfaceName
;
1090 } sjme_static_classInterface
;
1092 typedef struct sjme_static_classInterfaces
1094 /** The number of interfaces. */
1098 sjme_static_classInterface interfaces
[sjme_flexibleArrayCount
];
1099 } sjme_static_classInterfaces
;
1101 typedef struct sjme_static_resource
1103 /** The resource path. */
1106 /** The hash for the path. */
1109 /** The size of the resource. */
1112 /** The resource data. */
1113 const sjme_jbyte data
[sjme_flexibleArrayCount
];
1114 } sjme_static_resource
;
1116 typedef struct sjme_static_linkage_data_classObject
1118 /** The class name. */
1119 sjme_lpcstr className
;
1120 } sjme_static_linkage_data_classObject
;
1122 typedef struct sjme_static_linkage_data_fieldAccess
1124 /** Is this static? */
1125 sjme_jboolean isStatic
;
1127 /** Is this a store? */
1128 sjme_jboolean isStore
;
1130 /** The source method name. */
1131 sjme_lpcstr sourceMethodName
;
1133 /** The source method type. */
1134 sjme_lpcstr sourceMethodType
;
1136 /** The target class. */
1137 sjme_lpcstr targetClass
;
1139 /** The target field name. */
1140 sjme_lpcstr targetFieldName
;
1142 /** The target field type. */
1143 sjme_lpcstr targetFieldType
;
1144 } sjme_static_linkage_data_fieldAccess
;
1146 typedef struct sjme_static_linkage_data_invokeSpecial
1148 /** The source method name. */
1149 sjme_lpcstr sourceMethodName
;
1151 /** The source method type. */
1152 sjme_lpcstr sourceMethodType
;
1154 /** The target class. */
1155 sjme_lpcstr targetClass
;
1157 /** The target method name. */
1158 sjme_lpcstr targetMethodName
;
1160 /** The target method type. */
1161 sjme_lpcstr targetMethodType
;
1162 } sjme_static_linkage_data_invokeSpecial
;
1164 typedef struct sjme_static_linkage_data_invokeNormal
1166 /** Is this a static invocation? */
1167 sjme_jboolean isStatic
;
1169 /** The source method name. */
1170 sjme_lpcstr sourceMethodName
;
1172 /** The source method type. */
1173 sjme_lpcstr sourceMethodType
;
1175 /** The target class. */
1176 sjme_lpcstr targetClass
;
1178 /** The target method name. */
1179 sjme_lpcstr targetMethodName
;
1181 /** The target method type. */
1182 sjme_lpcstr targetMethodType
;
1183 } sjme_static_linkage_data_invokeNormal
;
1185 typedef struct sjme_static_linkage_data_stringObject
1187 /** The string value. */
1189 } sjme_static_linkage_data_stringObject
;
1191 typedef union sjme_static_linkage_data
1193 /** Reference to class object. */
1194 sjme_static_linkage_data_classObject classObject
;
1196 /** Field access. */
1197 sjme_static_linkage_data_fieldAccess fieldAccess
;
1199 /** Special invocation. */
1200 sjme_static_linkage_data_invokeSpecial invokeSpecial
;
1202 /** Normal invocation. */
1203 sjme_static_linkage_data_invokeNormal invokeNormal
;
1205 /** String object. */
1206 sjme_static_linkage_data_stringObject stringObject
;
1207 } sjme_static_linkage_data
;
1214 typedef struct sjme_static_linkage
1216 /** The type of linkage this is. */
1217 sjme_staticLinkageType type
;
1219 /** Linkage data. */
1220 sjme_static_linkage_data data
;
1221 } sjme_static_linkage
;
1223 typedef struct sjme_static_linkages
1225 /** The number of linkages. */
1228 /** The define linkages. */
1229 sjme_static_linkage linkages
[sjme_flexibleArrayCount
];
1230 } sjme_static_linkages
;
1232 typedef struct sjme_dynamic_linkage_data_classObject
1235 } sjme_dynamic_linkage_data_classObject
;
1237 typedef struct sjme_dynamic_linkage_data_fieldAccess
1240 } sjme_dynamic_linkage_data_fieldAccess
;
1242 typedef struct sjme_dynamic_linkage_data_invokeSpecial
1245 } sjme_dynamic_linkage_data_invokeSpecial
;
1247 typedef struct sjme_dynamic_linkage_data_invokeNormal
1250 } sjme_dynamic_linkage_data_invokeNormal
;
1252 typedef struct sjme_dynamic_linkage_data_stringObject
1255 } sjme_dynamic_linkage_data_stringObject
;
1257 typedef union sjme_dynamic_linkage_data
1259 /** Reference to class object. */
1260 sjme_dynamic_linkage_data_classObject classObject
;
1262 /** Field access. */
1263 sjme_dynamic_linkage_data_fieldAccess fieldAccess
;
1265 /** Special invocation. */
1266 sjme_dynamic_linkage_data_invokeSpecial invokeSpecial
;
1268 /** Normal invocation. */
1269 sjme_dynamic_linkage_data_invokeNormal invokeNormal
;
1271 /** String object. */
1272 sjme_dynamic_linkage_data_stringObject stringObject
;
1273 } sjme_dynamic_linkage_data
;
1280 typedef struct sjme_dynamic_linkage
1282 /** The type of linkage this is. */
1283 sjme_staticLinkageType type
;
1285 /** Linkage data. */
1286 sjme_dynamic_linkage_data data
;
1287 } sjme_dynamic_linkage
;
1290 * Represents the frame of a stack tread.
1294 typedef struct sjme_nvm_frameTread
1296 /** The number of items in this tread. */
1299 /** The base index for the stack index. */
1300 sjme_jint stackBaseIndex
;
1302 /** The maximum size this tread can be. */
1305 /** Values within the tread. */
1308 /** Integer values. */
1309 sjme_jint jints
[sjme_flexibleArrayCountUnion
];
1312 sjme_jlong jlongs
[sjme_flexibleArrayCountUnion
];
1314 /** Float values. */
1315 sjme_jfloat jfloats
[sjme_flexibleArrayCountUnion
];
1317 /** Double values. */
1318 sjme_jdouble jdoubles
[sjme_flexibleArrayCountUnion
];
1320 /** Object references. */
1321 sjme_jobject jobjects
[sjme_flexibleArrayCountUnion
];
1323 } sjme_nvm_frameTread
;
1326 * Calculates the size of a frame tread for a given type.
1328 * @param type The type to get the size for.
1329 * @param count The number if items to store.
1330 * @return The size in bytes for the tread.
1333 #define SJME_SIZEOF_FRAME_TREAD(type, count, baseType) \
1334 (sizeof(sjme_nvm_frameTread) + \
1335 /* Need to handle cases where values could be aligned up... */ \
1336 (offsetof(sjme_nvm_frameTread, values.SJME_TOKEN_PASTE(baseType,s)[0]) - \
1337 offsetof(sjme_nvm_frameTread, values)) + \
1338 (sizeof(type) * (size_t)(count)))
1341 * Calculates the size of a frame tread for a given type via variable.
1343 * @param typeId The type to get the size for.
1344 * @param count The number if items to store.
1345 * @return The size in bytes for the tread.
1348 static sjme_inline sjme_attrArtificial
size_t SJME_SIZEOF_FRAME_TREAD_VAR(
1349 sjme_javaTypeId typeId
, sjme_jint count
)
1353 case SJME_JAVA_TYPE_ID_INTEGER
:
1354 return SJME_SIZEOF_FRAME_TREAD(sjme_jint
, count
, jint
);
1356 case SJME_JAVA_TYPE_ID_LONG
:
1357 return SJME_SIZEOF_FRAME_TREAD(sjme_jlong
, count
, jlong
);
1359 case SJME_JAVA_TYPE_ID_FLOAT
:
1360 return SJME_SIZEOF_FRAME_TREAD(sjme_jfloat
, count
, jfloat
);
1362 case SJME_JAVA_TYPE_ID_DOUBLE
:
1363 return SJME_SIZEOF_FRAME_TREAD(sjme_jdouble
, count
, jdouble
);
1365 case SJME_JAVA_TYPE_ID_OBJECT
:
1366 return SJME_SIZEOF_FRAME_TREAD(sjme_jobject
, count
, jobject
);
1374 * Represents information on a frame's stack storage.
1378 typedef struct sjme_nvm_frameStack
1380 /** The number of items in the stack. */
1383 /** The current limit of this structure. */
1386 /** The stack order. */
1387 sjme_javaTypeId order
[sjme_flexibleArrayCount
];
1388 } sjme_nvm_frameStack
;
1391 * Calculates the size of a frame stack.
1393 * @param count The number if items to store.
1394 * @return The size in bytes for the tread.
1397 #define SJME_SIZEOF_FRAME_STACK(count) \
1398 (sizeof(sjme_nvm_frameStack) + \
1399 (sizeof(sjme_javaTypeId) * (size_t)(count)))
1401 typedef struct sjme_nvm_frameLocalMap
1403 /** The maximum number of locals. */
1406 /** Mapping of a specific variable to a given type index. */
1409 sjme_jbyte to
[SJME_NUM_JAVA_TYPE_IDS
];
1410 } maps
[sjme_flexibleArrayCount
];
1411 } sjme_nvm_frameLocalMap
;
1414 * Calculates the size of the frame local variable map.
1416 * @param count The number of items in the mapping.
1417 * @return The size in bytes of the local mapping.
1420 #define SJME_SIZEOF_FRAME_LOCAL_MAP(count) \
1421 (sizeof(sjme_nvm_frameLocalMap) + \
1422 (SJME_SIZEOF_STRUCT_MEMBER(sjme_nvm_frameLocalMap, maps[0]) * (count)))
1424 struct sjme_nvm_frame
1426 /** The thread this frame is in. */
1427 sjme_nvm_thread
* inThread
;
1429 /** The wrapper in the front end. */
1430 sjme_frontEnd frontEnd
;
1432 /** The parent frame. */
1433 sjme_nvm_frame
* parent
;
1435 /** The frame index in the thread. */
1436 sjme_jint frameIndex
;
1438 /** The current program counter. */
1441 /** Object which is waiting to be thrown for exception handling. */
1442 sjme_jobject waitingThrown
;
1444 /** Frame linkage. */
1445 sjme_dynamic_linkage
* linkage
;
1447 /** Temporary stack. */
1448 sjme_any
* tempStack
;
1450 /** Reference to this. */
1451 sjme_jobject thisRef
;
1453 /** Class reference. */
1454 sjme_jclass classObjectRef
;
1456 /** The current stack information. */
1457 sjme_nvm_frameStack
* stack
;
1459 /** Treads for the stack and locals. */
1460 sjme_nvm_frameTread
* treads
[SJME_NUM_BASIC_TYPE_IDS
];
1462 /** Mapping of local variables to the tread indexes per type. */
1463 const sjme_nvm_frameLocalMap
* localMap
;
1467 * Contains the payload information.
1471 typedef struct sjme_payload_config sjme_payload_config
;
1474 * Hook for garbage collection detection and/or cancel capability.
1476 * @param frame The frame this is garbage collecting in.
1477 * @param gcWhat what is being garbage collected?
1478 * @return Returns @c SJME_JNI_TRUE if garbage collection should continue.
1481 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc
)(sjme_nvm_frame
* frame
,
1482 sjme_jobject gcWhat
);
1485 * Hooks for alternative function.
1489 typedef struct sjme_nvm_stateHooks
1491 /** Garbage collection. */
1492 sjme_nvm_stateHookGcFunc gc
;
1493 } sjme_nvm_stateHooks
;
1496 * Structure which stores the pooled memory allocator.
1500 typedef struct sjme_alloc_pool sjme_alloc_pool
;
1503 * Boot parameters for NanoCoat.
1507 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam
;
1510 * Standard Suite structure.
1514 typedef struct sjme_rom_suiteCore sjme_rom_suiteCore
;
1517 * Opaque suite structure type.
1521 typedef struct sjme_rom_suiteCore
* sjme_rom_suite
;
1524 * Structure for a single task.
1528 typedef struct sjme_nvm_taskCore
* sjme_nvm_task
;
1531 * Represents the virtual machine state.
1535 struct sjme_nvm_state
1537 /** The wrapper in the front end. */
1538 sjme_frontEnd frontEnd
;
1540 /** The memory pool to use for allocations. */
1541 sjme_alloc_pool
* allocPool
;
1543 /** The reserved memory pool. */
1544 sjme_alloc_pool
* reservedPool
;
1546 /** The copy of the input boot parameters. */
1547 const sjme_nvm_bootParam
* bootParamCopy
;
1549 /** Hooks for the state. */
1550 const sjme_nvm_stateHooks
* hooks
;
1552 /* The suite containing all the libraries. */
1553 sjme_rom_suite suite
;
1557 * Method initialization start.
1561 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
1564 * Method closing end.
1568 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
1575 typedef enum sjme_errorCode
1578 SJME_ERROR_NONE
= 1,
1580 /** Generic unknown error. */
1581 SJME_ERROR_UNKNOWN
= 0,
1583 /** Generic unknown error. */
1584 SJME_ERROR_UNKNOWN_NEGATIVE
= -1,
1586 /** Null arguments. */
1587 SJME_ERROR_NULL_ARGUMENTS
= -2,
1589 /** Local variable out of bounds. */
1590 SJME_ERROR_LOCAL_INDEX_INVALID
= -3,
1592 /** Stack variable out of bounds. */
1593 SJME_ERROR_STACK_INDEX_INVALID
= -4,
1595 /** Stack underflow. */
1596 SJME_ERROR_STACK_UNDERFLOW
= -5,
1598 /** Stack overflow. */
1599 SJME_ERROR_STACK_OVERFLOW
= -6,
1601 /** Top is not an integer type. */
1602 SJME_ERROR_TOP_NOT_INTEGER
= -7,
1604 /** Top is not a long type. */
1605 SJME_ERROR_TOP_NOT_LONG
= -8,
1607 /** Top is not a float type. */
1608 SJME_ERROR_TOP_NOT_FLOAT
= -9,
1610 /** Top is not a double type. */
1611 SJME_ERROR_TOP_NOT_DOUBLE
= -10,
1613 /** Top is not a object type. */
1614 SJME_ERROR_TOP_NOT_OBJECT
= -11,
1616 /** Frame is missing stack treads. */
1617 SJME_ERROR_FRAME_MISSING_STACK_TREADS
= -12,
1619 /** Invalid read of stack. */
1620 SJME_ERROR_STACK_INVALID_READ
= -13,
1622 /** Invalid write of stack. */
1623 SJME_ERROR_STACK_INVALID_WRITE
= -14,
1625 /** Invalid read of stack. */
1626 SJME_ERROR_LOCAL_INVALID_READ
= -15,
1628 /** Invalid write of stack. */
1629 SJME_ERROR_LOCAL_INVALID_WRITE
= -16,
1631 /** Invalid reference pop. */
1632 SJME_ERROR_INVALID_REFERENCE_POP
= -17,
1634 /** Invalid reference push. */
1635 SJME_ERROR_INVALID_REFERENCE_PUSH
= -18,
1637 /** Failed to garbage collect object. */
1638 SJME_ERROR_COULD_NOT_GC_OBJECT
= -19,
1640 /** Object reference count is not zero. */
1641 SJME_ERROR_OBJECT_REFCOUNT_NOT_ZERO
= -20,
1643 /** Garbage collection of object cancelled. */
1644 SJME_ERROR_OBJECT_GC_CANCELLED
= -21,
1646 /** Out of memory. */
1647 SJME_ERROR_OUT_OF_MEMORY
= -22,
1649 /** Pool initialization failed. */
1650 SJME_ERROR_POOL_INIT_FAILED
= -23,
1652 /** Invalid argument. */
1653 SJME_ERROR_INVALID_ARGUMENT
= -24,
1655 /** Not implemented. */
1656 SJME_ERROR_NOT_IMPLEMENTED
= -25,
1658 /** Invalid tread read. */
1659 SJME_ERROR_TREAD_INVALID_READ
= -26,
1661 /** Invalid tread write. */
1662 SJME_ERROR_TREAD_INVALID_WRITE
= -27,
1664 /** There are no suites available. */
1665 SJME_ERROR_NO_SUITES
= -28,
1667 /** Classpath cannot be obtained by both ID and Name. */
1668 SJME_ERROR_CLASS_PATH_BY_BOTH
= -29,
1670 /** Illegal state. */
1671 SJME_ERROR_ILLEGAL_STATE
= -30,
1673 /** A library was not found. */
1674 SJME_ERROR_LIBRARY_NOT_FOUND
= -31,
1676 /** Boot failure. */
1677 SJME_ERROR_BOOT_FAILURE
= -32,
1679 /** Generic JNI exception. */
1680 SJME_ERROR_JNI_EXCEPTION
= -33,
1682 /** Memory has been corrupted. */
1683 SJME_ERROR_MEMORY_CORRUPTION
= -34,
1685 /** Index out of bounds. */
1686 SJME_ERROR_INDEX_OUT_OF_BOUNDS
= -35,
1688 /** Unsupported operation. */
1689 SJME_ERROR_UNSUPPORTED_OPERATION
= -36,
1691 /** Resource not found. */
1692 SJME_ERROR_RESOURCE_NOT_FOUND
= -37,
1694 /** Unexpected end of file. */
1695 SJME_ERROR_UNEXPECTED_EOF
= -38,
1697 /** Invalid identifier. */
1698 SJME_ERROR_INVALID_IDENTIFIER
= -39,
1700 /** Invalid binary name. */
1701 SJME_ERROR_INVALID_BINARY_NAME
= -40,
1703 /** Invalid field type. */
1704 SJME_ERROR_INVALID_FIELD_TYPE
= -41,
1706 /** Invalid method type. */
1707 SJME_ERROR_INVALID_METHOD_TYPE
= -42,
1709 /** Invalid class name. */
1710 SJME_ERROR_INVALID_CLASS_NAME
= -43,
1712 /** Could not load library. */
1713 SJME_ERROR_COULD_NOT_LOAD_LIBRARY
= -44,
1715 /** Invalid library symbol. */
1716 SJME_ERROR_INVALID_LIBRARY_SYMBOL
= -45,
1718 /** The number of error codes. */
1719 SJME_NUM_ERROR_CODES
= -46
1723 * Propagates an error code which allows others to run accordingly.
1725 * @param error The current error code.
1726 * @param expression The result from the expression.
1727 * @return If @c expression is an error, that will be returned otherwise
1728 * the value in @c error provided @c error is not an error.
1731 sjme_errorCode
sjme_error_also(
1732 sjme_errorCode error
, sjme_errorCode expression
);
1735 * Similar to @c sjme_error_also except this allows multiple error expressions
1736 * to be passed until the final is done via @c sjme_error_alsoVEnd() .
1738 * @param error The current error state.
1739 * @param ... All of the expressions, ends on @c sjme_error_alsoVEnd() .
1740 * @return The resultant error code.
1743 sjme_errorCode
sjme_error_alsoV(
1744 sjme_errorCode error
, ...);
1747 * The end expression for @c sjme_error_alsoV() .
1749 * @return The ending sequence for error codes.
1752 sjme_errorCode
sjme_error_alsoVEnd(void);
1755 * Is this expression considered an error?
1757 * @param error The expression.
1760 sjme_jboolean
sjme_error_is(
1761 sjme_errorCode error
);
1764 * Determines the default error code to use.
1766 * @param error The error code.
1767 * @return Either @c error or a default error.
1770 sjme_errorCode
sjme_error_default(
1771 sjme_errorCode error
);
1774 * Determines the default error code to use.
1776 * @param error The error code.
1777 * @param otherwise The other error code rather than @c SJME_ERROR_UNKNOWN.
1778 * @return Either @c error or @c otherwise if the former is not valid.
1781 sjme_errorCode
sjme_error_defaultOr(
1782 sjme_errorCode error
, sjme_errorCode otherwise
);
1784 /*--------------------------------------------------------------------------*/
1788 #ifdef SJME_CXX_SQUIRRELJME_NVM_H
1790 #undef SJME_CXX_SQUIRRELJME_NVM_H
1791 #undef SJME_CXX_IS_EXTERNED
1792 #endif /* #ifdef SJME_CXX_SQUIRRELJME_NVM_H */
1793 #endif /* #ifdef __cplusplus */
1795 #endif /* SQUIRRELJME_NVM_H */