forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbcode.c
1773 lines (1536 loc) · 31 KB
/
bcode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $OpenBSD: bcode.c,v 1.46 2014/10/08 03:59:56 doug Exp $ */
/*
* 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>
#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"
/* #define DEBUGGING */
#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 {
struct source *readstack;
struct stack *reg;
struct stack stack;
u_int scale;
u_int obase;
u_int ibase;
size_t readsp;
size_t reg_array_size;
size_t readstack_sz;
bool extended_regs;
};
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 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);
static void print_err(void);
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 },
{ 'e', print_err },
{ '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)
{
unsigned int i;
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;
}
u_int
bmachine_scale(void)
{
return bmachine.scale;
}
/* 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)
{
struct source *src = &bmachine.readstack[bmachine.readsp];
return (src->vtable->readchar(src));
}
static __inline void
unreadch(void)
{
struct source *src = &bmachine.readstack[bmachine.readsp];
src->vtable->unreadchar(src);
}
static __inline char *
readline(void)
{
struct source *src = &bmachine.readstack[bmachine.readsp];
return (src->vtable->readline(src));
}
static __inline void
src_free(void)
{
struct source *src = &bmachine.readstack[bmachine.readsp];
src->vtable->free(src);
}
#ifdef DEBUGGING
void
pn(const char *str, const struct number *n)
{
char *p = BN_bn2dec(n->number);
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)
{
char *p = BN_bn2dec(n);
if (p == NULL)
err(1, "BN_bn2dec failed");
fputs(str, stderr);
fprintf(stderr, " %s\n", p);
OPENSSL_free(p);
}
#endif
static unsigned long factors[] = {
0, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
100000000, 1000000000
};
/* Multiply n by 10^s */
void
scale_number(BIGNUM *n, int s)
{
unsigned int abs_scale;
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 {
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, 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)
{
u_long rem;
bn_checkp(BN_copy(i, n->number));
if (n->scale == 0 && f != NULL)
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);
}
}
/* Change the scale of n to s. Reducing scale may truncate the mantissa */
void
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)
{
BN_set_negative(n->number, !BN_is_negative(n->number));
}
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)
{
struct value *value = tos();
if (value != NULL) {
print_value(stdout, value, "", bmachine.obase);
putchar('\n');
}
else
warnx("stack empty");
}
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");
}
static void
pop_print(void)
{
struct value *value = pop();
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)
{
struct value *value = pop();
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)
{
struct value *v = pop();
if (v != NULL)
stack_free_value(v);
}
static void
get_scale(void)
{
struct number *n;
n = new_number();
bn_check(BN_set_word(n->number, bmachine.scale));
push_number(n);
}
static void
set_scale(void)
{
struct number *n;
u_long scale;
n = pop_number();
if (n != NULL) {
if (BN_is_negative(n->number))
warnx("scale must be a nonnegative number");
else {
scale = get_ulong(n);
if (scale != ULONG_MAX && scale <= UINT_MAX)
bmachine.scale = (u_int)scale;
else
warnx("scale too large");
}
free_number(n);
}
}
static void
get_obase(void)
{
struct number *n;
n = new_number();
bn_check(BN_set_word(n->number, bmachine.obase));
push_number(n);
}
static void
set_obase(void)
{
struct number *n;
u_long base;
n = pop_number();
if (n != NULL) {
base = get_ulong(n);
if (base != ULONG_MAX && 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)
{
struct number *n;
n = new_number();
bn_check(BN_set_word(n->number, bmachine.ibase));
push_number(n);
}
static void
set_ibase(void)
{
struct number *n;
u_long base;
n = pop_number();
if (n != NULL) {
base = get_ulong(n);
if (base != ULONG_MAX && 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)
{
struct number *n;
size_t i;
i = stack_size(&bmachine.stack);
n = new_number();
bn_check(BN_set_word(n->number, i));
push_number(n);
}
static void
push_scale(void)
{
struct number *n;
struct value *value;
u_int scale = 0;
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)
{
struct number *int_part, *fract_part;
u_int i;
if (BN_is_zero(n->number))
return n->scale ? n->scale : 1;
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)
{
struct number *n = NULL;
struct value *value;
size_t digits;
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)
{
struct number *n;
struct value *value;
char str[2];
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)
{
int ch1, ch2, idx;
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)
{
struct number *n;
struct value *v;
struct value copy;
int idx;
idx = readreg();
if (idx >= 0) {
v = stack_tos(&bmachine.reg[idx]);
if (v == NULL) {
n = new_number();
BN_zero(n->number);
push_number(n);
} else
push(stack_dup_value(v, ©));
}
}
static void
store(void)
{
struct value *val;
int idx;
idx = readreg();
if (idx >= 0) {
val = pop();
if (val == NULL) {
return;
}
stack_set_tos(&bmachine.reg[idx], val);
}
}
static void
load_stack(void)
{
struct stack *stack;
struct value *value;
int idx;
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)
{
struct value *value;
int idx;
idx = readreg();
if (idx >= 0) {
value = pop();
if (value == NULL)
return;
stack_push(&bmachine.reg[idx], value);
}
}
static void
load_array(void)
{
struct number *inumber, *n;
struct stack *stack;
struct value *v;
struct value copy;
u_long idx;
int reg;
reg = readreg();
if (reg >= 0) {
inumber = pop_number();
if (inumber == NULL)
return;
idx = get_ulong(inumber);
if (BN_is_negative(inumber->number))
warnx("negative idx");
else if (idx == ULONG_MAX || 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_zero(n->number);
push_number(n);
}
else
push(stack_dup_value(v, ©));
}
free_number(inumber);
}
}
static void
store_array(void)
{
struct number *inumber;
struct value *value;
struct stack *stack;
u_long idx;
int reg;
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);
if (BN_is_negative(inumber->number)) {
warnx("negative idx");
stack_free_value(value);
} else if (idx == ULONG_MAX || 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)
{
struct number *a, *b, *r;
a = pop_number();
if (a == NULL)
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)
{
struct number *a, *b, *r;
a = pop_number();
if (a == NULL)
return;
b = pop_number();
if (b == NULL) {
push_number(a);
return;