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
21 #include "sjme/closeable.h"
22 #include "sjme/config.h"
23 #include "sjme/stdTypes.h"
24 #include "sjme/tokenUtils.h"
25 #include "sjme/alloc.h"
26 #include "sjme/list.h"
27 #include "sjme/atomic.h"
31 #ifndef SJME_CXX_IS_EXTERNED
32 #define SJME_CXX_IS_EXTERNED
33 #define SJME_CXX_SQUIRRELJME_NVM_H
35 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
36 #endif /* #ifdef __cplusplus */
38 /*--------------------------------------------------------------------------*/
41 * Program counter address.
45 typedef sjme_jint sjme_pcAddr
;
48 * Static linkage type.
52 typedef sjme_jint sjme_staticLinkageType
;
54 struct sjme_jobjectBase
56 /** The reference count of this object, zero it becomes GCed. */
65 typedef sjme_jobject sjme_jthrowable
;
67 typedef union sjme_anyData
75 /** Temporary index. */
76 sjme_tempIndex tempIndex
;
79 typedef struct sjme_any
81 /** Data type used. */
82 sjme_basicTypeId type
;
84 /** Data stored within. */
89 * The type of structure a type is.
93 typedef enum sjme_nvm_structType
96 SJME_NVM_STRUCT_UNKNOWN
,
98 /** Class information. */
99 SJME_NVM_STRUCT_CLASS_INFO
,
102 SJME_NVM_STRUCT_CODE
,
104 /** Field information. */
105 SJME_NVM_STRUCT_FIELD_INFO
,
108 SJME_NVM_STRUCT_IDENTIFIER
,
110 /** Method information. */
111 SJME_NVM_STRUCT_METHOD_INFO
,
114 SJME_NVM_STRUCT_ROM_LIBRARY
,
117 SJME_NVM_STRUCT_ROM_SUITE
,
119 /** Constant pool. */
120 SJME_NVM_STRUCT_POOL
,
122 /** NanoCoat state. */
123 SJME_NVM_STRUCT_STATE
,
125 /** A string pool. */
126 SJME_NVM_STRUCT_STRING_POOL
,
128 /** A string in the string pool. */
129 SJME_NVM_STRUCT_STRING_POOL_STRING
,
131 /** The number of structure types. */
133 } sjme_nvm_structType
;
136 * Common data structure between all NanoCoat types.
140 typedef struct sjme_nvm_commonBase sjme_nvm_commonBase
;
143 * Common data structure pointer.
147 typedef sjme_nvm_commonBase
* sjme_nvm_common
;
149 /** Cast to common type. */
150 #define SJME_AS_NVM_COMMON(x) ((sjme_nvm_common)(x))
152 /** Cast to common pointer type. */
153 #define SJME_AS_NVM_COMMONP(x) ((sjme_nvm_common*)(x))
156 * Represents the virtual machine state.
160 typedef struct sjme_nvm_stateBase sjme_nvm_stateBase
;
163 * Represents the virtual machine state.
167 typedef sjme_nvm_stateBase
* sjme_nvm
;
170 * Frame of execution within a thread.
174 typedef struct sjme_nvm_frameBase sjme_nvm_frameBase
;
177 * Frame of execution within a thread.
181 typedef sjme_nvm_frameBase
* sjme_nvm_frame
;
184 * Base structure for virtual machine threads.
188 typedef struct sjme_nvm_threadBase sjme_nvm_threadBase
;
191 * Exception stack trace mechanism storage.
195 typedef struct sjme_exceptTrace sjme_exceptTrace
;
198 * A thread within SquirrelJME.
202 typedef sjme_nvm_threadBase
* sjme_nvm_thread
;
204 struct sjme_nvm_threadBase
206 /** The VM state this thread is in. */
209 /** The wrapper in the front end. */
210 sjme_frontEnd frontEnd
;
212 /** The thread ID. */
215 /** The top of the stack. */
218 /** The number of frames. */
221 /** Current exception handler go back. */
222 sjme_exceptTrace
* except
;
225 typedef struct sjme_static_constValue
227 /** Integer value. */
237 sjme_jdouble jdouble
;
244 } sjme_static_constValue
;
247 * Represents a field type.
251 typedef struct sjme_static_fieldType
253 /** The hash code for the field type. */
256 /** The field descriptor. */
257 sjme_lpcstr descriptor
;
259 /** The basic type. */
260 sjme_basicTypeId basicType
;
261 } sjme_static_fieldType
;
263 typedef struct sjme_static_classField
268 /** The field type. */
269 const sjme_static_fieldType
* type
;
274 /** The constant value type. */
275 sjme_basicTypeId valueType
;
278 sjme_static_constValue value
;
279 } sjme_static_classField
;
281 typedef struct sjme_static_classFields
283 /** The number of fields. */
287 sjme_static_classField fields
[sjme_flexibleArrayCount
];
288 } sjme_static_classFields
;
291 * Type used for method code functions.
293 * @param currentState The current virtual machine state.
294 * @param currentThread The current virtual machine thread.
295 * @return Will return @c SJME_JNI_TRUE if execution completed without throwing
296 * a @c Throwable object.
299 typedef sjme_jboolean (*sjme_methodCodeFunction
)(
300 sjme_nvm currentState
,
301 sjme_nvm_thread currentThread
);
304 * The variable mapping and setup for any given method.
308 typedef struct sjme_static_classCodeLimits
310 /** The maximum number of @c sjme_basicTypeId local/stack variables. */
311 const sjme_jubyte maxVariables
[SJME_NUM_JAVA_TYPE_IDS
];
312 } sjme_static_classCodeLimits
;
315 * Contains information about method code and how variables should be placed
316 * on execution and stack handling.
320 typedef struct sjme_static_classCode
322 /** The variable count and thrown index count used. */
323 const sjme_static_classCodeLimits
* limits
;
325 /** The index where thrown objects are placed. */
326 sjme_jshort thrownVarIndex
;
328 /** The method code. */
329 sjme_methodCodeFunction code
;
330 } sjme_static_classCode
;
333 * Represents a standard Java method type, using field descriptors.
337 typedef struct sjme_static_methodType
339 /** The hash code for the method type. */
342 /** The descriptor for the method type. */
343 sjme_lpcstr descriptor
;
345 /** The return type. */
346 const sjme_static_fieldType
* returnType
;
348 /** The number of arguments. */
351 /** The arguments to the method. */
352 const sjme_static_fieldType
* argTypes
[sjme_flexibleArrayCount
];
353 } sjme_static_methodType
;
355 typedef struct sjme_static_classMethod
364 const sjme_static_methodType
* type
;
366 /** Method code and any pertaining information. */
367 const sjme_static_classCode
* code
;
368 } sjme_static_classMethod
;
370 typedef struct sjme_static_classMethods
372 /** The number of methods. */
376 sjme_static_classMethod methods
[sjme_flexibleArrayCount
];
377 } sjme_static_classMethods
;
379 typedef struct sjme_static_classInterface
381 sjme_lpcstr interfaceName
;
382 } sjme_static_classInterface
;
384 typedef struct sjme_static_classInterfaces
386 /** The number of interfaces. */
390 sjme_static_classInterface interfaces
[sjme_flexibleArrayCount
];
391 } sjme_static_classInterfaces
;
393 typedef struct sjme_static_resource
395 /** The resource path. */
398 /** The hash for the path. */
401 /** The size of the resource. */
404 /** The resource data. */
405 const sjme_jbyte data
[sjme_flexibleArrayCount
];
406 } sjme_static_resource
;
408 typedef struct sjme_static_linkage_data_classObject
410 /** The class name. */
411 sjme_lpcstr className
;
412 } sjme_static_linkage_data_classObject
;
414 typedef struct sjme_static_linkage_data_fieldAccess
416 /** Is this static? */
417 sjme_jboolean isStatic
;
419 /** Is this a store? */
420 sjme_jboolean isStore
;
422 /** The source method name. */
423 sjme_lpcstr sourceMethodName
;
425 /** The source method type. */
426 sjme_lpcstr sourceMethodType
;
428 /** The target class. */
429 sjme_lpcstr targetClass
;
431 /** The target field name. */
432 sjme_lpcstr targetFieldName
;
434 /** The target field type. */
435 sjme_lpcstr targetFieldType
;
436 } sjme_static_linkage_data_fieldAccess
;
438 typedef struct sjme_static_linkage_data_invokeSpecial
440 /** The source method name. */
441 sjme_lpcstr sourceMethodName
;
443 /** The source method type. */
444 sjme_lpcstr sourceMethodType
;
446 /** The target class. */
447 sjme_lpcstr targetClass
;
449 /** The target method name. */
450 sjme_lpcstr targetMethodName
;
452 /** The target method type. */
453 sjme_lpcstr targetMethodType
;
454 } sjme_static_linkage_data_invokeSpecial
;
456 typedef struct sjme_static_linkage_data_invokeNormal
458 /** Is this a static invocation? */
459 sjme_jboolean isStatic
;
461 /** The source method name. */
462 sjme_lpcstr sourceMethodName
;
464 /** The source method type. */
465 sjme_lpcstr sourceMethodType
;
467 /** The target class. */
468 sjme_lpcstr targetClass
;
470 /** The target method name. */
471 sjme_lpcstr targetMethodName
;
473 /** The target method type. */
474 sjme_lpcstr targetMethodType
;
475 } sjme_static_linkage_data_invokeNormal
;
477 typedef struct sjme_static_linkage_data_stringObject
479 /** The string value. */
481 } sjme_static_linkage_data_stringObject
;
483 typedef union sjme_static_linkage_data
485 /** Reference to class object. */
486 sjme_static_linkage_data_classObject classObject
;
489 sjme_static_linkage_data_fieldAccess fieldAccess
;
491 /** Special invocation. */
492 sjme_static_linkage_data_invokeSpecial invokeSpecial
;
494 /** Normal invocation. */
495 sjme_static_linkage_data_invokeNormal invokeNormal
;
497 /** String object. */
498 sjme_static_linkage_data_stringObject stringObject
;
499 } sjme_static_linkage_data
;
506 typedef struct sjme_static_linkage
508 /** The type of linkage this is. */
509 sjme_staticLinkageType type
;
512 sjme_static_linkage_data data
;
513 } sjme_static_linkage
;
515 typedef struct sjme_static_linkages
517 /** The number of linkages. */
520 /** The define linkages. */
521 sjme_static_linkage linkages
[sjme_flexibleArrayCount
];
522 } sjme_static_linkages
;
524 typedef struct sjme_dynamic_linkage_data_classObject
527 } sjme_dynamic_linkage_data_classObject
;
529 typedef struct sjme_dynamic_linkage_data_fieldAccess
532 } sjme_dynamic_linkage_data_fieldAccess
;
534 typedef struct sjme_dynamic_linkage_data_invokeSpecial
537 } sjme_dynamic_linkage_data_invokeSpecial
;
539 typedef struct sjme_dynamic_linkage_data_invokeNormal
542 } sjme_dynamic_linkage_data_invokeNormal
;
544 typedef struct sjme_dynamic_linkage_data_stringObject
547 } sjme_dynamic_linkage_data_stringObject
;
549 typedef union sjme_dynamic_linkage_data
551 /** Reference to class object. */
552 sjme_dynamic_linkage_data_classObject classObject
;
555 sjme_dynamic_linkage_data_fieldAccess fieldAccess
;
557 /** Special invocation. */
558 sjme_dynamic_linkage_data_invokeSpecial invokeSpecial
;
560 /** Normal invocation. */
561 sjme_dynamic_linkage_data_invokeNormal invokeNormal
;
563 /** String object. */
564 sjme_dynamic_linkage_data_stringObject stringObject
;
565 } sjme_dynamic_linkage_data
;
572 typedef struct sjme_dynamic_linkage
574 /** The type of linkage this is. */
575 sjme_staticLinkageType type
;
578 sjme_dynamic_linkage_data data
;
579 } sjme_dynamic_linkage
;
582 * Represents the frame of a stack tread.
586 typedef struct sjme_nvm_frameTread
588 /** The number of items in this tread. */
591 /** The base index for the stack index. */
592 sjme_jint stackBaseIndex
;
594 /** The maximum size this tread can be. */
597 /** Values within the tread. */
600 /** Integer values. */
601 sjme_jint jints
[sjme_flexibleArrayCountUnion
];
604 sjme_jlong jlongs
[sjme_flexibleArrayCountUnion
];
607 sjme_jfloat jfloats
[sjme_flexibleArrayCountUnion
];
609 /** Double values. */
610 sjme_jdouble jdoubles
[sjme_flexibleArrayCountUnion
];
612 /** Object references. */
613 sjme_jobject jobjects
[sjme_flexibleArrayCountUnion
];
615 } sjme_nvm_frameTread
;
618 * Calculates the size of a frame tread for a given type.
620 * @param type The type to get the size for.
621 * @param count The number if items to store.
622 * @return The size in bytes for the tread.
625 #define SJME_SIZEOF_FRAME_TREAD(type, count, baseType) \
626 (sizeof(sjme_nvm_frameTread) + \
627 /* Need to handle cases where values could be aligned up... */ \
628 (offsetof(sjme_nvm_frameTread, values.SJME_TOKEN_PASTE(baseType,s)[0]) - \
629 offsetof(sjme_nvm_frameTread, values)) + \
630 (sizeof(type) * (size_t)(count)))
633 * Calculates the size of a frame tread for a given type via variable.
635 * @param typeId The type to get the size for.
636 * @param count The number if items to store.
637 * @return The size in bytes for the tread.
640 static sjme_inline sjme_attrArtificial
size_t SJME_SIZEOF_FRAME_TREAD_VAR(
641 sjme_javaTypeId typeId
, sjme_jint count
)
645 case SJME_JAVA_TYPE_ID_INTEGER
:
646 return SJME_SIZEOF_FRAME_TREAD(sjme_jint
, count
, jint
);
648 case SJME_JAVA_TYPE_ID_LONG
:
649 return SJME_SIZEOF_FRAME_TREAD(sjme_jlong
, count
, jlong
);
651 case SJME_JAVA_TYPE_ID_FLOAT
:
652 return SJME_SIZEOF_FRAME_TREAD(sjme_jfloat
, count
, jfloat
);
654 case SJME_JAVA_TYPE_ID_DOUBLE
:
655 return SJME_SIZEOF_FRAME_TREAD(sjme_jdouble
, count
, jdouble
);
657 case SJME_JAVA_TYPE_ID_OBJECT
:
658 return SJME_SIZEOF_FRAME_TREAD(sjme_jobject
, count
, jobject
);
666 * Represents information on a frame's stack storage.
670 typedef struct sjme_nvm_frameStack
672 /** The number of items in the stack. */
675 /** The current limit of this structure. */
678 /** The stack order. */
679 sjme_javaTypeId order
[sjme_flexibleArrayCount
];
680 } sjme_nvm_frameStack
;
683 * Calculates the size of a frame stack.
685 * @param count The number if items to store.
686 * @return The size in bytes for the tread.
689 #define SJME_SIZEOF_FRAME_STACK(count) \
690 (sizeof(sjme_nvm_frameStack) + \
691 (sizeof(sjme_javaTypeId) * (size_t)(count)))
693 typedef struct sjme_nvm_frameLocalMap
695 /** The maximum number of locals. */
698 /** Mapping of a specific variable to a given type index. */
701 sjme_jbyte to
[SJME_NUM_JAVA_TYPE_IDS
];
702 } maps
[sjme_flexibleArrayCount
];
703 } sjme_nvm_frameLocalMap
;
706 * Calculates the size of the frame local variable map.
708 * @param count The number of items in the mapping.
709 * @return The size in bytes of the local mapping.
712 #define SJME_SIZEOF_FRAME_LOCAL_MAP(count) \
713 (sizeof(sjme_nvm_frameLocalMap) + \
714 (SJME_SIZEOF_STRUCT_MEMBER(sjme_nvm_frameLocalMap, maps[0]) * (count)))
716 struct sjme_nvm_frameBase
718 /** The thread this frame is in. */
719 sjme_nvm_thread inThread
;
721 /** The wrapper in the front end. */
722 sjme_frontEnd frontEnd
;
724 /** The parent frame. */
725 sjme_nvm_frame parent
;
727 /** The frame index in the thread. */
728 sjme_jint frameIndex
;
730 /** The current program counter. */
733 /** Object which is waiting to be thrown for exception handling. */
734 sjme_jobject waitingThrown
;
736 /** Frame linkage. */
737 sjme_dynamic_linkage
* linkage
;
739 /** Temporary stack. */
742 /** Reference to this. */
743 sjme_jobject thisRef
;
745 /** Class reference. */
746 sjme_jclass classObjectRef
;
748 /** The current stack information. */
749 sjme_nvm_frameStack
* stack
;
751 /** Treads for the stack and locals. */
752 sjme_nvm_frameTread
* treads
[SJME_NUM_BASIC_TYPE_IDS
];
754 /** Mapping of local variables to the tread indexes per type. */
755 const sjme_nvm_frameLocalMap
* localMap
;
759 * Contains the payload information.
763 typedef struct sjme_payload_config sjme_payload_config
;
766 * Hook for garbage collection detection and/or cancel capability.
768 * @param frame The frame this is garbage collecting in.
769 * @param gcWhat what is being garbage collected?
770 * @return Returns @c SJME_JNI_TRUE if garbage collection should continue.
773 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc
)(sjme_nvm_frame frame
,
774 sjme_jobject gcWhat
);
777 * Hooks for alternative function.
781 typedef struct sjme_nvm_stateHooks
783 /** Garbage collection. */
784 sjme_nvm_stateHookGcFunc gc
;
785 } sjme_nvm_stateHooks
;
788 * Boot parameters for NanoCoat.
792 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam
;
795 * Standard Suite structure.
799 typedef struct sjme_rom_suiteBase sjme_rom_suiteBase
;
802 * Opaque suite structure type.
806 typedef struct sjme_rom_suiteBase
* sjme_rom_suite
;
809 * Structure for a single task.
813 typedef struct sjme_nvm_taskBase
* sjme_nvm_task
;
815 struct sjme_nvm_commonBase
817 /** Closeable for this NanoCoat object. */
818 sjme_closeableBase closeable
;
820 /** The type of item this is. */
821 sjme_nvm_structType type
;
823 /** The wrapper in the front end. */
824 sjme_frontEnd frontEnd
;
826 /** The lock to access this common item. */
827 sjme_thread_spinLock lock
;
830 struct sjme_nvm_stateBase
833 sjme_nvm_commonBase common
;
835 /** The memory pool to use for allocations. */
836 sjme_alloc_pool allocPool
;
838 /** The reserved memory pool. */
839 sjme_alloc_pool reservedPool
;
841 /** The copy of the input boot parameters. */
842 const sjme_nvm_bootParam
* bootParamCopy
;
844 /** Hooks for the state. */
845 const sjme_nvm_stateHooks
* hooks
;
847 /* The suite containing all the libraries. */
848 sjme_rom_suite suite
;
852 * Method initialization start.
856 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
859 * Method closing end.
863 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
865 /*--------------------------------------------------------------------------*/
869 #ifdef SJME_CXX_SQUIRRELJME_NVM_H
871 #undef SJME_CXX_SQUIRRELJME_NVM_H
872 #undef SJME_CXX_IS_EXTERNED
873 #endif /* #ifdef SJME_CXX_SQUIRRELJME_NVM_H */
874 #endif /* #ifdef __cplusplus */
876 #endif /* SQUIRRELJME_NVM_H */