2017-03-05 16:10:35 +00:00
|
|
|
/* $OpenBSD: bcode.c,v 1.46 2014/10/08 03:59:56 doug Exp $ */
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2003, Otto Moerbeek <otto@drijf.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "extern.h"
|
|
|
|
|
2014-04-16 23:14:05 +00:00
|
|
|
/* #define DEBUGGING */
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
#define MAX_ARRAY_INDEX 2048
|
|
|
|
#define READSTACK_SIZE 8
|
|
|
|
|
|
|
|
#define NO_ELSE -2 /* -1 is EOF */
|
|
|
|
#define REG_ARRAY_SIZE_SMALL (UCHAR_MAX + 1)
|
|
|
|
#define REG_ARRAY_SIZE_BIG (UCHAR_MAX + 1 + USHRT_MAX + 1)
|
|
|
|
|
|
|
|
struct bmachine {
|
2010-02-03 21:06:13 +00:00
|
|
|
struct source *readstack;
|
|
|
|
struct stack *reg;
|
2010-01-20 21:30:52 +00:00
|
|
|
struct stack stack;
|
|
|
|
u_int scale;
|
|
|
|
u_int obase;
|
|
|
|
u_int ibase;
|
|
|
|
size_t readsp;
|
|
|
|
size_t reg_array_size;
|
|
|
|
size_t readstack_sz;
|
2010-02-03 21:06:13 +00:00
|
|
|
bool extended_regs;
|
2010-01-20 21:30:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct bmachine bmachine;
|
|
|
|
|
|
|
|
static __inline int readch(void);
|
|
|
|
static __inline void unreadch(void);
|
|
|
|
static __inline char *readline(void);
|
|
|
|
static __inline void src_free(void);
|
|
|
|
|
|
|
|
static __inline u_int max(u_int, u_int);
|
|
|
|
static u_long get_ulong(struct number *);
|
|
|
|
|
|
|
|
static __inline void push_number(struct number *);
|
|
|
|
static __inline void push_string(char *);
|
|
|
|
static __inline void push(struct value *);
|
|
|
|
static __inline struct value *tos(void);
|
|
|
|
static __inline struct number *pop_number(void);
|
|
|
|
static __inline char *pop_string(void);
|
|
|
|
static __inline void clear_stack(void);
|
|
|
|
static __inline void print_tos(void);
|
2017-02-28 20:50:49 +00:00
|
|
|
static void print_err(void);
|
2010-01-20 21:30:52 +00:00
|
|
|
static void pop_print(void);
|
|
|
|
static void pop_printn(void);
|
|
|
|
static __inline void print_stack(void);
|
|
|
|
static __inline void dup(void);
|
|
|
|
static void swap(void);
|
|
|
|
static void drop(void);
|
|
|
|
|
|
|
|
static void get_scale(void);
|
|
|
|
static void set_scale(void);
|
|
|
|
static void get_obase(void);
|
|
|
|
static void set_obase(void);
|
|
|
|
static void get_ibase(void);
|
|
|
|
static void set_ibase(void);
|
|
|
|
static void stackdepth(void);
|
|
|
|
static void push_scale(void);
|
|
|
|
static u_int count_digits(const struct number *);
|
|
|
|
static void num_digits(void);
|
|
|
|
static void to_ascii(void);
|
|
|
|
static void push_line(void);
|
|
|
|
static void comment(void);
|
|
|
|
static void bexec(char *);
|
|
|
|
static void badd(void);
|
|
|
|
static void bsub(void);
|
|
|
|
static void bmul(void);
|
|
|
|
static void bdiv(void);
|
|
|
|
static void bmod(void);
|
|
|
|
static void bdivmod(void);
|
|
|
|
static void bexp(void);
|
|
|
|
static bool bsqrt_stop(const BIGNUM *, const BIGNUM *, u_int *);
|
|
|
|
static void bsqrt(void);
|
|
|
|
static void not(void);
|
|
|
|
static void equal_numbers(void);
|
|
|
|
static void less_numbers(void);
|
|
|
|
static void lesseq_numbers(void);
|
|
|
|
static void equal(void);
|
|
|
|
static void not_equal(void);
|
|
|
|
static void less(void);
|
|
|
|
static void not_less(void);
|
|
|
|
static void greater(void);
|
|
|
|
static void not_greater(void);
|
|
|
|
static void not_compare(void);
|
|
|
|
static bool compare_numbers(enum bcode_compare, struct number *,
|
|
|
|
struct number *);
|
|
|
|
static void compare(enum bcode_compare);
|
|
|
|
static int readreg(void);
|
|
|
|
static void load(void);
|
|
|
|
static void store(void);
|
|
|
|
static void load_stack(void);
|
|
|
|
static void store_stack(void);
|
|
|
|
static void load_array(void);
|
|
|
|
static void store_array(void);
|
|
|
|
static void nop(void);
|
|
|
|
static void quit(void);
|
|
|
|
static void quitN(void);
|
|
|
|
static void skipN(void);
|
|
|
|
static void skip_until_mark(void);
|
|
|
|
static void parse_number(void);
|
|
|
|
static void unknown(void);
|
|
|
|
static void eval_string(char *);
|
|
|
|
static void eval_line(void);
|
|
|
|
static void eval_tos(void);
|
|
|
|
|
|
|
|
|
|
|
|
typedef void (*opcode_function)(void);
|
|
|
|
|
|
|
|
struct jump_entry {
|
|
|
|
u_char ch;
|
|
|
|
opcode_function f;
|
|
|
|
};
|
|
|
|
|
|
|
|
static opcode_function jump_table[UCHAR_MAX];
|
|
|
|
|
|
|
|
static const struct jump_entry jump_table_data[] = {
|
|
|
|
{ ' ', nop },
|
|
|
|
{ '!', not_compare },
|
|
|
|
{ '#', comment },
|
|
|
|
{ '%', bmod },
|
|
|
|
{ '(', less_numbers },
|
|
|
|
{ '*', bmul },
|
|
|
|
{ '+', badd },
|
|
|
|
{ '-', bsub },
|
|
|
|
{ '.', parse_number },
|
|
|
|
{ '/', bdiv },
|
|
|
|
{ '0', parse_number },
|
|
|
|
{ '1', parse_number },
|
|
|
|
{ '2', parse_number },
|
|
|
|
{ '3', parse_number },
|
|
|
|
{ '4', parse_number },
|
|
|
|
{ '5', parse_number },
|
|
|
|
{ '6', parse_number },
|
|
|
|
{ '7', parse_number },
|
|
|
|
{ '8', parse_number },
|
|
|
|
{ '9', parse_number },
|
|
|
|
{ ':', store_array },
|
|
|
|
{ ';', load_array },
|
|
|
|
{ '<', less },
|
|
|
|
{ '=', equal },
|
|
|
|
{ '>', greater },
|
|
|
|
{ '?', eval_line },
|
|
|
|
{ 'A', parse_number },
|
|
|
|
{ 'B', parse_number },
|
|
|
|
{ 'C', parse_number },
|
|
|
|
{ 'D', parse_number },
|
|
|
|
{ 'E', parse_number },
|
|
|
|
{ 'F', parse_number },
|
|
|
|
{ 'G', equal_numbers },
|
|
|
|
{ 'I', get_ibase },
|
|
|
|
{ 'J', skipN },
|
|
|
|
{ 'K', get_scale },
|
|
|
|
{ 'L', load_stack },
|
|
|
|
{ 'M', nop },
|
|
|
|
{ 'N', not },
|
|
|
|
{ 'O', get_obase },
|
|
|
|
{ 'P', pop_print },
|
|
|
|
{ 'Q', quitN },
|
|
|
|
{ 'R', drop },
|
|
|
|
{ 'S', store_stack },
|
|
|
|
{ 'X', push_scale },
|
|
|
|
{ 'Z', num_digits },
|
|
|
|
{ '[', push_line },
|
|
|
|
{ '\f', nop },
|
|
|
|
{ '\n', nop },
|
|
|
|
{ '\r', nop },
|
|
|
|
{ '\t', nop },
|
|
|
|
{ '^', bexp },
|
|
|
|
{ '_', parse_number },
|
|
|
|
{ 'a', to_ascii },
|
|
|
|
{ 'c', clear_stack },
|
|
|
|
{ 'd', dup },
|
2017-02-28 20:50:49 +00:00
|
|
|
{ 'e', print_err },
|
2010-01-20 21:30:52 +00:00
|
|
|
{ 'f', print_stack },
|
|
|
|
{ 'i', set_ibase },
|
|
|
|
{ 'k', set_scale },
|
|
|
|
{ 'l', load },
|
|
|
|
{ 'n', pop_printn },
|
|
|
|
{ 'o', set_obase },
|
|
|
|
{ 'p', print_tos },
|
|
|
|
{ 'q', quit },
|
|
|
|
{ 'r', swap },
|
|
|
|
{ 's', store },
|
|
|
|
{ 'v', bsqrt },
|
|
|
|
{ 'x', eval_tos },
|
|
|
|
{ 'z', stackdepth },
|
|
|
|
{ '{', lesseq_numbers },
|
|
|
|
{ '~', bdivmod }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define JUMP_TABLE_DATA_SIZE \
|
|
|
|
(sizeof(jump_table_data)/sizeof(jump_table_data[0]))
|
|
|
|
|
|
|
|
void
|
|
|
|
init_bmachine(bool extended_registers)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
unsigned int i;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
bmachine.extended_regs = extended_registers;
|
|
|
|
bmachine.reg_array_size = bmachine.extended_regs ?
|
|
|
|
REG_ARRAY_SIZE_BIG : REG_ARRAY_SIZE_SMALL;
|
|
|
|
|
|
|
|
bmachine.reg = calloc(bmachine.reg_array_size,
|
|
|
|
sizeof(bmachine.reg[0]));
|
|
|
|
if (bmachine.reg == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < UCHAR_MAX; i++)
|
|
|
|
jump_table[i] = unknown;
|
|
|
|
for (i = 0; i < JUMP_TABLE_DATA_SIZE; i++)
|
|
|
|
jump_table[jump_table_data[i].ch] = jump_table_data[i].f;
|
|
|
|
|
|
|
|
stack_init(&bmachine.stack);
|
|
|
|
|
|
|
|
for (i = 0; i < bmachine.reg_array_size; i++)
|
|
|
|
stack_init(&bmachine.reg[i]);
|
|
|
|
|
|
|
|
bmachine.readstack_sz = READSTACK_SIZE;
|
|
|
|
bmachine.readstack = calloc(sizeof(struct source),
|
|
|
|
bmachine.readstack_sz);
|
|
|
|
if (bmachine.readstack == NULL)
|
|
|
|
err(1, NULL);
|
|
|
|
bmachine.obase = bmachine.ibase = 10;
|
2012-12-30 15:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u_int
|
|
|
|
bmachine_scale(void)
|
|
|
|
{
|
2014-04-16 23:14:05 +00:00
|
|
|
return bmachine.scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the things needed before processing a (new) file */
|
|
|
|
void
|
|
|
|
reset_bmachine(struct source *src)
|
|
|
|
{
|
|
|
|
|
|
|
|
bmachine.readsp = 0;
|
|
|
|
bmachine.readstack[0] = *src;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
|
|
|
readch(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct source *src = &bmachine.readstack[bmachine.readsp];
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
return (src->vtable->readchar(src));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
unreadch(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct source *src = &bmachine.readstack[bmachine.readsp];
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
src->vtable->unreadchar(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline char *
|
|
|
|
readline(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct source *src = &bmachine.readstack[bmachine.readsp];
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
return (src->vtable->readline(src));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
src_free(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct source *src = &bmachine.readstack[bmachine.readsp];
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
src->vtable->free(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
void
|
|
|
|
pn(const char *str, const struct number *n)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
char *p = BN_bn2dec(n->number);
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
err(1, "BN_bn2dec failed");
|
|
|
|
fputs(str, stderr);
|
|
|
|
fprintf(stderr, " %s (%u)\n" , p, n->scale);
|
|
|
|
OPENSSL_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pbn(const char *str, const BIGNUM *n)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
char *p = BN_bn2dec(n);
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
err(1, "BN_bn2dec failed");
|
|
|
|
fputs(str, stderr);
|
|
|
|
fprintf(stderr, " %s\n", p);
|
|
|
|
OPENSSL_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static __inline u_int
|
|
|
|
max(u_int a, u_int b)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (a > b ? a : b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long factors[] = {
|
|
|
|
0, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
|
|
|
|
100000000, 1000000000
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
scale_number(BIGNUM *n, int s)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
unsigned int abs_scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (s == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
abs_scale = s > 0 ? s : -s;
|
|
|
|
|
|
|
|
if (abs_scale < sizeof(factors)/sizeof(factors[0])) {
|
|
|
|
if (s > 0)
|
|
|
|
bn_check(BN_mul_word(n, factors[abs_scale]));
|
|
|
|
else
|
|
|
|
BN_div_word(n, factors[abs_scale]);
|
|
|
|
} else {
|
2010-02-03 21:06:13 +00:00
|
|
|
BIGNUM *a, *p;
|
|
|
|
BN_CTX *ctx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = BN_new();
|
|
|
|
bn_checkp(a);
|
|
|
|
p = BN_new();
|
|
|
|
bn_checkp(p);
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
|
|
|
|
bn_check(BN_set_word(a, 10));
|
|
|
|
bn_check(BN_set_word(p, abs_scale));
|
|
|
|
bn_check(BN_exp(a, a, p, ctx));
|
|
|
|
if (s > 0)
|
|
|
|
bn_check(BN_mul(n, n, a, ctx));
|
|
|
|
else
|
|
|
|
bn_check(BN_div(n, NULL, n, a, ctx));
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
split_number(const struct number *n, BIGNUM *i, BIGNUM *f)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
u_long rem;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
bn_checkp(BN_copy(i, n->number));
|
|
|
|
|
|
|
|
if (n->scale == 0 && f != NULL)
|
|
|
|
bn_check(BN_zero(f));
|
|
|
|
else if (n->scale < sizeof(factors)/sizeof(factors[0])) {
|
|
|
|
rem = BN_div_word(i, factors[n->scale]);
|
|
|
|
if (f != NULL)
|
|
|
|
bn_check(BN_set_word(f, rem));
|
|
|
|
} else {
|
|
|
|
BIGNUM *a, *p;
|
|
|
|
BN_CTX *ctx;
|
|
|
|
|
|
|
|
a = BN_new();
|
|
|
|
bn_checkp(a);
|
|
|
|
p = BN_new();
|
|
|
|
bn_checkp(p);
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
|
|
|
|
bn_check(BN_set_word(a, 10));
|
|
|
|
bn_check(BN_set_word(p, n->scale));
|
|
|
|
bn_check(BN_exp(a, a, p, ctx));
|
|
|
|
bn_check(BN_div(i, f, n->number, a, ctx));
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
BN_free(a);
|
|
|
|
BN_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-30 15:20:27 +00:00
|
|
|
void
|
2010-01-20 21:30:52 +00:00
|
|
|
normalize(struct number *n, u_int s)
|
|
|
|
{
|
|
|
|
|
|
|
|
scale_number(n->number, s - n->scale);
|
|
|
|
n->scale = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_long
|
|
|
|
get_ulong(struct number *n)
|
|
|
|
{
|
|
|
|
|
|
|
|
normalize(n, 0);
|
|
|
|
return (BN_get_word(n->number));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
negate(struct number *n)
|
|
|
|
{
|
2012-12-30 15:20:27 +00:00
|
|
|
BN_set_negative(n->number, !BN_is_negative(n->number));
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
push_number(struct number *n)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_pushnumber(&bmachine.stack, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
push_string(char *string)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_pushstring(&bmachine.stack, string);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
push(struct value *v)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_push(&bmachine.stack, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline struct value *
|
|
|
|
tos(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (stack_tos(&bmachine.stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline struct value *
|
|
|
|
pop(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (stack_pop(&bmachine.stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline struct number *
|
|
|
|
pop_number(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (stack_popnumber(&bmachine.stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline char *
|
|
|
|
pop_string(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (stack_popstring(&bmachine.stack));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
clear_stack(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_clear(&bmachine.stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
print_stack(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_print(stdout, &bmachine.stack, "", bmachine.obase);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
print_tos(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct value *value = tos();
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (value != NULL) {
|
|
|
|
print_value(stdout, value, "", bmachine.obase);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warnx("stack empty");
|
|
|
|
}
|
|
|
|
|
2017-02-28 20:50:49 +00:00
|
|
|
static void
|
|
|
|
print_err(void)
|
|
|
|
{
|
|
|
|
struct value *value = tos();
|
|
|
|
if (value != NULL) {
|
|
|
|
print_value(stderr, value, "", bmachine.obase);
|
|
|
|
(void)putc('\n', stderr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
warnx("stack empty");
|
|
|
|
}
|
|
|
|
|
2010-01-20 21:30:52 +00:00
|
|
|
static void
|
|
|
|
pop_print(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct value *value = pop();
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (value != NULL) {
|
|
|
|
switch (value->type) {
|
|
|
|
case BCODE_NONE:
|
|
|
|
break;
|
|
|
|
case BCODE_NUMBER:
|
|
|
|
normalize(value->u.num, 0);
|
|
|
|
print_ascii(stdout, value->u.num);
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
|
|
|
case BCODE_STRING:
|
|
|
|
fputs(value->u.string, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stack_free_value(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pop_printn(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct value *value = pop();
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (value != NULL) {
|
|
|
|
print_value(stdout, value, "", bmachine.obase);
|
|
|
|
fflush(stdout);
|
|
|
|
stack_free_value(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void
|
|
|
|
dup(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_dup(&bmachine.stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
swap(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
stack_swap(&bmachine.stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
drop(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct value *v = pop();
|
2010-01-20 21:30:52 +00:00
|
|
|
if (v != NULL)
|
|
|
|
stack_free_value(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_scale(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, bmachine.scale));
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_scale(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
u_long scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = pop_number();
|
|
|
|
if (n != NULL) {
|
2012-12-30 15:20:27 +00:00
|
|
|
if (BN_is_negative(n->number))
|
2010-01-20 21:30:52 +00:00
|
|
|
warnx("scale must be a nonnegative number");
|
|
|
|
else {
|
|
|
|
scale = get_ulong(n);
|
|
|
|
if (scale != BN_MASK2 && scale <= UINT_MAX)
|
|
|
|
bmachine.scale = (u_int)scale;
|
|
|
|
else
|
|
|
|
warnx("scale too large");
|
|
|
|
}
|
|
|
|
free_number(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_obase(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, bmachine.obase));
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_obase(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
u_long base;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = pop_number();
|
|
|
|
if (n != NULL) {
|
|
|
|
base = get_ulong(n);
|
|
|
|
if (base != BN_MASK2 && base > 1 && base <= UINT_MAX)
|
|
|
|
bmachine.obase = (u_int)base;
|
|
|
|
else
|
|
|
|
warnx("output base must be a number greater than 1");
|
|
|
|
free_number(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
get_ibase(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, bmachine.ibase));
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_ibase(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
u_long base;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = pop_number();
|
|
|
|
if (n != NULL) {
|
|
|
|
base = get_ulong(n);
|
|
|
|
if (base != BN_MASK2 && 2 <= base && base <= 16)
|
|
|
|
bmachine.ibase = (u_int)base;
|
|
|
|
else
|
|
|
|
warnx("input base must be a number between 2 and 16 "
|
|
|
|
"(inclusive)");
|
|
|
|
free_number(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
stackdepth(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
size_t i;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
i = stack_size(&bmachine.stack);
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, i));
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
push_scale(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
struct value *value;
|
|
|
|
u_int scale = 0;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
value = pop();
|
|
|
|
if (value != NULL) {
|
|
|
|
switch (value->type) {
|
|
|
|
case BCODE_NONE:
|
|
|
|
return;
|
|
|
|
case BCODE_NUMBER:
|
|
|
|
scale = value->u.num->scale;
|
|
|
|
break;
|
|
|
|
case BCODE_STRING:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stack_free_value(value);
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, scale));
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_int
|
|
|
|
count_digits(const struct number *n)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *int_part, *fract_part;
|
|
|
|
u_int i;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (BN_is_zero(n->number))
|
2014-04-16 23:14:05 +00:00
|
|
|
return n->scale ? n->scale : 1;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
int_part = new_number();
|
|
|
|
fract_part = new_number();
|
|
|
|
fract_part->scale = n->scale;
|
|
|
|
split_number(n, int_part->number, fract_part->number);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (!BN_is_zero(int_part->number)) {
|
|
|
|
BN_div_word(int_part->number, 10);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free_number(int_part);
|
|
|
|
free_number(fract_part);
|
|
|
|
return (i + n->scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
num_digits(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n = NULL;
|
|
|
|
struct value *value;
|
|
|
|
size_t digits;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
value = pop();
|
|
|
|
if (value != NULL) {
|
|
|
|
switch (value->type) {
|
|
|
|
case BCODE_NONE:
|
|
|
|
return;
|
|
|
|
case BCODE_NUMBER:
|
|
|
|
digits = count_digits(value->u.num);
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, digits));
|
|
|
|
break;
|
|
|
|
case BCODE_STRING:
|
|
|
|
digits = strlen(value->u.string);
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_set_word(n->number, digits));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stack_free_value(value);
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
to_ascii(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
struct value *value;
|
|
|
|
char str[2];
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
value = pop();
|
|
|
|
if (value != NULL) {
|
|
|
|
str[1] = '\0';
|
|
|
|
switch (value->type) {
|
|
|
|
case BCODE_NONE:
|
|
|
|
return;
|
|
|
|
case BCODE_NUMBER:
|
|
|
|
n = value->u.num;
|
|
|
|
normalize(n, 0);
|
|
|
|
if (BN_num_bits(n->number) > 8)
|
|
|
|
bn_check(BN_mask_bits(n->number, 8));
|
|
|
|
str[0] = (char)BN_get_word(n->number);
|
|
|
|
break;
|
|
|
|
case BCODE_STRING:
|
|
|
|
str[0] = value->u.string[0];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stack_free_value(value);
|
|
|
|
push_string(bstrdup(str));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
readreg(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
int ch1, ch2, idx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
idx = readch();
|
|
|
|
if (idx == 0xff && bmachine.extended_regs) {
|
|
|
|
ch1 = readch();
|
|
|
|
ch2 = readch();
|
|
|
|
if (ch1 == EOF || ch2 == EOF) {
|
|
|
|
warnx("unexpected eof");
|
|
|
|
idx = -1;
|
|
|
|
} else
|
|
|
|
idx = (ch1 << 8) + ch2 + UCHAR_MAX + 1;
|
|
|
|
}
|
|
|
|
if (idx < 0 || (unsigned)idx >= bmachine.reg_array_size) {
|
|
|
|
warnx("internal error: reg num = %d", idx);
|
|
|
|
idx = -1;
|
|
|
|
}
|
|
|
|
return (idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
struct value *v;
|
|
|
|
struct value copy;
|
|
|
|
int idx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
idx = readreg();
|
|
|
|
if (idx >= 0) {
|
|
|
|
v = stack_tos(&bmachine.reg[idx]);
|
|
|
|
if (v == NULL) {
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_zero(n->number));
|
|
|
|
push_number(n);
|
|
|
|
} else
|
|
|
|
push(stack_dup_value(v, ©));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
store(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct value *val;
|
|
|
|
int idx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
idx = readreg();
|
|
|
|
if (idx >= 0) {
|
|
|
|
val = pop();
|
|
|
|
if (val == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stack_set_tos(&bmachine.reg[idx], val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_stack(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct stack *stack;
|
|
|
|
struct value *value;
|
|
|
|
int idx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
idx = readreg();
|
|
|
|
if (idx >= 0) {
|
|
|
|
stack = &bmachine.reg[idx];
|
|
|
|
value = NULL;
|
|
|
|
if (stack_size(stack) > 0) {
|
|
|
|
value = stack_pop(stack);
|
|
|
|
}
|
|
|
|
if (value != NULL)
|
|
|
|
push(value);
|
|
|
|
else
|
|
|
|
warnx("stack register '%c' (0%o) is empty",
|
|
|
|
idx, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
store_stack(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct value *value;
|
|
|
|
int idx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
idx = readreg();
|
|
|
|
if (idx >= 0) {
|
|
|
|
value = pop();
|
|
|
|
if (value == NULL)
|
|
|
|
return;
|
|
|
|
stack_push(&bmachine.reg[idx], value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
load_array(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *inumber, *n;
|
|
|
|
struct stack *stack;
|
|
|
|
struct value *v;
|
|
|
|
struct value copy;
|
|
|
|
u_long idx;
|
|
|
|
int reg;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
reg = readreg();
|
|
|
|
if (reg >= 0) {
|
|
|
|
inumber = pop_number();
|
|
|
|
if (inumber == NULL)
|
|
|
|
return;
|
|
|
|
idx = get_ulong(inumber);
|
2012-12-30 15:20:27 +00:00
|
|
|
if (BN_is_negative(inumber->number))
|
2010-01-20 21:30:52 +00:00
|
|
|
warnx("negative idx");
|
|
|
|
else if (idx == BN_MASK2 || idx > MAX_ARRAY_INDEX)
|
|
|
|
warnx("idx too big");
|
|
|
|
else {
|
|
|
|
stack = &bmachine.reg[reg];
|
|
|
|
v = frame_retrieve(stack, idx);
|
|
|
|
if (v == NULL || v->type == BCODE_NONE) {
|
|
|
|
n = new_number();
|
|
|
|
bn_check(BN_zero(n->number));
|
|
|
|
push_number(n);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
push(stack_dup_value(v, ©));
|
|
|
|
}
|
|
|
|
free_number(inumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
store_array(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *inumber;
|
|
|
|
struct value *value;
|
|
|
|
struct stack *stack;
|
|
|
|
u_long idx;
|
|
|
|
int reg;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
reg = readreg();
|
|
|
|
if (reg >= 0) {
|
|
|
|
inumber = pop_number();
|
|
|
|
if (inumber == NULL)
|
|
|
|
return;
|
|
|
|
value = pop();
|
|
|
|
if (value == NULL) {
|
|
|
|
free_number(inumber);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
idx = get_ulong(inumber);
|
2012-12-30 15:20:27 +00:00
|
|
|
if (BN_is_negative(inumber->number)) {
|
2010-01-20 21:30:52 +00:00
|
|
|
warnx("negative idx");
|
|
|
|
stack_free_value(value);
|
|
|
|
} else if (idx == BN_MASK2 || idx > MAX_ARRAY_INDEX) {
|
|
|
|
warnx("idx too big");
|
|
|
|
stack_free_value(value);
|
|
|
|
} else {
|
|
|
|
stack = &bmachine.reg[reg];
|
|
|
|
frame_assign(stack, idx, value);
|
|
|
|
}
|
|
|
|
free_number(inumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
push_line(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
push_string(read_string(&bmachine.readstack[bmachine.readsp]));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
comment(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
free(readline());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bexec(char *line)
|
|
|
|
{
|
|
|
|
|
|
|
|
system(line);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
badd(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new_number();
|
|
|
|
r->scale = max(a->scale, b->scale);
|
|
|
|
if (r->scale > a->scale)
|
|
|
|
normalize(a, r->scale);
|
|
|
|
else if (r->scale > b->scale)
|
|
|
|
normalize(b, r->scale);
|
|
|
|
bn_check(BN_add(r->number, a->number, b->number));
|
|
|
|
push_number(r);
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bsub(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new_number();
|
|
|
|
|
|
|
|
r->scale = max(a->scale, b->scale);
|
|
|
|
if (r->scale > a->scale)
|
|
|
|
normalize(a, r->scale);
|
|
|
|
else if (r->scale > b->scale)
|
|
|
|
normalize(b, r->scale);
|
|
|
|
bn_check(BN_sub(r->number, b->number, a->number));
|
|
|
|
push_number(r);
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-30 15:20:27 +00:00
|
|
|
bmul_number(struct number *r, struct number *a, struct number *b, u_int scale)
|
2010-01-20 21:30:52 +00:00
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
BN_CTX *ctx;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
/* Create copies of the scales, since r might be equal to a or b */
|
|
|
|
u_int ascale = a->scale;
|
|
|
|
u_int bscale = b->scale;
|
|
|
|
u_int rscale = ascale + bscale;
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
bn_check(BN_mul(r->number, a->number, b->number, ctx));
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
|
2012-12-30 15:20:27 +00:00
|
|
|
r->scale = rscale;
|
|
|
|
if (rscale > bmachine.scale && rscale > ascale && rscale > bscale)
|
|
|
|
normalize(r, max(scale, max(ascale, bscale)));
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bmul(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new_number();
|
2012-12-30 15:20:27 +00:00
|
|
|
bmul_number(r, a, b, bmachine.scale);
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
push_number(r);
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bdiv(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
|
|
|
BN_CTX *ctx;
|
|
|
|
u_int scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new_number();
|
|
|
|
r->scale = bmachine.scale;
|
|
|
|
scale = max(a->scale, b->scale);
|
|
|
|
|
|
|
|
if (BN_is_zero(a->number))
|
|
|
|
warnx("divide by zero");
|
|
|
|
else {
|
|
|
|
normalize(a, scale);
|
|
|
|
normalize(b, scale + r->scale);
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
bn_check(BN_div(r->number, NULL, b->number, a->number, ctx));
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
push_number(r);
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bmod(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
|
|
|
BN_CTX *ctx;
|
|
|
|
u_int scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = new_number();
|
|
|
|
scale = max(a->scale, b->scale);
|
|
|
|
r->scale = max(b->scale, a->scale + bmachine.scale);
|
|
|
|
|
|
|
|
if (BN_is_zero(a->number))
|
|
|
|
warnx("remainder by zero");
|
|
|
|
else {
|
|
|
|
normalize(a, scale);
|
|
|
|
normalize(b, scale + bmachine.scale);
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
bn_check(BN_mod(r->number, b->number, a->number, ctx));
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
push_number(r);
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bdivmod(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *rdiv, *rmod;
|
|
|
|
BN_CTX *ctx;
|
|
|
|
u_int scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdiv = new_number();
|
|
|
|
rmod = new_number();
|
|
|
|
rdiv->scale = bmachine.scale;
|
|
|
|
rmod->scale = max(b->scale, a->scale + bmachine.scale);
|
|
|
|
scale = max(a->scale, b->scale);
|
|
|
|
|
|
|
|
if (BN_is_zero(a->number))
|
|
|
|
warnx("divide by zero");
|
|
|
|
else {
|
|
|
|
normalize(a, scale);
|
|
|
|
normalize(b, scale + bmachine.scale);
|
|
|
|
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
bn_check(BN_div(rdiv->number, rmod->number,
|
|
|
|
b->number, a->number, ctx));
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
}
|
|
|
|
push_number(rdiv);
|
|
|
|
push_number(rmod);
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bexp(void)
|
|
|
|
{
|
2014-04-16 23:14:05 +00:00
|
|
|
struct number *a, *p;
|
|
|
|
struct number *r;
|
|
|
|
bool neg;
|
|
|
|
u_int rscale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
p = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (p == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
a = pop_number();
|
|
|
|
if (a == NULL) {
|
|
|
|
push_number(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-30 15:20:27 +00:00
|
|
|
if (p->scale != 0) {
|
|
|
|
BIGNUM *i, *f;
|
|
|
|
i = BN_new();
|
|
|
|
bn_checkp(i);
|
|
|
|
f = BN_new();
|
|
|
|
bn_checkp(f);
|
|
|
|
split_number(p, i, f);
|
|
|
|
if (!BN_is_zero(f))
|
2014-04-16 23:14:05 +00:00
|
|
|
warnx("Runtime warning: non-zero fractional part in exponent");
|
2012-12-30 15:20:27 +00:00
|
|
|
BN_free(i);
|
|
|
|
BN_free(f);
|
|
|
|
}
|
|
|
|
|
2010-01-20 21:30:52 +00:00
|
|
|
normalize(p, 0);
|
|
|
|
|
|
|
|
neg = false;
|
2012-12-30 15:20:27 +00:00
|
|
|
if (BN_is_negative(p->number)) {
|
2010-01-20 21:30:52 +00:00
|
|
|
neg = true;
|
|
|
|
negate(p);
|
2012-12-30 15:20:27 +00:00
|
|
|
rscale = bmachine.scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
} else {
|
|
|
|
/* Posix bc says min(a.scale * b, max(a.scale, scale) */
|
2010-02-03 21:06:13 +00:00
|
|
|
u_long b;
|
|
|
|
u_int m;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
b = BN_get_word(p->number);
|
|
|
|
m = max(a->scale, bmachine.scale);
|
2012-12-30 15:20:27 +00:00
|
|
|
rscale = a->scale * (u_int)b;
|
|
|
|
if (rscale > m || (a->scale > 0 && (b == BN_MASK2 ||
|
2010-01-20 21:30:52 +00:00
|
|
|
b > UINT_MAX)))
|
2012-12-30 15:20:27 +00:00
|
|
|
rscale = m;
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (BN_is_zero(p->number)) {
|
|
|
|
r = new_number();
|
|
|
|
bn_check(BN_one(r->number));
|
2012-12-30 15:20:27 +00:00
|
|
|
normalize(r, rscale);
|
2010-01-20 21:30:52 +00:00
|
|
|
} else {
|
2012-12-30 15:20:27 +00:00
|
|
|
u_int ascale, mscale;
|
|
|
|
|
|
|
|
ascale = a->scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
while (!BN_is_bit_set(p->number, 0)) {
|
2012-12-30 15:20:27 +00:00
|
|
|
ascale *= 2;
|
|
|
|
bmul_number(a, a, a, ascale);
|
2010-01-20 21:30:52 +00:00
|
|
|
bn_check(BN_rshift1(p->number, p->number));
|
|
|
|
}
|
|
|
|
|
|
|
|
r = dup_number(a);
|
|
|
|
bn_check(BN_rshift1(p->number, p->number));
|
|
|
|
|
2012-12-30 15:20:27 +00:00
|
|
|
mscale = ascale;
|
2010-01-20 21:30:52 +00:00
|
|
|
while (!BN_is_zero(p->number)) {
|
2012-12-30 15:20:27 +00:00
|
|
|
ascale *= 2;
|
|
|
|
bmul_number(a, a, a, ascale);
|
|
|
|
if (BN_is_bit_set(p->number, 0)) {
|
|
|
|
mscale += ascale;
|
|
|
|
bmul_number(r, r, a, mscale);
|
|
|
|
}
|
2010-01-20 21:30:52 +00:00
|
|
|
bn_check(BN_rshift1(p->number, p->number));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (neg) {
|
2010-02-03 21:06:13 +00:00
|
|
|
BN_CTX *ctx;
|
|
|
|
BIGNUM *one;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
one = BN_new();
|
|
|
|
bn_checkp(one);
|
|
|
|
bn_check(BN_one(one));
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
2012-12-30 15:20:27 +00:00
|
|
|
scale_number(one, r->scale + rscale);
|
|
|
|
|
|
|
|
if (BN_is_zero(r->number))
|
|
|
|
warnx("divide by zero");
|
|
|
|
else
|
|
|
|
bn_check(BN_div(r->number, NULL, one,
|
|
|
|
r->number, ctx));
|
2010-01-20 21:30:52 +00:00
|
|
|
BN_free(one);
|
|
|
|
BN_CTX_free(ctx);
|
2012-12-30 15:20:27 +00:00
|
|
|
r->scale = rscale;
|
2010-01-20 21:30:52 +00:00
|
|
|
} else
|
2012-12-30 15:20:27 +00:00
|
|
|
normalize(r, rscale);
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
push_number(r);
|
|
|
|
free_number(a);
|
|
|
|
free_number(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
bsqrt_stop(const BIGNUM *x, const BIGNUM *y, u_int *onecount)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
BIGNUM *r;
|
|
|
|
bool ret;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
r = BN_new();
|
|
|
|
bn_checkp(r);
|
|
|
|
bn_check(BN_sub(r, x, y));
|
|
|
|
if (BN_is_one(r))
|
|
|
|
(*onecount)++;
|
|
|
|
ret = BN_is_zero(r);
|
|
|
|
BN_free(r);
|
|
|
|
return (ret || *onecount > 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bsqrt(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n, *r;
|
|
|
|
BIGNUM *x, *y;
|
|
|
|
BN_CTX *ctx;
|
|
|
|
u_int onecount, scale;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
onecount = 0;
|
|
|
|
n = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (n == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
if (BN_is_zero(n->number)) {
|
|
|
|
r = new_number();
|
|
|
|
push_number(r);
|
2012-12-30 15:20:27 +00:00
|
|
|
} else if (BN_is_negative(n->number))
|
2010-01-20 21:30:52 +00:00
|
|
|
warnx("square root of negative number");
|
|
|
|
else {
|
|
|
|
scale = max(bmachine.scale, n->scale);
|
|
|
|
normalize(n, 2*scale);
|
|
|
|
x = BN_dup(n->number);
|
|
|
|
bn_checkp(x);
|
|
|
|
bn_check(BN_rshift(x, x, BN_num_bits(x)/2));
|
|
|
|
y = BN_new();
|
|
|
|
bn_checkp(y);
|
|
|
|
ctx = BN_CTX_new();
|
|
|
|
bn_checkp(ctx);
|
|
|
|
for (;;) {
|
|
|
|
bn_checkp(BN_copy(y, x));
|
|
|
|
bn_check(BN_div(x, NULL, n->number, x, ctx));
|
|
|
|
bn_check(BN_add(x, x, y));
|
|
|
|
bn_check(BN_rshift1(x, x));
|
|
|
|
if (bsqrt_stop(x, y, &onecount))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
r = bmalloc(sizeof(*r));
|
|
|
|
r->scale = scale;
|
|
|
|
r->number = y;
|
|
|
|
BN_free(x);
|
|
|
|
BN_CTX_free(ctx);
|
|
|
|
push_number(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
free_number(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
not(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
a->scale = 0;
|
|
|
|
bn_check(BN_set_word(a->number, BN_get_word(a->number) ? 0 : 1));
|
|
|
|
push_number(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
equal(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare(BCODE_EQUAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
equal_numbers(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r = new_number();
|
|
|
|
bn_check(BN_set_word(r->number,
|
|
|
|
compare_numbers(BCODE_EQUAL, a, b) ? 1 : 0));
|
|
|
|
push_number(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
less_numbers(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r = new_number();
|
|
|
|
bn_check(BN_set_word(r->number,
|
|
|
|
compare_numbers(BCODE_LESS, a, b) ? 1 : 0));
|
|
|
|
push_number(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
lesseq_numbers(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b, *r;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
a = pop_number();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (a == NULL)
|
2010-01-20 21:30:52 +00:00
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r = new_number();
|
|
|
|
bn_check(BN_set_word(r->number,
|
|
|
|
compare_numbers(BCODE_NOT_GREATER, a, b) ? 1 : 0));
|
|
|
|
push_number(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
not_equal(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare(BCODE_NOT_EQUAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
less(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare(BCODE_LESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
not_compare(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
|
2010-01-20 21:30:52 +00:00
|
|
|
switch (readch()) {
|
|
|
|
case '<':
|
|
|
|
not_less();
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
not_greater();
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
not_equal();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
unreadch();
|
|
|
|
bexec(readline());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
not_less(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare(BCODE_NOT_LESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
greater(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare(BCODE_GREATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
not_greater(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
compare(BCODE_NOT_GREATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
compare_numbers(enum bcode_compare type, struct number *a, struct number *b)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
u_int scale;
|
|
|
|
int cmp;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
scale = max(a->scale, b->scale);
|
|
|
|
|
|
|
|
if (scale > a->scale)
|
|
|
|
normalize(a, scale);
|
|
|
|
else if (scale > b->scale)
|
|
|
|
normalize(b, scale);
|
|
|
|
|
|
|
|
cmp = BN_cmp(a->number, b->number);
|
|
|
|
|
|
|
|
free_number(a);
|
|
|
|
free_number(b);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case BCODE_EQUAL:
|
|
|
|
return (cmp == 0);
|
|
|
|
case BCODE_NOT_EQUAL:
|
|
|
|
return (cmp != 0);
|
|
|
|
case BCODE_LESS:
|
|
|
|
return (cmp < 0);
|
|
|
|
case BCODE_NOT_LESS:
|
|
|
|
return (cmp >= 0);
|
|
|
|
case BCODE_GREATER:
|
|
|
|
return (cmp > 0);
|
|
|
|
case BCODE_NOT_GREATER:
|
|
|
|
return (cmp <= 0);
|
|
|
|
}
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
compare(enum bcode_compare type)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *a, *b;
|
|
|
|
struct value *v;
|
|
|
|
int idx, elseidx;
|
|
|
|
bool ok;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
elseidx = NO_ELSE;
|
|
|
|
idx = readreg();
|
|
|
|
if (readch() == 'e')
|
|
|
|
elseidx = readreg();
|
|
|
|
else
|
|
|
|
unreadch();
|
|
|
|
|
|
|
|
a = pop_number();
|
|
|
|
if (a == NULL)
|
|
|
|
return;
|
|
|
|
b = pop_number();
|
|
|
|
if (b == NULL) {
|
|
|
|
push_number(a);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = compare_numbers(type, a, b);
|
|
|
|
|
|
|
|
if (!ok && elseidx != NO_ELSE)
|
|
|
|
idx = elseidx;
|
|
|
|
|
|
|
|
if (idx >= 0 && (ok || (!ok && elseidx != NO_ELSE))) {
|
|
|
|
v = stack_tos(&bmachine.reg[idx]);
|
|
|
|
if (v == NULL)
|
|
|
|
warnx("register '%c' (0%o) is empty", idx, idx);
|
|
|
|
else {
|
|
|
|
switch(v->type) {
|
|
|
|
case BCODE_NONE:
|
|
|
|
warnx("register '%c' (0%o) is empty", idx, idx);
|
|
|
|
break;
|
|
|
|
case BCODE_NUMBER:
|
|
|
|
warn("eval called with non-string argument");
|
|
|
|
break;
|
|
|
|
case BCODE_STRING:
|
|
|
|
eval_string(bstrdup(v->u.string));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
nop(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
quit(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
|
2010-01-20 21:30:52 +00:00
|
|
|
if (bmachine.readsp < 2)
|
|
|
|
exit(0);
|
|
|
|
src_free();
|
|
|
|
bmachine.readsp--;
|
|
|
|
src_free();
|
|
|
|
bmachine.readsp--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
quitN(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
u_long i;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = pop_number();
|
|
|
|
if (n == NULL)
|
|
|
|
return;
|
|
|
|
i = get_ulong(n);
|
|
|
|
free_number(n);
|
|
|
|
if (i == BN_MASK2 || i == 0)
|
|
|
|
warnx("Q command requires a number >= 1");
|
|
|
|
else if (bmachine.readsp < i)
|
|
|
|
warnx("Q command argument exceeded string execution depth");
|
|
|
|
else {
|
|
|
|
while (i-- > 0) {
|
|
|
|
src_free();
|
|
|
|
bmachine.readsp--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
skipN(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
struct number *n;
|
|
|
|
u_long i;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
n = pop_number();
|
|
|
|
if (n == NULL)
|
|
|
|
return;
|
|
|
|
i = get_ulong(n);
|
|
|
|
if (i == BN_MASK2)
|
|
|
|
warnx("J command requires a number >= 0");
|
|
|
|
else if (i > 0 && bmachine.readsp < i)
|
|
|
|
warnx("J command argument exceeded string execution depth");
|
|
|
|
else {
|
|
|
|
while (i-- > 0) {
|
|
|
|
src_free();
|
|
|
|
bmachine.readsp--;
|
|
|
|
}
|
|
|
|
skip_until_mark();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
skip_until_mark(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
for (;;) {
|
2010-02-03 19:13:41 +00:00
|
|
|
switch (readch()) {
|
2010-01-20 21:30:52 +00:00
|
|
|
case 'M':
|
|
|
|
return;
|
|
|
|
case EOF:
|
|
|
|
errx(1, "mark not found");
|
|
|
|
return;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
case ':':
|
|
|
|
case ';':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '=':
|
|
|
|
readreg();
|
|
|
|
if (readch() == 'e')
|
|
|
|
readreg();
|
|
|
|
else
|
|
|
|
unreadch();
|
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
free(read_string(&bmachine.readstack[bmachine.readsp]));
|
|
|
|
break;
|
|
|
|
case '!':
|
2010-02-03 19:13:41 +00:00
|
|
|
switch (readch()) {
|
2010-01-20 21:30:52 +00:00
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '=':
|
|
|
|
readreg();
|
|
|
|
if (readch() == 'e')
|
|
|
|
readreg();
|
|
|
|
else
|
|
|
|
unreadch();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
free(readline());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_number(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
unreadch();
|
|
|
|
push_number(readnumber(&bmachine.readstack[bmachine.readsp],
|
|
|
|
bmachine.ibase));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unknown(void)
|
|
|
|
{
|
|
|
|
int ch = bmachine.readstack[bmachine.readsp].lastchar;
|
|
|
|
warnx("%c (0%o) is unimplemented", ch, ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eval_string(char *p)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
int ch;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
if (bmachine.readsp > 0) {
|
|
|
|
/* Check for tail call. Do not recurse in that case. */
|
|
|
|
ch = readch();
|
|
|
|
if (ch == EOF) {
|
|
|
|
src_free();
|
|
|
|
src_setstring(&bmachine.readstack[bmachine.readsp], p);
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
unreadch();
|
|
|
|
}
|
|
|
|
if (bmachine.readsp == bmachine.readstack_sz - 1) {
|
|
|
|
size_t newsz = bmachine.readstack_sz * 2;
|
|
|
|
struct source *stack;
|
2017-03-05 16:10:35 +00:00
|
|
|
stack = reallocarray(bmachine.readstack, newsz,
|
2010-01-20 21:30:52 +00:00
|
|
|
sizeof(struct source));
|
|
|
|
if (stack == NULL)
|
|
|
|
err(1, "recursion too deep");
|
|
|
|
bmachine.readstack_sz = newsz;
|
|
|
|
bmachine.readstack = stack;
|
|
|
|
}
|
|
|
|
src_setstring(&bmachine.readstack[++bmachine.readsp], p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eval_line(void)
|
|
|
|
{
|
|
|
|
/* Always read from stdin */
|
2010-02-03 21:06:13 +00:00
|
|
|
struct source in;
|
|
|
|
char *p;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
clearerr(stdin);
|
|
|
|
src_setstream(&in, stdin);
|
|
|
|
p = (*in.vtable->readline)(&in);
|
|
|
|
eval_string(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
eval_tos(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
char *p;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
p = pop_string();
|
2017-02-26 22:17:06 +00:00
|
|
|
if (p != NULL)
|
|
|
|
eval_string(p);
|
2010-01-20 21:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
eval(void)
|
|
|
|
{
|
2010-02-03 21:06:13 +00:00
|
|
|
int ch;
|
2010-01-20 21:30:52 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ch = readch();
|
|
|
|
if (ch == EOF) {
|
|
|
|
if (bmachine.readsp == 0)
|
|
|
|
return;
|
|
|
|
src_free();
|
|
|
|
bmachine.readsp--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
fprintf(stderr, "# %c\n", ch);
|
|
|
|
stack_print(stderr, &bmachine.stack, "* ",
|
|
|
|
bmachine.obase);
|
|
|
|
fprintf(stderr, "%zd =>\n", bmachine.readsp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (0 <= ch && ch < (signed)UCHAR_MAX)
|
|
|
|
(*jump_table[ch])();
|
|
|
|
else
|
|
|
|
warnx("internal error: opcode %d", ch);
|
|
|
|
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
stack_print(stderr, &bmachine.stack, "* ",
|
|
|
|
bmachine.obase);
|
|
|
|
fprintf(stderr, "%zd ==\n", bmachine.readsp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|