2017-12-21 04:03:34 +00:00
|
|
|
/*
|
2018-05-18 03:41:20 +00:00
|
|
|
* Copyright (c) 2018 Calvin Rose
|
2017-12-21 04:03:34 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "compile.h"
|
2018-03-31 20:40:36 +00:00
|
|
|
#include "state.h"
|
2018-08-20 00:21:27 +00:00
|
|
|
#include "util.h"
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Generated header */
|
2018-07-04 04:17:34 +00:00
|
|
|
#include <generated/core.h>
|
2018-07-04 03:07:35 +00:00
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
/* Only include dynamic modules if enabled */
|
|
|
|
#ifdef DST_DYNAMIC_MODULES
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
/* Use LoadLibrary on windows or dlopen on posix to load dynamic libaries
|
|
|
|
* with native code. */
|
|
|
|
#ifdef DST_WINDOWS
|
|
|
|
#include <windows.h>
|
|
|
|
typedef HINSTANCE Clib;
|
|
|
|
#define load_clib(name) LoadLibrary((name))
|
|
|
|
#define symbol_clib(lib, sym) GetProcAddress((lib), (sym))
|
|
|
|
#define error_clib() "could not load dynamic library"
|
|
|
|
#elif defined(DST_WEB)
|
|
|
|
#include <emscripten.h>
|
|
|
|
/* TODO - figure out how loading modules will work in JS */
|
|
|
|
typedef int Clib;
|
|
|
|
#define load_clib(name) 0
|
|
|
|
#define symbol_clib(lib, sym) 0
|
|
|
|
#define error_clib() "dynamic libraries not supported"
|
|
|
|
#else
|
|
|
|
#include <dlfcn.h>
|
|
|
|
typedef void *Clib;
|
|
|
|
#define load_clib(name) dlopen((name), RTLD_NOW)
|
|
|
|
#define symbol_clib(lib, sym) dlsym((lib), (sym))
|
|
|
|
#define error_clib() dlerror()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
DstCFunction dst_native(const char *name, const uint8_t **error) {
|
|
|
|
Clib lib = load_clib(name);
|
|
|
|
DstCFunction init;
|
|
|
|
if (!lib) {
|
|
|
|
*error = dst_cstring(error_clib());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
init = (DstCFunction) symbol_clib(lib, "_dst_init");
|
|
|
|
if (!init) {
|
|
|
|
*error = dst_cstring("could not find _dst_init symbol");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return init;
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_native(DstArgs args) {
|
2018-07-04 03:07:35 +00:00
|
|
|
DstCFunction init;
|
|
|
|
const uint8_t *error = NULL;
|
|
|
|
const uint8_t *path = NULL;
|
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_STRING(path, args, 0);
|
|
|
|
init = dst_native((const char *)path, &error);
|
|
|
|
if (!init) {
|
|
|
|
DST_THROWV(args, dst_wrap_string(error));
|
|
|
|
}
|
|
|
|
DST_RETURN_CFUNCTION(args, init);
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
#endif
|
|
|
|
/* end DST_DYNAMIC_MODULES */
|
|
|
|
|
|
|
|
static int dst_core_print(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t j, len;
|
2018-01-14 17:10:45 +00:00
|
|
|
const uint8_t *vstr = dst_to_string(args.v[i]);
|
2017-12-30 21:46:59 +00:00
|
|
|
len = dst_string_length(vstr);
|
|
|
|
for (j = 0; j < len; ++j) {
|
|
|
|
putc(vstr[j], stdout);
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
putc('\n', stdout);
|
2018-08-06 01:13:14 +00:00
|
|
|
DST_RETURN_NIL(args);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_describe(DstArgs args) {
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t i;
|
2018-03-14 22:57:26 +00:00
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 0);
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2018-03-14 22:57:26 +00:00
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str = dst_description(args.v[i]);
|
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(&b, str, len);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2018-03-14 22:57:26 +00:00
|
|
|
*args.ret = dst_stringv(b.data, b.count);
|
|
|
|
dst_buffer_deinit(&b);
|
2018-01-04 02:36:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_string(DstArgs args) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t i;
|
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 0);
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; ++i) {
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t len;
|
2018-01-14 17:10:45 +00:00
|
|
|
const uint8_t *str = dst_to_string(args.v[i]);
|
2018-01-12 15:41:27 +00:00
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(&b, str, len);
|
|
|
|
}
|
2018-01-14 17:10:45 +00:00
|
|
|
*args.ret = dst_stringv(b.data, b.count);
|
2018-01-12 15:41:27 +00:00
|
|
|
dst_buffer_deinit(&b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_symbol(DstArgs args) {
|
2018-01-21 22:08:11 +00:00
|
|
|
int32_t i;
|
|
|
|
DstBuffer b;
|
|
|
|
dst_buffer_init(&b, 0);
|
|
|
|
for (i = 0; i < args.n; ++i) {
|
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str = dst_to_string(args.v[i]);
|
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(&b, str, len);
|
|
|
|
}
|
|
|
|
*args.ret = dst_symbolv(b.data, b.count);
|
|
|
|
dst_buffer_deinit(&b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_buffer(DstArgs args) {
|
2018-01-31 22:39:18 +00:00
|
|
|
int32_t i;
|
|
|
|
DstBuffer *b = dst_buffer(0);
|
|
|
|
for (i = 0; i < args.n; ++i) {
|
|
|
|
int32_t len;
|
|
|
|
const uint8_t *str = dst_to_string(args.v[i]);
|
|
|
|
len = dst_string_length(str);
|
|
|
|
dst_buffer_push_bytes(b, str, len);
|
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_BUFFER(args, b);
|
2018-01-16 01:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_scannumber(DstArgs args) {
|
2018-05-01 15:06:31 +00:00
|
|
|
const uint8_t *data;
|
|
|
|
Dst x;
|
|
|
|
int32_t len;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-05-01 15:06:31 +00:00
|
|
|
x = dst_scan_number(data, len);
|
2018-05-13 00:31:28 +00:00
|
|
|
if (dst_checktype(x, DST_NIL)) {
|
|
|
|
DST_THROW(args, "error parsing number");
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, x);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_scaninteger(DstArgs args) {
|
2018-05-01 15:06:31 +00:00
|
|
|
const uint8_t *data;
|
|
|
|
int32_t len, ret;
|
|
|
|
int err = 0;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-05-01 15:06:31 +00:00
|
|
|
ret = dst_scan_integer(data, len, &err);
|
|
|
|
if (err) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "error parsing integer");
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_INTEGER(args, ret);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_scanreal(DstArgs args) {
|
2018-05-01 15:06:31 +00:00
|
|
|
const uint8_t *data;
|
|
|
|
int32_t len;
|
|
|
|
double ret;
|
|
|
|
int err = 0;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_BYTES(data, len, args, 0);
|
2018-05-01 15:06:31 +00:00
|
|
|
ret = dst_scan_real(data, len, &err);
|
|
|
|
if (err) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "error parsing real");
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_REAL(args, ret);
|
2018-05-01 15:06:31 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_tuple(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_TUPLE(args, dst_tuple_n(args.v, args.n));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_array(DstArgs args) {
|
2018-01-14 17:10:45 +00:00
|
|
|
DstArray *array = dst_array(args.n);
|
|
|
|
array->count = args.n;
|
|
|
|
memcpy(array->data, args.v, args.n * sizeof(Dst));
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_ARRAY(args, array);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_table(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
DstTable *table = dst_table(args.n >> 1);
|
2018-05-20 01:16:00 +00:00
|
|
|
if (args.n & 1)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected even number of arguments");
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; i += 2) {
|
|
|
|
dst_table_put(table, args.v[i], args.v[i + 1]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_TABLE(args, table);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_struct(DstArgs args) {
|
2017-12-30 21:46:59 +00:00
|
|
|
int32_t i;
|
2018-01-14 17:10:45 +00:00
|
|
|
DstKV *st = dst_struct_begin(args.n >> 1);
|
2018-05-13 00:31:28 +00:00
|
|
|
if (args.n & 1)
|
|
|
|
DST_THROW(args, "expected even number of arguments");
|
2018-01-14 17:10:45 +00:00
|
|
|
for (i = 0; i < args.n; i += 2) {
|
|
|
|
dst_struct_put(st, args.v[i], args.v[i + 1]);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_STRUCT(args, dst_struct_end(st));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_gensym(DstArgs args) {
|
2018-07-01 19:49:33 +00:00
|
|
|
DST_FIXARITY(args, 0);
|
|
|
|
DST_RETURN_SYMBOL(args, dst_symbol_gen());
|
2018-01-05 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_gccollect(DstArgs args) {
|
2018-01-16 01:14:21 +00:00
|
|
|
(void) args;
|
|
|
|
dst_collect();
|
2017-12-30 21:46:59 +00:00
|
|
|
return 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_gcsetinterval(DstArgs args) {
|
2018-04-28 22:10:57 +00:00
|
|
|
int32_t val;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_ARG_INTEGER(val, args, 0);
|
2018-04-28 22:10:57 +00:00
|
|
|
if (val < 0)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_THROW(args, "expected non-negative integer");
|
2018-04-28 22:10:57 +00:00
|
|
|
dst_vm_gc_interval = val;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN_NIL(args);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_gcinterval(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 0);
|
|
|
|
DST_RETURN_INTEGER(args, dst_vm_gc_interval);
|
2018-02-07 05:44:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_type(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
2018-07-09 00:54:41 +00:00
|
|
|
DstType t = dst_type(args.v[0]);
|
|
|
|
if (t == DST_ABSTRACT) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, dst_csymbolv(dst_abstract_type(dst_unwrap_abstract(args.v[0]))->name));
|
2018-01-16 01:14:21 +00:00
|
|
|
} else {
|
2018-07-09 00:54:41 +00:00
|
|
|
DST_RETURN(args, dst_csymbolv(dst_type_names[t]));
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_next(DstArgs args) {
|
2018-01-17 14:58:32 +00:00
|
|
|
Dst ds;
|
|
|
|
const DstKV *kv;
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 2);
|
|
|
|
DST_CHECKMANY(args, 0, DST_TFLAG_DICTIONARY);
|
2018-01-17 14:58:32 +00:00
|
|
|
ds = args.v[0];
|
|
|
|
if (dst_checktype(ds, DST_TABLE)) {
|
|
|
|
DstTable *t = dst_unwrap_table(ds);
|
|
|
|
kv = dst_checktype(args.v[1], DST_NIL)
|
2018-02-03 23:12:07 +00:00
|
|
|
? NULL
|
2018-01-17 14:58:32 +00:00
|
|
|
: dst_table_find(t, args.v[1]);
|
2018-06-17 17:55:02 +00:00
|
|
|
kv = dst_table_next(t, kv);
|
2018-04-28 22:10:57 +00:00
|
|
|
} else {
|
2018-01-17 14:58:32 +00:00
|
|
|
const DstKV *st = dst_unwrap_struct(ds);
|
|
|
|
kv = dst_checktype(args.v[1], DST_NIL)
|
2018-02-03 23:12:07 +00:00
|
|
|
? NULL
|
2018-01-17 14:58:32 +00:00
|
|
|
: dst_struct_find(st, args.v[1]);
|
2018-06-17 17:55:02 +00:00
|
|
|
kv = dst_struct_next(st, kv);
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
2018-07-09 00:54:41 +00:00
|
|
|
if (kv)
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_RETURN(args, kv->key);
|
|
|
|
DST_RETURN_NIL(args);
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:00:11 +00:00
|
|
|
static int dst_core_hash(DstArgs args) {
|
2018-05-13 00:31:28 +00:00
|
|
|
DST_FIXARITY(args, 1);
|
|
|
|
DST_RETURN_INTEGER(args, dst_hash(args.v[0]));
|
2018-01-17 14:58:32 +00:00
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
static const DstReg cfuns[] = {
|
2018-07-04 18:00:11 +00:00
|
|
|
#ifdef DST_DYNAMIC_MODULES
|
2018-07-04 03:07:35 +00:00
|
|
|
{"native", dst_core_native},
|
2018-07-04 18:00:11 +00:00
|
|
|
#endif
|
2018-07-04 03:07:35 +00:00
|
|
|
{"print", dst_core_print},
|
|
|
|
{"describe", dst_core_describe},
|
|
|
|
{"string", dst_core_string},
|
|
|
|
{"symbol", dst_core_symbol},
|
|
|
|
{"buffer", dst_core_buffer},
|
|
|
|
{"table", dst_core_table},
|
|
|
|
{"array", dst_core_array},
|
|
|
|
{"scan-number", dst_core_scannumber},
|
|
|
|
{"scan-integer", dst_core_scaninteger},
|
|
|
|
{"scan-real", dst_core_scanreal},
|
|
|
|
{"tuple", dst_core_tuple},
|
|
|
|
{"struct", dst_core_struct},
|
|
|
|
{"buffer", dst_core_buffer},
|
|
|
|
{"gensym", dst_core_gensym},
|
|
|
|
{"gccollect", dst_core_gccollect},
|
|
|
|
{"gcsetinterval", dst_core_gcsetinterval},
|
|
|
|
{"gcinterval", dst_core_gcinterval},
|
|
|
|
{"type", dst_core_type},
|
|
|
|
{"next", dst_core_next},
|
|
|
|
{"hash", dst_core_hash},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Utility for inline assembly */
|
|
|
|
static void dst_quick_asm(
|
|
|
|
DstTable *env,
|
|
|
|
int32_t flags,
|
|
|
|
const char *name,
|
|
|
|
int32_t arity,
|
|
|
|
int32_t slots,
|
|
|
|
const uint32_t *bytecode,
|
|
|
|
size_t bytecode_size) {
|
|
|
|
DstFuncDef *def = dst_funcdef_alloc();
|
|
|
|
def->arity = arity;
|
|
|
|
def->flags = flags;
|
|
|
|
def->slotcount = slots;
|
|
|
|
def->bytecode = malloc(bytecode_size);
|
2018-08-06 01:32:32 +00:00
|
|
|
def->bytecode_length = (int32_t)(bytecode_size / sizeof(uint32_t));
|
2018-07-04 03:07:35 +00:00
|
|
|
def->name = dst_cstring(name);
|
|
|
|
if (!def->bytecode) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(def->bytecode, bytecode, bytecode_size);
|
|
|
|
dst_env_def(env, name, dst_wrap_function(dst_thunk(def)));
|
|
|
|
}
|
|
|
|
|
2018-07-07 01:50:59 +00:00
|
|
|
/* Macros for easier inline dst assembly */
|
2018-07-09 03:10:02 +00:00
|
|
|
#define SSS(op, a, b, c) ((op) | ((a) << 8) | ((b) << 16) | ((c) << 24))
|
|
|
|
#define SS(op, a, b) ((op) | ((a) << 8) | ((b) << 16))
|
|
|
|
#define SSI(op, a, b, I) ((op) | ((a) << 8) | ((b) << 16) | ((uint32_t)(I) << 24))
|
|
|
|
#define S(op, a) ((op) | ((a) << 8))
|
|
|
|
#define SI(op, a, I) ((op) | ((a) << 8) | ((uint32_t)(I) << 16))
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Templatize a varop */
|
|
|
|
static void templatize_varop(
|
|
|
|
DstTable *env,
|
|
|
|
int32_t flags,
|
|
|
|
const char *name,
|
|
|
|
int32_t nullary,
|
|
|
|
int32_t unary,
|
|
|
|
uint32_t op) {
|
2018-07-09 03:10:02 +00:00
|
|
|
|
|
|
|
/* Variadic operator assembly. Must be templatized for each different opcode. */
|
|
|
|
/* Reg 0: Argument tuple (args) */
|
|
|
|
/* Reg 1: Argument count (argn) */
|
|
|
|
/* Reg 2: Jump flag (jump?) */
|
|
|
|
/* Reg 3: Accumulator (accum) */
|
|
|
|
/* Reg 4: Next operand (operand) */
|
|
|
|
/* Reg 5: Loop iterator (i) */
|
|
|
|
uint32_t varop_asm[] = {
|
|
|
|
SS(DOP_LENGTH, 1, 0), /* Put number of arguments in register 1 -> argn = count(args) */
|
|
|
|
|
|
|
|
/* Check nullary */
|
|
|
|
SSS(DOP_EQUALS_IMMEDIATE, 2, 1, 0), /* Check if numargs equal to 0 */
|
|
|
|
SI(DOP_JUMP_IF_NOT, 2, 3), /* If not 0, jump to next check */
|
|
|
|
/* Nullary */
|
|
|
|
SI(DOP_LOAD_INTEGER, 3, nullary), /* accum = nullary value */
|
|
|
|
S(DOP_RETURN, 3), /* return accum */
|
|
|
|
|
|
|
|
/* Check unary */
|
|
|
|
SSI(DOP_EQUALS_IMMEDIATE, 2, 1, 1), /* Check if numargs equal to 1 */
|
|
|
|
SI(DOP_JUMP_IF_NOT, 2, 5), /* If not 1, jump to next check */
|
|
|
|
/* Unary */
|
|
|
|
SI(DOP_LOAD_INTEGER, 3, unary), /* accum = unary value */
|
|
|
|
SSI(DOP_GET_INDEX, 4, 0, 0), /* operand = args[0] */
|
|
|
|
SSS(op, 3, 3, 4), /* accum = accum op operand */
|
|
|
|
S(DOP_RETURN, 3), /* return accum */
|
|
|
|
|
|
|
|
/* Mutli (2 or more) arity */
|
|
|
|
/* Prime loop */
|
|
|
|
SSI(DOP_GET_INDEX, 3, 0, 0), /* accum = args[0] */
|
|
|
|
SI(DOP_LOAD_INTEGER, 5, 1), /* i = 1 */
|
|
|
|
/* Main loop */
|
|
|
|
SSS(DOP_GET, 4, 0, 5), /* operand = args[i] */
|
|
|
|
SSS(op, 3, 3, 4), /* accum = accum op operand */
|
|
|
|
SSI(DOP_ADD_IMMEDIATE, 5, 5, 1), /* i++ */
|
|
|
|
SSI(DOP_EQUALS_INTEGER, 2, 5, 1), /* jump? = (i == argn) */
|
|
|
|
SI(DOP_JUMP_IF_NOT, 2, -4), /* if not jump? go back 4 */
|
|
|
|
|
|
|
|
/* Done, do last and return accumulator */
|
|
|
|
S(DOP_RETURN, 3) /* return accum */
|
|
|
|
};
|
|
|
|
|
2018-07-04 03:07:35 +00:00
|
|
|
dst_quick_asm(
|
|
|
|
env,
|
|
|
|
flags | DST_FUNCDEF_FLAG_VARARG,
|
|
|
|
name,
|
|
|
|
0,
|
|
|
|
6,
|
|
|
|
varop_asm,
|
|
|
|
sizeof(varop_asm));
|
|
|
|
}
|
|
|
|
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Templatize variadic comparators */
|
|
|
|
static void templatize_comparator(
|
|
|
|
DstTable *env,
|
|
|
|
int32_t flags,
|
|
|
|
const char *name,
|
|
|
|
int invert,
|
|
|
|
uint32_t op) {
|
|
|
|
|
|
|
|
/* Reg 0: Argument tuple (args) */
|
|
|
|
/* Reg 1: Argument count (argn) */
|
|
|
|
/* Reg 2: Jump flag (jump?) */
|
|
|
|
/* Reg 3: Last value (last) */
|
|
|
|
/* Reg 4: Next operand (next) */
|
|
|
|
/* Reg 5: Loop iterator (i) */
|
|
|
|
uint32_t comparator_asm[] = {
|
|
|
|
SS(DOP_LENGTH, 1, 0), /* Put number of arguments in register 1 -> argn = count(args) */
|
|
|
|
SSS(DOP_LESS_THAN_IMMEDIATE, 2, 1, 2), /* Check if numargs less than 2 */
|
|
|
|
SI(DOP_JUMP_IF, 2, 10), /* If numargs < 2, jump to done */
|
|
|
|
|
|
|
|
/* Prime loop */
|
|
|
|
SSI(DOP_GET_INDEX, 3, 0, 0), /* last = args[0] */
|
|
|
|
SI(DOP_LOAD_INTEGER, 5, 1), /* i = 1 */
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
SSS(DOP_GET, 4, 0, 5), /* next = args[i] */
|
|
|
|
SSS(op, 2, 3, 4), /* jump? = last compare next */
|
|
|
|
SI(DOP_JUMP_IF_NOT, 2, 7), /* if not jump? goto fail (return false) */
|
|
|
|
SSI(DOP_ADD_IMMEDIATE, 5, 5, 1), /* i++ */
|
|
|
|
SS(DOP_MOVE_NEAR, 3, 4), /* last = next */
|
|
|
|
SSI(DOP_EQUALS_INTEGER, 2, 5, 1), /* jump? = (i == argn) */
|
|
|
|
SI(DOP_JUMP_IF_NOT, 2, -6), /* if not jump? go back 6 */
|
|
|
|
|
|
|
|
/* Done, return true */
|
|
|
|
S(invert ? DOP_LOAD_FALSE : DOP_LOAD_TRUE, 3),
|
|
|
|
S(DOP_RETURN, 3),
|
|
|
|
|
|
|
|
/* Failed, return false */
|
|
|
|
S(invert ? DOP_LOAD_TRUE : DOP_LOAD_FALSE, 3),
|
|
|
|
S(DOP_RETURN, 3)
|
|
|
|
};
|
|
|
|
|
|
|
|
dst_quick_asm(
|
|
|
|
env,
|
|
|
|
flags | DST_FUNCDEF_FLAG_VARARG,
|
|
|
|
name,
|
|
|
|
0,
|
|
|
|
6,
|
|
|
|
comparator_asm,
|
|
|
|
sizeof(comparator_asm));
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:53:39 +00:00
|
|
|
/* Make the apply function */
|
|
|
|
static void make_apply(DstTable *env) {
|
|
|
|
/* Reg 0: Function (fun) */
|
|
|
|
/* Reg 1: Argument tuple (args) */
|
|
|
|
/* Reg 2: Argument count (argn) */
|
|
|
|
/* Reg 3: Jump flag (jump?) */
|
|
|
|
/* Reg 4: Loop iterator (i) */
|
|
|
|
/* Reg 5: Loop values (x) */
|
|
|
|
uint32_t apply_asm[] = {
|
|
|
|
SS(DOP_LENGTH, 2, 1),
|
|
|
|
SSS(DOP_EQUALS_IMMEDIATE, 3, 2, 0), /* Immediate tail call if no args */
|
|
|
|
SI(DOP_JUMP_IF, 3, 9),
|
|
|
|
|
|
|
|
/* Prime loop */
|
|
|
|
SI(DOP_LOAD_INTEGER, 4, 0), /* i = 0 */
|
|
|
|
|
|
|
|
/* Main loop */
|
|
|
|
SSS(DOP_GET, 5, 1, 4), /* x = args[i] */
|
|
|
|
SSI(DOP_ADD_IMMEDIATE, 4, 4, 1), /* i++ */
|
|
|
|
SSI(DOP_EQUALS_INTEGER, 3, 4, 2), /* jump? = (i == argn) */
|
|
|
|
SI(DOP_JUMP_IF, 3, 3), /* if jump? go forward 3 */
|
|
|
|
S(DOP_PUSH, 5),
|
|
|
|
(DOP_JUMP | ((uint32_t)(-5) << 8)),
|
|
|
|
|
|
|
|
/* Push the array */
|
|
|
|
S(DOP_PUSH_ARRAY, 5),
|
|
|
|
|
|
|
|
/* Call the funciton */
|
|
|
|
S(DOP_TAILCALL, 0)
|
|
|
|
};
|
|
|
|
dst_quick_asm(env, DST_FUN_APPLY | DST_FUNCDEF_FLAG_VARARG,
|
|
|
|
"apply", 1, 6, apply_asm, sizeof(apply_asm));
|
|
|
|
}
|
|
|
|
|
2018-07-09 00:54:41 +00:00
|
|
|
DstTable *dst_core_env(void) {
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t error_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_ERROR
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t debug_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_SIGNAL | (2 << 24),
|
|
|
|
DOP_RETURN_NIL
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t yield_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_SIGNAL | (3 << 24),
|
|
|
|
DOP_RETURN
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t resume_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_RESUME | (1 << 24),
|
|
|
|
DOP_RETURN
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t get_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_GET | (1 << 24),
|
|
|
|
DOP_RETURN
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t put_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_PUT | (1 << 16) | (2 << 24),
|
|
|
|
DOP_RETURN
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t length_asm[] = {
|
2018-07-04 03:07:35 +00:00
|
|
|
DOP_LENGTH,
|
|
|
|
DOP_RETURN
|
|
|
|
};
|
2018-07-09 03:10:02 +00:00
|
|
|
static const uint32_t bnot_asm[] = {
|
|
|
|
DOP_BNOT,
|
|
|
|
DOP_RETURN
|
|
|
|
};
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
DstTable *env = dst_table(0);
|
|
|
|
Dst ret = dst_wrap_table(env);
|
|
|
|
|
|
|
|
/* Load main functions */
|
|
|
|
dst_env_cfuns(env, cfuns);
|
|
|
|
|
|
|
|
dst_quick_asm(env, DST_FUN_YIELD, "debug", 0, 1, debug_asm, sizeof(debug_asm));
|
|
|
|
dst_quick_asm(env, DST_FUN_ERROR, "error", 1, 1, error_asm, sizeof(error_asm));
|
|
|
|
dst_quick_asm(env, DST_FUN_YIELD, "yield", 1, 2, yield_asm, sizeof(yield_asm));
|
|
|
|
dst_quick_asm(env, DST_FUN_RESUME, "resume", 2, 2, resume_asm, sizeof(resume_asm));
|
|
|
|
dst_quick_asm(env, DST_FUN_GET, "get", 2, 2, get_asm, sizeof(get_asm));
|
|
|
|
dst_quick_asm(env, DST_FUN_PUT, "put", 3, 3, put_asm, sizeof(put_asm));
|
|
|
|
dst_quick_asm(env, DST_FUN_LENGTH, "length", 1, 1, length_asm, sizeof(length_asm));
|
2018-07-09 03:10:02 +00:00
|
|
|
dst_quick_asm(env, DST_FUN_BNOT, "~", 1, 1, bnot_asm, sizeof(bnot_asm));
|
2018-08-26 16:53:39 +00:00
|
|
|
make_apply(env);
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Variadic ops */
|
|
|
|
templatize_varop(env, DST_FUN_ADD, "+", 0, 0, DOP_ADD);
|
|
|
|
templatize_varop(env, DST_FUN_SUBTRACT, "-", 0, 0, DOP_SUBTRACT);
|
|
|
|
templatize_varop(env, DST_FUN_MULTIPLY, "*", 1, 1, DOP_MULTIPLY);
|
|
|
|
templatize_varop(env, DST_FUN_DIVIDE, "/", 1, 1, DOP_DIVIDE);
|
|
|
|
templatize_varop(env, DST_FUN_BAND, "&", -1, -1, DOP_BAND);
|
|
|
|
templatize_varop(env, DST_FUN_BOR, "|", 0, 0, DOP_BOR);
|
|
|
|
templatize_varop(env, DST_FUN_BXOR, "^", 0, 0, DOP_BXOR);
|
|
|
|
templatize_varop(env, DST_FUN_LSHIFT, "<<", 1, 1, DOP_SHIFT_LEFT);
|
|
|
|
templatize_varop(env, DST_FUN_RSHIFT, ">>", 1, 1, DOP_SHIFT_RIGHT);
|
|
|
|
templatize_varop(env, DST_FUN_RSHIFTU, ">>>", 1, 1, DOP_SHIFT_RIGHT_UNSIGNED);
|
|
|
|
|
2018-07-09 03:10:02 +00:00
|
|
|
/* Variadic comparators */
|
2018-07-10 01:24:22 +00:00
|
|
|
templatize_comparator(env, DST_FUN_ORDER_GT, "order>", 0, DOP_GREATER_THAN);
|
|
|
|
templatize_comparator(env, DST_FUN_ORDER_LT, "order<", 0, DOP_LESS_THAN);
|
|
|
|
templatize_comparator(env, DST_FUN_ORDER_GTE, "order>=", 1, DOP_LESS_THAN);
|
|
|
|
templatize_comparator(env, DST_FUN_ORDER_LTE, "order<=", 1, DOP_GREATER_THAN);
|
|
|
|
templatize_comparator(env, DST_FUN_ORDER_EQ, "=", 0, DOP_EQUALS);
|
|
|
|
templatize_comparator(env, DST_FUN_ORDER_NEQ, "not=", 1, DOP_EQUALS);
|
|
|
|
templatize_comparator(env, DST_FUN_GT, ">", 0, DOP_NUMERIC_GREATER_THAN);
|
|
|
|
templatize_comparator(env, DST_FUN_LT, "<", 0, DOP_NUMERIC_LESS_THAN);
|
|
|
|
templatize_comparator(env, DST_FUN_GTE, ">=", 0, DOP_NUMERIC_GREATER_THAN_EQUAL);
|
|
|
|
templatize_comparator(env, DST_FUN_LTE, "<=", 0, DOP_NUMERIC_LESS_THAN_EQUAL);
|
|
|
|
templatize_comparator(env, DST_FUN_EQ, "==", 0, DOP_NUMERIC_EQUAL);
|
|
|
|
templatize_comparator(env, DST_FUN_NEQ, "not==", 1, DOP_NUMERIC_EQUAL);
|
2018-07-09 03:10:02 +00:00
|
|
|
|
2018-08-07 04:54:47 +00:00
|
|
|
/* Platform detection */
|
|
|
|
dst_env_def(env, "dst.version", dst_cstringv(DST_VERSION));
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
/* Set as gc root */
|
|
|
|
dst_gcroot(dst_wrap_table(env));
|
|
|
|
|
|
|
|
/* Load auxiliary envs */
|
|
|
|
{
|
|
|
|
DstArgs args;
|
|
|
|
args.n = 1;
|
|
|
|
args.v = &ret;
|
|
|
|
args.ret = &ret;
|
|
|
|
dst_lib_io(args);
|
|
|
|
dst_lib_math(args);
|
|
|
|
dst_lib_array(args);
|
|
|
|
dst_lib_tuple(args);
|
|
|
|
dst_lib_buffer(args);
|
|
|
|
dst_lib_table(args);
|
|
|
|
dst_lib_fiber(args);
|
|
|
|
dst_lib_os(args);
|
|
|
|
dst_lib_parse(args);
|
|
|
|
dst_lib_compile(args);
|
|
|
|
dst_lib_asm(args);
|
|
|
|
dst_lib_string(args);
|
|
|
|
dst_lib_marsh(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow references to the environment */
|
|
|
|
dst_env_def(env, "_env", ret);
|
|
|
|
|
|
|
|
/* Run bootstrap source */
|
2018-07-04 04:17:34 +00:00
|
|
|
dst_dobytes(env, dst_gen_core, sizeof(dst_gen_core), "core.dst");
|
2018-07-04 03:07:35 +00:00
|
|
|
|
|
|
|
return env;
|
|
|
|
}
|