Initial base for string pools, which are reference counted; Have closeable variant...
[SquirrelJME.git] / nanocoat / include / sjme / nvm / nvm.h
blob79e5ab54ab6822e90fede24e35d7044ad5fe2cd5
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 /**
11 * SquirrelJME NanoCoat Virtual Machine Header Definitions.
13 * @since 2023/07/25
16 #ifndef SQUIRRELJME_NVM_H
17 #define SQUIRRELJME_NVM_H
19 #include <setjmp.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"
29 /* Anti-C++. */
30 #ifdef __cplusplus
31 #ifndef SJME_CXX_IS_EXTERNED
32 #define SJME_CXX_IS_EXTERNED
33 #define SJME_CXX_SQUIRRELJME_NVM_H
34 extern "C" {
35 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
36 #endif /* #ifdef __cplusplus */
38 /*--------------------------------------------------------------------------*/
40 /**
41 * Program counter address.
43 * @since 2023/07/25
45 typedef sjme_jint sjme_pcAddr;
47 /**
48 * Static linkage type.
50 * @since 2023/07/25
52 typedef sjme_jint sjme_staticLinkageType;
54 struct sjme_jobjectBase
56 /** The reference count of this object, zero it becomes GCed. */
57 sjme_jint refCount;
60 /**
61 * Class type.
63 * @since 2023/07/25
65 typedef sjme_jobject sjme_jclass;
67 /**
68 * Throwable type.
70 * @since 2023/07/25
72 typedef sjme_jobject sjme_jthrowable;
74 typedef union sjme_anyData
76 /** Integer. */
77 sjme_jint jint;
79 /** Object. */
80 sjme_jobject jobject;
82 /** Temporary index. */
83 sjme_tempIndex tempIndex;
84 } sjme_anyData;
86 typedef struct sjme_any
88 /** Data type used. */
89 sjme_basicTypeId type;
91 /** Data stored within. */
92 sjme_anyData data;
93 } sjme_any;
95 /**
96 * The type of structure a type is.
98 * @since 2024/08/09
100 typedef enum sjme_nvm_structType
102 /** Unknown. */
103 SJME_NVM_STRUCT_UNKNOWN,
105 /** Rom Library. */
106 SJME_NVM_STRUCT_ROM_LIBRARY,
108 /** Rom Suite. */
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. */
124 SJME_NVM_NUM_STRUCT
125 } sjme_nvm_structType;
128 * Common data structure between all NanoCoat types.
130 * @since 2024/08/09
132 typedef struct sjme_nvm_commonBase sjme_nvm_commonBase;
135 * Common data structure pointer.
137 * @since 2024/08/10
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.
147 * @since 2023/08/08
149 typedef struct sjme_nvm_stateBase sjme_nvm_stateBase;
152 * Represents the virtual machine state.
154 * @since 2023/07/28
156 typedef sjme_nvm_stateBase* sjme_nvm;
159 * Frame of execution within a thread.
161 * @since 2023/08/08
163 typedef struct sjme_nvm_frameBase sjme_nvm_frameBase;
166 * Frame of execution within a thread.
168 * @since 2023/07/25
170 typedef sjme_nvm_frameBase* sjme_nvm_frame;
173 * Base structure for virtual machine threads.
175 * @since 2024/08/08
177 typedef struct sjme_nvm_threadBase sjme_nvm_threadBase;
180 * Exception stack trace mechanism storage.
182 * @since 2023/12/08
184 typedef struct sjme_exceptTrace sjme_exceptTrace;
187 * A thread within SquirrelJME.
189 * @since 2024/08/08
191 typedef sjme_nvm_threadBase* sjme_nvm_thread;
193 struct sjme_nvm_threadBase
195 /** The VM state this thread is in. */
196 sjme_nvm inState;
198 /** The wrapper in the front end. */
199 sjme_frontEnd frontEnd;
201 /** The thread ID. */
202 sjme_jint threadId;
204 /** The top of the stack. */
205 sjme_nvm_frame top;
207 /** The number of frames. */
208 sjme_jint numFrames;
210 /** Current exception handler go back. */
211 sjme_exceptTrace* except;
214 typedef struct sjme_static_constValue
216 /** Integer value. */
217 sjme_jint jint;
219 /** Long value. */
220 sjme_jlong jlong;
222 /** Float value. */
223 sjme_jfloat jfloat;
225 /** Double value. */
226 sjme_jdouble jdouble;
228 /** String value. */
229 sjme_lpcstr jstring;
231 /** Class name. */
232 sjme_lpcstr jclass;
233 } sjme_static_constValue;
236 * Represents a field type.
238 * @since 2023/08/10
240 typedef struct sjme_static_fieldType
242 /** The hash code for the field type. */
243 sjme_jint hashCode;
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
254 /** Field name. */
255 sjme_lpstr name;
257 /** The field type. */
258 const sjme_static_fieldType* type;
260 /** Flags. */
261 sjme_jint flags;
263 /** The constant value type. */
264 sjme_basicTypeId valueType;
266 /** The value. */
267 sjme_static_constValue value;
268 } sjme_static_classField;
270 typedef struct sjme_static_classFields
272 /** The number of fields. */
273 sjme_jint count;
275 /** 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.
286 * @since 2023/07/25
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.
295 * @since 2023/08/09
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.
307 * @since 2023/08/09
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.
324 * @since 2023/08/10
326 typedef struct sjme_static_methodType
328 /** The hash code for the method type. */
329 sjme_jint hashCode;
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. */
338 sjme_jint argCount;
340 /** The arguments to the method. */
341 const sjme_static_fieldType* argTypes[sjme_flexibleArrayCount];
342 } sjme_static_methodType;
344 typedef struct sjme_static_classMethod
346 /** Method name. */
347 sjme_lpcstr name;
349 /** Flags. */
350 sjme_jint flags;
352 /** Name typed. */
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. */
362 sjme_jint count;
364 /** 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. */
376 sjme_jint count;
378 /** Interfaces. */
379 sjme_static_classInterface interfaces[sjme_flexibleArrayCount];
380 } sjme_static_classInterfaces;
382 typedef struct sjme_static_resource
384 /** The resource path. */
385 sjme_lpcstr path;
387 /** The hash for the path. */
388 sjme_jint pathHash;
390 /** The size of the resource. */
391 sjme_jint size;
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. */
469 sjme_lpcstr string;
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;
477 /** Field access. */
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;
491 * Static linkage.
493 * @since 2023/07/25
495 typedef struct sjme_static_linkage
497 /** The type of linkage this is. */
498 sjme_staticLinkageType type;
500 /** Linkage data. */
501 sjme_static_linkage_data data;
502 } sjme_static_linkage;
504 typedef struct sjme_static_linkages
506 /** The number of linkages. */
507 sjme_jint count;
509 /** The define linkages. */
510 sjme_static_linkage linkages[sjme_flexibleArrayCount];
511 } sjme_static_linkages;
513 typedef struct sjme_dynamic_linkage_data_classObject
515 int todo;
516 } sjme_dynamic_linkage_data_classObject;
518 typedef struct sjme_dynamic_linkage_data_fieldAccess
520 int todo;
521 } sjme_dynamic_linkage_data_fieldAccess;
523 typedef struct sjme_dynamic_linkage_data_invokeSpecial
525 int todo;
526 } sjme_dynamic_linkage_data_invokeSpecial;
528 typedef struct sjme_dynamic_linkage_data_invokeNormal
530 int todo;
531 } sjme_dynamic_linkage_data_invokeNormal;
533 typedef struct sjme_dynamic_linkage_data_stringObject
535 int todo;
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;
543 /** Field access. */
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;
557 * Dynamic linkage.
559 * @since 2023/07/25
561 typedef struct sjme_dynamic_linkage
563 /** The type of linkage this is. */
564 sjme_staticLinkageType type;
566 /** Linkage data. */
567 sjme_dynamic_linkage_data data;
568 } sjme_dynamic_linkage;
571 * Represents the frame of a stack tread.
573 * @since 2023/11/15
575 typedef struct sjme_nvm_frameTread
577 /** The number of items in this tread. */
578 sjme_jint count;
580 /** The base index for the stack index. */
581 sjme_jint stackBaseIndex;
583 /** The maximum size this tread can be. */
584 sjme_jint max;
586 /** Values within the tread. */
587 union
589 /** Integer values. */
590 sjme_jint jints[sjme_flexibleArrayCountUnion];
592 /** Long values. */
593 sjme_jlong jlongs[sjme_flexibleArrayCountUnion];
595 /** Float values. */
596 sjme_jfloat jfloats[sjme_flexibleArrayCountUnion];
598 /** Double values. */
599 sjme_jdouble jdoubles[sjme_flexibleArrayCountUnion];
601 /** Object references. */
602 sjme_jobject jobjects[sjme_flexibleArrayCountUnion];
603 } values;
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.
612 * @since 2023/11/15
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.
627 * @since 2023/11/15
629 static sjme_inline sjme_attrArtificial size_t SJME_SIZEOF_FRAME_TREAD_VAR(
630 sjme_javaTypeId typeId, sjme_jint count)
632 switch (typeId)
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);
650 /* Invalid. */
651 return 0;
655 * Represents information on a frame's stack storage.
657 * @since 2023/11/16
659 typedef struct sjme_nvm_frameStack
661 /** The number of items in the stack. */
662 sjme_jint count;
664 /** The current limit of this structure. */
665 sjme_jint limit;
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.
676 * @since 2023/11/16
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. */
685 sjme_jint max;
687 /** Mapping of a specific variable to a given type index. */
688 union
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.
699 * @since 2023/11/26
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. */
720 sjme_pcAddr pc;
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. */
729 sjme_any* tempStack;
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.
750 * @since 2023/07/27
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.
760 * @since 2023/11/17
762 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc)(sjme_nvm_frame frame,
763 sjme_jobject gcWhat);
766 * Hooks for alternative function.
768 * @since 2023/11/17
770 typedef struct sjme_nvm_stateHooks
772 /** Garbage collection. */
773 sjme_nvm_stateHookGcFunc gc;
774 } sjme_nvm_stateHooks;
777 * Boot parameters for NanoCoat.
779 * @since 2023/07/27
781 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam;
784 * Standard Suite structure.
786 * @since 2023/12/12
788 typedef struct sjme_rom_suiteBase sjme_rom_suiteBase;
791 * Opaque suite structure type.
793 * @since 2023/12/22
795 typedef struct sjme_rom_suiteBase* sjme_rom_suite;
798 * Structure for a single task.
800 * @since 2023/12/17
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
821 /** Common data. */
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.
843 * @since 2023/07/25
845 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
848 * Method closing end.
850 * @since 2023/07/25
852 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
854 /*--------------------------------------------------------------------------*/
856 /* Anti-C++. */
857 #ifdef __cplusplus
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 */