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_STRUCT_UNKNOWN
,
106 SJME_NVM_STRUCT_ROM_LIBRARY
,
109 SJME_NVM_STRUCT_ROM_SUITE
,
111 /** Constant pool. */
112 SJME_NVM_STRUCT_POOL
,
114 /** NanoCoat state. */
115 SJME_NVM_STRUCT_STATE
,
117 /** A string pool. */
118 SJME_NVM_STRUCT_STRING_POOL
,
120 /** A string in the string pool. */
121 SJME_NVM_STRUCT_STRING_POOL_STRING
,
123 /** The number of structure types. */
125 } sjme_nvm_structType
;
128 * Common data structure between all NanoCoat types.
132 typedef struct sjme_nvm_commonBase sjme_nvm_commonBase
;
135 * Common data structure pointer.
139 typedef sjme_nvm_commonBase
* sjme_nvm_common
;
141 /** Cast to common type. */
142 #define SJME_AS_NVM_COMMON(x) ((sjme_nvm_common)(x))
145 * Represents the virtual machine state.
149 typedef struct sjme_nvm_stateBase sjme_nvm_stateBase
;
152 * Represents the virtual machine state.
156 typedef sjme_nvm_stateBase
* sjme_nvm
;
159 * Frame of execution within a thread.
163 typedef struct sjme_nvm_frameBase sjme_nvm_frameBase
;
166 * Frame of execution within a thread.
170 typedef sjme_nvm_frameBase
* sjme_nvm_frame
;
173 * Base structure for virtual machine threads.
177 typedef struct sjme_nvm_threadBase sjme_nvm_threadBase
;
180 * Exception stack trace mechanism storage.
184 typedef struct sjme_exceptTrace sjme_exceptTrace
;
187 * A thread within SquirrelJME.
191 typedef sjme_nvm_threadBase
* sjme_nvm_thread
;
193 struct sjme_nvm_threadBase
195 /** The VM state this thread is in. */
198 /** The wrapper in the front end. */
199 sjme_frontEnd frontEnd
;
201 /** The thread ID. */
204 /** The top of the stack. */
207 /** The number of frames. */
210 /** Current exception handler go back. */
211 sjme_exceptTrace
* except
;
214 typedef struct sjme_static_constValue
216 /** Integer value. */
226 sjme_jdouble jdouble
;
233 } sjme_static_constValue
;
236 * Represents a field type.
240 typedef struct sjme_static_fieldType
242 /** The hash code for the field type. */
245 /** The field descriptor. */
246 sjme_lpcstr descriptor
;
248 /** The basic type. */
249 sjme_basicTypeId basicType
;
250 } sjme_static_fieldType
;
252 typedef struct sjme_static_classField
257 /** The field type. */
258 const sjme_static_fieldType
* type
;
263 /** The constant value type. */
264 sjme_basicTypeId valueType
;
267 sjme_static_constValue value
;
268 } sjme_static_classField
;
270 typedef struct sjme_static_classFields
272 /** The number of fields. */
276 sjme_static_classField fields
[sjme_flexibleArrayCount
];
277 } sjme_static_classFields
;
280 * Type used for method code functions.
282 * @param currentState The current virtual machine state.
283 * @param currentThread The current virtual machine thread.
284 * @return Will return @c SJME_JNI_TRUE if execution completed without throwing
285 * a @c Throwable object.
288 typedef sjme_jboolean (*sjme_methodCodeFunction
)(
289 sjme_nvm currentState
,
290 sjme_nvm_thread currentThread
);
293 * The variable mapping and setup for any given method.
297 typedef struct sjme_static_classCodeLimits
299 /** The maximum number of @c sjme_basicTypeId local/stack variables. */
300 const sjme_jubyte maxVariables
[SJME_NUM_JAVA_TYPE_IDS
];
301 } sjme_static_classCodeLimits
;
304 * Contains information about method code and how variables should be placed
305 * on execution and stack handling.
309 typedef struct sjme_static_classCode
311 /** The variable count and thrown index count used. */
312 const sjme_static_classCodeLimits
* limits
;
314 /** The index where thrown objects are placed. */
315 sjme_jshort thrownVarIndex
;
317 /** The method code. */
318 sjme_methodCodeFunction code
;
319 } sjme_static_classCode
;
322 * Represents a standard Java method type, using field descriptors.
326 typedef struct sjme_static_methodType
328 /** The hash code for the method type. */
331 /** The descriptor for the method type. */
332 sjme_lpcstr descriptor
;
334 /** The return type. */
335 const sjme_static_fieldType
* returnType
;
337 /** The number of arguments. */
340 /** The arguments to the method. */
341 const sjme_static_fieldType
* argTypes
[sjme_flexibleArrayCount
];
342 } sjme_static_methodType
;
344 typedef struct sjme_static_classMethod
353 const sjme_static_methodType
* type
;
355 /** Method code and any pertaining information. */
356 const sjme_static_classCode
* code
;
357 } sjme_static_classMethod
;
359 typedef struct sjme_static_classMethods
361 /** The number of methods. */
365 sjme_static_classMethod methods
[sjme_flexibleArrayCount
];
366 } sjme_static_classMethods
;
368 typedef struct sjme_static_classInterface
370 sjme_lpcstr interfaceName
;
371 } sjme_static_classInterface
;
373 typedef struct sjme_static_classInterfaces
375 /** The number of interfaces. */
379 sjme_static_classInterface interfaces
[sjme_flexibleArrayCount
];
380 } sjme_static_classInterfaces
;
382 typedef struct sjme_static_resource
384 /** The resource path. */
387 /** The hash for the path. */
390 /** The size of the resource. */
393 /** The resource data. */
394 const sjme_jbyte data
[sjme_flexibleArrayCount
];
395 } sjme_static_resource
;
397 typedef struct sjme_static_linkage_data_classObject
399 /** The class name. */
400 sjme_lpcstr className
;
401 } sjme_static_linkage_data_classObject
;
403 typedef struct sjme_static_linkage_data_fieldAccess
405 /** Is this static? */
406 sjme_jboolean isStatic
;
408 /** Is this a store? */
409 sjme_jboolean isStore
;
411 /** The source method name. */
412 sjme_lpcstr sourceMethodName
;
414 /** The source method type. */
415 sjme_lpcstr sourceMethodType
;
417 /** The target class. */
418 sjme_lpcstr targetClass
;
420 /** The target field name. */
421 sjme_lpcstr targetFieldName
;
423 /** The target field type. */
424 sjme_lpcstr targetFieldType
;
425 } sjme_static_linkage_data_fieldAccess
;
427 typedef struct sjme_static_linkage_data_invokeSpecial
429 /** The source method name. */
430 sjme_lpcstr sourceMethodName
;
432 /** The source method type. */
433 sjme_lpcstr sourceMethodType
;
435 /** The target class. */
436 sjme_lpcstr targetClass
;
438 /** The target method name. */
439 sjme_lpcstr targetMethodName
;
441 /** The target method type. */
442 sjme_lpcstr targetMethodType
;
443 } sjme_static_linkage_data_invokeSpecial
;
445 typedef struct sjme_static_linkage_data_invokeNormal
447 /** Is this a static invocation? */
448 sjme_jboolean isStatic
;
450 /** The source method name. */
451 sjme_lpcstr sourceMethodName
;
453 /** The source method type. */
454 sjme_lpcstr sourceMethodType
;
456 /** The target class. */
457 sjme_lpcstr targetClass
;
459 /** The target method name. */
460 sjme_lpcstr targetMethodName
;
462 /** The target method type. */
463 sjme_lpcstr targetMethodType
;
464 } sjme_static_linkage_data_invokeNormal
;
466 typedef struct sjme_static_linkage_data_stringObject
468 /** The string value. */
470 } sjme_static_linkage_data_stringObject
;
472 typedef union sjme_static_linkage_data
474 /** Reference to class object. */
475 sjme_static_linkage_data_classObject classObject
;
478 sjme_static_linkage_data_fieldAccess fieldAccess
;
480 /** Special invocation. */
481 sjme_static_linkage_data_invokeSpecial invokeSpecial
;
483 /** Normal invocation. */
484 sjme_static_linkage_data_invokeNormal invokeNormal
;
486 /** String object. */
487 sjme_static_linkage_data_stringObject stringObject
;
488 } sjme_static_linkage_data
;
495 typedef struct sjme_static_linkage
497 /** The type of linkage this is. */
498 sjme_staticLinkageType type
;
501 sjme_static_linkage_data data
;
502 } sjme_static_linkage
;
504 typedef struct sjme_static_linkages
506 /** The number of linkages. */
509 /** The define linkages. */
510 sjme_static_linkage linkages
[sjme_flexibleArrayCount
];
511 } sjme_static_linkages
;
513 typedef struct sjme_dynamic_linkage_data_classObject
516 } sjme_dynamic_linkage_data_classObject
;
518 typedef struct sjme_dynamic_linkage_data_fieldAccess
521 } sjme_dynamic_linkage_data_fieldAccess
;
523 typedef struct sjme_dynamic_linkage_data_invokeSpecial
526 } sjme_dynamic_linkage_data_invokeSpecial
;
528 typedef struct sjme_dynamic_linkage_data_invokeNormal
531 } sjme_dynamic_linkage_data_invokeNormal
;
533 typedef struct sjme_dynamic_linkage_data_stringObject
536 } sjme_dynamic_linkage_data_stringObject
;
538 typedef union sjme_dynamic_linkage_data
540 /** Reference to class object. */
541 sjme_dynamic_linkage_data_classObject classObject
;
544 sjme_dynamic_linkage_data_fieldAccess fieldAccess
;
546 /** Special invocation. */
547 sjme_dynamic_linkage_data_invokeSpecial invokeSpecial
;
549 /** Normal invocation. */
550 sjme_dynamic_linkage_data_invokeNormal invokeNormal
;
552 /** String object. */
553 sjme_dynamic_linkage_data_stringObject stringObject
;
554 } sjme_dynamic_linkage_data
;
561 typedef struct sjme_dynamic_linkage
563 /** The type of linkage this is. */
564 sjme_staticLinkageType type
;
567 sjme_dynamic_linkage_data data
;
568 } sjme_dynamic_linkage
;
571 * Represents the frame of a stack tread.
575 typedef struct sjme_nvm_frameTread
577 /** The number of items in this tread. */
580 /** The base index for the stack index. */
581 sjme_jint stackBaseIndex
;
583 /** The maximum size this tread can be. */
586 /** Values within the tread. */
589 /** Integer values. */
590 sjme_jint jints
[sjme_flexibleArrayCountUnion
];
593 sjme_jlong jlongs
[sjme_flexibleArrayCountUnion
];
596 sjme_jfloat jfloats
[sjme_flexibleArrayCountUnion
];
598 /** Double values. */
599 sjme_jdouble jdoubles
[sjme_flexibleArrayCountUnion
];
601 /** Object references. */
602 sjme_jobject jobjects
[sjme_flexibleArrayCountUnion
];
604 } sjme_nvm_frameTread
;
607 * Calculates the size of a frame tread for a given type.
609 * @param type The type to get the size for.
610 * @param count The number if items to store.
611 * @return The size in bytes for the tread.
614 #define SJME_SIZEOF_FRAME_TREAD(type, count, baseType) \
615 (sizeof(sjme_nvm_frameTread) + \
616 /* Need to handle cases where values could be aligned up... */ \
617 (offsetof(sjme_nvm_frameTread, values.SJME_TOKEN_PASTE(baseType,s)[0]) - \
618 offsetof(sjme_nvm_frameTread, values)) + \
619 (sizeof(type) * (size_t)(count)))
622 * Calculates the size of a frame tread for a given type via variable.
624 * @param typeId The type to get the size for.
625 * @param count The number if items to store.
626 * @return The size in bytes for the tread.
629 static sjme_inline sjme_attrArtificial
size_t SJME_SIZEOF_FRAME_TREAD_VAR(
630 sjme_javaTypeId typeId
, sjme_jint count
)
634 case SJME_JAVA_TYPE_ID_INTEGER
:
635 return SJME_SIZEOF_FRAME_TREAD(sjme_jint
, count
, jint
);
637 case SJME_JAVA_TYPE_ID_LONG
:
638 return SJME_SIZEOF_FRAME_TREAD(sjme_jlong
, count
, jlong
);
640 case SJME_JAVA_TYPE_ID_FLOAT
:
641 return SJME_SIZEOF_FRAME_TREAD(sjme_jfloat
, count
, jfloat
);
643 case SJME_JAVA_TYPE_ID_DOUBLE
:
644 return SJME_SIZEOF_FRAME_TREAD(sjme_jdouble
, count
, jdouble
);
646 case SJME_JAVA_TYPE_ID_OBJECT
:
647 return SJME_SIZEOF_FRAME_TREAD(sjme_jobject
, count
, jobject
);
655 * Represents information on a frame's stack storage.
659 typedef struct sjme_nvm_frameStack
661 /** The number of items in the stack. */
664 /** The current limit of this structure. */
667 /** The stack order. */
668 sjme_javaTypeId order
[sjme_flexibleArrayCount
];
669 } sjme_nvm_frameStack
;
672 * Calculates the size of a frame stack.
674 * @param count The number if items to store.
675 * @return The size in bytes for the tread.
678 #define SJME_SIZEOF_FRAME_STACK(count) \
679 (sizeof(sjme_nvm_frameStack) + \
680 (sizeof(sjme_javaTypeId) * (size_t)(count)))
682 typedef struct sjme_nvm_frameLocalMap
684 /** The maximum number of locals. */
687 /** Mapping of a specific variable to a given type index. */
690 sjme_jbyte to
[SJME_NUM_JAVA_TYPE_IDS
];
691 } maps
[sjme_flexibleArrayCount
];
692 } sjme_nvm_frameLocalMap
;
695 * Calculates the size of the frame local variable map.
697 * @param count The number of items in the mapping.
698 * @return The size in bytes of the local mapping.
701 #define SJME_SIZEOF_FRAME_LOCAL_MAP(count) \
702 (sizeof(sjme_nvm_frameLocalMap) + \
703 (SJME_SIZEOF_STRUCT_MEMBER(sjme_nvm_frameLocalMap, maps[0]) * (count)))
705 struct sjme_nvm_frameBase
707 /** The thread this frame is in. */
708 sjme_nvm_thread inThread
;
710 /** The wrapper in the front end. */
711 sjme_frontEnd frontEnd
;
713 /** The parent frame. */
714 sjme_nvm_frame parent
;
716 /** The frame index in the thread. */
717 sjme_jint frameIndex
;
719 /** The current program counter. */
722 /** Object which is waiting to be thrown for exception handling. */
723 sjme_jobject waitingThrown
;
725 /** Frame linkage. */
726 sjme_dynamic_linkage
* linkage
;
728 /** Temporary stack. */
731 /** Reference to this. */
732 sjme_jobject thisRef
;
734 /** Class reference. */
735 sjme_jclass classObjectRef
;
737 /** The current stack information. */
738 sjme_nvm_frameStack
* stack
;
740 /** Treads for the stack and locals. */
741 sjme_nvm_frameTread
* treads
[SJME_NUM_BASIC_TYPE_IDS
];
743 /** Mapping of local variables to the tread indexes per type. */
744 const sjme_nvm_frameLocalMap
* localMap
;
748 * Contains the payload information.
752 typedef struct sjme_payload_config sjme_payload_config
;
755 * Hook for garbage collection detection and/or cancel capability.
757 * @param frame The frame this is garbage collecting in.
758 * @param gcWhat what is being garbage collected?
759 * @return Returns @c SJME_JNI_TRUE if garbage collection should continue.
762 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc
)(sjme_nvm_frame frame
,
763 sjme_jobject gcWhat
);
766 * Hooks for alternative function.
770 typedef struct sjme_nvm_stateHooks
772 /** Garbage collection. */
773 sjme_nvm_stateHookGcFunc gc
;
774 } sjme_nvm_stateHooks
;
777 * Boot parameters for NanoCoat.
781 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam
;
784 * Standard Suite structure.
788 typedef struct sjme_rom_suiteBase sjme_rom_suiteBase
;
791 * Opaque suite structure type.
795 typedef struct sjme_rom_suiteBase
* sjme_rom_suite
;
798 * Structure for a single task.
802 typedef struct sjme_nvm_taskBase
* sjme_nvm_task
;
804 struct sjme_nvm_commonBase
806 /** Closeable for this NanoCoat object. */
807 sjme_closeableBase closeable
;
809 /** The type of item this is. */
810 sjme_nvm_structType type
;
812 /** The wrapper in the front end. */
813 sjme_frontEnd frontEnd
;
815 /** The lock to access this common item. */
816 sjme_thread_spinLock lock
;
819 struct sjme_nvm_stateBase
822 sjme_nvm_commonBase common
;
824 /** The memory pool to use for allocations. */
825 sjme_alloc_pool
* allocPool
;
827 /** The reserved memory pool. */
828 sjme_alloc_pool
* reservedPool
;
830 /** The copy of the input boot parameters. */
831 const sjme_nvm_bootParam
* bootParamCopy
;
833 /** Hooks for the state. */
834 const sjme_nvm_stateHooks
* hooks
;
836 /* The suite containing all the libraries. */
837 sjme_rom_suite suite
;
841 * Method initialization start.
845 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
848 * Method closing end.
852 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
854 /*--------------------------------------------------------------------------*/
858 #ifdef SJME_CXX_SQUIRRELJME_NVM_H
860 #undef SJME_CXX_SQUIRRELJME_NVM_H
861 #undef SJME_CXX_IS_EXTERNED
862 #endif /* #ifdef SJME_CXX_SQUIRRELJME_NVM_H */
863 #endif /* #ifdef __cplusplus */
865 #endif /* SQUIRRELJME_NVM_H */