cb22561995-10-11Fredrik Hübinette (Hubbe) /*\
06983f1996-09-22Fredrik Hübinette (Hubbe) ||| This file a part of Pike, and is copyright by Fredrik Hubinette ||| Pike is distributed as GPL (General Public License)
cb22561995-10-11Fredrik Hübinette (Hubbe) ||| See the files COPYING and DISCLAIMER for more information. \*/
8670791999-02-28Henrik Grubbström (Grubba) /**/
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h"
c6bdce1999-03-19Fredrik Hübinette (Hubbe) RCSID("$Id: interpret.c,v 1.118 1999/03/19 11:43:14 hubbe Exp $");
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "interpret.h" #include "object.h" #include "program.h" #include "svalue.h" #include "array.h" #include "mapping.h" #include "error.h" #include "language.h" #include "stralloc.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "constants.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "multiset.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "backend.h" #include "operators.h" #include "opcodes.h" #include "main.h" #include "lex.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "builtin_functions.h" #include "signal_handler.h" #include "gc.h"
07513e1996-10-04Fredrik Hübinette (Hubbe) #include "threads.h"
a29e021996-10-15Fredrik Hübinette (Hubbe) #include "callback.h"
9c6f7d1997-04-15Fredrik Hübinette (Hubbe) #include "fd_control.h"
7e97c31999-01-21Fredrik Hübinette (Hubbe) #include "security.h"
4218011999-01-31Fredrik Hübinette (Hubbe) #include "block_alloc.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
349fcb1996-10-09Fredrik Hübinette (Hubbe) #include <fcntl.h>
8aeeb21996-11-19Fredrik Hübinette (Hubbe) #include <errno.h>
349fcb1996-10-09Fredrik Hübinette (Hubbe) 
b208c11996-08-03Fredrik Hübinette (Hubbe) #ifdef HAVE_MMAP #ifdef HAVE_SYS_TYPES_H #include <sys/types.h> #endif #ifdef HAVE_SYS_MMAN_H #include <sys/mman.h> #endif
06983f1996-09-22Fredrik Hübinette (Hubbe) #ifdef MAP_NORESERVE
b208c11996-08-03Fredrik Hübinette (Hubbe) #define USE_MMAP_FOR_STACK #endif #endif
189fd01997-01-28Fredrik Hübinette (Hubbe) /* * Define the default evaluator stack size, used for just about everything. */ #define EVALUATOR_STACK_SIZE 100000
c5016f1996-04-13Fredrik Hübinette (Hubbe) #define TRACE_LEN (100 + t_flag * 10)
5267b71995-08-09Fredrik Hübinette (Hubbe)  /* sp points to first unused value on stack * (much simpler than letting it point at the last used value.) */
b208c11996-08-03Fredrik Hübinette (Hubbe) struct svalue *sp; /* Current position */ struct svalue *evaluator_stack; /* Start of stack */
0f887e1996-08-12Fredrik Hübinette (Hubbe) int stack_size = EVALUATOR_STACK_SIZE;
3c9f631996-09-23Fredrik Hübinette (Hubbe) int evaluator_stack_malloced = 0;
4908871998-08-10Fredrik Hübinette (Hubbe) char *stack_top;
5267b71995-08-09Fredrik Hübinette (Hubbe)  /* mark stack, used to store markers into the normal stack */
b208c11996-08-03Fredrik Hübinette (Hubbe) struct svalue **mark_sp; /* Current position */ struct svalue **mark_stack; /* Start of stack */
3c9f631996-09-23Fredrik Hübinette (Hubbe) int mark_stack_malloced = 0;
fa382f1996-06-21Fredrik Hübinette (Hubbe) 
e132001998-11-19Fredrik Hübinette (Hubbe) #ifdef PROFILING #ifdef HAVE_GETHRTIME long long accounted_time =0; long long time_base =0; #endif #endif
be478c1997-08-30Henrik Grubbström (Grubba) void push_sp_mark(void)
36feac1997-03-06Fredrik Hübinette (Hubbe) { if(mark_sp == mark_stack + stack_size) error("No more mark stack!\n"); *mark_sp++=sp; }
be478c1997-08-30Henrik Grubbström (Grubba) int pop_sp_mark(void)
36feac1997-03-06Fredrik Hübinette (Hubbe) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
36feac1997-03-06Fredrik Hübinette (Hubbe)  if(mark_sp < mark_stack) fatal("Mark stack underflow!\n"); #endif return sp - *--mark_sp; }
4218011999-01-31Fredrik Hübinette (Hubbe) struct pike_frame *fp; /* pike_frame pointer */
fa382f1996-06-21Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
05c7cd1997-07-19Fredrik Hübinette (Hubbe) static void gc_check_stack_callback(struct callback *foo, void *bar, void *gazonk) {
4218011999-01-31Fredrik Hübinette (Hubbe)  struct pike_frame *f;
4694111997-11-07Fredrik Hübinette (Hubbe)  debug_gc_xmark_svalues(evaluator_stack,sp-evaluator_stack-1,"interpreter stack");
62d3e41998-04-05Fredrik Hübinette (Hubbe) 
4218011999-01-31Fredrik Hübinette (Hubbe)  for(f=fp;f;f=f->next)
62d3e41998-04-05Fredrik Hübinette (Hubbe)  { if(f->context.parent) gc_external_mark(f->context.parent); gc_external_mark(f->current_object); gc_external_mark(f->context.prog); }
05c7cd1997-07-19Fredrik Hübinette (Hubbe) } #endif
be478c1997-08-30Henrik Grubbström (Grubba) void init_interpreter(void)
fa382f1996-06-21Fredrik Hübinette (Hubbe) {
b208c11996-08-03Fredrik Hübinette (Hubbe) #ifdef USE_MMAP_FOR_STACK
349fcb1996-10-09Fredrik Hübinette (Hubbe)  static int fd = -1;
b208c11996-08-03Fredrik Hübinette (Hubbe) 
05c7cd1997-07-19Fredrik Hübinette (Hubbe) 
b208c11996-08-03Fredrik Hübinette (Hubbe) #ifndef MAP_VARIABLE #define MAP_VARIABLE 0 #endif #ifndef MAP_PRIVATE #define MAP_PRIVATE 0 #endif
3c9f631996-09-23Fredrik Hübinette (Hubbe) #ifndef MAP_FAILED #define MAP_FAILED -1 #endif
349fcb1996-10-09Fredrik Hübinette (Hubbe) #ifndef MAP_ANONYMOUS
b208c11996-08-03Fredrik Hübinette (Hubbe) #define MAP_ANONYMOUS 0
349fcb1996-10-09Fredrik Hübinette (Hubbe)  if(fd == -1) {
cf39c21996-11-18Fredrik Hübinette (Hubbe)  while(1) { fd=open("/dev/zero",O_RDONLY); if(fd >= 0) break; if(errno != EINTR)
d77dfb1996-11-21Fredrik Hübinette (Hubbe)  { evaluator_stack=0; mark_stack=0; goto use_malloc; }
cf39c21996-11-18Fredrik Hübinette (Hubbe)  }
c36b851997-11-01Henrik Grubbström (Grubba)  /* Don't keep this fd on exec() */ set_close_on_exec(fd, 1);
349fcb1996-10-09Fredrik Hübinette (Hubbe)  }
b208c11996-08-03Fredrik Hübinette (Hubbe) #endif
06983f1996-09-22Fredrik Hübinette (Hubbe) #define MMALLOC(X,Y) (Y *)mmap(0,X*sizeof(Y),PROT_READ|PROT_WRITE, MAP_NORESERVE | MAP_PRIVATE | MAP_ANONYMOUS, fd, 0)
b208c11996-08-03Fredrik Hübinette (Hubbe) 
07513e1996-10-04Fredrik Hübinette (Hubbe)  evaluator_stack_malloced=0; mark_stack_malloced=0;
0f887e1996-08-12Fredrik Hübinette (Hubbe)  evaluator_stack=MMALLOC(stack_size,struct svalue); mark_stack=MMALLOC(stack_size, struct svalue *);
349fcb1996-10-09Fredrik Hübinette (Hubbe)  if((char *)MAP_FAILED == (char *)evaluator_stack) evaluator_stack=0;
3c9f631996-09-23Fredrik Hübinette (Hubbe)  if((char *)MAP_FAILED == (char *)mark_stack) mark_stack=0;
07513e1996-10-04Fredrik Hübinette (Hubbe) #else evaluator_stack=0; mark_stack=0;
b208c11996-08-03Fredrik Hübinette (Hubbe) #endif
07513e1996-10-04Fredrik Hübinette (Hubbe) 
d77dfb1996-11-21Fredrik Hübinette (Hubbe) use_malloc:
3c9f631996-09-23Fredrik Hübinette (Hubbe)  if(!evaluator_stack) {
07513e1996-10-04Fredrik Hübinette (Hubbe)  evaluator_stack=(struct svalue *)xalloc(stack_size*sizeof(struct svalue));
3c9f631996-09-23Fredrik Hübinette (Hubbe)  evaluator_stack_malloced=1; } if(!mark_stack) {
07513e1996-10-04Fredrik Hübinette (Hubbe)  mark_stack=(struct svalue **)xalloc(stack_size*sizeof(struct svalue *));
3c9f631996-09-23Fredrik Hübinette (Hubbe)  mark_stack_malloced=1; }
fa382f1996-06-21Fredrik Hübinette (Hubbe)  sp=evaluator_stack; mark_sp=mark_stack;
07513e1996-10-04Fredrik Hübinette (Hubbe)  fp=0;
62d3e41998-04-05Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
62d3e41998-04-05Fredrik Hübinette (Hubbe)  { static struct callback *spcb; if(!spcb) { spcb=add_gc_callback(gc_check_stack_callback,0,0); } } #endif
d65e531998-11-19Fredrik Hübinette (Hubbe) #ifdef PROFILING #ifdef HAVE_GETHRTIME
7163ed1998-11-20Fredrik Hübinette (Hubbe)  time_base = gethrtime(); accounted_time =0;
d65e531998-11-19Fredrik Hübinette (Hubbe) #endif #endif
fa382f1996-06-21Fredrik Hübinette (Hubbe) }
4908871998-08-10Fredrik Hübinette (Hubbe) 
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) static int eval_instruction(unsigned char *pc);
5267b71995-08-09Fredrik Hübinette (Hubbe)  /* * lvalues are stored in two svalues in one of these formats: * array[index] : { array, index } * mapping[index] : { mapping, index }
06983f1996-09-22Fredrik Hübinette (Hubbe)  * multiset[index] : { multiset, index }
b208c11996-08-03Fredrik Hübinette (Hubbe)  * object[index] : { object, index }
5267b71995-08-09Fredrik Hübinette (Hubbe)  * local variable : { svalue_pointer, nothing } * global variable : { svalue_pointer/short_svalue_pointer, nothing } */ void lvalue_to_svalue_no_free(struct svalue *to,struct svalue *lval) {
7e97c31999-01-21Fredrik Hübinette (Hubbe) #ifdef PIKE_SECURITY if(lval->type <= MAX_COMPLEX) if(!CHECK_DATA_SECURITY(lval->u.array, SECURITY_BIT_INDEX)) error("Index permission denied.\n"); #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  switch(lval->type) {
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY_LVALUE:
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
2a32691998-01-31Fredrik Hübinette (Hubbe)  INT32 e; struct array *a; ONERROR err; a=allocate_array(lval[1].u.array->size>>1); SET_ONERROR(err, do_free_array, a); for(e=0;e<a->size;e++) lvalue_to_svalue_no_free(a->item+e, lval[1].u.array->item+(e<<1)); to->type = T_ARRAY; to->u.array=a; UNSET_ONERROR(err); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_LVALUE: assign_svalue_no_free(to, lval->u.lval); break; case T_SHORT_LVALUE: assign_from_short_svalue_no_free(to, lval->u.short_lval, lval->subtype); break; case T_OBJECT: object_index_no_free(to, lval->u.object, lval+1); break; case T_ARRAY: simple_array_index_no_free(to, lval->u.array, lval+1); break; case T_MAPPING: mapping_index_no_free(to, lval->u.mapping, lval+1); break; case T_MULTISET: to->type=T_INT; if(multiset_member(lval->u.multiset,lval+1)) { to->u.integer=0; to->subtype=NUMBER_UNDEFINED; }else{ to->u.integer=0; to->subtype=NUMBER_NUMBER; } break; default: if(IS_ZERO(lval))
c6bdce1999-03-19Fredrik Hübinette (Hubbe)  index_error(0,0,0,lval,lval+1,"Indexing the NULL value.\n");
2a32691998-01-31Fredrik Hübinette (Hubbe)  else
c6bdce1999-03-19Fredrik Hübinette (Hubbe)  index_error(0,0,0,lval,lval+1,"Indexing a basic type.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe)  } } void assign_lvalue(struct svalue *lval,struct svalue *from) {
7e97c31999-01-21Fredrik Hübinette (Hubbe) #ifdef PIKE_SECURITY if(lval->type <= MAX_COMPLEX) if(!CHECK_DATA_SECURITY(lval->u.array, SECURITY_BIT_SET_INDEX)) error("Assign index permission denied.\n"); #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  switch(lval->type) {
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY_LVALUE: { INT32 e; if(from->type != T_ARRAY) error("Trying to assign combined lvalue from non-array.\n"); if(from->u.array->size < (lval[1].u.array->size>>1)) error("Not enough values for multiple assign.\n"); for(e=0;e<from->u.array->size;e++) assign_lvalue(lval[1].u.array->item+(e<<1),from->u.array->item+e); } break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  case T_LVALUE: assign_svalue(lval->u.lval,from); break; case T_SHORT_LVALUE: assign_to_short_svalue(lval->u.short_lval, lval->subtype, from); break; case T_OBJECT: object_set_index(lval->u.object, lval+1, from); break; case T_ARRAY: simple_set_index(lval->u.array, lval+1, from); break; case T_MAPPING: mapping_insert(lval->u.mapping, lval+1, from); break;
06983f1996-09-22Fredrik Hübinette (Hubbe)  case T_MULTISET:
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(IS_ZERO(from))
06983f1996-09-22Fredrik Hübinette (Hubbe)  multiset_delete(lval->u.multiset, lval+1);
5267b71995-08-09Fredrik Hübinette (Hubbe)  else
06983f1996-09-22Fredrik Hübinette (Hubbe)  multiset_insert(lval->u.multiset, lval+1);
5267b71995-08-09Fredrik Hübinette (Hubbe)  break; default:
6ec27f1996-12-05Per Hedbor  if(IS_ZERO(lval))
c6bdce1999-03-19Fredrik Hübinette (Hubbe)  index_error(0,0,0,lval,lval+1,"Indexing the NULL value.\n");
6ec27f1996-12-05Per Hedbor  else
c6bdce1999-03-19Fredrik Hübinette (Hubbe)  index_error(0,0,0,lval,lval+1,"Indexing a basic type.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe)  } } union anything *get_pointer_if_this_type(struct svalue *lval, TYPE_T t) {
7e97c31999-01-21Fredrik Hübinette (Hubbe) #ifdef PIKE_SECURITY if(lval->type <= MAX_COMPLEX) if(!CHECK_DATA_SECURITY(lval->u.array, SECURITY_BIT_SET_INDEX)) error("Assign index permission denied.\n"); #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  switch(lval->type) {
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY_LVALUE: return 0; case T_LVALUE: if(lval->u.lval->type == t) return & ( lval->u.lval->u ); return 0; case T_SHORT_LVALUE: if(lval->subtype == t) return lval->u.short_lval; return 0; case T_OBJECT: return object_get_item_ptr(lval->u.object,lval+1,t); case T_ARRAY: return array_get_item_ptr(lval->u.array,lval+1,t); case T_MAPPING: return mapping_get_item_ptr(lval->u.mapping,lval+1,t);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_MULTISET: return 0; default: if(IS_ZERO(lval))
c6bdce1999-03-19Fredrik Hübinette (Hubbe)  index_error(0,0,0,lval,lval+1,"Indexing the NULL value.\n"); else index_error(0,0,0,lval,lval+1,"Indexing a basic type.\n");
2a32691998-01-31Fredrik Hübinette (Hubbe)  return 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
be478c1997-08-30Henrik Grubbström (Grubba) void print_return_value(void)
5683de1995-11-06Fredrik Hübinette (Hubbe) { if(t_flag>3) { char *s; init_buf(); describe_svalue(sp-1,0,0); s=simple_free_buf(); if((long)strlen(s) > (long)TRACE_LEN) { s[TRACE_LEN]=0; s[TRACE_LEN-1]='.'; s[TRACE_LEN-2]='.'; s[TRACE_LEN-2]='.'; } fprintf(stderr,"- value: %s\n",s); free(s); } } #else
acc38d1995-11-20Fredrik Hübinette (Hubbe) #define print_return_value()
5683de1995-11-06Fredrik Hübinette (Hubbe) #endif
81b84e1996-12-03Fredrik Hübinette (Hubbe) struct callback_list evaluator_callbacks;
a29e021996-10-15Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
d0cd2f1998-11-08Fredrik Hübinette (Hubbe) static char trace_buffer[200];
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif #define CASE(X) case (X)-F_OFFSET:
06983f1996-09-22Fredrik Hübinette (Hubbe) #define DOJUMP() \
0a861b1997-09-17Fredrik Hübinette (Hubbe)  do { int tmp; tmp=EXTRACT_INT(pc); pc+=tmp; if(tmp < 0) fast_check_threads_etc(6); }while(0)
06983f1996-09-22Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe) #define COMPARISMENT(ID,EXPR) \ CASE(ID); \ instr=EXPR; \ pop_n_elems(2); \
7961fb1997-10-10Fredrik Hübinette (Hubbe) push_int(instr); \ break
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7961fb1997-10-10Fredrik Hübinette (Hubbe) #define LOOP(ID, OP1, OP2, OP3, OP4) \ CASE(ID) \ { \ union anything *i=get_pointer_if_this_type(sp-2, T_INT); \ if(i) \ { \ OP1 ( i->integer ); \ if(i->integer OP2 sp[-3].u.integer) \ { \ pc+=EXTRACT_INT(pc); \ fast_check_threads_etc(8); \ }else{ \ pc+=sizeof(INT32); \ } \ }else{ \ lvalue_to_svalue_no_free(sp-2,sp); sp++; \ push_int(1); \ OP3; \ assign_lvalue(sp-3,sp-1); \ if(OP4 ( sp-1, sp-4 )) \ { \ pc+=EXTRACT_INT(pc); \ fast_check_threads_etc(8); \ }else{ \ pc+=sizeof(INT32); \ } \ pop_stack(); \ } \ break; \
5267b71995-08-09Fredrik Hübinette (Hubbe) }
c5016f1996-04-13Fredrik Hübinette (Hubbe) #define CJUMP(X,Y) \ CASE(X); \ if(Y(sp-2,sp-1)) { \
06983f1996-09-22Fredrik Hübinette (Hubbe)  DOJUMP(); \
c5016f1996-04-13Fredrik Hübinette (Hubbe) }else{ \ pc+=sizeof(INT32); \ } \ pop_n_elems(2); \ break
5267b71995-08-09Fredrik Hübinette (Hubbe) /* * reset the stack machine. */
be478c1997-08-30Henrik Grubbström (Grubba) void reset_evaluator(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) { fp=0; pop_n_elems(sp - evaluator_stack); }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe) #define BACKLOG 512 struct backlog { INT32 instruction; INT32 arg; struct program *program; unsigned char *pc; }; struct backlog backlog[BACKLOG]; int backlogp=BACKLOG-1;
5c8e891995-10-29Fredrik Hübinette (Hubbe) void dump_backlog(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) { int e; if(!d_flag || backlogp<0 || backlogp>=BACKLOG) return; e=backlogp; do { e++; if(e>=BACKLOG) e=0; if(backlog[e].program) { char *file; INT32 line; file=get_line(backlog[e].pc-1,backlog[e].program, &line); fprintf(stderr,"%s:%ld: %s(%ld)\n", file,
f90e541995-08-17Fredrik Hübinette (Hubbe)  (long)line,
5267b71995-08-09Fredrik Hübinette (Hubbe)  low_get_f_name(backlog[e].instruction + F_OFFSET, backlog[e].program),
f90e541995-08-17Fredrik Hübinette (Hubbe)  (long)backlog[e].arg);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }while(e!=backlogp);
cb22561995-10-11Fredrik Hübinette (Hubbe) }
5267b71995-08-09Fredrik Hübinette (Hubbe)  #endif
b208c11996-08-03Fredrik Hübinette (Hubbe) static int o_catch(unsigned char *pc);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #define eval_instruction eval_instruction_with_debug #include "interpreter.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #undef eval_instruction #define eval_instruction eval_instruction_without_debug #undef PIKE_DEBUG #define print_return_value() #include "interpreter.h" #undef print_return_value #define PIKE_DEBUG #undef eval_instruction
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) static inline int eval_instruction(unsigned char *pc) { if(d_flag || t_flag>2) return eval_instruction_with_debug(pc); else return eval_instruction_without_debug(pc); }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #else #include "interpreter.h"
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) #endif
a599231996-09-25Fredrik Hübinette (Hubbe) 
cd83521998-02-02Fredrik Hübinette (Hubbe) static void trace_return_value(void) { char *s; init_buf(); my_strcat("Return: "); describe_svalue(sp-1,0,0); s=simple_free_buf(); if((long)strlen(s) > (long)TRACE_LEN) { s[TRACE_LEN]=0; s[TRACE_LEN-1]='.'; s[TRACE_LEN-2]='.'; s[TRACE_LEN-2]='.'; } fprintf(stderr,"%-*s%s\n",4,"-",s); free(s); } static void do_trace_call(INT32 args) { char *file,*s; INT32 linep,e; my_strcat("("); for(e=0;e<args;e++) { if(e) my_strcat(","); describe_svalue(sp-args+e,0,0); } my_strcat(")"); s=simple_free_buf(); if((long)strlen(s) > (long)TRACE_LEN) { s[TRACE_LEN]=0; s[TRACE_LEN-1]='.'; s[TRACE_LEN-2]='.'; s[TRACE_LEN-2]='.'; } if(fp && fp->pc) { char *f; file=get_line(fp->pc,fp->context.prog,&linep); while((f=STRCHR(file,'/'))) file=f+1; }else{ linep=0; file="-"; } fprintf(stderr,"- %s:%4ld: %s\n",file,(long)linep,s); free(s); }
4218011999-01-31Fredrik Hübinette (Hubbe)  #undef INIT_BLOCK #define INIT_BLOCK(X) do { X->refs=1; X->malloced_locals=0; X->scope=0; }while(0) #undef EXIT_BLOCK #define EXIT_BLOCK(X) do { \ free_object(X->current_object); \ if(X->context.prog) free_program(X->context.prog); \ if(X->context.parent) free_object(X->context.parent); \ if(X->scope) free_pike_frame(X->scope); \ if(X->malloced_locals) \ { \ free_svalues(X->locals,X->num_locals,BIT_MIXED); \ free((char *)(X->locals)); \ } \ }while(0) BLOCK_ALLOC(pike_frame,128)
7e97c31999-01-21Fredrik Hübinette (Hubbe) #ifdef PIKE_SECURITY static void restore_creds(struct object *creds) { if(current_creds) free_object(current_creds); current_creds=creds; }
cd83521998-02-02Fredrik Hübinette (Hubbe) 
7e97c31999-01-21Fredrik Hübinette (Hubbe) /* Magic trick */ static #else #define mega_apply2 mega_apply #endif
4218011999-01-31Fredrik Hübinette (Hubbe) 
7e97c31999-01-21Fredrik Hübinette (Hubbe) void mega_apply2(enum apply_type type, INT32 args, void *arg1, void *arg2)
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) { struct object *o;
4218011999-01-31Fredrik Hübinette (Hubbe)  struct pike_frame *scope=0;
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  int fun, tailrecurse=-1; struct svalue *save_sp=sp-args;
7e97c31999-01-21Fredrik Hübinette (Hubbe) 
a2a8801998-03-18Per Hedbor #ifdef PROFILING #ifdef HAVE_GETHRTIME
6189631998-11-12Fredrik Hübinette (Hubbe)  long long children_base = accounted_time;
3e5b131998-11-19Fredrik Hübinette (Hubbe)  long long start_time = gethrtime() - time_base;
6189631998-11-12Fredrik Hübinette (Hubbe)  unsigned INT32 self_time_base;
9750c31998-11-20Fredrik Hübinette (Hubbe)  if(start_time < 0) {
809d7c1998-11-20Fredrik Hübinette (Hubbe)  fatal("gethrtime() shrunk\n start_time=%ld\n gethrtime()=%ld\n time_base=%ld\n",
9750c31998-11-20Fredrik Hübinette (Hubbe)  (long)(start_time/100000),
ed86c61998-11-20Fredrik Hübinette (Hubbe)  (long)(gethrtime()/100000),
9750c31998-11-20Fredrik Hübinette (Hubbe)  (long)(time_base/100000)); }
a2a8801998-03-18Per Hedbor #endif #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  switch(type) { case APPLY_STACK: apply_stack:
bb21091998-04-10Henrik Grubbström (Grubba)  if(!args)
aa366d1998-04-16Fredrik Hübinette (Hubbe)  PIKE_ERROR("`()", "Too few arguments.\n", sp, 0);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  args--; if(sp-save_sp-args > (args<<2) + 32) { /* The test above assures these two areas * are not overlapping */ assign_svalues(save_sp, sp-args-1, args+1, BIT_MIXED); pop_n_elems(sp-save_sp-args-1); } arg1=(void *)(sp-args-1); case APPLY_SVALUE: apply_svalue: { struct svalue *s=(struct svalue *)arg1; switch(s->type) { case T_INT: if (!s->u.integer) {
aa366d1998-04-16Fredrik Hübinette (Hubbe)  PIKE_ERROR("0", "Attempt to call the NULL-value\n", sp, args);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  } else { error("Attempt to call the value %d\n", s->u.integer); } case T_STRING: if (s->u.string->len > 20) { error("Attempt to call the string \"%20s\"...\n", s->u.string->str); } else { error("Attempt to call the string \"%s\"\n", s->u.string->str); } case T_MAPPING: error("Attempt to call a mapping\n"); default:
bb21091998-04-10Henrik Grubbström (Grubba)  error("Call to non-function value type:%s.\n", get_name_of_type(s->type));
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  case T_FUNCTION: if(s->subtype == FUNCTION_BUILTIN) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
cd83521998-02-02Fredrik Hübinette (Hubbe)  if(t_flag>1) { init_buf(); describe_svalue(s,0,0); do_trace_call(args); } #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  (*(s->u.efun->function))(args); break; }else{ o=s->u.object; fun=s->subtype; goto apply_low; } break; case T_ARRAY:
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
cd83521998-02-02Fredrik Hübinette (Hubbe)  if(t_flag>1) { init_buf(); describe_svalue(s,0,0); do_trace_call(args); } #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  apply_array(s->u.array,args); break; case T_PROGRAM:
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
cd83521998-02-02Fredrik Hübinette (Hubbe)  if(t_flag>1) { init_buf(); describe_svalue(s,0,0); do_trace_call(args); } #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  push_object(clone_object(s->u.program,args)); break; case T_OBJECT: o=s->u.object;
4218011999-01-31Fredrik Hübinette (Hubbe)  if(o->prog == pike_trampoline_program) { fun=((struct pike_trampoline *)(o->storage))->func; scope=((struct pike_trampoline *)(o->storage))->frame; o=scope->current_object; goto apply_low_with_scope; }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  fun=LFUN_CALL; goto call_lfun; } break; } call_lfun:
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(fun < 0 || fun >= NUM_LFUNS) fatal("Apply lfun on illegal value!\n"); #endif if(!o->prog)
aa366d1998-04-16Fredrik Hübinette (Hubbe)  PIKE_ERROR("destructed object", "Apply on destructed object.\n", sp, args);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  fun=FIND_LFUN(o->prog,fun); goto apply_low; case APPLY_LOW: o=(struct object *)arg1; fun=(long)arg2; apply_low:
4218011999-01-31Fredrik Hübinette (Hubbe)  scope=0; apply_low_with_scope:
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  { struct program *p; struct reference *ref;
4218011999-01-31Fredrik Hübinette (Hubbe)  struct pike_frame *new_frame=alloc_pike_frame();
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  struct identifier *function; if(fun<0) {
0ffa001998-01-13Fredrik Hübinette (Hubbe)  pop_n_elems(sp-save_sp);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  push_int(0); return; }
7e97c31999-01-21Fredrik Hübinette (Hubbe) 
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  check_stack(256); check_mark_stack(256);
4908871998-08-10Fredrik Hübinette (Hubbe)  check_c_stack(8192);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(d_flag>2) do_debug(); #endif p=o->prog; if(!p)
aa366d1998-04-16Fredrik Hübinette (Hubbe)  PIKE_ERROR("destructed object->function",
bb21091998-04-10Henrik Grubbström (Grubba)  "Cannot call functions in destructed objects.\n", sp, args);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(fun>=(int)p->num_identifier_references)
56de851998-07-17Fredrik Hübinette (Hubbe)  { fprintf(stderr,"Function index out of range. %d >= %d\n",fun,(int)p->num_identifier_references); fprintf(stderr,"########Program is:\n"); describe(p); fprintf(stderr,"########Object is:\n"); describe(o);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  fatal("Function index out of range.\n");
56de851998-07-17Fredrik Hübinette (Hubbe)  }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) #endif ref = p->identifier_references + fun;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(ref->inherit_offset>=p->num_inherits) fatal("Inherit offset out of range in program.\n"); #endif
4218011999-01-31Fredrik Hübinette (Hubbe)  /* init a new evaluation pike_frame */ new_frame->next = fp; new_frame->current_object = o; new_frame->context = p->inherits[ ref->inherit_offset ];
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
4218011999-01-31Fredrik Hübinette (Hubbe)  function = new_frame->context.prog->identifiers + ref->identifier_offset;
a2a8801998-03-18Per Hedbor 
f41da61999-03-17Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG if(t_flag > 9) { fprintf(stderr,"-- ref: inoff=%d idoff=%d flags=%d\n", ref->inherit_offset, ref->identifier_offset, ref->id_flags); fprintf(stderr,"-- context: prog->id=%d inlev=%d idlev=%d pi=%d po=%d so=%d name=%s\n", new_frame->context.prog->id, new_frame->context.inherit_level, new_frame->context.identifier_level, new_frame->context.parent_identifier, new_frame->context.parent_offset, new_frame->context.storage_offset, new_frame->context.name ? new_frame->context.name->str : "NULL"); if(t_flag>19) { describe(new_frame->context.prog); } } #endif
7e97c31999-01-21Fredrik Hübinette (Hubbe) #ifdef PIKE_SECURITY CHECK_DATA_SECURITY_OR_ERROR(o, SECURITY_BIT_CALL, ("Function call permission denied.\n")); if(!CHECK_DATA_SECURITY(o, SECURITY_BIT_NOT_SETUID)) SET_CURRENT_CREDS(o->prot); #endif
a2a8801998-03-18Per Hedbor #ifdef PROFILING function->num_calls++; #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
4218011999-01-31Fredrik Hübinette (Hubbe)  new_frame->locals = sp - args; new_frame->expendible = new_frame->locals; new_frame->args = args; new_frame->fun = fun; new_frame->current_storage = o->storage+new_frame->context.storage_offset; new_frame->pc = 0; new_frame->scope=scope;
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
4218011999-01-31Fredrik Hübinette (Hubbe)  add_ref(new_frame->current_object); add_ref(new_frame->context.prog); if(new_frame->context.parent) add_ref(new_frame->context.parent); if(new_frame->scope) add_ref(new_frame->scope);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(t_flag) {
cd83521998-02-02Fredrik Hübinette (Hubbe)  char buf[50];
f41da61999-03-17Fredrik Hübinette (Hubbe) 
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  init_buf(); sprintf(buf,"%lx->",(long)o); my_strcat(buf); my_strcat(function->name->str);
cd83521998-02-02Fredrik Hübinette (Hubbe)  do_trace_call(args);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  }
4218011999-01-31Fredrik Hübinette (Hubbe)  fp = new_frame;
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(function->func.offset == -1)
8670791999-02-28Henrik Grubbström (Grubba)  PIKE_ERROR(function->name->str, "Calling undefined function.\n", sp, args);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
662e451998-01-22Fredrik Hübinette (Hubbe)  tailrecurse=-1;
6189631998-11-12Fredrik Hübinette (Hubbe)  #ifdef PROFILING #ifdef HAVE_GETHRTIME self_time_base=function->total_time; #endif #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  switch(function->identifier_flags & (IDENTIFIER_FUNCTION | IDENTIFIER_CONSTANT)) { case IDENTIFIER_C_FUNCTION: fp->num_args=args;
4218011999-01-31Fredrik Hübinette (Hubbe)  new_frame->num_locals=args;
773efa1998-07-16Fredrik Hübinette (Hubbe)  check_threads_etc();
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  (*function->func.c_fun)(args); break; case IDENTIFIER_CONSTANT: { struct svalue *s=fp->context.prog->constants+function->func.offset; if(s->type == T_PROGRAM) {
773efa1998-07-16Fredrik Hübinette (Hubbe)  struct object *tmp; check_threads_etc(); tmp=parent_clone_object(s->u.program, o, fun, args);
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  push_object(tmp); break; } /* Fall through */ } case 0: { if(sp-save_sp-args<=0) { /* Create an extra svalue for tail recursion style call */ sp++; MEMMOVE(sp-args,sp-args-1,sizeof(struct svalue)*args); sp[-args-1].type=T_INT; } low_object_index_no_free(sp-args-1,o,fun); tailrecurse=args+1; break; } case IDENTIFIER_PIKE_FUNCTION: { int num_args; int num_locals; unsigned char *pc;
4218011999-01-31Fredrik Hübinette (Hubbe)  pc=new_frame->context.prog->program + function->func.offset;
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  num_locals=EXTRACT_UCHAR(pc++); num_args=EXTRACT_UCHAR(pc++);
4218011999-01-31Fredrik Hübinette (Hubbe)  /* FIXME: this is only needed if this function contains * trampolines */ new_frame->expendible+=num_locals;
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  /* adjust arguments on stack */ if(args < num_args) /* push zeros */ { clear_svalues(sp, num_args-args); sp += num_args-args; args += num_args-args; } if(function->identifier_flags & IDENTIFIER_VARARGS) { f_aggregate(args - num_args); /* make array */ args = num_args+1; }else{ if(args > num_args) { /* pop excessive */ pop_n_elems(args - num_args); args=num_args; } } clear_svalues(sp, num_locals - args); sp += num_locals - args;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(num_locals < num_args) fatal("Wrong number of arguments or locals in function def.\n"); #endif
4218011999-01-31Fredrik Hübinette (Hubbe)  new_frame->num_locals=num_locals; new_frame->num_args=num_args;
773efa1998-07-16Fredrik Hübinette (Hubbe)  check_threads_etc();
aa5b801998-06-18Fredrik Hübinette (Hubbe)  { struct svalue **save_mark_sp=mark_sp; tailrecurse=eval_instruction(pc);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
aa5b801998-06-18Fredrik Hübinette (Hubbe)  if(mark_sp < save_mark_sp) fatal("Popped below save_mark_sp!\n"); #endif mark_sp=save_mark_sp; }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(sp<evaluator_stack) fatal("Stack error (also simple).\n"); #endif break; } }
a2a8801998-03-18Per Hedbor #ifdef PROFILING #ifdef HAVE_GETHRTIME
6189631998-11-12Fredrik Hübinette (Hubbe)  { long long time_passed, time_in_children, self_time; time_in_children= accounted_time - children_base;
3e5b131998-11-19Fredrik Hübinette (Hubbe)  time_passed = gethrtime() - time_base - start_time;
6189631998-11-12Fredrik Hübinette (Hubbe)  self_time=time_passed - time_in_children; accounted_time+=self_time;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
7163ed1998-11-20Fredrik Hübinette (Hubbe)  if(self_time < 0 || children_base <0 || accounted_time <0) fatal("Time is negative\n self_time=%ld\n time_passed=%ld\n time_in_children=%ld\n children_base=%ld\n accounted_time=%ld!\n time_base=%ld\n start_time=%ld\n",
7d17521998-11-20Fredrik Hübinette (Hubbe)  (long)(self_time/100000), (long)(time_passed/100000), (long)(time_in_children/100000), (long)(children_base/100000), (long)(accounted_time/100000), (long)(time_base/100000), (long)(start_time/100000)
6189631998-11-12Fredrik Hübinette (Hubbe)  ); #endif function->total_time=self_time_base + (INT32)(time_passed /1000); function->self_time+=(INT32)( self_time /1000); }
a2a8801998-03-18Per Hedbor #endif #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  #if 0
4218011999-01-31Fredrik Hübinette (Hubbe)  if(sp - new_frame->locals > 1)
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  {
4218011999-01-31Fredrik Hübinette (Hubbe)  pop_n_elems(sp - new_frame->locals -1); }else if(sp - new_frame->locals < 1){
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
4218011999-01-31Fredrik Hübinette (Hubbe)  if(sp - new_frame->locals<0) fatal("Frame underflow.\n");
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) #endif sp->u.integer = 0; sp->subtype=NUMBER_NUMBER; sp->type = T_INT; sp++; } #endif
7e97c31999-01-21Fredrik Hübinette (Hubbe) 
4218011999-01-31Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG if(fp!=new_frame) fatal("Frame stack out of whack!\n"); #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
4218011999-01-31Fredrik Hübinette (Hubbe)  POP_PIKE_FRAME();
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(tailrecurse>=0) { args=tailrecurse; goto apply_stack; } } } if(save_sp+1 < sp) { assign_svalue(save_sp,sp-1); pop_n_elems(sp-save_sp-1); }
9106ac1998-02-03Fredrik Hübinette (Hubbe)  if(save_sp+1 > sp)
cd83521998-02-02Fredrik Hübinette (Hubbe)  {
9106ac1998-02-03Fredrik Hübinette (Hubbe)  if(type != APPLY_SVALUE) push_int(0);
cd83521998-02-02Fredrik Hübinette (Hubbe)  }else{
9106ac1998-02-03Fredrik Hübinette (Hubbe)  if(t_flag>1) trace_return_value();
cd83521998-02-02Fredrik Hübinette (Hubbe)  }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) }
7e97c31999-01-21Fredrik Hübinette (Hubbe) #ifdef PIKE_SECURITY void mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2) { ONERROR tmp; if(current_creds) add_ref(current_creds); SET_ONERROR(tmp, restore_creds, current_creds); mega_apply2(type,args,arg1,arg2); CALL_AND_UNSET_ONERROR(tmp); } #endif
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
b208c11996-08-03Fredrik Hübinette (Hubbe) /* Put catch outside of eval_instruction, so * the setjmp won't affect the optimization of * eval_instruction */ static int o_catch(unsigned char *pc) { JMP_BUF tmp;
5017631998-06-05Fredrik Hübinette (Hubbe)  struct svalue *expendible=fp->expendible;
b208c11996-08-03Fredrik Hübinette (Hubbe)  if(SETJMP(tmp)) { *sp=throw_value; throw_value.type=T_INT; sp++; UNSETJMP(tmp);
5017631998-06-05Fredrik Hübinette (Hubbe)  fp->expendible=expendible;
b208c11996-08-03Fredrik Hübinette (Hubbe)  return 0; }else{
d265191998-07-28Fredrik Hübinette (Hubbe)  int x; fp->expendible=fp->locals + fp->num_locals; x=eval_instruction(pc); fp->expendible=expendible;
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  if(x!=-1) mega_apply(APPLY_STACK, x, 0,0);
b208c11996-08-03Fredrik Hübinette (Hubbe)  UNSETJMP(tmp); return 1; } }
3c0c281998-01-26Fredrik Hübinette (Hubbe) void f_call_function(INT32 args) { mega_apply(APPLY_STACK,args,0,0); }
5267b71995-08-09Fredrik Hübinette (Hubbe) int apply_low_safe_and_stupid(struct object *o, INT32 offset) { JMP_BUF tmp;
4218011999-01-31Fredrik Hübinette (Hubbe)  struct pike_frame *new_frame=alloc_pike_frame();
5267b71995-08-09Fredrik Hübinette (Hubbe)  int ret;
4218011999-01-31Fredrik Hübinette (Hubbe)  new_frame->next = fp; new_frame->current_object = o; new_frame->context=o->prog->inherits[0]; new_frame->locals = evaluator_stack; new_frame->expendible=new_frame->locals; new_frame->args = 0; new_frame->num_args=0; new_frame->num_locals=0; new_frame->fun = -1; new_frame->pc = 0; new_frame->current_storage=o->storage; new_frame->context.parent=0; fp = new_frame; add_ref(new_frame->current_object); add_ref(new_frame->context.prog);
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(SETJMP(tmp)) { ret=1; }else{
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  int tmp=eval_instruction(o->prog->program + offset); if(tmp!=-1) mega_apply(APPLY_STACK, tmp, 0,0);
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(sp<evaluator_stack)
b208c11996-08-03Fredrik Hübinette (Hubbe)  fatal("Stack error (simple).\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif ret=0; } UNSETJMP(tmp);
4218011999-01-31Fredrik Hübinette (Hubbe)  POP_PIKE_FRAME();
5267b71995-08-09Fredrik Hübinette (Hubbe)  return ret; } void safe_apply_low(struct object *o,int fun,int args) { JMP_BUF recovery; sp-=args;
dffa011997-01-15Fredrik Hübinette (Hubbe)  free_svalue(& throw_value); throw_value.type=T_INT;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(SETJMP(recovery)) {
dffa011997-01-15Fredrik Hübinette (Hubbe)  if(throw_value.type == T_ARRAY) { ONERROR tmp; SET_ONERROR(tmp,exit_on_error,"Error in handle_error in master object!"); assign_svalue_no_free(sp++, & throw_value); APPLY_MASTER("handle_error", 1); pop_stack(); UNSET_ONERROR(tmp); }
5267b71995-08-09Fredrik Hübinette (Hubbe)  sp->u.integer = 0; sp->subtype=NUMBER_NUMBER; sp->type = T_INT; sp++; }else{
fa382f1996-06-21Fredrik Hübinette (Hubbe)  INT32 expected_stack = sp - evaluator_stack + 1;
5267b71995-08-09Fredrik Hübinette (Hubbe)  sp+=args; apply_low(o,fun,args);
fa382f1996-06-21Fredrik Hübinette (Hubbe)  if(sp - evaluator_stack > expected_stack) pop_n_elems(sp - evaluator_stack - expected_stack); if(sp - evaluator_stack < expected_stack)
5267b71995-08-09Fredrik Hübinette (Hubbe)  { sp->u.integer = 0; sp->subtype=NUMBER_NUMBER; sp->type = T_INT; sp++; } } UNSETJMP(recovery); }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
5683de1995-11-06Fredrik Hübinette (Hubbe) void safe_apply(struct object *o, char *fun ,INT32 args) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5683de1995-11-06Fredrik Hübinette (Hubbe)  if(!o->prog) fatal("Apply safe on destructed object.\n"); #endif safe_apply_low(o, find_identifier(fun, o->prog), args); }
fa382f1996-06-21Fredrik Hübinette (Hubbe) void apply_lfun(struct object *o, int fun, int args) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
fa382f1996-06-21Fredrik Hübinette (Hubbe)  if(fun < 0 || fun >= NUM_LFUNS) fatal("Apply lfun on illegal value!\n"); #endif if(!o->prog)
aa366d1998-04-16Fredrik Hübinette (Hubbe)  PIKE_ERROR("destructed object", "Apply on destructed object.\n", sp, args);
fa382f1996-06-21Fredrik Hübinette (Hubbe) 
b1f4eb1998-01-13Fredrik Hübinette (Hubbe)  apply_low(o, (int)FIND_LFUN(o->prog,fun), args);
fa382f1996-06-21Fredrik Hübinette (Hubbe) }
5267b71995-08-09Fredrik Hübinette (Hubbe) void apply_shared(struct object *o,
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *fun,
5267b71995-08-09Fredrik Hübinette (Hubbe)  int args) {
fa382f1996-06-21Fredrik Hübinette (Hubbe)  apply_low(o, find_shared_string_identifier(fun, o->prog), args);
5267b71995-08-09Fredrik Hübinette (Hubbe) } void apply(struct object *o, char *fun, int args) {
fa382f1996-06-21Fredrik Hübinette (Hubbe)  apply_low(o, find_identifier(fun, o->prog), args);
5267b71995-08-09Fredrik Hübinette (Hubbe) } void apply_svalue(struct svalue *s, INT32 args) { if(s->type==T_INT) { pop_n_elems(args); push_int(0); }else{
fa382f1996-06-21Fredrik Hübinette (Hubbe)  INT32 expected_stack=sp-args+1 - evaluator_stack;
5267b71995-08-09Fredrik Hübinette (Hubbe)  strict_apply_svalue(s,args);
fa382f1996-06-21Fredrik Hübinette (Hubbe)  if(sp > (expected_stack + evaluator_stack))
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  {
fa382f1996-06-21Fredrik Hübinette (Hubbe)  pop_n_elems(sp-(expected_stack + evaluator_stack));
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  }
fa382f1996-06-21Fredrik Hübinette (Hubbe)  else if(sp < (expected_stack + evaluator_stack))
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  { push_int(0); }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
fa382f1996-06-21Fredrik Hübinette (Hubbe)  if(sp < (expected_stack + evaluator_stack)) fatal("Stack underflow!\n");
f6f02d1995-10-16Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
be478c1997-08-30Henrik Grubbström (Grubba) void slow_check_stack(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) { struct svalue *s,**m;
4218011999-01-31Fredrik Hübinette (Hubbe)  struct pike_frame *f;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
fa382f1996-06-21Fredrik Hübinette (Hubbe)  debug_check_stack();
5267b71995-08-09Fredrik Hübinette (Hubbe) 
0f887e1996-08-12Fredrik Hübinette (Hubbe)  if(sp > &(evaluator_stack[stack_size]))
0ae73a1998-10-16Henrik Grubbström (Grubba)  fatal("Stack overflow.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) 
0f887e1996-08-12Fredrik Hübinette (Hubbe)  if(mark_sp > &(mark_stack[stack_size]))
fa382f1996-06-21Fredrik Hübinette (Hubbe)  fatal("Mark stack overflow.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) 
fa382f1996-06-21Fredrik Hübinette (Hubbe)  if(mark_sp < mark_stack)
5267b71995-08-09Fredrik Hübinette (Hubbe)  fatal("Mark stack underflow.\n"); for(s=evaluator_stack;s<sp;s++) check_svalue(s); s=evaluator_stack; for(m=mark_stack;m<mark_sp;m++) { if(*m < s) fatal("Mark stack failiure.\n"); s=*m; }
0f887e1996-08-12Fredrik Hübinette (Hubbe)  if(s > &(evaluator_stack[stack_size]))
5267b71995-08-09Fredrik Hübinette (Hubbe)  fatal("Mark stack exceeds svalue stack\n");
4218011999-01-31Fredrik Hübinette (Hubbe)  for(f=fp;f;f=f->next)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
0f887e1996-08-12Fredrik Hübinette (Hubbe)  if(f->locals) { if(f->locals < evaluator_stack || f->locals > &(evaluator_stack[stack_size]))
5267b71995-08-09Fredrik Hübinette (Hubbe)  fatal("Local variable pointer points to Finspång.\n");
0f887e1996-08-12Fredrik Hübinette (Hubbe)  if(f->args < 0 || f->args > stack_size)
4218011999-01-31Fredrik Hübinette (Hubbe)  fatal("FEL FEL FEL! HELP!! (corrupted pike_frame)\n");
0f887e1996-08-12Fredrik Hübinette (Hubbe)  }
5267b71995-08-09Fredrik Hübinette (Hubbe)  } } #endif
be478c1997-08-30Henrik Grubbström (Grubba) void cleanup_interpret(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe)  int e; #endif while(fp) {
4218011999-01-31Fredrik Hübinette (Hubbe)  struct pike_frame *tmp=fp; fp=tmp->next; tmp->next=0; free_pike_frame(tmp);
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5267b71995-08-09Fredrik Hübinette (Hubbe)  for(e=0;e<BACKLOG;e++) { if(backlog[e].program) { free_program(backlog[e].program); backlog[e].program=0; } } #endif reset_evaluator();
b208c11996-08-03Fredrik Hübinette (Hubbe)  #ifdef USE_MMAP_FOR_STACK
3c9f631996-09-23Fredrik Hübinette (Hubbe)  if(!evaluator_stack_malloced) { munmap((char *)evaluator_stack, stack_size*sizeof(struct svalue)); evaluator_stack=0; } if(!mark_stack_malloced) { munmap((char *)mark_stack, stack_size*sizeof(struct svalue *)); mark_stack=0; }
b208c11996-08-03Fredrik Hübinette (Hubbe) #endif
3c9f631996-09-23Fredrik Hübinette (Hubbe)  if(evaluator_stack) free((char *)evaluator_stack); if(mark_stack) free((char *)mark_stack); mark_stack=0; evaluator_stack=0; mark_stack_malloced=0; evaluator_stack_malloced=0;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
b660c81999-03-01Fredrik Hübinette (Hubbe)  void really_clean_up_interpret(void) { free_all_pike_frame_blocks(); }