| 1 |
/* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */ |
| 2 |
/* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */ |
| 3 |
|
| 4 |
/* |
| 5 |
* compiler.d - bytecode compiler |
| 6 |
* |
| 7 |
* Copyright (c) 2001 Juan Jose Garcia Ripoll |
| 8 |
* |
| 9 |
* See file 'LICENSE' for the copyright details. |
| 10 |
* |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Remarks: |
| 14 |
|
| 15 |
[1] The virtual machine has a word size of 16 bits. Operands and arguments |
| 16 |
have this very size, so that for instance, a jump |
| 17 |
|
| 18 |
OP_JMP increment |
| 19 |
|
| 20 |
takes two words of memory: one for the operator and one for the argument. |
| 21 |
The interpreter is written with this assumption in mind, but it should be |
| 22 |
easily modified, because arguments are retrieved with "next_arg" and |
| 23 |
operators with "next_op". Parts which will require a careful modification |
| 24 |
are marked with flag [1]. |
| 25 |
*/ |
| 26 |
#include <string.h> |
| 27 |
#include <ecl/ecl.h> |
| 28 |
#include <ecl/ecl-inl.h> |
| 29 |
#include <ecl/internal.h> |
| 30 |
#include <ecl/bytecodes.h> |
| 31 |
|
| 32 |
/********************* EXPORTS *********************/ |
| 33 |
|
| 34 |
/* Flags for the compilation routines: */ |
| 35 |
/* + Push the output of this form */ |
| 36 |
#define FLAG_PUSH 1 |
| 37 |
/* + Set the output of this form in VALUES */ |
| 38 |
#define FLAG_VALUES 2 |
| 39 |
/* + Set the output of this form in REG0 */ |
| 40 |
#define FLAG_REG0 4 |
| 41 |
/* + Search function binding in the global environment */ |
| 42 |
#define FLAG_GLOBAL 8 |
| 43 |
/* + Ignore this form */ |
| 44 |
#define FLAG_IGNORE 0 |
| 45 |
#define FLAG_USEFUL (FLAG_PUSH | FLAG_VALUES | FLAG_REG0) |
| 46 |
|
| 47 |
#define FLAG_EXECUTE 16 |
| 48 |
#define FLAG_LOAD 32 |
| 49 |
#define FLAG_COMPILE 64 |
| 50 |
|
| 51 |
/********************* PRIVATE ********************/ |
| 52 |
|
| 53 |
typedef struct cl_compiler_env *cl_compiler_ptr; |
| 54 |
|
| 55 |
#define asm_begin(env) current_pc(env) |
| 56 |
#define current_pc(env) ECL_STACK_INDEX(env) |
| 57 |
#define set_pc(env,n) asm_clear(env,n) |
| 58 |
#define asm_ref(env,n) (cl_fixnum)((env)->run_stack.org[n]) |
| 59 |
static void asm_clear(cl_env_ptr env, cl_index h); |
| 60 |
static void asm_op(cl_env_ptr env, cl_fixnum op); |
| 61 |
static void asm_op2(cl_env_ptr env, int op, int arg); |
| 62 |
static cl_object asm_end(cl_env_ptr env, cl_index handle, cl_object definition); |
| 63 |
static cl_index asm_jmp(cl_env_ptr env, int op); |
| 64 |
static void asm_complete(cl_env_ptr env, int op, cl_index original); |
| 65 |
|
| 66 |
static struct cl_compiler_ref |
| 67 |
c_var_ref(cl_env_ptr env, cl_object var, bool allow_sym_mac, bool ensure_def); |
| 68 |
|
| 69 |
void c_sym_ref(cl_env_ptr env, cl_object name); |
| 70 |
void c_mac_ref(cl_env_ptr env, cl_object name); |
| 71 |
|
| 72 |
static int c_block(cl_env_ptr env, cl_object args, int flags); |
| 73 |
static int c_case(cl_env_ptr env, cl_object args, int flags); |
| 74 |
static int c_catch(cl_env_ptr env, cl_object args, int flags); |
| 75 |
static int c_compiler_let(cl_env_ptr env, cl_object args, int flags); |
| 76 |
static int c_cond(cl_env_ptr env, cl_object args, int flags); |
| 77 |
static int c_eval_when(cl_env_ptr env, cl_object args, int flags); |
| 78 |
static int c_flet(cl_env_ptr env, cl_object args, int flags); |
| 79 |
static int c_funcall(cl_env_ptr env, cl_object args, int flags); |
| 80 |
static int c_function(cl_env_ptr env, cl_object args, int flags); |
| 81 |
static int c_go(cl_env_ptr env, cl_object args, int flags); |
| 82 |
static int c_if(cl_env_ptr env, cl_object args, int flags); |
| 83 |
static int c_labels(cl_env_ptr env, cl_object args, int flags); |
| 84 |
static int c_let(cl_env_ptr env, cl_object args, int flags); |
| 85 |
static int c_leta(cl_env_ptr env, cl_object args, int flags); |
| 86 |
static int c_load_time_value(cl_env_ptr env, cl_object args, int flags); |
| 87 |
static int c_locally(cl_env_ptr env, cl_object args, int flags); |
| 88 |
static int c_macrolet(cl_env_ptr env, cl_object args, int flags); |
| 89 |
static int c_multiple_value_bind(cl_env_ptr env, cl_object args, int flags); |
| 90 |
static int c_multiple_value_call(cl_env_ptr env, cl_object args, int flags); |
| 91 |
static int c_multiple_value_prog1(cl_env_ptr env, cl_object args, int flags); |
| 92 |
static int c_multiple_value_setq(cl_env_ptr env, cl_object args, int flags); |
| 93 |
static int c_not(cl_env_ptr env, cl_object args, int flags); |
| 94 |
static int c_nth_value(cl_env_ptr env, cl_object args, int flags); |
| 95 |
static int c_prog1(cl_env_ptr env, cl_object args, int flags); |
| 96 |
static int c_progv(cl_env_ptr env, cl_object args, int flags); |
| 97 |
static int c_psetq(cl_env_ptr env, cl_object args, int flags); |
| 98 |
static int c_quote(cl_env_ptr env, cl_object args, int flags); |
| 99 |
static int c_values(cl_env_ptr env, cl_object args, int flags); |
| 100 |
static int c_setq(cl_env_ptr env, cl_object args, int flags); |
| 101 |
static int c_return(cl_env_ptr env, cl_object args, int flags); |
| 102 |
static int c_return_from(cl_env_ptr env, cl_object args, int flags); |
| 103 |
static int c_symbol_macrolet(cl_env_ptr env, cl_object args, int flags); |
| 104 |
static int c_tagbody(cl_env_ptr env, cl_object args, int flags); |
| 105 |
static int c_the(cl_env_ptr env, cl_object args, int flags); |
| 106 |
static int c_throw(cl_env_ptr env, cl_object args, int flags); |
| 107 |
static int c_unwind_protect(cl_env_ptr env, cl_object args, int flags); |
| 108 |
static int c_while(cl_env_ptr env, cl_object args, int flags); |
| 109 |
static int c_with_backend(cl_env_ptr env, cl_object args, int flags); |
| 110 |
static int c_until(cl_env_ptr env, cl_object args, int flags); |
| 111 |
static void eval_form(cl_env_ptr env, cl_object form); |
| 112 |
static int execute_each_form(cl_env_ptr env, cl_object body); |
| 113 |
static int compile_toplevel_body(cl_env_ptr env, cl_object args, int flags); |
| 114 |
static int compile_body(cl_env_ptr env, cl_object args, int flags); |
| 115 |
static int compile_form(cl_env_ptr env, cl_object args, int push); |
| 116 |
static int compile_with_load_time_forms(cl_env_ptr env, cl_object form, int flags); |
| 117 |
static int compile_constant(cl_env_ptr env, cl_object stmt, int flags); |
| 118 |
static void maybe_make_load_forms(cl_env_ptr env, cl_object constant); |
| 119 |
|
| 120 |
static int c_cons(cl_env_ptr env, cl_object args, int push); |
| 121 |
static int c_endp(cl_env_ptr env, cl_object args, int push); |
| 122 |
static int c_car(cl_env_ptr env, cl_object args, int push); |
| 123 |
static int c_cdr(cl_env_ptr env, cl_object args, int push); |
| 124 |
static int c_list(cl_env_ptr env, cl_object args, int push); |
| 125 |
static int c_listA(cl_env_ptr env, cl_object args, int push); |
| 126 |
static int c_cons_car(cl_env_ptr env, cl_object args, int push); |
| 127 |
static int c_cons_cdr(cl_env_ptr env, cl_object args, int push); |
| 128 |
|
| 129 |
static cl_object ecl_make_lambda(cl_env_ptr env, cl_object name, cl_object lambda); |
| 130 |
|
| 131 |
static void FEill_formed_input(void) ecl_attr_noreturn; |
| 132 |
|
| 133 |
static int asm_function(cl_env_ptr env, cl_object args, int flags); |
| 134 |
|
| 135 |
/* -------------------- SAFE LIST HANDLING -------------------- */ |
| 136 |
static cl_object |
| 137 |
pop(cl_object *l) { |
| 138 |
cl_object head, list = *l; |
| 139 |
unlikely_if (ECL_ATOM(list)) |
| 140 |
FEill_formed_input(); |
| 141 |
head = ECL_CONS_CAR(list); |
| 142 |
*l = ECL_CONS_CDR(list); |
| 143 |
return head; |
| 144 |
} |
| 145 |
|
| 146 |
static cl_object |
| 147 |
pop_maybe_nil(cl_object *l) { |
| 148 |
cl_object head, list = *l; |
| 149 |
if (list == ECL_NIL) |
| 150 |
return ECL_NIL; |
| 151 |
unlikely_if (!ECL_LISTP(list)) |
| 152 |
FEill_formed_input(); |
| 153 |
head = ECL_CONS_CAR(list); |
| 154 |
*l = ECL_CONS_CDR(list); |
| 155 |
return head; |
| 156 |
} |
| 157 |
|
| 158 |
static cl_object |
| 159 |
push(cl_object v, cl_object *l) { |
| 160 |
cl_object list = *l; |
| 161 |
unlikely_if (!ECL_LISTP(list)) |
| 162 |
FEill_formed_input(); |
| 163 |
*l = ecl_cons(v, *l); |
| 164 |
return *l; |
| 165 |
} |
| 166 |
|
| 167 |
/* ------------------------------ ASSEMBLER ------------------------------ */ |
| 168 |
|
| 169 |
static cl_object |
| 170 |
asm_end(cl_env_ptr env, cl_index beginning, cl_object definition) { |
| 171 |
const cl_compiler_ptr c_env = env->c_env; |
| 172 |
cl_object output; |
| 173 |
cl_index code_size, i; |
| 174 |
cl_opcode *code; |
| 175 |
cl_object file = ECL_SYM_VAL(env,@'ext::*source-location*'), position; |
| 176 |
if (Null(file)) { |
| 177 |
file = ECL_SYM_VAL(env,@'*load-truename*'); |
| 178 |
position = ecl_make_fixnum(0); |
| 179 |
} else { |
| 180 |
position = cl_cdr(file); |
| 181 |
file = cl_car(file); |
| 182 |
} |
| 183 |
|
| 184 |
/* Save bytecodes from this session in a new vector */ |
| 185 |
code_size = current_pc(env) - beginning; |
| 186 |
output = ecl_alloc_object(t_bytecodes); |
| 187 |
output->bytecodes.name = @'si::bytecodes'; |
| 188 |
output->bytecodes.definition = definition; |
| 189 |
output->bytecodes.code_size = code_size; |
| 190 |
output->bytecodes.code = ecl_alloc_atomic(code_size * sizeof(cl_opcode)); |
| 191 |
output->bytecodes.data = c_env->constants; |
| 192 |
output->bytecodes.flex = ECL_NIL; |
| 193 |
output->bytecodes.nlcl = ecl_make_fixnum(c_env->env_width); |
| 194 |
for (i = 0, code = (cl_opcode *)output->bytecodes.code; i < code_size; i++) { |
| 195 |
code[i] = (cl_opcode)(cl_fixnum)(env->run_stack.org[beginning+i]); |
| 196 |
} |
| 197 |
output->bytecodes.entry = _ecl_bytecodes_dispatch_vararg; |
| 198 |
ecl_set_function_source_file_info(output, (file == OBJNULL)? ECL_NIL : file, |
| 199 |
(file == OBJNULL)? ECL_NIL : position); |
| 200 |
asm_clear(env, beginning); |
| 201 |
return output; |
| 202 |
} |
| 203 |
|
| 204 |
#define asm_arg(env,n) asm_op(env,n) |
| 205 |
|
| 206 |
static void |
| 207 |
asm_op(cl_env_ptr env, cl_fixnum code) { |
| 208 |
cl_object v = (cl_object)code; |
| 209 |
ECL_STACK_PUSH(env,v); |
| 210 |
} |
| 211 |
|
| 212 |
static void |
| 213 |
asm_clear(cl_env_ptr env, cl_index h) { |
| 214 |
ECL_STACK_UNWIND(env, h); |
| 215 |
} |
| 216 |
|
| 217 |
static void |
| 218 |
asm_op2(cl_env_ptr env, int code, int n) { |
| 219 |
if (ecl_unlikely(n < -MAX_OPARG || MAX_OPARG < n)) |
| 220 |
FEprogram_error("Argument to bytecode is too large", 0); |
| 221 |
asm_op(env, code); |
| 222 |
asm_arg(env, n); |
| 223 |
} |
| 224 |
|
| 225 |
static cl_index |
| 226 |
asm_constant(cl_env_ptr env, cl_object c) |
| 227 |
{ |
| 228 |
const cl_compiler_ptr c_env = env->c_env; |
| 229 |
cl_object constants = c_env->constants; |
| 230 |
cl_vector_push_extend(2, c, constants); |
| 231 |
return constants->vector.fillp-1; |
| 232 |
} |
| 233 |
|
| 234 |
static cl_index |
| 235 |
asm_captured(cl_env_ptr env, cl_object c) |
| 236 |
{ |
| 237 |
const cl_compiler_ptr c_env = env->c_env; |
| 238 |
cl_object captured = c_env->captured; |
| 239 |
cl_vector_push_extend(2, c, captured); |
| 240 |
return captured->vector.fillp-1; |
| 241 |
} |
| 242 |
|
| 243 |
static cl_index |
| 244 |
asm_jmp(cl_env_ptr env, int op) { |
| 245 |
cl_index output; |
| 246 |
asm_op(env, op); |
| 247 |
output = current_pc(env); |
| 248 |
asm_arg(env, 0); |
| 249 |
return output; |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
asm_complete(cl_env_ptr env, int op, cl_index pc) { |
| 254 |
cl_fixnum delta = current_pc(env) - pc; /* [1] */ |
| 255 |
if (ecl_unlikely(op && (asm_ref(env, pc-1) != op))) |
| 256 |
FEprogram_error("Non matching codes in ASM-COMPLETE2", 0); |
| 257 |
else if (ecl_unlikely(delta < -MAX_OPARG || delta > MAX_OPARG)) |
| 258 |
FEprogram_error("Too large jump", 0); |
| 259 |
else { |
| 260 |
env->run_stack.org[pc] = (cl_object)(cl_fixnum)delta; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/* ------------------------------ COMPILER ------------------------------ */ |
| 265 |
|
| 266 |
typedef struct { |
| 267 |
void *symbol; |
| 268 |
int (*compiler)(cl_env_ptr, cl_object, int); |
| 269 |
int lexical_increment; |
| 270 |
} compiler_record; |
| 271 |
|
| 272 |
static compiler_record database[] = { |
| 273 |
{@'block', c_block, 1}, |
| 274 |
{@'case', c_case, 1}, |
| 275 |
{@'catch', c_catch, 1}, |
| 276 |
{@'ext::compiler-let', c_compiler_let, 0}, |
| 277 |
{@'cond', c_cond, 1}, |
| 278 |
{@'eval-when', c_eval_when, 0}, |
| 279 |
{@'flet', c_flet, 1}, |
| 280 |
{@'function', c_function, 1}, |
| 281 |
{@'funcall', c_funcall, 1}, |
| 282 |
{@'go', c_go, 1}, |
| 283 |
{@'if', c_if, 1}, |
| 284 |
{@'labels', c_labels, 1}, |
| 285 |
{@'let', c_let, 1}, |
| 286 |
{@'let*', c_leta, 1}, |
| 287 |
{@'locally', c_locally, 0}, |
| 288 |
{@'load-time-value', c_load_time_value, 1}, |
| 289 |
{@'macrolet', c_macrolet, 0}, |
| 290 |
{@'multiple-value-bind', c_multiple_value_bind, 1}, |
| 291 |
{@'multiple-value-call', c_multiple_value_call, 1}, |
| 292 |
{@'multiple-value-prog1', c_multiple_value_prog1, 1}, |
| 293 |
{@'multiple-value-setq', c_multiple_value_setq, 1}, |
| 294 |
{@'not', c_not, 1}, |
| 295 |
{@'nth-value', c_nth_value, 1}, |
| 296 |
{@'null', c_not, 1}, |
| 297 |
{@'progn', compile_toplevel_body, 0}, |
| 298 |
{@'prog1', c_prog1, 1}, |
| 299 |
{@'progv', c_progv, 1}, |
| 300 |
{@'psetq', c_psetq, 1}, |
| 301 |
{@'quote', c_quote, 1}, |
| 302 |
{@'return', c_return, 1}, |
| 303 |
{@'return-from', c_return_from, 1}, |
| 304 |
{@'setq', c_setq, 1}, |
| 305 |
{@'symbol-macrolet', c_symbol_macrolet, 0}, |
| 306 |
{@'tagbody', c_tagbody, 1}, |
| 307 |
{@'the', c_the, 0}, |
| 308 |
{@'ext::truly-the', c_the, 0}, |
| 309 |
{@'throw', c_throw, 1}, |
| 310 |
{@'unwind-protect', c_unwind_protect, 1}, |
| 311 |
{@'values', c_values, 1}, |
| 312 |
{@'si::while', c_while, 0}, |
| 313 |
{@'ext::with-backend', c_with_backend, 0}, |
| 314 |
{@'si::until', c_until, 0}, |
| 315 |
/* Inlined functions */ |
| 316 |
{@'cons', c_cons, 1}, |
| 317 |
{@'car', c_car, 1}, |
| 318 |
{@'cdr', c_cdr, 1}, |
| 319 |
{@'first', c_car, 1}, |
| 320 |
{@'rest', c_cdr, 1}, |
| 321 |
{@'list', c_list, 1}, |
| 322 |
{@'list*', c_listA, 1}, |
| 323 |
{@'endp', c_endp, 1}, |
| 324 |
/* Primops */ |
| 325 |
{@'si::cons-car', c_cons_car, 1}, |
| 326 |
{@'si::cons-cdr', c_cons_cdr, 1}, |
| 327 |
|
| 328 |
{NULL, NULL, 1} |
| 329 |
}; |
| 330 |
|
| 331 |
/* ----------------- LEXICAL ENVIRONMENT HANDLING -------------------- */ |
| 332 |
|
| 333 |
static void |
| 334 |
assert_type_symbol(cl_object v) |
| 335 |
{ |
| 336 |
if (ecl_t_of(v) != t_symbol) |
| 337 |
FEprogram_error("Expected a symbol, found ~S.", 1, v); |
| 338 |
} |
| 339 |
|
| 340 |
static void |
| 341 |
FEill_formed_input() |
| 342 |
{ |
| 343 |
FEprogram_error("Syntax error: list with too few elements or improperly terminated.", 0); |
| 344 |
} |
| 345 |
|
| 346 |
static int |
| 347 |
c_search_constant(cl_env_ptr env, cl_object c) |
| 348 |
{ |
| 349 |
const cl_compiler_ptr c_env = env->c_env; |
| 350 |
cl_object p = c_env->constants; |
| 351 |
int n; |
| 352 |
for (n = 0; n < p->vector.fillp; n++) { |
| 353 |
if (ecl_eql(p->vector.self.t[n], c)) { |
| 354 |
return n; |
| 355 |
} |
| 356 |
} |
| 357 |
return -1; |
| 358 |
} |
| 359 |
|
| 360 |
static int |
| 361 |
c_register_constant(cl_env_ptr env, cl_object c) |
| 362 |
{ |
| 363 |
int n = c_search_constant(env, c); |
| 364 |
return (n < 0) ? asm_constant(env, c) : n; |
| 365 |
} |
| 366 |
|
| 367 |
static void |
| 368 |
asm_arg_data(cl_env_ptr env, cl_object o) { |
| 369 |
asm_arg(env, c_register_constant(env, o)); |
| 370 |
} |
| 371 |
|
| 372 |
static void |
| 373 |
asm_op2c(cl_env_ptr env, int code, cl_object o) { |
| 374 |
asm_op2(env, code, c_register_constant(env, o)); |
| 375 |
} |
| 376 |
|
| 377 |
/* Captured variables */ |
| 378 |
static int |
| 379 |
c_search_captured(cl_env_ptr env, cl_object c) |
| 380 |
{ |
| 381 |
const cl_compiler_ptr c_env = env->c_env; |
| 382 |
cl_object p = c_env->captured; |
| 383 |
int n; |
| 384 |
if(Null(p)) { |
| 385 |
p = si_make_vector(ECL_T, ecl_make_fixnum(16), |
| 386 |
ECL_T, /* Adjustable */ |
| 387 |
ecl_make_fixnum(0), /* Fillp */ |
| 388 |
ECL_NIL, /* displacement */ |
| 389 |
ECL_NIL); |
| 390 |
c_env->captured = p; |
| 391 |
} |
| 392 |
for (n = 0; n < p->vector.fillp; n++) { |
| 393 |
if (ecl_eql(p->vector.self.t[n], c)) { |
| 394 |
return n; |
| 395 |
} |
| 396 |
} |
| 397 |
return -1; |
| 398 |
} |
| 399 |
|
| 400 |
static int |
| 401 |
c_register_captured(cl_env_ptr env, cl_object c) |
| 402 |
{ |
| 403 |
int n = c_search_captured(env, c); |
| 404 |
return (n < 0) ? asm_captured(env, c) : n; |
| 405 |
} |
| 406 |
|
| 407 |
/* |
| 408 |
* Note: the following should match the definitions in cmp/cmpenv.lsp, as |
| 409 |
* well as CMP-ENV-REGISTER-MACROLET (lsp/defmacro.lsp) |
| 410 |
* |
| 411 |
* The compiler environment consists of two lists, one stored in |
| 412 |
* env->variables, the other one stored in env->macros. |
| 413 |
* |
| 414 |
* variable-record = |
| 415 |
* (:block block-name [used-p | block-object] location) | |
| 416 |
* (:tag ({tag-name [. tag-id]}*) [used-p | tag-object] location) | |
| 417 |
* (:function function-name used-p [location]) | |
| 418 |
* (var-name {:special | nil} bound-p [location]) | |
| 419 |
* (symbol si::symbol-macro macro-function) | |
| 420 |
* (:declare type arguments) | |
| 421 |
* SI:FUNCTION-BOUNDARY | |
| 422 |
* SI:UNWIND-PROTECT-BOUNDARY |
| 423 |
* (:declare declaration-arguments*) |
| 424 |
* (:type type-name [type-definition | expansion-function]) |
| 425 |
* macro-record = |
| 426 |
* (function-name FUNCTION [| function-object]) | |
| 427 |
* (macro-name si::macro macro-function) | |
| 428 |
* (:declare name declaration) | |
| 429 |
* (compiler-macro-name si::compiler-macro macro-function) | |
| 430 |
* SI:FUNCTION-BOUNDARY | |
| 431 |
* SI:UNWIND-PROTECT-BOUNDARY |
| 432 |
* |
| 433 |
* A *-NAME is a symbol. A TAG-ID is a number. A MACRO-FUNCTION is a function |
| 434 |
* that provides us with the expansion for that local macro or symbol |
| 435 |
* macro. BOUND-P is true when the variable has been bound by an enclosing form, |
| 436 |
* while it is NIL if the variable-record corresponds just to a special |
| 437 |
* declaration. SI:FUNCTION-BOUNDARY and SI:UNWIND-PROTECT-BOUNDARY denote |
| 438 |
* function and unwind-protect boundaries. |
| 439 |
* |
| 440 |
* The brackets [] denote differences between the bytecodes and C compiler |
| 441 |
* environments, with the first option belonging to the interpreter and the |
| 442 |
* second alternative to the compiler. |
| 443 |
* |
| 444 |
* A LOCATION object is proper to the bytecodes compiler and denotes the |
| 445 |
* position of this variable, block, tag or function, in the lexical |
| 446 |
* environment. Currently, it is a CONS with two integers (DEPTH . ORDER), |
| 447 |
* denoting the depth of the nested environments and the position in the |
| 448 |
* environment (from the beginning, not from the tail). |
| 449 |
* |
| 450 |
* The BLOCK-, TAG- and FUNCTION- objects are proper of the compiler and carry |
| 451 |
* further information. |
| 452 |
* |
| 453 |
* The last variable records are devoted to declarations and are only used by |
| 454 |
* the C compiler. Read cmpenv.lsp for more details on the structure of these |
| 455 |
* declaration forms, as they do not completely match those of Common-Lisp. |
| 456 |
*/ |
| 457 |
|
| 458 |
static cl_object |
| 459 |
c_push_record(const cl_compiler_ptr c_env, cl_object type, |
| 460 |
cl_object arg1, cl_object arg2) |
| 461 |
{ |
| 462 |
cl_object depth = ecl_make_fixnum(c_env->env_depth); |
| 463 |
cl_object index = ecl_make_fixnum(c_env->env_size++); |
| 464 |
cl_object loc = CONS(depth, index); |
| 465 |
if (c_env->env_width < c_env->env_size) |
| 466 |
c_env->env_width = c_env->env_size; |
| 467 |
return cl_list(4, type, arg1, arg2, loc); |
| 468 |
} |
| 469 |
|
| 470 |
static cl_object |
| 471 |
c_make_record(const cl_compiler_ptr c_env, cl_object type, |
| 472 |
cl_object arg1, cl_object arg2) |
| 473 |
{ |
| 474 |
return cl_list(3, type, arg1, arg2); |
| 475 |
} |
| 476 |
|
| 477 |
static void |
| 478 |
c_register_block(cl_env_ptr env, cl_object name) |
| 479 |
{ |
| 480 |
const cl_compiler_ptr c_env = env->c_env; |
| 481 |
cl_object entry = c_push_record(c_env, @':block', name, ECL_NIL); |
| 482 |
c_env->variables = CONS(entry, c_env->variables); |
| 483 |
} |
| 484 |
|
| 485 |
static void |
| 486 |
c_register_tags(cl_env_ptr env, cl_object all_tags) |
| 487 |
{ |
| 488 |
const cl_compiler_ptr c_env = env->c_env; |
| 489 |
cl_object entry = c_push_record(c_env, @':tag', all_tags, ECL_NIL); |
| 490 |
c_env->variables = CONS(entry, c_env->variables); |
| 491 |
} |
| 492 |
|
| 493 |
static void |
| 494 |
c_register_var(cl_env_ptr env, cl_object var, bool special, bool bound) |
| 495 |
{ |
| 496 |
const cl_compiler_ptr c_env = env->c_env; |
| 497 |
cl_object boundp = bound? ECL_T : ECL_NIL; |
| 498 |
cl_object entry = (special |
| 499 |
? c_make_record(c_env, var, ECL_T, boundp) |
| 500 |
: c_push_record(c_env, var, ECL_NIL, boundp)); |
| 501 |
c_env->variables = CONS(entry, c_env->variables); |
| 502 |
} |
| 503 |
|
| 504 |
static void |
| 505 |
c_register_function(cl_env_ptr env, cl_object name) |
| 506 |
{ |
| 507 |
const cl_compiler_ptr c_env = env->c_env; |
| 508 |
cl_object entry = c_push_record(c_env, @':function', name, ECL_NIL); |
| 509 |
c_env->variables = CONS(entry, c_env->variables); |
| 510 |
c_env->macros = CONS(cl_list(2, name, @'function'), c_env->macros); |
| 511 |
} |
| 512 |
|
| 513 |
static void |
| 514 |
c_register_symbol_macro(cl_env_ptr env, cl_object name, cl_object exp_fun) |
| 515 |
{ |
| 516 |
const cl_compiler_ptr c_env = env->c_env; |
| 517 |
cl_object entry = c_make_record(c_env, name, @'si::symbol-macro', exp_fun); |
| 518 |
c_env->variables = CONS(entry, c_env->variables); |
| 519 |
} |
| 520 |
|
| 521 |
static void |
| 522 |
c_register_macro(cl_env_ptr env, cl_object name, cl_object exp_fun) |
| 523 |
{ |
| 524 |
const cl_compiler_ptr c_env = env->c_env; |
| 525 |
c_env->macros = CONS(cl_list(3, name, @'si::macro', exp_fun), c_env->macros); |
| 526 |
} |
| 527 |
|
| 528 |
static void |
| 529 |
c_register_boundary(cl_env_ptr env, cl_object type) |
| 530 |
{ |
| 531 |
const cl_compiler_ptr c_env = env->c_env; |
| 532 |
c_env->variables = CONS(type, c_env->variables); |
| 533 |
c_env->macros = CONS(type, c_env->macros); |
| 534 |
} |
| 535 |
|
| 536 |
static cl_object |
| 537 |
c_macro_expand1(cl_env_ptr env, cl_object stmt) |
| 538 |
{ |
| 539 |
const cl_compiler_ptr c_env = env->c_env; |
| 540 |
if(ECL_ATOM(stmt)) { |
| 541 |
if(!ECL_SYMBOLP(stmt)) return stmt; |
| 542 |
c_sym_ref(env, stmt); |
| 543 |
} else { |
| 544 |
cl_object name = ECL_CONS_CAR(stmt); |
| 545 |
if(!ECL_SYMBOLP(name)) return stmt; |
| 546 |
c_mac_ref(env, name); |
| 547 |
} |
| 548 |
return cl_macroexpand_1(2, stmt, CONS(c_env->variables, c_env->macros)); |
| 549 |
} |
| 550 |
|
| 551 |
static void |
| 552 |
import_lexenv(cl_env_ptr env, cl_object lexenv) |
| 553 |
{ |
| 554 |
if (!ECL_VECTORP(lexenv)) |
| 555 |
return; |
| 556 |
/* |
| 557 |
* Given the environment of an interpreted function, we guess a |
| 558 |
* suitable compiler enviroment to compile forms that access the |
| 559 |
* variables and local functions of this interpreted code. |
| 560 |
*/ |
| 561 |
cl_object *lex = lexenv->vector.self.t; |
| 562 |
cl_index index = lexenv->vector.dim; |
| 563 |
cl_compiler_env_ptr c_env = env->c_env; |
| 564 |
cl_object record; |
| 565 |
while(index>0) { |
| 566 |
index--; |
| 567 |
record = lex[index]; |
| 568 |
if (!LISTP(record)) { |
| 569 |
if (ecl_t_of(record) == t_bclosure) |
| 570 |
record = record->bclosure.code; |
| 571 |
c_register_function(env, record->bytecodes.name); |
| 572 |
} else { |
| 573 |
cl_object record0 = ECL_CONS_CAR(record); |
| 574 |
cl_object record1 = ECL_CONS_CDR(record); |
| 575 |
if (ECL_SYMBOLP(record0)) { |
| 576 |
if (record0 == @'si::macro') |
| 577 |
c_register_macro(env, ECL_CONS_CDR(record1), ECL_CONS_CAR(record1)); |
| 578 |
else if (record0 == @'si::symbol-macro') |
| 579 |
c_register_symbol_macro(env, ECL_CONS_CDR(record1), ECL_CONS_CAR(record1)); |
| 580 |
else |
| 581 |
c_register_var(env, record0, FALSE, TRUE); |
| 582 |
} else if (record1 == ecl_make_fixnum(0)) { |
| 583 |
/* We have lost the information, which tag corresponds to |
| 584 |
the lex-env record. If we are compiling a closure over a |
| 585 |
tag, we will get an error later on. */ |
| 586 |
} else { |
| 587 |
c_register_block(env, record1); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
/* INV we register a function boundary so that objects are not looked for in |
| 592 |
the parent locals. -- jd 2025-01-13*/ |
| 593 |
c_register_boundary(env, @'si::function-boundary'); |
| 594 |
c_env->lex_env = lexenv; |
| 595 |
} |
| 596 |
|
| 597 |
static void |
| 598 |
import_cmpenv(cl_env_ptr env, cl_object cmpenv) |
| 599 |
{ |
| 600 |
if (!ECL_CONSP(cmpenv)) |
| 601 |
return; |
| 602 |
cl_object variables = ECL_CONS_CAR(cmpenv); |
| 603 |
cl_object functions = ECL_CONS_CDR(cmpenv); |
| 604 |
cl_compiler_env_ptr c_env = env->c_env; |
| 605 |
cl_object record, reg0, reg1; |
| 606 |
c_env->variables = variables; |
| 607 |
c_env->macros = functions; |
| 608 |
loop_for_on_unsafe(variables) { |
| 609 |
record = ECL_CONS_CAR(variables); |
| 610 |
if (ECL_ATOM(record)) |
| 611 |
continue; |
| 612 |
reg0 = pop(&record); |
| 613 |
reg1 = pop(&record); |
| 614 |
if (reg0 == @':declare' || reg1 == @'si::symbol-macro') { |
| 615 |
continue; |
| 616 |
} else { |
| 617 |
c_env->lexical_level = 1; |
| 618 |
break; |
| 619 |
} |
| 620 |
} end_loop_for_on_unsafe(); |
| 621 |
/* INV we register a function boundary so that objects are not looked for in |
| 622 |
the parent locals. -- jd 2025-01-13*/ |
| 623 |
c_register_boundary(env, @'si::function-boundary'); |
| 624 |
} |
| 625 |
|
| 626 |
static void |
| 627 |
c_new_env(cl_env_ptr the_env, cl_compiler_env_ptr new, cl_compiler_env_ptr old) |
| 628 |
{ |
| 629 |
the_env->c_env = new; |
| 630 |
if (old) { |
| 631 |
*new = *old; |
| 632 |
new->captured = ECL_NIL; |
| 633 |
new->parent_env = old; |
| 634 |
new->env_size = 0; |
| 635 |
new->env_width = 0; |
| 636 |
new->env_depth = old->env_depth + 1; |
| 637 |
} else { |
| 638 |
new->code_walker = ECL_SYM_VAL(the_env, @'si::*code-walker*'); |
| 639 |
new->constants = si_make_vector(ECL_T, ecl_make_fixnum(16), |
| 640 |
ECL_T, /* Adjustable */ |
| 641 |
ecl_make_fixnum(0), /* Fillp */ |
| 642 |
ECL_NIL, /* displacement */ |
| 643 |
ECL_NIL); |
| 644 |
new->stepping = 0; |
| 645 |
new->lex_env = ECL_NIL; |
| 646 |
new->lexical_level = 0; |
| 647 |
new->load_time_forms = ECL_NIL; |
| 648 |
new->ltf_being_created = ECL_NIL; |
| 649 |
new->ltf_defer_init_until = ECL_T; |
| 650 |
new->ltf_locations = ECL_NIL; |
| 651 |
new->captured = ECL_NIL; |
| 652 |
new->parent_env = NULL; |
| 653 |
new->env_size = 0; |
| 654 |
new->env_width = 0; |
| 655 |
new->env_depth = 0; |
| 656 |
new->macros = ECL_NIL; |
| 657 |
new->variables = ECL_NIL; |
| 658 |
new->mode = FLAG_EXECUTE; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
static void |
| 663 |
c_restore_env(cl_env_ptr the_env, cl_compiler_env_ptr new_c_env, cl_compiler_env_ptr old_c_env) |
| 664 |
{ |
| 665 |
if (new_c_env->env_depth == 0) { |
| 666 |
/* Clear created constants (they cannot be printed) */ |
| 667 |
loop_for_in(new_c_env->ltf_locations) { |
| 668 |
cl_index loc = ecl_fixnum(ECL_CONS_CAR(new_c_env->ltf_locations)); |
| 669 |
new_c_env->constants->vector.self.t[loc] = ecl_make_fixnum(0); |
| 670 |
} end_loop_for_in; |
| 671 |
} |
| 672 |
the_env->c_env = old_c_env; |
| 673 |
} |
| 674 |
|
| 675 |
/* c_sym_ref and c_mac_ref ensure that symbol macros and macros that are |
| 676 |
referenced across the function boundary are captured. We capture the entry |
| 677 |
verbatim and we don't bind any objects at runtime -- these objects are |
| 678 |
supplied to enable recompilation by CCMP and BCMP. */ |
| 679 |
|
| 680 |
static void |
| 681 |
close_around_macros(cl_env_ptr env, cl_object mfun) |
| 682 |
{ |
| 683 |
cl_object lex = mfun->bclosure.lex; |
| 684 |
cl_object *lex_vec = lex->vector.self.t; |
| 685 |
for (cl_index i = 0; i < lex->vector.dim; i++) { |
| 686 |
cl_object reg = lex_vec[i]; /* INV see interpreter.d for lexenv structure */ |
| 687 |
cl_object type = CAR(reg); /* lexenv tag */ |
| 688 |
cl_object name = CDDR(reg); /* macro name */ |
| 689 |
if (type == @'si::macro') { |
| 690 |
c_mac_ref(env, name); |
| 691 |
} else if (type == @'si::symbol_macro') { |
| 692 |
c_sym_ref(env, name); |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
void |
| 698 |
c_sym_ref(cl_env_ptr env, cl_object name) |
| 699 |
{ |
| 700 |
const cl_compiler_ptr c_env = env->c_env; |
| 701 |
int function_boundary_crossed = 0; |
| 702 |
cl_object l = c_env->variables; |
| 703 |
loop_for_on_unsafe(l) { |
| 704 |
cl_object record = ECL_CONS_CAR(l), reg, type, other; |
| 705 |
if (record == @'si::function-boundary') |
| 706 |
function_boundary_crossed++; |
| 707 |
if(ECL_ATOM(record)) |
| 708 |
continue; |
| 709 |
reg = record; |
| 710 |
type = pop(®); |
| 711 |
other = pop(®); |
| 712 |
if (type == name) { |
| 713 |
if (other == @'si::symbol-macro') { |
| 714 |
if (function_boundary_crossed) { |
| 715 |
c_register_captured(env, record); |
| 716 |
} else { |
| 717 |
cl_object mfun = ECL_CONS_CAR(reg); |
| 718 |
if (ecl_t_of(mfun) == t_bclosure) { |
| 719 |
close_around_macros(env, mfun); |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
return; |
| 724 |
} |
| 725 |
} end_loop_for_on_unsafe(l); |
| 726 |
} |
| 727 |
|
| 728 |
/* This looks in c_env->macros so it is unlike other c_*_ref functions. */ |
| 729 |
void |
| 730 |
c_mac_ref(cl_env_ptr env, cl_object name) |
| 731 |
{ |
| 732 |
const cl_compiler_ptr c_env = env->c_env; |
| 733 |
int function_boundary_crossed = 0; |
| 734 |
cl_object l = c_env->macros; |
| 735 |
loop_for_on_unsafe(l) { |
| 736 |
cl_object record = ECL_CONS_CAR(l), reg, type, other; |
| 737 |
if (record == @'si::function-boundary') |
| 738 |
function_boundary_crossed++; |
| 739 |
if(ECL_ATOM(record)) |
| 740 |
continue; |
| 741 |
reg = record; |
| 742 |
type = pop(®); |
| 743 |
other = pop(®); |
| 744 |
if (type == name) { |
| 745 |
if(other == @':function') |
| 746 |
return; |
| 747 |
if(other == @'si::macro') { |
| 748 |
if (function_boundary_crossed) { |
| 749 |
c_register_captured(env, record); |
| 750 |
} else { |
| 751 |
cl_object mfun = ECL_CONS_CAR(reg); |
| 752 |
if (ecl_t_of(mfun) == t_bclosure) { |
| 753 |
close_around_macros(env, mfun); |
| 754 |
} |
| 755 |
} |
| 756 |
return; |
| 757 |
} |
| 758 |
} |
| 759 |
} end_loop_for_on_unsafe(l); |
| 760 |
} |
| 761 |
|
| 762 |
/* Computes the local index from the function boundary. */ |
| 763 |
static cl_fixnum |
| 764 |
c_lcl_idx(cl_env_ptr env, cl_object entry) |
| 765 |
{ |
| 766 |
cl_fixnum n = 0, i = -1; |
| 767 |
const cl_compiler_ptr c_env = env->c_env; |
| 768 |
cl_object l = c_env->variables; |
| 769 |
loop_for_on_unsafe(l) { |
| 770 |
cl_object record = ECL_CONS_CAR(l), type; |
| 771 |
if (record == @'si::function-boundary') { |
| 772 |
break; |
| 773 |
} |
| 774 |
if(ECL_ATOM(record)) |
| 775 |
continue; |
| 776 |
if(record == entry) { |
| 777 |
i = n; |
| 778 |
continue; |
| 779 |
} |
| 780 |
type = pop(&record); |
| 781 |
if (type == @':block' || type == @':function' || type == @':tag' |
| 782 |
/* type == @'variable' && Null(specialp) */ |
| 783 |
|| Null(pop(&record))) { |
| 784 |
n++; |
| 785 |
} |
| 786 |
} end_loop_for_on_unsafe(l); |
| 787 |
if (i<0) ecl_miscompilation_error(); |
| 788 |
return n-i; |
| 789 |
} |
| 790 |
|
| 791 |
/* This function is called after we compile lambda in the parent's |
| 792 |
environment. Its responsibility is to propagate closures. */ |
| 793 |
static struct cl_compiler_ref |
| 794 |
c_any_ref(cl_env_ptr env, cl_object entry) |
| 795 |
{ |
| 796 |
int function_boundary_crossed = 0; |
| 797 |
struct cl_compiler_ref output = { ECL_CMPREF_UNDEFINED }; |
| 798 |
const cl_compiler_ptr c_env = env->c_env; |
| 799 |
cl_object l = c_env->variables; |
| 800 |
loop_for_on_unsafe(l) { |
| 801 |
cl_object record = ECL_CONS_CAR(l); |
| 802 |
if (record == @'si::function-boundary') |
| 803 |
function_boundary_crossed++; |
| 804 |
if(ECL_ATOM(record)) |
| 805 |
continue; |
| 806 |
if(record == entry) { |
| 807 |
if (function_boundary_crossed) { |
| 808 |
output.place = ECL_CMPREF_CLOSE; |
| 809 |
output.index = c_register_captured(env, record); |
| 810 |
} else { |
| 811 |
output.place = ECL_CMPREF_LOCAL; |
| 812 |
output.index = c_lcl_idx(env, record); |
| 813 |
} |
| 814 |
output.entry = record; |
| 815 |
return output; |
| 816 |
} |
| 817 |
} end_loop_for_on_unsafe(l); |
| 818 |
return output; |
| 819 |
} |
| 820 |
|
| 821 |
static struct cl_compiler_ref |
| 822 |
c_tag_ref(cl_env_ptr env, cl_object the_tag) |
| 823 |
{ |
| 824 |
cl_object l, reg; |
| 825 |
int function_boundary_crossed = 0; |
| 826 |
struct cl_compiler_ref output = { ECL_CMPREF_UNDEFINED }; |
| 827 |
const cl_compiler_ptr c_env = env->c_env; |
| 828 |
for (l = c_env->variables; CONSP(l); l = ECL_CONS_CDR(l)) { |
| 829 |
cl_object type, all_tags, record = ECL_CONS_CAR(l); |
| 830 |
if (record == @'si::function-boundary') |
| 831 |
function_boundary_crossed++; |
| 832 |
if (ECL_ATOM(record)) |
| 833 |
continue; |
| 834 |
reg = record; |
| 835 |
type = pop(®); |
| 836 |
all_tags = pop(®); |
| 837 |
if (type == @':tag') { |
| 838 |
cl_object label = ecl_assql(the_tag, all_tags); |
| 839 |
if (!Null(label)) { |
| 840 |
/* Mark as used */ |
| 841 |
ECL_RPLACA(reg, ECL_T); |
| 842 |
if (function_boundary_crossed) { |
| 843 |
output.place = ECL_CMPREF_CLOSE; |
| 844 |
output.index = c_register_captured(env, record); |
| 845 |
} else { |
| 846 |
output.place = ECL_CMPREF_LOCAL; |
| 847 |
output.index = c_lcl_idx(env, record); |
| 848 |
} |
| 849 |
output.entry = record; |
| 850 |
output.label = ecl_fixnum(ECL_CONS_CDR(label)); |
| 851 |
return output; |
| 852 |
} |
| 853 |
} |
| 854 |
} |
| 855 |
return output; |
| 856 |
} |
| 857 |
|
| 858 |
static struct cl_compiler_ref |
| 859 |
c_blk_ref(cl_env_ptr env, cl_object the_tag) |
| 860 |
{ |
| 861 |
cl_object l, reg; |
| 862 |
int function_boundary_crossed = 0; |
| 863 |
struct cl_compiler_ref output = { ECL_CMPREF_UNDEFINED }; |
| 864 |
const cl_compiler_ptr c_env = env->c_env; |
| 865 |
for (l = c_env->variables; CONSP(l); l = ECL_CONS_CDR(l)) { |
| 866 |
cl_object type, name, record = ECL_CONS_CAR(l); |
| 867 |
if (record == @'si::function-boundary') |
| 868 |
function_boundary_crossed++; |
| 869 |
if (ECL_ATOM(record)) |
| 870 |
continue; |
| 871 |
reg = record; |
| 872 |
type = pop(®); |
| 873 |
name = pop(®); |
| 874 |
if (type == @':block') { |
| 875 |
if(ecl_eql(name, the_tag)) { |
| 876 |
/* Mark as used */ |
| 877 |
ECL_RPLACA(reg, ECL_T); |
| 878 |
if (function_boundary_crossed) { |
| 879 |
output.place = ECL_CMPREF_CLOSE; |
| 880 |
output.index = c_register_captured(env, record); |
| 881 |
} else { |
| 882 |
output.place = ECL_CMPREF_LOCAL; |
| 883 |
output.index = c_lcl_idx(env, record); |
| 884 |
} |
| 885 |
output.entry = record; |
| 886 |
return output; |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
return output; |
| 891 |
} |
| 892 |
|
| 893 |
static struct cl_compiler_ref |
| 894 |
c_fun_ref(cl_env_ptr env, cl_object the_tag) |
| 895 |
{ |
| 896 |
cl_object l, reg; |
| 897 |
int function_boundary_crossed = 0; |
| 898 |
struct cl_compiler_ref output = { ECL_CMPREF_UNDEFINED }; |
| 899 |
const cl_compiler_ptr c_env = env->c_env; |
| 900 |
for (l = c_env->variables; CONSP(l); l = ECL_CONS_CDR(l)) { |
| 901 |
cl_object type, name, record = ECL_CONS_CAR(l); |
| 902 |
if (record == @'si::function-boundary') |
| 903 |
function_boundary_crossed++; |
| 904 |
if (ECL_ATOM(record)) |
| 905 |
continue; |
| 906 |
reg = record; |
| 907 |
type = pop(®); |
| 908 |
name = pop(®); |
| 909 |
if (type == @':function') { |
| 910 |
/* We compare with EQUAL, because of (SETF fname) */ |
| 911 |
if(ecl_equal(name, the_tag)) { |
| 912 |
/* Mark as used */ |
| 913 |
ECL_RPLACA(reg, ECL_T); |
| 914 |
if (function_boundary_crossed) { |
| 915 |
output.place = ECL_CMPREF_CLOSE; |
| 916 |
output.index = c_register_captured(env, record); |
| 917 |
} else { |
| 918 |
output.place = ECL_CMPREF_LOCAL; |
| 919 |
output.index = c_lcl_idx(env, record); |
| 920 |
} |
| 921 |
output.entry = record; |
| 922 |
return output; |
| 923 |
} |
| 924 |
} |
| 925 |
} |
| 926 |
return output; |
| 927 |
} |
| 928 |
|
| 929 |
ecl_def_ct_base_string(undefined_variable, |
| 930 |
"Undefined variable referenced in interpreted code" |
| 931 |
".~%Name: ~A", 60, static, const); |
| 932 |
|
| 933 |
static struct cl_compiler_ref |
| 934 |
c_var_ref(cl_env_ptr env, cl_object var, bool allow_sym_mac, bool ensure_def) |
| 935 |
{ |
| 936 |
cl_object l, reg; |
| 937 |
int function_boundary_crossed = 0; |
| 938 |
struct cl_compiler_ref output; |
| 939 |
output.place = ECL_CMPREF_UNDEFINED; |
| 940 |
output.label = ECL_CMPVAR_UNDEFINED; |
| 941 |
const cl_compiler_ptr c_env = env->c_env; |
| 942 |
for (l = c_env->variables; CONSP(l); l = ECL_CONS_CDR(l)) { |
| 943 |
cl_object type, special, record = ECL_CONS_CAR(l); |
| 944 |
if (record == @'si::function-boundary') |
| 945 |
function_boundary_crossed++; |
| 946 |
if (ECL_ATOM(record)) |
| 947 |
continue; |
| 948 |
reg = record; |
| 949 |
type = pop(®); |
| 950 |
special = pop(®); |
| 951 |
if (type == @':block' || type == @':tag' || type == @':function' |
| 952 |
|| type == @':declare' || type == @':type' || type != var) { |
| 953 |
continue; |
| 954 |
} else if (Null(special)) { |
| 955 |
if (function_boundary_crossed) { |
| 956 |
output.place = ECL_CMPREF_CLOSE; |
| 957 |
output.index = c_register_captured(env, record); |
| 958 |
} else { |
| 959 |
output.place = ECL_CMPREF_LOCAL; |
| 960 |
output.index = c_lcl_idx(env, record); |
| 961 |
} |
| 962 |
output.entry = record; |
| 963 |
output.label = ECL_CMPVAR_LEXICAL; |
| 964 |
return output; |
| 965 |
} else if (special == @'si::symbol-macro') { |
| 966 |
if(!allow_sym_mac) |
| 967 |
FEprogram_error("Internal error: symbol macro ~S used as variable", 1, var); |
| 968 |
/* We can only get here when we try to redefine a symbol macro. */ |
| 969 |
/* We don't close over symbol macros (but we will). */ |
| 970 |
output.place = function_boundary_crossed |
| 971 |
? ECL_CMPREF_CLOSE |
| 972 |
: ECL_CMPREF_LOCAL; |
| 973 |
output.entry = record; |
| 974 |
output.label = ECL_CMPVAR_SYM_MACRO; |
| 975 |
return output; |
| 976 |
} else { |
| 977 |
/* We don't close over special variables. */ |
| 978 |
output.place = function_boundary_crossed |
| 979 |
? ECL_CMPREF_CLOSE |
| 980 |
: ECL_CMPREF_LOCAL; |
| 981 |
output.index = -1; |
| 982 |
output.entry = record; |
| 983 |
output.label = ECL_CMPVAR_SPECIAL; |
| 984 |
return output; |
| 985 |
} |
| 986 |
} |
| 987 |
if (ensure_def) { |
| 988 |
l = ecl_cmp_symbol_value(env, @'ext::*action-on-undefined-variable*'); |
| 989 |
if (l != ECL_NIL) { |
| 990 |
cl_funcall(3, l, undefined_variable, var); |
| 991 |
} |
| 992 |
} |
| 993 |
return output; |
| 994 |
} |
| 995 |
|
| 996 |
/* Depending on whether the variable is special, local or closed over, we emit |
| 997 |
different opcodes since they are handled differently. -- jd 2025-01-07*/ |
| 998 |
static int |
| 999 |
c_var_ref_fix_op(struct cl_compiler_ref ref, int op) { |
| 1000 |
bool special = (ref.label == ECL_CMPVAR_SPECIAL |
| 1001 |
|| ref.label == ECL_CMPVAR_UNDEFINED); |
| 1002 |
bool closure = (!special && ref.place == ECL_CMPREF_CLOSE); |
| 1003 |
if(!special && !closure) return op; |
| 1004 |
switch(op) { |
| 1005 |
/* setters */ |
| 1006 |
case OP_SETQ: return (special ? OP_SETQS : OP_SETQC); |
| 1007 |
case OP_PSETQ: return (special ? OP_PSETQS : OP_PSETQC); |
| 1008 |
case OP_VSETQ: return (special ? OP_VSETQS : OP_VSETQC); |
| 1009 |
/* getters */ |
| 1010 |
case OP_VAR: return (special ? OP_VARS : OP_VARC); |
| 1011 |
case OP_PUSHV: return (special ? OP_PUSHVS : OP_PUSHVC); |
| 1012 |
default: |
| 1013 |
ecl_miscompilation_error(); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
static bool |
| 1018 |
c_declared_special(cl_object var, cl_object specials) |
| 1019 |
{ |
| 1020 |
return ((ecl_symbol_type(var) & ecl_stp_special) || ecl_member_eq(var, specials)); |
| 1021 |
} |
| 1022 |
|
| 1023 |
static void |
| 1024 |
c_declare_specials(cl_env_ptr env, cl_object specials) |
| 1025 |
{ |
| 1026 |
while (!Null(specials)) { |
| 1027 |
cl_object var = pop(&specials); |
| 1028 |
struct cl_compiler_ref ref = c_var_ref(env, var, TRUE, FALSE); |
| 1029 |
switch(ref.label) { |
| 1030 |
case ECL_CMPVAR_UNDEFINED: |
| 1031 |
case ECL_CMPVAR_SYM_MACRO: |
| 1032 |
case ECL_CMPVAR_LEXICAL: |
| 1033 |
c_register_var(env, var, TRUE, FALSE); |
| 1034 |
break; |
| 1035 |
default: |
| 1036 |
break; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
static cl_object |
| 1042 |
c_process_declarations(cl_object body) |
| 1043 |
{ |
| 1044 |
const cl_env_ptr the_env = ecl_process_env(); |
| 1045 |
@si::process-declarations(1, body); |
| 1046 |
body = ecl_nth_value(the_env, 1); |
| 1047 |
return body; |
| 1048 |
} |
| 1049 |
|
| 1050 |
static bool |
| 1051 |
c_pbind(cl_env_ptr env, cl_object var, cl_object specials) |
| 1052 |
{ |
| 1053 |
bool special; |
| 1054 |
if (!ECL_SYMBOLP(var)) |
| 1055 |
FEillegal_variable_name(var); |
| 1056 |
else if ((special = c_declared_special(var, specials))) { |
| 1057 |
c_register_var(env, var, TRUE, TRUE); |
| 1058 |
asm_op2c(env, OP_PBINDS, var); |
| 1059 |
} else { |
| 1060 |
c_register_var(env, var, FALSE, TRUE); |
| 1061 |
asm_op2c(env, OP_PBIND, var); |
| 1062 |
} |
| 1063 |
return special; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static bool |
| 1067 |
c_bind(cl_env_ptr env, cl_object var, cl_object specials) |
| 1068 |
{ |
| 1069 |
bool special; |
| 1070 |
if (!ECL_SYMBOLP(var)) |
| 1071 |
FEillegal_variable_name(var); |
| 1072 |
else if ((special = c_declared_special(var, specials))) { |
| 1073 |
c_register_var(env, var, TRUE, TRUE); |
| 1074 |
asm_op2c(env, OP_BINDS, var); |
| 1075 |
} else { |
| 1076 |
c_register_var(env, var, FALSE, TRUE); |
| 1077 |
asm_op2c(env, OP_BIND, var); |
| 1078 |
} |
| 1079 |
return special; |
| 1080 |
} |
| 1081 |
|
| 1082 |
static void |
| 1083 |
c_undo_bindings(cl_env_ptr the_env, cl_object old_vars, int only_specials) |
| 1084 |
{ |
| 1085 |
cl_object env; |
| 1086 |
cl_index num_lexical = 0; |
| 1087 |
cl_index num_special = 0; |
| 1088 |
const cl_compiler_ptr c_env = the_env->c_env; |
| 1089 |
for (env = c_env->variables; env != old_vars && !Null(env); env = ECL_CONS_CDR(env)) |
| 1090 |
{ |
| 1091 |
cl_object record, name, special, boundp; |
| 1092 |
record = ECL_CONS_CAR(env); |
| 1093 |
if (ECL_ATOM(record)) |
| 1094 |
continue; |
| 1095 |
name = ECL_CONS_CAR(record); |
| 1096 |
record = ECL_CONS_CDR(record); |
| 1097 |
special = ECL_CONS_CAR(record); |
| 1098 |
if (name == @':block' || name == @':tag') { |
| 1099 |
if (!only_specials) num_lexical++; |
| 1100 |
} else if (name == @':function' || Null(special)) { |
| 1101 |
if (!only_specials) num_lexical++; |
| 1102 |
} else if (name == @':declare' || name == @':type') { |
| 1103 |
/* Ignored */ |
| 1104 |
} else if (special != @'si::symbol-macro') { |
| 1105 |
/* If (third special) = NIL, the variable was declared |
| 1106 |
special, but there is no binding! */ |
| 1107 |
record = ECL_CONS_CDR(record); |
| 1108 |
boundp = ECL_CONS_CAR(record); |
| 1109 |
if (!Null(boundp)) { |
| 1110 |
num_special++; |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
c_env->variables = env; |
| 1115 |
if (num_lexical) { |
| 1116 |
c_env->env_size -= num_lexical; |
| 1117 |
asm_op2(the_env, OP_UNBIND, num_lexical); |
| 1118 |
} |
| 1119 |
if (num_special) asm_op2(the_env, OP_UNBINDS, num_special); |
| 1120 |
} |
| 1121 |
|
| 1122 |
static void |
| 1123 |
compile_setq(cl_env_ptr env, int op, cl_object var) |
| 1124 |
{ |
| 1125 |
cl_index ndx; |
| 1126 |
struct cl_compiler_ref ref; |
| 1127 |
if (!ECL_SYMBOLP(var)) |
| 1128 |
FEillegal_variable_name(var); |
| 1129 |
ref = c_var_ref(env, var,FALSE,TRUE); |
| 1130 |
ndx = ref.index; |
| 1131 |
switch(ref.label) { |
| 1132 |
case ECL_CMPVAR_SPECIAL: |
| 1133 |
case ECL_CMPVAR_UNDEFINED: |
| 1134 |
if (ecl_symbol_type(var) & ecl_stp_constant) { |
| 1135 |
FEassignment_to_constant(var); |
| 1136 |
} |
| 1137 |
ndx = c_register_constant(env, var); |
| 1138 |
/* fall through */ |
| 1139 |
default: |
| 1140 |
op = c_var_ref_fix_op(ref, op); |
| 1141 |
break; |
| 1142 |
} |
| 1143 |
asm_op2(env, op, ndx); |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* |
| 1147 |
* This routine is used to change the compilation flags in optimizers |
| 1148 |
* that do not want to push values onto the stack. Its purpose is to |
| 1149 |
* keep ignorable forms ignored, while preserving the value of useful |
| 1150 |
* forms. Qualitative behavior: |
| 1151 |
* FLAG_PUSH -> FLAG_VALUES |
| 1152 |
* FLAG_VALUES -> FLAG_VALUES |
| 1153 |
* FLAG_REG0 -> FLAG_REG0 |
| 1154 |
* FLAG_IGNORE -> FLAG_IGNORE |
| 1155 |
*/ |
| 1156 |
static int |
| 1157 |
maybe_values_or_reg0(int flags) { |
| 1158 |
if (flags & FLAG_PUSH) |
| 1159 |
return (flags | FLAG_VALUES) & ~FLAG_PUSH; |
| 1160 |
else |
| 1161 |
return flags; |
| 1162 |
} |
| 1163 |
|
| 1164 |
/* |
| 1165 |
* This routine is used to change the compilation flags in optimizers |
| 1166 |
* that do not want to push values onto the stack, but also do not want |
| 1167 |
* to use REG0 (maybe because the call a nested ecl_interpret()). Ignorable |
| 1168 |
* forms are kept ignored: |
| 1169 |
* FLAG_PUSH -> FLAG_VALUES |
| 1170 |
* FLAG_VALUES -> FLAG_VALUES |
| 1171 |
* FLAG_REG0 -> FLAG_VALUES |
| 1172 |
* FLAG_IGNORE -> FLAG_IGNORE |
| 1173 |
*/ |
| 1174 |
static int |
| 1175 |
maybe_values(int flags) { |
| 1176 |
if (flags & FLAG_USEFUL) |
| 1177 |
return (flags & ~(FLAG_PUSH | FLAG_REG0)) | FLAG_VALUES; |
| 1178 |
else |
| 1179 |
return flags; |
| 1180 |
} |
| 1181 |
|
| 1182 |
/* |
| 1183 |
* This routine is used to change the compilation flags in optimizers |
| 1184 |
* that do not want to push values onto the stack. Its purpose is to |
| 1185 |
* keep ignorable forms ignored, while preserving the value of useful |
| 1186 |
* forms. Qualitative behavior: |
| 1187 |
* FLAG_PUSH -> FLAG_REG0 |
| 1188 |
* FLAG_VALUES -> FLAG_REG0 |
| 1189 |
* FLAG_REG0 -> FLAG_REG0 |
| 1190 |
* FLAG_IGNORE -> FLAG_IGNORE |
| 1191 |
*/ |
| 1192 |
static int |
| 1193 |
maybe_reg0(int flags) { |
| 1194 |
if (flags & FLAG_USEFUL) |
| 1195 |
return (flags & ~(FLAG_VALUES | FLAG_PUSH)) | FLAG_REG0; |
| 1196 |
else |
| 1197 |
return flags; |
| 1198 |
} |
| 1199 |
|
| 1200 |
/* -------------------- THE COMPILER -------------------- */ |
| 1201 |
|
| 1202 |
/* |
| 1203 |
The OP_BLOCK operator encloses several forms within a block |
| 1204 |
named BLOCK_NAME, thus catching any OP_RETFROM whose argument |
| 1205 |
matches BLOCK_NAME. The end of this block is marked both by |
| 1206 |
the OP_EXIT operator and the LABELZ which is packed within |
| 1207 |
the OP_BLOCK operator. |
| 1208 |
|
| 1209 |
[OP_BLOCK + name + labelz] |
| 1210 |
.... |
| 1211 |
OP_EXIT_FRAME |
| 1212 |
labelz: ... |
| 1213 |
*/ |
| 1214 |
|
| 1215 |
static int |
| 1216 |
c_block(cl_env_ptr env, cl_object body, int old_flags) { |
| 1217 |
struct cl_compiler_env old_env; |
| 1218 |
cl_object name = pop(&body); |
| 1219 |
cl_object block_record; |
| 1220 |
cl_index labelz, pc, constants; |
| 1221 |
int flags; |
| 1222 |
|
| 1223 |
if (!ECL_SYMBOLP(name)) |
| 1224 |
FEprogram_error("BLOCK: Not a valid block name, ~S", 1, name); |
| 1225 |
|
| 1226 |
old_env = *(env->c_env); |
| 1227 |
constants = old_env.constants->vector.fillp; |
| 1228 |
pc = current_pc(env); |
| 1229 |
|
| 1230 |
flags = maybe_values_or_reg0(old_flags); |
| 1231 |
c_register_block(env, name); |
| 1232 |
block_record = ECL_CONS_CAR(env->c_env->variables); |
| 1233 |
if (Null(name)) { |
| 1234 |
asm_op(env, OP_DO); |
| 1235 |
} else { |
| 1236 |
asm_op2c(env, OP_BLOCK, name); |
| 1237 |
} |
| 1238 |
labelz = asm_jmp(env, OP_FRAME); |
| 1239 |
compile_body(env, body, flags); |
| 1240 |
if (CADDR(block_record) == ECL_NIL) { |
| 1241 |
/* Block unused. We remove the enclosing OP_BLOCK/OP_DO */ |
| 1242 |
/* We also have to remove the constants we compiled, because */ |
| 1243 |
/* some of them might be from load-time-value */ |
| 1244 |
old_env.constants->vector.fillp = constants; |
| 1245 |
*(env->c_env) = old_env; |
| 1246 |
set_pc(env, pc); |
| 1247 |
return compile_body(env, body, old_flags); |
| 1248 |
} else { |
| 1249 |
c_undo_bindings(env, old_env.variables, 0); |
| 1250 |
asm_op(env, OP_EXIT_FRAME); |
| 1251 |
asm_complete(env, 0, labelz); |
| 1252 |
return flags; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
/* |
| 1257 |
There are several ways to invoke functions and to handle the |
| 1258 |
output arguments. These are |
| 1259 |
|
| 1260 |
[OP_CALL + nargs] |
| 1261 |
function_name |
| 1262 |
|
| 1263 |
[OP_FCALL + nargs] |
| 1264 |
|
| 1265 |
OP_CALL and OP_FCALL leave all arguments in the VALUES() array, |
| 1266 |
while OP_PCALL and OP_PFCALL leave the first argument in the |
| 1267 |
stack. |
| 1268 |
|
| 1269 |
OP_CALL and OP_PCALL use the value in VALUES(0) to retrieve the |
| 1270 |
function, while OP_FCALL and OP_PFCALL use a value from the |
| 1271 |
stack. |
| 1272 |
*/ |
| 1273 |
static int |
| 1274 |
c_arguments(cl_env_ptr env, cl_object args) { |
| 1275 |
cl_index nargs; |
| 1276 |
for (nargs = 0; !Null(args); nargs++) { |
| 1277 |
compile_form(env, pop(&args), FLAG_PUSH); |
| 1278 |
} |
| 1279 |
return nargs; |
| 1280 |
} |
| 1281 |
|
| 1282 |
static int |
| 1283 |
c_call(cl_env_ptr env, cl_object args, int flags) { |
| 1284 |
cl_object name; |
| 1285 |
cl_index nargs; |
| 1286 |
|
| 1287 |
name = pop(&args); |
| 1288 |
if (name >= (cl_object)cl_symbols |
| 1289 |
&& name < (cl_object)(cl_symbols + cl_num_symbols_in_core)) |
| 1290 |
{ |
| 1291 |
cl_object f = ECL_SYM_FUN(name); |
| 1292 |
cl_type t = (!ECL_FBOUNDP(name))? t_other : ecl_t_of(f); |
| 1293 |
if (t == t_cfunfixed) { |
| 1294 |
cl_index n = ecl_length(args); |
| 1295 |
if (f->cfun.narg == 1 && n == 1) { |
| 1296 |
compile_form(env, ECL_CONS_CAR(args), FLAG_REG0); |
| 1297 |
asm_op2c(env, OP_CALLG1, name); |
| 1298 |
return FLAG_VALUES; |
| 1299 |
} else if (f->cfun.narg == 2 && n == 2) { |
| 1300 |
compile_form(env, ECL_CONS_CAR(args), FLAG_PUSH); |
| 1301 |
args = ECL_CONS_CDR(args); |
| 1302 |
compile_form(env, ECL_CONS_CAR(args), FLAG_REG0); |
| 1303 |
asm_op2c(env, OP_CALLG2, name); |
| 1304 |
return FLAG_VALUES; |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
nargs = c_arguments(env, args); |
| 1309 |
if (env->c_env->stepping) { |
| 1310 |
/* When stepping, we only have one opcode to do function |
| 1311 |
* calls: OP_STEPFCALL. */ |
| 1312 |
asm_function(env, name, (flags & FLAG_GLOBAL) | FLAG_REG0); |
| 1313 |
asm_op2(env, OP_STEPCALL, nargs); |
| 1314 |
flags = FLAG_VALUES; |
| 1315 |
} else if (ECL_SYMBOLP(name) && |
| 1316 |
((flags & FLAG_GLOBAL) || |
| 1317 |
c_fun_ref(env, name).place == ECL_CMPREF_UNDEFINED)) |
| 1318 |
{ |
| 1319 |
asm_op2(env, OP_CALLG, nargs); |
| 1320 |
asm_arg_data(env, name); |
| 1321 |
flags = FLAG_VALUES; |
| 1322 |
} else { |
| 1323 |
/* Fixme!! We can optimize the case of global functions! */ |
| 1324 |
asm_function(env, name, (flags & FLAG_GLOBAL) | FLAG_REG0); |
| 1325 |
asm_op2(env, OP_CALL, nargs); |
| 1326 |
flags = FLAG_VALUES; |
| 1327 |
} |
| 1328 |
return flags; |
| 1329 |
} |
| 1330 |
|
| 1331 |
static int |
| 1332 |
c_funcall(cl_env_ptr env, cl_object args, int flags) { |
| 1333 |
cl_object name; |
| 1334 |
cl_index nargs; |
| 1335 |
|
| 1336 |
name = pop(&args); |
| 1337 |
if (CONSP(name)) { |
| 1338 |
cl_object kind = ECL_CONS_CAR(name); |
| 1339 |
if (kind == @'function') { |
| 1340 |
if (cl_list_length(name) != ecl_make_fixnum(2)) |
| 1341 |
FEprogram_error("FUNCALL: Invalid function name ~S.", 1, name); |
| 1342 |
return c_call(env, CONS(CADR(name), args), flags); |
| 1343 |
} |
| 1344 |
if (kind == @'quote') { |
| 1345 |
if (cl_list_length(name) != ecl_make_fixnum(2)) |
| 1346 |
FEprogram_error("FUNCALL: Invalid function name ~S.", 1, name); |
| 1347 |
return c_call(env, CONS(CADR(name), args), flags | FLAG_GLOBAL); |
| 1348 |
} |
| 1349 |
} |
| 1350 |
compile_form(env, name, FLAG_PUSH); |
| 1351 |
nargs = c_arguments(env, args); |
| 1352 |
if (env->c_env->stepping) { |
| 1353 |
asm_op2(env, OP_STEPCALL, nargs); |
| 1354 |
flags = FLAG_VALUES; |
| 1355 |
} else { |
| 1356 |
asm_op2(env, OP_FCALL, nargs); |
| 1357 |
flags = FLAG_VALUES; |
| 1358 |
} |
| 1359 |
asm_op(env, OP_POP1); |
| 1360 |
return flags; |
| 1361 |
} |
| 1362 |
|
| 1363 |
static int |
| 1364 |
perform_c_case(cl_env_ptr env, cl_object args, int flags) { |
| 1365 |
cl_object test, clause; |
| 1366 |
|
| 1367 |
do { |
| 1368 |
if (Null(args)) |
| 1369 |
return compile_body(env, ECL_NIL, flags); |
| 1370 |
clause = pop(&args); |
| 1371 |
if (ECL_ATOM(clause)) |
| 1372 |
FEprogram_error("CASE: Illegal clause ~S.",1,clause); |
| 1373 |
test = pop(&clause); |
| 1374 |
} while (test == ECL_NIL); |
| 1375 |
|
| 1376 |
if (@'otherwise' == test || test == ECL_T) { |
| 1377 |
unlikely_if (args != ECL_NIL) { |
| 1378 |
FEprogram_error("CASE: The selector ~A can only appear at the last position.", |
| 1379 |
1, test); |
| 1380 |
} |
| 1381 |
compile_body(env, clause, flags); |
| 1382 |
} else { |
| 1383 |
cl_index labeln, labelz; |
| 1384 |
if (CONSP(test)) { |
| 1385 |
cl_index n = ecl_length(test); |
| 1386 |
while (n-- > 1) { |
| 1387 |
cl_object v = pop(&test); |
| 1388 |
asm_op(env, OP_JEQL); |
| 1389 |
maybe_make_load_forms(env, v); |
| 1390 |
asm_arg_data(env, v); |
| 1391 |
asm_arg(env, n * (OPCODE_SIZE + OPARG_SIZE * 2) |
| 1392 |
+ OPARG_SIZE); |
| 1393 |
} |
| 1394 |
test = ECL_CONS_CAR(test); |
| 1395 |
} |
| 1396 |
asm_op(env, OP_JNEQL); |
| 1397 |
maybe_make_load_forms(env, test); |
| 1398 |
asm_arg_data(env, test); |
| 1399 |
labeln = current_pc(env); |
| 1400 |
asm_arg(env, 0); |
| 1401 |
compile_body(env, clause, flags); |
| 1402 |
if (Null(args) && !(flags & FLAG_USEFUL)) { |
| 1403 |
/* Ther is no otherwise. The test has failed and |
| 1404 |
we need no output value. We simply close jumps. */ |
| 1405 |
asm_complete(env, 0 & OP_JNEQL, labeln); |
| 1406 |
} else { |
| 1407 |
labelz = asm_jmp(env, OP_JMP); |
| 1408 |
asm_complete(env, 0 & OP_JNEQL, labeln); |
| 1409 |
perform_c_case(env, args, flags); |
| 1410 |
asm_complete(env, OP_JMP, labelz); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
return flags; |
| 1414 |
} |
| 1415 |
|
| 1416 |
static int |
| 1417 |
c_case(cl_env_ptr env, cl_object clause, int flags) { |
| 1418 |
compile_form(env, pop(&clause), FLAG_REG0); |
| 1419 |
return perform_c_case(env, clause, maybe_values_or_reg0(flags)); |
| 1420 |
} |
| 1421 |
|
| 1422 |
/* |
| 1423 |
The OP_CATCH takes the object in VALUES(0) and uses it to catch |
| 1424 |
any OP_THROW operation which uses that value as argument. If a |
| 1425 |
catch occurs, or when all forms have been properly executed, it |
| 1426 |
jumps to LABELZ. LABELZ is packed within the OP_CATCH operator. |
| 1427 |
[OP_CATCH + labelz] |
| 1428 |
... |
| 1429 |
"forms to be caught" |
| 1430 |
... |
| 1431 |
OP_EXIT_FRAME |
| 1432 |
labelz: ... |
| 1433 |
*/ |
| 1434 |
|
| 1435 |
static int |
| 1436 |
c_catch(cl_env_ptr env, cl_object args, int flags) { |
| 1437 |
cl_index labelz; |
| 1438 |
cl_object old_env; |
| 1439 |
|
| 1440 |
/* Compile evaluation of tag */ |
| 1441 |
compile_form(env, pop(&args), FLAG_REG0); |
| 1442 |
|
| 1443 |
/* Compile binding of tag */ |
| 1444 |
old_env = env->c_env->variables; |
| 1445 |
c_register_block(env, ecl_make_fixnum(0)); |
| 1446 |
asm_op(env, OP_CATCH); |
| 1447 |
|
| 1448 |
/* Compile jump point */ |
| 1449 |
labelz = asm_jmp(env, OP_FRAME); |
| 1450 |
|
| 1451 |
/* Compile body of CATCH */ |
| 1452 |
compile_body(env, args, FLAG_VALUES); |
| 1453 |
|
| 1454 |
c_undo_bindings(env, old_env, 0); |
| 1455 |
asm_op(env, OP_EXIT_FRAME); |
| 1456 |
asm_complete(env, 0, labelz); |
| 1457 |
|
| 1458 |
return FLAG_VALUES; |
| 1459 |
} |
| 1460 |
|
| 1461 |
static int |
| 1462 |
c_compiler_let(cl_env_ptr env, cl_object args, int flags) { |
| 1463 |
cl_object bindings; |
| 1464 |
cl_index old_bds_ndx = env->bds_stack.top - env->bds_stack.org; |
| 1465 |
|
| 1466 |
for (bindings = pop(&args); !Null(bindings); ) { |
| 1467 |
cl_object form = pop(&bindings); |
| 1468 |
cl_object var = pop(&form); |
| 1469 |
cl_object value = pop_maybe_nil(&form); |
| 1470 |
ecl_bds_bind(env, var, value); |
| 1471 |
} |
| 1472 |
flags = compile_toplevel_body(env, args, flags); |
| 1473 |
ecl_bds_unwind(env, old_bds_ndx); |
| 1474 |
return flags; |
| 1475 |
} |
| 1476 |
|
| 1477 |
/* |
| 1478 |
There are three operators which perform explicit jumps, but |
| 1479 |
almost all other operators use labels in one way or |
| 1480 |
another. |
| 1481 |
|
| 1482 |
1) Jumps are always relative to the place where the jump label |
| 1483 |
is retrieved so that if the label is in vector[0], then the |
| 1484 |
destination is roughly vector + vector[0]. |
| 1485 |
|
| 1486 |
2) The three jump forms are |
| 1487 |
|
| 1488 |
[OP_JMP + label] ; Unconditional jump |
| 1489 |
[OP_JNIL + label] ; Jump if VALUES(0) == ECL_NIL |
| 1490 |
[OP_JT + label] ; Jump if VALUES(0) != ECL_NIL |
| 1491 |
|
| 1492 |
It is important to remark that both OP_JNIL and OP_JT truncate |
| 1493 |
the values stack, so that always NVALUES = 1 after performing |
| 1494 |
any of these operations. |
| 1495 |
*/ |
| 1496 |
static int |
| 1497 |
c_cond(cl_env_ptr env, cl_object args, int flags) { |
| 1498 |
cl_object test, clause; |
| 1499 |
cl_index label_nil, label_exit; |
| 1500 |
|
| 1501 |
if (Null(args)) |
| 1502 |
return compile_form(env, ECL_NIL, flags); |
| 1503 |
clause = pop(&args); |
| 1504 |
if (ECL_ATOM(clause)) |
| 1505 |
FEprogram_error("COND: Illegal clause ~S.",1,clause); |
| 1506 |
test = pop(&clause); |
| 1507 |
flags = maybe_values_or_reg0(flags); |
| 1508 |
if (ECL_T == test) { |
| 1509 |
/* Default sentence. If no forms, just output T. */ |
| 1510 |
if (Null(clause)) |
| 1511 |
compile_form(env, ECL_T, flags); |
| 1512 |
else |
| 1513 |
compile_body(env, clause, flags); |
| 1514 |
} else { |
| 1515 |
/* Compile the test. If no more forms, just output |
| 1516 |
the first value (this is guaranteed by OP_JT), but make |
| 1517 |
sure it is stored in the appropriate place. */ |
| 1518 |
if (Null(args)) { |
| 1519 |
if (Null(clause)) { |
| 1520 |
c_values(env, cl_list(1,test), flags); |
| 1521 |
} else { |
| 1522 |
compile_form(env, test, FLAG_REG0); |
| 1523 |
if (flags & FLAG_VALUES) asm_op(env, OP_VALUEREG0); |
| 1524 |
label_nil = asm_jmp(env, OP_JNIL); |
| 1525 |
compile_body(env, clause, flags); |
| 1526 |
asm_complete(env, OP_JNIL, label_nil); |
| 1527 |
} |
| 1528 |
} else if (Null(clause)) { |
| 1529 |
compile_form(env, test, FLAG_REG0); |
| 1530 |
if (flags & FLAG_VALUES) asm_op(env, OP_VALUEREG0); |
| 1531 |
label_exit = asm_jmp(env, OP_JT); |
| 1532 |
c_cond(env, args, flags); |
| 1533 |
asm_complete(env, OP_JT, label_exit); |
| 1534 |
} else { |
| 1535 |
compile_form(env, test, FLAG_REG0); |
| 1536 |
label_nil = asm_jmp(env, OP_JNIL); |
| 1537 |
compile_body(env, clause, flags); |
| 1538 |
label_exit = asm_jmp(env, OP_JMP); |
| 1539 |
asm_complete(env, OP_JNIL, label_nil); |
| 1540 |
c_cond(env, args, flags); |
| 1541 |
asm_complete(env, OP_JMP, label_exit); |
| 1542 |
} |
| 1543 |
} |
| 1544 |
return flags; |
| 1545 |
} |
| 1546 |
|
| 1547 |
/* The OP_DO operator saves the lexical environment and establishes |
| 1548 |
a NIL block to execute the enclosed forms, which are typically |
| 1549 |
like the ones shown below. At the exit of the block, either by |
| 1550 |
means of a OP_RETFROM jump or because of normal termination, |
| 1551 |
the lexical environment is restored, and all bindings undone. |
| 1552 |
|
| 1553 |
[OP_DO + labelz] |
| 1554 |
... ; bindings |
| 1555 |
[JMP + labelt] |
| 1556 |
labelb: ... ; body |
| 1557 |
... ; stepping forms |
| 1558 |
labelt: ... ; test form |
| 1559 |
[JNIL + label] |
| 1560 |
... ; output form |
| 1561 |
OP_EXIT_FRAME |
| 1562 |
labelz: |
| 1563 |
|
| 1564 |
*/ |
| 1565 |
static int |
| 1566 |
c_while_until(cl_env_ptr env, cl_object body, int flags, bool is_while) { |
| 1567 |
cl_object test = pop(&body); |
| 1568 |
cl_index labelt, labelb; |
| 1569 |
|
| 1570 |
flags = maybe_reg0(flags); |
| 1571 |
|
| 1572 |
/* Jump to test */ |
| 1573 |
labelt = asm_jmp(env, OP_JMP); |
| 1574 |
|
| 1575 |
/* Compile body */ |
| 1576 |
labelb = current_pc(env); |
| 1577 |
c_tagbody(env, body, flags); |
| 1578 |
|
| 1579 |
/* Compile test */ |
| 1580 |
asm_complete(env, OP_JMP, labelt); |
| 1581 |
compile_form(env, test, FLAG_REG0); |
| 1582 |
asm_op(env, is_while? OP_JT : OP_JNIL); |
| 1583 |
asm_arg(env, labelb - current_pc(env)); |
| 1584 |
|
| 1585 |
return flags; |
| 1586 |
} |
| 1587 |
|
| 1588 |
static int |
| 1589 |
c_while(cl_env_ptr env, cl_object body, int flags) { |
| 1590 |
return c_while_until(env, body, flags, 1); |
| 1591 |
} |
| 1592 |
|
| 1593 |
static int |
| 1594 |
c_until(cl_env_ptr env, cl_object body, int flags) { |
| 1595 |
return c_while_until(env, body, flags, 0); |
| 1596 |
} |
| 1597 |
|
| 1598 |
static int |
| 1599 |
c_with_backend(cl_env_ptr env, cl_object args, int flags) |
| 1600 |
{ |
| 1601 |
cl_object forms = ECL_NIL; |
| 1602 |
while (!Null(args)) { |
| 1603 |
cl_object tag = pop(&args); |
| 1604 |
cl_object form = pop(&args); |
| 1605 |
if (tag == @':bytecodes') |
| 1606 |
forms = CONS(form, forms); |
| 1607 |
} |
| 1608 |
return compile_toplevel_body(env, forms, flags); |
| 1609 |
} |
| 1610 |
|
| 1611 |
static int |
| 1612 |
eval_when_flags(cl_object situation) |
| 1613 |
{ |
| 1614 |
int code = 0; |
| 1615 |
cl_object p; |
| 1616 |
for (p = situation; p != ECL_NIL; p = ECL_CONS_CDR(p)) { |
| 1617 |
cl_object keyword; |
| 1618 |
unlikely_if (!ECL_LISTP(p)) |
| 1619 |
FEtype_error_proper_list(situation); |
| 1620 |
keyword = ECL_CONS_CAR(p); |
| 1621 |
if (keyword == @'load') |
| 1622 |
code |= FLAG_LOAD; |
| 1623 |
else if (keyword == @':load-toplevel') |
| 1624 |
code |= FLAG_LOAD; |
| 1625 |
else if (keyword == @'compile') |
| 1626 |
code |= FLAG_COMPILE; |
| 1627 |
else if (keyword == @':compile-toplevel') |
| 1628 |
code |= FLAG_COMPILE; |
| 1629 |
else if (keyword == @'eval') |
| 1630 |
code |= FLAG_EXECUTE; |
| 1631 |
else if (keyword == @':execute') |
| 1632 |
code |= FLAG_EXECUTE; |
| 1633 |
} |
| 1634 |
return code; |
| 1635 |
} |
| 1636 |
|
| 1637 |
#define when_load_p(s) ((s) & FLAG_LOAD) |
| 1638 |
#define when_compile_p(s) ((s) & FLAG_COMPILE) |
| 1639 |
#define when_execute_p(s) ((s) & FLAG_EXECUTE) |
| 1640 |
|
| 1641 |
static int |
| 1642 |
c_eval_when(cl_env_ptr env, cl_object args, int flags) { |
| 1643 |
cl_object situation_list = pop(&args); |
| 1644 |
int situation = eval_when_flags(situation_list); |
| 1645 |
const cl_compiler_ptr c_env = env->c_env; |
| 1646 |
int mode = c_env->mode; |
| 1647 |
if (c_env->lexical_level || mode == FLAG_EXECUTE) { |
| 1648 |
if (!when_execute_p(situation)) |
| 1649 |
args = ECL_NIL; |
| 1650 |
} else if (when_load_p(situation)) { |
| 1651 |
if (when_compile_p(situation)) { |
| 1652 |
int current_mode = c_env->mode; |
| 1653 |
c_env->mode = FLAG_EXECUTE; |
| 1654 |
execute_each_form(env, args); |
| 1655 |
c_env->mode = current_mode; |
| 1656 |
} |
| 1657 |
} else if (when_compile_p(situation)) { |
| 1658 |
int current_mode = c_env->mode; |
| 1659 |
c_env->mode = FLAG_EXECUTE; |
| 1660 |
execute_each_form(env, args); |
| 1661 |
c_env->mode = current_mode; |
| 1662 |
args = ECL_NIL; |
| 1663 |
} else { |
| 1664 |
args = ECL_NIL; |
| 1665 |
} |
| 1666 |
return compile_toplevel_body(env, args, flags); |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
/* |
| 1671 |
The OP_FLET/OP_FLABELS operators change the lexical environment |
| 1672 |
to add a few local functions. |
| 1673 |
|
| 1674 |
[OP_FLET/OP_FLABELS + nfun + fun1] |
| 1675 |
... |
| 1676 |
OP_UNBIND nfun |
| 1677 |
labelz: |
| 1678 |
*/ |
| 1679 |
static cl_index |
| 1680 |
c_register_functions(cl_env_ptr env, cl_object l) |
| 1681 |
{ |
| 1682 |
cl_index nfun; |
| 1683 |
for (nfun = 0; !Null(l); nfun++) { |
| 1684 |
cl_object definition = pop(&l); |
| 1685 |
cl_object name = pop(&definition); |
| 1686 |
c_register_function(env, name); |
| 1687 |
} |
| 1688 |
return nfun; |
| 1689 |
} |
| 1690 |
|
| 1691 |
static int |
| 1692 |
c_labels_flet(cl_env_ptr env, int op, cl_object args, int flags) { |
| 1693 |
#define push_back(v,l) { cl_object c = *l = CONS(v, *l); l = &ECL_CONS_CDR(c); } |
| 1694 |
const cl_compiler_ptr c_env = env->c_env; |
| 1695 |
cl_object l, def_list = pop(&args); |
| 1696 |
cl_object old_vars = c_env->variables; |
| 1697 |
cl_object old_funs = c_env->macros; |
| 1698 |
cl_object fnames = ECL_NIL; |
| 1699 |
cl_object v, *f = &fnames; |
| 1700 |
cl_index nfun, lex_idx; |
| 1701 |
|
| 1702 |
if (def_list == ECL_NIL) { |
| 1703 |
return c_locally(env, args, flags); |
| 1704 |
} |
| 1705 |
|
| 1706 |
/* ANSI doesn't specify what should happen if we define |
| 1707 |
multiple functions of the same name in the flet/labels |
| 1708 |
block – ECL treats this undefined behavior as an error */ |
| 1709 |
for (l = def_list, nfun = 0; !Null(l); nfun++) { |
| 1710 |
v = CAR(pop(&l)); |
| 1711 |
if (ecl_member_eq(v, fnames)) |
| 1712 |
FEprogram_error |
| 1713 |
("~s: The function ~s was already defined.", |
| 1714 |
2, (op == OP_LABELS ? @'LABELS' : @'FLET'), v); |
| 1715 |
push_back(v, f); |
| 1716 |
} |
| 1717 |
|
| 1718 |
/* If compiling a LABELS form, add the function names to the lexical |
| 1719 |
environment before compiling the functions */ |
| 1720 |
if (op == OP_LABELS) |
| 1721 |
c_register_functions(env, def_list); |
| 1722 |
|
| 1723 |
/* Push the operator (OP_LABELS/OP_FLET) with the number of functions */ |
| 1724 |
asm_op2(env, op, nfun); |
| 1725 |
|
| 1726 |
/* Compile the local functions now. */ |
| 1727 |
for (l = def_list; !Null(l); ) { |
| 1728 |
cl_object definition = pop(&l); |
| 1729 |
cl_object name = pop(&definition); |
| 1730 |
cl_object lambda = ecl_make_lambda(env, name, definition); |
| 1731 |
lex_idx = c_register_constant(env, lambda); |
| 1732 |
asm_arg(env, lex_idx); |
| 1733 |
} |
| 1734 |
|
| 1735 |
/* If compiling a FLET form, add the function names to the lexical |
| 1736 |
environment after compiling the functions */ |
| 1737 |
if (op == OP_FLET) |
| 1738 |
c_register_functions(env, def_list); |
| 1739 |
|
| 1740 |
/* Compile the body of the form with the local functions in the lexical |
| 1741 |
environment. */ |
| 1742 |
flags = c_locally(env, args, flags); |
| 1743 |
|
| 1744 |
/* Restore and return */ |
| 1745 |
c_undo_bindings(env, old_vars, 0); |
| 1746 |
env->c_env->macros = old_funs; |
| 1747 |
|
| 1748 |
return flags; |
| 1749 |
#undef push |
| 1750 |
} |
| 1751 |
|
| 1752 |
|
| 1753 |
static int |
| 1754 |
c_flet(cl_env_ptr env, cl_object args, int flags) { |
| 1755 |
return c_labels_flet(env, OP_FLET, args, flags); |
| 1756 |
} |
| 1757 |
|
| 1758 |
|
| 1759 |
/* |
| 1760 |
There are two operators that produce functions. The first one is |
| 1761 |
[OP_FUNCTION + name] which takes the function binding of SYMBOL. |
| 1762 |
|
| 1763 |
The second one is OP_CLOSE interpreted which encloses the INTERPRETED |
| 1764 |
function in the current lexical environment. |
| 1765 |
*/ |
| 1766 |
static int |
| 1767 |
c_function(cl_env_ptr env, cl_object args, int flags) { |
| 1768 |
cl_object function = pop(&args); |
| 1769 |
if (!Null(args)) |
| 1770 |
FEprogram_error("FUNCTION: Too many arguments.", 0); |
| 1771 |
return asm_function(env, function, flags); |
| 1772 |
} |
| 1773 |
|
| 1774 |
static int /* XXX: here we look for function in cmpenv */ |
| 1775 |
asm_function(cl_env_ptr env, cl_object function, int flags) { |
| 1776 |
if (!Null(si_valid_function_name_p(function))) { |
| 1777 |
struct cl_compiler_ref ref = c_fun_ref(env, function); |
| 1778 |
switch(ref.place) { |
| 1779 |
case ECL_CMPREF_UNDEFINED: |
| 1780 |
/* Globally defined function */ |
| 1781 |
asm_op2c(env, OP_FUNCTION, function); |
| 1782 |
return FLAG_REG0; |
| 1783 |
case ECL_CMPREF_LOCAL: |
| 1784 |
/* Function from a FLET/LABELS form */ |
| 1785 |
asm_op2(env, OP_LFUNCTION, ref.index); |
| 1786 |
return FLAG_REG0; |
| 1787 |
case ECL_CMPREF_CLOSE: |
| 1788 |
/* Function from a FLET/LABELS form (cfb) */ |
| 1789 |
asm_op2(env, OP_CFUNCTION, ref.index); |
| 1790 |
return FLAG_REG0; |
| 1791 |
default: |
| 1792 |
ecl_miscompilation_error(); |
| 1793 |
} |
| 1794 |
} |
| 1795 |
if (CONSP(function)) { |
| 1796 |
cl_object kind = ECL_CONS_CAR(function); |
| 1797 |
cl_object body = ECL_CONS_CDR(function); |
| 1798 |
cl_object name; |
| 1799 |
if (kind == @'lambda') { |
| 1800 |
name = ECL_NIL; |
| 1801 |
} else if (kind == @'ext::lambda-block') { |
| 1802 |
name = ECL_CONS_CAR(body); |
| 1803 |
body = ECL_CONS_CDR(body); |
| 1804 |
} else { |
| 1805 |
goto ERROR; |
| 1806 |
} |
| 1807 |
|
| 1808 |
cl_object lambda = ecl_make_lambda(env, name, body); |
| 1809 |
cl_object cfb = ecl_nth_value(env, 1); |
| 1810 |
if (Null(cfb)) { |
| 1811 |
/* No closure */ |
| 1812 |
asm_op2c(env, OP_QUOTE, lambda); |
| 1813 |
} else { |
| 1814 |
/* Close around referenced objects */ |
| 1815 |
asm_op2c(env, OP_CLOSE, lambda); |
| 1816 |
} |
| 1817 |
return FLAG_REG0; |
| 1818 |
} |
| 1819 |
ERROR: |
| 1820 |
FEprogram_error("FUNCTION: Not a valid argument ~S.", 1, function); |
| 1821 |
} |
| 1822 |
|
| 1823 |
static int |
| 1824 |
c_go(cl_env_ptr env, cl_object args, int flags) { |
| 1825 |
cl_object tag = pop(&args); |
| 1826 |
if (!Null(args)) { |
| 1827 |
FEprogram_error("GO: Too many arguments.",0); |
| 1828 |
} |
| 1829 |
if (Null(tag)) { |
| 1830 |
tag = ECL_NIL_SYMBOL; |
| 1831 |
} |
| 1832 |
struct cl_compiler_ref ref = c_tag_ref(env, tag); |
| 1833 |
switch(ref.place) { |
| 1834 |
case ECL_CMPREF_UNDEFINED: |
| 1835 |
FEprogram_error("GO: Unknown tag ~S.", 1, tag); |
| 1836 |
case ECL_CMPREF_LOCAL: |
| 1837 |
asm_op(env, OP_GO); |
| 1838 |
asm_arg(env, ref.index); |
| 1839 |
asm_arg(env, ref.label); |
| 1840 |
break; |
| 1841 |
case ECL_CMPREF_CLOSE: |
| 1842 |
asm_op(env, OP_GO_CFB); |
| 1843 |
asm_arg(env, ref.index); |
| 1844 |
asm_arg(env, ref.label); |
| 1845 |
break; |
| 1846 |
default: |
| 1847 |
ecl_miscompilation_error(); |
| 1848 |
} |
| 1849 |
return flags; |
| 1850 |
} |
| 1851 |
|
| 1852 |
/* |
| 1853 |
(if a b) -> (cond (a b)) |
| 1854 |
(if a b c) -> (cond (a b) (t c)) |
| 1855 |
*/ |
| 1856 |
static int |
| 1857 |
c_if(cl_env_ptr env, cl_object form, int flags) { |
| 1858 |
cl_object test = pop(&form); |
| 1859 |
cl_object then = pop(&form); |
| 1860 |
then = cl_list(2, test, then); |
| 1861 |
if (Null(form)) { |
| 1862 |
return c_cond(env, ecl_list1(then), flags); |
| 1863 |
} else { |
| 1864 |
return c_cond(env, cl_list(2, then, CONS(ECL_T, form)), flags); |
| 1865 |
} |
| 1866 |
} |
| 1867 |
|
| 1868 |
|
| 1869 |
static int |
| 1870 |
c_labels(cl_env_ptr env, cl_object args, int flags) { |
| 1871 |
return c_labels_flet(env, OP_LABELS, args, flags); |
| 1872 |
} |
| 1873 |
|
| 1874 |
|
| 1875 |
/* |
| 1876 |
The OP_PUSHENV saves the current lexical environment to allow |
| 1877 |
several bindings. |
| 1878 |
OP_PUSHENV |
| 1879 |
... ; binding forms |
| 1880 |
... ; body |
| 1881 |
OP_EXIT |
| 1882 |
|
| 1883 |
There are four forms which perform bindings |
| 1884 |
OP_PBIND name ; Bind NAME in the lexical env. using |
| 1885 |
; a value from the stack |
| 1886 |
OP_PBINDS name ; Bind NAME as special variable using |
| 1887 |
; a value from the stack |
| 1888 |
OP_BIND name ; Bind NAME in the lexical env. using |
| 1889 |
; VALUES(0) |
| 1890 |
OP_BINDS name ; Bind NAME as special variable using |
| 1891 |
; VALUES(0) |
| 1892 |
|
| 1893 |
After a variable has been bound, there are several ways to |
| 1894 |
refer to it. |
| 1895 |
|
| 1896 |
1) Refer to the n-th variable in the lexical environment |
| 1897 |
[SYMVAL + n] |
| 1898 |
|
| 1899 |
2) Refer to the value of a special variable or constant |
| 1900 |
SYMVALS |
| 1901 |
name |
| 1902 |
|
| 1903 |
3) Push the value of the n-th variable of the lexical environment |
| 1904 |
[PUSHV + n] |
| 1905 |
|
| 1906 |
4) Push the value of a special variable or constant |
| 1907 |
PUSHVS |
| 1908 |
name |
| 1909 |
*/ |
| 1910 |
|
| 1911 |
static int |
| 1912 |
c_let_leta(cl_env_ptr env, int op, cl_object args, int flags) { |
| 1913 |
cl_object bindings, specials, body, l, vars; |
| 1914 |
cl_object old_variables = env->c_env->variables; |
| 1915 |
|
| 1916 |
bindings = cl_car(args); |
| 1917 |
body = c_process_declarations(ECL_CONS_CDR(args)); |
| 1918 |
specials = env->values[3]; |
| 1919 |
|
| 1920 |
/* Optimize some common cases */ |
| 1921 |
if (bindings == ECL_NIL) |
| 1922 |
return c_locally(env, CDR(args), flags); |
| 1923 |
if (ECL_CONS_CDR(bindings) == ECL_NIL) |
| 1924 |
op = OP_BIND; |
| 1925 |
|
| 1926 |
for (vars=ECL_NIL, l=bindings; !Null(l); ) { |
| 1927 |
cl_object aux = pop(&l); |
| 1928 |
cl_object var, value; |
| 1929 |
if (ECL_ATOM(aux)) { |
| 1930 |
var = aux; |
| 1931 |
value = ECL_NIL; |
| 1932 |
} else { |
| 1933 |
var = pop(&aux); |
| 1934 |
value = pop_maybe_nil(&aux); |
| 1935 |
if (!Null(aux)) |
| 1936 |
FEprogram_error("LET: Ill formed declaration.",0); |
| 1937 |
} |
| 1938 |
if (!ECL_SYMBOLP(var)) |
| 1939 |
FEillegal_variable_name(var); |
| 1940 |
if (ecl_symbol_type(var) & ecl_stp_constant) |
| 1941 |
FEbinding_a_constant(var); |
| 1942 |
if (op == OP_PBIND) { |
| 1943 |
compile_form(env, value, FLAG_PUSH); |
| 1944 |
if (ecl_member_eq(var, vars)) |
| 1945 |
FEprogram_error |
| 1946 |
("LET: The variable ~s occurs more than " |
| 1947 |
"once in the LET.", 1, var); |
| 1948 |
vars = CONS(var, vars); |
| 1949 |
} else { |
| 1950 |
compile_form(env, value, FLAG_REG0); |
| 1951 |
c_bind(env, var, specials); |
| 1952 |
} |
| 1953 |
} |
| 1954 |
while (!Null(vars)) |
| 1955 |
c_pbind(env, pop(&vars), specials); |
| 1956 |
|
| 1957 |
/* We have to register all specials, because in the list |
| 1958 |
* there might be some variable that is not bound by this LET form |
| 1959 |
*/ |
| 1960 |
c_declare_specials(env, specials); |
| 1961 |
|
| 1962 |
flags = compile_body(env, body, flags); |
| 1963 |
|
| 1964 |
c_undo_bindings(env, old_variables, 0); |
| 1965 |
return flags; |
| 1966 |
} |
| 1967 |
|
| 1968 |
static int |
| 1969 |
c_let(cl_env_ptr env, cl_object args, int flags) { |
| 1970 |
return c_let_leta(env, OP_PBIND, args, flags); |
| 1971 |
} |
| 1972 |
|
| 1973 |
static int |
| 1974 |
c_leta(cl_env_ptr env, cl_object args, int flags) { |
| 1975 |
return c_let_leta(env, OP_BIND, args, flags); |
| 1976 |
} |
| 1977 |
|
| 1978 |
static int |
| 1979 |
c_load_time_value(cl_env_ptr env, cl_object args, int flags) |
| 1980 |
{ |
| 1981 |
const cl_compiler_ptr c_env = env->c_env; |
| 1982 |
cl_object value; |
| 1983 |
unlikely_if (Null(args) || cl_cddr(args) != ECL_NIL) |
| 1984 |
FEprogram_error("LOAD-TIME-VALUE: Wrong number of arguments.", 0); |
| 1985 |
value = ECL_CONS_CAR(args); |
| 1986 |
if (c_env->mode == FLAG_EXECUTE) { |
| 1987 |
value = si_eval_with_env(1, value); |
| 1988 |
} else if (ECL_SYMBOLP(value) || ECL_LISTP(value)) { |
| 1989 |
/* Using the form as constant, we force the system to coalesce multiple |
| 1990 |
* copies of the same load-time-value form */ |
| 1991 |
push(cl_list(3, args, value, ECL_NIL), &c_env->load_time_forms); |
| 1992 |
value = args; |
| 1993 |
} |
| 1994 |
return compile_constant(env, value, flags); |
| 1995 |
} |
| 1996 |
|
| 1997 |
static int |
| 1998 |
c_locally(cl_env_ptr env, cl_object args, int flags) { |
| 1999 |
cl_object old_env = env->c_env->variables; |
| 2000 |
|
| 2001 |
/* First use declarations by declaring special variables... */ |
| 2002 |
args = c_process_declarations(args); |
| 2003 |
c_declare_specials(env, env->values[3]); |
| 2004 |
|
| 2005 |
/* ...and then process body */ |
| 2006 |
flags = compile_toplevel_body(env, args, flags); |
| 2007 |
|
| 2008 |
c_undo_bindings(env, old_env, 0); |
| 2009 |
|
| 2010 |
return flags; |
| 2011 |
} |
| 2012 |
|
| 2013 |
/* |
| 2014 |
MACROLET |
| 2015 |
|
| 2016 |
The current lexical environment is saved. A new one is prepared with the |
| 2017 |
definitions of these macros, and this environment is used to compile the body. |
| 2018 |
*/ |
| 2019 |
static int |
| 2020 |
c_macrolet(cl_env_ptr the_env, cl_object args, int flags) |
| 2021 |
{ |
| 2022 |
const cl_compiler_ptr c_env = the_env->c_env; |
| 2023 |
cl_object old_env = c_env->macros; |
| 2024 |
cl_object env = funcall(3, @'si::cmp-env-register-macrolet', pop(&args), |
| 2025 |
CONS(c_env->variables, c_env->macros)); |
| 2026 |
c_env->macros = CDR(env); |
| 2027 |
flags = c_locally(the_env, args, flags); |
| 2028 |
c_env->macros = old_env; |
| 2029 |
return flags; |
| 2030 |
} |
| 2031 |
|
| 2032 |
static void |
| 2033 |
c_vbind(cl_env_ptr env, cl_object var, int n, cl_object specials) |
| 2034 |
{ |
| 2035 |
if (c_declared_special(var, specials)) { |
| 2036 |
c_register_var(env, var, FLAG_PUSH, TRUE); |
| 2037 |
if (n) { |
| 2038 |
asm_op2(env, OP_VBINDS, n); |
| 2039 |
} else { |
| 2040 |
asm_op(env, OP_BINDS); |
| 2041 |
} |
| 2042 |
} else { |
| 2043 |
c_register_var(env, var, FALSE, TRUE); |
| 2044 |
if (n) { |
| 2045 |
asm_op2(env, OP_VBIND, n); |
| 2046 |
} else { |
| 2047 |
asm_op(env, OP_BIND); |
| 2048 |
} |
| 2049 |
} |
| 2050 |
asm_arg_data(env, var); |
| 2051 |
} |
| 2052 |
|
| 2053 |
static int |
| 2054 |
c_multiple_value_bind(cl_env_ptr env, cl_object args, int flags) |
| 2055 |
{ |
| 2056 |
cl_object vars = pop(&args); |
| 2057 |
int n = ecl_length(vars); |
| 2058 |
switch (n) { |
| 2059 |
case 0: |
| 2060 |
compile_form(env, pop(&args), FLAG_IGNORE); |
| 2061 |
return c_locally(env, args, flags); |
| 2062 |
case 1: |
| 2063 |
vars = ECL_CONS_CAR(vars); |
| 2064 |
vars = ecl_list1(cl_list(2, vars, pop(&args))); |
| 2065 |
return c_leta(env, cl_listX(2, vars, args), flags); |
| 2066 |
default: { |
| 2067 |
cl_object value = pop(&args); |
| 2068 |
cl_object old_variables = env->c_env->variables; |
| 2069 |
cl_object body = c_process_declarations(args); |
| 2070 |
cl_object specials = env->values[3]; |
| 2071 |
compile_form(env, value, FLAG_VALUES); |
| 2072 |
for (vars=cl_reverse(vars); n--; ) { |
| 2073 |
cl_object var = pop(&vars); |
| 2074 |
if (!ECL_SYMBOLP(var)) |
| 2075 |
FEillegal_variable_name(var); |
| 2076 |
if (ecl_symbol_type(var) & ecl_stp_constant) |
| 2077 |
FEbinding_a_constant(var); |
| 2078 |
c_vbind(env, var, n, specials); |
| 2079 |
} |
| 2080 |
c_declare_specials(env, specials); |
| 2081 |
flags = compile_body(env, body, flags); |
| 2082 |
c_undo_bindings(env, old_variables, 0); |
| 2083 |
return flags; |
| 2084 |
}} |
| 2085 |
} |
| 2086 |
|
| 2087 |
|
| 2088 |
static int |
| 2089 |
c_multiple_value_call(cl_env_ptr env, cl_object args, int flags) { |
| 2090 |
cl_object name; |
| 2091 |
int op; |
| 2092 |
|
| 2093 |
name = pop(&args); |
| 2094 |
if (Null(args)) { |
| 2095 |
/* If no arguments, just use ordinary call */ |
| 2096 |
return c_funcall(env, cl_list(1, name), flags); |
| 2097 |
} |
| 2098 |
compile_form(env, name, FLAG_PUSH); |
| 2099 |
for (op = OP_PUSHVALUES; !Null(args); op = OP_PUSHMOREVALUES) { |
| 2100 |
compile_form(env, pop(&args), FLAG_VALUES); |
| 2101 |
asm_op(env, op); |
| 2102 |
} |
| 2103 |
asm_op(env, OP_MCALL); |
| 2104 |
asm_op(env, OP_POP1); |
| 2105 |
|
| 2106 |
return FLAG_VALUES; |
| 2107 |
} |
| 2108 |
|
| 2109 |
|
| 2110 |
static int |
| 2111 |
c_multiple_value_prog1(cl_env_ptr env, cl_object args, int flags) { |
| 2112 |
compile_form(env, pop(&args), FLAG_VALUES); |
| 2113 |
if (!Null(args)) { |
| 2114 |
asm_op(env, OP_PUSHVALUES); |
| 2115 |
compile_body(env, args, FLAG_IGNORE); |
| 2116 |
asm_op(env, OP_POPVALUES); |
| 2117 |
} |
| 2118 |
return FLAG_VALUES; |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
static int |
| 2123 |
c_multiple_value_setq(cl_env_ptr env, cl_object orig_args, int flags) { |
| 2124 |
cl_object args = orig_args; |
| 2125 |
cl_object orig_vars; |
| 2126 |
cl_object vars = ECL_NIL, values; |
| 2127 |
cl_object old_variables = env->c_env->variables; |
| 2128 |
cl_index nvars = 0; |
| 2129 |
|
| 2130 |
/* Look for symbol macros, building the list of variables |
| 2131 |
and the list of late assignments. */ |
| 2132 |
for (orig_vars = pop(&args); !Null(orig_vars); ) { |
| 2133 |
cl_object v = pop(&orig_vars); |
| 2134 |
if (!ECL_SYMBOLP(v)) |
| 2135 |
FEillegal_variable_name(v); |
| 2136 |
v = c_macro_expand1(env, v); |
| 2137 |
if (!ECL_SYMBOLP(v)) { |
| 2138 |
/* If any of the places to be set is not a variable, |
| 2139 |
* transform MULTIPLE-VALUE-SETQ into (SETF (VALUES ...)) |
| 2140 |
*/ |
| 2141 |
args = orig_args; |
| 2142 |
return compile_form(env, cl_listX(3, @'setf', |
| 2143 |
CONS(@'values', CAR(args)), |
| 2144 |
CDR(args)), |
| 2145 |
flags); |
| 2146 |
} |
| 2147 |
vars = CONS(v, vars); |
| 2148 |
nvars++; |
| 2149 |
} |
| 2150 |
|
| 2151 |
/* Compile values */ |
| 2152 |
values = pop(&args); |
| 2153 |
if (args != ECL_NIL) |
| 2154 |
FEprogram_error("MULTIPLE-VALUE-SETQ: Too many arguments.", 0); |
| 2155 |
if (nvars == 0) { |
| 2156 |
/* No variables */ |
| 2157 |
return compile_form(env, cl_list(2, @'values', values), flags); |
| 2158 |
} |
| 2159 |
compile_form(env, values, FLAG_VALUES); |
| 2160 |
|
| 2161 |
/* Compile variables */ |
| 2162 |
for (nvars = 0, vars = cl_nreverse(vars); vars != ECL_NIL; nvars++, vars = ECL_CONS_CDR(vars)) { |
| 2163 |
if (nvars) { |
| 2164 |
compile_setq(env, OP_VSETQ, ECL_CONS_CAR(vars)); |
| 2165 |
asm_arg(env, nvars); |
| 2166 |
} else { |
| 2167 |
compile_setq(env, OP_SETQ, ECL_CONS_CAR(vars)); |
| 2168 |
} |
| 2169 |
} |
| 2170 |
|
| 2171 |
c_undo_bindings(env, old_variables, 0); |
| 2172 |
|
| 2173 |
return FLAG_REG0; |
| 2174 |
} |
| 2175 |
|
| 2176 |
/* |
| 2177 |
The OP_NOT operator reverses the boolean value of VALUES(0). |
| 2178 |
*/ |
| 2179 |
static int |
| 2180 |
c_not(cl_env_ptr env, cl_object args, int flags) { |
| 2181 |
flags = maybe_reg0(flags); |
| 2182 |
if (flags & FLAG_USEFUL) { |
| 2183 |
/* The value is useful */ |
| 2184 |
compile_form(env, pop(&args), FLAG_REG0); |
| 2185 |
asm_op(env, OP_NOT); |
| 2186 |
} else { |
| 2187 |
/* The value may be ignored. */ |
| 2188 |
flags = compile_form(env, pop(&args), flags); |
| 2189 |
} |
| 2190 |
if (!Null(args)) |
| 2191 |
FEprogram_error("NOT/NULL: Too many arguments.", 0); |
| 2192 |
return flags; |
| 2193 |
} |
| 2194 |
|
| 2195 |
/* |
| 2196 |
The OP_NTHVAL operator moves a value from VALUES(ndx) to |
| 2197 |
VALUES(0). The index NDX is taken from the stack. |
| 2198 |
|
| 2199 |
OP_NTHVAL |
| 2200 |
*/ |
| 2201 |
static int |
| 2202 |
c_nth_value(cl_env_ptr env, cl_object args, int flags) { |
| 2203 |
compile_form(env, pop(&args), FLAG_PUSH); /* INDEX */ |
| 2204 |
compile_form(env, pop(&args), FLAG_VALUES); /* VALUES */ |
| 2205 |
if (args != ECL_NIL) |
| 2206 |
FEprogram_error("NTH-VALUE: Too many arguments.",0); |
| 2207 |
asm_op(env, OP_NTHVAL); |
| 2208 |
return FLAG_REG0; |
| 2209 |
} |
| 2210 |
|
| 2211 |
|
| 2212 |
static int |
| 2213 |
c_prog1(cl_env_ptr env, cl_object args, int flags) { |
| 2214 |
cl_object form = pop(&args); |
| 2215 |
if (!(flags & FLAG_USEFUL) || (flags & FLAG_PUSH)) { |
| 2216 |
flags = compile_form(env, form, flags); |
| 2217 |
compile_body(env, args, FLAG_IGNORE); |
| 2218 |
} else { |
| 2219 |
flags = FLAG_REG0; |
| 2220 |
compile_form(env, form, FLAG_PUSH); |
| 2221 |
compile_body(env, args, FLAG_IGNORE); |
| 2222 |
asm_op(env, OP_POP); |
| 2223 |
} |
| 2224 |
return flags; |
| 2225 |
} |
| 2226 |
|
| 2227 |
|
| 2228 |
/* |
| 2229 |
The OP_PROGV operator exectures a set of statements in a lexical |
| 2230 |
environment that has been extended with special variables. The |
| 2231 |
list of special variables is taken from the top of the stack, |
| 2232 |
while the list of values is in VALUES(0). |
| 2233 |
|
| 2234 |
... ; list of variables |
| 2235 |
OP_PUSH |
| 2236 |
... ; list of values |
| 2237 |
OP_PROGV |
| 2238 |
... ; body of progv |
| 2239 |
OP_EXIT |
| 2240 |
*/ |
| 2241 |
static int |
| 2242 |
c_progv(cl_env_ptr env, cl_object args, int flags) { |
| 2243 |
cl_object vars = pop(&args); |
| 2244 |
cl_object values = pop(&args); |
| 2245 |
|
| 2246 |
/* The list of variables is in the stack */ |
| 2247 |
compile_form(env, vars, FLAG_PUSH); |
| 2248 |
|
| 2249 |
/* The list of values is in reg0 */ |
| 2250 |
compile_form(env, values, FLAG_REG0); |
| 2251 |
|
| 2252 |
/* The body is interpreted within an extended lexical |
| 2253 |
environment. However, as all the new variables are |
| 2254 |
special, the compiler need not take care of them |
| 2255 |
*/ |
| 2256 |
asm_op(env, OP_PROGV); |
| 2257 |
flags = compile_body(env, args, FLAG_VALUES); |
| 2258 |
asm_op(env, OP_EXIT_PROGV); |
| 2259 |
|
| 2260 |
return flags; |
| 2261 |
} |
| 2262 |
|
| 2263 |
|
| 2264 |
/* |
| 2265 |
There are four assignment operators. They are |
| 2266 |
|
| 2267 |
1) Assign VALUES(0) to the lexical variable which occupies the |
| 2268 |
N-th position |
| 2269 |
[OP_SETQ + n] |
| 2270 |
|
| 2271 |
2) Assign VALUES(0) to the special variable NAME |
| 2272 |
[OP_SETQS + name] |
| 2273 |
|
| 2274 |
3) Pop a value from the stack and assign it to the lexical |
| 2275 |
variable in the N-th position. |
| 2276 |
[OP_PSETQ + n] |
| 2277 |
|
| 2278 |
4) Pop a value from the stack and assign it to the special |
| 2279 |
variable denoted by NAME |
| 2280 |
[OP_PSETQS + name] |
| 2281 |
*/ |
| 2282 |
static int |
| 2283 |
c_psetq(cl_env_ptr env, cl_object old_args, int flags) { |
| 2284 |
cl_object args = ECL_NIL, vars = ECL_NIL; |
| 2285 |
bool use_psetf = FALSE; |
| 2286 |
cl_index nvars = 0; |
| 2287 |
|
| 2288 |
if (Null(old_args)) |
| 2289 |
return compile_body(env, ECL_NIL, flags); |
| 2290 |
/* We have to make sure that non of the variables which |
| 2291 |
are to be assigned is actually a symbol macro. If that |
| 2292 |
is the case, we invoke (PSETF ...) to handle the |
| 2293 |
macro expansions. |
| 2294 |
*/ |
| 2295 |
do { |
| 2296 |
cl_object var = pop(&old_args); |
| 2297 |
cl_object value = pop(&old_args); |
| 2298 |
if (!ECL_SYMBOLP(var)) |
| 2299 |
FEillegal_variable_name(var); |
| 2300 |
var = c_macro_expand1(env, var); |
| 2301 |
if (!ECL_SYMBOLP(var)) |
| 2302 |
use_psetf = TRUE; |
| 2303 |
args = ecl_nconc(args, cl_list(2, var, value)); |
| 2304 |
nvars++; |
| 2305 |
} while (!Null(old_args)); |
| 2306 |
if (use_psetf) { |
| 2307 |
return compile_form(env, CONS(@'psetf', args), flags); |
| 2308 |
} |
| 2309 |
do { |
| 2310 |
cl_object var = pop(&args); |
| 2311 |
cl_object value = pop(&args); |
| 2312 |
vars = CONS(var, vars); |
| 2313 |
compile_form(env, value, FLAG_PUSH); |
| 2314 |
} while (!Null(args)); |
| 2315 |
do { |
| 2316 |
compile_setq(env, OP_PSETQ, pop(&vars)); |
| 2317 |
} while (!Null(vars)); |
| 2318 |
return compile_form(env, ECL_NIL, flags); |
| 2319 |
} |
| 2320 |
|
| 2321 |
|
| 2322 |
/* |
| 2323 |
The OP_RETURN operator returns from a block putting result in VALUES(). |
| 2324 |
|
| 2325 |
... ; output form |
| 2326 |
OP_RETURN | OP_RETURN_CFB |
| 2327 |
idx ; index of the output block |
| 2328 |
*/ |
| 2329 |
static int |
| 2330 |
c_return_aux(cl_env_ptr env, cl_object name, cl_object args, int flags) |
| 2331 |
{ |
| 2332 |
struct cl_compiler_ref ref = c_blk_ref(env, name); |
| 2333 |
cl_object output = pop_maybe_nil(&args); |
| 2334 |
if (!Null(args)) |
| 2335 |
FEprogram_error("RETURN-FROM: Too many arguments.", 0); |
| 2336 |
switch(ref.place) { |
| 2337 |
case ECL_CMPREF_UNDEFINED: |
| 2338 |
FEprogram_error("RETURN-FROM: Unknown block name ~S.", 1, name); |
| 2339 |
case ECL_CMPREF_LOCAL: |
| 2340 |
compile_form(env, output, FLAG_VALUES); |
| 2341 |
asm_op2(env, OP_RETURN, ref.index); |
| 2342 |
break; |
| 2343 |
case ECL_CMPREF_CLOSE: |
| 2344 |
compile_form(env, output, FLAG_VALUES); |
| 2345 |
asm_op2(env, OP_RETURN_CFB, ref.index); |
| 2346 |
break; |
| 2347 |
default: |
| 2348 |
ecl_miscompilation_error(); |
| 2349 |
} |
| 2350 |
return FLAG_VALUES; |
| 2351 |
} |
| 2352 |
|
| 2353 |
static int |
| 2354 |
c_return(cl_env_ptr env, cl_object stmt, int flags) { |
| 2355 |
return c_return_aux(env, ECL_NIL, stmt, flags); |
| 2356 |
} |
| 2357 |
|
| 2358 |
|
| 2359 |
static int |
| 2360 |
c_return_from(cl_env_ptr env, cl_object stmt, int flags) { |
| 2361 |
cl_object name = pop(&stmt); |
| 2362 |
return c_return_aux(env, name, stmt, flags); |
| 2363 |
} |
| 2364 |
|
| 2365 |
|
| 2366 |
static int |
| 2367 |
c_setq(cl_env_ptr env, cl_object args, int flags) { |
| 2368 |
if (Null(args)) |
| 2369 |
return compile_form(env, ECL_NIL, flags); |
| 2370 |
do { |
| 2371 |
cl_object var = pop(&args); |
| 2372 |
cl_object value = pop(&args); |
| 2373 |
if (!ECL_SYMBOLP(var)) |
| 2374 |
FEillegal_variable_name(var); |
| 2375 |
var = c_macro_expand1(env, var); |
| 2376 |
if (ECL_SYMBOLP(var)) { |
| 2377 |
flags = FLAG_REG0; |
| 2378 |
compile_form(env, value, FLAG_REG0); |
| 2379 |
compile_setq(env, OP_SETQ, var); |
| 2380 |
} else { |
| 2381 |
flags = ecl_endp(args)? FLAG_VALUES : FLAG_REG0; |
| 2382 |
compile_form(env, cl_list(3, @'setf', var, value), flags); |
| 2383 |
} |
| 2384 |
} while (!Null(args)); |
| 2385 |
return flags; |
| 2386 |
} |
| 2387 |
|
| 2388 |
|
| 2389 |
static int |
| 2390 |
c_symbol_macrolet(cl_env_ptr env, cl_object args, int flags) |
| 2391 |
{ |
| 2392 |
cl_object def_list, specials, body; |
| 2393 |
cl_object old_variables = env->c_env->variables; |
| 2394 |
|
| 2395 |
def_list = pop(&args); |
| 2396 |
body = c_process_declarations(args); |
| 2397 |
specials = env->values[3]; |
| 2398 |
|
| 2399 |
/* Scan the list of definitions */ |
| 2400 |
while (!Null(def_list)) { |
| 2401 |
cl_object definition = pop(&def_list); |
| 2402 |
cl_object name = pop(&definition); |
| 2403 |
cl_object expansion = pop(&definition); |
| 2404 |
cl_object arglist = cl_list(2, @gensym(0), @gensym(0)); |
| 2405 |
cl_object function; |
| 2406 |
if ((ecl_symbol_type(name) & (ecl_stp_constant|ecl_stp_special)) || |
| 2407 |
ecl_member_eq(name, specials)) |
| 2408 |
{ |
| 2409 |
FEprogram_error("SYMBOL-MACROLET: Symbol ~A cannot be \ |
| 2410 |
declared special and appear in a symbol-macrolet.", 1, name); |
| 2411 |
} |
| 2412 |
definition = cl_list(2, arglist, cl_list(2, @'quote', expansion)); |
| 2413 |
function = ecl_make_lambda(env, name, definition); |
| 2414 |
c_register_symbol_macro(env, name, function); |
| 2415 |
} |
| 2416 |
c_declare_specials(env, specials); |
| 2417 |
flags = compile_toplevel_body(env, body, flags); |
| 2418 |
c_undo_bindings(env, old_variables, 0); |
| 2419 |
return flags; |
| 2420 |
} |
| 2421 |
|
| 2422 |
static int |
| 2423 |
c_tagbody(cl_env_ptr env, cl_object args, int flags) |
| 2424 |
{ |
| 2425 |
cl_object old_env = env->c_env->variables; |
| 2426 |
cl_index tag_base; |
| 2427 |
cl_object labels = ECL_NIL, label, body; |
| 2428 |
cl_type item_type; |
| 2429 |
int nt, i; |
| 2430 |
|
| 2431 |
/* count the tags */ |
| 2432 |
for (nt = 0, body = args; !Null(body); ) { |
| 2433 |
label = pop(&body); |
| 2434 |
if (Null(label)) { |
| 2435 |
label = ECL_NIL_SYMBOL; |
| 2436 |
} |
| 2437 |
item_type = ecl_t_of(label); |
| 2438 |
if (item_type == t_symbol || item_type == t_fixnum || |
| 2439 |
item_type == t_bignum) { |
| 2440 |
labels = CONS(CONS(label,ecl_make_fixnum(nt)), labels); |
| 2441 |
nt += 1; |
| 2442 |
} |
| 2443 |
} |
| 2444 |
if (nt == 0) { |
| 2445 |
compile_body(env, args, 0); |
| 2446 |
return compile_form(env, ECL_NIL, flags); |
| 2447 |
} |
| 2448 |
asm_op2c(env, OP_BLOCK, ecl_make_fixnum(0)); |
| 2449 |
c_register_tags(env, labels); |
| 2450 |
asm_op2(env, OP_TAGBODY, nt); |
| 2451 |
tag_base = current_pc(env); |
| 2452 |
for (i = nt; i; i--) |
| 2453 |
asm_arg(env, 0); |
| 2454 |
|
| 2455 |
for (body = args; !Null(body); ) { |
| 2456 |
label = pop(&body); |
| 2457 |
if (Null(label)) { |
| 2458 |
label = ECL_NIL_SYMBOL; |
| 2459 |
} |
| 2460 |
item_type = ecl_t_of(label); |
| 2461 |
if (item_type == t_symbol || item_type == t_fixnum || |
| 2462 |
item_type == t_bignum) { |
| 2463 |
asm_complete(env, 0, tag_base); |
| 2464 |
tag_base += OPARG_SIZE; |
| 2465 |
} else { |
| 2466 |
compile_form(env, label, FLAG_IGNORE); |
| 2467 |
} |
| 2468 |
} |
| 2469 |
asm_op(env, OP_EXIT_TAGBODY); |
| 2470 |
c_undo_bindings(env, old_env, 0); |
| 2471 |
return FLAG_REG0; |
| 2472 |
} |
| 2473 |
|
| 2474 |
static int |
| 2475 |
c_the(cl_env_ptr env, cl_object stmt, int flags) { |
| 2476 |
cl_object value; |
| 2477 |
/* ignore first element */ |
| 2478 |
pop(&stmt); |
| 2479 |
value = pop(&stmt); |
| 2480 |
if (stmt != ECL_NIL) { |
| 2481 |
FEprogram_error("THE: Too many arguments",0); |
| 2482 |
} |
| 2483 |
return compile_form(env, value, flags); |
| 2484 |
} |
| 2485 |
|
| 2486 |
/* |
| 2487 |
The OP_THROW jumps to an enclosing OP_CATCH whose tag |
| 2488 |
matches the one of the throw. The tag is taken from the |
| 2489 |
stack, while the output values are left in VALUES(). |
| 2490 |
*/ |
| 2491 |
static int |
| 2492 |
c_throw(cl_env_ptr env, cl_object stmt, int flags) { |
| 2493 |
cl_object tag = pop(&stmt); |
| 2494 |
cl_object form = pop(&stmt); |
| 2495 |
if (stmt != ECL_NIL) |
| 2496 |
FEprogram_error("THROW: Too many arguments.",0); |
| 2497 |
compile_form(env, tag, FLAG_PUSH); |
| 2498 |
compile_form(env, form, FLAG_VALUES); |
| 2499 |
asm_op(env, OP_THROW); |
| 2500 |
return flags; |
| 2501 |
} |
| 2502 |
|
| 2503 |
|
| 2504 |
static int |
| 2505 |
c_unwind_protect(cl_env_ptr env, cl_object args, int flags) { |
| 2506 |
cl_index label = asm_jmp(env, OP_PROTECT); |
| 2507 |
|
| 2508 |
/* We register unwind-protect boundary. This mark is not used in bytecode |
| 2509 |
compiler but we do it anyway to have better compilation environment. */ |
| 2510 |
c_register_boundary(env, @'si::unwind-protect-boundary'); |
| 2511 |
|
| 2512 |
flags = maybe_values(flags); |
| 2513 |
|
| 2514 |
/* Compile form to be protected */ |
| 2515 |
flags = compile_form(env, pop(&args), flags); |
| 2516 |
asm_op(env, OP_PROTECT_NORMAL); |
| 2517 |
|
| 2518 |
/* Compile exit clause */ |
| 2519 |
asm_complete(env, OP_PROTECT, label); |
| 2520 |
compile_body(env, args, FLAG_IGNORE); |
| 2521 |
asm_op(env, OP_PROTECT_EXIT); |
| 2522 |
|
| 2523 |
return flags; |
| 2524 |
} |
| 2525 |
|
| 2526 |
|
| 2527 |
/* |
| 2528 |
The OP_VALUES moves N values from the stack to VALUES(). |
| 2529 |
|
| 2530 |
[OP_VALUES + n] |
| 2531 |
*/ |
| 2532 |
static int |
| 2533 |
c_values(cl_env_ptr env, cl_object args, int flags) { |
| 2534 |
if (!(flags & FLAG_USEFUL)) { |
| 2535 |
/* This value will be discarded. We do not care to |
| 2536 |
push it or to save it in VALUES */ |
| 2537 |
if (Null(args)) |
| 2538 |
return flags; |
| 2539 |
return compile_body(env, args, flags); |
| 2540 |
} else if (flags & FLAG_PUSH) { |
| 2541 |
/* We only need the first value. However, the rest |
| 2542 |
of arguments HAVE to be be evaluated */ |
| 2543 |
if (Null(args)) |
| 2544 |
return compile_form(env, ECL_NIL, flags); |
| 2545 |
flags = compile_form(env, pop(&args), FLAG_PUSH); |
| 2546 |
compile_body(env, args, FLAG_IGNORE); |
| 2547 |
return flags; |
| 2548 |
} else if (Null(args)) { |
| 2549 |
asm_op(env, OP_NOP); |
| 2550 |
} else { |
| 2551 |
int n = 0; |
| 2552 |
while (!Null(args)) { |
| 2553 |
compile_form(env, pop_maybe_nil(&args), FLAG_PUSH); |
| 2554 |
n++; |
| 2555 |
} |
| 2556 |
asm_op2(env, OP_VALUES, n); |
| 2557 |
} |
| 2558 |
return FLAG_VALUES; |
| 2559 |
} |
| 2560 |
|
| 2561 |
static void |
| 2562 |
defer_load_object(cl_env_ptr env, cl_object place, cl_object created) |
| 2563 |
{ |
| 2564 |
const cl_compiler_ptr c_env = env->c_env; |
| 2565 |
if (c_env->ltf_defer_init_until == ECL_T) { |
| 2566 |
FEerror("Circular dependency in load time forms involving ~S.", 1, ECL_CONS_CAR(place)); |
| 2567 |
} |
| 2568 |
if (c_env->ltf_defer_init_until != ECL_NIL |
| 2569 |
&& ecl_member_eq(c_env->ltf_defer_init_until, created)) { |
| 2570 |
/* We are already deferring the init form long enough, nothing to do. */ |
| 2571 |
return; |
| 2572 |
} |
| 2573 |
c_env->ltf_defer_init_until = place; |
| 2574 |
} |
| 2575 |
|
| 2576 |
static void |
| 2577 |
maybe_make_load_forms(cl_env_ptr env, cl_object constant) |
| 2578 |
{ |
| 2579 |
const cl_compiler_ptr c_env = env->c_env; |
| 2580 |
cl_object init, make, created; |
| 2581 |
if ((c_env->mode != FLAG_LOAD) |
| 2582 |
|| (si_need_to_make_load_form_p(constant) == ECL_NIL)) |
| 2583 |
return; |
| 2584 |
created = c_env->ltf_being_created; |
| 2585 |
/* If we are compiling a creation form for another load time form, defer the |
| 2586 |
* init form until after this creation form has been compiled. */ |
| 2587 |
loop_for_in(created) { |
| 2588 |
cl_object place = ECL_CONS_CAR(created); |
| 2589 |
if (ECL_CONS_CAR(place) == constant) { |
| 2590 |
defer_load_object(env, place, created); |
| 2591 |
return; |
| 2592 |
} |
| 2593 |
} end_loop_for_in; |
| 2594 |
make = _ecl_funcall2(@'make-load-form', constant); |
| 2595 |
init = (env->nvalues > 1)? env->values[1] : ECL_NIL; |
| 2596 |
push(cl_list(3, constant, make, init), &c_env->load_time_forms); |
| 2597 |
} |
| 2598 |
|
| 2599 |
static int |
| 2600 |
compile_constant(cl_env_ptr env, cl_object stmt, int flags) |
| 2601 |
{ |
| 2602 |
if (flags & FLAG_USEFUL) { |
| 2603 |
bool push = flags & FLAG_PUSH; |
| 2604 |
cl_fixnum n; |
| 2605 |
maybe_make_load_forms(env, stmt); |
| 2606 |
if (stmt == ECL_NIL) { |
| 2607 |
asm_op(env, push? OP_PUSHNIL : OP_NIL); |
| 2608 |
} else if (ECL_FIXNUMP(stmt) && (n = ecl_fixnum(stmt)) <= MAX_OPARG |
| 2609 |
&& n >= -MAX_OPARG) { |
| 2610 |
asm_op2(env, push? OP_PINT : OP_INT, n); |
| 2611 |
} else { |
| 2612 |
asm_op2c(env, push? OP_PUSHQ : OP_QUOTE, stmt); |
| 2613 |
} |
| 2614 |
if (flags & FLAG_VALUES) |
| 2615 |
flags = (flags & ~FLAG_VALUES) | FLAG_REG0; |
| 2616 |
} |
| 2617 |
return flags; |
| 2618 |
} |
| 2619 |
|
| 2620 |
static int |
| 2621 |
c_quote(cl_env_ptr env, cl_object args, int flags) |
| 2622 |
{ |
| 2623 |
if (ECL_ATOM(args) || ECL_CONS_CDR(args) != ECL_NIL) |
| 2624 |
FEill_formed_input(); |
| 2625 |
return compile_constant(env, ECL_CONS_CAR(args), flags); |
| 2626 |
} |
| 2627 |
|
| 2628 |
static int |
| 2629 |
compile_symbol(cl_env_ptr env, cl_object stmt, int flags) |
| 2630 |
{ |
| 2631 |
cl_object stmt1 = c_macro_expand1(env, stmt); |
| 2632 |
if (stmt1 != stmt) { |
| 2633 |
return compile_form(env, stmt1, flags); |
| 2634 |
} else { |
| 2635 |
struct cl_compiler_ref ref = c_var_ref(env, stmt, FALSE, FALSE); |
| 2636 |
bool push = flags & FLAG_PUSH; |
| 2637 |
int op = c_var_ref_fix_op(ref, push ? OP_PUSHV : OP_VAR); |
| 2638 |
switch (ref.label) { |
| 2639 |
case ECL_CMPVAR_LEXICAL: |
| 2640 |
asm_op2(env, op, ref.index); |
| 2641 |
break; |
| 2642 |
case ECL_CMPVAR_SPECIAL: |
| 2643 |
case ECL_CMPVAR_UNDEFINED: |
| 2644 |
asm_op2c(env, op, stmt); |
| 2645 |
break; |
| 2646 |
default: |
| 2647 |
ecl_miscompilation_error(); |
| 2648 |
} |
| 2649 |
if (flags & FLAG_VALUES) |
| 2650 |
return (flags & ~FLAG_VALUES) | FLAG_REG0; |
| 2651 |
else |
| 2652 |
return flags; |
| 2653 |
} |
| 2654 |
} |
| 2655 |
|
| 2656 |
static int |
| 2657 |
compile_form(cl_env_ptr env, cl_object stmt, int flags) { |
| 2658 |
const cl_compiler_ptr c_env = env->c_env; |
| 2659 |
cl_object function; |
| 2660 |
int new_flags; |
| 2661 |
|
| 2662 |
ecl_bds_bind(env, @'si::*current-form*', stmt); |
| 2663 |
BEGIN: |
| 2664 |
if (c_env->code_walker != OBJNULL) { |
| 2665 |
stmt = funcall(3, c_env->code_walker, stmt, |
| 2666 |
CONS(c_env->variables, c_env->macros)); |
| 2667 |
} |
| 2668 |
/* |
| 2669 |
* First try with variable references and quoted constants |
| 2670 |
*/ |
| 2671 |
if (Null(stmt)) { |
| 2672 |
new_flags = compile_constant(env, stmt, flags); |
| 2673 |
goto OUTPUT; |
| 2674 |
} |
| 2675 |
if (!ECL_LISTP(stmt)) { |
| 2676 |
if (ECL_SYMBOLP(stmt)) { |
| 2677 |
new_flags = compile_symbol(env, stmt, flags); |
| 2678 |
} else { |
| 2679 |
new_flags = compile_constant(env, stmt, flags); |
| 2680 |
} |
| 2681 |
goto OUTPUT; |
| 2682 |
} |
| 2683 |
/* |
| 2684 |
* Next try with special forms. |
| 2685 |
*/ |
| 2686 |
function = ECL_CONS_CAR(stmt); |
| 2687 |
if (ECL_SYMBOLP(function)) { |
| 2688 |
cl_object index = ecl_gethash(function, cl_core.compiler_dispatch); |
| 2689 |
if (index != OBJNULL) { |
| 2690 |
compiler_record *l = database + ecl_fixnum(index); |
| 2691 |
c_env->lexical_level += l->lexical_increment; |
| 2692 |
if (c_env->stepping && function != @'function' && c_env->lexical_level) |
| 2693 |
asm_op2c(env, OP_STEPIN, stmt); |
| 2694 |
new_flags = (*(l->compiler))(env, ECL_CONS_CDR(stmt), flags); |
| 2695 |
if (c_env->stepping && function != @'function' && c_env->lexical_level) |
| 2696 |
asm_op(env, OP_STEPOUT); |
| 2697 |
c_env->lexical_level -= l->lexical_increment; |
| 2698 |
goto OUTPUT; |
| 2699 |
} |
| 2700 |
/* |
| 2701 |
* Next try to macroexpand |
| 2702 |
*/ |
| 2703 |
cl_object new_stmt = c_macro_expand1(env, stmt); |
| 2704 |
if (new_stmt != stmt) { |
| 2705 |
stmt = new_stmt; |
| 2706 |
goto BEGIN; |
| 2707 |
} |
| 2708 |
} |
| 2709 |
/* |
| 2710 |
* Finally resort to ordinary function calls. |
| 2711 |
*/ |
| 2712 |
if (c_env->stepping) { |
| 2713 |
asm_op2c(env, OP_STEPIN, stmt); |
| 2714 |
} |
| 2715 |
c_env->lexical_level++; |
| 2716 |
new_flags = c_call(env, stmt, flags); |
| 2717 |
c_env->lexical_level--; |
| 2718 |
OUTPUT: |
| 2719 |
/* |
| 2720 |
flags new_flags action |
| 2721 |
PUSH PUSH --- |
| 2722 |
PUSH VALUES OP_PUSH |
| 2723 |
PUSH REG0 OP_PUSH |
| 2724 |
VALUES PUSH Impossible |
| 2725 |
VALUES VALUES --- |
| 2726 |
VALUES REG0 OP_VALUEREG0 |
| 2727 |
REG0 PUSH Impossible |
| 2728 |
REG0 VALUES --- |
| 2729 |
REG0 REG0 --- |
| 2730 |
*/ |
| 2731 |
if (flags & FLAG_PUSH) { |
| 2732 |
if (new_flags & (FLAG_REG0 | FLAG_VALUES)) |
| 2733 |
asm_op(env, OP_PUSH); |
| 2734 |
} else if (flags & FLAG_VALUES) { |
| 2735 |
if (new_flags & FLAG_REG0) { |
| 2736 |
asm_op(env, OP_VALUEREG0); |
| 2737 |
} else if (new_flags & FLAG_PUSH) { |
| 2738 |
FEerror("Internal error in bytecodes compiler", 0); |
| 2739 |
} |
| 2740 |
} else if (new_flags & FLAG_PUSH) { |
| 2741 |
FEerror("Internal error in bytecodes compiler", 0); |
| 2742 |
} |
| 2743 |
ecl_bds_unwind1(env); |
| 2744 |
return flags; |
| 2745 |
} |
| 2746 |
|
| 2747 |
static void |
| 2748 |
eval_nontrivial_form(cl_env_ptr env, cl_object form) { |
| 2749 |
const cl_compiler_ptr old_c_env = env->c_env; |
| 2750 |
struct cl_compiler_env new_c_env = *old_c_env; |
| 2751 |
cl_index handle; |
| 2752 |
cl_object bytecodes; |
| 2753 |
struct ecl_stack_frame frame; |
| 2754 |
frame.t = t_frame; |
| 2755 |
frame.opened = 0; |
| 2756 |
frame.base = 0; |
| 2757 |
frame.size = 0; |
| 2758 |
frame.sp = 0; |
| 2759 |
frame.env = env; |
| 2760 |
env->nvalues = 0; |
| 2761 |
env->values[0] = ECL_NIL; |
| 2762 |
new_c_env.constants = si_make_vector(ECL_T, ecl_make_fixnum(16), |
| 2763 |
ECL_T, /* Adjustable */ |
| 2764 |
ecl_make_fixnum(0), /* Fillp */ |
| 2765 |
ECL_NIL, /* displacement */ |
| 2766 |
ECL_NIL); |
| 2767 |
new_c_env.load_time_forms = ECL_NIL; |
| 2768 |
new_c_env.ltf_being_created = ECL_NIL; |
| 2769 |
new_c_env.ltf_defer_init_until = ECL_T; |
| 2770 |
new_c_env.ltf_locations = ECL_NIL; |
| 2771 |
new_c_env.captured = ECL_NIL; |
| 2772 |
new_c_env.parent_env = NULL; |
| 2773 |
new_c_env.env_depth = 0; |
| 2774 |
new_c_env.env_width = 0; |
| 2775 |
new_c_env.env_size = 0; |
| 2776 |
env->c_env = &new_c_env; |
| 2777 |
handle = asm_begin(env); |
| 2778 |
compile_with_load_time_forms(env, form, FLAG_VALUES); |
| 2779 |
if (current_pc(env) != handle) { |
| 2780 |
asm_op(env, OP_EXIT); |
| 2781 |
bytecodes = asm_end(env, handle, form); |
| 2782 |
env->values[0] = ecl_interpret((cl_object)&frame, |
| 2783 |
new_c_env.lex_env, |
| 2784 |
bytecodes); |
| 2785 |
#ifdef GBC_BOEHM |
| 2786 |
GC_FREE(bytecodes->bytecodes.code); |
| 2787 |
GC_FREE(bytecodes); |
| 2788 |
#endif |
| 2789 |
} |
| 2790 |
env->c_env = old_c_env; |
| 2791 |
} |
| 2792 |
|
| 2793 |
static void |
| 2794 |
eval_form(cl_env_ptr env, cl_object form) { |
| 2795 |
if (ECL_LISTP(form) || ECL_SYMBOLP(form)) { |
| 2796 |
eval_nontrivial_form(env, form); |
| 2797 |
} else { |
| 2798 |
env->values[0] = form; |
| 2799 |
env->nvalues = 1; |
| 2800 |
} |
| 2801 |
} |
| 2802 |
|
| 2803 |
static int |
| 2804 |
execute_each_form(cl_env_ptr env, cl_object body) |
| 2805 |
{ |
| 2806 |
cl_object form = ECL_NIL, next_form; |
| 2807 |
for (form = ECL_NIL; !Null(body); form = next_form) { |
| 2808 |
unlikely_if (!ECL_LISTP(body)) |
| 2809 |
FEtype_error_proper_list(body); |
| 2810 |
next_form = ECL_CONS_CAR(body); |
| 2811 |
body = ECL_CONS_CDR(body); |
| 2812 |
eval_form(env, form); |
| 2813 |
} |
| 2814 |
eval_form(env, form); |
| 2815 |
return FLAG_VALUES; |
| 2816 |
} |
| 2817 |
|
| 2818 |
static cl_object |
| 2819 |
save_bytecodes(cl_env_ptr env, cl_index start, cl_index end) |
| 2820 |
{ |
| 2821 |
cl_index l = end - start; |
| 2822 |
cl_object bytecodes = ecl_alloc_simple_vector(l, ecl_aet_index); |
| 2823 |
cl_index *p; |
| 2824 |
for (p = bytecodes->vector.self.index; end > start; end--, p++) { |
| 2825 |
*p = (cl_index)ECL_STACK_POP_UNSAFE(env); |
| 2826 |
} |
| 2827 |
return bytecodes; |
| 2828 |
} |
| 2829 |
|
| 2830 |
static void |
| 2831 |
restore_bytecodes(cl_env_ptr env, cl_object bytecodes) |
| 2832 |
{ |
| 2833 |
cl_index *p = bytecodes->vector.self.index; |
| 2834 |
cl_index l; |
| 2835 |
for (l = bytecodes->vector.dim; l; l--) { |
| 2836 |
ECL_STACK_PUSH(env, (cl_object)p[l-1]); |
| 2837 |
} |
| 2838 |
ecl_dealloc(bytecodes); |
| 2839 |
} |
| 2840 |
|
| 2841 |
static cl_index |
| 2842 |
add_load_form(cl_env_ptr env, cl_object object) |
| 2843 |
{ |
| 2844 |
const cl_compiler_ptr c_env = env->c_env; |
| 2845 |
cl_object constant = pop(&object); |
| 2846 |
cl_object make_form = pop(&object); |
| 2847 |
cl_object init_form = pop(&object); |
| 2848 |
cl_object deferred_init_forms; |
| 2849 |
cl_index loc = c_register_constant(env, constant); |
| 2850 |
{ |
| 2851 |
cl_object previous_locs = c_env->ltf_locations; |
| 2852 |
loop_for_in(previous_locs) { |
| 2853 |
if (ecl_fixnum(ECL_CONS_CAR(previous_locs)) == loc) { |
| 2854 |
/* We already compiled this load time form, nothing to do */ |
| 2855 |
return loc; |
| 2856 |
} |
| 2857 |
} end_loop_for_in; |
| 2858 |
} |
| 2859 |
/* compile the MAKE-FORM */ |
| 2860 |
/* c_env->ltf_being_created holds a list with the constant whose |
| 2861 |
* creation form is being compiled as first element... */ |
| 2862 |
push(ecl_list1(constant), &c_env->ltf_being_created); |
| 2863 |
compile_with_load_time_forms(env, make_form, FLAG_REG0); |
| 2864 |
asm_op2(env, OP_CSET, loc); |
| 2865 |
/* ... and bytecodes for init forms which need to be deferred |
| 2866 |
* until the creation form has been evaluated in the following |
| 2867 |
* elements */ |
| 2868 |
deferred_init_forms = ECL_CONS_CDR(pop(&c_env->ltf_being_created)); |
| 2869 |
/* save the location of the created constant. This also serves as an |
| 2870 |
* indicator that we already compiled the load form for constant and |
| 2871 |
* don't need to do that again if we encouter constant in any other |
| 2872 |
* load time forms. */ |
| 2873 |
push(ecl_make_fixnum(loc), &c_env->ltf_locations); |
| 2874 |
/* compile the INIT-FORM ... */ |
| 2875 |
if (init_form != ECL_NIL) { |
| 2876 |
cl_index handle_init = current_pc(env); |
| 2877 |
cl_object old_init_until = c_env->ltf_defer_init_until; |
| 2878 |
c_env->ltf_defer_init_until = ECL_NIL; |
| 2879 |
compile_with_load_time_forms(env, init_form, FLAG_IGNORE); |
| 2880 |
/* ... and if it needs to be deferred, add it to c_env->ltf_being_created */ |
| 2881 |
if (c_env->ltf_defer_init_until != ECL_NIL |
| 2882 |
&& c_env->ltf_defer_init_until != object) { |
| 2883 |
cl_object bytecodes_init = save_bytecodes(env, handle_init, current_pc(env)); |
| 2884 |
cl_object l = si_memq(c_env->ltf_defer_init_until, c_env->ltf_being_created); |
| 2885 |
if (l != ECL_NIL) { |
| 2886 |
cl_object constant_and_inits = ECL_CONS_CAR(l); |
| 2887 |
ECL_RPLACD(constant_and_inits, |
| 2888 |
CONS(bytecodes_init, ECL_CONS_CDR(constant_and_inits))); |
| 2889 |
} |
| 2890 |
} |
| 2891 |
c_env->ltf_defer_init_until = old_init_until; |
| 2892 |
} |
| 2893 |
/* restore bytecodes for deferred init-forms. This comes after |
| 2894 |
* compiling the init form for constant since we are required to |
| 2895 |
* evaluate init forms as soon as possible. */ |
| 2896 |
loop_for_in(deferred_init_forms) { |
| 2897 |
restore_bytecodes(env, ECL_CONS_CAR(deferred_init_forms)); |
| 2898 |
} end_loop_for_in; |
| 2899 |
return loc; |
| 2900 |
} |
| 2901 |
|
| 2902 |
|
| 2903 |
/* First we compile the form as usual. If some constants need to be built, |
| 2904 |
* insert the code _before_ the actual forms; to do that we first save the |
| 2905 |
* bytecodes for the form, and then we compile forms that build constants; |
| 2906 |
* only after that we restore bytecodes of the compiled form. */ |
| 2907 |
static int |
| 2908 |
compile_with_load_time_forms(cl_env_ptr env, cl_object form, int flags) |
| 2909 |
{ |
| 2910 |
const cl_compiler_ptr c_env = env->c_env; |
| 2911 |
cl_index handle = asm_begin(env); |
| 2912 |
int output_flags = compile_form(env, form, flags); |
| 2913 |
if (c_env->load_time_forms != ECL_NIL) { |
| 2914 |
/* load_time_forms are collected in a reverse order, so we need to reverse |
| 2915 |
the list. Forms should not be compiled as top-level forms - to ensure |
| 2916 |
that we increment the lexical_level. */ |
| 2917 |
cl_object bytecodes = save_bytecodes(env, handle, current_pc(env)); |
| 2918 |
cl_object p = cl_nreverse(c_env->load_time_forms); |
| 2919 |
c_env->load_time_forms = ECL_NIL; |
| 2920 |
c_env->lexical_level++; |
| 2921 |
loop_for_in(p) { |
| 2922 |
add_load_form(env, ECL_CONS_CAR(p)); |
| 2923 |
} end_loop_for_in; |
| 2924 |
c_env->lexical_level--; |
| 2925 |
restore_bytecodes(env, bytecodes); |
| 2926 |
} |
| 2927 |
return output_flags; |
| 2928 |
} |
| 2929 |
|
| 2930 |
static int |
| 2931 |
compile_each_form(cl_env_ptr env, cl_object body, int flags) |
| 2932 |
{ |
| 2933 |
cl_object form = ECL_NIL, next_form; |
| 2934 |
for (form = ECL_NIL; !Null(body); form = next_form) { |
| 2935 |
unlikely_if (!ECL_LISTP(body)) |
| 2936 |
FEtype_error_proper_list(body); |
| 2937 |
next_form = ECL_CONS_CAR(body); |
| 2938 |
body = ECL_CONS_CDR(body); |
| 2939 |
compile_with_load_time_forms(env, form, FLAG_IGNORE); |
| 2940 |
} |
| 2941 |
return compile_with_load_time_forms(env, form, flags); |
| 2942 |
} |
| 2943 |
|
| 2944 |
static int |
| 2945 |
compile_toplevel_body(cl_env_ptr env, cl_object body, int flags) |
| 2946 |
{ |
| 2947 |
const cl_compiler_ptr c_env = env->c_env; |
| 2948 |
if (!c_env->lexical_level) { |
| 2949 |
if (c_env->mode == FLAG_EXECUTE) |
| 2950 |
return execute_each_form(env, body); |
| 2951 |
else |
| 2952 |
return compile_each_form(env, body, flags); |
| 2953 |
} else { |
| 2954 |
return compile_body(env, body, flags); |
| 2955 |
} |
| 2956 |
} |
| 2957 |
|
| 2958 |
static int |
| 2959 |
compile_body(cl_env_ptr env, cl_object body, int flags) |
| 2960 |
{ |
| 2961 |
cl_object form = ECL_NIL, next_form; |
| 2962 |
for (form = ECL_NIL; !Null(body); form = next_form) { |
| 2963 |
unlikely_if (!ECL_LISTP(body)) |
| 2964 |
FEtype_error_proper_list(body); |
| 2965 |
next_form = ECL_CONS_CAR(body); |
| 2966 |
body = ECL_CONS_CDR(body); |
| 2967 |
compile_form(env, form, FLAG_IGNORE); |
| 2968 |
} |
| 2969 |
return compile_form(env, form, flags); |
| 2970 |
} |
| 2971 |
|
| 2972 |
/* ------------------------ INLINED FUNCTIONS -------------------------------- */ |
| 2973 |
|
| 2974 |
static int |
| 2975 |
c_cons(cl_env_ptr env, cl_object args, int flags) |
| 2976 |
{ |
| 2977 |
if (ecl_length(args) != 2) { |
| 2978 |
FEprogram_error("CONS: Wrong number of arguments", 0); |
| 2979 |
} |
| 2980 |
compile_form(env, cl_first(args), FLAG_PUSH); |
| 2981 |
compile_form(env, cl_second(args), FLAG_REG0); |
| 2982 |
asm_op(env, OP_CONS); |
| 2983 |
return FLAG_REG0; |
| 2984 |
} |
| 2985 |
|
| 2986 |
static int |
| 2987 |
c_endp(cl_env_ptr env, cl_object args, int flags) |
| 2988 |
{ |
| 2989 |
cl_object list = pop(&args); |
| 2990 |
if (args != ECL_NIL) { |
| 2991 |
FEprogram_error("ENDP: Too many arguments", 0); |
| 2992 |
} |
| 2993 |
compile_form(env, list, FLAG_REG0); |
| 2994 |
asm_op(env, OP_ENDP); |
| 2995 |
return FLAG_REG0; |
| 2996 |
} |
| 2997 |
|
| 2998 |
static int |
| 2999 |
c_car(cl_env_ptr env, cl_object args, int flags) |
| 3000 |
{ |
| 3001 |
cl_object list = pop(&args); |
| 3002 |
if (args != ECL_NIL) { |
| 3003 |
FEprogram_error("CAR: Too many arguments", 0); |
| 3004 |
} |
| 3005 |
compile_form(env, list, FLAG_REG0); |
| 3006 |
asm_op(env, OP_CAR); |
| 3007 |
return FLAG_REG0; |
| 3008 |
} |
| 3009 |
|
| 3010 |
static int |
| 3011 |
c_cdr(cl_env_ptr env, cl_object args, int flags) |
| 3012 |
{ |
| 3013 |
cl_object list = pop(&args); |
| 3014 |
if (args != ECL_NIL) { |
| 3015 |
FEprogram_error("CDR: Too many arguments", 0); |
| 3016 |
} |
| 3017 |
compile_form(env, list, FLAG_REG0); |
| 3018 |
asm_op(env, OP_CDR); |
| 3019 |
return FLAG_REG0; |
| 3020 |
} |
| 3021 |
|
| 3022 |
static int |
| 3023 |
c_list_listA(cl_env_ptr env, cl_object args, int flags, int op) |
| 3024 |
{ |
| 3025 |
cl_index n = ecl_length(args); |
| 3026 |
if (n == 0) { |
| 3027 |
return compile_form(env, ECL_NIL, flags); |
| 3028 |
} else { |
| 3029 |
while (ECL_CONS_CDR(args) != ECL_NIL) { |
| 3030 |
compile_form(env, ECL_CONS_CAR(args), FLAG_PUSH); |
| 3031 |
args = ECL_CONS_CDR(args); |
| 3032 |
} |
| 3033 |
compile_form(env, ECL_CONS_CAR(args), FLAG_REG0); |
| 3034 |
asm_op2(env, op, n); |
| 3035 |
return FLAG_REG0; |
| 3036 |
} |
| 3037 |
} |
| 3038 |
|
| 3039 |
static int |
| 3040 |
c_list(cl_env_ptr env, cl_object args, int flags) |
| 3041 |
{ |
| 3042 |
return c_list_listA(env, args, flags, OP_LIST); |
| 3043 |
} |
| 3044 |
|
| 3045 |
static int |
| 3046 |
c_listA(cl_env_ptr env, cl_object args, int flags) |
| 3047 |
{ |
| 3048 |
return c_list_listA(env, args, flags, OP_LISTA); |
| 3049 |
} |
| 3050 |
|
| 3051 |
/* -- Primops --------------------------------------------------------------- */ |
| 3052 |
|
| 3053 |
static int |
| 3054 |
c_cons_car(cl_env_ptr env, cl_object args, int flags) |
| 3055 |
{ |
| 3056 |
cl_object list = pop(&args); |
| 3057 |
if (args != ECL_NIL) { |
| 3058 |
FEprogram_error("CAR: Too many arguments", 0); |
| 3059 |
} |
| 3060 |
compile_form(env, list, FLAG_REG0); |
| 3061 |
asm_op(env, OP_CONS_CAR); |
| 3062 |
return FLAG_REG0; |
| 3063 |
} |
| 3064 |
|
| 3065 |
static int |
| 3066 |
c_cons_cdr(cl_env_ptr env, cl_object args, int flags) |
| 3067 |
{ |
| 3068 |
cl_object list = pop(&args); |
| 3069 |
if (args != ECL_NIL) { |
| 3070 |
FEprogram_error("CDR: Too many arguments", 0); |
| 3071 |
} |
| 3072 |
compile_form(env, list, FLAG_REG0); |
| 3073 |
asm_op(env, OP_CONS_CDR); |
| 3074 |
return FLAG_REG0; |
| 3075 |
} |
| 3076 |
|
| 3077 |
/* ----------------------------- PUBLIC INTERFACE ---------------------------- */ |
| 3078 |
|
| 3079 |
/* ------------------------------------------------------------ |
| 3080 |
LAMBDA OBJECTS: An interpreted function is a vector made of |
| 3081 |
the following components |
| 3082 |
|
| 3083 |
#(LAMBDA |
| 3084 |
{block-name | NIL} |
| 3085 |
{variable-env | NIL} |
| 3086 |
{function-env | NIL} |
| 3087 |
{block-env | NIL} |
| 3088 |
(list of variables declared special) |
| 3089 |
Nreq {var}* ; required arguments |
| 3090 |
Nopt {var value flag}* ; optional arguments |
| 3091 |
{rest-var NIL} ; rest variable |
| 3092 |
{T | NIL} ; allow other keys? |
| 3093 |
Nkey {key var value flag}* ; keyword arguments |
| 3094 |
Naux {var init} ; auxiliary variables |
| 3095 |
documentation-string |
| 3096 |
list-of-declarations |
| 3097 |
{form}* ; body) |
| 3098 |
|
| 3099 |
------------------------------------------------------------ */ |
| 3100 |
|
| 3101 |
/* |
| 3102 |
Determine whether the form can be externalized using the lisp |
| 3103 |
printer or we should rather use MAKE-LOAD-FORM. |
| 3104 |
*/ |
| 3105 |
cl_object |
| 3106 |
si_need_to_make_load_form_p(cl_object object) |
| 3107 |
{ |
| 3108 |
cl_object load_form_cache = cl__make_hash_table(@'eq', |
| 3109 |
ecl_make_fixnum(16), |
| 3110 |
cl_core.rehash_size, |
| 3111 |
cl_core.rehash_threshold); |
| 3112 |
cl_object waiting_objects = ecl_list1(object); |
| 3113 |
cl_type type = t_start; |
| 3114 |
|
| 3115 |
loop: |
| 3116 |
if (waiting_objects == ECL_NIL) |
| 3117 |
return ECL_NIL; |
| 3118 |
object = pop(&waiting_objects); |
| 3119 |
type = ecl_t_of(object); |
| 3120 |
/* For simple, atomic objects we just return NIL. There is no need |
| 3121 |
to call MAKE-LOAD-FORM on them. */ |
| 3122 |
switch (type) { |
| 3123 |
case t_character: |
| 3124 |
case t_fixnum: |
| 3125 |
case t_bignum: |
| 3126 |
case t_ratio: |
| 3127 |
case t_singlefloat: |
| 3128 |
case t_doublefloat: |
| 3129 |
case t_longfloat: |
| 3130 |
case t_complex: |
| 3131 |
#ifdef ECL_COMPLEX_FLOAT |
| 3132 |
case t_csfloat: |
| 3133 |
case t_cdfloat: |
| 3134 |
case t_clfloat: |
| 3135 |
#endif |
| 3136 |
#ifdef ECL_SSE2 |
| 3137 |
case t_sse_pack: |
| 3138 |
#endif |
| 3139 |
case t_symbol: |
| 3140 |
case t_pathname: |
| 3141 |
#ifdef ECL_UNICODE |
| 3142 |
case t_string: |
| 3143 |
#endif |
| 3144 |
case t_base_string: |
| 3145 |
case t_bitvector: |
| 3146 |
goto loop; |
| 3147 |
case t_list: |
| 3148 |
if (Null(object)) goto loop; |
| 3149 |
default: |
| 3150 |
; |
| 3151 |
} |
| 3152 |
/* For compound objects we set up a cache and run through the objects content |
| 3153 |
looking for components that require MAKE-LOAD-FORM to be externalized. The |
| 3154 |
cache is used to solve the problem of circularity and of EQ references. */ |
| 3155 |
if(ecl_gethash(object, load_form_cache)) |
| 3156 |
goto loop; |
| 3157 |
ecl_sethash(object, load_form_cache, ECL_T); |
| 3158 |
switch (type) { |
| 3159 |
case t_array: |
| 3160 |
case t_vector: |
| 3161 |
if (object->array.elttype == ecl_aet_object) { |
| 3162 |
cl_index i = 0; |
| 3163 |
for(; i<object->array.dim; i++) { |
| 3164 |
push(object->array.self.t[i], &waiting_objects); |
| 3165 |
} |
| 3166 |
} |
| 3167 |
goto loop; |
| 3168 |
case t_list: |
| 3169 |
push(ECL_CONS_CDR(object), &waiting_objects); |
| 3170 |
push(ECL_CONS_CAR(object), &waiting_objects); |
| 3171 |
goto loop; |
| 3172 |
case t_bclosure: { |
| 3173 |
cl_object bc = object->bclosure.code; |
| 3174 |
push(object->bclosure.lex, &waiting_objects); |
| 3175 |
push(bc->bytecodes.data, &waiting_objects); |
| 3176 |
push(bc->bytecodes.flex, &waiting_objects); |
| 3177 |
push(bc->bytecodes.name, &waiting_objects); |
| 3178 |
goto loop; |
| 3179 |
} |
| 3180 |
case t_bytecodes: |
| 3181 |
push(object->bytecodes.data, &waiting_objects); |
| 3182 |
push(object->bytecodes.flex, &waiting_objects); |
| 3183 |
push(object->bytecodes.name, &waiting_objects); |
| 3184 |
goto loop; |
| 3185 |
default: |
| 3186 |
return ECL_T; |
| 3187 |
} |
| 3188 |
_ecl_unexpected_return(); |
| 3189 |
} |
| 3190 |
|
| 3191 |
/* |
| 3192 |
Handles special declarations, removes declarations from body |
| 3193 |
*/ |
| 3194 |
@(defun si::process-declarations (body &optional doc) |
| 3195 |
cl_object documentation = ECL_NIL, declarations = ECL_NIL, specials = ECL_NIL; |
| 3196 |
@ |
| 3197 |
for (; !Null(body); body = ECL_CONS_CDR(body)) { |
| 3198 |
cl_object form; |
| 3199 |
unlikely_if (!ECL_LISTP(body)) |
| 3200 |
FEill_formed_input(); |
| 3201 |
form = ECL_CONS_CAR(body); |
| 3202 |
if (!Null(doc) && ecl_stringp(form) && !Null(ECL_CONS_CDR(body))) { |
| 3203 |
if (documentation != ECL_NIL) |
| 3204 |
break; |
| 3205 |
documentation = form; |
| 3206 |
continue; |
| 3207 |
} |
| 3208 |
if (ECL_ATOM(form) || (ECL_CONS_CAR(form) != @'declare')) { |
| 3209 |
break; |
| 3210 |
} |
| 3211 |
for (form = ECL_CONS_CDR(form); !Null(form); ) { |
| 3212 |
cl_object sentence = pop(&form); |
| 3213 |
push(sentence, &declarations); |
| 3214 |
if (pop(&sentence) == @'special') { |
| 3215 |
while (!Null(sentence)) { |
| 3216 |
cl_object v = pop(&sentence); |
| 3217 |
assert_type_symbol(v); |
| 3218 |
specials = push(v, &specials); |
| 3219 |
} |
| 3220 |
} |
| 3221 |
} |
| 3222 |
} |
| 3223 |
@(return cl_nreverse(declarations) body documentation specials); |
| 3224 |
@) |
| 3225 |
|
| 3226 |
cl_object |
| 3227 |
si_process_lambda(cl_object lambda) |
| 3228 |
{ |
| 3229 |
cl_object documentation, declarations, specials; |
| 3230 |
cl_object lambda_list, body; |
| 3231 |
const cl_env_ptr env = ecl_process_env(); |
| 3232 |
unlikely_if (ECL_ATOM(lambda)) |
| 3233 |
FEprogram_error("LAMBDA: No lambda list.", 0); |
| 3234 |
|
| 3235 |
lambda_list = ECL_CONS_CAR(lambda); |
| 3236 |
body = ECL_CONS_CDR(lambda); |
| 3237 |
declarations = @si::process-declarations(2, body, ECL_T); |
| 3238 |
body = env->values[1]; |
| 3239 |
documentation = env->values[2]; |
| 3240 |
specials = env->values[3]; |
| 3241 |
|
| 3242 |
lambda_list = si_process_lambda_list(lambda_list, @'function'); |
| 3243 |
{ |
| 3244 |
cl_index n = env->nvalues; |
| 3245 |
env->values[0] = lambda_list; |
| 3246 |
env->values[n++] = documentation; |
| 3247 |
env->values[n++] = specials; |
| 3248 |
env->values[n++] = declarations; |
| 3249 |
env->values[n++] = body; |
| 3250 |
env->nvalues = n; |
| 3251 |
} |
| 3252 |
return lambda_list; |
| 3253 |
} |
| 3254 |
|
| 3255 |
/* |
| 3256 |
* (si::process-lambda-list lambda-list context) |
| 3257 |
* |
| 3258 |
* Parses different types of lambda lists. CONTEXT may be MACRO, |
| 3259 |
* FTYPE, FUNCTION, METHOD or DESTRUCTURING-BIND, and determines the |
| 3260 |
* valid sytax. The output is made of several values: |
| 3261 |
* |
| 3262 |
* VALUES(0) = (N req1 ... ) ; required values |
| 3263 |
* VALUES(1) = (N opt1 init1 flag1 ... ) ; optional values |
| 3264 |
* VALUES(2) = rest-var ; rest-variable, if any |
| 3265 |
* VALUES(3) = key-flag ; T if &key was supplied |
| 3266 |
* VALUES(4) = (N key1 var1 init1 flag1 ... ) ; keyword arguments |
| 3267 |
* VALUES(5) = allow-other-keys ; flag &allow-other-keys |
| 3268 |
* VALUES(6) = (N aux1 init1 ... ) ; auxiliary variables |
| 3269 |
* |
| 3270 |
* 1) The prefix "N" is an integer value denoting the number of variables |
| 3271 |
* which are declared within this section of the lambda list. |
| 3272 |
* |
| 3273 |
* 2) The INIT* arguments are lisp forms which are evaluated when no value is |
| 3274 |
* provided. |
| 3275 |
* |
| 3276 |
* 3) The FLAG* arguments is the name of a variable which holds a boolean |
| 3277 |
* value in case an optional or keyword argument was provided. If it is NIL, |
| 3278 |
* no such variable exists. |
| 3279 |
*/ |
| 3280 |
|
| 3281 |
cl_object |
| 3282 |
si_process_lambda_list(cl_object org_lambda_list, cl_object context) |
| 3283 |
{ |
| 3284 |
#define push(v,l) { cl_object c = *l = CONS(v, *l); l = &ECL_CONS_CDR(c); } |
| 3285 |
#define assert_var_name(v) \ |
| 3286 |
if (context == @'function') { \ |
| 3287 |
unlikely_if (ecl_symbol_type(v) & ecl_stp_constant) \ |
| 3288 |
FEillegal_variable_name(v); } |
| 3289 |
cl_object lists[4] = {ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL}; |
| 3290 |
cl_object *reqs = lists, *opts = lists+1, *keys = lists+2, *auxs = lists+3; |
| 3291 |
cl_object v, rest = ECL_NIL, lambda_list = org_lambda_list; |
| 3292 |
int nreq = 0, nopt = 0, nkey = 0, naux = 0; |
| 3293 |
cl_object allow_other_keys = ECL_NIL; |
| 3294 |
cl_object key_flag = ECL_NIL; |
| 3295 |
enum { AT_REQUIREDS, |
| 3296 |
AT_OPTIONALS, |
| 3297 |
AT_REST, |
| 3298 |
AT_KEYS, |
| 3299 |
AT_OTHER_KEYS, |
| 3300 |
AT_AUXS |
| 3301 |
} stage = AT_REQUIREDS; |
| 3302 |
|
| 3303 |
if (!ECL_LISTP(lambda_list)) |
| 3304 |
goto ILLEGAL_LAMBDA; |
| 3305 |
LOOP: |
| 3306 |
if (Null(lambda_list)) |
| 3307 |
goto OUTPUT; |
| 3308 |
if (!ECL_LISTP(lambda_list)) { |
| 3309 |
unlikely_if (context == @'function' || context == @'ftype') |
| 3310 |
goto ILLEGAL_LAMBDA; |
| 3311 |
v = lambda_list; |
| 3312 |
lambda_list = ECL_NIL; |
| 3313 |
goto REST; |
| 3314 |
} |
| 3315 |
v = ECL_CONS_CAR(lambda_list); |
| 3316 |
lambda_list = ECL_CONS_CDR(lambda_list); |
| 3317 |
if (v == @'&optional') { |
| 3318 |
unlikely_if (stage >= AT_OPTIONALS) |
| 3319 |
goto ILLEGAL_LAMBDA; |
| 3320 |
stage = AT_OPTIONALS; |
| 3321 |
goto LOOP; |
| 3322 |
} |
| 3323 |
if (v == @'&rest' |
| 3324 |
|| (v == @'&body' |
| 3325 |
&& (context == @'si::macro' || context == @'destructuring-bind'))) { |
| 3326 |
unlikely_if (ECL_ATOM(lambda_list)) |
| 3327 |
goto ILLEGAL_LAMBDA; |
| 3328 |
v = ECL_CONS_CAR(lambda_list); |
| 3329 |
lambda_list = ECL_CONS_CDR(lambda_list); |
| 3330 |
REST: unlikely_if (stage >= AT_REST) |
| 3331 |
goto ILLEGAL_LAMBDA; |
| 3332 |
stage = AT_REST; |
| 3333 |
rest = v; |
| 3334 |
goto LOOP; |
| 3335 |
} |
| 3336 |
if (v == @'&key') { |
| 3337 |
unlikely_if (stage >= AT_KEYS) |
| 3338 |
goto ILLEGAL_LAMBDA; |
| 3339 |
key_flag = ECL_T; |
| 3340 |
stage = AT_KEYS; |
| 3341 |
goto LOOP; |
| 3342 |
} |
| 3343 |
if (v == @'&aux') { |
| 3344 |
unlikely_if (stage >= AT_AUXS) |
| 3345 |
goto ILLEGAL_LAMBDA; |
| 3346 |
stage = AT_AUXS; |
| 3347 |
goto LOOP; |
| 3348 |
} |
| 3349 |
if (v == @'&allow-other-keys') { |
| 3350 |
allow_other_keys = ECL_T; |
| 3351 |
unlikely_if (stage != AT_KEYS) |
| 3352 |
goto ILLEGAL_LAMBDA; |
| 3353 |
stage = AT_OTHER_KEYS; |
| 3354 |
goto LOOP; |
| 3355 |
} |
| 3356 |
switch (stage) { |
| 3357 |
case AT_REQUIREDS: |
| 3358 |
nreq++; |
| 3359 |
assert_var_name(v); |
| 3360 |
if (context == @'function' && ecl_member_eq(v, lists[0])) |
| 3361 |
/* note: ftype isn't valid context for this check */ |
| 3362 |
FEprogram_error |
| 3363 |
("The variable ~s occurs more than once as the " |
| 3364 |
"required parameter in the lambda list.", 1, v); |
| 3365 |
push(v, reqs); |
| 3366 |
break; |
| 3367 |
case AT_OPTIONALS: { |
| 3368 |
cl_object spp = ECL_NIL; |
| 3369 |
cl_object init = ECL_NIL; |
| 3370 |
if (!ECL_ATOM(v) && (context != @'ftype')) { |
| 3371 |
cl_object x = v; |
| 3372 |
unlikely_if (!ECL_LISTP(x)) goto ILLEGAL_LAMBDA; |
| 3373 |
v = ECL_CONS_CAR(x); |
| 3374 |
x = ECL_CONS_CDR(x); |
| 3375 |
if (!Null(x)) { |
| 3376 |
unlikely_if (!ECL_LISTP(x)) goto ILLEGAL_LAMBDA; |
| 3377 |
init = ECL_CONS_CAR(x); |
| 3378 |
x = ECL_CONS_CDR(x); |
| 3379 |
if (!Null(x)) { |
| 3380 |
unlikely_if (!ECL_LISTP(x)) goto ILLEGAL_LAMBDA; |
| 3381 |
spp = ECL_CONS_CAR(x); |
| 3382 |
x = ECL_CONS_CDR(x); |
| 3383 |
if (spp != ECL_NIL) assert_var_name(spp); |
| 3384 |
unlikely_if (!Null(x)) |
| 3385 |
goto ILLEGAL_LAMBDA; |
| 3386 |
} |
| 3387 |
} |
| 3388 |
} |
| 3389 |
nopt++; |
| 3390 |
assert_var_name(v); |
| 3391 |
push(v, opts); |
| 3392 |
push(init, opts); |
| 3393 |
push(spp, opts); |
| 3394 |
break; |
| 3395 |
} |
| 3396 |
case AT_REST: |
| 3397 |
/* If we get here, the user has declared more than one |
| 3398 |
* &rest variable, as in (lambda (&rest x y) ...) */ |
| 3399 |
goto ILLEGAL_LAMBDA; |
| 3400 |
case AT_KEYS: { |
| 3401 |
cl_object init = ECL_NIL; |
| 3402 |
cl_object spp = ECL_NIL; |
| 3403 |
cl_object key; |
| 3404 |
if (context == @'ftype') { |
| 3405 |
unlikely_if (ECL_ATOM(v)) |
| 3406 |
goto ILLEGAL_LAMBDA; |
| 3407 |
key = ECL_CONS_CAR(v); |
| 3408 |
v = CADR(v); |
| 3409 |
goto KEY_PUSH; |
| 3410 |
} |
| 3411 |
if (!ECL_ATOM(v)) { |
| 3412 |
cl_object x = v; |
| 3413 |
v = ECL_CONS_CAR(x); |
| 3414 |
x = ECL_CONS_CDR(x); |
| 3415 |
if (!Null(x)) { |
| 3416 |
unlikely_if (!ECL_LISTP(x)) goto ILLEGAL_LAMBDA; |
| 3417 |
init = ECL_CONS_CAR(x); |
| 3418 |
x = ECL_CONS_CDR(x); |
| 3419 |
if (!Null(x)) { |
| 3420 |
unlikely_if (!ECL_LISTP(x)) goto ILLEGAL_LAMBDA; |
| 3421 |
spp = ECL_CONS_CAR(x); |
| 3422 |
x = ECL_CONS_CDR(x); |
| 3423 |
unlikely_if (!Null(x)) |
| 3424 |
goto ILLEGAL_LAMBDA; |
| 3425 |
if (spp != ECL_NIL) assert_var_name(spp); |
| 3426 |
} |
| 3427 |
} |
| 3428 |
} |
| 3429 |
if (CONSP(v)) { |
| 3430 |
key = ECL_CONS_CAR(v); |
| 3431 |
v = ECL_CONS_CDR(v); |
| 3432 |
unlikely_if (ECL_ATOM(v) || !Null(ECL_CONS_CDR(v))) |
| 3433 |
goto ILLEGAL_LAMBDA; |
| 3434 |
v = ECL_CONS_CAR(v); |
| 3435 |
if (context == @'function') |
| 3436 |
assert_type_symbol(v); |
| 3437 |
assert_type_symbol(key); |
| 3438 |
} else { |
| 3439 |
int intern_flag; |
| 3440 |
key = ecl_intern(ecl_symbol_name(v), cl_core.keyword_package, |
| 3441 |
&intern_flag); |
| 3442 |
} |
| 3443 |
KEY_PUSH: |
| 3444 |
nkey++; |
| 3445 |
push(key, keys); |
| 3446 |
assert_var_name(v); |
| 3447 |
push(v, keys); |
| 3448 |
push(init, keys); |
| 3449 |
push(spp, keys); |
| 3450 |
break; |
| 3451 |
} |
| 3452 |
default: { |
| 3453 |
cl_object init; |
| 3454 |
if (ECL_ATOM(v)) { |
| 3455 |
init = ECL_NIL; |
| 3456 |
} else if (Null(CDDR(v))) { |
| 3457 |
cl_object x = v; |
| 3458 |
v = ECL_CONS_CAR(x); |
| 3459 |
init = CADR(x); |
| 3460 |
} else |
| 3461 |
goto ILLEGAL_LAMBDA; |
| 3462 |
naux++; |
| 3463 |
assert_var_name(v); |
| 3464 |
push(v, auxs); |
| 3465 |
push(init, auxs); |
| 3466 |
} |
| 3467 |
} |
| 3468 |
goto LOOP; |
| 3469 |
|
| 3470 |
OUTPUT: |
| 3471 |
if ((nreq+nopt+(!Null(rest))+nkey) >= ECL_CALL_ARGUMENTS_LIMIT) |
| 3472 |
FEprogram_error("LAMBDA: Argument list is too long, ~S.", 1, org_lambda_list); |
| 3473 |
|
| 3474 |
@(return |
| 3475 |
CONS(ecl_make_fixnum(nreq), lists[0]) |
| 3476 |
CONS(ecl_make_fixnum(nopt), lists[1]) |
| 3477 |
rest |
| 3478 |
key_flag |
| 3479 |
CONS(ecl_make_fixnum(nkey), lists[2]) |
| 3480 |
allow_other_keys |
| 3481 |
CONS(ecl_make_fixnum(naux), lists[3])) |
| 3482 |
|
| 3483 |
ILLEGAL_LAMBDA: |
| 3484 |
FEprogram_error("LAMBDA: Illegal lambda list ~S.", 1, org_lambda_list); |
| 3485 |
|
| 3486 |
#undef push |
| 3487 |
#undef assert_var_name |
| 3488 |
} |
| 3489 |
|
| 3490 |
static void |
| 3491 |
c_default(cl_env_ptr env, cl_object var, cl_object stmt, cl_object flag, cl_object specials) |
| 3492 |
{ |
| 3493 |
/* Flag is in REG0, value, if it exists, in stack */ |
| 3494 |
cl_index label; |
| 3495 |
label = asm_jmp(env, OP_JT); |
| 3496 |
compile_form(env, stmt, FLAG_PUSH); |
| 3497 |
if (Null(flag)) { |
| 3498 |
asm_complete(env, OP_JT, label); |
| 3499 |
} else { |
| 3500 |
compile_form(env, ECL_NIL, FLAG_REG0); |
| 3501 |
asm_complete(env, OP_JT, label); |
| 3502 |
c_bind(env, flag, specials); |
| 3503 |
} |
| 3504 |
c_pbind(env, var, specials); |
| 3505 |
} |
| 3506 |
|
| 3507 |
static cl_object |
| 3508 |
fix_macro_to_lexenv(cl_env_ptr env, cl_object record) { |
| 3509 |
cl_object arg1 = pop(&record); |
| 3510 |
cl_object arg2 = pop(&record); |
| 3511 |
cl_object arg3 = pop(&record); |
| 3512 |
if (arg2 == @'si::macro') { |
| 3513 |
c_mac_ref(env, arg1); |
| 3514 |
return CONS(arg2, CONS(arg3, arg1)); |
| 3515 |
} else if (arg2 == @'si::symbol-macro') { |
| 3516 |
c_sym_ref(env, arg1); |
| 3517 |
return CONS(arg2, CONS(arg3, arg1)); |
| 3518 |
} else { |
| 3519 |
return ECL_NIL; |
| 3520 |
} |
| 3521 |
} |
| 3522 |
|
| 3523 |
cl_object |
| 3524 |
ecl_make_lambda(cl_env_ptr env, cl_object name, cl_object lambda) { |
| 3525 |
cl_object reqs, opts, rest, key, keys, auxs, allow_other_keys; |
| 3526 |
cl_object specials, decl, body, output; |
| 3527 |
cl_object cfb = ECL_NIL, captured; |
| 3528 |
cl_index handle; |
| 3529 |
struct cl_compiler_env *old_c_env, new_c_env[1]; |
| 3530 |
|
| 3531 |
ecl_bds_bind(env, @'si::*current-form*', |
| 3532 |
@list*(3, @'ext::lambda-block', name, lambda)); |
| 3533 |
|
| 3534 |
old_c_env = env->c_env; |
| 3535 |
c_new_env(env, new_c_env, old_c_env); |
| 3536 |
new_c_env->lexical_level++; |
| 3537 |
reqs = si_process_lambda(lambda); |
| 3538 |
opts = env->values[1]; |
| 3539 |
rest = env->values[2]; |
| 3540 |
key = env->values[3]; |
| 3541 |
keys = env->values[4]; |
| 3542 |
allow_other_keys = env->values[5]; |
| 3543 |
auxs = env->values[6]; |
| 3544 |
/* doc = env->values[7]; unused */; |
| 3545 |
specials = env->values[8]; |
| 3546 |
decl = env->values[9]; |
| 3547 |
body = env->values[10]; |
| 3548 |
|
| 3549 |
handle = asm_begin(env); |
| 3550 |
|
| 3551 |
/* Transform (SETF fname) => fname */ |
| 3552 |
if (!Null(name) && Null(si_valid_function_name_p(name))) |
| 3553 |
FEprogram_error("LAMBDA: Not a valid function name ~S.",1,name); |
| 3554 |
|
| 3555 |
/* We register the function boundary. We use this mark in both variables and |
| 3556 |
* macros for code-walking. */ |
| 3557 |
c_register_boundary(env, @'si::function-boundary'); |
| 3558 |
|
| 3559 |
reqs = ECL_CONS_CDR(reqs); /* Required arguments */ |
| 3560 |
while (!Null(reqs)) { |
| 3561 |
cl_object var = pop(&reqs); |
| 3562 |
asm_op(env, OP_POPREQ); |
| 3563 |
c_bind(env, var, specials); |
| 3564 |
} |
| 3565 |
opts = ECL_CONS_CDR(opts); |
| 3566 |
while (!Null(opts)) { /* Optional arguments */ |
| 3567 |
cl_object var = pop(&opts); |
| 3568 |
cl_object stmt = pop(&opts); |
| 3569 |
cl_object flag = pop(&opts); |
| 3570 |
asm_op(env, OP_POPOPT); |
| 3571 |
c_default(env, var, stmt, flag, specials); |
| 3572 |
} |
| 3573 |
if (Null(rest) && Null(key)) { /* Check no excess arguments */ |
| 3574 |
asm_op(env, OP_NOMORE); |
| 3575 |
} |
| 3576 |
if (!Null(rest)) { /* &rest argument */ |
| 3577 |
asm_op(env, OP_POPREST); |
| 3578 |
c_bind(env, rest, specials); |
| 3579 |
} |
| 3580 |
if (!Null(key)) { |
| 3581 |
cl_object aux = CONS(allow_other_keys,ECL_NIL); |
| 3582 |
cl_object names = ECL_NIL; |
| 3583 |
asm_op2c(env, OP_PUSHKEYS, aux); |
| 3584 |
keys = ECL_CONS_CDR(keys); |
| 3585 |
while (!Null(keys)) { |
| 3586 |
cl_object name = pop(&keys); |
| 3587 |
cl_object var = pop(&keys); |
| 3588 |
cl_object stmt = pop(&keys); |
| 3589 |
cl_object flag = pop(&keys); |
| 3590 |
names = CONS(name, names); |
| 3591 |
asm_op(env, OP_POP); |
| 3592 |
c_default(env, var, stmt, flag, specials); |
| 3593 |
} |
| 3594 |
ECL_RPLACD(aux, names); |
| 3595 |
} |
| 3596 |
auxs = ECL_CONS_CDR(auxs); |
| 3597 |
while (!Null(auxs)) { /* Local bindings */ |
| 3598 |
cl_object var = pop(&auxs); |
| 3599 |
cl_object value = pop(&auxs); |
| 3600 |
compile_form(env, value, FLAG_REG0); |
| 3601 |
c_bind(env, var, specials); |
| 3602 |
} |
| 3603 |
c_declare_specials(env, specials); |
| 3604 |
|
| 3605 |
if (!Null(name)) { |
| 3606 |
compile_form(env, @list*(3, @'block', si_function_block_name(name), |
| 3607 |
body), FLAG_VALUES); |
| 3608 |
} else { |
| 3609 |
while (!Null(decl)) { |
| 3610 |
cl_object l = ECL_CONS_CAR(decl); |
| 3611 |
if (ECL_CONSP(l) && ECL_CONS_CAR(l) == @'si::function-block-name') { |
| 3612 |
name = ECL_CONS_CAR(ECL_CONS_CDR(l)); |
| 3613 |
break; |
| 3614 |
} |
| 3615 |
decl = ECL_CONS_CDR(decl); |
| 3616 |
} |
| 3617 |
compile_body(env, body, FLAG_VALUES); |
| 3618 |
} |
| 3619 |
|
| 3620 |
/* Only undo special bindings */ |
| 3621 |
c_undo_bindings(env, old_c_env->variables, 1); |
| 3622 |
asm_op(env, OP_EXIT); |
| 3623 |
|
| 3624 |
if (Null(ecl_cmp_symbol_value(env, @'si::*keep-definitions*'))) |
| 3625 |
lambda = ECL_NIL; |
| 3626 |
output = asm_end(env, handle, lambda); |
| 3627 |
output->bytecodes.name = name; |
| 3628 |
output->bytecodes.flex = ECL_NIL; |
| 3629 |
output->bytecodes.nlcl = ecl_make_fixnum(new_c_env->env_width); |
| 3630 |
|
| 3631 |
old_c_env->load_time_forms = new_c_env->load_time_forms; |
| 3632 |
|
| 3633 |
c_restore_env(env, new_c_env, old_c_env); |
| 3634 |
ecl_bds_unwind1(env); /* @'si::*current-form*', */ |
| 3635 |
|
| 3636 |
/* Process closed over entries. */ |
| 3637 |
captured = new_c_env->captured; |
| 3638 |
if (!Null(captured)) { |
| 3639 |
cl_object flex, entry, macro_entry; |
| 3640 |
struct cl_compiler_ref ref; |
| 3641 |
int i, n; |
| 3642 |
n = captured->vector.fillp; |
| 3643 |
flex = si_make_vector(ECL_T, ecl_make_fixnum(n), |
| 3644 |
ECL_NIL, ECL_NIL, ECL_NIL, ECL_NIL); |
| 3645 |
output->bytecodes.flex = flex; |
| 3646 |
for (i = 0; i < n; i++) { |
| 3647 |
entry = captured->vector.self.t[i]; |
| 3648 |
captured->vector.self.t[i] = ECL_NIL; |
| 3649 |
macro_entry = fix_macro_to_lexenv(env, entry); |
| 3650 |
if(!Null(macro_entry)) { |
| 3651 |
flex->vector.self.t[i] = macro_entry; |
| 3652 |
continue; |
| 3653 |
} |
| 3654 |
ref = c_any_ref(env, entry); |
| 3655 |
switch(ref.place) { |
| 3656 |
case ECL_CMPREF_LOCAL: |
| 3657 |
flex->vector.self.t[i] = ecl_make_fixnum(-ref.index-1); |
| 3658 |
break; |
| 3659 |
case ECL_CMPREF_CLOSE: |
| 3660 |
flex->vector.self.t[i] = ecl_make_fixnum(ref.index); |
| 3661 |
break; |
| 3662 |
default: |
| 3663 |
ecl_miscompilation_error(); |
| 3664 |
} |
| 3665 |
} |
| 3666 |
cfb = ECL_T; |
| 3667 |
} |
| 3668 |
ecl_return2(env, output, cfb); |
| 3669 |
} |
| 3670 |
|
| 3671 |
static cl_object |
| 3672 |
ecl_function_block_name(cl_object name) |
| 3673 |
{ |
| 3674 |
if (ECL_SYMBOLP(name)) { |
| 3675 |
return name; |
| 3676 |
} else if (CONSP(name) && ECL_CONS_CAR(name) == @'setf') { |
| 3677 |
name = ECL_CONS_CDR(name); |
| 3678 |
if (CONSP(name)) { |
| 3679 |
cl_object output = ECL_CONS_CAR(name); |
| 3680 |
if (ECL_SYMBOLP(output) && Null(ECL_CONS_CDR(name))) |
| 3681 |
return output; |
| 3682 |
} |
| 3683 |
} |
| 3684 |
return NULL; |
| 3685 |
} |
| 3686 |
|
| 3687 |
cl_object |
| 3688 |
si_function_block_name(cl_object name) |
| 3689 |
{ |
| 3690 |
cl_object output = ecl_function_block_name(name); |
| 3691 |
if (!output) |
| 3692 |
FEinvalid_function_name(name); |
| 3693 |
@(return output); |
| 3694 |
} |
| 3695 |
|
| 3696 |
cl_object |
| 3697 |
si_valid_function_name_p(cl_object name) |
| 3698 |
{ |
| 3699 |
name = ecl_function_block_name(name); |
| 3700 |
@(return (name? ECL_T : ECL_NIL)); |
| 3701 |
} |
| 3702 |
|
| 3703 |
cl_object |
| 3704 |
si_make_lambda(cl_object name, cl_object rest) |
| 3705 |
{ |
| 3706 |
cl_object lambda; |
| 3707 |
const cl_env_ptr the_env = ecl_process_env(); |
| 3708 |
cl_compiler_env_ptr old_c_env = the_env->c_env; |
| 3709 |
struct cl_compiler_env new_c_env; |
| 3710 |
|
| 3711 |
c_new_env(the_env, &new_c_env, NULL); |
| 3712 |
ECL_UNWIND_PROTECT_BEGIN(the_env) { |
| 3713 |
lambda = ecl_make_lambda(the_env, name, rest); |
| 3714 |
} ECL_UNWIND_PROTECT_EXIT { |
| 3715 |
c_restore_env(the_env, &new_c_env, old_c_env); |
| 3716 |
} ECL_UNWIND_PROTECT_END; |
| 3717 |
@(return lambda); |
| 3718 |
} |
| 3719 |
|
| 3720 |
cl_object |
| 3721 |
si_bc_compile_from_stream(cl_object input) |
| 3722 |
{ |
| 3723 |
/* Compile all forms read from input stream to bytecodes */ |
| 3724 |
cl_env_ptr the_env = ecl_process_env(); |
| 3725 |
cl_compiler_env_ptr old_c_env; |
| 3726 |
struct cl_compiler_env new_c_env; |
| 3727 |
cl_object bytecodes = ECL_NIL; |
| 3728 |
old_c_env = the_env->c_env; |
| 3729 |
c_new_env(the_env, &new_c_env, NULL); |
| 3730 |
new_c_env.mode = FLAG_LOAD; |
| 3731 |
|
| 3732 |
ECL_UNWIND_PROTECT_BEGIN(the_env) { |
| 3733 |
while (TRUE) { |
| 3734 |
cl_object position, form, source_location; |
| 3735 |
cl_index handle; |
| 3736 |
position = ecl_file_position(input); |
| 3737 |
form = cl_read(3, input, ECL_NIL, @':eof'); |
| 3738 |
if (form == @':eof') |
| 3739 |
break; |
| 3740 |
source_location = ECL_SYM_VAL(the_env, @'ext::*source-location*'); |
| 3741 |
if (source_location != ECL_NIL) |
| 3742 |
cl_rplacd(source_location, position); |
| 3743 |
|
| 3744 |
handle = asm_begin(the_env); |
| 3745 |
compile_with_load_time_forms(the_env, form, FLAG_VALUES); |
| 3746 |
asm_op(the_env, OP_EXIT); |
| 3747 |
push(asm_end(the_env, handle, form), &bytecodes); |
| 3748 |
} |
| 3749 |
} ECL_UNWIND_PROTECT_EXIT { |
| 3750 |
c_restore_env(the_env, &new_c_env, old_c_env); |
| 3751 |
} ECL_UNWIND_PROTECT_END; |
| 3752 |
|
| 3753 |
return cl_nreverse(bytecodes); |
| 3754 |
} |
| 3755 |
|
| 3756 |
@(defun si::eval-with-env (form &optional (env ECL_NIL) (stepping ECL_NIL) |
| 3757 |
(compiler_env_p ECL_NIL) (mode @':execute')) |
| 3758 |
cl_compiler_env_ptr old_c_env; |
| 3759 |
struct cl_compiler_env new_c_env; |
| 3760 |
@ |
| 3761 |
/* |
| 3762 |
* Compile to bytecodes. |
| 3763 |
* Parameter mode is interpreted as follows: |
| 3764 |
* - execute: Execute the compiled form |
| 3765 |
* - load-toplevel: Compile the form without executing. Calls |
| 3766 |
* make-load-form for literal objects encountered during |
| 3767 |
* compilation. |
| 3768 |
* - compile-toplevel: Compile the form without executing, do not |
| 3769 |
* call make-load-form. Useful for code walking. |
| 3770 |
*/ |
| 3771 |
if (!(mode == @':execute' || mode == @':load-toplevel' || mode == @':compile-toplevel')) { |
| 3772 |
FEerror("Invalid mode in SI:EVAL-WITH-ENV", 0); |
| 3773 |
} |
| 3774 |
old_c_env = the_env->c_env; |
| 3775 |
c_new_env(the_env, &new_c_env, NULL); |
| 3776 |
(Null(compiler_env_p) |
| 3777 |
? import_lexenv(the_env, env) |
| 3778 |
: import_cmpenv(the_env, env)); |
| 3779 |
new_c_env.stepping = stepping != ECL_NIL; |
| 3780 |
the_env->stepper = @'si::stepper-hook'; |
| 3781 |
ECL_UNWIND_PROTECT_BEGIN(the_env) { |
| 3782 |
if (mode == @':execute') { |
| 3783 |
eval_form(the_env, form); |
| 3784 |
} else { |
| 3785 |
cl_index handle = asm_begin(the_env); |
| 3786 |
new_c_env.mode = (mode == @':load-toplevel') ? FLAG_LOAD : FLAG_COMPILE; |
| 3787 |
compile_with_load_time_forms(the_env, form, FLAG_VALUES); |
| 3788 |
asm_op(the_env, OP_EXIT); |
| 3789 |
the_env->values[0] = asm_end(the_env, handle, form); |
| 3790 |
the_env->nvalues = 1; |
| 3791 |
} |
| 3792 |
} ECL_UNWIND_PROTECT_EXIT { |
| 3793 |
c_restore_env(the_env, &new_c_env, old_c_env); |
| 3794 |
} ECL_UNWIND_PROTECT_END; |
| 3795 |
return the_env->values[0]; |
| 3796 |
@) |
| 3797 |
|
| 3798 |
void |
| 3799 |
init_compiler() |
| 3800 |
{ |
| 3801 |
cl_object dispatch_table = |
| 3802 |
cl_core.compiler_dispatch = |
| 3803 |
cl__make_hash_table(@'eq', ecl_make_fixnum(128), /* size */ |
| 3804 |
cl_core.rehash_size, |
| 3805 |
cl_core.rehash_threshold); |
| 3806 |
int i; |
| 3807 |
for (i = 0; database[i].symbol; i++) { |
| 3808 |
ecl_sethash(database[i].symbol, dispatch_table, ecl_make_fixnum(i)); |
| 3809 |
} |
| 3810 |
} |