u6a/src/vm_stack.h

173 lines
5.2 KiB
C
Raw Normal View History

2020-01-30 10:11:10 +00:00
/*
* vm_stack.h - Unlambda VM segmented stacks definitions
*
* Copyright (C) 2020 CismonX <admin@cismon.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef U6A_VM_STACK_H_
#define U6A_VM_STACK_H_
#include "common.h"
#include "vm_defs.h"
#include <stdint.h>
#include <stdbool.h>
2020-06-15 12:22:50 +00:00
struct u6a_vm_stack {
struct u6a_vm_stack* prev;
uint32_t top;
uint32_t refcnt;
struct u6a_vm_var_fn elems[];
};
struct u6a_vm_stack_ctx {
struct u6a_vm_stack* active_stack;
uint32_t stack_seg_len;
struct u6a_vm_pool_ctx* pool_ctx;
const char* err_stage;
};
2020-01-30 10:11:10 +00:00
bool
2020-06-15 12:22:50 +00:00
u6a_vm_stack_init(struct u6a_vm_stack_ctx* ctx, uint32_t stack_seg_len, const char* err_stage);
static inline struct u6a_vm_var_fn
u6a_vm_stack_top(struct u6a_vm_stack_ctx* ctx) {
struct u6a_vm_stack* vs = ctx->active_stack;
if (UNLIKELY(vs->top == UINT32_MAX)) {
vs = vs->prev;
if (UNLIKELY(vs == NULL)) {
return U6A_VM_VAR_FN_EMPTY;
}
ctx->active_stack = vs;
}
return vs->elems[vs->top];
}
2020-01-30 10:11:10 +00:00
bool
2020-06-15 12:22:50 +00:00
u6a_vm_stack_push1_split_(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0);
static inline bool
u6a_vm_stack_push1(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0) {
struct u6a_vm_stack* vs = ctx->active_stack;
if (LIKELY(vs->top + 1 < ctx->stack_seg_len)) {
vs->elems[++vs->top] = v0;
return true;
}
return u6a_vm_stack_push1_split_(ctx, v0);
}
2020-01-30 10:11:10 +00:00
bool
2020-06-15 12:22:50 +00:00
u6a_vm_stack_push2_split_(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1);
static inline bool
u6a_vm_stack_push2(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1) {
struct u6a_vm_stack* vs = ctx->active_stack;
if (LIKELY(vs->top + 2 < ctx->stack_seg_len)) {
vs->elems[++vs->top] = v0;
vs->elems[++vs->top] = v1;
return true;
}
return u6a_vm_stack_push2_split_(ctx, v0, v1);
}
2020-01-30 10:11:10 +00:00
bool
2020-06-15 12:22:50 +00:00
u6a_vm_stack_push3_split_(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1,
struct u6a_vm_var_fn v2);
static inline bool
u6a_vm_stack_push3(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1,
struct u6a_vm_var_fn v2)
{
struct u6a_vm_stack* vs = ctx->active_stack;
if (LIKELY(vs->top + 3 < ctx->stack_seg_len)) {
vs->elems[++vs->top] = v0;
vs->elems[++vs->top] = v1;
vs->elems[++vs->top] = v2;
return true;
}
return u6a_vm_stack_push3_split_(ctx, v0, v1, v2);
}
2020-01-30 10:11:10 +00:00
bool
2020-06-15 12:22:50 +00:00
u6a_vm_stack_push4_split_(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1,
struct u6a_vm_var_fn v2, struct u6a_vm_var_fn v3);
static inline bool
u6a_vm_stack_push4(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0, struct u6a_vm_var_fn v1,
struct u6a_vm_var_fn v2, struct u6a_vm_var_fn v3)
{
struct u6a_vm_stack* vs = ctx->active_stack;
if (LIKELY(vs->top + 4 < ctx->stack_seg_len)) {
vs->elems[++vs->top] = v0;
vs->elems[++vs->top] = v1;
vs->elems[++vs->top] = v2;
vs->elems[++vs->top] = v3;
return true;
}
return u6a_vm_stack_push4_split_(ctx, v0, v1, v2, v3);
}
2020-01-30 10:11:10 +00:00
2020-06-15 12:22:50 +00:00
bool
u6a_vm_stack_pop_split_(struct u6a_vm_stack_ctx* ctx);
2020-01-30 10:11:10 +00:00
2020-06-15 12:22:50 +00:00
static inline bool
u6a_vm_stack_pop(struct u6a_vm_stack_ctx* ctx) {
struct u6a_vm_stack* vs = ctx->active_stack;
if (LIKELY(vs->top-- != UINT32_MAX)) {
return true;
}
return u6a_vm_stack_pop_split_(ctx);
}
2020-01-30 10:11:10 +00:00
2020-06-15 12:22:50 +00:00
struct u6a_vm_var_fn
u6a_vm_stack_xch_split_(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0);
static inline struct u6a_vm_var_fn
u6a_vm_stack_xch(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_var_fn v0) {
struct u6a_vm_stack* vs = ctx->active_stack;
struct u6a_vm_var_fn elem;
// XCH on segmented stacks is inefficient, perhaps there's a better solution?
if (LIKELY(vs->top != 0 && vs->top != UINT32_MAX)) {
elem = vs->elems[vs->top - 1];
vs->elems[vs->top - 1] = v0;
} else {
elem = u6a_vm_stack_xch_split_(ctx, v0);
}
return elem;
}
struct u6a_vm_stack*
u6a_vm_stack_dup(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_stack* vs);
static inline struct u6a_vm_stack*
u6a_vm_stack_save(struct u6a_vm_stack_ctx* ctx) {
return u6a_vm_stack_dup(ctx, ctx->active_stack);
}
2020-02-04 18:47:45 +00:00
2020-01-30 10:11:10 +00:00
void
2020-06-15 12:22:50 +00:00
u6a_vm_stack_discard(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_stack* vs);
2020-01-30 10:11:10 +00:00
void
2020-06-15 12:22:50 +00:00
u6a_vm_stack_destroy(struct u6a_vm_stack_ctx* ctx);
2020-01-30 10:11:10 +00:00
2020-06-15 12:22:50 +00:00
static inline void
u6a_vm_stack_resume(struct u6a_vm_stack_ctx* ctx, struct u6a_vm_stack* vs) {
u6a_vm_stack_destroy(ctx);
ctx->active_stack = vs;
}
2020-01-30 10:11:10 +00:00
#endif