interpret.c
./driver3.2@242/interpret.c · 273.4 KB
From the archive driver3.2@242.tar.gz
Download the archive « Back to the archive contents
#include <varargs.h>
#include <stdio.h>
#include <setjmp.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef MARK
#include <prof.h>
#endif
#ifdef MARK
#define CASE(x) case (x)-F_OFFSET: MARK(x);
#else
#define CASE(x) case (x)-F_OFFSET:
#endif
#define INTERPRET
#include "lint.h"
#include "interpret.h"
#include "lang.h"
#include "exec.h"
#include "object.h"
#include "wiz_list.h"
#include "instrs.h"
#include "comm.h"
#include "sent.h"
#include "switch.h"
#define USES_SVALUE_STRLEN
#include "smalloc.h"
#include "stralloc.h"
#ifdef GETRUSAGE_VIA_SYSCALL
/* hpux */
#include <sys/syscall.h>
#define getrusage(a, b) syscall(SYS_GETRUSAGE, a, b)
#endif
#ifdef HAVE_GETRUSAGE /* Defined in machine.h */
#ifdef HAVE_SYS_RUSAGE_H
/* solaris */
#include <sys/rusage.h>
#endif /* HAVE_SYS_RUSAGE */
#include <sys/resource.h>
#ifdef sun
extern int getpagesize();
#endif
#ifndef RUSAGE_SELF
#define RUSAGE_SELF 0
#endif
#ifdef sun
extern int getrusage PROT((int, struct rusage *));
#endif
#endif /* HAVE_GETRUSAGE */
#if defined(__GNUC__) && !defined(lint) && !defined(DEBUG)
#define INLINE inline
#else
#define INLINE
#endif
#ifdef MAPPINGS
/* mapping prototypes */
struct mapping *allocate_mapping PROT((int, int));
void free_mapping PROT((struct mapping*));
void free_protector_mapping PROT((struct mapping*));
struct svalue *get_map_lvalue PROT((struct mapping*, struct svalue*, int));
struct mapping *copy_mapping PROT((struct mapping *));
void remove_mapping PROT((struct mapping *, struct svalue *));
struct mapping *add_mapping PROT((struct mapping*, struct mapping*));
void add_to_mapping PROT((struct mapping*, struct mapping*));
void sub_from_mapping_filter PROT((struct svalue *, struct svalue *, char *));
struct mapping *subtract_mapping PROT((struct mapping *, struct mapping *));
struct svalue *filter_mapping PROT(( struct svalue*, int));
struct svalue *map_mapping PROT((struct svalue*, int));
void check_map_for_destr PROT((struct mapping *));
void walk_mapping PROT((
struct mapping *,
void (*)(struct svalue *, struct svalue *, char *),
char *
));
void m_values_filter PROT((struct svalue *, struct svalue *, char *));
struct vector *m_indices PROT((struct mapping *));
#endif /* MAPPINGS */
extern struct object *master_ob;
extern int do_rename PROT((char *, char *));
extern void print_svalue PROT((struct svalue *));
static void free_protector_svalue PROT((struct svalue *));
struct svalue *sapply PROT((char *, struct object *, int));
static void do_trace PROT((char *, char *, char *));
static int apply_low PROT((char *, struct object *, int));
static int inter_sscanf PROT((int, struct svalue *));
struct vector *inter_add_array PROT((struct vector *, struct vector**));
static int strpref PROT((char *, char *));
static void transfer_pointer_range PROT((struct svalue *source));
static void transfer_protected_pointer_range
PROT((struct protected_range_lvalue *dest, struct svalue *source));
static void assign_string_range PROT((struct svalue *source, int do_free));
static void assign_protected_string_range
PROT((struct protected_range_lvalue *dest,struct svalue *source, int do_free));
static void call_simul_efun PROT((int code, struct object *ob, int num_arg));
#if defined(F_SPRINTF) || defined(F_PRINTF)
extern char *string_print_formatted PROT((char *, int, struct svalue *));
#endif /* defined(F_SPRINTF) || defined(F_PRINTF) */
extern struct object *previous_ob;
extern char *last_verb;
extern struct svalue const0, const1;
struct program *current_prog;
extern int current_time;
extern struct object *current_heart_beat, *current_interactive;
extern int out_of_memory;
extern char *out_of_memory_string;
/* A function call can cause an eval_cost overflow linear to the number of
* shadows. Well, adding more than a million is likely to cause memory
* trouble anyway.
*/
#define MIN_TRACE_COST (0x100000 + \
CATCH_RESERVED_COST + 2 * MASTER_RESERVED_COST + 2 * MAX_TRACE)
#define MAX_TRACE_COST ((int32)(0x80000000 - MIN_TRACE_COST))
int tracedepth;
int trace_level;
int trace_test PROT((int));
#define TRACE_CALL 1
#define TRACE_CALL_OTHER 2
#define TRACE_RETURN 4
#define TRACE_ARGS 8
#define TRACE_EXEC 16
#define TRACE_HEART_BEAT 32
#define TRACE_APPLY 64
#define TRACE_OBJNAME 128
#define TRACETST(b) (command_giver->interactive->trace_level & (b))
#define TRACEP(b) (trace_level & (b) && trace_test(b))
#define SET_TRACE_EXEC (trace_level & TRACE_EXEC && eval_cost < 0 && \
(eval_cost += MAX_TRACE_COST, assigned_eval_cost += MAX_TRACE_COST))
#define TRACE_EXEC_P \
( TRACEP(TRACE_EXEC) || \
(eval_cost -= MAX_TRACE_COST, assigned_eval_cost -= MAX_TRACE_COST, \
MY_FALSE) )
#define TRACEHB (current_heart_beat == 0 || (command_giver->interactive->trace_level & TRACE_HEART_BEAT))
/*
* 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 separetly. X will then be reloaded again.
*/
extern int d_flag;
extern int port_number;
extern int32 current_line, initial_eval_cost, eval_cost, assigned_eval_cost;
/*
* 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.
*/
#ifndef SMALLOC_LPC_TRACE
static
#endif
char *inter_pc; /* Program pointer. */
static struct svalue *inter_fp; /* Pointer to first argument. */
struct svalue *inter_sp; /* Points to value of last push. */
static short *break_sp; /* Points to address to branch to
* at next F_BREAK */
int function_index_offset; /* Needed for inheritance */
static int variable_index_offset; /* Needed for inheritance */
struct svalue *current_variables;
static char **current_strings;
/* reserve space to store values + overflow space */
static struct svalue start_of_stack[EVALUATOR_STACK_SIZE<<1];
struct svalue catch_value = { T_INVALID } ;
/* Used to throw an error to a catch */
static struct control_stack control_stack[MAX_TRACE];
static struct control_stack *csp; /* Points to last element pushed */
#define ERRORF(s) {inter_pc = pc; inter_sp = sp; error s ;}
#define ERROR(s) ERRORF((s))
#define STACK_OVERFLOW(sp, fp, pc) stack_overflow(sp, fp, pc)
#define pop_n_elems(n) (sp = _pop_n_elems((n), sp))
INLINE static struct svalue *_pop_n_elems PROT((int, struct svalue *));
static void stack_overflow(sp, fp, pc)
struct svalue *sp;
struct svalue *fp;
char *pc;
{
pop_n_elems(sp-fp);
ERROR("stack overflow\n")
}
#define ASSIGN_EVAL_COST \
if (current_object->user)\
current_object->user->cost += eval_cost - assigned_eval_cost;\
assigned_eval_cost = eval_cost;
void assign_eval_cost() { ASSIGN_EVAL_COST }
/*
* Information about assignments of values:
*
* There are three types of l-values: Local variables, global variables
* and vector 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 vectors are similar to global variables. There is a reference
* count to the whole vector, that states when to deallocate the vector.
* The elements consists of 'struct svalue'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
static struct svalue *_push_object(ob, sp)
struct object *ob;
struct svalue *sp;
{
sp++;
sp->type = T_OBJECT;
sp->u.ob = ob;
add_ref(ob, "push_object");
return sp;
}
void push_object(ob)
struct object *ob;
{
inter_sp++;
inter_sp->type = T_OBJECT;
inter_sp->u.ob = ob;
add_ref(ob, "push_object");
}
#define push_object(ob) (sp = _push_object((ob), sp))
static INLINE
void _put_object(ob, sp)
struct object *ob;
struct svalue *sp;
{
sp->type = T_OBJECT;
sp->u.ob = ob;
add_ref(ob, "push_object");
}
#define put_object(ob) _put_object(ob, sp)
/*
* Push a number on the value stack.
*/
INLINE
static struct svalue *_push_number(n, sp)
int n;
struct svalue *sp;
{
sp++;
sp->type = T_NUMBER;
sp->u.number = n;
return sp;
}
#define put_number(n) (sp->type = T_NUMBER, sp->u.number = n)
void push_number(n)
int n;
{
inter_sp++;
inter_sp->type = T_NUMBER;
inter_sp->u.number = n;
}
/*
* Push a string on the stack that is already shared.
*/
INLINE
struct svalue *_push_shared_string(p, sp)
char *p;
struct svalue *sp;
{
sp++;
sp->type = T_STRING;
sp->x.string_type = STRING_SHARED;
increment_string_ref( sp->u.string = p );
return sp;
}
void push_shared_string(p) char *p; {
inter_sp = _push_shared_string(p, inter_sp);
}
#define push_shared_string(p) (sp = _push_shared_string((p), sp))
void push_referenced_shared_string(p)
char *p;
{
struct svalue *sp = inter_sp;
sp++;
sp->type = T_STRING;
sp->x.string_type = STRING_SHARED;
sp->u.string = p;
inter_sp = sp;
}
/*
* Get address to a valid global variable.
*/
#ifdef DEBUG
static INLINE struct svalue *find_value(num)
int num;
{
if (num >= current_object->prog->num_variables) {
fatal("Illegal variable access %d(%d). See trace above.\n",
num, current_object->prog->num_variables);
}
return ¤t_variables[num];
}
#else
#define find_value(num) (¤t_variables[(num)])
#endif
/* DICE v2.06 pessimizes constant pointer arithmetic,
* assuming 0 as difference of constant pointers. */
#if defined(AMIGA) && defined(_DCC)
#define PTRTYPE unsigned long
#else
#define PTRTYPE char *
#endif
static INLINE struct svalue *find_virtual_value(num)
int num;
{
struct inherit *inheritp;
struct program *progp;
char *progpp; /* points to a struct program *, but some compilers... */
inheritp = current_prog->inherit;
while
(inheritp->variable_index_offset + inheritp->prog->num_variables < num)
inheritp++;
num -= inheritp->variable_index_offset;
progp = inheritp->prog;
progpp = (char *)¤t_object->prog->inherit->prog;
while (*(struct program **)progpp != progp)
progpp += sizeof(struct inherit);
num += (
(struct inherit *)(
((PTRTYPE)(progpp))-
((PTRTYPE)(&((struct inherit *)0)->prog)-(PTRTYPE) 0)
)
)->variable_index_offset;
return ¤t_object->variables[num];
}
INLINE void free_string_svalue(v)
struct svalue *v;
{
switch(v->x.string_type) {
case STRING_MALLOC:
xfree(v->u.string);
break;
case STRING_SHARED:
free_string(v->u.string);
break;
}
}
void free_object_svalue(v)
struct svalue *v;
{
struct object *ob = v->u.ob;
free_object(ob, "free_object_svalue");
}
static void zero_object_svalue(v)
struct svalue *v;
{
struct object *ob = v->u.ob;
free_object(ob, "zero_object_svalue");
v->type = T_NUMBER;
v->u.number = 0;
}
struct protected_lvalue {
struct svalue v; /* this must come first, to simulate superclass */
struct svalue protector; /* for v.u.lvalue */
};
struct protected_char_lvalue {
struct svalue v; /* this must come first, to simulate superclass */
/* *v.u.string is the character to access */
struct svalue protector; /* for the following lvalue... */
struct svalue *lvalue;
char *start;
/* this lvalue is invalid when start != lvalue->u.string */
};
struct protected_range_lvalue {
struct svalue v; /* this must come first, to simulate superclass */
struct svalue protector; /* for the following lvalue... */
struct svalue *lvalue;
int index1, index2, size;
};
/*
* Free the data that an svalue is pointing to. Not the svalue
* itself.
*/
void free_svalue(v)
struct svalue *v;
{
switch(v->type) {
case T_STRING:
switch(v->x.string_type) {
case STRING_MALLOC:
xfree(v->u.string);
break;
case STRING_SHARED:
free_string(v->u.string);
break;
}
break;
case T_OBJECT:
{
struct object *ob = v->u.ob;
free_object(ob, "free_svalue");
break;
}
case T_QUOTED_ARRAY:
case T_POINTER:
free_vector(v->u.vec);
break;
case T_SYMBOL:
free_string(v->u.string);
break;
case T_CLOSURE:
free_closure(v);
break;
case T_LVALUE:
#ifdef DEBUG
if (d_flag > 2) {
fprintf(stderr, "free_svalue called for T_LVALUE\n");
dump_trace(1);
}
#endif
switch (v->u.lvalue->type) {
case T_PROTECTED_LVALUE:
{
struct protected_lvalue *p;
p = v->u.protected_lvalue;
free_protector_svalue(&p->protector);
xfree((char*)p);
break;
}
case T_PROTECTED_CHAR_LVALUE:
{
struct protected_char_lvalue *p;
p = v->u.protected_char_lvalue;
if (p->lvalue->type == T_STRING &&
p->lvalue->u.string == p->start)
{
p->lvalue->x.string_type = STRING_MALLOC;
} else {
xfree(p->start);
}
free_protector_svalue(&p->protector);
xfree((char*)p);
break;
}
case T_PROTECTED_STRING_RANGE_LVALUE:
{
struct protected_range_lvalue *p;
p = v->u.protected_range_lvalue;
if (p->lvalue->type == T_STRING &&
p->lvalue->u.string == p->v.u.string)
{
p->lvalue->x.string_type = STRING_MALLOC;
} else {
xfree(p->v.u.string);
}
free_protector_svalue(&p->protector);
xfree((char*)p);
break;
}
case T_PROTECTED_POINTER_RANGE_LVALUE:
{
struct protected_range_lvalue *p;
p = v->u.protected_range_lvalue;
free_vector(p->v.u.vec);
free_protector_svalue(&p->protector);
xfree((char*)p);
break;
}
case T_ERROR_HANDLER:
/* this is nested here only to save overhead in the general case.
* It has actually little connection to lvalues.
*/
{
struct svalue *p;
p = v->u.lvalue;
(*p->u.error_handler)(p);
break;
}
}
break;
#ifdef MAPPINGS
case T_MAPPING:
free_mapping(v->u.map);
break;
#endif
}
#if 0
*v = const0; /* marion - clear this value all away */
#endif
}
static void free_protector_svalue(v)
struct svalue *v;
{
switch(v->type) {
case T_POINTER:
free_vector(v->u.vec);
break;
#ifdef MAPPINGS
case T_MAPPING:
free_mapping(v->u.map);
break;
case T_PROTECTOR_MAPPING:
free_protector_mapping(v->u.map);
break;
#endif
}
}
/*
* Prepend a slash in front of a string.
*/
static char *add_slash(str)
char *str;
{
char *tmp;
tmp = xalloc(strlen(str)+2);
if (tmp) {
*tmp = '/';
strcpy(tmp+1,str);
}
return tmp;
}
/*
* Assign to a svalue.
* This is done either when element in vector, or when to an identifier
* (as all identifiers are kept in a vector pointed to by the object).
*/
static void malloced_string_copy(svp, str)
struct svalue *svp;
char *str;
{
char *p;
p = xalloc(malloced_strlen(str));
if (!p) {
svp->type = T_NUMBER;
error("Out of memory\n");
}
(void)strcpy(p, str);
svp->u.string = p;
}
static struct {
struct svalue v;
int index1, index2, size;
} special_lvalue;
INLINE void assign_svalue_no_free(to, from)
struct svalue *to;
struct svalue *from;
{
#ifdef DEBUG
if (from == 0)
fatal("Null pointer to assign_svalue().\n");
#endif
*to = *from;
switch(from->type) {
case T_STRING:
switch(from->x.string_type) {
case STRING_MALLOC: /* No idea to make the string shared */
malloced_string_copy(to, from->u.string);
break;
case STRING_VOLATILE: /* Good idea to make it shared */
to->x.string_type = STRING_SHARED;
if ( !(to->u.string = make_shared_string(from->u.string)) ) {
to->type = T_NUMBER;
error("Out of memory\n");
}
break;
case STRING_SHARED: /* It already is shared */
increment_string_ref(from->u.string);
break;
#ifdef DEBUG
default:
fatal("Bad string type %d\n", from->x.string_type);
#endif
}
break;
case T_OBJECT:
add_ref(to->u.ob, "ass to var");
break;
case T_QUOTED_ARRAY:
case T_POINTER:
to->u.vec->ref++;
break;
case T_SYMBOL:
increment_string_ref(to->u.string);
break;
case T_CLOSURE:
if (CLOSURE_MALLOCED(to->x.closure_type))
to->u.lambda->ref++;
else
add_ref(to->u.ob, "ass to var");
break;
#ifdef MAPPINGS
case T_MAPPING:
to->u.map->ref++;
break;
#endif
}
}
INLINE void assign_lrvalue_no_free(to, from)
struct svalue *to;
struct svalue *from;
{
#ifdef DEBUG
if (from == 0)
fatal("Null pointer to assign_lrvalue_no_free().\n");
#endif
*to = *from;
switch(from->type) {
case T_STRING:
if (to->x.string_type != STRING_SHARED) {
to->x.string_type = STRING_SHARED;
to->u.string = make_shared_string(from->u.string);
if (from->x.string_type == STRING_MALLOC) {
xfree(from->u.string);
}
*from = *to;
}
increment_string_ref(from->u.string);
break;
case T_OBJECT:
add_ref(to->u.ob, "ass to var");
break;
case T_QUOTED_ARRAY:
case T_POINTER:
to->u.vec->ref++;
break;
case T_SYMBOL:
increment_string_ref(to->u.string);
break;
case T_CLOSURE:
if (CLOSURE_MALLOCED(to->x.closure_type))
to->u.lambda->ref++;
else
add_ref(to->u.ob, "ass to var");
break;
#ifdef MAPPINGS
case T_MAPPING:
to->u.map->ref++;
break;
#endif
case T_LVALUE:
to->u.lvalue = from;
break;
}
}
void assign_svalue(dest, v)
struct svalue *dest;
struct svalue *v;
{
for (;;) {
/* Deallocate the previous value. Structured types will necessiate to
* do the assignment first, then deallocate, lest recursive structures
* could cause crashs.
*/
switch(dest->type) {
case T_LVALUE:
case T_PROTECTED_LVALUE:
dest = dest->u.lvalue;
continue;
case T_STRING:
switch(dest->x.string_type) {
case STRING_MALLOC:
xfree(dest->u.string);
break;
case STRING_SHARED:
free_string(dest->u.string);
break;
}
break;
case T_OBJECT:
{
struct object *ob = dest->u.ob;
free_object(ob, "assign_svalue");
break;
}
case T_QUOTED_ARRAY:
case T_POINTER:
{
struct vector *vec = dest->u.vec;
assign_svalue_no_free(dest, v); /* leaks vec if out fo memory */
free_vector(vec);
return;
}
case T_SYMBOL:
free_string(dest->u.string);
break;
case T_CLOSURE:
free_closure(dest);
break;
case T_CHAR_LVALUE:
if (v->type == T_NUMBER)
*dest->u.string = v->u.number;
return;
case T_PROTECTED_CHAR_LVALUE:
{
struct protected_char_lvalue *p;
p = (struct protected_char_lvalue *)dest;
if (p->lvalue->type == T_STRING &&
p->lvalue->u.string == p->start)
{
if (v->type == T_NUMBER)
*p->v.u.string = v->u.number;
}
return;
}
case T_POINTER_RANGE_LVALUE:
if (v->type == T_POINTER) {
v->u.vec->ref++;
transfer_pointer_range(v);
}
return;
case T_PROTECTED_POINTER_RANGE_LVALUE:
if (v->type == T_POINTER) {
v->u.vec->ref++;
transfer_protected_pointer_range(
(struct protected_range_lvalue *)dest, v
);
}
return;
case T_STRING_RANGE_LVALUE:
assign_string_range(v, 0);
return;
case T_PROTECTED_STRING_RANGE_LVALUE:
assign_protected_string_range(
(struct protected_range_lvalue *)dest, v, 0
);
return;
#ifdef MAPPINGS
case T_MAPPING:
{
struct mapping *map = dest->u.map;
assign_svalue_no_free(dest, v); /* leaks map if out of memory */
free_mapping(map);
return;
}
#endif
}
break;
} /* end for */
assign_svalue_no_free(dest, v);
return;
}
INLINE void transfer_svalue_no_free(dest, v)
struct svalue *dest;
struct svalue *v;
{
if (v->type == T_STRING && v->x.string_type == STRING_VOLATILE) {
dest->type = T_STRING;
dest->x.string_type = STRING_SHARED;
if ( !(dest->u.string = make_shared_string(v->u.string)) ) {
dest->type = T_NUMBER;
error("Out of memory\n");
}
} else {
*dest = *v;
}
}
INLINE static void transfer_svalue_no_free_spc(dest, v, sp, pc)
struct svalue *dest;
struct svalue *v;
struct svalue *sp; char *pc;
{
if (v->type == T_STRING && v->x.string_type == STRING_VOLATILE) {
dest->type = T_STRING;
dest->x.string_type = STRING_SHARED;
if ( !(dest->u.string = make_shared_string(v->u.string)) ) {
dest->type = T_NUMBER;
inter_sp = sp;
inter_pc = pc;
error("Out of memory\n");
}
} else {
*dest = *v;
}
}
void transfer_svalue(dest, v)
struct svalue *dest;
struct svalue *v;
{
for(;;) {
/* First deallocate the previous value. */
switch(dest->type) {
case T_LVALUE:
case T_PROTECTED_LVALUE:
dest = dest->u.lvalue;
continue;
case T_STRING:
switch(dest->x.string_type) {
case STRING_MALLOC:
xfree(dest->u.string);
break;
case STRING_SHARED:
free_string(dest->u.string);
break;
}
break;
case T_OBJECT:
{
struct object *ob = dest->u.ob;
free_object(ob, "transfer_svalue");
break;
}
case T_QUOTED_ARRAY:
case T_POINTER:
free_vector(dest->u.vec);
break;
case T_SYMBOL:
free_string(dest->u.string);
break;
case T_CLOSURE:
free_closure(dest);
break;
case T_CHAR_LVALUE:
if (v->type == T_NUMBER) {
*dest->u.string = v->u.number;
} else
free_svalue(v);
return;
case T_PROTECTED_CHAR_LVALUE:
{
struct protected_char_lvalue *p;
p = (struct protected_char_lvalue *)dest;
if (p->lvalue->type == T_STRING &&
p->lvalue->u.string == p->start)
{
if (v->type == T_NUMBER) {
*p->v.u.string = v->u.number;
return;
}
}
free_svalue(v);
return;
}
case T_POINTER_RANGE_LVALUE:
transfer_pointer_range(v);
return;
case T_PROTECTED_POINTER_RANGE_LVALUE:
transfer_protected_pointer_range(
(struct protected_range_lvalue *)dest, v
);
return;
case T_STRING_RANGE_LVALUE:
assign_string_range(v, 1);
return;
case T_PROTECTED_STRING_RANGE_LVALUE:
assign_protected_string_range(
(struct protected_range_lvalue *)dest, v, 1
);
return;
#ifdef MAPPINGS
case T_MAPPING:
free_mapping(dest->u.map);
break;
#endif
} /* end switch */
break;
} /* end for */
if (v->type == T_STRING && v->x.string_type == STRING_VOLATILE) {
dest->type = T_STRING;
dest->x.string_type = STRING_SHARED;
dest->u.string = make_shared_string(v->u.string);
} else {
*dest = *v;
}
}
static void transfer_pointer_range(source)
struct svalue *source;
{
if (source->type == T_POINTER) {
struct vector *sv, *dv, *rv;
int dsize, ssize, index1, index2;
int i;
dsize = special_lvalue.size;
index1 = special_lvalue.index1;
index2 = special_lvalue.index2;
dv = special_lvalue.v.u.lvalue->u.vec;
sv = source->u.vec;
ssize = sv->size;
if (ssize + index1 - index2 == 0) {
struct svalue *s, *d;
s = sv->item;
d = dv->item + index1;
dv->ref++; /* This way, it won't be freed while we are
* using it */
if (!--sv->ref) {
sv->ref++;
for (i = ssize; --i >= 0; ) {
transfer_svalue(d++, s++);
}
free_empty_vector(sv);
} else {
for (i = ssize; --i >= 0; ) {
assign_svalue(d++, s++);
}
free_vector(sv);
}
free_vector(dv);
} else {
struct svalue *s, *d;
rv = allocate_array(dsize + ssize + index1 - index2);
special_lvalue.v.u.lvalue->u.vec = rv;
s = dv->item;
d = rv->item;
for (i = index1; --i >= 0; ) {
assign_svalue_no_free(d++, s++);
}
s = sv->item;
for (i = ssize; --i >= 0; ) {
assign_svalue_no_free(d++, s++);
}
free_vector(sv);
s = dv->item + index2;
for (i = dsize - index2; --i >= 0; ) {
assign_svalue_no_free(d++, s++);
}
free_vector(dv); /* this can make the lvalue invalid to use */
}
} else
free_svalue(source);
return;
}
static void transfer_protected_pointer_range(dest, source)
struct protected_range_lvalue *dest;
struct svalue *source;
{
if (source->type == T_POINTER && dest->v.u.vec == dest->lvalue->u.vec) {
struct vector *sv, *dv, *rv;
int dsize, ssize, index1, index2;
int i;
dsize = dest->size;
index1 = dest->index1;
index2 = dest->index2;
dv = dest->v.u.vec;
sv = source->u.vec;
ssize = sv->size;
if (ssize + index1 - index2 == 0) {
struct svalue *s, *d;
s = sv->item;
d = dv->item + index1;
if (!--sv->ref) {
sv->ref++;
for (i = ssize; --i >= 0; ) {
transfer_svalue(d++, s++);
}
free_empty_vector(sv);
} else {
for (i = ssize; --i >= 0; ) {
assign_svalue(d++, s++);
}
free_vector(sv);
}
} else {
struct svalue *s, *d;
rv = allocate_array(dsize + ssize + index1 - index2);
dest->lvalue->u.vec = rv;
s = dv->item;
d = rv->item;
for (i = index1; --i >= 0; ) {
assign_svalue_no_free(d++, s++);
}
s = sv->item;
for (i = ssize; --i >= 0; ) {
assign_svalue_no_free(d++, s++);
}
free_vector(sv);
s = dv->item + index2;
for (i = dsize - index2; --i >= 0; ) {
assign_svalue_no_free(d++, s++);
}
free_vector(dv); /* this can make the lvalue invalid to use */
}
} else
free_svalue(source);
return;
}
static void assign_string_range(source, do_free)
struct svalue *source;
int do_free;
{
if (source->type == T_STRING) {
struct svalue *dsvp;
char *ss, *ds, *rs;
int dsize, ssize, index1, index2;
dsize = special_lvalue.size;
index1 = special_lvalue.index1;
index2 = special_lvalue.index2;
dsvp = special_lvalue.v.u.lvalue;
ds = dsvp->u.string;
ss = source->u.string;
ssize = svalue_strlen(source);
rs = xalloc(dsize + ssize + index1 - index2 + 1);
if (!rs) {
/* We don't pop the stack here --> don't free source */
error("Out of memory\n");
}
if (index1) memcpy(rs, ds, index1);
if (ssize) memcpy(rs + index1, ss, ssize);
strcpy(rs + index1 + ssize, ds + index2);
free_string_svalue(dsvp);
dsvp->x.string_type = STRING_MALLOC;
dsvp->u.string = rs;
if (do_free)
free_string_svalue(source);
} else {
if (do_free)
free_svalue(source);
}
return;
}
static void assign_protected_string_range(dest, source, do_free)
struct protected_range_lvalue *dest;
struct svalue *source;
int do_free;
{
if (source->type == T_STRING) {
struct svalue *dsvp;
char *ss, *ds, *rs;
int dsize, ssize, index1, index2;
dsize = dest->size;
index1 = dest->index1;
index2 = dest->index2;
dsvp = dest->lvalue;
ds = dest->v.u.string;
if (dsvp->u.string != ds) {
if (do_free) {
free_svalue(source);
xfree(dest->v.u.string);
xfree((char *)dest);
}
return;
}
ss = source->u.string;
ssize = svalue_strlen(source);
rs = xalloc(dsize + ssize + index1 - index2 + 1);
if (!rs) {
error("Out of memory\n");
}
if (index1) memcpy(rs, ds, index1);
if (ssize) memcpy(rs + index1, ss, ssize);
strcpy(rs + (dest->index2 = index1 + ssize), ds + index2);
xfree(ds);
dest->v.u.string = dsvp->u.string = rs;
if (do_free) {
free_string_svalue(source);
dest->v.x.string_type = STRING_MALLOC;
free_protector_svalue(&dest->protector);
xfree((char *)dest);
}
} else {
if (do_free) {
free_svalue(source);
dest->v.x.string_type = STRING_MALLOC;
free_protector_svalue(&dest->protector);
xfree((char *)dest);
}
}
return;
}
static int add_number_to_svalue(dest, i)
struct svalue *dest; /* dest->type == T_LVALUE, or should be treated as such */
int i;
{
do
dest = dest->u.lvalue;
while(dest->type == T_LVALUE);
switch (dest->type) {
default:
error("Reference to bad type to ++/--\n");
return i;
case T_NUMBER:
return dest->u.number += i;
case T_PROTECTED_LVALUE:
return add_number_to_svalue(dest, i);
case T_CHAR_LVALUE:
return (*dest->u.string) += i;
case T_PROTECTED_CHAR_LVALUE:
{
struct protected_char_lvalue *p;
p = (struct protected_char_lvalue *)dest;
if (p->lvalue->type == T_STRING &&
p->lvalue->u.string == p->start)
{
i = *p->v.u.string += i;
}
return i;
}
}
}
void push_svalue(v)
struct svalue *v;
{
assign_svalue_no_free(++inter_sp, v);
}
void push_lrvalue(v)
struct svalue *v;
{
assign_lrvalue_no_free(++inter_sp, v);
}
void push_svalue_block(num, v)
int num;
struct svalue *v;
{
struct svalue *w;
for (w = inter_sp; --num >= 0; v++) {
w++;
assign_lrvalue_no_free(w, v);
}
inter_sp = w;
}
/* 'from' is meant to point to a variable or vector element, so it might
* contain a destructed object.
* if 'from' is an argument to the current lfun, it might be of type
* volatile.
*/
static INLINE void assign_checked_svalue_no_free(to, from, sp, pc)
struct svalue *to, *from;
struct svalue *sp; char *pc;
{
switch (from->type) {
case T_STRING:
switch(from->x.string_type) {
case STRING_MALLOC: /* No idea to make the string shared */
{
char *p;
char *str;
p = xalloc(malloced_strlen(str = from->u.string));
if (!p) {
to->type = T_NUMBER;
inter_sp = sp;
inter_pc = pc;
error("Out of memory\n");
}
(void)strcpy(p, str);
to->type = T_STRING;
to->x.string_type = STRING_MALLOC;
to->u.string = p;
return;
}
case STRING_SHARED: /* It already is shared */
increment_string_ref( to->u.string = from->u.string );
break;
case STRING_VOLATILE:
if ( !(to->u.string = make_shared_string(from->u.string)) ) {
to->type = T_STRING;
to->x.string_type = STRING_SHARED;
increment_string_ref(to->u.string = out_of_memory_string);
inter_sp = sp;
inter_pc = pc;
error("Out of memory\n");
}
break;
#ifdef DEBUG
default:
fatal("Bad string type %d\n", from->x.string_type);
#endif
}
to->type = T_STRING;
to->x.string_type = STRING_SHARED;
return;
case T_OBJECT:
{
struct object *ob = from->u.ob;
if ( !(ob->flags & O_DESTRUCTED) ) {
add_ref(ob, "ass to var");
break;
}
zero_object_svalue(from);
break;
}
case T_QUOTED_ARRAY:
case T_POINTER:
from->u.vec->ref++;
break;
case T_SYMBOL:
increment_string_ref(from->u.string);
break;
case T_CLOSURE:
if (CLOSURE_MALLOCED(from->x.closure_type))
from->u.lambda->ref++;
else
add_ref(from->u.ob, "ass to var");
break;
#ifdef MAPPINGS
case T_MAPPING:
from->u.map->ref++;
break;
#endif
case T_LVALUE:
case T_PROTECTED_LVALUE:
assign_checked_svalue_no_free(to, from->u.lvalue, sp, pc);
return;
case T_PROTECTED_CHAR_LVALUE:
to->type = T_NUMBER;
to->u.number = *from->u.string;
return;
}
*to = *from;
}
/*
* 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 xfree(), and destroyed.
*/
INLINE void pop_stack() {
#ifdef DEBUG
if (inter_sp < start_of_stack)
fatal("Stack underflow.\n");
#endif
free_svalue(inter_sp--);
}
#define pop_stack() free_svalue(sp--)
void drop_stack() {
inter_sp--;
}
static struct svalue indexing_quickfix = { T_NUMBER };
/*
* Compute the address of an array element.
*/
static INLINE struct svalue *push_indexed_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *item;
int ind;
i = sp;
vec = sp - 1;
if (vec->type == T_POINTER) {
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp;
}
sp = vec;
if (ind >= vec->u.vec->size) ERROR ("Index out of bounds\n")
item = &vec->u.vec->item[ind];
if (vec->u.vec->ref == 1) {
/* marion says: but this is crude too */
/* marion blushes. */
assign_svalue (&indexing_quickfix, item);
item = &indexing_quickfix;
}
free_vector(vec->u.vec); /* This will make 'vec' invalid to use */
vec->type = T_LVALUE;
vec->u.lvalue = item;
return sp;
}
#ifdef MAPPINGS
if (vec->type == T_MAPPING) {
struct mapping *m;
m = vec->u.map;
if (m->num_values) {
item = get_map_lvalue(m, i, 1);
free_svalue(sp--);
if (m->ref == 1) {
assign_svalue (&indexing_quickfix, item);
item = &indexing_quickfix;
}
free_mapping(m); /* This will make 'vec' invalid to use */
vec->type = T_LVALUE;
vec->u.lvalue = item;
return sp;
}
}
#endif /* MAPPINGS */
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
return sp;
}
static INLINE struct svalue *push_rindexed_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *item;
int ind;
i = sp;
vec = sp - 1;
if (vec->type == T_POINTER) {
if (i->type != T_NUMBER || (ind = i->u.number) <= 0) {
ERROR("Illegal index\n")
return sp;
}
sp = vec;
if ( (ind = vec->u.vec->size - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
item = &vec->u.vec->item[ind];
if (vec->u.vec->ref == 1) {
/* marion says: but this is crude too */
/* marion blushes. */
assign_svalue (&indexing_quickfix, item);
item = &indexing_quickfix;
}
free_vector(vec->u.vec); /* This will make 'vec' invalid to use */
vec->type = T_LVALUE;
vec->u.lvalue = item;
return sp;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
}
/* NB: the mapping as a whole still needs an ref count increment to
* protect the lvalue.
*/
#define BUILD_MAP_PROTECTOR(dest, m) \
{ \
struct hash_mapping *hm; \
\
if (hm = (m)->hash) { \
if (!hm->ref++) \
hm->deleted = 0; \
dest.type = T_PROTECTOR_MAPPING;\
} else { \
dest.type = T_MAPPING; \
} \
dest.u.map = m; \
}
static INLINE struct svalue *push_protected_indexed_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *item;
struct protected_lvalue *lvalue;
int ind;
i = sp;
vec = sp - 1;
if (vec->type == T_POINTER) {
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp;
}
sp = vec;
if (ind >= vec->u.vec->size) ERROR ("Index out of bounds\n")
item = &vec->u.vec->item[ind];
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = item;
lvalue->protector.type = T_POINTER;
lvalue->protector.u.vec = vec->u.vec;
vec->type = T_LVALUE;
vec->u.lvalue = &lvalue->v;
return sp;
}
#ifdef MAPPINGS
if (vec->type == T_MAPPING) {
struct mapping *m;
m = vec->u.map;
if (m->num_values) {
item = get_map_lvalue(m, i, 1);
pop_stack();
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = item;
BUILD_MAP_PROTECTOR(lvalue->protector, m)
vec->type = T_LVALUE;
vec->u.lvalue = &lvalue->v;
return sp;
}
}
#endif /* MAPPINGS */
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
}
static INLINE struct svalue *push_protected_rindexed_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *item;
struct protected_lvalue *lvalue;
int ind;
i = sp;
vec = sp - 1;
if (vec->type == T_POINTER) {
if (i->type != T_NUMBER || (ind = i->u.number) <= 0) {
ERROR("Illegal index\n")
return sp;
}
sp = vec;
if ( (ind = vec->u.vec->size - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
item = &vec->u.vec->item[ind];
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = item;
lvalue->protector.type = T_POINTER;
lvalue->protector.u.vec = vec->u.vec;
vec->type = T_LVALUE;
vec->u.lvalue = &lvalue->v;
return sp;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
}
#ifdef MAPPINGS
static INLINE struct svalue *push_protected_indexed_map_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *item;
struct protected_lvalue *lvalue;
i = sp - 1;
vec = sp - 2;
if (vec->type == T_MAPPING && sp->type == T_NUMBER) {
struct mapping *m;
m = vec->u.map;
if ( (p_uint)sp->u.number >= m->num_values) {
ERROR("Illegal index\n")
return sp;
}
item = get_map_lvalue(m, i, 1) + sp->u.number;
sp--;
pop_stack();
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = item;
BUILD_MAP_PROTECTOR(lvalue->protector, m)
vec->type = T_LVALUE;
vec->u.lvalue = &lvalue->v;
return sp;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
}
#endif /* MAPPINGS */
/*
* Compute the address of an array element.
*/
static INLINE struct svalue *index_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *vecp;
int ind;
short type;
vecp = sp;
i = sp -1;
vec = vecp->u.lvalue;
for(;;) {
type = vec->type;
if (type == T_POINTER) {
struct vector *v = vec->u.vec;
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp;
}
sp = i;
if (ind >= v->size) {
ERROR ("Index out of bounds\n")
return sp;
}
i->type = T_LVALUE;
i->u.lvalue = &v->item[ind];
return sp;
}
if (type == T_STRING) {
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->x.string_type = STRING_MALLOC;
vec->u.string = p;
}
if (ind >= _svalue_strlen(vec) ) {
ERROR ("Index out of bounds\n")
return sp;
}
sp = i;
i->type = T_LVALUE;
i->u.lvalue = &special_lvalue.v;
special_lvalue.v.type = T_CHAR_LVALUE;
special_lvalue.v.u.string = &vec->u.string[ind];
return sp;
}
#ifdef MAPPINGS
if (type == T_MAPPING) {
struct svalue *item;
struct mapping *m;
m = vec->u.map;
if (m->num_values) {
item = get_map_lvalue(m, i, 1);
sp = i;
free_svalue(i);
i->type = T_LVALUE;
i->u.lvalue = item;
return sp;
}
}
#endif /* MAPPINGS */
if (type == T_LVALUE) {
vec = vec->u.lvalue;
continue;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
} /* end for */
return sp;
}
static INLINE struct svalue *rindex_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *vecp;
int ind;
short type;
vecp = sp;
i = sp -1;
vec = vecp->u.lvalue;
if (i->type != T_NUMBER || (ind = i->u.number) <= 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
for(;;) {
type = vec->type;
if (type == T_POINTER) {
struct vector *v = vec->u.vec;
sp = i;
if ( (ind = v->size - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
i->type = T_LVALUE;
i->u.lvalue = &v->item[ind];
return sp;
}
if (type == T_STRING) {
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->x.string_type = STRING_MALLOC;
vec->u.string = p;
}
if ( (ind = _svalue_strlen(vec) - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
sp = i;
i->type = T_LVALUE;
i->u.lvalue = &special_lvalue.v;
special_lvalue.v.type = T_CHAR_LVALUE;
special_lvalue.v.u.string = &vec->u.string[ind];
return sp;
}
if (type == T_LVALUE) {
vec = vec->u.lvalue;
continue;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
return sp;
} /* end for */
}
struct svalue last_indexing_protector = { T_NUMBER };
/*
* Compute the address of an array element.
*/
static INLINE struct svalue *protected_index_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *vecp;
int ind;
short type;
vecp = sp;
i = sp -1;
vec = vecp->u.lvalue;
for (;;) {
type = vec->type;
if (type == T_POINTER) {
struct vector *v = vec->u.vec;
struct protected_lvalue *lvalue;
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp;
}
sp = i;
if (ind >= v->size) { ERROR ("Index out of bounds\n") return sp; }
v->ref++;
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = &v->item[ind];
lvalue->protector.type = T_POINTER;
lvalue->protector.u.vec = v;
i->type = T_LVALUE;
i->u.lvalue = &lvalue->v;
return sp;
}
if (type == T_STRING) {
struct protected_char_lvalue *val;
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->u.string = p;
}
if (ind > svalue_strlen(vec) ) {
vec->x.string_type = STRING_VOLATILE;
ERROR ("Index out of bounds\n")
return sp;
}
vec->x.string_type = STRING_VOLATILE;
val = (struct protected_char_lvalue *)xalloc(sizeof *val);
i->type = T_LVALUE;
i->u.protected_char_lvalue = val;
val->v.type = T_PROTECTED_CHAR_LVALUE;
val->v.u.string = &vec->u.string[ind];
val->lvalue = vec;
val->start = vec->u.string;
val->protector.type = T_INVALID;
sp = i;
return sp;
}
#ifdef MAPPINGS
if (type == T_MAPPING) {
struct svalue *item;
struct protected_lvalue *lvalue;
struct mapping *m;
m = vec->u.map;
if (m->num_values) {
item = get_map_lvalue(m, i, 1);
m->ref++;
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = item;
BUILD_MAP_PROTECTOR(lvalue->protector, m)
sp = i;
free_svalue(i);
i->type = T_LVALUE;
i->u.lvalue = &lvalue->v;
return sp;
}
}
#endif /* MAPPINGS */
if (type == T_LVALUE) {
vec = vec->u.lvalue;
continue;
}
if (type == T_PROTECTED_LVALUE) {
struct protected_lvalue *lvalue;
struct protected_char_lvalue *val;
lvalue = (struct protected_lvalue *)vec;
if (lvalue->v.u.lvalue->type != T_STRING) {
if (vec == sp->u.lvalue) {
free_protector_svalue(&last_indexing_protector);
last_indexing_protector = lvalue->protector;
vec = lvalue->v.u.lvalue;
xfree((char*)lvalue);
continue;
}
vec = lvalue->v.u.lvalue;
continue;
}
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
vec = lvalue->v.u.lvalue;
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->u.string = p;
}
vec->x.string_type = STRING_VOLATILE;
if (ind > svalue_strlen(vec) ) {
ERROR ("Index out of bounds\n")
return sp;
}
val = (struct protected_char_lvalue *)xalloc(sizeof *val);
i->type = T_LVALUE;
i->u.protected_char_lvalue = val;
val->v.type = T_PROTECTED_CHAR_LVALUE;
val->v.u.string = &vec->u.string[ind];
val->lvalue = vec;
val->start = vec->u.string;
if (lvalue == sp->u.protected_lvalue) {
sp = i;
val->protector = lvalue->protector;
xfree((char*)lvalue);
} else {
sp = i;
val->protector.type = T_INVALID;
}
return sp;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
return sp;
}
return sp;
}
static INLINE struct svalue *protected_rindex_lvalue(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec, *vecp;
int ind;
short type;
vecp = sp;
i = sp -1;
if (i->type != T_NUMBER || (ind = i->u.number) <= 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
vec = vecp->u.lvalue;
for (;;) {
type = vec->type;
if (type == T_POINTER) {
struct vector *v = vec->u.vec;
struct protected_lvalue *lvalue;
sp = i;
if ( (ind = v->size - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
v->ref++;
lvalue = (struct protected_lvalue *)xalloc(sizeof *lvalue);
lvalue->v.type = T_PROTECTED_LVALUE;
lvalue->v.u.lvalue = &v->item[ind];
lvalue->protector.type = T_POINTER;
lvalue->protector.u.vec = v;
i->type = T_LVALUE;
i->u.lvalue = &lvalue->v;
return sp;
}
if (type == T_STRING) {
struct protected_char_lvalue *val;
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->u.string = p;
}
if ( (ind = svalue_strlen(vec) - ind) < 0) {
vec->x.string_type = STRING_VOLATILE;
ERROR ("Index out of bounds\n")
return sp;
}
vec->x.string_type = STRING_VOLATILE;
val = (struct protected_char_lvalue *)xalloc(sizeof *val);
i->type = T_LVALUE;
i->u.protected_char_lvalue = val;
val->v.type = T_PROTECTED_CHAR_LVALUE;
val->v.u.string = &vec->u.string[ind];
val->lvalue = vec;
val->start = vec->u.string;
val->protector.type = T_INVALID;
sp = i;
return sp;
}
if (type == T_LVALUE) {
vec = vec->u.lvalue;
continue;
}
if (type == T_PROTECTED_LVALUE) {
struct protected_lvalue *lvalue;
struct protected_char_lvalue *val;
lvalue = (struct protected_lvalue *)vec;
if (lvalue->v.u.lvalue->type != T_STRING) {
if (vec == sp->u.lvalue) {
free_protector_svalue(&last_indexing_protector);
last_indexing_protector = lvalue->protector;
vec = lvalue->v.u.lvalue;
xfree((char*)lvalue);
continue;
}
vec = lvalue->v.u.lvalue;
continue;
}
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
vec = lvalue->v.u.lvalue;
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->u.string = p;
}
vec->x.string_type = STRING_VOLATILE;
if ( (ind = svalue_strlen(vec) - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
val = (struct protected_char_lvalue *)xalloc(sizeof *val);
i->type = T_LVALUE;
i->u.protected_char_lvalue = val;
val->v.type = T_PROTECTED_CHAR_LVALUE;
val->v.u.string = &vec->u.string[ind];
val->lvalue = vec;
val->start = vec->u.string;
if (lvalue == sp->u.protected_lvalue) {
sp = i;
val->protector = lvalue->protector;
xfree((char*)lvalue);
} else {
sp = i;
val->protector.type = T_INVALID;
}
return sp;
}
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(lvalue)Indexing on illegal type.\n");
return sp;
}
}
/*
* Compile lvalue information for a range.
*/
static struct svalue *range_lvalue(code, sp)
int code;
struct svalue *sp;
{
struct svalue *i, *vec, *vecp;
int ind1, ind2;
short type;
int size;
vecp = sp;
#ifdef DEBUG
if (vecp->type != T_LVALUE) {
inter_sp = sp;
error("wrong type to range_lvalue\n");
}
#endif
vec = vecp->u.lvalue;
for (;;) {
type = vec->type;
if (type == T_POINTER) {
special_lvalue.v.type = T_POINTER_RANGE_LVALUE;
size = vec->u.vec->size;
break;
} else if (type == T_STRING) {
special_lvalue.v.type = T_STRING_RANGE_LVALUE;
size = svalue_strlen(vec);
break;
} else if (type == T_LVALUE || type == T_PROTECTED_LVALUE) {
vec = vec->u.lvalue;
continue;
} else {
inter_sp = sp;
dump_trace(1);
error("(lvalue)range lvalue on illegal type.\n");
return sp;
}
}
special_lvalue.v.u.lvalue = vec;
special_lvalue.size = size;
if ((i = vecp-1)->type != T_NUMBER)
{
inter_sp = sp;
error("Illegal index\n");
return sp; /* flow control hint... */
}
if (code & 0xff) {
ind2 = size - i->u.number;
} else {
ind2 = i->u.number;
}
if (++ind2 < 0 || ind2 > size) {
inter_sp = sp;
error ("Index out of bounds\n");
return sp;
}
special_lvalue.index2 = ind2;
if ((--i)->type != T_NUMBER)
{
inter_sp = sp;
error("Illegal index\n");
return sp;
}
if (code & 0xff00) {
ind1 = size - i->u.number;
} else {
ind1 = i->u.number;
}
if (ind1 < 0 || ind1 > size) { /* Appending is allowed */
inter_sp = sp;
error ("Index out of bounds\n");
return sp;
}
special_lvalue.index1 = ind1;
sp = i;
i->type = T_LVALUE;
i->u.lvalue = &special_lvalue.v;
return sp;
}
/*
* Compile lvalue information for an unterminated range.
*/
static struct svalue *extract_lvalue(sp)
struct svalue *sp;
{
struct svalue *vec, *vecp;
int ind1;
short type;
int size;
vecp = sp;
#ifdef DEBUG
if (vecp->type != T_LVALUE) {
inter_sp = sp;
error("wrong type to range_lvalue\n");
}
#endif
vec = vecp->u.lvalue;
for (;;) {
type = vec->type;
if (type == T_POINTER) {
special_lvalue.v.type = T_POINTER_RANGE_LVALUE;
size = vec->u.vec->size;
break;
} else if (type == T_STRING) {
special_lvalue.v.type = T_STRING_RANGE_LVALUE;
size = svalue_strlen(vec);
break;
} else if (type == T_LVALUE || type == T_PROTECTED_LVALUE) {
vec = vec->u.lvalue;
continue;
} else {
inter_sp = sp;
dump_trace(1);
error("(lvalue)range lvalue on illegal type.\n");
return sp;
}
}
special_lvalue.v.u.lvalue = vec;
special_lvalue.size = size;
sp = vecp-1;
special_lvalue.index2 = size;
if (sp->type != T_NUMBER)
{
inter_sp = sp+1;
error("Illegal index\n");
return sp;
}
if (sp->u.number < 0) {
ind1 = size + sp->u.number;
} else {
ind1 = sp->u.number;
}
if (ind1 < 0 || ind1 > size) { /* Appending is allowed */
inter_sp = sp+1;
error ("Index out of bounds\n");
return sp;
}
special_lvalue.index1 = ind1;
sp->type = T_LVALUE;
sp->u.lvalue = &special_lvalue.v;
return sp;
}
static struct svalue *protected_range_lvalue(code, sp)
int code;
struct svalue *sp;
{
struct svalue *i, *vec, *vecp;
int ind1, ind2;
short type;
int size;
short lvalue_type;
struct svalue protector;
struct protected_range_lvalue *new_lvalue;
vecp = sp;
#ifdef DEBUG
if (vecp->type != T_LVALUE) {
inter_sp = sp;
error("wrong type to range_lvalue\n");
}
#endif
vec = vecp->u.lvalue;
protector.type = T_INVALID;
for (;;) {
type = vec->type;
if (type == T_POINTER) {
vec->u.vec->ref++;
lvalue_type = T_PROTECTED_POINTER_RANGE_LVALUE;
size = vec->u.vec->size;
break;
} else if (type == T_STRING) {
if (vec->x.string_type != STRING_MALLOC) {
char *p = string_copy(vec->u.string);
if (vec->x.string_type == STRING_SHARED)
free_string(vec->u.string);
vec->u.string = p;
}
vec->x.string_type = STRING_VOLATILE;
lvalue_type = T_PROTECTED_STRING_RANGE_LVALUE;
size = svalue_strlen(vec);
break;
} else if (type == T_LVALUE) {
vec = vec->u.lvalue;
continue;
} else if (type == T_PROTECTED_LVALUE) {
if (vec == vecp->u.lvalue) {
protector = ((struct protected_lvalue*)vec)->protector;
}
vec = vec->u.lvalue;
continue;
} else {
inter_sp = sp;
dump_trace(1);
error("(lvalue)range lvalue on illegal type.\n");
return sp;
}
}
if ((i = vecp-1)->type != T_NUMBER)
{
inter_sp = sp;
error("Illegal index\n");
return sp; /* flow control hint... */
}
if (code & 0xff) {
ind2 = size - i->u.number;
} else {
ind2 = i->u.number;
}
if (++ind2 < 0 || ind2 > size) {
inter_sp = sp;
error ("Index out of bounds\n");
return sp;
}
if ((--i)->type != T_NUMBER)
{
inter_sp = sp;
error("Illegal index\n");
return sp;
}
if (code & 0xff00) {
ind1 = size - i->u.number;
} else {
ind1 = i->u.number;
}
if (ind1 < 0 || ind1 > size) { /* Appending is allowed */
inter_sp = sp;
error ("Index out of bounds\n");
return sp;
}
new_lvalue = (struct protected_range_lvalue *)xalloc(sizeof *new_lvalue);
new_lvalue->v.type = lvalue_type;
new_lvalue->v.u = vec->u;
new_lvalue->protector = protector;
new_lvalue->lvalue = vec;
new_lvalue->index2 = ind2;
new_lvalue->index1 = ind1;
new_lvalue->size = size;
sp = i;
i->type = T_LVALUE;
i->u.protected_range_lvalue = new_lvalue;
return sp;
}
/*
* Compute an array element. If it is an destructed object, replace it by 0.
*/
static INLINE struct svalue *push_indexed_value(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec;
int ind;
i = sp;
vec = sp - 1;
switch (vec->type) {
case T_STRING:
{
if (i->type != T_NUMBER || (ind = i->u.number) < 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
if (ind > _svalue_strlen(vec))
ind = 0;
else
ind = vec->u.string[ind];
free_string_svalue(vec);
vec->type = T_NUMBER;
vec->u.number = ind;
sp = vec;
return sp;
}
case T_POINTER:
if (i->type != T_NUMBER || (ind = i->u.number) < 0)
{ ERROR("Illegal index\n") return sp; }
sp = vec;
if (ind >= vec->u.vec->size) ERROR ("Index out of bounds\n")
if (!--vec->u.vec->ref) {
struct svalue *p, tmp;
vec->u.vec->ref++;
p = &vec->u.vec->item[ind];
if (p->type == T_OBJECT && p->u.ob->flags & O_DESTRUCTED) {
free_object_svalue(p);
tmp.type = T_NUMBER;
tmp.u.number = 0;
} else {
tmp = *p;
}
p->type = T_NUMBER;
free_vector(vec->u.vec);
*vec = tmp;
return sp;
}
assign_checked_svalue_no_free(vec, &vec->u.vec->item[ind], sp, pc);
return sp;
#ifdef MAPPINGS
case T_MAPPING:
{
struct svalue *item;
struct mapping *m;
m = vec->u.map;
if (!m->num_values) {
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(value)Indexing on illegal type.\n");
return sp;
}
item = get_map_lvalue(m, i, 0);
free_svalue(i); /* must be here: consider i == vec->u.map not in i */
if (m->ref == 1) {
assign_svalue (&indexing_quickfix, item);
item = &indexing_quickfix;
}
free_mapping(m); /* This will make 'vec' invalid to use */
assign_checked_svalue_no_free(vec, item, sp, pc);
sp = vec;
return sp;
}
#endif /* MAPPINGS */
default:
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(value)Indexing on illegal type.\n");
return sp;
}
return sp;
}
static INLINE struct svalue *push_rindexed_value(sp, pc)
struct svalue *sp;
char *pc;
{
struct svalue *i, *vec;
int ind;
i = sp;
vec = sp - 1;
if (i->type != T_NUMBER || (ind = i->u.number) <= 0) {
ERROR("Illegal index\n")
return sp; /* flow control hint... */
}
switch (vec->type) {
case T_STRING:
{
if ( (ind = _svalue_strlen(vec) - ind) < 0 )
ind = 0;
else
ind = vec->u.string[ind];
free_string_svalue(vec);
vec->type = T_NUMBER;
vec->u.number = ind;
sp = vec;
return sp;
}
case T_POINTER:
sp = vec;
if ( (ind = vec->u.vec->size - ind) < 0) {
ERROR ("Index out of bounds\n")
return sp;
}
if (!--vec->u.vec->ref) {
struct svalue *p, tmp;
vec->u.vec->ref++;
p = &vec->u.vec->item[ind];
if (p->type == T_OBJECT && p->u.ob->flags & O_DESTRUCTED) {
free_object_svalue(p);
tmp.type = T_NUMBER;
tmp.u.number = 0;
} else {
tmp = *p;
}
p->type = T_NUMBER;
free_vector(vec->u.vec);
*vec = tmp;
return sp;
}
assign_checked_svalue_no_free(vec, &vec->u.vec->item[ind], sp, pc);
return sp;
default:
inter_sp = sp;
inter_pc = pc;
dump_trace(1);
error("(value)Indexing on illegal type.\n");
return sp;
}
return sp;
}
#ifdef OPCPROF
#define MAXOPC 512
static int opcount[MAXOPC];
#endif
/*
* Deallocate 'n' values from the stack.
*/
INLINE
static struct svalue *_pop_n_elems(n, sp)
int n;
struct svalue *sp;
{
#ifdef DEBUG
if (n < 0)
fatal("pop_n_elems: %d elements.\n", n);
#endif
for (; --n >= 0; )
pop_stack();
return sp;
}
static VOLATILE void bad_arg_pc(arg, instr, sp, pc)
int arg, instr;
struct svalue *sp;
char *pc;
{
ERRORF(("Bad argument %d to %s()\n", arg, get_f_name(instr)))
}
VOLATILE void bad_efun_arg(arg, instr, sp)
int arg, instr;
struct svalue *sp;
{
inter_sp = sp;
error("Bad argument %d to %s()\n", arg, get_f_name(instr));
}
INLINE
static void push_control_stack(sp, pc, fp)
struct svalue *sp;
char *pc;
struct svalue *fp;
{
if (csp >= &control_stack[MAX_USER_TRACE-1]) {
extern int num_error;
if (!num_error || csp == &control_stack[MAX_TRACE-1]) {
ERROR("Too deep recursion.\n")
}
}
csp++;
/* csp->funstart has to be set later, it is used only for tracebacks. */
csp->fp = fp;
csp->prog = current_prog;
/* csp->extern_call = 0; It is set by eval_instruction() */
csp->pc = pc;
csp->function_index_offset = function_index_offset;
csp->current_variables = current_variables;
csp->break_sp = break_sp;
}
/*
* Pop the control stack one element, and restore registers.
* extern_call must not be modified here, as it is used imediately after pop.
*/
static void pop_control_stack() {
#ifdef DEBUG
if (csp == control_stack - 1)
fatal("Popped out of the control stack");
#endif
if (current_prog = csp->prog) /* is 0 when we reach the bottom */
current_strings = current_prog->strings;
inter_pc = csp->pc;
inter_fp = csp->fp;
function_index_offset = csp->function_index_offset;
current_variables = csp->current_variables;
break_sp = csp->break_sp;
csp--;
}
/*
* Push a pointer to a vector on the stack. Note that the reference count
* is incremented. Newly created vectors normally have a reference count
* initialized to 1.
*/
void push_vector(v)
struct vector *v;
{
v->ref++;
inter_sp++;
inter_sp->type = T_POINTER;
inter_sp->u.vec = v;
}
void push_referenced_vector(v)
struct vector *v;
{
inter_sp++;
inter_sp->type = T_POINTER;
inter_sp->u.vec = v;
}
#define push_referenced_vector(v) ( \
sp++,\
sp->type = T_POINTER,\
sp->u.vec = (v)\
)
#define put_vector(v) ( \
sp->type = T_POINTER,\
(sp->u.vec = (v))->ref++\
)
#define put_referenced_vector(v) ( \
sp->type = T_POINTER,\
sp->u.vec = (v)\
)
#ifdef MAPPINGS
void push_referenced_mapping(m)
struct mapping *m;
{
inter_sp++;
inter_sp->type = T_MAPPING;
inter_sp->u.map = m;
}
#define push_mapping(m) ( \
sp++,\
sp->type = T_MAPPING,\
(sp->u.map = (m))->ref++\
)
void m_indices_filter(key, data, extra)
struct svalue *key, *data;
char *extra;
{
struct svalue **svpp = (struct svalue **)extra;
assign_svalue_no_free( (*svpp)++, key );
}
void m_values_filter(key, data, extra)
struct svalue *key, *data;
char *extra;
{
struct svalue **svpp = (struct svalue **)extra;
assign_svalue_no_free( (*svpp)++, data );
}
#endif /* MAPPINGS */
/*
* Push a string on the stack that is to be malloced.
*/
void push_string_malloced(p)
char *p;
{
struct svalue *sp;
char *s;
s = xalloc(strlen(p)+1);
strcpy(s, p);
sp = ++inter_sp;
sp->type = T_STRING;
sp->x.string_type = STRING_MALLOC;
sp->u.string = s;
}
static struct svalue *_push_string_malloced(p, sp)
char *p;
struct svalue *sp;
{
char *s;
s = xalloc(strlen(p)+1);
strcpy(s, p);
sp++;
sp->type = T_STRING;
sp->x.string_type = STRING_MALLOC;
sp->u.string = s;
return sp;
}
#define push_string_malloced(p) (sp = _push_string_malloced(p, sp))
#if 0
static struct svalue *_put_string_malloced(p, sp)
char *p;
struct svalue *sp;
{
char *s;
s = xalloc(strlen(p)+1);
strcpy(s, p);
sp->type = T_STRING;
sp->x.string_type = STRING_MALLOC;
sp->u.string = s;
return sp;
}
#define put_string_malloced(p) _put_string_malloced(p, sp)
#endif
/*
* Push a string on the stack that is to be shared.
*/
void push_string_shared(p)
char *p;
{
inter_sp++;
inter_sp->type = T_STRING;
inter_sp->x.string_type = STRING_SHARED;
inter_sp->u.string = make_shared_string(p);
}
/*
* Push a string on the stack that is already malloced.
*/
INLINE static struct svalue *_push_malloced_string(p, sp)
char *p;
struct svalue *sp;
{
sp++;
sp->type = T_STRING;
sp->x.string_type = STRING_MALLOC;
sp->u.string = p;
return sp;
}
INLINE void push_malloced_string(p)
char *p;
{
inter_sp++;
inter_sp->type = T_STRING;
inter_sp->x.string_type = STRING_MALLOC;
inter_sp->u.string = p;
}
static INLINE void put_malloced_string(p, sp)
char *p;
struct svalue *sp;
{
sp->type = T_STRING;
sp->x.string_type = STRING_MALLOC;
sp->u.string = p;
}
/*
* Push a string on the stack that is constant in the current stack frame.
*/
INLINE
struct svalue *_push_volatile_string(p, sp)
char *p;
struct svalue *sp;
{
sp++;
sp->type = T_STRING;
sp->x.string_type = STRING_VOLATILE;
sp->u.string = p;
return sp;
}
void push_volatile_string(p)
char *p;
{
inter_sp++;
inter_sp->type = T_STRING;
inter_sp->x.string_type = STRING_VOLATILE;
inter_sp->u.string = p;
}
#define push_volatile_string(s) (sp = _push_volatile_string((s), sp))
#if 0
/*
* Push a string on the stack that is already constant.
*/
INLINE
void push_constant_string(p)
char *p;
{
sp++;
sp->type = T_STRING;
sp->x.string_type = STRING_CONSTANT;
sp->u.string = p;
}
#endif
#define _push_constant_string _push_volatile_string
/*
* 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 int validate_shadowing(ob)
struct object *ob;
{
int i, j;
struct object *cob;
struct program *shadow, *victim;
struct svalue *ret;
cob = current_object;
shadow = cob->prog;
if (cob->flags & O_DESTRUCTED)
return 0;
if (ob->flags & O_SWAPPED)
load_ob_from_swap(ob);
victim = ob->prog;
if (cob->shadowing)
error("shadow: Already shadowing.\n");
if (cob->shadowed)
error("shadow: Can't shadow when shadowed.\n");
if (cob->super)
error("The shadow must not reside inside another object.\n");
if (ob->shadowing)
error("Can't shadow a shadow.\n");
if (ob == cob)
error("Can't shadow self.\n");
for (i = shadow->num_function_names; --i >= 0; ) {
uint32 flags;
char *name;
struct program *progp;
j = shadow->function_names[i];
flags = shadow->functions[j];
progp = shadow;
while (flags & NAME_INHERITED) {
struct inherit *inheritp;
inheritp = &progp->inherit[flags & INHERIT_MASK];
j -= inheritp->function_index_offset;
progp = inheritp->prog;
flags = progp->functions[j];
}
memcpy(
(char *)&name,
progp->program + (flags & FUNSTART_MASK) - 1 - sizeof name,
sizeof name
);
if (victim->functions[
find_function(name, victim)
] & TYPE_MOD_NO_MASK)
error("Illegal to shadow 'nomask' function \"%s\".\n", name);
}
inter_sp = _push_object(ob, inter_sp);
ret = apply_master_ob("query_allow_shadow", 1);
if (out_of_memory)
error("Out of memory\n");
if (!((ob->flags|cob->flags) & O_DESTRUCTED) &&
ret && !(ret->type == T_NUMBER && ret->u.number == 0))
{
return 1;
}
return 0;
}
int _privilege_violation(what, where, sp)
char *what;
struct svalue *where;
struct svalue *sp;
{
struct svalue *svp;
extern struct object *simul_efun_object;
if (current_object == master_ob) return 1;
if (current_object == simul_efun_object) return 1;
push_volatile_string(what);
push_object(current_object);
sp++;
assign_svalue_no_free(sp, where);
inter_sp = sp;
svp = apply_master_ob("privilege_violation", 3);
if (!svp || svp->type != T_NUMBER || svp->u.number < 0) {
inter_sp = sp-3;
error("privilege violation : %s\n", what);
}
return svp->u.number;
}
int privilege_violation4(what, whom, how, sp)
char *what;
struct object *whom;
char *how;
struct svalue *sp;
{
struct svalue *svp;
extern struct object *simul_efun_object;
if (current_object == master_ob) return 1;
if (current_object == simul_efun_object) return 1;
push_volatile_string(what);
push_object(current_object);
push_object(whom);
push_volatile_string(how);
inter_sp = sp;
svp = apply_master_ob("privilege_violation", 4);
if (!svp || svp->type != T_NUMBER || svp->u.number < 0) {
inter_sp = sp-4;
error("privilege violation : %s\n", what);
}
return svp->u.number;
}
static void do_trace_call(funstart)
unsigned char *funstart;
{
char *name;
memcpy((char *)&name, funstart - 1 - sizeof name, sizeof name);
do_trace("Call direct ", name, " ");
if (TRACEHB) {
if (TRACETST(TRACE_ARGS)) {
int i;
struct svalue *svp;
add_message(" with %d arguments: ", funstart[0]);
svp = inter_sp - csp->num_local_variables + 1;
for(i = funstart[0]; --i >= 0; ) {
print_svalue(svp++);
add_message(" ");
}
}
add_message("\n");
}
}
static void do_trace_return(sp)
struct svalue *sp;
{
tracedepth--;
if (trace_test(TRACE_RETURN)) {
inter_sp = sp;
do_trace("Return", "", "");
if (TRACEHB) {
if (TRACETST(TRACE_ARGS)) {
add_message(" with value: ");
print_svalue(sp);
}
add_message("\n");
}
}
(void)SET_TRACE_EXEC;
}
/*
* Argument is the function to execute. If it is defined by inheritance,
* then search for the real definition, and return it.
* There is a number of arguments on the stack. Normalize them and initialize
* local variables, so that the called function is pleased.
*/
static INLINE uint32 setup_new_frame1(fx, fun_ix_offs, var_ix_offs)
int fx;
int fun_ix_offs, var_ix_offs;
{
struct program *progp;
uint32 flags;
progp = current_prog;
flags = progp->functions[fx];
fun_ix_offs += fx;
while(flags & NAME_INHERITED) {
struct inherit *inheritp;
inheritp = &progp->inherit[flags & INHERIT_MASK];
progp = inheritp->prog;
fx -= inheritp->function_index_offset;
var_ix_offs += inheritp->variable_index_offset;
flags = progp->functions[fx];
}
current_prog = progp;
function_index_offset = fun_ix_offs - fx;
variable_index_offset = var_ix_offs;
return flags;
}
static INLINE struct svalue *setup_new_frame2(funstart, sp, pc)
unsigned char *funstart;
struct svalue *sp;
char *pc;
{
short i;
int num_arg;
num_arg = funstart[0];
/* Remove excessive arguments */
while(csp->num_local_variables > num_arg) {
free_svalue(sp--);
csp->num_local_variables--;
}
/* Correct number of arguments and local variables */
if (i = num_arg + funstart[1] - csp->num_local_variables) {
#if defined(__GNUC__) && __GNUC__ >= 2
/* there seems to be a problem with gcc 1.36 */
/* local structs are handled more efficiently. */
struct svalue const_0 = {T_NUMBER, 0, 0};
#else
#define const_0 const0
#endif
csp->num_local_variables += i;
sp++;
do {
*sp++ = const_0;
} while (--i);
sp--;
#undef const_0
}
if ( sp >= &start_of_stack[EVALUATOR_STACK_SIZE] )
STACK_OVERFLOW(sp, csp->fp, pc);
tracedepth++;
if (TRACEP(TRACE_CALL)) {
inter_sp = sp;
do_trace_call(funstart);
}
inter_fp = sp - csp->num_local_variables + 1;
break_sp = (short*)(sp+1);
return sp;
}
static uint32 setup_new_frame(fx, pc)
int fx;
char *pc;
{
uint32 flags;
flags = setup_new_frame1(fx, 0, 0);
inter_sp = setup_new_frame2(
current_prog->program + (flags & FUNSTART_MASK), inter_sp, pc
);
current_variables = current_object->variables + variable_index_offset;
current_strings = current_prog->strings;
return flags;
}
static void break_point(sp, fp)
struct svalue *sp, *fp;
{
if (sp - fp - csp->num_local_variables + 1 != 0)
fatal("Bad stack pointer.\n");
}
/* marion
* maintain a small and inefficient stack of error recovery context
* data structures.
* This routine is called in three different ways:
* push=-1 Pop the stack.
* push=1 push the stack.
* push=0 No error occured, so the pushed value does not have to be
* restored. The pushed value can simply be popped into the void.
*
* The stack is implemented as a linked list of stack-objects, allocated
* from the heap, and deallocated when popped.
*/
/* Amylaar: I like it a bit less inefficient. Besides, the value of
* error_recovery_pointer (as a pointer to an opaque structure)
* is needed to cure an old bug in the player_parser.
*/
struct catch_context {
struct error_recovery_info recovery_info; /* must be first */
struct control_stack *save_csp;
struct object *save_command_giver;
struct svalue *save_sp;
};
static INLINE jmp_buf *push_error_context (sp)
struct svalue *sp;
{
struct catch_context *p;
/*
* Save some global variables that must be restored separately
* after a longjmp. The stack will have to be manually popped all
* the way.
*/
p = (struct catch_context *)xalloc (sizeof *p);
p->save_sp = sp;
p->save_csp = csp;
p->save_command_giver = command_giver;
p->recovery_info.last = error_recovery_pointer;
p->recovery_info.type = ERROR_RECOVERY_CATCH;
error_recovery_pointer = &p->recovery_info;
#if defined(AMIGA) && defined(_DCC)
return p->recovery_info.context;
#else
return &p->recovery_info.context;
#endif
}
static INLINE void pop_error_context (sp)
struct svalue *sp;
{
struct catch_context *p;
p = (struct catch_context *)error_recovery_pointer;
#ifdef DEBUG
if (p->recovery_info.type != ERROR_RECOVERY_CATCH)
fatal("Catch: error context stack underflow");
if (csp != p->save_csp-1)
fatal("Catch: Lost track of csp");
#if 0
/*
* This test is not valid! The statement catch(exec("...")) will
* change the value of command_giver.
*/
if (command_giver != p->save_command_giver)
fatal("Catch: Lost track of command_giver");
#endif
#endif
error_recovery_pointer = p->recovery_info.last;
xfree ((char *)p);
}
struct svalue *pull_error_context (sp)
struct svalue *sp;
{
/* They did a throw() or error. That means that the control
* stack must be restored manually here.
*/
struct catch_context *p;
struct control_stack *csp2;
p = (struct catch_context *)error_recovery_pointer;
if (p->recovery_info.type != ERROR_RECOVERY_CATCH)
fatal("Catch: error context stack underflow");
csp2 = p->save_csp;
while (++csp2 <= csp) {
if (csp2->extern_call) {
previous_ob = csp2->prev_ob;
current_object = csp2->ob;
break;
}
}
csp = p->save_csp;
pop_n_elems (sp - p->save_sp);
command_giver = p->save_command_giver;
error_recovery_pointer = p->recovery_info.last;
xfree ((char *)p);
return sp;
}
struct program *search_inherited(str, prg, offpnt)
char *str;
struct program *prg;
int *offpnt;
{
struct program *tmp;
int i;
#ifdef DEBUG
if (d_flag) {
debug_message("search_inherited started\n");
debug_message("searching for PRG(%s) in PRG(%s)\n",str,prg->name);
debug_message("num_inherited=%d\n", prg->num_inherited);
}
#endif
for (i=0; i < prg->num_inherited; i++) {
#ifdef DEBUG
if (d_flag) {
debug_message("index %d:\n",i);
debug_message("checking PRG(%s)\n", prg->inherit[i].prog->name);
}
#endif
if ( strcmp(str, prg->inherit[i].prog->name ) == 0 ) {
#ifdef DEBUG
if (d_flag)
debug_message("match found\n");
#endif
offpnt[0] = prg->inherit[i].variable_index_offset;
offpnt[1] = prg->inherit[i].function_index_offset;
return prg->inherit[i].prog;
} else if ( tmp = search_inherited(str, prg->inherit[i].prog,
offpnt) )
{
#ifdef DEBUG
if (d_flag)
debug_message("deferred match found\n");
#endif
offpnt[0] += prg->inherit[i].variable_index_offset;
offpnt[1] += prg->inherit[i].function_index_offset;
return tmp;
}
}
#ifdef DEBUG
if (d_flag)
debug_message("search_inherited failed\n");
#endif
return (struct program *)0;
}
struct replace_ob *retrieve_replace_program_entry() {
struct replace_ob *r_ob;
for (r_ob = obj_list_replace; r_ob; r_ob = r_ob->next) {
if (r_ob->ob == current_object) {
return r_ob;
}
}
return 0;
}
/*
* When a vector is given as argument to an efun, all items has to be
* checked if there would be an destructed object.
* A bad problem currently is that a vector can contain another vector, so this
* should be tested too. But, there is currently no prevention against
* recursive vectors, which means that this can not be tested. Thus, the game
* may crash if a vector contains a vector that contains a destructed object
* and this top-most vector is used as an argument to an efun.
*/
/* The game won't crash when doing simple operations like assign_svalue
* on a destructed object. You have to watch out, of course, that you don't
* apply a function to it.
* to save space it is preferable that destructed objects are freed soon.
* amylaar
*/
void check_for_destr(v)
struct vector *v;
{
int i;
struct svalue *p;
for (p = v->item, i = v->size; --i >= 0 ; p++ ) {
if (p->type != T_OBJECT)
continue;
if (!(p->u.ob->flags & O_DESTRUCTED))
continue;
zero_object_svalue(p);
}
}
#ifdef DEBUG
#define GET_NUM_ARG if (num_arg != EXTRACT_UCHAR(pc-1)) {\
fprintf(stderr, "%d vs. %d\n", num_arg, EXTRACT_UCHAR(pc-1));\
fatal("argument count error\n");}
#else /* DEBUG */
#define GET_NUM_ARG num_arg = EXTRACT_UCHAR(pc); pc++;
#endif /* DEBUG */
#define TYPE_TEST1(arg, t) if ( (arg)->type != t ) goto bad_arg_1;
#define TYPE_TEST2(arg, t) if ( (arg)->type != t ) goto bad_arg_2;
#define TYPE_TEST3(arg, t) if ( (arg)->type != t ) goto bad_arg_3;
/*
* Evaluate instructions at address 'p'. All program offsets are
* to current_prog->program. 'current_prog' must be setup before
* call of this function.
*
* There must not be destructed objects on the stack. The destruct_object()
* function will automatically remove all occurences. The effect is that
* all called efuns knows that they won't have destructed objects as
* arguments.
*/
#define push_number(n) (sp = _push_number(n, sp))
#define push_malloced_string(s) (sp = _push_malloced_string((s), sp))
#define privilege_violation(what, where) (\
inter_pc = pc,\
_privilege_violation(what, where, sp)\
)
#ifdef TRACE_CODE
/* keep some more information in the core than we show... */
int previous_instruction[TOTAL_TRACE_LENGTH];
int stack_size[TOTAL_TRACE_LENGTH];
char *previous_pc[TOTAL_TRACE_LENGTH];
struct program *previous_programs[TOTAL_TRACE_LENGTH];
struct object *previous_objects[TOTAL_TRACE_LENGTH];
static int last;
#endif
static void eval_instruction(first_instruction, sp)
char *first_instruction;
register struct svalue *sp;
/* gcc feels better about setjmp() when variables are declared register */
{
register char *pc;
register struct svalue *fp;
int num_arg;
int instruction;
#ifdef DEBUG
struct svalue *expected_stack;
#endif
struct svalue *argp;
/* Next F_RETURN at this level will return out of eval_instruction() */
csp->extern_call = 1;
pc = first_instruction;
fp = inter_fp;
(void)SET_TRACE_EXEC;
again:
instruction = EXTRACT_UCHAR(pc);
#ifdef TRACE_CODE
previous_instruction[last] = instruction + F_OFFSET;
previous_pc[last] = pc;
stack_size[last] = sp - fp - csp->num_local_variables;
if (previous_objects[last])
if (d_flag> 4) {
free_object(previous_objects[last], "TRACE_CODE");
} else {
if (!--previous_objects[last]->ref) {
previous_objects[last]->ref++;
free_object(previous_objects[last], "TRACE_CODE");
}
}
previous_objects[last] = current_object;
if (d_flag > 4)
add_ref(current_object, "TRACE_CODE");
else
current_object->ref++;
previous_programs[last] = current_prog;
if (++last == TOTAL_TRACE_LENGTH)
last = 0;
#endif
pc++;
#ifdef SMALLOC_LPC_TRACE
inter_pc = pc;
#endif
if (++eval_cost >= 0) {
if (eval_cost >= MIN_TRACE_COST && eval_cost < MAX_TRACE_COST) {
if (TRACE_EXEC_P) {
inter_sp = sp;
do_trace("Exec ", get_f_name(instruction), "\n");
}
} else {
if (eval_cost >= MAX_TRACE_COST) {
eval_cost -= MAX_TRACE_COST;
assigned_eval_cost -= MAX_TRACE_COST;
}
printf("eval_cost too big %d\n", eval_cost - initial_eval_cost);
assign_eval_cost();
if (error_recovery_pointer->type <= ERROR_RECOVERY_BACKEND) {
CLEAR_EVAL_COST;
}
inter_pc = pc;
inter_fp = fp;
ERROR("Too long evaluation. Execution aborted.\n")
}
}
/*
* Execute current instruction. Note that all functions callable
* from LPC must return a value or be declared void. This does not apply
* to control instructions, like F_JUMP.
*/
#if defined( DEBUG )
if (instrs[instruction].min_arg != instrs[instruction].max_arg) {
num_arg = EXTRACT_UCHAR(pc);
pc++;
} else {
/*
* Safety measure. It is supposed that the evaluator knows
* the number of arguments.
*/
num_arg = -1;
}
if (num_arg != -1) {
expected_stack = sp - num_arg +
( instrs[instruction].ret_type == TYPE_VOID ? 0 : 1 );
} else {
expected_stack = 0;
}
#endif /* DEBUG */
#ifdef OPCPROF
if (instruction >= 0 && instruction < MAXOPC) opcount[instruction]++;
#endif
/*
* Execute the instructions. The number of arguments are correct,
* and the type of the two first arguments are also correct.
*/
switch(instruction) {
default:
fatal("Undefined instruction %s (%d)\n", get_f_name(instruction),
instruction);
/*NOTREACHED*/
bad_arg_1: bad_arg_pc(1, instruction, sp, pc);
bad_arg_2: bad_arg_pc(2, instruction, sp, pc);
bad_arg_3: bad_arg_pc(3, instruction, sp, pc);
bad_left: ERRORF(("Bad left type to %s.\n", get_f_name(instruction)))
bad_right: ERRORF(("Bad right type to %s.\n", get_f_name(instruction)))
/*NOTREACHED*/
return; /* hint for data flow analysis */
#ifdef F_ILLEGAL
case 255:
CASE(F_ILLEGAL);
fatal("Illegal instruction\n");
/*NOTREACHED*/
#endif /* F_ILLEGAL */
#ifdef F_SPRINTF
CASE(F_SPRINTF);
{
char *s;
/*
* string_print_formatted() returns a pointer to it's internal
* buffer, or to an internal constant... Either way, it must
* be copied before it's returned as a string.
*/
assign_eval_cost();
GET_NUM_ARG
inter_pc = pc;
inter_sp = sp;
TYPE_TEST1(sp - num_arg + 1, T_STRING)
s = string_print_formatted((sp-num_arg+1)->u.string,
num_arg-1, sp-num_arg+2);
pop_n_elems(num_arg);
if (!s) push_number(0);
else push_malloced_string(string_copy(s));
break;
}
#endif /* F_SPRINTF */
#ifdef F_PRINTF
CASE(F_PRINTF);
assign_eval_cost();
GET_NUM_ARG
inter_pc = pc;
inter_sp = sp;
TYPE_TEST1(sp - num_arg + 1, T_STRING)
add_message("%s", string_print_formatted((sp-num_arg+1)->u.string,
num_arg-1, sp-num_arg+2));
pop_n_elems(num_arg);
break;
#endif /* F_PRINTF */
CASE(F_REGEXP);
{
struct vector *v;
TYPE_TEST1(sp-1, T_POINTER)
TYPE_TEST2(sp, T_STRING)
v = match_regexp((sp-1)->u.vec, sp->u.string);
pop_stack();
free_svalue(sp);
if (v == 0)
put_number(0);
else {
put_referenced_vector(v);
}
break;
}
CASE(F_SHADOW);
{
struct object *ob;
TYPE_TEST1(sp-1, T_OBJECT)
TYPE_TEST2(sp , T_NUMBER)
sp--;
ob = sp->u.ob;
free_object_svalue(sp);
if (sp[1].u.number == 0) {
ob = ob->shadowed;
if (ob)
put_object(ob);
else
put_number(0);
break;
}
sp->type = T_NUMBER; /* validate_shadowing might destruct ob */
assign_eval_cost();
inter_sp = sp;
inter_pc = pc;
if (validate_shadowing(ob)) {
/*
* The shadow is entered first in the chain.
*/
if (ob->interactive)
ob->interactive->catch_tell_activ = 1;
while (ob->shadowed)
ob = ob->shadowed;
current_object->shadowing = ob;
ob->shadowed = current_object;
put_object(ob);
break;
}
sp->u.number = 0;
break;
}
CASE(F_POP_VALUE);
pop_stack();
break;
CASE(F_DUP);
sp++;
assign_svalue_no_free(sp, sp-1);
break;
#ifdef F_JUMP_WHEN_ZERO
CASE(F_JUMP_WHEN_ZERO);
{
unsigned short offset[2];
((char *)offset)[0] = pc[0];
((char *)offset)[1] = pc[1];
if (sp->type == T_NUMBER && sp->u.number == 0)
pc = current_prog->program + offset[0];
else
pc += 2;
pop_stack();
break;
}
#endif /* F_JUMP_WHEN_ZERO */
#ifdef F_JUMP
CASE(F_JUMP);
{
unsigned short offset[2];
((char *)offset)[0] = pc[0];
((char *)offset)[1] = pc[1];
pc = current_prog->program + offset[0];
break;
}
#endif /* F_JUMP */
#ifdef F_JUMP_WHEN_NON_ZERO
CASE(F_JUMP_WHEN_NON_ZERO);
{
unsigned short offset[2];
((char *)offset)[0] = pc[0];
((char *)offset)[1] = pc[1];
if (sp->type == T_NUMBER && sp->u.number == 0)
pc += 2;
else
pc = current_prog->program + offset[0];
pop_stack();
break;
}
#endif /* F_JUMP_WHEN_NON_ZERO */
CASE(F_LBRANCH);
{
short offset[2];
((char *)offset)[0] = pc[0];
((char *)offset)[1] = pc[1];
pc += offset[0];
break;
}
CASE(F_LBRANCH_WHEN_ZERO);
{
short offset[2];
if (sp->type == T_NUMBER && sp->u.number == 0) {
((char *)offset)[0] = pc[0];
((char *)offset)[1] = pc[1];
pc += offset[0];
sp--;
break;
}
pc += 2;
pop_stack();
break;
}
CASE(F_LBRANCH_WHEN_NON_ZERO);
{
short offset[2];
if (sp->type != T_NUMBER || sp->u.number != 0) {
((char *)offset)[0] = pc[0];
((char *)offset)[1] = pc[1];
pc += offset[0];
pop_stack();
break;
}
pc += 2;
sp--;
break;
}
CASE(F_BRANCH);
{
pc += EXTRACT_UCHAR(pc)+1;
break;
}
CASE(F_BRANCH_WHEN_ZERO);
{
if (sp->type == T_NUMBER) {
if (sp->u.number == 0) {
sp--;
pc += EXTRACT_UCHAR(pc) + 1;
break;
}
sp--;
pc++;
break;
} else {
free_svalue(sp);
sp--;
pc++;
break;
}
}
CASE(F_BRANCH_WHEN_NON_ZERO);
{
if (sp->type == T_NUMBER) {
if (sp->u.number == 0) {
sp--;
pc++;
break;
}
} else {
free_svalue(sp);
}
sp--;
pc += EXTRACT_UCHAR(pc) + 1;
break;
}
CASE(F_BBRANCH_WHEN_ZERO);
{
if (sp->type == T_NUMBER && sp->u.number == 0) {
sp--;
pc -= EXTRACT_UCHAR(pc);
break;
}
pc += 1;
pop_stack();
break;
}
CASE(F_BBRANCH_WHEN_NON_ZERO);
{
if (sp->type == T_NUMBER) {
if (sp->u.number == 0) {
pc += 1;
sp--;
break;
}
} else free_svalue(sp);
sp--;
pc -= EXTRACT_UCHAR(pc);
break;
}
CASE(F_LOR);
{
if (sp->type == T_NUMBER && sp->u.number == 0)
sp--;
else
pc += EXTRACT_UCHAR(pc);
pc++;
break;
}
CASE(F_LAND);
{
if (sp->type == T_NUMBER) {
if (sp->u.number == 0) {
pc += EXTRACT_UCHAR(pc) + 1;
break;
}
} else {
free_svalue(sp);
}
sp--;
pc++;
break;
}
#ifdef F_INDIRECT
CASE(F_INDIRECT);
#ifdef DEBUG
if (sp->type != T_LVALUE)
fatal("Bad type to F_INDIRECT\n");
#endif
assign_svalue(sp, sp->u.lvalue);
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
if (sp->type == T_OBJECT && (sp->u.ob->flags & O_DESTRUCTED)) {
free_svalue(sp);
*sp = const0;
}
break;
#endif /* F_INDIRECT */
CASE(F_IDENTIFIER);
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
sp++;
assign_checked_svalue_no_free(sp, find_value((int)(EXTRACT_UCHAR(pc)
)), sp, pc);
pc++;
break;
CASE(F_PUSH_IDENTIFIER_LVALUE);
sp++;
sp->type = T_LVALUE;
sp->u.lvalue = find_value((int)(EXTRACT_UCHAR(pc) ));
pc++;
break;
CASE(F_VIRTUAL_VARIABLE);
sp++;
assign_checked_svalue_no_free(
sp,
find_virtual_value((int)(EXTRACT_UCHAR(pc))),
sp, pc
);
pc++;
break;
CASE(F_PUSH_VIRTUAL_VARIABLE_LVALUE);
sp++;
sp->type = T_LVALUE;
sp->u.lvalue = find_virtual_value((int)(EXTRACT_UCHAR(pc) ));
pc++;
break;
CASE(F_PUSH_INDEXED_LVALUE);
sp = push_indexed_lvalue(sp, pc);
break;
CASE(F_PUSH_RINDEXED_LVALUE);
sp = push_rindexed_lvalue(sp, pc);
break;
CASE(F_INDEX_LVALUE);
sp = index_lvalue(sp, pc);
break;
CASE(F_RINDEX_LVALUE);
sp = rindex_lvalue(sp, pc);
break;
CASE(F_INDEX);
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case,
* push_indexed_value will replace it by 0 .
*/
sp = push_indexed_value(sp, pc);
break;
CASE(F_RINDEX);
sp = push_rindexed_value(sp, pc);
break;
CASE(F_LOCAL);
/*
* Fetch value of a variable. It is possible that it is a variable
* that points to a destructed object. In that case, it has to
* be replaced by 0.
*/
sp++;
assign_checked_svalue_no_free(sp, fp + EXTRACT_UCHAR(pc), sp, pc);
pc++;
break;
CASE(F_PUSH_LOCAL_VARIABLE_LVALUE);
sp++;
sp->type = T_LVALUE;
sp->u.lvalue = fp + EXTRACT_UCHAR(pc);
pc++;
break;
CASE(F_RETURN0);
push_number(0);
/* fall through */
CASE(F_RETURN);
{
int i;
struct svalue *svp;
svp = sp;
/*
* Deallocate frame and return.
*/
for (i = csp->num_local_variables; --i >= 0; )
free_svalue(--sp);
#ifdef DEBUG
if (sp != fp)
fatal("Bad stack at F_RETURN\n"); /* marion */
#endif
*sp = *svp; /* This way, the same ref counts are maintained */
if (current_prog = csp->prog) /* is 0 when we reach the bottom */
current_strings = current_prog->strings;
function_index_offset = csp->function_index_offset;
current_variables = csp->current_variables;
break_sp = csp->break_sp;
if (csp->extern_call) {
ASSIGN_EVAL_COST
current_object = csp->ob;
previous_ob = csp->prev_ob;
inter_pc = csp->pc;
inter_fp = csp->fp;
if (trace_level & (TRACE_RETURN|TRACE_EXEC))
do_trace_return(sp);
csp--;
inter_sp = sp;
return;
}
if (trace_level & (TRACE_RETURN|TRACE_EXEC))
do_trace_return(sp);
pc = csp->pc;
fp = csp->fp;
csp--;
break;
}
CASE(F_BREAK_POINT);
break_point(sp, fp);/* generated by lang.y when -d. Will check stack. */
break;
#ifdef F_CLONE_OBJECT
CASE(F_CLONE_OBJECT);
{
struct object *ob;
assign_eval_cost();
TYPE_TEST1(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
ob = clone_object(sp->u.string);
free_svalue(sp);
if (ob) {
sp->type = T_OBJECT;
sp->u.ob = ob;
add_ref(ob, "F_CLONE_OBJECT");
} else {
put_number(0);
}
break;
}
#endif /* F_CLONE_OBJECT */
CASE(F_AGGREGATE);
{
int i;
struct vector *v;
unsigned short num[2];
struct svalue *value, *item;
((char *)num)[0] = pc[0];
((char *)num)[1] = pc[1];
pc += 2;
i = num[0];
v = allocate_array(i);
sp = value = sp - i + 1;
item = v->item;
while (--i >= 0)
transfer_svalue_no_free_spc(item++, value++, sp, pc);
sp->type = T_POINTER;
sp->u.vec = v; /* Ref count already initialized */
break;
}
#ifdef MAPPINGS
CASE(F_M_AGGREGATE);
{
int i, j;
struct mapping *m;
struct svalue *data;
int num_values;
struct svalue *value;
{
unsigned short num[2];
((char *)num)[0] = pc[0];
((char *)num)[1] = pc[1];
((char *)num)[2] = pc[2];
((char *)num)[3] = pc[3];
pc += 4;
i = num[0];
num_values = num[1];
}
#ifndef _DCC /* DICE understands only a subset of C . */
if (0) {
CASE(F_M_CAGGREGATE);
i = EXTRACT_UCHAR(pc);
num_values = EXTRACT_UCHAR(pc+1);
pc += 2;
}
#endif /* _DCC */
m = allocate_mapping(i, num_values);
sp = value = sp - (i * (num_values+1)) + 1;
while (--i >= 0) {
data = get_map_lvalue(m, value, 1);
free_svalue(value++);
for (j = num_values; --j >= 0;) {
if (data->type != T_NUMBER)
free_svalue(data);
transfer_svalue_no_free_spc(data++, value++, sp, pc);
}
}
sp->type = T_MAPPING;
sp->u.map = m;
break;
}
#ifdef _DCC
CASE(F_M_CAGGREGATE);
{
int i, j;
struct mapping *m;
struct svalue *data;
int num_values;
struct svalue *value;
i = EXTRACT_UCHAR(pc);
num_values = EXTRACT_UCHAR(pc+1);
pc += 2;
m = allocate_mapping(i, num_values);
sp = value = sp - (i * (num_values+1)) + 1;
while (--i >= 0) {
data = get_map_lvalue(m, value, 1);
free_svalue(value++);
for (j = num_values; --j >= 0;) {
if (data->type != T_NUMBER)
free_svalue(data);
transfer_svalue_no_free_spc(data++, value++, sp, pc);
}
}
sp->type = T_MAPPING;
sp->u.map = m;
break;
}
#endif /* _DCC */
CASE(F_FILTER_MAPPING);
GET_NUM_ARG
sp = filter_mapping(sp, num_arg);
break;
CASE(F_MAPPINGP);
{
int i;
i = sp->type == T_MAPPING;
free_svalue(sp);
put_number(i);
break;
}
CASE(F_MKMAPPING);
{
int i, length, num_values;
struct mapping *m;
struct svalue *key;
GET_NUM_ARG
length = MAX_ARRAY_SIZE;
for (i = -num_arg; ++i <= 0; ) {
if ( sp[i].type != T_POINTER )
bad_arg_pc(i+num_arg, instruction, sp, pc);
if (length > sp[i].u.vec->size)
length = sp[i].u.vec->size;
}
num_values = num_arg - 1;
m = allocate_mapping(length, num_values);
key = &(sp-num_values)->u.vec->item[length];
while (--length >= 0) {
struct svalue *dest;
dest = get_map_lvalue(m, --key, 1);
for (i = -num_values; ++i <= 0; )
assign_svalue_no_free(dest++, &sp[i].u.vec->item[length]);
}
pop_n_elems(num_arg);
push_mapping(m);
sp->u.map->ref--; /* This will make ref count == 1 */
break;
}
CASE(F_M_INDICES);
{
struct mapping *m;
struct vector *v;
TYPE_TEST1(sp, T_MAPPING)
m = sp->u.map;
check_map_for_destr(m);
inter_pc = pc;
inter_sp = sp;
v = m_indices(m);
free_mapping(m);
put_referenced_vector(v);
break;
}
CASE(F_M_VALUES);
{
struct mapping *m;
struct vector *v;
struct svalue *svp;
mp_int size;
if (sp->type != T_MAPPING || (m = sp->u.map)->num_values < 1)
goto bad_arg_1;
check_map_for_destr(m);
size = m->condensed->string_size / sizeof(char *) +
m->condensed->misc_size / sizeof(struct svalue) +
(m->hash ? m->hash->used - m->hash->condensed_deleted : 0);
v = allocate_array(size);
svp = v->item;
walk_mapping(m, m_values_filter, (char *)&svp);
free_mapping(m);
put_referenced_vector(v);
break;
}
CASE(F_M_DELETE);
{
struct mapping *m;
TYPE_TEST1(sp-1, T_MAPPING)
m = (sp-1)->u.map;
remove_mapping(m, sp);
pop_stack();
/* leave the mapping unaltered on the stack */
break;
}
CASE(F_MAP_MAPPING);
GET_NUM_ARG
sp = map_mapping(sp, num_arg);
break;
#endif /* MAPPINGS */
CASE(F_TAIL);
assign_eval_cost();
TYPE_TEST1(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
if (tail(sp->u.string))
assign_svalue(sp, &const1);
else
assign_svalue(sp, &const0);
break;
CASE(F_CALL_FUNCTION_BY_ADDRESS);
{
unsigned short func_index[2];
unsigned short func_offset;
uint32 flags;
unsigned char *funstart;
((char *)func_index)[0] = pc[0];
((char *)func_index)[1] = pc[1];
pc += 2;
func_offset = func_index[0] + function_index_offset;
/*
* Find the function in the function table. As the function may have
* been redefined by inheritance, we must look in the last table,
* which is pointed to by current_object.
*/
#ifdef DEBUG
if (func_offset >= current_object->prog->num_functions * sizeof(struct function))
fatal("Illegal function index\n");
#endif
/* NOT current_prog, which can be an inherited object. */
flags = current_object->prog->functions[func_offset];
if (flags & NAME_CROSS_DEFINED) {
func_offset += (flags & INHERIT_MASK) - (INHERIT_MASK + 1 >> 1);
}
/* Save all important global stack machine registers */
push_control_stack(sp, pc+1, fp);
/* This assigment must be done after push_control_stack() */
current_prog = current_object->prog;
/*
* If it is an inherited function, search for the real
* definition.
*/
csp->num_local_variables = EXTRACT_UCHAR(pc);
flags = setup_new_frame1(func_offset, 0, 0);
funstart = current_prog->program + (flags & FUNSTART_MASK);
csp->funstart = funstart;
sp = setup_new_frame2(funstart, sp, pc);
current_variables = current_object->variables + variable_index_offset;
current_strings = current_prog->strings;
fp = inter_fp;
pc = funstart + 2;
csp->extern_call = 0;
break;
}
CASE(F_CALL_EXPLICIT_INHERITED);
{
unsigned short tmp_ushort[2];
unsigned short prog_index, func_index;
uint32 flags;
unsigned char *funstart;
struct inherit *inheritp;
((char *)tmp_ushort)[0] = pc[0];
((char *)tmp_ushort)[1] = pc[1];
((char *)tmp_ushort)[2] = pc[2];
((char *)tmp_ushort)[3] = pc[3];
pc += 4;
prog_index = tmp_ushort[0];
func_index = tmp_ushort[1];
inheritp = ¤t_prog->inherit[prog_index];
#ifdef DEBUG
if (func_index >= inheritp->prog->num_functions) {
fprintf(stderr, "program index : %d\n", prog_index);
fprintf(stderr, "function index: %d\n", func_index);
fprintf(stderr, "#functions : %d\n", inheritp->prog->num_functions);
fatal("Illegal function index\n");
}
#endif
/* Save all important global stack machine registers */
push_control_stack(sp, pc+1, fp);
/* This assigment must be done after push_control_stack() */
current_prog = inheritp->prog;
/*
* If it is an inherited function, search for the real
* definition.
*/
csp->num_local_variables = EXTRACT_UCHAR(pc);
flags = setup_new_frame1(
func_index,
function_index_offset + inheritp->function_index_offset,
inheritp->variable_index_offset
);
funstart = current_prog->program + (flags & FUNSTART_MASK);
csp->funstart = funstart;
sp = setup_new_frame2(funstart, sp, pc);
fp = inter_fp;
current_variables += variable_index_offset;
pc = funstart + 2;
current_strings = current_prog->strings;
csp->extern_call = 0;
break;
}
CASE(F_SAVE_OBJECT);
assign_eval_cost();
TYPE_TEST1(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
save_object(current_object, sp->u.string);
pop_stack();
break;
CASE(F_FIND_OBJECT);
{
struct object *ob;
TYPE_TEST1(sp, T_STRING)
ob = find_object2(sp->u.string);
free_svalue(sp);
if (ob)
put_object(ob);
else
put_number(0);
break;
}
CASE(F_FIND_PLAYER);
{
struct object *ob;
TYPE_TEST1(sp, T_STRING)
ob = find_living_object(sp->u.string, 1);
free_svalue(sp);
if (!ob)
put_number(0);
else
put_object(ob);
break;
}
CASE(F_WRITE_FILE);
{
int i;
assign_eval_cost();
TYPE_TEST1(sp-1, T_STRING)
TYPE_TEST2(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
i = write_file((sp-1)->u.string, sp->u.string);
pop_stack();
free_svalue(sp);
put_number(i);
break;
}
CASE(F_READ_FILE);
{
char *str;
struct svalue *arg;
int start, len;
assign_eval_cost();
GET_NUM_ARG
inter_pc = pc;
inter_sp = sp;
arg = sp- num_arg + 1;
TYPE_TEST1(arg, T_STRING)
start = 0; len = 0;
if (num_arg > 1) {
TYPE_TEST2(arg+1, T_NUMBER)
start = arg[1].u.number;
if (num_arg == 3) {
if (arg[2].type != T_NUMBER)
goto bad_arg_3;
len = arg[2].u.number;
sp--;
}
sp--;
}
str = read_file(arg[0].u.string, start, len);
pop_stack();
if (str == 0)
push_number(0);
else {
push_malloced_string(str);
}
break;
}
CASE(F_READ_BYTES);
{
char *str;
struct svalue *arg;
int start, len;
assign_eval_cost();
GET_NUM_ARG
inter_pc = pc;
inter_sp = sp;
arg = sp- num_arg + 1;
TYPE_TEST1(arg, T_STRING)
start = 0; len = 0;
if (num_arg > 1) {
TYPE_TEST2(arg+1, T_NUMBER)
start = arg[1].u.number;
if (num_arg == 3) {
if (arg[2].type != T_NUMBER)
goto bad_arg_2;
len = arg[2].u.number;
sp--;
}
sp--;
}
str = read_bytes(arg[0].u.string, start, len);
pop_stack();
if (str == 0)
push_number(0);
else {
push_string_malloced(str);
xfree(str);
}
break;
}
CASE(F_WRITE_BYTES);
{
int i;
assign_eval_cost();
TYPE_TEST1(sp-2, T_STRING)
TYPE_TEST2(sp-1, T_NUMBER)
inter_sp = sp;
inter_pc = pc;
if (sp->type != T_STRING)
goto bad_arg_3;
i = write_bytes((sp-2)->u.string, (sp-1)->u.number, sp->u.string);
pop_stack();
sp--;
free_svalue(sp);
put_number(i);
break;
}
CASE(F_FILE_SIZE);
{
int i;
assign_eval_cost();
TYPE_TEST1(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
i = file_size(sp->u.string);
free_svalue(sp);
put_number(i);
break;
}
CASE(F_FIND_LIVING);
{
struct object *ob;
TYPE_TEST1(sp, T_STRING)
ob = find_living_object(sp->u.string, 0);
free_svalue(sp);
if (!ob)
put_number(0);
else
put_object(ob);
break;
}
CASE(F_TELL_OBJECT);
ASSIGN_EVAL_COST
TYPE_TEST1(sp-1, T_OBJECT)
TYPE_TEST2(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
tell_object((sp-1)->u.ob, sp->u.string);
free_string_svalue(sp);
sp--;
if (sp->type == T_OBJECT) /* not self-destructed */
free_object_svalue(sp);
sp--;
break;
CASE(F_RESTORE_OBJECT);
{
int i;
assign_eval_cost();
TYPE_TEST1(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
i = restore_object(current_object, sp->u.string);
free_svalue(sp);
put_number(i);
break;
}
CASE(F_THIS_PLAYER);
if (command_giver && !(command_giver->flags & O_DESTRUCTED))
push_object(command_giver);
else
push_number(0);
break;
CASE(F_THIS_INTERACTIVE);
if (current_interactive &&
!(current_interactive->flags & O_DESTRUCTED))
push_object(current_interactive);
else
push_number(0);
break;
#ifdef F_FIRST_INVENTORY
CASE(F_FIRST_INVENTORY);
{
struct object *ob;
if (sp->type == T_OBJECT) {
ob = sp->u.ob->contains;
free_object_svalue(sp);
} else if (sp->type == T_STRING) {
inter_sp = sp;
inter_pc = pc;
ob = first_inventory(sp);
free_string_svalue(sp);
} else goto bad_arg_1;
if (ob)
put_object(ob);
else
put_number(0);
break;
}
#endif /* FIRST_INVENTORY */
CASE(F_LIVING);
{
int i;
TYPE_TEST1(sp, T_OBJECT)
i = (sp->u.ob->flags & O_ENABLE_COMMANDS) != 0;
free_object_svalue(sp);
put_number(i);
break;
}
#if defined(F_GETUID) && !defined(F_CREATOR)
CASE(F_GETUID);
{
/*
* Are there any reasons to support this one in -o mode ?
*/
struct object *ob;
TYPE_TEST1(sp, T_OBJECT)
ob = sp->u.ob;
#ifdef DEBUG
if (ob->user == 0)
fatal("User is null pointer\n");
#endif
{ char *tmp;
tmp = ob->user->name;
pop_stack();
push_constant_string(tmp);
}
break;
}
#endif /* F_GETUID */
#ifdef F_GETEUID
CASE(F_GETEUID);
{
struct object *ob;
/*
* Are there any reasons to support this one in -o mode ?
*/
TYPE_TEST1(sp, T_OBJECT)
ob = sp->u.ob;
if (ob->eff_user) {
char *tmp;
tmp = ob->eff_user->name;
pop_stack();
push_constant_string(tmp);
}
else {
free_svalue(sp);
put_number(0);
}
break;
}
#endif /* F_GETEUID */
#ifdef F_EXPORT_UID
CASE(F_EXPORT_UID);
{
struct object *ob;
TYPE_TEST1(sp, T_OBJECT)
if (current_object->eff_user == 0)
ERROR("Illegal to export uid 0\n")
ob = sp->u.ob;
if (!ob->eff_user) /* Only allowed to export when null */
ob->user = current_object->eff_user;
free_object(ob, "export_uid");
sp--;
break;
}
#endif /* F_EXPORT_UID */
#ifdef F_SETEUID
CASE(F_SETEUID);
{
struct svalue *ret;
argp = sp;
if (argp->type == T_NUMBER) {
if (argp->u.number != 0)
goto bad_arg_1;
current_object->eff_user = 0;
free_svalue(argp);
argp->type = T_NUMBER;
argp->u.number = 1;
break;
}
if (argp->type != T_STRING)
goto bad_arg_1;
assign_eval_cost();
inter_sp = _push_volatile_string(argp->u.string,
_push_object(current_object, sp) );
inter_pc = pc;
ret = apply_master_ob("valid_seteuid", 2);
if (ret == 0 || ret->type != T_NUMBER || ret->u.number != 1) {
if (out_of_memory) {
error("Out of memory\n");
}
free_svalue(argp);
argp->type = T_NUMBER;
argp->u.number = 0;
break;
}
current_object->eff_user = add_name(argp->u.string);
free_svalue(argp);
argp->type = T_NUMBER;
argp->u.number = 1;
break;
}
#endif /* F_SETEUID */
#ifdef F_SETUID
CASE(F_SETUID)
TYPE_TEST
setuid();
push_number(0);
break;
#endif /* F_SETUID */
#ifdef F_CREATOR
CASE(F_CREATOR);
{
struct object *ob;
TYPE_TEST1(sp, T_OBJECT)
ob = sp->u.ob;
free_object_svalue(sp);
if (ob->user == 0) {
put_number(0);
} else {
sp->type = T_STRING;
sp->x.string_type = STRING_SHARED;
increment_string_ref( sp->u.string = ob->user->name );
}
break;
}
#endif
CASE(F_EXPLODE);
{
struct vector *v;
TYPE_TEST1(sp-1, T_STRING)
TYPE_TEST2(sp, T_STRING)
inter_sp = sp;
inter_pc = pc;
#ifdef OLD_EXPLODE_BEHAVIOUR
v = explode_string((sp-1)->u.string, sp->u.string);
#else
v = new_explode_string((sp-1)->u.string, sp->u.string);
#endif
free_string_svalue(sp);
sp--;
free_string_svalue(sp);
put_referenced_vector(v);
break;
}
CASE(F_FILTER_ARRAY);
{
struct vector *v;
struct svalue *arg;
struct object *ob;
char *func;
int num_extra;
assign_eval_cost();
GET_NUM_ARG
inter_pc = pc;
inter_sp = sp;
arg =Showing the first 100 KB of 273.4 KB. Download the archive for the full file.