-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathopcodes.c
3965 lines (3514 loc) · 76.9 KB
/
opcodes.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
/*
* opcodes - opcode execution module
*
* Copyright (C) 1999-2004 David I. Bell and Ernest Bowen
*
* Primary author: David I. Bell
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc 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 Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Revision: 29.6 $
* @(#) $Id: opcodes.c,v 29.6 2004/02/23 14:04:01 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/RCS/opcodes.c,v $
*
* Under source code control: 1990/02/15 01:48:19
* File existed as early as: before 1990
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include <sys/types.h>
#include <setjmp.h>
#include "calc.h"
#include "opcodes.h"
#include "func.h"
#include "symbol.h"
/*#include "hist.h"
#include "file.h"
#include "zrand.h"
#include "zrandom.h"
#include "have_fpos.h"
#include "custom.h" -TL*/
#include "math_error.h"
/*#include "block.h"
#include "string.h" -TL*/
#include "have_unused.h"
#define QUICKLOCALS 20 /* local vars to handle quickly */
static VALUE stackarray[MAXSTACK]; /* storage for stack */
static VALUE oldvalue; /* previous calculation value */
static BOOL saveval = TRUE; /* to enable or disable saving */
static int calc_errno; /* most recent error-number */
static int errcount; /* counts calls to error_value */
static BOOL go;
static long calc_depth;
/*
* global symbols
*/
VALUE *stack; /* current location of top of stack */
int dumpnames; /* names if TRUE, otherwise indices */
char *funcname; /* function being executed */
long funcline; /* function line being executed */
/*
* forward declarations
*/
static void showsizes(void);
static void o_paramaddr(FUNC *fp, int argcnt, VALUE *args, long index);
static void o_getvalue(void);
/*
* Types of opcodes (depends on arguments saved after the opcode).
*/
#define OPNUL 1 /* opcode has no arguments */
#define OPONE 2 /* opcode has one integer argument */
#define OPTWO 3 /* opcode has two integer arguments */
#define OPJMP 4 /* opcode is a jump (with one pointer argument) */
#define OPRET 5 /* opcode is a return (with no argument) */
#define OPGLB 6 /* opcode has global symbol pointer argument */
#define OPPAR 7 /* opcode has parameter index argument */
#define OPLOC 8 /* opcode needs local variable pointer (with one arg) */
#define OPSTR 9 /* opcode has a string constant arg */
#define OPARG 10 /* opcode is given number of arguments */
#define OPSTI 11 /* opcode is static initialization */
/*
* opcode - info about each opcode
*/
struct opcode {
void (*o_func)(); /* routine to call for opcode */
int o_type; /* type of opcode */
char *o_name; /* name of opcode */
};
/*
* external configuration functions
*/
extern void config_value(CONFIG *cfg, int type, VALUE *ret);
extern void setconfig(int type, VALUE *vp);
/*
* Initialize the stack.
*/
void
initstack(void)
{
unsigned int i;
/* on first init, setup the stack array */
if (stack == NULL) {
for (i=0; i < sizeof(stackarray)/sizeof(stackarray[0]); ++i) {
stackarray[i].v_type = V_NULL;
stackarray[i].v_subtype = V_NOSUBTYPE;
}
stack = stackarray;
/* on subsequent inits, free the old stack */
} else {
while (stack > stackarray) {
freevalue(stack--);
}
}
/* initialize calc_depth */
calc_depth = 0;
}
/*
* The various opcodes
*/
static void
o_nop(void)
{
}
static void
o_localaddr(FUNC *fp, VALUE *locals, long index)
{
if ((unsigned long)index >= fp->f_localcount) {
math_error("Bad local variable index");
/*NOTREACHED*/
}
locals += index;
stack++;
stack->v_addr = locals;
stack->v_type = V_ADDR;
stack->v_subtype = V_NOSUBTYPE;
}
/*ARGSUSED*/
static void
o_globaladdr(FUNC UNUSED *fp, GLOBAL *sp)
{
if (sp == NULL) {
/* CID: 2211 */
//math_error("Global variable \"%s\" not initialized", sp->g_name);
/*NOTREACHED*/
}
stack++;
stack->v_addr = &sp->g_value;
stack->v_type = V_ADDR;
stack->v_subtype = V_NOSUBTYPE;
}
/*ARGSUSED*/
static void
o_paramaddr(FUNC UNUSED *fp, int argcount, VALUE *args, long index)
{
if ((long)index >= argcount) {
math_error("Bad parameter index");
/*NOTREACHED*/
}
args += index;
stack++;
if (args->v_type == V_OCTET || args->v_type == V_ADDR) {
*stack = *args;
return;
}
stack->v_addr = args;
stack->v_type = V_ADDR;
stack->v_subtype = V_NOSUBTYPE;
}
static void
o_localvalue(FUNC *fp, VALUE *locals, long index)
{
if ((unsigned long)index >= fp->f_localcount) {
math_error("Bad local variable index");
/*NOTREACHED*/
}
locals += index;
copyvalue(locals, ++stack);
}
/*ARGSUSED*/
static void
o_globalvalue(FUNC UNUSED *fp, GLOBAL *sp)
{
if (sp == NULL) {
math_error("Global variable not defined");
/*NOTREACHED*/
}
copyvalue(&sp->g_value, ++stack);
}
/*ARGSUSED*/
static void
o_paramvalue(FUNC UNUSED *fp, int argcount, VALUE *args, long index)
{
if ((long)index >= argcount) {
math_error("Bad parameter index");
/*NOTREACHED*/
}
args += index;
if (args->v_type == V_ADDR)
args = args->v_addr;
copyvalue(args, ++stack);
}
static void
o_argvalue(FUNC *fp, int argcount, VALUE *args)
{
VALUE *vp;
long index;
vp = stack;
if (vp->v_type == V_ADDR)
vp = vp->v_addr;
if ((vp->v_type != V_NUM) || qisneg(vp->v_num) ||
qisfrac(vp->v_num)) {
math_error("Illegal argument for arg function");
/*NOTREACHED*/
}
if (qiszero(vp->v_num)) {
if (stack->v_type == V_NUM)
qfree(stack->v_num);
stack->v_num = itoq((long) argcount);
stack->v_type = V_NUM;
stack->v_subtype = V_NOSUBTYPE;
return;
}
index = qtoi(vp->v_num) - 1;
if (stack->v_type == V_NUM)
qfree(stack->v_num);
stack--;
(void) o_paramaddr(fp, argcount, args, index);
}
/*ARGSUSED*/
static void
o_number(FUNC UNUSED *fp, long arg)
{
NUMBER *q;
q = constvalue(arg);
if (q == NULL) {
math_error("Numeric constant value not found");
/*NOTREACHED*/
}
stack++;
stack->v_num = qlink(q);
stack->v_type = V_NUM;
stack->v_subtype = V_NOSUBTYPE;
}
/*ARGSUSED*/
static void
o_imaginary(FUNC UNUSED *fp, long arg)
{
NUMBER *q;
COMPLEX *c;
q = constvalue(arg);
if (q == NULL) {
math_error("Numeric constant value not found");
/*NOTREACHED*/
}
stack++;
stack->v_subtype = V_NOSUBTYPE;
if (qiszero(q)) {
stack->v_num = qlink(q);
stack->v_type = V_NUM;
return;
}
c = comalloc();
qfree(c->imag);
c->imag = qlink(q);
stack->v_com = c;
stack->v_type = V_COM;
}
/*ARGSUSED*/
static void
o_string(FUNC UNUSED *fp, long arg)
{
stack++;
stack->v_str = slink(findstring(arg));
stack->v_type = V_STR;
stack->v_subtype = V_NOSUBTYPE;
}
static void
o_undef(void)
{
stack++;
stack->v_type = V_NULL;
stack->v_subtype = V_NOSUBTYPE;
}
/*ARGSUSED*/
static void
o_matcreate(FUNC UNUSED *fp, long dim)
{
register MATRIX *mp; /* matrix being defined */
NUMBER *num1; /* first number from stack */
NUMBER *num2; /* second number from stack */
VALUE *v1, *v2;
long min[MAXDIM]; /* minimum range */
long max[MAXDIM]; /* maximum range */
long i; /* index */
long tmp; /* temporary */
long size; /* size of matrix */
if ((dim < 0) || (dim > MAXDIM)) {
math_error("Bad dimension %ld for matrix", dim);
/*NOTREACHED*/
}
size = 1;
for (i = dim - 1; i >= 0; i--) {
v1 = &stack[-1];
v2 = &stack[0];
if (v1->v_type == V_ADDR)
v1 = v1->v_addr;
if (v2->v_type == V_ADDR)
v2 = v2->v_addr;
if ((v1->v_type != V_NUM) || (v2->v_type != V_NUM)) {
math_error("Non-numeric bounds for matrix");
/*NOTREACHED*/
}
num1 = v1->v_num;
num2 = v2->v_num;
if (qisfrac(num1) || qisfrac(num2)) {
math_error("Non-integral bounds for matrix");
/*NOTREACHED*/
}
if (zge31b(num1->num) || zge31b(num2->num)) {
math_error("Very large bounds for matrix");
/*NOTREACHED*/
}
min[i] = qtoi(num1);
max[i] = qtoi(num2);
if (min[i] > max[i]) {
tmp = min[i];
min[i] = max[i];
max[i] = tmp;
}
size *= (max[i] - min[i] + 1);
if (size > 10000000) {
math_error("Very large size for matrix");
/*NOTREACHED*/
}
freevalue(stack--);
freevalue(stack--);
}
mp = matalloc(size);
mp->m_dim = dim;
for (i = 0; i < dim; i++) {
mp->m_min[i] = min[i];
mp->m_max[i] = max[i];
}
stack++;
stack->v_type = V_MAT;
stack->v_subtype = V_NOSUBTYPE;
stack->v_mat = mp;
}
/*ARGSUSED*/
static void
o_eleminit(FUNC UNUSED *fp, long index)
{
VALUE *vp;
static VALUE *oldvp;
VALUE tmp;
OCTET *ptr;
BLOCK *blk;
int subtype;
vp = &stack[-1];
if (vp->v_type == V_ADDR)
vp = vp->v_addr;
switch (vp->v_type) {
case V_MAT:
if ((index < 0) || (index >= vp->v_mat->m_size)) {
math_error("Too many initializer values");
/*NOTREACHED*/
}
oldvp = &vp->v_mat->m_table[index];
break;
case V_OBJ:
if (index < 0 || index >= vp->v_obj->o_actions->oa_count) {
math_error("Too many initializer values");
/*NOTREACHED*/
}
oldvp = &vp->v_obj->o_table[index];
break;
case V_LIST:
oldvp = listfindex(vp->v_list, index);
if (oldvp == NULL) {
math_error("Too many initializer values");
/*NOTREACHED*/
}
break;
case V_STR:
if (index < 0 || index >= vp->v_str->s_len) {
math_error("Bad index for string initializing");
/*NOTREACHED*/
}
ptr = (OCTET *)(&vp->v_str->s_str[index]);
vp = stack;
if (vp->v_type == V_ADDR)
vp = vp->v_addr;
copy2octet(vp, ptr);
freevalue(stack--);
return;
case V_NBLOCK:
case V_BLOCK:
if (vp->v_type == V_NBLOCK) {
blk = vp->v_nblock->blk;
if (blk->data == NULL) {
math_error("Attempt to initialize freed block");
/*NOTREACHED*/
}
}
else
blk = vp->v_block;
if (index >= blk->maxsize) {
math_error("Too many initializer values");
/*NOTREACHED*/
}
ptr = blk->data + index;
vp = stack;
if (vp->v_type == V_ADDR)
vp = vp->v_addr;
copy2octet(vp, ptr);
if (index >= blk->datalen)
blk->datalen = index + 1;
freevalue(stack--);
return;
default:
math_error("Bad destination type for eleminit");
/*NOTREACHED*/
}
vp = stack--;
subtype = oldvp->v_subtype;
if (vp->v_type == V_ADDR) {
vp = vp->v_addr;
if (vp == oldvp)
return;
copyvalue(vp, &tmp);
}
else
tmp = *vp;
freevalue(oldvp);
*oldvp = tmp;
oldvp->v_subtype = subtype;
}
/*
* o_indexaddr
*
* given:
* fp function to calculate
* dim dimension of matrix
* writeflag nonzero if element will be written
*/
/*ARGSUSED*/
static void
o_indexaddr(FUNC UNUSED *fp, long dim, long writeflag)
{
int i;
BOOL flag;
VALUE *val;
VALUE *vp;
VALUE indices[MAXDIM]; /* index values */
long index; /* single dimension index for blocks */
VALUE ret; /* OCTET from as indexed from a block */
BLOCK *blk;
flag = (writeflag != 0);
if (dim < 0) {
math_error("Negative dimension for indexing");
/*NOTREACHED*/
}
val = &stack[-dim];
if (val->v_type != V_NBLOCK && val->v_type != V_FILE) {
if (val->v_type != V_ADDR) {
math_error("Non-pointer for indexaddr");
/*NOTREACHED*/
}
val = val->v_addr;
}
blk = NULL;
vp = &stack[-dim + 1];
for (i = 0; i < dim; i++) {
if (vp->v_type == V_ADDR)
indices[i] = vp->v_addr[0];
else
indices[i] = vp[0];
vp++;
}
switch (val->v_type) {
case V_MAT:
vp = matindex(val->v_mat, flag, dim, indices);
break;
case V_ASSOC:
vp = associndex(val->v_assoc, flag, dim, indices);
break;
case V_NBLOCK:
case V_BLOCK:
if (val->v_type == V_BLOCK)
blk = val->v_block;
else
blk = val->v_nblock->blk;
if (blk->data == NULL) {
math_error("Freed block");
/*NOTREACHED*/
}
/*
* obtain single dimensional block index
*/
if (dim != 1) {
math_error("block has only one dimension");
/*NOTREACHED*/
}
if (indices[0].v_type != V_NUM) {
math_error("Non-numeric index for block");
/*NOTREACHED*/
}
if (qisfrac(indices[0].v_num)) {
math_error("Non-integral index for block");
/*NOTREACHED*/
}
if (zge31b(indices[0].v_num->num) ||
zisneg(indices[0].v_num->num)) {
math_error("Index out of bounds for block");
/*NOTREACHED*/
}
index = ztoi(indices[0].v_num->num);
if (index >= blk->maxsize) {
math_error("Index out of bounds for block");
/*NOTREACHED*/
}
if (index >= blk->datalen)
blk->datalen = index + 1;
ret.v_type = V_OCTET;
ret.v_subtype = V_NOSUBTYPE;
ret.v_octet = &blk->data[index];
freevalue(stack--);
*stack = ret;
return;
case V_STR:
if (dim != 1) {
math_error("string has only one dimension");
/*NOTREACHED*/
}
if (indices[0].v_type != V_NUM) {
math_error("Non-numeric index for string");
/*NOTREACHED*/
}
if (qisfrac(indices[0].v_num)) {
math_error("Non-integral index for string");
/*NOTREACHED*/
}
if (zge31b(indices[0].v_num->num) ||
zisneg(indices[0].v_num->num)) {
math_error("Index out of bounds for string");
/*NOTREACHED*/
}
index = ztoi(indices[0].v_num->num);
if (index >= val->v_str->s_len) {
math_error("Index out of bounds for string");
/*NOTREACHED*/
}
ret.v_type = V_OCTET;
ret.v_subtype = V_NOSUBTYPE;
ret.v_octet = (OCTET *)(val->v_str->s_str + index);
freevalue(stack--);
*stack = ret;
return;
case V_LIST:
if (dim != 1) {
math_error("list has only one dimension");
/*NOTREACHED*/
}
if (indices[0].v_type != V_NUM) {
math_error("Non-numeric index for list");
/*NOTREACHED*/
}
if (qisfrac(indices[0].v_num)) {
math_error("Non-integral index for list");
/*NOTREACHED*/
}
if (zge31b(indices[0].v_num->num) ||
zisneg(indices[0].v_num->num)) {
math_error("Index out of bounds for list");
/*NOTREACHED*/
}
index = ztoi(indices[0].v_num->num);
vp = listfindex(val->v_list, index);
if (vp == NULL) {
math_error("Index out of bounds for list");
/*NOTREACHED*/
}
break;
default:
math_error("Illegal value for indexing");
/*NOTREACHED*/
}
while (dim-- > 0)
freevalue(stack--);
stack->v_type = V_ADDR;
stack->v_addr = vp;
}
/*ARGSUSED*/
static void
o_elemaddr(FUNC UNUSED *fp, long index)
{
VALUE *vp;
MATRIX *mp;
OBJECT *op;
int offset;
vp = stack;
if (vp->v_type == V_ADDR)
vp = stack->v_addr;
switch (vp->v_type) {
case V_MAT:
mp = vp->v_mat;
if ((index < 0) || (index >= mp->m_size)) {
math_error("Non-existent element for matrix");
/*NOTREACHED*/
}
vp = &mp->m_table[index];
break;
case V_OBJ:
op = vp->v_obj;
offset = objoffset(op, index);
if (offset < 0) {
math_error("Non-existent element for object");
/*NOTREACHED*/
}
vp = &op->o_table[offset];
break;
case V_LIST:
vp = listfindex(vp->v_list, index);
if (vp == NULL) {
math_error("Index out of bounds for list");
/*NOTREACHED*/
}
break;
default:
math_error("Not initializing matrix, object or list");
/*NOTREACHED*/
}
stack->v_type = V_ADDR;
stack->v_addr = vp;
}
static void
o_elemvalue(FUNC *fp, long index)
{
o_elemaddr(fp, index);
copyvalue(stack->v_addr, stack);
}
/*ARGSUSED*/
static void
o_objcreate(FUNC UNUSED *fp, long arg)
{
stack++;
stack->v_type = V_OBJ;
stack->v_subtype = V_NOSUBTYPE;
stack->v_obj = objalloc(arg);
}
static void
o_assign(void)
{
VALUE *var; /* variable value */
VALUE *vp;
VALUE tmp;
short subtype;
/*
* get what we will store into
*/
var = &stack[-1];
/*
* If what we will store into is an OCTET, we must
* handle this specially. Only the bottom 8 bits of
* certain value types will be assigned ... not the
* entire value.
*/
if (var->v_type == V_OCTET) {
if (var->v_subtype & V_NOCOPYTO) {
math_error("No-copy-to octet destination");
/*NOTREACHED*/
}
copy2octet(stack, var->v_octet);
freevalue(stack--);
return;
}
if (var->v_type != V_ADDR) {
math_error("Assignment into non-variable");
/*NOTREACHED*/
}
var = var->v_addr;
subtype = var->v_subtype;
if (subtype & V_NOASSIGNTO) {
math_error("No-assign-to destination for assign");
/*NOTREACHED*/
}
vp = stack;
if (var->v_type == V_OBJ) {
if (vp->v_type == V_ADDR)
vp = vp->v_addr;
(void) objcall(OBJ_ASSIGN, var, vp, NULL_VALUE);
freevalue(stack--);
return;
}
stack--;
/*
* Get what we will store from
* If what will store from is an address, make a copy
* of the de-referenced address instead.
*/
if (vp->v_type == V_ADDR) {
vp = vp->v_addr;
if (vp == var)
return;
if (vp->v_subtype & V_NOASSIGNFROM) {
math_error("No-assign-from source for assign");
/*NOTREACHED*/
}
tmp.v_subtype = V_NOSUBTYPE;
copyvalue(vp, &tmp);
} else if (vp->v_type == V_OCTET) {
tmp.v_subtype = V_NOSUBTYPE;
copyvalue(vp, &tmp);
} else {
tmp = *vp;
}
/*
* perform the assignment
*/
if ((subtype & V_NONEWVALUE) && comparevalue(var, &tmp)) {
freevalue(&tmp);
math_error("Change of value in assign not permitted");
/*NOTREACHED*/
}
if ((subtype & V_NONEWTYPE) && var->v_type != tmp.v_type) {
freevalue(&tmp);
math_error("Change of type in assign not permitted");
/*NOTREACHED*/
}
if ((subtype & V_NOERROR) && tmp.v_type < 0) {
math_error("Error value in assign not permitted");
/*NOTREACHED*/
}
freevalue(var);
*var = tmp;
var->v_subtype = subtype;
}
static void
o_assignback(void)
{
VALUE tmp;
tmp = stack[-1];
stack[-1] = stack[0];
stack[0] = tmp;
o_assign();
}
static void
o_assignpop(void)
{
o_assign();
stack--;
}
static void
o_ptr(void)
{
switch (stack->v_type) {
case V_ADDR:
stack->v_type = V_VPTR;
break;
case V_OCTET:
stack->v_type = V_OPTR;
break;
case V_STR:
sfree(stack->v_str);
stack->v_type = V_SPTR;
break;
case V_NUM:
qfree(stack->v_num);
stack->v_type = V_NPTR;
break;
default:
math_error("Addressing non-addressable type");
/*NOTREACHED*/
}
}
static void
o_deref(void)
{
VALUE *vp;
short subtype;
vp = stack;
subtype = stack->v_subtype;
if (stack->v_type == V_OCTET) {
stack->v_num = itoq(*vp->v_octet);
stack->v_type = V_NUM;
return;
}
if (stack->v_type == V_OPTR) {
stack->v_type = V_OCTET;
return;
}
if (stack->v_type == V_VPTR) {
stack->v_type = V_ADDR;
return;
}
if (stack->v_type == V_SPTR) {
stack->v_type = V_STR;
return;
}
if (stack->v_type == V_NPTR) {
if (stack->v_num->links == 0) {
stack->v_type = V_NULL;
return;
}
stack->v_type = V_NUM;
stack->v_num->links++;
return;
}
if (stack->v_type != V_ADDR) {
math_error("Dereferencing a non-variable");
/*NOTREACHED*/
}
vp = vp->v_addr;
switch (vp->v_type) {
case V_ADDR:
case V_OCTET:
*stack = *vp;
break;
case V_OPTR:
*stack = *vp;
stack->v_type = V_OCTET;
break;
case V_VPTR:
*stack = *vp;
stack->v_type = V_ADDR;
break;
case V_SPTR:
*stack = *vp;
stack->v_type = V_STR;
break;
case V_NPTR:
if (vp->v_num->links == 0) {
stack->v_type = V_NULL;
break;
}
stack->v_type = V_NUM;
stack->v_num = vp->v_num;
stack->v_num->links++;
break;
default:
copyvalue(vp, stack);
}
stack->v_subtype = subtype;
}
static void
o_swap(void)
{
VALUE *v1, *v2; /* variables to be swapped */
VALUE tmp;
USB8 usb;
short s1, s2; /* for subtypes */
v1 = stack--;
v2 = stack;
if (v1->v_type == V_OCTET && v2->v_type == V_OCTET) {
usb = *v1->v_octet;
*v1->v_octet = *v2->v_octet;
*v2->v_octet = usb;
} else if (v1->v_type == V_ADDR && v2->v_type == V_ADDR) {
v1 = v1->v_addr;
v2 = v2->v_addr;
s1 = v1->v_subtype;
s2 = v2->v_subtype;
if ((s1 | s2) & (V_NOASSIGNTO | V_NOASSIGNFROM)) {
math_error("Swap not permitted by protection levels");
/*NOTREACHED*/
}
tmp = *v1;
*v1 = *v2;
*v2 = tmp;
v1->v_subtype = s1;
v2->v_subtype = s2;
} else {
math_error("Swapping values of non-variables");
/*NOTREACHED*/
}
stack->v_type = V_NULL;
stack->v_subtype = V_NOSUBTYPE;
}
static void
o_add(void)
{
VALUE *v1, *v2;
VALUE tmp;
VALUE w1, w2;
v1 = &stack[-1];
v2 = &stack[0];
if (v1->v_type == V_ADDR)
v1 = v1->v_addr;
if (v2->v_type == V_ADDR)
v2 = v2->v_addr;
if (v1->v_type == V_OCTET) {
w1.v_type = V_NUM;
w1.v_subtype = V_NOSUBTYPE;
w1.v_num = itoq(*v1->v_octet);
v1 = &w1;
}
if (v2->v_type == V_OCTET) {
w2.v_type = V_NUM;
w2.v_subtype = V_NOSUBTYPE;
w2.v_num = itoq(*v2->v_octet);
v2 = &w2;