2018-01-24 22:59:00 +00:00
|
|
|
/*
|
2023-01-07 21:03:35 +00:00
|
|
|
* Copyright (c) 2023 Calvin Rose
|
2018-01-24 22:59:00 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2018-01-24 22:59:00 +00:00
|
|
|
#include "compile.h"
|
2018-07-01 15:52:15 +00:00
|
|
|
#include "emit.h"
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "vector.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-01-24 22:59:00 +00:00
|
|
|
|
2020-02-08 19:03:03 +00:00
|
|
|
static int arity1or2(JanetFopts opts, JanetSlot *args) {
|
|
|
|
(void) opts;
|
|
|
|
int32_t arity = janet_v_count(args);
|
|
|
|
return arity == 1 || arity == 2;
|
|
|
|
}
|
2020-06-28 20:03:01 +00:00
|
|
|
static int arity2or3(JanetFopts opts, JanetSlot *args) {
|
|
|
|
(void) opts;
|
|
|
|
int32_t arity = janet_v_count(args);
|
|
|
|
return arity == 2 || arity == 3;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static int fixarity1(JanetFopts opts, JanetSlot *args) {
|
2018-07-01 23:35:45 +00:00
|
|
|
(void) opts;
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_v_count(args) == 1;
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2019-06-15 16:21:08 +00:00
|
|
|
static int maxarity1(JanetFopts opts, JanetSlot *args) {
|
|
|
|
(void) opts;
|
|
|
|
return janet_v_count(args) <= 1;
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static int minarity2(JanetFopts opts, JanetSlot *args) {
|
2018-08-26 16:53:39 +00:00
|
|
|
(void) opts;
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_v_count(args) >= 2;
|
2018-08-26 16:53:39 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static int fixarity2(JanetFopts opts, JanetSlot *args) {
|
2018-07-01 23:35:45 +00:00
|
|
|
(void) opts;
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_v_count(args) == 2;
|
2018-11-28 21:38:48 +00:00
|
|
|
}
|
|
|
|
static int fixarity3(JanetFopts opts, JanetSlot *args) {
|
2018-07-11 00:01:39 +00:00
|
|
|
(void) opts;
|
2018-09-06 02:18:42 +00:00
|
|
|
return janet_v_count(args) == 3;
|
2018-01-24 22:59:00 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Generic handling for $A = op $B */
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot genericSS(JanetFopts opts, int op, JanetSlot s) {
|
|
|
|
JanetSlot target = janetc_gettarget(opts);
|
|
|
|
janetc_emit_ss(opts.compiler, op, target, s, 1);
|
2018-07-01 23:35:45 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Generic handling for $A = $B op I */
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot genericSSI(JanetFopts opts, int op, JanetSlot s, int32_t imm) {
|
|
|
|
JanetSlot target = janetc_gettarget(opts);
|
|
|
|
janetc_emit_ssi(opts.compiler, op, target, s, imm, 1);
|
2018-07-01 23:35:45 +00:00
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:03:03 +00:00
|
|
|
/* Emit an insruction that implements a form by itself. */
|
|
|
|
static JanetSlot opfunction(
|
2020-02-22 02:21:17 +00:00
|
|
|
JanetFopts opts,
|
|
|
|
JanetSlot *args,
|
|
|
|
int op,
|
|
|
|
Janet defaultArg2) {
|
2020-02-08 19:03:03 +00:00
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
int32_t len;
|
|
|
|
len = janet_v_count(args);
|
|
|
|
JanetSlot t;
|
|
|
|
if (len == 1) {
|
|
|
|
t = janetc_gettarget(opts);
|
|
|
|
janetc_emit_sss(c, op, t, args[0], janetc_cslot(defaultArg2), 1);
|
|
|
|
return t;
|
2020-02-22 02:21:17 +00:00
|
|
|
} else {
|
|
|
|
/* len == 2 */
|
2020-02-08 19:03:03 +00:00
|
|
|
t = janetc_gettarget(opts);
|
|
|
|
janetc_emit_sss(c, op, t, args[0], args[1], 1);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2020-06-28 23:16:57 +00:00
|
|
|
/* Check if a value can be coerced to an immediate value */
|
|
|
|
static int can_be_imm(Janet x, int8_t *out) {
|
|
|
|
if (!janet_checkint(x)) return 0;
|
|
|
|
int32_t integer = janet_unwrap_integer(x);
|
|
|
|
if (integer > 127 || integer < -127) return 0;
|
|
|
|
*out = (int8_t) integer;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if a slot can be coerced to an immediate value */
|
|
|
|
static int can_slot_be_imm(JanetSlot s, int8_t *out) {
|
|
|
|
if (!(s.flags & JANET_SLOT_CONSTANT)) return 0;
|
|
|
|
return can_be_imm(s.constant, out);
|
|
|
|
}
|
|
|
|
|
2018-07-02 23:53:35 +00:00
|
|
|
/* Emit a series of instructions instead of a function call to a math op */
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot opreduce(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetFopts opts,
|
|
|
|
JanetSlot *args,
|
|
|
|
int op,
|
2020-06-28 23:16:57 +00:00
|
|
|
int opim,
|
2019-02-20 01:51:34 +00:00
|
|
|
Janet nullary) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetCompiler *c = opts.compiler;
|
2018-07-02 23:53:35 +00:00
|
|
|
int32_t i, len;
|
2020-06-28 23:16:57 +00:00
|
|
|
int8_t imm = 0;
|
|
|
|
int neg = opim < 0;
|
|
|
|
if (opim < 0) opim = -opim;
|
2018-09-06 02:18:42 +00:00
|
|
|
len = janet_v_count(args);
|
|
|
|
JanetSlot t;
|
2018-07-02 23:53:35 +00:00
|
|
|
if (len == 0) {
|
2018-09-06 02:18:42 +00:00
|
|
|
return janetc_cslot(nullary);
|
2018-07-02 23:53:35 +00:00
|
|
|
} else if (len == 1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
t = janetc_gettarget(opts);
|
2020-06-28 23:16:57 +00:00
|
|
|
/* Special case subtract to be times -1 */
|
|
|
|
if (op == JOP_SUBTRACT) {
|
|
|
|
janetc_emit_ssi(c, JOP_MULTIPLY_IMMEDIATE, t, args[0], -1, 1);
|
|
|
|
} else {
|
|
|
|
janetc_emit_sss(c, op, t, janetc_cslot(nullary), args[0], 1);
|
|
|
|
}
|
2018-07-04 03:07:35 +00:00
|
|
|
return t;
|
2018-07-02 23:53:35 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
t = janetc_gettarget(opts);
|
2020-06-28 23:16:57 +00:00
|
|
|
if (opim && can_slot_be_imm(args[1], &imm)) {
|
|
|
|
janetc_emit_ssi(c, opim, t, args[0], neg ? -imm : imm, 1);
|
|
|
|
} else {
|
|
|
|
janetc_emit_sss(c, op, t, args[0], args[1], 1);
|
|
|
|
}
|
|
|
|
for (i = 2; i < len; i++) {
|
|
|
|
if (opim && can_slot_be_imm(args[i], &imm)) {
|
|
|
|
janetc_emit_ssi(c, opim, t, t, neg ? -imm : imm, 1);
|
|
|
|
} else {
|
|
|
|
janetc_emit_sss(c, op, t, t, args[i], 1);
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 23:53:35 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-07-02 04:12:36 +00:00
|
|
|
/* Function optimizers */
|
2018-07-01 23:35:45 +00:00
|
|
|
|
2019-06-24 16:44:13 +00:00
|
|
|
static JanetSlot do_propagate(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_PROPAGATE, 0, janet_wrap_nil());
|
2019-06-24 16:44:13 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_error(JanetFopts opts, JanetSlot *args) {
|
|
|
|
janetc_emit_s(opts.compiler, JOP_ERROR, args[0], 0);
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_debug(JanetFopts opts, JanetSlot *args) {
|
2018-07-01 23:35:45 +00:00
|
|
|
(void)args;
|
2019-11-24 23:45:53 +00:00
|
|
|
int32_t len = janet_v_count(args);
|
|
|
|
JanetSlot t = janetc_gettarget(opts);
|
|
|
|
janetc_emit_ssu(opts.compiler, JOP_SIGNAL, t,
|
2019-11-26 00:14:34 +00:00
|
|
|
(len == 1) ? args[0] : janetc_cslot(janet_wrap_nil()),
|
|
|
|
JANET_SIGNAL_DEBUG,
|
|
|
|
1);
|
2019-11-24 23:45:53 +00:00
|
|
|
return t;
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2019-11-08 23:35:27 +00:00
|
|
|
static JanetSlot do_in(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_IN, 0, janet_wrap_nil());
|
2019-12-03 03:15:08 +00:00
|
|
|
}
|
|
|
|
static JanetSlot do_get(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 20:03:01 +00:00
|
|
|
if (janet_v_count(args) == 3) {
|
|
|
|
JanetCompiler *c = opts.compiler;
|
|
|
|
JanetSlot t = janetc_gettarget(opts);
|
2020-06-28 20:52:59 +00:00
|
|
|
int target_is_default = janetc_sequal(t, args[2]);
|
|
|
|
JanetSlot dflt_slot = args[2];
|
|
|
|
if (target_is_default) {
|
|
|
|
dflt_slot = janetc_farslot(c);
|
|
|
|
janetc_copy(c, dflt_slot, t);
|
|
|
|
}
|
2020-06-28 20:03:01 +00:00
|
|
|
janetc_emit_sss(c, JOP_GET, t, args[0], args[1], 1);
|
|
|
|
int32_t label = janetc_emit_si(c, JOP_JUMP_IF_NOT_NIL, t, 0, 0);
|
2020-06-28 20:52:59 +00:00
|
|
|
janetc_copy(c, t, dflt_slot);
|
|
|
|
if (target_is_default) janetc_freeslot(c, dflt_slot);
|
2020-06-28 20:03:01 +00:00
|
|
|
int32_t current = janet_v_count(c->buffer);
|
|
|
|
c->buffer[label] |= (current - label) << 16;
|
|
|
|
return t;
|
|
|
|
} else {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_GET, 0, janet_wrap_nil());
|
2020-06-28 20:03:01 +00:00
|
|
|
}
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2020-01-18 23:55:07 +00:00
|
|
|
static JanetSlot do_next(JanetFopts opts, JanetSlot *args) {
|
2020-02-08 19:03:03 +00:00
|
|
|
return opfunction(opts, args, JOP_NEXT, janet_wrap_nil());
|
2020-01-18 23:55:07 +00:00
|
|
|
}
|
2020-01-24 00:54:30 +00:00
|
|
|
static JanetSlot do_modulo(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_MODULO, 0, janet_wrap_nil());
|
2020-01-24 00:54:30 +00:00
|
|
|
}
|
|
|
|
static JanetSlot do_remainder(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_REMAINDER, 0, janet_wrap_nil());
|
2020-01-24 00:54:30 +00:00
|
|
|
}
|
2020-06-24 21:00:00 +00:00
|
|
|
static JanetSlot do_cmp(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_COMPARE, 0, janet_wrap_nil());
|
2020-06-24 21:00:00 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_put(JanetFopts opts, JanetSlot *args) {
|
2019-01-29 01:30:45 +00:00
|
|
|
if (opts.flags & JANET_FOPTS_DROP) {
|
|
|
|
janetc_emit_sss(opts.compiler, JOP_PUT, args[0], args[1], args[2], 0);
|
|
|
|
return janetc_cslot(janet_wrap_nil());
|
|
|
|
} else {
|
|
|
|
JanetSlot t = janetc_gettarget(opts);
|
|
|
|
janetc_copy(opts.compiler, t, args[0]);
|
|
|
|
janetc_emit_sss(opts.compiler, JOP_PUT, t, args[1], args[2], 0);
|
|
|
|
return t;
|
|
|
|
}
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_length(JanetFopts opts, JanetSlot *args) {
|
|
|
|
return genericSS(opts, JOP_LENGTH, args[0]);
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_yield(JanetFopts opts, JanetSlot *args) {
|
2019-06-15 16:21:08 +00:00
|
|
|
if (janet_v_count(args) == 0) {
|
|
|
|
return genericSSI(opts, JOP_SIGNAL, janetc_cslot(janet_wrap_nil()), 3);
|
|
|
|
} else {
|
|
|
|
return genericSSI(opts, JOP_SIGNAL, args[0], 3);
|
|
|
|
}
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_resume(JanetFopts opts, JanetSlot *args) {
|
2020-02-08 19:03:03 +00:00
|
|
|
return opfunction(opts, args, JOP_RESUME, janet_wrap_nil());
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
2020-08-22 20:35:37 +00:00
|
|
|
static JanetSlot do_cancel(JanetFopts opts, JanetSlot *args) {
|
|
|
|
return opfunction(opts, args, JOP_CANCEL, janet_wrap_nil());
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_apply(JanetFopts opts, JanetSlot *args) {
|
2018-07-01 23:35:45 +00:00
|
|
|
/* Push phase */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetCompiler *c = opts.compiler;
|
2018-08-26 16:53:39 +00:00
|
|
|
int32_t i;
|
2018-09-06 02:18:42 +00:00
|
|
|
for (i = 1; i < janet_v_count(args) - 3; i += 3)
|
2019-02-20 01:51:34 +00:00
|
|
|
janetc_emit_sss(c, JOP_PUSH_3, args[i], args[i + 1], args[i + 2], 0);
|
2018-09-06 02:18:42 +00:00
|
|
|
if (i == janet_v_count(args) - 3)
|
2019-02-20 01:51:34 +00:00
|
|
|
janetc_emit_ss(c, JOP_PUSH_2, args[i], args[i + 1], 0);
|
2018-09-06 02:18:42 +00:00
|
|
|
else if (i == janet_v_count(args) - 2)
|
|
|
|
janetc_emit_s(c, JOP_PUSH, args[i], 0);
|
2018-08-26 16:53:39 +00:00
|
|
|
/* Push array phase */
|
2018-09-06 02:18:42 +00:00
|
|
|
janetc_emit_s(c, JOP_PUSH_ARRAY, janet_v_last(args), 0);
|
2018-07-01 23:35:45 +00:00
|
|
|
/* Call phase */
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot target;
|
|
|
|
if (opts.flags & JANET_FOPTS_TAIL) {
|
|
|
|
janetc_emit_s(c, JOP_TAILCALL, args[0], 0);
|
|
|
|
target = janetc_cslot(janet_wrap_nil());
|
|
|
|
target.flags |= JANET_SLOT_RETURNED;
|
2018-07-01 23:35:45 +00:00
|
|
|
} else {
|
2018-09-06 02:18:42 +00:00
|
|
|
target = janetc_gettarget(opts);
|
|
|
|
janetc_emit_ss(c, JOP_CALL, target, args[0], 1);
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
2018-07-02 23:53:35 +00:00
|
|
|
|
2019-01-06 08:23:03 +00:00
|
|
|
/* Variadic operators specialization */
|
2018-07-02 23:53:35 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_add(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_ADD, JOP_ADD_IMMEDIATE, janet_wrap_integer(0));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_sub(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_SUBTRACT, -JOP_ADD_IMMEDIATE, janet_wrap_integer(0));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_mul(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_MULTIPLY, JOP_MULTIPLY_IMMEDIATE, janet_wrap_integer(1));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_div(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_DIVIDE, JOP_DIVIDE_IMMEDIATE, janet_wrap_integer(1));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_band(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_BAND, 0, janet_wrap_integer(-1));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_bor(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_BOR, 0, janet_wrap_integer(0));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_bxor(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_BXOR, 0, janet_wrap_integer(0));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_lshift(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_SHIFT_LEFT, JOP_SHIFT_LEFT_IMMEDIATE, janet_wrap_integer(1));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_rshift(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_SHIFT_RIGHT, JOP_SHIFT_RIGHT_IMMEDIATE, janet_wrap_integer(1));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_rshiftu(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return opreduce(opts, args, JOP_SHIFT_RIGHT_UNSIGNED, JOP_SHIFT_RIGHT_UNSIGNED_IMMEDIATE, janet_wrap_integer(1));
|
2018-07-02 04:12:36 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_bnot(JanetFopts opts, JanetSlot *args) {
|
|
|
|
return genericSS(opts, JOP_BNOT, args[0]);
|
2018-07-09 03:10:02 +00:00
|
|
|
}
|
2018-07-01 23:35:45 +00:00
|
|
|
|
2018-07-10 01:24:22 +00:00
|
|
|
/* Specialization for comparators */
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot compreduce(
|
2019-02-20 01:51:34 +00:00
|
|
|
JanetFopts opts,
|
|
|
|
JanetSlot *args,
|
|
|
|
int op,
|
2020-06-28 23:16:57 +00:00
|
|
|
int opim,
|
2019-02-20 01:51:34 +00:00
|
|
|
int invert) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetCompiler *c = opts.compiler;
|
2018-07-10 01:24:22 +00:00
|
|
|
int32_t i, len;
|
2020-06-28 23:16:57 +00:00
|
|
|
int8_t imm = 0;
|
2018-09-06 02:18:42 +00:00
|
|
|
len = janet_v_count(args);
|
2018-07-10 01:24:22 +00:00
|
|
|
int32_t *labels = NULL;
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetSlot t;
|
2018-07-10 01:24:22 +00:00
|
|
|
if (len < 2) {
|
|
|
|
return invert
|
2019-02-20 01:51:34 +00:00
|
|
|
? janetc_cslot(janet_wrap_false())
|
|
|
|
: janetc_cslot(janet_wrap_true());
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
t = janetc_gettarget(opts);
|
2018-07-10 01:24:22 +00:00
|
|
|
for (i = 1; i < len; i++) {
|
2020-06-28 23:16:57 +00:00
|
|
|
if (opim && can_slot_be_imm(args[i], &imm)) {
|
|
|
|
janetc_emit_ssi(c, opim, t, args[i - 1], imm, 1);
|
|
|
|
} else {
|
|
|
|
janetc_emit_sss(c, op, t, args[i - 1], args[i], 1);
|
|
|
|
}
|
2018-07-10 01:24:22 +00:00
|
|
|
if (i != (len - 1)) {
|
2020-06-28 23:16:57 +00:00
|
|
|
int32_t label = janetc_emit_si(c, invert ? JOP_JUMP_IF : JOP_JUMP_IF_NOT, t, 0, 1);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_v_push(labels, label);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
int32_t end = janet_v_count(c->buffer);
|
|
|
|
for (i = 0; i < janet_v_count(labels); i++) {
|
2018-07-10 01:24:22 +00:00
|
|
|
int32_t label = labels[i];
|
|
|
|
c->buffer[label] |= ((end - label) << 16);
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_v_free(labels);
|
2018-07-10 01:24:22 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_gt(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return compreduce(opts, args, JOP_GREATER_THAN, JOP_GREATER_THAN_IMMEDIATE, 0);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_lt(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return compreduce(opts, args, JOP_LESS_THAN, JOP_LESS_THAN_IMMEDIATE, 0);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_gte(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return compreduce(opts, args, JOP_GREATER_THAN_EQUAL, 0, 0);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_lte(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return compreduce(opts, args, JOP_LESS_THAN_EQUAL, 0, 0);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_eq(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return compreduce(opts, args, JOP_EQUALS, JOP_EQUALS_IMMEDIATE, 0);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
static JanetSlot do_neq(JanetFopts opts, JanetSlot *args) {
|
2020-06-28 23:16:57 +00:00
|
|
|
return compreduce(opts, args, JOP_NOT_EQUALS, JOP_NOT_EQUALS_IMMEDIATE, 1);
|
2018-07-10 01:24:22 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 23:35:45 +00:00
|
|
|
/* Arranged by tag */
|
2018-09-06 02:18:42 +00:00
|
|
|
static const JanetFunOptimizer optimizers[] = {
|
2019-11-24 23:45:53 +00:00
|
|
|
{maxarity1, do_debug},
|
2018-07-01 23:35:45 +00:00
|
|
|
{fixarity1, do_error},
|
2018-08-26 16:53:39 +00:00
|
|
|
{minarity2, do_apply},
|
2019-06-15 16:21:08 +00:00
|
|
|
{maxarity1, do_yield},
|
2020-02-08 19:03:03 +00:00
|
|
|
{arity1or2, do_resume},
|
2019-11-08 23:35:27 +00:00
|
|
|
{fixarity2, do_in},
|
2018-07-11 00:01:39 +00:00
|
|
|
{fixarity3, do_put},
|
2018-07-02 04:12:36 +00:00
|
|
|
{fixarity1, do_length},
|
|
|
|
{NULL, do_add},
|
|
|
|
{NULL, do_sub},
|
|
|
|
{NULL, do_mul},
|
|
|
|
{NULL, do_div},
|
|
|
|
{NULL, do_band},
|
|
|
|
{NULL, do_bor},
|
|
|
|
{NULL, do_bxor},
|
|
|
|
{NULL, do_lshift},
|
|
|
|
{NULL, do_rshift},
|
2018-07-09 03:10:02 +00:00
|
|
|
{NULL, do_rshiftu},
|
2018-07-10 01:24:22 +00:00
|
|
|
{fixarity1, do_bnot},
|
|
|
|
{NULL, do_gt},
|
|
|
|
{NULL, do_lt},
|
|
|
|
{NULL, do_gte},
|
|
|
|
{NULL, do_lte},
|
|
|
|
{NULL, do_eq},
|
2019-06-24 16:44:13 +00:00
|
|
|
{NULL, do_neq},
|
2019-12-03 03:15:08 +00:00
|
|
|
{fixarity2, do_propagate},
|
2020-06-28 20:03:01 +00:00
|
|
|
{arity2or3, do_get},
|
2020-02-08 19:03:03 +00:00
|
|
|
{arity1or2, do_next},
|
2020-01-24 00:54:30 +00:00
|
|
|
{fixarity2, do_modulo},
|
|
|
|
{fixarity2, do_remainder},
|
2020-06-24 21:00:00 +00:00
|
|
|
{fixarity2, do_cmp},
|
2020-08-22 20:35:37 +00:00
|
|
|
{fixarity2, do_cancel},
|
2018-07-01 23:35:45 +00:00
|
|
|
};
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
const JanetFunOptimizer *janetc_funopt(uint32_t flags) {
|
|
|
|
uint32_t tag = flags & JANET_FUNCDEF_FLAG_TAG;
|
2018-07-10 01:24:22 +00:00
|
|
|
if (tag == 0)
|
|
|
|
return NULL;
|
|
|
|
uint32_t index = tag - 1;
|
2019-02-20 01:51:34 +00:00
|
|
|
if (index >= (sizeof(optimizers) / sizeof(optimizers[0])))
|
2018-07-02 04:12:36 +00:00
|
|
|
return NULL;
|
2018-07-10 01:24:22 +00:00
|
|
|
return optimizers + index;
|
2018-07-01 23:35:45 +00:00
|
|
|
}
|
|
|
|
|