mirror of
https://github.com/janet-lang/janet
synced 2025-11-08 03:23:01 +00:00
Much work on compiler. Fixing bugs and gradually cleaning
up code. Generalized some vector code.
This commit is contained in:
@@ -95,18 +95,19 @@ const uint8_t *dst_symbol_gen(const uint8_t *buf, int32_t len);
|
||||
#define dst_csymbolv(cstr) dst_wrap_symbol(dst_csymbol(cstr))
|
||||
|
||||
/* Structs */
|
||||
#define dst_struct_raw(t) ((int32_t *)(t) - 2)
|
||||
#define dst_struct_raw(t) ((int32_t *)(t) - 4)
|
||||
#define dst_struct_length(t) (dst_struct_raw(t)[0])
|
||||
#define dst_struct_capacity(t) (dst_struct_length(t) * 4)
|
||||
#define dst_struct_hash(t) ((dst_struct_raw(t)[1]))
|
||||
DstValue *dst_struct_begin(int32_t count);
|
||||
void dst_struct_put(DstValue *st, DstValue key, DstValue value);
|
||||
const DstValue *dst_struct_end(DstValue *st);
|
||||
DstValue dst_struct_get(const DstValue *st, DstValue key);
|
||||
DstValue dst_struct_next(const DstValue *st, DstValue key);
|
||||
DstTable *dst_struct_to_table(const DstValue *st);
|
||||
int dst_struct_equal(const DstValue *lhs, const DstValue *rhs);
|
||||
int dst_struct_compare(const DstValue *lhs, const DstValue *rhs);
|
||||
#define dst_struct_capacity(t) (dst_struct_raw(t)[1])
|
||||
#define dst_struct_hash(t) (dst_struct_raw(t)[2])
|
||||
/* Do something with the 4th header slot - flags? */
|
||||
DstKV *dst_struct_begin(int32_t count);
|
||||
void dst_struct_put(DstKV *st, DstValue key, DstValue value);
|
||||
const DstKV *dst_struct_end(DstKV *st);
|
||||
DstValue dst_struct_get(const DstKV *st, DstValue key);
|
||||
const DstKV *dst_struct_next(const DstKV *st, const DstKV *kv);
|
||||
DstTable *dst_struct_to_table(const DstKV *st);
|
||||
int dst_struct_equal(const DstKV *lhs, const DstKV *rhs);
|
||||
int dst_struct_compare(const DstKV *lhs, const DstKV *rhs);
|
||||
|
||||
/* Table functions */
|
||||
DstTable *dst_table(int32_t capacity);
|
||||
@@ -115,9 +116,9 @@ void dst_table_deinit(DstTable *table);
|
||||
DstValue dst_table_get(DstTable *t, DstValue key);
|
||||
DstValue dst_table_remove(DstTable *t, DstValue key);
|
||||
void dst_table_put(DstTable *t, DstValue key, DstValue value);
|
||||
DstValue dst_table_next(DstTable *t, DstValue key);
|
||||
const DstValue *dst_table_to_struct(DstTable *t);
|
||||
void dst_table_merge(DstTable *t, DstValue other);
|
||||
const DstKV *dst_table_next(DstTable *t, const DstKV *kv);
|
||||
const DstKV *dst_table_to_struct(DstTable *t);
|
||||
void dst_table_merge(DstTable *t, DstValue other);
|
||||
|
||||
/* Fiber */
|
||||
DstFiber *dst_fiber(int32_t capacity);
|
||||
@@ -129,15 +130,11 @@ void dst_fiber_push(DstFiber *fiber, DstValue x);
|
||||
void dst_fiber_push2(DstFiber *fiber, DstValue x, DstValue y);
|
||||
void dst_fiber_push3(DstFiber *fiber, DstValue x, DstValue y, DstValue z);
|
||||
void dst_fiber_pushn(DstFiber *fiber, const DstValue *arr, int32_t n);
|
||||
DstValue dst_fiber_popvalue(DstFiber *fiber);
|
||||
void dst_fiber_funcframe(DstFiber *fiber, DstFunction *func);
|
||||
void dst_fiber_funcframe_tail(DstFiber *fiber, DstFunction *func);
|
||||
void dst_fiber_cframe(DstFiber *fiber);
|
||||
void dst_fiber_popframe(DstFiber *fiber);
|
||||
|
||||
/* Functions */
|
||||
void dst_function_detach(DstFunction *func);
|
||||
|
||||
/* Assembly */
|
||||
DstAssembleResult dst_asm(DstAssembleOptions opts);
|
||||
DstFunction *dst_asm_func(DstAssembleResult result);
|
||||
@@ -147,7 +144,7 @@ DstValue dst_asm_decode_instruction(uint32_t instr);
|
||||
/* Treat similar types through uniform interfaces for iteration */
|
||||
int dst_seq_view(DstValue seq, const DstValue **data, int32_t *len);
|
||||
int dst_chararray_view(DstValue str, const uint8_t **data, int32_t *len);
|
||||
int dst_hashtable_view(DstValue tab, const DstValue **data, int32_t *len, int32_t *cap);
|
||||
int dst_hashtable_view(DstValue tab, const DstKV **data, int32_t *len, int32_t *cap);
|
||||
|
||||
/* Abstract */
|
||||
#define dst_abstract_header(u) ((DstAbstractHeader *)(u) - 1)
|
||||
@@ -160,7 +157,7 @@ int32_t dst_hash(DstValue x);
|
||||
int dst_compare(DstValue x, DstValue y);
|
||||
DstValue dst_get(DstValue ds, DstValue key);
|
||||
void dst_put(DstValue ds, DstValue key, DstValue value);
|
||||
DstValue dst_next(DstValue ds, DstValue key);
|
||||
const DstKV *dst_next(DstValue ds, const DstKV *kv);
|
||||
int32_t dst_length(DstValue x);
|
||||
int32_t dst_capacity(DstValue x);
|
||||
DstValue dst_getindex(DstValue ds, int32_t index);
|
||||
@@ -169,11 +166,19 @@ void dst_setindex(DstValue ds, DstValue value, int32_t index);
|
||||
/* Utils */
|
||||
extern const char dst_base64[65];
|
||||
int32_t dst_array_calchash(const DstValue *array, int32_t len);
|
||||
int32_t dst_kv_calchash(const DstKV *kvs, int32_t len);
|
||||
int32_t dst_string_calchash(const uint8_t *str, int32_t len);
|
||||
int32_t dst_tablen(int32_t n);
|
||||
DstValue dst_loadreg(DstReg *regs, size_t count);
|
||||
DstValue dst_scan_number(const uint8_t *src, int32_t len);
|
||||
int32_t dst_scan_integer(const uint8_t *str, int32_t len, int *err);
|
||||
double dst_scan_real(const uint8_t *str, int32_t len, int *err);
|
||||
int dst_cstrcmp(const uint8_t *str, const char *other);
|
||||
const void *dst_strbinsearch(
|
||||
const void *tab,
|
||||
size_t tabcount,
|
||||
size_t itemsize,
|
||||
const uint8_t *key);
|
||||
|
||||
/* Parsing */
|
||||
DstParseResult dst_parse(const uint8_t *src, int32_t len);
|
||||
|
||||
@@ -28,7 +28,8 @@
|
||||
|
||||
/*
|
||||
* Detect OS and endianess.
|
||||
* From webkit source.
|
||||
* From webkit source. There is likely some extreneous
|
||||
* detection for unsupported platforms
|
||||
*/
|
||||
|
||||
/* Check Unix */
|
||||
@@ -116,6 +117,14 @@
|
||||
#define DST_RECURSION_GUARD 1000
|
||||
|
||||
/* Use nanboxed values - uses 8 bytes per value instead of 12 or 16. */
|
||||
//#define DST_NANBOX
|
||||
#define DST_NANBOX
|
||||
#define DST_NANBOX_47
|
||||
|
||||
/* Alignment for pointers */
|
||||
#ifdef DST_32
|
||||
#define DST_WALIGN 4
|
||||
#else
|
||||
#define DST_WALIGN 8
|
||||
#endif
|
||||
|
||||
#endif /* DST_CONFIG_H_defined */
|
||||
|
||||
@@ -44,6 +44,7 @@ typedef struct DstReg DstReg;
|
||||
typedef struct DstAbstractHeader DstAbstractHeader;
|
||||
typedef struct DstFuncDef DstFuncDef;
|
||||
typedef struct DstFuncEnv DstFuncEnv;
|
||||
typedef struct DstKV DstKV;
|
||||
typedef struct DstStackFrame DstStackFrame;
|
||||
typedef struct DstAbstractType DstAbstractType;
|
||||
typedef int (*DstCFunction)(int32_t argn, DstValue *argv, DstValue *ret);
|
||||
@@ -109,11 +110,13 @@ union DstValue {
|
||||
double real;
|
||||
};
|
||||
|
||||
#define dst_u64(x) ((x).u64)
|
||||
|
||||
/* This representation uses 48 bit pointers. The trade off vs. the LuaJIT style
|
||||
* 47 bit payload representaion is that the type bits are no long contiguous. Type
|
||||
* checking can still be fast, but typewise polymorphism takes a bit longer. However,
|
||||
* hopefully we can avoid some annoying problems that occur when trying to use 47 bit pointers
|
||||
* in a 48 bit address space (Linux on ARM) */
|
||||
* in a 48 bit address space (Linux on ARM). If DST_NANBOX_47 is set, use 47 bit tagged pointers. */
|
||||
|
||||
/* |.......Tag.......|.......................Payload..................| */
|
||||
/* Non-double: t|11111111111|1ttt|xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
|
||||
@@ -121,44 +124,61 @@ union DstValue {
|
||||
|
||||
/* Double (no NaNs): x xxxxxxxxxxx xxxx xxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */
|
||||
|
||||
/* A simple scheme for nan boxed values */
|
||||
/* normal doubles, denormalized doubles, and infinities are doubles */
|
||||
/* Quiet nan is nil. Sign bit should be 0. */
|
||||
#if defined (DST_NANBOX_47) || defined (DST_32)
|
||||
|
||||
#define DST_NANBOX_TAGBITS 0xFFFF800000000000lu
|
||||
#define DST_NANBOX_PAYLOADBITS 0x00007FFFFFFFFFFFlu
|
||||
|
||||
|
||||
#define dst_nanbox_lowtag(type) \
|
||||
((uint64_t)(type) | 0x1FFF0)
|
||||
|
||||
#define dst_nanbox_tag(type) \
|
||||
(dst_nanbox_lowtag(type) << 47)
|
||||
|
||||
#define dst_type(x) \
|
||||
(isnan((x).real) \
|
||||
? (((x).u64 >> 47) & 0xF) \
|
||||
: DST_REAL)
|
||||
|
||||
#else /* defined (DST_NANBOX_47) || defined (DST_32) */
|
||||
|
||||
#define DST_NANBOX_TYPEBITS 0x0007000000000000lu
|
||||
#define DST_NANBOX_TAGBITS 0xFFFF000000000000lu
|
||||
#define DST_NANBOX_PAYLOADBITS 0x0000FFFFFFFFFFFFlu
|
||||
#ifdef DST_64
|
||||
#define DST_NANBOX_POINTERBITS 0x0000FFFFFFFFFFFFlu
|
||||
#else
|
||||
#define DST_NANBOX_POINTERBITS 0x00000000FFFFFFFFlu
|
||||
#endif
|
||||
|
||||
#define dst_u64(x) ((x).u64)
|
||||
#define dst_nanbox_lowtag(type) \
|
||||
((((uint64_t)(type) & 0x8) << 12) | 0x7FF8 | (type))
|
||||
((((uint64_t)(type) & 0x1) << 15) | 0x7FF8 | ((type) >> 1))
|
||||
|
||||
#define dst_nanbox_tag(type) \
|
||||
(dst_nanbox_lowtag(type) << 48)
|
||||
|
||||
#define dst_type(x) \
|
||||
(isnan((x).real) \
|
||||
? (((x).u64 >> 47) & 0xE) | ((x).u64 >> 63) \
|
||||
: DST_REAL)
|
||||
|
||||
#endif /* defined (DST_NANBOX_47) || defined (DST_32) */
|
||||
|
||||
/* 32 bit mode will not use the full payload for pointers. */
|
||||
#ifdef DST_32
|
||||
#define DST_NANBOX_POINTERBITS 0xFFFFFFFFlu
|
||||
#else
|
||||
#define DST_NANBOX_POINTERBITS DST_NANBOX_PAYLOADBITS
|
||||
#endif
|
||||
|
||||
#define dst_nanbox_checkauxtype(x, type) \
|
||||
(((x).u64 & DST_NANBOX_TAGBITS) == dst_nanbox_tag((type)))
|
||||
|
||||
/* Check if number is nan or if number is real double */
|
||||
#define dst_nanbox_isreal(x) \
|
||||
(!isnan((x).real) || dst_nanbox_checkauxtype((x), DST_REAL))
|
||||
|
||||
#define dst_type(x) \
|
||||
(isnan((x).real) \
|
||||
? (((x).u64 & DST_NANBOX_TYPEBITS) >> 48) | (((x).u64 >> 60) & 0x8) \
|
||||
: DST_REAL)
|
||||
|
||||
#define dst_checktype(x, t) \
|
||||
(((t) == DST_REAL) \
|
||||
? dst_nanbox_isreal(x) \
|
||||
: dst_nanbox_checkauxtype((x), (t)))
|
||||
|
||||
void *dst_nanbox_to_pointer(DstValue x);
|
||||
void dst_nanbox_memempty(DstValue *mem, int32_t count);
|
||||
void dst_nanbox_memempty(DstKV *mem, int32_t count);
|
||||
void *dst_nanbox_memalloc_empty(int32_t count);
|
||||
DstValue dst_nanbox_from_pointer(void *p, uint64_t tagmask);
|
||||
DstValue dst_nanbox_from_cpointer(const void *p, uint64_t tagmask);
|
||||
@@ -176,10 +196,10 @@ DstValue dst_nanbox_from_bits(uint64_t bits);
|
||||
dst_nanbox_from_bits(dst_nanbox_tag(t) | (p))
|
||||
|
||||
#define dst_nanbox_wrap_(p, t) \
|
||||
dst_nanbox_from_pointer((p), dst_nanbox_tag(t) | 0x7FF8000000000000lu)
|
||||
dst_nanbox_from_pointer((p), dst_nanbox_tag(t))
|
||||
|
||||
#define dst_nanbox_wrap_c(p, t) \
|
||||
dst_nanbox_from_cpointer((p), dst_nanbox_tag(t) | 0x7FF8000000000000lu)
|
||||
dst_nanbox_from_cpointer((p), dst_nanbox_tag(t))
|
||||
|
||||
/* Wrap the simple types */
|
||||
#define dst_wrap_nil() dst_nanbox_from_payload(DST_NIL, 1)
|
||||
@@ -191,7 +211,7 @@ DstValue dst_nanbox_from_bits(uint64_t bits);
|
||||
|
||||
/* Unwrap the simple types */
|
||||
#define dst_unwrap_boolean(x) \
|
||||
(((x).u64 >> 48) == dst_nanbox_lowtag(DST_TRUE))
|
||||
(dst_checktype(x, DST_TRUE))
|
||||
#define dst_unwrap_integer(x) \
|
||||
((int32_t)((x).u64 & 0xFFFFFFFFlu))
|
||||
#define dst_unwrap_real(x) ((x).real)
|
||||
@@ -210,7 +230,7 @@ DstValue dst_nanbox_from_bits(uint64_t bits);
|
||||
#define dst_wrap_cfunction(s) dst_nanbox_wrap_((s), DST_CFUNCTION)
|
||||
|
||||
/* Unwrap the pointer types */
|
||||
#define dst_unwrap_struct(x) ((const DstValue *)dst_nanbox_to_pointer(x))
|
||||
#define dst_unwrap_struct(x) ((const DstKV *)dst_nanbox_to_pointer(x))
|
||||
#define dst_unwrap_tuple(x) ((const DstValue *)dst_nanbox_to_pointer(x))
|
||||
#define dst_unwrap_fiber(x) ((DstFiber *)dst_nanbox_to_pointer(x))
|
||||
#define dst_unwrap_array(x) ((DstArray *)dst_nanbox_to_pointer(x))
|
||||
@@ -239,14 +259,14 @@ struct DstValue {
|
||||
};
|
||||
|
||||
#define dst_u64(x) ((x).as.u64)
|
||||
#define dst_memempty(mem, count) memset((mem), 0, sizeof(DstValue) * (count))
|
||||
#define dst_memalloc_empty(count) calloc((count), sizeof(DstValue))
|
||||
#define dst_memempty(mem, count) memset((mem), 0, sizeof(DstKV) * (count))
|
||||
#define dst_memalloc_empty(count) calloc((count), sizeof(DstKV))
|
||||
#define dst_type(x) ((x).type)
|
||||
#define dst_checktype(x, t) ((x).type == (t))
|
||||
#define dst_truthy(x) \
|
||||
((x).type != DST_NIL && (x).type != DST_FALSE)
|
||||
|
||||
#define dst_unwrap_struct(x) ((const DstValue *)(x).as.pointer)
|
||||
#define dst_unwrap_struct(x) ((const DstKV *)(x).as.pointer)
|
||||
#define dst_unwrap_tuple(x) ((const DstValue *)(x).as.pointer)
|
||||
#define dst_unwrap_fiber(x) ((DstFiber *)(x).as.pointer)
|
||||
#define dst_unwrap_array(x) ((DstArray *)(x).as.pointer)
|
||||
@@ -272,7 +292,7 @@ DstValue dst_wrap_string(const uint8_t *x);
|
||||
DstValue dst_wrap_symbol(const uint8_t *x);
|
||||
DstValue dst_wrap_array(DstArray *x);
|
||||
DstValue dst_wrap_tuple(const DstValue *x);
|
||||
DstValue dst_wrap_struct(const DstValue *x);
|
||||
DstValue dst_wrap_struct(const DstKV *x);
|
||||
DstValue dst_wrap_fiber(DstFiber *x);
|
||||
DstValue dst_wrap_buffer(DstBuffer *x);
|
||||
DstValue dst_wrap_function(DstFunction *x);
|
||||
@@ -332,12 +352,18 @@ struct DstBuffer {
|
||||
|
||||
/* A mutable associative data type. Backed by a hashtable. */
|
||||
struct DstTable {
|
||||
DstValue *data;
|
||||
DstKV *data;
|
||||
int32_t count;
|
||||
int32_t capacity;
|
||||
int32_t deleted;
|
||||
};
|
||||
|
||||
/* A key value pair in a struct or table */
|
||||
struct DstKV {
|
||||
DstValue key;
|
||||
DstValue value;
|
||||
};
|
||||
|
||||
/* Some function defintion flags */
|
||||
#define DST_FUNCDEF_FLAG_VARARG 1
|
||||
#define DST_FUNCDEF_FLAG_NEEDSENV 4
|
||||
|
||||
Reference in New Issue
Block a user