Go to the documentation of this file.
1 #pragma once
9 #include <stdarg.h>
10 #include <time.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include "kuroko.h"
14 #include "value.h"
15 #include "table.h"
16 #include "object.h"
22 #define KRK_CALL_FRAMES_MAX 64
46 typedef struct {
48  uint8_t * ip;
49  size_t slots;
50  size_t outSlots;
54  struct timespec in_time;
55 #endif
56 } KrkCallFrame;
68 struct Exceptions {
88 };
102 struct BaseClasses {
150 };
161 typedef struct KrkThreadState {
162  struct KrkThreadState * next;
165  size_t frameCount;
166  size_t stackSize;
170  ssize_t exitOnFrame;
174  int flags;
188 typedef struct KrkVM {
190  char * binpath;
199  /* Garbage collector state */
201  size_t bytesAllocated;
202  size_t nextGC;
203  size_t grayCount;
204  size_t grayCapacity;
208  FILE * callgrindFile;
213 /* Thread-specific flags */
214 #define KRK_THREAD_ENABLE_TRACING (1 << 0)
216 /* reserved, formerly SCAN_TRACING */
217 #define KRK_THREAD_HAS_EXCEPTION (1 << 3)
218 #define KRK_THREAD_SINGLE_STEP (1 << 4)
219 #define KRK_THREAD_SIGNALLED (1 << 5)
220 #define KRK_THREAD_DEFER_STACK_FREE (1 << 6)
222 /* Global flags */
223 #define KRK_GLOBAL_ENABLE_STRESS_GC (1 << 8)
224 #define KRK_GLOBAL_GC_PAUSED (1 << 9)
225 #define KRK_GLOBAL_CLEAN_OUTPUT (1 << 10)
226 #define KRK_GLOBAL_CALLGRIND (1 << 11)
227 #define KRK_GLOBAL_REPORT_GC_COLLECTS (1 << 12)
228 #define KRK_GLOBAL_THREADS (1 << 13)
229 #define KRK_GLOBAL_NO_DEFAULT_MODULES (1 << 14)
232 # define threadLocal __thread
233 #else
234 # define threadLocal
235 #endif
242 #if !defined(KRK_DISABLE_THREADS) && ((defined(__APPLE__)) && defined(__aarch64__))
243 extern void krk_forceThreadData(void);
244 #define krk_currentThread (*_macos_currentThread())
245 #pragma clang diagnostic ignored "-Wlanguage-extension-token"
246 __attribute__((always_inline))
247 inline KrkThreadState * _macos_currentThread(void) {
248  extern const uint64_t tls_desc[] asm("_krk_currentThread");
249  const uintptr_t * threadptr; asm ("mrs %0, TPIDRRO_EL0" : "=r"(threadptr));
250  return (KrkThreadState*)(threadptr[tls_desc[1]] + tls_desc[2]);
251 }
252 #elif !defined(KRK_DISABLE_THREADS) && ((defined(_WIN32) && !defined(KRKINLIB)) || defined(KRK_MEDIOCRE_TLS))
253 #define krk_currentThread (*krk_getCurrentThread())
254 #else
255 extern threadLocal KrkThreadState krk_currentThread;
256 #endif
261 extern KrkVM krk_vm;
267 #define vm krk_vm
281 extern void krk_initVM(int flags);
293 extern void krk_freeVM(void);
303 extern void krk_resetStack(void);
324 extern KrkValue krk_interpret(const char * src, char * fromFile);
338 extern KrkValue krk_runfile(const char * fileName, char * fromFile);
348 extern void krk_push(KrkValue value);
360 extern KrkValue krk_pop(void);
370 extern KrkValue krk_peek(int distance);
380 extern void krk_swap(int distance);
393 extern const char * krk_typeName(KrkValue value);
410 extern KrkNative * krk_defineNative(KrkTable * table, const char * name, NativeFn function);
425 extern KrkNative * krk_defineNativeProperty(KrkTable * table, const char * name, NativeFn func);
449 extern void krk_attachNamedValue(KrkTable * table, const char name[], KrkValue obj);
475 extern void krk_attachNamedObject(KrkTable * table, const char name[], KrkObj * obj);
506 extern KrkValue krk_runtimeError(KrkClass * type, const char * fmt, ...);
524 extern void krk_raiseException(KrkValue base, KrkValue cause);
535 extern void krk_attachInnerException(KrkValue innerException);
546 extern KrkThreadState * krk_getCurrentThread(void);
561 extern KrkValue krk_runNext(void);
574 extern KrkClass * krk_getType(KrkValue value);
593 extern int krk_isInstanceOf(KrkValue obj, const KrkClass * type);
611 extern int krk_bindMethod(KrkClass * _class, KrkString * name);
627 extern int krk_bindMethodSuper(KrkClass * baseClass, KrkString * name, KrkClass * realClass);
647 extern int krk_callValue(KrkValue callee, int argCount, int callableOnStack);
653 extern KrkValue krk_list_of(int argc, const KrkValue argv[], int hasKw);
659 extern KrkValue krk_dict_of(int argc, const KrkValue argv[], int hasKw);
665 extern KrkValue krk_tuple_of(int argc, const KrkValue argv[], int hasKw);
671 extern KrkValue krk_set_of(int argc, const KrkValue argv[], int hasKw);
677 extern KrkValue krk_slice_of(int argc, const KrkValue argv[], int hasKw);
691 extern KrkValue krk_callStack(int argCount);
704 extern KrkValue krk_callDirect(KrkObj * callable, int argCount);
719 extern KrkClass * krk_makeClass(KrkInstance * module, KrkClass ** _class, const char * name, KrkClass * base);
731 extern void krk_finalizeClass(KrkClass * _class);
744 extern void krk_dumpTraceback(void);
756 extern KrkInstance * krk_startModule(const char * name);
763 extern KrkValue krk_dirObject(int argc, const KrkValue argv[], int hasKw);
778 extern int krk_loadModule(KrkString * path, KrkValue * moduleOut, KrkString * runAs, KrkValue parent);
789 extern int krk_doRecursiveModuleLoad(KrkString * name);
803 extern int krk_importModule(KrkString * name, KrkString * runAs);
816 extern int krk_isFalsey(KrkValue value);
834 extern KrkValue krk_valueGetAttribute(KrkValue value, char * name);
839 extern KrkValue krk_valueGetAttribute_default(KrkValue value, char * name, KrkValue defaultVal);
858 extern KrkValue krk_valueSetAttribute(KrkValue owner, char * name, KrkValue to);
873 extern KrkValue krk_valueDelAttribute(KrkValue owner, char * name);
881 extern void krk_addObjects(void);
916 extern void krk_setMaximumRecursionDepth(size_t maxDepth);
928 extern KrkValue krk_callNativeOnStack(size_t argCount, const KrkValue *stackArgs, int hasKw, NativeFn native);
959 extern int krk_getAttribute(KrkString * name);
976 extern int krk_setAttribute(KrkString * name);
991 extern int krk_delAttribute(KrkString * name);
996 extern void krk_module_init_kuroko(void);
1001 extern void krk_module_init_gc(void);
1006 extern void krk_module_init_time(void);
1011 extern void krk_module_init_os(void);
1016 extern void krk_module_init_fileio(void);
1023 extern void krk_module_init_dis(void);
1030 extern void krk_module_init_threading(void);
Top-level header with configuration macros.
Struct definitions for core object types.
Table of classes for built-in object types.
Definition: vm.h:102
KrkClass * floatClass
Definition: vm.h:107
KrkClass * dictitemsClass
Definition: vm.h:122
KrkClass * dictvaluesClass
Definition: vm.h:130
KrkClass * LicenseReaderClass
Definition: vm.h:138
KrkClass * stat_resultClass
Definition: vm.h:142
KrkClass * striteratorClass
Definition: vm.h:118
KrkClass * notImplClass
Definition: vm.h:128
KrkClass * longClass
Definition: vm.h:132
KrkClass * FileClass
Definition: vm.h:139
KrkClass * propertyClass
Definition: vm.h:125
KrkClass * moduleClass
Definition: vm.h:104
KrkClass * dictkeysClass
Definition: vm.h:123
KrkClass * setiteratorClass
Definition: vm.h:145
KrkClass * generatorClass
Definition: vm.h:127
KrkClass * functionClass
Definition: vm.h:111
KrkClass * bytesClass
Definition: vm.h:114
KrkClass * CompilerStateClass
Definition: vm.h:148
KrkClass * intClass
Definition: vm.h:106
KrkClass * noneTypeClass
Definition: vm.h:109
KrkClass * typeClass
Definition: vm.h:105
KrkClass * filterClass
Definition: vm.h:135
KrkClass * bytesiteratorClass
Definition: vm.h:124
KrkClass * tupleClass
Definition: vm.h:113
KrkClass * HelperClass
Definition: vm.h:137
KrkClass * rangeClass
Definition: vm.h:116
KrkClass * boolClass
Definition: vm.h:108
KrkClass * methodClass
Definition: vm.h:112
KrkClass * EnvironClass
Definition: vm.h:143
KrkClass * listiteratorClass
Definition: vm.h:115
KrkClass * enumerateClass
Definition: vm.h:136
KrkClass * rangeiteratorClass
Definition: vm.h:117
KrkClass * dictClass
Definition: vm.h:121
KrkClass * tupleiteratorClass
Definition: vm.h:119
KrkClass * setClass
Definition: vm.h:144
KrkClass * LockClass
Definition: vm.h:147
KrkClass * CellClass
Definition: vm.h:149
KrkClass * objectClass
Definition: vm.h:103
KrkClass * mapClass
Definition: vm.h:133
KrkClass * DirectoryClass
Definition: vm.h:141
KrkClass * codeobjectClass
Definition: vm.h:126
KrkClass * listClass
Definition: vm.h:120
KrkClass * sliceClass
Definition: vm.h:131
KrkClass * bytearrayClass
Definition: vm.h:129
KrkClass * ThreadClass
Definition: vm.h:146
KrkClass * zipClass
Definition: vm.h:134
KrkClass * strClass
Definition: vm.h:110
KrkClass * BinaryFileClass
Definition: vm.h:140
Table of basic exception types.
Definition: vm.h:68
KrkClass * indexError
Definition: vm.h:72
KrkClass * ThreadError
Definition: vm.h:85
KrkClass * typeError
Definition: vm.h:70
KrkClass * importError
Definition: vm.h:76
KrkClass * Exception
Definition: vm.h:86
KrkClass * keyboardInterrupt
Definition: vm.h:79
KrkClass * SystemError
Definition: vm.h:87
KrkClass * keyError
Definition: vm.h:73
KrkClass * attributeError
Definition: vm.h:74
KrkClass * valueError
Definition: vm.h:78
KrkClass * ioError
Definition: vm.h:77
KrkClass * nameError
Definition: vm.h:75
KrkClass * zeroDivisionError
Definition: vm.h:80
KrkClass * OSError
Definition: vm.h:84
KrkClass * baseException
Definition: vm.h:69
KrkClass * assertionError
Definition: vm.h:83
KrkClass * argumentError
Definition: vm.h:71
KrkClass * syntaxError
Definition: vm.h:82
KrkClass * notImplementedError
Definition: vm.h:81
Represents a managed call state in a VM thread.
Definition: vm.h:46
size_t slots
Definition: vm.h:49
KrkTable * globals
Definition: vm.h:51
size_t outSlots
Definition: vm.h:50
KrkValue globalsOwner
Definition: vm.h:52
KrkClosure * closure
Definition: vm.h:47
uint8_t * ip
Definition: vm.h:48
Type object.
Definition: object.h:189
KrkClass * krk_makeClass(KrkInstance *module, KrkClass **_class, const char *name, KrkClass *base)
Convenience function for creating new types.
Definition: vm.c:203
void krk_finalizeClass(KrkClass *_class)
Finalize a class by collecting pointers to core methods.
int krk_bindMethod(KrkClass *_class, KrkString *name)
Perform method binding on the stack.
Definition: vm.c:1693
int krk_bindMethodSuper(KrkClass *baseClass, KrkString *name, KrkClass *realClass)
Bind a method with super() semantics.
Definition: vm.c:1664
Function object.
Definition: object.h:169
KrkValue krk_dict_of(int argc, const KrkValue argv[], int hasKw)
Create a dict object.
Definition: obj_dict.c:11
An object of a class.
Definition: object.h:255
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:283
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:839
KrkNative * krk_defineNative(KrkTable *table, const char *name, NativeFn function)
Attach a native C function to an attribute table.
Definition: vm.c:194
void krk_attachNamedValue(KrkTable *table, const char name[], KrkValue obj)
Attach a value to an attribute table.
Definition: vm.c:825
KrkNative * krk_defineNativeProperty(KrkTable *table, const char *name, NativeFn func)
Attach a native dynamic property to an attribute table.
Definition: builtins.c:1183
Execution state of a VM thread.
Definition: vm.h:161
KrkValue currentException
Definition: vm.h:173
struct KrkThreadState * next
Definition: vm.h:162
size_t stackSize
Definition: vm.h:166
size_t frameCount
Definition: vm.h:165
KrkValue * stack
Definition: vm.h:167
KrkValue * stackMax
Definition: vm.h:175
KrkUpvalue * openUpvalues
Definition: vm.h:169
KrkValue * stackTop
Definition: vm.h:168
ssize_t exitOnFrame
Definition: vm.h:170
KrkCallFrame * frames
Definition: vm.h:164
int flags
Definition: vm.h:174
KrkValue scratchSpace[KRK_THREAD_SCRATCH_SIZE]
Definition: vm.h:177
KrkInstance * module
Definition: vm.h:172
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:188
size_t maximumCallDepth
Definition: vm.h:209
KrkTable strings
Definition: vm.h:191
struct Exceptions * exceptions
Definition: vm.h:197
KrkThreadState * threads
Definition: vm.h:207
void krk_freeVM(void)
Release resources from the VM.
Definition: vm.c:992
KrkValue * specialMethodNames
Definition: vm.h:195
size_t grayCapacity
Definition: vm.h:204
KrkObj * objects
Definition: vm.h:200
int globalFlags
Definition: vm.h:189
KrkInstance * builtins
Definition: vm.h:193
FILE * callgrindFile
Definition: vm.h:208
struct BaseClasses * baseClasses
Definition: vm.h:196
void krk_initVM(int flags)
Initialize the VM at program startup.
Definition: vm.c:904
KrkInstance * system
Definition: vm.h:194
KrkObj ** grayStack
Definition: vm.h:205
KrkTable modules
Definition: vm.h:192
size_t bytesAllocated
Definition: vm.h:201
size_t grayCount
Definition: vm.h:203
char * binpath
Definition: vm.h:190
struct DebuggerState * dbgState
Definition: vm.h:210
size_t nextGC
Definition: vm.h:202
Stack reference or primative value.
const char * krk_typeName(KrkValue value)
Get the name of the type of a value.
Definition: vm.c:1023
int krk_callValue(KrkValue callee, int argCount, int callableOnStack)
Call a callable value in the current stack context.
Definition: vm.c:722
KrkValue krk_valueSetAttribute(KrkValue owner, char *name, KrkValue to)
Set a property of an object by name.
Definition: vm.c:1940
int krk_isInstanceOf(KrkValue obj, const KrkClass *type)
Determine if a class is an instance or subclass of a given type.
Definition: vm.c:317
KrkValue krk_valueDelAttribute(KrkValue owner, char *name)
Delete a property of an object by name.
Definition: vm.c:1865
KrkClass * krk_getType(KrkValue value)
Get the class representing a value.
Definition: vm.c:275
KrkValue krk_valueGetAttribute(KrkValue value, char *name)
Obtain a property of an object by name.
Definition: vm.c:1805
int krk_isFalsey(KrkValue value)
Determine the truth of a value.
Definition: vm.c:852
KrkValue krk_set_of(int argc, const KrkValue argv[], int hasKw)
Create a set object.
Definition: obj_set.c:346
Implementation of a generic hash table.
Definitions for primitive stack references.
KrkValue krk_runNext(void)
Continue VM execution until the next exit trigger.
Definition: vm.c:3211
int krk_getAttribute(KrkString *name)
Implementation of the GET_PROPERTY instruction.
Definition: vm.c:1801
int krk_importModule(KrkString *name, KrkString *runAs)
Load the dotted name name with the final element as runAs.
Definition: vm.c:1416
void krk_attachInnerException(KrkValue innerException)
Attach an inner exception to the current exception object.
Definition: exceptions.c:408
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:124
int krk_setAttribute(KrkString *name)
Implementation of the SET_PROPERTY instruction.
Definition: vm.c:1936
void krk_module_init_fileio(void)
Initialize the built-in 'fileio' module.
Definition: fileio.c:492
void krk_module_init_threading(void)
Initialize the built-in 'threading' module.
Definition: threads.c:202
KrkValue krk_callStack(int argCount)
Call a callable on the stack with argCount arguments.
Definition: vm.c:763
void krk_module_init_os(void)
Initialize the built-in 'os' module.
Definition: os.c:655
int krk_doRecursiveModuleLoad(KrkString *name)
Load a module by a dotted name.
Definition: vm.c:1610
KrkVM krk_vm
Singleton instance of the shared VM state.
KrkThreadState * krk_getCurrentThread(void)
Get a pointer to the current thread state.
Definition: vm.c:115
int krk_delAttribute(KrkString *name)
Implementation of the DEL_PROPERTY instruction.
Definition: vm.c:1861
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:637
KrkValue krk_pop(void)
Pop the top of the stack.
Definition: vm.c:170
void krk_raiseException(KrkValue base, KrkValue cause)
Raise an exception value.
Definition: exceptions.c:420
threadLocal KrkThreadState krk_currentThread
Thread-local VM state.
int krk_loadModule(KrkString *path, KrkValue *moduleOut, KrkString *runAs, KrkValue parent)
Load a module from a file with a specified name.
Definition: vm.c:1185
KrkValue krk_valueGetAttribute_default(KrkValue value, char *name, KrkValue defaultVal)
See krk_valueGetAttribute.
Definition: vm.c:1816
void krk_module_init_time(void)
Initialize the built-in 'time' module.
Definition: time.c:41
void krk_setMaximumRecursionDepth(size_t maxDepth)
Set the maximum recursion call depth.
Definition: vm.c:894
void krk_swap(int distance)
Swap the top of the stack of the value distance slots down.
Definition: vm.c:184
KrkValue krk_runtimeError(KrkClass *type, const char *fmt,...)
Produce and raise an exception with a formatted message.
Definition: exceptions.c:445
Extra space for each thread to store a set of working values safe from the GC.
Definition: vm.h:32
KrkValue krk_runfile(const char *fileName, char *fromFile)
Load and run a source file and return when execution completes.
Definition: vm.c:3246
void krk_module_init_kuroko(void)
Initialize the built-in 'kuroko' module.
Definition: sys.c:204
KrkValue krk_dirObject(int argc, const KrkValue argv[], int hasKw)
Obtain a list of properties for an object.
Definition: builtins.c:13
void krk_module_init_dis(void)
Initialize the built-in 'dis' module.
Definition: debug.c:768
struct KrkVM KrkVM
Global VM state.
void krk_dumpTraceback(void)
If there is an active exception, print a traceback to stderr.
Definition: exceptions.c:351
void krk_addObjects(void)
Concatenate two strings.
Definition: obj_str.c:909
void krk_push(KrkValue value)
Push a stack value.
Definition: vm.c:157
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:3219
KrkValue krk_callDirect(KrkObj *callable, int argCount)
Call a closure or native function with argCount arguments.
Definition: vm.c:771
KrkValue krk_instanceSetAttribute_wrapper(KrkValue owner, KrkString *name, KrkValue to)
Set an attribute of an instance object, bypassing __setattr__.
Definition: vm.c:1898
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:178
KrkValue krk_operator_gt(KrkValue, KrkValue)
Compare to values, returning True if the left is greater than the right.
void krk_module_init_gc(void)
Initialize the built-in 'gc' module.
Definition: memory.c:584
KrkValue krk_interpret(const char *src, char *fromFile)
Compile and execute a source code input.
Definition: vm.c:3229