-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathinterpret.c
5633 lines (5140 loc) · 139 KB
/
interpret.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
#define SUPPRESS_COMPILER_INLINES
#include "std.h"
#include "lpc_incl.h"
#include "efuns_incl.h"
#include "file.h"
#include "file_incl.h"
#include "patchlevel.h"
#include "backend.h"
#include "simul_efun.h"
#include "eoperators.h"
#include "efunctions.h"
#include "sprintf.h"
#include "swap.h"
#include "comm.h"
#include "port.h"
#include "qsort.h"
#include "compiler.h"
#include "regexp.h"
#include "master.h"
#ifdef OPCPROF
#include "opc.h"
static int opc_eoper[BASE];
#endif
#ifdef OPCPROF_2D
/* warning, this is typically 4 * 100 * 100 = 40k */
static int opc_eoper_2d[BASE+1][BASE+1];
static int last_eop = 0;
#endif
static char *type_names[] = {
"int",
"string",
"array",
"object",
"mapping",
"function",
"float",
"buffer",
"class"
};
#define TYPE_CODES_END 0x400
#define TYPE_CODES_START 0x2
#ifdef PACKAGE_UIDS
extern userid_t *backbone_uid;
#endif
extern int max_cost;
extern int call_origin;
INLINE void push_indexed_lvalue PROT((int));
#ifdef TRACE
static void do_trace_call PROT((int));
#endif
void break_point PROT((void));
INLINE_STATIC void do_loop_cond_number PROT((void));
INLINE_STATIC void do_loop_cond_local PROT((void));
static void do_catch PROT((char *, unsigned short));
#ifdef DEBUG
int last_instructions PROT((void));
#endif
static float _strtof PROT((char *, char **));
#ifdef TRACE_CODE
static char *get_arg PROT((int, int));
#endif
#ifdef DEBUG
int stack_in_use_as_temporary = 0;
#endif
int inter_sscanf PROT((svalue_t *, svalue_t *, svalue_t *, int));
program_t *current_prog;
short int caller_type;
static int tracedepth;
int num_varargs;
/*
* Inheritance:
* An object X can inherit from another object Y. This is done with
* the statement 'inherit "file";'
* The inherit statement will clone a copy of that file, call reset
* in it, and set a pointer to Y from X.
* Y has to be removed from the linked list of all objects.
* All variables declared by Y will be copied to X, so that X has access
* to them.
*
* If Y isn't loaded when it is needed, X will be discarded, and Y will be
* loaded separately. X will then be reloaded again.
*/
/*
* These are the registers used at runtime.
* The control stack saves registers to be restored when a function
* will return. That means that control_stack[0] will have almost no
* interesting values, as it will terminate execution.
*/
char *pc; /* Program pointer. */
svalue_t *fp; /* Pointer to first argument. */
svalue_t *sp;
svalue_t const0, const1, const0u;
int function_index_offset; /* Needed for inheritance */
int variable_index_offset; /* Needed for inheritance */
int st_num_arg;
static svalue_t start_of_stack[CFG_EVALUATOR_STACK_SIZE];
svalue_t *end_of_stack = start_of_stack + CFG_EVALUATOR_STACK_SIZE - 5;
/* Used to throw an error to a catch */
svalue_t catch_value = {T_NUMBER};
/* used by routines that want to return a pointer to an svalue */
svalue_t apply_ret_value = {T_NUMBER};
control_stack_t control_stack[CFG_MAX_CALL_DEPTH];
control_stack_t *csp; /* Points to last element pushed */
int too_deep_error = 0, max_eval_error = 0;
ref_t *global_ref_list = 0;
void kill_ref P1(ref_t *, ref) {
if (ref->sv.type == T_MAPPING && (ref->sv.u.map->count & MAP_LOCKED)) {
ref_t *r = global_ref_list;
/* if some other ref references this mapping, it needs to remain
locked */
while (r) {
if (r->sv.u.map == ref->sv.u.map)
break;
r = r->next;
}
if (!r)
unlock_mapping(ref->sv.u.map);
}
free_svalue(&ref->sv, "kill_ref");
if (ref->next)
ref->next->prev = ref->prev;
if (ref->prev)
ref->prev->next = ref->next;
else
global_ref_list = ref->next;
if (ref->ref > 0) {
/* still referenced */
ref->lvalue = 0;
} else {
FREE(ref);
}
}
ref_t *make_ref PROT((void)) {
ref_t *ref = ALLOCATE(ref_t, TAG_TEMPORARY, "make_ref");
ref->next = global_ref_list;
ref->prev = NULL;
if (ref->next)
ref->next->prev = ref;
global_ref_list = ref;
ref->csp = csp;
ref->ref = 1;
return ref;
}
void get_version P1(char *, buff)
{
sprintf(buff, "MudOS %s", PATCH_LEVEL);
}
/*
* Information about assignments of values:
*
* There are three types of l-values: Local variables, global variables
* and array elements.
*
* The local variables are allocated on the stack together with the arguments.
* the register 'frame_pointer' points to the first argument.
*
* The global variables must keep their values between executions, and
* have space allocated at the creation of the object.
*
* Elements in arrays are similar to global variables. There is a reference
* count to the whole array, that states when to deallocate the array.
* The elements consists of 'svalue_t's, and will thus have to be freed
* immediately when over written.
*/
/*
* Push an object pointer on the stack. Note that the reference count is
* incremented.
* A destructed object must never be pushed onto the stack.
*/
INLINE
void push_object P1(object_t *, ob)
{
STACK_INC;
if (!ob || (ob->flags & O_DESTRUCTED)) {
*sp = const0u;
return;
}
sp->type = T_OBJECT;
sp->u.ob = ob;
add_ref(ob, "push_object");
}
char * type_name P1(int, c) {
int j = 0;
int limit = TYPE_CODES_START;
do {
if (c & limit) return type_names[j];
j++;
} while (!((limit <<= 1) & TYPE_CODES_END));
/* Oh crap. Take some time and figure out what we have. */
switch (c) {
case T_INVALID: return "*invalid*";
case T_LVALUE: return "*lvalue*";
case T_REF: return "*ref*";
case T_LVALUE_BYTE: return "*lvalue_byte*";
case T_LVALUE_RANGE: return "*lvalue_range*";
case T_ERROR_HANDLER: return "*error_handler*";
IF_DEBUG(case T_FREED: return "*freed*");
}
return "*unknown*";
}
/*
* May current_object shadow object 'ob' ? We rely heavily on the fact that
* function names are pointers to shared strings, which means that equality
* can be tested simply through pointer comparison.
*/
static program_t *ffbn_recurse PROT((program_t *, char *, int *, int *));
static program_t *ffbn_recurse2 PROT((program_t *, char *, int *, int *, int *, int *));
#ifndef NO_SHADOWS
static char *check_shadow_functions P2(program_t *, shadow, program_t *, victim) {
int i;
int index, runtime_index;
program_t *prog;
for (i = 0; i < shadow->num_functions_defined; i++) {
prog = ffbn_recurse(victim, shadow->function_table[i].name, &index, &runtime_index);
if (prog && (victim->function_flags[runtime_index] & DECL_NOMASK))
return prog->function_table[index].name;
}
return 0;
}
int validate_shadowing P1(object_t *, ob)
{
program_t *shadow = current_object->prog, *victim = ob->prog;
svalue_t *ret;
char *fun;
if (current_object->shadowing)
error("shadow: Already shadowing.\n");
if (current_object->shadowed)
error("shadow: Can't shadow when shadowed.\n");
#ifndef NO_ENVIRONMENT
if (current_object->super)
error("shadow: The shadow must not reside inside another object.\n");
#endif
if (ob == master_ob)
error("shadow: cannot shadow the master object.\n");
if (ob->shadowing)
error("shadow: Can't shadow a shadow.\n");
if ((fun = check_shadow_functions(shadow, victim)))
error("Illegal to shadow 'nomask' function \"%s\".\n", fun);
push_object(ob);
ret = apply_master_ob(APPLY_VALID_SHADOW, 1);
if (!(ob->flags & O_DESTRUCTED) && MASTER_APPROVED(ret)) {
return 1;
}
return 0;
}
#endif
/*
* Push a number on the value stack.
*/
INLINE void
push_number P1(int, n)
{
STACK_INC;
sp->type = T_NUMBER;
sp->subtype = 0;
sp->u.number = n;
}
INLINE void
push_real P1(double, n)
{
STACK_INC;
sp->type = T_REAL;
sp->u.real = n;
}
/*
* Push undefined (const0u) onto the value stack.
*/
INLINE
void push_undefined()
{
STACK_INC;
*sp = const0u;
}
INLINE_STATIC void push_undefineds P1(int, num)
{
CHECK_STACK_OVERFLOW(num);
while (num--) *++sp = const0u;
}
INLINE
void copy_and_push_string P1(char *, p) {
STACK_INC;
sp->type = T_STRING;
sp->subtype = STRING_MALLOC;
sp->u.string = string_copy(p, "copy_and_push_string");
}
INLINE
void share_and_push_string P1(char *, p) {
STACK_INC;
sp->type = T_STRING;
sp->subtype = STRING_SHARED;
sp->u.string = make_shared_string(p);
}
/*
* Get address to a valid global variable.
*/
#ifdef DEBUG
INLINE_STATIC svalue_t *find_value P1(int, num)
{
DEBUG_CHECK2(num >= (int) current_object->prog->num_variables_total,
"Illegal variable access %d(%d).\n",
num, current_object->prog->num_variables_total);
return ¤t_object->variables[num];
}
#else
#define find_value(num) (¤t_object->variables[num])
#endif
INLINE void
free_string_svalue P1(svalue_t *, v)
{
char *str = v->u.string;
if (v->subtype & STRING_COUNTED) {
#ifdef STRING_STATS
int size = MSTR_SIZE(str);
#endif
if (DEC_COUNTED_REF(str)) {
SUB_STRING(size);
NDBG(BLOCK(str));
if (v->subtype & STRING_HASHED) {
SUB_NEW_STRING(size, sizeof(block_t));
deallocate_string(str);
CHECK_STRING_STATS;
} else {
SUB_NEW_STRING(size, sizeof(malloc_block_t));
FREE(MSTR_BLOCK(str));
CHECK_STRING_STATS;
}
} else {
SUB_STRING(size);
NDBG(BLOCK(str));
}
}
}
void unlink_string_svalue P1(svalue_t *, s) {
char *str;
switch (s->subtype) {
case STRING_MALLOC:
if (MSTR_REF(s->u.string) > 1)
s->u.string = string_unlink(s->u.string, "unlink_string_svalue");
break;
case STRING_SHARED:
{
int l = SHARED_STRLEN(s->u.string);
str = new_string(l, "unlink_string_svalue");
strncpy(str, s->u.string, l + 1);
free_string(s->u.string);
s->subtype = STRING_MALLOC;
s->u.string = str;
break;
}
case STRING_CONSTANT:
s->u.string = string_copy(s->u.string, "unlink_string_svalue");
s->subtype = STRING_MALLOC;
break;
}
}
/*
* Free the data that an svalue is pointing to. Not the svalue
* itself.
* Use the free_svalue() define to call this
*/
#ifdef DEBUG
INLINE void int_free_svalue P2(svalue_t *, v, char *, tag)
#else
INLINE void int_free_svalue P1(svalue_t *, v)
#endif
{
/* Marius, 30-Mar-2001: T_FREED could be OR'd in with the type now if the
* svalue has been 'freed' as an optimization by the F_TRANSFER_LOCAL op.
* This will allow us to keep the type of the variable known for error
* handler purposes but not duplicate the free.
*/
if (v->type == T_STRING) {
char *str = v->u.string;
if (v->subtype & STRING_COUNTED) {
#ifdef STRING_STATS
int size = MSTR_SIZE(str);
#endif
if (DEC_COUNTED_REF(str)) {
SUB_STRING(size);
NDBG(BLOCK(str));
if (v->subtype & STRING_HASHED) {
SUB_NEW_STRING(size, sizeof(block_t));
deallocate_string(str);
CHECK_STRING_STATS;
} else {
SUB_NEW_STRING(size, sizeof(malloc_block_t));
FREE(MSTR_BLOCK(str));
CHECK_STRING_STATS;
}
} else {
SUB_STRING(size);
NDBG(BLOCK(str));
}
}
} else if ((v->type & T_REFED) && !(v->type & T_FREED)) {
#ifdef DEBUG_MACRO
if (v->type == T_OBJECT)
debug(d_flag, ("Free_svalue %s (%d) from %s\n", v->u.ob->name, v->u.ob->ref - 1, tag));
#endif
if (!(--v->u.refed->ref)) {
switch (v->type) {
case T_OBJECT:
dealloc_object(v->u.ob, "free_svalue");
break;
case T_CLASS:
dealloc_class(v->u.arr);
break;
case T_ARRAY:
if (v->u.arr != &the_null_array)
dealloc_array(v->u.arr);
break;
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
if (v->u.buf != &null_buf)
FREE((char *)v->u.buf);
break;
#endif
case T_MAPPING:
dealloc_mapping(v->u.map);
break;
case T_FUNCTION:
dealloc_funp(v->u.fp);
break;
case T_REF:
if (!v->u.ref->lvalue)
kill_ref(v->u.ref);
break;
}
}
} else if (v->type == T_ERROR_HANDLER) {
(*v->u.error_handler)();
}
#ifdef DEBUG
else if (v->type == T_FREED) {
fatal("T_FREED svalue freed. Previously freed by %s.\n", v->u.string);
}
v->type = T_FREED;
v->u.string = tag;
#endif
}
void process_efun_callback P3(int, narg, function_to_call_t *, ftc, int, f) {
int argc = st_num_arg;
svalue_t *arg = sp - argc + 1 + narg;
if (arg->type == T_FUNCTION) {
ftc->f.fp = arg->u.fp;
ftc->ob = 0;
ftc->narg = argc - narg - 1;
ftc->args = arg + 1;
} else {
ftc->f.str = arg->u.string;
if (argc < narg + 2) {
ftc->ob = current_object;
ftc->narg = 0;
} else {
if ((arg+1)->type == T_OBJECT) {
ftc->ob = (arg+1)->u.ob;
} else
if ((arg+1)->type == T_STRING) {
if (!(ftc->ob = find_object((arg+1)->u.string)) ||
!object_visible(ftc->ob))
bad_argument(arg+1, T_STRING | T_OBJECT, 3, f);
} else
bad_argument(arg+1, T_STRING | T_OBJECT, 3, f);
ftc->narg = argc - narg - 2;
ftc->args = arg + 2;
if (ftc->ob->flags & O_DESTRUCTED)
bad_argument(arg+1, T_STRING | T_OBJECT, 3, f);
}
}
}
svalue_t *call_efun_callback P2(function_to_call_t *, ftc, int, n) {
svalue_t *v;
if (ftc->narg)
push_some_svalues(ftc->args, ftc->narg);
if (ftc->ob) {
if (ftc->ob->flags & O_DESTRUCTED)
error("Object destructed during efun callback.\n");
v = apply(ftc->f.str, ftc->ob, n + ftc->narg, ORIGIN_EFUN);
} else
v = call_function_pointer(ftc->f.fp, n + ftc->narg);
return v;
}
/*
* Free several svalues, and free up the space used by the svalues.
* The svalues must be sequentially located.
*/
INLINE void free_some_svalues P2(svalue_t *, v, int, num)
{
while (num--)
free_svalue(v + num, "free_some_svalues");
FREE(v);
}
/*
* Prepend a slash in front of a string.
*/
char *add_slash P1(char *, str)
{
char *tmp;
if (str[0] == '<' && strcmp(str + 1, "function>") == 0)
return string_copy(str, "add_slash");
tmp = new_string(strlen(str) + 1, "add_slash");
*tmp = '/';
strcpy(tmp + 1, str);
return tmp;
}
/*
* Assign to a svalue.
* This is done either when element in array, or when to an identifier
* (as all identifiers are kept in a array pointed to by the object).
*/
INLINE void assign_svalue_no_free P2(svalue_t *, to, svalue_t *, from)
{
DEBUG_CHECK(from == 0, "Attempt to assign_svalue() from a null ptr.\n");
DEBUG_CHECK(to == 0, "Attempt to assign_svalue() to a null ptr.\n");
DEBUG_CHECK((from->type & (from->type - 1)) & ~T_FREED, "from->type is corrupt; >1 bit set.\n");
if (from->type == T_OBJECT && (!from->u.ob || (from->u.ob->flags & O_DESTRUCTED))) {
*to = const0u;
return;
}
*to = *from;
if ((to->type & T_FREED) && to->type != T_FREED)
to->type &= ~T_FREED;
if (from->type == T_STRING) {
if (from->subtype & STRING_COUNTED) {
INC_COUNTED_REF(to->u.string);
ADD_STRING(MSTR_SIZE(to->u.string));
NDBG(BLOCK(to->u.string));
}
} else if (from->type & T_REFED) {
#ifdef DEBUG_MACRO
if (from->type == T_OBJECT)
add_ref(from->u.ob, "assign_svalue_no_free");
else
#endif
from->u.refed->ref++;
}
}
INLINE void assign_svalue P2(svalue_t *, dest, svalue_t *, v)
{
/* First deallocate the previous value. */
free_svalue(dest, "assign_svalue");
assign_svalue_no_free(dest, v);
}
INLINE void push_some_svalues P2(svalue_t *, v, int, num)
{
while (num--) push_svalue(v++);
}
/*
* Copies an array of svalues to another location, which should be
* free space.
*/
INLINE void copy_some_svalues P3(svalue_t *, dest, svalue_t *, v, int, num)
{
while (num--)
assign_svalue_no_free(dest+num, v+num);
}
INLINE void transfer_push_some_svalues P2(svalue_t *, v, int, num)
{
CHECK_STACK_OVERFLOW(num);
memcpy(sp + 1, v, num * sizeof(svalue_t));
sp += num;
}
/*
* Pop the top-most value of the stack.
* Don't do this if it is a value that will be used afterwards, as the
* data may be sent to FREE(), and destroyed.
*/
INLINE void pop_stack()
{
DEBUG_CHECK(sp < start_of_stack, "Stack underflow.\n");
free_svalue(sp--, "pop_stack");
}
svalue_t global_lvalue_byte = { T_LVALUE_BYTE };
int lv_owner_type;
refed_t *lv_owner;
/*
* Compute the address of an array element.
*/
INLINE void push_indexed_lvalue P1(int, code)
{
int ind;
svalue_t *lv;
if (sp->type == T_LVALUE) {
lv = sp->u.lvalue;
if (!code && lv->type == T_MAPPING) {
sp--;
if (!(lv = find_for_insert(lv->u.map, sp, 0)))
mapping_too_large();
free_svalue(sp, "push_indexed_lvalue: 1");
sp->type = T_LVALUE;
sp->u.lvalue = lv;
#ifdef REF_RESERVED_WORD
lv_owner_type = T_MAPPING;
lv_owner = (refed_t *)lv->u.map;
#endif
return;
}
if (!((--sp)->type == T_NUMBER))
error("Illegal type of index\n");
ind = sp->u.number;
switch(lv->type) {
case T_STRING:
{
int len = SVALUE_STRLEN(lv);
if (code) ind = len - ind;
if (ind >= len || ind < 0)
error("Index out of bounds in string index lvalue.\n");
unlink_string_svalue(lv);
sp->type = T_LVALUE;
sp->u.lvalue = &global_lvalue_byte;
global_lvalue_byte.subtype = 0;
global_lvalue_byte.u.lvalue_byte = (unsigned char *)&lv->u.string[ind];
#ifdef REF_RESERVED_WORD
lv_owner_type = T_STRING;
lv_owner = (refed_t *)lv->u.string;
#endif
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
if (code) ind = lv->u.buf->size - ind;
if (ind >= lv->u.buf->size || ind < 0)
error("Buffer index out of bounds.\n");
sp->type = T_LVALUE;
sp->u.lvalue = &global_lvalue_byte;
global_lvalue_byte.subtype = 1;
global_lvalue_byte.u.lvalue_byte = &lv->u.buf->item[ind];
#ifdef REF_RESERVED_WORD
lv_owner_type = T_BUFFER;
lv_owner = (refed_t *)lv->u.buf;
#endif
break;
}
#endif
case T_ARRAY:
{
if (code) ind = lv->u.arr->size - ind;
if (ind >= lv->u.arr->size || ind < 0)
error("Array index out of bounds\n");
sp->type = T_LVALUE;
sp->u.lvalue = lv->u.arr->item + ind;
#ifdef REF_RESERVED_WORD
lv_owner_type = T_ARRAY;
lv_owner = (refed_t *)lv->u.arr;
#endif
break;
}
default:
if (lv->type == T_NUMBER && !lv->u.number)
error("Value being indexed is zero.\n");
error("Cannot index value of type '%s'.\n", type_name(lv->type));
}
} else {
/* It is now coming from (x <assign_type> y)[index]... = rhs */
/* Where x is a _valid_ lvalue */
/* Hence the reference to sp is at least 2 :) */
if (!code && (sp->type == T_MAPPING)) {
if (!(lv = find_for_insert(sp->u.map, sp-1, 0)))
mapping_too_large();
sp->u.map->ref--;
#ifdef REF_RESERVED_WORD
lv_owner_type = T_MAPPING;
lv_owner = (refed_t *)sp->u.map;
#endif
free_svalue(--sp, "push_indexed_lvalue: 2");
sp->type = T_LVALUE;
sp->u.lvalue = lv;
return;
}
if (!((sp-1)->type == T_NUMBER))
error("Illegal type of index\n");
ind = (sp-1)->u.number;
switch (sp->type) {
case T_STRING:
{
error("Illegal to make char lvalue from assigned string\n");
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
if (code) ind = sp->u.buf->size - ind;
if (ind >= sp->u.buf->size || ind < 0)
error("Buffer index out of bounds.\n");
sp->u.buf->ref--;
#ifdef REF_RESERVED_WORD
lv_owner_type = T_BUFFER;
lv_owner = (refed_t *)sp->u.buf;
#endif
(--sp)->type = T_LVALUE;
sp->u.lvalue = &global_lvalue_byte;
global_lvalue_byte.subtype = 1;
global_lvalue_byte.u.lvalue_byte = (sp+1)->u.buf->item + ind;
break;
}
#endif
case T_ARRAY:
{
if (code) ind = sp->u.arr->size - ind;
if (ind >= sp->u.arr->size || ind < 0)
error("Array index out of bounds.\n");
sp->u.arr->ref--;
#ifdef REF_RESERVED_WORD
lv_owner_type = T_ARRAY;
lv_owner = (refed_t *)sp->u.arr;
#endif
(--sp)->type = T_LVALUE;
sp->u.lvalue = (sp+1)->u.arr->item + ind;
break;
}
default:
if (sp->type == T_NUMBER && !sp->u.number)
error("Value being indexed is zero.\n");
error("Cannot index value of type '%s'.\n", type_name(sp->type));
}
}
}
static struct lvalue_range {
int ind1, ind2, size;
svalue_t *owner;
} global_lvalue_range;
static svalue_t global_lvalue_range_sv = { T_LVALUE_RANGE };
INLINE_STATIC void push_lvalue_range P1(int, code)
{
int ind1, ind2, size;
svalue_t *lv;
if (sp->type == T_LVALUE) {
switch((lv = global_lvalue_range.owner = sp->u.lvalue)->type) {
case T_ARRAY:
size = lv->u.arr->size;
break;
case T_STRING: {
size = SVALUE_STRLEN(lv);
unlink_string_svalue(lv);
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
size = lv->u.buf->size;
break;
#endif
default:
error("Range lvalue on illegal type\n");
IF_DEBUG(size = 0);
}
} else
error("Range lvalue on illegal type\n");
if (!((--sp)->type == T_NUMBER)) error("Illegal 2nd index type to range lvalue\n");
ind2 = (code & 0x01) ? (size - sp->u.number) : sp->u.number;
if (++ind2 < 0 || (ind2 > size))
error("The 2nd index to range lvalue must be >= -1 and < sizeof(indexed value)\n");
if (!((--sp)->type == T_NUMBER)) error("Illegal 1st index type to range lvalue\n");
ind1 = (code & 0x10) ? (size - sp->u.number) : sp->u.number;
if (ind1 < 0 || ind1 > size)
error("The 1st index to range lvalue must be >= 0 and <= sizeof(indexed value)\n");
global_lvalue_range.ind1 = ind1;
global_lvalue_range.ind2 = ind2;
global_lvalue_range.size = size;
sp->type = T_LVALUE;
sp->u.lvalue = &global_lvalue_range_sv;
}
INLINE void copy_lvalue_range P1(svalue_t *, from)
{
int ind1, ind2, size, fsize;
svalue_t *owner;
ind1 = global_lvalue_range.ind1;
ind2 = global_lvalue_range.ind2;
size = global_lvalue_range.size;
owner = global_lvalue_range.owner;
switch(owner->type) {
case T_ARRAY:
{
array_t *fv, *dv;
svalue_t *fptr, *dptr;
if (from->type != T_ARRAY) error("Illegal rhs to array range lvalue\n");
fv = from->u.arr;
fptr = fv->item;
if ((fsize = fv->size) == ind2 - ind1) {
dptr = (owner->u.arr)->item + ind1;
if (fv->ref == 1) {
/* Transfer the svalues */
while (fsize--) {
free_svalue(dptr, "copy_lvalue_range : 1");
*dptr++ = *fptr++;
}
free_empty_array(fv);
} else {
while (fsize--) assign_svalue(dptr++, fptr++);
fv->ref--;
}
} else {
array_t *old_dv = owner->u.arr;
svalue_t *old_dptr = old_dv->item;
/* Need to reallocate the array */
dv = allocate_empty_array(size - ind2 + ind1 + fsize);
dptr = dv->item;
/* ind1 can range from 0 to sizeof(old_dv) */
while (ind1--) assign_svalue_no_free(dptr++, old_dptr++);
if (fv->ref == 1) {
while (fsize--) *dptr++ = *fptr++;
free_empty_array(fv);
} else {
while (fsize--) assign_svalue_no_free(dptr++, fptr++);
fv->ref--;
}
/* ind2 can range from 0 to sizeof(old_dv) */
old_dptr = old_dv->item + ind2;
size -= ind2;
while (size--) assign_svalue_no_free(dptr++, old_dptr++);
free_array(old_dv);
owner->u.arr = dv;
}
break;
}
case T_STRING:
{
if (from->type != T_STRING) error("Illegal rhs to string range lvalue.\n");
if ((fsize = SVALUE_STRLEN(from)) == ind2 - ind1) {
/* since fsize >= 0, ind2 - ind1 <= strlen(orig string) */
/* because both of them can only range from 0 to len */
strncpy(owner->u.string + ind1, from->u.string, fsize);
} else {
char *tmp, *dstr = owner->u.string;
owner->u.string = tmp = new_string(size - ind2 + ind1 + fsize, "copy_lvalue_range");
if (ind1 >= 1) {
strncpy(tmp, dstr, ind1);
tmp += ind1;
}
strcpy(tmp, from->u.string);
tmp += fsize;
size -= ind2;
if (size >= 1) {
strncpy(tmp, dstr + ind2, size);
*(tmp + size) = 0;
}
FREE_MSTR(dstr);
}
free_string_svalue(from);
break;
}
#ifndef NO_BUFFER_TYPE
case T_BUFFER:
{
if (from->type != T_BUFFER) error("Illegal rhs to buffer range lvalue.\n");
if ((fsize = from->u.buf->size) == ind2 - ind1) {
memcpy((owner->u.buf)->item + ind1, from->u.buf->item, fsize);
} else {
buffer_t *b;
unsigned char *old_item = (owner->u.buf)->item;
unsigned char *new_item;
b = allocate_buffer(size - ind2 + ind1 + fsize);
new_item = b->item;
if (ind1 >= 1) {
memcpy(b->item, old_item, ind1);
new_item += ind1;
}
memcpy(new_item, from->u.buf, fsize);
new_item += fsize;
if ((size -= ind2) >= 1)
memcpy(new_item, old_item + ind2, size);
free_buffer(owner->u.buf);
owner->u.buf = b;
}
free_buffer(from->u.buf);
break;
}
#endif
}
}
INLINE void assign_lvalue_range P1(svalue_t *, from)
{
int ind1, ind2, size, fsize;
svalue_t *owner;
ind1 = global_lvalue_range.ind1;
ind2 = global_lvalue_range.ind2;
size = global_lvalue_range.size;
owner = global_lvalue_range.owner;
switch(owner->type) {