2017-12-03 17:52:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* 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>
|
2017-12-30 21:46:59 +00:00
|
|
|
#include <dst/dststl.h>
|
2017-12-15 00:33:45 +00:00
|
|
|
#include "compile.h"
|
2017-12-21 04:03:34 +00:00
|
|
|
#include "gc.h"
|
2017-12-30 21:46:59 +00:00
|
|
|
#include "sourcemap.h"
|
2018-01-06 16:09:15 +00:00
|
|
|
#include "util.h"
|
2017-12-03 17:52:09 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Throw an error with a dst string. */
|
2018-01-06 16:09:15 +00:00
|
|
|
void dstc_error(DstCompiler *c, const Dst *sourcemap, const uint8_t *m) {
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Don't override first error */
|
|
|
|
if (c->result.status == DST_COMPILE_ERROR) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
if (NULL != sourcemap) {
|
|
|
|
c->result.error_start = dst_unwrap_integer(sourcemap[0]);
|
|
|
|
c->result.error_end = dst_unwrap_integer(sourcemap[1]);
|
|
|
|
} else {
|
|
|
|
c->result.error_start = -1;
|
|
|
|
c->result.error_end = -1;
|
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
c->result.status = DST_COMPILE_ERROR;
|
2017-12-30 21:46:59 +00:00
|
|
|
c->result.error = m;
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
/* Throw an error with a message in a cstring */
|
2018-01-06 16:09:15 +00:00
|
|
|
void dstc_cerror(DstCompiler *c, const Dst *sourcemap, const char *m) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_error(c, sourcemap, dst_cstring(m));
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Use these to get sub options. They will traverse the source map so
|
|
|
|
* compiler errors make sense. Then modify the returned options. */
|
2018-01-06 16:09:15 +00:00
|
|
|
DstFopts dstc_getindex(DstFopts opts, int32_t index) {
|
|
|
|
const Dst *sourcemap = dst_sourcemap_index(opts.sourcemap, index);
|
|
|
|
Dst nextval = dst_getindex(opts.x, index);
|
2017-12-15 00:33:45 +00:00
|
|
|
opts.x = nextval;
|
2018-01-04 02:36:10 +00:00
|
|
|
opts.flags = 0;
|
2017-12-15 00:33:45 +00:00
|
|
|
opts.sourcemap = sourcemap;
|
|
|
|
return opts;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Index into the key of a table or struct */
|
2018-01-06 16:09:15 +00:00
|
|
|
DstFopts dstc_getkey(DstFopts opts, Dst key) {
|
|
|
|
const Dst *sourcemap = dst_sourcemap_key(opts.sourcemap, key);
|
2017-12-15 00:33:45 +00:00
|
|
|
opts.x = key;
|
|
|
|
opts.sourcemap = sourcemap;
|
2018-01-04 02:36:10 +00:00
|
|
|
opts.flags = 0;
|
2017-12-15 00:33:45 +00:00
|
|
|
return opts;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Index into the value of a table or struct */
|
2018-01-06 16:09:15 +00:00
|
|
|
DstFopts dstc_getvalue(DstFopts opts, Dst key) {
|
|
|
|
const Dst *sourcemap = dst_sourcemap_value(opts.sourcemap, key);
|
|
|
|
Dst nextval = dst_get(opts.x, key);
|
2017-12-15 00:33:45 +00:00
|
|
|
opts.x = nextval;
|
|
|
|
opts.sourcemap = sourcemap;
|
2018-01-04 02:36:10 +00:00
|
|
|
opts.flags = 0;
|
2017-12-15 00:33:45 +00:00
|
|
|
return opts;
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Check error */
|
2018-01-12 15:41:27 +00:00
|
|
|
int dstc_iserr(DstFopts *opts) {
|
2018-01-05 21:17:55 +00:00
|
|
|
return (opts->compiler->result.status == DST_COMPILE_ERROR);
|
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Allocate a slot index */
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t dstc_lsloti(DstCompiler *c) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstScope *scope = &dst_v_last(c->scopes);
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Get the nth bit in the array */
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i, biti, len;
|
2017-12-21 04:03:34 +00:00
|
|
|
biti = -1;
|
2018-01-05 21:17:55 +00:00
|
|
|
len = dst_v_count(scope->slots);
|
|
|
|
for (i = 0; i < len; i++) {
|
2017-12-21 04:03:34 +00:00
|
|
|
uint32_t block = scope->slots[i];
|
2018-01-05 21:17:55 +00:00
|
|
|
if (block == 0xFFFFFFFF) continue;
|
|
|
|
biti = i << 5; /* + clz(block) */
|
|
|
|
while (block & 1) {
|
|
|
|
biti++;
|
|
|
|
block >>= 1;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
break;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
if (biti == -1) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_v_push(scope->slots, len == 7 ? 0xFFFF0000 : 0);
|
|
|
|
biti = len << 5;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
/* set the bit at index biti */
|
|
|
|
scope->slots[biti >> 5] |= 1 << (biti & 0x1F);
|
|
|
|
if (biti > scope->smax)
|
|
|
|
scope->smax = biti;
|
|
|
|
return biti;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free a slot index */
|
2018-01-12 15:41:27 +00:00
|
|
|
void dstc_sfreei(DstCompiler *c, int32_t index) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstScope *scope = &dst_v_last(c->scopes);
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Don't free the pre allocated slots */
|
2018-01-05 21:17:55 +00:00
|
|
|
if (index >= 0 && (index < 0xF0 || index > 0xFF) &&
|
|
|
|
index < (dst_v_count(scope->slots) << 5))
|
2017-12-21 04:03:34 +00:00
|
|
|
scope->slots[index >> 5] &= ~(1 << (index & 0x1F));
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Allocate a local near (n) slot and return its index. Slot
|
|
|
|
* has maximum index max. Common value for max would be 0xFF,
|
|
|
|
* the highest slot index representable with one byte. */
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t dstc_lslotn(DstCompiler *c, int32_t max, int32_t nth) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t ret = dstc_lsloti(c);
|
2018-01-04 02:36:10 +00:00
|
|
|
if (ret > max) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_sfreei(c, ret);
|
2018-01-04 02:36:10 +00:00
|
|
|
ret = 0xF0 + nth;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Free a slot */
|
2018-01-05 21:17:55 +00:00
|
|
|
void dstc_freeslot(DstCompiler *c, DstSlot s) {
|
2018-01-04 02:36:10 +00:00
|
|
|
if (s.flags & (DST_SLOT_CONSTANT | DST_SLOT_NAMED)) return;
|
|
|
|
if (s.envindex > 0) return;
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_sfreei(c, s.index);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a slot to a scope with a symbol associated with it (def or var). */
|
2018-01-12 15:41:27 +00:00
|
|
|
void dstc_nameslot(DstCompiler *c, const uint8_t *sym, DstSlot s) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstScope *scope = &dst_v_last(c->scopes);
|
|
|
|
SymPair sp;
|
|
|
|
sp.sym = sym;
|
|
|
|
sp.slot = s;
|
|
|
|
sp.slot.flags |= DST_SLOT_NAMED;
|
|
|
|
dst_v_push(scope->syms, sp);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enter a new scope */
|
2018-01-05 21:17:55 +00:00
|
|
|
void dstc_scope(DstCompiler *c, int flags) {
|
|
|
|
DstScope scope;
|
|
|
|
scope.consts = NULL;
|
|
|
|
scope.syms = NULL;
|
|
|
|
scope.envs = NULL;
|
|
|
|
scope.defs = NULL;
|
|
|
|
scope.slots = NULL;
|
|
|
|
scope.smax = -1;
|
|
|
|
scope.bytecode_start = dst_v_count(c->buffer);
|
|
|
|
scope.flags = flags;
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Inherit slots */
|
2018-01-05 21:17:55 +00:00
|
|
|
if ((!(flags & DST_SCOPE_FUNCTION)) && dst_v_count(c->scopes)) {
|
|
|
|
DstScope *oldscope = &dst_v_last(c->scopes);
|
|
|
|
scope.smax = oldscope->smax;
|
|
|
|
scope.slots = dst_v_copy(oldscope->slots);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_v_push(c->scopes, scope);
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
/* Leave a scope. */
|
2018-01-05 21:17:55 +00:00
|
|
|
void dstc_popscope(DstCompiler *c) {
|
|
|
|
DstScope scope;
|
|
|
|
int32_t oldcount = dst_v_count(c->scopes);
|
|
|
|
dst_assert(oldcount, "could not pop scope");
|
|
|
|
scope = dst_v_last(c->scopes);
|
|
|
|
/* Free the scope */
|
|
|
|
dst_v_free(scope.consts);
|
|
|
|
dst_v_free(scope.syms);
|
|
|
|
dst_v_free(scope.envs);
|
|
|
|
dst_v_free(scope.defs);
|
|
|
|
dst_v_free(scope.slots);
|
|
|
|
dst_v_pop(c->scopes);
|
2018-01-06 16:09:15 +00:00
|
|
|
/* Move free slots to parent scope if not a new function.
|
|
|
|
* We need to know the total number of slots used when compiling the function. */
|
|
|
|
if (!(scope.flags & (DST_SCOPE_FUNCTION | DST_SCOPE_UNUSED)) && oldcount > 1) {
|
|
|
|
DstScope *newscope = &dst_v_last(c->scopes);
|
|
|
|
if (newscope->smax < scope.smax)
|
|
|
|
newscope->smax = scope.smax;
|
|
|
|
}
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Create a slot with a constant */
|
2018-01-06 16:09:15 +00:00
|
|
|
DstSlot dstc_cslot(Dst x) {
|
2017-12-21 04:03:34 +00:00
|
|
|
DstSlot ret;
|
|
|
|
ret.flags = (1 << dst_type(x)) | DST_SLOT_CONSTANT;
|
|
|
|
ret.index = -1;
|
|
|
|
ret.constant = x;
|
|
|
|
ret.envindex = 0;
|
2017-12-15 00:33:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow searching for symbols. Return information about the symbol */
|
2018-01-05 21:17:55 +00:00
|
|
|
DstSlot dstc_resolve(
|
2017-12-15 00:33:45 +00:00
|
|
|
DstCompiler *c,
|
2018-01-06 16:09:15 +00:00
|
|
|
const Dst *sourcemap,
|
2017-12-15 00:33:45 +00:00
|
|
|
const uint8_t *sym) {
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
DstSlot ret = dstc_cslot(dst_wrap_nil());
|
|
|
|
DstScope *top = &dst_v_last(c->scopes);
|
|
|
|
DstScope *scope = top;
|
2017-12-16 06:17:53 +00:00
|
|
|
int foundlocal = 1;
|
2018-01-04 02:36:10 +00:00
|
|
|
int unused = 0;
|
2017-12-15 00:33:45 +00:00
|
|
|
|
|
|
|
/* Search scopes for symbol, starting from top */
|
|
|
|
while (scope >= c->scopes) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i, len;
|
2018-01-04 02:36:10 +00:00
|
|
|
if (scope->flags & DST_SCOPE_UNUSED)
|
|
|
|
unused = 1;
|
2018-01-05 21:17:55 +00:00
|
|
|
len = dst_v_count(scope->syms);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (scope->syms[i].sym == sym) {
|
|
|
|
ret = scope->syms[i].slot;
|
|
|
|
ret.flags |= DST_SLOT_NAMED;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
2017-12-16 06:17:53 +00:00
|
|
|
if (scope->flags & DST_SCOPE_FUNCTION)
|
|
|
|
foundlocal = 0;
|
2017-12-21 04:03:34 +00:00
|
|
|
scope--;
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Symbol not found - check for global */
|
2018-01-05 21:17:55 +00:00
|
|
|
{
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst check = dst_get(c->env, dst_wrap_symbol(sym));
|
|
|
|
Dst ref;
|
2018-01-05 21:17:55 +00:00
|
|
|
if (!(dst_checktype(check, DST_STRUCT) || dst_checktype(check, DST_TABLE))) {
|
|
|
|
dstc_error(c, sourcemap, dst_formatc("unknown symbol %q", sym));
|
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
|
|
|
ref = dst_get(check, dst_csymbolv("ref"));
|
|
|
|
if (dst_checktype(ref, DST_ARRAY)) {
|
|
|
|
DstSlot ret = dstc_cslot(ref);
|
|
|
|
/* TODO save type info */
|
|
|
|
ret.flags |= DST_SLOT_REF | DST_SLOT_NAMED | DST_SLOT_MUTABLE | DST_SLOTTYPE_ANY;
|
|
|
|
ret.flags &= ~DST_SLOT_CONSTANT;
|
|
|
|
return ret;
|
|
|
|
} else {
|
2018-01-06 16:09:15 +00:00
|
|
|
Dst value = dst_get(check, dst_csymbolv("value"));
|
2018-01-05 21:17:55 +00:00
|
|
|
return dstc_cslot(value);
|
|
|
|
}
|
|
|
|
}
|
2017-12-15 00:33:45 +00:00
|
|
|
|
|
|
|
/* Symbol was found */
|
|
|
|
found:
|
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
/* Constants can be returned immediately (they are stateless) */
|
2018-01-04 02:36:10 +00:00
|
|
|
if (ret.flags & (DST_SLOT_CONSTANT | DST_SLOT_REF))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* Unused references and locals shouldn't add captured envs. */
|
|
|
|
if (unused || foundlocal) {
|
|
|
|
ret.envindex = 0;
|
2017-12-15 00:33:45 +00:00
|
|
|
return ret;
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2017-12-15 00:33:45 +00:00
|
|
|
|
|
|
|
/* non-local scope needs to expose its environment */
|
|
|
|
if (!foundlocal) {
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Find function scope */
|
|
|
|
while (scope >= c->scopes && !(scope->flags & DST_SCOPE_FUNCTION)) scope--;
|
|
|
|
dst_assert(scope >= c->scopes, "invalid scopes");
|
2017-12-15 00:33:45 +00:00
|
|
|
scope->flags |= DST_SCOPE_ENV;
|
2018-01-05 21:17:55 +00:00
|
|
|
if (!dst_v_count(scope->envs)) dst_v_push(scope->envs, 0);
|
2017-12-15 00:33:45 +00:00
|
|
|
scope++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Propogate env up to current scope */
|
2018-01-04 02:36:10 +00:00
|
|
|
int32_t envindex = 0;
|
2018-01-05 21:17:55 +00:00
|
|
|
while (scope <= top) {
|
2017-12-16 06:17:53 +00:00
|
|
|
if (scope->flags & DST_SCOPE_FUNCTION) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t j, len;
|
2017-12-16 06:17:53 +00:00
|
|
|
int scopefound = 0;
|
|
|
|
/* Check if scope already has env. If so, break */
|
2018-01-05 21:17:55 +00:00
|
|
|
len = dst_v_count(scope->envs);
|
|
|
|
for (j = 1; j < len; j++) {
|
2017-12-21 04:03:34 +00:00
|
|
|
if (scope->envs[j] == envindex) {
|
2017-12-16 06:17:53 +00:00
|
|
|
scopefound = 1;
|
2017-12-21 04:03:34 +00:00
|
|
|
envindex = j;
|
2017-12-16 06:17:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Add the environment if it is not already referenced */
|
2018-01-05 21:17:55 +00:00
|
|
|
if (!scopefound) {
|
|
|
|
if (!dst_v_count(scope->envs)) dst_v_push(scope->envs, 0);
|
2018-01-13 19:08:42 +00:00
|
|
|
len = dst_v_count(scope->envs);
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_v_push(scope->envs, envindex);
|
|
|
|
envindex = len;
|
|
|
|
}
|
2017-12-15 00:33:45 +00:00
|
|
|
}
|
|
|
|
scope++;
|
|
|
|
}
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
ret.envindex = envindex;
|
2017-12-15 00:33:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-16 06:17:53 +00:00
|
|
|
/* Emit a raw instruction with source mapping. */
|
2018-01-06 16:09:15 +00:00
|
|
|
void dstc_emit(DstCompiler *c, const Dst *sourcemap, uint32_t instr) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_v_push(c->buffer, instr);
|
2017-12-16 06:17:53 +00:00
|
|
|
if (NULL != sourcemap) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dst_v_push(c->mapbuffer, dst_unwrap_integer(sourcemap[0]));
|
|
|
|
dst_v_push(c->mapbuffer, dst_unwrap_integer(sourcemap[1]));
|
|
|
|
} else {
|
|
|
|
dst_v_push(c->mapbuffer, -1);
|
|
|
|
dst_v_push(c->mapbuffer, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Add a constant to the current scope. Return the index of the constant. */
|
|
|
|
static int32_t dstc_const(DstCompiler *c, const Dst *sourcemap, Dst x) {
|
|
|
|
DstScope *scope = &dst_v_last(c->scopes);
|
|
|
|
int32_t i, len;
|
|
|
|
/* Get the topmost function scope */
|
|
|
|
while (scope > c->scopes) {
|
|
|
|
if (scope->flags & DST_SCOPE_FUNCTION)
|
|
|
|
break;
|
|
|
|
scope--;
|
|
|
|
}
|
|
|
|
/* Check if already added */
|
|
|
|
len = dst_v_count(scope->consts);
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (dst_equals(x, scope->consts[i]))
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/* Ensure not too many constsants. */
|
|
|
|
if (len >= 0xFFFF) {
|
|
|
|
dstc_cerror(c, sourcemap, "too many constants");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dst_v_push(scope->consts, x);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Load a constant into a local slot */
|
2018-01-06 16:09:15 +00:00
|
|
|
static void dstc_loadconst(DstCompiler *c, const Dst *sourcemap, Dst k, int32_t dest) {
|
2018-01-05 21:17:55 +00:00
|
|
|
switch (dst_type(k)) {
|
|
|
|
case DST_NIL:
|
|
|
|
dstc_emit(c, sourcemap, (dest << 8) | DOP_LOAD_NIL);
|
|
|
|
break;
|
|
|
|
case DST_TRUE:
|
|
|
|
dstc_emit(c, sourcemap, (dest << 8) | DOP_LOAD_TRUE);
|
|
|
|
break;
|
|
|
|
case DST_FALSE:
|
|
|
|
dstc_emit(c, sourcemap, (dest << 8) | DOP_LOAD_FALSE);
|
|
|
|
break;
|
|
|
|
case DST_INTEGER:
|
|
|
|
{
|
|
|
|
int32_t i = dst_unwrap_integer(k);
|
|
|
|
if (i <= INT16_MAX && i >= INT16_MIN) {
|
|
|
|
dstc_emit(c, sourcemap,
|
|
|
|
(i << 16) |
|
|
|
|
(dest << 8) |
|
|
|
|
DOP_LOAD_INTEGER);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
int32_t cindex = dstc_const(c, sourcemap, k);
|
|
|
|
dstc_emit(c, sourcemap,
|
|
|
|
(cindex << 16) |
|
|
|
|
(dest << 8) |
|
|
|
|
DOP_LOAD_CONSTANT);
|
|
|
|
break;
|
|
|
|
}
|
2017-12-16 06:17:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Realize any slot to a local slot. Call this to get a slot index
|
|
|
|
* that can be used in an instruction. */
|
2018-01-12 15:41:27 +00:00
|
|
|
int32_t dstc_preread(
|
2017-12-16 06:17:53 +00:00
|
|
|
DstCompiler *c,
|
2018-01-06 16:09:15 +00:00
|
|
|
const Dst *sourcemap,
|
2017-12-16 06:17:53 +00:00
|
|
|
int32_t max,
|
|
|
|
int nth,
|
2017-12-21 04:03:34 +00:00
|
|
|
DstSlot s) {
|
2017-12-15 00:33:45 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
int32_t ret;
|
|
|
|
|
|
|
|
if (s.flags & DST_SLOT_REF)
|
|
|
|
max = 0xFF;
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
if (s.flags & (DST_SLOT_CONSTANT | DST_SLOT_REF)) {
|
2018-01-05 21:17:55 +00:00
|
|
|
ret = dstc_lslotn(c, 0xFF, nth);
|
|
|
|
dstc_loadconst(c, sourcemap, s.constant, ret);
|
2017-12-21 04:03:34 +00:00
|
|
|
/* If we also are a reference, deref the one element array */
|
|
|
|
if (s.flags & DST_SLOT_REF) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-21 04:03:34 +00:00
|
|
|
(ret << 16) |
|
|
|
|
(ret << 8) |
|
|
|
|
DOP_GET_INDEX);
|
|
|
|
}
|
|
|
|
} else if (s.envindex > 0 || s.index > max) {
|
2018-01-05 21:17:55 +00:00
|
|
|
ret = dstc_lslotn(c, max, nth);
|
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
((uint32_t)(s.index) << 24) |
|
|
|
|
((uint32_t)(s.envindex) << 16) |
|
|
|
|
((uint32_t)(ret) << 8) |
|
|
|
|
DOP_LOAD_UPVALUE);
|
|
|
|
} else if (s.index > max) {
|
2018-01-05 21:17:55 +00:00
|
|
|
ret = dstc_lslotn(c, max, nth);
|
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
((uint32_t)(s.index) << 16) |
|
|
|
|
((uint32_t)(ret) << 8) |
|
2017-12-21 04:03:34 +00:00
|
|
|
DOP_MOVE_NEAR);
|
2017-12-16 06:17:53 +00:00
|
|
|
} else {
|
|
|
|
/* We have a normal slot that fits in the required bit width */
|
2017-12-21 04:03:34 +00:00
|
|
|
ret = s.index;
|
2017-12-16 06:17:53 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Call this to release a read handle after emitting the instruction. */
|
2018-01-12 15:41:27 +00:00
|
|
|
void dstc_postread(DstCompiler *c, DstSlot s, int32_t index) {
|
2017-12-21 04:03:34 +00:00
|
|
|
if (index != s.index || s.envindex > 0 || s.flags & DST_SLOT_CONSTANT) {
|
|
|
|
/* We need to free the temporary slot */
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_sfreei(c, index);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Move values from one slot to another. The destination must
|
|
|
|
* be writeable (not a literal). */
|
2018-01-12 15:41:27 +00:00
|
|
|
void dstc_copy(
|
2017-12-16 06:17:53 +00:00
|
|
|
DstCompiler *c,
|
2018-01-06 16:09:15 +00:00
|
|
|
const Dst *sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
DstSlot dest,
|
|
|
|
DstSlot src) {
|
|
|
|
int writeback = 0;
|
|
|
|
int32_t destlocal = -1;
|
|
|
|
int32_t srclocal = -1;
|
|
|
|
int32_t reflocal = -1;
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Can't write to constants */
|
|
|
|
if (dest.flags & DST_SLOT_CONSTANT) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_cerror(c, sourcemap, "cannot write to constant");
|
|
|
|
return;
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Short circuit if dest and source are equal */
|
|
|
|
if (dest.flags == src.flags &&
|
|
|
|
dest.index == src.index &&
|
|
|
|
dest.envindex == src.envindex) {
|
2018-01-04 02:36:10 +00:00
|
|
|
if (dest.flags & (DST_SLOT_REF)) {
|
2017-12-30 21:46:59 +00:00
|
|
|
if (dst_equals(dest.constant, src.constant))
|
|
|
|
return;
|
2017-12-21 04:03:34 +00:00
|
|
|
} else {
|
2017-12-30 21:46:59 +00:00
|
|
|
return;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Types of slots - src */
|
|
|
|
/* constants */
|
|
|
|
/* upvalues */
|
|
|
|
/* refs */
|
|
|
|
/* near index */
|
|
|
|
/* far index */
|
|
|
|
|
|
|
|
/* Types of slots - dest */
|
|
|
|
/* upvalues */
|
|
|
|
/* refs */
|
|
|
|
/* near index */
|
|
|
|
/* far index */
|
|
|
|
|
|
|
|
/* If dest is a near index, do some optimization */
|
|
|
|
if (dest.envindex == 0 && dest.index >= 0 && dest.index <= 0xFF) {
|
|
|
|
if (src.flags & DST_SLOT_CONSTANT) {
|
|
|
|
dstc_loadconst(c, sourcemap, src.constant, dest.index);
|
|
|
|
} else if (src.flags & DST_SLOT_REF) {
|
|
|
|
dstc_loadconst(c, sourcemap, src.constant, dest.index);
|
|
|
|
dstc_emit(c, sourcemap,
|
|
|
|
(dest.index << 16) |
|
|
|
|
(dest.index << 8) |
|
|
|
|
DOP_GET_INDEX);
|
|
|
|
} else if (src.envindex > 0) {
|
|
|
|
dstc_emit(c, sourcemap,
|
|
|
|
(src.index << 24) |
|
|
|
|
(src.envindex << 16) |
|
|
|
|
(dest.index << 8) |
|
|
|
|
DOP_LOAD_UPVALUE);
|
|
|
|
} else {
|
|
|
|
dstc_emit(c, sourcemap,
|
|
|
|
(src.index << 16) |
|
|
|
|
(dest.index << 8) |
|
|
|
|
DOP_MOVE_NEAR);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Process: src -> srclocal -> destlocal -> dest */
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* src -> srclocal */
|
2018-01-05 21:17:55 +00:00
|
|
|
srclocal = dstc_preread(c, sourcemap, 0xFF, 1, src);
|
2017-12-30 21:46:59 +00:00
|
|
|
|
|
|
|
/* Pull down dest (find destlocal) */
|
|
|
|
if (dest.flags & DST_SLOT_REF) {
|
|
|
|
writeback = 1;
|
|
|
|
destlocal = srclocal;
|
2018-01-05 21:17:55 +00:00
|
|
|
reflocal = dstc_lslotn(c, 0xFF, 2);
|
|
|
|
dstc_emit(c, sourcemap,
|
|
|
|
(dstc_const(c, sourcemap, dest.constant) << 16) |
|
2017-12-30 21:46:59 +00:00
|
|
|
(reflocal << 8) |
|
2017-12-21 04:03:34 +00:00
|
|
|
DOP_LOAD_CONSTANT);
|
2017-12-30 21:46:59 +00:00
|
|
|
} else if (dest.envindex > 0) {
|
|
|
|
writeback = 2;
|
|
|
|
destlocal = srclocal;
|
|
|
|
} else if (dest.index > 0xFF) {
|
|
|
|
writeback = 3;
|
|
|
|
destlocal = srclocal;
|
|
|
|
} else {
|
|
|
|
destlocal = dest.index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* srclocal -> destlocal */
|
|
|
|
if (srclocal != destlocal) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
((uint32_t)(srclocal) << 16) |
|
|
|
|
((uint32_t)(destlocal) << 8) |
|
|
|
|
DOP_MOVE_NEAR);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
/* destlocal -> dest */
|
|
|
|
if (writeback == 1) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
(destlocal << 16) |
|
|
|
|
(reflocal << 8) |
|
|
|
|
DOP_PUT_INDEX);
|
|
|
|
} else if (writeback == 2) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
((uint32_t)(dest.index) << 24) |
|
|
|
|
((uint32_t)(dest.envindex) << 16) |
|
|
|
|
((uint32_t)(destlocal) << 8) |
|
2017-12-21 04:03:34 +00:00
|
|
|
DOP_SET_UPVALUE);
|
2017-12-30 21:46:59 +00:00
|
|
|
} else if (writeback == 3) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_emit(c, sourcemap,
|
2017-12-30 21:46:59 +00:00
|
|
|
((uint32_t)(dest.index) << 16) |
|
|
|
|
((uint32_t)(destlocal) << 8) |
|
2017-12-21 04:03:34 +00:00
|
|
|
DOP_MOVE_FAR);
|
|
|
|
}
|
2017-12-30 21:46:59 +00:00
|
|
|
|
|
|
|
/* Cleanup */
|
|
|
|
if (reflocal >= 0) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_sfreei(c, reflocal);
|
2017-12-16 06:17:53 +00:00
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_postread(c, src, srclocal);
|
2017-12-16 06:17:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-17 04:11:51 +00:00
|
|
|
/* Generate the return instruction for a slot. */
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSlot dstc_return(DstCompiler *c, const Dst *sourcemap, DstSlot s) {
|
2018-01-04 02:36:10 +00:00
|
|
|
if (!(s.flags & DST_SLOT_RETURNED)) {
|
|
|
|
if (s.flags & DST_SLOT_CONSTANT && dst_checktype(s.constant, DST_NIL)) {
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_emit(c, sourcemap, DOP_RETURN_NIL);
|
2018-01-04 02:36:10 +00:00
|
|
|
} else {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t ls = dstc_preread(c, sourcemap, 0xFFFF, 1, s);
|
|
|
|
dstc_emit(c, sourcemap, DOP_RETURN | (ls << 8));
|
|
|
|
dstc_postread(c, s, ls);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
s.flags |= DST_SLOT_RETURNED;
|
2017-12-17 04:11:51 +00:00
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
return s;
|
2017-12-17 04:11:51 +00:00
|
|
|
}
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Get a target slot for emitting an instruction. Will always return
|
|
|
|
* a local slot. */
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSlot dstc_gettarget(DstFopts opts) {
|
2018-01-04 02:36:10 +00:00
|
|
|
DstSlot slot;
|
|
|
|
if ((opts.flags & DST_FOPTS_HINT) &&
|
|
|
|
(opts.hint.envindex == 0) &&
|
|
|
|
(opts.hint.index >= 0 && opts.hint.index <= 0xFF)) {
|
|
|
|
slot = opts.hint;
|
|
|
|
} else {
|
|
|
|
slot.envindex = 0;
|
|
|
|
slot.constant = dst_wrap_nil();
|
|
|
|
slot.flags = 0;
|
2018-01-05 21:17:55 +00:00
|
|
|
slot.index = dstc_lslotn(opts.compiler, 0xFF, 4);
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
return slot;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Get a bunch of slots for function arguments */
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSM *dstc_toslots(DstFopts opts, int32_t start) {
|
2017-12-21 04:03:34 +00:00
|
|
|
int32_t i, len;
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSM *ret = NULL;
|
2018-01-05 21:17:55 +00:00
|
|
|
len = dst_length(opts.x);
|
|
|
|
for (i = start; i < len; i++) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSM sm;
|
2018-01-06 16:09:15 +00:00
|
|
|
DstFopts subopts = dstc_getindex(opts, i);
|
2018-01-05 21:17:55 +00:00
|
|
|
sm.slot = dstc_value(subopts);
|
|
|
|
sm.map = subopts.sourcemap;
|
|
|
|
dst_v_push(ret, sm);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Get a bunch of slots for function arguments */
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSM *dstc_toslotskv(DstFopts opts) {
|
|
|
|
DstSM *ret = NULL;
|
2018-01-05 21:17:55 +00:00
|
|
|
const DstKV *kv = NULL;
|
|
|
|
while (NULL != (kv = dst_next(opts.x, kv))) {
|
2018-01-12 15:41:27 +00:00
|
|
|
DstSM km, vm;
|
2018-01-06 16:09:15 +00:00
|
|
|
DstFopts kopts = dstc_getkey(opts, kv->key);
|
|
|
|
DstFopts vopts = dstc_getvalue(opts, kv->key);
|
2018-01-05 21:17:55 +00:00
|
|
|
km.slot = dstc_value(kopts);
|
|
|
|
km.map = kopts.sourcemap;
|
|
|
|
vm.slot = dstc_value(vopts);
|
|
|
|
vm.map = vopts.sourcemap;
|
|
|
|
dst_v_push(ret, km);
|
|
|
|
dst_v_push(ret, vm);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Push slots load via dstc_toslots. */
|
|
|
|
void dstc_pushslots(DstFopts opts, DstSM *sms) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
2018-01-06 16:09:15 +00:00
|
|
|
const Dst *sm = opts.sourcemap;
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < dst_v_count(sms) - 2; i += 3) {
|
|
|
|
int32_t ls1 = dstc_preread(c, sms[i].map, 0xFF, 1, sms[i].slot);
|
|
|
|
int32_t ls2 = dstc_preread(c, sms[i + 1].map, 0xFF, 2, sms[i + 1].slot);
|
|
|
|
int32_t ls3 = dstc_preread(c, sms[i + 2].map, 0xFF, 3, sms[i + 2].slot);
|
|
|
|
dstc_emit(c, sm,
|
2017-12-21 04:03:34 +00:00
|
|
|
(ls3 << 24) |
|
|
|
|
(ls2 << 16) |
|
|
|
|
(ls1 << 8) |
|
|
|
|
DOP_PUSH_3);
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_postread(c, sms[i].slot, ls1);
|
|
|
|
dstc_postread(c, sms[i + 1].slot, ls2);
|
|
|
|
dstc_postread(c, sms[i + 2].slot, ls3);
|
|
|
|
}
|
|
|
|
if (i == dst_v_count(sms) - 2) {
|
|
|
|
int32_t ls1 = dstc_preread(c, sms[i].map, 0xFF, 1, sms[i].slot);
|
|
|
|
int32_t ls2 = dstc_preread(c, sms[i + 1].map, 0xFFFF, 2, sms[i + 1].slot);
|
|
|
|
dstc_emit(c, sm,
|
2017-12-21 04:03:34 +00:00
|
|
|
(ls2 << 16) |
|
|
|
|
(ls1 << 8) |
|
|
|
|
DOP_PUSH_2);
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_postread(c, sms[i].slot, ls1);
|
|
|
|
dstc_postread(c, sms[i + 1].slot, ls2);
|
|
|
|
} else if (i == dst_v_count(sms) - 1) {
|
|
|
|
int32_t ls1 = dstc_preread(c, sms[i].map, 0xFFFFFF, 1, sms[i].slot);
|
|
|
|
dstc_emit(c, sm,
|
2017-12-21 04:03:34 +00:00
|
|
|
(ls1 << 8) |
|
|
|
|
DOP_PUSH);
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_postread(c, sms[i].slot, ls1);
|
2017-12-17 04:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Free slots loaded via dstc_toslots */
|
|
|
|
void dstc_freeslots(DstFopts opts, DstSM *sms) {
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t i;
|
|
|
|
for (i = 0; i < dst_v_count(sms); i++) {
|
|
|
|
dstc_freeslot(opts.compiler, sms[i].slot);
|
|
|
|
}
|
|
|
|
dst_v_free(sms);
|
|
|
|
}
|
|
|
|
|
2018-01-04 02:36:10 +00:00
|
|
|
/* Compile some code that will be thrown away. Used to ensure
|
|
|
|
* that dead code is well formed without including it in the final
|
|
|
|
* bytecode. */
|
2018-01-12 15:41:27 +00:00
|
|
|
void dstc_throwaway(DstFopts opts) {
|
2018-01-04 02:36:10 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
2018-01-05 21:17:55 +00:00
|
|
|
int32_t bufstart = dst_v_count(c->buffer);
|
|
|
|
dstc_scope(c, DST_SCOPE_UNUSED);
|
|
|
|
dstc_value(opts);
|
|
|
|
dstc_popscope(c);
|
|
|
|
if (NULL != c->buffer) {
|
|
|
|
dst_v__cnt(c->buffer) = bufstart;
|
|
|
|
if (NULL != c->mapbuffer)
|
|
|
|
dst_v__cnt(c->mapbuffer) = bufstart;
|
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile a call or tailcall instruction */
|
|
|
|
static DstSlot dstc_call(DstFopts opts, DstSM *sms, DstSlot fun) {
|
|
|
|
DstSlot retslot;
|
|
|
|
int32_t localindex;
|
2018-01-04 02:36:10 +00:00
|
|
|
DstCompiler *c = opts.compiler;
|
2018-01-06 16:09:15 +00:00
|
|
|
const Dst *sm = opts.sourcemap;
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_pushslots(opts, sms);
|
|
|
|
dstc_freeslots(opts, sms);
|
|
|
|
localindex = dstc_preread(c, sm, 0xFF, 1, fun);
|
|
|
|
if (opts.flags & DST_FOPTS_TAIL) {
|
|
|
|
dstc_emit(c, sm, (localindex << 8) | DOP_TAILCALL);
|
|
|
|
retslot = dstc_cslot(dst_wrap_nil());
|
|
|
|
retslot.flags = DST_SLOT_RETURNED;
|
|
|
|
} else {
|
|
|
|
retslot = dstc_gettarget(opts);
|
|
|
|
dstc_emit(c, sm, (localindex << 16) | (retslot.index << 8) | DOP_CALL);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
dstc_postread(c, fun, localindex);
|
|
|
|
return retslot;
|
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile a tuple */
|
|
|
|
DstSlot dstc_tuple(DstFopts opts) {
|
|
|
|
DstSlot head;
|
|
|
|
DstFopts subopts;
|
|
|
|
const Dst *tup = dst_unwrap_tuple(opts.x);
|
|
|
|
/* Empty tuple is tuple literal */
|
|
|
|
if (dst_tuple_length(tup) == 0) return dstc_cslot(opts.x);
|
|
|
|
/* Symbols could be specials */
|
|
|
|
if (dst_checktype(tup[0], DST_SYMBOL)) {
|
|
|
|
const DstSpecial *s = dstc_special(dst_unwrap_symbol(tup[0]));
|
|
|
|
if (NULL != s) {
|
|
|
|
return s->compile(opts, dst_tuple_length(tup) - 1, tup + 1);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile the head of the tuple */
|
|
|
|
subopts = dstc_getindex(opts, 0);
|
|
|
|
subopts.flags = DST_FUNCTION | DST_CFUNCTION;
|
|
|
|
head = dstc_value(subopts);
|
|
|
|
return dstc_call(opts, dstc_toslots(opts, 1), head);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
static DstSlot dstc_array(DstFopts opts) {
|
|
|
|
return dstc_call(opts, dstc_toslots(opts, 0), dstc_cslot(dst_wrap_cfunction(dst_stl_array)));
|
2017-12-30 21:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
static DstSlot dstc_tablector(DstFopts opts, DstCFunction cfun) {
|
|
|
|
return dstc_call(opts, dstc_toslotskv(opts), dstc_cslot(dst_wrap_cfunction(cfun)));
|
|
|
|
}
|
2018-01-04 02:36:10 +00:00
|
|
|
|
2018-01-12 15:41:27 +00:00
|
|
|
/* Compile a single value */
|
|
|
|
DstSlot dstc_value(DstFopts opts) {
|
|
|
|
DstSlot ret;
|
|
|
|
if (dstc_iserr(&opts)) {
|
2018-01-05 21:17:55 +00:00
|
|
|
return dstc_cslot(dst_wrap_nil());
|
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
if (opts.compiler->recursion_guard <= 0) {
|
|
|
|
dstc_cerror(opts.compiler, opts.sourcemap, "recursed too deeply");
|
|
|
|
return dstc_cslot(dst_wrap_nil());
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
opts.compiler->recursion_guard--;
|
|
|
|
switch (dst_type(opts.x)) {
|
|
|
|
default:
|
|
|
|
ret = dstc_cslot(opts.x);
|
|
|
|
break;
|
|
|
|
case DST_SYMBOL:
|
|
|
|
{
|
|
|
|
const uint8_t *sym = dst_unwrap_symbol(opts.x);
|
|
|
|
ret = dstc_resolve(opts.compiler, opts.sourcemap, sym);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DST_TUPLE:
|
|
|
|
ret = dstc_tuple(opts);
|
|
|
|
break;
|
|
|
|
case DST_ARRAY:
|
|
|
|
ret = dstc_array(opts);
|
|
|
|
break;
|
|
|
|
case DST_STRUCT:
|
|
|
|
ret = dstc_tablector(opts, dst_stl_struct);
|
|
|
|
break;
|
|
|
|
case DST_TABLE:
|
|
|
|
ret = dstc_tablector(opts, dst_stl_table);
|
|
|
|
break;
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
if (opts.flags & DST_FOPTS_TAIL) {
|
|
|
|
ret = dstc_return(opts.compiler, opts.sourcemap, ret);
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
2018-01-12 15:41:27 +00:00
|
|
|
opts.compiler->recursion_guard++;
|
|
|
|
return ret;
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Compile a funcdef */
|
2018-01-12 15:41:27 +00:00
|
|
|
DstFuncDef *dstc_pop_funcdef(DstCompiler *c) {
|
2018-01-05 21:17:55 +00:00
|
|
|
DstScope scope = dst_v_last(c->scopes);
|
|
|
|
DstFuncDef *def = dst_gcalloc(DST_MEMORY_FUNCDEF, sizeof(DstFuncDef));
|
2018-01-04 02:36:10 +00:00
|
|
|
def->source = NULL;
|
|
|
|
def->sourcepath = NULL;
|
2018-01-05 21:17:55 +00:00
|
|
|
def->sourcemap = NULL;
|
|
|
|
def->slotcount = scope.smax + 1;
|
2018-01-04 02:36:10 +00:00
|
|
|
|
|
|
|
/* Copy envs */
|
2018-01-05 21:17:55 +00:00
|
|
|
def->environments_length = dst_v_count(scope.envs);
|
|
|
|
def->environments = NULL;
|
|
|
|
if (def->environments_length > 1) def->environments = dst_v_flatten(scope.envs);
|
2018-01-04 02:36:10 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
def->constants_length = dst_v_count(scope.consts);
|
|
|
|
def->constants = dst_v_flatten(scope.consts);
|
2018-01-04 02:36:10 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
def->defs_length = dst_v_count(scope.defs);
|
|
|
|
def->defs = dst_v_flatten(scope.defs);
|
2018-01-04 02:36:10 +00:00
|
|
|
|
|
|
|
/* Copy bytecode */
|
2018-01-05 21:17:55 +00:00
|
|
|
def->bytecode_length = dst_v_count(c->buffer) - scope.bytecode_start;
|
2018-01-04 02:36:10 +00:00
|
|
|
if (def->bytecode_length) {
|
2018-01-05 21:17:55 +00:00
|
|
|
size_t s = sizeof(int32_t) * def->bytecode_length;
|
|
|
|
def->bytecode = malloc(s);
|
2018-01-04 02:36:10 +00:00
|
|
|
if (NULL == def->bytecode) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
memcpy(def->bytecode, c->buffer + scope.bytecode_start, s);
|
|
|
|
dst_v__cnt(c->buffer) = scope.bytecode_start;
|
|
|
|
if (NULL != c->mapbuffer) {
|
|
|
|
def->sourcemap = malloc(2 * s);
|
|
|
|
if (NULL == def->sourcemap) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(def->sourcemap, c->mapbuffer + scope.bytecode_start, 2 * s);
|
|
|
|
dst_v__cnt(c->mapbuffer) = scope.bytecode_start;
|
2018-01-04 02:36:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def->arity = 0;
|
|
|
|
def->flags = 0;
|
2018-01-05 21:17:55 +00:00
|
|
|
if (scope.flags & DST_SCOPE_ENV) {
|
2018-01-04 02:36:10 +00:00
|
|
|
def->flags |= DST_FUNCDEF_FLAG_NEEDSENV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop the scope */
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_popscope(c);
|
2018-01-04 02:36:10 +00:00
|
|
|
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2017-12-17 04:11:51 +00:00
|
|
|
|
2017-12-21 04:03:34 +00:00
|
|
|
/* Initialize a compiler */
|
2018-01-06 16:09:15 +00:00
|
|
|
static void dstc_init(DstCompiler *c, Dst env) {
|
2017-12-21 04:03:34 +00:00
|
|
|
c->scopes = NULL;
|
|
|
|
c->buffer = NULL;
|
|
|
|
c->mapbuffer = NULL;
|
|
|
|
c->recursion_guard = DST_RECURSION_GUARD;
|
2017-12-30 21:46:59 +00:00
|
|
|
c->env = env;
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Init result */
|
|
|
|
c->result.error = NULL;
|
|
|
|
c->result.status = DST_COMPILE_OK;
|
|
|
|
c->result.error_start = -1;
|
|
|
|
c->result.error_end = -1;
|
|
|
|
c->result.funcdef = NULL;
|
2017-12-21 04:03:34 +00:00
|
|
|
}
|
2017-12-17 04:11:51 +00:00
|
|
|
|
|
|
|
/* Deinitialize a compiler struct */
|
2018-01-05 21:17:55 +00:00
|
|
|
static void dstc_deinit(DstCompiler *c) {
|
|
|
|
while (dst_v_count(c->scopes)) dstc_popscope(c);
|
|
|
|
dst_v_free(c->scopes);
|
|
|
|
dst_v_free(c->buffer);
|
|
|
|
dst_v_free(c->mapbuffer);
|
2017-12-21 04:03:34 +00:00
|
|
|
c->env = dst_wrap_nil();
|
2017-12-16 06:17:53 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Compile a form. */
|
|
|
|
DstCompileResult dst_compile(DstCompileOptions opts) {
|
|
|
|
DstCompiler c;
|
2018-01-06 16:09:15 +00:00
|
|
|
DstFopts fopts;
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_init(&c, opts.env);
|
2017-12-16 06:17:53 +00:00
|
|
|
|
2017-12-17 04:11:51 +00:00
|
|
|
/* Push a function scope */
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_scope(&c, DST_SCOPE_FUNCTION | DST_SCOPE_TOP);
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
/* Set initial form options */
|
|
|
|
fopts.compiler = &c;
|
2017-12-17 04:11:51 +00:00
|
|
|
fopts.sourcemap = opts.sourcemap;
|
|
|
|
fopts.flags = DST_FOPTS_TAIL | DST_SLOTTYPE_ANY;
|
2018-01-05 21:17:55 +00:00
|
|
|
fopts.hint = dstc_cslot(dst_wrap_nil());
|
2017-12-17 04:11:51 +00:00
|
|
|
fopts.x = opts.source;
|
|
|
|
|
|
|
|
/* Compile the value */
|
2018-01-12 22:38:06 +00:00
|
|
|
dstc_value(fopts);
|
2017-12-21 04:03:34 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
if (c.result.status == DST_COMPILE_OK) {
|
|
|
|
c.result.funcdef = dstc_pop_funcdef(&c);
|
|
|
|
}
|
2017-12-17 04:11:51 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
dstc_deinit(&c);
|
2017-12-17 04:11:51 +00:00
|
|
|
|
2018-01-05 21:17:55 +00:00
|
|
|
return c.result;
|
2017-12-17 04:11:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 21:46:59 +00:00
|
|
|
DstFunction *dst_compile_func(DstCompileResult res) {
|
2017-12-17 04:11:51 +00:00
|
|
|
if (res.status != DST_COMPILE_OK) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-12-21 04:03:34 +00:00
|
|
|
DstFunction *func = dst_gcalloc(DST_MEMORY_FUNCTION, sizeof(DstFunction));
|
2017-12-17 04:11:51 +00:00
|
|
|
func->def = res.funcdef;
|
|
|
|
func->envs = NULL;
|
|
|
|
return func;
|
2017-12-16 06:17:53 +00:00
|
|
|
}
|