e576bb2002-10-11Martin Nilsson /* || This file is part of Pike. For copyright information see COPYRIGHT. || Pike is distributed under GPL, LGPL and MPL. See the file COPYING || for more information. */
aedfb12002-10-09Martin Nilsson 
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "global.h" #include "interpret.h" #include "object.h" #include "program.h" #include "svalue.h" #include "array.h" #include "mapping.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #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"
1a26b22004-12-30Henrik Grubbström (Grubba) #include "pike_embed.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #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"
43899f1999-10-23Fredrik Noring #include "bignum.h"
e498d91999-12-13Henrik Grubbström (Grubba) #include "pike_types.h"
0e0cd72001-07-20Henrik Grubbström (Grubba) #include "pikecode.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>
688f082001-02-27Martin Stjernholm #include <ctype.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
cf84342011-05-19Jonas Walldén #ifdef USE_DTRACE #include "dtrace_probes.h" #else #include "dtrace/dtrace_probes_disabled.h" #endif
189fd01997-01-28Fredrik Hübinette (Hubbe) /* * Define the default evaluator stack size, used for just about everything. */ #define EVALUATOR_STACK_SIZE 100000
97ebb32003-01-09Henrik Grubbström (Grubba) #define TRACE_LEN (100 + Pike_interpreter.trace_level * 10)
5267b71995-08-09Fredrik Hübinette (Hubbe) 
6697042000-11-20Martin Stjernholm /* Keep some margin on the stack space checks. They're lifted when * handle_error runs to give it some room. */
30def62004-06-01Martin Stjernholm /* Observed in 7.1: 40 was enough, 30 wasn't. */ #define SVALUE_STACK_MARGIN (100 + LOW_SVALUE_STACK_MARGIN) /* Observed in 7.4: 11000 was enough, 10000 wasn't. */ #define C_STACK_MARGIN (20000 + LOW_C_STACK_MARGIN) /* Another extra margin to use while dumping the raw error in * exit_on_error, so that backtrace_frame._sprintf can be called * then. */ #define LOW_SVALUE_STACK_MARGIN 20 #define LOW_C_STACK_MARGIN 500
6697042000-11-20Martin Stjernholm 
88149c2001-07-08Henrik Grubbström (Grubba) #ifdef HAVE_COMPUTED_GOTO PIKE_OPCODE_T *fcode_to_opcode = NULL;
afa1c62001-07-09Henrik Grubbström (Grubba) struct op_2_f *opcode_to_fcode = NULL;
88149c2001-07-08Henrik Grubbström (Grubba) #endif /* HAVE_COMPUTED_GOTO */
5267b71995-08-09Fredrik Hübinette (Hubbe) 
c299402001-11-10Martin Stjernholm PMOD_EXPORT const char Pike_check_stack_errmsg[] =
c915472000-12-04Martin Stjernholm  "Svalue stack overflow. " "(%ld of %ld entries on stack, needed %ld more entries)\n";
c299402001-11-10Martin Stjernholm PMOD_EXPORT const char Pike_check_mark_stack_errmsg[] =
c915472000-12-04Martin Stjernholm  "Mark stack overflow.\n";
c299402001-11-10Martin Stjernholm PMOD_EXPORT const char Pike_check_c_stack_errmsg[] =
c915472000-12-04Martin Stjernholm  "C stack overflow.\n";
c299402001-11-10Martin Stjernholm #ifdef PIKE_DEBUG PMOD_EXPORT const char msg_stack_error[] = "Stack error.\n"; PMOD_EXPORT const char msg_pop_neg[] = "Popping negative number of args.... (%"PRINTPTRDIFFT"d) \n"; #endif
c915472000-12-04Martin Stjernholm 
342fef2000-08-23Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG static char trace_buffer[2000]; #endif
0f65e12002-09-14Martin Stjernholm #ifdef INTERNAL_PROFILING PMOD_EXPORT unsigned long evaluator_callback_calls = 0; #endif
342fef2000-08-23Fredrik Hübinette (Hubbe) 
51d7d82008-11-18Martin Stjernholm int fast_check_threads_counter = 0;
7965d72001-01-24Fredrik Hübinette (Hubbe) PMOD_EXPORT int Pike_stack_size = EVALUATOR_STACK_SIZE;
f077582000-07-06Fredrik Hübinette (Hubbe) 
9fa0ee2003-11-09Martin Stjernholm static void do_trace_call(INT32 args, dynamic_buffer *old_buf);
bd8fb82004-04-03Martin Stjernholm static void do_trace_func_return (int got_retval, struct object *o, int fun); static void do_trace_return (int got_retval, dynamic_buffer *old_buf);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
be478c1997-08-30Henrik Grubbström (Grubba) void push_sp_mark(void)
36feac1997-03-06Fredrik Hübinette (Hubbe) {
7965d72001-01-24Fredrik Hübinette (Hubbe)  if(Pike_mark_sp == Pike_interpreter.mark_stack + Pike_stack_size)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("No more mark stack!\n");
fc26f62000-07-06Fredrik Hübinette (Hubbe)  *Pike_mark_sp++=Pike_sp;
36feac1997-03-06Fredrik Hübinette (Hubbe) }
6d8c692000-08-08Henrik Grubbström (Grubba) ptrdiff_t pop_sp_mark(void)
36feac1997-03-06Fredrik Hübinette (Hubbe) {
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(Pike_mark_sp < Pike_interpreter.mark_stack)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Mark stack underflow!\n");
36feac1997-03-06Fredrik Hübinette (Hubbe) #endif
fc26f62000-07-06Fredrik Hübinette (Hubbe)  return Pike_sp - *--Pike_mark_sp;
36feac1997-03-06Fredrik Hübinette (Hubbe) }
fa382f1996-06-21Fredrik Hübinette (Hubbe) 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
cc2e9f2003-09-24Martin Stjernholm void gc_mark_stack_external (struct pike_frame *f, struct svalue *stack_p, struct svalue *stack)
05c7cd1997-07-19Fredrik Hübinette (Hubbe) {
cc2e9f2003-09-24Martin Stjernholm  for (; f; f = f->next) GC_ENTER (f, T_PIKE_FRAME) {
9b2fbb2003-09-29Martin Stjernholm  if (!debug_gc_check (f, " as frame on stack")) { gc_mark_external (f->current_object, " in current_object in frame on stack");
fa93a52008-02-28Henrik Grubbström (Grubba)  gc_mark_external (f->current_program, " in current_program in frame on stack");
9b2fbb2003-09-29Martin Stjernholm  if (f->locals) { /* Check really needed? */ if (f->flags & PIKE_FRAME_MALLOCED_LOCALS) { gc_mark_external_svalues(f->locals, f->num_locals, " in malloced locals of trampoline frame on stack"); } else { if (f->locals > stack_p || (stack_p - f->locals) >= 0x10000) { fatal("Unreasonable locals: stack:%p locals:%p\n", stack_p, f->locals); } gc_mark_external_svalues (f->locals, stack_p - f->locals, " on svalue stack"); stack_p = f->locals;
1452042003-09-26Henrik Grubbström (Grubba)  } }
cc2e9f2003-09-24Martin Stjernholm  } } GC_LEAVE; if (stack != stack_p)
f2bfde2003-09-24Martin Stjernholm  gc_mark_external_svalues (stack, stack_p - stack, " on svalue stack");
cc2e9f2003-09-24Martin Stjernholm }
62d3e41998-04-05Fredrik Hübinette (Hubbe) 
61017f2014-02-25Per Hedbor static void gc_check_stack_callback(struct callback *UNUSED(foo), void *UNUSED(bar), void *UNUSED(gazonk))
cc2e9f2003-09-24Martin Stjernholm {
c512322008-08-18Martin Stjernholm  if (Pike_interpreter.evaluator_stack #ifdef PIKE_DEBUG /* Avoid this if the thread is swapped out. Useful when calling * locate_references from gdb. */ && Pike_sp != (void *) -1 #endif )
aa1ba42004-09-30Martin Stjernholm  gc_mark_stack_external (Pike_fp, Pike_sp, Pike_interpreter.evaluator_stack);
05c7cd1997-07-19Fredrik Hübinette (Hubbe) } #endif
28498e2002-11-09Henrik Grubbström (Grubba) /* Execute Pike code starting at pc. * * Called once with NULL to initialize tables. * * Returns 0 if pc is NULL. * * Returns -1 if the code terminated due to a RETURN. *
ca176b2006-02-27Martin Stjernholm  * Note: All callers except catching_eval_instruction need to save * Pike_interpreter.catching_eval_jmpbuf, zero it, and restore it * afterwards.
28498e2002-11-09Henrik Grubbström (Grubba)  */
eff6212001-07-09Henrik Grubbström (Grubba) static int eval_instruction(PIKE_OPCODE_T *pc);
a078232001-07-08Henrik Grubbström (Grubba) 
d97eb72011-07-10Henrik Grubbström (Grubba) PMOD_EXPORT int low_init_interpreter(struct Pike_interpreter_struct *interpreter)
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)  #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)  {
ded0492005-01-25Henrik Grubbström (Grubba)  interpreter->evaluator_stack=0; interpreter->mark_stack=0;
d77dfb1996-11-21Fredrik Hübinette (Hubbe)  goto use_malloc;
529b842004-03-24Henrik Grubbström (Grubba) #define NEED_USE_MALLOC_LABEL
d77dfb1996-11-21Fredrik Hübinette (Hubbe)  }
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
ded0492005-01-25Henrik Grubbström (Grubba) #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) 
ded0492005-01-25Henrik Grubbström (Grubba)  interpreter->evaluator_stack_malloced = 0; interpreter->mark_stack_malloced = 0; interpreter->evaluator_stack = MMALLOC(Pike_stack_size,struct svalue); interpreter->mark_stack = MMALLOC(Pike_stack_size, struct svalue *); if((char *)MAP_FAILED == (char *)interpreter->evaluator_stack) { interpreter->evaluator_stack = 0; interpreter->evaluator_stack_malloced = 1; } if((char *)MAP_FAILED == (char *)interpreter->mark_stack) { interpreter->mark_stack = 0; interpreter->mark_stack_malloced = 1; }
a5cd6a2001-09-24Henrik Grubbström (Grubba) 
529b842004-03-24Henrik Grubbström (Grubba) #ifdef NEED_USE_MALLOC_LABEL
a5cd6a2001-09-24Henrik Grubbström (Grubba) use_malloc:
529b842004-03-24Henrik Grubbström (Grubba) #endif /* NEED_USE_MALLOC_LABEL */
a5cd6a2001-09-24Henrik Grubbström (Grubba) 
529b842004-03-24Henrik Grubbström (Grubba) #else /* !USE_MMAP_FOR_STACK */
ded0492005-01-25Henrik Grubbström (Grubba)  interpreter->evaluator_stack = 0; interpreter->evaluator_stack_malloced = 1; interpreter->mark_stack = 0; interpreter->mark_stack_malloced = 1;
529b842004-03-24Henrik Grubbström (Grubba) #endif /* USE_MMAP_FOR_STACK */
07513e1996-10-04Fredrik Hübinette (Hubbe) 
ded0492005-01-25Henrik Grubbström (Grubba)  if(!interpreter->evaluator_stack)
3c9f631996-09-23Fredrik Hübinette (Hubbe)  {
ded0492005-01-25Henrik Grubbström (Grubba)  if (!(interpreter->evaluator_stack = (struct svalue *)malloc(Pike_stack_size*sizeof(struct svalue)))) return 1; /* Out of memory (evaluator stack). */
3c9f631996-09-23Fredrik Hübinette (Hubbe)  }
ded0492005-01-25Henrik Grubbström (Grubba)  if(!interpreter->mark_stack)
3c9f631996-09-23Fredrik Hübinette (Hubbe)  {
ded0492005-01-25Henrik Grubbström (Grubba)  if (!(interpreter->mark_stack = (struct svalue **)malloc(Pike_stack_size*sizeof(struct svalue *)))) return 2; /* Out of memory (mark stack). */
3c9f631996-09-23Fredrik Hübinette (Hubbe)  }
ded0492005-01-25Henrik Grubbström (Grubba)  interpreter->stack_pointer = interpreter->evaluator_stack; interpreter->mark_stack_pointer = interpreter->mark_stack; interpreter->frame_pointer = 0;
ca176b2006-02-27Martin Stjernholm  interpreter->catch_ctx = NULL; interpreter->catching_eval_jmpbuf = NULL;
62d3e41998-04-05Fredrik Hübinette (Hubbe) 
ded0492005-01-25Henrik Grubbström (Grubba)  interpreter->svalue_stack_margin = SVALUE_STACK_MARGIN; interpreter->c_stack_margin = C_STACK_MARGIN; #ifdef PROFILING interpreter->unlocked_time = 0; interpreter->accounted_time = 0; #endif
59d7632011-11-16Marcus Comstedt  interpreter->trace_level = default_t_flag;
ded0492005-01-25Henrik Grubbström (Grubba)  return 0; /* OK. */ }
411bb52013-08-02Arne Goedeke static struct pike_frame *free_pike_frame;
6af0032013-08-01Arne Goedeke 
ded0492005-01-25Henrik Grubbström (Grubba) PMOD_EXPORT void init_interpreter(void) {
b629e42013-10-09Arne Goedeke #ifdef USE_VALGRIND { static int create_mempool = 1; if (create_mempool) { PIKE_MEMPOOL_CREATE(&free_pike_frame); create_mempool = 0; } } #endif
d97eb72011-07-10Henrik Grubbström (Grubba)  if (low_init_interpreter(Pike_interpreter_pointer)) {
ded0492005-01-25Henrik Grubbström (Grubba)  Pike_fatal("Out of memory initializing the interpreter stack.\n"); }
6697042000-11-20Martin Stjernholm 
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);
424d9c1999-05-02Fredrik Hübinette (Hubbe)  dmalloc_accept_leak(spcb);
62d3e41998-04-05Fredrik Hübinette (Hubbe)  } } #endif
2b506e2002-10-16Marcus Comstedt #if defined(HAVE_COMPUTED_GOTO) || defined(PIKE_USE_MACHINE_CODE)
9271c02002-10-16Marcus Comstedt  { static int tables_need_init=1; if(tables_need_init) { /* Initialize the fcode_to_opcode table / jump labels. */
c606bc2013-08-15Per Hedbor #if !defined(OPCODE_INLINE_RETURN)
9271c02002-10-16Marcus Comstedt  eval_instruction(NULL);
c606bc2013-08-15Per Hedbor #endif
440a122001-07-24Henrik Grubbström (Grubba) #if defined(PIKE_USE_MACHINE_CODE) && !defined(PIKE_DEBUG)
9271c02002-10-16Marcus Comstedt  /* Simple operator opcodes... */
440a122001-07-24Henrik Grubbström (Grubba) #define SET_INSTR_ADDRESS(X, Y) (instrs[(X)-F_OFFSET].address = (void *)Y)
9271c02002-10-16Marcus Comstedt  SET_INSTR_ADDRESS(F_COMPL, o_compl); SET_INSTR_ADDRESS(F_LSH, o_lsh); SET_INSTR_ADDRESS(F_RSH, o_rsh); SET_INSTR_ADDRESS(F_SUBTRACT, o_subtract); SET_INSTR_ADDRESS(F_AND, o_and); SET_INSTR_ADDRESS(F_OR, o_or); SET_INSTR_ADDRESS(F_XOR, o_xor); SET_INSTR_ADDRESS(F_MULTIPLY, o_multiply); SET_INSTR_ADDRESS(F_DIVIDE, o_divide); SET_INSTR_ADDRESS(F_MOD, o_mod); SET_INSTR_ADDRESS(F_CAST, f_cast); SET_INSTR_ADDRESS(F_CAST_TO_INT, o_cast_to_int); SET_INSTR_ADDRESS(F_CAST_TO_STRING, o_cast_to_string);
408a1e2004-10-30Martin Stjernholm  SET_INSTR_ADDRESS(F_RANGE, o_range2);
9271c02002-10-16Marcus Comstedt  SET_INSTR_ADDRESS(F_SSCANF, o_sscanf);
440a122001-07-24Henrik Grubbström (Grubba) #endif /* PIKE_USE_MACHINE_CODE && !PIKE_DEBUG */
9271c02002-10-16Marcus Comstedt  tables_need_init=0;
e22bbe2005-11-08Henrik Grubbström (Grubba) #ifdef INIT_INTERPRETER_STATE INIT_INTERPRETER_STATE(); #endif
9271c02002-10-16Marcus Comstedt  } } #endif /* HAVE_COMPUTED_GOTO || PIKE_USE_MACHINE_CODE */
fa382f1996-06-21Fredrik Hübinette (Hubbe) }
5267b71995-08-09Fredrik Hübinette (Hubbe) /* * lvalues are stored in two svalues in one of these formats:
13670c2015-05-25Martin Nilsson  * array[index] : { array, index } * mapping[index] : { mapping, index } * multiset[index] : { multiset, index }
a903032003-02-16Martin Stjernholm  * object[index] : { object, index } (external object indexing) * local variable : { svalue pointer (T_SVALUE_PTR), nothing (T_VOID) } * global variable : { object, identifier index (T_OBJ_INDEX) } (internal object indexing)
24ef732008-03-29Martin Stjernholm  * lvalue array: { T_ARRAY_LVALUE, array with lvalue pairs }
5267b71995-08-09Fredrik Hübinette (Hubbe)  */
52f3d72015-02-08Henrik Grubbström (Grubba) int lvalue_to_svalue_no_free(struct svalue *to, struct svalue *lval)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
52f3d72015-02-08Henrik Grubbström (Grubba)  int run_time_type; switch(run_time_type = TYPEOF(*lval))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
d6591a2001-03-08Fredrik Hübinette (Hubbe)  case T_ARRAY_LVALUE:
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
2a32691998-01-31Fredrik Hübinette (Hubbe)  INT32 e; struct array *a;
2523ce2003-04-28Martin Stjernholm  TYPE_FIELD types = 0;
2a32691998-01-31Fredrik Hübinette (Hubbe)  ONERROR err; a=allocate_array(lval[1].u.array->size>>1); SET_ONERROR(err, do_free_array, a);
2523ce2003-04-28Martin Stjernholm  for(e=0;e<a->size;e++) { lvalue_to_svalue_no_free(ITEM(a)+e, ITEM(lval[1].u.array)+(e<<1));
017b572011-10-28Henrik Grubbström (Grubba)  types |= 1 << TYPEOF(ITEM(a)[e]);
2523ce2003-04-28Martin Stjernholm  } a->type_field = types;
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(*to, T_ARRAY, 0, array, a);
2a32691998-01-31Fredrik Hübinette (Hubbe)  UNSET_ONERROR(err); break;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
13670c2015-05-25Martin Nilsson 
a903032003-02-16Martin Stjernholm  case T_SVALUE_PTR:
50ea682003-03-14Henrik Grubbström (Grubba)  dmalloc_touch_svalue(lval->u.lval);
2a32691998-01-31Fredrik Hübinette (Hubbe)  assign_svalue_no_free(to, lval->u.lval); break;
a903032003-02-16Martin Stjernholm 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_OBJECT:
4e80f22015-02-08Henrik Grubbström (Grubba)  /* FIXME: Index subtypes! */
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(lval[1]) == T_OBJ_INDEX)
4e80f22015-02-08Henrik Grubbström (Grubba)  run_time_type = low_object_index_no_free(to, lval->u.object, lval[1].u.identifier);
a903032003-02-16Martin Stjernholm  else
4e80f22015-02-08Henrik Grubbström (Grubba)  run_time_type = object_index_no_free(to, lval->u.object, SUBTYPEOF(*lval), lval+1);
2a32691998-01-31Fredrik Hübinette (Hubbe)  break;
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY: simple_array_index_no_free(to, lval->u.array, lval+1); break;
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_MAPPING: mapping_index_no_free(to, lval->u.mapping, lval+1); break;
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_MULTISET: if(multiset_member(lval->u.multiset,lval+1)) {
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(*to, T_INT, NUMBER_NUMBER, integer, 1);
2a32691998-01-31Fredrik Hübinette (Hubbe)  }else{
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(*to, T_INT, NUMBER_UNDEFINED, integer, 0);
2a32691998-01-31Fredrik Hübinette (Hubbe)  } break;
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  default:
9f516a2001-12-16Martin Stjernholm  if(SAFE_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)  }
52f3d72015-02-08Henrik Grubbström (Grubba)  return run_time_type;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void assign_lvalue(struct svalue *lval,struct svalue *from)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
017b572011-10-28Henrik Grubbström (Grubba)  switch(TYPEOF(*lval))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY_LVALUE: { INT32 e;
017b572011-10-28Henrik Grubbström (Grubba)  if(TYPEOF(*from) != T_ARRAY)
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Trying to assign combined lvalue from non-array.\n");
2a32691998-01-31Fredrik Hübinette (Hubbe)  if(from->u.array->size < (lval[1].u.array->size>>1))
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Not enough values for multiple assign.\n");
2a32691998-01-31Fredrik Hübinette (Hubbe) 
bd8d601999-05-13Fredrik Hübinette (Hubbe)  if(from->u.array->size > (lval[1].u.array->size>>1))
b2d3e42000-12-01Fredrik Hübinette (Hubbe)  Pike_error("Too many values for multiple assign.\n");
bd8d601999-05-13Fredrik Hübinette (Hubbe) 
2a32691998-01-31Fredrik Hübinette (Hubbe)  for(e=0;e<from->u.array->size;e++) assign_lvalue(lval[1].u.array->item+(e<<1),from->u.array->item+e); } break;
a903032003-02-16Martin Stjernholm  case T_SVALUE_PTR:
50ea682003-03-14Henrik Grubbström (Grubba)  dmalloc_touch_svalue(from); dmalloc_touch_svalue(lval->u.lval);
5267b71995-08-09Fredrik Hübinette (Hubbe)  assign_svalue(lval->u.lval,from); break; case T_OBJECT:
f54c782004-12-22Henrik Grubbström (Grubba)  /* FIXME: Object subtypes! */
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(lval[1]) == T_OBJ_INDEX)
a903032003-02-16Martin Stjernholm  object_low_set_index (lval->u.object, lval[1].u.identifier, from); else
017b572011-10-28Henrik Grubbström (Grubba)  object_set_index(lval->u.object, SUBTYPEOF(*lval), lval+1, from);
5267b71995-08-09Fredrik Hübinette (Hubbe)  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:
9f516a2001-12-16Martin Stjernholm  if(UNSAFE_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;
13670c2015-05-25Martin Nilsson 
5267b71995-08-09Fredrik Hübinette (Hubbe)  default:
9f516a2001-12-16Martin Stjernholm  if(SAFE_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)  } }
da601b2007-06-11Henrik Grubbström (Grubba) /* On error callback. lvalue is followed by value to assign. */ static void o_assign_lvalue(struct svalue *lvalue) { assign_lvalue(lvalue, lvalue+2); }
5267b71995-08-09Fredrik Hübinette (Hubbe) union anything *get_pointer_if_this_type(struct svalue *lval, TYPE_T t) {
017b572011-10-28Henrik Grubbström (Grubba)  switch(TYPEOF(*lval))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY_LVALUE: return 0;
13670c2015-05-25Martin Nilsson 
a903032003-02-16Martin Stjernholm  case T_SVALUE_PTR:
50ea682003-03-14Henrik Grubbström (Grubba)  dmalloc_touch_svalue(lval->u.lval);
017b572011-10-28Henrik Grubbström (Grubba)  if(TYPEOF(*(lval->u.lval)) == t) return & ( lval->u.lval->u );
2a32691998-01-31Fredrik Hübinette (Hubbe)  return 0;
a903032003-02-16Martin Stjernholm 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_OBJECT:
f54c782004-12-22Henrik Grubbström (Grubba)  /* FIXME: What about object subtypes? */
017b572011-10-28Henrik Grubbström (Grubba)  return object_get_item_ptr(lval->u.object, SUBTYPEOF(*lval), lval+1, t);
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  case T_ARRAY: return array_get_item_ptr(lval->u.array,lval+1,t);
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  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;
13670c2015-05-25Martin Nilsson 
2a32691998-01-31Fredrik Hübinette (Hubbe)  default:
9f516a2001-12-16Martin Stjernholm  if(SAFE_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)  } }
d4425e2000-08-24Henrik Grubbström (Grubba) #ifdef PIKE_DEBUG
54a1302004-06-02Martin Nilsson INLINE void pike_trace(int level,char *fmt, ...) ATTRIBUTE((format (printf, 2, 3))); INLINE void pike_trace(int level,char *fmt, ...)
342fef2000-08-23Fredrik Hübinette (Hubbe) {
97ebb32003-01-09Henrik Grubbström (Grubba)  if(Pike_interpreter.trace_level > level)
342fef2000-08-23Fredrik Hübinette (Hubbe)  { va_list args; va_start(args,fmt); vsprintf(trace_buffer,fmt,args); va_end(args); write_to_stderr(trace_buffer,strlen(trace_buffer)); } } void my_describe_inherit_structure(struct program *p) { struct inherit *in,*last=0; int e,i=0; last=p->inherits-1; fprintf(stderr,"PROGRAM[%d]: inherits=%d identifers_refs=%d ppid=%d\n", p->id, p->num_inherits, p->num_identifier_references,
3af3412002-09-11Martin Stjernholm  p->parent ? p->parent->id : -1);
342fef2000-08-23Fredrik Hübinette (Hubbe)  for(e=0;e<p->num_identifier_references;e++) { in=INHERIT_FROM_INT(p,e); while(last < in) { last++;
08de582000-08-24Henrik Grubbström (Grubba)  fprintf(stderr, "[%ld]%*s parent{ offset=%d ident=%d id=%d } " "id{ level=%d } prog=%d\n",
bd67392015-10-14Martin Nilsson  (long)(last - p->inherits),
342fef2000-08-23Fredrik Hübinette (Hubbe)  last->inherit_level*2,"", last->parent_offset, last->parent_identifier,
3af3412002-09-11Martin Stjernholm  last->prog->parent ? last->prog->parent->id : -1,
342fef2000-08-23Fredrik Hübinette (Hubbe)  last->identifier_level, last->prog->id); i=0; } fprintf(stderr," %*s %d,%d: %s\n", in->inherit_level*2,"", e,i, ID_FROM_INT(p,e)->name->str); i++; } } #define TRACE(X) pike_trace X #else #define TRACE(X) #endif
3478312003-08-03Martin Stjernholm static struct inherit dummy_inherit #ifdef PIKE_DEBUG = {-4711, -4711, -4711, -4711, (size_t) -4711, -4711, NULL, NULL, NULL} #endif ;
5586202006-01-20Henrik Grubbström (Grubba) /* Find the lexical scope @[depth] levels out. * * @[loc]: * Input: * struct object *o // object to start from. * struct inherit *inherit // inherit in o->prog.
9eb5fc2008-05-12Henrik Grubbström (Grubba)  * (int parent_identifier) // identifier in o to start from. * // Only if depth == 0.
5586202006-01-20Henrik Grubbström (Grubba)  * * Output: * struct object *o // object containing the scope. * struct inherit *inherit // inherit in o->prog being the scope. * int parent_identifier // identifier in o from the inherit. */
342fef2000-08-23Fredrik Hübinette (Hubbe) PMOD_EXPORT void find_external_context(struct external_variable_context *loc,
5201c62003-08-02Martin Stjernholm  int depth)
342fef2000-08-23Fredrik Hübinette (Hubbe) {
e6a2672004-02-09Martin Stjernholm  struct program *p;
e428262001-06-10Henrik Grubbström (Grubba) 
5201c62003-08-02Martin Stjernholm  TRACE((4, "-find_external_context(%d, inherit=%ld)\n", depth,
bd67392015-10-14Martin Nilsson  (long)(loc->o->prog ? loc->inherit - loc->o->prog->inherits : 0)));
342fef2000-08-23Fredrik Hübinette (Hubbe) 
3478312003-08-03Martin Stjernholm #ifdef PIKE_DEBUG
342fef2000-08-23Fredrik Hübinette (Hubbe)  if(!loc->o)
3478312003-08-03Martin Stjernholm  Pike_fatal("No object\n"); #endif
342fef2000-08-23Fredrik Hübinette (Hubbe) 
4994ac2004-08-19Henrik Grubbström (Grubba)  if (!(p = loc->o->prog)) { /* magic fallback */ p = get_program_for_object_being_destructed(loc->o); if(!p) { Pike_error("Cannot access parent of destructed object.\n"); } }
e6a2672004-02-09Martin Stjernholm  #ifdef DEBUG_MALLOC if (loc->o->refs == 0x55555555) { fprintf(stderr, "The object %p has been zapped!\n", loc->o); describe(p); Pike_fatal("Object zapping detected.\n"); } if (p && p->refs == 0x55555555) { fprintf(stderr, "The program %p has been zapped!\n", p); describe(p); fprintf(stderr, "Which taken from the object %p\n", loc->o); describe(loc->o); Pike_fatal("Looks like the program %p has been zapped!\n", p); } #endif /* DEBUG_MALLOC */
5201c62003-08-02Martin Stjernholm  while(--depth>=0)
342fef2000-08-23Fredrik Hübinette (Hubbe)  {
3478312003-08-03Martin Stjernholm  struct inherit *inh = loc->inherit; if (!p) Pike_error("Attempting to access parent of destructed object.\n");
13670c2015-05-25Martin Nilsson #ifdef PIKE_DEBUG
3478312003-08-03Martin Stjernholm  if(Pike_interpreter.trace_level>8) my_describe_inherit_structure(p);
342fef2000-08-23Fredrik Hübinette (Hubbe) #endif TRACE((4,"- i->parent_offset=%d i->parent_identifier=%d\n",
3478312003-08-03Martin Stjernholm  inh->parent_offset, inh->parent_identifier));
f3c7152001-04-14Fredrik Hübinette (Hubbe) 
3478312003-08-03Martin Stjernholm  TRACE((4,"- o->parent_identifier=%d inherit->identifier_level=%d\n", (p->flags & PROGRAM_USES_PARENT) ?
4994ac2004-08-19Henrik Grubbström (Grubba)  LOW_PARENT_INFO(loc->o, p)->parent_identifier : -1,
3478312003-08-03Martin Stjernholm  inh->identifier_level));
342fef2000-08-23Fredrik Hübinette (Hubbe) 
3478312003-08-03Martin Stjernholm  switch(inh->parent_offset)
342fef2000-08-23Fredrik Hübinette (Hubbe)  { default: {
5586202006-01-20Henrik Grubbström (Grubba)  /* Find the program that inherited us. */
3478312003-08-03Martin Stjernholm  int my_level = inh->inherit_level;
342fef2000-08-23Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
3478312003-08-03Martin Stjernholm  if(!my_level)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Gahhh! inherit level zero in wrong place!\n");
342fef2000-08-23Fredrik Hübinette (Hubbe) #endif
3478312003-08-03Martin Stjernholm  while(loc->inherit->inherit_level >= my_level)
342fef2000-08-23Fredrik Hübinette (Hubbe)  {
5586202006-01-20Henrik Grubbström (Grubba)  TRACE((5,"- inherit-- (%d >= %d)\n", loc->inherit->inherit_level, my_level));
3478312003-08-03Martin Stjernholm  loc->inherit--;
5586202006-01-20Henrik Grubbström (Grubba)  TRACE((5, "- identifier_level: %d\n", loc->inherit->identifier_level));
342fef2000-08-23Fredrik Hübinette (Hubbe)  }
3478312003-08-03Martin Stjernholm  find_external_context(loc, inh->parent_offset);
5586202006-01-20Henrik Grubbström (Grubba)  TRACE((5, "- inh->parent_identifier: %d\n" "- inh->identifier_level: %d\n" "- loc->parent_identifier: %d\n" "- loc->inherit->parent_offset: %d\n" "- loc->inherit->identifier_level: %d\n", inh->parent_identifier, inh->identifier_level, loc->parent_identifier, loc->inherit->parent_offset, loc->inherit->identifier_level));
342fef2000-08-23Fredrik Hübinette (Hubbe)  loc->parent_identifier =
3478312003-08-03Martin Stjernholm  inh->parent_identifier + loc->inherit->identifier_level;
5586202006-01-20Henrik Grubbström (Grubba)  TRACE((5, "- parent_identifier: %d\n", loc->parent_identifier));
342fef2000-08-23Fredrik Hübinette (Hubbe)  } break;
5201c62003-08-02Martin Stjernholm  case INHERIT_PARENT:
342fef2000-08-23Fredrik Hübinette (Hubbe)  TRACE((5,"- Following inherit->parent\n"));
3478312003-08-03Martin Stjernholm  loc->parent_identifier=inh->parent_identifier; loc->o=inh->parent;
13670c2015-05-25Martin Nilsson #ifdef PIKE_DEBUG
5586202006-01-20Henrik Grubbström (Grubba)  TRACE((5, "- parent_identifier: %d\n" "- o: %p\n"
251dbf2006-07-05Martin Stjernholm  "- inh: %"PRINTPTRDIFFT"d\n",
5586202006-01-20Henrik Grubbström (Grubba)  loc->parent_identifier, loc->o, loc->inherit - loc->o->prog->inherits)); if(Pike_interpreter.trace_level>5) { dump_program_tables(loc->o->prog, 4); } #endif
342fef2000-08-23Fredrik Hübinette (Hubbe)  break;
5201c62003-08-02Martin Stjernholm  case OBJECT_PARENT:
342fef2000-08-23Fredrik Hübinette (Hubbe)  TRACE((5,"- Following o->parent\n"));
3478312003-08-03Martin Stjernholm  #ifdef PIKE_DEBUG /* Can this happen legitimately? Well, someone will hopefully * let me know in that case. /mast */ if (!(p->flags & PROGRAM_USES_PARENT)) Pike_fatal ("Attempting to access parent of object without parent pointer.\n"); #endif loc->parent_identifier=LOW_PARENT_INFO(loc->o,p)->parent_identifier; loc->o=LOW_PARENT_INFO(loc->o,p)->parent;
13670c2015-05-25Martin Nilsson #ifdef PIKE_DEBUG
5586202006-01-20Henrik Grubbström (Grubba)  TRACE((5, "- parent_identifier: %d\n" "- o: %p\n", loc->parent_identifier, loc->o)); if(Pike_interpreter.trace_level>5) { dump_program_tables(loc->o->prog, 4); } #endif
342fef2000-08-23Fredrik Hübinette (Hubbe)  break; }
3478312003-08-03Martin Stjernholm  #ifdef PIKE_DEBUG /* I don't think this should happen either. The gc doesn't zap the * pointer even if the object is destructed, at least. /mast */ if (!loc->o) Pike_fatal ("No parent object.\n"); #endif p = loc->o->prog;
342fef2000-08-23Fredrik Hübinette (Hubbe) #ifdef DEBUG_MALLOC if (loc->o->refs == 0x55555555) { fprintf(stderr, "The object %p has been zapped!\n", loc->o); describe(p);
5aad932002-08-15Marcus Comstedt  Pike_fatal("Object zapping detected.\n");
342fef2000-08-23Fredrik Hübinette (Hubbe)  }
3478312003-08-03Martin Stjernholm  if (p && p->refs == 0x55555555) {
342fef2000-08-23Fredrik Hübinette (Hubbe)  fprintf(stderr, "The program %p has been zapped!\n", p); describe(p); fprintf(stderr, "Which taken from the object %p\n", loc->o); describe(loc->o);
5aad932002-08-15Marcus Comstedt  Pike_fatal("Looks like the program %p has been zapped!\n", p);
342fef2000-08-23Fredrik Hübinette (Hubbe)  } #endif /* DEBUG_MALLOC */
e6a2672004-02-09Martin Stjernholm  if (p) {
342fef2000-08-23Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
e6a2672004-02-09Martin Stjernholm  if(loc->parent_identifier < 0 || loc->parent_identifier > p->num_identifier_references) Pike_fatal("Identifier out of range, loc->parent_identifer=%d!\n", loc->parent_identifier);
342fef2000-08-23Fredrik Hübinette (Hubbe) #endif
3478312003-08-03Martin Stjernholm  loc->inherit=INHERIT_FROM_INT(p, loc->parent_identifier);
251dbf2006-07-05Martin Stjernholm  TRACE((5, "- loc->inherit: %"PRINTPTRDIFFT"d\n",
5586202006-01-20Henrik Grubbström (Grubba)  loc->inherit - loc->o->prog->inherits));
e6a2672004-02-09Martin Stjernholm  }
3478312003-08-03Martin Stjernholm  else /* Return a valid pointer to a dummy inherit for the convenience * of the caller. Identifier offsets will be bogus but it'll * never get to that since the object is destructed. */ loc->inherit = &dummy_inherit;
342fef2000-08-23Fredrik Hübinette (Hubbe) 
d1cac52000-09-07Henrik Grubbström (Grubba)  TRACE((5,"- Parent identifier = %d (%s), inherit # = %ld\n",
342fef2000-08-23Fredrik Hübinette (Hubbe)  loc->parent_identifier,
3478312003-08-03Martin Stjernholm  p ? ID_FROM_INT(p, loc->parent_identifier)->name->str : "N/A",
bd67392015-10-14Martin Nilsson  p ? (long)(loc->inherit - p->inherits) : -1));
13670c2015-05-25Martin Nilsson 
342fef2000-08-23Fredrik Hübinette (Hubbe) #ifdef DEBUG_MALLOC
3478312003-08-03Martin Stjernholm  if (p && loc->inherit->storage_offset == 0x55555555) {
342fef2000-08-23Fredrik Hübinette (Hubbe)  fprintf(stderr, "The inherit %p has been zapped!\n", loc->inherit); debug_malloc_dump_references(loc->inherit,0,2,0); fprintf(stderr, "It was extracted from the program %p %d\n", p, loc->parent_identifier); describe(p); fprintf(stderr, "Which was in turn taken from the object %p\n", loc->o); describe(loc->o);
5aad932002-08-15Marcus Comstedt  Pike_fatal("Looks like the program %p has been zapped!\n", p);
342fef2000-08-23Fredrik Hübinette (Hubbe)  } #endif /* DEBUG_MALLOC */ } TRACE((4,"--find_external_context: parent_id=%d (%s)\n", loc->parent_identifier,
3478312003-08-03Martin Stjernholm  p ? ID_FROM_INT(p,loc->parent_identifier)->name->str : "N/A"
342fef2000-08-23Fredrik 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) {
97ebb32003-01-09Henrik Grubbström (Grubba)  if(Pike_interpreter.trace_level>3)
5683de1995-11-06Fredrik Hübinette (Hubbe)  { char *s;
9fa0ee2003-11-09Martin Stjernholm  dynamic_buffer save_buf; init_buf(&save_buf);
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue(Pike_sp-1,0,0);
9fa0ee2003-11-09Martin Stjernholm  s=simple_free_buf(&save_buf);
6d8c692000-08-08Henrik Grubbström (Grubba)  if((size_t)strlen(s) > (size_t)TRACE_LEN)
5683de1995-11-06Fredrik Hübinette (Hubbe)  { s[TRACE_LEN]=0; s[TRACE_LEN-1]='.'; s[TRACE_LEN-2]='.';
53bb632000-02-15Henrik Grubbström (Grubba)  s[TRACE_LEN-3]='.';
5683de1995-11-06Fredrik Hübinette (Hubbe)  } 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;
c5016f1996-04-13Fredrik Hübinette (Hubbe) 
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) {
f077582000-07-06Fredrik Hübinette (Hubbe)  Pike_fp=0; pop_n_elems(Pike_sp - Pike_interpreter.evaluator_stack);
ca176b2006-02-27Martin Stjernholm  #ifdef PIKE_DEBUG if (Pike_interpreter.catch_ctx) Pike_fatal ("Catch context spillover.\n"); if (Pike_interpreter.catching_eval_jmpbuf) Pike_fatal ("Got an active catching_eval_jmpbuf.\n"); #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
31984c2003-03-20Martin Stjernholm 
c6830f2008-03-29Martin Stjernholm #define BACKLOG 100
5267b71995-08-09Fredrik Hübinette (Hubbe) struct backlog {
31984c2003-03-20Martin Stjernholm  PIKE_INSTR_T instruction;
c7ef072000-04-21Fredrik Hübinette (Hubbe)  INT32 arg,arg2;
c6bc962008-05-11Martin Stjernholm  INT32 program_id;
eff6212001-07-09Henrik Grubbström (Grubba)  PIKE_OPCODE_T *pc;
8eaef52000-04-21Fredrik Hübinette (Hubbe) #ifdef _REENTRANT
114e2a2003-02-16Martin Stjernholm  struct thread_state *thread_state;
8eaef52000-04-21Fredrik Hübinette (Hubbe) #endif
6d8c692000-08-08Henrik Grubbström (Grubba)  ptrdiff_t stack; ptrdiff_t mark_stack;
5267b71995-08-09Fredrik Hübinette (Hubbe) }; struct backlog backlog[BACKLOG]; int backlogp=BACKLOG-1;
54a1302004-06-02Martin Nilsson static INLINE void low_debug_instr_prologue (PIKE_INSTR_T instr)
31984c2003-03-20Martin Stjernholm { if(Pike_interpreter.trace_level > 2) {
18e0182003-08-07Henrik Grubbström (Grubba)  char *file = NULL, *f;
31984c2003-03-20Martin Stjernholm  struct pike_string *filep;
ef24a82012-01-12Henrik Grubbström (Grubba)  INT_TYPE linep;
31984c2003-03-20Martin Stjernholm 
fa93a52008-02-28Henrik Grubbström (Grubba)  filep = get_line(Pike_fp->pc,Pike_fp->context->prog,&linep);
31984c2003-03-20Martin Stjernholm  if (filep && !filep->size_shift) { file = filep->str;
5aa54c2014-09-03Martin Nilsson  while((f=strchr(file,'/')))
31984c2003-03-20Martin Stjernholm  file=f+1; }
41f3632011-05-17Henrik Grubbström (Grubba)  fprintf(stderr,"- %s:%4ld:%p(%"PRINTPTRDIFFT"d): "
31984c2003-03-20Martin Stjernholm  "%-25s %4"PRINTPTRDIFFT"d %4"PRINTPTRDIFFT"d\n", file ? file : "-",(long)linep,
41f3632011-05-17Henrik Grubbström (Grubba)  Pike_fp->pc, Pike_fp->pc - Pike_fp->context->prog->program,
31984c2003-03-20Martin Stjernholm  get_opcode_name(instr), Pike_sp-Pike_interpreter.evaluator_stack, Pike_mark_sp-Pike_interpreter.mark_stack); free_string(filep); } #ifdef HAVE_COMPUTED_GOTO
13670c2015-05-25Martin Nilsson  if (instr)
31984c2003-03-20Martin Stjernholm  ADD_RUNNED(instr); else Pike_fatal("NULL Instruction!\n"); #else /* !HAVE_COMPUTED_GOTO */
13670c2015-05-25Martin Nilsson  if(instr + F_OFFSET < F_MAX_OPCODE)
31984c2003-03-20Martin Stjernholm  ADD_RUNNED(instr); #endif /* HAVE_COMPUTED_GOTO */
5db0912012-06-28Per Hedbor  if(d_flag )
31984c2003-03-20Martin Stjernholm  { backlogp++; if(backlogp >= BACKLOG) backlogp=0;
c6bc962008-05-11Martin Stjernholm  backlog[backlogp].program_id = Pike_fp->context->prog->id;
31984c2003-03-20Martin Stjernholm  backlog[backlogp].instruction=instr;
b1f6a62003-03-21Martin Stjernholm  backlog[backlogp].pc = Pike_fp->pc;
31984c2003-03-20Martin Stjernholm  backlog[backlogp].stack = Pike_sp - Pike_interpreter.evaluator_stack; backlog[backlogp].mark_stack = Pike_mark_sp - Pike_interpreter.mark_stack; #ifdef _REENTRANT backlog[backlogp].thread_state=Pike_interpreter.thread_state; #endif #ifdef _REENTRANT CHECK_INTERPRETER_LOCK(); if(d_flag>1) DEBUG_CHECK_THREAD(); #endif
1f4d702011-10-28Henrik Grubbström (Grubba)  INVALIDATE_SVAL(Pike_sp[0]); INVALIDATE_SVAL(Pike_sp[1]); INVALIDATE_SVAL(Pike_sp[2]); INVALIDATE_SVAL(Pike_sp[3]);
5db0912012-06-28Per Hedbor 
31984c2003-03-20Martin Stjernholm  if(Pike_sp<Pike_interpreter.evaluator_stack || Pike_mark_sp < Pike_interpreter.mark_stack || Pike_fp->locals>Pike_sp) Pike_fatal("Stack error (generic) sp=%p/%p mark_sp=%p/%p locals=%p.\n", Pike_sp, Pike_interpreter.evaluator_stack, Pike_mark_sp, Pike_interpreter.mark_stack, Pike_fp->locals);
5db0912012-06-28Per Hedbor 
31984c2003-03-20Martin Stjernholm  if(Pike_mark_sp > Pike_interpreter.mark_stack+Pike_stack_size) Pike_fatal("Mark Stack error (overflow).\n"); if(Pike_mark_sp < Pike_interpreter.mark_stack) Pike_fatal("Mark Stack error (underflow).\n"); if(Pike_sp > Pike_interpreter.evaluator_stack+Pike_stack_size) Pike_fatal("stack error (overflow).\n");
5db0912012-06-28Per Hedbor  /* The locals will not be correct when running FILL_STACK (actually, they will always be incorrect before running FILL_STACK, but at least currently that is the first opcode run). */
5909f92013-08-15Per Hedbor  /* as it turns out, this is no longer true.. */ /* if( instr+F_OFFSET != F_FILL_STACK ) */ /* { */ /* if(/\* Pike_fp->fun>=0 && *\/ Pike_fp->current_object->prog && */ /* Pike_fp->locals+Pike_fp->num_locals > Pike_sp) */ /* Pike_fatal("Stack error (stupid! %p %p+%x).\n",Pike_sp, */ /* Pike_fp->locals, Pike_fp->num_locals*sizeof(struct svalue)); */ /* } */
31984c2003-03-20Martin Stjernholm  if(Pike_interpreter.recoveries && (Pike_sp-Pike_interpreter.evaluator_stack < Pike_interpreter.recoveries->stack_pointer)) Pike_fatal("Stack error (underflow).\n"); if(Pike_mark_sp > Pike_interpreter.mark_stack && Pike_mark_sp[-1] > Pike_sp) Pike_fatal("Stack error (underflow?)\n");
13670c2015-05-25Martin Nilsson 
31984c2003-03-20Martin Stjernholm  if(d_flag > 9) do_debug(); debug_malloc_touch(Pike_fp->current_object); switch(d_flag) { default: case 3: check_object(Pike_fp->current_object); /* break; */ case 2: check_object_context(Pike_fp->current_object,
fa93a52008-02-28Henrik Grubbström (Grubba)  Pike_fp->context->prog,
31984c2003-03-20Martin Stjernholm  Pike_fp->current_object->storage+
fa93a52008-02-28Henrik Grubbström (Grubba)  Pike_fp->context->storage_offset);
31984c2003-03-20Martin Stjernholm  case 1: case 0: break; } } } #define DEBUG_LOG_ARG(ARG) \ (backlog[backlogp].arg = (ARG), \ (Pike_interpreter.trace_level>3 ? \ sprintf(trace_buffer, "- Arg = %ld\n", \ (long) backlog[backlogp].arg), \ write_to_stderr(trace_buffer,strlen(trace_buffer)) : 0)) #define DEBUG_LOG_ARG2(ARG2) \ (backlog[backlogp].arg2 = (ARG2), \ (Pike_interpreter.trace_level>3 ? \ sprintf(trace_buffer, "- Arg2 = %ld\n", \ (long) backlog[backlogp].arg2), \ write_to_stderr(trace_buffer,strlen(trace_buffer)) : 0))
bf10442015-04-30Martin Nilsson PMOD_EXPORT void dump_backlog(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
086d652000-04-26Fredrik Hübinette (Hubbe) #ifdef _REENTRANT
114e2a2003-02-16Martin Stjernholm  struct thread_state *thread=0;
086d652000-04-26Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  int e; if(!d_flag || backlogp<0 || backlogp>=BACKLOG) return; e=backlogp; do {
c6bc962008-05-11Martin Stjernholm  struct program *p;
5267b71995-08-09Fredrik Hübinette (Hubbe)  e++; if(e>=BACKLOG) e=0;
c6bc962008-05-11Martin Stjernholm  p = id_to_program (backlog[e].program_id); if (p)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
50edc82001-07-13Henrik Grubbström (Grubba)  struct pike_string *file;
ef24a82012-01-12Henrik Grubbström (Grubba)  INT_TYPE line;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
086d652000-04-26Fredrik Hübinette (Hubbe) #ifdef _REENTRANT
114e2a2003-02-16Martin Stjernholm  if(thread != backlog[e].thread_state)
086d652000-04-26Fredrik Hübinette (Hubbe)  {
114e2a2003-02-16Martin Stjernholm  fprintf(stderr,"[Thread swap, Pike_interpreter.thread_state=%p]\n",backlog[e].thread_state); thread = backlog[e].thread_state;
086d652000-04-26Fredrik Hübinette (Hubbe)  } #endif
c6bc962008-05-11Martin Stjernholm  file = get_line(backlog[e].pc,p, &line);
eff6212001-07-09Henrik Grubbström (Grubba) #ifdef HAVE_COMPUTED_GOTO
31984c2003-03-20Martin Stjernholm  fprintf(stderr,"%s:%ld:(%"PRINTPTRDIFFT"d): %s",
50edc82001-07-13Henrik Grubbström (Grubba)  file->str,
eff6212001-07-09Henrik Grubbström (Grubba)  (long)line,
c6bc962008-05-11Martin Stjernholm  backlog[e].pc - p->program,
afa1c62001-07-09Henrik Grubbström (Grubba)  get_opcode_name(backlog[e].instruction));
eff6212001-07-09Henrik Grubbström (Grubba) #else /* !HAVE_COMPUTED_GOTO */
31984c2003-03-20Martin Stjernholm  if(backlog[e].instruction+F_OFFSET > F_MAX_OPCODE)
c7ef072000-04-21Fredrik Hübinette (Hubbe)  {
31984c2003-03-20Martin Stjernholm  fprintf(stderr,"%s:%ld:(%"PRINTPTRDIFFT"d): ILLEGAL INSTRUCTION %d\n",
50edc82001-07-13Henrik Grubbström (Grubba)  file->str,
c7ef072000-04-21Fredrik Hübinette (Hubbe)  (long)line,
c6bc962008-05-11Martin Stjernholm  backlog[e].pc - p->program,
c7ef072000-04-21Fredrik Hübinette (Hubbe)  backlog[e].instruction + F_OFFSET);
50edc82001-07-13Henrik Grubbström (Grubba)  free_string(file);
c7ef072000-04-21Fredrik Hübinette (Hubbe)  continue; }
31984c2003-03-20Martin Stjernholm  fprintf(stderr,"%s:%ld:(%"PRINTPTRDIFFT"d): %s",
50edc82001-07-13Henrik Grubbström (Grubba)  file->str,
b9e2202000-05-03Fredrik Hübinette (Hubbe)  (long)line,
c6bc962008-05-11Martin Stjernholm  backlog[e].pc - p->program, low_get_f_name(backlog[e].instruction + F_OFFSET, p));
c7ef072000-04-21Fredrik Hübinette (Hubbe)  if(instrs[backlog[e].instruction].flags & I_HASARG2) {
b9e2202000-05-03Fredrik Hübinette (Hubbe)  fprintf(stderr,"(%ld,%ld)",
c7ef072000-04-21Fredrik Hübinette (Hubbe)  (long)backlog[e].arg, (long)backlog[e].arg2); }
f142842003-08-06Martin Stjernholm  else if(instrs[backlog[e].instruction].flags & I_POINTER)
ac61a32001-01-31Martin Stjernholm  { fprintf(stderr,"(%+ld)", (long)backlog[e].arg); }
c7ef072000-04-21Fredrik Hübinette (Hubbe)  else if(instrs[backlog[e].instruction].flags & I_HASARG) {
b9e2202000-05-03Fredrik Hübinette (Hubbe)  fprintf(stderr,"(%ld)", (long)backlog[e].arg);
c7ef072000-04-21Fredrik Hübinette (Hubbe)  }
bd67392015-10-14Martin Nilsson  fprintf(stderr," %ld, %ld\n", (long)backlog[e].stack, (long)backlog[e].mark_stack);
eff6212001-07-09Henrik Grubbström (Grubba) #endif /* HAVE_COMPUTED_GOTO */
50edc82001-07-13Henrik Grubbström (Grubba)  free_string(file);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }while(e!=backlogp);
cb22561995-10-11Fredrik Hübinette (Hubbe) }
5267b71995-08-09Fredrik Hübinette (Hubbe) 
31984c2003-03-20Martin Stjernholm #else /* PIKE_DEBUG */ #define DEBUG_LOG_ARG(arg) 0 #define DEBUG_LOG_ARG2(arg2) 0 #endif /* !PIKE_DEBUG */
cc08512013-06-12Per Hedbor  #define POP_CATCH_CONTEXT do { \ struct catch_context *cc = Pike_interpreter.catch_ctx; \ DO_IF_DEBUG ( \ TRACE((3,"- Popping catch context %p ==> %p\n", \ cc, cc ? cc->prev : NULL)); \ if (!Pike_interpreter.catching_eval_jmpbuf) \ Pike_fatal ("Not in catching eval.\n"); \ if (!cc) \ Pike_fatal ("Catch context dropoff.\n"); \ if (cc->frame != Pike_fp) \ Pike_fatal ("Catch context doesn't belong to this frame.\n"); \
ca176b2006-02-27Martin Stjernholm  if (Pike_mark_sp != cc->recovery.mark_sp + Pike_interpreter.mark_stack) \
cc08512013-06-12Per Hedbor  Pike_fatal ("Mark sp diff in catch context pop.\n"); \ ); \ debug_malloc_touch (cc); \ UNSETJMP (cc->recovery); \ Pike_fp->expendible = cc->save_expendible; \ Pike_interpreter.catch_ctx = cc->prev; \ really_free_catch_context (cc); \
ca176b2006-02-27Martin Stjernholm  } while (0)
a64f6b2001-01-12Martin Stjernholm 
cc08512013-06-12Per Hedbor static struct catch_context *free_catch_context; static int num_catch_ctx, num_free_catch_ctx; PMOD_EXPORT void really_free_catch_context( struct catch_context *data ) { if( num_free_catch_ctx > 100 && free_catch_context ) { num_catch_ctx--; free( data ); } else { data->prev = free_catch_context;
8ba86d2013-07-04Arne Goedeke 
cc08512013-06-12Per Hedbor  num_free_catch_ctx++;
8ba86d2013-07-04Arne Goedeke  PIKE_MEM_NA(*data);
ac5a552013-10-09Arne Goedeke  PIKE_MEM_RW(data->prev);
cc08512013-06-12Per Hedbor  free_catch_context = data; } } struct catch_context *alloc_catch_context(void) { struct catch_context *res; if( free_catch_context ) { num_free_catch_ctx--; res = free_catch_context;
8ba86d2013-07-04Arne Goedeke  PIKE_MEM_RW(res->prev);
cc08512013-06-12Per Hedbor  free_catch_context = res->prev;
8ba86d2013-07-04Arne Goedeke  PIKE_MEM_WO(*res);
cc08512013-06-12Per Hedbor  } else { num_catch_ctx++; res = xalloc( sizeof( struct catch_context ) ); } return res; } void count_memory_in_catch_contexts(size_t *num, size_t *size ) { *num = (num_catch_ctx-num_free_catch_ctx); *size = num_catch_ctx * (sizeof(struct catch_context)+8); /* assumes 8 bytes overhead. */ }
e551452014-03-01Martin Nilsson #ifdef DO_PIKE_CLEANUP
cc08512013-06-12Per Hedbor static void free_all_catch_context_blocks(void) { struct catch_context *x = free_catch_context, *n; while( x ) {
8ba86d2013-07-04Arne Goedeke  PIKE_MEM_RW(x->prev);
cc08512013-06-12Per Hedbor  n = x->prev; free( x ); x = n; } free_catch_context = NULL; }
e551452014-03-01Martin Nilsson #endif
cc08512013-06-12Per Hedbor 
ca176b2006-02-27Martin Stjernholm static int catching_eval_instruction (PIKE_OPCODE_T *pc);
b7c1ee2001-01-10Martin Stjernholm 
f822262001-07-16Fredrik Hübinette (Hubbe)  #ifdef PIKE_USE_MACHINE_CODE
4555f32011-05-11Henrik Grubbström (Grubba) #ifdef OPCODE_INLINE_RETURN /* Catch notes: * * Typical F_CATCH use: * * F_CATCH * F_PTR continue_label * * ENTRY * * catch body * * F_EXIT_CATCH * * F_BRANCH * F_PTR continue_label * * ENTRY * * continue_label: * * rest of code. */ /* Modified calling-conventions to simplify code-generation when * INTER_RETURN is inlined. * * cf interpret_functions.h:F_CATCH * * Arguments: * addr:
55e9462011-05-16Henrik Grubbström (Grubba)  * Address where the continue POINTER (INT32) is stored. * Directly after the POINTER is the ENTRY for the catch block.
4555f32011-05-11Henrik Grubbström (Grubba)  * * Returns: * (PIKE_OPCODE_T *)-1 on INTER_RETURN. * jump_destination otherwise. */
55e9462011-05-16Henrik Grubbström (Grubba) PIKE_OPCODE_T *inter_return_opcode_F_CATCH(PIKE_OPCODE_T *addr)
4555f32011-05-11Henrik Grubbström (Grubba) {
be9d5f2011-05-12Dan Nelson #ifdef PIKE_DEBUG
4555f32011-05-11Henrik Grubbström (Grubba)  if (d_flag || Pike_interpreter.trace_level > 2) { low_debug_instr_prologue (F_CATCH - F_OFFSET); if (Pike_interpreter.trace_level>3) {
55e9462011-05-16Henrik Grubbström (Grubba)  sprintf(trace_buffer, "- Addr = %p\n", addr);
4555f32011-05-11Henrik Grubbström (Grubba)  write_to_stderr(trace_buffer,strlen(trace_buffer)); } }
be9d5f2011-05-12Dan Nelson #endif
4555f32011-05-11Henrik Grubbström (Grubba)  { struct catch_context *new_catch_ctx = alloc_catch_context(); #ifdef PIKE_DEBUG new_catch_ctx->frame = Pike_fp; init_recovery (&new_catch_ctx->recovery, 0, 0, PERR_LOCATION()); #else init_recovery (&new_catch_ctx->recovery, 0); #endif new_catch_ctx->save_expendible = Pike_fp->expendible;
2978c32014-01-11Tobias S. Josefowitz  new_catch_ctx->continue_reladdr = (INT32)get_unaligned32(addr)
4555f32011-05-11Henrik Grubbström (Grubba)  /* We need to run the entry prologue... */ - ENTRY_PROLOGUE_SIZE; new_catch_ctx->next_addr = addr; new_catch_ctx->prev = Pike_interpreter.catch_ctx; Pike_interpreter.catch_ctx = new_catch_ctx; DO_IF_DEBUG({
f805212011-05-17Henrik Grubbström (Grubba)  TRACE((3,"- Pushed catch context %p\n", new_catch_ctx));
4555f32011-05-11Henrik Grubbström (Grubba)  }); } Pike_fp->expendible = Pike_fp->locals + Pike_fp->num_locals; /* Need to adjust next_addr by sizeof(INT32) to skip past the jump * address to the continue position after the catch block. */ addr = (PIKE_OPCODE_T *) ((INT32 *) addr + 1); if (Pike_interpreter.catching_eval_jmpbuf) { /* There's already a catching_eval_instruction around our * eval_instruction, so we can just continue. */ debug_malloc_touch_named (Pike_interpreter.catch_ctx, "(1)"); /* Skip past the entry prologue... */ addr += ENTRY_PROLOGUE_SIZE; DO_IF_DEBUG({ TRACE((3,"- In active catch; continuing at %p\n", addr)); }); return addr; } else { debug_malloc_touch_named (Pike_interpreter.catch_ctx, "(2)"); while (1) { /* Loop here every time an exception is caught. Once we've * gotten here and set things up to run eval_instruction from * inside catching_eval_instruction, we keep doing it until it's * time to return. */ int res; DO_IF_DEBUG({ TRACE((3,"- Activating catch; calling %p in context %p\n", addr, Pike_interpreter.catch_ctx)); }); res = catching_eval_instruction (addr); DO_IF_DEBUG({ TRACE((3,"- catching_eval_instruction(%p) returned %d\n", addr, res)); }); if (res != -3) { /* There was an inter return inside the evaluated code. Just * propagate it. */ DO_IF_DEBUG ({ TRACE((3,"- Returning from catch.\n")); if (res != -1) Pike_fatal ("Unexpected return value from " "catching_eval_instruction: %d\n", res); }); break; } else { /* Caught an exception. */ struct catch_context *cc = Pike_interpreter.catch_ctx; DO_IF_DEBUG ({ TRACE((3,"- Caught exception. catch context: %p\n", cc)); if (!cc) Pike_fatal ("Catch context dropoff.\n"); if (cc->frame != Pike_fp) Pike_fatal ("Catch context doesn't belong to this frame.\n"); }); debug_malloc_touch_named (cc, "(3)"); UNSETJMP (cc->recovery); Pike_fp->expendible = cc->save_expendible; move_svalue (Pike_sp++, &throw_value); mark_free_svalue (&throw_value); low_destruct_objects_to_destruct(); if (cc->continue_reladdr < 0) FAST_CHECK_THREADS_ON_BRANCH(); addr = cc->next_addr + cc->continue_reladdr; DO_IF_DEBUG({ TRACE((3,"- Popping catch context %p ==> %p\n", cc, cc->prev)); if (!addr) Pike_fatal ("Unexpected null continue addr.\n"); }); Pike_interpreter.catch_ctx = cc->prev; really_free_catch_context (cc); } } return (PIKE_OPCODE_T *)(ptrdiff_t)-1; /* INTER_RETURN; */ } } void *do_inter_return_label = (void*)(ptrdiff_t)-1; #else
f822262001-07-16Fredrik Hübinette (Hubbe) /* Labels to jump to to cause eval_instruction to return */ /* FIXME: Replace these with assembler lables */
2b506e2002-10-16Marcus Comstedt void *do_inter_return_label = NULL;
9271c02002-10-16Marcus Comstedt void *dummy_label = NULL;
4555f32011-05-11Henrik Grubbström (Grubba) #endif
f822262001-07-16Fredrik Hübinette (Hubbe) 
9a41452001-08-01Marcus Comstedt #ifndef CALL_MACHINE_CODE #define CALL_MACHINE_CODE(pc) \ do { \ /* The test is needed to get the labels to work... */ \ if (pc) { \ /* No extra setup needed! \ */ \ return ((int (*)(void))(pc))(); \ } \
7293b72001-08-03Henrik Grubbström (Grubba)  } while(0)
9a41452001-08-01Marcus Comstedt #endif /* !CALL_MACHINE_CODE */
fd9b662003-03-22Martin Stjernholm #ifndef EXIT_MACHINE_CODE
be79d72003-03-23Martin Stjernholm #define EXIT_MACHINE_CODE()
fd9b662003-03-22Martin Stjernholm #endif
d6d7892008-11-19Martin Stjernholm /* Intended to be called from machine code before inlined function * calls (primarily the CALL_BUILTIN opcodes), to ensure thread * switching. */
3b54e52014-08-21Martin Nilsson void call_check_threads_etc(void)
aebaa42008-11-19Martin Stjernholm { FAST_CHECK_THREADS_ON_CALL(); }
8434c22003-12-09Henrik Grubbström (Grubba) #if defined(OPCODE_INLINE_BRANCH) || defined(INS_F_JUMP) || \ defined(INS_F_JUMP_WITH_ARG) || defined(INS_F_JUMP_WITH_TWO_ARGS)
142b002003-11-25Martin Stjernholm /* Intended to be called from machine code on backward branch jumps, * to ensure thread switching. */
3b54e52014-08-21Martin Nilsson void branch_check_threads_etc(void)
142b002003-11-25Martin Stjernholm {
51d7d82008-11-18Martin Stjernholm  FAST_CHECK_THREADS_ON_BRANCH();
142b002003-11-25Martin Stjernholm } #endif
31984c2003-03-20Martin Stjernholm #ifdef PIKE_DEBUG
b1f6a62003-03-21Martin Stjernholm static void debug_instr_prologue (PIKE_INSTR_T instr)
31984c2003-03-20Martin Stjernholm {
b1f6a62003-03-21Martin Stjernholm  low_debug_instr_prologue (instr);
31984c2003-03-20Martin Stjernholm } #define DEBUG_PROLOGUE(OPCODE, EXTRA) do { \ if (d_flag || Pike_interpreter.trace_level > 2) { \
b1f6a62003-03-21Martin Stjernholm  debug_instr_prologue ((OPCODE) - F_OFFSET); \
31984c2003-03-20Martin Stjernholm  EXTRA; \ } \ } while (0) /* The following are intended to be called directly from generated * machine code. */
b1f6a62003-03-21Martin Stjernholm void simple_debug_instr_prologue_0 (PIKE_INSTR_T instr)
31984c2003-03-20Martin Stjernholm { if (d_flag || Pike_interpreter.trace_level > 2)
b1f6a62003-03-21Martin Stjernholm  low_debug_instr_prologue (instr);
31984c2003-03-20Martin Stjernholm }
b1f6a62003-03-21Martin Stjernholm void simple_debug_instr_prologue_1 (PIKE_INSTR_T instr, INT32 arg)
31984c2003-03-20Martin Stjernholm { if (d_flag || Pike_interpreter.trace_level > 2) {
b1f6a62003-03-21Martin Stjernholm  low_debug_instr_prologue (instr);
31984c2003-03-20Martin Stjernholm  DEBUG_LOG_ARG (arg); } }
b1f6a62003-03-21Martin Stjernholm void simple_debug_instr_prologue_2 (PIKE_INSTR_T instr, INT32 arg1, INT32 arg2)
31984c2003-03-20Martin Stjernholm { if (d_flag || Pike_interpreter.trace_level > 2) {
b1f6a62003-03-21Martin Stjernholm  low_debug_instr_prologue (instr);
31984c2003-03-20Martin Stjernholm  DEBUG_LOG_ARG (arg1); DEBUG_LOG_ARG2 (arg2); } } #endif /* !PIKE_DEBUG */
9606eb2004-11-12Henrik Grubbström (Grubba) #endif /* PIKE_USE_MACHINE_CODE */
ca176b2006-02-27Martin Stjernholm /* These don't change when eval_instruction_without_debug is compiled. */ #ifdef PIKE_DEBUG #define REAL_PIKE_DEBUG #define DO_IF_REAL_DEBUG(X) X #define DO_IF_NOT_REAL_DEBUG(X) #else #define DO_IF_REAL_DEBUG(X) #define DO_IF_NOT_REAL_DEBUG(X) X #endif
9606eb2004-11-12Henrik Grubbström (Grubba) #ifdef PIKE_SMALL_EVAL_INSTRUCTION #undef PROG_COUNTER #define PROG_COUNTER Pike_fp->pc+1 #endif /* PIKE_SMALL_EVAL_INSTRUCTION */ #if defined(PIKE_USE_MACHINE_CODE) || defined(PIKE_SMALL_EVAL_INSTRUCTION) #ifndef DEF_PROG_COUNTER #define DEF_PROG_COUNTER #endif /* !DEF_PROG_COUNTER */ #ifndef DEBUG_PROLOGUE #define DEBUG_PROLOGUE(OPCODE, EXTRA) do {} while (0) #endif
2bcc0d2002-05-11Martin Stjernholm #define OPCODE0(O,N,F,C) \
1524392001-07-16Fredrik Hübinette (Hubbe) void PIKE_CONCAT(opcode_,O)(void) { \
8328ca2001-07-18Henrik Grubbström (Grubba)  DEF_PROG_COUNTER; \
31984c2003-03-20Martin Stjernholm  DEBUG_PROLOGUE (O, ;); \
1524392001-07-16Fredrik Hübinette (Hubbe) C }
f822262001-07-16Fredrik Hübinette (Hubbe) 
2bcc0d2002-05-11Martin Stjernholm #define OPCODE1(O,N,F,C) \
1524392001-07-16Fredrik Hübinette (Hubbe) void PIKE_CONCAT(opcode_,O)(INT32 arg1) {\
8328ca2001-07-18Henrik Grubbström (Grubba)  DEF_PROG_COUNTER; \
31984c2003-03-20Martin Stjernholm  DEBUG_PROLOGUE (O, DEBUG_LOG_ARG (arg1)); \
1524392001-07-16Fredrik Hübinette (Hubbe) C }
f822262001-07-16Fredrik Hübinette (Hubbe) 
2bcc0d2002-05-11Martin Stjernholm #define OPCODE2(O,N,F,C) \
1524392001-07-16Fredrik Hübinette (Hubbe) void PIKE_CONCAT(opcode_,O)(INT32 arg1,INT32 arg2) { \
8328ca2001-07-18Henrik Grubbström (Grubba)  DEF_PROG_COUNTER; \
31984c2003-03-20Martin Stjernholm  DEBUG_PROLOGUE (O, DEBUG_LOG_ARG (arg1); DEBUG_LOG_ARG2 (arg2)); \
1524392001-07-16Fredrik Hübinette (Hubbe) C }
9606eb2004-11-12Henrik Grubbström (Grubba) #if defined(OPCODE_RETURN_JUMPADDR) || defined(PIKE_SMALL_EVAL_INSTRUCTION)
f142842003-08-06Martin Stjernholm  #define OPCODE0_JUMP(O,N,F,C) \ void *PIKE_CONCAT(jump_opcode_,O)(void) { \ void *jumpaddr DO_IF_DEBUG(= NULL); \ DEF_PROG_COUNTER; \ DEBUG_PROLOGUE (O, ;); \ C; \ JUMP_DONE; \ } #define OPCODE1_JUMP(O,N,F,C) \ void *PIKE_CONCAT(jump_opcode_,O)(INT32 arg1) { \ void *jumpaddr DO_IF_DEBUG(= NULL); \ DEF_PROG_COUNTER; \ DEBUG_PROLOGUE (O, DEBUG_LOG_ARG (arg1)); \ C; \ JUMP_DONE; \ } #define OPCODE2_JUMP(O,N,F,C) \ void *PIKE_CONCAT(jump_opcode_,O)(INT32 arg1, INT32 arg2) { \ void *jumpaddr DO_IF_DEBUG(= NULL); \ DEF_PROG_COUNTER; \ DEBUG_PROLOGUE (O, DEBUG_LOG_ARG (arg1); DEBUG_LOG_ARG2 (arg2)); \ C; \ JUMP_DONE; \ } #define SET_PROG_COUNTER(X) (jumpaddr = (X)) #ifdef PIKE_DEBUG #define JUMP_DONE do { \ if (!jumpaddr) \ Pike_fatal ("Instruction didn't set jump address.\n"); \ return jumpaddr; \ } while (0) #else #define JUMP_DONE return jumpaddr #endif
9606eb2004-11-12Henrik Grubbström (Grubba) #else /* !OPCODE_RETURN_JUMPADDR && !PIKE_SMALL_EVAL_INSTRUCTION */
f142842003-08-06Martin Stjernholm #define OPCODE0_JUMP OPCODE0 #define OPCODE1_JUMP OPCODE1 #define OPCODE2_JUMP OPCODE2 #define JUMP_DONE DONE
9606eb2004-11-12Henrik Grubbström (Grubba) #endif /* OPCODE_RETURN_JUMPADDR || PIKE_SMALL_EVAL_INSTRUCTION */
f142842003-08-06Martin Stjernholm 
9606eb2004-11-12Henrik Grubbström (Grubba) #if defined(OPCODE_INLINE_BRANCH) || defined(PIKE_SMALL_EVAL_INSTRUCTION)
3e25ec2002-11-02Henrik Grubbström (Grubba) #define TEST_OPCODE0(O,N,F,C) \
4555f32011-05-11Henrik Grubbström (Grubba) ptrdiff_t PIKE_CONCAT(test_opcode_,O)(void) { \ ptrdiff_t branch_taken = 0; \
3e25ec2002-11-02Henrik Grubbström (Grubba)  DEF_PROG_COUNTER; \
31984c2003-03-20Martin Stjernholm  DEBUG_PROLOGUE (O, ;); \
3e25ec2002-11-02Henrik Grubbström (Grubba)  C; \ return branch_taken; \ } #define TEST_OPCODE1(O,N,F,C) \
4555f32011-05-11Henrik Grubbström (Grubba) ptrdiff_t PIKE_CONCAT(test_opcode_,O)(INT32 arg1) {\ ptrdiff_t branch_taken = 0; \
3e25ec2002-11-02Henrik Grubbström (Grubba)  DEF_PROG_COUNTER; \
31984c2003-03-20Martin Stjernholm  DEBUG_PROLOGUE (O, DEBUG_LOG_ARG (arg1)); \
3e25ec2002-11-02Henrik Grubbström (Grubba)  C; \ return branch_taken; \ } #define TEST_OPCODE2(O,N,F,C) \
4555f32011-05-11Henrik Grubbström (Grubba) ptrdiff_t PIKE_CONCAT(test_opcode_,O)(INT32 arg1, INT32 arg2) { \ ptrdiff_t branch_taken = 0; \
3e25ec2002-11-02Henrik Grubbström (Grubba)  DEF_PROG_COUNTER; \
31984c2003-03-20Martin Stjernholm  DEBUG_PROLOGUE (O, DEBUG_LOG_ARG (arg1); DEBUG_LOG_ARG2 (arg2)); \
3e25ec2002-11-02Henrik Grubbström (Grubba)  C; \ return branch_taken; \ }
13684b2002-11-04Marcus Comstedt #define DO_BRANCH() (branch_taken = -1) #define DONT_BRANCH() (branch_taken = 0)
9606eb2004-11-12Henrik Grubbström (Grubba) #else /* !OPCODE_INLINE_BRANCH && !PIKE_SMALL_EVAL_INSTRUCTION */
f142842003-08-06Martin Stjernholm #define TEST_OPCODE0(O,N,F,C) OPCODE0_PTRJUMP(O,N,F,C) #define TEST_OPCODE1(O,N,F,C) OPCODE1_PTRJUMP(O,N,F,C) #define TEST_OPCODE2(O,N,F,C) OPCODE2_PTRJUMP(O,N,F,C)
9606eb2004-11-12Henrik Grubbström (Grubba) #endif /* OPCODE_INLINE_BRANCH || PIKE_SMALL_EVAL_INSTRUCTION */
1524392001-07-16Fredrik Hübinette (Hubbe) 
2bcc0d2002-05-11Martin Stjernholm #define OPCODE0_TAIL(O,N,F,C) OPCODE0(O,N,F,C) #define OPCODE1_TAIL(O,N,F,C) OPCODE1(O,N,F,C) #define OPCODE2_TAIL(O,N,F,C) OPCODE2(O,N,F,C)
1524392001-07-16Fredrik Hübinette (Hubbe) 
f142842003-08-06Martin Stjernholm #define OPCODE0_PTRJUMP(O,N,F,C) OPCODE0_JUMP(O,N,F,C) #define OPCODE1_PTRJUMP(O,N,F,C) OPCODE1_JUMP(O,N,F,C) #define OPCODE2_PTRJUMP(O,N,F,C) OPCODE2_JUMP(O,N,F,C) #define OPCODE0_TAILPTRJUMP(O,N,F,C) OPCODE0_PTRJUMP(O,N,F,C) #define OPCODE1_TAILPTRJUMP(O,N,F,C) OPCODE1_PTRJUMP(O,N,F,C) #define OPCODE2_TAILPTRJUMP(O,N,F,C) OPCODE2_PTRJUMP(O,N,F,C)
f822262001-07-16Fredrik Hübinette (Hubbe) 
4555f32011-05-11Henrik Grubbström (Grubba) #define OPCODE0_RETURN(O,N,F,C) OPCODE0_JUMP(O,N,F | I_RETURN,C) #define OPCODE1_RETURN(O,N,F,C) OPCODE1_JUMP(O,N,F | I_RETURN,C) #define OPCODE2_RETURN(O,N,F,C) OPCODE2_JUMP(O,N,F | I_RETURN,C)
f142842003-08-06Martin Stjernholm #define OPCODE0_TAILRETURN(O,N,F,C) OPCODE0_RETURN(O,N,F,C) #define OPCODE1_TAILRETURN(O,N,F,C) OPCODE1_RETURN(O,N,F,C) #define OPCODE2_TAILRETURN(O,N,F,C) OPCODE2_RETURN(O,N,F,C)
7997532001-07-27Henrik Grubbström (Grubba) 
3e25ec2002-11-02Henrik Grubbström (Grubba) /* BRANCH opcodes only generate code for the test, * so that the branch instruction can be inlined. */ #define OPCODE0_BRANCH(O,N,F,C) TEST_OPCODE0(O,N,F,C) #define OPCODE1_BRANCH(O,N,F,C) TEST_OPCODE1(O,N,F,C) #define OPCODE2_BRANCH(O,N,F,C) TEST_OPCODE2(O,N,F,C) #define OPCODE0_TAILBRANCH(O,N,F,C) TEST_OPCODE0(O,N,F,C) #define OPCODE1_TAILBRANCH(O,N,F,C) TEST_OPCODE1(O,N,F,C) #define OPCODE2_TAILBRANCH(O,N,F,C) TEST_OPCODE2(O,N,F,C)
105be62002-11-10Henrik Grubbström (Grubba) #define OPCODE0_ALIAS(O,N,F,C) #define OPCODE1_ALIAS(O,N,F,C) #define OPCODE2_ALIAS(O,N,F,C)
f822262001-07-16Fredrik Hübinette (Hubbe) #undef HAVE_COMPUTED_GOTO
58bc3d2003-08-07Martin Stjernholm #ifdef GLOBAL_DEF_PROG_COUNTER GLOBAL_DEF_PROG_COUNTER; #endif
9606eb2004-11-12Henrik Grubbström (Grubba) #ifndef SET_PROG_COUNTER #define SET_PROG_COUNTER(X) (PROG_COUNTER=(X)) #endif /* SET_PROG_COUNTER */ #undef DONE #undef FETCH #undef INTER_RETURN #define DONE return #define FETCH #define INTER_RETURN {SET_PROG_COUNTER(do_inter_return_label);JUMP_DONE;} #if defined(PIKE_USE_MACHINE_CODE) && defined(_M_IX86) /* Disable frame pointer optimization */ #pragma optimize("y", off) #endif #include "interpret_functions_fixed.h" #if defined(PIKE_USE_MACHINE_CODE) && defined(_M_IX86) /* Restore optimization */ #pragma optimize("", on) #endif #ifdef PIKE_SMALL_EVAL_INSTRUCTION #undef SET_PROG_COUNTER #undef PROG_COUNTER #define PROG_COUNTER pc #endif #endif /* PIKE_USE_MACHINE_CODE || PIKE_SMALL_EVAL_INSTRUCTION */ #ifdef PIKE_USE_MACHINE_CODE
62a1bc2002-11-24Henrik Grubbström (Grubba) #ifdef PIKE_DEBUG /* Note: The debug code is extracted, to keep the frame size constant. */ static int eval_instruction_low(PIKE_OPCODE_T *pc); #endif /* PIKE_DEBUG */
f822262001-07-16Fredrik Hübinette (Hubbe) 
62a1bc2002-11-24Henrik Grubbström (Grubba) static int eval_instruction(PIKE_OPCODE_T *pc)
0996a92001-07-17Henrik Grubbström (Grubba) #ifdef PIKE_DEBUG
62a1bc2002-11-24Henrik Grubbström (Grubba) {
46072b2006-03-15Henrik Grubbström (Grubba)  int x;
fcc65d2003-02-26Martin Stjernholm  if (Pike_interpreter.trace_level > 5 && pc) {
0996a92001-07-17Henrik Grubbström (Grubba)  int i;
e73f112002-10-30Martin Nilsson  fprintf(stderr, "Calling code at %p:\n", pc);
2f401a2001-07-26Henrik Grubbström (Grubba) #ifdef PIKE_OPCODE_ALIGN if (((INT32)pc) % PIKE_OPCODE_ALIGN) {
5aad932002-08-15Marcus Comstedt  Pike_fatal("Odd offset!\n");
c696242001-07-18Henrik Grubbström (Grubba)  }
2f401a2001-07-26Henrik Grubbström (Grubba) #endif /* PIKE_OPCODE_ALIGN */
e22bbe2005-11-08Henrik Grubbström (Grubba) #ifdef DISASSEMBLE_CODE DISASSEMBLE_CODE(pc, 16*4); #else /* !DISASSEMBLE_CODE */
0996a92001-07-17Henrik Grubbström (Grubba)  for (i=0; i < 16; i+=4) { fprintf(stderr, " 0x%08x 0x%08x 0x%08x 0x%08x\n", ((int *)pc)[i], ((int *)pc)[i+1], ((int *)pc)[i+2], ((int *)pc)[i+3]); }
e22bbe2005-11-08Henrik Grubbström (Grubba) #endif /* DISASSEMBLE_CODE */
0996a92001-07-17Henrik Grubbström (Grubba)  }
46072b2006-03-15Henrik Grubbström (Grubba)  x = eval_instruction_low(pc); pike_trace(3, "- eval_instruction(%p) ==> %d\n", pc, x); return x;
62a1bc2002-11-24Henrik Grubbström (Grubba) } static int eval_instruction_low(PIKE_OPCODE_T *pc)
0996a92001-07-17Henrik Grubbström (Grubba) #endif /* PIKE_DEBUG */
62a1bc2002-11-24Henrik Grubbström (Grubba) {
4555f32011-05-11Henrik Grubbström (Grubba) #ifndef OPCODE_INLINE_RETURN
c606bc2013-08-15Per Hedbor  if(pc == NULL) {
62a1bc2002-11-24Henrik Grubbström (Grubba)  if(do_inter_return_label != NULL) Pike_fatal("eval_instruction called with NULL (twice).\n");
7448ce2003-08-06Martin Stjernholm #ifdef __GNUC__
62a1bc2002-11-24Henrik Grubbström (Grubba)  do_inter_return_label = && inter_return_label;
7448ce2003-08-06Martin Stjernholm #elif defined (_M_IX86) /* MSVC. */
1d9d952003-07-31Tomas Nilsson  _asm {
7448ce2003-08-06Martin Stjernholm  lea eax,inter_return_label
1d9d952003-07-31Tomas Nilsson  mov do_inter_return_label,eax }
7448ce2003-08-06Martin Stjernholm #else #error Machine code not supported with this compiler. #endif
1d9d952003-07-31Tomas Nilsson 
ca176b2006-02-27Martin Stjernholm #if 0
a770ac2003-08-10Henrik Grubbström (Grubba)  /* Paranoia. * * This can happen on systems with delay slots if the labels aren't * used explicitly. */ if (do_inter_return_label == do_escape_catch_label) { Pike_fatal("Inter return and escape catch labels are equal: %p\n", do_inter_return_label); }
ca176b2006-02-27Martin Stjernholm #endif
a770ac2003-08-10Henrik Grubbström (Grubba) 
1d9d952003-07-31Tomas Nilsson  /* Trick optimizer */ if(!dummy_label) return 0; }
09fca82006-02-28Martin Stjernholm  /* This else is important to avoid an overoptimization bug in (at
35cf6c2006-02-28Martin Stjernholm  * least) gcc 4.0.2 20050808 which caused the address stored in * do_inter_return_label to be at the CALL_MACHINE_CODE below. */
09fca82006-02-28Martin Stjernholm  else {
c606bc2013-08-15Per Hedbor #endif /* !OPCODE_INLINE_RETURN */
09fca82006-02-28Martin Stjernholm  CALL_MACHINE_CODE(pc);
1d9d952003-07-31Tomas Nilsson 
c606bc2013-08-15Per Hedbor #ifndef OPCODE_INLINE_RETURN
09fca82006-02-28Martin Stjernholm  /* This code is never reached, but will * prevent gcc from optimizing the labels below too much */
1d9d952003-07-31Tomas Nilsson 
7448ce2003-08-06Martin Stjernholm #ifdef PIKE_DEBUG
09fca82006-02-28Martin Stjernholm  fprintf(stderr,"We have reached the end of the world!\n");
7448ce2003-08-06Martin Stjernholm #endif
09fca82006-02-28Martin Stjernholm  }
1d9d952003-07-31Tomas Nilsson 
3205122003-08-10Henrik Grubbström (Grubba) #ifdef __GNUC__ goto *dummy_label; #endif
7448ce2003-08-06Martin Stjernholm  inter_return_label:
c606bc2013-08-15Per Hedbor #endif /*!OPCODE_INLINE_RETURUN */
46072b2006-03-15Henrik Grubbström (Grubba) #ifdef PIKE_DEBUG pike_trace(3, "- Inter return\n"); #endif
c606bc2013-08-15Per Hedbor  EXIT_MACHINE_CODE();
1d9d952003-07-31Tomas Nilsson  return -1; }
f822262001-07-16Fredrik Hübinette (Hubbe) #else /* PIKE_USE_MACHINE_CODE */
7b2c002001-07-18Henrik Grubbström (Grubba) #ifndef SET_PROG_COUNTER #define SET_PROG_COUNTER(X) (PROG_COUNTER=(X)) #endif /* SET_PROG_COUNTER */
afa1c62001-07-09Henrik Grubbström (Grubba) #ifdef HAVE_COMPUTED_GOTO int lookup_sort_fun(const void *a, const void *b) { return (int)(((ptrdiff_t)((struct op_2_f *)a)->opcode) - ((ptrdiff_t)((struct op_2_f *)b)->opcode)); } #endif /* HAVE_COMPUTED_GOTO */
88149c2001-07-08Henrik Grubbström (Grubba) /* NOTE: Due to the implementation of computed goto, * interpreter.h may only be included once. */ #if defined(PIKE_DEBUG) && !defined(HAVE_COMPUTED_GOTO)
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #define eval_instruction eval_instruction_with_debug
d49add2001-04-25Fredrik Hübinette (Hubbe) #include "interpreter_debug.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #undef eval_instruction #define eval_instruction eval_instruction_without_debug
f822262001-07-16Fredrik Hübinette (Hubbe) 
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #undef PIKE_DEBUG
f822262001-07-16Fredrik Hübinette (Hubbe) #undef NDEBUG #undef DO_IF_DEBUG #define DO_IF_DEBUG(X)
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #define print_return_value() #include "interpreter.h"
f822262001-07-16Fredrik Hübinette (Hubbe) 
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #define PIKE_DEBUG
f822262001-07-16Fredrik Hübinette (Hubbe) #define NDEBUG #undef DO_IF_DEBUG #define DO_IF_DEBUG(X) X #undef print_return_value
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #undef eval_instruction
5267b71995-08-09Fredrik Hübinette (Hubbe) 
54a1302004-06-02Martin Nilsson static INLINE int eval_instruction(unsigned char *pc)
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) {
97ebb32003-01-09Henrik Grubbström (Grubba)  if(d_flag || Pike_interpreter.trace_level>2)
7d7d7e1999-01-31Fredrik Hübinette (Hubbe)  return eval_instruction_with_debug(pc); else return eval_instruction_without_debug(pc); }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
9606eb2004-11-12Henrik Grubbström (Grubba) #else /* !PIKE_DEBUG || HAVE_COMPUTED_GOTO */
7d7d7e1999-01-31Fredrik Hübinette (Hubbe) #include "interpreter.h"
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) #endif
a599231996-09-25Fredrik Hübinette (Hubbe) 
f822262001-07-16Fredrik Hübinette (Hubbe)  #endif /* PIKE_USE_MACHINE_CODE */
ca176b2006-02-27Martin Stjernholm #undef REAL_PIKE_DEBUG #undef DO_IF_REAL_DEBUG #undef DO_IF_NOT_REAL_DEBUG
bd8fb82004-04-03Martin Stjernholm static void do_trace_call(INT32 args, dynamic_buffer *old_buf)
cd83521998-02-02Fredrik Hübinette (Hubbe) {
bd8fb82004-04-03Martin Stjernholm  struct pike_string *filep = NULL; char *file, *s;
ef24a82012-01-12Henrik Grubbström (Grubba)  INT_TYPE linep; INT32 e;
a0e0012007-04-16Henrik Grubbström (Grubba)  ptrdiff_t len = 0;
f142842003-08-06Martin Stjernholm 
bd8fb82004-04-03Martin Stjernholm  my_strcat("("); for(e=0;e<args;e++) { if(e) my_strcat(",");
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue(Pike_sp-args+e,0,0);
bd8fb82004-04-03Martin Stjernholm  } my_strcat(")"); s=simple_free_buf(old_buf);
6d8c692000-08-08Henrik Grubbström (Grubba)  if((size_t)strlen(s) > (size_t)TRACE_LEN)
cd83521998-02-02Fredrik Hübinette (Hubbe)  { s[TRACE_LEN]=0; s[TRACE_LEN-1]='.'; s[TRACE_LEN-2]='.';
bd8fb82004-04-03Martin Stjernholm  s[TRACE_LEN-3]='.'; } if(Pike_fp && Pike_fp->pc) { char *f;
fa93a52008-02-28Henrik Grubbström (Grubba)  filep = get_line(Pike_fp->pc,Pike_fp->context->prog,&linep);
bd8fb82004-04-03Martin Stjernholm  if (filep->size_shift) file = "..."; else { file = filep->str;
5aa54c2014-09-03Martin Nilsson  while((f = strchr(file, '/'))
a0e0012007-04-16Henrik Grubbström (Grubba) #ifdef __NT__
5aa54c2014-09-03Martin Nilsson  || (f = strchr(file, '\\'))
a0e0012007-04-16Henrik Grubbström (Grubba) #endif /* __NT__ */ )
bd8fb82004-04-03Martin Stjernholm  file=f+1;
a0e0012007-04-16Henrik Grubbström (Grubba)  len = filep->len - (file - filep->str);
bd8fb82004-04-03Martin Stjernholm  } }else{ linep=0; file="-"; }
a0e0012007-04-16Henrik Grubbström (Grubba)  if (len < 30)
bd8fb82004-04-03Martin Stjernholm  { char buf[40]; if (linep)
7f102e2014-10-21Martin Nilsson  snprintf(buf, sizeof (buf), "%s:%ld:", file, (long)linep);
bd8fb82004-04-03Martin Stjernholm  else
7f102e2014-10-21Martin Nilsson  snprintf(buf, sizeof (buf), "%s:", file);
a0e0012007-04-16Henrik Grubbström (Grubba)  fprintf(stderr, "- %-20s %s\n",buf,s); } else if (linep) { fprintf(stderr, "- %s:%ld: %s\n", file, (long)linep, s); } else { fprintf(stderr, "- %s: %s\n", file, s);
bd8fb82004-04-03Martin Stjernholm  } if (filep) { free_string(filep);
cd83521998-02-02Fredrik Hübinette (Hubbe)  } free(s); }
bd8fb82004-04-03Martin Stjernholm static void do_trace_func_return (int got_retval, struct object *o, int fun) { dynamic_buffer save_buf; init_buf (&save_buf);
a93c4d2004-04-03Martin Stjernholm  if (o) {
bd8fb82004-04-03Martin Stjernholm  if (o->prog) { struct identifier *id = ID_FROM_INT (o->prog, fun); char buf[50];
bd67392015-10-14Martin Nilsson  sprintf(buf, "%lx->", (long) PTR_TO_INT (o));
bd8fb82004-04-03Martin Stjernholm  my_strcat(buf); if (id->name->size_shift) my_strcat ("[widestring function name]"); else my_strcat(id->name->str); my_strcat ("() "); } else my_strcat ("function in destructed object "); } do_trace_return (got_retval, &save_buf); } static void do_trace_return (int got_retval, dynamic_buffer *old_buf)
cd83521998-02-02Fredrik Hübinette (Hubbe) {
50edc82001-07-13Henrik Grubbström (Grubba)  struct pike_string *filep = NULL; char *file, *s;
ef24a82012-01-12Henrik Grubbström (Grubba)  INT_TYPE linep;
bd8fb82004-04-03Martin Stjernholm  if (got_retval) { my_strcat ("returns: ");
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue(Pike_sp-1,0,0);
cd83521998-02-02Fredrik Hübinette (Hubbe)  }
bd8fb82004-04-03Martin Stjernholm  else my_strcat ("returns with no value");
9fa0ee2003-11-09Martin Stjernholm  s=simple_free_buf(old_buf);
6d8c692000-08-08Henrik Grubbström (Grubba)  if((size_t)strlen(s) > (size_t)TRACE_LEN)
cd83521998-02-02Fredrik Hübinette (Hubbe)  { s[TRACE_LEN]=0; s[TRACE_LEN-1]='.'; s[TRACE_LEN-2]='.';
bd8fb82004-04-03Martin Stjernholm  s[TRACE_LEN-3]='.';
cd83521998-02-02Fredrik Hübinette (Hubbe)  }
bd8fb82004-04-03Martin Stjernholm 
f077582000-07-06Fredrik Hübinette (Hubbe)  if(Pike_fp && Pike_fp->pc)
cd83521998-02-02Fredrik Hübinette (Hubbe)  { char *f;
fa93a52008-02-28Henrik Grubbström (Grubba)  filep = get_line(Pike_fp->pc,Pike_fp->context->prog,&linep);
bd8fb82004-04-03Martin Stjernholm  if (filep->size_shift) file = "..."; else { file = filep->str;
5aa54c2014-09-03Martin Nilsson  while((f=strchr(file,'/')))
bd8fb82004-04-03Martin Stjernholm  file=f+1; }
cd83521998-02-02Fredrik Hübinette (Hubbe)  }else{ linep=0; file="-"; }
bd8fb82004-04-03Martin Stjernholm  { char buf[40]; if (linep)
7f102e2014-10-21Martin Nilsson  snprintf(buf, sizeof (buf), "%s:%ld:", file, (long)linep);
bd8fb82004-04-03Martin Stjernholm  else
7f102e2014-10-21Martin Nilsson  snprintf(buf, sizeof (buf), "%s:", file);
bd8fb82004-04-03Martin Stjernholm  fprintf(stderr,"- %-20s %s\n",buf,s); }
50edc82001-07-13Henrik Grubbström (Grubba)  if (filep) { free_string(filep); }
cd83521998-02-02Fredrik Hübinette (Hubbe)  free(s); }
cc08512013-06-12Per Hedbor static struct pike_frame_chunk { struct pike_frame_chunk *next; } *pike_frame_chunks; static int num_pike_frame_chunks; static int num_pike_frames; PMOD_EXPORT void really_free_pike_frame( struct pike_frame *X ) { free_object(X->current_object); if(X->current_program) free_program(X->current_program); if(X->scope) free_pike_scope(X->scope); DO_IF_DEBUG( if(X->flags & PIKE_FRAME_MALLOCED_LOCALS) Pike_fatal("Pike frame is not supposed to have malloced locals here!\n")); DO_IF_DMALLOC( X->current_program=0; X->context=0; X->scope=0; X->current_object=0; X->flags=0; X->expendible=0; X->locals=0; ); X->next = free_pike_frame;
411bb52013-08-02Arne Goedeke  PIKE_MEMPOOL_FREE(&free_pike_frame, X, sizeof(struct pike_frame));
cc08512013-06-12Per Hedbor  free_pike_frame = X; } struct pike_frame *alloc_pike_frame(void) { struct pike_frame *res; if( free_pike_frame ) { res = free_pike_frame;
411bb52013-08-02Arne Goedeke  PIKE_MEMPOOL_ALLOC(&free_pike_frame, res, sizeof(struct pike_frame)); PIKE_MEM_RW_RANGE(&res->next, sizeof(void*));
cc08512013-06-12Per Hedbor  free_pike_frame = res->next;
411bb52013-08-02Arne Goedeke  PIKE_MEM_WO_RANGE(&res->next, sizeof(void*));
cc08512013-06-12Per Hedbor  res->refs=0; add_ref(res); /* For DMALLOC... */ res->flags=0; res->next=0; res->scope=0; return res; } /* Need to allocate more. */ { unsigned int i;
5cd9072013-08-02Arne Goedeke #define FRAMES_PER_CHUNK ((4096*4-8-sizeof(struct pike_frame_chunk))/sizeof(struct pike_frame)) #define FRAME_CHUNK_SIZE (FRAMES_PER_CHUNK*sizeof(struct pike_frame))+sizeof(struct pike_frame_chunk)
cc08512013-06-12Per Hedbor  void *p = xalloc( FRAME_CHUNK_SIZE ); num_pike_frame_chunks++; ((struct pike_frame_chunk*)p)->next = pike_frame_chunks; pike_frame_chunks = p; free_pike_frame = res = (struct pike_frame*)((char*)p+sizeof(struct pike_frame_chunk)); for( i=1; i<FRAMES_PER_CHUNK; i++ ) { res->next = &free_pike_frame[i]; res = res->next; } res->next = NULL; num_pike_frames+=FRAMES_PER_CHUNK; } return alloc_pike_frame(); }
4218011999-01-31Fredrik Hübinette (Hubbe) 
cc08512013-06-12Per Hedbor void count_memory_in_pike_frames(size_t *num, size_t *size ) { *num = num_pike_frames; *size = num_pike_frame_chunks * (FRAME_CHUNK_SIZE*8); } #undef FRAMES_PER_CHUNK #undef FRAME_CHUNK_SIZE
fa21262001-08-31Fredrik Hübinette (Hubbe) 
e551452014-03-01Martin Nilsson #ifdef DO_PIKE_CLEANUP
cc08512013-06-12Per Hedbor static void free_all_pike_frame_blocks(void) { struct pike_frame_chunk *x = pike_frame_chunks, *n; while( x ) { n = x->next; free(x); x = n; } free_pike_frame = NULL; pike_frame_chunks = NULL; num_pike_frames=0; num_pike_frame_chunks=0; }
e551452014-03-01Martin Nilsson #endif
fa21262001-08-31Fredrik Hübinette (Hubbe)  void really_free_pike_scope(struct pike_frame *scope) { if(scope->flags & PIKE_FRAME_MALLOCED_LOCALS) { free_mixed_svalues(scope->locals,scope->num_locals);
0ec7522014-04-27Martin Nilsson  free(scope->locals);
fa21262001-08-31Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG scope->flags&=~PIKE_FRAME_MALLOCED_LOCALS; #endif } really_free_pike_frame(scope); }
4218011999-01-31Fredrik Hübinette (Hubbe) 
5909f92013-08-15Per Hedbor void *lower_mega_apply( INT32 args, struct object *o, ptrdiff_t fun )
6417722013-06-12Per Hedbor { struct program *p; check_stack(256); check_mark_stack(256); if( (p = o->prog) ) { struct svalue *save_sp = Pike_sp - args; struct reference *ref = p->identifier_references + fun; struct inherit *context = p->inherits + ref->inherit_offset; struct identifier *function = context->prog->identifiers + ref->identifier_offset; struct svalue *constant = NULL; struct pike_frame *new_frame = NULL; int type = (function->identifier_flags & (IDENTIFIER_TYPE_MASK|IDENTIFIER_ALIAS)); if( o->prog != pike_trampoline_program && function->func.offset != -1 ) { switch( type ) { case IDENTIFIER_CONSTANT: constant = &context->prog->constants[function->func.const_info.offset].sval; if( TYPEOF(*constant) != PIKE_T_PROGRAM ) break; case IDENTIFIER_C_FUNCTION: case IDENTIFIER_PIKE_FUNCTION: if( !new_frame ) { new_frame=alloc_pike_frame(); debug_malloc_touch(new_frame); } #ifdef PROFILING new_frame->children_base = Pike_interpreter.accounted_time; new_frame->start_time = get_cpu_time() - Pike_interpreter.unlocked_time; /* This is mostly for profiling, but * could also be used to find out the name of a function * in a destructed object. -hubbe * * Since it not used for anything but profiling yet, I will * put it here until someone needs it. -Hubbe */ new_frame->ident = ref->identifier_offset; DO_IF_PROFILING_DEBUG({ fprintf(stderr, "%p{: Push at %" PRINT_CPU_TIME " %" PRINT_CPU_TIME "\n", Pike_interpreter.thread_state, new_frame->start_time, new_frame->children_base); }); #endif new_frame->next = Pike_fp; add_ref(new_frame->current_object = o); add_ref(new_frame->current_program = p); new_frame->context = context;
bd67392015-10-14Martin Nilsson  new_frame->fun = (unsigned INT16)fun;
6417722013-06-12Per Hedbor  new_frame->expendible = new_frame->locals = save_sp; new_frame->args = args; new_frame->save_sp = save_sp; #ifdef PIKE_DEBUG if (Pike_in_gc > GC_PASS_PREPARE && Pike_in_gc < GC_PASS_FREE) Pike_fatal("Pike code called within gc.\n"); #endif Pike_fp = new_frame; debug_malloc_touch(Pike_fp); #ifdef PROFILING function->num_calls++; function->recur_depth++; #endif
0055c22013-06-12Tobias S. Josefowitz #ifdef PIKE_USE_MACHINE_CODE
6417722013-06-12Per Hedbor  call_check_threads_etc();
0055c22013-06-12Tobias S. Josefowitz #endif
6417722013-06-12Per Hedbor  if( !constant ) { if (PIKE_FN_START_ENABLED()) { /* DTrace enter probe arg0: function name arg1: object */ dynamic_buffer save_buf; dynbuf_string obj_name; struct svalue obj_sval; SET_SVAL(obj_sval, T_OBJECT, 0, object, o); init_buf(&save_buf); safe_describe_svalue(&obj_sval, 0, NULL); obj_name = complex_free_buf(&save_buf); PIKE_FN_START(function->name->size_shift == 0 ? function->name->str : "[widestring fn name]", obj_name.str); } if(UNLIKELY(Pike_interpreter.trace_level)) { dynamic_buffer save_buf; char buf[50]; init_buf(&save_buf);
bd67392015-10-14Martin Nilsson  sprintf(buf, "%lx->", (long) PTR_TO_INT (o));
6417722013-06-12Per Hedbor  my_strcat(buf); if (function->name->size_shift) my_strcat ("[widestring function name]"); else my_strcat(function->name->str); do_trace_call(args, &save_buf); } if( type == IDENTIFIER_C_FUNCTION ) { new_frame->num_args = args; new_frame->num_locals = args; new_frame->current_storage = o->storage+context->storage_offset; new_frame->pc = 0;
7724072013-11-03Tobias S. Josefowitz #ifndef PIKE_USE_MACHINE_CODE FAST_CHECK_THREADS_ON_CALL(); #endif
6417722013-06-12Per Hedbor  (*function->func.c_fun)(args); /* .. and below follows what is basically a copy of the * low_return function... */ if(save_sp+1 > Pike_sp) { push_int(0); } else if(save_sp+1 < Pike_sp) { stack_pop_n_elems_keep_top( Pike_sp-save_sp-1 ); } if(UNLIKELY(Pike_interpreter.trace_level>1)) do_trace_func_return (1, o, fun); goto pop; } new_frame->save_mark_sp=new_frame->mark_sp_base=Pike_mark_sp; new_frame->pc = new_frame->context->prog->program + function->func.offset #ifdef ENTRY_PROLOGUE_SIZE + ENTRY_PROLOGUE_SIZE #endif /* ENTRY_PROLOGUE_SIZE */ ;
5909f92013-08-15Per Hedbor  return new_frame->pc;
6417722013-06-12Per Hedbor  } else { struct object *tmp;
352ae82013-07-04Tobias S. Josefowitz  new_frame->pc = 0; new_frame->num_args = 0;
6417722013-06-12Per Hedbor  tmp=parent_clone_object(constant->u.program, o, fun, args); push_object(tmp); pop: POP_PIKE_FRAME(); return 0; } } } } return low_mega_apply( APPLY_LOW, args, o, (void*)fun ); }
28498e2002-11-09Henrik Grubbström (Grubba) /* Apply a function. * * Application types: * * APPLY_STACK: Apply Pike_sp[-args] with args-1 arguments. * * APPLY_SVALUE: Apply the svalue at arg1, and adjust the stack * to leave a return value. * * APPLY_SVALUE_STRICT: Apply the svalue at arg1, and don't adjust the * stack for functions that return void. * * APPLY_LOW: Apply function #arg2 in object arg1. * * Return values: * * Returns zero if the function was invalid or has been executed. * * Returns one if a frame has been set up to start the function * with eval_instruction(Pike_fp->pc - ENTRY_PROLOGUE_SIZE). After * eval_instruction() is done the frame needs to be removed by a call * to low_return() or low_return_pop(). */
5909f92013-08-15Per Hedbor void* low_mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2)
9ddbf22001-05-10Fredrik Hübinette (Hubbe) {
a93c4d2004-04-03Martin Stjernholm  struct object *o = NULL;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  struct pike_frame *scope=0;
766bc82004-10-16Marcus Agehall  ptrdiff_t fun=0;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  struct svalue *save_sp=Pike_sp-args; #if defined(PIKE_DEBUG) && defined(_REENTRANT) if(d_flag) { THREAD_T self = th_self(); CHECK_INTERPRETER_LOCK();
54e0d62007-07-01Henrik Grubbström (Grubba)  if (Pike_interpreter.thread_state) { if (!th_equal(Pike_interpreter.thread_state->id, self)) Pike_fatal("Current thread is wrong.\n");
442be32003-02-16Henrik Grubbström (Grubba) 
54e0d62007-07-01Henrik Grubbström (Grubba)  DEBUG_CHECK_THREAD(); }
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  } #endif switch(type) { case APPLY_STACK: if(!args) PIKE_ERROR("`()", "Too few arguments (apply stack).\n", Pike_sp, 0); args--; arg1=(void *)(Pike_sp-args-1);
39e8332015-04-19Henrik Grubbström (Grubba)  /* FALL_THROUGH */
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  case APPLY_SVALUE:
24c37f2001-05-24Fredrik Hübinette (Hubbe)  case APPLY_SVALUE_STRICT:
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  apply_svalue: { struct svalue *s=(struct svalue *)arg1;
017b572011-10-28Henrik Grubbström (Grubba)  switch(TYPEOF(*s))
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  { case T_INT: if (!s->u.integer) { PIKE_ERROR("0", "Attempt to call the NULL-value\n", Pike_sp, args); } else {
13670c2015-05-25Martin Nilsson  Pike_error("Attempt to call the value %"PRINTPIKEINT"d\n",
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  s->u.integer); }
c6b5bf2015-04-19Henrik Grubbström (Grubba)  break;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  case T_STRING: if (s->u.string->len > 20) {
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_error("Attempt to call the string \"%20S\"...\n", s->u.string);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  } else {
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_error("Attempt to call the string \"%S\"\n", s->u.string);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }
c6b5bf2015-04-19Henrik Grubbström (Grubba)  break;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  case T_MAPPING: Pike_error("Attempt to call a mapping\n");
c6b5bf2015-04-19Henrik Grubbström (Grubba)  break;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  default: Pike_error("Call to non-function value type:%s.\n",
017b572011-10-28Henrik Grubbström (Grubba)  get_name_of_type(TYPEOF(*s)));
c6b5bf2015-04-19Henrik Grubbström (Grubba)  break;
cc08512013-06-12Per Hedbor 
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  case T_FUNCTION:
017b572011-10-28Henrik Grubbström (Grubba)  if(SUBTYPEOF(*s) == FUNCTION_BUILTIN)
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  { #ifdef PIKE_DEBUG struct svalue *expected_stack = Pike_sp-args;
bd8fb82004-04-03Martin Stjernholm #endif
97ebb32003-01-09Henrik Grubbström (Grubba)  if(Pike_interpreter.trace_level>1)
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  {
9fa0ee2003-11-09Martin Stjernholm  dynamic_buffer save_buf; init_buf(&save_buf);
bd8fb82004-04-03Martin Stjernholm  if (s->u.efun->name->size_shift) my_strcat ("[widestring function name]"); else my_strcat (s->u.efun->name->str);
9fa0ee2003-11-09Martin Stjernholm  do_trace_call(args, &save_buf);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }
cf84342011-05-19Jonas Walldén  if (PIKE_FN_START_ENABLED()) { /* DTrace enter probe arg0: function name arg1: object */ PIKE_FN_START(s->u.efun->name->size_shift == 0 ? s->u.efun->name->str : "[widestring fn name]", ""); }
51d7d82008-11-18Martin Stjernholm  FAST_CHECK_THREADS_ON_CALL();
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  (*(s->u.efun->function))(args);
cf84342011-05-19Jonas Walldén  if (PIKE_FN_DONE_ENABLED()) { /* DTrace leave probe arg0: function name */ PIKE_FN_DONE(s->u.efun->name->size_shift == 0 ? s->u.efun->name->str : "[widestring fn name]"); }
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  #ifdef PIKE_DEBUG
d9a93b2001-07-01Fredrik Hübinette (Hubbe)  s->u.efun->runs++;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  if(Pike_sp != expected_stack + !s->u.efun->may_return_void) { if(Pike_sp < expected_stack)
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_fatal("Function popped too many arguments: %S\n", s->u.efun->name);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  if(Pike_sp>expected_stack+1)
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_fatal("Function left droppings on stack: %S\n", s->u.efun->name);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  if(Pike_sp == expected_stack && !s->u.efun->may_return_void)
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_fatal("Non-void function returned without return value on stack: %S %d\n", s->u.efun->name, s->u.efun->may_return_void);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  if(Pike_sp==expected_stack+1 && s->u.efun->may_return_void)
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_fatal("Void function returned with a value on the stack: %S %d\n", s->u.efun->name, s->u.efun->may_return_void);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  } #endif break; }else{
70be662001-06-19Fredrik Hübinette (Hubbe)  type=APPLY_SVALUE;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  o=s->u.object;
6417722013-06-12Per Hedbor  fun = SUBTYPEOF(*s);
87338f2003-01-15Martin Stjernholm  if(o->prog == pike_trampoline_program &&
6417722013-06-12Per Hedbor  fun == QUICK_FIND_LFUN(pike_trampoline_program, LFUN_CALL))
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  { fun=((struct pike_trampoline *)(o->storage))->func; scope=((struct pike_trampoline *)(o->storage))->frame; o=scope->current_object; }
6417722013-06-12Per Hedbor  goto apply_low;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  } break; case T_ARRAY:
bd8fb82004-04-03Martin Stjernholm  if(Pike_interpreter.trace_level)
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  {
9fa0ee2003-11-09Martin Stjernholm  dynamic_buffer save_buf; init_buf(&save_buf);
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue(s,0,0);
9fa0ee2003-11-09Martin Stjernholm  do_trace_call(args, &save_buf);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }
cf84342011-05-19Jonas Walldén  if (PIKE_FN_START_ENABLED()) { /* DTrace enter probe arg0: function name arg1: object */ PIKE_FN_START("[array]", ""); }
3e7f002008-07-13Henrik Grubbström (Grubba)  apply_array(s->u.array, args, (type == APPLY_STACK));
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  break;
34b26e2008-05-10Henrik Grubbström (Grubba)  case PIKE_T_TYPE: if (args != 1) { /* FIXME: Casts to object ought to propagate to apply program below. */ SIMPLE_WRONG_NUM_ARGS_ERROR("cast", 1); } o_cast(s->u.type, compile_type_to_runtime_type(s->u.type)); break;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  case T_PROGRAM:
bd8fb82004-04-03Martin Stjernholm  if(Pike_interpreter.trace_level)
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  {
9fa0ee2003-11-09Martin Stjernholm  dynamic_buffer save_buf; init_buf(&save_buf);
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue(s,0,0);
9fa0ee2003-11-09Martin Stjernholm  do_trace_call(args, &save_buf);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }
cf84342011-05-19Jonas Walldén  if (PIKE_FN_START_ENABLED()) { /* DTrace enter probe arg0: function name arg1: object */ dynamic_buffer save_buf; dynbuf_string prog_name; init_buf(&save_buf); safe_describe_svalue(s,0,0); prog_name = complex_free_buf(&save_buf); PIKE_FN_START("[program]", prog_name.str); }
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  push_object(clone_object(s->u.program,args)); break; case T_OBJECT:
f54c782004-12-22Henrik Grubbström (Grubba)  /* FIXME: Object subtypes! */
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  o=s->u.object; if(o->prog == pike_trampoline_program) { fun=((struct pike_trampoline *)(o->storage))->func; scope=((struct pike_trampoline *)(o->storage))->frame; o=scope->current_object;
6417722013-06-12Per Hedbor  goto apply_low;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  } fun=LFUN_CALL;
70be662001-06-19Fredrik Hübinette (Hubbe)  type=APPLY_SVALUE;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  goto call_lfun; } break; }
5cc20b2003-07-30Martin Stjernholm  call_lfun: { int lfun;
9ddbf22001-05-10Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG if(fun < 0 || fun >= NUM_LFUNS)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Apply lfun on illegal value!\n");
9ddbf22001-05-10Fredrik Hübinette (Hubbe) #endif if(!o->prog) PIKE_ERROR("destructed object", "Apply on destructed object.\n", Pike_sp, args);
5cc20b2003-07-30Martin Stjernholm  lfun = FIND_LFUN(o->prog, fun); if (lfun < 0) Pike_error ("Cannot call undefined lfun %s.\n", lfun_names[fun]); fun = lfun;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  goto apply_low;
5cc20b2003-07-30Martin Stjernholm  }
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  case APPLY_LOW: o = (struct object *)arg1;
d2361e2003-06-30Martin Stjernholm  fun = PTR_TO_INT(arg2);
87338f2003-01-15Martin Stjernholm  if(o->prog == pike_trampoline_program && fun == QUICK_FIND_LFUN(pike_trampoline_program, LFUN_CALL)) { fun=((struct pike_trampoline *)(o->storage))->func; scope=((struct pike_trampoline *)(o->storage))->frame; o=scope->current_object; }
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  apply_low:
4b05702001-08-30Fredrik Hübinette (Hubbe) #include "apply_low.h" break;
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  } if(save_sp+1 > Pike_sp) {
bd8fb82004-04-03Martin Stjernholm  if(type != APPLY_SVALUE_STRICT) {
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  push_int(0);
bd8fb82004-04-03Martin Stjernholm  if(Pike_interpreter.trace_level>1) do_trace_func_return (1, o, fun); } else if(Pike_interpreter.trace_level>1) do_trace_func_return (0, o, fun);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }else{
4b05702001-08-30Fredrik Hübinette (Hubbe)  if(save_sp+1 < Pike_sp) { assign_svalue(save_sp,Pike_sp-1); pop_n_elems(Pike_sp-save_sp-1); low_destruct_objects_to_destruct(); /* consider using a flag for immediate destruct instead... */ }
bd8fb82004-04-03Martin Stjernholm  if(Pike_interpreter.trace_level>1) do_trace_func_return (1, o, fun);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }
cf84342011-05-19Jonas Walldén  if (PIKE_FN_DONE_ENABLED()) { /* DTrace leave probe arg0: function name */ char *fn = "(unknown)"; if (o && o->prog) { struct identifier *id = ID_FROM_INT(o->prog, fun); fn = id->name->size_shift == 0 ? id->name->str : "[widestring fn name]"; } PIKE_FN_DONE(fn); }
f663c92003-04-03Henrik Grubbström (Grubba)  return 0;
9ddbf22001-05-10Fredrik Hübinette (Hubbe) }
2ac9502001-08-14Fredrik Hübinette (Hubbe) 
56854a2003-08-08Martin Stjernholm #define basic_low_return(save_sp) \ DO_IF_DEBUG( \ if(Pike_mark_sp < Pike_fp->save_mark_sp) \ Pike_fatal("Popped below save_mark_sp!\n"); \ if(Pike_sp<Pike_interpreter.evaluator_stack) \ Pike_fatal("Stack error (also simple).\n"); \
2ac9502001-08-14Fredrik Hübinette (Hubbe)  ) \ \ Pike_mark_sp=Pike_fp->save_mark_sp; \ \
56854a2003-08-08Martin Stjernholm  POP_PIKE_FRAME()
2ac9502001-08-14Fredrik Hübinette (Hubbe)  void low_return(void) {
e5b5d72013-06-12Per Hedbor  struct svalue *save_sp = Pike_fp->save_sp+1; struct object *o = Pike_fp->current_object; int fun = Pike_fp->fun;
bd8fb82004-04-03Martin Stjernholm 
cf84342011-05-19Jonas Walldén  if (PIKE_FN_DONE_ENABLED()) { /* DTrace leave probe arg0: function name */ char *fn = "(unknown)"; if (o && o->prog) { struct identifier *id = ID_FROM_INT(o->prog, fun); fn = id->name->size_shift == 0 ? id->name->str : "[widestring fn name]"; } PIKE_FN_DONE(fn); }
f2f2242003-08-08Martin Stjernholm #if defined (PIKE_USE_MACHINE_CODE) && defined (OPCODE_RETURN_JUMPADDR) /* If the function that returns is the only ref to the current * object and its program then the program would be freed in * destruct_objects_to_destruct below. However, we're still * executing in an opcode in its code so we need prog->program to * stick around for a little while more to handle the returned * address. We therefore add a ref to the current object so that * it'll live through this function. */ add_ref (o); #endif
56854a2003-08-08Martin Stjernholm  basic_low_return (save_sp);
f46d582013-06-13Per Hedbor  stack_pop_n_elems_keep_top (Pike_sp - save_sp);
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  {
f46d582013-06-13Per Hedbor  /* consider using a flag for immediate destruct instead... */
e5b5d72013-06-12Per Hedbor  extern struct object *objects_to_destruct; if( objects_to_destruct ) destruct_objects_to_destruct();
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  }
f2f2242003-08-08Martin Stjernholm 
e5b5d72013-06-12Per Hedbor #ifdef PIKE_DEBUG if(save_sp > Pike_sp) Pike_fatal("Pike function did not leave an return value\n"); #endif if(UNLIKELY(Pike_interpreter.trace_level>1))
bd8fb82004-04-03Martin Stjernholm  do_trace_func_return (1, o, fun);
f2f2242003-08-08Martin Stjernholm #if defined (PIKE_USE_MACHINE_CODE) && defined (OPCODE_RETURN_JUMPADDR) free_object (o); #endif
9ddbf22001-05-10Fredrik Hübinette (Hubbe) }
2ac9502001-08-14Fredrik Hübinette (Hubbe) void low_return_pop(void) {
56854a2003-08-08Martin Stjernholm  struct svalue *save_sp = Pike_fp->save_sp;
f2f2242003-08-08Martin Stjernholm #if defined (PIKE_USE_MACHINE_CODE) && defined (OPCODE_RETURN_JUMPADDR) /* See note above. */ struct object *o = Pike_fp->current_object; add_ref (o); #endif
cf84342011-05-19Jonas Walldén  if (PIKE_FN_DONE_ENABLED()) { /* DTrace leave probe arg0: function name */ char *fn = "(unknown)"; struct object *o = Pike_fp->current_object; int fun = Pike_fp->fun; if (o && o->prog) { struct identifier *id = ID_FROM_INT(o->prog, fun); fn = id->name->size_shift == 0 ? id->name->str : "[widestring fn name]"; } PIKE_FN_DONE(fn); }
56854a2003-08-08Martin Stjernholm  basic_low_return (save_sp);
2ac9502001-08-14Fredrik Hübinette (Hubbe) 
f46d582013-06-13Per Hedbor  pop_n_elems(Pike_sp-save_sp); /* consider using a flag for immediate destruct instead... */ destruct_objects_to_destruct();
f2f2242003-08-08Martin Stjernholm  #if defined (PIKE_USE_MACHINE_CODE) && defined (OPCODE_RETURN_JUMPADDR)
b579632003-08-08Martin Stjernholm  free_object (o);
f2f2242003-08-08Martin Stjernholm #endif
2ac9502001-08-14Fredrik Hübinette (Hubbe) }
9ddbf22001-05-10Fredrik Hübinette (Hubbe) void unlink_previous_frame(void) { struct pike_frame *current, *prev;
3958992001-06-23Fredrik Hübinette (Hubbe) 
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  current=Pike_interpreter.frame_pointer;
3958992001-06-23Fredrik Hübinette (Hubbe)  prev=current->next; #ifdef PIKE_DEBUG { JMP_BUF *rec;
b4ac672003-12-05Henrik Grubbström (Grubba)  /* Check if any recoveries belong to the frame we're * about to unlink. */
3958992001-06-23Fredrik Hübinette (Hubbe)  if((rec=Pike_interpreter.recoveries)) { while(rec->frame_pointer == current) rec=rec->previous;
b4ac672003-12-05Henrik Grubbström (Grubba)  /* FIXME: Wouldn't a simple return be ok? */
3958992001-06-23Fredrik Hübinette (Hubbe)  if(rec->frame_pointer == current->next)
5aad932002-08-15Marcus Comstedt  Pike_fatal("You can't touch this!\n");
3958992001-06-23Fredrik Hübinette (Hubbe)  } } #endif
b4ac672003-12-05Henrik Grubbström (Grubba)  /* Save various fields from the previous frame. */ current->save_sp=prev->save_sp; current->save_mark_sp=prev->save_mark_sp;
e5b5d72013-06-12Per Hedbor  current->flags = prev->flags;
3958992001-06-23Fredrik Hübinette (Hubbe) 
b4ac672003-12-05Henrik Grubbström (Grubba)  /* Unlink the top frame temporarily. */
3958992001-06-23Fredrik Hübinette (Hubbe)  Pike_interpreter.frame_pointer=prev;
9ddbf22001-05-10Fredrik Hübinette (Hubbe) 
b2630d2004-05-20Henrik Grubbström (Grubba) #ifdef PROFILING { /* We must update the profiling info of the previous frame * to account for that the current frame has gone away. */ cpu_time_t total_time = get_cpu_time() - (Pike_interpreter.unlocked_time + current->start_time); cpu_time_t child_time = Pike_interpreter.accounted_time - current->children_base; struct identifier *function =
fa93a52008-02-28Henrik Grubbström (Grubba)  current->context->prog->identifiers + current->ident;
5ca7b52012-01-07Jonas Walldén  if (!function->recur_depth) function->total_time += total_time;
b2630d2004-05-20Henrik Grubbström (Grubba)  total_time -= child_time; function->self_time += total_time; Pike_interpreter.accounted_time += total_time; #ifdef PROFILING_DEBUG fprintf(stderr, "%p: Unlinking previous frame.\n" "Previous: %" PRINT_CPU_TIME " %" PRINT_CPU_TIME "\n" "Current: %" PRINT_CPU_TIME " %" PRINT_CPU_TIME "\n", Pike_interpreter.thread_state, prev->start_time, prev->children_base, current->start_time, current->children_base); #endif /* PROFILING_DEBUG */ } #endif /* PROFILING */
b4ac672003-12-05Henrik Grubbström (Grubba)  /* Unlink the frame. */
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  POP_PIKE_FRAME();
b4ac672003-12-05Henrik Grubbström (Grubba)  /* Hook our frame again. */ current->next=Pike_interpreter.frame_pointer; Pike_interpreter.frame_pointer=current;
9ddbf22001-05-10Fredrik Hübinette (Hubbe) 
b2630d2004-05-20Henrik Grubbström (Grubba) #ifdef PROFILING current->children_base = Pike_interpreter.accounted_time; current->start_time = get_cpu_time() - Pike_interpreter.unlocked_time; #endif /* PROFILING */
9ddbf22001-05-10Fredrik Hübinette (Hubbe) }
ca176b2006-02-27Martin Stjernholm static void restore_catching_eval_jmpbuf (LOW_JMP_BUF *p) { Pike_interpreter.catching_eval_jmpbuf = p; }
4218011999-01-31Fredrik Hübinette (Hubbe) 
e68c782006-07-05Martin Stjernholm PMOD_EXPORT void mega_apply(enum apply_type type, INT32 args, void *arg1, void *arg2)
9ddbf22001-05-10Fredrik Hübinette (Hubbe) {
a9f8ba2008-04-12Henrik Grubbström (Grubba)  /* Save and clear Pike_interpreter.catching_eval_jmpbuf so that the * following eval_instruction will install a LOW_JMP_BUF of its * own to handle catches. */ LOW_JMP_BUF *saved_jmpbuf = Pike_interpreter.catching_eval_jmpbuf; ONERROR uwp; Pike_interpreter.catching_eval_jmpbuf = NULL; SET_ONERROR (uwp, restore_catching_eval_jmpbuf, saved_jmpbuf);
30def62004-06-01Martin Stjernholm  /* The C stack margin is normally 8 kb, but if we get here during a * lowered margin then don't fail just because of that, unless it's * practically zero. */ check_c_stack(Pike_interpreter.c_stack_margin ? Pike_interpreter.c_stack_margin : 100);
6417722013-06-12Per Hedbor  if( low_mega_apply(type, args, arg1, arg2) ) { eval_instruction(Pike_fp->pc #ifdef ENTRY_PROLOGUE_SIZE - ENTRY_PROLOGUE_SIZE #endif /* ENTRY_PROLOGUE_SIZE */ ); low_return(); } CALL_AND_UNSET_ONERROR(uwp); } PMOD_EXPORT void mega_apply_low(INT32 args, void *arg1, ptrdiff_t arg2) { /* Save and clear Pike_interpreter.catching_eval_jmpbuf so that the * following eval_instruction will install a LOW_JMP_BUF of its * own to handle catches. */ LOW_JMP_BUF *saved_jmpbuf = Pike_interpreter.catching_eval_jmpbuf; ONERROR uwp; Pike_interpreter.catching_eval_jmpbuf = NULL; SET_ONERROR (uwp, restore_catching_eval_jmpbuf, saved_jmpbuf);
30def62004-06-01Martin Stjernholm 
6417722013-06-12Per Hedbor  /* The C stack margin is normally 8 kb, but if we get here during a * lowered margin then don't fail just because of that, unless it's * practically zero. */ check_c_stack(Pike_interpreter.c_stack_margin ? Pike_interpreter.c_stack_margin : 100); if( lower_mega_apply( args, arg1, arg2 ) )
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  {
1c9ebc2001-07-24Henrik Grubbström (Grubba)  eval_instruction(Pike_fp->pc #ifdef ENTRY_PROLOGUE_SIZE - ENTRY_PROLOGUE_SIZE #endif /* ENTRY_PROLOGUE_SIZE */ );
9ddbf22001-05-10Fredrik Hübinette (Hubbe)  low_return(); }
a9f8ba2008-04-12Henrik Grubbström (Grubba)  CALL_AND_UNSET_ONERROR(uwp);
9ddbf22001-05-10Fredrik Hübinette (Hubbe) }
4218011999-01-31Fredrik Hübinette (Hubbe) 
ca176b2006-02-27Martin Stjernholm /* Put catch outside of eval_instruction, so the setjmp won't affect * the optimization of eval_instruction.
b208c11996-08-03Fredrik Hübinette (Hubbe)  */
ca176b2006-02-27Martin Stjernholm static int catching_eval_instruction (PIKE_OPCODE_T *pc)
b208c11996-08-03Fredrik Hübinette (Hubbe) {
ca176b2006-02-27Martin Stjernholm  LOW_JMP_BUF jmpbuf;
cfa1db2000-05-01Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
ca176b2006-02-27Martin Stjernholm  if (Pike_interpreter.catching_eval_jmpbuf) Pike_fatal ("catching_eval_jmpbuf already active.\n");
cfa1db2000-05-01Fredrik Hübinette (Hubbe) #endif
ca176b2006-02-27Martin Stjernholm  Pike_interpreter.catching_eval_jmpbuf = &jmpbuf; if (LOW_SETJMP (jmpbuf)) { Pike_interpreter.catching_eval_jmpbuf = NULL;
46072b2006-03-15Henrik Grubbström (Grubba) #ifdef PIKE_DEBUG pike_trace(3, "- catching_eval_instruction(%p) caught error ==> -3\n", pc); #endif
ca176b2006-02-27Martin Stjernholm  return -3; }else{
e3572e2009-04-01Martin Stjernholm  int x; check_c_stack(8192); x = eval_instruction(pc);
ca176b2006-02-27Martin Stjernholm  Pike_interpreter.catching_eval_jmpbuf = NULL;
46072b2006-03-15Henrik Grubbström (Grubba) #ifdef PIKE_DEBUG pike_trace(3, "- catching_eval_instruction(%p) ==> %d\n", pc, x); #endif
ca176b2006-02-27Martin Stjernholm  return x;
b208c11996-08-03Fredrik Hübinette (Hubbe)  } }
1939cb2003-11-22Henrik Grubbström (Grubba) /*! @decl mixed `()(function fun, mixed ... args) *! @decl mixed call_function(function fun, mixed ... args)
7c0df72001-02-06Henrik Grubbström (Grubba)  *! *! Call a function.
1939cb2003-11-22Henrik Grubbström (Grubba)  *! *! Calls the function @[fun] with the arguments specified by @[args]. *! *! @seealso *! @[lfun::`()()]
7c0df72001-02-06Henrik Grubbström (Grubba)  */
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void f_call_function(INT32 args)
3c0c281998-01-26Fredrik Hübinette (Hubbe) { mega_apply(APPLY_STACK,args,0,0); }
ea3b2d2002-12-07Henrik Grubbström (Grubba) /*! @class MasterObject */ /*! @decl void handle_error(mixed exception) *! *! Called by the Pike runtime if an exception isn't caught. *!
b5cd8a2002-12-07Henrik Grubbström (Grubba)  *! @param exception
ea3b2d2002-12-07Henrik Grubbström (Grubba)  *! Value that was @[throw()]'n. *! *! @seealso *! @[describe_backtrace()] */ /*! @endclass */
6697042000-11-20Martin Stjernholm PMOD_EXPORT void call_handle_error(void) {
eaa4da2001-10-04Fredrik Hübinette (Hubbe)  dmalloc_touch_svalue(&throw_value);
c9e3382003-07-16Martin Stjernholm 
30def62004-06-01Martin Stjernholm  if (Pike_interpreter.svalue_stack_margin > LOW_SVALUE_STACK_MARGIN) {
97ebb32003-01-09Henrik Grubbström (Grubba)  int old_t_flag = Pike_interpreter.trace_level; Pike_interpreter.trace_level = 0;
30def62004-06-01Martin Stjernholm  Pike_interpreter.svalue_stack_margin = LOW_SVALUE_STACK_MARGIN; Pike_interpreter.c_stack_margin = LOW_C_STACK_MARGIN;
1ab4ac2008-01-26Martin Stjernholm  move_svalue (Pike_sp++, &throw_value); mark_free_svalue (&throw_value);
c9e3382003-07-16Martin Stjernholm  if (get_master()) { /* May return NULL at odd times. */ ONERROR tmp; SET_ONERROR(tmp,exit_on_error,"Error in handle_error in master object!"); APPLY_MASTER("handle_error", 1); UNSET_ONERROR(tmp); } else {
9fa0ee2003-11-09Martin Stjernholm  dynamic_buffer save_buf;
c9e3382003-07-16Martin Stjernholm  char *s;
10dfe52003-07-18Henrik Grubbström (Grubba)  fprintf (stderr, "There's no master to handle the error. Dumping it raw:\n");
9fa0ee2003-11-09Martin Stjernholm  init_buf(&save_buf);
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue (Pike_sp - 1, 0, 0);
9fa0ee2003-11-09Martin Stjernholm  s=simple_free_buf(&save_buf);
c9e3382003-07-16Martin Stjernholm  fprintf(stderr,"%s\n",s); free(s);
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(Pike_sp[-1]) == PIKE_T_OBJECT && Pike_sp[-1].u.object->prog) {
e22bbe2005-11-08Henrik Grubbström (Grubba)  int fun = find_identifier("backtrace", Pike_sp[-1].u.object->prog); if (fun != -1) { fprintf(stderr, "Attempting to extract the backtrace.\n"); safe_apply_low2(Pike_sp[-1].u.object, fun, 0, 0); init_buf(&save_buf);
6a45bb2006-02-28Martin Stjernholm  safe_describe_svalue(Pike_sp - 1, 0, 0);
e22bbe2005-11-08Henrik Grubbström (Grubba)  pop_stack(); s=simple_free_buf(&save_buf); fprintf(stderr,"%s\n",s); free(s); } }
c9e3382003-07-16Martin Stjernholm  }
6697042000-11-20Martin Stjernholm  pop_stack(); Pike_interpreter.svalue_stack_margin = SVALUE_STACK_MARGIN; Pike_interpreter.c_stack_margin = C_STACK_MARGIN;
97ebb32003-01-09Henrik Grubbström (Grubba)  Pike_interpreter.trace_level = old_t_flag;
6697042000-11-20Martin Stjernholm  }
c9e3382003-07-16Martin Stjernholm 
a2988f2001-06-08Martin Stjernholm  else { free_svalue(&throw_value);
1ab4ac2008-01-26Martin Stjernholm  mark_free_svalue (&throw_value);
a2988f2001-06-08Martin Stjernholm  }
6697042000-11-20Martin Stjernholm }
93e87b2003-06-07Henrik Grubbström (Grubba) /* NOTE: This function may only be called from the compiler! */
70fbc12011-05-02Per Hedbor int apply_low_safe_and_stupid(struct object *o, INT32 offset)
5267b71995-08-09Fredrik Hübinette (Hubbe) { 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;
6417722013-06-12Per Hedbor  volatile int use_dummy_reference = 1;
e8c1552010-01-21Henrik Grubbström (Grubba)  struct program *prog = o->prog; int p_flags = prog->flags;
ca176b2006-02-27Martin Stjernholm  LOW_JMP_BUF *saved_jmpbuf;
e8c1552010-01-21Henrik Grubbström (Grubba)  int fun = -1;
93e87b2003-06-07Henrik Grubbström (Grubba) 
e8c1552010-01-21Henrik Grubbström (Grubba)  /* Search for a function that belongs to the current program, * since this is needed for opcodes that use INHERIT_FROM_*
93e87b2003-06-07Henrik Grubbström (Grubba)  * (eg F_EXTERN) to work. */
e8c1552010-01-21Henrik Grubbström (Grubba)  for (fun = prog->num_identifier_references; fun--;) { if (!prog->identifier_references[fun].inherit_offset) { use_dummy_reference = 0; break; } }
93e87b2003-06-07Henrik Grubbström (Grubba)  if (use_dummy_reference) {
e8c1552010-01-21Henrik Grubbström (Grubba)  /* No suitable function was found, so add one. */
81455d2008-03-09Henrik Grubbström (Grubba)  struct identifier dummy;
93e87b2003-06-07Henrik Grubbström (Grubba)  struct reference dummy_ref = { 0, 0, ID_HIDDEN,
77fbd72010-11-25Henrik Grubbström (Grubba)  PIKE_T_UNKNOWN, { 0, },
93e87b2003-06-07Henrik Grubbström (Grubba)  };
e8c1552010-01-21Henrik Grubbström (Grubba)  /* FIXME: Assert that o->prog == Pike_compiler->new_program */
8459b42008-03-09Henrik Grubbström (Grubba)  copy_shared_string(dummy.name, empty_pike_string);
81455d2008-03-09Henrik Grubbström (Grubba)  copy_pike_type(dummy.type, function_type_string);
8c42122008-05-31Henrik Grubbström (Grubba)  dummy.filename_strno = -1; dummy.linenumber = 0;
61c6e02008-03-14Henrik Grubbström (Grubba)  dummy.run_time_type = PIKE_T_FUNCTION;
81455d2008-03-09Henrik Grubbström (Grubba)  dummy.identifier_flags = IDENTIFIER_PIKE_FUNCTION|IDENTIFIER_HAS_BODY; dummy.func.offset = offset; dummy.opt_flags = 0;
e8c1552010-01-21Henrik Grubbström (Grubba)  dummy_ref.identifier_offset = prog->num_identifiers;
6a20ef2008-03-09Henrik Grubbström (Grubba)  add_to_identifiers(dummy);
e8c1552010-01-21Henrik Grubbström (Grubba)  fun = prog->num_identifier_references;
93e87b2003-06-07Henrik Grubbström (Grubba)  add_to_identifier_references(dummy_ref); }
5267b71995-08-09Fredrik Hübinette (Hubbe) 
3601d32002-11-23Martin Stjernholm  /* FIXME: Is this up-to-date with mega_apply? */
f077582000-07-06Fredrik Hübinette (Hubbe)  new_frame->next = Pike_fp;
fa93a52008-02-28Henrik Grubbström (Grubba)  add_ref(new_frame->current_object = o);
e8c1552010-01-21Henrik Grubbström (Grubba)  add_ref(new_frame->current_program = prog); new_frame->context = prog->inherits;
a068db2003-10-01Henrik Grubbström (Grubba)  new_frame->locals = Pike_sp;
4218011999-01-31Fredrik Hübinette (Hubbe)  new_frame->expendible=new_frame->locals; new_frame->args = 0; new_frame->num_args=0; new_frame->num_locals=0;
e8c1552010-01-21Henrik Grubbström (Grubba)  new_frame->fun = fun;
4218011999-01-31Fredrik Hübinette (Hubbe)  new_frame->pc = 0; new_frame->current_storage=o->storage;
a068db2003-10-01Henrik Grubbström (Grubba) 
13670c2015-05-25Martin Nilsson #ifdef PIKE_DEBUG
a068db2003-10-01Henrik Grubbström (Grubba)  if (Pike_fp && (new_frame->locals < Pike_fp->locals)) { fatal("New locals below old locals: %p < %p\n", new_frame->locals, Pike_fp->locals); } #endif /* PIKE_DEBUG */
f077582000-07-06Fredrik Hübinette (Hubbe)  Pike_fp = new_frame;
4218011999-01-31Fredrik Hübinette (Hubbe) 
ca176b2006-02-27Martin Stjernholm  saved_jmpbuf = Pike_interpreter.catching_eval_jmpbuf; Pike_interpreter.catching_eval_jmpbuf = NULL;
5267b71995-08-09Fredrik Hübinette (Hubbe)  if(SETJMP(tmp)) { ret=1; }else{
105ade2001-05-13Fredrik Hübinette (Hubbe)  int tmp; new_frame->mark_sp_base=new_frame->save_mark_sp=Pike_mark_sp;
e8c1552010-01-21Henrik Grubbström (Grubba)  tmp=eval_instruction(prog->program + offset);
105ade2001-05-13Fredrik Hübinette (Hubbe)  Pike_mark_sp=new_frame->save_mark_sp;
13670c2015-05-25Martin Nilsson 
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
ca176b2006-02-27Martin Stjernholm  if (tmp != -1) Pike_fatal ("Unexpected return value from eval_instruction: %d\n", tmp);
f077582000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp<Pike_interpreter.evaluator_stack)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Stack error (simple).\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) #endif ret=0; } UNSETJMP(tmp);
ca176b2006-02-27Martin Stjernholm  Pike_interpreter.catching_eval_jmpbuf = saved_jmpbuf;
93e87b2003-06-07Henrik Grubbström (Grubba)  if (use_dummy_reference) {
81455d2008-03-09Henrik Grubbström (Grubba)  /* Pop the dummy identifier. */
6a20ef2008-03-09Henrik Grubbström (Grubba)  free_type(function_type_string); free_string(empty_pike_string);
e8c1552010-01-21Henrik Grubbström (Grubba)  prog->num_identifier_references--; prog->num_identifiers--;
93e87b2003-06-07Henrik Grubbström (Grubba)  }
35efd52007-06-17Martin Stjernholm  assert (new_frame == Pike_fp); LOW_POP_PIKE_FRAME (new_frame);
5267b71995-08-09Fredrik Hübinette (Hubbe)  return ret; }
70fbc12011-05-02Per Hedbor int safe_apply_low2(struct object *o, int fun, int args,
ef8d2a2008-04-14Henrik Grubbström (Grubba)  const char *fun_name)
5267b71995-08-09Fredrik Hübinette (Hubbe) { JMP_BUF recovery;
ef8d2a2008-04-14Henrik Grubbström (Grubba)  int ret = 0;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
dffa011997-01-15Fredrik Hübinette (Hubbe)  free_svalue(& throw_value);
1ab4ac2008-01-26Martin Stjernholm  mark_free_svalue (&throw_value);
3601d32002-11-23Martin Stjernholm  if(SETJMP_SP(recovery, args))
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
c4accf2006-03-17Henrik Grubbström (Grubba)  if(fun_name) call_handle_error();
3601d32002-11-23Martin Stjernholm  push_int(0);
ef8d2a2008-04-14Henrik Grubbström (Grubba)  ret = 0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }else{
6deaa32006-03-17Henrik Grubbström (Grubba)  if (fun >= 0) { apply_low(o,fun,args);
142e7a2006-03-17Henrik Grubbström (Grubba)  } else if (fun_name) { Pike_error("Cannot call unknown function \"%s\".\n", fun_name);
6deaa32006-03-17Henrik Grubbström (Grubba)  } else { pop_n_elems(args); push_int(0); }
ef8d2a2008-04-14Henrik Grubbström (Grubba)  ret = 1;
5267b71995-08-09Fredrik Hübinette (Hubbe)  } UNSETJMP(recovery);
ef8d2a2008-04-14Henrik Grubbström (Grubba)  return ret;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
ef8d2a2008-04-14Henrik Grubbström (Grubba) PMOD_EXPORT int safe_apply_low(struct object *o, int fun, int args)
c1b8f02001-07-02Martin Stjernholm {
ef8d2a2008-04-14Henrik Grubbström (Grubba)  return safe_apply_low2(o, fun, args, "Unknown function.");
c1b8f02001-07-02Martin Stjernholm }
b1f4eb1998-01-13Fredrik Hübinette (Hubbe) 
ef8d2a2008-04-14Henrik Grubbström (Grubba) PMOD_EXPORT int safe_apply(struct object *o, const char *fun, INT32 args)
5683de1995-11-06Fredrik Hübinette (Hubbe) {
00fe4d2003-08-04Martin Stjernholm  int id;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
5aad932002-08-15Marcus Comstedt  if(!o->prog) Pike_fatal("Apply safe on destructed object.\n");
5683de1995-11-06Fredrik Hübinette (Hubbe) #endif
00fe4d2003-08-04Martin Stjernholm  id = find_identifier(fun, o->prog);
ef8d2a2008-04-14Henrik Grubbström (Grubba)  return safe_apply_low2(o, id, args, fun);
5683de1995-11-06Fredrik Hübinette (Hubbe) }
61863e2001-12-20Martin Stjernholm /* Returns nonzero if the function was called in some handler. */
70fbc12011-05-02Per Hedbor int low_unsafe_apply_handler(const char *fun,
61863e2001-12-20Martin Stjernholm  struct object *handler, struct object *compat, INT32 args) { int i; #if 0 fprintf(stderr, "low_unsafe_apply_handler(\"%s\", 0x%08p, 0x%08p, %d)\n", fun, handler, compat, args); #endif /* 0 */ if (handler && handler->prog && (i = find_identifier(fun, handler->prog)) != -1) { apply_low(handler, i, args); } else if (compat && compat->prog && (i = find_identifier(fun, compat->prog)) != -1) { apply_low(compat, i, args); } else {
9abc252002-09-21Martin Stjernholm  struct object *master_obj = get_master(); if (master_obj && (i = find_identifier(fun, master_obj->prog)) != -1)
61863e2001-12-20Martin Stjernholm  apply_low(master_obj, i, args); else { pop_n_elems(args); push_undefined(); return 0; } } return 1; }
70fbc12011-05-02Per Hedbor void low_safe_apply_handler(const char *fun,
9036e82001-08-16Martin Stjernholm  struct object *handler, struct object *compat, INT32 args)
c3805a2001-06-08Henrik Grubbström (Grubba) { int i;
68296f2001-07-07Henrik Grubbström (Grubba) #if 0
9036e82001-08-16Martin Stjernholm  fprintf(stderr, "low_safe_apply_handler(\"%s\", 0x%08p, 0x%08p, %d)\n",
68296f2001-07-07Henrik Grubbström (Grubba)  fun, handler, compat, args); #endif /* 0 */
6991a22001-06-11Henrik Grubbström (Grubba)  if (handler && handler->prog &&
c3805a2001-06-08Henrik Grubbström (Grubba)  (i = find_identifier(fun, handler->prog)) != -1) {
6deaa32006-03-17Henrik Grubbström (Grubba)  safe_apply_low2(handler, i, args, fun);
6991a22001-06-11Henrik Grubbström (Grubba)  } else if (compat && compat->prog &&
2174722001-06-08Henrik Grubbström (Grubba)  (i = find_identifier(fun, compat->prog)) != -1) {
6deaa32006-03-17Henrik Grubbström (Grubba)  safe_apply_low2(compat, i, args, fun);
c3805a2001-06-08Henrik Grubbström (Grubba)  } else {
2174722001-06-08Henrik Grubbström (Grubba)  struct object *master_obj = master();
dd79492001-08-15Martin Stjernholm  if ((i = find_identifier(fun, master_obj->prog)) != -1)
6deaa32006-03-17Henrik Grubbström (Grubba)  safe_apply_low2(master_obj, i, args, fun);
dd79492001-08-15Martin Stjernholm  else { pop_n_elems(args); push_undefined(); }
c1b8f02001-07-02Martin Stjernholm  }
9036e82001-08-16Martin Stjernholm }
f8404e2014-05-23Arne Goedeke PMOD_EXPORT void push_text( const char *x )
bf76fe2014-05-22Per Hedbor { struct svalue *_sp_ = Pike_sp++; SET_SVAL_SUBTYPE(*_sp_, 0); _sp_->u.string=make_shared_string(x); debug_malloc_touch(_sp_->u.string); SET_SVAL_TYPE(*_sp_, PIKE_T_STRING); }
75367d2014-08-22Arne Goedeke PMOD_EXPORT void push_static_text( const char *x ) { struct svalue *_sp_ = Pike_sp++; SET_SVAL_SUBTYPE(*_sp_, 0); _sp_->u.string=make_shared_static_string(x, strlen(x), eightbit); debug_malloc_touch(_sp_->u.string); SET_SVAL_TYPE(*_sp_, PIKE_T_STRING); }
6d8e742002-11-11Henrik Grubbström (Grubba) /* NOTE: Returns 1 if result on stack, 0 otherwise. */
9036e82001-08-16Martin Stjernholm PMOD_EXPORT int safe_apply_handler(const char *fun, struct object *handler, struct object *compat, INT32 args, TYPE_FIELD rettypes) { JMP_BUF recovery;
89ee9e2002-10-28Martin Nilsson  int ret;
6d8e742002-11-11Henrik Grubbström (Grubba)  STACK_LEVEL_START(args);
9036e82001-08-16Martin Stjernholm #if 0 fprintf(stderr, "safe_apply_handler(\"%s\", 0x%08p, 0x%08p, %d)\n", fun, handler, compat, args); #endif /* 0 */ free_svalue(& throw_value);
1ab4ac2008-01-26Martin Stjernholm  mark_free_svalue (&throw_value);
9036e82001-08-16Martin Stjernholm 
3601d32002-11-23Martin Stjernholm  if (SETJMP_SP(recovery, args)) {
9036e82001-08-16Martin Stjernholm  ret = 0;
6d8e742002-11-11Henrik Grubbström (Grubba)  } else {
61863e2001-12-20Martin Stjernholm  if (low_unsafe_apply_handler (fun, handler, compat, args) &&
017b572011-10-28Henrik Grubbström (Grubba)  rettypes && !((1 << TYPEOF(Pike_sp[-1])) & rettypes)) {
9abc252002-09-21Martin Stjernholm  if ((rettypes & BIT_ZERO) && SAFE_IS_ZERO (Pike_sp - 1)) { pop_stack(); push_int(0); } else {
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_error("Invalid return value from %s: %O\n", fun, Pike_sp-1);
9abc252002-09-21Martin Stjernholm  }
9036e82001-08-16Martin Stjernholm  } ret = 1;
c3805a2001-06-08Henrik Grubbström (Grubba)  }
9036e82001-08-16Martin Stjernholm  UNSETJMP(recovery);
6d8e742002-11-11Henrik Grubbström (Grubba)  STACK_LEVEL_DONE(ret);
9036e82001-08-16Martin Stjernholm  return ret;
c3805a2001-06-08Henrik Grubbström (Grubba) }
1f7c8f2012-05-18Henrik Grubbström (Grubba) PMOD_EXPORT void apply_lfun(struct object *o, int lfun, int args)
fa382f1996-06-21Fredrik Hübinette (Hubbe) {
1f7c8f2012-05-18Henrik Grubbström (Grubba)  int fun;
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
1f7c8f2012-05-18Henrik Grubbström (Grubba)  if(lfun < 0 || lfun >= NUM_LFUNS)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Apply lfun on illegal value!\n");
fa382f1996-06-21Fredrik Hübinette (Hubbe) #endif if(!o->prog)
1f7c8f2012-05-18Henrik Grubbström (Grubba)  PIKE_ERROR("destructed object", "Apply on destructed object.\n", Pike_sp, args);
fa382f1996-06-21Fredrik Hübinette (Hubbe) 
1f7c8f2012-05-18Henrik Grubbström (Grubba)  if ((fun = (int)FIND_LFUN(o->prog, lfun)) < 0) Pike_error("Calling undefined lfun::%s.\n", lfun_names[lfun]); apply_low(o, fun, args);
fa382f1996-06-21Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void apply_shared(struct object *o,
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *fun,
5267b71995-08-09Fredrik Hübinette (Hubbe)  int args) {
5cc20b2003-07-30Martin Stjernholm  int id = find_shared_string_identifier(fun, o->prog); if (id >= 0) apply_low(o, id, args); else
9606eb2004-11-12Henrik Grubbström (Grubba)  Pike_error("Cannot call unknown function \"%S\".\n", fun);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
6424ff2014-11-06Martin Nilsson PMOD_EXPORT void apply(struct object *o, const char *fun, int args)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
5cc20b2003-07-30Martin Stjernholm  int id = find_identifier(fun, o->prog); if (id >= 0) apply_low(o, id, args); else Pike_error ("Cannot call unknown function \"%s\".\n", fun);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT void apply_svalue(struct svalue *s, INT32 args)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
017b572011-10-28Henrik Grubbström (Grubba)  if(TYPEOF(*s) == T_INT)
5267b71995-08-09Fredrik Hübinette (Hubbe)  { pop_n_elems(args); push_int(0); }else{
6d8c692000-08-08Henrik Grubbström (Grubba)  ptrdiff_t expected_stack=Pike_sp-args+1 - Pike_interpreter.evaluator_stack;
fa382f1996-06-21Fredrik Hübinette (Hubbe) 
5267b71995-08-09Fredrik Hübinette (Hubbe)  strict_apply_svalue(s,args);
f077582000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp > (expected_stack + Pike_interpreter.evaluator_stack))
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  {
f077582000-07-06Fredrik Hübinette (Hubbe)  pop_n_elems(Pike_sp-(expected_stack + Pike_interpreter.evaluator_stack));
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  }
f077582000-07-06Fredrik Hübinette (Hubbe)  else if(Pike_sp < (expected_stack + Pike_interpreter.evaluator_stack))
f6f02d1995-10-16Fredrik Hübinette (Hubbe)  { push_int(0); }
71f3a21998-11-22Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG
f077582000-07-06Fredrik Hübinette (Hubbe)  if(Pike_sp < (expected_stack + Pike_interpreter.evaluator_stack))
5aad932002-08-15Marcus Comstedt  Pike_fatal("Stack underflow!\n");
f6f02d1995-10-16Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }
6026c62004-09-22Martin Stjernholm PMOD_EXPORT void safe_apply_svalue(struct svalue *s, int args, int handle_errors) { JMP_BUF recovery; free_svalue(& throw_value);
1ab4ac2008-01-26Martin Stjernholm  mark_free_svalue (&throw_value);
6026c62004-09-22Martin Stjernholm  if(SETJMP_SP(recovery, args)) { if(handle_errors) call_handle_error(); push_int(0); }else{ apply_svalue (s, args); } UNSETJMP(recovery); }
9335ea2003-01-17Henrik Grubbström (Grubba) /* Apply function @[fun] in parent @[depth] levels up with @[args] arguments. */ PMOD_EXPORT void apply_external(int depth, int fun, INT32 args) { struct external_variable_context loc; loc.o = Pike_fp->current_object; loc.parent_identifier = Pike_fp->fun;
d8f4f52009-08-06Henrik Grubbström (Grubba)  if (loc.o->prog) {
3478312003-08-03Martin Stjernholm  loc.inherit = INHERIT_FROM_INT(loc.o->prog, loc.parent_identifier);
9335ea2003-01-17Henrik Grubbström (Grubba) 
d8f4f52009-08-06Henrik Grubbström (Grubba)  find_external_context(&loc, depth);
9335ea2003-01-17Henrik Grubbström (Grubba) 
d8f4f52009-08-06Henrik Grubbström (Grubba)  apply_low(loc.o, fun + loc.inherit->identifier_level, args); } else { PIKE_ERROR("destructed object", "Apply on parent of destructed object.\n", Pike_sp, args); }
9335ea2003-01-17Henrik Grubbström (Grubba) }
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) 
7965d72001-01-24Fredrik Hübinette (Hubbe)  if(Pike_sp > &(Pike_interpreter.evaluator_stack[Pike_stack_size]))
5aad932002-08-15Marcus Comstedt  Pike_fatal("Svalue stack overflow. "
6f95902000-08-17Henrik Grubbström (Grubba)  "(%ld entries on stack, stack_size is %ld entries)\n", PTRDIFF_T_TO_LONG(Pike_sp - Pike_interpreter.evaluator_stack),
7965d72001-01-24Fredrik Hübinette (Hubbe)  PTRDIFF_T_TO_LONG(Pike_stack_size));
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7965d72001-01-24Fredrik Hübinette (Hubbe)  if(Pike_mark_sp > &(Pike_interpreter.mark_stack[Pike_stack_size]))
5aad932002-08-15Marcus Comstedt  Pike_fatal("Mark stack overflow.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) 
fc26f62000-07-06Fredrik Hübinette (Hubbe)  if(Pike_mark_sp < Pike_interpreter.mark_stack)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Mark stack underflow.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) 
9eb5fc2008-05-12Henrik Grubbström (Grubba)  for(s=Pike_interpreter.evaluator_stack;s<Pike_sp;s++) { /* NOTE: Freed svalues are allowed on the stack. */
017b572011-10-28Henrik Grubbström (Grubba)  if (TYPEOF(*s) != PIKE_T_FREE) check_svalue(s);
9eb5fc2008-05-12Henrik Grubbström (Grubba)  }
5267b71995-08-09Fredrik Hübinette (Hubbe) 
f077582000-07-06Fredrik Hübinette (Hubbe)  s=Pike_interpreter.evaluator_stack;
fc26f62000-07-06Fredrik Hübinette (Hubbe)  for(m=Pike_interpreter.mark_stack;m<Pike_mark_sp;m++)
5267b71995-08-09Fredrik Hübinette (Hubbe)  { if(*m < s)
5aad932002-08-15Marcus Comstedt  Pike_fatal("Mark stack failure.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe)  s=*m; }
7965d72001-01-24Fredrik Hübinette (Hubbe)  if(s > &(Pike_interpreter.evaluator_stack[Pike_stack_size]))
5aad932002-08-15Marcus Comstedt  Pike_fatal("Mark stack exceeds svalue stack\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) 
f077582000-07-06Fredrik Hübinette (Hubbe)  for(f=Pike_fp;f;f=f->next)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
0f887e1996-08-12Fredrik Hübinette (Hubbe)  if(f->locals) {
f077582000-07-06Fredrik Hübinette (Hubbe)  if(f->locals < Pike_interpreter.evaluator_stack ||
7965d72001-01-24Fredrik Hübinette (Hubbe)  f->locals > &(Pike_interpreter.evaluator_stack[Pike_stack_size]))
5aad932002-08-15Marcus Comstedt  Pike_fatal("Local variable pointer points to Finspång.\n");
5267b71995-08-09Fredrik Hübinette (Hubbe) 
7965d72001-01-24Fredrik Hübinette (Hubbe)  if(f->args < 0 || f->args > Pike_stack_size)
5aad932002-08-15Marcus Comstedt  Pike_fatal("FEL FEL FEL! HELP!! (corrupted pike_frame)\n");
0f887e1996-08-12Fredrik Hübinette (Hubbe)  }
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }
70fbc12011-05-02Per Hedbor 
688f082001-02-27Martin Stjernholm 
9bdebf2001-10-22Martin Stjernholm static const char *safe_idname_from_int(struct program *prog, int func) { /* ID_FROM_INT with a thick layer of checks. */ struct reference *ref; struct inherit *inher; struct identifier *id; if (!prog) return "<null program *>"; if (func < 0 || func >= prog->num_identifier_references) return "<offset outside prog->identifier_references>"; if (!prog->identifier_references) return "<null prog->identifier_references>"; ref = prog->identifier_references + func; if (ref->inherit_offset >= prog->num_inherits) return "<offset outside prog->inherits>"; if (!prog->inherits) return "<null prog->inherits>"; inher = prog->inherits + ref->inherit_offset; prog = inher->prog; if (!prog) return "<null inherited prog>"; if (ref->identifier_offset >= prog->num_identifiers) return "<offset outside inherited prog->identifiers>"; if (!prog->identifiers) return "<null inherited prog->identifiers>"; id = prog->identifiers + ref->identifier_offset; if (!id->name) return "<null identifier->name>";
dfbba62008-01-13Henrik Grubbström (Grubba)  if (id->name->size_shift) return "<wide identifier->name->str>"; /* FIXME: Convert wide string identifiers to narrow strings? */
9bdebf2001-10-22Martin Stjernholm  return id->name->str; }
5f152f2001-03-17Henrik Grubbström (Grubba) /*: Prints the Pike backtrace for the interpreter context in the given *: thread to stderr, without messing in the internals (doesn't even *: use dynamic_buffer). *: *: This function is intended only for convenient use inside a *: debugger session; it can't be used from inside the code.
688f082001-02-27Martin Stjernholm  */ void gdb_backtrace ( #ifdef PIKE_THREADS THREAD_T thread_id #endif ) { struct pike_frame *f, *of; #ifdef PIKE_THREADS extern struct thread_state *gdb_thread_state_for_id(THREAD_T); struct thread_state *ts = gdb_thread_state_for_id(thread_id); if (!ts) { fputs ("Not a Pike thread.\n", stderr); return; }
d97eb72011-07-10Henrik Grubbström (Grubba)  f = ts->state.frame_pointer;
688f082001-02-27Martin Stjernholm #else f = Pike_fp; #endif for (of = 0; f; f = (of = f)->next) if (f->refs) { int args, i;
aacbec2003-10-20Martin Stjernholm  char *file = NULL;
ef24a82012-01-12Henrik Grubbström (Grubba)  INT_TYPE line;
688f082001-02-27Martin Stjernholm 
fa93a52008-02-28Henrik Grubbström (Grubba)  if (f->context) {
688f082001-02-27Martin Stjernholm  if (f->pc)
fa93a52008-02-28Henrik Grubbström (Grubba)  file = low_get_line_plain (f->pc, f->context->prog, &line, 0);
688f082001-02-27Martin Stjernholm  else
fa93a52008-02-28Henrik Grubbström (Grubba)  file = low_get_program_line_plain (f->context->prog, &line, 0);
688f082001-02-27Martin Stjernholm  }
aacbec2003-10-20Martin Stjernholm  if (file)
ef24a82012-01-12Henrik Grubbström (Grubba)  fprintf (stderr, "%s:%ld: ", file, (long)line);
aacbec2003-10-20Martin Stjernholm  else
688f082001-02-27Martin Stjernholm  fputs ("unknown program: ", stderr);
fa93a52008-02-28Henrik Grubbström (Grubba)  if (f->current_program) {
688f082001-02-27Martin Stjernholm  /* FIXME: Wide string identifiers. */
fa93a52008-02-28Henrik Grubbström (Grubba)  fputs (safe_idname_from_int(f->current_program, f->fun), stderr);
688f082001-02-27Martin Stjernholm  fputc ('(', stderr); } else fputs ("unknown function(", stderr); if(!f->locals) { args=0; }else{
7a216f2015-01-27Arne Goedeke  ptrdiff_t tmp; if(of) { tmp = of->locals - f->locals; } else { tmp = ts->state.stack_pointer - f->locals; } args = (INT32)tmp; args = MAXIMUM(MINIMUM(args, f->num_args),0);
688f082001-02-27Martin Stjernholm  } for (i = 0; i < args; i++) { struct svalue *arg = f->locals + i;
017b572011-10-28Henrik Grubbström (Grubba)  switch (TYPEOF(*arg)) {
688f082001-02-27Martin Stjernholm  case T_INT: fprintf (stderr, "%ld", (long) arg->u.integer); break; case T_TYPE:
2db4d72001-03-03Henrik Grubbström (Grubba)  /* FIXME: */ fputs("type-value", stderr);
688f082001-02-27Martin Stjernholm  break; case T_STRING: { int i,j=0; fputc ('"', stderr); for(i=0; i < arg->u.string->len && i < 100; i++) { switch(j=index_shared_string(arg->u.string,i)) { case '\n': fputc ('\\', stderr); fputc ('n', stderr); break; case '\t': fputc ('\\', stderr); fputc ('t', stderr); break; case '\b': fputc ('\\', stderr); fputc ('b', stderr); break; case '\r': fputc ('\\', stderr); fputc ('r', stderr); break; case '"': case '\\': fputc ('\\', stderr); fputc (j, stderr); break; default: if(j>=0 && j<256 && isprint(j)) { fputc (j, stderr); break; } fputc ('\\', stderr); fprintf (stderr, "%o", j); switch(index_shared_string(arg->u.string,i+1)) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': fputc ('"', stderr); fputc ('"', stderr); } break;
13670c2015-05-25Martin Nilsson  }
688f082001-02-27Martin Stjernholm  } fputc ('"', stderr); if (i < arg->u.string->len) fprintf (stderr, "+[%ld]", (long) (arg->u.string->len - i)); break; } case T_FUNCTION: /* FIXME: Wide string identifiers. */
017b572011-10-28Henrik Grubbström (Grubba)  if(SUBTYPEOF(*arg) == FUNCTION_BUILTIN)
688f082001-02-27Martin Stjernholm  fputs (arg->u.efun->name->str, stderr); else if(arg->u.object->prog)
017b572011-10-28Henrik Grubbström (Grubba)  fputs (safe_idname_from_int(arg->u.object->prog, SUBTYPEOF(*arg)), stderr);
688f082001-02-27Martin Stjernholm  else fputc ('0', stderr); break; case T_OBJECT: { struct program *p = arg->u.object->prog; if (p && p->num_linenumbers) {
aacbec2003-10-20Martin Stjernholm  file = low_get_program_line_plain (p, &line, 0);
ef24a82012-01-12Henrik Grubbström (Grubba)  fprintf (stderr, "object(%s:%ld)", file, (long)line);
688f082001-02-27Martin Stjernholm  } else fputs ("object", stderr); break; } case T_PROGRAM: { struct program *p = arg->u.program; if (p->num_linenumbers) {
aacbec2003-10-20Martin Stjernholm  file = low_get_program_line_plain (p, &line, 0);
ef24a82012-01-12Henrik Grubbström (Grubba)  fprintf (stderr, "program(%s:%ld)", file, (long)line);
688f082001-02-27Martin Stjernholm  } else fputs ("program", stderr); break; } case T_FLOAT: fprintf (stderr, "%f",(double) arg->u.float_number); break; case T_ARRAY: fprintf (stderr, "array[%ld]", (long) arg->u.array->size); break; case T_MULTISET:
5b15bb2001-12-10Martin Stjernholm  fprintf (stderr, "multiset[%ld]", (long) multiset_sizeof (arg->u.multiset));
688f082001-02-27Martin Stjernholm  break; case T_MAPPING: fprintf (stderr, "mapping[%ld]", (long) m_sizeof (arg->u.mapping)); break; default:
017b572011-10-28Henrik Grubbström (Grubba)  fprintf (stderr, "<Unknown %d>", TYPEOF(*arg));
688f082001-02-27Martin Stjernholm  } if (i < args - 1) fputs (", ", stderr); } fputs (")\n", stderr); } else fputs ("frame with no references\n", stderr); }
5f152f2001-03-17Henrik Grubbström (Grubba) /*: Prints the Pike backtraces for the interpreter contexts in all *: Pike threads to stderr, using @[gdb_backtrace]. *: *: This function is intended only for convenient use inside a *: debugger session; it can't be used from inside the program.
688f082001-02-27Martin Stjernholm  */ void gdb_backtraces() { #ifdef PIKE_THREADS extern INT32 gdb_next_thread_state(INT32, struct thread_state **); INT32 i = 0; struct thread_state *ts = 0; while ((i = gdb_next_thread_state (i, &ts)), ts) {
e851e92003-06-06Martin Nilsson  fprintf (stderr, "\nTHREAD_ID %p (swapped %s):\n",
ef24a82012-01-12Henrik Grubbström (Grubba)  (void *)(ptrdiff_t)ts->id, ts->swapped ? "out" : "in");
688f082001-02-27Martin Stjernholm  gdb_backtrace (ts->id); } #else gdb_backtrace(); #endif }
70fbc12011-05-02Per Hedbor #endif
688f082001-02-27Martin Stjernholm 
66a1572001-01-12Martin Stjernholm PMOD_EXPORT void custom_check_stack(ptrdiff_t amount, const char *fmt, ...)
c915472000-12-04Martin Stjernholm { if (low_stack_check(amount)) { va_list args; va_start(args, fmt); va_error(fmt, args); } }
d97eb72011-07-10Henrik Grubbström (Grubba) PMOD_EXPORT void low_cleanup_interpret(struct Pike_interpreter_struct *interpreter)
4051312005-01-25Henrik Grubbström (Grubba) { #ifdef USE_MMAP_FOR_STACK if(!interpreter->evaluator_stack_malloced) { munmap((char *)interpreter->evaluator_stack, Pike_stack_size*sizeof(struct svalue)); interpreter->evaluator_stack = 0; } if(!interpreter->mark_stack_malloced) { munmap((char *)interpreter->mark_stack, Pike_stack_size*sizeof(struct svalue *)); interpreter->mark_stack = 0; } #endif if(interpreter->evaluator_stack)
0ec7522014-04-27Martin Nilsson  free(interpreter->evaluator_stack);
4051312005-01-25Henrik Grubbström (Grubba)  if(interpreter->mark_stack)
0ec7522014-04-27Martin Nilsson  free(interpreter->mark_stack);
4051312005-01-25Henrik Grubbström (Grubba)  interpreter->mark_stack = 0; interpreter->evaluator_stack = 0; interpreter->mark_stack_malloced = 0; interpreter->evaluator_stack_malloced = 0; interpreter->stack_pointer = 0; interpreter->mark_stack_pointer = 0; interpreter->frame_pointer = 0; }
fa8c692000-11-30Fredrik Hübinette (Hubbe) PMOD_EXPORT void cleanup_interpret(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
f077582000-07-06Fredrik Hübinette (Hubbe)  while(Pike_fp)
424d9c1999-05-02Fredrik Hübinette (Hubbe)  POP_PIKE_FRAME();
5267b71995-08-09Fredrik Hübinette (Hubbe)  reset_evaluator();
b208c11996-08-03Fredrik Hübinette (Hubbe) 
4051312005-01-25Henrik Grubbström (Grubba)  low_cleanup_interpret(&Pike_interpreter);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
b660c81999-03-01Fredrik Hübinette (Hubbe)  void really_clean_up_interpret(void) {
5b7b061999-04-08Fredrik Hübinette (Hubbe) #ifdef DO_PIKE_CLEANUP
424d9c1999-05-02Fredrik Hübinette (Hubbe) #if 0 struct pike_frame_block *p; int e; for(p=pike_frame_blocks;p;p=p->next) for(e=0;e<128;e++) debug_malloc_dump_references( p->x + e); #endif
ae07882006-01-24Martin Stjernholm  free_callback_list (&evaluator_callbacks);
b660c81999-03-01Fredrik Hübinette (Hubbe)  free_all_pike_frame_blocks();
ca176b2006-02-27Martin Stjernholm  free_all_catch_context_blocks();
5b7b061999-04-08Fredrik Hübinette (Hubbe) #endif
b660c81999-03-01Fredrik Hübinette (Hubbe) }