Cleanup and moving of NanoCoat VM specific headers to their own include directory...
[SquirrelJME.git] / nanocoat / include / sjme / nvm / nvm.h
blob0cd4fe842c6f6c90de0fda9e07e2ae49bb2f6f29
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_STRUCTTYPE_UNKNOWN,
105 /** Rom Library. */
106 SJME_NVM_STRUCTTYPE_ROM_LIBRARY,
108 /** Rom Suite. */
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.
121 * @since 2024/08/09
123 typedef struct sjme_nvm_commonBase sjme_nvm_commonBase;
126 * Common data structure pointer.
128 * @since 2024/08/10
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.
138 * @since 2023/08/08
140 typedef struct sjme_nvm_stateBase sjme_nvm_stateBase;
143 * Represents the virtual machine state.
145 * @since 2023/07/28
147 typedef sjme_nvm_stateBase* sjme_nvm;
150 * Frame of execution within a thread.
152 * @since 2023/08/08
154 typedef struct sjme_nvm_frameBase sjme_nvm_frameBase;
157 * Frame of execution within a thread.
159 * @since 2023/07/25
161 typedef sjme_nvm_frameBase* sjme_nvm_frame;
164 * Base structure for virtual machine threads.
166 * @since 2024/08/08
168 typedef struct sjme_nvm_threadBase sjme_nvm_threadBase;
171 * Exception stack trace mechanism storage.
173 * @since 2023/12/08
175 typedef struct sjme_exceptTrace sjme_exceptTrace;
178 * A thread within SquirrelJME.
180 * @since 2024/08/08
182 typedef sjme_nvm_threadBase* sjme_nvm_thread;
184 struct sjme_nvm_threadBase
186 /** The VM state this thread is in. */
187 sjme_nvm inState;
189 /** The wrapper in the front end. */
190 sjme_frontEnd frontEnd;
192 /** The thread ID. */
193 sjme_jint threadId;
195 /** The top of the stack. */
196 sjme_nvm_frame top;
198 /** The number of frames. */
199 sjme_jint numFrames;
201 /** Current exception handler go back. */
202 sjme_exceptTrace* except;
205 typedef struct sjme_static_constValue
207 /** Integer value. */
208 sjme_jint jint;
210 /** Long value. */
211 sjme_jlong jlong;
213 /** Float value. */
214 sjme_jfloat jfloat;
216 /** Double value. */
217 sjme_jdouble jdouble;
219 /** String value. */
220 sjme_lpcstr jstring;
222 /** Class name. */
223 sjme_lpcstr jclass;
224 } sjme_static_constValue;
227 * Represents a field type.
229 * @since 2023/08/10
231 typedef struct sjme_static_fieldType
233 /** The hash code for the field type. */
234 sjme_jint hashCode;
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
245 /** Field name. */
246 sjme_lpstr name;
248 /** The field type. */
249 const sjme_static_fieldType* type;
251 /** Flags. */
252 sjme_jint flags;
254 /** The constant value type. */
255 sjme_basicTypeId valueType;
257 /** The value. */
258 sjme_static_constValue value;
259 } sjme_static_classField;
261 typedef struct sjme_static_classFields
263 /** The number of fields. */
264 sjme_jint count;
266 /** 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.
277 * @since 2023/07/25
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.
286 * @since 2023/08/09
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.
298 * @since 2023/08/09
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.
315 * @since 2023/08/10
317 typedef struct sjme_static_methodType
319 /** The hash code for the method type. */
320 sjme_jint hashCode;
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. */
329 sjme_jint argCount;
331 /** The arguments to the method. */
332 const sjme_static_fieldType* argTypes[sjme_flexibleArrayCount];
333 } sjme_static_methodType;
335 typedef struct sjme_static_classMethod
337 /** Method name. */
338 sjme_lpcstr name;
340 /** Flags. */
341 sjme_jint flags;
343 /** Name typed. */
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. */
353 sjme_jint count;
355 /** 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. */
367 sjme_jint count;
369 /** Interfaces. */
370 sjme_static_classInterface interfaces[sjme_flexibleArrayCount];
371 } sjme_static_classInterfaces;
373 typedef struct sjme_static_resource
375 /** The resource path. */
376 sjme_lpcstr path;
378 /** The hash for the path. */
379 sjme_jint pathHash;
381 /** The size of the resource. */
382 sjme_jint size;
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. */
460 sjme_lpcstr string;
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;
468 /** Field access. */
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;
482 * Static linkage.
484 * @since 2023/07/25
486 typedef struct sjme_static_linkage
488 /** The type of linkage this is. */
489 sjme_staticLinkageType type;
491 /** Linkage data. */
492 sjme_static_linkage_data data;
493 } sjme_static_linkage;
495 typedef struct sjme_static_linkages
497 /** The number of linkages. */
498 sjme_jint count;
500 /** The define linkages. */
501 sjme_static_linkage linkages[sjme_flexibleArrayCount];
502 } sjme_static_linkages;
504 typedef struct sjme_dynamic_linkage_data_classObject
506 int todo;
507 } sjme_dynamic_linkage_data_classObject;
509 typedef struct sjme_dynamic_linkage_data_fieldAccess
511 int todo;
512 } sjme_dynamic_linkage_data_fieldAccess;
514 typedef struct sjme_dynamic_linkage_data_invokeSpecial
516 int todo;
517 } sjme_dynamic_linkage_data_invokeSpecial;
519 typedef struct sjme_dynamic_linkage_data_invokeNormal
521 int todo;
522 } sjme_dynamic_linkage_data_invokeNormal;
524 typedef struct sjme_dynamic_linkage_data_stringObject
526 int todo;
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;
534 /** Field access. */
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;
548 * Dynamic linkage.
550 * @since 2023/07/25
552 typedef struct sjme_dynamic_linkage
554 /** The type of linkage this is. */
555 sjme_staticLinkageType type;
557 /** Linkage data. */
558 sjme_dynamic_linkage_data data;
559 } sjme_dynamic_linkage;
562 * Represents the frame of a stack tread.
564 * @since 2023/11/15
566 typedef struct sjme_nvm_frameTread
568 /** The number of items in this tread. */
569 sjme_jint count;
571 /** The base index for the stack index. */
572 sjme_jint stackBaseIndex;
574 /** The maximum size this tread can be. */
575 sjme_jint max;
577 /** Values within the tread. */
578 union
580 /** Integer values. */
581 sjme_jint jints[sjme_flexibleArrayCountUnion];
583 /** Long values. */
584 sjme_jlong jlongs[sjme_flexibleArrayCountUnion];
586 /** Float values. */
587 sjme_jfloat jfloats[sjme_flexibleArrayCountUnion];
589 /** Double values. */
590 sjme_jdouble jdoubles[sjme_flexibleArrayCountUnion];
592 /** Object references. */
593 sjme_jobject jobjects[sjme_flexibleArrayCountUnion];
594 } values;
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.
603 * @since 2023/11/15
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.
618 * @since 2023/11/15
620 static sjme_inline sjme_attrArtificial size_t SJME_SIZEOF_FRAME_TREAD_VAR(
621 sjme_javaTypeId typeId, sjme_jint count)
623 switch (typeId)
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);
641 /* Invalid. */
642 return 0;
646 * Represents information on a frame's stack storage.
648 * @since 2023/11/16
650 typedef struct sjme_nvm_frameStack
652 /** The number of items in the stack. */
653 sjme_jint count;
655 /** The current limit of this structure. */
656 sjme_jint limit;
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.
667 * @since 2023/11/16
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. */
676 sjme_jint max;
678 /** Mapping of a specific variable to a given type index. */
679 union
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.
690 * @since 2023/11/26
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. */
711 sjme_pcAddr pc;
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. */
720 sjme_any* tempStack;
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.
741 * @since 2023/07/27
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.
751 * @since 2023/11/17
753 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc)(sjme_nvm_frame frame,
754 sjme_jobject gcWhat);
757 * Hooks for alternative function.
759 * @since 2023/11/17
761 typedef struct sjme_nvm_stateHooks
763 /** Garbage collection. */
764 sjme_nvm_stateHookGcFunc gc;
765 } sjme_nvm_stateHooks;
768 * Boot parameters for NanoCoat.
770 * @since 2023/07/27
772 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam;
775 * Standard Suite structure.
777 * @since 2023/12/12
779 typedef struct sjme_rom_suiteBase sjme_rom_suiteBase;
782 * Opaque suite structure type.
784 * @since 2023/12/22
786 typedef struct sjme_rom_suiteBase* sjme_rom_suite;
789 * Structure for a single task.
791 * @since 2023/12/17
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
812 /** Common data. */
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.
834 * @since 2023/07/25
836 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
839 * Method closing end.
841 * @since 2023/07/25
843 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
845 /*--------------------------------------------------------------------------*/
847 /* Anti-C++. */
848 #ifdef __cplusplus
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 */