Debian Codegraph @

src/compaux.c

raw
1 /*
2 * compaux.c - C API bridge for the compiler
3 *
4 * Copyright (c) 2000-2024 Shiro Kawai <[email protected]>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /* This file serves as a bridge to the compiler, which is implemented
35 in Scheme (see compile.scm) */
36
37 #include <stdlib.h>
38 #define LIBGAUCHE_BODY
39 #include "gauche.h"
40 #include "gauche/priv/configP.h"
41 #include "gauche/vm.h"
42 #include "gauche/vminsn.h"
43 #include "gauche/code.h"
44 #include "gauche/priv/glocP.h"
45 #include "gauche/priv/identifierP.h"
46 #include "gauche/priv/builtin-syms.h"
47 #include "gauche/priv/readerP.h" /* for Scm_MakeReadReference */
48
49 /*
50 * Syntax
51 */
52
53
54 /*
55 * Compiler Entry
56 */
57
58 static ScmGloc *compile_gloc = NULL;
59 static ScmGloc *compile_partial_gloc = NULL;
60 static ScmGloc *compile_finish_gloc = NULL;
61 static ScmGloc *init_compiler_gloc = NULL;
62
63 static ScmInternalMutex compile_finish_mutex;
64
65 ScmObj Scm_Compile(ScmObj program, ScmObj env)
66 {
67 return Scm_ApplyRec2(SCM_GLOC_GET(compile_gloc), program, env);
68 }
69
70 ScmObj Scm_CompilePartial(ScmObj program, ScmObj env)
71 {
72 return Scm_ApplyRec2(SCM_GLOC_GET(compile_partial_gloc), program, env);
73 }
74
75 void Scm_CompileFinish(ScmCompiledCode *cc)
76 {
77 if (cc->code == NULL) {
78 SCM_INTERNAL_MUTEX_LOCK(compile_finish_mutex);
79 SCM_UNWIND_PROTECT {
80 if (cc->code == NULL) {
81 Scm_ApplyRec1(SCM_GLOC_GET(compile_finish_gloc), SCM_OBJ(cc));
82 }
83 }
84 SCM_WHEN_ERROR {
85 SCM_INTERNAL_MUTEX_UNLOCK(compile_finish_mutex);
86 SCM_NEXT_HANDLER;
87 }
88 SCM_END_PROTECT;
89 SCM_INTERNAL_MUTEX_UNLOCK(compile_finish_mutex);
90 }
91 }
92
93 /*-------------------------------------------------------------
94 * Identifier object
95 */
96
97 /*
98 * About identifier's ENV slot.
99 * We close identifier's binding environment (list of frames), so that later
100 * we can look up its bindings hygienically. We truncate the frames up to
101 * where the binding occur, for the efficient lookup and comparison.
102 * Notably, the identifiers that are unbound or refer to toplevel variable
103 * has () in its env.
104 * A caveat--we can't truncate frames at the time of construction, since
105 * the entire frame structure may not be fixed while we're processing internal
106 * defines. The identifier may refer to another identifier that will be
107 * inserted later. Thus, we delay the truncation operation until it is
108 * needed.
109 * The ENV slot itself now contains (<flag> . <frames>), where <flag> is #f
110 * if truncation hasn't be done, #t otherwise. ENV should be treated as
111 * an opaque data for the others; you should always get it with
112 * Scm_IdentifierEnv(), and never directly access ENV slot itself.
113 */
114
115 static void identifier_print(ScmObj obj, ScmPort *port,
116 ScmWriteContext *ctx SCM_UNUSED)
117 {
118 ScmIdentifier *id = SCM_IDENTIFIER(obj);
119 /* We may want to have an external identifier syntax, so that an
120 identifier can be written out and then read back. It will be
121 convenient if we can embed a reference to other module's global
122 binding directly in the program. However, it can also breaches
123 module-based sandbox implementation, so further consideration is
124 required.
125 */
126 Scm_Printf(port, "#<identifier %S#%S.%x>",
127 id->module->name, id->name, SCM_WORD(id));
128 }
129
130 SCM_DEFINE_BUILTIN_CLASS_SIMPLE(Scm_IdentifierClass, identifier_print);
131
132 /* Truncate local frames */
133 static ScmObj get_binding_frame(ScmObj var, ScmObj env)
134 {
135 ScmObj frame, fp;
136 SCM_FOR_EACH(frame, env) {
137 if (!SCM_PAIRP(SCM_CAR(frame))) continue;
138 SCM_FOR_EACH(fp, SCM_CDAR(frame)) {
139 if (SCM_CAAR(fp) == var) {
140 SCM_ASSERT(SCM_LISTP(frame));
141 return frame;
142 }
143 }
144 }
145 return SCM_NIL;
146 }
147
148 ScmObj Scm_MakeIdentifier(ScmObj name, ScmModule *mod, ScmObj env)
149 {
150 ScmIdentifier *id = SCM_NEW(ScmIdentifier);
151 SCM_SET_CLASS(id, SCM_CLASS_IDENTIFIER);
152 id->name = name;
153 id->module = mod? mod : SCM_CURRENT_MODULE();
154 id->frames = Scm_Cons(SCM_FALSE, env); /* see the above comment */
155 return SCM_OBJ(id);
156 }
157
158 ScmObj Scm_IdentifierEnv(ScmIdentifier *id)
159 {
160 SCM_ASSERT(SCM_PAIRP(id->frames));
161 if (SCM_FALSEP(SCM_CAR(id->frames))) {
162 /* MT safety: This operation is idempotent, so it's ok if more than
163 one thread execute here. */
164 ScmObj f = get_binding_frame(id->name, SCM_CDR(id->frames));
165 SCM_SET_CDR_UNCHECKED(id->frames, f);
166 SCM_SET_CAR_UNCHECKED(id->frames, SCM_TRUE);
167 }
168 return SCM_CDR(id->frames);
169 }
170
171 ScmIdentifier *Scm_OutermostIdentifier(ScmIdentifier *id)
172 {
173 while (SCM_IDENTIFIERP(id->name)) {
174 id = SCM_IDENTIFIER(SCM_IDENTIFIER(id)->name);
175 }
176 return id;
177 }
178
179 ScmSymbol *Scm_UnwrapIdentifier(ScmIdentifier *id)
180 {
181 ScmObj z = Scm_OutermostIdentifier(id)->name;
182 SCM_ASSERT(SCM_SYMBOLP(z));
183 return SCM_SYMBOL(z);
184 }
185
186 /* Returns global binding of the identifier */
187 ScmGloc *Scm_IdentifierGlobalBinding(ScmIdentifier *id)
188 {
189 ScmIdentifier *z = Scm_OutermostIdentifier(id);
190 return Scm_FindBinding(z->module, SCM_SYMBOL(z->name), 0);
191 }
192
193 /* Runtime reference/mutation of global variables. The are called
194 from VM instructoins GREF and GSET, and will also be used from
195 the AOT compiled code.
196
197 We cache the result of global variable to GLOC lookup in VM instructions,
198 so these procedures can also store the looked up GLOC in *pgloc. You can
199 pass NULL if you don't need it.
200
201 If the referenced variable is unbound, or is immutable when attempted to
202 set!, an error is thrown.
203 */
204 ScmObj Scm_IdentifierGlobalRef(ScmIdentifier *id,
205 ScmGloc **pgloc /* out */)
206 {
207 ScmGloc *gloc = Scm_IdentifierGlobalBinding(id);
208 if (gloc == NULL) {
209 Scm_VMThrowException(Scm_VM(),
210 Scm_MakeUnboundVariableError(id->name),
211 0);
212 }
213 if (pgloc != NULL) *pgloc = gloc;
214
215 ScmObj v = Scm_GlocGetValue(gloc);
216 if (SCM_AUTOLOADP(v)) {
217 return Scm_ResolveAutoload(SCM_AUTOLOAD(v), 0);
218 } else {
219 return v;
220 }
221 }
222
223 void Scm_IdentifierGlobalSet(ScmIdentifier *id,
224 ScmObj val,
225 ScmGloc **pgloc /* out */)
226 {
227 /* If runtime flag LIMIT_MODULE_MUTATION is set,
228 we search only for the id's module, so that set! won't
229 mutate bindings in the other module. */
230 ScmIdentifier *z = Scm_OutermostIdentifier(id);
231 int limit = SCM_VM_RUNTIME_FLAG_IS_SET(Scm_VM(), SCM_LIMIT_MODULE_MUTATION);
232 ScmGloc *gloc = Scm_FindBinding(z->module, SCM_SYMBOL(z->name),
233 (limit ? SCM_BINDING_STAY_IN_MODULE : 0));
234 if (gloc == NULL) {
235 /* Search again for meaningful error message */
236 if (limit) {
237 gloc = Scm_FindBinding(z->module, SCM_SYMBOL(z->name), 0);
238 if (gloc != NULL) {
239 Scm_Error("Can't mutate binding of %S, which is in another module",
240 z->name);
241 }
242 /* FALLTHROUGH */
243 }
244 Scm_Error("Symbol not defined: %S", z->name);
245 }
246 SCM_GLOC_SET(gloc, val);
247 if (pgloc != NULL) *pgloc = gloc;
248 }
249
250 /* returns true if SYM has the same binding with ID in ENV. */
251 int Scm_IdentifierBindingEqv(ScmIdentifier *id, ScmSymbol *sym, ScmObj env)
252 {
253 ScmObj env1 = Scm_IdentifierEnv(id);
254 ScmObj env2 = get_binding_frame(SCM_OBJ(sym), env);
255 return (env1 == env2);
256 }
257
258 ScmObj Scm_WrapIdentifier(ScmIdentifier *orig)
259 {
260 ScmIdentifier *id = SCM_NEW(ScmIdentifier);
261 SCM_SET_CLASS(id, SCM_CLASS_IDENTIFIER);
262 id->name = SCM_OBJ(orig);
263 id->module = orig->module;
264 id->frames = orig->frames;
265 return SCM_OBJ(id);
266 }
267
268 static ScmObj identifier_name_get(ScmObj obj)
269 {
270 return SCM_OBJ(SCM_IDENTIFIER(obj)->name);
271 }
272
273 static void identifier_name_set(ScmObj obj, ScmObj val)
274 {
275 if (!SCM_SYMBOLP(val) && !SCM_IDENTIFIERP(val)) {
276 Scm_Error("symbol or identifier required, but got %S", val);
277 }
278 SCM_IDENTIFIER(obj)->name = val;
279 }
280
281 static ScmObj identifier_module_get(ScmObj obj)
282 {
283 return SCM_OBJ(SCM_IDENTIFIER(obj)->module);
284 }
285
286 static ScmObj identifier_env_get(ScmObj obj)
287 {
288 return Scm_IdentifierEnv(SCM_IDENTIFIER(obj));
289 }
290
291 /* Identifier name can be mutated during macro expansion to avoid
292 conflicts on macro-inserted toplevel identifiers. See
293 %rename-toplevel-identifier! in compiler pass1.
294 Other than that, identifiers must be treated as immutable objects. */
295 static ScmClassStaticSlotSpec identifier_slots[] = {
296 SCM_CLASS_SLOT_SPEC("name", identifier_name_get, identifier_name_set),
297 SCM_CLASS_SLOT_SPEC("module", identifier_module_get, NULL),
298 SCM_CLASS_SLOT_SPEC("env", identifier_env_get, NULL),
299 SCM_CLASS_SLOT_SPEC_END()
300 };
301
302 /*------------------------------------------------------------------
303 * Unwrapping syntax
304 *
305 * Traverses a form and replaces identifiers for bare symbols.
306 * It's complicated because the form may have cycles.
307 * We use Scm_ReadReference as the temporary placeholder to handle
308 * circular structure. All the read references are replaced before
309 * Scm_UnwrapSyntax returns.
310 */
311
312 typedef struct unwrap_ctx_rec {
313 ScmHashCore history; /* object -> #f | R() | R(v)
314 where
315 SCM_UNBOUND - the object has been visited
316 only once
317 value - the object is realized
318 R() - empty ReadReference. The object
319 has been visited more than once,
320 but its final replacement hasn't
321 been materialized yet.
322 R(v) - ReadReference with value.
323 The object should correspond to
324 the value.
325 */
326 ScmHashCore refs; /* location -> read reference*/
327 int immutable; /* flag */
328 } unwrap_ctx;
329
330 static void register_location(unwrap_ctx *ctx,
331 ScmObj *loc,
332 ScmObj ref)
333 {
334 if (!SCM_READ_REFERENCE_P(ref)) return;
335 if (SCM_READ_REFERENCE_REALIZED(ref)) {
336 *loc = SCM_READ_REFERENCE(ref)->value;
337 } else {
338 ScmDictEntry *e =
339 Scm_HashCoreSearch(&ctx->refs, (intptr_t)loc, SCM_DICT_CREATE);
340 e->value = (intptr_t)ref;
341 }
342 }
343
344 static void fill_history(ScmDictEntry *e, ScmObj value)
345 {
346 if (e->value) {
347 if (SCM_READ_REFERENCE_P(e->value)) {
348 SCM_READ_REFERENCE(e->value)->value = value;
349 }
350 } else {
351 e->value = (intptr_t)value;
352 }
353 }
354
355 static void patch_locations(unwrap_ctx *ctx)
356 {
357 ScmHashIter iter;
358 Scm_HashIterInit(&iter, &ctx->refs);
359 for (;;) {
360 ScmDictEntry *e = Scm_HashIterNext(&iter);
361 if (e == NULL) break;
362 ScmObj *loc = (ScmObj*)SCM_DICT_KEY(e);
363 ScmReadReference *ref = SCM_READ_REFERENCE(SCM_DICT_VALUE(e));
364 if (SCM_READ_REFERENCE_P(ref)) {
365 SCM_ASSERT(SCM_READ_REFERENCE_REALIZED(ref));
366 *loc = SCM_READ_REFERENCE(ref)->value;
367 }
368 }
369 }
370
371 /* Returns either original form, or converted form, or a read reference. */
372 static ScmObj unwrap_rec(ScmObj form, unwrap_ctx *ctx)
373 {
374 if (!SCM_PTRP(form)) return form;
375 ScmDictEntry *e = Scm_HashCoreSearch(&ctx->history,
376 (intptr_t)form,
377 SCM_DICT_GET);
378 if (e) {
379 /* We've visited FORM before. If this is the second time,
380 we allocate ReadReference to hold the value. It will be
381 filled later by the caller. */
382 if (!e->value) {
383 e->value = (intptr_t)Scm_MakeReadReference();
384 }
385 return SCM_DICT_VALUE(e);
386 }
387
388 if (SCM_PAIRP(form)) {
389 e = Scm_HashCoreSearch(&ctx->history, (intptr_t)form, SCM_DICT_CREATE);
390 ScmObj ca = unwrap_rec(SCM_CAR(form), ctx);
391 ScmObj cd = unwrap_rec(SCM_CDR(form), ctx);
392 if (ca == SCM_CAR(form) && cd == SCM_CDR(form)
393 && (!ctx->immutable || Scm_ImmutablePairP(form))) {
394 fill_history(e, form);
395 /* no need to create a new cons */
396 return form;
397 }
398 /* We have to create a new cons. Make sure to keep attributes. */
399 ScmObj p = SCM_NIL;
400 if (SCM_EXTENDED_PAIR_P(form)) {
401 if (ctx->immutable || Scm_ImmutablePairP(form)) {
402 p = Scm_MakeImmutablePair(ca, cd, Scm_PairAttr(SCM_PAIR(form)));
403 } else {
404 p = Scm_MakeExtendedPair(ca, cd, Scm_PairAttr(SCM_PAIR(form)));
405 }
406 } else {
407 if (ctx->immutable) {
408 p = Scm_MakeImmutablePair(ca, cd, SCM_NIL);
409 } else {
410 p = Scm_Cons(ca, cd);
411 }
412 }
413 fill_history(e, p);
414 register_location(ctx, &SCM_PAIR(p)->car, ca);
415 register_location(ctx, &SCM_PAIR(p)->cdr, cd);
416 return p;
417 }
418 if (SCM_IDENTIFIERP(form)) {
419 return SCM_OBJ(Scm_UnwrapIdentifier(SCM_IDENTIFIER(form)));
420 }
421 if (SCM_VECTORP(form)) {
422 int len = SCM_VECTOR_SIZE(form);
423 ScmObj *pelt = SCM_VECTOR_ELEMENTS(form);
424 e = Scm_HashCoreSearch(&ctx->history, (intptr_t)form, SCM_DICT_CREATE);
425 for (int i=0; i<len; i++, pelt++) {
426 ScmObj elt = unwrap_rec(*pelt, ctx);
427 if (elt != *pelt
428 || (ctx->immutable && !SCM_VECTOR_IMMUTABLE_P(form))) {
429 ScmObj newvec = Scm_MakeVector(len, SCM_FALSE);
430 pelt = SCM_VECTOR_ELEMENTS(form);
431 int j;
432 for (j=0; j<i; j++, pelt++) {
433 SCM_VECTOR_ELEMENT(newvec, j) = *pelt;
434 }
435 register_location(ctx, &SCM_VECTOR_ELEMENT(newvec, i), elt);
436 SCM_VECTOR_ELEMENT(newvec, i) = elt;
437 for (j=i+1, pelt++; j<len; j++, pelt++) {
438 elt = unwrap_rec(*pelt, ctx);
439 register_location(ctx, &SCM_VECTOR_ELEMENT(newvec, i), elt);
440 SCM_VECTOR_ELEMENT(newvec, j) = elt;
441 }
442 if (ctx->immutable) {
443 SCM_VECTOR_IMMUTABLE_SET(newvec, TRUE);
444 }
445 fill_history(e, newvec);
446 return newvec;
447 }
448 }
449 fill_history(e, form);
450 return form;
451 }
452 return form;
453 }
454
455 #if GAUCHE_API_VERSION < 98
456 ScmObj Scm_UnwrapSyntax(ScmObj form)
457 {
458 return Scm_UnwrapSyntax2(form, FALSE);
459 }
460 ScmObj Scm_UnwrapSyntax2(ScmObj form, int immutablep)
461 #else /* GAUCHE_API_VERSION >= 98 */
462 ScmObj Scm_UnwrapSyntax(ScmObj form, int immutablep)
463 #endif /* GAUCHE_API_VERSION >= 98 */
464 {
465 unwrap_ctx ctx;
466 Scm_HashCoreInitSimple(&ctx.history, SCM_HASH_EQ, 0, NULL);
467 Scm_HashCoreInitSimple(&ctx.refs, SCM_HASH_EQ, 0, NULL);
468 ctx.immutable = immutablep;
469 ScmObj r = unwrap_rec(form, &ctx);
470 patch_locations(&ctx);
471 return r;
472 }
473
474 /*===================================================================
475 * Initializer
476 */
477
478 #define INIT_GLOC(gloc, name, mod) \
479 do { \
480 gloc = Scm_FindBinding(mod, SCM_SYMBOL(SCM_INTERN(name)), \
481 SCM_BINDING_STAY_IN_MODULE); \
482 if (gloc == NULL) { \
483 Scm_Panic("no " name " procedure in gauche.internal"); \
484 } \
485 } while (0)
486
487 void Scm__InitCompaux(void)
488 {
489 ScmModule *g = Scm_GaucheModule();
490 ScmModule *gi = Scm_GaucheInternalModule();
491
492 Scm_InitStaticClass(SCM_CLASS_IDENTIFIER, "<identifier>", g,
493 identifier_slots, 0);
494
495 SCM_INTERNAL_MUTEX_INIT(compile_finish_mutex);
496
497 /* Grab the entry points of compile.scm */
498 INIT_GLOC(init_compiler_gloc, "init-compiler", gi);
499 INIT_GLOC(compile_gloc, "compile", gi);
500 INIT_GLOC(compile_partial_gloc, "compile-partial", gi);
501 INIT_GLOC(compile_finish_gloc, "compile-finish", gi);
502
503 Scm_ApplyRec0(SCM_GLOC_GET(init_compiler_gloc));
504 }