Remove Twitter links.
[SquirrelJME.git] / nanocoat / include / sjme / nvm / nvm.h
blob75572ee95e66aa413792a4b875b21516288b971a
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 * Throwable type.
63 * @since 2023/07/25
65 typedef sjme_jobject sjme_jthrowable;
67 typedef union sjme_anyData
69 /** Integer. */
70 sjme_jint jint;
72 /** Object. */
73 sjme_jobject jobject;
75 /** Temporary index. */
76 sjme_tempIndex tempIndex;
77 } sjme_anyData;
79 typedef struct sjme_any
81 /** Data type used. */
82 sjme_basicTypeId type;
84 /** Data stored within. */
85 sjme_anyData data;
86 } sjme_any;
88 /**
89 * The type of structure a type is.
91 * @since 2024/08/09
93 typedef enum sjme_nvm_structType
95 /** Unknown. */
96 SJME_NVM_STRUCT_UNKNOWN,
98 /** Class information. */
99 SJME_NVM_STRUCT_CLASS_INFO,
101 /** Method code. */
102 SJME_NVM_STRUCT_CODE,
104 /** Field information. */
105 SJME_NVM_STRUCT_FIELD_INFO,
107 /** Identifier. */
108 SJME_NVM_STRUCT_IDENTIFIER,
110 /** Method information. */
111 SJME_NVM_STRUCT_METHOD_INFO,
113 /** Rom Library. */
114 SJME_NVM_STRUCT_ROM_LIBRARY,
116 /** Rom Suite. */
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. */
132 SJME_NVM_NUM_STRUCT
133 } sjme_nvm_structType;
136 * Common data structure between all NanoCoat types.
138 * @since 2024/08/09
140 typedef struct sjme_nvm_commonBase sjme_nvm_commonBase;
143 * Common data structure pointer.
145 * @since 2024/08/10
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.
158 * @since 2023/08/08
160 typedef struct sjme_nvm_stateBase sjme_nvm_stateBase;
163 * Represents the virtual machine state.
165 * @since 2023/07/28
167 typedef sjme_nvm_stateBase* sjme_nvm;
170 * Frame of execution within a thread.
172 * @since 2023/08/08
174 typedef struct sjme_nvm_frameBase sjme_nvm_frameBase;
177 * Frame of execution within a thread.
179 * @since 2023/07/25
181 typedef sjme_nvm_frameBase* sjme_nvm_frame;
184 * Base structure for virtual machine threads.
186 * @since 2024/08/08
188 typedef struct sjme_nvm_threadBase sjme_nvm_threadBase;
191 * Exception stack trace mechanism storage.
193 * @since 2023/12/08
195 typedef struct sjme_exceptTrace sjme_exceptTrace;
198 * A thread within SquirrelJME.
200 * @since 2024/08/08
202 typedef sjme_nvm_threadBase* sjme_nvm_thread;
204 struct sjme_nvm_threadBase
206 /** The VM state this thread is in. */
207 sjme_nvm inState;
209 /** The wrapper in the front end. */
210 sjme_frontEnd frontEnd;
212 /** The thread ID. */
213 sjme_jint threadId;
215 /** The top of the stack. */
216 sjme_nvm_frame top;
218 /** The number of frames. */
219 sjme_jint numFrames;
221 /** Current exception handler go back. */
222 sjme_exceptTrace* except;
225 typedef struct sjme_static_constValue
227 /** Integer value. */
228 sjme_jint jint;
230 /** Long value. */
231 sjme_jlong jlong;
233 /** Float value. */
234 sjme_jfloat jfloat;
236 /** Double value. */
237 sjme_jdouble jdouble;
239 /** String value. */
240 sjme_lpcstr jstring;
242 /** Class name. */
243 sjme_lpcstr jclass;
244 } sjme_static_constValue;
247 * Represents a field type.
249 * @since 2023/08/10
251 typedef struct sjme_static_fieldType
253 /** The hash code for the field type. */
254 sjme_jint hashCode;
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
265 /** Field name. */
266 sjme_lpstr name;
268 /** The field type. */
269 const sjme_static_fieldType* type;
271 /** Flags. */
272 sjme_jint flags;
274 /** The constant value type. */
275 sjme_basicTypeId valueType;
277 /** The value. */
278 sjme_static_constValue value;
279 } sjme_static_classField;
281 typedef struct sjme_static_classFields
283 /** The number of fields. */
284 sjme_jint count;
286 /** 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.
297 * @since 2023/07/25
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.
306 * @since 2023/08/09
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.
318 * @since 2023/08/09
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.
335 * @since 2023/08/10
337 typedef struct sjme_static_methodType
339 /** The hash code for the method type. */
340 sjme_jint hashCode;
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. */
349 sjme_jint argCount;
351 /** The arguments to the method. */
352 const sjme_static_fieldType* argTypes[sjme_flexibleArrayCount];
353 } sjme_static_methodType;
355 typedef struct sjme_static_classMethod
357 /** Method name. */
358 sjme_lpcstr name;
360 /** Flags. */
361 sjme_jint flags;
363 /** Name typed. */
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. */
373 sjme_jint count;
375 /** 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. */
387 sjme_jint count;
389 /** Interfaces. */
390 sjme_static_classInterface interfaces[sjme_flexibleArrayCount];
391 } sjme_static_classInterfaces;
393 typedef struct sjme_static_resource
395 /** The resource path. */
396 sjme_lpcstr path;
398 /** The hash for the path. */
399 sjme_jint pathHash;
401 /** The size of the resource. */
402 sjme_jint size;
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. */
480 sjme_lpcstr string;
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;
488 /** Field access. */
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;
502 * Static linkage.
504 * @since 2023/07/25
506 typedef struct sjme_static_linkage
508 /** The type of linkage this is. */
509 sjme_staticLinkageType type;
511 /** Linkage data. */
512 sjme_static_linkage_data data;
513 } sjme_static_linkage;
515 typedef struct sjme_static_linkages
517 /** The number of linkages. */
518 sjme_jint count;
520 /** The define linkages. */
521 sjme_static_linkage linkages[sjme_flexibleArrayCount];
522 } sjme_static_linkages;
524 typedef struct sjme_dynamic_linkage_data_classObject
526 int todo;
527 } sjme_dynamic_linkage_data_classObject;
529 typedef struct sjme_dynamic_linkage_data_fieldAccess
531 int todo;
532 } sjme_dynamic_linkage_data_fieldAccess;
534 typedef struct sjme_dynamic_linkage_data_invokeSpecial
536 int todo;
537 } sjme_dynamic_linkage_data_invokeSpecial;
539 typedef struct sjme_dynamic_linkage_data_invokeNormal
541 int todo;
542 } sjme_dynamic_linkage_data_invokeNormal;
544 typedef struct sjme_dynamic_linkage_data_stringObject
546 int todo;
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;
554 /** Field access. */
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;
568 * Dynamic linkage.
570 * @since 2023/07/25
572 typedef struct sjme_dynamic_linkage
574 /** The type of linkage this is. */
575 sjme_staticLinkageType type;
577 /** Linkage data. */
578 sjme_dynamic_linkage_data data;
579 } sjme_dynamic_linkage;
582 * Represents the frame of a stack tread.
584 * @since 2023/11/15
586 typedef struct sjme_nvm_frameTread
588 /** The number of items in this tread. */
589 sjme_jint count;
591 /** The base index for the stack index. */
592 sjme_jint stackBaseIndex;
594 /** The maximum size this tread can be. */
595 sjme_jint max;
597 /** Values within the tread. */
598 union
600 /** Integer values. */
601 sjme_jint jints[sjme_flexibleArrayCountUnion];
603 /** Long values. */
604 sjme_jlong jlongs[sjme_flexibleArrayCountUnion];
606 /** Float values. */
607 sjme_jfloat jfloats[sjme_flexibleArrayCountUnion];
609 /** Double values. */
610 sjme_jdouble jdoubles[sjme_flexibleArrayCountUnion];
612 /** Object references. */
613 sjme_jobject jobjects[sjme_flexibleArrayCountUnion];
614 } values;
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.
623 * @since 2023/11/15
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.
638 * @since 2023/11/15
640 static sjme_inline sjme_attrArtificial size_t SJME_SIZEOF_FRAME_TREAD_VAR(
641 sjme_javaTypeId typeId, sjme_jint count)
643 switch (typeId)
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);
661 /* Invalid. */
662 return 0;
666 * Represents information on a frame's stack storage.
668 * @since 2023/11/16
670 typedef struct sjme_nvm_frameStack
672 /** The number of items in the stack. */
673 sjme_jint count;
675 /** The current limit of this structure. */
676 sjme_jint limit;
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.
687 * @since 2023/11/16
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. */
696 sjme_jint max;
698 /** Mapping of a specific variable to a given type index. */
699 union
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.
710 * @since 2023/11/26
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. */
731 sjme_pcAddr pc;
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. */
740 sjme_any* tempStack;
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.
761 * @since 2023/07/27
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.
771 * @since 2023/11/17
773 typedef sjme_jboolean (*sjme_nvm_stateHookGcFunc)(sjme_nvm_frame frame,
774 sjme_jobject gcWhat);
777 * Hooks for alternative function.
779 * @since 2023/11/17
781 typedef struct sjme_nvm_stateHooks
783 /** Garbage collection. */
784 sjme_nvm_stateHookGcFunc gc;
785 } sjme_nvm_stateHooks;
788 * Boot parameters for NanoCoat.
790 * @since 2023/07/27
792 typedef struct sjme_nvm_bootParam sjme_nvm_bootParam;
795 * Standard Suite structure.
797 * @since 2023/12/12
799 typedef struct sjme_rom_suiteBase sjme_rom_suiteBase;
802 * Opaque suite structure type.
804 * @since 2023/12/22
806 typedef struct sjme_rom_suiteBase* sjme_rom_suite;
809 * Structure for a single task.
811 * @since 2023/12/17
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
832 /** Common data. */
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.
854 * @since 2023/07/25
856 #define SJME_NANOCOAT_START_CALL ((sjme_pcAddr)-1)
859 * Method closing end.
861 * @since 2023/07/25
863 #define SJME_NANOCOAT_END_CALL ((sjme_pcAddr)-2)
865 /*--------------------------------------------------------------------------*/
867 /* Anti-C++. */
868 #ifdef __cplusplus
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 */