| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* gfun.d - dispatch for generic functions |
| 6 |
* |
| 7 |
* Copyright (c) 1990 Giuseppe Attardi |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
#include <ecl/ecl.h> |
| 15 |
#include <ecl/ecl-inl.h> |
| 16 |
#include <ecl/internal.h> |
| 17 |
#include <ecl/cache.h> |
| 18 |
|
| 19 |
static cl_object generic_function_dispatch_vararg(cl_narg, ...); |
| 20 |
|
| 21 |
cl_object |
| 22 |
FEnot_funcallable_fixed() |
| 23 |
{ |
| 24 |
cl_env_ptr env = ecl_process_env(); |
| 25 |
cl_object fun = env->function; |
| 26 |
FEerror("Not a funcallable instance ~A.", 1, fun); |
| 27 |
@(return); |
| 28 |
} |
| 29 |
|
| 30 |
cl_object |
| 31 |
FEnot_funcallable_vararg(cl_narg narg, ...) |
| 32 |
{ |
| 33 |
return FEnot_funcallable_fixed(); |
| 34 |
} |
| 35 |
|
| 36 |
static cl_object |
| 37 |
user_function_dispatch(cl_narg narg, ...) |
| 38 |
{ |
| 39 |
int i; |
| 40 |
cl_object output; |
| 41 |
cl_env_ptr env = ecl_process_env(); |
| 42 |
cl_object fun = env->function; |
| 43 |
struct ecl_stack_frame frame_aux; |
| 44 |
const cl_object frame = ecl_stack_frame_open(env, (cl_object)&frame_aux, narg); |
| 45 |
ecl_va_list args; ecl_va_start(args, narg, narg, 0); |
| 46 |
for (i = 0; i < narg; i++) { |
| 47 |
ECL_STACK_FRAME_SET(frame, i, ecl_va_arg(args)); |
| 48 |
} |
| 49 |
fun = fun->instance.slots[fun->instance.length - 1]; |
| 50 |
output = ecl_apply_from_stack_frame(frame, fun); |
| 51 |
ecl_stack_frame_close(frame); |
| 52 |
ecl_va_end(args); |
| 53 |
return output; |
| 54 |
} |
| 55 |
|
| 56 |
static void |
| 57 |
reshape_instance(cl_object x, int delta) |
| 58 |
{ |
| 59 |
cl_fixnum size = x->instance.length + delta; |
| 60 |
cl_object aux = ecl_allocate_instance(ECL_CLASS_OF(x), size); |
| 61 |
/* Except for the different size, this must match si_copy_instance */ |
| 62 |
aux->instance.slotds = x->instance.slotds; |
| 63 |
aux->instance.stamp = x->instance.stamp; |
| 64 |
aux->instance.class_stamp = x->instance.class_stamp; |
| 65 |
memcpy(aux->instance.slots, x->instance.slots, |
| 66 |
(delta < 0 ? aux->instance.length : x->instance.length) * |
| 67 |
sizeof(cl_object)); |
| 68 |
x->instance = aux->instance; |
| 69 |
} |
| 70 |
|
| 71 |
cl_object |
| 72 |
clos_set_funcallable_instance_function(cl_object x, cl_object function_or_t) |
| 73 |
{ |
| 74 |
if (ecl_unlikely(!ECL_INSTANCEP(x))) |
| 75 |
FEwrong_type_nth_arg(@[clos::set-funcallable-instance-function], |
| 76 |
1, x, @[ext::instance]); |
| 77 |
if (x->instance.isgf == ECL_USER_DISPATCH) { |
| 78 |
reshape_instance(x, -1); |
| 79 |
x->instance.isgf = ECL_NOT_FUNCALLABLE; |
| 80 |
} |
| 81 |
if (function_or_t == ECL_T) { |
| 82 |
x->instance.isgf = ECL_STANDARD_DISPATCH; |
| 83 |
x->instance.entry = generic_function_dispatch_vararg; |
| 84 |
} else if (function_or_t == @'standard-generic-function') { |
| 85 |
x->instance.isgf = ECL_RESTRICTED_DISPATCH; |
| 86 |
x->instance.entry = generic_function_dispatch_vararg; |
| 87 |
} else if (function_or_t == ECL_NIL) { |
| 88 |
x->instance.isgf = ECL_NOT_FUNCALLABLE; |
| 89 |
x->instance.entry = FEnot_funcallable_vararg; |
| 90 |
} else if (function_or_t == @'clos::standard-optimized-reader-method') { |
| 91 |
x->instance.isgf = ECL_READER_DISPATCH; |
| 92 |
x->instance.entry = ecl_slot_reader_dispatch; |
| 93 |
} else if (function_or_t == @'clos::standard-optimized-writer-method') { |
| 94 |
x->instance.isgf = ECL_WRITER_DISPATCH; |
| 95 |
x->instance.entry = ecl_slot_writer_dispatch; |
| 96 |
} else if (Null(cl_functionp(function_or_t))) { |
| 97 |
FEwrong_type_argument(@'function', function_or_t); |
| 98 |
} else { |
| 99 |
reshape_instance(x, +1); |
| 100 |
x->instance.slots[x->instance.length - 1] = function_or_t; |
| 101 |
x->instance.isgf = ECL_USER_DISPATCH; |
| 102 |
x->instance.entry = user_function_dispatch; |
| 103 |
} |
| 104 |
@(return x); |
| 105 |
} |
| 106 |
|
| 107 |
cl_object |
| 108 |
si_generic_function_p(cl_object x) |
| 109 |
{ |
| 110 |
@(return ((ECL_INSTANCEP(x) && (x->instance.isgf))? ECL_T : ECL_NIL)); |
| 111 |
} |
| 112 |
|
| 113 |
static cl_object |
| 114 |
fill_spec_vector(cl_object vector, cl_object frame, cl_object gf) |
| 115 |
{ |
| 116 |
cl_object *args = ECL_STACK_FRAME_PTR(frame); |
| 117 |
cl_index narg = frame->frame.size; |
| 118 |
cl_object spec_how_list = GFUN_SPEC(gf); |
| 119 |
cl_object *argtype = vector->vector.self.t; |
| 120 |
int spec_no = 1; |
| 121 |
argtype[0] = gf; |
| 122 |
loop_for_on_unsafe(spec_how_list) { |
| 123 |
cl_object spec_how = ECL_CONS_CAR(spec_how_list); |
| 124 |
cl_object spec_eql = ECL_CONS_CDR(spec_how); |
| 125 |
cl_object eql_spec; |
| 126 |
cl_object this_arg; |
| 127 |
unlikely_if (spec_no > narg) |
| 128 |
FEwrong_num_arguments(gf); |
| 129 |
unlikely_if (spec_no >= vector->vector.dim) |
| 130 |
ecl_internal_error("Too many arguments to fill_spec_vector()."); |
| 131 |
unlikely_if (!ECL_LISTP(spec_eql)) |
| 132 |
ecl_internal_error("Invalid GF specialization profile."); |
| 133 |
this_arg = args[spec_no-1]; |
| 134 |
/* Need to differentiate between EQL specializers and class specializers, |
| 135 |
because the EQL value can be a class, and may clash with a class |
| 136 |
specializer. Store the cons cell containing the EQL value. */ |
| 137 |
if (!Null(eql_spec = ecl_memql(this_arg, spec_eql))) { |
| 138 |
argtype[spec_no++] = eql_spec; |
| 139 |
} else { |
| 140 |
argtype[spec_no++] = cl_class_of(this_arg); |
| 141 |
} |
| 142 |
} end_loop_for_on_unsafe(spec_how_list); |
| 143 |
vector->vector.fillp = spec_no; |
| 144 |
return vector; |
| 145 |
} |
| 146 |
|
| 147 |
static cl_object |
| 148 |
frame_to_list(cl_object frame) |
| 149 |
{ |
| 150 |
cl_object arglist, *p; |
| 151 |
cl_object *base = ECL_STACK_FRAME_PTR(frame); |
| 152 |
for (p = base + frame->frame.size, arglist = ECL_NIL; p != base; ) { |
| 153 |
arglist = CONS(*(--p), arglist); |
| 154 |
} |
| 155 |
return arglist; |
| 156 |
} |
| 157 |
|
| 158 |
static cl_object |
| 159 |
frame_to_classes(cl_object frame) |
| 160 |
{ |
| 161 |
cl_object arglist, *p; |
| 162 |
cl_object *base = ECL_STACK_FRAME_PTR(frame); |
| 163 |
for (p = base + frame->frame.size, arglist = ECL_NIL; p != base; ) { |
| 164 |
arglist = CONS(cl_class_of(*(--p)), arglist); |
| 165 |
} |
| 166 |
return arglist; |
| 167 |
} |
| 168 |
|
| 169 |
static cl_object |
| 170 |
generic_compute_applicable_method(cl_env_ptr env, cl_object frame, cl_object gf) |
| 171 |
{ |
| 172 |
/* method not cached */ |
| 173 |
cl_object methods = _ecl_funcall3(@'clos::compute-applicable-methods-using-classes', |
| 174 |
gf, frame_to_classes(frame)); |
| 175 |
unlikely_if (Null(env->values[1])) { |
| 176 |
cl_object arglist = frame_to_list(frame); |
| 177 |
methods = _ecl_funcall3(@'compute-applicable-methods', |
| 178 |
gf, arglist); |
| 179 |
unlikely_if (methods == ECL_NIL) { |
| 180 |
env->values[1] = ECL_NIL; |
| 181 |
return methods; |
| 182 |
} |
| 183 |
} |
| 184 |
methods = clos_compute_effective_method_function(gf, GFUN_COMB(gf), methods); |
| 185 |
env->values[1] = ECL_T; |
| 186 |
return methods; |
| 187 |
} |
| 188 |
|
| 189 |
static cl_object |
| 190 |
restricted_compute_applicable_method(cl_env_ptr env, cl_object frame, cl_object gf) |
| 191 |
{ |
| 192 |
/* method not cached */ |
| 193 |
cl_object arglist = frame_to_list(frame); |
| 194 |
cl_object methods = clos_std_compute_applicable_methods(gf, arglist); |
| 195 |
unlikely_if (methods == ECL_NIL) { |
| 196 |
env->values[1] = ECL_NIL; |
| 197 |
return methods; |
| 198 |
} |
| 199 |
methods = clos_std_compute_effective_method(gf, GFUN_COMB(gf), methods); |
| 200 |
env->values[1] = ECL_T; |
| 201 |
return methods; |
| 202 |
} |
| 203 |
|
| 204 |
static cl_object |
| 205 |
compute_applicable_method(cl_env_ptr env, cl_object frame, cl_object gf) |
| 206 |
{ |
| 207 |
if (gf->instance.isgf == ECL_RESTRICTED_DISPATCH) |
| 208 |
return restricted_compute_applicable_method(env, frame, gf); |
| 209 |
else |
| 210 |
return generic_compute_applicable_method(env, frame, gf); |
| 211 |
} |
| 212 |
|
| 213 |
cl_object |
| 214 |
_ecl_standard_dispatch(cl_object frame, cl_object gf) |
| 215 |
{ |
| 216 |
cl_object func, vector; |
| 217 |
const cl_env_ptr env = frame->frame.env; |
| 218 |
ecl_cache_ptr cache = env->method_cache; |
| 219 |
ecl_cache_record_ptr e; |
| 220 |
ECL_WITHOUT_INTERRUPTS_BEGIN(env) { |
| 221 |
vector = fill_spec_vector(cache->keys, frame, gf); |
| 222 |
e = ecl_search_cache(cache); |
| 223 |
if (e->key != OBJNULL) { |
| 224 |
func = e->value; |
| 225 |
} else { |
| 226 |
/* The keys and the cache may change while we |
| 227 |
* compute the applicable methods. We must save |
| 228 |
* the keys and recompute the cache location if |
| 229 |
* it was filled. */ |
| 230 |
cl_object keys = cl_copy_seq(vector); |
| 231 |
func = compute_applicable_method(env, frame, gf); |
| 232 |
if (env->values[1] != ECL_NIL) { |
| 233 |
if (e->key != OBJNULL) { |
| 234 |
e = ecl_search_cache(cache); |
| 235 |
} |
| 236 |
e->key = keys; |
| 237 |
e->value = func; |
| 238 |
} |
| 239 |
} |
| 240 |
} ECL_WITHOUT_INTERRUPTS_END; |
| 241 |
if (func == ECL_NIL) |
| 242 |
func = cl_apply(3, @'no-applicable-method', gf, frame); |
| 243 |
else { |
| 244 |
func = _ecl_funcall3(func, frame, ECL_NIL); |
| 245 |
} |
| 246 |
return func; |
| 247 |
} |
| 248 |
|
| 249 |
static cl_object |
| 250 |
generic_function_dispatch_vararg(cl_narg narg, ...) |
| 251 |
{ |
| 252 |
cl_object output; |
| 253 |
ECL_STACK_FRAME_VARARGS_BEGIN(narg, narg, frame) { |
| 254 |
output = _ecl_standard_dispatch(frame, frame->frame.env->function); |
| 255 |
} ECL_STACK_FRAME_VARARGS_END(frame); |
| 256 |
return output; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
cl_object |
| 261 |
si_clear_gfun_hash(cl_object what) |
| 262 |
{ |
| 263 |
/* |
| 264 |
* This function clears the generic function call hashes selectively. |
| 265 |
* what = ECL_T means clear the hash completely |
| 266 |
* what = generic function, means cleans only these entries |
| 267 |
* If we work on a multithreaded environment, we simply enqueue these |
| 268 |
* operations and wait for the destination thread to update its own hash. |
| 269 |
*/ |
| 270 |
cl_env_ptr the_env = ecl_process_env(); |
| 271 |
#ifdef ECL_THREADS |
| 272 |
cl_object list; |
| 273 |
for (list = mp_all_processes(); !Null(list); list = ECL_CONS_CDR(list)) { |
| 274 |
cl_object process = ECL_CONS_CAR(list); |
| 275 |
struct cl_env_struct *env = process->process.env; |
| 276 |
if (the_env != env && env) { |
| 277 |
if (env->method_cache) |
| 278 |
ecl_cache_remove_one(env->method_cache, what); |
| 279 |
if (env->slot_cache) |
| 280 |
ecl_cache_remove_one(env->slot_cache, what); |
| 281 |
} |
| 282 |
} |
| 283 |
#endif |
| 284 |
ecl_cache_remove_one(the_env->method_cache, what); |
| 285 |
ecl_cache_remove_one(the_env->slot_cache, what); |
| 286 |
ecl_return0(the_env); |
| 287 |
} |