1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-15 05:04:49 +00:00
janet/src/core/compile.c

727 lines
22 KiB
C
Raw Normal View History

/*
* Copyright (c) 2018 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-15 00:33:45 +00:00
#include "compile.h"
2018-07-01 15:52:15 +00:00
#include "emit.h"
#include "vector.h"
DstFopts dstc_fopts_default(DstCompiler *c) {
DstFopts ret;
ret.compiler = c;
ret.flags = 0;
ret.hint = dstc_cslot(dst_wrap_nil());
return ret;
}
/* Throw an error with a dst string. */
2018-06-29 03:36:31 +00:00
void dstc_error(DstCompiler *c, const uint8_t *m) {
/* Don't override first error */
if (c->result.status == DST_COMPILE_ERROR) {
return;
}
c->result.status = DST_COMPILE_ERROR;
c->result.error = m;
2017-12-15 00:33:45 +00:00
}
/* Throw an error with a message in a cstring */
2018-06-29 03:36:31 +00:00
void dstc_cerror(DstCompiler *c, const char *m) {
dstc_error(c, dst_cstring(m));
2017-12-15 00:33:45 +00:00
}
/* Check error */
int dstc_iserr(DstFopts *opts) {
return (opts->compiler->result.status == DST_COMPILE_ERROR);
}
/* Get the next key in an associative data structure. Used for iterating through an
* associative data structure. */
const DstKV *dstc_next(Dst ds, const DstKV *kv) {
switch(dst_type(ds)) {
default:
return NULL;
case DST_TABLE:
return (const DstKV *) dst_table_next(dst_unwrap_table(ds), kv);
case DST_STRUCT:
return dst_struct_next(dst_unwrap_struct(ds), kv);
}
}
/* Free a slot */
void dstc_freeslot(DstCompiler *c, DstSlot s) {
2018-01-16 04:31:39 +00:00
if (s.flags & (DST_SLOT_CONSTANT | DST_SLOT_REF | DST_SLOT_NAMED)) return;
if (s.envindex >= 0) return;
2018-07-01 15:52:15 +00:00
dstc_regalloc_free(&c->scope->ra, s.index);
}
/* Add a slot to a scope with a symbol associated with it (def or var). */
void dstc_nameslot(DstCompiler *c, const uint8_t *sym, DstSlot s) {
SymPair sp;
sp.sym = sym;
sp.slot = s;
2018-01-21 19:39:32 +00:00
sp.keep = 0;
sp.slot.flags |= DST_SLOT_NAMED;
2018-07-01 15:52:15 +00:00
dst_v_push(c->scope->syms, sp);
}
/* Create a slot with a constant */
DstSlot dstc_cslot(Dst x) {
DstSlot ret;
ret.flags = (1 << dst_type(x)) | DST_SLOT_CONSTANT;
ret.index = -1;
ret.constant = x;
ret.envindex = -1;
return ret;
}
/* Get a local slot */
2018-07-01 15:52:15 +00:00
DstSlot dstc_farslot(DstCompiler *c) {
DstSlot ret;
ret.flags = DST_SLOTTYPE_ANY;
2018-07-01 19:49:33 +00:00
ret.index = dstc_allocfar(c);
2018-07-01 15:52:15 +00:00
ret.constant = dst_wrap_nil();
ret.envindex = -1;
return ret;
}
/* Enter a new scope */
2018-07-01 15:52:15 +00:00
void dstc_scope(DstScope *s, DstCompiler *c, int flags, const char *name) {
DstScope scope;
2018-07-01 15:52:15 +00:00
scope.name = name;
scope.child = NULL;
scope.consts = NULL;
scope.syms = NULL;
scope.envs = NULL;
scope.defs = NULL;
scope.selfconst = -1;
scope.bytecode_start = dst_v_count(c->buffer);
scope.flags = flags;
2018-07-01 15:52:15 +00:00
*s = scope;
/* Inherit slots */
2018-07-01 15:52:15 +00:00
if ((!(flags & DST_SCOPE_FUNCTION)) && c->scope) {
dstc_regalloc_clone(&s->ra, &(c->scope->ra));
} else {
dstc_regalloc_init(&s->ra);
}
2018-07-01 15:52:15 +00:00
/* Link parent and child and update pointer */
s->parent = c->scope;
if (c->scope)
c->scope->child = s;
c->scope = s;
2017-12-15 00:33:45 +00:00
}
/* Leave a scope. */
void dstc_popscope(DstCompiler *c) {
2018-07-01 15:52:15 +00:00
DstScope *oldscope = c->scope;
DstScope *newscope = oldscope->parent;
/* 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. */
2018-07-01 15:52:15 +00:00
if (!(oldscope->flags & (DST_SCOPE_FUNCTION | DST_SCOPE_UNUSED)) && newscope) {
if (newscope->ra.max < oldscope->ra.max)
newscope->ra.max = oldscope->ra.max;
2018-01-21 19:39:32 +00:00
/* Keep upvalue slots */
2018-07-01 15:52:15 +00:00
for (int32_t i = 0; i < dst_v_count(oldscope->syms); i++) {
SymPair pair = oldscope->syms[i];
2018-01-21 19:39:32 +00:00
if (pair.keep) {
/* The variable should not be lexically accessible */
pair.sym = NULL;
dst_v_push(newscope->syms, pair);
2018-07-01 15:52:15 +00:00
dstc_regalloc_touch(&newscope->ra, pair.slot.index);
2018-01-21 19:39:32 +00:00
}
}
}
2018-07-01 15:52:15 +00:00
/* Free the old scope */
dst_v_free(oldscope->consts);
dst_v_free(oldscope->syms);
dst_v_free(oldscope->envs);
dst_v_free(oldscope->defs);
dstc_regalloc_deinit(&oldscope->ra);
/* Update pointer */
if (newscope)
newscope->child = NULL;
c->scope = newscope;
2017-12-15 00:33:45 +00:00
}
2018-01-16 04:31:39 +00:00
/* Leave a scope but keep a slot allocated. */
void dstc_popscope_keepslot(DstCompiler *c, DstSlot retslot) {
2018-07-01 15:52:15 +00:00
DstScope *scope;
2018-01-16 04:31:39 +00:00
dstc_popscope(c);
2018-07-01 15:52:15 +00:00
scope = c->scope;
if (scope && retslot.envindex < 0 && retslot.index >= 0) {
dstc_regalloc_touch(&scope->ra, retslot.index);
2018-01-16 04:31:39 +00:00
}
}
2017-12-15 00:33:45 +00:00
/* Allow searching for symbols. Return information about the symbol */
DstSlot dstc_resolve(
2017-12-15 00:33:45 +00:00
DstCompiler *c,
const uint8_t *sym) {
DstSlot ret = dstc_cslot(dst_wrap_nil());
2018-07-01 15:52:15 +00:00
DstScope *scope = c->scope;
2018-01-21 19:39:32 +00:00
SymPair *pair;
int foundlocal = 1;
int unused = 0;
2017-12-15 00:33:45 +00:00
/* Search scopes for symbol, starting from top */
2018-07-01 15:52:15 +00:00
while (scope) {
int32_t i, len;
if (scope->flags & DST_SCOPE_UNUSED)
unused = 1;
len = dst_v_count(scope->syms);
2018-01-29 20:46:26 +00:00
/* Search in reverse order */
for (i = len - 1; i >= 0; i--) {
2018-01-21 19:39:32 +00:00
pair = scope->syms + i;
if (pair->sym == sym) {
ret = pair->slot;
goto found;
}
}
if (scope->flags & DST_SCOPE_FUNCTION)
foundlocal = 0;
2018-07-01 15:52:15 +00:00
scope = scope->parent;
2017-12-15 00:33:45 +00:00
}
/* Symbol not found - check for global */
{
Dst check;
DstBindingType btype = dst_env_resolve(c->env, sym, &check);
switch (btype) {
default:
case DST_BINDING_NONE:
2018-06-29 03:36:31 +00:00
dstc_error(c, dst_formatc("unknown symbol %q", sym));
return dstc_cslot(dst_wrap_nil());
case DST_BINDING_DEF:
case DST_BINDING_MACRO: /* Macro should function like defs when not in calling pos */
return dstc_cslot(check);
case DST_BINDING_VAR:
{
DstSlot ret = dstc_cslot(check);
/* 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;
}
}
}
2017-12-15 00:33:45 +00:00
/* Symbol was found */
found:
/* Constants can be returned immediately (they are stateless) */
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 = -1;
2017-12-15 00:33:45 +00:00
return ret;
}
2017-12-15 00:33:45 +00:00
/* non-local scope needs to expose its environment */
2018-01-21 19:39:32 +00:00
pair->keep = 1;
2018-07-01 15:52:15 +00:00
while (scope && !(scope->flags & DST_SCOPE_FUNCTION))
scope = scope->parent;
dst_assert(scope, "invalid scopes");
2018-01-21 19:39:32 +00:00
scope->flags |= DST_SCOPE_ENV;
2018-07-01 15:52:15 +00:00
scope = scope->child;
2017-12-15 00:33:45 +00:00
/* Propogate env up to current scope */
int32_t envindex = -1;
2018-07-01 15:52:15 +00:00
while (scope) {
if (scope->flags & DST_SCOPE_FUNCTION) {
int32_t j, len;
int scopefound = 0;
/* Check if scope already has env. If so, break */
len = dst_v_count(scope->envs);
for (j = 0; j < len; j++) {
if (scope->envs[j] == envindex) {
scopefound = 1;
envindex = j;
break;
}
2017-12-15 00:33:45 +00:00
}
/* Add the environment if it is not already referenced */
if (!scopefound) {
2018-01-13 19:08:42 +00:00
len = dst_v_count(scope->envs);
dst_v_push(scope->envs, envindex);
envindex = len;
}
2017-12-15 00:33:45 +00:00
}
2018-07-01 15:52:15 +00:00
scope = scope->child;
2017-12-15 00:33:45 +00:00
}
ret.envindex = envindex;
2017-12-15 00:33:45 +00:00
return ret;
}
/* Generate the return instruction for a slot. */
2018-06-29 03:36:31 +00:00
DstSlot dstc_return(DstCompiler *c, DstSlot s) {
if (!(s.flags & DST_SLOT_RETURNED)) {
2018-07-01 15:52:15 +00:00
if (s.flags & DST_SLOT_CONSTANT && dst_checktype(s.constant, DST_NIL))
2018-06-29 03:36:31 +00:00
dstc_emit(c, DOP_RETURN_NIL);
2018-07-01 15:52:15 +00:00
else
dstc_emit_s(c, DOP_RETURN, s, 0);
s.flags |= DST_SLOT_RETURNED;
}
return s;
}
/* Get a target slot for emitting an instruction. */
DstSlot dstc_gettarget(DstFopts opts) {
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 = -1;
slot.constant = dst_wrap_nil();
slot.flags = 0;
slot.index = dstc_allocfar(opts.compiler);
}
return slot;
}
/* Get a bunch of slots for function arguments */
2018-06-29 03:36:31 +00:00
DstSlot *dstc_toslots(DstCompiler *c, const Dst *vals, int32_t len) {
int32_t i;
2018-06-29 03:36:31 +00:00
DstSlot *ret = NULL;
DstFopts subopts = dstc_fopts_default(c);
for (i = 0; i < len; i++) {
2018-06-29 03:36:31 +00:00
dst_v_push(ret, dstc_value(subopts, vals[i]));
}
return ret;
}
/* Get a bunch of slots for function arguments */
2018-06-29 03:36:31 +00:00
DstSlot *dstc_toslotskv(DstCompiler *c, Dst ds) {
DstSlot *ret = NULL;
const DstKV *kv = NULL;
DstFopts subopts = dstc_fopts_default(c);
while ((kv = dstc_next(ds, kv))) {
2018-06-29 03:36:31 +00:00
dst_v_push(ret, dstc_value(subopts, kv->key));
dst_v_push(ret, dstc_value(subopts, kv->value));
}
return ret;
}
/* Push slots load via dstc_toslots. */
2018-06-29 03:36:31 +00:00
void dstc_pushslots(DstCompiler *c, DstSlot *slots) {
int32_t i;
2018-07-01 15:52:15 +00:00
for (i = 0; i < dst_v_count(slots) - 2; i += 3)
dstc_emit_sss(c, DOP_PUSH_3, slots[i], slots[i+1], slots[i+2], 0);
2018-07-01 15:52:15 +00:00
if (i == dst_v_count(slots) - 2)
dstc_emit_ss(c, DOP_PUSH_2, slots[i], slots[i+1], 0);
2018-07-01 15:52:15 +00:00
else if (i == dst_v_count(slots) - 1)
dstc_emit_s(c, DOP_PUSH, slots[i], 0);
}
/* Free slots loaded via dstc_toslots */
2018-06-29 03:36:31 +00:00
void dstc_freeslots(DstCompiler *c, DstSlot *slots) {
int32_t i;
2018-06-29 03:36:31 +00:00
for (i = 0; i < dst_v_count(slots); i++) {
dstc_freeslot(c, slots[i]);
}
2018-06-29 03:36:31 +00:00
dst_v_free(slots);
}
/* Compile some code that will be thrown away. Used to ensure
* that dead code is well formed without including it in the final
* bytecode. */
void dstc_throwaway(DstFopts opts, Dst x) {
DstCompiler *c = opts.compiler;
2018-07-01 15:52:15 +00:00
DstScope unusedScope;
int32_t bufstart = dst_v_count(c->buffer);
int32_t mapbufstart = dst_v_count(c->mapbuffer);
2018-07-01 15:52:15 +00:00
dstc_scope(&unusedScope, c, DST_SCOPE_UNUSED, "unusued");
dstc_value(opts, x);
dstc_popscope(c);
2018-07-01 15:52:15 +00:00
if (c->buffer) {
dst_v__cnt(c->buffer) = bufstart;
2018-07-01 15:52:15 +00:00
if (c->mapbuffer)
dst_v__cnt(c->mapbuffer) = mapbufstart;
}
}
/* Compile a call or tailcall instruction */
2018-06-29 03:36:31 +00:00
static DstSlot dstc_call(DstFopts opts, DstSlot *slots, DstSlot fun) {
DstSlot retslot;
DstCompiler *c = opts.compiler;
int specialized = 0;
if (fun.flags & DST_SLOT_CONSTANT) {
if (dst_checktype(fun.constant, DST_FUNCTION)) {
2018-07-01 23:35:45 +00:00
DstFunction *f = dst_unwrap_function(fun.constant);
const DstFunOptimizer *o = dstc_funopt(f->def->flags);
if (o && (!o->can_optimize || o->can_optimize(opts, slots))) {
specialized = 1;
2018-06-29 03:36:31 +00:00
retslot = o->optimize(opts, slots);
}
}
/* TODO dst function inlining (no c functions)*/
}
if (!specialized) {
2018-06-29 03:36:31 +00:00
dstc_pushslots(c, slots);
if (opts.flags & DST_FOPTS_TAIL) {
dstc_emit_s(c, DOP_TAILCALL, fun, 0);
retslot = dstc_cslot(dst_wrap_nil());
retslot.flags = DST_SLOT_RETURNED;
} else {
retslot = dstc_gettarget(opts);
dstc_emit_ss(c, DOP_CALL, retslot, fun, 1);
}
}
2018-06-29 03:36:31 +00:00
dstc_freeslots(c, slots);
return retslot;
}
static DstSlot dstc_maker(DstFopts opts, DstSlot *slots, int op) {
DstCompiler *c = opts.compiler;
DstSlot retslot;
dstc_pushslots(c, slots);
dstc_freeslots(c, slots);
retslot = dstc_gettarget(opts);
dstc_emit_s(c, op, retslot, 1);
return retslot;
}
2018-06-29 03:36:31 +00:00
static DstSlot dstc_array(DstFopts opts, Dst x) {
DstCompiler *c = opts.compiler;
DstArray *a = dst_unwrap_array(x);
return dstc_maker(opts,
dstc_toslots(c, a->data, a->count),
DOP_MAKE_ARRAY);
}
static DstSlot dstc_tablector(DstFopts opts, Dst x, int op) {
DstCompiler *c = opts.compiler;
return dstc_maker(opts,
dstc_toslotskv(c, x),
op);
}
2018-06-29 03:36:31 +00:00
static DstSlot dstc_bufferctor(DstFopts opts, Dst x) {
2018-02-03 23:12:07 +00:00
DstCompiler *c = opts.compiler;
DstBuffer *b = dst_unwrap_buffer(x);
Dst onearg = dst_stringv(b->data, b->count);
return dstc_maker(opts,
2018-02-03 23:12:07 +00:00
dstc_toslots(c, &onearg, 1),
DOP_MAKE_BUFFER);
2018-02-03 23:12:07 +00:00
}
static DstSlot dstc_symbol(DstFopts opts, const uint8_t *sym) {
if (dst_string_length(sym) && sym[0] != ':') {
2018-06-29 03:36:31 +00:00
return dstc_resolve(opts.compiler, sym);
} else {
return dstc_cslot(dst_wrap_symbol(sym));
}
}
/* Expand a macro one time. Also get the special form compiler if we
* find that instead. */
static int macroexpand1(
DstCompiler *c,
Dst x,
Dst *out,
const DstSpecial **spec) {
if (!dst_checktype(x, DST_TUPLE))
return 0;
const Dst *form = dst_unwrap_tuple(x);
if (dst_tuple_length(form) == 0)
return 0;
/* Source map - only set when we get a tuple */
if (dst_tuple_sm_line(form) > 0) {
c->current_mapping.line = dst_tuple_sm_line(form);
c->current_mapping.column = dst_tuple_sm_col(form);
}
if (!dst_checktype(form[0], DST_SYMBOL))
return 0;
const uint8_t *name = dst_unwrap_symbol(form[0]);
const DstSpecial *s = dstc_special(name);
if (s) {
*spec = s;
return 0;
}
Dst macroval;
DstBindingType btype = dst_env_resolve(c->env, name, &macroval);
if (btype != DST_BINDING_MACRO ||
!dst_checktype(macroval, DST_FUNCTION))
return 0;
/* Evaluate macro */
DstFiber *fiberp;
DstFunction *macro = dst_unwrap_function(macroval);
int lock = dst_gclock();
DstSignal status = dst_call(
macro,
dst_tuple_length(form) - 1,
form + 1,
&x,
&fiberp);
dst_gcunlock(lock);
if (status != DST_SIGNAL_OK) {
const uint8_t *es = dst_formatc("(macro) %V", x);
c->result.macrofiber = fiberp;
dstc_error(c, es);
} else {
*out = x;
}
return 1;
}
/* Compile a single value */
DstSlot dstc_value(DstFopts opts, Dst x) {
DstSlot ret;
2018-01-28 20:29:47 +00:00
DstCompiler *c = opts.compiler;
2018-07-01 15:52:15 +00:00
DstSourceMapping last_mapping = c->current_mapping;
c->recursion_guard--;
/* Guard against previous errors and unbounded recursion */
if (dstc_iserr(&opts)) return dstc_cslot(dst_wrap_nil());
if (c->recursion_guard <= 0) {
dstc_cerror(c, "recursed too deeply");
return dstc_cslot(dst_wrap_nil());
}
/* Macro expand. Also gets possible special form and
* refines source mapping cursor if possible. */
const DstSpecial *spec = NULL;
int macroi = DST_RECURSION_GUARD;
while (macroi && !dstc_iserr(&opts) && macroexpand1(c, x, &x, &spec))
macroi--;
if (macroi == 0) {
dstc_cerror(c, "recursed too deeply in macro expansion");
return dstc_cslot(dst_wrap_nil());
}
/* Special forms */
if (spec) {
const Dst *tup = dst_unwrap_tuple(x);
ret = spec->compile(opts, dst_tuple_length(tup) - 1, tup + 1);
} else {
switch (dst_type(x)) {
case DST_TUPLE:
{
DstFopts subopts = dstc_fopts_default(c);
const Dst *tup = dst_unwrap_tuple(x);
/* Empty tuple is tuple literal */
if (dst_tuple_length(tup) == 0) {
ret = dstc_cslot(x);
} else {
DstSlot head = dstc_value(subopts, tup[0]);
2018-01-28 20:29:47 +00:00
subopts.flags = DST_FUNCTION | DST_CFUNCTION;
2018-06-29 03:36:31 +00:00
ret = dstc_call(opts, dstc_toslots(c, tup + 1, dst_tuple_length(tup) - 1), head);
dstc_freeslot(c, head);
2018-01-28 20:29:47 +00:00
}
}
break;
case DST_SYMBOL:
ret = dstc_symbol(opts, dst_unwrap_symbol(x));
break;
case DST_ARRAY:
ret = dstc_array(opts, x);
break;
case DST_STRUCT:
ret = dstc_tablector(opts, x, DOP_MAKE_STRUCT);
break;
case DST_TABLE:
ret = dstc_tablector(opts, x, DOP_MAKE_TABLE);
break;
case DST_BUFFER:
ret = dstc_bufferctor(opts, x);
break;
default:
ret = dstc_cslot(x);
break;
}
}
2018-01-21 19:39:32 +00:00
if (dstc_iserr(&opts)) {
return dstc_cslot(dst_wrap_nil());
}
c->current_mapping = last_mapping;
if (opts.flags & DST_FOPTS_TAIL) {
2018-06-29 03:36:31 +00:00
ret = dstc_return(opts.compiler, ret);
}
2018-07-01 15:52:15 +00:00
if (opts.flags & DST_FOPTS_HINT) {
2018-06-29 03:36:31 +00:00
dstc_copy(opts.compiler, opts.hint, ret);
2018-01-16 04:31:39 +00:00
ret = opts.hint;
}
opts.compiler->recursion_guard++;
return ret;
}
/* Compile a funcdef */
DstFuncDef *dstc_pop_funcdef(DstCompiler *c) {
2018-07-01 15:52:15 +00:00
DstScope *scope = c->scope;
DstFuncDef *def = dst_funcdef_alloc();
2018-07-01 15:52:15 +00:00
def->slotcount = scope->ra.max + 1;
2018-07-01 15:52:15 +00:00
dst_assert(scope->flags & DST_SCOPE_FUNCTION, "expected function scope");
2018-01-21 19:39:32 +00:00
/* Copy envs */
2018-07-01 15:52:15 +00:00
def->environments_length = dst_v_count(scope->envs);
def->environments = dst_v_flatten(scope->envs);
2018-07-01 15:52:15 +00:00
def->constants_length = dst_v_count(scope->consts);
def->constants = dst_v_flatten(scope->consts);
2018-07-01 15:52:15 +00:00
def->defs_length = dst_v_count(scope->defs);
def->defs = dst_v_flatten(scope->defs);
2018-06-29 03:36:31 +00:00
/* Copy bytecode (only last chunk) */
2018-07-01 15:52:15 +00:00
def->bytecode_length = dst_v_count(c->buffer) - scope->bytecode_start;
if (def->bytecode_length) {
size_t s = sizeof(int32_t) * def->bytecode_length;
def->bytecode = malloc(s);
if (NULL == def->bytecode) {
DST_OUT_OF_MEMORY;
}
2018-07-01 15:52:15 +00:00
memcpy(def->bytecode, c->buffer + scope->bytecode_start, s);
dst_v__cnt(c->buffer) = scope->bytecode_start;
if (NULL != c->mapbuffer) {
size_t s = sizeof(DstSourceMapping) * def->bytecode_length;
def->sourcemap = malloc(s);
if (NULL == def->sourcemap) {
DST_OUT_OF_MEMORY;
}
2018-07-01 15:52:15 +00:00
memcpy(def->sourcemap, c->mapbuffer + scope->bytecode_start, s);
dst_v__cnt(c->mapbuffer) = scope->bytecode_start;
}
}
2018-06-29 03:36:31 +00:00
/* Get source from parser */
def->source = c->source;
2018-06-29 03:36:31 +00:00
def->arity = 0;
def->flags = 0;
2018-07-01 15:52:15 +00:00
if (scope->flags & DST_SCOPE_ENV) {
def->flags |= DST_FUNCDEF_FLAG_NEEDSENV;
}
/* Pop the scope */
dstc_popscope(c);
return def;
}
/* Initialize a compiler */
static void dstc_init(DstCompiler *c, DstTable *env, const uint8_t *where) {
2018-07-01 15:52:15 +00:00
c->scope = NULL;
c->buffer = NULL;
c->mapbuffer = NULL;
c->recursion_guard = DST_RECURSION_GUARD;
c->env = env;
c->source = where;
c->current_mapping.line = 0;
c->current_mapping.column = 0;
/* Init result */
c->result.error = NULL;
c->result.status = DST_COMPILE_OK;
2018-06-29 05:15:47 +00:00
c->result.funcdef = NULL;
c->result.macrofiber = NULL;
c->result.error_mapping.line = 0;
c->result.error_mapping.column = 0;
}
/* Deinitialize a compiler struct */
static void dstc_deinit(DstCompiler *c) {
dst_v_free(c->buffer);
dst_v_free(c->mapbuffer);
2018-01-18 22:25:45 +00:00
c->env = NULL;
}
/* Compile a form. */
DstCompileResult dst_compile(Dst source, DstTable *env, const uint8_t *where) {
DstCompiler c;
2018-07-01 15:52:15 +00:00
DstScope rootscope;
DstFopts fopts;
dstc_init(&c, env, where);
/* Push a function scope */
2018-07-01 15:52:15 +00:00
dstc_scope(&rootscope, &c, DST_SCOPE_FUNCTION | DST_SCOPE_TOP, "root");
/* Set initial form options */
fopts.compiler = &c;
fopts.flags = DST_FOPTS_TAIL | DST_SLOTTYPE_ANY;
fopts.hint = dstc_cslot(dst_wrap_nil());
/* Compile the value */
2018-01-18 22:25:45 +00:00
dstc_value(fopts, source);
if (c.result.status == DST_COMPILE_OK) {
2018-01-20 22:19:47 +00:00
DstFuncDef *def = dstc_pop_funcdef(&c);
2018-06-29 05:15:47 +00:00
def->name = dst_cstring("_thunk");
2018-01-20 22:19:47 +00:00
c.result.funcdef = def;
2018-06-29 05:15:47 +00:00
} else {
c.result.error_mapping = c.current_mapping;
2018-07-01 15:52:15 +00:00
dstc_popscope(&c);
}
dstc_deinit(&c);
return c.result;
}
2018-01-16 04:31:39 +00:00
/* C Function for compiling */
2018-07-01 16:44:41 +00:00
static int cfun(DstArgs args) {
2018-01-16 04:31:39 +00:00
DstCompileResult res;
DstTable *t;
2018-01-18 22:25:45 +00:00
DstTable *env;
2018-06-29 03:36:31 +00:00
DST_MINARITY(args, 2);
DST_MAXARITY(args, 3);
DST_ARG_TABLE(env, args, 1);
const uint8_t *source = NULL;
2018-06-29 03:36:31 +00:00
if (args.n == 3) {
DST_ARG_STRING(source, args, 2);
2018-06-29 03:36:31 +00:00
}
res = dst_compile(args.v[0], env, source);
2018-01-16 04:31:39 +00:00
if (res.status == DST_COMPILE_OK) {
DST_RETURN_FUNCTION(args, dst_thunk(res.funcdef));
2018-01-16 04:31:39 +00:00
} else {
t = dst_table(4);
dst_table_put(t, dst_csymbolv(":error"), dst_wrap_string(res.error));
dst_table_put(t, dst_csymbolv(":line"), dst_wrap_integer(res.error_mapping.line));
dst_table_put(t, dst_csymbolv(":column"), dst_wrap_integer(res.error_mapping.column));
if (res.macrofiber) {
dst_table_put(t, dst_csymbolv(":fiber"), dst_wrap_fiber(res.macrofiber));
}
DST_RETURN_TABLE(args, t);
2018-01-16 04:31:39 +00:00
}
}
2018-07-01 16:44:41 +00:00
static const DstReg cfuns[] = {
{"compile", cfun},
{NULL, NULL}
};
int dst_lib_compile(DstArgs args) {
DstTable *env = dst_env_arg(args);
2018-07-01 16:44:41 +00:00
dst_env_cfuns(env, cfuns);
return 0;
}