vm.h
Go to the documentation of this file.
1 #pragma once
9 #include <stdarg.h>
10 #include <sys/types.h>
11 #include "kuroko.h"
12 #include "value.h"
13 #include "table.h"
14 #include "object.h"
15 
20 #define KRK_CALL_FRAMES_MAX 1000
21 
30 #define KRK_THREAD_SCRATCH_SIZE 3
31 
44 typedef struct {
46  uint8_t * ip;
47  size_t slots;
48  size_t outSlots;
51 } KrkCallFrame;
52 
63 struct Exceptions {
83 };
84 
97 struct BaseClasses {
141 };
142 
152 typedef struct KrkThreadState {
153  struct KrkThreadState * next;
156  size_t frameCount;
157  size_t stackSize;
161  ssize_t exitOnFrame;
165  int flags;
166  unsigned int maximumCallDepth;
171 
180 typedef struct KrkVM {
182  char * binpath;
191  /* Garbage collector state */
193  size_t bytesAllocated;
194  size_t nextGC;
195  size_t grayCount;
196  size_t grayCapacity;
202 
203 /* Thread-specific flags */
204 #define KRK_THREAD_ENABLE_TRACING (1 << 0)
205 #define KRK_THREAD_ENABLE_DISASSEMBLY (1 << 1)
206 /* reserved, formerly SCAN_TRACING */
207 #define KRK_THREAD_HAS_EXCEPTION (1 << 3)
208 #define KRK_THREAD_SINGLE_STEP (1 << 4)
209 #define KRK_THREAD_SIGNALLED (1 << 5)
210 #define KRK_THREAD_DEFER_STACK_FREE (1 << 6)
211 
212 /* Global flags */
213 #define KRK_GLOBAL_ENABLE_STRESS_GC (1 << 8)
214 #define KRK_GLOBAL_GC_PAUSED (1 << 9)
215 #define KRK_GLOBAL_CLEAN_OUTPUT (1 << 10)
216 /* 11 is available again */
217 #define KRK_GLOBAL_REPORT_GC_COLLECTS (1 << 12)
218 #define KRK_GLOBAL_THREADS (1 << 13)
219 #define KRK_GLOBAL_NO_DEFAULT_MODULES (1 << 14)
220 
221 #ifndef KRK_DISABLE_THREADS
222 # define krk_threadLocal __thread
223 #else
224 # define krk_threadLocal
225 #endif
226 
232 #if !defined(KRK_DISABLE_THREADS) && ((defined(__APPLE__)) && defined(__aarch64__))
233 extern void krk_forceThreadData(void);
234 #define krk_currentThread (*_macos_currentThread())
235 #pragma clang diagnostic ignored "-Wlanguage-extension-token"
236 __attribute__((always_inline))
237 inline KrkThreadState * _macos_currentThread(void) {
238  extern const uint64_t tls_desc[] asm("_krk_currentThread");
239  const uintptr_t * threadptr; asm ("mrs %0, TPIDRRO_EL0" : "=r"(threadptr));
240  return (KrkThreadState*)(threadptr[tls_desc[1]] + tls_desc[2]);
241 }
242 #elif !defined(KRK_DISABLE_THREADS) && ((defined(_WIN32) && !defined(KRKINLIB)) || defined(KRK_MEDIOCRE_TLS))
243 #define krk_currentThread (*krk_getCurrentThread())
244 #else
245 extern krk_threadLocal KrkThreadState krk_currentThread;
246 #endif
247 
251 extern KrkVM krk_vm;
252 
257 #define vm krk_vm
258 
271 extern void krk_initVM(int flags);
272 
283 extern void krk_freeVM(void);
284 
293 extern void krk_resetStack(void);
294 
314 extern KrkValue krk_interpret(const char * src, const char * fromFile);
315 
328 extern KrkValue krk_runfile(const char * fileName, const char * fromFile);
329 
338 extern void krk_push(KrkValue value);
339 
350 extern KrkValue krk_pop(void);
351 
360 extern KrkValue krk_peek(int distance);
361 
370 extern void krk_swap(int distance);
371 
383 extern const char * krk_typeName(KrkValue value);
384 
400 extern KrkNative * krk_defineNative(KrkTable * table, const char * name, NativeFn function);
401 
415 extern KrkNative * krk_defineNativeProperty(KrkTable * table, const char * name, NativeFn func);
416 
439 extern void krk_attachNamedValue(KrkTable * table, const char name[], KrkValue obj);
440 
465 extern void krk_attachNamedObject(KrkTable * table, const char name[], KrkObj * obj);
466 
496 extern KrkValue krk_runtimeError(KrkClass * type, const char * fmt, ...);
497 
514 extern void krk_raiseException(KrkValue base, KrkValue cause);
515 
525 extern void krk_attachInnerException(KrkValue innerException);
526 
537 
551 extern KrkValue krk_runNext(void);
552 
564 extern KrkClass * krk_getType(KrkValue value);
565 
583 extern int krk_isInstanceOf(KrkValue obj, const KrkClass * type);
584 
601 extern int krk_bindMethod(KrkClass * _class, KrkString * name);
602 
617 extern int krk_bindMethodSuper(KrkClass * baseClass, KrkString * name, KrkClass * realClass);
618 
637 extern int krk_callValue(KrkValue callee, int argCount, int callableOnStack);
638 
643 extern KrkValue krk_list_of(int argc, const KrkValue argv[], int hasKw);
644 
649 extern KrkValue krk_dict_of(int argc, const KrkValue argv[], int hasKw);
650 
655 extern KrkValue krk_tuple_of(int argc, const KrkValue argv[], int hasKw);
656 
661 extern KrkValue krk_set_of(int argc, const KrkValue argv[], int hasKw);
662 
667 extern KrkValue krk_slice_of(int argc, const KrkValue argv[], int hasKw);
668 
681 extern KrkValue krk_callStack(int argCount);
682 
694 extern KrkValue krk_callDirect(KrkObj * callable, int argCount);
695 
709 extern KrkClass * krk_makeClass(KrkInstance * module, KrkClass ** _class, const char * name, KrkClass * base);
710 
721 extern void krk_finalizeClass(KrkClass * _class);
722 
734 extern void krk_dumpTraceback(void);
735 
746 extern KrkInstance * krk_startModule(const char * name);
747 
753 extern KrkValue krk_dirObject(int argc, const KrkValue argv[], int hasKw);
754 
768 extern int krk_loadModule(KrkString * path, KrkValue * moduleOut, KrkString * runAs, KrkValue parent);
769 
779 extern int krk_doRecursiveModuleLoad(KrkString * name);
780 
793 extern int krk_importModule(KrkString * name, KrkString * runAs);
794 
806 extern int krk_isFalsey(KrkValue value);
807 
824 extern KrkValue krk_valueGetAttribute(KrkValue value, char * name);
825 
829 extern KrkValue krk_valueGetAttribute_default(KrkValue value, char * name, KrkValue defaultVal);
830 
848 extern KrkValue krk_valueSetAttribute(KrkValue owner, char * name, KrkValue to);
849 
863 extern KrkValue krk_valueDelAttribute(KrkValue owner, char * name);
864 
871 extern void krk_addObjects(void);
872 
879 
886 
893 
900 
906 extern void krk_setMaximumRecursionDepth(size_t maxDepth);
907 
918 extern KrkValue krk_callNativeOnStack(size_t argCount, const KrkValue *stackArgs, int hasKw, NativeFn native);
919 
935 
949 extern int krk_getAttribute(KrkString * name);
950 
966 extern int krk_setAttribute(KrkString * name);
967 
981 extern int krk_delAttribute(KrkString * name);
982 
986 extern void krk_module_init_kuroko(void);
987 
993 extern void krk_module_init_threading(void);
994 
995 
Top-level header with configuration macros.
Struct definitions for core object types.
Table of classes for built-in object types.
Definition: vm.h:97
KrkClass * floatClass
Definition: vm.h:102
KrkClass * dictitemsClass
Definition: vm.h:117
KrkClass * dictvaluesClass
Definition: vm.h:125
KrkClass * LicenseReaderClass
Definition: vm.h:133
KrkClass * striteratorClass
Definition: vm.h:113
KrkClass * notImplClass
Definition: vm.h:123
KrkClass * longClass
Definition: vm.h:127
KrkClass * propertyClass
Definition: vm.h:120
KrkClass * moduleClass
Definition: vm.h:99
KrkClass * dictkeysClass
Definition: vm.h:118
KrkClass * setiteratorClass
Definition: vm.h:137
KrkClass * generatorClass
Definition: vm.h:122
KrkClass * functionClass
Definition: vm.h:106
KrkClass * bytesClass
Definition: vm.h:109
KrkClass * CompilerStateClass
Definition: vm.h:134
KrkClass * ellipsisClass
Definition: vm.h:140
KrkClass * intClass
Definition: vm.h:101
KrkClass * noneTypeClass
Definition: vm.h:104
KrkClass * typeClass
Definition: vm.h:100
KrkClass * filterClass
Definition: vm.h:130
KrkClass * bytesiteratorClass
Definition: vm.h:119
KrkClass * tupleClass
Definition: vm.h:108
KrkClass * HelperClass
Definition: vm.h:132
KrkClass * rangeClass
Definition: vm.h:111
KrkClass * boolClass
Definition: vm.h:103
KrkClass * methodClass
Definition: vm.h:107
KrkClass * listiteratorClass
Definition: vm.h:110
KrkClass * enumerateClass
Definition: vm.h:131
KrkClass * rangeiteratorClass
Definition: vm.h:112
KrkClass * dictClass
Definition: vm.h:116
KrkClass * tupleiteratorClass
Definition: vm.h:114
KrkClass * setClass
Definition: vm.h:136
KrkClass * LockClass
Definition: vm.h:139
KrkClass * CellClass
Definition: vm.h:135
KrkClass * objectClass
Definition: vm.h:98
KrkClass * mapClass
Definition: vm.h:128
KrkClass * codeobjectClass
Definition: vm.h:121
KrkClass * listClass
Definition: vm.h:115
KrkClass * sliceClass
Definition: vm.h:126
KrkClass * bytearrayClass
Definition: vm.h:124
KrkClass * ThreadClass
Definition: vm.h:138
KrkClass * zipClass
Definition: vm.h:129
KrkClass * strClass
Definition: vm.h:105
Table of basic exception types.
Definition: vm.h:63
KrkClass * indexError
Definition: vm.h:67
KrkClass * ThreadError
Definition: vm.h:80
KrkClass * typeError
Definition: vm.h:65
KrkClass * importError
Definition: vm.h:71
KrkClass * Exception
Definition: vm.h:81
KrkClass * keyboardInterrupt
Definition: vm.h:74
KrkClass * SystemError
Definition: vm.h:82
KrkClass * keyError
Definition: vm.h:68
KrkClass * attributeError
Definition: vm.h:69
KrkClass * valueError
Definition: vm.h:73
KrkClass * ioError
Definition: vm.h:72
KrkClass * nameError
Definition: vm.h:70
KrkClass * zeroDivisionError
Definition: vm.h:75
KrkClass * OSError
Definition: vm.h:79
KrkClass * baseException
Definition: vm.h:64
KrkClass * assertionError
Definition: vm.h:78
KrkClass * argumentError
Definition: vm.h:66
KrkClass * syntaxError
Definition: vm.h:77
KrkClass * notImplementedError
Definition: vm.h:76
Represents a managed call state in a VM thread.
Definition: vm.h:44
size_t slots
Definition: vm.h:47
KrkTable * globals
Definition: vm.h:49
size_t outSlots
Definition: vm.h:48
KrkValue globalsOwner
Definition: vm.h:50
KrkClosure * closure
Definition: vm.h:45
uint8_t * ip
Definition: vm.h:46
Type object.
Definition: object.h:215
KrkClass * krk_makeClass(KrkInstance *module, KrkClass **_class, const char *name, KrkClass *base)
Convenience function for creating new types.
Definition: vm.c:164
void krk_finalizeClass(KrkClass *_class)
Finalize a class by collecting pointers to core methods.
Definition: vm.c:189
int krk_bindMethod(KrkClass *_class, KrkString *name)
Perform method binding on the stack.
Definition: vm.c:1655
int krk_bindMethodSuper(KrkClass *baseClass, KrkString *name, KrkClass *realClass)
Bind a method with super() semantics.
Definition: vm.c:1626
Function object.
Definition: object.h:195
KrkValue krk_dict_of(int argc, const KrkValue argv[], int hasKw)
Create a dict object.
Definition: obj_dict.c:19
An object of a class.
Definition: object.h:281
KrkValue krk_list_of(int argc, const KrkValue argv[], int hasKw)
Create a list object.
Definition: obj_list.c:30
Managed binding to a C function.
Definition: object.h:309
The most basic object type.
Definition: object.h:41
KrkValue krk_slice_of(int argc, const KrkValue argv[], int hasKw)
Create a slice object.
Definition: obj_slice.c:14
Immutable sequence of Unicode codepoints.
Definition: object.h:93
Simple hash table of arbitrary keys to values.
Definition: table.h:28
void krk_attachNamedObject(KrkTable *table, const char name[], KrkObj *obj)
Attach an object to an attribute table.
Definition: vm.c:808
KrkNative * krk_defineNative(KrkTable *table, const char *name, NativeFn function)
Attach a native C function to an attribute table.
Definition: vm.c:155
void krk_attachNamedValue(KrkTable *table, const char name[], KrkValue obj)
Attach a value to an attribute table.
Definition: vm.c:794
KrkNative * krk_defineNativeProperty(KrkTable *table, const char *name, NativeFn func)
Attach a native dynamic property to an attribute table.
Definition: builtins.c:1227
Execution state of a VM thread.
Definition: vm.h:152
KrkValue currentException
Definition: vm.h:164
struct KrkThreadState * next
Definition: vm.h:153
size_t stackSize
Definition: vm.h:157
size_t frameCount
Definition: vm.h:156
KrkValue * stack
Definition: vm.h:158
unsigned int maximumCallDepth
Definition: vm.h:166
KrkValue * stackMax
Definition: vm.h:167
KrkUpvalue * openUpvalues
Definition: vm.h:160
KrkValue * stackTop
Definition: vm.h:159
ssize_t exitOnFrame
Definition: vm.h:161
KrkCallFrame * frames
Definition: vm.h:155
int flags
Definition: vm.h:165
KrkValue scratchSpace[KRK_THREAD_SCRATCH_SIZE]
Definition: vm.h:169
KrkInstance * module
Definition: vm.h:163
KrkValue krk_tuple_of(int argc, const KrkValue argv[], int hasKw)
Create a tuple object.
Definition: obj_tuple.c:40
Storage for values referenced from nested functions.
Definition: object.h:115
Global VM state.
Definition: vm.h:180
KrkTable strings
Definition: vm.h:183
struct Exceptions * exceptions
Definition: vm.h:189
KrkThreadState * threads
Definition: vm.h:199
void krk_freeVM(void)
Release resources from the VM.
Definition: vm.c:953
KrkValue * specialMethodNames
Definition: vm.h:187
size_t grayCapacity
Definition: vm.h:196
KrkObj * objects
Definition: vm.h:192
int globalFlags
Definition: vm.h:181
KrkInstance * builtins
Definition: vm.h:185
struct BaseClasses * baseClasses
Definition: vm.h:188
void krk_initVM(int flags)
Initialize the VM at program startup.
Definition: vm.c:868
KrkInstance * system
Definition: vm.h:186
KrkObj ** grayStack
Definition: vm.h:197
KrkTable modules
Definition: vm.h:184
size_t bytesAllocated
Definition: vm.h:193
size_t grayCount
Definition: vm.h:195
char * binpath
Definition: vm.h:182
struct DebuggerState * dbgState
Definition: vm.h:200
size_t nextGC
Definition: vm.h:194
Stack reference or primative value.
const char * krk_typeName(KrkValue value)
Get the name of the type of a value.
Definition: vm.c:984
int krk_callValue(KrkValue callee, int argCount, int callableOnStack)
Call a callable value in the current stack context.
Definition: vm.c:691
KrkValue krk_valueSetAttribute(KrkValue owner, char *name, KrkValue to)
Set a property of an object by name.
Definition: vm.c:1907
int krk_isInstanceOf(KrkValue obj, const KrkClass *type)
Determine if a class is an instance or subclass of a given type.
Definition: vm.c:282
KrkValue krk_valueDelAttribute(KrkValue owner, char *name)
Delete a property of an object by name.
Definition: vm.c:1829
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
Definition: vm.c:240
KrkValue krk_valueGetAttribute(KrkValue value, char *name)
Obtain a property of an object by name.
Definition: vm.c:1769
int krk_isFalsey(KrkValue value)
Determine the truth of a value.
Definition: vm.c:821
KrkValue krk_set_of(int argc, const KrkValue argv[], int hasKw)
Create a set object.
Definition: obj_set.c:343
Implementation of a generic hash table.
Definitions for primitive stack references.
krk_threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
KrkValue krk_runNext(void)
Continue VM execution until the next exit trigger.
Definition: vm.c:3201
int krk_getAttribute(KrkString *name)
Implementation of the GET_PROPERTY instruction.
Definition: vm.c:1765
int krk_importModule(KrkString *name, KrkString *runAs)
Load the dotted name name with the final element as runAs.
Definition: vm.c:1378
void krk_attachInnerException(KrkValue innerException)
Attach an inner exception to the current exception object.
Definition: exceptions.c:423
KrkValue krk_operator_le(KrkValue, KrkValue)
Compare two values, returning True if the left is less than or equal to the right.
void krk_resetStack(void)
Reset the current thread's stack state to the top level.
Definition: vm.c:85
int krk_setAttribute(KrkString *name)
Implementation of the SET_PROPERTY instruction.
Definition: vm.c:1903
void krk_module_init_threading(void)
Initialize the built-in 'threading' module.
Definition: threads.c:207
KrkValue krk_callStack(int argCount)
Call a callable on the stack with argCount arguments.
Definition: vm.c:732
int krk_doRecursiveModuleLoad(KrkString *name)
Load a module by a dotted name.
Definition: vm.c:1572
KrkVM krk_vm
Singleton instance of the shared VM state.
KrkThreadState * krk_getCurrentThread(void)
Get a pointer to the current thread state.
int krk_delAttribute(KrkString *name)
Implementation of the DEL_PROPERTY instruction.
Definition: vm.c:1825
KrkValue krk_operator_lt(KrkValue, KrkValue)
Compare two values, returning True if the left is less than the right.
KrkValue krk_callNativeOnStack(size_t argCount, const KrkValue *stackArgs, int hasKw, NativeFn native)
Call a native function using a reference to stack arguments safely.
Definition: vm.c:601
KrkValue krk_pop(void)
Pop the top of the stack.
Definition: vm.c:131
void krk_raiseException(KrkValue base, KrkValue cause)
Raise an exception value.
Definition: exceptions.c:435
int krk_loadModule(KrkString *path, KrkValue *moduleOut, KrkString *runAs, KrkValue parent)
Load a module from a file with a specified name.
Definition: vm.c:1146
KrkValue krk_valueGetAttribute_default(KrkValue value, char *name, KrkValue defaultVal)
See krk_valueGetAttribute.
Definition: vm.c:1780
void krk_setMaximumRecursionDepth(size_t maxDepth)
Set the maximum recursion call depth.
Definition: vm.c:863
void krk_swap(int distance)
Swap the top of the stack of the value distance slots down.
Definition: vm.c:145
KrkValue krk_runtimeError(KrkClass *type, const char *fmt,...)
Produce and raise an exception with a formatted message.
Definition: exceptions.c:460
KrkValue krk_interpret(const char *src, const char *fromFile)
Compile and execute a source code input.
Definition: vm.c:3219
#define KRK_THREAD_SCRATCH_SIZE
Extra space for each thread to store a set of working values safe from the GC.
Definition: vm.h:30
void krk_module_init_kuroko(void)
Initialize the built-in 'kuroko' module.
Definition: sys.c:227
KrkValue krk_dirObject(int argc, const KrkValue argv[], int hasKw)
Obtain a list of properties for an object.
Definition: builtins.c:13
KrkValue krk_runfile(const char *fileName, const char *fromFile)
Load and run a source file and return when execution completes.
Definition: vm.c:3236
struct KrkVM KrkVM
Global VM state.
void krk_dumpTraceback(void)
If there is an active exception, print a traceback to stderr.
Definition: exceptions.c:366
void krk_addObjects(void)
Concatenate two strings.
Definition: obj_str.c:941
void krk_push(KrkValue value)
Push a stack value.
Definition: vm.c:118
KrkValue krk_operator_ge(KrkValue, KrkValue)
Compare to values, returning True if the left is greater than or equal to the right.
KrkInstance * krk_startModule(const char *name)
Set up a new module object in the current thread.
Definition: vm.c:3209
KrkValue krk_callDirect(KrkObj *callable, int argCount)
Call a closure or native function with argCount arguments.
Definition: vm.c:740
KrkValue krk_instanceSetAttribute_wrapper(KrkValue owner, KrkString *name, KrkValue to)
Set an attribute of an instance object, bypassing __setattr__.
Definition: vm.c:1862
struct KrkThreadState KrkThreadState
Execution state of a VM thread.
KrkValue krk_peek(int distance)
Peek down from the top of the stack.
Definition: vm.c:139
KrkValue krk_operator_gt(KrkValue, KrkValue)
Compare to values, returning True if the left is greater than the right.