1
0
mirror of https://github.com/janet-lang/janet synced 2025-04-05 14:56:55 +00:00

Add quick asm for adding apply and error to the stl.

This commit is contained in:
bakpakin 2018-01-21 16:41:15 -05:00
parent 911b0b15e8
commit 42a88de9e7
5 changed files with 37 additions and 25 deletions

View File

@ -243,3 +243,17 @@ DstFunction *dst_function(DstFuncDef *def, DstFunction *parent) {
}
return func;
}
/* Utility for inline assembly */
DstFunction *dst_quick_asm(int32_t arity, int varargs, int32_t slots, const uint32_t *bytecode, size_t bytecode_size) {
DstFuncDef *def = dst_funcdef_alloc();
def->arity = arity;
def->flags = varargs ? DST_FUNCDEF_FLAG_VARARG : 0;
def->slotcount = slots;
def->bytecode = malloc(bytecode_size);
if (!def->bytecode) {
DST_OUT_OF_MEMORY;
}
memcpy(def->bytecode, bytecode, bytecode_size);
return dst_function(def, NULL);
}

View File

@ -23,6 +23,7 @@
#include <dst/dst.h>
#include <dst/dststl.h>
#include <dst/dstasm.h>
#include <dst/dstopcodes.h>
int dst_stl_exit(DstArgs args) {
int32_t exitcode = 0;
@ -274,12 +275,24 @@ static const DstReg cfuns[] = {
};
DstTable *dst_stl_env() {
static uint32_t error_asm[] = {
DOP_ERROR
};
static uint32_t apply_asm[] = {
DOP_PUSH_ARRAY | (1 << 8),
DOP_TAILCALL
};
DstTable *env = dst_table(0);
Dst ret = dst_wrap_table(env);
/* Load main functions */
dst_env_cfuns(env, cfuns);
dst_env_def(env, "error", dst_wrap_function(dst_quick_asm(1, 0, 1, error_asm, sizeof(uint32_t))));
dst_env_def(env, "apply", dst_wrap_function(dst_quick_asm(2, 0, 2, apply_asm, 2 * sizeof(uint32_t))));
/* Allow references to the environment */
dst_env_def(env, "_env", ret);

View File

@ -165,6 +165,7 @@ int dst_gcunrootall(Dst root);
DstFuncDef *dst_funcdef_alloc();
DstFunction *dst_function(DstFuncDef *def, DstFunction *parent);
int dst_verify(DstFuncDef *def);
DstFunction *dst_quick_asm(int32_t arity, int varargs, int32_t slots, const uint32_t *bytecode, size_t bytecode_size);
/* Misc */
int dst_equals(Dst x, Dst y);

View File

@ -1,9 +0,0 @@
(def outerfun (fn [x y]
(def c (do
#(+ 1 2 3 4)
(def someval (+ x y))
(def ctemp (if x (fn [] someval) (fn [] y)))
ctemp
))
#(+ 1 2 3 4 5 6 7 8 9 10)
c))

View File

@ -141,17 +141,18 @@
(varset! count (+ 1 count)))
(assert (= accum 65536) "loop globally")
(assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter")
(assert (= (struct 1 2 3 4 5 6 7 8) (struct 7 8 5 6 3 4 1 2)) "struct order does not matter 1")
(assert (= (struct
:apple 1
6 :bork
'(1 2 3) 5)
(struct
6 :bork
'(1 2 3) 5
:apple 1)) "struct order does not matter 2")
# Fiber tests
(def error (asm '{
arity 1
bytecode [
(error 0)
]
}))
(def afiber (fiber (fn [x]
(error (string "hello, " x)))))
@ -171,14 +172,6 @@
# Var arg tests
(def apply (asm '{
arity 2
bytecode [
(push-array 1)
(tailcall 0)
]
}))
(def vargf (fn [more] (apply + more)))
(assert (= 0 (vargf [])) "var arg no arguments")