-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack.c
1620 lines (1524 loc) · 55.9 KB
/
stack.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
/*
** This file is part of the Matrix Brandy Basic VI Interpreter.
** Copyright (C) 2000-2014 David Daniels
** Copyright (C) 2018-2024 Michael McConnell and contributors
**
** Brandy is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** Brandy is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Brandy; see the file COPYING. If not, write to
** the Free Software Foundation, 59 Temple Place - Suite 330,
** Boston, MA 02111-1307, USA.
**
**
** This file contains various functions that are used to manipulate the
** Basic stack
*/
#include <string.h>
#include "common.h"
#include "target.h"
#include "basicdefs.h"
#include "stack.h"
#include "miscprocs.h"
#include "strings.h"
#include "tokens.h"
#include "errors.h"
#ifdef DEBUG
#include <stdio.h>
#endif
static void restore(int32 parmcount);
/*
** Stack overflow
** --------------
** The interpreter tries to be clever about checking for stack overflow.
** Whilst the functions that add control blocks to the stack, for example,
** for 'WHILE' statements, explicitly check for stack overflow, functions
** that add numeric and string values do not. The only time the code needs
** to check for overflow in these cases is when adding an extra value to
** the stack because it has found an operator of a higher priority than
** the last one it saw. There will be one entry on the Basic stack for each
** entry on the operator stack, so as the operator stack is of a fixed
** size, it is only necessary to check that the Basic stack will hold that
** many entries. The stack limit is also set a little way above the Basic
** heap so that the stack can be extended beyond that point (by about half
** a dozen entries) without causing any damage.
*/
/*
** 'entrysize' gives the size of each type of entry possible on the
** BASIC stack
*/
static int32 entrysize [] = { 0,
0, ALIGNSIZE(stack_uint8), /* 02 */
ALIGNSIZE(stack_int), ALIGNSIZE(stack_int64), /* 04 */
ALIGNSIZE(stack_float), ALIGNSIZE(stack_string), /* 06 */
ALIGNSIZE(stack_string), ALIGNSIZE(stack_array), /* 08 */
ALIGNSIZE(stack_arraytemp), ALIGNSIZE(stack_array), /* 0A */
ALIGNSIZE(stack_arraytemp), ALIGNSIZE(stack_array), /* 0C */
ALIGNSIZE(stack_arraytemp), ALIGNSIZE(stack_array), /* 0E */
ALIGNSIZE(stack_arraytemp), ALIGNSIZE(stack_array), /* 10 */
ALIGNSIZE(stack_arraytemp), ALIGNSIZE(stack_locarray), /* 12 */
ALIGNSIZE(stack_locarray), ALIGNSIZE(stack_gosub), /* 14 */
ALIGNSIZE(stack_proc), ALIGNSIZE(stack_fn), /* 16 */
ALIGNSIZE(stack_local), ALIGNSIZE(stack_retparm), /* 18 */
ALIGNSIZE(stack_while), ALIGNSIZE(stack_repeat), /* 1A */
ALIGNSIZE(stack_for), ALIGNSIZE(stack_for), /* 1C */
ALIGNSIZE(stack_for), ALIGNSIZE(stack_error), /* 1E */
ALIGNSIZE(stack_data), ALIGNSIZE(stack_opstack), /* 20 */
ALIGNSIZE(stack_restart) /* 21 */
};
/*
** This table says which types of entries can be simply discarded
** from the Basic stack
*/
static boolean disposable [] = {
FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE,
TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
TRUE, TRUE, TRUE
};
#ifdef DEBUG
static int32 entryLen = 64;
static char entry [64];
static char *entryname(stackitem what) {
switch (what) {
case STACK_UNKNOWN: return "<unknown>";
case STACK_LVALUE: return "lvalue";
case STACK_UINT8: return "uint8";
case STACK_INT: return "integer";
case STACK_INT64: return "int64";
case STACK_FLOAT: return "floating point";
case STACK_STRING: return "string";
case STACK_STRTEMP: return "temporary string";
case STACK_INTARRAY: return "ineger array";
case STACK_IATEMP: return "temp integer array";
case STACK_UINT8ARRAY: return "uint8 array";
case STACK_U8ATEMP: return "temp uint8 array";
case STACK_INT64ARRAY: return "int64 array";
case STACK_I64ATEMP: return "temp int64 array";
case STACK_FLOATARRAY: return "floating point array";
case STACK_FATEMP: return "temp floating point array";
case STACK_STRARRAY: return "string array";
case STACK_SATEMP: return "temp string array";
case STACK_LOCARRAY: return "local array";
case STACK_LOCSTRING: return "local string array";
case STACK_GOSUB: return "GOSUB";
case STACK_PROC: return "PROC";
case STACK_FN: return "FN";
case STACK_LOCAL: return "local variable";
case STACK_RETPARM: return "return parameter";
case STACK_WHILE: return "WHILE";
case STACK_REPEAT: return "REPEAT";
case STACK_INTFOR: return "integer FOR";
case STACK_INT64FOR: return "int64 FOR";
case STACK_FLOATFOR: return "floating point FOR";
case STACK_ERROR: return "ON ERROR";
case STACK_DATA: return "DATA";
case STACK_OPSTACK: return "operator stack";
case STACK_RESTART: return "siglongjmp block";
default:
snprintf(entry, entryLen, "** Bad type %X **", what);
return entry;
}
}
void dump(byte *sp) {
int m, *ip;
m = 4;
fprintf(stderr, "sp = %8p ", sp);
for (ip = (int *)(sp-32); ip < (int *)(sp+288); ip++) {
if (m == 4) fprintf(stderr, "\n%8p ", ip), m = 0;
fprintf(stderr, "%08x ", *ip);
m++;
}
fprintf(stderr, "\n");
}
#endif
/*
** 'make_opstack' is called to create a new operator stack. It also
** checks that there is enough room on the Basic stack to hold
** 'OPSTACKSIZE' numeric or string entries. It returns a pointer to
** the base of the stack
*/
size_t *make_opstack(void) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_opstack);
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "stack.c:make_opstack: stacktop=%p, stacklimit=%p, OPSTACKSIZE*LARGEST_ENTRY=%lX\n", basicvars.stacktop.bytesp, basicvars.stacklimit.bytesp, (long unsigned int)OPSTACKSIZE*LARGEST_ENTRY);
#endif
if (basicvars.stacktop.bytesp-OPSTACKSIZE*LARGEST_ENTRY<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return NULL;
}
basicvars.stacktop.opstacksp->itemtype = STACK_OPSTACK;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create operator stack at %p\n", basicvars.stacktop.bytesp);
#endif
return &basicvars.stacktop.opstacksp->opstack[0];
}
/*
** 'make_restart' creates an entry on the Basic stack for the
** environment block used by 'siglongjmp' when handling errors when
** an 'ON ERROR LOCAL' has been executed. It returns a pointer to
** the block for the siglongjmp's 'sigjmp_buf' structure
*/
sigjmp_buf *make_restart(void) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_restart);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return NULL;
}
basicvars.stacktop.restartsp->itemtype = STACK_RESTART;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create restart block at %p\n", basicvars.stacktop.bytesp);
#endif
return &basicvars.stacktop.restartsp->restart;
}
/*
** 'get_safestack' returns the value that the stack pointer is set
** to after an error to restore the stack to a known condition
*/
#if 0 /* Code disabled; only used in one place and it's a global struct variable */
byte *get_safestack(void) {
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Get safestack = %p\n", basicvars.safestack.bytesp);
#endif
return basicvars.safestack.bytesp;
}
#endif
/* Pushes an int of variable size, using the most appropriate type */
void push_varyint(int64 value) {
if (value == (uint8)value) {
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "push_varyint: Pushing %lld (&%llX) as uint8\n", value, value);
#endif
push_uint8((uint8)value);
} else if (value == (int32)value) {
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "push_varyint: Pushing %lld (&%llX) as int32\n", value, value);
#endif
push_int((int32)value);
} else {
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "push_varyint: Pushing %lld (&%llX) as int64\n", value, value);
#endif
push_int64(value);
}
}
/*
** 'push_int' pushes an integer value on to the Basic stack
*/
void push_int(int32 x) {
#ifdef DEBUG
byte *oldsp = basicvars.stacktop.bytesp;
#endif
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_int);
basicvars.stacktop.intsp->itemtype = STACK_INT;
basicvars.stacktop.intsp->intvalue = x;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push 32-bit integer value on to stack at %p (moved from %p), value %d\n", basicvars.stacktop.intsp, oldsp, x);
#endif
}
/*
** 'push_uint8' pushes an unsigned 8-bit integer value on to the Basic stack
*/
void push_uint8(uint8 x) {
#ifdef DEBUG
byte *oldsp = basicvars.stacktop.bytesp;
#endif
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_uint8);
basicvars.stacktop.uint8sp->itemtype = STACK_UINT8;
basicvars.stacktop.uint8sp->uint8value = x;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push unsigned 8-bit integer value on to stack at %p (moved from %p), value %d\n", basicvars.stacktop.uint8sp, oldsp, x);
#endif
}
/*
** 'push_int64' pushes a 64-bit integer value on to the Basic stack
*/
void push_int64(int64 x) {
#ifdef DEBUG
byte *oldsp = basicvars.stacktop.bytesp;
#endif
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_int64);
basicvars.stacktop.int64sp->itemtype = STACK_INT64;
basicvars.stacktop.int64sp->int64value = x;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push 64-bit integer value on to stack at %p (moved from %p), value %lld\n", basicvars.stacktop.int64sp, oldsp, x);
#endif
}
/*
** 'push_float' pushes a floating point value on to the Basic stack
*/
void push_float(float64 x) {
#ifdef DEBUG
byte *oldsp = basicvars.stacktop.bytesp;
#endif
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_float);
basicvars.stacktop.floatsp->itemtype = STACK_FLOAT;
basicvars.stacktop.floatsp->floatvalue = x;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push floating point value on to stack at %p (moved from %p), value %g\n", basicvars.stacktop.floatsp, oldsp, x);
#endif
}
/*
** 'push_string' copies a string descriptor on to the Basic stack
*/
void push_string(basicstring x) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_string);
basicvars.stacktop.stringsp->itemtype = STACK_STRING;
basicvars.stacktop.stringsp->descriptor = x;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push string value on to stack at %p, address %p, length %d\n",
basicvars.stacktop.stringsp, x.stringaddr, x.stringlen);
#endif
}
/*
** 'push_strtemp' creates a string descriptor on the Basic stack for an
** 'intermediate value' string, that is, a string created as a result of a
** string operation such as 'STRING$'.
*/
void push_strtemp(int32 stringlen, char *stringaddr) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_string);
basicvars.stacktop.stringsp->itemtype = STACK_STRTEMP;
basicvars.stacktop.stringsp->descriptor.stringlen = stringlen;
basicvars.stacktop.stringsp->descriptor.stringaddr = stringaddr;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push string temp on to stack at %p, address %p, length %d\n",
basicvars.stacktop.stringsp, stringaddr, stringlen);
#endif
}
/*
** 'push_dolstring' is called to push a reference to a '$<string>'
** type of string on to the Basic stack. It creates a descriptor
** for the string and copies that on to the stack
*/
void push_dolstring(int32 strlength, char *strtext) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_string);
basicvars.stacktop.stringsp->itemtype = STACK_STRING;
basicvars.stacktop.stringsp->descriptor.stringlen = strlength;
basicvars.stacktop.stringsp->descriptor.stringaddr = strtext;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push $<string> string on to stack at %p, address %p, length %d\n",
basicvars.stacktop.stringsp, strtext, strlength);
#endif
}
static stackitem arraytype [] = { /* Variable type -> array type */
STACK_UNKNOWN, STACK_UNKNOWN, STACK_INTARRAY, STACK_FLOATARRAY,
STACK_STRARRAY, STACK_UNKNOWN, STACK_INT64ARRAY, STACK_UINT8ARRAY
};
static stackitem arraytemptype [] = { /* Variable type -> temporary array type */
STACK_UNKNOWN, STACK_UNKNOWN, STACK_IATEMP, STACK_FATEMP,
STACK_SATEMP, STACK_UNKNOWN, STACK_I64ATEMP, STACK_U8ATEMP
};
/*
** 'push_array' pushes a pointer to an array descriptor on to the Basic stack
*/
void push_array(basicarray *descriptor, int32 type) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_array);
basicvars.stacktop.arraysp->itemtype = arraytype[type & TYPEMASK];
basicvars.stacktop.arraysp->descriptor = descriptor;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push array descriptor block at %p\n", basicvars.stacktop.arraysp);
#endif
}
/*
** 'push_arraytemp' pushes a descriptor for a temporary array on
** to the Basic stack. As this is a temporary array, the entire
** descriptor is copied on to the stack rather than just a pointer
** to it
*/
void push_arraytemp(basicarray *descriptor, int32 type) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_arraytemp);
basicvars.stacktop.arraytempsp->itemtype = arraytemptype[type & TYPEMASK];
basicvars.stacktop.arraytempsp->descriptor = *descriptor;
#ifdef DEBUG
if (basicvars.debug_flags.allstack) fprintf(stderr, "Push temp array descriptor block at %p\n", basicvars.stacktop.arraytempsp);
#endif
}
/*
** 'push_proc' pushes the return address and so forth for a procedure call
*/
void push_proc(char *name, int32 count) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_proc);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.procsp->itemtype = STACK_PROC;
basicvars.stacktop.procsp->fnprocblock.lastcall = basicvars.procstack;
basicvars.stacktop.procsp->fnprocblock.retaddr = basicvars.current;
basicvars.stacktop.procsp->fnprocblock.parmcount = count;
basicvars.stacktop.procsp->fnprocblock.fnprocname = name;
basicvars.procstack = &basicvars.stacktop.procsp->fnprocblock;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving PROC return block at %p\n", basicvars.stacktop.procsp);
#endif
}
/*
** 'push_fn' pushes the return address and so forth for a function call
*/
void push_fn(char *name, int32 count) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_fn);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.fnsp->itemtype = STACK_FN;
basicvars.stacktop.fnsp->lastopstop = basicvars.opstop;
basicvars.stacktop.fnsp->lastopstlimit = basicvars.opstlimit;
basicvars.stacktop.fnsp->lastrestart = basicvars.local_restart;
basicvars.stacktop.fnsp->fnprocblock.lastcall = basicvars.procstack;
basicvars.stacktop.fnsp->fnprocblock.retaddr = basicvars.current;
basicvars.stacktop.fnsp->fnprocblock.parmcount = count;
basicvars.stacktop.fnsp->fnprocblock.fnprocname = name;
basicvars.procstack = &basicvars.stacktop.fnsp->fnprocblock;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving FN return block at %p\n", basicvars.stacktop.fnsp);
#endif
}
/*
** 'push_gosub' pushes a 'GOSUB' return block on to the Basic stack
*/
void push_gosub(void) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_gosub);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.gosubsp->itemtype = STACK_GOSUB;
basicvars.stacktop.gosubsp->gosublock.lastcall = basicvars.gosubstack;
basicvars.stacktop.gosubsp->gosublock.retaddr = basicvars.current;
basicvars.gosubstack = &basicvars.stacktop.gosubsp->gosublock;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving GOSUB return block at %p\n", basicvars.stacktop.gosubsp);
#endif
}
/*
** 'alloc_stackmem' is called to allocate a block of memory on the
** Basic stack. It is used to acquire memory for local arrays. The
** space is automatically reclaimed when the procedure or function
** call ends. It returns a pointer to the area of memory allocated
** or NIL if there is not enough room for the block.
** **NOTE** It is up to the calling function to trap the error if
** this function returns NIL.
*/
void *alloc_stackmem(size_t size) {
byte *p, *base;
size = ALIGN(size);
base = basicvars.stacktop.bytesp-size;
p = base-ALIGNSIZE(stack_locarray);
if (p<basicvars.stacklimit.bytesp) return NIL; /* Bail out if there is no room */
basicvars.stacktop.bytesp = p; /* Reset the stack pointer */
basicvars.stacktop.locarraysp->itemtype = STACK_LOCARRAY;
basicvars.stacktop.locarraysp->arraysize = size;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Allocate memory on stack at %p, size=%lld\n", p, (int64)size);
#endif
return base;
}
/*
** 'alloc_stackstrmem' is called to allocate a block of memory on
** the Basic stack for a string array. It returns a pointer to the
** array or NIL if there was no memory available.
** **NOTE** It is up to the calling function to trap the error if
** this function returns NIL.
*/
void *alloc_stackstrmem(int32 size) {
void *p = alloc_stackmem(size);
if (p==NIL) return NIL;
basicvars.stacktop.locarraysp->itemtype = STACK_LOCSTRING;
return p;
}
/*
** 'free_stackmem' reclaims the stack space used for temporary array
*/
void free_stackmem(void) {
basicvars.stacktop.bytesp+=ALIGNSIZE(stack_locarray)+basicvars.stacktop.locarraysp->arraysize;
}
/*
** 'push_while' creates a control block on the Basic stack for a 'WHILE' loop
*/
void push_while(byte *expr) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_while);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.whilesp->itemtype = STACK_WHILE;
basicvars.stacktop.whilesp->whilexpr = expr;
basicvars.stacktop.whilesp->whileaddr = basicvars.current;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create 'WHILE' block at %p\n", basicvars.stacktop.whilesp);
#endif
}
/*
** 'push_repeat' creates a control block on the Basic stack for a 'REPEAT' loop
*/
void push_repeat(void) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_repeat);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.repeatsp->itemtype = STACK_REPEAT;
basicvars.stacktop.repeatsp->repeataddr = basicvars.current;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create 'REPEAT' block at %p\n", basicvars.stacktop.repeatsp);
#endif
}
/*
** 'push_intfor' creates a control block on the Basic stack for a 'FOR'
** loop with a 32-bit integer control variable
*/
void push_intfor(lvalue forvar, byte *foraddr, int32 limit, int32 step, boolean simple) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_for);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.forsp->itemtype = STACK_INTFOR;
basicvars.stacktop.forsp->simplefor = simple;
basicvars.stacktop.forsp->forvar = forvar;
basicvars.stacktop.forsp->foraddr = foraddr;
basicvars.stacktop.forsp->fortype.intfor.intlimit = limit;
basicvars.stacktop.forsp->fortype.intfor.intstep = step;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create integer 'FOR' block at %p\n", basicvars.stacktop.forsp);
#endif
}
/*
** 'push_int64for' creates a control block on the Basic stack for a 'FOR'
** loop with a 64-bit integer control variable
*/
void push_int64for(lvalue forvar, byte *foraddr, int64 limit, int64 step, boolean simple) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_for);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.forsp->itemtype = STACK_INT64FOR;
basicvars.stacktop.forsp->simplefor = simple;
basicvars.stacktop.forsp->forvar = forvar;
basicvars.stacktop.forsp->foraddr = foraddr;
basicvars.stacktop.forsp->fortype.int64for.int64limit = limit;
basicvars.stacktop.forsp->fortype.int64for.int64step = step;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create integer 'FOR' block at %p\n", basicvars.stacktop.forsp);
#endif
}
/*
** 'push_floatfor' creates a control block on the Basic stack for a 'FOR'
** loop with a floating point control variable
*/
void push_floatfor(lvalue forvar, byte *foraddr, float64 limit, float64 step, boolean simple) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_for);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.forsp->itemtype = STACK_FLOATFOR;
basicvars.stacktop.forsp->simplefor = simple;
basicvars.stacktop.forsp->forvar = forvar;
basicvars.stacktop.forsp->foraddr = foraddr;
basicvars.stacktop.forsp->fortype.floatfor.floatlimit = limit;
basicvars.stacktop.forsp->fortype.floatfor.floatstep = step;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create floating point 'FOR' block at %p\n", basicvars.stacktop.forsp);
#endif
}
/*
** 'push_data' is called to save the current value of the 'DATA'
** pointer on the Basic stack
*/
void push_data(byte *address) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_data);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.datasp->itemtype = STACK_DATA;
basicvars.stacktop.datasp->address = address;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create saved 'DATA' block at %p\n", basicvars.stacktop.datasp);
#endif
}
/*
** 'push_error' creates a control block on the stack for a Basic
** error handler
*/
void push_error(errorblock handler) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_error);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.errorsp->itemtype = STACK_ERROR;
basicvars.stacktop.errorsp->handler = handler;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Create saved 'ON ERROR' block at %p\n", basicvars.stacktop.errorsp);
#endif
}
/*
** 'save_int' saves an integer value on the stack. It is used when
** dealing with local variables
*/
void save_int(lvalue details, int32 value) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_local);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.localsp->itemtype = STACK_LOCAL;
basicvars.stacktop.localsp->savedetails = details;
basicvars.stacktop.localsp->value.savedint = value;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "LOCAL variable - saving 32-bit integer from %p at %p\n",
details.address.intaddr, basicvars.stacktop.localsp);
#endif
}
/*
** 'save_uint8' saves an integer value on the stack. It is used when
** dealing with local variables
*/
void save_uint8(lvalue details, uint8 value) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_local);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.localsp->itemtype = STACK_LOCAL;
basicvars.stacktop.localsp->savedetails = details;
basicvars.stacktop.localsp->value.saveduint8 = value;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "LOCAL variable - saving unsigned 8-bit integer from %p at %p with value &%X\n",
details.address.uint8addr, basicvars.stacktop.localsp, value);
#endif
}
/*
** 'save_int64' saves an integer value on the stack. It is used when
** dealing with local variables
*/
void save_int64(lvalue details, int64 value) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_local);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.localsp->itemtype = STACK_LOCAL;
basicvars.stacktop.localsp->savedetails = details;
basicvars.stacktop.localsp->value.savedint64 = value;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "LOCAL variable - saving 64-bit integer from %p at %p with value &%llX\n",
details.address.int64addr, basicvars.stacktop.localsp, value);
#endif
}
/*
** 'save_float' saves a floating point value on the stack. It is used when
** dealing with local variables
*/
void save_float(lvalue details, float64 floatvalue) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_local);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.localsp->itemtype = STACK_LOCAL;
basicvars.stacktop.localsp->savedetails = details;
basicvars.stacktop.localsp->value.savedfloat = floatvalue;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "LOCAL variable - saving floating point value from %p at %p\n",
details.address.floataddr, basicvars.stacktop.localsp);
#endif
}
/*
** 'save_string' saves a string descriptor on the stack. It is used when
** dealing with local variables.
** Note that the string descriptor is passed separately as the address
** given in 'details' as the home of the string descriptor is in fact the
** address of the string itself in the case of '$<string>' type strings.
** In this case the descriptor represents the place at which the string
** has been saved
*/
void save_string(lvalue details, basicstring thestring) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_local);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.localsp->itemtype = STACK_LOCAL;
basicvars.stacktop.localsp->savedetails = details;
basicvars.stacktop.localsp->value.savedstring = thestring;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "LOCAL variable - saving string from %p at %p\n",
details.address.straddr, basicvars.stacktop.localsp);
#endif
}
/*
** save_array is called to save an array descriptor on the stack when
* creating a local array
*/
void save_array(lvalue details) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_local);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.localsp->itemtype = STACK_LOCAL;
basicvars.stacktop.localsp->savedetails = details;
basicvars.stacktop.localsp->value.savedarray = *details.address.arrayaddr;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "LOCAL variable - saving array dimensions from %p at %p\n",
details.address.arrayaddr, basicvars.stacktop.localsp);
#endif
}
/*
** 'save_retint' is called to set up the control block on the stack for a
** 'RETURN' type PROC/FN parameter where the parameter is an integer.
** 'retaddr' details the place were the return value is to be saved,
** 'details' and 'value' refer to the variable that will be used for
** the value in the procedure
*/
void save_retint(lvalue retdetails, lvalue details, int32 value) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_retparm);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.retparmsp->itemtype = STACK_RETPARM;
basicvars.stacktop.retparmsp->retdetails = retdetails;
basicvars.stacktop.retparmsp->savedetails = details;
basicvars.stacktop.retparmsp->value.savedint = value;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving 32-bit integer variable from %p at %p\n",
details.address.intaddr, basicvars.stacktop.retparmsp);
#endif
}
/*
** 'save_retuint8' sets up the control block on the stack for a floating point
**'RETURN' type PROC/FN parameter
*/
void save_retuint8(lvalue retdetails, lvalue details, uint8 value) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_retparm);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.retparmsp->itemtype = STACK_RETPARM;
basicvars.stacktop.retparmsp->retdetails = retdetails;
basicvars.stacktop.retparmsp->savedetails = details;
basicvars.stacktop.retparmsp->value.saveduint8 = value;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving 64-bit integer variable from %p at %p\n",
details.address.intaddr, basicvars.stacktop.retparmsp);
#endif
}
/*
** 'save_retint64' sets up the control block on the stack for a floating point
**'RETURN' type PROC/FN parameter
*/
void save_retint64(lvalue retdetails, lvalue details, int64 value) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_retparm);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.retparmsp->itemtype = STACK_RETPARM;
basicvars.stacktop.retparmsp->retdetails = retdetails;
basicvars.stacktop.retparmsp->savedetails = details;
basicvars.stacktop.retparmsp->value.savedint64 = value;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving 64-bit integer variable from %p at %p\n",
details.address.intaddr, basicvars.stacktop.retparmsp);
#endif
}
/*
** 'save_retfloat' sets up the control block on the stack for a floating point
**'RETURN' type PROC/FN parameter
*/
void save_retfloat(lvalue retdetails, lvalue details, float64 floatvalue) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_retparm);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.retparmsp->itemtype = STACK_RETPARM;
basicvars.stacktop.retparmsp->retdetails = retdetails;
basicvars.stacktop.retparmsp->savedetails = details;
basicvars.stacktop.retparmsp->value.savedfloat = floatvalue;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving floating point variable from %p at %p\n",
details.address.floataddr, basicvars.stacktop.retparmsp);
#endif
}
/*
** 'save_retstring' sets up the control block on the Basic stack for a
** string 'RETURN' type PROC/FN parameter.
** Note that the string descriptor is passed separately as the address
** given in 'details' as the home of the string descriptor is in fact the
** address of the string itself in the case of '$<string>' type strings.
** In this case the descriptor represents the place at which the string
** has been saved
*/
void save_retstring(lvalue retdetails, lvalue details, basicstring thestring) {
basicvars.stacktop.bytesp-=ALIGNSIZE(stack_retparm);
if (basicvars.stacktop.bytesp<basicvars.stacklimit.bytesp) {
error(ERR_STACKFULL);
return;
}
basicvars.stacktop.retparmsp->itemtype = STACK_RETPARM;
basicvars.stacktop.retparmsp->retdetails = retdetails;
basicvars.stacktop.retparmsp->savedetails = details;
basicvars.stacktop.retparmsp->value.savedstring = thestring;
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Saving string variable from %p at %p\n",
details.address.straddr, basicvars.stacktop.retparmsp);
#endif
}
/*
** 'restore_retparm' is called when a 'return parameter' block is found on the
** stack. It saves the value currently in the parameter at the address stored as
** the return parameter address and then returns the local variable to its
** correct value
*/
static void restore_retparm(int32 parmcount) {
stack_retparm *p;
int32 vartype = 0, intvalue = 0;
float64 floatvalue = 0.0;
basicstring stringvalue = {0, NULL};
p = basicvars.stacktop.retparmsp; /* Not needed, but the code is unreadable otherwise */
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Restoring RETURN variable at %p from %p, return dest=%p\n",
p->savedetails.address.intaddr, p, p->retdetails.address.intaddr);
#endif
basicvars.stacktop.retparmsp++;
switch (p->savedetails.typeinfo & PARMTYPEMASK) { /* Fetch value from local variable and restore local var */
case VAR_INTWORD: /* Integer variable */
intvalue = *p->savedetails.address.intaddr; /* Fetch current value of local variable */
*p->savedetails.address.intaddr = p->value.savedint; /* Restore local variable to its old value */
vartype = VAR_INTWORD;
break;
case VAR_FLOAT: /* Floating point variable */
floatvalue = *p->savedetails.address.floataddr;
*p->savedetails.address.floataddr = p->value.savedfloat;
vartype = VAR_FLOAT;
break;
case VAR_STRINGDOL: /* String variable */
stringvalue = *p->savedetails.address.straddr;
*p->savedetails.address.straddr = p->value.savedstring;
vartype = VAR_STRINGDOL;
break;
case VAR_INTBYTEPTR: /* Indirect byte integer variable */
intvalue = basicvars.memory[p->savedetails.address.offset];
basicvars.memory[p->savedetails.address.offset] = p->value.savedint;
vartype = VAR_INTWORD;
break;
case VAR_INTWORDPTR: /* Indirect word integer variable */
intvalue = get_integer(p->savedetails.address.offset);
store_integer(p->savedetails.address.offset, p->value.savedint);
vartype = VAR_INTWORD;
break;
case VAR_FLOATPTR: /* Indirect floating point variable */
floatvalue = get_float(p->savedetails.address.offset);
store_float(p->savedetails.address.offset, p->value.savedfloat);
vartype = VAR_FLOAT;
break;
case VAR_DOLSTRPTR: /* Indirect string variable */
intvalue = stringvalue.stringlen = get_stringlen(p->savedetails.address.offset);
stringvalue.stringaddr = alloc_string(intvalue);
if (intvalue>0) memmove(stringvalue.stringaddr, &basicvars.memory[p->savedetails.address.offset], intvalue);
memmove(&basicvars.memory[p->savedetails.address.offset], p->value.savedstring.stringaddr, p->value.savedstring.stringlen);
free_string(p->value.savedstring); /* Discard saved copy of original '$ string' */
vartype = VAR_DOLSTRPTR;
break;
case VAR_INTARRAY: case VAR_INT64ARRAY: case VAR_UINT8ARRAY: case VAR_FLOATARRAY: case VAR_STRARRAY: /* Array - Do nothing */
break;
default:
#ifdef DEBUG
fprintf(stderr,"stack.c:restore_retparm: Oops. Trying to switch on value &%X didn't work\n",(p->savedetails.typeinfo & PARMTYPEMASK));
#endif
error(ERR_BROKEN, __LINE__, "stack");
return;
}
/* Now restore the next parameter */
parmcount--;
if (parmcount>0) { /* There are still some parameters to do */
if (basicvars.stacktop.intsp->itemtype==STACK_LOCAL)
restore(parmcount);
else { /* Must be a return parameter */
restore_retparm(parmcount);
}
}
/* Now we can store the returned value in original variable */
switch (p->retdetails.typeinfo) {
case VAR_INTWORD:
*p->retdetails.address.intaddr = vartype==VAR_INTWORD ? intvalue : TOINT(floatvalue);
break;
case VAR_FLOAT:
*p->retdetails.address.floataddr = vartype==VAR_INTWORD ? TOFLOAT(intvalue) : floatvalue;
break;
case VAR_STRINGDOL:
free_string(*p->retdetails.address.straddr);
*p->retdetails.address.straddr = stringvalue;
break;
case VAR_INTBYTEPTR:
basicvars.memory[p->retdetails.address.offset] = vartype==VAR_INTWORD ? intvalue : TOINT(floatvalue);
break;
case VAR_INTWORDPTR:
store_integer(p->retdetails.address.offset, vartype==VAR_INTWORD ? intvalue : TOINT(floatvalue));
break;
case VAR_FLOATPTR:
store_float(p->retdetails.address.offset, vartype==VAR_INTWORD ? TOFLOAT(intvalue) : floatvalue);
break;
case VAR_DOLSTRPTR:
if (stringvalue.stringlen>0) memmove(&basicvars.memory[p->retdetails.address.offset], stringvalue.stringaddr, stringvalue.stringlen);
if (vartype==VAR_STRINGDOL) { /* Local var was a normal string variable */
basicvars.memory[p->retdetails.address.offset+stringvalue.stringlen] = asc_CR; /* So add a 'CR' at the end of the string */
}
free_string(stringvalue);
break;
case VAR_INTARRAY: case VAR_FLOATARRAY: case VAR_STRARRAY: /* 'RETURN' dest is array - Do nothing */
break;
default:
error(ERR_BROKEN, __LINE__, "stack");
}
}
/*
** 'restore' is called to restore a variable to its saved value.
*/
static void restore(int32 parmcount) {
stack_local *p;
stackitem localitem;
do {
p = basicvars.stacktop.localsp;
basicvars.stacktop.bytesp+=ALIGNSIZE(stack_local);
#ifdef DEBUG
if (basicvars.debug_flags.stack) fprintf(stderr, "Restoring variable at %p from %p\n", p->savedetails.address.intaddr, p);
#endif
if (p->savedetails.typeinfo==VAR_INTWORD) /* Deal with most common case first */
*p->savedetails.address.intaddr = p->value.savedint;
else {
switch (p->savedetails.typeinfo & PARMTYPEMASK) {
case VAR_UINT8:
*p->savedetails.address.uint8addr = p->value.saveduint8;
break;
case VAR_INTLONG:
*p->savedetails.address.int64addr = p->value.savedint64;
break;
case VAR_FLOAT:
*p->savedetails.address.floataddr = p->value.savedfloat;
break;
case VAR_STRINGDOL:
free_string(*p->savedetails.address.straddr);
*p->savedetails.address.straddr = p->value.savedstring;
break;
case VAR_INTBYTEPTR:
basicvars.memory[p->savedetails.address.offset] = p->value.savedint;
break;
case VAR_INTWORDPTR:
store_integer(p->savedetails.address.offset, p->value.savedint);
break;
case VAR_FLOATPTR:
store_float(p->savedetails.address.offset, p->value.savedfloat);
break;
case VAR_DOLSTRPTR:
memmove(&basicvars.memory[p->savedetails.address.offset], p->value.savedstring.stringaddr, p->value.savedstring.stringlen);
free_string(p->value.savedstring);
break;
case VAR_INTARRAY: case VAR_INT64ARRAY: case VAR_UINT8ARRAY: case VAR_FLOATARRAY: case VAR_STRARRAY:
*p->savedetails.address.arrayaddr = p->value.savedarray;
break;
default:
error(ERR_BROKEN, __LINE__, "stack");
return;
}
}
/* Now restore the next parameter */