Files
alnyan 91961bcec5 Lots of changes with long commit message again
1. (list ...)
2. string-* functions
3. (native str) to resolve native functions at runtime
4. Memory fuckups in compiler core unit loading
5. Now can call non-identifiers in compiler
2021-04-09 00:18:48 +03:00

96 lines
2.0 KiB
C

#pragma once
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
#include <stdio.h>
#include "vmstring.h"
#include "vmstate.h"
enum vm_type {
VT_CONS,
VT_STRING,
VT_FUNC,
VT_CFUNC,
};
struct vm_value {
enum vm_type type;
size_t refcount;
union {
struct {
uintptr_t fat_ar, fat_dr;
} v_cons;
struct {
size_t lib_index, fn_index;
} v_func;
struct vm_string v_string;
uintptr_t v_cfunc;
};
};
static inline int ref_q(uint64_t w) {
return (w & FLAG_REF) != 0;
}
static inline int integer_q(uint64_t w) {
return !(w & FLAG_REF);
}
static inline int null_q(uint64_t w) {
return w == FLAG_REF;
}
static inline struct vm_value *getref(uint64_t w) {
assert(ref_q(w));
return (struct vm_value *) (w << 1);
}
static inline int string_q(uint64_t w) {
return !null_q(w) && ref_q(w) && getref(w)->type == VT_STRING;
}
static inline int cons_q(uint64_t w) {
if (ref_q(w)) {
return null_q(w) || getref(w)->type == VT_CONS;
} else {
return 0;
}
}
static inline int func_q(uint64_t w) {
return ref_q(w) && !null_q(w) && getref(w)->type == VT_FUNC;
}
static inline int cfunc_q(uint64_t w) {
return ref_q(w) && !null_q(w) && getref(w)->type == VT_CFUNC;
}
static inline int pair_q(uint64_t w) {
return ref_q(w) && (!null_q(w) && getref(w)->type == VT_CONS);
}
void vm_value_ref(struct vm_value *v);
int vm_value_unref(struct vm_value *v);
static inline void vm_ref(uint64_t w) {
if (ref_q(w)) {
vm_value_ref(getref(w));
}
}
static inline int vm_unref(uint64_t w) {
if (ref_q(w)) {
return vm_value_unref(getref(w));
}
return 0;
}
void vm_value_free(struct vm_value *val);
struct vm_value *vm_cons(uint64_t w0, uint64_t w1);
struct vm_value *vm_makestr(const char *str);
struct vm_value *vm_func(size_t lib_index, size_t fn_index);
struct vm_value *vm_cfunc(uintptr_t address);
void vm_print(FILE *dst, uint64_t w);