Debian Codegraph @

src/c/eval.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * eval.d - evaluation
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <ecl/ecl.h>
16 #include <ecl/ecl-inl.h>
17 #include <ecl/internal.h>
18
19 cl_object *
20 _ecl_va_sp(cl_narg narg)
21 {
22 return ECL_STACK_FRAME_PTR(ecl_process_env()->stack_frame) + narg;
23 }
24
25 /* Calling conventions:
26 * Compiled C code calls lisp function supplying #args, and args.
27 * Linking function performs check_args, gets jmp_buf with _setjmp, then
28 * if cfun then stores C code address into function link location
29 * and transfers to jmp_buf at cf_self
30 * if cclosure then replaces #args with cc_env and calls cc_self
31 * otherwise, it emulates funcall.
32 */
33
34 cl_object
35 ecl_apply_from_stack_frame(cl_object frame, cl_object x)
36 {
37 cl_object *sp = ECL_STACK_FRAME_PTR(frame);
38 cl_index narg = frame->frame.size;
39 cl_object fun = x;
40 cl_object ret;
41 frame->frame.env->stack_frame = frame;
42 AGAIN:
43 frame->frame.env->function = fun;
44 if (ecl_unlikely(fun == ECL_NIL))
45 FEundefined_function(x);
46 switch (ecl_t_of(fun)) {
47 case t_cfunfixed:
48 if (ecl_unlikely(narg != (cl_index)fun->cfun.narg))
49 FEwrong_num_arguments(fun);
50 ret = APPLY_fixed(narg, fun->cfunfixed.entry_fixed, sp);
51 break;
52 case t_cfun:
53 ret = APPLY(narg, fun->cfun.entry, sp);
54 break;
55 case t_cclosure:
56 ret = APPLY(narg, fun->cclosure.entry, sp);
57 break;
58 case t_instance:
59 switch (fun->instance.isgf) {
60 case ECL_STANDARD_DISPATCH:
61 case ECL_RESTRICTED_DISPATCH:
62 ret = _ecl_standard_dispatch(frame, fun);
63 break;
64 case ECL_USER_DISPATCH:
65 fun = fun->instance.slots[fun->instance.length - 1];
66 goto AGAIN;
67 case ECL_READER_DISPATCH:
68 case ECL_WRITER_DISPATCH:
69 ret = APPLY(narg, fun->instance.entry, sp);
70 break;
71 default:
72 FEinvalid_function(fun);
73 }
74 break;
75 case t_symbol:
76 if (ecl_unlikely(!ECL_FBOUNDP(fun)))
77 FEundefined_function(fun);
78 fun = ECL_SYM_FUN(fun);
79 goto AGAIN;
80 case t_bytecodes:
81 ret = ecl_interpret(frame, ECL_NIL, fun);
82 break;
83 case t_bclosure:
84 ret = ecl_interpret(frame, fun->bclosure.lex, fun->bclosure.code);
85 break;
86 default:
87 FEinvalid_function(x);
88 }
89 frame->frame.env->stack_frame = NULL; /* for gc's sake */
90 return ret;
91 }
92
93 cl_objectfn
94 ecl_function_dispatch(cl_env_ptr env, cl_object x)
95 {
96 cl_object fun = x;
97 if (ecl_unlikely(fun == ECL_NIL))
98 FEundefined_function(x);
99 switch (ecl_t_of(fun)) {
100 case t_cfunfixed:
101 env->function = fun;
102 return fun->cfunfixed.entry;
103 case t_cfun:
104 env->function = fun;
105 return fun->cfun.entry;
106 case t_cclosure:
107 env->function = fun;
108 return fun->cclosure.entry;
109 case t_instance:
110 env->function = fun;
111 return fun->instance.entry;
112 case t_symbol:
113 fun = ECL_SYM_FUN(fun);
114 env->function = fun;
115 return fun->cfun.entry;
116 case t_bytecodes:
117 env->function = fun;
118 return fun->bytecodes.entry;
119 case t_bclosure:
120 env->function = fun;
121 return fun->bclosure.entry;
122 default:
123 FEinvalid_function(x);
124 }
125 }
126
127 cl_object
128 cl_funcall(cl_narg narg, cl_object function, ...)
129 {
130 cl_object output;
131 --narg;
132 {
133 ECL_STACK_FRAME_VARARGS_BEGIN(narg, function, frame);
134 output = ecl_apply_from_stack_frame(frame, function);
135 ECL_STACK_FRAME_VARARGS_END(frame);
136 }
137 return output;
138 }
139
140 @(defun apply (fun lastarg &rest args)
141 @ {
142 if (narg == 2 && ecl_t_of(lastarg) == t_frame) {
143 return ecl_apply_from_stack_frame(lastarg, fun);
144 } else {
145 cl_object out;
146 cl_index i;
147 struct ecl_stack_frame frame_aux;
148 const cl_object frame = ecl_stack_frame_open(the_env,
149 (cl_object)&frame_aux,
150 narg -= 2);
151 for (i = 0; i < narg; i++) {
152 ecl_stack_frame_push(frame, lastarg);
153 lastarg = ecl_va_arg(args);
154 }
155 if (ecl_t_of(lastarg) == t_frame) {
156 /* This could be replaced with a memcpy() */
157 for (i = 0; i < lastarg->frame.size; i++) {
158 ecl_stack_frame_push(frame, ECL_STACK_FRAME_REF(lastarg, i));
159 }
160 } else loop_for_in (lastarg) {
161 if (ecl_unlikely(i >= ECL_CALL_ARGUMENTS_LIMIT)) {
162 ecl_stack_frame_close(frame);
163 FEprogram_error("CALL-ARGUMENTS-LIMIT exceeded",0);
164 }
165 ecl_stack_frame_push(frame, CAR(lastarg));
166 i++;
167 } end_loop_for_in;
168 out = ecl_apply_from_stack_frame(frame, fun);
169 ecl_stack_frame_close(frame);
170 return out;
171 }
172 }@)
173
174 cl_object
175 cl_eval(cl_object form)
176 {
177 return si_eval_with_env(1, form);
178 }
179
180 @(defun constantp (arg &optional env)
181 @
182 return _ecl_funcall3(@'ext::constantp-inner', arg, env);
183 @)
184
185 @(defun ext::constantp-inner (form &optional env)
186 cl_object value;
187 @ {
188 AGAIN:
189 switch (ecl_t_of(form)) {
190 case t_list:
191 if (Null(form)) {
192 value = ECL_T;
193 break;
194 }
195 if (ECL_CONS_CAR(form) == @'quote') {
196 value = ECL_T;
197 break;
198 }
199 /*
200 value = cl_macroexpand(2, form, env);
201 if (value != form) {
202 form = value;
203 goto AGAIN;
204 }
205 */
206 value = ECL_NIL;
207 break;
208 case t_symbol:
209 value = cl_macroexpand(2, form, env);
210 if (value != form) {
211 form = value;
212 goto AGAIN;
213 }
214 if (!(form->symbol.stype & ecl_stp_constant)) {
215 value = ECL_NIL;
216 break;
217 }
218 default:
219 value = ECL_T;
220 }
221 ecl_return1(the_env, value);
222 } @)
223
224 @(defun ext::constant-form-value (form &optional env)
225 cl_object value;
226 @ {
227 AGAIN:
228 switch (ecl_t_of(form)) {
229 case t_list:
230 if (Null(form)) {
231 value = ECL_NIL;
232 break;
233 }
234 if (ECL_CONS_CAR(form) == @'quote') {
235 return cl_second(form);
236 }
237 FEerror("EXT:CONSTANT-FORM-VALUE invoked with a non-constant form ~A",
238 0, form);
239 break;
240 case t_symbol:
241 value = cl_macroexpand(2, form, env);
242 if (value != form) {
243 form = value;
244 goto AGAIN;
245 }
246 value = ECL_SYM_VAL(the_env, value);
247 break;
248 default:
249 value = form;
250 }
251 @(return value);
252 } @)
253
254
255 cl_object
256 ecl_undefined_function_entry(cl_narg narg, ...)
257 {
258 FEundefined_function(ecl_process_env()->function); /* see object.h */
259 }