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_jclass
;
72 typedef sjme_jobject sjme_jthrowable
;
74 typedef union sjme_anyData
82 /** Temporary index. */
83 sjme_tempIndex tempIndex
;
86 typedef struct sjme_any
88 /** Data type used. */
89 sjme_basicTypeId type
;
91 /** Data stored within. */
96 * The type of structure a type is.
100 typedef enum sjme_nvm_structType
103 SJME_NVM_STRUCTTYPE_UNKNOWN
,
106 SJME_NVM_STRUCTTYPE_ROM_LIBRARY
,
109 SJME_NVM_STRUCTTYPE_ROM_SUITE
,
111 /** NanoCoat state. */
112 SJME_NVM_STRUCTTYPE_STATE
,
114 /** The number of structure types. */
115 SJME_NVM_NUM_STRUCTTYPE
116 } sjme_nvm_structType
;
119 * Common data structure between all NanoCoat types.
123 typedef struct sjme_nvm_commonBase sjme_nvm_commonBase
;
126 * Common data structure pointer.
130 typedef sjme_nvm_commonBase
* sjme_nvm_common
;
132 /** Cast to common type. */
133 #define SJME_AS_COMMON(x) ((sjme_nvm_common)(x))
136 * Represents the virtual machine state.
140 typedef struct sjme_nvm_stateBase sjme_nvm_stateBase
;
143 * Represents the virtual machine state.
147 typedef sjme_nvm_stateBase
* sjme_nvm
;
150 * Frame of execution within a thread.
154 typedef struct sjme_nvm_frameBase sjme_nvm_frameBase
;
157 * Frame of execution within a thread.
161 typedef sjme_nvm_frameBase
* sjme_nvm_frame
;
164 * Base structure for virtual machine threads.
168 typedef struct sjme_nvm_threadBase sjme_nvm_threadBase
;
171 * Exception stack trace mechanism storage.
175 typedef struct sjme_exceptTrace sjme_exceptTrace
;
178 * A thread within SquirrelJME.
182 typedef sjme_nvm_threadBase
* sjme_nvm_thread
;
184 struct sjme_nvm_threadBase
186 /** The VM state this thread is in. */
189 /** The wrapper in the front end. */
190 sjme_frontEnd frontEnd
;
192 /** The thread ID. */
195 /** The top of the stack. */
198 /** The number of frames. */
201 /** Current exception handler go back. */
202 sjme_exceptTrace
* except
;
205 typedef struct sjme_static_constValue
207 /** Integer value. */
217 sjme_jdouble jdouble
;
224 } sjme_static_constValue
;
227 * Represents a field type.
231 typedef struct sjme_static_fieldType
233 /** The hash code for the field type. */
236 /** The field descriptor. */
237 sjme_lpcstr descriptor
;
239 /** The basic type. */
240 sjme_basicTypeId basicType
;
241 } sjme_static_fieldType
;
243 typedef struct sjme_static_classField
248 /** The field type. */
249 const sjme_static_fieldType
* type
;
254 /** The constant value type. */
255 sjme_basicTypeId valueType
;
258 sjme_static_constValue value
;
259 } sjme_static_classField
;
261 typedef struct sjme_static_classFields
263 /** The number of fields. */
267 sjme_static_classField fields
[sjme_flexibleArrayCount
];
268 } sjme_static_classFields
;
271 * Type used for method code functions.
273 * @param currentState The current virtual machine state.
274 * @param currentThread The current virtual machine thread.
275 * @return Will return @c SJME_JNI_TRUE if execution completed without throwing
276 * a @c Throwable object.
279 typedef sjme_jboolean (*sjme_methodCodeFunction
)(
280 sjme_nvm currentState
,
281 sjme_nvm_thread currentThread
);
284 * The variable mapping and setup for any given method.
288 typedef struct sjme_static_classCodeLimits
290 /** The maximum number of @c sjme_basicTypeId local/stack variables. */
291 const sjme_jubyte maxVariables
[SJME_NUM_JAVA_TYPE_IDS
];
292 } sjme_static_classCodeLimits
;
295 * Contains information about method code and how variables should be placed
296 * on execution and stack handling.
300 typedef struct sjme_static_classCode
302 /** The variable count and thrown index count used. */
303 const sjme_static_classCodeLimits
* limits
;
305 /** The index where thrown objects are placed. */
306 sjme_jshort thrownVarIndex
;
308 /** The method code. */
309 sjme_methodCodeFunction code
;
310 } sjme_static_classCode
;
313 * Represents a standard Java method type, using field descriptors.
317 typedef struct sjme_static_methodType
319 /** The hash code for the method type. */
322 /** The descriptor for the method type. */
323 sjme_lpcstr descriptor
;
325 /** The return type. */
326 const sjme_static_fieldType
* returnType
;
328 /** The number of arguments. */
331 /** The arguments to the method. */
332 const sjme_static_fieldType
* argTypes
[sjme_flexibleArrayCount
];
333 } sjme_static_methodType
;
335 typedef struct sjme_static_classMethod
344 const sjme_static_methodType
* type
;
346 /** Method code and any pertaining information. */
347 const sjme_static_classCode
* code
;
348 } sjme_static_classMethod
;
350 typedef struct sjme_static_classMethods
352 /** The number of methods. */
356 sjme_static_classMethod methods
[sjme_flexibleArrayCount
];
357 } sjme_static_classMethods
;
359 typedef struct sjme_static_classInterface
361 sjme_lpcstr interfaceName
;
362 } sjme_static_classInterface
;
364 typedef struct sjme_static_classInterfaces
366 /** The number of interfaces. */
370 sjme_static_classInterface interfaces
[sjme_flexibleArrayCount
];
371 } sjme_static_classInterfaces
;
373 typedef struct sjme_static_resource
375 /** The resource path. */
378 /** The hash for the path. */
381 /** The size of the resource. */
384 /** The resource data. */
385 const sjme_jbyte data
[sjme_flexibleArrayCount
];
386 } sjme_static_resource
;
388 typedef struct sjme_static_linkage_data_classObject
390 /** The class name. */
391 sjme_lpcstr className
;
392 } sjme_static_linkage_data_classObject
;
394 typedef struct sjme_static_linkage_data_fieldAccess
396 /** Is this static? */
397 sjme_jboolean isStatic
;
399 /** Is this a store? */
400 sjme_jboolean isStore
;
402 /** The source method name. */
403 sjme_lpcstr sourceMethodName
;
405 /** The source method type. */
406 sjme_lpcstr sourceMethodType
;
408 /** The target class. */
409 sjme_lpcstr targetClass
;
411 /** The target field name. */
412 sjme_lpcstr targetFieldName
;
414 /** The target field type. */
415 sjme_lpcstr targetFieldType
;
416 } sjme_static_linkage_data_fieldAccess
;
418 typedef struct sjme_static_linkage_data_invokeSpecial
420 /** The source method name. */
421 sjme_lpcstr sourceMethodName
;
423 /** The source method type. */
424 sjme_lpcstr sourceMethodType
;
426 /** The target class. */
427 sjme_lpcstr targetClass
;
429 /** The target method name. */
430 sjme_lpcstr targetMethodName
;
432 /** The target method type. */
433 sjme_lpcstr targetMethodType
;
434 } sjme_static_linkage_data_invokeSpecial
;
436 typedef struct sjme_static_linkage_data_invokeNormal
438 /** Is this a static invocation? */
439 sjme_jboolean isStatic
;
441 /** The source method name. */
442 sjme_lpcstr sourceMethodName
;
444 /** The source method type. */
445 sjme_lpcstr sourceMethodType
;
447 /** The target class. */
448 sjme_lpcstr targetClass
;
450 /** The target method name. */
451 sjme_lpcstr targetMethodName
;
453 /** The target method type. */
454 sjme_lpcstr targetMethodType
;
455 } sjme_static_linkage_data_invokeNormal
;
457 typedef struct sjme_static_linkage_data_stringObject
459 /** The string value. */
461 } sjme_static_linkage_data_stringObject
;
463 typedef union sjme_static_linkage_data
465 /** Reference to class object. */
466 sjme_static_linkage_data_classObject classObject
;
469 sjme_static_linkage_data_fieldAccess fieldAccess
;
471 /** Special invocation. */
472 sjme_static_linkage_data_invokeSpecial invokeSpecial
;
474 /** Normal invocation. */
475 sjme_static_linkage_data_invokeNormal invokeNormal
;
477 /** String object. */
478 sjme_static_linkage_data_stringObject stringObject
;
479 } sjme_static_linkage_data
;
486 typedef struct sjme_static_linkage
488 /** The type of linkage this is. */
489 sjme_staticLinkageType type
;
492 sjme_static_linkage_data data
;
493 } sjme_static_linkage
;
495 typedef struct sjme_static_linkages
497 /** The number of linkages. */
500 /** The define linkages. */
501 sjme_static_linkage linkages
[sjme_flexibleArrayCount
];
502 } sjme_static_linkages
;
504 typedef struct sjme_dynamic_linkage_data_classObject
507 } sjme_dynamic_linkage_data_classObject
;
509 typedef struct sjme_dynamic_linkage_data_fieldAccess
512 } sjme_dynamic_linkage_data_fieldAccess
;
514 typedef struct sjme_dynamic_linkage_data_invokeSpecial
517 } sjme_dynamic_linkage_data_invokeSpecial
;
519 typedef struct sjme_dynamic_linkage_data_invokeNormal
522 } sjme_dynamic_linkage_data_invokeNormal
;
524 typedef struct sjme_dynamic_linkage_data_stringObject
527 } sjme_dynamic_linkage_data_stringObject
;
529 typedef union sjme_dynamic_linkage_data
531 /** Reference to class object. */
532 sjme_dynamic_linkage_data_classObject classObject
;
535 sjme_dynamic_linkage_data_fieldAccess fieldAccess
;
537 /** Special invocation. */
538 sjme_dynamic_linkage_data_invokeSpecial invokeSpecial
;
540 /** Normal invocation. */
541 sjme_dynamic_linkage_data_invokeNormal invokeNormal
;
543 /** String object. */
544 sjme_dynamic_linkage_data_stringObject stringObject
;
545 } sjme_dynamic_linkage_data
;
552 typedef struct sjme_dynamic_linkage
554 /** The type of linkage this is. */
555 sjme_staticLinkageType type
;
558 sjme_dynamic_linkage_data data
;
559 } sjme_dynamic_linkage
;
562 * Represents the frame of a stack tread.
566 typedef struct sjme_nvm_frameTread
568 /** The number of items in this tread. */
571 /** The base index for the stack index. */
572 sjme_jint stackBaseIndex
;
574 /** The maximum size this tread can be. */
577 /** Values within the tread. */
580 /** Integer values. */
581 sjme_jint jints
[sjme_flexibleArrayCountUnion
];
584 sjme_jlong jlongs
[sjme_flexibleArrayCountUnion
];
587 sjme_jfloat jfloats
[sjme_flexibleArrayCountUnion
];
589 /** Double values. */
590 sjme_jdouble jdoubles
[sjme_flexibleArrayCountUnion
];
592 /** Object references. */
593 sjme_jobject jobjects
[sjme_flexibleArrayCountUnion
];
595 } sjme_nvm_frameTread
;
598 * Calculates the size of a frame tread for a given type.
600 * @param type The type to get the size for.
601 * @param count The number if items to store.
602 * @return The size in bytes for the tread.
605 #define SJME_SIZEOF_FRAME_TREAD(type, count, baseType) \
606 (sizeof(sjme_nvm_frameTread) + \
607 /* Need to handle cases where values could be aligned up... */ \
608 (offsetof(sjme_nvm_frameTread, values.SJME_TOKEN_PASTE(baseType,s)[0]) - \
609 offsetof(sjme_nvm_frameTread, values)) + \
610 (sizeof(type) * (size_t)(count)))
613 * Calculates the size of a frame tread for a given type via variable.
615 * @param typeId The type to get the size for.
616 * @param count The number if items to store.
617 * @return The size in bytes for the tread.
620 static sjme_inline sjme_attrArtificial
size_t SJME_SIZEOF_FRAME_TREAD_VAR(
621 sjme_javaTypeId typeId
, sjme_jint count
)
625 case SJME_JAVA_TYPE_ID_INTEGER
:
626 return SJME_SIZEOF_FRAME_TREAD(sjme_jint
, count
, jint
);
628 case SJME_JAVA_TYPE_ID_LONG
:
629 return SJME_SIZEOF_FRAME_TREAD(sjme_jlong
, count
, jlong
);
631 case SJME_JAVA_TYPE_ID_FLOAT
:
632 return SJME_SIZEOF_FRAME_TREAD(sjme_jfloat
, count
, jfloat
);
634 case SJME_JAVA_TYPE_ID_DOUBLE
:
635 return SJME_SIZEOF_FRAME_TREAD(sjme_jdouble
, count
, jdouble
);
637 case SJME_JAVA_TYPE_ID_OBJECT
:
638 return SJME_SIZEOF_FRAME_TREAD(sjme_jobject
, count
, jobject
);
646 * Represents information on a frame's stack storage.
650 typedef struct sjme_nvm_frameStack
652 /** The number of items in the stack. */
655 /** The current limit of this structure. */
658 /** The stack order. */
659 sjme_javaTypeId order
[sjme_flexibleArrayCount
];
660 } sjme_nvm_frameStack
;
663 * Calculates the size of a frame stack.
665 * @param count The number if items to store.
666 * @return The size in bytes for the tread.
669 #define SJME_SIZEOF_FRAME_STACK(count) \
670 (sizeof(sjme_nvm_frameStack) + \
671 (sizeof(sjme_javaTypeId) * (size_t)(count)))
673 typedef struct sjme_nvm_frameLocalMap
675 /** The maximum number of locals. */
678 /** Mapping of a specific variable to a given type index. */
681 sjme_jbyte to
[SJME_NUM_JAVA_TYPE_IDS
];
682 } maps
[sjme_flexibleArrayCount
];
683 } sjme_nvm_frameLocalMap
;
686 * Calculates the size of the frame local variable map.
688 * @param count The number of items in the mapping.
689 * @return The size in bytes of the local mapping.
692 #define SJME_SIZEOF_FRAME_LOCAL_MAP(count) \
693 (sizeof(sjme_nvm_frameLocalMap) + \
694 (SJME_SIZEOF_STRUCT_MEMBER(sjme_nvm_frameLocalMap, maps[0]) * (count)))
696 struct sjme_nvm_frameBase
698 /** The thread this frame is in. */
699 sjme_nvm_thread inThread
;
701 /** The wrapper in the front end. */
702 sjme_frontEnd frontEnd
;
704 /** The parent frame. */
705 sjme_nvm_frame parent
;
707 /** The frame index in the thread. */
708 sjme_jint frameIndex
;
710 /** The current program counter. */
713 /** Object which is waiting to be thrown for exception handling. */
714 sjme_jobject waitingThrown
;
716 /** Frame linkage. */
717 sjme_dynamic_linkage
* linkage
;
719 /** Temporary stack. */
722 /** Reference to this. */
723 sjme_jobject thisRef
;
725 /** Class reference. */
726 sjme_jclass classObjectRef
;
728 /** The current stack information. */
729 sjme_nvm_frameStack
* stack
;
731 /** Treads for the stack and locals. */
732 sjme_nvm_frameTread
* treads
[SJME_NUM_BASIC_TYPE_IDS
];
734 /** Mapping of local variables to the tread indexes per type. */
735 const sjme_nvm_frameLocalMap
* localMap
;
739 * Contains the payload information.
743 typedef struct sjme_payload_config sjme_payload_config
;
746 * Hook for garbage collection detection and/or cancel capability.
748 * @param frame The frame this is garbage collecting in.
749 * @param gcWhat what is being garbage collected?
750 * @return Returns @c SJME_JNI_TRUE if garbage collection should continue.
753 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc
)(sjme_nvm_frame frame
,
754 sjme_jobject gcWhat
);
757 * Hooks for alternative function.
761 typedef struct sjme_nvm_stateHooks
763 /** Garbage collection. */
764 sjme_nvm_stateHookGcFunc gc
;
765 } sjme_nvm_stateHooks
;
768 * Boot parameters for NanoCoat.
772 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam
;
775 * Standard Suite structure.
779 typedef struct sjme_rom_suiteBase sjme_rom_suiteBase
;
782 * Opaque suite structure type.
786 typedef struct sjme_rom_suiteBase
* sjme_rom_suite
;
789 * Structure for a single task.
793 typedef struct sjme_nvm_taskBase
* sjme_nvm_task
;
795 struct sjme_nvm_commonBase
797 /** Closeable for this NanoCoat object. */
798 sjme_closeableBase closeable
;
800 /** The type of item this is. */
801 sjme_nvm_structType type
;
803 /** The wrapper in the front end. */
804 sjme_frontEnd frontEnd
;
806 /** The lock to access this common item. */
807 sjme_thread_spinLock lock
;
810 struct sjme_nvm_stateBase
813 sjme_nvm_commonBase common
;
815 /** The memory pool to use for allocations. */
816 sjme_alloc_pool
* allocPool
;
818 /** The reserved memory pool. */
819 sjme_alloc_pool
* reservedPool
;
821 /** The copy of the input boot parameters. */
822 const sjme_nvm_bootParam
* bootParamCopy
;
824 /** Hooks for the state. */
825 const sjme_nvm_stateHooks
* hooks
;
827 /* The suite containing all the libraries. */
828 sjme_rom_suite suite
;
832 * Method initialization start.
836 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
839 * Method closing end.
843 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
845 /*--------------------------------------------------------------------------*/
849 #ifdef SJME_CXX_SQUIRRELJME_NVM_H
851 #undef SJME_CXX_SQUIRRELJME_NVM_H
852 #undef SJME_CXX_IS_EXTERNED
853 #endif /* #ifdef SJME_CXX_SQUIRRELJME_NVM_H */
854 #endif /* #ifdef __cplusplus */
856 #endif /* SQUIRRELJME_NVM_H */