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 
07513e1996-10-04Fredrik Hübinette (Hubbe) #include "global.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "constants.h"
bb55f81997-03-16Fredrik Hübinette (Hubbe) #include "pike_macros.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "program.h"
06983f1996-09-22Fredrik Hübinette (Hubbe) #include "pike_types.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "stralloc.h"
9aa6fa1997-05-19Fredrik Hübinette (Hubbe) #include "pike_memory.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) #include "interpret.h"
244ce61997-01-26Fredrik Hübinette (Hubbe) #include "mapping.h"
b2d3e42000-12-01Fredrik Hübinette (Hubbe) #include "pike_error.h"
37775c2004-04-06Martin Nilsson #include "pike_security.h"
5e83442008-05-11Martin Stjernholm #include "gc.h"
6eff5e2013-10-09Arne Goedeke #include "block_allocator.h"
5267b71995-08-09Fredrik Hübinette (Hubbe) 
5f61da2000-04-13Fredrik Hübinette (Hubbe) struct mapping *builtin_constants = 0;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
1e93882003-08-20Martin Stjernholm #ifdef PIKE_DEBUG struct callable *first_callable = NULL; #endif
6c3a6f2013-11-20Henrik Grubbström (Grubba) /* This is the mapping returned by all_constants(). */
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct mapping *get_builtin_constants(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
244ce61997-01-26Fredrik Hübinette (Hubbe)  return builtin_constants;
5267b71995-08-09Fredrik Hübinette (Hubbe) }
06983f1996-09-22Fredrik Hübinette (Hubbe) void low_add_efun(struct pike_string *name, struct svalue *fun)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
244ce61997-01-26Fredrik Hübinette (Hubbe)  struct svalue s;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(s, T_STRING, 0, string, name);
5267b71995-08-09Fredrik Hübinette (Hubbe) 
244ce61997-01-26Fredrik Hübinette (Hubbe)  if(fun) { mapping_insert(builtin_constants, &s, fun); }else{ map_delete(builtin_constants, &s);
5267b71995-08-09Fredrik Hübinette (Hubbe)  } }
0929a02002-09-12Marcus Comstedt void low_add_constant(const char *name, struct svalue *fun)
cecf511997-02-06Fredrik Hübinette (Hubbe) { struct pike_string *p; p=make_shared_string(name); low_add_efun(p, fun); free_string(p); }
7abd892004-12-30Henrik Grubbström (Grubba) void add_pike_string_constant(const char *name, const char *str, int len) { struct pike_string *key = make_shared_string(name); struct pike_string *val = make_shared_binary_string(str, len); mapping_string_insert_string(builtin_constants, key, val); free_string(val); free_string(key); }
0929a02002-09-12Marcus Comstedt PMOD_EXPORT void add_global_program(const char *name, struct program *p)
cecf511997-02-06Fredrik Hübinette (Hubbe) { struct svalue s;
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(s, T_PROGRAM, 0, program, p);
ac0bdd2007-04-17Henrik Grubbström (Grubba)  low_add_constant(name, p?&s:NULL);
cecf511997-02-06Fredrik Hübinette (Hubbe) }
d476592013-06-12Arne Goedeke static struct block_allocator callable_allocator = BA_INIT_PAGES(sizeof(struct callable), 2); void really_free_callable(struct callable * c) {
6eff5e2013-10-09Arne Goedeke #ifdef PIKE_DEBUG DOUBLEUNLINK (first_callable, c); #endif free_type(c->type); free_string(c->name); c->name=0; EXIT_PIKE_MEMOBJ(c);
d476592013-06-12Arne Goedeke  ba_free(&callable_allocator, c); }
6eff5e2013-10-09Arne Goedeke 
d476592013-06-12Arne Goedeke void count_memory_in_callables(size_t * num, size_t * size) { ba_count_all(&callable_allocator, num, size); }
3b54e52014-08-21Martin Nilsson void free_all_callable_blocks(void) {
d476592013-06-12Arne Goedeke  ba_destroy(&callable_allocator); }
45ee5d1999-02-10Fredrik Hübinette (Hubbe) 
c39e432001-08-02Fredrik Hübinette (Hubbe) int global_callable_flags=0;
45ee5d1999-02-10Fredrik Hübinette (Hubbe) /* Eats one ref to 'type' and 'name' */
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct callable *low_make_callable(c_fun fun,
167da22008-06-18Henrik Grubbström (Grubba)  struct pike_string *name, struct pike_type *type, int flags, optimize_fun optimize, docode_fun docode)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
dc8d022014-04-27Martin Nilsson  struct callable *f=ba_alloc(&callable_allocator);
d476592013-06-12Arne Goedeke #ifdef PIKE_DEBUG DOUBLELINK(first_callable, f); #endif
9386322011-07-21Henrik Grubbström (Grubba)  INIT_PIKE_MEMOBJ(f, T_STRUCT_CALLABLE);
5267b71995-08-09Fredrik Hübinette (Hubbe)  f->function=fun;
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  f->name=name; f->type=type;
48a6242003-05-31Martin Stjernholm  f->prog=Pike_compiler->new_program;
5267b71995-08-09Fredrik Hübinette (Hubbe)  f->flags=flags;
7bd0ea1996-02-19Fredrik Hübinette (Hubbe)  f->docode=docode; f->optimize=optimize;
c39e432001-08-02Fredrik Hübinette (Hubbe)  f->internal_flags = global_callable_flags;
044c621999-04-14Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG {
167da22008-06-18Henrik Grubbström (Grubba)  struct pike_type *z = NULL; add_ref(type); type = check_splice_call(name, type, 1, mixed_type_string, NULL, CALL_INHIBIT_WARNINGS); if (type) { z = new_get_return_type(type, CALL_INHIBIT_WARNINGS); free_type(type); }
babd872001-02-23Henrik Grubbström (Grubba)  f->may_return_void = (z == void_type_string);
a1fd452002-12-12Martin Nilsson  if(!z) Pike_fatal("Function has no valid return type.\n");
3699402001-02-20Henrik Grubbström (Grubba)  free_type(z);
044c621999-04-14Fredrik Hübinette (Hubbe)  }
d9a93b2001-07-01Fredrik Hübinette (Hubbe)  f->runs=0;
044c621999-04-14Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe)  return f; }
1f21332000-07-28Fredrik Hübinette (Hubbe) PMOD_EXPORT struct callable *make_callable(c_fun fun,
0929a02002-09-12Marcus Comstedt  const char *name, const char *type,
7e877a2003-04-02Martin Stjernholm  int flags,
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  optimize_fun optimize, docode_fun docode)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
babd872001-02-23Henrik Grubbström (Grubba)  return low_make_callable(fun, make_shared_string(name), parse_type(type), flags, optimize, docode);
5267b71995-08-09Fredrik Hübinette (Hubbe) }
5d1e952011-11-05Martin Nilsson PMOD_EXPORT void add_efun2(const char *name, c_fun fun, const char *type, int flags, optimize_fun optimize, docode_fun docode)
5267b71995-08-09Fredrik Hübinette (Hubbe) { struct svalue s;
06983f1996-09-22Fredrik Hübinette (Hubbe)  struct pike_string *n;
5267b71995-08-09Fredrik Hübinette (Hubbe) 
63077e2014-09-05Arne Goedeke  n=make_shared_static_string(name, strlen(name), eightbit);
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(s, T_FUNCTION, FUNCTION_BUILTIN, efun, make_callable(fun, name, type, flags, optimize, docode));
5267b71995-08-09Fredrik Hübinette (Hubbe)  low_add_efun(n, &s); free_svalue(&s); free_string(n); }
5d1e952011-11-05Martin Nilsson PMOD_EXPORT void add_efun(const char *name, c_fun fun, const char *type, int flags)
7bd0ea1996-02-19Fredrik Hübinette (Hubbe) {
5d1e952011-11-05Martin Nilsson  add_efun2(name,fun,type,flags,0,0);
7bd0ea1996-02-19Fredrik Hübinette (Hubbe) }
5d1e952011-11-05Martin Nilsson PMOD_EXPORT void quick_add_efun(const char *name, ptrdiff_t name_length, c_fun fun,
74dfe82012-12-30Jonas Walldén  const char *type, ptrdiff_t UNUSED(type_length),
5d1e952011-11-05Martin Nilsson  int flags, optimize_fun optimize, docode_fun docode)
45ee5d1999-02-10Fredrik Hübinette (Hubbe) { struct svalue s;
3699402001-02-20Henrik Grubbström (Grubba)  struct pike_string *n; struct pike_type *t;
45ee5d1999-02-10Fredrik Hübinette (Hubbe) 
3866ad2004-05-18David Gourdelier #ifdef PIKE_DEBUG
85649e2004-01-13Martin Nilsson  if(simple_mapping_string_lookup(builtin_constants, name)) Pike_fatal("%s added as efun more than once.\n", name); #endif
63077e2014-09-05Arne Goedeke  n = make_shared_static_string(name, name_length, eightbit);
8a2a522001-03-03Henrik Grubbström (Grubba)  t = make_pike_type(type);
45ee5d1999-02-10Fredrik Hübinette (Hubbe) #ifdef DEBUG check_type_string(t); #endif add_ref(n);
017b572011-10-28Henrik Grubbström (Grubba)  SET_SVAL(s, T_FUNCTION, FUNCTION_BUILTIN, efun,
5d1e952011-11-05Martin Nilsson  low_make_callable(fun, n, t, flags, optimize, docode));
85649e2004-01-13Martin Nilsson  mapping_string_insert(builtin_constants, n, &s);
45ee5d1999-02-10Fredrik Hübinette (Hubbe)  free_svalue(&s); free_string(n); }
7c36d52014-06-17Henrik Grubbström (Grubba) PMOD_EXPORT void visit_callable (struct callable *c, int action, void *extra)
5e83442008-05-11Martin Stjernholm {
c42e092014-06-18Henrik Grubbström (Grubba)  visit_enter(c, T_STRUCT_CALLABLE, extra);
5e83442008-05-11Martin Stjernholm  switch (action) { #ifdef PIKE_DEBUG default: Pike_fatal ("Unknown visit action %d.\n", action); case VISIT_NORMAL: case VISIT_COMPLEX_ONLY: break; #endif case VISIT_COUNT_BYTES: mc_counted_bytes += sizeof (struct callable); break; } if (!(action & VISIT_COMPLEX_ONLY)) {
7c36d52014-06-17Henrik Grubbström (Grubba)  visit_type_ref (c->type, REF_TYPE_NORMAL, extra); visit_string_ref (c->name, REF_TYPE_NORMAL, extra);
5e83442008-05-11Martin Stjernholm  } /* Looks like the c->prog isn't refcounted..? */ /* visit_program_ref (c->prog, REF_TYPE_NORMAL); */
c42e092014-06-18Henrik Grubbström (Grubba)  visit_leave(c, T_STRUCT_CALLABLE, extra);
5e83442008-05-11Martin Stjernholm }
d9a93b2001-07-01Fredrik Hübinette (Hubbe) #ifdef PIKE_DEBUG void present_constant_profiling(void) {
d476592013-06-12Arne Goedeke  struct callable *c; for (c = first_callable; c; c = c->next) { fprintf(stderr,"%010ld @E@: %s\n",c->runs, c->name->str);
d9a93b2001-07-01Fredrik Hübinette (Hubbe)  } } #endif
d8073b2002-01-27Martin Stjernholm void init_builtin_constants(void) {
56fce32014-02-19Martin Nilsson  builtin_constants = allocate_mapping(325);
d8073b2002-01-27Martin Stjernholm }
1c5b342003-03-26Martin Nilsson void exit_builtin_constants(void)
5267b71995-08-09Fredrik Hübinette (Hubbe) {
5b7b061999-04-08Fredrik Hübinette (Hubbe) #ifdef DO_PIKE_CLEANUP
244ce61997-01-26Fredrik Hübinette (Hubbe)  if(builtin_constants)
5267b71995-08-09Fredrik Hübinette (Hubbe)  {
244ce61997-01-26Fredrik Hübinette (Hubbe)  free_mapping(builtin_constants); builtin_constants=0;
5267b71995-08-09Fredrik Hübinette (Hubbe)  }
5b7b061999-04-08Fredrik Hübinette (Hubbe) #endif
5267b71995-08-09Fredrik Hübinette (Hubbe) }