2017-11-06 03:05:47 +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 <setjmp.h>
|
|
|
|
|
|
|
|
#include <dst/dst.h>
|
2017-11-21 02:39:44 +00:00
|
|
|
#include "opcodes.h"
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Bytecode op argument types */
|
|
|
|
|
|
|
|
/* s - a slot */
|
|
|
|
/* c - a constant */
|
|
|
|
/* i - a small integer */
|
|
|
|
/* t - a type (have a simple type for non unions) */
|
|
|
|
/* l - a label */
|
|
|
|
|
|
|
|
typedef enum DstOpArgType DstOpArgType;
|
|
|
|
enum DstOpArgType {
|
|
|
|
DST_OAT_SLOT,
|
|
|
|
DST_OAT_ENVIRONMENT,
|
|
|
|
DST_OAT_CONSTANT,
|
|
|
|
DST_OAT_INTEGER,
|
|
|
|
DST_OAT_TYPE,
|
|
|
|
DST_OAT_SIMPLETYPE,
|
|
|
|
DST_OAT_LABEL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Convert a slot to to an integer for bytecode */
|
|
|
|
|
|
|
|
/* Types of instructions */
|
|
|
|
/* _0arg - op.---.--.-- (return-nil, noop, vararg arguments)
|
|
|
|
* _s - op.src.--.-- (push1)
|
|
|
|
* _l - op.XX.XX.XX (jump)
|
|
|
|
* _ss - op.dest.XX.XX (move, swap)
|
|
|
|
* _sl - op.check.XX.XX (jump-if)
|
|
|
|
* _st - op.check.TT.TT (typecheck)
|
|
|
|
* _si - op.dest.XX.XX (load-integer)
|
|
|
|
* _sss - op.dest.op1.op2 (add, subtract, arithmetic, comparison)
|
|
|
|
* _ses - op.dest.up.which (load-upvalue, save-upvalue)
|
|
|
|
* _sc - op.dest.CC.CC (load-constant, closure)
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Various types of instructions */
|
|
|
|
typedef enum DstInstructionType DstInstructionType;
|
|
|
|
enum DstInstructionType {
|
|
|
|
DIT_0, /* No args */
|
|
|
|
DIT_S, /* One slot */
|
|
|
|
DIT_L, /* One label */
|
|
|
|
DIT_SS, /* Two slots */
|
|
|
|
DIT_SL,
|
|
|
|
DIT_ST,
|
|
|
|
DIT_SI,
|
|
|
|
DIT_SU, /* Unsigned */
|
|
|
|
DIT_SSS,
|
2017-11-21 02:39:44 +00:00
|
|
|
DIT_SSI,
|
2017-11-25 04:17:04 +00:00
|
|
|
DIT_SSU,
|
2017-11-06 03:05:47 +00:00
|
|
|
DIT_SES,
|
|
|
|
DIT_SC
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Definition for an instruction in the assembler */
|
|
|
|
typedef struct DstInstructionDef DstInstructionDef;
|
|
|
|
struct DstInstructionDef {
|
|
|
|
const char *name;
|
|
|
|
DstInstructionType type;
|
2017-11-21 02:39:44 +00:00
|
|
|
DstOpCode opcode;
|
2017-11-06 03:05:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Hold all state needed during assembly */
|
|
|
|
typedef struct DstAssembler DstAssembler;
|
|
|
|
struct DstAssembler {
|
|
|
|
DstAssembler *parent;
|
|
|
|
DstFuncDef *def;
|
|
|
|
jmp_buf on_error;
|
2017-11-21 02:39:44 +00:00
|
|
|
const uint8_t *errmessage;
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *errmap;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t environments_capacity;
|
|
|
|
int32_t bytecode_count; /* Used for calculating labels */
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
DstTable labels; /* symbol -> bytecode index */
|
|
|
|
DstTable constants; /* symbol -> constant index */
|
|
|
|
DstTable slots; /* symbol -> slot index */
|
|
|
|
DstTable envs; /* symbol -> environment index */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Dst opcode descriptions in lexographic order. This
|
|
|
|
* allows a binary search over the elements to find the
|
|
|
|
* correct opcode given a name. This works in reasonable
|
|
|
|
* time and is easier to setup statically than a hash table or
|
|
|
|
* prefix tree. */
|
|
|
|
static const DstInstructionDef dst_ops[] = {
|
2017-11-21 02:39:44 +00:00
|
|
|
{"add", DIT_SSS, DOP_ADD},
|
|
|
|
{"add-immediate", DIT_SSI, DOP_ADD_IMMEDIATE},
|
|
|
|
{"add-integer", DIT_SSS, DOP_ADD_INTEGER},
|
|
|
|
{"add-real", DIT_SSS, DOP_ADD_REAL},
|
|
|
|
{"bitand", DIT_SSS, DOP_BAND},
|
|
|
|
{"bitnot", DIT_SS, DOP_BNOT},
|
|
|
|
{"bitor", DIT_SSS, DOP_BOR},
|
|
|
|
{"bitxor", DIT_SSS, DOP_BXOR},
|
|
|
|
{"call", DIT_SS, DOP_CALL},
|
|
|
|
{"closure", DIT_SC, DOP_CLOSURE},
|
|
|
|
{"compare", DIT_SSS, DOP_COMPARE},
|
|
|
|
{"divide", DIT_SSS, DOP_DIVIDE},
|
|
|
|
{"divide-immediate", DIT_SSI, DOP_DIVIDE_IMMEDIATE},
|
|
|
|
{"divide-integer", DIT_SSS, DOP_DIVIDE_INTEGER},
|
|
|
|
{"divide-real", DIT_SSS, DOP_DIVIDE_REAL},
|
|
|
|
{"equals", DIT_SSS, DOP_EQUALS},
|
|
|
|
{"error", DIT_S, DOP_ERROR},
|
2017-11-25 04:17:04 +00:00
|
|
|
{"get", DIT_SSS, DOP_GET},
|
|
|
|
{"get-index", DIT_SSU, DOP_GET_INDEX},
|
2017-11-21 02:39:44 +00:00
|
|
|
{"greater-than", DIT_SSS, DOP_GREATER_THAN},
|
|
|
|
{"jump", DIT_L, DOP_JUMP},
|
|
|
|
{"jump-if", DIT_SL, DOP_JUMP_IF},
|
2017-11-25 04:17:04 +00:00
|
|
|
{"jump-if-not", DIT_SL, DOP_JUMP_IF_NOT},
|
|
|
|
{"less-than", DIT_SSS, DOP_LESS_THAN},
|
2017-11-21 02:39:44 +00:00
|
|
|
{"load-boolean", DIT_S, DOP_LOAD_BOOLEAN},
|
|
|
|
{"load-constant", DIT_SC, DOP_LOAD_CONSTANT},
|
|
|
|
{"load-integer", DIT_SI, DOP_LOAD_INTEGER},
|
|
|
|
{"load-nil", DIT_S, DOP_LOAD_NIL},
|
|
|
|
{"load-syscall", DIT_SU, DOP_LOAD_SYSCALL},
|
|
|
|
{"load-upvalue", DIT_SES, DOP_LOAD_UPVALUE},
|
|
|
|
{"move", DIT_SS, DOP_MOVE},
|
|
|
|
{"multiply", DIT_SSS, DOP_MULTIPLY},
|
|
|
|
{"multiply-immediate", DIT_SSI, DOP_MULTIPLY_IMMEDIATE},
|
|
|
|
{"multiply-integer", DIT_SSS, DOP_MULTIPLY_INTEGER},
|
|
|
|
{"multiply-real", DIT_SSS, DOP_MULTIPLY_REAL},
|
|
|
|
{"noop", DIT_0, DOP_NOOP},
|
|
|
|
{"push", DIT_S, DOP_PUSH},
|
2017-11-25 04:17:04 +00:00
|
|
|
{"push-array", DIT_S, DOP_PUSH_ARRAY},
|
2017-11-21 02:39:44 +00:00
|
|
|
{"push2", DIT_SS, DOP_PUSH_2},
|
|
|
|
{"push3", DIT_SSS, DOP_PUSH_3},
|
2017-11-25 04:17:04 +00:00
|
|
|
{"put", DIT_SSS, DOP_PUT},
|
|
|
|
{"put-index", DIT_SSU, DOP_PUT_INDEX},
|
2017-11-21 02:39:44 +00:00
|
|
|
{"return", DIT_S, DOP_RETURN},
|
|
|
|
{"return-nil", DIT_0, DOP_RETURN_NIL},
|
|
|
|
{"set-upvalue", DIT_SES, DOP_SET_UPVALUE},
|
|
|
|
{"shift-left", DIT_SSS, DOP_SHIFT_LEFT},
|
|
|
|
{"shift-left-immediate", DIT_SSI, DOP_SHIFT_LEFT_IMMEDIATE},
|
|
|
|
{"shift-right", DIT_SSS, DOP_SHIFT_RIGHT},
|
|
|
|
{"shift-right-immediate", DIT_SSI, DOP_SHIFT_RIGHT_IMMEDIATE},
|
|
|
|
{"shift-right-unsigned", DIT_SSS, DOP_SHIFT_RIGHT_UNSIGNED},
|
|
|
|
{"shift-right-unsigned-immediate", DIT_SSS, DOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE},
|
2017-11-06 03:05:47 +00:00
|
|
|
{"subtract", DIT_SSS, 0x1F},
|
2017-11-21 02:39:44 +00:00
|
|
|
{"syscall", DIT_SU, DOP_SYSCALL},
|
|
|
|
{"tailcall", DIT_S, DOP_TAILCALL},
|
|
|
|
{"transfer", DIT_SSS, DOP_TRANSFER},
|
|
|
|
{"typecheck", DIT_ST, DOP_TYPECHECK},
|
2017-11-06 03:05:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Compare a DST string to a native 0 terminated c string. Used in the
|
|
|
|
* binary search for the instruction definition. */
|
|
|
|
static int dst_strcompare(const uint8_t *str, const char *other) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t len = dst_string_length(str);
|
|
|
|
int32_t index;
|
2017-11-06 03:05:47 +00:00
|
|
|
for (index = 0; index < len; index++) {
|
|
|
|
uint8_t c = str[index];
|
|
|
|
uint8_t k = ((const uint8_t *)other)[index];
|
|
|
|
if (c < k) return -1;
|
|
|
|
if (c > k) return 1;
|
|
|
|
if (k == '\0') break;
|
|
|
|
}
|
|
|
|
return (other[index] == '\0') ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find an instruction definition given its name */
|
|
|
|
static const DstInstructionDef *dst_findi(const uint8_t *key) {
|
|
|
|
const DstInstructionDef *low = dst_ops;
|
|
|
|
const DstInstructionDef *hi = dst_ops + (sizeof(dst_ops) / sizeof(DstInstructionDef));
|
|
|
|
while (low < hi) {
|
|
|
|
const DstInstructionDef *mid = low + ((hi - low) / 2);
|
|
|
|
int comp = dst_strcompare(key, mid->name);
|
|
|
|
if (comp < 0) {
|
|
|
|
hi = mid;
|
|
|
|
} else if (comp > 0) {
|
|
|
|
low = mid + 1;
|
|
|
|
} else {
|
|
|
|
return mid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check a dst string against a bunch of test_strings. Return the
|
|
|
|
* index of the matching test_string, or -1 if not found. */
|
2017-11-28 23:27:55 +00:00
|
|
|
static int32_t strsearch(const uint8_t *str, const char **test_strings) {
|
|
|
|
int32_t len = dst_string_length(str);
|
2017-11-06 03:05:47 +00:00
|
|
|
int index;
|
|
|
|
for (index = 0; ; index++) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i;
|
2017-11-06 03:05:47 +00:00
|
|
|
const char *testword = test_strings[index];
|
|
|
|
if (NULL == testword)
|
|
|
|
break;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (testword[i] != str[i])
|
|
|
|
goto nextword;
|
|
|
|
}
|
|
|
|
return index;
|
|
|
|
nextword:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deinitialize an Assembler. Does not deinitialize the parents. */
|
|
|
|
static void dst_asm_deinit(DstAssembler *a) {
|
|
|
|
dst_table_deinit(&a->slots);
|
|
|
|
dst_table_deinit(&a->labels);
|
|
|
|
dst_table_deinit(&a->envs);
|
|
|
|
dst_table_deinit(&a->constants);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Throw some kind of assembly error */
|
2017-12-08 20:57:02 +00:00
|
|
|
static void dst_asm_error(DstAssembler *a, const DstValue *map, const char *message) {
|
2017-11-21 02:39:44 +00:00
|
|
|
a->errmessage = dst_cstring(message);
|
2017-12-08 20:57:02 +00:00
|
|
|
a->errmap = map;
|
2017-11-06 03:05:47 +00:00
|
|
|
longjmp(a->on_error, 1);
|
|
|
|
}
|
2017-12-08 20:57:02 +00:00
|
|
|
#define dst_asm_assert(a, c, map, m) do { if (!(c)) dst_asm_error((a), (map), (m)); } while (0)
|
2017-11-06 03:05:47 +00:00
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Throw some kind of assembly error */
|
2017-12-08 20:57:02 +00:00
|
|
|
static void dst_asm_errorv(DstAssembler *a, const DstValue *map, const uint8_t *m) {
|
2017-11-21 02:39:44 +00:00
|
|
|
a->errmessage = m;
|
2017-12-08 20:57:02 +00:00
|
|
|
a->errmap = map;
|
2017-11-21 02:39:44 +00:00
|
|
|
longjmp(a->on_error, 1);
|
|
|
|
}
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Parse an argument to an assembly instruction, and return the result as an
|
|
|
|
* integer. This integer will need to be trimmed and bound checked. */
|
2017-12-08 20:57:02 +00:00
|
|
|
static int32_t doarg_1(
|
|
|
|
DstAssembler *a,
|
|
|
|
const DstValue *map,
|
|
|
|
DstOpArgType argtype,
|
|
|
|
DstValue x) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t ret = -1;
|
2017-11-06 03:05:47 +00:00
|
|
|
DstTable *c;
|
|
|
|
switch (argtype) {
|
|
|
|
case DST_OAT_SLOT:
|
|
|
|
c = &a->slots;
|
|
|
|
break;
|
|
|
|
case DST_OAT_ENVIRONMENT:
|
|
|
|
c = &a->envs;
|
|
|
|
break;
|
|
|
|
case DST_OAT_CONSTANT:
|
|
|
|
c = &a->constants;
|
|
|
|
break;
|
|
|
|
case DST_OAT_INTEGER:
|
|
|
|
c = NULL;
|
|
|
|
break;
|
|
|
|
case DST_OAT_TYPE:
|
|
|
|
case DST_OAT_SIMPLETYPE:
|
|
|
|
c = NULL;
|
|
|
|
break;
|
|
|
|
case DST_OAT_LABEL:
|
|
|
|
c = &a->labels;
|
|
|
|
break;
|
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
switch (dst_type(x)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
default:
|
2017-11-25 04:17:04 +00:00
|
|
|
goto error;
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
case DST_INTEGER:
|
2017-11-28 23:27:55 +00:00
|
|
|
ret = dst_unwrap_integer(x);
|
2017-11-25 04:17:04 +00:00
|
|
|
break;
|
2017-11-06 03:05:47 +00:00
|
|
|
case DST_TUPLE:
|
|
|
|
{
|
2017-11-28 23:27:55 +00:00
|
|
|
const DstValue *t = dst_unwrap_tuple(x);
|
2017-11-06 03:05:47 +00:00
|
|
|
if (argtype == DST_OAT_TYPE) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t i = 0;
|
2017-11-25 04:17:04 +00:00
|
|
|
ret = 0;
|
2017-11-28 23:27:55 +00:00
|
|
|
for (i = 0; i < dst_tuple_length(t); i++) {
|
2017-12-08 20:57:02 +00:00
|
|
|
ret |= doarg_1(a, map, DST_OAT_SIMPLETYPE, t[i]);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-11-25 04:17:04 +00:00
|
|
|
} else {
|
|
|
|
goto error;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DST_SYMBOL:
|
|
|
|
{
|
|
|
|
if (NULL != c) {
|
|
|
|
DstValue result = dst_table_get(c, x);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(result, DST_INTEGER)) {
|
2017-11-25 04:17:04 +00:00
|
|
|
if (argtype == DST_OAT_LABEL) {
|
2017-11-28 23:27:55 +00:00
|
|
|
ret = dst_unwrap_integer(result) - a->bytecode_count;
|
2017-11-25 04:17:04 +00:00
|
|
|
} else {
|
2017-11-28 23:27:55 +00:00
|
|
|
ret = dst_unwrap_integer(result);
|
2017-11-25 04:17:04 +00:00
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(a, map, dst_formatc("unknown name %q", x));
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
} else if (argtype == DST_OAT_TYPE || argtype == DST_OAT_SIMPLETYPE) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t index = strsearch(dst_unwrap_symbol(x), dst_type_names);
|
2017-11-06 03:05:47 +00:00
|
|
|
if (index != -1) {
|
2017-11-28 23:27:55 +00:00
|
|
|
ret = index;
|
2017-11-06 03:05:47 +00:00
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(a, map, dst_formatc("unknown type %q", x));
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-11-25 04:17:04 +00:00
|
|
|
} else {
|
|
|
|
goto error;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-11-25 04:17:04 +00:00
|
|
|
if (argtype == DST_OAT_SLOT && ret >= a->def->slotcount)
|
2017-11-28 23:27:55 +00:00
|
|
|
a->def->slotcount = (int32_t) ret + 1;
|
2017-11-25 04:17:04 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
error:
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(a, map, dst_formatc("error parsing instruction argument %v", x));
|
2017-11-06 03:05:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Parse a single argument to an instruction. Trims it as well as
|
|
|
|
* try to convert arguments to bit patterns */
|
|
|
|
static uint32_t doarg(
|
|
|
|
DstAssembler *a,
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *map,
|
2017-11-21 02:39:44 +00:00
|
|
|
DstOpArgType argtype,
|
|
|
|
int nth,
|
|
|
|
int nbytes,
|
|
|
|
int hassign,
|
|
|
|
DstValue x) {
|
2017-12-08 20:57:02 +00:00
|
|
|
int32_t arg = doarg_1(a, map, argtype, x);
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Calculate the min and max values that can be stored given
|
|
|
|
* nbytes, and whether or not the storage is signed */
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t min = (-hassign) << ((nbytes << 3) - 1);
|
|
|
|
int32_t max = ~((-1) << ((nbytes << 3) - hassign));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (arg < min)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(a, map, dst_formatc("instruction argument %v is too small, must be %d byte%s",
|
2017-11-21 02:39:44 +00:00
|
|
|
x, nbytes, nbytes > 1 ? "s" : ""));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (arg > max)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(a, map, dst_formatc("instruction argument %v is too large, must be %d byte%s",
|
2017-11-21 02:39:44 +00:00
|
|
|
x, nbytes, nbytes > 1 ? "s" : ""));
|
2017-11-28 23:27:55 +00:00
|
|
|
return ((uint32_t) arg) << (nth << 3);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Provide parsing methods for the different kinds of arguments */
|
2017-12-08 20:57:02 +00:00
|
|
|
static uint32_t read_instruction(
|
|
|
|
DstAssembler *a,
|
|
|
|
const DstValue *map,
|
|
|
|
const DstInstructionDef *idef,
|
|
|
|
const DstValue *argt) {
|
2017-11-06 03:05:47 +00:00
|
|
|
uint32_t instr = idef->opcode;
|
|
|
|
switch (idef->type) {
|
|
|
|
case DIT_0:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 1)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 0 arguments: (op)");
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_S:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 2)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 1 argument: (op, slot)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 3, 0, argt[1]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_L:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 2)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 1 argument: (op, label)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_LABEL, 1, 3, 1, argt[1]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_SS:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 3)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 2 arguments: (op, slot, slot)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_SLOT, 2, 2, 0, argt[2]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_SL:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 3)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 2 arguments: (op, slot, label)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_LABEL, 2, 2, 1, argt[2]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_ST:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 3)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 2 arguments: (op, slot, type)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_TYPE, 2, 2, 0, argt[2]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_SI:
|
|
|
|
case DIT_SU:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 3)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 2 arguments: (op, slot, integer)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_INTEGER, 2, 2, idef->type == DIT_SI, argt[2]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_SSS:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 4)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 3 arguments: (op, slot, slot, slot)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_SLOT, 2, 1, 0, argt[2]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 3), DST_OAT_SLOT, 3, 1, 0, argt[3]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
case DIT_SSI:
|
2017-11-25 04:17:04 +00:00
|
|
|
case DIT_SSU:
|
2017-11-21 02:39:44 +00:00
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 4)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 3 arguments: (op, slot, slot, integer)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_SLOT, 2, 1, 0, argt[2]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 3), DST_OAT_INTEGER, 3, 1, idef->type == DIT_SSI, argt[3]);
|
2017-11-21 02:39:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-11-06 03:05:47 +00:00
|
|
|
case DIT_SES:
|
|
|
|
{
|
|
|
|
DstAssembler *b = a;
|
|
|
|
uint32_t env;
|
|
|
|
if (dst_tuple_length(argt) != 4)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 3 arguments: (op, slot, environment, envslot)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
env = doarg(a, dst_parse_submap_index(map, 2), DST_OAT_ENVIRONMENT, 0, 1, 0, argt[2]);
|
2017-11-06 03:05:47 +00:00
|
|
|
instr |= env << 16;
|
|
|
|
for (env += 1; env > 0; env--) {
|
|
|
|
b = b->parent;
|
|
|
|
if (NULL == b)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, dst_parse_submap_index(map, 2), "invalid environment index");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-12-08 20:57:02 +00:00
|
|
|
instr |= doarg(b, dst_parse_submap_index(map, 3), DST_OAT_SLOT, 3, 1, 0, argt[3]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIT_SC:
|
|
|
|
{
|
|
|
|
if (dst_tuple_length(argt) != 3)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(a, map, "expected 2 arguments: (op, slot, constant)");
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 1), DST_OAT_SLOT, 1, 1, 0, argt[1]);
|
|
|
|
instr |= doarg(a, dst_parse_submap_index(map, 2), DST_OAT_CONSTANT, 2, 2, 0, argt[2]);
|
2017-11-06 03:05:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return instr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a closure environment to the assembler. Sub funcdefs may need
|
|
|
|
* to reference outer function environments, and may change the outer environment.
|
|
|
|
* Returns the index of the environment in the assembler's environments, or -1
|
|
|
|
* if not found. */
|
2017-11-28 23:27:55 +00:00
|
|
|
static int32_t dst_asm_addenv(DstAssembler *a, DstValue envname) {
|
2017-11-06 03:05:47 +00:00
|
|
|
DstValue check;
|
|
|
|
DstFuncDef *def = a->def;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t oldlen;
|
2017-11-06 03:05:47 +00:00
|
|
|
int64_t res;
|
|
|
|
/* Check for memoized value */
|
|
|
|
check = dst_table_get(&a->envs, envname);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (!dst_checktype(check, DST_NIL)) {
|
|
|
|
return dst_unwrap_integer(check);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
if (NULL == a->parent) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
res = dst_asm_addenv(a->parent, envname);
|
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
oldlen = def->environments_length;
|
|
|
|
dst_table_put(&a->envs, envname, dst_wrap_integer(def->environments_length));
|
|
|
|
if (oldlen >= a->environments_capacity) {
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t newcap = 2 + 2 * oldlen;
|
|
|
|
def->environments = realloc(def->environments, newcap * sizeof(int32_t));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (NULL == def->environments) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
a->environments_capacity = newcap;
|
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
def->environments[def->environments_length++] = (int32_t) res;
|
|
|
|
return (int32_t) oldlen;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Helper to assembly. Return the assembly result */
|
|
|
|
static DstAssembleResult dst_asm1(DstAssembler *parent, DstAssembleOptions opts) {
|
|
|
|
DstAssembleResult result;
|
2017-11-06 03:05:47 +00:00
|
|
|
DstAssembler a;
|
2017-11-28 23:27:55 +00:00
|
|
|
const DstValue *st = dst_unwrap_struct(opts.source);
|
2017-11-06 03:05:47 +00:00
|
|
|
DstFuncDef *def;
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t count, i;
|
2017-11-06 03:05:47 +00:00
|
|
|
const DstValue *arr;
|
|
|
|
DstValue x;
|
|
|
|
|
|
|
|
/* Initialize funcdef */
|
|
|
|
def = dst_alloc(DST_MEMORY_FUNCDEF, sizeof(DstFuncDef));
|
|
|
|
def->environments = NULL;
|
|
|
|
def->constants = NULL;
|
|
|
|
def->bytecode = NULL;
|
|
|
|
def->flags = 0;
|
|
|
|
def->slotcount = 0;
|
|
|
|
def->arity = 0;
|
|
|
|
def->constants_length = 0;
|
|
|
|
def->bytecode_length = 0;
|
2017-11-21 02:39:44 +00:00
|
|
|
def->environments_length = 1;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Initialize Assembler */
|
|
|
|
a.def = def;
|
|
|
|
a.parent = parent;
|
|
|
|
a.errmessage = NULL;
|
|
|
|
a.environments_capacity = 0;
|
|
|
|
a.bytecode_count = 0;
|
2017-12-08 20:57:02 +00:00
|
|
|
a.errmap = NULL;
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_table_init(&a.labels, 10);
|
|
|
|
dst_table_init(&a.constants, 10);
|
|
|
|
dst_table_init(&a.slots, 10);
|
|
|
|
dst_table_init(&a.envs, 10);
|
|
|
|
|
2017-12-08 20:57:02 +00:00
|
|
|
/* Initialize result */
|
|
|
|
result.error_start = -1;
|
|
|
|
result.error_end = -1;
|
|
|
|
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Set error jump */
|
|
|
|
if (setjmp(a.on_error)) {
|
|
|
|
if (NULL != a.parent) {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_deinit(&a);
|
2017-11-06 03:05:47 +00:00
|
|
|
longjmp(a.parent->on_error, 1);
|
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
result.result.error = a.errmessage;
|
|
|
|
result.status = DST_ASSEMBLE_ERROR;
|
2017-12-08 20:57:02 +00:00
|
|
|
if (a.errmap != NULL) {
|
|
|
|
result.error_start = dst_unwrap_integer(a.errmap[0]);
|
|
|
|
result.error_end = dst_unwrap_integer(a.errmap[1]);
|
|
|
|
}
|
|
|
|
dst_asm_deinit(&a);
|
2017-11-21 02:39:44 +00:00
|
|
|
return result;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_assert(&a, dst_checktype(opts.source, DST_STRUCT), opts.sourcemap, "expected struct for assembly source");
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Set function arity */
|
2017-11-27 19:03:34 +00:00
|
|
|
x = dst_struct_get(st, dst_csymbolv("arity"));
|
2017-11-28 23:27:55 +00:00
|
|
|
def->arity = dst_checktype(x, DST_INTEGER) ? dst_unwrap_integer(x) : 0;
|
2017-11-06 03:05:47 +00:00
|
|
|
|
|
|
|
/* Create slot aliases */
|
2017-11-27 19:03:34 +00:00
|
|
|
x = dst_struct_get(st, dst_csymbolv("slots"));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_seq_view(x, &arr, &count)) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *slotmap =
|
|
|
|
dst_parse_submap_value(opts.sourcemap, dst_csymbolv("slots"));
|
2017-11-06 03:05:47 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *imap = dst_parse_submap_index(slotmap, i);
|
2017-11-06 03:05:47 +00:00
|
|
|
DstValue v = arr[i];
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(v, DST_TUPLE)) {
|
|
|
|
const DstValue *t = dst_unwrap_tuple(v);
|
|
|
|
int32_t j;
|
|
|
|
for (j = 0; j < dst_tuple_length(t); j++) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *tjmap = dst_parse_submap_index(imap, j);
|
2017-11-28 23:27:55 +00:00
|
|
|
if (!dst_checktype(t[j], DST_SYMBOL))
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(&a, tjmap, "slot names must be symbols");
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_table_put(&a.slots, t[j], dst_wrap_integer(i));
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(v, DST_SYMBOL)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_table_put(&a.slots, v, dst_wrap_integer(i));
|
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(&a, imap, "slot names must be symbols or tuple of symbols");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create environment aliases */
|
2017-11-27 19:03:34 +00:00
|
|
|
x = dst_struct_get(st, dst_csymbolv("environments"));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_seq_view(x, &arr, &count)) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *emap =
|
|
|
|
dst_parse_submap_value(opts.sourcemap, dst_csymbolv("environments"));
|
2017-11-06 03:05:47 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *imap = dst_parse_submap_index(emap, i);
|
|
|
|
dst_asm_assert(&a, dst_checktype(arr[i], DST_SYMBOL), imap, "environment must be a symbol");
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_asm_addenv(&a, arr[i]) < 0) {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(&a, imap, "environment not found");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse constants */
|
2017-11-27 19:03:34 +00:00
|
|
|
x = dst_struct_get(st, dst_csymbolv("constants"));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_seq_view(x, &arr, &count)) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *cmap =
|
|
|
|
dst_parse_submap_value(opts.sourcemap, dst_csymbolv("constants"));
|
2017-11-06 03:05:47 +00:00
|
|
|
def->constants_length = count;
|
|
|
|
def->constants = malloc(sizeof(DstValue) * count);
|
|
|
|
if (NULL == def->constants) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
for (i = 0; i < count; i++) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *imap = dst_parse_submap_index(cmap, i);
|
2017-11-06 03:05:47 +00:00
|
|
|
DstValue ct = arr[i];
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(ct, DST_TUPLE) &&
|
|
|
|
dst_tuple_length(dst_unwrap_tuple(ct)) > 1 &&
|
|
|
|
dst_checktype(dst_unwrap_tuple(ct)[0], DST_SYMBOL)) {
|
|
|
|
const DstValue *t = dst_unwrap_tuple(ct);
|
|
|
|
int32_t tcount = dst_tuple_length(t);
|
|
|
|
const uint8_t *macro = dst_unwrap_symbol(t[0]);
|
2017-11-06 03:05:47 +00:00
|
|
|
if (0 == dst_strcompare(macro, "quote")) {
|
2017-11-28 23:27:55 +00:00
|
|
|
def->constants[i] = t[1];
|
2017-11-06 03:05:47 +00:00
|
|
|
} else if (tcount == 3 &&
|
2017-11-28 23:27:55 +00:00
|
|
|
dst_checktype(t[1], DST_SYMBOL) &&
|
2017-11-06 03:05:47 +00:00
|
|
|
0 == dst_strcompare(macro, "def")) {
|
2017-11-28 23:27:55 +00:00
|
|
|
def->constants[i] = t[2];
|
|
|
|
dst_table_put(&a.constants, t[1], dst_wrap_integer(i));
|
2017-11-06 03:05:47 +00:00
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(&a, imap, dst_formatc("could not parse constant \"%v\"", ct));
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
/* Todo - parse nested funcdefs */
|
|
|
|
} else {
|
|
|
|
def->constants[i] = ct;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
def->constants = NULL;
|
|
|
|
def->constants_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse bytecode and labels */
|
2017-11-27 19:03:34 +00:00
|
|
|
x = dst_struct_get(st, dst_csymbolv("bytecode"));
|
2017-11-06 03:05:47 +00:00
|
|
|
if (dst_seq_view(x, &arr, &count)) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *bmap =
|
|
|
|
dst_parse_submap_value(opts.sourcemap, dst_csymbolv("bytecode"));
|
2017-11-06 03:05:47 +00:00
|
|
|
/* Do labels and find length */
|
2017-11-28 23:27:55 +00:00
|
|
|
int32_t blength = 0;
|
2017-11-06 03:05:47 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *imap = dst_parse_submap_index(bmap, i);
|
2017-11-06 03:05:47 +00:00
|
|
|
DstValue instr = arr[i];
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(instr, DST_SYMBOL)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
dst_table_put(&a.labels, instr, dst_wrap_integer(blength));
|
2017-11-28 23:27:55 +00:00
|
|
|
} else if (dst_checktype(instr, DST_TUPLE)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
blength++;
|
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(&a, imap, "expected assembly instruction");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Allocate bytecode array */
|
|
|
|
def->bytecode_length = blength;
|
2017-11-28 23:27:55 +00:00
|
|
|
def->bytecode = malloc(sizeof(int32_t) * blength);
|
2017-11-06 03:05:47 +00:00
|
|
|
if (NULL == def->bytecode) {
|
|
|
|
DST_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
/* Do bytecode */
|
|
|
|
for (i = 0; i < count; ++i) {
|
2017-12-08 20:57:02 +00:00
|
|
|
const DstValue *imap = dst_parse_submap_index(bmap, i);
|
2017-11-06 03:05:47 +00:00
|
|
|
DstValue instr = arr[i];
|
2017-11-28 23:27:55 +00:00
|
|
|
if (dst_checktype(instr, DST_SYMBOL)) {
|
2017-11-06 03:05:47 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
uint32_t op;
|
|
|
|
const DstInstructionDef *idef;
|
2017-11-28 23:27:55 +00:00
|
|
|
const DstValue *t;
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_assert(&a, dst_checktype(instr, DST_TUPLE), imap, "expected tuple");
|
2017-11-28 23:27:55 +00:00
|
|
|
t = dst_unwrap_tuple(instr);
|
|
|
|
if (dst_tuple_length(t) == 0) {
|
2017-11-06 03:05:47 +00:00
|
|
|
op = 0;
|
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_assert(&a, dst_checktype(t[0], DST_SYMBOL), imap,
|
2017-11-06 03:05:47 +00:00
|
|
|
"expected symbol in assembly instruction");
|
2017-11-28 23:27:55 +00:00
|
|
|
idef = dst_findi(dst_unwrap_symbol(t[0]));
|
2017-11-21 02:39:44 +00:00
|
|
|
if (NULL == idef)
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_errorv(&a, imap, dst_formatc("unknown instruction %v", instr));
|
|
|
|
op = read_instruction(&a, imap, idef, t);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
def->bytecode[a.bytecode_count++] = op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-08 20:57:02 +00:00
|
|
|
dst_asm_error(&a, opts.sourcemap, "bytecode expected");
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish everything and return funcdef */
|
|
|
|
dst_asm_deinit(&a);
|
|
|
|
def->environments =
|
2017-11-28 23:27:55 +00:00
|
|
|
realloc(def->environments, def->environments_length * sizeof(int32_t));
|
2017-11-21 02:39:44 +00:00
|
|
|
result.result.def = def;
|
|
|
|
result.status = DST_ASSEMBLE_OK;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assemble a function */
|
|
|
|
DstAssembleResult dst_asm(DstAssembleOptions opts) {
|
|
|
|
return dst_asm1(NULL, opts);
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 02:39:44 +00:00
|
|
|
/* Build a function from the result */
|
|
|
|
DstFunction *dst_asm_func(DstAssembleResult result) {
|
|
|
|
if (result.status != DST_ASSEMBLE_OK) {
|
|
|
|
return NULL;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|
2017-11-21 02:39:44 +00:00
|
|
|
DstFunction *func = dst_alloc(DST_MEMORY_FUNCTION, sizeof(DstFunction));
|
|
|
|
func->def = result.result.def;
|
|
|
|
func->envs = NULL;
|
|
|
|
return func;
|
2017-11-06 03:05:47 +00:00
|
|
|
}
|